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.

Advertisements

Posted January 11, 2010 by ewgl in webGL

6 responses to “uniforms

Subscribe to comments with RSS.

  1. Excellent tip, I hadn’t even considered that there would be a cost to looking up uniform locations every frame.

  2. Pingback: WebGL around the net, 14 Jan 2010 | Learning WebGL

  3. An sich ne gute Sache, ich frag mich nur, ob das auch dauerhaft brauchbar bleibt.

  4. Pingback: Retrospective changes: storing attributes and uniforms on the program | Learning WebGL

  5. Pingback: Stargames Casino online

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: