- Home /
Replacing a geometry shader with a compute shader. How do I output an arbitrary number of triangles?
Hi everyone. I have been trying to recreate a geometry shader in a compute shader, and I am having trouble outputting an arbitrary number of triangles.
Basically, I want to take in mesh, and for each triangle on that mesh, create a variable amount of triangles extruded above it. I have a buffer filled with vertex data and another with the triangle indices taken from the mesh. Then, I dispatch a compute shader to run once per triangle. Once that's done, I call DrawProceduralIndirect.
To create triangles, I was I was using an append buffer and appending a triplet of vertices. Like this:
#pragma kernel Main
...
StructuredBuffer<SourceVertex> _SourceVertices;
StructuredBuffer<int> _SourceTriangles;
AppendStructuredBuffer<DrawVertex> _DrawVertices;
...
[numthreads(128,1,1)]
void Main(uint3 id : SV_DispatchThreadID) {
int triStart = id.x * 3;
SourceVertex a = _SourceVertices[_SourceTriangles[triStart]];
SourceVertex b = _SourceVertices[_SourceTriangles[triStart + 1]];
SourceVertex c = _SourceVertices[_SourceTriangles[triStart + 2]];
...
for(int i = 0; i < NumExtrusions; i++) {
_DrawVertices.Append(MakeDrawVertex(a), i);
_DrawVertices.Append(MakeDrawVertex(b), i);
_DrawVertices.Append(MakeDrawVertex(c), i);
}
}
The problem is that the order of an append buffer is not guaranteed to be the order of insertion (because of multithreading). So, when I get to the graphics shader, the triangles are all out of order. I feel like I'm missing something simple. Is the "correct" way to do this to output a triangle structure and then dispatch another compute shader which decomposes those into vertices?
Thanks so much for your help.