From bf9828fa35595fdb1d4a8b84c84ca4cf49e7af8f Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Wed, 28 Aug 2024 10:23:45 -0700 Subject: [PATCH] indexgenerator: Add templated overload for meshopt_generateProvokingIndexBuffer This is useful for signed indices as well as 16-bit indices, although this comes with a caveat: an input 16-bit index buffer might not fit in 16 bits after the conversion if the provoking buffer size becomes larger than 64K. This is avoidable if the caller checks the bounds, and 16-bit support might still be beneficial. For extra safety we guard this with an assertion. --- src/meshoptimizer.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/meshoptimizer.h b/src/meshoptimizer.h index b135d03e..dddec4aa 100644 --- a/src/meshoptimizer.h +++ b/src/meshoptimizer.h @@ -666,6 +666,8 @@ inline void meshopt_generateAdjacencyIndexBuffer(T* destination, const T* indice template inline void meshopt_generateTessellationIndexBuffer(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride); template +inline size_t meshopt_generateProvokingIndexBuffer(T* destination, unsigned int* reorder, const T* indices, size_t index_count, size_t vertex_count); +template inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count); template inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count); @@ -902,6 +904,19 @@ inline void meshopt_generateTessellationIndexBuffer(T* destination, const T* ind meshopt_generateTessellationIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride); } +template +inline size_t meshopt_generateProvokingIndexBuffer(T* destination, unsigned int* reorder, const T* indices, size_t index_count, size_t vertex_count) +{ + meshopt_IndexAdapter in(NULL, indices, index_count); + meshopt_IndexAdapter out(destination, NULL, index_count); + + size_t bound = vertex_count + (index_count / 3); + assert(size_t(T(bound - 1)) == bound - 1); // bound - 1 must fit in T + (void)bound; + + return meshopt_generateProvokingIndexBuffer(out.data, reorder, in.data, index_count, vertex_count); +} + template inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count) {