RWStructuredBuffer read and write ?

Started by
3 comments, last by Alundra 10 years, 2 months ago

Hello I have a little question about the UAVs in directx. I read some explanations about UAVs but there are always 2 types of buffers for input and output in the examples. For my implementation I need a buffer that is only used in the shaders, so I use a pixel or compute shader to write to this buffer and in an other shader I want to read this data from it...so there is no need to manipulate the buffer in my c++ code.

The problem is I saw in the tutorials that there are used 2 types of buffers, the StructuredBuffer as a ShaderResource and a RWStructuredBuffer as a UAV ( for output). My question is : why they use 2 buffers ?? Isn't it possible to create in my c++ code the UAV -> set it to the pixelshader and that write in my first shader to it and then read it in my second pixel shader ??

Something like:


//Create UAV
ID3D11UnorderedAccessView* uav = ....
...
 m_pImmediateContext->PSSetUnorderedAccessViews( 0, 1, &m_destDataGPUBufferView, 
                                                    NULL );


Shader one:

struct VPL {
  float3 pos;
  float3 norm;
  float3 flux;
};

RWStructuredBuffer<VPL> buffer : register( u0 );

...

buffer[0].pos = float3(0.1f,0.5f,0.4f);



Shader two:
RWStructuredBuffer<VPL> buffer : register( u0 );

float3 pos = buffer[0].pos;

Advertisement

It's a bad name from DirectX, RWStructuredBuffer output and you set a StructuredBuffer to read.

Pixel shader can't bind a UAV, it has not a PSSetUnorderedAccessViews, pixel shader output to a texture.

It's important to notice that a SRV and a UAV must not be binded same time.

You can certainly read from a RWStructuredBuffer if you want to. In some cases it might make sense to read from one buffer and then write to the same buffer, but in many cases you want to read from one buffer and then write to a different one. This might be because the output structure is different from the input format, or the shader needs to write to a different location than it's reading from. In the latter case you don't want to have one thread writing to an element that a different thread is reading from, since this will lead to race conditions. So the simplest way to handle it is to read from one buffer as a StructuredBuffer, and then write to another as a RWStructuredBuffer.

It's a bad name from DirectX, RWStructuredBuffer output and you set a StructuredBuffer to read.

Pixel shader can't bind a UAV, it has not a PSSetUnorderedAccessViews, pixel shader output to a texture.

It's important to notice that a SRV and a UAV must not be binded same time.

I'm not sure if you're implying that a RWStructuredBuffer can't be read from, since that's not the case as I explained above. Also you certainly can bind a UAV to a pixel shader, however it's done through ID3D11DeviceContext::OMSetRenderTargetsAndUnorderedAccessViews.

And as of DX 11.1 (or DX 11.2?) you can also bind UAVs to ALL other shader stages (vertex, hull, ...). When I was looking into this, however, I found a great lack of examples of how to do this exactly. I mean examples of C++ and HLSL showing both SRVs, RTVs and UAVs of all kinds together in a pixel shader. But I guess you'll figure out according to the debug layer error messages :)

Also you certainly can bind a UAV to a pixel shader, however it's done through ID3D11DeviceContext::OMSetRenderTargetsAndUnorderedAccessViews.

Didn't knew that, thanks for the info.

And as of DX 11.1 (or DX 11.2?) you can also bind UAVs to ALL other shader stages (vertex, hull, ...).

It's good, like that all is on the same way, no need to find another way to do the same thing, nice update.

This topic is closed to new replies.

Advertisement