GLSL Uniform Variable Initialization/Management/Wrangling

Started by
10 comments, last by dpadam450 13 years, 4 months ago
Perhaps my skill in Google is not what I believe it to be, but I haven't found much information on how people manage the binding of uniform variables in their shaders?

By this, I mean, you write a shader and declare some uniforms. Now you have to set them up on the client side with glGetUniformLocation() and glUniform(). Can you "automagically" set some of these uniforms? I don't want to have to rewrite code in two different places because I changed a variable name in one place. Is this simply the reality of writing shaders?
Advertisement
Do you mean a default value?

From opengl.org
Quote:
Uniforms are intended to be set by the user. However, you can initialize them to a default value using standard GLSL initalizer syntax:

uniform vec3 initialUniform = vec3(1.0, 0.0, 0.0);

This will cause the uniform to have this vector as its value until the user changes it.


Other than this, you're pretty much on your own, you can encapsulate your shaders in any kind of class that you want, but essentially at some point you have to tell OpenGL what you want to do with the uniforms you declare.

I guess the best thing you could do would just be to use consistent naming across all of your shaders, i.e. make the name of the projection matrix uniform the same across all of your shaders.

You also can get a list of the uniforms in your shader with glGetActiveUniform, which may help you somehow.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I want to be able to make changes to shader code and have it be as independent as possible from the rest of the client code. Basically, if I have a uniform and I change the name of it, I don't want to have to go find the setup code and change everything to match.

But it seems like the way the binding works, it is fundamentally not possible to make such bindings automatic (you need to know which client side variable maps to which shader variable, but only the programmer can know what this mapping is). Is my observation accurate?
Yes I think you have the right understanding. If you change the name of the uniform, you have to change the corresponding code in the client. Otherwise how would you know which uniform to send the data to?
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Precisely. So I guess my next question then is: how do professional developers deal with this? Do they simply develop some sort of uniform naming convention (as you mentioned) and add any other uniforms as they need it? Or do they simply try to avoid using "non-standard" uniforms and try to hack some functionality in by way of textures?
well, I'm not a professional but academic developer. The best way to me is to write something like this in glsl:
#AutoBind( typename, globalVariable )
This implies parsing the shader code once yourself.
which is really, *really* suggested. You can also solve includes that way and do other funny stuff...
On the cpu-side, I have a variable manager which is basically a manager for hashmaps of different type (int, bool, vec2, vec3, etc.)
Quote:Original post by snowmanZOMG
Perhaps my skill in Google is not what I believe it to be, but I haven't found much information on how people manage the binding of uniform variables in their shaders?

By this, I mean, you write a shader and declare some uniforms. Now you have to set them up on the client side with glGetUniformLocation() and glUniform(). Can you "automagically" set some of these uniforms? I don't want to have to rewrite code in two different places because I changed a variable name in one place. Is this simply the reality of writing shaders?


Sounds like you don't want uniform.
The purpose of a uniform is so that you can set it from your C++ (or whatever) code.

You want to have a constant.
Just declare this in your GLSL
const vec4 myvalue=vec4(0.0, 1.0, 2.0, 2.5);

Yup, you haven't been Googling enough or perhaps the Wiki over at opengl.org/wiki sucks.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by snowmanZOMG
Precisely. So I guess my next question then is: how do professional developers deal with this? Do they simply develop some sort of uniform naming convention (as you mentioned) and add any other uniforms as they need it? Or do they simply try to avoid using "non-standard" uniforms and try to hack some functionality in by way of textures?


I think they try to avoid using opengl (oh snap!).

Seriously though in DirectX you have the ability to bind uniforms directly to constant registers without having to know the shader's name for the variable. The same way you can bind vertex attributes by semantic rather than by name as you have to in opengl. I've found this to be quite a problem when working with opengl. Especially if you use lots of different shaders, you waste a lot of performance calling glGetUniformLocation and glGetAttribLocation all the time. If there's a better solution to this I'd love to hear it as well.
call opengl for the attributs uniforms only once?

also with opengl3.3 (?) u can bind attributs in your glsl code directly to an location.
And uniforms can be shared with uniform buffer objects
Quote:Original post by doesnotcompute
Quote:Original post by snowmanZOMG
Precisely. So I guess my next question then is: how do professional developers deal with this? Do they simply develop some sort of uniform naming convention (as you mentioned) and add any other uniforms as they need it? Or do they simply try to avoid using "non-standard" uniforms and try to hack some functionality in by way of textures?


I think they try to avoid using opengl (oh snap!).

Seriously though in DirectX you have the ability to bind uniforms directly to constant registers without having to know the shader's name for the variable. The same way you can bind vertex attributes by semantic rather than by name as you have to in opengl. I've found this to be quite a problem when working with opengl. Especially if you use lots of different shaders, you waste a lot of performance calling glGetUniformLocation and glGetAttribLocation all the time. If there's a better solution to this I'd love to hear it as well.


1) Explicit_Attrib_Location (don't remember if it's ARB or EXT) is an extension which allows you to bind attribs to numbers (or semantics if you do the #define POSITION 0 stuff).

2) glBindAttribLocation is a no-cost way to bind attributes after the shader linkage is complete
OpenGL fanboy.

This topic is closed to new replies.

Advertisement