<p>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 <code>layout(location = X)</code> syntax in GLSL, but can also be done with <a href="https://www.opengl.org/sdk/docs/man/html/glBindAttribLocation.xhtml"><code>glBindAttribLocation</code></a> if former is not available.)</p>

<p>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 <a href="https://www.opengl.org/wiki/Uniform_Buffer_Object">Uniform Buffer Object</a>, which will contain all your uniform values and can then be bound to a shader with a single call.</p>

CC BY-SA 3.0
2017-02-02T18:07:02.573Z
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."
cmourglia
CC BY-SA 3.0
2015-08-20T21:17:33.460Z
2015-08-20T22:56:54.543Z
2015-08-20T22:56:54.543Z
yuriks
<p>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.</p>

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

<p>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)?</p>

<opengl><glsl><shader>
Do I need to rebind uniforms or attributes when changing shader programs?
Nero
# Do I need to rebind uniforms or attributes when changing shader programs?

(Asked by: username_0 on 2015-08-20)

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)?

**Tags:** opengl, glsl, shader


---

## 1 answers

### Answer by username_1 on 2015-08-20. Score: 12 | Accepted answer

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`](https://www.opengl.org/sdk/docs/man/html/glBindAttribLocation.xhtml) 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](https://www.opengl.org/wiki/Uniform_Buffer_Object), which will contain all your uniform values and can then be bound to a shader with a single call.

> **Comments:**
> **username_2**: 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."  

---

