meshoptimizer.h 56 KB

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