17 ComputeMeshlets
Chuck Walbourn редактировал(а) эту страницу 2022-01-20 17:37:50 -08:00
DirectXMesh

Computes meshlets from mesh subsets for use with DirectX 12 Mesh Shaders.

HRESULT ComputeMeshlets(
    const uint16_t* indices, size_t nFaces,
    const XMFLOAT3* positions, size_t nVerts,
    const uint32_t* adjacency,
    std::vector<Meshlet>& meshlets,
    std::vector<uint8_t>& uniqueVertexIB,
    std::vector<MeshletTriangle>& primitiveIndices,
    size_t maxVerts = MESHLET_DEFAULT_MAX_VERTS,
    size_t maxPrims = MESHLET_DEFAULT_MAX_PRIMS);

HRESULT ComputeMeshlets(
    const uint32_t* indices, size_t nFaces,
    const XMFLOAT3* positions, size_t nVerts,
    const uint32_t* adjacency,
    std::vector<Meshlet>& meshlets,
    std::vector<uint8_t>& uniqueVertexIB,
    std::vector<MeshletTriangle>& primitiveIndices,
    size_t maxVerts = MESHLET_DEFAULT_MAX_VERTS,
    size_t maxPrims = MESHLET_DEFAULT_MAX_PRIMS);

Computes meshlets for multiple subsets of a mesh:

HRESULT ComputeMeshlets(
    const uint16_t* indices, size_t nFaces,
    const XMFLOAT3* positions, size_t nVerts,
    const std::pair<size_t, size_t>* subsets, size_t nSubsets,
    _In_reads_opt_(nFaces * 3) const uint32_t* adjacency,
    std::vector<Meshlet>& meshlets,
    std::vector<uint8_t>& uniqueVertexIB,
    std::vector<MeshletTriangle>& primitiveIndices,
    std::pair<size_t, size_t>* meshletSubsets,
    size_t maxVerts = MESHLET_DEFAULT_MAX_VERTS,
    size_t maxPrims = MESHLET_DEFAULT_MAX_PRIMS);

HRESULT ComputeMeshlets(
    const uint32_t* indices, size_t nFaces,
    const XMFLOAT3* positions, size_t nVerts,
    const std::pair<size_t, size_t>* subsets, size_t nSubsets,
    const uint32_t* adjacency,
    std::vector<Meshlet>& meshlets,
    std::vector<uint8_t>& uniqueVertexIB,
    std::vector<MeshletTriangle>& primitiveIndices,
    std::pair<size_t, size_t>* meshletSubsets,
    size_t maxVerts = MESHLET_DEFAULT_MAX_VERTS,
    size_t maxPrims = MESHLET_DEFAULT_MAX_PRIMS);

Parameters

adjacency can be nullptr, in which case it will call GenerateAdjacencyAndPointReps internally.

The computed meshlets are returned in meshlets, uniqueVertexIB, and primitiveIndices. These std::vectors can be empty or not, and the function will append to any existing data.

The uniqueVertexIB parameter is a byte buffer. The contents are uint16_t for the 16-bit indices input or uint32_t for 32-bit indices input. Be sure that that uniqueVertexIB size is always evenly divisible by 2 for 16-bit indices or 4 for 32-bit indices.

The maxVerts value controls the maximum number of vertices per meshlet, and must be in the range MESHLET_MINIMUM_SIZE (32) to MESHLET_MAXIMUM_SIZE (256). The default value is MESHLET_DEFAULT_MAX_VERTS (128).

The maxPrims value controls the maximum number of primitives per meshlet, and must be in the range MESHLET_MINIMUM_SIZE (32) to MESHLET_MAXIMUM_SIZE (256). The default value is MESHLET_DEFAULT_MAX_PRIMS (128).

For the multisubset versions, the input subsets is data in the same form as ComputeSubsets. meshletSubsets returns subsets as offsets and counts into the meshlets output array.

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;

std::vector<Meshlet> meshlets;
std::vector<uint8_t> uniqueVertexIB;
std::vector<MeshletTriangle> primitiveIndices;
if ( FAILED( ComputeMeshlets( mesh->indices.data(), nFaces,
    pos.get(), nVerts,
    nullptr,
    meshlets, uniqueVertexIB, primitiveIndices) )
    // Error

auto uniqueVertexIndices = reinterpret_cast<const uint16_t*>(uniqueVertexIB.data());
size_t vertIndices = uniqueVertexIB.size() / sizeof(uint16_t);

Further reading

DirectX Blog: Coming to DirectX 12— Mesh Shaders and Amplification Shaders

DirectX Dev Day: Reinventing the Geometry Pipeline: Mesh Shaders in DirectX 12 (YouTube)