Sign in to follow this  

Iterate over append/consume buffer without consuming?

Recommended Posts

Hi,

 

I have a compute shader which populates an append buffer, and a another shader that reads from it as a consume buffer. 

 

Between these invocations, I would like to read every element in the resource in order to populate a second buffer.

 

 

I can think of a couple of ways to do it, such as using Consume() in my intermediate shader, and re-setting the count of the buffer afterwards. Or, binding the resource as a regular buffer and reading the whole thing. 

 

There doesn't seem to be a way to set the count entirely on the GPU, and its not clear if the second method is supported (e.g. "Use these resources through their methods, these resources do not use resource variables.").

 

Is there any supported way to read an AppendStructuredBuffer without decreasing its count?

 

Thanks!

 

 

(PS. Cross-post at SO: http://stackoverflow.com/questions/41416272/set-counter-of-append-consume-buffer-on-gpu)

Share this post


Link to post
Share on other sites

You can just bind it as a normal structured buffer in your intermediate shader and then read elements by index. There's nothing really special about AppendStructuredBuffer/ConsumeStructuredBuffer, they're just a little bit of syntactical sugar on top of the "hidden counter" functionality available with structured buffers.

 

If you need to know the count of the buffer in your shader, you can use CopyStructureCount to copy the hidden counter value to a constant buffer (or any other buffer type). It's also possible to use this value to drive the dispatch count of a DispatchIndirect call so that you can launch the appropriate number of threads for a shader that reads the buffer. The process goes something like this:

 

  • Run shader that populates buffer using AppendStructuredBuffer
  • Copy counter value to a constant buffer
  • Run simple 1-thread compute shader that reads the counter value and divides by the thread group count to get the dispatch count, and outputs it to an indirect args buffer
  • Run DispatchIndirect with the args buffer from the previous step

Share this post


Link to post
Share on other sites

Thanks MJP!

 

Do you know what MSDN meant by that line in my original post? It says 'resource' specifically, rather than view - but then the whole thing is pretty ambiguous.

 

Sj

Share this post


Link to post
Share on other sites

That line from MSDN is just saying that if you have an AppendStructuredBuffer in HLSL, you need to call .Append() instead of doing appendBuffer[idx] = value. So if you want to read to read from the buffer without changing the count, you need to declare a StructuredBuffer in your HLSL code instead of declaring a AppendStructuredBuffer or ConsumeStructuredBuffer. Then you just need to bind a shader resource view to the appropriate slot instead of binding an unordered access view.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this