How to: Create an Index Buffer

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

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

Ff476897.wedge(en-us,VS.85).gifTo 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 );
    

Related topics

Buffers

 

 

Send comments about this topic to Microsoft

Build date: 11/28/2012

Did you find this helpful?
(1500 characters remaining)

Community Additions

© 2013 Microsoft. All rights reserved.