Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd like to get a list of all the uniforms & attribs used by a shader program object. glGetAttribLocation() & glGetUniformLocation() can be used to map a string to a location, but what I would really like is the list of strings without having to parse the glsl code.

Note: In OpenGL 2.0 glGetObjectParameteriv() is replaced by glGetProgramiv(). And the enum is GL_ACTIVE_UNIFORMS & GL_ACTIVE_ATTRIBUTES.

like image 945
hyperlogic Avatar asked Jan 13 '09 18:01

hyperlogic


People also ask

How many uniforms can a shader have?

It is illegal to assign the same uniform location to two uniforms in the same shader or the same program.

What is uniform buffer?

A Buffer Object that is used to store uniform data for a shader program is called a Uniform Buffer Object. They can be used to share uniforms between different programs, as well as quickly change between sets of uniforms for the same program object.

What is uniform WebGL?

According to the OpenGL wiki, a uniform is “a global GLSL variable declared with the 'uniform' storage qualifier.” To be a little more specific: your shader executes on the GPU, which is physically distinct from the rest of the computer, separated by a bus.

What is the difference between OpenGL and GLSL?

The short version is: OpenGL is an API for rendering graphics, while GLSL (which stands for GL shading language) is a language that gives programmers the ability to modify pipeline shaders. To put it another way, GLSL is a (small) part of the overall OpenGL framework.


1 Answers

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
like image 160
NeARAZ Avatar answered Sep 23 '22 17:09

NeARAZ