Graphics Reference
In-Depth Information
ten parameters to the buffer between every draw call, just to update the two varying pa-
rameters, since the entire buffer is always completely updated. Depending on the number
of objects to render, these additional parameter updates can add up to a large amount of
unnecessarily wasted bandwidth. If we instead create two constant buffers, where one of
them holds the eight static parameters and the second one holds the two dynamic param-
eters, we can update only the changing parameters and significantly reduce the volume of
the required data update for each frame. However, we would need to take care to ensure
that the buffers are only updated when they really need to be!
Another potential reduction in updating constant buffers comes from the fact that
these buffers only support read accesses from the shader programs. Since it is read-only,
a single constant buffer can be simultaneously bound to multiple locations in the pipeline,
without the possibility of causing memory access conflicts.
Creating constant buffers. Constant buffers also provide a number of different resource
configurations for the best possible performance. Depending on how frequently a constant
buffer will be updated by the CPU, one of two typical configurations is chosen. If constant
buffers are updated many times throughout an application's lifetime, a dynamic buffer
resource makes the most sense. Of course, if a constant buffer will contain some data
that will not change throughout an application (such as a fixed back buffer size) it would
be more efficient to create it as a completely static buffer, with immutable usage flags.
Listing 2.8 demonstrates how a constant buffer is typically created.
ID3DllBuffer* CreateConstantBuffer( UINT size,
bool dynamic,
bool CPUupdates,
D3D11_SUBRES0URCE_DATA* pData )
{
D3D11_BUFFER_DESC desc;
desc.ByteWidth = size;
desc.HiscFlags = 0;
desc.StructureByteStride = 0;
desc.BindFlags = D3Dll_BIND_C0NSTANT_BUFFERj
// Select the appropriate usage and CPU access flags based on the passed
// in flags
if ( dynamic && CPUupdates )
{
desc. Usage = D3D11_USAGE_DYNAMIC;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
}
else if ( dynamic && !CPUupdates )
{
desc.Usage = D3D11_USAGE_DEFAULT;
desc.CPUAccessFlags = 0;
}
else
Search WWH ::




Custom Search