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