How to: Create an Index Buffer

Index buffers contain index data. This topic shows how to initialize an index buffer in preparation for rendering.

To initialize an index buffer

  1. Create a buffer that contains your index information.
  2. Create a buffer description by filling in a D3D11_BUFFER_DESC structure. Pass the D3D11_BIND_INDEX_BUFFER flag to the BindFlags member and pass the size of the buffer in bytes to the ByteWidth member.
  3. Create a subresource data description by filling in a D3D11_SUBRESOURCE_DATA structure. The pSysMem member should point directly to the index data created in step one.
  4. Call ID3D11Device::CreateBuffer while passing the D3D11_BUFFER_DESC structure, the D3D11_SUBRESOURCE_DATA structure, and the address of a pointer to the ID3D11Buffer interface to initialize.

The following code example demonstrates how to create an index buffer. This example assumes that

g_pd3dDevice

is a valid ID3D11Device object and that

g_pd3dContext

is a valid ID3D11DeviceContext object.

ID3D11Buffer *g_pIndexBuffer = NULL;

// Create indices.
unsigned int indices[] = { 0, 1, 2 };

// Fill in a buffer description.
D3D11_BUFFER_DESC bufferDesc;
bufferDesc.Usage           = D3D11_USAGE_DEFAULT;
bufferDesc.ByteWidth       = sizeof( unsigned int ) * 3;
bufferDesc.BindFlags       = D3D11_BIND_INDEX_BUFFER;
bufferDesc.CPUAccessFlags  = 0;
bufferDesc.MiscFlags       = 0;

// Define the resource data.
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = indices;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

// Create the buffer with the device.
hr = g_pd3dDevice->CreateBuffer( &bufferDesc, &InitData, &g_pIndexBuffer );
if( FAILED( hr ) )
    return hr;

// Set the buffer.
g_pd3dContext->IASetIndexBuffer( g_pIndexBuffer, DXGI_FORMAT_R32_UINT, 0 );
    

Buffers

How to Use Direct3D 11