Reputation: 787
I am trying to use multiple uniform blocks in one GLSL shader and I debug my shader using the following code to print available uniform blocks:
void printUniformBlocks(void)
{
GLint numBlocks;
GLint nameLen;
vector<string> nameList;
glGetProgramiv(this->program, GL_ACTIVE_UNIFORM_BLOCKS, &numBlocks);
nameList.reserve(numBlocks);
cout << "found " << numBlocks << " block in shader" << endl;
for (int blockIx = 0; blockIx < numBlocks; blockIx++) {
glGetActiveUniformBlockiv(this->program, blockIx, GL_UNIFORM_BLOCK_NAME_LENGTH, &nameLen);
vector<GLchar> name;
name.resize(nameLen);
glGetActiveUniformBlockName(this->program, blockIx, nameLen, NULL, &name[0]);
nameList.push_back(std::string());
nameList.back().assign(name.begin(), name.end() - 1); //Remove the null terminator.
}
for (unsigned int il = 0; il < nameList.size(); il++) {
cout << "Block name: " << nameList[il] << endl;
}
}
My shader has the following
layout(std140) uniform Matrices {
mat4 model[16];
};
layout(std140) uniform Matrices2 {
mat4 model2[16];
};
.....
What could be the reason why this only reports 1 block in the shader and only prints the "Matrices" block?
Upvotes: 0
Views: 796
Reputation: 45948
Make sure you actually use that uniform block in the rest of the shader. The GLSL compiler is allowed, encouraged, and pretty probable, to remove any unused uniforms and attributes from the shaders. That's the reason why the uniforms resulting after this optimization are called "active" uniforms and the reason why a uniform declared but not otherwise used inside the shader code can (and most probably will) disappear in the resulting executable.
Upvotes: 5