4

If I am generating 0-12 triangles in a compute shader, is there a way I can stream them to a buffer that will then be used for rendering to screen?

My current strategy is:

  • create a buffer of float3 of size threads * 12, so can store the maximum possible number of triangles;
  • write to the buffer using an index that depends on the thread position in the grid, so there are no race conditions.

If I want to render from this though, I would need to skip the empty memory. It sounds ugly, but probably there is no other way currently. I know CUDA geometry shaders can have variable length output, but I wonder if/how games on iOS can generate variable-length data on GPU.

UPDATE 1:

As soon as I wrote the question, I thought about the possibility of using a second buffer that would point out how many triangles are available for each block. The vertex shader would then process all vertices of all triangles of that block.

This will not solve the problem of the unused memory though and as I have a big number of threads, the total memory wasted would be considerable.

1 Answer 1

4

What you're looking for is the Metal equivalent of D3D's "AppendStructuredBuffer". You want a type that can have structures added to it atomically.

I'm not familiar with Metal, but it does support Atomic operations such as 'Add' which is all you really need to roll your own Append Buffer. Initialise the counter to 0 and have each thread add '1' to the counter and use the original value as the index to write to in your buffer.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I came up with: increment counter atomically to reserve a specific zone of the buffer, then write on buffer. At the end of the pass, copy buffer to render buffer using atomic int size and delete work buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.