Sign in to follow this  
Gyiove Sparkle

Shader variables (uniform)

Recommended Posts

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!

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this