Graphics Reference
In-Depth Information
{ desc.Usage = D3D11_USAGE_IMMUTABLE;
desc.CPUAccessFlags = 0;
}
// Create the buffer with the specified configuration
ID3D11Buffer* pBuffer = 0;
HRESULT hr = g_pDevice->CreateBuffer( &desc, pData, &pBuffer );
if ( FAILED( hr ) )
{
// Handle the error here...
return( 0 );
}
return( pBuffer );
}
Listing 2.8. A method for creating a constant buffer depending on its intended usage.
As with vertex and index buffers, the creation of dynamic constant buffers uses the
D3D11_USAGE_DYNAMIC usage flag, in combination with the D3D11_CPU_ACCESS_WRITE
flag, to allow the CPU to update the resource at runtime. For static contents, the D3D11_
USAGE_IMMUTABLE usage is used without any CPU access flags. One additional possibility
is to have a buffer that is updated at runtime only by the GPU, such as when copying the
number of elements in an Append/Consume buffer to a constant buffer with the ID3D11
DeviceContext: :CopyStructureCount() method. In this case, we would require a de-
fault usage flag, but we would not set any of the CPU access flags. This allows the runtime
to optimize the created resource for use on the GPU only.
There are several items of interest that are not shown in Listing 2.8. The first thing to
notice is that the application typically defines a C++ structure that mirrors the desired con-
tents of the constant buffer in HLSL. This allows the application updates to be applied to a
system memory instance of this structure, which can then be directly copied into the buffer.
We will explore the methods of updating the contents of resources later in this chapter. The
second point of interest is that a constant buffer must be created with a ByteWidth that is a
multiple of 16 bytes. This requirement allows for efficient processing of the buffer with the
4-tuple register types of the GPU, and it only appears as a requirement for constant buffers.
This must be accounted for in the structure declaration within C/C++. The third interesting
point is that the buffer description is not allowed to contain any other bind flags than the
D3D11_BIND_C0NSTAI\IT_BUFFER. Once again, in practice this is not really a big restriction,
since there typically are no situations where it would be desirable to bind a constant buffer
to another location in the pipeline.
Resource view requirements. Although constant buffers are bound to the programmable
shader stages and are accessible through HLSL, their contents are not interpreted with
Search WWH ::




Custom Search