In OpenGL is there a way to get a list of all uniforms & attribs used by a shader program?

后端 未结 4 1313
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 11:01

I\'d like to get a list of all the uniforms & attribs used by a shader program object. glGetAttribLocation() & glGetUniformLocation() can

4条回答
  •  萌比男神i
    2020-12-12 11:21

    Variables shared between both examples:

    GLint i;
    GLint count;
    
    GLint size; // size of the variable
    GLenum type; // type of the variable (float, vec3 or mat4, etc)
    
    const GLsizei bufSize = 16; // maximum name length
    GLchar name[bufSize]; // variable name in GLSL
    GLsizei length; // name length
    

    Attributes

    glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &count);
    printf("Active Attributes: %d\n", count);
    
    for (i = 0; i < count; i++)
    {
        glGetActiveAttrib(program, (GLuint)i, bufSize, &length, &size, &type, name);
    
        printf("Attribute #%d Type: %u Name: %s\n", i, type, name);
    }
    

    Uniforms

    glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &count);
    printf("Active Uniforms: %d\n", count);
    
    for (i = 0; i < count; i++)
    {
        glGetActiveUniform(program, (GLuint)i, bufSize, &length, &size, &type, name);
    
        printf("Uniform #%d Type: %u Name: %s\n", i, type, name);
    }
    

    OpenGL Documentation / Variable Types

    The various macros representing variable types can be found in the docs. Such as GL_FLOAT, GL_FLOAT_VEC3, GL_FLOAT_MAT4, etc.

    • glGetActiveAttrib
    • glGetActiveUniform

提交回复
热议问题