Reputation: 461
So, in my DirectX application I am collecting various buffers for processing. In my small code snippet, I am using Map() and memcpy() to copy the resource (vertex buffer in this case) to system memory. Which appears to work with no issues.
I'd like to access some of the vertices in the buffers (in vert). To do so I've been told I need to cast from type void to the appropriate vertex structure. Which I tried to express in a generic statement below... From what I understand though, I will need to know the buffers vertex structure. Which I do not have readily available.
For the sake of simplicity I'm wondering if I can query the vertex structure from the returned buffers. The buffers in question are randomly generated, and it would be easier for me to get the structures from here if possible.
Is there a way for me to query this structure from a buffer?
pContext->IAGetVertexBuffers(0, 1, &veBuffer, &Stride, &veBufferOffset);
if (veBuffer)
veBuffer->GetDesc(&vedesc);
D3D11_MAPPED_SUBRESOURCE mapped_rsrc;
HRESULT = hrm pContext->Map(readVB, NULL, D3D11_MAP_READ, NULL, &mapped_rsrc);
assert(hrm == S_OK);
void* vert = new BYTE[mapped_rsrc.DepthPitch];
memcpy(vert, mapped_rsrc.pData, mapped_rsrc.DepthPitch);
pContext->unmap(readVB, 0);
//I'd like to access vertices here...
VertexType* vert = static_cast<VertexType*>(mapped_rsrc.pdata);
Edit: After reading through this question: (DirectX 11) Dynamic Vertex/Index Buffers implementation with constant scene content changes
I'm thinking that maybe using GetDesc() should return whats needed to describe the buffers structure?
Upvotes: 1
Views: 71
Reputation: 41127
The binary layout of your Vertex Buffer is determined by your active Input Layout. It's up to the application to track that information along with your vertex stride as the DirectX runtime doesn't have any specific linkage between Input Layout and VBs.
All the GetDesc
can tell you is the total size in bytes. See D3D11_BUFFER_DESC
While intended for content tools and not for runtime use, DirectXMesh contains code for reading & writing individual data elements for Vertex Buffers based on the input layout. See
VBReader
/VBWriter
.
Upvotes: 1