writing to UAV buffer

Started by
8 comments, last by AmrNasser 8 years, 5 months ago

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;
}

Advertisement

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.

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.

"error X4509: UAV registers live in the same name space as outputs, so they must be bound to at least u1, manual bind to slot u0 failed"

As MJP suggested, move the UAV to u1.

Adam Miles - Principal Software Development Engineer - Microsoft Xbox Advanced Technology Group

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.

changed u0 to u1 and got this error

error X4509: maximum UAV register index exceeded, target has 0 slots, manual bind to slot u1 failed

What shader target are you compiling the code with when it gives that error?

i'm new to shader programming, but if you mean shader model, its shader model 4.0.

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.

thank you,It worked

changed target to ps_5_0

thank you all

This topic is closed to new replies.

Advertisement