-
Notifications
You must be signed in to change notification settings - Fork 151
OptimizeVertices
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 );
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.
Any 'unused' vertices are eliminated and the extra space is left at the end of the vertex buffer when applied.
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
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 [email protected] with any additional questions or comments.
- 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
- x86
- x64
- ARM64
- 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
DirectX Tool Kit for DirectX 11