Skip to content

OptimizeVertices

Chuck Walbourn edited this page Jan 21, 2022 · 10 revisions
DirectXMesh

Reorders vertices in order of use by the index buffer which optimizes for the vertex shader pre-transform cache.

HRESULT OptimizeVertices(
   const uint16_t* indices, size_t nFaces, size_t nVerts,
   uint32_t* vertexRemap, size_t* trailingUnused = nullptr );

HRESULT OptimizeVertices(
   const uint32_t* indices, size_t nFaces, size_t nVerts,
   uint32_t* vertexRemap, size_t* trailingUnused = nullptr );

Parameters

vertexRemap is an array describing the reordering, and must be nVerts in size: oldLoc = vertexRemap[newLoc]. See FinalizeIB, FinalizeVB, and FinalizeVBAndPointReps for details.

Note this matches the D3DXMesh::Optimize method and D3DXOptimizeVertices function definitions of the vertex remap array.

trailingUnused is an optional output parameter that returns the number of "unused" vertex positions in the remap array. In the optimized remap, these will always be at the end of the list. You can use this value in combination with CompactVB to trim out unused vertex data.

Remarks

Any 'unused' vertices are eliminated and the extra space is left at the end of the vertex buffer when applied.

Example

auto mesh = std::make_unique<WaveFrontReader<uint16_t>>();

if ( FAILED( mesh->Load( L"test.obj" ) ) )
   // Error

size_t nFaces = mesh->indices.size() / 3;
size_t nVerts = mesh->vertices.size();

auto pos = std::make_unique<XMFLOAT3[]>(nVerts);
for( size_t j = 0; j < nVerts; ++j )
   pos[ j ] = mesh->vertices[ j ].position;

auto adj = std::make_unique<uint32_t[]>(mesh->indices.size());
if ( FAILED( GenerateAdjacencyAndPointReps( mesh->indices.data(), nFaces,
   pos.get(), nVerts, 0.f, nullptr, adj.get() ) ) )
   // Error

auto faceRemap = std::make_unique<uint32_t[]>(nFaces);
if ( FAILED( OptimizeFaces( mesh->indices.data(), nFaces, adj.get(),
   faceRemap.get() ) ) )
   // Error

auto newIndices = std::make_unique<uint16_t[]>(nFaces * 3);
if ( FAILED( ReorderIB( mesh->indices.data(), nFaces,
   faceRemap.get(), newIndices.get() ) ) )
   // Error

auto vertRemap = std::make_unique<uint32_t[]>(nVerts);
if ( FAILED( OptimizeVertices( newIndices.get(), nFaces, nVerts, vertRemap.get() ) ) )
   // Error

if ( FAILED( FinalizeIB( newIndices.get(), nFaces, vertRemap.get(), nVerts ) ) )
   // Error

std::unique_ptr<WaveFrontReader<uint16_t>::Vertex> vb(
   new WaveFrontReader<uint16_t>::Vertex[ nVerts ] );

if ( FAILED( FinalizeVB( mesh->vertices.data(),
   sizeof(WaveFrontReader<uint16_t>::Vertex),
   nVerts, nullptr, 0, vertRemap.get(), vb.get() ) ) )
   // Error

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