Sign in to follow this  

UAV write, SRV read problem

This topic is 676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
// 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.

Share this post


Link to post
Share on other sites

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!

Edited by zeo4

Share this post


Link to post
Share on other sites

This topic is 676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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