Debian Codegraph @

thirdparty/meshoptimizer/meshoptimizer.h

raw symbols SCIP
1 /**
2 * meshoptimizer - version 1.0
3 *
4 * Copyright (C) 2016-2025, 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
11 #include <assert.h>
12 #include <stddef.h>
13
14 /* Version macro; major * 1000 + minor * 10 + patch */
15 #define MESHOPTIMIZER_VERSION 1000 /* 1.0 */
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 #ifndef MESHOPTIMIZER_EXPERIMENTAL
33 #define MESHOPTIMIZER_EXPERIMENTAL MESHOPTIMIZER_API
34 #endif
35
36 /* C interface */
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41
42 /**
43 * Vertex attribute stream
44 * Each element takes size bytes, beginning at data, with stride controlling the spacing between successive elements (stride >= size).
45 */
46 struct meshopt_Stream
47 {
48 const void* data;
49 size_t size;
50 size_t stride;
51 };
52
53 /**
54 * Generates a vertex remap table from the vertex buffer and an optional index buffer and returns number of unique vertices
55 * As a result, all vertices that are binary equivalent map to the same (new) location, with no gaps in the resulting sequence.
56 * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer/meshopt_remapIndexBuffer.
57 * Note that binary equivalence considers all vertex_size bytes, including padding which should be zero-initialized.
58 *
59 * destination must contain enough space for the resulting remap table (vertex_count elements)
60 * indices can be NULL if the input is unindexed
61 */
62 MESHOPTIMIZER_API size_t meshopt_generateVertexRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
63
64 /**
65 * Generates a vertex remap table from multiple vertex streams and an optional index buffer and returns number of unique vertices
66 * As a result, all vertices that are binary equivalent map to the same (new) location, with no gaps in the resulting sequence.
67 * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer/meshopt_remapIndexBuffer.
68 * To remap vertex buffers, you will need to call meshopt_remapVertexBuffer for each vertex stream.
69 * Note that binary equivalence considers all size bytes in each stream, including padding which should be zero-initialized.
70 *
71 * destination must contain enough space for the resulting remap table (vertex_count elements)
72 * indices can be NULL if the input is unindexed
73 * stream_count must be <= 16
74 */
75 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);
76
77 /**
78 * Generates a vertex remap table from the vertex buffer and an optional index buffer and returns number of unique vertices
79 * As a result, all vertices that are equivalent map to the same (new) location, with no gaps in the resulting sequence.
80 * Equivalence is checked in two steps: vertex positions are compared for equality, and then the user-specified equality function is called (if provided).
81 * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer/meshopt_remapIndexBuffer.
82 *
83 * destination must contain enough space for the resulting remap table (vertex_count elements)
84 * indices can be NULL if the input is unindexed
85 * vertex_positions should have float3 position in the first 12 bytes of each vertex
86 * callback can be NULL if no additional equality check is needed; otherwise, it should return 1 if vertices with specified indices are equivalent and 0 if they are not
87 */
88 MESHOPTIMIZER_API size_t meshopt_generateVertexRemapCustom(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, int (*callback)(void*, unsigned int, unsigned int), void* context);
89
90 /**
91 * Generates vertex buffer from the source vertex buffer and remap table generated by meshopt_generateVertexRemap
92 *
93 * destination must contain enough space for the resulting vertex buffer (unique_vertex_count elements, returned by meshopt_generateVertexRemap)
94 * vertex_count should be the initial vertex count and not the value returned by meshopt_generateVertexRemap
95 */
96 MESHOPTIMIZER_API void meshopt_remapVertexBuffer(void* destination, const void* vertices, size_t vertex_count, size_t vertex_size, const unsigned int* remap);
97
98 /**
99 * Generate index buffer from the source index buffer and remap table generated by meshopt_generateVertexRemap
100 *
101 * destination must contain enough space for the resulting index buffer (index_count elements)
102 * indices can be NULL if the input is unindexed
103 */
104 MESHOPTIMIZER_API void meshopt_remapIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const unsigned int* remap);
105
106 /**
107 * Generate index buffer that can be used for more efficient rendering when only a subset of the vertex attributes is necessary
108 * All vertices that are binary equivalent (wrt first vertex_size bytes) map to the first vertex in the original vertex buffer.
109 * 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.
110 * Note that binary equivalence considers all vertex_size bytes, including padding which should be zero-initialized.
111 *
112 * destination must contain enough space for the resulting index buffer (index_count elements)
113 */
114 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);
115
116 /**
117 * Generate index buffer that can be used for more efficient rendering when only a subset of the vertex attributes is necessary
118 * All vertices that are binary equivalent (wrt specified streams) map to the first vertex in the original vertex buffer.
119 * 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.
120 * Note that binary equivalence considers all size bytes in each stream, including padding which should be zero-initialized.
121 *
122 * destination must contain enough space for the resulting index buffer (index_count elements)
123 * stream_count must be <= 16
124 */
125 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);
126
127 /**
128 * Generates a remap table that maps all vertices with the same position to the same (existing) index.
129 * Similarly to meshopt_generateShadowIndexBuffer, this can be helpful to pre-process meshes for position-only rendering.
130 * This can also be used to implement algorithms that require positional-only connectivity, such as hierarchical simplification.
131 *
132 * destination must contain enough space for the resulting remap table (vertex_count elements)
133 * vertex_positions should have float3 position in the first 12 bytes of each vertex
134 */
135 MESHOPTIMIZER_API void meshopt_generatePositionRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
136
137 /**
138 * Generate index buffer that can be used as a geometry shader input with triangle adjacency topology
139 * Each triangle is converted into a 6-vertex patch with the following layout:
140 * - 0, 2, 4: original triangle vertices
141 * - 1, 3, 5: vertices adjacent to edges 02, 24 and 40
142 * The resulting patch can be rendered with geometry shaders using e.g. VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY.
143 * This can be used to implement algorithms like silhouette detection/expansion and other forms of GS-driven rendering.
144 *
145 * destination must contain enough space for the resulting index buffer (index_count*2 elements)
146 * vertex_positions should have float3 position in the first 12 bytes of each vertex
147 */
148 MESHOPTIMIZER_API void meshopt_generateAdjacencyIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
149
150 /**
151 * Generate index buffer that can be used for PN-AEN tessellation with crack-free displacement
152 * Each triangle is converted into a 12-vertex patch with the following layout:
153 * - 0, 1, 2: original triangle vertices
154 * - 3, 4: opposing edge for edge 0, 1
155 * - 5, 6: opposing edge for edge 1, 2
156 * - 7, 8: opposing edge for edge 2, 0
157 * - 9, 10, 11: dominant vertices for corners 0, 1, 2
158 * The resulting patch can be rendered with hardware tessellation using PN-AEN and displacement mapping.
159 * See "Tessellation on Any Budget" (John McDonald, GDC 2011) for implementation details.
160 *
161 * destination must contain enough space for the resulting index buffer (index_count*4 elements)
162 * vertex_positions should have float3 position in the first 12 bytes of each vertex
163 */
164 MESHOPTIMIZER_API void meshopt_generateTessellationIndexBuffer(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
165
166 /**
167 * Generate index buffer that can be used for visibility buffer rendering and returns the size of the reorder table
168 * Each triangle's provoking vertex index is equal to primitive id; this allows passing it to the fragment shader using flat/nointerpolation attribute.
169 * This is important for performance on hardware where primitive id can't be accessed efficiently in fragment shader.
170 * The reorder table stores the original vertex id for each vertex in the new index buffer, and should be used in the vertex shader to load vertex data.
171 * The provoking vertex is assumed to be the first vertex in the triangle; if this is not the case (OpenGL), rotate each triangle (abc -> bca) before rendering.
172 * For maximum efficiency the input index buffer should be optimized for vertex cache first.
173 *
174 * destination must contain enough space for the resulting index buffer (index_count elements)
175 * reorder must contain enough space for the worst case reorder table (vertex_count + index_count/3 elements)
176 */
177 MESHOPTIMIZER_API size_t meshopt_generateProvokingIndexBuffer(unsigned int* destination, unsigned int* reorder, const unsigned int* indices, size_t index_count, size_t vertex_count);
178
179 /**
180 * Vertex transform cache optimizer
181 * Reorders indices to reduce the number of GPU vertex shader invocations
182 * If index buffer contains multiple ranges for multiple draw calls, this function needs to be called on each range individually.
183 *
184 * destination must contain enough space for the resulting index buffer (index_count elements)
185 */
186 MESHOPTIMIZER_API void meshopt_optimizeVertexCache(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
187
188 /**
189 * Vertex transform cache optimizer for strip-like caches
190 * Produces inferior results to meshopt_optimizeVertexCache from the GPU vertex cache perspective
191 * However, the resulting index order is more optimal if the goal is to reduce the triangle strip length or improve compression efficiency
192 *
193 * destination must contain enough space for the resulting index buffer (index_count elements)
194 */
195 MESHOPTIMIZER_API void meshopt_optimizeVertexCacheStrip(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
196
197 /**
198 * Vertex transform cache optimizer for FIFO caches
199 * Reorders indices to reduce the number of GPU vertex shader invocations
200 * Generally takes ~3x less time to optimize meshes but produces inferior results compared to meshopt_optimizeVertexCache
201 * If index buffer contains multiple ranges for multiple draw calls, this function needs to be called on each range individually.
202 *
203 * destination must contain enough space for the resulting index buffer (index_count elements)
204 * cache_size should be less than the actual GPU cache size to avoid cache thrashing
205 */
206 MESHOPTIMIZER_API void meshopt_optimizeVertexCacheFifo(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count, unsigned int cache_size);
207
208 /**
209 * Overdraw optimizer
210 * Reorders indices to reduce the number of GPU vertex shader invocations and the pixel overdraw
211 * If index buffer contains multiple ranges for multiple draw calls, this function needs to be called on each range individually.
212 *
213 * destination must contain enough space for the resulting index buffer (index_count elements)
214 * indices must contain index data that is the result of meshopt_optimizeVertexCache (*not* the original mesh indices!)
215 * vertex_positions should have float3 position in the first 12 bytes of each vertex
216 * threshold indicates how much the overdraw optimizer can degrade vertex cache efficiency (1.05 = up to 5%) to reduce overdraw more efficiently
217 */
218 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);
219
220 /**
221 * Vertex fetch cache optimizer
222 * Reorders vertices and changes indices to reduce the amount of GPU memory fetches during vertex processing
223 * Returns the number of unique vertices, which is the same as input vertex count unless some vertices are unused
224 * This function works for a single vertex stream; for multiple vertex streams, use meshopt_optimizeVertexFetchRemap + meshopt_remapVertexBuffer for each stream.
225 *
226 * destination must contain enough space for the resulting vertex buffer (vertex_count elements)
227 * indices is used both as an input and as an output index buffer
228 */
229 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);
230
231 /**
232 * Vertex fetch cache optimizer
233 * Generates vertex remap to reduce the amount of GPU memory fetches during vertex processing
234 * Returns the number of unique vertices, which is the same as input vertex count unless some vertices are unused
235 * The resulting remap table should be used to reorder vertex/index buffers using meshopt_remapVertexBuffer/meshopt_remapIndexBuffer
236 *
237 * destination must contain enough space for the resulting remap table (vertex_count elements)
238 */
239 MESHOPTIMIZER_API size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const unsigned int* indices, size_t index_count, size_t vertex_count);
240
241 /**
242 * Index buffer encoder
243 * 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.
244 * Input index buffer must represent a triangle list.
245 * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
246 * For maximum efficiency the index buffer being encoded has to be optimized for vertex cache and vertex fetch first.
247 *
248 * buffer must contain enough space for the encoded index buffer (use meshopt_encodeIndexBufferBound to compute worst case size)
249 */
250 MESHOPTIMIZER_API size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const unsigned int* indices, size_t index_count);
251 MESHOPTIMIZER_API size_t meshopt_encodeIndexBufferBound(size_t index_count, size_t vertex_count);
252
253 /**
254 * Set index encoder format version (defaults to 1)
255 *
256 * version must specify the data format version to encode; valid values are 0 (decodable by all library versions) and 1 (decodable by 0.14+)
257 */
258 MESHOPTIMIZER_API void meshopt_encodeIndexVersion(int version);
259
260 /**
261 * Index buffer decoder
262 * Decodes index data from an array of bytes generated by meshopt_encodeIndexBuffer
263 * Returns 0 if decoding was successful, and an error code otherwise
264 * The decoder is safe to use for untrusted input, but it may produce garbage data (e.g. out of range indices).
265 *
266 * destination must contain enough space for the resulting index buffer (index_count elements)
267 */
268 MESHOPTIMIZER_API int meshopt_decodeIndexBuffer(void* destination, size_t index_count, size_t index_size, const unsigned char* buffer, size_t buffer_size);
269
270 /**
271 * Get encoded index format version
272 * Returns format version of the encoded index buffer/sequence, or -1 if the buffer header is invalid
273 * Note that a non-negative value doesn't guarantee that the buffer will be decoded correctly if the input is malformed.
274 */
275 MESHOPTIMIZER_API int meshopt_decodeIndexVersion(const unsigned char* buffer, size_t buffer_size);
276
277 /**
278 * Index sequence encoder
279 * Encodes index sequence into an array of bytes that is generally smaller and compresses better compared to original.
280 * Input index sequence can represent arbitrary topology; for triangle lists meshopt_encodeIndexBuffer is likely to be better.
281 * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
282 *
283 * buffer must contain enough space for the encoded index sequence (use meshopt_encodeIndexSequenceBound to compute worst case size)
284 */
285 MESHOPTIMIZER_API size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const unsigned int* indices, size_t index_count);
286 MESHOPTIMIZER_API size_t meshopt_encodeIndexSequenceBound(size_t index_count, size_t vertex_count);
287
288 /**
289 * Index sequence decoder
290 * Decodes index data from an array of bytes generated by meshopt_encodeIndexSequence
291 * Returns 0 if decoding was successful, and an error code otherwise
292 * The decoder is safe to use for untrusted input, but it may produce garbage data (e.g. out of range indices).
293 *
294 * destination must contain enough space for the resulting index sequence (index_count elements)
295 */
296 MESHOPTIMIZER_API int meshopt_decodeIndexSequence(void* destination, size_t index_count, size_t index_size, const unsigned char* buffer, size_t buffer_size);
297
298 /**
299 * Vertex buffer encoder
300 * Encodes vertex data into an array of bytes that is generally smaller and compresses better compared to original.
301 * Returns encoded data size on success, 0 on error; the only error condition is if buffer doesn't have enough space
302 * This function works for a single vertex stream; for multiple vertex streams, call meshopt_encodeVertexBuffer for each stream.
303 * Note that all vertex_size bytes of each vertex are encoded verbatim, including padding which should be zero-initialized.
304 * For maximum efficiency the vertex buffer being encoded has to be quantized and optimized for locality of reference (cache/fetch) first.
305 *
306 * buffer must contain enough space for the encoded vertex buffer (use meshopt_encodeVertexBufferBound to compute worst case size)
307 * vertex_size must be a multiple of 4 (and <= 256)
308 */
309 MESHOPTIMIZER_API size_t meshopt_encodeVertexBuffer(unsigned char* buffer, size_t buffer_size, const void* vertices, size_t vertex_count, size_t vertex_size);
310 MESHOPTIMIZER_API size_t meshopt_encodeVertexBufferBound(size_t vertex_count, size_t vertex_size);
311
312 /**
313 * Vertex buffer encoder
314 * Encodes vertex data just like meshopt_encodeVertexBuffer, but allows to override compression level.
315 * For compression level to take effect, the vertex encoding version must be set to 1.
316 * The default compression level implied by meshopt_encodeVertexBuffer is 2.
317 *
318 * buffer must contain enough space for the encoded vertex buffer (use meshopt_encodeVertexBufferBound to compute worst case size)
319 * vertex_size must be a multiple of 4 (and <= 256)
320 * level should be in the range [0, 3] with 0 being the fastest and 3 being the slowest and producing the best compression ratio.
321 * version should be -1 to use the default version (specified via meshopt_encodeVertexVersion), or 0/1 to override the version; per above, level won't take effect if version is 0.
322 */
323 MESHOPTIMIZER_API size_t meshopt_encodeVertexBufferLevel(unsigned char* buffer, size_t buffer_size, const void* vertices, size_t vertex_count, size_t vertex_size, int level, int version);
324
325 /**
326 * Set vertex encoder format version (defaults to 1)
327 *
328 * version must specify the data format version to encode; valid values are 0 (decodable by all library versions) and 1 (decodable by 0.23+)
329 */
330 MESHOPTIMIZER_API void meshopt_encodeVertexVersion(int version);
331
332 /**
333 * Vertex buffer decoder
334 * Decodes vertex data from an array of bytes generated by meshopt_encodeVertexBuffer
335 * Returns 0 if decoding was successful, and an error code otherwise
336 * The decoder is safe to use for untrusted input, but it may produce garbage data.
337 *
338 * destination must contain enough space for the resulting vertex buffer (vertex_count * vertex_size bytes)
339 * vertex_size must be a multiple of 4 (and <= 256)
340 */
341 MESHOPTIMIZER_API int meshopt_decodeVertexBuffer(void* destination, size_t vertex_count, size_t vertex_size, const unsigned char* buffer, size_t buffer_size);
342
343 /**
344 * Get encoded vertex format version
345 * Returns format version of the encoded vertex buffer, or -1 if the buffer header is invalid
346 * Note that a non-negative value doesn't guarantee that the buffer will be decoded correctly if the input is malformed.
347 */
348 MESHOPTIMIZER_API int meshopt_decodeVertexVersion(const unsigned char* buffer, size_t buffer_size);
349
350 /**
351 * Vertex buffer filters
352 * These functions can be used to filter output of meshopt_decodeVertexBuffer in-place.
353 *
354 * meshopt_decodeFilterOct decodes octahedral encoding of a unit vector with K-bit signed X/Y as an input; Z must store 1.0f.
355 * 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.
356 *
357 * meshopt_decodeFilterQuat decodes 3-component quaternion encoding with K-bit component encoding and a 2-bit component index indicating which component to reconstruct.
358 * Each component is stored as an 16-bit integer; stride must be equal to 8.
359 *
360 * meshopt_decodeFilterExp decodes exponential encoding of floating-point data with 8-bit exponent and 24-bit integer mantissa as 2^E*M.
361 * Each 32-bit component is decoded in isolation; stride must be divisible by 4.
362 *
363 * meshopt_decodeFilterColor decodes RGBA colors from YCoCg (+A) color encoding where RGB is converted to YCoCg space with K-bit component encoding, and A is stored using K-1 bits.
364 * Each component is stored as an 8-bit or 16-bit normalized integer; stride must be equal to 4 or 8.
365 */
366 MESHOPTIMIZER_API void meshopt_decodeFilterOct(void* buffer, size_t count, size_t stride);
367 MESHOPTIMIZER_API void meshopt_decodeFilterQuat(void* buffer, size_t count, size_t stride);
368 MESHOPTIMIZER_API void meshopt_decodeFilterExp(void* buffer, size_t count, size_t stride);
369 MESHOPTIMIZER_API void meshopt_decodeFilterColor(void* buffer, size_t count, size_t stride);
370
371 /**
372 * Vertex buffer filter encoders
373 * These functions can be used to encode data in a format that meshopt_decodeFilter can decode
374 *
375 * meshopt_encodeFilterOct encodes unit vectors with K-bit (2 <= K <= 16) signed X/Y as an output.
376 * Each component is stored as an 8-bit or 16-bit normalized integer; stride must be equal to 4 or 8. Z will store 1.0f, W is preserved as is.
377 * Input data must contain 4 floats for every vector (count*4 total).
378 *
379 * meshopt_encodeFilterQuat encodes unit quaternions with K-bit (4 <= K <= 16) component encoding.
380 * Each component is stored as an 16-bit integer; stride must be equal to 8.
381 * Input data must contain 4 floats for every quaternion (count*4 total).
382 *
383 * meshopt_encodeFilterExp encodes arbitrary (finite) floating-point data with 8-bit exponent and K-bit integer mantissa (1 <= K <= 24).
384 * Exponent can be shared between all components of a given vector as defined by stride or all values of a given component; stride must be divisible by 4.
385 * Input data must contain stride/4 floats for every vector (count*stride/4 total).
386 *
387 * meshopt_encodeFilterColor encodes RGBA color data by converting RGB to YCoCg color space with K-bit (2 <= K <= 16) component encoding; A is stored using K-1 bits.
388 * Each component is stored as an 8-bit or 16-bit integer; stride must be equal to 4 or 8.
389 * Input data must contain 4 floats for every color (count*4 total).
390 */
391 enum meshopt_EncodeExpMode
392 {
393 /* When encoding exponents, use separate values for each component (maximum quality) */
394 meshopt_EncodeExpSeparate,
395 /* When encoding exponents, use shared value for all components of each vector (better compression) */
396 meshopt_EncodeExpSharedVector,
397 /* When encoding exponents, use shared value for each component of all vectors (best compression) */
398 meshopt_EncodeExpSharedComponent,
399 /* When encoding exponents, use separate values for each component, but clamp to 0 (good quality if very small values are not important) */
400 meshopt_EncodeExpClamped,
401 };
402
403 MESHOPTIMIZER_API void meshopt_encodeFilterOct(void* destination, size_t count, size_t stride, int bits, const float* data);
404 MESHOPTIMIZER_API void meshopt_encodeFilterQuat(void* destination, size_t count, size_t stride, int bits, const float* data);
405 MESHOPTIMIZER_API void meshopt_encodeFilterExp(void* destination, size_t count, size_t stride, int bits, const float* data, enum meshopt_EncodeExpMode mode);
406 MESHOPTIMIZER_API void meshopt_encodeFilterColor(void* destination, size_t count, size_t stride, int bits, const float* data);
407
408 /**
409 * Simplification options
410 */
411 enum
412 {
413 /* 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. */
414 meshopt_SimplifyLockBorder = 1 << 0,
415 /* Improve simplification performance assuming input indices are a sparse subset of the mesh. Note that error becomes relative to subset extents. */
416 meshopt_SimplifySparse = 1 << 1,
417 /* Treat error limit and resulting error as absolute instead of relative to mesh extents. */
418 meshopt_SimplifyErrorAbsolute = 1 << 2,
419 /* Remove disconnected parts of the mesh during simplification incrementally, regardless of the topological restrictions inside components. */
420 meshopt_SimplifyPrune = 1 << 3,
421 /* Produce more regular triangle sizes and shapes during simplification, at some cost to geometric and attribute quality. */
422 meshopt_SimplifyRegularize = 1 << 4,
423 /* Experimental: Allow collapses across attribute discontinuities, except for vertices that are tagged with meshopt_SimplifyVertex_Protect in vertex_lock. */
424 meshopt_SimplifyPermissive = 1 << 5,
425 };
426
427 /**
428 * Experimental: Simplification vertex flags/locks, for use in `vertex_lock` arrays in simplification APIs
429 */
430 enum
431 {
432 /* Do not move this vertex. */
433 meshopt_SimplifyVertex_Lock = 1 << 0,
434 /* Protect attribute discontinuity at this vertex; must be used together with meshopt_SimplifyPermissive option. */
435 meshopt_SimplifyVertex_Protect = 1 << 1,
436 };
437
438 /**
439 * Mesh simplifier
440 * Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible
441 * The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error.
442 * If not all attributes from the input mesh are needed, it's recommended to reindex the mesh without them prior to simplification.
443 * Returns the number of indices after simplification, with destination containing new index data
444 *
445 * The resulting index buffer references vertices from the original vertex buffer.
446 * If the original vertex data isn't needed, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
447 *
448 * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
449 * vertex_positions should have float3 position in the first 12 bytes of each vertex
450 * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation; value range [0..1]
451 * options must be a bitmask composed of meshopt_SimplifyX options; 0 is a safe default
452 * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
453 */
454 MESHOPTIMIZER_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);
455
456 /**
457 * Mesh simplifier with attribute metric
458 * Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible.
459 * Similar to meshopt_simplify, but incorporates attribute values into the error metric used to prioritize simplification order.
460 * The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error.
461 * If not all attributes from the input mesh are needed, it's recommended to reindex the mesh without them prior to simplification.
462 * Returns the number of indices after simplification, with destination containing new index data
463 *
464 * The resulting index buffer references vertices from the original vertex buffer.
465 * If the original vertex data isn't needed, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
466 * Note that the number of attributes with non-zero weights affects memory requirements and running time.
467 *
468 * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
469 * vertex_positions should have float3 position in the first 12 bytes of each vertex
470 * vertex_attributes should have attribute_count floats for each vertex
471 * attribute_weights should have attribute_count floats in total; the weights determine relative priority of attributes between each other and wrt position
472 * attribute_count must be <= 32
473 * vertex_lock can be NULL; when it's not NULL, it should have a value for each vertex; 1 denotes vertices that can't be moved
474 * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation; value range [0..1]
475 * options must be a bitmask composed of meshopt_SimplifyX options; 0 is a safe default
476 * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
477 */
478 MESHOPTIMIZER_API size_t meshopt_simplifyWithAttributes(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error);
479
480 /**
481 * Mesh simplifier with position/attribute update
482 * Reduces the number of triangles in the mesh, attempting to preserve mesh appearance as much as possible.
483 * Similar to meshopt_simplifyWithAttributes, but destructively updates positions and attribute values for optimal appearance.
484 * The algorithm tries to preserve mesh topology and can stop short of the target goal based on topology constraints or target error.
485 * If not all attributes from the input mesh are needed, it's recommended to reindex the mesh without them prior to simplification.
486 * Returns the number of indices after simplification, indices are destructively updated with new index data
487 *
488 * The updated index buffer references vertices from the original vertex buffer, however the vertex positions and attributes are updated in-place.
489 * Creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended; if the original vertex data is needed, it should be copied before simplification.
490 * Note that the number of attributes with non-zero weights affects memory requirements and running time. Attributes with zero weights are not updated.
491 *
492 * vertex_positions should have float3 position in the first 12 bytes of each vertex
493 * vertex_attributes should have attribute_count floats for each vertex
494 * attribute_weights should have attribute_count floats in total; the weights determine relative priority of attributes between each other and wrt position
495 * attribute_count must be <= 32
496 * vertex_lock can be NULL; when it's not NULL, it should have a value for each vertex; 1 denotes vertices that can't be moved
497 * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation; value range [0..1]
498 * options must be a bitmask composed of meshopt_SimplifyX options; 0 is a safe default
499 * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
500 */
501 MESHOPTIMIZER_API size_t meshopt_simplifyWithUpdate(unsigned int* indices, size_t index_count, float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error);
502
503 /**
504 * Mesh simplifier (sloppy)
505 * Reduces the number of triangles in the mesh, sacrificing mesh appearance for simplification performance
506 * The algorithm doesn't preserve mesh topology but can stop short of the target goal based on target error.
507 * Returns the number of indices after simplification, with destination containing new index data
508 * The resulting index buffer references vertices from the original vertex buffer.
509 * If the original vertex data isn't needed, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
510 *
511 * destination must contain enough space for the target index buffer, worst case is index_count elements (*not* target_index_count)!
512 * vertex_positions should have float3 position in the first 12 bytes of each vertex
513 * vertex_lock can be NULL; when it's not NULL, it should have a value for each vertex; vertices that can't be moved should set 1 consistently for all indices with the same position
514 * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation; value range [0..1]
515 * result_error can be NULL; when it's not NULL, it will contain the resulting (relative) error after simplification
516 */
517 MESHOPTIMIZER_API 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, const unsigned char* vertex_lock, size_t target_index_count, float target_error, float* result_error);
518
519 /**
520 * Mesh simplifier (pruner)
521 * Reduces the number of triangles in the mesh by removing small isolated parts of the mesh
522 * Returns the number of indices after simplification, with destination containing new index data
523 * The resulting index buffer references vertices from the original vertex buffer.
524 * If the original vertex data isn't needed, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
525 *
526 * destination must contain enough space for the target index buffer, worst case is index_count elements
527 * vertex_positions should have float3 position in the first 12 bytes of each vertex
528 * target_error represents the error relative to mesh extents that can be tolerated, e.g. 0.01 = 1% deformation; value range [0..1]
529 */
530 MESHOPTIMIZER_API size_t meshopt_simplifyPrune(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 target_error);
531
532 /**
533 * Point cloud simplifier
534 * Reduces the number of points in the cloud to reach the given target
535 * Returns the number of points after simplification, with destination containing new index data
536 * The resulting index buffer references vertices from the original vertex buffer.
537 * If the original vertex data isn't needed, creating a compact vertex buffer using meshopt_optimizeVertexFetch is recommended.
538 *
539 * destination must contain enough space for the target index buffer (target_vertex_count elements)
540 * vertex_positions should have float3 position in the first 12 bytes of each vertex
541 * vertex_colors can be NULL; when it's not NULL, it should have float3 color in the first 12 bytes of each vertex
542 * color_weight determines relative priority of color wrt position; 1.0 is a safe default
543 */
544 MESHOPTIMIZER_API size_t meshopt_simplifyPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_colors, size_t vertex_colors_stride, float color_weight, size_t target_vertex_count);
545
546 /**
547 * Returns the error scaling factor used by the simplifier to convert between absolute and relative extents
548 *
549 * Absolute error must be *divided* by the scaling factor before passing it to meshopt_simplify as target_error
550 * Relative error returned by meshopt_simplify via result_error must be *multiplied* by the scaling factor to get absolute error.
551 */
552 MESHOPTIMIZER_API float meshopt_simplifyScale(const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
553
554 /**
555 * Mesh stripifier
556 * Converts a previously vertex cache optimized triangle list to triangle strip, stitching strips using restart index or degenerate triangles
557 * Returns the number of indices in the resulting strip, with destination containing new index data
558 * For maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
559 * Using restart indices can result in ~10% smaller index buffers, but on some GPUs restart indices may result in decreased performance.
560 *
561 * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_stripifyBound
562 * restart_index should be 0xffff or 0xffffffff depending on index size, or 0 to use degenerate triangles
563 */
564 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);
565 MESHOPTIMIZER_API size_t meshopt_stripifyBound(size_t index_count);
566
567 /**
568 * Mesh unstripifier
569 * Converts a triangle strip to a triangle list
570 * Returns the number of indices in the resulting list, with destination containing new index data
571 *
572 * destination must contain enough space for the target index buffer, worst case can be computed with meshopt_unstripifyBound
573 */
574 MESHOPTIMIZER_API size_t meshopt_unstripify(unsigned int* destination, const unsigned int* indices, size_t index_count, unsigned int restart_index);
575 MESHOPTIMIZER_API size_t meshopt_unstripifyBound(size_t index_count);
576
577 struct meshopt_VertexCacheStatistics
578 {
579 unsigned int vertices_transformed;
580 unsigned int warps_executed;
581 float acmr; /* transformed vertices / triangle count; best case 0.5, worst case 3.0, optimum depends on topology */
582 float atvr; /* transformed vertices / vertex count; best case 1.0, worst case 6.0, optimum is 1.0 (each vertex is transformed once) */
583 };
584
585 /**
586 * Vertex transform cache analyzer
587 * Returns cache hit statistics using a simplified FIFO model
588 * Results may not match actual GPU performance
589 */
590 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);
591
592 struct meshopt_VertexFetchStatistics
593 {
594 unsigned int bytes_fetched;
595 float overfetch; /* fetched bytes / vertex buffer size; best case 1.0 (each byte is fetched once) */
596 };
597
598 /**
599 * Vertex fetch cache analyzer
600 * Returns cache hit statistics using a simplified direct mapped model
601 * Results may not match actual GPU performance
602 */
603 MESHOPTIMIZER_API struct meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const unsigned int* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
604
605 struct meshopt_OverdrawStatistics
606 {
607 unsigned int pixels_covered;
608 unsigned int pixels_shaded;
609 float overdraw; /* shaded pixels / covered pixels; best case 1.0 */
610 };
611
612 /**
613 * Overdraw analyzer
614 * Returns overdraw statistics using a software rasterizer
615 * Results may not match actual GPU performance
616 *
617 * vertex_positions should have float3 position in the first 12 bytes of each vertex
618 */
619 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);
620
621 struct meshopt_CoverageStatistics
622 {
623 float coverage[3];
624 float extent; /* viewport size in mesh coordinates */
625 };
626
627 /**
628 * Coverage analyzer
629 * Returns coverage statistics (ratio of viewport pixels covered from each axis) using a software rasterizer
630 *
631 * vertex_positions should have float3 position in the first 12 bytes of each vertex
632 */
633 MESHOPTIMIZER_API struct meshopt_CoverageStatistics meshopt_analyzeCoverage(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
634
635 /**
636 * Meshlet is a small mesh cluster (subset) that consists of:
637 * - triangles, an 8-bit micro triangle (index) buffer, that for each triangle specifies three local vertices to use;
638 * - vertices, a 32-bit vertex indirection buffer, that for each local vertex specifies which mesh vertex to fetch vertex attributes from.
639 *
640 * For efficiency, meshlet triangles and vertices are packed into two large arrays; this structure contains offsets and counts to access the data.
641 */
642 struct meshopt_Meshlet
643 {
644 /* offsets within meshlet_vertices and meshlet_triangles arrays with meshlet data */
645 unsigned int vertex_offset;
646 unsigned int triangle_offset;
647
648 /* number of vertices and triangles used in the meshlet; data is stored in consecutive range [offset..offset+count) for vertices and [offset..offset+count*3) for triangles */
649 unsigned int vertex_count;
650 unsigned int triangle_count;
651 };
652
653 /**
654 * Meshlet builder
655 * 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
656 * The resulting data can be used to render meshes using NVidia programmable mesh shading pipeline, or in other cluster-based renderers.
657 * When targeting mesh shading hardware, for maximum efficiency meshlets should be further optimized using meshopt_optimizeMeshlet.
658 * When using buildMeshlets, vertex positions need to be provided to minimize the size of the resulting clusters.
659 * When using buildMeshletsScan, for maximum efficiency the index buffer being converted has to be optimized for vertex cache first.
660 *
661 * meshlets must contain enough space for all meshlets, worst case size can be computed with meshopt_buildMeshletsBound
662 * meshlet_vertices must contain enough space for all meshlets, worst case is index_count elements (*not* vertex_count!)
663 * meshlet_triangles must contain enough space for all meshlets, worst case is index_count elements
664 * vertex_positions should have float3 position in the first 12 bytes of each vertex
665 * max_vertices and max_triangles must not exceed implementation limits (max_vertices <= 256, max_triangles <= 512)
666 * 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
667 */
668 MESHOPTIMIZER_API size_t meshopt_buildMeshlets(struct meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t max_vertices, size_t max_triangles, float cone_weight);
669 MESHOPTIMIZER_API size_t meshopt_buildMeshletsScan(struct meshopt_Meshlet* meshlets, unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, const unsigned int* indices, size_t index_count, size_t vertex_count, size_t max_vertices, size_t max_triangles);
670 MESHOPTIMIZER_API size_t meshopt_buildMeshletsBound(size_t index_count, size_t max_vertices, size_t max_triangles);
671
672 /**
673 * Meshlet builder with flexible cluster sizes
674 * Splits the mesh into a set of meshlets, similarly to meshopt_buildMeshlets, but allows to specify minimum and maximum number of triangles per meshlet.
675 * Clusters between min and max triangle counts are split when the cluster size would have exceeded the expected cluster size by more than split_factor.
676 *
677 * meshlets must contain enough space for all meshlets, worst case size can be computed with meshopt_buildMeshletsBound using min_triangles (*not* max!)
678 * meshlet_vertices must contain enough space for all meshlets, worst case is index_count elements (*not* vertex_count!)
679 * meshlet_triangles must contain enough space for all meshlets, worst case is index_count elements
680 * vertex_positions should have float3 position in the first 12 bytes of each vertex
681 * max_vertices, min_triangles and max_triangles must not exceed implementation limits (max_vertices <= 256, max_triangles <= 512; min_triangles <= max_triangles)
682 * 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
683 * split_factor should be set to a non-negative value; when greater than 0, clusters that have large bounds may be split unless they are under the min_triangles threshold
684 */
685 MESHOPTIMIZER_API size_t meshopt_buildMeshletsFlex(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 min_triangles, size_t max_triangles, float cone_weight, float split_factor);
686
687 /**
688 * Meshlet builder that produces clusters optimized for raytracing
689 * Splits the mesh into a set of meshlets, similarly to meshopt_buildMeshlets, but optimizes cluster subdivision for raytracing and allows to specify minimum and maximum number of triangles per meshlet.
690 *
691 * meshlets must contain enough space for all meshlets, worst case size can be computed with meshopt_buildMeshletsBound using min_triangles (*not* max!)
692 * meshlet_vertices must contain enough space for all meshlets, worst case is index_count elements (*not* vertex_count!)
693 * meshlet_triangles must contain enough space for all meshlets, worst case is index_count elements
694 * vertex_positions should have float3 position in the first 12 bytes of each vertex
695 * max_vertices, min_triangles and max_triangles must not exceed implementation limits (max_vertices <= 256, max_triangles <= 512; min_triangles <= max_triangles)
696 * fill_weight allows to prioritize clusters that are closer to maximum size at some cost to SAH quality; 0.5 is a safe default
697 */
698 MESHOPTIMIZER_API size_t meshopt_buildMeshletsSpatial(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 min_triangles, size_t max_triangles, float fill_weight);
699
700 /**
701 * Meshlet optimizer
702 * Reorders meshlet vertices and triangles to maximize locality which can improve rasterizer throughput or ray tracing performance when using fast-build modes.
703 *
704 * meshlet_triangles and meshlet_vertices must refer to meshlet data; when buildMeshlets* is used, these need to be computed from meshlet's vertex_offset and triangle_offset
705 * triangle_count and vertex_count must not exceed implementation limits (vertex_count <= 256, triangle_count <= 512)
706 */
707 MESHOPTIMIZER_API void meshopt_optimizeMeshlet(unsigned int* meshlet_vertices, unsigned char* meshlet_triangles, size_t triangle_count, size_t vertex_count);
708
709 struct meshopt_Bounds
710 {
711 /* bounding sphere, useful for frustum and occlusion culling */
712 float center[3];
713 float radius;
714
715 /* normal cone, useful for backface culling */
716 float cone_apex[3];
717 float cone_axis[3];
718 float cone_cutoff; /* = cos(angle/2) */
719
720 /* normal cone axis and cutoff, stored in 8-bit SNORM format; decode using x/127.0 */
721 signed char cone_axis_s8[3];
722 signed char cone_cutoff_s8;
723 };
724
725 /**
726 * Cluster bounds generator
727 * Creates bounding volumes that can be used for frustum, backface and occlusion culling.
728 *
729 * For backface culling with orthographic projection, use the following formula to reject backfacing clusters:
730 * dot(view, cone_axis) >= cone_cutoff
731 *
732 * For perspective projection, you can use the formula that needs cone apex in addition to axis & cutoff:
733 * dot(normalize(cone_apex - camera_position), cone_axis) >= cone_cutoff
734 *
735 * Alternatively, you can use the formula that doesn't need cone apex and uses bounding sphere instead:
736 * dot(normalize(center - camera_position), cone_axis) >= cone_cutoff + radius / length(center - camera_position)
737 * or an equivalent formula that doesn't have a singularity at center = camera_position:
738 * dot(center - camera_position, cone_axis) >= cone_cutoff * length(center - camera_position) + radius
739 *
740 * The formula that uses the apex is slightly more accurate but needs the apex; if you are already using bounding sphere
741 * to do frustum/occlusion culling, the formula that doesn't use the apex may be preferable (for derivation see
742 * Real-Time Rendering 4th Edition, section 19.3).
743 *
744 * vertex_positions should have float3 position in the first 12 bytes of each vertex
745 * vertex_count should specify the number of vertices in the entire mesh, not cluster or meshlet
746 * index_count/3 and triangle_count must not exceed implementation limits (<= 512)
747 */
748 MESHOPTIMIZER_API struct meshopt_Bounds meshopt_computeClusterBounds(const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
749 MESHOPTIMIZER_API struct meshopt_Bounds meshopt_computeMeshletBounds(const unsigned int* meshlet_vertices, const unsigned char* meshlet_triangles, size_t triangle_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
750
751 /**
752 * Sphere bounds generator
753 * Creates bounding sphere around a set of points or a set of spheres; returns the center and radius of the sphere, with other fields of the result set to 0.
754 *
755 * positions should have float3 position in the first 12 bytes of each element
756 * radii can be NULL; when it's not NULL, it should have a non-negative float radius in the first 4 bytes of each element
757 */
758 MESHOPTIMIZER_API struct meshopt_Bounds meshopt_computeSphereBounds(const float* positions, size_t count, size_t positions_stride, const float* radii, size_t radii_stride);
759
760 /**
761 * Cluster partitioner
762 * Partitions clusters into groups of similar size, prioritizing grouping clusters that share vertices or are close to each other.
763 * When vertex positions are not provided, only clusters that share vertices will be grouped together, which may result in small partitions for some inputs.
764 *
765 * destination must contain enough space for the resulting partition data (cluster_count elements)
766 * destination[i] will contain the partition id for cluster i, with the total number of partitions returned by the function
767 * cluster_indices should have the vertex indices referenced by each cluster, stored sequentially
768 * cluster_index_counts should have the number of indices in each cluster; sum of all cluster_index_counts must be equal to total_index_count
769 * vertex_positions can be NULL; when it's not NULL, it should have float3 position in the first 12 bytes of each vertex
770 * target_partition_size is a target size for each partition, in clusters; the resulting partitions may be smaller or larger (up to target + target/3)
771 */
772 MESHOPTIMIZER_API size_t meshopt_partitionClusters(unsigned int* destination, const unsigned int* cluster_indices, size_t total_index_count, const unsigned int* cluster_index_counts, size_t cluster_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_partition_size);
773
774 /**
775 * Spatial sorter
776 * Generates a remap table that can be used to reorder points for spatial locality.
777 * Resulting remap table maps old vertices to new vertices and can be used in meshopt_remapVertexBuffer.
778 *
779 * destination must contain enough space for the resulting remap table (vertex_count elements)
780 * vertex_positions should have float3 position in the first 12 bytes of each vertex
781 */
782 MESHOPTIMIZER_API void meshopt_spatialSortRemap(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
783
784 /**
785 * Spatial sorter
786 * Reorders triangles for spatial locality, and generates a new index buffer. The resulting index buffer can be used with other functions like optimizeVertexCache.
787 *
788 * destination must contain enough space for the resulting index buffer (index_count elements)
789 * vertex_positions should have float3 position in the first 12 bytes of each vertex
790 */
791 MESHOPTIMIZER_API 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);
792
793 /**
794 * Spatial clusterizer
795 * Reorders points into clusters optimized for spatial locality, and generates a new index buffer.
796 * Ensures the output can be split into cluster_size chunks where each chunk has good positional locality. Only the last chunk will be smaller than cluster_size.
797 *
798 * destination must contain enough space for the resulting index buffer (vertex_count elements)
799 * vertex_positions should have float3 position in the first 12 bytes of each vertex
800 */
801 MESHOPTIMIZER_API void meshopt_spatialClusterPoints(unsigned int* destination, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t cluster_size);
802
803 /**
804 * Quantize a float into half-precision (as defined by IEEE-754 fp16) floating point value
805 * Generates +-inf for overflow, preserves NaN, flushes denormals to zero, rounds to nearest
806 * Representable magnitude range: [6e-5; 65504]
807 * Maximum relative reconstruction error: 5e-4
808 */
809 MESHOPTIMIZER_API unsigned short meshopt_quantizeHalf(float v);
810
811 /**
812 * Quantize a float into a floating point value with a limited number of significant mantissa bits, preserving the IEEE-754 fp32 binary representation
813 * Preserves infinities/NaN, flushes denormals to zero, rounds to nearest
814 * Assumes N is in a valid mantissa precision range, which is 1..23
815 */
816 MESHOPTIMIZER_API float meshopt_quantizeFloat(float v, int N);
817
818 /**
819 * Reverse quantization of a half-precision (as defined by IEEE-754 fp16) floating point value
820 * Preserves Inf/NaN, flushes denormals to zero
821 */
822 MESHOPTIMIZER_API float meshopt_dequantizeHalf(unsigned short h);
823
824 /**
825 * Set allocation callbacks
826 * These callbacks will be used instead of the default operator new/operator delete for all temporary allocations in the library.
827 * Note that all algorithms only allocate memory for temporary use.
828 * allocate/deallocate are always called in a stack-like order - last pointer to be allocated is deallocated first.
829 */
830 MESHOPTIMIZER_API void meshopt_setAllocator(void* (MESHOPTIMIZER_ALLOC_CALLCONV* allocate)(size_t), void (MESHOPTIMIZER_ALLOC_CALLCONV* deallocate)(void*));
831
832 #ifdef __cplusplus
833 } /* extern "C" */
834 #endif
835
836 /* Quantization into fixed point normalized formats; these are only available as inline C++ functions */
837 #ifdef __cplusplus
838 /**
839 * Quantize a float in [0..1] range into an N-bit fixed point unorm value
840 * Assumes reconstruction function (q / (2^N-1)), which is the case for fixed-function normalized fixed point conversion
841 * Maximum reconstruction error: 1/2^(N+1)
842 */
843 inline int meshopt_quantizeUnorm(float v, int N);
844
845 /**
846 * Quantize a float in [-1..1] range into an N-bit fixed point snorm value
847 * Assumes reconstruction function (q / (2^(N-1)-1)), which is the case for fixed-function normalized fixed point conversion (except early OpenGL versions)
848 * Maximum reconstruction error: 1/2^N
849 */
850 inline int meshopt_quantizeSnorm(float v, int N);
851 #endif
852
853 /**
854 * C++ template interface
855 *
856 * These functions mirror the C interface the library provides, providing template-based overloads so that
857 * the caller can use an arbitrary type for the index data, both for input and output.
858 * When the supplied type is the same size as that of unsigned int, the wrappers are zero-cost; when it's not,
859 * the wrappers end up allocating memory and copying index data to convert from one type to another.
860 */
861 #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
862 template <typename T>
863 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);
864 template <typename T>
865 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);
866 template <typename F>
867 inline size_t meshopt_generateVertexRemapCustom(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, F callback);
868 template <typename T, typename F>
869 inline size_t meshopt_generateVertexRemapCustom(unsigned int* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, F callback);
870 template <typename T>
871 inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap);
872 template <typename T>
873 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);
874 template <typename T>
875 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);
876 template <typename T>
877 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);
878 template <typename T>
879 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);
880 template <typename T>
881 inline size_t meshopt_generateProvokingIndexBuffer(T* destination, unsigned int* reorder, const T* indices, size_t index_count, size_t vertex_count);
882 template <typename T>
883 inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count);
884 template <typename T>
885 inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count);
886 template <typename T>
887 inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size);
888 template <typename T>
889 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);
890 template <typename T>
891 inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count);
892 template <typename T>
893 inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size);
894 template <typename T>
895 inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
896 template <typename T>
897 inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
898 template <typename T>
899 inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count);
900 template <typename T>
901 inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size);
902 inline size_t meshopt_encodeVertexBufferLevel(unsigned char* buffer, size_t buffer_size, const void* vertices, size_t vertex_count, size_t vertex_size, int level);
903 template <typename T>
904 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, unsigned int options = 0, float* result_error = NULL);
905 template <typename T>
906 inline size_t meshopt_simplifyWithAttributes(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options = 0, float* result_error = NULL);
907 template <typename T>
908 inline size_t meshopt_simplifyWithUpdate(T* indices, size_t index_count, float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options = 0, float* result_error = NULL);
909 template <typename T>
910 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 = NULL);
911 template <typename T>
912 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, const unsigned char* vertex_lock, size_t target_index_count, float target_error, float* result_error = NULL);
913 template <typename T>
914 inline size_t meshopt_simplifyPrune(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float target_error);
915 template <typename T>
916 inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index);
917 template <typename T>
918 inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index);
919 template <typename T>
920 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 primgroup_size);
921 template <typename T>
922 inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size);
923 template <typename T>
924 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);
925 template <typename T>
926 inline meshopt_CoverageStatistics meshopt_analyzeCoverage(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride);
927 template <typename T>
928 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);
929 template <typename T>
930 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);
931 template <typename T>
932 inline size_t meshopt_buildMeshletsFlex(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 min_triangles, size_t max_triangles, float cone_weight, float split_factor);
933 template <typename T>
934 inline size_t meshopt_buildMeshletsSpatial(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 min_triangles, size_t max_triangles, float fill_weight);
935 template <typename T>
936 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);
937 template <typename T>
938 inline size_t meshopt_partitionClusters(unsigned int* destination, const T* cluster_indices, size_t total_index_count, const unsigned int* cluster_index_counts, size_t cluster_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_partition_size);
939 template <typename T>
940 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);
941 #endif
942
943 /* Inline implementation */
944 #ifdef __cplusplus
945 inline int meshopt_quantizeUnorm(float v, int N)
946 {
947 const float scale = float((1 << N) - 1);
948
949 v = (v >= 0) ? v : 0;
950 v = (v <= 1) ? v : 1;
951
952 return int(v * scale + 0.5f);
953 }
954
955 inline int meshopt_quantizeSnorm(float v, int N)
956 {
957 const float scale = float((1 << (N - 1)) - 1);
958
959 float round = (v >= 0 ? 0.5f : -0.5f);
960
961 v = (v >= -1) ? v : -1;
962 v = (v <= +1) ? v : +1;
963
964 return int(v * scale + round);
965 }
966 #endif
967
968 /* Internal implementation helpers */
969 #ifdef __cplusplus
970 class meshopt_Allocator
971 {
972 public:
973 struct Storage
974 {
975 void* (MESHOPTIMIZER_ALLOC_CALLCONV* allocate)(size_t);
976 void (MESHOPTIMIZER_ALLOC_CALLCONV* deallocate)(void*);
977 };
978
979 #ifdef MESHOPTIMIZER_ALLOC_EXPORT
980 MESHOPTIMIZER_API static Storage& storage();
981 #else
982 static Storage& storage()
983 {
984 static Storage s = {::operator new, ::operator delete };
985 return s;
986 }
987 #endif
988
989 meshopt_Allocator()
990 : blocks()
991 , count(0)
992 {
993 }
994
995 ~meshopt_Allocator()
996 {
997 for (size_t i = count; i > 0; --i)
998 storage().deallocate(blocks[i - 1]);
999 }
1000
1001 template <typename T>
1002 T* allocate(size_t size)
1003 {
1004 assert(count < sizeof(blocks) / sizeof(blocks[0]));
1005 T* result = static_cast<T*>(storage().allocate(size > size_t(-1) / sizeof(T) ? size_t(-1) : size * sizeof(T)));
1006 blocks[count++] = result;
1007 return result;
1008 }
1009
1010 void deallocate(void* ptr)
1011 {
1012 assert(count > 0 && blocks[count - 1] == ptr);
1013 storage().deallocate(ptr);
1014 count--;
1015 }
1016
1017 private:
1018 void* blocks[24];
1019 size_t count;
1020 };
1021 #endif
1022
1023 /* Inline implementation for C++ templated wrappers */
1024 #if defined(__cplusplus) && !defined(MESHOPTIMIZER_NO_WRAPPERS)
1025 template <typename T, bool ZeroCopy = sizeof(T) == sizeof(unsigned int)>
1026 struct meshopt_IndexAdapter;
1027
1028 template <typename T>
1029 struct meshopt_IndexAdapter<T, false>
1030 {
1031 T* result;
1032 unsigned int* data;
1033 size_t count;
1034
1035 meshopt_IndexAdapter(T* result_, const T* input, size_t count_)
1036 : result(result_)
1037 , data(NULL)
1038 , count(count_)
1039 {
1040 size_t size = count > size_t(-1) / sizeof(unsigned int) ? size_t(-1) : count * sizeof(unsigned int);
1041
1042 data = static_cast<unsigned int*>(meshopt_Allocator::storage().allocate(size));
1043
1044 if (input)
1045 {
1046 for (size_t i = 0; i < count; ++i)
1047 data[i] = input[i];
1048 }
1049 }
1050
1051 ~meshopt_IndexAdapter()
1052 {
1053 if (result)
1054 {
1055 for (size_t i = 0; i < count; ++i)
1056 result[i] = T(data[i]);
1057 }
1058
1059 meshopt_Allocator::storage().deallocate(data);
1060 }
1061 };
1062
1063 template <typename T>
1064 struct meshopt_IndexAdapter<T, true>
1065 {
1066 unsigned int* data;
1067
1068 meshopt_IndexAdapter(T* result, const T* input, size_t)
1069 : data(reinterpret_cast<unsigned int*>(result ? result : const_cast<T*>(input)))
1070 {
1071 }
1072 };
1073
1074 template <typename T>
1075 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)
1076 {
1077 meshopt_IndexAdapter<T> in(NULL, indices, indices ? index_count : 0);
1078
1079 return meshopt_generateVertexRemap(destination, indices ? in.data : NULL, index_count, vertices, vertex_count, vertex_size);
1080 }
1081
1082 template <typename T>
1083 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)
1084 {
1085 meshopt_IndexAdapter<T> in(NULL, indices, indices ? index_count : 0);
1086
1087 return meshopt_generateVertexRemapMulti(destination, indices ? in.data : NULL, index_count, vertex_count, streams, stream_count);
1088 }
1089
1090 template <typename F>
1091 inline size_t meshopt_generateVertexRemapCustom(unsigned int* destination, const unsigned int* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, F callback)
1092 {
1093 struct Call
1094 {
1095 static int compare(void* context, unsigned int lhs, unsigned int rhs) { return (*static_cast<F*>(context))(lhs, rhs) ? 1 : 0; }
1096 };
1097
1098 return meshopt_generateVertexRemapCustom(destination, indices, index_count, vertex_positions, vertex_count, vertex_positions_stride, &Call::compare, &callback);
1099 }
1100
1101 template <typename T, typename F>
1102 inline size_t meshopt_generateVertexRemapCustom(unsigned int* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, F callback)
1103 {
1104 struct Call
1105 {
1106 static int compare(void* context, unsigned int lhs, unsigned int rhs) { return (*static_cast<F*>(context))(lhs, rhs) ? 1 : 0; }
1107 };
1108
1109 meshopt_IndexAdapter<T> in(NULL, indices, indices ? index_count : 0);
1110
1111 return meshopt_generateVertexRemapCustom(destination, indices ? in.data : NULL, index_count, vertex_positions, vertex_count, vertex_positions_stride, &Call::compare, &callback);
1112 }
1113
1114 template <typename T>
1115 inline void meshopt_remapIndexBuffer(T* destination, const T* indices, size_t index_count, const unsigned int* remap)
1116 {
1117 meshopt_IndexAdapter<T> in(NULL, indices, indices ? index_count : 0);
1118 meshopt_IndexAdapter<T> out(destination, 0, index_count);
1119
1120 meshopt_remapIndexBuffer(out.data, indices ? in.data : NULL, index_count, remap);
1121 }
1122
1123 template <typename T>
1124 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)
1125 {
1126 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1127 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1128
1129 meshopt_generateShadowIndexBuffer(out.data, in.data, index_count, vertices, vertex_count, vertex_size, vertex_stride);
1130 }
1131
1132 template <typename T>
1133 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)
1134 {
1135 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1136 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1137
1138 meshopt_generateShadowIndexBufferMulti(out.data, in.data, index_count, vertex_count, streams, stream_count);
1139 }
1140
1141 template <typename T>
1142 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)
1143 {
1144 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1145 meshopt_IndexAdapter<T> out(destination, NULL, index_count * 2);
1146
1147 meshopt_generateAdjacencyIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1148 }
1149
1150 template <typename T>
1151 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)
1152 {
1153 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1154 meshopt_IndexAdapter<T> out(destination, NULL, index_count * 4);
1155
1156 meshopt_generateTessellationIndexBuffer(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1157 }
1158
1159 template <typename T>
1160 inline size_t meshopt_generateProvokingIndexBuffer(T* destination, unsigned int* reorder, const T* indices, size_t index_count, size_t vertex_count)
1161 {
1162 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1163 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1164
1165 size_t bound = vertex_count + (index_count / 3);
1166 assert(size_t(T(bound - 1)) == bound - 1); // bound - 1 must fit in T
1167 (void)bound;
1168
1169 return meshopt_generateProvokingIndexBuffer(out.data, reorder, in.data, index_count, vertex_count);
1170 }
1171
1172 template <typename T>
1173 inline void meshopt_optimizeVertexCache(T* destination, const T* indices, size_t index_count, size_t vertex_count)
1174 {
1175 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1176 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1177
1178 meshopt_optimizeVertexCache(out.data, in.data, index_count, vertex_count);
1179 }
1180
1181 template <typename T>
1182 inline void meshopt_optimizeVertexCacheStrip(T* destination, const T* indices, size_t index_count, size_t vertex_count)
1183 {
1184 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1185 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1186
1187 meshopt_optimizeVertexCacheStrip(out.data, in.data, index_count, vertex_count);
1188 }
1189
1190 template <typename T>
1191 inline void meshopt_optimizeVertexCacheFifo(T* destination, const T* indices, size_t index_count, size_t vertex_count, unsigned int cache_size)
1192 {
1193 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1194 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1195
1196 meshopt_optimizeVertexCacheFifo(out.data, in.data, index_count, vertex_count, cache_size);
1197 }
1198
1199 template <typename T>
1200 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)
1201 {
1202 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1203 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1204
1205 meshopt_optimizeOverdraw(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, threshold);
1206 }
1207
1208 template <typename T>
1209 inline size_t meshopt_optimizeVertexFetchRemap(unsigned int* destination, const T* indices, size_t index_count, size_t vertex_count)
1210 {
1211 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1212
1213 return meshopt_optimizeVertexFetchRemap(destination, in.data, index_count, vertex_count);
1214 }
1215
1216 template <typename T>
1217 inline size_t meshopt_optimizeVertexFetch(void* destination, T* indices, size_t index_count, const void* vertices, size_t vertex_count, size_t vertex_size)
1218 {
1219 meshopt_IndexAdapter<T> inout(indices, indices, index_count);
1220
1221 return meshopt_optimizeVertexFetch(destination, inout.data, index_count, vertices, vertex_count, vertex_size);
1222 }
1223
1224 template <typename T>
1225 inline size_t meshopt_encodeIndexBuffer(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
1226 {
1227 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1228
1229 return meshopt_encodeIndexBuffer(buffer, buffer_size, in.data, index_count);
1230 }
1231
1232 template <typename T>
1233 inline int meshopt_decodeIndexBuffer(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
1234 {
1235 char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
1236 (void)index_size_valid;
1237
1238 return meshopt_decodeIndexBuffer(destination, index_count, sizeof(T), buffer, buffer_size);
1239 }
1240
1241 template <typename T>
1242 inline size_t meshopt_encodeIndexSequence(unsigned char* buffer, size_t buffer_size, const T* indices, size_t index_count)
1243 {
1244 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1245
1246 return meshopt_encodeIndexSequence(buffer, buffer_size, in.data, index_count);
1247 }
1248
1249 template <typename T>
1250 inline int meshopt_decodeIndexSequence(T* destination, size_t index_count, const unsigned char* buffer, size_t buffer_size)
1251 {
1252 char index_size_valid[sizeof(T) == 2 || sizeof(T) == 4 ? 1 : -1];
1253 (void)index_size_valid;
1254
1255 return meshopt_decodeIndexSequence(destination, index_count, sizeof(T), buffer, buffer_size);
1256 }
1257
1258 inline size_t meshopt_encodeVertexBufferLevel(unsigned char* buffer, size_t buffer_size, const void* vertices, size_t vertex_count, size_t vertex_size, int level)
1259 {
1260 return meshopt_encodeVertexBufferLevel(buffer, buffer_size, vertices, vertex_count, vertex_size, level, -1);
1261 }
1262
1263 template <typename T>
1264 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, unsigned int options, float* result_error)
1265 {
1266 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1267 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1268
1269 return meshopt_simplify(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_index_count, target_error, options, result_error);
1270 }
1271
1272 template <typename T>
1273 inline size_t meshopt_simplifyWithAttributes(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, const float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error)
1274 {
1275 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1276 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1277
1278 return meshopt_simplifyWithAttributes(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, vertex_attributes, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, result_error);
1279 }
1280
1281 template <typename T>
1282 inline size_t meshopt_simplifyWithUpdate(T* indices, size_t index_count, float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float* vertex_attributes, size_t vertex_attributes_stride, const float* attribute_weights, size_t attribute_count, const unsigned char* vertex_lock, size_t target_index_count, float target_error, unsigned int options, float* result_error)
1283 {
1284 meshopt_IndexAdapter<T> inout(indices, indices, index_count);
1285
1286 return meshopt_simplifyWithUpdate(inout.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, vertex_attributes, vertex_attributes_stride, attribute_weights, attribute_count, vertex_lock, target_index_count, target_error, options, result_error);
1287 }
1288
1289 template <typename T>
1290 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)
1291 {
1292 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1293 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1294
1295 return meshopt_simplifySloppy(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, NULL, target_index_count, target_error, result_error);
1296 }
1297
1298 template <typename T>
1299 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, const unsigned char* vertex_lock, size_t target_index_count, float target_error, float* result_error)
1300 {
1301 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1302 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1303
1304 return meshopt_simplifySloppy(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, vertex_lock, target_index_count, target_error, result_error);
1305 }
1306
1307 template <typename T>
1308 inline size_t meshopt_simplifyPrune(T* destination, const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, float target_error)
1309 {
1310 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1311 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1312
1313 return meshopt_simplifyPrune(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, target_error);
1314 }
1315
1316 template <typename T>
1317 inline size_t meshopt_stripify(T* destination, const T* indices, size_t index_count, size_t vertex_count, T restart_index)
1318 {
1319 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1320 meshopt_IndexAdapter<T> out(destination, NULL, (index_count / 3) * 5);
1321
1322 return meshopt_stripify(out.data, in.data, index_count, vertex_count, unsigned(restart_index));
1323 }
1324
1325 template <typename T>
1326 inline size_t meshopt_unstripify(T* destination, const T* indices, size_t index_count, T restart_index)
1327 {
1328 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1329 meshopt_IndexAdapter<T> out(destination, NULL, (index_count - 2) * 3);
1330
1331 return meshopt_unstripify(out.data, in.data, index_count, unsigned(restart_index));
1332 }
1333
1334 template <typename T>
1335 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 primgroup_size)
1336 {
1337 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1338
1339 return meshopt_analyzeVertexCache(in.data, index_count, vertex_count, cache_size, warp_size, primgroup_size);
1340 }
1341
1342 template <typename T>
1343 inline meshopt_VertexFetchStatistics meshopt_analyzeVertexFetch(const T* indices, size_t index_count, size_t vertex_count, size_t vertex_size)
1344 {
1345 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1346
1347 return meshopt_analyzeVertexFetch(in.data, index_count, vertex_count, vertex_size);
1348 }
1349
1350 template <typename T>
1351 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)
1352 {
1353 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1354
1355 return meshopt_analyzeOverdraw(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1356 }
1357
1358 template <typename T>
1359 inline meshopt_CoverageStatistics meshopt_analyzeCoverage(const T* indices, size_t index_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride)
1360 {
1361 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1362
1363 return meshopt_analyzeCoverage(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1364 }
1365
1366 template <typename T>
1367 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)
1368 {
1369 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1370
1371 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);
1372 }
1373
1374 template <typename T>
1375 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)
1376 {
1377 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1378
1379 return meshopt_buildMeshletsScan(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_count, max_vertices, max_triangles);
1380 }
1381
1382 template <typename T>
1383 inline size_t meshopt_buildMeshletsFlex(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 min_triangles, size_t max_triangles, float cone_weight, float split_factor)
1384 {
1385 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1386
1387 return meshopt_buildMeshletsFlex(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, max_vertices, min_triangles, max_triangles, cone_weight, split_factor);
1388 }
1389
1390 template <typename T>
1391 inline size_t meshopt_buildMeshletsSpatial(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 min_triangles, size_t max_triangles, float fill_weight)
1392 {
1393 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1394
1395 return meshopt_buildMeshletsSpatial(meshlets, meshlet_vertices, meshlet_triangles, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride, max_vertices, min_triangles, max_triangles, fill_weight);
1396 }
1397
1398 template <typename T>
1399 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)
1400 {
1401 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1402
1403 return meshopt_computeClusterBounds(in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1404 }
1405
1406 template <typename T>
1407 inline size_t meshopt_partitionClusters(unsigned int* destination, const T* cluster_indices, size_t total_index_count, const unsigned int* cluster_index_counts, size_t cluster_count, const float* vertex_positions, size_t vertex_count, size_t vertex_positions_stride, size_t target_partition_size)
1408 {
1409 meshopt_IndexAdapter<T> in(NULL, cluster_indices, total_index_count);
1410
1411 return meshopt_partitionClusters(destination, in.data, total_index_count, cluster_index_counts, cluster_count, vertex_positions, vertex_count, vertex_positions_stride, target_partition_size);
1412 }
1413
1414 template <typename T>
1415 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)
1416 {
1417 meshopt_IndexAdapter<T> in(NULL, indices, index_count);
1418 meshopt_IndexAdapter<T> out(destination, NULL, index_count);
1419
1420 meshopt_spatialSortTriangles(out.data, in.data, index_count, vertex_positions, vertex_count, vertex_positions_stride);
1421 }
1422 #endif
1423
1424 /**
1425 * Copyright (c) 2016-2025 Arseny Kapoulkine
1426 *
1427 * Permission is hereby granted, free of charge, to any person
1428 * obtaining a copy of this software and associated documentation
1429 * files (the "Software"), to deal in the Software without
1430 * restriction, including without limitation the rights to use,
1431 * copy, modify, merge, publish, distribute, sublicense, and/or sell
1432 * copies of the Software, and to permit persons to whom the
1433 * Software is furnished to do so, subject to the following
1434 * conditions:
1435 *
1436 * The above copyright notice and this permission notice shall be
1437 * included in all copies or substantial portions of the Software.
1438 *
1439 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1440 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1441 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1442 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
1443 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
1444 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1445 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
1446 * OTHER DEALINGS IN THE SOFTWARE.
1447 */