I have a basic Problem using shader.
I’m new to shader and I want to understand the basics.
I got plenty tutorials to read but no one seems to work with monogame. (won’t compile or blank screen)
So my question is what do I wrong?
But there is no output. I have a blank screen.
This seems like a noob-question, I just want to understand how this works and why I can’t get a shader to work.
return float4(1, 0, 0, 1); works for the test (red square where the image is)
Thank you. This is working =)
And I understand now how I can manipulate the pixels on runtime with parameters and SetValue.
This is cool.
I have another question:
If I want a second image as alpha mask how can i load this in the shader just for the alpha information?
Tried with a second parameter sampler alphamask;
But if I want to fill it with _AlphaShader.Parameters["alphamask"].SetValue(_AlphaMap);
I get a NullReferenceException.
if (!any(color)) return color; //not sure if this even works
You will want to avoid predictive instruction in shader as much as possible, it is kinda giving hell to the whole point of parallelization which makes GPU effective. I am not saying that modern GPU can´t deal with one or two ifs in shader, but nested condition…
Anyway you are not sampling your alpha texture anywhere, if you want to use alphaTex as alpha of texture in sampler 0 then:
It is giving you null reference because you never use that alphasampler, you never sample single pixel from it, thus compiler removes it as part of optimization. As I said:
No problem, glad it works. Also yeah, that compiler is aggresive as hell. Even worse there is strange fallthrough. For instance let´s say you have 4 samplers to be used in shader. You have to use them (float4 normal = tex2D(…) ) in same order as they are declared, otherwise if you will for instance use 4th sampler before 3rd then texture assigned to 4th will suddenly be assigned to 3rd sampler.
This is issue when you are using for example normal map to distort uv coords of texture in sampler0 because in that case you need to sample second sampler first in order to get texCoords for first sampler, but you kinda can´t. So you have to shuffle with textures in code while draw call is being created. Something to keep in mind.
In your case order doesn´t matter, so it means nothing. This “white stuff” is alpha blending of nonpremultiplied alpha channel, either use .nonpremultiplied blendstate or multiply rgb with alpha channel, thus textcolor.rgb *= textcolor.a before output.
I seriously recommend you to carefully read those tutorials and materials. I work as technical artist for two years (while having master degree in computer science - computer graphic focus) and still I am considering myself a beginner, you are not going to get any deeper explanation in this thread as it is extremely complex subject. Also after obtaining basics I recommend you to find GPU gems 1 - 3 which were made accessible for free by Nvidia on web.
Well it doesn´t matter much (the fact that they are outdated), most things are the same and now we have covered main differences in behavior between XNA and Monogame. Lot of techniques from 3d are applicable in 2d.
I worked years ago in a small company (while studying computer science - didn’t finished it though) and had to work with C# and XNA there. Many tutorials I know back then don’t work with monogame without recoding.
I was sad as I heard XNA will not be developed further.
Never had to use shader ^^
But now I will seriously coding a game and need to know as much as possible for shading 2D grafics.
I will read the gems for sure! Just had to understand the basics.