meshoptimizer.h 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /**
  2. * meshoptimizer - version 0.17
  3. *
  4. * Copyright (C) 2016-2021, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
  5. * Report bugs and download new versions at https://github.com/zeux/meshoptimizer
  6. *
  7. * This library is distributed under the MIT License. See notice at the end of this file.
  8. */
  9. #pragma once
  10. #include <assert.h>
  11. #include <stddef.h>
  12. /* Version macro; major * 1000 + minor * 10 + patch */
  13. #define MESHOPTIMIZER_VERSION 170 /* 0.17 */
  14. /* If no API is defined, assume default */
  15. #ifndef MESHOPTIMIZER_API
  16. #define MESHOPTIMIZER_API
  17. #endif
  18. /* Experimental APIs have unstable interface and might have implementation that's not fully tested or optimized */
  19. #define MESHOPTIMIZER_EXPERIMENTAL MESHOPTIMIZER_API
  20. /* C interface */
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * Vertex attribute stream, similar to glVertexPointer
  26. * Each element takes size bytes, with stride controlling the spacing between successive elements.
  27. */
  28. struct meshopt_Stream
  29. {
  30. const void* data;
  31. size_t size;
  32. size_t stride;
  33. };
  34. /**
  35. * Generates a vertex remap table from the vertex buffer and an optional index buffer and returns number of unique vertices
  36. * As a result, all vertices that are binary equivalent map to the same (new) location, with no gaps in the resulting sequence.
  37. * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer/meshopt_remapIndexBuffer.
  38. * Note that binary equivalence considers all vertex_size bytes, including padding which should be zero-initialized.
  39. *
  40. * destination must contain enough space for the resulting remap table (vertex_count elements)
  41. * indices can be NULL if the input is unindexed
  42. */
  43. MESHOPTIMIZER_API size_t meshopt_generateVertexRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
  44. /**
  45. * Generates a vertex remap table from multiple vertex streams and an optional index buffer and returns number of unique vertices
  46. * As a result, all vertices that are binary equivalent map to the same (new) location, with no gaps in the resulting sequence.
  47. * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer/meshopt_remapIndexBuffer.
  48. * To remap vertex buffers, you will need to call meshopt_remapVertexBuffer for each vertex stream.
  49. * Note that binary equivalence considers all size bytes in each stream, including padding which should be zero-initialized.
  50. *
  51. * destination must contain enough space for the resulting remap table (vertex_count elements)
  52. * indices can be NULL if the input is unindexed
  53. */
  54. MESHOPTIMIZER_API size_t meshopt_generateVertexRemapMulti(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, const struct meshopt_Stream* streams, size_t stream_count);
  55. /**
  56. * Generates vertex buffer from the source vertex buffer and remap table generated by meshopt_generateVertexRemap
  57. *
  58. * destination must contain enough space for the resulting vertex buffer (unique_vertex_count elements, returned by meshopt_generateVertexRemap)
  59. * vertex_count should be the initial vertex count and not the value returned by meshopt_generateVertexRemap
  60. */
  61. MESHOPTIMIZER_API void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap);
  62. /**
  63. * Generate index buffer from the source index buffer and remap table generated by meshopt_generateVertexRemap
  64. *
  65. * destination must contain enough space for the resulting index buffer (index_count elements)
  66. * indices can be NULL if the input is unindexed
  67. */
  68. MESHOPTIMIZER_API void meshopt_remapIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const unsigned int* remap);
  69. /**
  70. * Generate index buffer that can be used for more efficient rendering when only a subset of the vertex attributes is necessary
  71. * All vertices that are binary equivalent (wrt first vertex_size bytes) map to the first vertex in the original vertex buffer.
  72. * This makes it possible to use the index buffer for Z pre-pass or shadowmap rendering, while using the original index buffer for regular rendering.
  73. * Note that binary equivalence considers all vertex_size bytes, including padding which should be zero-initialized.
  74. *
  75. * destination must contain enough space for the resulting index buffer (index_count elements)
  76. */
  77. MESHOPTIMIZER_API void meshopt_generateShadowIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size, size_t vertex_stride);
  78. /**
  79. * Generate index buffer that can be used for more efficient rendering when only a subset of the vertex attributes is necessary
  80. * All vertices that are binary equivalent (wrt specified streams) map to the first vertex in the original vertex buffer.
  81. * This makes it possible to use the index buffer for Z pre-pass or shadowmap rendering, while using the original index buffer for regular rendering.
  82. * Note that binary equivalence considers all size bytes in each stream, including padding which should be zero-initialized.
  83. *
  84. * destination must contain enough space for the resulting index buffer (index_count elements)
  85. */
  86. MESHOPTIMIZER_API void meshopt_generateShadowIndexBufferMulti(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, const struct meshopt_Stream* streams, size_t stream_count);
  87. /**
  88. * Generate index buffer that can be used as a geometry shader input with triangle adjacency topology
  89. * Each triangle is converted into a 6-vertex patch with the following layout:
  90. * - 0, 2, 4: original triangle vertices
  91. * - 1, 3, 5: vertices adjacent to edges 02, 24 and 40
  92. * The resulting patch can be rendered with geometry shaders using e.g. VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY.
  93. * This can be used to implement algorithms like silhouette detection/expansion and other forms of GS-driven rendering.
  94. *
  95. * destination must contain enough space for the resulting index buffer (index_count*2 elements)
  96. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  97. */
  98. MESHOPTIMIZER_API void meshopt_generateAdjacencyIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  99. /**
  100. * Generate index buffer that can be used for PN-AEN tessellation with crack-free displacement
  101. * Each triangle is converted into a 12-vertex patch with the following layout:
  102. * - 0, 1, 2: original triangle vertices
  103. * - 3, 4: opposing edge for edge 0, 1
  104. * - 5, 6: opposing edge for edge 1, 2
  105. * - 7, 8: opposing edge for edge 2, 0
  106. * - 9, 10, 11: dominant vertices for corners 0, 1, 2
  107. * The resulting patch can be rendered with hardware tessellation using PN-AEN and displacement mapping.
  108. * See "Tessellation on Any Budget" (John McDonald, GDC 2011) for implementation details.
  109. *
  110. * destination must contain enough space for the resulting index buffer (index_count*4 elements)
  111. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  112. */
  113. MESHOPTIMIZER_API void meshopt_generateTessellationIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  114. /**
  115. * Vertex transform cache optimizer
  116. * Reorders indices to reduce the number of GPU vertex shader invocations
  117. * If index buffer contains multiple ranges for multiple draw calls, this functions needs to be called on each range individually.
  118. *
  119. * destination must contain enough space for the resulting index buffer (index_count elements)
  120. */
  121. MESHOPTIMIZER_API void meshopt_optimizeVertexCache(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
  122. /**
  123. * Vertex transform cache optimizer for strip-like caches
  124. * Produces inferior results to meshopt_optimizeVertexCache from the GPU vertex cache perspective
  125. * However, the resulting index order is more optimal if the goal is to reduce the triangle strip length or improve compression efficiency
  126. *
  127. * destination must contain enough space for the resulting index buffer (index_count elements)
  128. */
  129. MESHOPTIMIZER_API void meshopt_optimizeVertexCacheStrip(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
  130. /**
  131. * Vertex transform cache optimizer for FIFO caches
  132. * Reorders indices to reduce the number of GPU vertex shader invocations
  133. * Generally takes ~3x less time to optimize meshes but produces inferior results compared to meshopt_optimizeVertexCache
  134. * If index buffer contains multiple ranges for multiple draw calls, this functions needs to be called on each range individually.
  135. *
  136. * destination must contain enough space for the resulting index buffer (index_count elements)
  137. * cache_size should be less than the actual GPU cache size to avoid cache thrashing
  138. */
  139. MESHOPTIMIZER_API void meshopt_optimizeVertexCacheFifo(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int cache_size);
  140. /**
  141. * Overdraw optimizer
  142. * Reorders indices to reduce the number of GPU vertex shader invocations and the pixel overdraw
  143. * If index buffer contains multiple ranges for multiple draw calls, this functions needs to be called on each range individually.
  144. *
  145. * destination must contain enough space for the resulting index buffer (index_count elements)
  146. * indices must contain index data that is the result of meshopt_optimizeVertexCache (*not* the original mesh indices!)
  147. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  148. * threshold indicates how much the overdraw optimizer can degrade vertex cache efficiency (1.05 = up to 5%) to reduce overdraw more efficiently
  149. */
  150. MESHOPTIMIZER_API void meshopt_optimizeOverdraw(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float threshold);
  151. /**
  152. * Vertex fetch cache optimizer
  153. * Reorders vertices and changes indices to reduce the amount of GPU memory fetches during vertex processing
  154. * Returns the number of unique vertices, which is the same as input vertex count unless some vertices are unused
  155. * This functions works for a single vertex stream; for multiple vertex streams, use meshopt_optimizeVertexFetchRemap + meshopt_remapVertexBuffer for each stream.
  156. *
  157. * destination must contain enough space for the resulting vertex buffer (vertex_count elements)
  158. * indices is used both as an input and as an output index buffer
  159. */
  160. MESHOPTIMIZER_API size_t meshopt_optimizeVertexFetch(void* destination, unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
  161. /**
  162. * Vertex fetch cache optimizer
  163. * Generates vertex remap to reduce the amount of GPU memory fetches during vertex processing
  164. * Returns the number of unique vertices, which is the same as input vertex count unless some vertices are unused
  165. * The resulting remap table should be used to reorder vertex/index buffers using meshopt_remapVertexBuffer/meshopt_remapIndexBuffer
  166. *
  167. * destination must contain enough space for the resulting remap table (vertex_count elements)
  168. */
  169. MESHOPTIMIZER_API size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
  170. /**
  171. * Index buffer encoder
  172. * Encodes index data into an array of bytes that is generally much smaller (<1.5 bytes/triangle) and compresses better (<1 bytes/triangle) compared to original.
  173. * Input index buffer must represent a triangle list.
  174. * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
  175. * For maximum efficiency the index buffer being encoded has to be optimized for vertex cache and vertex fetch first.
  176. *
  177. * buffer must contain enough space for the encoded index buffer (use meshopt_encodeIndexBufferBound to compute worst case size)
  178. */
  179. MESHOPTIMIZER_API size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const unsigned int* indices, size_t index_count);
  180. MESHOPTIMIZER_API size_t meshopt_encodeIndexBufferBound(size_t index_count, size_t vertex_count);
  181. /**
  182. * Set index encoder format version
  183. * version must specify the data format version to encode; valid values are 0 (decodable by all library versions) and 1 (decodable by 0.14+)
  184. */
  185. MESHOPTIMIZER_API void meshopt_encodeIndexVersion(int version);
  186. /**
  187. * Index buffer decoder
  188. * Decodes index data from an array of bytes generated by meshopt_encodeIndexBuffer
  189. * Returns 0 if decoding was successful, and an error code otherwise
  190. * The decoder is safe to use for untrusted input, but it may produce garbage data (e.g. out of range indices).
  191. *
  192. * destination must contain enough space for the resulting index buffer (index_count elements)
  193. */
  194. MESHOPTIMIZER_API int meshopt_decodeIndexBuffer(void* destination, size_t index_count, size_t index_size, const unsigned char* buffer, size_t buffer_size);
  195. /**
  196. * Index sequence encoder
  197. * Encodes index sequence into an array of bytes that is generally smaller and compresses better compared to original.
  198. * Input index sequence can represent arbitrary topology; for triangle lists meshopt_encodeIndexBuffer is likely to be better.
  199. * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
  200. *
  201. * buffer must contain enough space for the encoded index sequence (use meshopt_encodeIndexSequenceBound to compute worst case size)
  202. */
  203. MESHOPTIMIZER_API size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const unsigned int* indices, size_t index_count);
  204. MESHOPTIMIZER_API size_t meshopt_encodeIndexSequenceBound(size_t index_count, size_t vertex_count);
  205. /**
  206. * Index sequence decoder
  207. * Decodes index data from an array of bytes generated by meshopt_encodeIndexSequence
  208. * Returns 0 if decoding was successful, and an error code otherwise
  209. * The decoder is safe to use for untrusted input, but it may produce garbage data (e.g. out of range indices).
  210. *
  211. * destination must contain enough space for the resulting index sequence (index_count elements)
  212. */
  213. MESHOPTIMIZER_API int meshopt_decodeIndexSequence(void* destination, size_t index_count, size_t index_size, const unsigned char* buffer, size_t buffer_size);
  214. /**
  215. * Vertex buffer encoder
  216. * Encodes vertex data into an array of bytes that is generally smaller and compresses better compared to original.
  217. * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
  218. * This function works for a single vertex stream; for multiple vertex streams, call meshopt_encodeVertexBuffer for each stream.
  219. * Note that all vertex_size bytes of each vertex are encoded verbatim, including padding which should be zero-initialized.
  220. *
  221. * buffer must contain enough space for the encoded vertex buffer (use meshopt_encodeVertexBufferBound to compute worst case size)
  222. */
  223. MESHOPTIMIZER_API size_t meshopt_encodeVertexBuffer(unsigned char* buffer, size_t buffer_size, const void* vertices, size_t vertex_count, size_t vertex_size);
  224. MESHOPTIMIZER_API size_t meshopt_encodeVertexBufferBound(size_t vertex_count, size_t vertex_size);
  225. /**
  226. * Set vertex encoder format version
  227. * version must specify the data format version to encode; valid values are 0 (decodable by all library versions)
  228. */
  229. MESHOPTIMIZER_API void meshopt_encodeVertexVersion(int version);
  230. /**
  231. * Vertex buffer decoder
  232. * Decodes vertex data from an array of bytes generated by meshopt_encodeVertexBuffer
  233. * Returns 0 if decoding was successful, and an error code otherwise
  234. * The decoder is safe to use for untrusted input, but it may produce garbage data.
  235. *
  236. * destination must contain enough space for the resulting vertex buffer (vertex_count * vertex_size bytes)
  237. */
  238. MESHOPTIMIZER_API int meshopt_decodeVertexBuffer(void* destination, size_t vertex_count, size_t vertex_size, const unsigned char* buffer, size_t buffer_size);
  239. /**
  240. * Vertex buffer filters
  241. * These functions can be used to filter output of meshopt_decodeVertexBuffer in-place.
  242. *
  243. * meshopt_decodeFilterOct decodes octahedral encoding of a unit vector with K-bit (K <= 16) signed X/Y as an input; Z must store 1.0f.
  244. * Each component is stored as an 8-bit or 16-bit normalized integer; stride must be equal to 4 or 8. W is preserved as is.
  245. *
  246. * meshopt_decodeFilterQuat decodes 3-component quaternion encoding with K-bit (4 <= K <= 16) component encoding and a 2-bit component index indicating which component to reconstruct.
  247. * Each component is stored as an 16-bit integer; stride must be equal to 8.
  248. *
  249. * meshopt_decodeFilterExp decodes exponential encoding of floating-point data with 8-bit exponent and 24-bit integer mantissa as 2^E*M.
  250. * Each 32-bit component is decoded in isolation; stride must be divisible by 4.
  251. */
  252. MESHOPTIMIZER_EXPERIMENTAL void meshopt_decodeFilterOct(void* buffer, size_t count, size_t stride);
  253. MESHOPTIMIZER_EXPERIMENTAL void meshopt_decodeFilterQuat(void* buffer, size_t count, size_t stride);
  254. MESHOPTIMIZER_EXPERIMENTAL void meshopt_decodeFilterExp(void* buffer, size_t count, size_t stride);
  255. /**
  256. * Vertex buffer filter encoders
  257. * These functions can be used to encode data in a format that meshopt_decodeFilter can decode
  258. *
  259. * meshopt_encodeFilterOct encodes unit vectors with K-bit (K <= 16) signed X/Y as an output.
  260. * Each component is stored as an 8-bit or 16-bit normalized integer; stride must be equal to 4 or 8. W is preserved as is.
  261. * Input data must contain 4 floats for every vector (count*4 total).
  262. *
  263. * meshopt_encodeFilterQuat encodes unit quaternions with K-bit (4 <= K <= 16) component encoding.
  264. * Each component is stored as an 16-bit integer; stride must be equal to 8.
  265. * Input data must contain 4 floats for every quaternion (count*4 total).
  266. *
  267. * meshopt_encodeFilterExp encodes arbitrary (finite) floating-point data with 8-bit exponent and K-bit integer mantissa (1 <= K <= 24).
  268. * Mantissa is shared between all components of a given vector as defined by stride; stride must be divisible by 4.
  269. * Input data must contain stride/4 floats for every vector (count*stride/4 total).
  270. * When individual (scalar) encoding is desired, simply pass stride=4 and adjust count accordingly.
  271. */
  272. MESHOPTIMIZER_EXPERIMENTAL void meshopt_encodeFilterOct(void* destination, size_t count, size_t stride, int bits, const float* data);
  273. MESHOPTIMIZER_EXPERIMENTAL void meshopt_encodeFilterQuat(void* destination, size_t count, size_t stride, int bits, const float* data);
  274. MESHOPTIMIZER_EXPERIMENTAL void meshopt_encodeFilterExp(void* destination, size_t count, size_t stride, int bits, const float* data);
  275. /**
  276. * Experimental: Mesh simplifier
  277. * Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible
  278. * The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error.
  279. * If not all attributes from the input mesh are required, it's recommended to reindex the mesh using meshopt_generateShadowIndexBuffer prior to simplification.
  280. * Returns the number of indices after simplification, with destination containing new index data
  281. * The resulting index buffer references vertices from the original vertex buffer.
  282. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  283. *
  284. * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
  285. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  286. * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation
  287. * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
  288. */
  289. MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_simplify(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error);
  290. /**
  291. * Experimental: Mesh simplifier (sloppy)
  292. * Reduces the number of triangles in the mesh, sacrificing mesh appearance for simplification performance
  293. * The algorithm doesn't preserve mesh topology but can stop short of the target goal based on target error.
  294. * Returns the number of indices after simplification, with destination containing new index data
  295. * The resulting index buffer references vertices from the original vertex buffer.
  296. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  297. *
  298. * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
  299. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  300. * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation
  301. * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
  302. */
  303. MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_simplifySloppy(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error);
  304. /**
  305. * Experimental: Point cloud simplifier
  306. * Reduces the number of points in the cloud to reach the given target
  307. * Returns the number of points after simplification, with destination containing new index data
  308. * The resulting index buffer references vertices from the original vertex buffer.
  309. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  310. *
  311. * destination must contain enough space for the target index buffer (target_vertex_count elements)
  312. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  313. */
  314. MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_vertex_count);
  315. /**
  316. * Experimental: Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
  317. *
  318. * Absolute error must be *divided* by the scaling factor before passing it to meshopt_simplify as target_error
  319. * Relative error returned by meshopt_simplify via result_error must be *multiplied* by the scaling factor to get absolute error.
  320. */
  321. MESHOPTIMIZER_EXPERIMENTAL float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  322. /**
  323. * Mesh stripifier
  324. * Converts a previously vertex cache optimized triangle list to triangle strip, stitching strips using restart index or degenerate triangles
  325. * Returns the number of indices in the resulting strip, with destination containing new index data
  326. * For maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
  327. * Using restart indices can result in ~10% smaller index buffers, but on some GPUs restart indices may result in decreased performance.
  328. *
  329. * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_stripifyBound
  330. * restart_index should be 0xffff or 0xffffffff depending on index size, or 0 to use degenerate triangles
  331. */
  332. MESHOPTIMIZER_API size_t meshopt_stripify(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int restart_index);
  333. MESHOPTIMIZER_API size_t meshopt_stripifyBound(size_t index_count);
  334. /**
  335. * Mesh unstripifier
  336. * Converts a triangle strip to a triangle list
  337. * Returns the number of indices in the resulting list, with destination containing new index data
  338. *
  339. * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_unstripifyBound
  340. */
  341. MESHOPTIMIZER_API size_t meshopt_unstripify(unsigned int* destination, const unsigned int* indices, size_t index_count, unsigned int restart_index);
  342. MESHOPTIMIZER_API size_t meshopt_unstripifyBound(size_t index_count);
  343. struct meshopt_VertexCacheStatistics
  344. {
  345. unsigned int vertices_transformed;
  346. unsigned int warps_executed;
  347. float acmr; /* transformed vertices / triangle count; best case 0.5, worst case 3.0, optimum depends on topology */
  348. float atvr; /* transformed vertices / vertex count; best case 1.0, worst case 6.0, optimum is 1.0 (each vertex is transformed once) */
  349. };
  350. /**
  351. * Vertex transform cache analyzer
  352. * Returns cache hit statistics using a simplified FIFO model
  353. * Results may not match actual GPU performance
  354. */
  355. MESHOPTIMIZER_API struct meshopt_VertexCacheStatistics meshopt_analyzeVertexCache(const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int cache_size, unsigned int warp_size, unsigned int primgroup_size);
  356. struct meshopt_OverdrawStatistics
  357. {
  358. unsigned int pixels_covered;
  359. unsigned int pixels_shaded;
  360. float overdraw; /* shaded pixels / covered pixels; best case 1.0 */
  361. };
  362. /**
  363. * Overdraw analyzer
  364. * Returns overdraw statistics using a software rasterizer
  365. * Results may not match actual GPU performance
  366. *
  367. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  368. */
  369. MESHOPTIMIZER_API struct meshopt_OverdrawStatistics meshopt_analyzeOverdraw(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  370. struct meshopt_VertexFetchStatistics
  371. {
  372. unsigned int bytes_fetched;
  373. float overfetch; /* fetched bytes / vertex buffer size; best case 1.0 (each byte is fetched once) */
  374. };
  375. /**
  376. * Vertex fetch cache analyzer
  377. * Returns cache hit statistics using a simplified direct mapped model
  378. * Results may not match actual GPU performance
  379. */
  380. MESHOPTIMIZER_API struct meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const unsigned int* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
  381. struct meshopt_Meshlet
  382. {
  383. /* offsets within meshlet_vertices and meshlet_triangles arrays with meshlet data */
  384. unsigned int vertex_offset;
  385. unsigned int triangle_offset;
  386. /* number of vertices and triangles used in the meshlet; data is stored in consecutive range defined by offset and count */
  387. unsigned int vertex_count;
  388. unsigned int triangle_count;
  389. };
  390. /**
  391. * Meshlet builder
  392. * Splits the mesh into a set of meshlets where each meshlet has a micro index buffer indexing into meshlet vertices that refer to the original vertex buffer
  393. * The resulting data can be used to render meshes using NVidia programmable mesh shading pipeline, or in other cluster-based renderers.
  394. * When using buildMeshlets, vertex positions need to be provided to minimize the size of the resulting clusters.
  395. * When using buildMeshletsScan, for maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
  396. *
  397. * meshlets must contain enough space for all meshlets, worst case size can be computed with meshopt_buildMeshletsBound
  398. * meshlet_vertices must contain enough space for all meshlets, worst case size is equal to max_meshlets * max_vertices
  399. * meshlet_triangles must contain enough space for all meshlets, worst case size is equal to max_meshlets * max_triangles * 3
  400. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  401. * max_vertices and max_triangles must not exceed implementation limits (max_vertices <= 255 - not 256!, max_triangles <= 512)
  402. * cone_weight should be set to 0 when cone culling is not used, and a value between 0 and 1 otherwise to balance between cluster size and cone culling efficiency
  403. */
  404. MESHOPTIMIZER_API size_t meshopt_buildMeshlets(struct meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t max_vertices, size_t max_triangles, float cone_weight);
  405. MESHOPTIMIZER_API size_t meshopt_buildMeshletsScan(struct meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const unsigned int* indices, size_t index_count, size_t vertex_count, size_t max_vertices, size_t max_triangles);
  406. MESHOPTIMIZER_API size_t meshopt_buildMeshletsBound(size_t index_count, size_t max_vertices, size_t max_triangles);
  407. struct meshopt_Bounds
  408. {
  409. /* bounding sphere, useful for frustum and occlusion culling */
  410. float center[3];
  411. float radius;
  412. /* normal cone, useful for backface culling */
  413. float cone_apex[3];
  414. float cone_axis[3];
  415. float cone_cutoff; /* = cos(angle/2) */
  416. /* normal cone axis and cutoff, stored in 8-bit SNORM format; decode using x/127.0 */
  417. signed char cone_axis_s8[3];
  418. signed char cone_cutoff_s8;
  419. };
  420. /**
  421. * Cluster bounds generator
  422. * Creates bounding volumes that can be used for frustum, backface and occlusion culling.
  423. *
  424. * For backface culling with orthographic projection, use the following formula to reject backfacing clusters:
  425. * dot(view, cone_axis) >= cone_cutoff
  426. *
  427. * For perspective projection, you can the formula that needs cone apex in addition to axis & cutoff:
  428. * dot(normalize(cone_apex - camera_position), cone_axis) >= cone_cutoff
  429. *
  430. * Alternatively, you can use the formula that doesn't need cone apex and uses bounding sphere instead:
  431. * dot(normalize(center - camera_position), cone_axis) >= cone_cutoff + radius / length(center - camera_position)
  432. * or an equivalent formula that doesn't have a singularity at center = camera_position:
  433. * dot(center - camera_position, cone_axis) >= cone_cutoff * length(center - camera_position) + radius
  434. *
  435. * The formula that uses the apex is slightly more accurate but needs the apex; if you are already using bounding sphere
  436. * to do frustum/occlusion culling, the formula that doesn't use the apex may be preferable.
  437. *
  438. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  439. * index_count/3 should be less than or equal to 512 (the function assumes clusters of limited size)
  440. */
  441. MESHOPTIMIZER_API struct meshopt_Bounds meshopt_computeClusterBounds(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  442. MESHOPTIMIZER_API struct meshopt_Bounds meshopt_computeMeshletBounds(const unsigned int* meshlet_vertices, const unsigned char* meshlet_triangles, size_t triangle_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  443. /**
  444. * Experimental: Spatial sorter
  445. * Generates a remap table that can be used to reorder points for spatial locality.
  446. * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer.
  447. *
  448. * destination must contain enough space for the resulting remap table (vertex_count elements)
  449. */
  450. MESHOPTIMIZER_EXPERIMENTAL void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  451. /**
  452. * Experimental: Spatial sorter
  453. * Reorders triangles for spatial locality, and generates a new index buffer. The resulting index buffer can be used with other functions like optimizeVertexCache.
  454. *
  455. * destination must contain enough space for the resulting index buffer (index_count elements)
  456. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  457. */
  458. MESHOPTIMIZER_EXPERIMENTAL void meshopt_spatialSortTriangles(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  459. /**
  460. * Set allocation callbacks
  461. * These callbacks will be used instead of the default operator new/operator delete for all temporary allocations in the library.
  462. * Note that all algorithms only allocate memory for temporary use.
  463. * allocate/deallocate are always called in a stack-like order - last pointer to be allocated is deallocated first.
  464. */
  465. MESHOPTIMIZER_API void meshopt_setAllocator(void* (*allocate)(size_t), void (*deallocate)(void*));
  466. #ifdef __cplusplus
  467. } /* extern "C" */
  468. #endif
  469. /* Quantization into commonly supported data formats */
  470. #ifdef __cplusplus
  471. /**
  472. * Quantize a float in [0..1] range into an N-bit fixed point unorm value
  473. * Assumes reconstruction function (q / (2^N-1)), which is the case for fixed-function normalized fixed point conversion
  474. * Maximum reconstruction error: 1/2^(N+1)
  475. */
  476. inline int meshopt_quantizeUnorm(float v, int N);
  477. /**
  478. * Quantize a float in [-1..1] range into an N-bit fixed point snorm value
  479. * Assumes reconstruction function (q / (2^(N-1)-1)), which is the case for fixed-function normalized fixed point conversion (except early OpenGL versions)
  480. * Maximum reconstruction error: 1/2^N
  481. */
  482. inline int meshopt_quantizeSnorm(float v, int N);
  483. /**
  484. * Quantize a float into half-precision floating point value
  485. * Generates +-inf for overflow, preserves NaN, flushes denormals to zero, rounds to nearest
  486. * Representable magnitude range: [6e-5; 65504]
  487. * Maximum relative reconstruction error: 5e-4
  488. */
  489. inline unsigned short meshopt_quantizeHalf(float v);
  490. /**
  491. * Quantize a float into a floating point value with a limited number of significant mantissa bits
  492. * Generates +-inf for overflow, preserves NaN, flushes denormals to zero, rounds to nearest
  493. * Assumes N is in a valid mantissa precision range, which is 1..23
  494. */
  495. inline float meshopt_quantizeFloat(float v, int N);
  496. #endif
  497. /**
  498. * C++ template interface
  499. *
  500. * These functions mirror the C interface the library provides, providing template-based overloads so that
  501. * the caller can use an arbitrary type for the index data, both for input and output.
  502. * When the supplied type is the same size as that of unsigned int, the wrappers are zero-cost; when it's not,
  503. * the wrappers end up allocating memory and copying index data to convert from one type to another.
  504. */
  505. #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
  506. template <typename T>
  507. inline size_t meshopt_generateVertexRemap(unsigned int* destination, const T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
  508. template <typename T>
  509. inline size_t meshopt_generateVertexRemapMulti(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count, const meshopt_Stream* streams, size_t stream_count);
  510. template <typename T>
  511. inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap);
  512. template <typename T>
  513. inline void meshopt_generateShadowIndexBuffer(T* destination, const T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size, size_t vertex_stride);
  514. template <typename T>
  515. inline void meshopt_generateShadowIndexBufferMulti(T* destination, const T* indices, size_t index_count, size_t vertex_count, const meshopt_Stream* streams, size_t stream_count);
  516. template <typename T>
  517. inline void meshopt_generateAdjacencyIndexBuffer(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  518. template <typename T>
  519. 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);
  520. template <typename T>
  521. inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count);
  522. template <typename T>
  523. inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count);
  524. template <typename T>
  525. inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size);
  526. template <typename T>
  527. inline void meshopt_optimizeOverdraw(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float threshold);
  528. template <typename T>
  529. inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count);
  530. template <typename T>
  531. inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
  532. template <typename T>
  533. inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
  534. template <typename T>
  535. inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
  536. template <typename T>
  537. inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
  538. template <typename T>
  539. inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
  540. template <typename T>
  541. inline size_t meshopt_simplify(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error = 0);
  542. template <typename T>
  543. inline size_t meshopt_simplifySloppy(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error = 0);
  544. template <typename T>
  545. inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index);
  546. template <typename T>
  547. inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index);
  548. template <typename T>
  549. inline meshopt_VertexCacheStatistics meshopt_analyzeVertexCache(const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size, unsigned int warp_size, unsigned int buffer_size);
  550. template <typename T>
  551. inline meshopt_OverdrawStatistics meshopt_analyzeOverdraw(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  552. template <typename T>
  553. inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
  554. template <typename T>
  555. inline size_t meshopt_buildMeshlets(meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t max_vertices, size_t max_triangles, float cone_weight);
  556. template <typename T>
  557. inline size_t meshopt_buildMeshletsScan(meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const T* indices, size_t index_count, size_t vertex_count, size_t max_vertices, size_t max_triangles);
  558. template <typename T>
  559. inline meshopt_Bounds meshopt_computeClusterBounds(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  560. template <typename T>
  561. inline void meshopt_spatialSortTriangles(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  562. #endif
  563. /* Inline implementation */
  564. #ifdef __cplusplus
  565. inline int meshopt_quantizeUnorm(float v, int N)
  566. {
  567. const float scale = float((1 << N) - 1);
  568. v = (v >= 0) ? v : 0;
  569. v = (v <= 1) ? v : 1;
  570. return int(v * scale + 0.5f);
  571. }
  572. inline int meshopt_quantizeSnorm(float v, int N)
  573. {
  574. const float scale = float((1 << (N - 1)) - 1);
  575. float round = (v >= 0 ? 0.5f : -0.5f);
  576. v = (v >= -1) ? v : -1;
  577. v = (v <= +1) ? v : +1;
  578. return int(v * scale + round);
  579. }
  580. inline unsigned short meshopt_quantizeHalf(float v)
  581. {
  582. union { float f; unsigned int ui; } u = {v};
  583. unsigned int ui = u.ui;
  584. int s = (ui >> 16) & 0x8000;
  585. int em = ui & 0x7fffffff;
  586. /* bias exponent and round to nearest; 112 is relative exponent bias (127-15) */
  587. int h = (em - (112 << 23) + (1 << 12)) >> 13;
  588. /* underflow: flush to zero; 113 encodes exponent -14 */
  589. h = (em < (113 << 23)) ? 0 : h;
  590. /* overflow: infinity; 143 encodes exponent 16 */
  591. h = (em >= (143 << 23)) ? 0x7c00 : h;
  592. /* NaN; note that we convert all types of NaN to qNaN */
  593. h = (em > (255 << 23)) ? 0x7e00 : h;
  594. return (unsigned short)(s | h);
  595. }
  596. inline float meshopt_quantizeFloat(float v, int N)
  597. {
  598. union { float f; unsigned int ui; } u = {v};
  599. unsigned int ui = u.ui;
  600. const int mask = (1 << (23 - N)) - 1;
  601. const int round = (1 << (23 - N)) >> 1;
  602. int e = ui & 0x7f800000;
  603. unsigned int rui = (ui + round) & ~mask;
  604. /* round all numbers except inf/nan; this is important to make sure nan doesn't overflow into -0 */
  605. ui = e == 0x7f800000 ? ui : rui;
  606. /* flush denormals to zero */
  607. ui = e == 0 ? 0 : ui;
  608. u.ui = ui;
  609. return u.f;
  610. }
  611. #endif
  612. /* Internal implementation helpers */
  613. #ifdef __cplusplus
  614. class meshopt_Allocator
  615. {
  616. public:
  617. template <typename T>
  618. struct StorageT
  619. {
  620. static void* (*allocate)(size_t);
  621. static void (*deallocate)(void*);
  622. };
  623. typedef StorageT<void> Storage;
  624. meshopt_Allocator()
  625. : blocks()
  626. , count(0)
  627. {
  628. }
  629. ~meshopt_Allocator()
  630. {
  631. for (size_t i = count; i > 0; --i)
  632. Storage::deallocate(blocks[i - 1]);
  633. }
  634. template <typename T> T* allocate(size_t size)
  635. {
  636. assert(count < sizeof(blocks) / sizeof(blocks[0]));
  637. T* result = static_cast<T*>(Storage::allocate(size > size_t(-1) / sizeof(T) ? size_t(-1) : size * sizeof(T)));
  638. blocks[count++] = result;
  639. return result;
  640. }
  641. private:
  642. void* blocks[24];
  643. size_t count;
  644. };
  645. // This makes sure that allocate/deallocate are lazily generated in translation units that need them and are deduplicated by the linker
  646. template <typename T> void* (*meshopt_Allocator::StorageT<T>::allocate)(size_t) = operator new;
  647. template <typename T> void (*meshopt_Allocator::StorageT<T>::deallocate)(void*) = operator delete;
  648. #endif
  649. /* Inline implementation for C++ templated wrappers */
  650. #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
  651. template <typename T, bool ZeroCopy = sizeof(T) == sizeof(unsigned int)>
  652. struct meshopt_IndexAdapter;
  653. template <typename T>
  654. struct meshopt_IndexAdapter<T, false>
  655. {
  656. T* result;
  657. unsigned int* data;
  658. size_t count;
  659. meshopt_IndexAdapter(T* result_, const T* input, size_t count_)
  660. : result(result_)
  661. , data(0)
  662. , count(count_)
  663. {
  664. size_t size = count > size_t(-1) / sizeof(unsigned int) ? size_t(-1) : count * sizeof(unsigned int);
  665. data = static_cast<unsigned int*>(meshopt_Allocator::Storage::allocate(size));
  666. if (input)
  667. {
  668. for (size_t i = 0; i < count; ++i)
  669. data[i] = input[i];
  670. }
  671. }
  672. ~meshopt_IndexAdapter()
  673. {
  674. if (result)
  675. {
  676. for (size_t i = 0; i < count; ++i)
  677. result[i] = T(data[i]);
  678. }
  679. meshopt_Allocator::Storage::deallocate(data);
  680. }
  681. };
  682. template <typename T>
  683. struct meshopt_IndexAdapter<T, true>
  684. {
  685. unsigned int* data;
  686. meshopt_IndexAdapter(T* result, const T* input, size_t)
  687. : data(reinterpret_cast<unsigned int*>(result ? result : const_cast<T*>(input)))
  688. {
  689. }
  690. };
  691. template <typename T>
  692. inline size_t meshopt_generateVertexRemap(unsigned int* destination, const T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)
  693. {
  694. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  695. return meshopt_generateVertexRemap(destination, indices ? in.data : 0, index_count, vertices, vertex_count, vertex_size);
  696. }
  697. template <typename T>
  698. inline size_t meshopt_generateVertexRemapMulti(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count, const meshopt_Stream* streams, size_t stream_count)
  699. {
  700. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  701. return meshopt_generateVertexRemapMulti(destination, indices ? in.data : 0, index_count, vertex_count, streams, stream_count);
  702. }
  703. template <typename T>
  704. inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap)
  705. {
  706. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  707. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  708. meshopt_remapIndexBuffer(out.data, indices ? in.data : 0, index_count, remap);
  709. }
  710. template <typename T>
  711. inline void meshopt_generateShadowIndexBuffer(T* destination, const T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size, size_t vertex_stride)
  712. {
  713. meshopt_IndexAdapter<T> in(0, indices, index_count);
  714. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  715. meshopt_generateShadowIndexBuffer(out.data, in.data, index_count, vertices, vertex_count, vertex_size, vertex_stride);
  716. }
  717. template <typename T>
  718. inline void meshopt_generateShadowIndexBufferMulti(T* destination, const T* indices, size_t index_count, size_t vertex_count, const meshopt_Stream* streams, size_t stream_count)
  719. {
  720. meshopt_IndexAdapter<T> in(0, indices, index_count);
  721. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  722. meshopt_generateShadowIndexBufferMulti(out.data, in.data, index_count, vertex_count, streams, stream_count);
  723. }
  724. template <typename T>
  725. inline void meshopt_generateAdjacencyIndexBuffer(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  726. {
  727. meshopt_IndexAdapter<T> in(0, indices, index_count);
  728. meshopt_IndexAdapter<T> out(destination, 0, index_count * 2);
  729. meshopt_generateAdjacencyIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  730. }
  731. template <typename T>
  732. 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)
  733. {
  734. meshopt_IndexAdapter<T> in(0, indices, index_count);
  735. meshopt_IndexAdapter<T> out(destination, 0, index_count * 4);
  736. meshopt_generateTessellationIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  737. }
  738. template <typename T>
  739. inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count)
  740. {
  741. meshopt_IndexAdapter<T> in(0, indices, index_count);
  742. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  743. meshopt_optimizeVertexCache(out.data, in.data, index_count, vertex_count);
  744. }
  745. template <typename T>
  746. inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count)
  747. {
  748. meshopt_IndexAdapter<T> in(0, indices, index_count);
  749. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  750. meshopt_optimizeVertexCacheStrip(out.data, in.data, index_count, vertex_count);
  751. }
  752. template <typename T>
  753. inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size)
  754. {
  755. meshopt_IndexAdapter<T> in(0, indices, index_count);
  756. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  757. meshopt_optimizeVertexCacheFifo(out.data, in.data, index_count, vertex_count, cache_size);
  758. }
  759. template <typename T>
  760. inline void meshopt_optimizeOverdraw(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float threshold)
  761. {
  762. meshopt_IndexAdapter<T> in(0, indices, index_count);
  763. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  764. meshopt_optimizeOverdraw(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, threshold);
  765. }
  766. template <typename T>
  767. inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count)
  768. {
  769. meshopt_IndexAdapter<T> in(0, indices, index_count);
  770. return meshopt_optimizeVertexFetchRemap(destination, in.data, index_count, vertex_count);
  771. }
  772. template <typename T>
  773. inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)
  774. {
  775. meshopt_IndexAdapter<T> inout(indices, indices, index_count);
  776. return meshopt_optimizeVertexFetch(destination, inout.data, index_count, vertices, vertex_count, vertex_size);
  777. }
  778. template <typename T>
  779. inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
  780. {
  781. meshopt_IndexAdapter<T> in(0, indices, index_count);
  782. return meshopt_encodeIndexBuffer(buffer, buffer_size, in.data, index_count);
  783. }
  784. template <typename T>
  785. inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
  786. {
  787. char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
  788. (void)index_size_valid;
  789. return meshopt_decodeIndexBuffer(destination, index_count, sizeof(T), buffer, buffer_size);
  790. }
  791. template <typename T>
  792. inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
  793. {
  794. meshopt_IndexAdapter<T> in(0, indices, index_count);
  795. return meshopt_encodeIndexSequence(buffer, buffer_size, in.data, index_count);
  796. }
  797. template <typename T>
  798. inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
  799. {
  800. char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
  801. (void)index_size_valid;
  802. return meshopt_decodeIndexSequence(destination, index_count, sizeof(T), buffer, buffer_size);
  803. }
  804. template <typename T>
  805. inline size_t meshopt_simplify(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error)
  806. {
  807. meshopt_IndexAdapter<T> in(0, indices, index_count);
  808. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  809. return meshopt_simplify(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_index_count, target_error, result_error);
  810. }
  811. template <typename T>
  812. inline size_t meshopt_simplifySloppy(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_index_count, float target_error, float* result_error)
  813. {
  814. meshopt_IndexAdapter<T> in(0, indices, index_count);
  815. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  816. return meshopt_simplifySloppy(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_index_count, target_error, result_error);
  817. }
  818. template <typename T>
  819. inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index)
  820. {
  821. meshopt_IndexAdapter<T> in(0, indices, index_count);
  822. meshopt_IndexAdapter<T> out(destination, 0, (index_count / 3) * 5);
  823. return meshopt_stripify(out.data, in.data, index_count, vertex_count, unsigned(restart_index));
  824. }
  825. template <typename T>
  826. inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index)
  827. {
  828. meshopt_IndexAdapter<T> in(0, indices, index_count);
  829. meshopt_IndexAdapter<T> out(destination, 0, (index_count - 2) * 3);
  830. return meshopt_unstripify(out.data, in.data, index_count, unsigned(restart_index));
  831. }
  832. template <typename T>
  833. inline meshopt_VertexCacheStatistics meshopt_analyzeVertexCache(const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size, unsigned int warp_size, unsigned int buffer_size)
  834. {
  835. meshopt_IndexAdapter<T> in(0, indices, index_count);
  836. return meshopt_analyzeVertexCache(in.data, index_count, vertex_count, cache_size, warp_size, buffer_size);
  837. }
  838. template <typename T>
  839. inline meshopt_OverdrawStatistics meshopt_analyzeOverdraw(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  840. {
  841. meshopt_IndexAdapter<T> in(0, indices, index_count);
  842. return meshopt_analyzeOverdraw(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  843. }
  844. template <typename T>
  845. inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size)
  846. {
  847. meshopt_IndexAdapter<T> in(0, indices, index_count);
  848. return meshopt_analyzeVertexFetch(in.data, index_count, vertex_count, vertex_size);
  849. }
  850. template <typename T>
  851. inline size_t meshopt_buildMeshlets(meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t max_vertices, size_t max_triangles, float cone_weight)
  852. {
  853. meshopt_IndexAdapter<T> in(0, indices, index_count);
  854. return meshopt_buildMeshlets(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, max_vertices, max_triangles, cone_weight);
  855. }
  856. template <typename T>
  857. inline size_t meshopt_buildMeshletsScan(meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const T* indices, size_t index_count, size_t vertex_count, size_t max_vertices, size_t max_triangles)
  858. {
  859. meshopt_IndexAdapter<T> in(0, indices, index_count);
  860. return meshopt_buildMeshletsScan(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_count, max_vertices, max_triangles);
  861. }
  862. template <typename T>
  863. inline meshopt_Bounds meshopt_computeClusterBounds(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  864. {
  865. meshopt_IndexAdapter<T> in(0, indices, index_count);
  866. return meshopt_computeClusterBounds(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  867. }
  868. template <typename T>
  869. inline void meshopt_spatialSortTriangles(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
  870. {
  871. meshopt_IndexAdapter<T> in(0, indices, index_count);
  872. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  873. meshopt_spatialSortTriangles(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  874. }
  875. #endif
  876. /**
  877. * Copyright (c) 2016-2021 Arseny Kapoulkine
  878. *
  879. * Permission is hereby granted, free of charge, to any person
  880. * obtaining a copy of this software and associated documentation
  881. * files (the "Software"), to deal in the Software without
  882. * restriction, including without limitation the rights to use,
  883. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  884. * copies of the Software, and to permit persons to whom the
  885. * Software is furnished to do so, subject to the following
  886. * conditions:
  887. *
  888. * The above copyright notice and this permission notice shall be
  889. * included in all copies or substantial portions of the Software.
  890. *
  891. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  892. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  893. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  894. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  895. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  896. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  897. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  898. * OTHER DEALINGS IN THE SOFTWARE.
  899. */