Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often do I have to update my uniforms?

Tags:

webgl

All of the WebGL sample code I've seen calls gl.useProgram(), and then sets every uniform for the program, once per frame. The thing is, the two most costly things you can do in a WebGL project are run lines of Javascript and copy stuff from the Javascript into the shader, and here's everybody doing both sixty times a second. Also, I know that most of MY uniforms, in my own program, do NOT change from frame to frame. (It's mostly light positions and such, stuff that you could mess with but usually don't.)

My question is, does the WebGL specification have anything to say about how long uniforms' values are preserved? If I call gl.useProgram(), might the program I've just stopped using lose its uniforms? If calling gl.useProgram() scrambles the uniforms, can I just not call it every frame, and have my uniforms preserved?

If I could just force all users to just run Firefox 22.0 forever, I could write and run some test code, and have this figured out in a jiffy. Sadly...

like image 760
mjfgates Avatar asked Nov 03 '22 18:11

mjfgates


1 Answers

A uniform's value is associated with a particular program. It will not go away just because you switched to another program. (I don't have a spec reference handy.)

Probably the code you're seeing is setting the uniforms every frame for one of these reasons:

  • Simple laziness: this will definitely work, other things would require thought.

  • Copying code from other cases where the uniform does need to be set every frame.

  • Complex laziness: it's often hard to define exactly the circumstances under which the correct value of a parameter does change, so just recompute it every frame. Or, it may even be more computationally expensive to figure out whether it needs to change!

Also, doing uniforms every frame isn't that bad, because there's only a few uniforms and they're not much data. Computing or copying vertex arrays or other per-scene-object stuff is the sort of thing you should worry about first.

like image 102
Kevin Reid Avatar answered Nov 08 '22 10:11

Kevin Reid