3

Does OpenGL provide API to gain number of fragment shaders outputs?

I've found functions such as glBindFragDataLocation, glBindFragDataLocationIndexed, glGetFragDataIndex and glGetFragDataLocation but all of them are designed to set indices or locations of FragData with known name.

I think that I'm looking for something like glGetProgram(handle, GL_NUM_FRAGDATA, &i). Any ideas?

There is very similar API for getting number of uniforms and attrbutes:

  • glGetProgramiv(handle, GL_ACTIVE_UNIFORMS, &numUniforms)
  • glGetProgramiv(handle, GL_ACTIVE_ATTRIBUTES, &numAttribs)

Thanks in advance.

t91
  • 163
  • 1
  • 8

1 Answers1

3

The section of the API on Program Interfaces is what you're looking for:

int num_frag_outputs;           //Where GL will write the number of outputs
glGetProgramInterfaceiv(program_handle, GL_PROGRAM_OUTPUT,
    GL_ACTIVE_RESOURCES, &num_frag_outputs);

//Now you can query for the names and indices of the outputs

for (int i = 0; i < num_frag_outs; i++)
{
    int identifier_length;
    char identifier[128];            //Where GL will write the variable name
    glGetProgramResourceName(program_handle, GL_PROGRAM_OUTPUT,
        i, 128, &identifier_length, identifier);

    if (identifier_length > 128) {
      //If this happens then the variable name had more than 128 characters
      //You will need to query again with an array of size identifier_length 
    }
    unsigned int output_index = glGetProgramResourceIndex(program_handle,
    GL_PROGRAM_OUTPUT, identifier);

    //Use output_index to bind data to the parameter called identifier
}

You use GL_PROGRAM_OUTPUT to specify that you want the outputs from the final shader stage. Using other values you can query other Program Interfaces to find shader outputs from other stages as well. Check out section 7.3.1 from the OpenGL 4.5 spec for more info.