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

source code of qtquick3d/src/3rdparty/meshoptimizer/src/meshoptimizer.h