Buffer as both StructuredBuffer & RWStructuredBuffer

Started by
5 comments, last by MJP 6 years, 8 months ago

I am trying to use a buffer as a rwstructuredbuffer in one call and in another reuse that as a structuredbuffer to read the data from. However, that's not working. Can you not do that? There is no error thrown when binding the buffer as both ShaderResource & UnordererdAccess. It also works fine if I replace the StructuredBuffer with a RWStructuredBuffer in the reading call.

and I am also doing the following already:
- use StructuredBuffer<uint> : register(tx) vs RWStructuredBuffer<uint> : register(ux) in the shader
- use CSSetShaderResource for structured vs CSSetUnorderedAccessViews for rwstructured

I am hoping to use this buffer in different shaders (vs, ps, cs, but these are all different draw call/dispatches), and since rwstructuredbuffer is not usable in VS AFAIK, I was hoping I can bind it as both structured and rwstructured. Any thoughts?

Thanks!

Advertisement

Ok i hate my life... apparently draw call bindings are different from dispatch bindings?? So the buffer when bound as UAV is written from a draw call pixel shader (i.e. bound using OMSetRenderTargetsAndUnorderedAccessViews to slot u1 since rtv defaults to u0). Then in the following dispatch that uses it as a SRV, I am calling CSSetUnorderedAccessViews with 2 other uav's, thinking that'll overwrite the ones from the previous draw call. Apparently not! I have to call OMSetRenderTargetsAndUnorderedAccessViews again with nulls before the dispatch otherwise it complains of double bind as srv/uav... I guess draw call and dispatch bind slots are separate??

You have to unbind from uav to srv because the driver may ( have to ) flush caches and set barriers to make sure your dispatch happen after everything from the draws is done and available to it.

If you enable the debug layer for your device (which you should *definitely* do for debug builds), you will get error messages when you have a resource simultaneously bound as both an input (SRV) and an output (RTV or UAV).

Sorry for very late response, but what I meant was this:

I have a resource with both uav & srv views, lets say uav0 & srv0, and I call things in following order:

OMSetRenderTargetsAndUnorderedAccessViews (rtv bound and uav0 bound to u1)
DrawIndexed
CSSetUnorderedAccessViews (some other uavs bound to u0 and u1) <------ this does not override the u1 binding above
CSSetShaderResources (srv0 bound to t0) <------- this fails since it complains resource already bound as uav
Dispatch

Hence it seems uav slots for dispatches and draws are not the same?

And you're right debug layer caught this, but I would have thought the CSSetUnorderedAccessViews call would have overridden u1 slot and essentially unbind the previous, guess not?..

That's correct, the set of UAV's bound for the CS stage is distinct from the set that's bound for rendering stages. You need to clear the rendering UAV bindings before you can bind the resource as an SRV.

This topic is closed to new replies.

Advertisement