My code worked. Firstly, I create a buffer to get buffer from compute shader by CopyResource.
Then I create a pointer call p1 and get result pointer from MappedResource1. After that, I create vertex buffer and render.
But I don't think it's the best way to get vertex buffer from compute shader:
D3D11_BUFFER_DESC desc;
ZeroMemory(&desc, sizeof(desc));
mFirstBuffer->GetDesc(&desc);
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.MiscFlags = 0;
if (SUCCEEDED(device->CreateBuffer(&desc, nullptr, &debugbuf)))
{
context->CopyResource(debugbuf, mFirstBuffer.Get());
}
unsigned int stride = sizeof(InstanceData);
unsigned int offset = 0;
D3D11_MAPPED_SUBRESOURCE MappedResource1;
InstanceData *p1;
context->Map(debugbuf, 0, D3D11_MAP_READ, 0, &MappedResource1);
p1 = (InstanceData*)MappedResource1.pData;
context->Unmap(debugbuf, 0);
D3D11_SUBRESOURCE_DATA VertexBufferData;
VertexBufferData.pSysMem = p1;
VertexBufferData.SysMemPitch = 0;
VertexBufferData.SysMemSlicePitch = 0;
D3D11_BUFFER_DESC desc2;
desc2.ByteWidth = sizeof(InstanceData) * MillionParticleCount;
desc2.Usage = D3D11_USAGE_DEFAULT;
desc2.BindFlags = D3D11_BIND_VERTEX_BUFFER;
desc2.CPUAccessFlags = 0;
desc2.MiscFlags = 0;
desc2.StructureByteStride = 0;
device->CreateBuffer(&desc2, &VertexBufferData, &vertexBuffer);
context->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
I have read many sample, all of them write compute shader and vertex shader together in only one file and therefor they can get result from compute shader directly.
In my code, compute shader and vertex shader are write in different files. How should I do to optimize my code?