The Task is to compute the lengths of 64 3D Vectors in ComputeShader using a ConsumeStructuredBuffer as Input and AppendStructuredBuffer as Output.
I always get the result 0 for the length. Even if i try to get the content of the ConsumeStructuredBuffer in the shader i always get 0. I checked the Dimensions of the ConsumeBuffer and the AppendBuffer in Shader and it gets me the correct results.
Here is the code:
struct InputData
{
float3 data;
};
struct OutputData
{
float data;
};
ConsumeStructuredBuffer<InputData> gInput : register(u0);
AppendStructuredBuffer<OutputData> gOutput : register(u1);
[numthreads(64, 1, 1)]
void CS(int3 dtid : SV_DispatchThreadID)
{
OutputData output;
InputData input = gInput.Consume();
float len = length(input.data);
output.data = len;
gOutput.Append(output);
}
technique11 VecAdd
{
pass P0
{
SetVertexShader( NULL );
SetPixelShader( NULL );
SetComputeShader( CompileShader(cs_5_0, CS() ) );
}
}
And here ist the Code which created and sets the Buffers in C++:
struct InputData
{
//XMFLOAT3 data;
float data[3];
};
struct OutputData
{
float data;
};
std::vector<InputData> vecbuffer(64);
// Initialize the vecbuffer with values [1,10]
for(UINT i = 0; i < vecbuffer.size(); ++i)
{
vecbuffer.data[0] = MathHelper::RandF(1,10);
vecbuffer.data[1] = MathHelper::RandF(1,10);
vecbuffer.data[2] = MathHelper::RandF(1,10);
}
mNumElements = vecbuffer.size();
// Create a buffer to be bound as a shader input
D3D11_BUFFER_DESC inputDesc;
inputDesc.Usage = D3D11_USAGE_DEFAULT;
inputDesc.ByteWidth = sizeof(InputData) * mNumElements;
inputDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
inputDesc.CPUAccessFlags = 0;
inputDesc.StructureByteStride = sizeof(InputData);
inputDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
D3D11_SUBRESOURCE_DATA vinitInputData;
vinitInputData.pSysMem = &vecbuffer[0];
ID3D11Buffer* inputBuffer = 0;
HR(md3dDevice->CreateBuffer(&inputDesc, &vinitInputData, &inputBuffer));
// Create a buffer the compute shader can write to
D3D11_BUFFER_DESC outputDesc;
outputDesc.Usage = D3D11_USAGE_DEFAULT;
outputDesc.ByteWidth = sizeof(OutputData) * mNumElements;
outputDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
outputDesc.CPUAccessFlags = 0;
outputDesc.StructureByteStride = sizeof(OutputData);
outputDesc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
HR(md3dDevice->CreateBuffer(&outputDesc, 0, &mOutputBuffer));
D3D11_UNORDERED_ACCESS_VIEW_DESC uavDesc;
uavDesc.Format = DXGI_FORMAT_UNKNOWN;
uavDesc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
uavDesc.Buffer.FirstElement = 0;
uavDesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_APPEND;
uavDesc.Buffer.NumElements = mNumElements;
HR(md3dDevice->CreateUnorderedAccessView(inputBuffer, &uavDesc, &mInputUAV));
HR(md3dDevice->CreateUnorderedAccessView(mOutputBuffer, &uavDesc, &mOutputUAV));
Has someone a solution or sees and error? I already tried the BindFlags with only D3D11_BIND_UNORDERED_ACCESS but its the same result.