Program constant optimization

I’m in the process of making a graphical shader creation system that outputs GLSL at the back end.

I’m debating about whether to put user defined constants inside the GLSL program itself or to use uniform variables to send those constants in from the main program.

My question is this: Can current drivers take advantage of hard-coded constants (normally input as default values for temporary variables) by optimizing out code that ends up not mattering, like adding 0 or multiplying by 1? For example, if an artist supplied the color (0,0,0) to the specular material input, would modern GLSL compilers be smart enough to optimize out the entire specular calculation since it gets multiplied by 0 at the end?

Are there any other considerations I should be aware of when making this decision?

Thanks in advance :slight_smile:

Shader switching is an expensive operation - it is usualy wise to stick to changing uniforms as much as you can.

Ofcourse, if there are many objects with specular color (0,0,0) just generate a shader without the specular part.

A long time ago I had heard that changing a uniform on nVidia caused a recompile. It wasn’t about GLSL. It was probably about ARB_vp/fp and NV_vp/fp and about GeforceFX

With GLSL, I think if you do

if(my_boolean)
{
lots of code;
}

the driver may actually create a second shader if it thinks it’s saves time. Someone from ATI (Humus) said this.
Both companies do their best to optimize so don’t be surprised if they do tricks like this.

Originally posted by speedy:
[b] Shader switching is an expensive operation - it is usualy wise to stick to changing uniforms as much as you can.

Ofcourse, if there are many objects with specular color (0,0,0) just generate a shader without the specular part. [/b]
I don’t know how often objects will share the exact same shader with different input parameters, but that may very well be a common thing, so that would make it more efficient to send the constants in from the program. Good point.

Right, your latter part makes sense, but it’s not that easy. My GUI editor is going to be linking various different bits of shader code together graphically - I can certainly check if the user inputs zero, but at the moment my program doesn’t have to pay attention to what the underlying code does, and I’d like to keep it that way. My goal is not to build a GLSL compiler, just a GLSL linker.

I guess if no one from NVIDIA or ATI responds, I can just run some benchmarks on a fragment program with a long shader whose result is multiplied by zero to see if it gets optimized out.

I read about about that in nvidia optimization docs and they say there is some overhead when using constant uniforms - and developers should try to use constant values in the code directly.

if you switch the uniform variables of a single shader the shader will quite likely be recompiled every value change - so its maybe whise to use different shaders for different use-cases.

lg Clemens

if you switch the uniform variables of a single shader the shader will quite likely be recompiled every value change
I’m pretty sure recompiles only happen when using a uniform for branching in a shader, but I could be wrong. Also, this behavior probably varies depending on the hardware/drivers used.

Kevin B

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.