如果未绑定,统一值是否保留在 GLSL 着色器中?
我正在编写一个程序,该程序对不同的图元使用两种不同的着色器.我的问题是:如果我绑定一个程序,给它发送统一变量,然后使用另一个着色器程序并返回到第一个,传递的统一值会保留吗?这是一些伪代码:
I am writing a program that uses two different shaders for different primitives. My question is: if I bind a program, send it uniform variables, then use another shader program and come back to the first one, will the passed uniform values remain? Here is some pseudocode:
glUseProgram(shader1);
glUniform(shader1,...);
//stuff
for(elements in a list) {
if(element.type = 1) {
glUseProgram(shader2);
element.draw();
} else {
glUseProgram(shader1); //Here, do the uniforms from above remain, if shader2 was bound before?
element.draw();
}
}
推荐答案
是的,uniforms 是特定于某个程序的,如果你解除绑定和重新绑定它,它就会持久化.
Yes, uniforms are specific to a program, and will be persistent if you unbind and rebind it.
此外,如果您愿意,您可以在该示例中使用 glGetUniform.
Also, if you want, you could easily verify this yourself in that sample with glGetUniform.
来自 OpenGL 4.1 规范:
2.11.7 统一变量
...制服是程序对象特定的状态.一旦加载,它们就会保留它们的值,只要程序对象被使用,它们的值就会恢复,只要程序对象没有被重新链接....2.11.7 Uniform Variables
... Uniforms are program object-specific state. They retain their values once loaded, and their values are restored whenever a program object is used, as long as the program object has not been re-linked. ...
相关文章