Sorting uniform values, useful ?

Started by
3 comments, last by Bakura 14 years, 11 months ago
Hi everyone, I have designed my shader system, which looks good to me. Sorting each shader is really easy, but I wonder if it is useful to sort uniform values, and if so, how to do it ? For instance, let's say I have two different shaders that do completely different things. I first sort all objects according to those two shaders, so I have two sets of working queue. Let's take the first shader, and... well, we're gonna say it has three variables : - Uniform float : specular exponent - Uniform vec3 : color - Uniform mat4 : WorldViewMatrix Many objects can have the same shader, but maybe different parameters. For now, what I do is this : - Object 1 has 1.0f for specular exponent, (1.0f ; 0.5f ; 0.3f) for color, and retrieve the world view matrix from a function). - Object 2 has 5.0f for specular exponent, (1.0f ; 0.5f ; 0.3f) for color, and retrieve the world view matrix from a function). - Object 3 has 1.0f for specular exponent, (0.0f ; 0.3f ; 0.3f) for color, and retrieve the world view matrix from a function). So the setup, each frame, is : SetShader (shader1); SetUniform (1.0); SetUniform (1.0, 0.5, 0.3); SetUniform (WorldMatrix()); Draw (object1); SetUniform (5.0); SetUniform (1.0, 0.5, 0.3); SetUniform (WorldMatrix()); Draw (object2); SetUniform (1.0); SetUniform (0.0, 0.3, 0.3); SetUniform (WorldMatrix()); Draw (object3); As you can see, some SetUniform calls are quite redundant, and I wonder if it is important to worry about that (and how to sort those data ?), or if glUniform calls are fast enough ? For the moment, I have just thought about setting one bool called IsShared, for instance for WorldMatrix, because it will be always the same for each object that are using this shader, so I can only specify it once, but... is it possible to do better ? Thanks
Advertisement
I don't think that setting the uniform values is a problem - all you are doing ( from my understanding) is altering the constant registers on the GPU. Compared to the computational power/bandwidth used for drawing your object, I think they are minimal.

But there is not a good way to get around your problem anyway. For example, if your object has uniform color it is certainly better to set the color as a constant rather than passing a color value in through a vertex array.
Yes, but I think one great example is the specular exponant, for isntance. It can varies across each object that use the shader, and each object can have different values (while some objects CAN have the same value, so some calls are redundant). If there would be only one value it would be easy, just sort each object with that value, but it's not so simple when there are two or more variables like taht...
I would think that as long as your objects have more than a few vertices/pixels associated with them the calls to SetUniform will be negligible. There is definitely some overhead with setting constant values, but I think this should only be an issue if you do something like render 5 vertices with one value, then switch values.
I have also thought of keeping a list of uniform buffer objects (new extension of GLSL 1.40) for each object (but maybe the problem will move to binding buffer), so each object would have its own buffer. If, for example, the specular exponent never change for that object, the buffer will never bu updated.

But this means having one buffer for each object...

By the way, thank you for your answers, I think I'll go with the simple way at first ^^.

This topic is closed to new replies.

Advertisement