How does glGetUniformBlockIndex know whether to look in the vertex shader or the fragment shader?

Go To StackoverFlow.com

1

I start by attaching to my program both shaders and then call glGetUniformBlockIndex to bind the uniform buffers. I would expect a glBindShader function to let me specify which shader I want to parse to find the uniform, but there is no such thing. How can I tell OpenGL which shader it should look into?

2012-04-04 23:28
by qdii
"I start by attaching to my program both shaders and then call glGetUniformBlockIndex to bind the uniform buffers" You need to have a successful call to <code>glLinkProgram</code> in between these steps - Nicol Bolas 2012-04-04 23:43
I just confirmed that the glLinkProgram is carried out between the shaders’ compilation and calling glGetUniformBlockIndex. And it succeeds - qdii 2012-04-04 23:52


4

Assuming the program has been fully linked, it doesn't matter. Here are the possibilities for glGetUniformBlockIndex and their consequences:

  • The uniform block name given is not in any of the shaders. Then you get back GL_INVALID_INDEX.
  • The uniform block name given is used in one of the shaders. Then you get back the block index for it, to be used with glUniformBlockBinding.
  • The uniform block name given is used in multiple shaders. Then you get back the block index that means all of them, to be used with glUniformBlockBinding.

The last part is key. If you specify a uniform block in two shaders, with the same name, then GLSL requires that the definitions of those uniform blocks be the same. If they aren't, then you get an error in glLinkProgram. Since they are the same (otherwise you wouldn't have gotten here), GLSL considers them to be the same uniform block.

Even if they're in two shader stages, they're still the same uniform block. So you can share uniform blocks between stages, all with one block binding. As long as it truly is the same uniform block.

2012-04-04 23:53
by Nicol Bolas
Ads