2
0

meshoptimizer.h 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. /**
  2. * meshoptimizer - version 0.16
  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 160 /* 0.16 */
  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_EXPERIMENTAL 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_EXPERIMENTAL 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. * Experimental: 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_EXPERIMENTAL 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. * Experimental: 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_EXPERIMENTAL size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const unsigned int* indices, size_t index_count);
  204. MESHOPTIMIZER_EXPERIMENTAL 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_EXPERIMENTAL 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. * Experimental: 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_EXPERIMENTAL 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 vertex_count, size_t vertex_size);
  253. MESHOPTIMIZER_EXPERIMENTAL void meshopt_decodeFilterQuat(void* buffer, size_t vertex_count, size_t vertex_size);
  254. MESHOPTIMIZER_EXPERIMENTAL void meshopt_decodeFilterExp(void* buffer, size_t vertex_count, size_t vertex_size);
  255. /**
  256. * Experimental: Mesh simplifier
  257. * Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible
  258. * The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error.
  259. * If not all attributes from the input mesh are required, it's recommended to reindex the mesh using meshopt_generateShadowIndexBuffer prior to simplification.
  260. * Returns the number of indices after simplification, with destination containing new index data
  261. * The resulting index buffer references vertices from the original vertex buffer.
  262. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  263. *
  264. * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
  265. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  266. * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation
  267. * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
  268. */
  269. 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);
  270. /**
  271. * Experimental: Mesh simplifier with attribute metric; attributes follow xyz position data atm (vertex data must contain 3 + attribute_count floats per vertex)
  272. */
  273. 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);
  274. /**
  275. * Experimental: Mesh simplifier (sloppy)
  276. * Reduces the number of triangles in the mesh, sacrificing mesh apperance for simplification performance
  277. * The algorithm doesn't preserve mesh topology but can stop short of the target goal based on target error.
  278. * Returns the number of indices after simplification, with destination containing new index data
  279. * The resulting index buffer references vertices from the original vertex buffer.
  280. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  281. *
  282. * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
  283. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  284. * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation
  285. * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
  286. */
  287. 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);
  288. /**
  289. * Experimental: Point cloud simplifier
  290. * Reduces the number of points in the cloud to reach the given target
  291. * Returns the number of points after simplification, with destination containing new index data
  292. * The resulting index buffer references vertices from the original vertex buffer.
  293. * If the original vertex data isn't required, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
  294. *
  295. * destination must contain enough space for the target index buffer (target_vertex_count elements)
  296. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  297. */
  298. 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);
  299. /**
  300. * Experimental: Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
  301. *
  302. * Absolute error must be *divided* by the scaling factor before passing it to meshopt_simplify as target_error
  303. * Relative error returned by meshopt_simplify via result_error must be *multiplied* by the scaling factor to get absolute error.
  304. */
  305. MESHOPTIMIZER_EXPERIMENTAL float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  306. /**
  307. * Mesh stripifier
  308. * Converts a previously vertex cache optimized triangle list to triangle strip, stitching strips using restart index or degenerate triangles
  309. * Returns the number of indices in the resulting strip, with destination containing new index data
  310. * For maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
  311. * Using restart indices can result in ~10% smaller index buffers, but on some GPUs restart indices may result in decreased performance.
  312. *
  313. * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_stripifyBound
  314. * restart_index should be 0xffff or 0xffffffff depending on index size, or 0 to use degenerate triangles
  315. */
  316. 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);
  317. MESHOPTIMIZER_API size_t meshopt_stripifyBound(size_t index_count);
  318. /**
  319. * Mesh unstripifier
  320. * Converts a triangle strip to a triangle list
  321. * Returns the number of indices in the resulting list, with destination containing new index data
  322. *
  323. * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_unstripifyBound
  324. */
  325. MESHOPTIMIZER_API size_t meshopt_unstripify(unsigned int* destination, const unsigned int* indices, size_t index_count, unsigned int restart_index);
  326. MESHOPTIMIZER_API size_t meshopt_unstripifyBound(size_t index_count);
  327. struct meshopt_VertexCacheStatistics
  328. {
  329. unsigned int vertices_transformed;
  330. unsigned int warps_executed;
  331. float acmr; /* transformed vertices / triangle count; best case 0.5, worst case 3.0, optimum depends on topology */
  332. float atvr; /* transformed vertices / vertex count; best case 1.0, worst case 6.0, optimum is 1.0 (each vertex is transformed once) */
  333. };
  334. /**
  335. * Vertex transform cache analyzer
  336. * Returns cache hit statistics using a simplified FIFO model
  337. * Results may not match actual GPU performance
  338. */
  339. 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);
  340. struct meshopt_OverdrawStatistics
  341. {
  342. unsigned int pixels_covered;
  343. unsigned int pixels_shaded;
  344. float overdraw; /* shaded pixels / covered pixels; best case 1.0 */
  345. };
  346. /**
  347. * Overdraw analyzer
  348. * Returns overdraw statistics using a software rasterizer
  349. * Results may not match actual GPU performance
  350. *
  351. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  352. */
  353. 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);
  354. struct meshopt_VertexFetchStatistics
  355. {
  356. unsigned int bytes_fetched;
  357. float overfetch; /* fetched bytes / vertex buffer size; best case 1.0 (each byte is fetched once) */
  358. };
  359. /**
  360. * Vertex fetch cache analyzer
  361. * Returns cache hit statistics using a simplified direct mapped model
  362. * Results may not match actual GPU performance
  363. */
  364. MESHOPTIMIZER_API struct meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const unsigned int* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
  365. struct meshopt_Meshlet
  366. {
  367. /* offsets within meshlet_vertices and meshlet_triangles arrays with meshlet data */
  368. unsigned int vertex_offset;
  369. unsigned int triangle_offset;
  370. /* number of vertices and triangles used in the meshlet; data is stored in consecutive range defined by offset and count */
  371. unsigned int vertex_count;
  372. unsigned int triangle_count;
  373. };
  374. /**
  375. * Experimental: Meshlet builder
  376. * 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
  377. * The resulting data can be used to render meshes using NVidia programmable mesh shading pipeline, or in other cluster-based renderers.
  378. * When using buildMeshlets, vertex positions need to be provided to minimize the size of the resulting clusters.
  379. * When using buildMeshletsScan, for maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
  380. *
  381. * meshlets must contain enough space for all meshlets, worst case size can be computed with meshopt_buildMeshletsBound
  382. * meshlet_vertices must contain enough space for all meshlets, worst case size is equal to max_meshlets * max_vertices
  383. * meshlet_triangles must contain enough space for all meshlets, worst case size is equal to max_meshlets * max_triangles * 3
  384. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  385. * max_vertices and max_triangles must not exceed implementation limits (max_vertices <= 255 - not 256!, max_triangles <= 512)
  386. * 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
  387. */
  388. MESHOPTIMIZER_EXPERIMENTAL 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);
  389. MESHOPTIMIZER_EXPERIMENTAL 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);
  390. MESHOPTIMIZER_EXPERIMENTAL size_t meshopt_buildMeshletsBound(size_t index_count, size_t max_vertices, size_t max_triangles);
  391. struct meshopt_Bounds
  392. {
  393. /* bounding sphere, useful for frustum and occlusion culling */
  394. float center[3];
  395. float radius;
  396. /* normal cone, useful for backface culling */
  397. float cone_apex[3];
  398. float cone_axis[3];
  399. float cone_cutoff; /* = cos(angle/2) */
  400. /* normal cone axis and cutoff, stored in 8-bit SNORM format; decode using x/127.0 */
  401. signed char cone_axis_s8[3];
  402. signed char cone_cutoff_s8;
  403. };
  404. /**
  405. * Experimental: Cluster bounds generator
  406. * Creates bounding volumes that can be used for frustum, backface and occlusion culling.
  407. *
  408. * For backface culling with orthographic projection, use the following formula to reject backfacing clusters:
  409. * dot(view, cone_axis) >= cone_cutoff
  410. *
  411. * For perspective projection, you can the formula that needs cone apex in addition to axis & cutoff:
  412. * dot(normalize(cone_apex - camera_position), cone_axis) >= cone_cutoff
  413. *
  414. * Alternatively, you can use the formula that doesn't need cone apex and uses bounding sphere instead:
  415. * dot(normalize(center - camera_position), cone_axis) >= cone_cutoff + radius / length(center - camera_position)
  416. * or an equivalent formula that doesn't have a singularity at center = camera_position:
  417. * dot(center - camera_position, cone_axis) >= cone_cutoff * length(center - camera_position) + radius
  418. *
  419. * The formula that uses the apex is slightly more accurate but needs the apex; if you are already using bounding sphere
  420. * to do frustum/occlusion culling, the formula that doesn't use the apex may be preferable.
  421. *
  422. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  423. * index_count/3 should be less than or equal to 512 (the function assumes clusters of limited size)
  424. */
  425. MESHOPTIMIZER_EXPERIMENTAL 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);
  426. MESHOPTIMIZER_EXPERIMENTAL 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);
  427. /**
  428. * Experimental: Spatial sorter
  429. * Generates a remap table that can be used to reorder points for spatial locality.
  430. * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer.
  431. *
  432. * destination must contain enough space for the resulting remap table (vertex_count elements)
  433. */
  434. MESHOPTIMIZER_EXPERIMENTAL void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
  435. /**
  436. * Experimental: Spatial sorter
  437. * Reorders triangles for spatial locality, and generates a new index buffer. The resulting index buffer can be used with other functions like optimizeVertexCache.
  438. *
  439. * destination must contain enough space for the resulting index buffer (index_count elements)
  440. * vertex_positions should have float3 position in the first 12 bytes of each vertex - similar to glVertexPointer
  441. */
  442. 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);
  443. /**
  444. * Set allocation callbacks
  445. * These callbacks will be used instead of the default operator new/operator delete for all temporary allocations in the library.
  446. * Note that all algorithms only allocate memory for temporary use.
  447. * allocate/deallocate are always called in a stack-like order - last pointer to be allocated is deallocated first.
  448. */
  449. MESHOPTIMIZER_API void meshopt_setAllocator(void* (*allocate)(size_t), void (*deallocate)(void*));
  450. #ifdef __cplusplus
  451. } /* extern "C" */
  452. #endif
  453. /* Quantization into commonly supported data formats */
  454. #ifdef __cplusplus
  455. /**
  456. * Quantize a float in [0..1] range into an N-bit fixed point unorm value
  457. * Assumes reconstruction function (q / (2^N-1)), which is the case for fixed-function normalized fixed point conversion
  458. * Maximum reconstruction error: 1/2^(N+1)
  459. */
  460. inline int meshopt_quantizeUnorm(float v, int N);
  461. /**
  462. * Quantize a float in [-1..1] range into an N-bit fixed point snorm value
  463. * Assumes reconstruction function (q / (2^(N-1)-1)), which is the case for fixed-function normalized fixed point conversion (except early OpenGL versions)
  464. * Maximum reconstruction error: 1/2^N
  465. */
  466. inline int meshopt_quantizeSnorm(float v, int N);
  467. /**
  468. * Quantize a float into half-precision floating point value
  469. * Generates +-inf for overflow, preserves NaN, flushes denormals to zero, rounds to nearest
  470. * Representable magnitude range: [6e-5; 65504]
  471. * Maximum relative reconstruction error: 5e-4
  472. */
  473. inline unsigned short meshopt_quantizeHalf(float v);
  474. /**
  475. * Quantize a float into a floating point value with a limited number of significant mantissa bits
  476. * Generates +-inf for overflow, preserves NaN, flushes denormals to zero, rounds to nearest
  477. * Assumes N is in a valid mantissa precision range, which is 1..23
  478. */
  479. inline float meshopt_quantizeFloat(float v, int N);
  480. #endif
  481. /**
  482. * C++ template interface
  483. *
  484. * These functions mirror the C interface the library provides, providing template-based overloads so that
  485. * the caller can use an arbitrary type for the index data, both for input and output.
  486. * When the supplied type is the same size as that of unsigned int, the wrappers are zero-cost; when it's not,
  487. * the wrappers end up allocating memory and copying index data to convert from one type to another.
  488. */
  489. #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
  490. template <typename T>
  491. 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);
  492. template <typename T>
  493. 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);
  494. template <typename T>
  495. inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap);
  496. template <typename T>
  497. 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);
  498. template <typename T>
  499. 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);
  500. template <typename T>
  501. 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);
  502. template <typename T>
  503. 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);
  504. template <typename T>
  505. inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count);
  506. template <typename T>
  507. inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count);
  508. template <typename T>
  509. inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size);
  510. template <typename T>
  511. 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);
  512. template <typename T>
  513. inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count);
  514. template <typename T>
  515. inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
  516. template <typename T>
  517. inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
  518. template <typename T>
  519. inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
  520. template <typename T>
  521. inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
  522. template <typename T>
  523. inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
  524. template <typename T>
  525. 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);
  526. template <typename T>
  527. 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);
  528. template <typename T>
  529. inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index);
  530. template <typename T>
  531. inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index);
  532. template <typename T>
  533. 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);
  534. template <typename T>
  535. 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);
  536. template <typename T>
  537. inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
  538. template <typename T>
  539. 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);
  540. template <typename T>
  541. 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);
  542. template <typename T>
  543. 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);
  544. template <typename T>
  545. 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);
  546. #endif
  547. /* Inline implementation */
  548. #ifdef __cplusplus
  549. inline int meshopt_quantizeUnorm(float v, int N)
  550. {
  551. const float scale = float((1 << N) - 1);
  552. v = (v >= 0) ? v : 0;
  553. v = (v <= 1) ? v : 1;
  554. return int(v * scale + 0.5f);
  555. }
  556. inline int meshopt_quantizeSnorm(float v, int N)
  557. {
  558. const float scale = float((1 << (N - 1)) - 1);
  559. float round = (v >= 0 ? 0.5f : -0.5f);
  560. v = (v >= -1) ? v : -1;
  561. v = (v <= +1) ? v : +1;
  562. return int(v * scale + round);
  563. }
  564. inline unsigned short meshopt_quantizeHalf(float v)
  565. {
  566. union { float f; unsigned int ui; } u = {v};
  567. unsigned int ui = u.ui;
  568. int s = (ui >> 16) & 0x8000;
  569. int em = ui & 0x7fffffff;
  570. /* bias exponent and round to nearest; 112 is relative exponent bias (127-15) */
  571. int h = (em - (112 << 23) + (1 << 12)) >> 13;
  572. /* underflow: flush to zero; 113 encodes exponent -14 */
  573. h = (em < (113 << 23)) ? 0 : h;
  574. /* overflow: infinity; 143 encodes exponent 16 */
  575. h = (em >= (143 << 23)) ? 0x7c00 : h;
  576. /* NaN; note that we convert all types of NaN to qNaN */
  577. h = (em > (255 << 23)) ? 0x7e00 : h;
  578. return (unsigned short)(s | h);
  579. }
  580. inline float meshopt_quantizeFloat(float v, int N)
  581. {
  582. union { float f; unsigned int ui; } u = {v};
  583. unsigned int ui = u.ui;
  584. const int mask = (1 << (23 - N)) - 1;
  585. const int round = (1 << (23 - N)) >> 1;
  586. int e = ui & 0x7f800000;
  587. unsigned int rui = (ui + round) & ~mask;
  588. /* round all numbers except inf/nan; this is important to make sure nan doesn't overflow into -0 */
  589. ui = e == 0x7f800000 ? ui : rui;
  590. /* flush denormals to zero */
  591. ui = e == 0 ? 0 : ui;
  592. u.ui = ui;
  593. return u.f;
  594. }
  595. #endif
  596. /* Internal implementation helpers */
  597. #ifdef __cplusplus
  598. class meshopt_Allocator
  599. {
  600. public:
  601. template <typename T>
  602. struct StorageT
  603. {
  604. static void* (*allocate)(size_t);
  605. static void (*deallocate)(void*);
  606. };
  607. typedef StorageT<void> Storage;
  608. meshopt_Allocator()
  609. : blocks()
  610. , count(0)
  611. {
  612. }
  613. ~meshopt_Allocator()
  614. {
  615. for (size_t i = count; i > 0; --i)
  616. Storage::deallocate(blocks[i - 1]);
  617. }
  618. template <typename T> T* allocate(size_t size)
  619. {
  620. assert(count < sizeof(blocks) / sizeof(blocks[0]));
  621. T* result = static_cast<T*>(Storage::allocate(size > size_t(-1) / sizeof(T) ? size_t(-1) : size * sizeof(T)));
  622. blocks[count++] = result;
  623. return result;
  624. }
  625. private:
  626. void* blocks[24];
  627. size_t count;
  628. };
  629. // This makes sure that allocate/deallocate are lazily generated in translation units that need them and are deduplicated by the linker
  630. template <typename T> void* (*meshopt_Allocator::StorageT<T>::allocate)(size_t) = operator new;
  631. template <typename T> void (*meshopt_Allocator::StorageT<T>::deallocate)(void*) = operator delete;
  632. #endif
  633. /* Inline implementation for C++ templated wrappers */
  634. #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
  635. template <typename T, bool ZeroCopy = sizeof(T) == sizeof(unsigned int)>
  636. struct meshopt_IndexAdapter;
  637. template <typename T>
  638. struct meshopt_IndexAdapter<T, false>
  639. {
  640. T* result;
  641. unsigned int* data;
  642. size_t count;
  643. meshopt_IndexAdapter(T* result_, const T* input, size_t count_)
  644. : result(result_)
  645. , data(0)
  646. , count(count_)
  647. {
  648. size_t size = count > size_t(-1) / sizeof(unsigned int) ? size_t(-1) : count * sizeof(unsigned int);
  649. data = static_cast<unsigned int*>(meshopt_Allocator::Storage::allocate(size));
  650. if (input)
  651. {
  652. for (size_t i = 0; i < count; ++i)
  653. data[i] = input[i];
  654. }
  655. }
  656. ~meshopt_IndexAdapter()
  657. {
  658. if (result)
  659. {
  660. for (size_t i = 0; i < count; ++i)
  661. result[i] = T(data[i]);
  662. }
  663. meshopt_Allocator::Storage::deallocate(data);
  664. }
  665. };
  666. template <typename T>
  667. struct meshopt_IndexAdapter<T, true>
  668. {
  669. unsigned int* data;
  670. meshopt_IndexAdapter(T* result, const T* input, size_t)
  671. : data(reinterpret_cast<unsigned int*>(result ? result : const_cast<T*>(input)))
  672. {
  673. }
  674. };
  675. template <typename T>
  676. 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)
  677. {
  678. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  679. return meshopt_generateVertexRemap(destination, indices ? in.data : 0, index_count, vertices, vertex_count, vertex_size);
  680. }
  681. template <typename T>
  682. 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)
  683. {
  684. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  685. return meshopt_generateVertexRemapMulti(destination, indices ? in.data : 0, index_count, vertex_count, streams, stream_count);
  686. }
  687. template <typename T>
  688. inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap)
  689. {
  690. meshopt_IndexAdapter<T> in(0, indices, indices ? index_count : 0);
  691. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  692. meshopt_remapIndexBuffer(out.data, indices ? in.data : 0, index_count, remap);
  693. }
  694. template <typename T>
  695. 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)
  696. {
  697. meshopt_IndexAdapter<T> in(0, indices, index_count);
  698. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  699. meshopt_generateShadowIndexBuffer(out.data, in.data, index_count, vertices, vertex_count, vertex_size, vertex_stride);
  700. }
  701. template <typename T>
  702. 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)
  703. {
  704. meshopt_IndexAdapter<T> in(0, indices, index_count);
  705. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  706. meshopt_generateShadowIndexBufferMulti(out.data, in.data, index_count, vertex_count, streams, stream_count);
  707. }
  708. template <typename T>
  709. 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)
  710. {
  711. meshopt_IndexAdapter<T> in(0, indices, index_count);
  712. meshopt_IndexAdapter<T> out(destination, 0, index_count * 2);
  713. meshopt_generateAdjacencyIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  714. }
  715. template <typename T>
  716. 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)
  717. {
  718. meshopt_IndexAdapter<T> in(0, indices, index_count);
  719. meshopt_IndexAdapter<T> out(destination, 0, index_count * 4);
  720. meshopt_generateTessellationIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  721. }
  722. template <typename T>
  723. inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count)
  724. {
  725. meshopt_IndexAdapter<T> in(0, indices, index_count);
  726. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  727. meshopt_optimizeVertexCache(out.data, in.data, index_count, vertex_count);
  728. }
  729. template <typename T>
  730. inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count)
  731. {
  732. meshopt_IndexAdapter<T> in(0, indices, index_count);
  733. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  734. meshopt_optimizeVertexCacheStrip(out.data, in.data, index_count, vertex_count);
  735. }
  736. template <typename T>
  737. inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size)
  738. {
  739. meshopt_IndexAdapter<T> in(0, indices, index_count);
  740. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  741. meshopt_optimizeVertexCacheFifo(out.data, in.data, index_count, vertex_count, cache_size);
  742. }
  743. template <typename T>
  744. 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)
  745. {
  746. meshopt_IndexAdapter<T> in(0, indices, index_count);
  747. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  748. meshopt_optimizeOverdraw(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, threshold);
  749. }
  750. template <typename T>
  751. inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count)
  752. {
  753. meshopt_IndexAdapter<T> in(0, indices, index_count);
  754. return meshopt_optimizeVertexFetchRemap(destination, in.data, index_count, vertex_count);
  755. }
  756. template <typename T>
  757. inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)
  758. {
  759. meshopt_IndexAdapter<T> inout(indices, indices, index_count);
  760. return meshopt_optimizeVertexFetch(destination, inout.data, index_count, vertices, vertex_count, vertex_size);
  761. }
  762. template <typename T>
  763. inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
  764. {
  765. meshopt_IndexAdapter<T> in(0, indices, index_count);
  766. return meshopt_encodeIndexBuffer(buffer, buffer_size, in.data, index_count);
  767. }
  768. template <typename T>
  769. inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
  770. {
  771. char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
  772. (void)index_size_valid;
  773. return meshopt_decodeIndexBuffer(destination, index_count, sizeof(T), buffer, buffer_size);
  774. }
  775. template <typename T>
  776. inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
  777. {
  778. meshopt_IndexAdapter<T> in(0, indices, index_count);
  779. return meshopt_encodeIndexSequence(buffer, buffer_size, in.data, index_count);
  780. }
  781. template <typename T>
  782. inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
  783. {
  784. char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
  785. (void)index_size_valid;
  786. return meshopt_decodeIndexSequence(destination, index_count, sizeof(T), buffer, buffer_size);
  787. }
  788. template <typename T>
  789. 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)
  790. {
  791. meshopt_IndexAdapter<T> in(0, indices, index_count);
  792. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  793. return meshopt_simplify(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_index_count, target_error, result_error);
  794. }
  795. template <typename T>
  796. 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)
  797. {
  798. meshopt_IndexAdapter<T> in(0, indices, index_count);
  799. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  800. return meshopt_simplifySloppy(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_index_count, target_error, result_error);
  801. }
  802. template <typename T>
  803. inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index)
  804. {
  805. meshopt_IndexAdapter<T> in(0, indices, index_count);
  806. meshopt_IndexAdapter<T> out(destination, 0, (index_count / 3) * 5);
  807. return meshopt_stripify(out.data, in.data, index_count, vertex_count, unsigned(restart_index));
  808. }
  809. template <typename T>
  810. inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index)
  811. {
  812. meshopt_IndexAdapter<T> in(0, indices, index_count);
  813. meshopt_IndexAdapter<T> out(destination, 0, (index_count - 2) * 3);
  814. return meshopt_unstripify(out.data, in.data, index_count, unsigned(restart_index));
  815. }
  816. template <typename T>
  817. 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)
  818. {
  819. meshopt_IndexAdapter<T> in(0, indices, index_count);
  820. return meshopt_analyzeVertexCache(in.data, index_count, vertex_count, cache_size, warp_size, buffer_size);
  821. }
  822. template <typename T>
  823. 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)
  824. {
  825. meshopt_IndexAdapter<T> in(0, indices, index_count);
  826. return meshopt_analyzeOverdraw(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  827. }
  828. template <typename T>
  829. inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size)
  830. {
  831. meshopt_IndexAdapter<T> in(0, indices, index_count);
  832. return meshopt_analyzeVertexFetch(in.data, index_count, vertex_count, vertex_size);
  833. }
  834. template <typename T>
  835. 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)
  836. {
  837. meshopt_IndexAdapter<T> in(0, indices, index_count);
  838. 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);
  839. }
  840. template <typename T>
  841. 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)
  842. {
  843. meshopt_IndexAdapter<T> in(0, indices, index_count);
  844. return meshopt_buildMeshletsScan(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_count, max_vertices, max_triangles);
  845. }
  846. template <typename T>
  847. 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)
  848. {
  849. meshopt_IndexAdapter<T> in(0, indices, index_count);
  850. return meshopt_computeClusterBounds(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  851. }
  852. template <typename T>
  853. 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)
  854. {
  855. meshopt_IndexAdapter<T> in(0, indices, index_count);
  856. meshopt_IndexAdapter<T> out(destination, 0, index_count);
  857. meshopt_spatialSortTriangles(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
  858. }
  859. #endif
  860. /**
  861. * Copyright (c) 2016-2021 Arseny Kapoulkine
  862. *
  863. * Permission is hereby granted, free of charge, to any person
  864. * obtaining a copy of this software and associated documentation
  865. * files (the "Software"), to deal in the Software without
  866. * restriction, including without limitation the rights to use,
  867. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  868. * copies of the Software, and to permit persons to whom the
  869. * Software is furnished to do so, subject to the following
  870. * conditions:
  871. *
  872. * The above copyright notice and this permission notice shall be
  873. * included in all copies or substantial portions of the Software.
  874. *
  875. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  876. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  877. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  878. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  879. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  880. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  881. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  882. * OTHER DEALINGS IN THE SOFTWARE.
  883. */