Introspection du programme

Un certain nombre de fonctionnalités d’objets de programme peuvent être récupérées via l’API de programme.

## Informations sur les attributs de sommet Les informations sur les attributs de vertex peuvent être récupérées avec la fonction OGL glGetProgram et les paramètres GL_ACTIVE_ATTRIBUTES et GL_ACTIVE_ATTRIBUTE_MAX_LENGTH.

L’emplacement d’un attribut de shader actif peut être déterminé par la fonction OGL glGetAttribLocation, par l’index de l’attribut.

GLuint shaderProg = ...;
std::map< std::string, GLint > attributeLocation;

GLint maxAttribLen, nAttribs;
glGetProgramiv( shaderProg, GL_ACTIVE_ATTRIBUTES, &nAttribs );
glGetProgramiv( shaderProg, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxAttribLen 
GLint written, size;
GLenum type;
std::vector< GLchar >attrName( maxAttribLen );
for( int attribInx = 0; attribInx < nAttribs; attribInx++ )
{
    glGetActiveAttrib( shaderProg, attribInx, maxAttribLen, &written, &size, &type, &attrName[0] );
    attributeLocation[attrName] = glGetAttribLocation( shaderProg, attrName.data() );
}

Informations uniformes

Les informations sur les uniformes actifs dans un programme peuvent être récupérées avec la fonction OGL glGetProgram et les paramètres GL_ACTIVE_UNIFORMS et GL_ACTIVE_UNIFORM_MAX_LENGTH.

L’emplacement d’une variable uniforme de shader actif peut être déterminé par la fonction OGL glGetActiveUniform, par l’index de l’attribut .

GLuint shaderProg = ...;
std::map< std::string, GLint > unifomLocation;

GLint maxUniformLen, nUniforms;
glGetProgramiv( shaderProg, GL_ACTIVE_UNIFORMS, &nUniforms );
glGetProgramiv( shaderProg, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxUniformLen );

GLint written, size;
GLenum type;
std::vector< GLchar >uniformName( maxUniformLen );
for( int uniformInx = 0; uniformInx < nUniforms; uniformInx++ )
{
    glGetActiveUniform( shaderProg, uniformInx, maxUniformLen, &written, &size, &type, &uniformName[0] );
    unifomLocation[uniformName] = glGetUniformLocation( shaderProg, uniformName.data() );
}