Skip to content

VBReader

Chuck Walbourn edited this page Apr 27, 2022 · 12 revisions
DirectXMesh

This C++ class is used to read elements from vertex buffer(s) based on a Direct3D input layout description. Data is extracted by individual semantics (i.e. POSITION or NORMALS).

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

Header

#include "DirectXMesh.h"

By default, the library supports Direct3D 11. If you want to support Direct3D 12, then you need to #include <d3d12.h> before you do #include "DirectXMesh.h".

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.

auto reader = std::make_unique<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

// For Direct3D 12, use:
//    D3D12_INPUT_LAYOUT_DESC desc = { layout, 2 };
//    if ( FAILED( reader->Initialize( desc ) ) )

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.

Biased data

The Read method takes an optional bool x2bias that defaults to false. If set to true, then DXGI_FORMAT_x_UNORM and DXGI_FORMAT_R11G11B10_FLOAT values are scaled so that the 0 to 1 range becomes -1 to 1. Typically this is used to store normals in compressed formats such as DXGI_FORMAT_R10G10B10A2_UNORM.

Accessors

The GetElement11 method can be used to obtain the original Direct3D 11 input layout element from the semantic information.

The GetElement12 method can be used to obtain the original Direct3D 12 input layout element from the semantic information.

Further reading

Semantics

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Windows 7 Service Pack 1
  • Xbox One
  • Xbox Series X|S
  • Windows Subsystem for Linux

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • GCC 10.5, 11.4, 12.3
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 11

DirectX Tool Kit for DirectX 12

DirectXTex

DirectXMath

Tools

Test Suite

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally