Learning basics of shader programming.
I can make a shader with my own parameters… But the only way I can assign values to these parameters at run-time, is if they are used in the shader code to return a value… I can’t just use the values in my shader code, I have to RETURN them, or at least provide a path that MAY return them…
If I try to assign value to a parameter that is NOT used to return a value, i get a crash from “‘Object reference not set to an instance of an object.’”
… In other words, I can use my GAME code to assign values to a shader parameter, like so:
But ONLY if my SHADER code includes THIS:
Add this: I can just put in bogus if this, return that statements, that are never triggered… So I have a work around. But it doesn’t make sense to me.
I’ve not run into this issue before. Have you globally declared these variables you’re trying to set? i.e.
float4 filterColor; at the top of your .fx? Or is the only mention of it in the return statement?
Yes, they are declared globally within the shader.
But if I assign to them in my game, and don’t provide a path for the value to “return” somewhere, it crashes… Darndest thing.
Odd. I see this behavior if my variable is unused within the pass functions, presumably because if it is unused it is removed when the shader is compiled, but they’ve never required to specifically be involved with a “return”.
Thank you, and nice video. However, I copy pasted your whole shader code, but it didn’t run. Tried with and without the little update logic in my game, but it just won’t load. Crash on launch.
I’m stuck on another issue right now, maybe I’ll get back to this. Don’t have time to fiddle with it now.
Can you try running the whole project? There is a repo link in the description.
Just Post your shader since the code you use to set it is already shown thats the easiest way to see whats wrong with it.
Yea, so following your code example 4, your shader code includes a spritedrawing technique, but this is never called or used anywhere in your example game code.
k, so your shader wont build, using the content tool… I get a “bad token” warning…
I can add the default shader just fine… but when I copy paste your code, It wont even load.
Can you just download the whole project and run it?
I’m a littly busy already, already reaching out for help myself. But there is another comment on your video also suggesting problems.
I have plugged your example shader into my own game code, because the only thing on my end was passing “amount” to the shader, and then using the shader to draw a sprite…
Your shader code crashes before any code runs, and when I copy paste EMPTY or default code in it place, it works fine, drawing nothing to the screen, but not crashing.
Hi, I have also followed this other guide, and it works fine:
Maybe you should check it yourself? I feel like you left some stuff out, there are things I’ve seen in other guides that you are not including, but I’m still such a shader noob, I could just be doing something wrong, honestly.
Still, since I am able to load and run other shaders just fine, and even alter them to do stuff similar to what you intended with yours, so you know… Just fyi.
ok, so, I think I found out I don’t have to RETURN the variables I declare…
But I do have to make use of them somehow within the shader code.
If I declare a variable in the shader, that isn’t referenced anywhere within the shader code, and then try to assign to it from my game, It crashes… ‘Object reference not set to an instance of an object.’
I can leave the variable declared in the shader code, and not reference it, and it runs fine…
But If I try to assign to an unreferenced variable it crashes.
Is this normal?
The shader compiler is extremely aggressive in removing unused variables from your shader(and thus making them unavailable to be set from the game side). This includes individual elements of array variables as well, btw.