8
\$\begingroup\$

How can I create a geometry shader that can handle multiple primitives? For example when creating a geometry shader for triangles, I define a layout like so:

layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;

But if I use this shader then lines or points won't show up. So adding:

layout(triangles) in;
layout(triangle_strip, max_vertices=3) out;
layout(lines) in;
layout(line_strip, max_vertices=2) out;

The shader will compile and run, but will only render lines (or whatever the last primitive defined is).

So how do I define a single geometry shader that will handle multiple types of primitives? Or is that not possible and I need to create multiple shader programs and change shader programs before drawing each type?

\$\endgroup\$
7
\$\begingroup\$

I don't believe OpenGL supports geometry shaders which output multiple types of primitives. I'm actually surprised that your second code block compiled... Perhaps it just overwrote the configuration?

You will have to create multiple shaders and switch between them, at least for triangles/lines and points. There's actually a trick you can do with a fragment shader to receive triangles but render only lines by discarding fragments that fall too far inside the triangle according to barycentric coordinates. For the exact implementation of that, I'd refer you to the OpenGL Orange Book and its section on geometry shaders and drawing a wireframe (I'll come back and edit after I track down the exact chapter).

Happy coding!

\$\endgroup\$
2
\$\begingroup\$

It isn't possible to create a program with geometry shaders that handle multiple primitive types.

That being said, you don't have to do a whole lot of work to support multiple primitive types. Thanks to ARB_separate_shader_objects, you no longer have to link all of your shader stages together. You can have one vertex-only program, one fragment-only program, and 2-3 geometry-only programs that you mix-and-match together. As long as they use compatible interfaces, you'll be fine.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.