This C++ class is used to write elements into vertex buffer(s) based on a Direct3D 11 input layout description. Data is provided as arrays by individual semantics (i.e. POSITION or NORMALS).

Note: This class supports multi-stream vertex buffer descriptions, but does not support instancing vertex buffers.

Initialization

The writer is initialized by providing a input layout description for the vertex buffer data, and then one or more memory buffers for the output streams.

std::unique_ptr<VBWriter> writer( new VBWriter() );

D3D11_INPUT_ELEMENT_DESC layout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

struct Vertex
{
    XMFLOAT3 Pos;
    XMFLOAT2 Tex;
};

if ( FAILED( writer->Initialize( layout, 2 ) ) )
   // Error

std::unique_ptr<uint8_t[]> vb( new uint8_t[ sizeof(Vertex) * nVerts ] );

memset( vb.get(), 0, sizeof(Vertex) * nVerts );

if ( FAILED( writer->AddStream( vb.get(), nVerts, 0, sizeof(Vertex) ) ) )
   // Error

Note that AddStream can accept a stride of 0 which means to use the stride implied by the input layout description.

Inserting vertex data

The writer is designed to insert an array of XMVECTOR values given the layout semantic name and semantic index.

struct aligned_deleter { void operator()(void* p) { _aligned_free(p); } };

std::unique_ptr<XMVECTOR[], aligned_deleter> buff(
    reinterpret_cast<XMVECTOR*>( _aligned_malloc( sizeof(XMVECTOR) * nVerts, 16 ) ) );

// Store position data into XMVECTOR buff array

if ( FAILED( writer->Write( buff.get(), "POSITION", 0, nVerts ) ) )
   // Error

To simplify implementation of applications, the writer will accept either POSITION or SV_Position as an alias for the actual semantic name provided in the layout.

Further Reading

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647.aspx

Last edited Nov 20, 2014 at 6:55 PM by walbourn, version 6