Hi Paul,

> 
> Maybe somebody on this list can save me from reinventing the wheel...
> 
> I'm looking to get a better understanding of performance 
> issues related to uniforms.
> 
> How much overhead is there in having a uniform?  If there 
> only a performance hit if the uniform changes values or every 
> frame. What if I change the Uniform in OSG to exactly the 
> same value it already has, would there be a performance hit?
> 
At an OpenGL level a uniform change will result in a state change, in the same 
way as changing the diffuse color or a texture parameter would. This can mean 
that the graphics hardware may need to wait whilst some currently executing 
commands are flushed.

If you only change it once a frame and you have any objects with different 
state in your scene, it will not make any difference. The main cost of uniforms 
is when you have many objects with slightly difference uniform values in the 
same frame as this introduces state changes.

Uniforms are part of the osg::StateSet, so you need to be careful about making 
too many different ones as too many can cause performance problems.

> I have uniforms that might not change values very often. Some 
> are simply boolean flags. Can I have different shaders and 
> somehow switch between them?  Can I "recompile" the shader 
> on-the-fly (i.e. defining these boolean using #define/#if-#endif)? 

Compiling the shader on the fly is certainly possible, how long it takes I'm 
not sure. I expect it is an expensive state change ( like a texture download ), 
rather than a cheapish sort ( such as a uniform ), so probably not good if you 
are doing it a lot.

> In some cases, I have variables which can change within the 
> shader, but I know these values when I create the scene graph 
> so I currently use #define instead of passing them as 
> uniforms (which will ever only have one value). Does this 
> gain me much in performance? 

I assume that the #define values are in the shader. The only way I've found to 
use #defines is with a different shader program ( character array or string ). 
As such it will mean using a different osg::Shader object and therefore more 
gfx memory and a different shader handle.
I expect the cost of switching shaders is the same or more than switching the 
value of Uniforms.

> 
> Also, is there a difference in performance in using four 
> "float" uniforms versus setting a Vec4?
>
>From my understanding a Vec4 will be cheaper than a four float uniforms in 
>terms of state changing. Each uniform needs to be checked with the uniforms in 
>the shader by the OpenGL driver. This is some sort of string compare. Also 
>increasing the number of uniforms increases the length of the list in 
>osg::StateSet and therefore how long it takes to walk.
It is also feasible that the hardware is faster at dealing with shaders with 
less uniforms, so that your shaders may run faster with a Vec4 rather than 4 
floats. However this is entirely dependant on the hardware optimisations. I've 
not tested this.

Colin.
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to