How to: Create a Constant 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.
1 out of 2 rated this helpful - Rate this topic

Constant buffers contain shader constant data. This topic shows how to initialize a constant buffer in preparation for rendering.

Ff476896.wedge(en-us,VS.85).gifTo initialize a constant buffer

  1. Define a structure that describes the vertex shader constant data.
  2. Allocate memory for the structure that you defined in step one. Fill this buffer with vertex shader constant data. You can use malloc or new to allocate the memory, or you can allocate memory for the structure from the stack.
  3. Create a buffer description by filling in a D3D11_BUFFER_DESC structure. Pass the D3D11_BIND_CONSTANT_BUFFER flag to the BindFlags member and pass the size of the constant buffer description structure in bytes to the ByteWidth member.

    Note  The D3D11_BIND_CONSTANT_BUFFER flag cannot be combined with any other flags.

  4. Create a subresource data description by filling in a D3D11_SUBRESOURCE_DATA structure. The pSysMem member of the D3D11_SUBRESOURCE_DATA structure must point directly to the vertex shader constant data that you created in step two.
  5. 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 a constant buffer. This example assumes that g_pd3dDevice is a valid ID3D11Device object and that g_pd3dContext is a valid ID3D11DeviceContext object.



ID3D11Buffer*   g_pConstantBuffer11 = NULL;

// Define the constant data used to communicate with shaders.
struct VS_CONSTANT_BUFFER
{
    D3DXMATRIX mWorldViewProj;                              
    D3DXVECTOR4 vSomeVectorThatMayBeNeededByASpecificShader;
    float fSomeFloatThatMayBeNeededByASpecificShader;
    float fTime;                                            
    float fSomeFloatThatMayBeNeededByASpecificShader2;
    float fSomeFloatThatMayBeNeededByASpecificShader3;
} VS_CONSTANT_BUFFER;

// Supply the vertex shader constant data.
VS_CONSTANT_BUFFER VsConstData;
VsConstData.mWorldViewProj = {...};
VsConstData.vSomeVectorThatMayBeNeededByASpecificShader = D3DXVECTOR4(1,2,3,4);
VsConstData.fSomeFloatThatMayBeNeededByASpecificShader = 3.0f;
VsConstData.fTime = 1.0f;
VsConstData.fSomeFloatThatMayBeNeededByASpecificShader2 = 2.0f;
VsConstData.fSomeFloatThatMayBeNeededByASpecificShader3 = 4.0f;

// Fill in a buffer description.
D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = sizeof( VS_CONSTANT_BUFFER );
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;

// Fill in the subresource data.
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = &VsConstData;
InitData.SysMemPitch = 0;
InitData.SysMemSlicePitch = 0;

// Create the buffer.
hr = g_pd3dDevice->CreateBuffer( &cbDesc, &InitData, 
                                 &g_pConstantBuffer11 );

if( FAILED( hr ) )
   return hr;

// Set the buffer.
g_pd3dContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer11 );
    

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.