Archive for January 2010

uniforms   6 comments

Hey there,
I am still not a regular blogger but I hope I can change that. So now I am going to talk about uniforms. As many know, uniform variables are used to specify things that change for each VBO and not for each vertex. That means many uniform variables are normally used in a shader. So that is why I started researching uniforms. First thing I noticed although everyone saved the location of an attribute but not so of the uniforms. This seemed a weird phenomena so I started looking at the openGL ES draft.

quote from openGL ES draft:

“The actual locations assigned to uniform variables are not known until the program object is linked successfully. After linking has occurred, the command glGetUniformLocation can be used to obtain the location of a uniform variable. This location value can then be passed to glUniform to set the value of the uniform variable or to glGetUniform in order to query the current value of the uniform variable. After a program object has been linked successfully, the index values for uniform variables remain fixed until the next link command occurs.Uniform variable locations and values can only be queried after a link if the link was successful.”

So now I say to everyone using shaders, don’t do such costly operations as getting the location of uniforms in your draw function. It is better to do it only when shaderprograms get added. This would be a one time cost that makes sure that everything in your draw function get faster.

But then of course most people want to play safe against typo’s so you will need to know if a uniform variable is actually in the shader. because some browsers doesn’t show anything different about whether it has found the location or not as it shows only “WebGLUniformLocation”.

This solves that:

var uniformlocation,uniform;
uniformlocation = gl.getUniformLocation(shaderProgram, NameOfUniform);
if (uniformlocation !== -1){
uniform = gl.getUniform(shaderProgram, uniformlocation);
} else {
uniform = null;
if (uniform === null){
alert("uniform " + NameOfUniform + " doesn't exist in shaders");

So I hope that everyone will use a store uniform locations in a variable to allow even more faster webgl demos. I hope to see you readers for the next blogpost.


Posted January 11, 2010 by ewgl in webGL