opengltesselation

Tessellation Shaders


I am trying to learn tessellation shaders in openGL 4.1. I understood most of the things. I have one question.

What is gl_InvocationID?

Can any body please explain in some easy way?


Solution

  • gl_InvocationID has two current uses, but it represents the same concept in both.

    In Geometry Shaders, you can have GL run your geometry shader multiple times per-primitive. This is useful in scenarios where you want to draw the same thing from several perspectives. Each time the shader runs on the same set of data, gl_InvocationID is incremented.

    The common theme between Geometry and Tessellation Shaders is that each invocation shares the same input data. A Tessellation Control Shader can read every single vertex in the input patch primitive, and you actually need gl_InvocationID to make sense of which data point you are supposed to be processing.

    This is why you generally see Tessellation Control Shaders written something like this:

    gl_out [gl_InvocationID].gl_Position = gl_in [gl_InvocationID].gl_Position;
    

    gl_in and gl_out are potentially very large arrays in Tessellation Control Shaders (equal in size to GL_PATCH_VERTICES), and you have to know which vertex you are interested in.

    Also, keep in mind that you are not allowed to write to any index other than gl_out [gl_InvocationID] from a Tessellation Control Shader. That property keeps invoking Tessellation Control Shaders in parallel sane (it avoids order dependencies and prevents overwriting data that a different invocation already wrote).