DirectXMesh |
---|
This C++ class is used to write elements into vertex buffer(s) based on a Direct3D input layout description. Data is provided as arrays by individual semantics (i.e. POSITION
or NORMALS
).
This can be used to 'edit' an existing Vertex Buffer by overwriting some or all of the elements of the layout, or to create a new vertex buffer interleaving and encoding the data for use with Direct3D.
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 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.
auto writer = std::make_unique<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
// For Direct3D 12, use:
// D3D12_INPUT_LAYOUT_DESC desc = { layout, 2 };
// if ( FAILED( reader->Initialize( desc ) ) )
auto vb = std::make_unique<uint8_t[]>(sizeof(Vertex) * nVerts);
// We are creating a new VB, so a good practice would be to zero-fill it.
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 ) ) );
std::unique_ptr<XMVECTOR[], aligned_deleter> buff2(
reinterpret_cast<XMVECTOR*>( _aligned_malloc( sizeof(XMVECTOR) * nVerts, 16 ) ) );
// Store position data into XMVECTOR buff array
// Store texcoord data into XMVECTOR buff2 array
if ( FAILED( writer->Write( buff.get(), "POSITION", 0, nVerts ) ) )
// Error
if ( FAILED( writer->Write( buff2.get(), "TEXCOORD", 0, nVerts ) ) )
// Error
To simplify implementation of applications, the writer will accept either
POSITION
orSV_Position
as an alias for the actual semantic name provided in the layout.
Biased data
The Write
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 -1 to 1 range becomes 0 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
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
Tools
See also
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.