meshoptimizer-rs 0.2.2

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
//! Ordered batches of independent sequential operations, using Rayon.
//!
//! Every successful item has exactly the sequential operation's meaningful
//! bytes, including float bits. There are no parallel reductions. Results stay
//! in input order, and an item's [`Error`] does not cancel other items. The outer
//! [`BatchResult`] error concerns only allocation of the result slots, before
//! any item runs. Panics follow Rayon's usual propagation rules.
//!
//! Each executing thread has a private [`Workspace`]. Scratch is cleared before
//! and after each item, so an earlier item's retained capacity cannot change a
//! later item's limit outcome. The supplied [`Limits`] apply independently to
//! each item. A LOD chain includes all its live output, scratch and cumulative
//! work in that budget. The batch result slots, caller inputs and Rayon's pool
//! infrastructure are excluded. There is no aggregate batch memory limit: all
//! successful outputs remain live until the caller drops them.
//!
//! Work uses the current Rayon pool (normally the global pool). Use
//! [`rayon::ThreadPool::install`] to select an existing pool; the crate does not
//! explicitly create pools. Rayon lazily initializes the global pool on first
//! use if none was configured; initialization failure panics inside Rayon,
//! rather than returning a per-item [`Error`]. Native threads are required for
//! parallel execution.
//!
//! ```
//! use meshoptimizer_rs::{parallel::{encode_buffers_batch, EncodeInput}, codec::IndexEncoding, Limits};
//! let indices = [0, 1, 2, 2, 1, 3];
//! let views = [EncodeInput::Triangles { indices: &indices, encoding: IndexEncoding::DEFAULT }];
//! let outputs = encode_buffers_batch(&views, Limits::default())?;
//! assert!(outputs[0].is_ok());
//! # Ok::<(), meshoptimizer_rs::Error>(())
//! ```
//!
//! ```
//! use meshoptimizer_rs::{parallel::{build_meshlets_batch, MeshletInput, MeshletBuilder},
//!     Limits, MeshletSettings, Positions};
//! let positions = [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]];
//! let indices = [0, 1, 2];
//! let inputs = [MeshletInput { indices: &indices, positions: Positions::from_packed(&positions),
//!     settings: MeshletSettings::default(), builder: MeshletBuilder::Scan }];
//! let pool = rayon::ThreadPoolBuilder::new().num_threads(2).build()?;
//! let outputs = pool.install(|| build_meshlets_batch(&inputs, Limits::default()))?;
//! assert_eq!(outputs[0].as_ref().unwrap().meshlets.len(), 1);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::codec::{self, BufferView, IndexEncoding, VertexEncoding};
use crate::workspace::checked_bytes;
use crate::{
    Attributes, Error, Limits, MeshletSettings, Meshlets, Positions, SimplifiedMesh,
    SimplifySettings, VertexFlags, Workspace,
};
use alloc::vec::Vec;
use core::mem::size_of;
use rayon::prelude::*;
use std::cell::RefCell;

/// Ordered per-item results, or a result-slot allocation error before execution.
pub type BatchResult<T> = Result<Vec<Result<T, Error>>, Error>;

std::thread_local! {
    static WORKSPACE: RefCell<Workspace> = RefCell::new(Workspace::default());
}

fn slots<T>(count: usize) -> BatchResult<T> {
    checked_bytes(count, size_of::<Result<T, Error>>())?;
    let mut result = Vec::new();
    result
        .try_reserve_exact(count)
        .map_err(|_| Error::AllocationFailed)?;
    result.resize_with(count, || Err(Error::AllocationFailed));
    Ok(result)
}

fn item<T>(
    limits: Limits,
    run: impl FnOnce(&mut Workspace) -> Result<T, Error>,
) -> Result<T, Error> {
    WORKSPACE.with(|cell| {
        let mut workspace = cell.borrow_mut();
        workspace.set_limits(limits);
        let result = run(&mut workspace);
        workspace.clear();
        result
    })
}

fn batch<I: Sync, T: Send>(
    inputs: &[I],
    limits: Limits,
    run: impl Fn(&I, &mut Workspace) -> Result<T, Error> + Sync,
) -> BatchResult<T> {
    let mut result = slots(inputs.len())?;
    result
        .par_iter_mut()
        .zip(inputs.par_iter())
        .for_each(|(out, input)| {
            *out = item(limits, |workspace| run(input, workspace));
        });
    Ok(result)
}

/// Optional attributes and flags used at every level of a LOD chain.
#[derive(Clone, Copy, Debug)]
pub struct LodAttributes<'a> {
    /// Attribute records for the unchanged full vertex array.
    pub attributes: Attributes<'a>,
    /// One weight per component.
    pub weights: &'a [f32],
    /// Optional per-vertex simplifier flags.
    pub vertex_flags: Option<&'a [VertexFlags]>,
}

/// One chain. Each level simplifies the preceding level's topology.
#[derive(Clone, Copy, Debug)]
pub struct LodChainInput<'a> {
    /// Original triangle topology.
    pub indices: &'a [u32],
    /// Full vertex positions; returned indices keep their original references.
    pub positions: Positions<'a>,
    /// Per-level settings, in execution order. Targets are index counts relative
    /// to that level's input. Error values are returned unchanged, not accumulated.
    pub levels: &'a [SimplifySettings],
    /// None selects plain simplification; Some selects attribute simplification.
    pub attributes: Option<LodAttributes<'a>>,
}

/// Sequential reference for [`simplify_lod_chains_batch`].
///
/// Calls [`crate::simplify`] or [`crate::simplify_with_attributes`] in level
/// order. No cache optimization, compaction or error adjustment is added.
/// Empty settings produce an empty chain without validating unused mesh data.
/// A failed level drops the entire chain. The workspace reports cumulative work
/// (including one visit per level) and peak live storage for the whole chain.
///
/// ```
/// use meshoptimizer_rs::{parallel::{simplify_lod_chain, LodChainInput}, Positions,
///     SimplifySettings, SimplifyOptions, Workspace};
/// let p = [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.]];
/// let indices = [0, 1, 2];
/// let levels = [SimplifySettings { target_index_count: 3, target_error: 0.01,
///     options: SimplifyOptions::EMPTY }];
/// let chain = simplify_lod_chain(&LodChainInput { indices: &indices,
///     positions: Positions::from_packed(&p), levels: &levels, attributes: None },
///     &mut Workspace::default())?;
/// assert_eq!(chain[0].indices, indices);
/// # Ok::<(), meshoptimizer_rs::Error>(())
/// ```
pub fn simplify_lod_chain(
    input: &LodChainInput<'_>,
    workspace: &mut Workspace,
) -> Result<Vec<SimplifiedMesh>, Error> {
    let mut work = workspace.begin();
    let result = (|| {
        let limits = workspace.limits();
        let retained = workspace.retained_bytes()?;
        let mut levels =
            crate::budget::Budget::new(workspace).reserved::<SimplifiedMesh>(input.levels.len())?;
        let mut live = checked_bytes(levels.capacity(), size_of::<SimplifiedMesh>())?;
        let mut spent = 0u64;
        for &settings in input.levels {
            work.add(1)?;
            spent = spent.checked_add(1).ok_or(Error::SizeOverflow)?;
            let mut child = Workspace::new(Limits {
                max_bytes: limits
                    .max_bytes
                    .checked_sub(retained.checked_add(live).ok_or(Error::SizeOverflow)?)
                    .ok_or(Error::LimitExceeded)?,
                max_work: limits
                    .max_work
                    .checked_sub(spent)
                    .ok_or(Error::LimitExceeded)?,
            });
            let indices = levels.last().map_or(input.indices, |level| &level.indices);
            let result = match input.attributes {
                None => crate::simplify(indices, input.positions, settings, &mut child),
                Some(a) => crate::simplify_with_attributes(
                    indices,
                    input.positions,
                    a.attributes,
                    a.weights,
                    a.vertex_flags,
                    settings,
                    &mut child,
                ),
            };
            work.add_processing(child.usage().work)?;
            spent = spent
                .checked_add(child.usage().work)
                .ok_or(Error::SizeOverflow)?;
            workspace.charge_owned(
                retained,
                live.checked_add(child.usage().bytes)
                    .ok_or(Error::SizeOverflow)?,
            )?;
            let level = result?;
            live = live
                .checked_add(checked_bytes(level.indices.capacity(), 4)?)
                .ok_or(Error::SizeOverflow)?;
            levels.push(level);
        }
        Ok(levels)
    })();
    workspace.finish(&work);
    result
}

/// Simplify independent LOD chains in parallel, returning one result per chain.
/// Levels within each chain retain the sequential reference's order and budget.
pub fn simplify_lod_chains_batch(
    inputs: &[LodChainInput<'_>],
    limits: Limits,
) -> BatchResult<Vec<SimplifiedMesh>> {
    batch(inputs, limits, simplify_lod_chain)
}

/// An independent buffer to encode. Configuration is local to each descriptor.
#[derive(Clone, Copy, Debug)]
pub enum EncodeInput<'a> {
    /// Initialized vertex bytes, including supplied padding.
    Vertices {
        /// Exact count times stride bytes.
        vertices: &'a [u8],
        /// Number of vertex records.
        count: usize,
        /// Record stride in bytes.
        stride: usize,
        /// Format version and compression level.
        encoding: VertexEncoding,
    },
    /// Triangle-list indices.
    Triangles {
        /// Topology in u32 form.
        indices: &'a [u32],
        /// Format version.
        encoding: IndexEncoding,
    },
    /// General index sequence.
    Sequence {
        /// Values in u32 form.
        indices: &'a [u32],
        /// Format version.
        encoding: IndexEncoding,
    },
}
impl EncodeInput<'_> {
    /// Encode this descriptor through the corresponding sequential codec API.
    pub fn encode(self, workspace: &mut Workspace) -> Result<Vec<u8>, Error> {
        match self {
            Self::Vertices {
                vertices,
                count,
                stride,
                encoding,
            } => codec::encode_vertex_buffer(vertices, count, stride, encoding, workspace),
            Self::Triangles { indices, encoding } => {
                codec::encode_index_buffer(indices, encoding, workspace)
            }
            Self::Sequence { indices, encoding } => {
                codec::encode_index_sequence(indices, encoding, workspace)
            }
        }
    }
}

/// Encode vertex, triangle and sequence buffers in input order.
pub fn encode_buffers_batch(inputs: &[EncodeInput<'_>], limits: Limits) -> BatchResult<Vec<u8>> {
    batch(inputs, limits, |input, workspace| input.encode(workspace))
}

/// One checked EXT buffer view and its compressed bytes.
#[derive(Clone, Copy, Debug)]
pub struct DecodeInput<'a> {
    /// Mode, filter, count and stride; construct through [`BufferView::new`].
    pub view: BufferView,
    /// Compressed source bytes.
    pub source: &'a [u8],
}

/// Decode complete independent EXT views through [`BufferView::decode`].
pub fn decode_buffer_views_batch(
    inputs: &[DecodeInput<'_>],
    limits: Limits,
) -> BatchResult<Vec<u8>> {
    batch(inputs, limits, |input, workspace| {
        input.view.decode(input.source, workspace)
    })
}

/// Sequential builder selection for a meshlet batch item.
#[derive(Clone, Copy, Debug)]
pub enum MeshletBuilder {
    /// Consume triangles in source order; only the position count is used.
    Scan,
    /// Standard connectivity and cone-aware builder.
    Standard {
        /// Cone score weight.
        cone_weight: f32,
    },
    /// Flexible connectivity builder.
    Flex {
        /// Minimum triangles per meshlet.
        min_triangles: usize,
        /// Cone score weight.
        cone_weight: f32,
        /// Spatial split factor.
        split_factor: f32,
    },
    /// Surface-area heuristic spatial builder.
    Spatial {
        /// Minimum triangles per meshlet.
        min_triangles: usize,
        /// Fill weight.
        fill_weight: f32,
    },
}

/// Mesh and builder settings for one independent clustering operation.
#[derive(Clone, Copy, Debug)]
pub struct MeshletInput<'a> {
    /// Triangle topology.
    pub indices: &'a [u32],
    /// Full vertex positions.
    pub positions: Positions<'a>,
    /// Maximum vertices and triangles per meshlet.
    pub settings: MeshletSettings,
    /// Sequential algorithm and its parameters.
    pub builder: MeshletBuilder,
}
impl MeshletInput<'_> {
    /// Run this descriptor through the selected sequential meshlet builder.
    pub fn build(self, workspace: &mut Workspace) -> Result<Meshlets, Error> {
        let Self {
            indices,
            positions,
            settings,
            builder,
        } = self;
        match builder {
            MeshletBuilder::Scan => {
                crate::build_meshlets_scan(indices, positions.len(), settings, workspace)
            }
            MeshletBuilder::Standard { cone_weight } => {
                crate::build_meshlets(indices, positions, settings, cone_weight, workspace)
            }
            MeshletBuilder::Flex {
                min_triangles,
                cone_weight,
                split_factor,
            } => crate::build_meshlets_flex(
                indices,
                positions,
                settings,
                min_triangles,
                cone_weight,
                split_factor,
                workspace,
            ),
            MeshletBuilder::Spatial {
                min_triangles,
                fill_weight,
            } => crate::build_meshlets_spatial(
                indices,
                positions,
                settings,
                min_triangles,
                fill_weight,
                workspace,
            ),
        }
    }
}

/// Build meshlets for independent meshes, preserving descriptors and packed bytes.
pub fn build_meshlets_batch(inputs: &[MeshletInput<'_>], limits: Limits) -> BatchResult<Meshlets> {
    batch(inputs, limits, |input, workspace| input.build(workspace))
}

/// One independent cluster-LOD build, including its exclusive mutable positions.
#[cfg(feature = "clusterlod")]
pub struct ClusterLodInput<'a> {
    /// Complete pinned-demo configuration.
    pub config: crate::clusterlod::Config,
    /// Mesh input; dilation can mutate positions even when this item fails.
    pub mesh: crate::clusterlod::Mesh<'a>,
}

/// Build independent cluster-LOD DAGs in parallel using [`crate::clusterlod::build`].
///
/// Each mesh retains the demo's group/cluster order and refinement identifiers.
/// Dilation has the same partial-mutation contract as the sequential builder.
/// Groups within a mesh stay sequential: shared dilation and later refinement
/// consume earlier results. Parallelism is across meshes with disjoint positions.
#[cfg(feature = "clusterlod")]
pub fn build_cluster_lod_batch(
    inputs: &mut [ClusterLodInput<'_>],
    limits: Limits,
) -> BatchResult<Vec<crate::clusterlod::GroupOutput>> {
    let mut result = slots(inputs.len())?;
    result
        .par_iter_mut()
        .zip(inputs.par_iter_mut())
        .for_each(|(out, input)| {
            *out = item(limits, |workspace| {
                crate::clusterlod::build(
                    input.config,
                    crate::clusterlod::Mesh {
                        indices: input.mesh.indices,
                        positions: &mut *input.mesh.positions,
                        attributes: input.mesh.attributes,
                        vertex_lock: input.mesh.vertex_lock,
                        attribute_weights: input.mesh.attribute_weights,
                        attribute_protect_mask: input.mesh.attribute_protect_mask,
                    },
                    workspace,
                )
            });
        });
    Ok(result)
}

/// One independent cluster-LOD spatial forest.
#[cfg(feature = "clusterlod")]
#[derive(Clone, Copy, Debug)]
pub struct ClusterHierarchyInput<'a> {
    /// Groups in sequential callback order.
    pub groups: &'a [crate::clusterlod::Group],
    /// Maximum children per node, at least two.
    pub node_width: usize,
    /// Number of nonempty DAG levels.
    pub level_count: usize,
}

/// Build independent spatial forests using [`crate::clusterlod::build_hierarchy`].
///
/// Small batches (at most 256 total groups and 2048 group-by-level visits) run
/// sequentially on the calling thread to avoid dispatch overhead. Larger batches
/// use the current Rayon pool. Ordering, workspace reset and per-item limits are
/// identical on both paths. This cutoff is a workload heuristic, not a speed guarantee.
#[cfg(feature = "clusterlod")]
pub fn build_cluster_hierarchies_batch(
    inputs: &[ClusterHierarchyInput<'_>],
    limits: Limits,
) -> BatchResult<Vec<crate::clusterlod::Node>> {
    // D158: the measured 231-group/1848-visit family does not amortize
    // Rayon dispatch. Saturate this heuristic; validation stays per item.
    let (groups, visits) = inputs.iter().fold((0usize, 0usize), |(groups, visits), i| {
        (
            groups.saturating_add(i.groups.len()),
            visits.saturating_add(i.groups.len().saturating_mul(i.level_count.max(1))),
        )
    });
    let run = |input: &ClusterHierarchyInput<'_>, workspace: &mut Workspace| {
        crate::clusterlod::build_hierarchy(
            input.groups,
            input.node_width,
            input.level_count,
            workspace,
        )
    };
    if groups <= 256 && visits <= 2048 {
        let mut result = slots(inputs.len())?;
        for (out, input) in result.iter_mut().zip(inputs) {
            *out = item(limits, |workspace| run(input, workspace));
        }
        Ok(result)
    } else {
        batch(inputs, limits, run)
    }
}