11
$\begingroup$

Rendering the scene usually involves more than one shader program which, in my case, all use the same attributes and share at least some of the uniforms. To have them working properly, I currently play safe, meaning I rebind the attributes and get the appropriate uniform locations every time I switch between shader programs. So basically multiple times in every frame, which is probably not the best approach.

So, is it (in general) necessary to rebind attributes and uniforms after switching shader programs? And why?

If so, is there a way to do this once at start of the program and never have to touch them again (except for setting the uniform values)?

$\endgroup$
12
$\begingroup$

You don't need to rebind the attributes, so long as you ensure that their location stays the same in both shaders. (Usually using the layout(location = X) syntax in GLSL, but can also be done with glBindAttribLocation if former is not available.)

Uniforms, however, are part of the Shader Object state, and so will need to be set at least once for every shader. One way to minimize the cost of this is to use a Uniform Buffer Object, which will contain all your uniform values and can then be bound to a shader with a single call.

$\endgroup$
  • $\begingroup$ On your UBO link: "Lastly, they can be used to share information between different programs. So modifying a single buffer can effectively allow uniforms in multiple programs to be updated." $\endgroup$ – Zouch Feb 2 '17 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.