openglnoob
openglnoob

Reputation: 1

How to access gl_Position variable for a specific vertex?

In the vertex shader, the position of the current vertex is stored in gl_Position from what I understand. If in the tessellation shader I wanted to change that variable for a specific vertex how would I do that? Does each vertex contain it's own gl_Position variable? Is there a way to do something like vertex1.gl_Position so then OpenGL would know that I want to modify vertex1's gl_Position variable?

Upvotes: 0

Views: 471

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474536

The tessellation control shader operates on patches, specific groups of vertices that are conceptually tessellated as a bundle. As such, the TCS's inputs are arrayed. The pre-defined VS outputs are aggregated into an array of TCS inputs in the following input interface block:

in gl_PerVertex
{
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
} gl_in[gl_MaxPatchVertices];

So gl_in[1].gl_Position is the gl_Position value output by the vertex shader for the second vertex in the patch.

Note that, while the array is sized on the maximum supported number of patch vertices, the actual limit is gl_PatchVerticesIn, the number of vertices in the tessellation patch.

Each TCS invocation outputs to a single vertex in the output patch (which may have a different number of vertices compared to the input patch). TCS outputs are also arrayed, and the built-in outputs have a similar array compared to the inputs:

out gl_PerVertex
{
  vec4 gl_Position;
  float gl_PointSize;
  float gl_ClipDistance[];
} gl_out[];

Each TCS invocation is meant to write only to the output index gl_InvocationID. If a barrier is used, they can read from output vertices that were written by other invocations (or other patch outputs).

Tessellation evaluation shaders operate on individual vertices in the abstract patch generated by the primitive generator. However, all TES invocations operating on the same patch get the entire output patch from the TCS. This is arrayed in the same way as the TCS input patch, just with an array size defined by the TCS's output patch size.

If the TCS did not write to the predefined TCS outputs, the corresponding TES cannot access those outputs. As such, they're not actually special, they have no intrinsic meaning. gl_in[1].gl_Position in the TES is only a "position" if the TCS put a position data value there.

Upvotes: 2

Related Questions