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