From d7ea5c20bf22b4aeb3aad18a640e230c18f0de16 Mon Sep 17 00:00:00 2001
From: Sebastian Gaida <gaida@ca-digit.com>
Date: Tue, 27 Jul 2021 11:35:49 +0200
Subject: [PATCH] [#87] add skippedIndice vector

---
 .../meshlet/include/vkcv/meshlet/Tipsify.hpp  | 20 +++++++++++++++++--
 modules/meshlet/src/vkcv/meshlet/Tipsify.cpp  | 12 ++++++-----
 projects/mesh_shader/src/main.cpp             |  6 +++---
 3 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/modules/meshlet/include/vkcv/meshlet/Tipsify.hpp b/modules/meshlet/include/vkcv/meshlet/Tipsify.hpp
index a5bcef76..70fbd39c 100644
--- a/modules/meshlet/include/vkcv/meshlet/Tipsify.hpp
+++ b/modules/meshlet/include/vkcv/meshlet/Tipsify.hpp
@@ -5,6 +5,19 @@
 #include <iostream>
 
 namespace vkcv::meshlet {
+    struct tipsifyResult{
+        /**
+         *
+         * @param indexBuffer new indexBuffer
+         * @param skippedIndices indices that have a spacial break
+         */
+        tipsifyResult(const std::vector<uint32_t> indexBuffer, const std::vector<uint32_t> skippedIndices)
+        :indexBuffer(indexBuffer), skippedIndices(skippedIndices) {}
+
+        std::vector<uint32_t> indexBuffer;
+        std::vector<uint32_t>  skippedIndices;
+    };
+
     /**
      * reorders the IndexBuffer, so all usages of vertices to triangle are as close as possible
      * @param indexBuffer32Bit current IndexBuffer
@@ -13,9 +26,12 @@ namespace vkcv::meshlet {
      * Recommended: 20. Keep the value between 5 and 50 <br>
      * low:         more random and patchy<br>
      * high:        closer vertices have higher chance -> leads to sinuous lines
-     * @return new IndexBuffer that replaces the input IndexBuffer
+     * @return new IndexBuffer that replaces the input IndexBuffer, and the indices that are skipped
+     *
+     * https://gfx.cs.princeton.edu/pubs/Sander_2007_%3ETR/tipsy.pdf
+     * https://www.martin.st/thesis/efficient_triangle_reordering.pdf
      */
-    std::vector<uint32_t> tipsifyMesh(
+    tipsifyResult tipsifyMesh(
             const std::vector<uint32_t> &indexBuffer32Bit,
             const int vertexCount, const unsigned int cacheSize = 20);
 
diff --git a/modules/meshlet/src/vkcv/meshlet/Tipsify.cpp b/modules/meshlet/src/vkcv/meshlet/Tipsify.cpp
index a41d37c1..21c03645 100644
--- a/modules/meshlet/src/vkcv/meshlet/Tipsify.cpp
+++ b/modules/meshlet/src/vkcv/meshlet/Tipsify.cpp
@@ -3,10 +3,11 @@
 #include "vkcv/meshlet/Tipsify.hpp"
 #include <iostream>
 
-const int maxUsedVertices = 128;
-
 namespace vkcv::meshlet {
 
+    const int maxUsedVertices           = 128;
+    std::vector<uint32_t> skippedIndices;
+
     /**
      * modulo operation with maxUsedVertices
      * @param number for modulo operation
@@ -47,6 +48,7 @@ namespace vkcv::meshlet {
         while (lowestLivingVertexIndex + 1 < vertexCount) {
             lowestLivingVertexIndex++;
             if (livingTriangles[lowestLivingVertexIndex] > 0) {
+                skippedIndices.push_back(static_cast<uint32_t>(lowestLivingVertexIndex));
                 return lowestLivingVertexIndex;
             }
         }
@@ -115,14 +117,14 @@ namespace vkcv::meshlet {
         return nextVertexIndex;
     }
 
-    std::vector<uint32_t> tipsifyMesh(
+    tipsifyResult tipsifyMesh(
             const std::vector<uint32_t> &indexBuffer32Bit,
             const int vertexCount,
             const unsigned int cacheSize) {
 
         if (indexBuffer32Bit.empty() || vertexCount <= 0) {
             vkcv_log(LogLevel::ERROR, "Invalid Input.");
-            return indexBuffer32Bit;
+            return tipsifyResult(indexBuffer32Bit ,skippedIndices );
         }
         int triangleCount = indexBuffer32Bit.size() / 3;
 
@@ -264,6 +266,6 @@ namespace vkcv::meshlet {
             }
         }
 
-        return reorderedIndexBuffer;
+        return tipsifyResult(reorderedIndexBuffer, skippedIndices);
     }
 }
\ No newline at end of file
diff --git a/projects/mesh_shader/src/main.cpp b/projects/mesh_shader/src/main.cpp
index 7d3c785e..bfbd595e 100644
--- a/projects/mesh_shader/src/main.cpp
+++ b/projects/mesh_shader/src/main.cpp
@@ -140,10 +140,10 @@ int main(int argc, const char** argv) {
 	// mesh shader buffers
 	const auto& assetLoaderIndexBuffer              = mesh.vertexGroups[0].indexBuffer;
 	std::vector<uint32_t> indexBuffer32Bit          = vkcv::meshlet::assetLoaderIndicesTo32BitIndices(assetLoaderIndexBuffer.data, assetLoaderIndexBuffer.type);
-    //std::vector<uint32_t> reorderedIndexBuffer32Bit = vkcv::meshlet::tipsifyMesh(indexBuffer32Bit, interleavedVertices.size());
-    std::vector<uint32_t> reorderedIndexBuffer32Bit = vkcv::meshlet::forsythReorder(indexBuffer32Bit, interleavedVertices.size());
+    vkcv::meshlet::tipsifyResult tipsifyResult      = vkcv::meshlet::tipsifyMesh(indexBuffer32Bit, interleavedVertices.size());
+    //std::vector<uint32_t> reorderedIndexBuffer32Bit = vkcv::meshlet::forsythReorder(indexBuffer32Bit, interleavedVertices.size());
 
-    const auto meshShaderModelData = createMeshShaderModelData(interleavedVertices, reorderedIndexBuffer32Bit);
+    const auto meshShaderModelData = createMeshShaderModelData(interleavedVertices, tipsifyResult.indexBuffer);
 
 	auto meshShaderVertexBuffer = core.createBuffer<vkcv::meshlet::Vertex>(
 		vkcv::BufferType::STORAGE,
-- 
GitLab