Using the new "invocation count" feature in geometry shaders

Modern OpenGL lets you specify an invocation count in your geometry shader:

layout(triangles, invocations = 3) in;

I’m updating some old GLSL and I wish to use this functionality because it lets me avoid looping over verts. However, I’m either doing something incorrectly or hitting a driver issue. Here’s my old code:

for (int i = 0; i < 3; i++) { gl_Position = gl_in[i].gl_Position; EmitVertex(); }

Here’s my new code:

gl_Position = gl_in[gl_InvocationID].gl_Position;
if (gl_InvocationID == 2) EndPrimitive();

This renders bad triangles, so what am doing wrong? Do I need to synchronize the invocations before calling EndPrimitive? If so, how?

You’ve misunderstood how instanced geometry shaders work.

A single geometry invocation still has to emit the complete primitive. You cannot “distribute” the processing of a single primitive across invocations. You can only “distribute” separate primitives between invocations.

The sample you’ve presented could not be made more parallel with instanced geometry shader, however, if you would have to emit 3 separate triangles then you could do it by performing 3 invocations and emit only one triangle in each invocation. Geometry shader invocations still have to emit complete primitives.

Perfect, thanks for the help!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.