Sign in to follow this  
AmrNasser

writing to UAV buffer

Recommended Posts

I am trying to make a buffer to store object index then read it from CPU to implement mouse picking algorithm, but I don't know how to write to UAV buffer from a pixel shader. I coped tutorial 4 and modified it,when I add the first line in PS function the code doesn't compile.

Can anyone tell me what is the problem.

Thank you in advance.


//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer ConstantBuffer : register( b0 )
{
	matrix World;
	matrix View;
	matrix Projection;
}

// dynamic 
RWTexture2D<uint> DynamicBuffer : register(u0);


//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
	uint  id : ID;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR )
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Pos = mul( Pos, World );
    output.Pos = mul( output.Pos, View );
    output.Pos = mul( output.Pos, Projection );
    output.Color = Color;
    return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 PS( VS_OUTPUT input ) : SV_Target
{
    DynamicBuffer[uint2(input.Pos.xy)] = float(input.id); // error
    return input.Color;
}

Share this post


Link to post
Share on other sites

the code doesn't compile.


What's the error? I'm guessing that it's telling you that your UAV is bound to a register that overlaps with your render target, but I'd rather not guess. smile.png

For pixel shaders, UAV's use the same slots as render target views. As explained by the docs for OMSetRenderTargetsAndUnorderedAccessViews, you'll need to bind your render target to slot 0 and your UAV to slot 1.

Share this post


Link to post
Share on other sites

thanks for reply 

the function CompileShaderFromFile() returns E-error when called for pixel shader.

the error only occurs when I add the first line in pixel shader function "DynamicBuffer[uint2(input.Pos.xy)] = float(input.id);",when I comment this line out the file compiles normally.

Share this post


Link to post
Share on other sites

the function CompileShaderFromFile() returns E-error when called for pixel shader.


What's the actual compilation error? When compilation fails, the D3D shader compilation functions will return an ID3DBlob that contains a string with the compilation error(s) and warnings. It will look like the message that ajmiles posted above, which he probably got by compiling your code and extracting the error message.

Share this post


Link to post
Share on other sites

Within each shader model, there are several shader targets -- e.g. within SM4, there's cs_4_0, gs_4_0, ps_4_0, vs_4_0, cs_4_1, gs_4_1, ps_4_1, vs_4_1, lib_4_0, lib_4_1.

 

The RWTexture2D page shows it requires SM5; SM4 can only use RWStructuredBuffer and RWByteAddressBuffer UAV types.

 

However, SM4 pixel shaders do not support UAV's whatsoever, and SM4 compute shaders only support a single UAV.

So, to implement your algorithm, you'll have to move to SM5.

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