Game Development Reference
In-Depth Information
Constant buffers
We have vertices and indices in place, and the matrices from the camera are ready,
but how do we get those matrices up to the GPU? The answer is another buffer! This
time we use a constant buffer to define constants for the shader stage that we will
cover in the next section.
A constant buffer is a simple block of data that can be updated as required and trans-
ferred to the GPU. Creating the constant buffer is pretty much the same as the vertex
or index buffer; however, you want to ensure you have CPU write access and the
bind flag set to D3D11_BIND_CONSTANT_BUFFER . Using D3D11_USAGE_DYNAMIC
for the usage flag is recommended here, as you will probably be updating this buffer
each frame. You can avoid the sync cost by reusing the buffer as much as possible.
When setting up the shaders in the next stage, use the following commands to set a
constant buffer on the vertex shader and pixel shader, respectively:
ID3D11Buffer *buffer;
// ... setup your constant buffer
m_d3dContext->VSSetConstantBuffers(0, 1,
&buffer);
m_d3dContext->PSSetConstantBuffers(0, 1,
&buffer);
Updating the buffers
Now that you know how to create and set the buffers, you need to know how to up-
date them so they have the latest data. In many cases you won't need to do this;
however, items such as constant buffers may require updating each frame.
First you need to ensure you've chosen the right usage type and CPU access flags
during creation to ensure that you are allowed to update the buffer. Along with that
it is important to remember that a usage type of DYNAMIC only allows for the use of
the Map() method for updating.
Search WWH ::




Custom Search