This C++ class is used to read elements from vertex buffer(s) based on a Direct3D 11 input layout description. Data is extracted 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 reader is initialized by providing a input layout description for the vertex buffer data, and then one or more memory buffers for the input streams.

std::unique_ptr<VBReader> reader( new VBReader() );

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( reader->Initialize( layout, 2 ) ) )
   // Error

if ( FAILED( reader->AddStream( vbdata, 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.

Extracting vertex data

The reader is designed to extract 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 ) ) );

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

// Load position data from XMVECTOR buff array

To simplify implementation of applications, the reader 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:54 PM by walbourn, version 10