Coverage Report

Created: 2025-08-29 06:29

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