DirectX Graphics API Reference
HRESULT CreateBuffer( const D3D11_BUFFER_DESC *pDesc, const D3D11_SUBRESOURCE_DATA *pInitialData, ID3D11Buffer **ppBuffer );
The D3D11_BUFFER_DESC structure specifies the buffer's usage, memory access, and size. For example, you can create a vertex buffer by setting the Usage member to D3D11_USAGE_DEFAULT and the BindFlags member to D3D11_BIND_VERTEX_BUFFER.
The D3D11_SUBRESOURCE_DATA structure provides the initial data for the buffer. If pInitialData is NULL, the buffer will be created without any initial data. This is useful for dynamic buffers that will be updated frequently.
The CreateBuffer method creates a buffer resource, which can be used for various purposes in Direct3D, such as storing vertex data, index data, constant data, or stream output. The type of buffer created is determined by the BindFlags member of the D3D11_BUFFER_DESC structure.
// Define the buffer description D3D11_BUFFER_DESC bufferDesc; ZeroMemory(&bufferDesc, sizeof(bufferDesc)); bufferDesc.Usage = D3D11_USAGE_DEFAULT; bufferDesc.ByteWidth = sizeof( MyVertexStructure ) * NUM_VERTICES; bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.CPUAccessFlags = 0; // Define the initial data (optional) D3D11_SUBRESOURCE_DATA initData; ZeroMemory(&initData, sizeof(initData)); initData.pSysMem = new MyVertexStructure[NUM_VERTICES]; // Populate initData.pSysMem with vertex data ID3D11Buffer *pVertexBuffer = nullptr; HRESULT hr; // Create the buffer hr = pDevice->CreateBuffer(&bufferDesc, &initData, &pVertexBuffer); if( FAILED(hr) ) { // Handle error } // Clean up initial data if it was allocated on the heap delete[] initData.pSysMem;
Minimum supported client | Windows 7 |
Minimum supported server | Windows Server 2008 R2 |
Header | d3d11.h |
Library | d3d11.lib |
DLL | d3d11.dll |