Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

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!

share|improve this answer
Thanks, I suspected that might be the case. – Byte56 Oct 14 '12 at 8:10

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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