rate up
1
rate down
DirectX 12 Storing vertex data in separate buffers
In tutorial DirectX 12 - 05. Adding Color, for vertex data struct is used which contain both position and color data struct Vertex { Vertex(float x, float y, float z, float r, float g, float b, float a) : pos(x, y, z), color(r, g, b, z) {} XMFLOAT3 pos; XMFLOAT4 color; }; then set input layout D3D12_INPUT_LAYOUT_DESC inputLayoutDesc = {}; inputLayoutDesc.NumElements = sizeof(inputLayout) / sizeof(D3D12_INPUT_ELEMENT_DESC); inputLayoutDesc.pInputElementDescs = inputLayout; fill vertex buffer view descriptor vertexBufferView.BufferLocation = vertexBuffer->GetGPUVirtualAddress(); vertexBufferView.StrideInBytes = sizeof(Vertex); vertexBufferView.SizeInBytes = vBufferSize; and set the buffer descriptor to command list via commandList->IASetVertexBuffers(0, 1, &vertexBufferView); And I have one question - Is it possible to have this data in two separate buffers, and if yes how to set both of this buffers to command list? Thank you!
Chosen Answer
rate up
0
rate down
Hi nameless323! I'm glad you asked that question. It is very possible and very useful. It's a must for instancing. Anyway, this is how you do it. The first thing you will need to change, is the **Input Slot** for the input element you wish to store in another buffer: // Assuming you have vertex positions in one buffer, and vertex colors in a second buffer // The vertex positions will be put into the "first" buffer, with the input slot set to "0" // The vertex colors will be put into the "second" buffer, with the input slot set to "1" D3D11_INPUT_ELEMENT_DESC vertexDesc[] = { // data in first buffer { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // data in second buffer { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; In the above code, notice for the "COLOR" element, the 5th parameter (AlignedByteOffset) is 0, the same as "POSITION". Just wanted to make sure that's clear, since "COLOR" is in a separate buffer (input slot) than "POSITION", their aligned byte offsets are relative to their own buffer. Now create your buffers. Once you are ready to draw, all you do is provide arrays when binding the vertex buffer instead of pointers to a single buffer, offset, and stride, like this: // vertexPositionBuffer and vertexColorBuffer are defined like: ID3D11Buffer* vertexPositionBuffer; ID3D11Buffer* vertexColorBuffer ; // create the buffers... ID3D11Buffer* vertBuffers[2] = { vertexPositionBuffer, vertexColorBuffer }; UINT strides[2] = { sizeof(VertexPosition), sizeof(VertexColor) }; // where VertexPosition is probably a float3 and VertexColor is a float4 UINT offsets[2] = { 0, 0 }; GetD3DDeviceContext()->IASetVertexBuffers( 0, // start slot, i'll explain below 2, // number of buffers to bind vertBuffers, // your array of buffers strides, // array of strides offsets // array of offsets ); Another way instead of binding them both at the same time, is to use the start slot, so something like this: // bind vertex positions first UINT stride = sizeof( VertexPosition ); UINT offset = 0; GetD3DDeviceContext()->IASetVertexBuffers( 0, // bind to first slot (this is the first buffer) 1, // binding only one buffer here &vertexPositionBuffer, // reference to the vertex position buffer &stride, // reference to the stride &offset // reference to the offset ); // later, or right after, bind the second buffer UINT stride = sizeof( VertexColor ); UINT offset = 0; GetD3DDeviceContext()->IASetVertexBuffers( 1, // this is the second buffer, bind to slot 1 1, // again only binding 1 buffer &vertexColorBuffer, // reference to the vertex color buffer &stride, // reference to the stride &offset //reference to the offset ); That should be all you need
Comments
Hi, iedoc. Thank you for such a good answer!
on Jun 30 `16
nameless323
rate up
0
rate down
The interface in the answer is Dx11. What should I do with Dx12 interface?
Sign in to answer!