meshoptimizer-rs 0.2.9

Rust port of meshoptimizer 1.3 with byte-identical output. Covers mesh simplification, meshlets, cluster LOD, vertex/index compression analysis, remeshing and optional parallel batch APIs and audited SIMD codecs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Public encoder API: explicit per-call version/level configuration replaces
// upstream's global meshopt_encodeVertexVersion/meshopt_encodeIndexVersion.
use super::{filter_encode, index_encode, vertex_encode};
use crate::{workspace::checked_bytes, Error, Workspace};
use alloc::vec::Vec;

/// Vertex codec format version and compression level
/// (the `version` and `level` of meshopt_encodeVertexBufferLevel).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct VertexEncoding {
    version: u8,
    level: u8,
}
impl VertexEncoding {
    /// Upstream defaults of meshopt_encodeVertexBuffer: version 1, level 2.
    pub const DEFAULT: Self = Self {
        version: 1,
        level: 2,
    };
    /// Check a version (0 or 1) and level (0 through 9, the range the pinned
    /// reference accepts; 1.3 behaves identically for levels 3 through 9).
    /// Version 0 ignores the level, as upstream does.
    pub const fn new(version: u8, level: u8) -> Result<Self, Error> {
        if version > 1 {
            return Err(Error::UnsupportedVersion);
        }
        if level > 9 {
            return Err(Error::InvalidParameter);
        }
        Ok(Self { version, level })
    }
    /// Format version.
    pub const fn version(self) -> u8 {
        self.version
    }
    /// Compression level.
    pub const fn level(self) -> u8 {
        self.level
    }
}
impl Default for VertexEncoding {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Index buffer and index sequence format version
/// (the setting of meshopt_encodeIndexVersion).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IndexEncoding {
    version: u8,
}
impl IndexEncoding {
    /// Upstream default: version 1.
    pub const DEFAULT: Self = Self { version: 1 };
    /// Check a version: 0 (decodable by all versions) or 1.
    pub const fn new(version: u8) -> Result<Self, Error> {
        if version > 1 {
            return Err(Error::UnsupportedVersion);
        }
        Ok(Self { version })
    }
    /// Format version.
    pub const fn version(self) -> u8 {
        self.version
    }
}
impl Default for IndexEncoding {
    fn default() -> Self {
        Self::DEFAULT
    }
}

/// Exponent sharing of the exponential filter encoder (meshopt_EncodeExpMode).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ExpMode {
    /// A separate exponent for each component (maximum quality).
    Separate,
    /// One exponent for all components of a vector.
    SharedVector,
    /// One exponent for each component across all vectors.
    SharedComponent,
    /// Separate exponents clamped to at least zero.
    Clamped,
}

#[inline]
fn vertex_layout(count: usize, stride: usize) -> Result<usize, Error> {
    if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
        return Err(Error::InvalidLayout);
    }
    checked_bytes(count, stride)
}

/// meshopt_encodeVertexBufferBound: worst-case encoded size for either version.
#[inline]
pub fn encode_vertex_buffer_bound(count: usize, stride: usize) -> Result<usize, Error> {
    if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
        return Err(Error::InvalidLayout);
    }
    vertex_encode::bound(count, stride)
}

/// meshopt_encodeIndexBufferBound. `index_count` must be a multiple of three.
#[inline]
pub fn encode_index_buffer_bound(index_count: usize, vertex_count: usize) -> Result<usize, Error> {
    if !index_count.is_multiple_of(3) {
        return Err(Error::InvalidTopology);
    }
    index_encode::index_bound(index_count, vertex_count as u64)
}

/// meshopt_encodeIndexSequenceBound.
#[inline]
pub fn encode_index_sequence_bound(
    index_count: usize,
    vertex_count: usize,
) -> Result<usize, Error> {
    index_encode::sequence_bound(index_count, vertex_count as u64)
}

/// Shared allocating driver: account the bound, reserve fallibly, encode and
/// truncate to the used length (the capacity stays at the accounted bound).
fn allocate(
    bound: usize,
    input: usize,
    workspace: &mut Workspace,
    encode: impl FnOnce(&mut [u8]) -> Result<usize, Error>,
) -> Result<Vec<u8>, Error> {
    let mut work = workspace.begin();
    let check = (|| {
        workspace.account_codec(bound)?;
        work.add(input)?;
        work.add(bound)
    })();
    workspace.finish(&work);
    check?;
    let mut out = Vec::new();
    out.try_reserve_exact(bound)
        .map_err(|_| Error::AllocationFailed)?;
    if out.capacity() != bound {
        workspace.account_codec(out.capacity())?;
    }
    out.resize(bound, 0);
    let used = encode(&mut out)?;
    out.truncate(used);
    Ok(out)
}

/// Shared caller-buffer driver. Work charges input bytes plus the bytes the
/// encoder may examine in the destination (at most the bound).
fn into(
    bound: usize,
    capacity: usize,
    input: usize,
    workspace: &mut Workspace,
    encode: impl FnOnce() -> Result<usize, Error>,
) -> Result<usize, Error> {
    let mut work = workspace.begin();
    let result = (|| {
        workspace.account_codec(0)?;
        work.add(input)?;
        work.add(bound.min(capacity))?;
        encode()
    })();
    workspace.finish(&work);
    result
}

/// Encode a vertex buffer (meshopt_encodeVertexBufferLevel) with explicit
/// version and level into a vector. `vertices` holds exactly count * stride
/// bytes; all bytes, including padding, are encoded verbatim. stride is a
/// multiple of four from 4 to 256.
pub fn encode_vertex_buffer(
    vertices: &[u8],
    count: usize,
    stride: usize,
    encoding: VertexEncoding,
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    let _ = workspace.begin();
    if vertices.len() != vertex_layout(count, stride)? {
        return Err(Error::InvalidLayout);
    }
    let bound = vertex_encode::bound(count, stride)?;
    allocate(bound, vertices.len(), workspace, |out| {
        vertex_encode::encode(
            out,
            vertices,
            count,
            stride,
            encoding.version,
            encoding.level,
        )
    })
}

/// Encode a vertex buffer into a caller buffer and return the encoded length.
/// Fails with BufferTooSmall exactly where the reference returns 0; the
/// encoder needs slack beyond the final size, so use
/// [`encode_vertex_buffer_bound`]. On failure the buffer prefix may change.
pub fn encode_vertex_buffer_into(
    destination: &mut [u8],
    vertices: &[u8],
    count: usize,
    stride: usize,
    encoding: VertexEncoding,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let _ = workspace.begin();
    if vertices.len() != vertex_layout(count, stride)? {
        return Err(Error::InvalidLayout);
    }
    let bound = vertex_encode::bound(count, stride)?;
    let capacity = destination.len();
    into(bound, capacity, vertices.len(), workspace, || {
        vertex_encode::encode(
            destination,
            vertices,
            count,
            stride,
            encoding.version,
            encoding.level,
        )
    })
}

/// A vertex count giving the same bound as `max index + 1`: the bound only
/// depends on the bit length of the largest index, which the bitwise OR of
/// all indices shares (and an OR reduction vectorizes on baseline x86-64).
fn max_vertices(indices: &[u32]) -> u64 {
    if indices.is_empty() {
        return 0;
    }
    u64::from(indices.iter().fold(0, |a, &b| a | b)) + 1
}

/// Encode a triangle list (meshopt_encodeIndexBuffer) with an explicit version.
/// The length is a multiple of three. Corners may be rotated within each
/// triangle, as upstream does; decoding returns the rotated list.
pub fn encode_index_buffer(
    indices: &[u32],
    encoding: IndexEncoding,
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    let _ = workspace.begin();
    if !indices.len().is_multiple_of(3) {
        return Err(Error::InvalidTopology);
    }
    let bound = index_encode::index_bound(indices.len(), max_vertices(indices))?;
    let input = checked_bytes(indices.len(), 4)?;
    allocate(bound, input, workspace, |out| {
        index_encode::encode_index_buffer(out, indices, encoding.version)
    })
}

/// Encode a triangle list into a caller buffer and return the encoded length.
/// Fails with BufferTooSmall exactly where the reference returns 0; on
/// failure the buffer prefix may change.
pub fn encode_index_buffer_into(
    destination: &mut [u8],
    indices: &[u32],
    encoding: IndexEncoding,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let _ = workspace.begin();
    if !indices.len().is_multiple_of(3) {
        return Err(Error::InvalidTopology);
    }
    let bound = index_encode::index_bound(indices.len(), 1 << 32)?;
    let input = checked_bytes(indices.len(), 4)?;
    let capacity = destination.len();
    into(bound, capacity, input, workspace, || {
        index_encode::encode_index_buffer(destination, indices, encoding.version)
    })
}

/// Encode an arbitrary index sequence (meshopt_encodeIndexSequence) with an
/// explicit version.
pub fn encode_index_sequence(
    indices: &[u32],
    encoding: IndexEncoding,
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    let _ = workspace.begin();
    let bound = index_encode::sequence_bound(indices.len(), max_vertices(indices))?;
    let input = checked_bytes(indices.len(), 4)?;
    allocate(bound, input, workspace, |out| {
        index_encode::encode_index_sequence(out, indices, encoding.version)
    })
}

/// Encode an index sequence into a caller buffer and return the encoded
/// length. Fails with BufferTooSmall exactly where the reference returns 0;
/// on failure the buffer prefix may change.
pub fn encode_index_sequence_into(
    destination: &mut [u8],
    indices: &[u32],
    encoding: IndexEncoding,
    workspace: &mut Workspace,
) -> Result<usize, Error> {
    let _ = workspace.begin();
    let bound = index_encode::sequence_bound(indices.len(), 1 << 32)?;
    let input = checked_bytes(indices.len(), 4)?;
    let capacity = destination.len();
    into(bound, capacity, input, workspace, || {
        index_encode::encode_index_sequence(destination, indices, encoding.version)
    })
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum Kind {
    Oct,
    Quat,
    Exp(ExpMode),
    Color,
}

/// Validate a filter encoder call; returns the output byte length.
fn filter_layout(
    kind: Kind,
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
) -> Result<usize, Error> {
    let (layout, floats, range) = match kind {
        Kind::Oct | Kind::Color => (stride == 4 || stride == 8, 4, 2..=16),
        Kind::Quat => (stride == 8, 4, 4..=16),
        Kind::Exp(_) => (
            stride > 0 && stride <= 256 && stride.is_multiple_of(4),
            stride / 4,
            1..=24,
        ),
    };
    if !layout {
        return Err(Error::InvalidLayout);
    }
    if !range.contains(&bits)
        || (stride == 4 && bits > 8 && matches!(kind, Kind::Oct | Kind::Color))
    {
        return Err(Error::InvalidParameter);
    }
    if data.len() != checked_bytes(count, floats)? {
        return Err(Error::InvalidLayout);
    }
    checked_bytes(count, stride)
}

fn filter_run(
    kind: Kind,
    out: &mut [u8],
    stride: usize,
    bits: u32,
    data: &[f32],
) -> Result<(), Error> {
    match kind {
        Kind::Oct => filter_encode::oct(out, stride, bits, data),
        Kind::Quat => filter_encode::quat(out, bits, data),
        Kind::Color => filter_encode::color(out, stride, bits, data),
        Kind::Exp(mode) => return filter_encode::exp(out, stride, bits, data, mode),
    }
    Ok(())
}

fn filter_into(
    kind: Kind,
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<(), Error> {
    filter_into_with(
        kind,
        destination,
        count,
        stride,
        bits,
        data,
        workspace,
        |out| filter_run(kind, out, stride, bits, data),
    )
}

// Monomorphize Oct/Quat calls so generic filter dispatch and unrelated kernel
// stack setup do not dominate small inputs. Validation/accounting is shared.
#[allow(clippy::too_many_arguments)]
fn filter_into_with(
    kind: Kind,
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
    run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
) -> Result<(), Error> {
    let mut work = workspace.begin();
    let result = (|| {
        let bytes = filter_layout(kind, count, stride, bits, data)?;
        if destination.len() < bytes {
            return Err(Error::BufferTooSmall);
        }
        workspace.account_codec(0)?;
        work.add(data.len())?;
        work.add(bytes / 4)?;
        run(&mut destination[..bytes])
    })();
    workspace.finish(&work);
    result
}

fn filter_vec(
    kind: Kind,
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    filter_vec_with(kind, count, stride, bits, data, workspace, |out| {
        filter_run(kind, out, stride, bits, data)
    })
}

fn filter_vec_with(
    kind: Kind,
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
    run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
) -> Result<Vec<u8>, Error> {
    let mut work = workspace.begin();
    let check = (|| {
        let bytes = filter_layout(kind, count, stride, bits, data)?;
        workspace.account_codec(bytes)?;
        work.add(data.len())?;
        work.add(bytes / 4)?;
        Ok(bytes)
    })();
    workspace.finish(&work);
    let bytes = check?;
    let mut out = Vec::new();
    out.try_reserve_exact(bytes)
        .map_err(|_| Error::AllocationFailed)?;
    if out.capacity() != bytes {
        workspace.account_codec(out.capacity())?;
    }
    out.resize(bytes, 0);
    run(&mut out)?;
    Ok(out)
}

/// Octahedral filter encoder (meshopt_encodeFilterOct): `data` holds four
/// floats per vector; stride 4 takes 2..=8 bits, stride 8 takes 2..=16.
/// Z stores 1.0 and W is quantized to the full byte width.
pub fn encode_filter_oct(
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    filter_vec_with(Kind::Oct, count, stride, bits, data, workspace, |out| {
        filter_encode::oct(out, stride, bits, data);
        Ok(())
    })
}
/// Caller-buffer form of [`encode_filter_oct`]; the destination tail is preserved.
pub fn encode_filter_oct_into(
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<(), Error> {
    filter_into_with(
        Kind::Oct,
        destination,
        count,
        stride,
        bits,
        data,
        workspace,
        |out| {
            filter_encode::oct(out, stride, bits, data);
            Ok(())
        },
    )
}
/// Quaternion filter encoder (meshopt_encodeFilterQuat): four floats per
/// quaternion, stride 8, 4..=16 bits.
pub fn encode_filter_quat(
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    filter_vec_with(Kind::Quat, count, stride, bits, data, workspace, |out| {
        filter_encode::quat(out, bits, data);
        Ok(())
    })
}
/// Caller-buffer form of [`encode_filter_quat`]; the destination tail is preserved.
pub fn encode_filter_quat_into(
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<(), Error> {
    filter_into_with(
        Kind::Quat,
        destination,
        count,
        stride,
        bits,
        data,
        workspace,
        |out| {
            filter_encode::quat(out, bits, data);
            Ok(())
        },
    )
}
/// Exponential filter encoder (meshopt_encodeFilterExp): stride / 4 floats
/// per vector, stride a multiple of four up to 256, 1..=24 mantissa bits.
/// Returns NumericalFailure where the reference conversion is undefined
/// (non-finite input; one-bit mantissas with exponent 128); the output may
/// then be partially written.
pub fn encode_filter_exp(
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    mode: ExpMode,
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    filter_vec(Kind::Exp(mode), count, stride, bits, data, workspace)
}
/// Caller-buffer form of [`encode_filter_exp`]; the destination tail is preserved.
pub fn encode_filter_exp_into(
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    mode: ExpMode,
    workspace: &mut Workspace,
) -> Result<(), Error> {
    filter_into(
        Kind::Exp(mode),
        destination,
        count,
        stride,
        bits,
        data,
        workspace,
    )
}
/// Color filter encoder (meshopt_encodeFilterColor): four floats per RGBA
/// color into YCoCg-R plus alpha; stride 4 takes 2..=8 bits, stride 8 2..=16.
pub fn encode_filter_color(
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<Vec<u8>, Error> {
    filter_vec(Kind::Color, count, stride, bits, data, workspace)
}
/// Caller-buffer form of [`encode_filter_color`]; the destination tail is preserved.
pub fn encode_filter_color_into(
    destination: &mut [u8],
    count: usize,
    stride: usize,
    bits: u32,
    data: &[f32],
    workspace: &mut Workspace,
) -> Result<(), Error> {
    filter_into(
        Kind::Color,
        destination,
        count,
        stride,
        bits,
        data,
        workspace,
    )
}