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