Does Metal 2 have geometry shaders?

I don't think they were supported in Metal, but are they now with Metal 2?

Replies

Geometry shaders are not supported in Metal 2. What's your use case? If you feel that what you want to do can't be accomplished with compute shaders, tesselation and/or indirect draw calls, I'd encourage you to file an enhancement request at http://bugreport.apple.com.

I was thinking about writing something similar to this fancy line-drawing algorithm in Metal:


https://github.com/paulhoux/Cinder-Samples/tree/master/GeometryShader

I think these shaders could be implmented using compute kernels. You would emulate the vertex shader with a compute kernel by writing what a vertex shader normally outputs to a buffer. Then read that in with a second compute kernel emulating this geometry shader writing out the wide version of the vertices to another buffer. Finally you render using a passthrough vertex shader with the vertices created by your second compute kernel.

Ok thanks I will give it a shot. I don't know much about how compute kernels work yet. Do you know of any simple online examples of this kind of multi-stage rendering?

Hey, I am not Dan, but I believe you don't need to emulate geometry shaders for (relatively) simple stuff like drawing of the thick lines. Thing is, for thick lines you know number of vertices in advance - for example if you're drawing them as single triangle strip, you'll need two times the line vertices. If as a quad (two-triangle strip) you'll need 4 vertices per line segment, and so on. I believe that geometry shaders (or their emulation) are needed when you don't know the number of vertices in advance, like drawing/updating particle systems where "lifetimes" of individual particles may differ.


But if all you want is to draw nice, thick lines, then good example will be in this thread: https://forums.developer.apple.com/thread/86098. We discussed doing wide lines efficiently just a few weeks ago. Check it out.

Regards

Michal

OK thanks I will check this out. And yes, with lines you know the number of vertices -- but would it not be more efficient if you could just submit 1/4 the number of vertices and then have the rest produced by shaders? Maybe not - maybe the extra compute passes would be worse than submitting 4x the data. I have not enough experience with shader programming yet.


Bob

Yeah, you submit 1/4 of vertices - only two per segment (if not connected) or first and then one per next segment. Then you ask Metal to draw as many instances of 4-vertex triangle strips (quads) as number of segments you draw. Within vertex shader, you use instance_id to get vertex (your disconnected line segment will go from vertices[instance_id * 2] to vertices[instance_id * 2 + 1], for example), and then vertex_id to choose which particular vertex of quad you're going to generate. This was/is called "programmable vertex pulling", I believe.


Michal