Buffers store data that are used by shaders.
Create Buffer - ID3D11Device::CreateBuffer
A buffer is created by filling in a D3D_BUFFER_DESC and a D3D11_SUBRESOURCE_DATA structure and then calling ID3D11Device::CreateBuffer. The first buffer describe the settings for the buffer to create and the other one specifies the data to put in the buffer.
Identify how the buffer will be bound to the pipeline. The flags can be combined with a logical OR but using a single flag only when possible is best for performence.
Flags that set if and how the buffer can be accessed by the CPU. Can be combined with a logical OR. No flags set enables the best performance use of the resource.
D3D11_CPU_ACCESS_WRITE CPU can map the resource to write to it. Must be crated with dynamic or staging usage.
D3D11_CPU_ACCESS_READ CPU can map the resource to read from it. Must be created with staging usage.
Describe the frequency of how the buffer is expected to be read and written to.
D3D11_USAGE_DEFAULT A resource that the GPU can read and write.
D3D11_USAGE_IMMUTABLE The resource can only be read by the GPU. It can not be written by the GPU or accessed by the CPU at all. It must be initalized when it is created.
D3D11_USAGE_DYNAMIC The resource can be read by the GPU and written by the CPU. Good choice for something that changes at least once per frame.
D3D11_USAGE_STAGING The resource supports data copy from the GPU to the CPU.
Create vertex input layout - ID3D11Device::CreateInputLayout
To provide the GPU with vertex data one give it a vertex buffer. The vertex input layout tell the GPU what vertex attribute is where in the buffer so the GPU can extract them and give them to the shaders. To make a input layout fill in a array of D3D11_INPUT_ELEMENT_DESC and then call ID3D11Device::CreateInputLayout.
- SemanticName: The name of this attribute in the shader.
- SemanticIndex: Makes it possible to use the same name with different indices. Ex TexCoord0 and TexCoord1.
- InputSlot: It is possible to read from more then one vertex buffer. Each vertex buffer is assigned to a slot (0-n) and this select what slot to read the attribute from.
- AlignedByteOffset: How many bytes from the start of the vertex data this attribute starts. Use D3D12_APPEND_ALIGNED_ELEMENT to calculate this automatically from the previous element.
Select the input layout to use - ID3D11DeviceContext:IASetInputLayout
Use ID3D11DeviceContext:IASetInputLayout to select the input layout to use to pull data from the bound vertex buffers.
Using the Buffer
To set one or more vertex buffers to use call ID3D11DeviceContext:IASetVertexBuffers. To select the index buffer call ID3D11DeviceContext:IASetIndexBuffer.