Shaders Using shader get uniform from other another shader works?

Dumb question but out of laziness, I used reused the same uniform float constant for two shaders.

I initialized the shader uniform float as so:
GML:
u_time = shader_get_uniform(shd_tree,"time");
and used it for another shader like so:
GML:
shader_set(shd_terrain); //Not shd_tree
shader_set_uniform_f(u_time,current_time/300);
u_time in both shd_terrain and shd_tree work the same.

This works but I'm worried this is a bad way of coding. The thing is, these two won't be the only shaders that will use u_time. Is it better to declare a new variable for every shader or do this "dumb" but working technique?
 

Ido-f

Member
Well doing a short test, shader_get_uniform for 2 uniformed in the same shader returned 13 and 14 (only when actually used in the shader's function, otherwise it leaves them out at compilation and returns -1).
My guess is that all uniform handles start with 13 and increment for each uniform defined.
So yea, leaving the same uniform handle for a shader is probably a bad idea. If the shader will be changed to no longer match the uniforms declaration order it will stop working. Seeing that shaders in GM don't throw error messages, it could cause a pretty sneaky bug.
 

kburkhart84

Firehammer Games
I'm thinking that the shader uniforms themselves are going to have to be separate. However, if you add a little helper code, you could have an object that updates a time variable, and then simply access that variable for each shader when you set the uniform value. It doesn't save setting the uniforms, but it does let you have that single variable get used instead of having each object handle its own. If I ever make a node based shader creation system(another thing on my list of assets to make for GM), I would handle it that way.
 
Top