I'm trying to code a particle system which uses compute shader. My goal is to load a fixed number of particles in the gpu and let them permanently there while the compute shader does the gravity simulation.
I have created a class that should handle everything related to the compute shader. It hast two buffers (source and destination) and for each buffer a UAV and for the destination buffer also a SRV. The compute shader just copies the particles from the source Buffer to the destination buffer in the compute shader and then sets the destination buffer's SRV as a shader resource to the vertex buffer. But somehow no operations on my particles in the compute shader seem to work.
EDIT:
I cut it down to the main problem:
I created to buffers (source and destination), with unordered acces view and as shader resources. Both have an UAV with the "D3D11_BUFFER_UAV_FLAG_APPEND" flag bound to them. The destination buffer also gets a SRV, so that i can bind it to the vertex shaders as a shader resource. The source buffer gets all the particles which are just XMFLOAT3 variable during its initialisation.
So I've got two Buffers and their UAV's and the destination's SRV.
ID3D11Buffer* _srcParticleBuffer;
ID3D11UnorderedAccessView* _srcParticleBufferViewUAV;
ID3D11Buffer* _dstParticleBuffer;
ID3D11ShaderResourceView* _dstParticleBufferView;
ID3D11UnorderedAccessView* _dstParticleBufferViewUAV;
Everytime I render the scene I call the following functions:
devContext->CSSetShader(_computeShader, 0, 0);
devContext->CSSetUnorderedAccessViews(0, 1, &_srcParticleBufferViewUAV, 0);
devContext->CSSetUnorderedAccessViews(1, 1, &_dstParticleBufferViewUAV, 0);
devContext->Dispatch(100, 1, 1);
devContext->CopyResource(_srcParticleBuffer, _dstParticleBuffer);
devContext->VSSetShaderResources(0, 1, &_dstParticleBufferView);
My compute shader looks like this:
struct Particle{
float3 pos;
};
ConsumeStructuredBuffer<Particle> srcParticleBuffer : register(u0);
AppendStructuredBuffer<Particle> dstParticleBuffer : register(u1);
[numthreads(100, 1, 1)]
void CS_Main( uint3 dispatchThreadID : SV_DispatchThreadID )
{
Particle p = srcParticleBuffer.Consume();
p.pos.z = 5.0f;
dstParticleBuffer.Append(p);
}
And my vertex shader:
struct VS_INPUT{
uint vertexID : SV_VertexID;
};
struct Particle{
float3 pos : POSITION;
};
StructuredBuffer<Particle> particleBuffer : register(u0);
GS_INPUT VS_Main( VS_INPUT input){
GS_INPUT output = (GS_INPUT)0;
output.pos.xyz = particleBuffer[input.vertexID].pos;
output.pos.w = 1.0f;
output.tex0 = float2(0,0);
return output;
}
But somehow nothing happens. All my particles are created at the origin although they are supposed to be created at (0,0,5). How can I use the output buffer (destination buffer) as a valid shader resource which has all my particle positions to create the vertices in the vertex shader?