ID3D11Device::CreateBuffer

DirectX Graphics API Reference

Syntax

                HRESULT CreateBuffer(
                  const D3D11_BUFFER_DESC *pDesc,
                  const D3D11_SUBRESOURCE_DATA *pInitialData,
                  ID3D11Buffer **ppBuffer
                );
            

Parameters

Return Value

S_OK if the method succeeds.
E_OUTOFMEMORY if the method failed because it could not allocate memory.
Other error codes may be returned. For more information about Direct3D 11 Return Values, see Remarks.

Remarks

Buffer Description

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.

Initial Data

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.

Resource Creation

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.

Example Usage

// 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;
                

Requirements

Minimum supported client Windows 7
Minimum supported server Windows Server 2008 R2
Header d3d11.h
Library d3d11.lib
DLL d3d11.dll