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