UAV write, SRV read problem

Started by
1 comment, last by zeo4 8 years, 2 months ago

Hi,

I have a small question. In my code I use a compute shader writing to a buffer, and right after it, another compute shader reading from the buffer.

The problem is that in the second compute shader, when I read using SRV and StructuredBuffer I get zeros, but if I read using UAV and RWStructuredBuffer I get correct values. Here's the code. Take a look at "Worlds" buffer.

CPP


uint32_t _InitCounts = -1;

Context->CSSetShader( CS1, 0, 0 );
Context->CSSetUnorderedAccessViews( 0, 1, &PositionsUAV, &_InitCounts );
Context->CSSetUnorderedAccessViews( 1, 1, &VelocitiesUAV, &_InitCounts );
Context->CSSetUnorderedAccessViews( 2, 1, &WorldsUAV, &_InitCounts );
Context->Dispatch( 1, 1, 1 );

Context->CSSetShader( CS2, 0, 0 );
Context->CSSetShaderResources( 0, 1, &WorldsSRV );
// Context->CSSetShaderResources( 1, 1, &WorldsUAV, &_InitCounts ); <-- alternative, working case
Context->CSSetConstantBuffers( 0, 1, &ViewProjection );
Context->CSSetUnorderedAccessViews( 0, 1, &WVPsUAV, &_InitCounts );
Context->Dispatch( 1, 1, 1 );

CS1


RWStructuredBuffer<float3> Positions : register( u0 );
RWStructuredBuffer<float3> Velocities : register( u1 );
RWStructuredBuffer<float4x4> Worlds : register( u2 );

[ numthreads( 32, 1, 1 ) ]
void Main( uint3 Index : SV_DispatchThreadID )
{
	Positions[ Index.x ] += Velocities[ Index.x ];
	Velocities[ Index.x ] = float3( 0, 0, 0 );
	Worlds[ Index.x ] = float4x4
	(
		1,			0,			0,			0,
		0,			1,			0,			0,
		0,			0,			1,			0,
		Positions[ Index.x ].x, Positions[ Index.x ].y, Positions[ Index.x ].z, 1
	);

CS2


cbuffer View : register( b0 )
{
	float4x4 ViewProjection;
}
StructuredBuffer<float4x4> Worlds : register( t0 );
// RWStructuredBuffer<float4x4> Worlds : register( u1 ); <-- alternative, working case
RWStructuredBuffer<float4x4> WVPs : register( u0 );

[ numthreads( 128, 1, 1 ) ]
void Main( uint3 Index : SV_DispatchThreadID )
{
	WVPs[ Index.x ] = mul( Worlds[ Index.x ], ViewProjection ); // Worlds[ Index.x ] returns zeros!!!
}
Advertisement

// Context->CSSetShaderResources( 1, 1, &WorldsUAV, &_InitCounts ); <-- alternative, working case

I'm not sure how the above could possibly be working given that you're calling CSSetShaderResources using a UAV, you would have had to call CSSetUnorderedAccessViews for it to work.

This leads me to believe you probably haven't tried turning on the D3D11 Debug Layer. My guess would be that this is an issue with having the StructuredBuffer bound as a UAV and an SRV at the same time, which is probably (I can't recall for sure) a validation issue (you hadn't unbound WorldsUAV before trying to bind WorldsSRV). Try the debug layer in your "not working" case and see if it runs clean.

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

Yes, you're right. I use CSSetUnorderedAccessViews and not CSSetShaderResources.

Yes, you're right. That was a problem of an unbound resource at first compute shader, and yes debug layer reports that. I didn't look at it.

I'm such a noob. Sorry and thanks!

This topic is closed to new replies.

Advertisement