PDA

View Full Version : Program constant optimization



Zeno
09-12-2006, 03:58 PM
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 :)

speedy
09-12-2006, 05:23 PM
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.

V-man
09-13-2006, 06:30 AM
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.

Zeno
09-13-2006, 03:18 PM
Originally posted by speedy:
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. 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.

Linuxhippy
09-18-2006, 07:50 AM
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

ebray99
09-26-2006, 09:12 AM
if you switch the uniform variables of a single shader the shader will quite likely be recompiled every value changeI'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