メッシュレット描画

1. はじめに

メッシュシェーダを用いたメッシュレット描画のサンプルプログラムです。Direct3D 12ゲームグラフィックス実践ガイドのChapter.11の”11.3 メッシュレットを用いた描画”に対応したVisual Studio 2026のサンプルプログラムです。詳細な説明については書籍をご参照ください。

実行結果

2. meshoptimizer v1.0 への更新

書籍では,v0.14を使用していましたが,先日version 1.0が出たため,meshoptimizerを1.0ベースに変更しました。
 そのため,Framework/ResMesh.cppのMeshLoader::ParseMesh()メソッドを1.0に対応するように関数の引数などを変更してください。変更したコードは次のようになります。

    // メッシュレット生成.
    {
        const size_t kMaxVertices   = 64;
        const size_t kMaxPrimitives = 126;
        const float  kConeWeight    = 0.0f;

        auto maxMeshlets = meshopt_buildMeshletsBound(
            dstMesh.Indices.size(),
            kMaxVertices,
            kMaxPrimitives);

        std::vector<meshopt_Meshlet> meshlets (maxMeshlets);
        std::vector<uint32_t> meshletVertices (maxMeshlets * kMaxVertices);
        std::vector<uint8_t>  meshletTriangles(maxMeshlets * kMaxPrimitives * 3);

        auto meshletCount = meshopt_buildMeshlets(
            meshlets.data(),
            meshletVertices.data(),
            meshletTriangles.data(),
            dstMesh.Indices.data(),
            dstMesh.Indices.size(),
            &dstMesh.Vertices[0].Position.x,
            dstMesh.Vertices.size(),
            sizeof(dstMesh.Vertices[0]),
            kMaxVertices,
            kMaxPrimitives,
            kConeWeight);

        // 最大値でメモリを予約.
        dstMesh.UniqueVertexIndices.reserve(meshletCount * kMaxVertices);
        dstMesh.PrimitiveIndices   .reserve(meshletCount * kMaxPrimitives);

        for(auto& meshlet : meshlets)
        {
            auto vertexOffset    = uint32_t(dstMesh.UniqueVertexIndices.size());
            auto primitiveOffset = uint32_t(dstMesh.PrimitiveIndices   .size());

            for(auto i=0u; i<meshlet.vertex_count; ++i)
            { dstMesh.UniqueVertexIndices.push_back(meshletVertices[i + meshlet.vertex_offset]); }

            for(auto i=0u; i<meshlet.triangle_count * 3; i+=3)
            {
                ResPrimitiveIndex tris = {};
                tris.index0 = meshletTriangles[i + 0 + meshlet.triangle_offset];
                tris.index1 = meshletTriangles[i + 1 + meshlet.triangle_offset];
                tris.index2 = meshletTriangles[i + 2 + meshlet.triangle_offset];
                dstMesh.PrimitiveIndices.push_back(tris);
            }

            // メッシュレットデータ設定.
            ResMeshlet m = {};
            m.VertexCount       = meshlet.vertex_count;
            m.VertexOffset      = vertexOffset;
            m.PrimitiveCount    = meshlet.triangle_count;
            m.PrimitiveOffset   = primitiveOffset;

            dstMesh.Meshlets.push_back(m);
        }

        // サイズ最適化.
        dstMesh.UniqueVertexIndices .shrink_to_fit();
        dstMesh.PrimitiveIndices    .shrink_to_fit();
        dstMesh.Meshlets            .shrink_to_fit();
    }

3. 参考文献

Pocol, “Direct3D 12ゲームグラフィックス実践ガイド”, pp.384-405, 技術評論社, 2021.

サンプルコード

本ソースコードおよびプログラムはMITライセンスに準じます。
プログラムの作成にはWindows 11, およびMicrosoft Visual Studio 2026 Communityを用いています。




Copyright © 2006-2026 Pocol.