Shader variables (uniform)

Started by
3 comments, last by nattfoedd 9 years, 7 months ago

Hi everone!

I have this code here:


uniform int something;
varying vec2 texcoord;

void main()
{

	vec4 tex = texture2D ( img, texcoord );
        if(something)
        {
	    if( tex.a == 0.0 ) discard;
        }
	gl_FragColor = tex;
}

I was wondering, is
if(something) called every time it renders pixels?

To think logically, the variable something doens't change so it doesn't have to check it everytime. It just have to check it when im activating this shader right?
Thanks!

Advertisement

It will run the check every time the shader is run (which is every fragment/pixel).

The shader won't necessarily have the uniform value at the time you activate the shader; it would likely get sent to the GPU when a draw call is issued.

If your drivers are really nice, they might recompile your shader code every time you change the value of the uniform, making it free...

But you should assume that the driver isn't going to be that magically nice, and that you're going to pay the cost of that if for every pixel that you draw.

Often, games design these kinds of shaders using #if / #endif instead, and then compile the shader code multiple times.

Thanks guys, that was what i wanted to know.

Another way would be to use subroutines for this behaviour:

http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/glsl-core-tutorial-subroutines/

This topic is closed to new replies.

Advertisement