Difference between revisions of "Example/Program Interface Uniforms In Blocks"

From OpenGL Wiki
Jump to: navigation, search
(Example page for uniform block iteration code.)
 
 
(3 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
<source lang="cpp">
 
<source lang="cpp">
 
GLint numBlocks = 0;
 
GLint numBlocks = 0;
glGetProgramInterfaceiv(prog, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &numBlocks );
+
glGetProgramInterfaceiv(prog, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &numBlocks);
const GLenum blockProperties[1] = {GL_NUM_ACTIVE_VARIABLES}
+
const GLenum blockProperties[1] = {GL_NUM_ACTIVE_VARIABLES};
const GLenum activeUnifProp[1] = {GL_ACTIVE_VARIABLES}
+
const GLenum activeUnifProp[1] = {GL_ACTIVE_VARIABLES};
 
const GLenum unifProperties[3] = {GL_NAME_LENGTH​, GL_TYPE​, GL_LOCATION};
 
const GLenum unifProperties[3] = {GL_NAME_LENGTH​, GL_TYPE​, GL_LOCATION};
  
for(int blockIx = 0; blockIx < numBlocks; ++numBlocks)
+
for(int blockIx = 0; blockIx < numBlocks; ++blockIx)
 
{
 
{
 
   GLint numActiveUnifs = 0;
 
   GLint numActiveUnifs = 0;
Line 22: Line 22:
 
   {
 
   {
 
     GLint values[3];
 
     GLint values[3];
     glGetProgramResourceiv(prog, GL_UNIFORM, unifIx, 3, unifProperties, 4, NULL, values);
+
     glGetProgramResourceiv(prog, GL_UNIFORM, blockUnifs[unifIx], 3, unifProperties, 3, NULL, values);
  
     //Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
+
     // Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
     //C++11 would let you use a std::string directly.
+
     // C++11 would let you use a std::string directly.
 
     std::vector<char> nameData(values[0]);
 
     std::vector<char> nameData(values[0]);
     glGetProgramResourceName(prog, GL_UNIFORM, unifIx, nameData.size(), NULL, &nameData[0]);
+
     glGetProgramResourceName(prog, GL_UNIFORM, blockUnifs[unifIx], nameData.size(), NULL, &nameData[0]);
 
     std::string name(nameData.begin(), nameData.end() - 1);
 
     std::string name(nameData.begin(), nameData.end() - 1);
 
   }
 
   }
 
}
 
}
 
</source>
 
</source>
 +
<noinclude>[[Category:Example Code]]</noinclude>

Latest revision as of 11:18, 3 January 2018

Iteration over all uniforms within each uniform block.

GLint numBlocks = 0;
glGetProgramInterfaceiv(prog, GL_UNIFORM_BLOCK, GL_ACTIVE_RESOURCES, &numBlocks);
const GLenum blockProperties[1] = {GL_NUM_ACTIVE_VARIABLES};
const GLenum activeUnifProp[1] = {GL_ACTIVE_VARIABLES};
const GLenum unifProperties[3] = {GL_NAME_LENGTH​, GL_TYPE​, GL_LOCATION};

for(int blockIx = 0; blockIx < numBlocks; ++blockIx)
{
  GLint numActiveUnifs = 0;
  glGetProgramResourceiv(prog, GL_UNIFORM_BLOCK, blockIx, 1, blockProperties, 1, NULL, numActiveUnifs);

  if(!numActiveUnifs)
    continue;

  std::vector<GLint> blockUnifs(numActiveUnifs);
  glGetProgramResourceiv(prog, GL_UNIFORM_BLOCK, blockIx, 1, activeUnifProp, numActiveUnifs, NULL, &blockUnifs[0]);

  for(int unifIx = 0; unifIx < numActiveUnifs; ++unifIx)
  {
    GLint values[3];
    glGetProgramResourceiv(prog, GL_UNIFORM, blockUnifs[unifIx], 3, unifProperties, 3, NULL, values);

    // Get the name. Must use a std::vector rather than a std::string for C++03 standards issues.
    // C++11 would let you use a std::string directly.
    std::vector<char> nameData(values[0]);
    glGetProgramResourceName(prog, GL_UNIFORM, blockUnifs[unifIx], nameData.size(), NULL, &nameData[0]);
    std::string name(nameData.begin(), nameData.end() - 1);
  }
}