Skip to main content

meshoptimizer_rs/codec/
encode.rs

1// Public encoder API: explicit per-call version/level configuration replaces
2// upstream's global meshopt_encodeVertexVersion/meshopt_encodeIndexVersion.
3use super::{filter_encode, index_encode, vertex_encode};
4use crate::{workspace::checked_bytes, Error, Workspace};
5use alloc::vec::Vec;
6
7/// Vertex codec format version and compression level
8/// (the `version` and `level` of meshopt_encodeVertexBufferLevel).
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct VertexEncoding {
11    version: u8,
12    level: u8,
13}
14impl VertexEncoding {
15    /// Upstream defaults of meshopt_encodeVertexBuffer: version 1, level 2.
16    pub const DEFAULT: Self = Self {
17        version: 1,
18        level: 2,
19    };
20    /// Check a version (0 or 1) and level (0 through 9, the range the pinned
21    /// reference accepts; 1.3 behaves identically for levels 3 through 9).
22    /// Version 0 ignores the level, as upstream does.
23    ///
24    /// # Errors
25    /// `UnsupportedVersion` if the version is not 0 or 1; `InvalidParameter` if the vertex encoding level exceeds 9.
26    pub const fn new(version: u8, level: u8) -> Result<Self, Error> {
27        if version > 1 {
28            return Err(Error::UnsupportedVersion);
29        }
30        if level > 9 {
31            return Err(Error::InvalidParameter);
32        }
33        Ok(Self { version, level })
34    }
35    /// Format version.
36    pub const fn version(self) -> u8 {
37        self.version
38    }
39    /// Compression level.
40    pub const fn level(self) -> u8 {
41        self.level
42    }
43}
44impl Default for VertexEncoding {
45    fn default() -> Self {
46        Self::DEFAULT
47    }
48}
49
50/// Index buffer and index sequence format version
51/// (the setting of meshopt_encodeIndexVersion).
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct IndexEncoding {
54    version: u8,
55}
56impl IndexEncoding {
57    /// Upstream default: version 1.
58    pub const DEFAULT: Self = Self { version: 1 };
59    /// Check a version: 0 (decodable by all versions) or 1.
60    ///
61    /// # Errors
62    /// `UnsupportedVersion` if the version is not 0 or 1;
63    pub const fn new(version: u8) -> Result<Self, Error> {
64        if version > 1 {
65            return Err(Error::UnsupportedVersion);
66        }
67        Ok(Self { version })
68    }
69    /// Format version.
70    pub const fn version(self) -> u8 {
71        self.version
72    }
73}
74impl Default for IndexEncoding {
75    fn default() -> Self {
76        Self::DEFAULT
77    }
78}
79
80/// Exponent sharing of the exponential filter encoder (meshopt_EncodeExpMode).
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub enum ExpMode {
83    /// A separate exponent for each component (maximum quality).
84    Separate,
85    /// One exponent for all components of a vector.
86    SharedVector,
87    /// One exponent for each component across all vectors.
88    SharedComponent,
89    /// Separate exponents clamped to at least zero.
90    Clamped,
91}
92
93#[inline]
94fn vertex_layout(count: usize, stride: usize) -> Result<usize, Error> {
95    if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
96        return Err(Error::InvalidLayout);
97    }
98    checked_bytes(count, stride)
99}
100
101/// meshopt_encodeVertexBufferBound: worst-case encoded size for either version.
102///
103/// # Errors
104/// `InvalidLayout` for an unsupported stride;
105/// `SizeOverflow` if the worst-case encoded size cannot be represented.
106#[inline]
107pub fn encode_vertex_buffer_bound(count: usize, stride: usize) -> Result<usize, Error> {
108    if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
109        return Err(Error::InvalidLayout);
110    }
111    vertex_encode::bound(count, stride)
112}
113
114/// meshopt_encodeIndexBufferBound. `index_count` must be a multiple of three.
115///
116/// # Errors
117/// `InvalidTopology` for incomplete triangles;
118/// `SizeOverflow` if the worst-case encoded size cannot be represented.
119#[inline]
120pub fn encode_index_buffer_bound(index_count: usize, vertex_count: usize) -> Result<usize, Error> {
121    if !index_count.is_multiple_of(3) {
122        return Err(Error::InvalidTopology);
123    }
124    index_encode::index_bound(index_count, vertex_count as u64)
125}
126
127/// meshopt_encodeIndexSequenceBound.
128///
129/// # Errors
130/// `SizeOverflow` if the worst-case encoded size cannot be represented.
131#[inline]
132pub fn encode_index_sequence_bound(
133    index_count: usize,
134    vertex_count: usize,
135) -> Result<usize, Error> {
136    index_encode::sequence_bound(index_count, vertex_count as u64)
137}
138
139/// Account an allocating encode against the limits before any allocation.
140fn charge(bound: usize, input: usize, workspace: &mut Workspace) -> Result<(), Error> {
141    let mut work = workspace.begin();
142    let check = (|| {
143        workspace.account_codec(bound)?;
144        work.add(input)?;
145        work.add(bound)
146    })();
147    workspace.finish(&work);
148    check
149}
150
151/// Vertex encode on the current Rayon pool with the serial encoder's bytes,
152/// limits and errors; one worker or a short buffer encodes serially.
153#[cfg(feature = "parallel")]
154pub(crate) fn encode_vertex_buffer_parallel(
155    vertices: &[u8],
156    count: usize,
157    stride: usize,
158    encoding: VertexEncoding,
159    workspace: &mut Workspace,
160) -> Result<Vec<u8>, Error> {
161    let valid = stride != 0 && stride <= 256 && stride.is_multiple_of(4);
162    if !valid
163        || rayon::current_num_threads() < 2
164        || count.div_ceil(vertex_encode::block_size(stride)) < 2 * vertex_encode::MIN_CHUNK_BLOCKS
165    {
166        return encode_vertex_buffer(vertices, count, stride, encoding, workspace);
167    }
168    let _ = workspace.begin();
169    if vertices.len() != vertex_layout(count, stride)? {
170        return Err(Error::InvalidLayout);
171    }
172    let bound = vertex_encode::bound(count, stride)?;
173    charge(bound, vertices.len(), workspace)?;
174    vertex_encode::encode_parallel(vertices, count, stride, encoding.version, encoding.level)
175}
176
177/// Shared allocating driver: account the bound, reserve fallibly, encode and
178/// truncate to the used length (the capacity stays at the accounted bound).
179fn allocate(
180    bound: usize,
181    input: usize,
182    workspace: &mut Workspace,
183    encode: impl FnOnce(&mut [u8]) -> Result<usize, Error>,
184) -> Result<Vec<u8>, Error> {
185    charge(bound, input, workspace)?;
186    let mut out = Vec::new();
187    out.try_reserve_exact(bound)
188        .map_err(|_| Error::AllocationFailed)?;
189    if out.capacity() != bound {
190        workspace.account_codec(out.capacity())?;
191    }
192    out.resize(bound, 0);
193    let used = encode(&mut out)?;
194    out.truncate(used);
195    Ok(out)
196}
197
198/// Shared caller-buffer driver. Work charges input bytes plus the bytes the
199/// encoder may examine in the destination (at most the bound).
200fn into(
201    bound: usize,
202    capacity: usize,
203    input: usize,
204    workspace: &mut Workspace,
205    encode: impl FnOnce() -> Result<usize, Error>,
206) -> Result<usize, Error> {
207    let mut work = workspace.begin();
208    let result = (|| {
209        workspace.account_codec(0)?;
210        work.add(input)?;
211        work.add(bound.min(capacity))?;
212        encode()
213    })();
214    workspace.finish(&work);
215    result
216}
217
218/// Encode a vertex buffer (meshopt_encodeVertexBufferLevel) with explicit
219/// version and level into a vector. `vertices` holds exactly count * stride
220/// bytes; all bytes, including padding, are encoded verbatim. stride is a
221/// multiple of four from 4 to 256.
222///
223/// # Errors
224/// `InvalidLayout` for invalid stride or input byte length;
225/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
226pub fn encode_vertex_buffer(
227    vertices: &[u8],
228    count: usize,
229    stride: usize,
230    encoding: VertexEncoding,
231    workspace: &mut Workspace,
232) -> Result<Vec<u8>, Error> {
233    let _ = workspace.begin();
234    if vertices.len() != vertex_layout(count, stride)? {
235        return Err(Error::InvalidLayout);
236    }
237    let bound = vertex_encode::bound(count, stride)?;
238    allocate(bound, vertices.len(), workspace, |out| {
239        vertex_encode::encode(
240            out,
241            vertices,
242            count,
243            stride,
244            encoding.version,
245            encoding.level,
246        )
247    })
248}
249
250/// Encode a vertex buffer into a caller buffer and return the encoded length.
251/// Fails with BufferTooSmall exactly where the reference returns 0; the
252/// encoder needs slack beyond the final size, so use
253/// [`encode_vertex_buffer_bound`]. On failure the buffer prefix may change.
254///
255/// # Errors
256/// `InvalidLayout` for invalid stride or input byte length;
257/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
258/// `BufferTooSmall` if the destination cannot hold the encoded stream.
259pub fn encode_vertex_buffer_into(
260    destination: &mut [u8],
261    vertices: &[u8],
262    count: usize,
263    stride: usize,
264    encoding: VertexEncoding,
265    workspace: &mut Workspace,
266) -> Result<usize, Error> {
267    let _ = workspace.begin();
268    if vertices.len() != vertex_layout(count, stride)? {
269        return Err(Error::InvalidLayout);
270    }
271    let bound = vertex_encode::bound(count, stride)?;
272    let capacity = destination.len();
273    into(bound, capacity, vertices.len(), workspace, || {
274        vertex_encode::encode(
275            destination,
276            vertices,
277            count,
278            stride,
279            encoding.version,
280            encoding.level,
281        )
282    })
283}
284
285/// A vertex count giving the same bound as `max index + 1`: the bound only
286/// depends on the bit length of the largest index, which the bitwise OR of
287/// all indices shares (and an OR reduction vectorizes on baseline x86-64).
288fn max_vertices(indices: &[u32]) -> u64 {
289    if indices.is_empty() {
290        return 0;
291    }
292    u64::from(indices.iter().fold(0, |a, &b| a | b)) + 1
293}
294
295/// Encode a triangle list (meshopt_encodeIndexBuffer) with an explicit version.
296/// The length is a multiple of three. Corners may be rotated within each
297/// triangle, as upstream does; decoding returns the rotated list.
298///
299/// # Errors
300/// `InvalidTopology` for incomplete triangles;
301/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
302#[inline]
303pub fn encode_index_buffer_generic<I: crate::Index>(
304    indices: &[I],
305    encoding: IndexEncoding,
306    workspace: &mut Workspace,
307) -> Result<Vec<u8>, Error> {
308    I::with_input(indices, workspace, |indices, workspace| {
309        encode_index_buffer_u32(indices, encoding, workspace)
310    })
311}
312
313fn encode_index_buffer_u32(
314    indices: &[u32],
315    encoding: IndexEncoding,
316    workspace: &mut Workspace,
317) -> Result<Vec<u8>, Error> {
318    let _ = workspace.begin();
319    if !indices.len().is_multiple_of(3) {
320        return Err(Error::InvalidTopology);
321    }
322    let bound = index_encode::index_bound(indices.len(), max_vertices(indices))?;
323    let input = checked_bytes(indices.len(), 4)?;
324    allocate(bound, input, workspace, |out| {
325        index_encode::encode_index_buffer(out, indices, encoding.version)
326    })
327}
328
329/// Encode a triangle list into a caller buffer and return the encoded length.
330/// Fails with BufferTooSmall exactly where the reference returns 0; on
331/// failure the buffer prefix may change.
332///
333/// # Errors
334/// `InvalidTopology` for incomplete triangles;
335/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
336/// `BufferTooSmall` if the destination cannot hold the encoded stream.
337#[inline]
338pub fn encode_index_buffer_into_generic<I: crate::Index>(
339    destination: &mut [u8],
340    indices: &[I],
341    encoding: IndexEncoding,
342    workspace: &mut Workspace,
343) -> Result<usize, Error> {
344    I::with_input(indices, workspace, |indices, workspace| {
345        encode_index_buffer_into_u32(destination, indices, encoding, workspace)
346    })
347}
348
349fn encode_index_buffer_into_u32(
350    destination: &mut [u8],
351    indices: &[u32],
352    encoding: IndexEncoding,
353    workspace: &mut Workspace,
354) -> Result<usize, Error> {
355    let _ = workspace.begin();
356    if !indices.len().is_multiple_of(3) {
357        return Err(Error::InvalidTopology);
358    }
359    let bound = index_encode::index_bound(indices.len(), 1 << 32)?;
360    let input = checked_bytes(indices.len(), 4)?;
361    let capacity = destination.len();
362    into(bound, capacity, input, workspace, || {
363        index_encode::encode_index_buffer(destination, indices, encoding.version)
364    })
365}
366
367/// Encode an arbitrary index sequence (meshopt_encodeIndexSequence) with an
368/// explicit version.
369///
370/// # Errors
371/// `SizeOverflow` for overflowing input/output sizes;
372/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
373#[inline]
374pub fn encode_index_sequence_generic<I: crate::Index>(
375    indices: &[I],
376    encoding: IndexEncoding,
377    workspace: &mut Workspace,
378) -> Result<Vec<u8>, Error> {
379    I::with_input(indices, workspace, |indices, workspace| {
380        encode_index_sequence_u32(indices, encoding, workspace)
381    })
382}
383
384fn encode_index_sequence_u32(
385    indices: &[u32],
386    encoding: IndexEncoding,
387    workspace: &mut Workspace,
388) -> Result<Vec<u8>, Error> {
389    let _ = workspace.begin();
390    let bound = index_encode::sequence_bound(indices.len(), max_vertices(indices))?;
391    let input = checked_bytes(indices.len(), 4)?;
392    allocate(bound, input, workspace, |out| {
393        index_encode::encode_index_sequence(out, indices, encoding.version)
394    })
395}
396
397/// Encode an index sequence into a caller buffer and return the encoded
398/// length. Fails with BufferTooSmall exactly where the reference returns 0;
399/// on failure the buffer prefix may change.
400///
401/// # Errors
402/// `SizeOverflow` for overflowing input/output sizes;
403/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
404/// `BufferTooSmall` if the destination cannot hold the encoded stream.
405#[inline]
406pub fn encode_index_sequence_into_generic<I: crate::Index>(
407    destination: &mut [u8],
408    indices: &[I],
409    encoding: IndexEncoding,
410    workspace: &mut Workspace,
411) -> Result<usize, Error> {
412    I::with_input(indices, workspace, |indices, workspace| {
413        encode_index_sequence_into_u32(destination, indices, encoding, workspace)
414    })
415}
416
417fn encode_index_sequence_into_u32(
418    destination: &mut [u8],
419    indices: &[u32],
420    encoding: IndexEncoding,
421    workspace: &mut Workspace,
422) -> Result<usize, Error> {
423    let _ = workspace.begin();
424    let bound = index_encode::sequence_bound(indices.len(), 1 << 32)?;
425    let input = checked_bytes(indices.len(), 4)?;
426    let capacity = destination.len();
427    into(bound, capacity, input, workspace, || {
428        index_encode::encode_index_sequence(destination, indices, encoding.version)
429    })
430}
431
432#[derive(Clone, Copy, PartialEq, Eq)]
433enum Kind {
434    Oct,
435    Quat,
436    Exp(ExpMode),
437    Color,
438}
439
440/// Validate a filter encoder call; returns the output byte length.
441fn filter_layout(
442    kind: Kind,
443    count: usize,
444    stride: usize,
445    bits: u32,
446    data: &[f32],
447) -> Result<usize, Error> {
448    let (layout, floats, range) = match kind {
449        Kind::Oct | Kind::Color => (stride == 4 || stride == 8, 4, 2..=16),
450        Kind::Quat => (stride == 8, 4, 4..=16),
451        Kind::Exp(_) => (
452            stride > 0 && stride <= 256 && stride.is_multiple_of(4),
453            stride / 4,
454            1..=24,
455        ),
456    };
457    if !layout {
458        return Err(Error::InvalidLayout);
459    }
460    if !range.contains(&bits)
461        || (stride == 4 && bits > 8 && matches!(kind, Kind::Oct | Kind::Color))
462    {
463        return Err(Error::InvalidParameter);
464    }
465    if data.len() != checked_bytes(count, floats)? {
466        return Err(Error::InvalidLayout);
467    }
468    checked_bytes(count, stride)
469}
470
471fn filter_run(
472    kind: Kind,
473    out: &mut [u8],
474    stride: usize,
475    bits: u32,
476    data: &[f32],
477) -> Result<(), Error> {
478    match kind {
479        Kind::Oct => filter_encode::oct(out, stride, bits, data),
480        Kind::Quat => filter_encode::quat(out, bits, data),
481        Kind::Color => filter_encode::color(out, stride, bits, data),
482        Kind::Exp(mode) => return filter_encode::exp(out, stride, bits, data, mode),
483    }
484    Ok(())
485}
486
487fn filter_into(
488    kind: Kind,
489    destination: &mut [u8],
490    count: usize,
491    stride: usize,
492    bits: u32,
493    data: &[f32],
494    workspace: &mut Workspace,
495) -> Result<(), Error> {
496    filter_into_with(
497        kind,
498        destination,
499        count,
500        stride,
501        bits,
502        data,
503        workspace,
504        |out| filter_run(kind, out, stride, bits, data),
505    )
506}
507
508// Monomorphize Oct/Quat calls so generic filter dispatch and unrelated kernel
509// stack setup do not dominate small inputs. Validation/accounting is shared.
510#[allow(clippy::too_many_arguments)]
511fn filter_into_with(
512    kind: Kind,
513    destination: &mut [u8],
514    count: usize,
515    stride: usize,
516    bits: u32,
517    data: &[f32],
518    workspace: &mut Workspace,
519    run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
520) -> Result<(), Error> {
521    let mut work = workspace.begin();
522    let result = (|| {
523        let bytes = filter_layout(kind, count, stride, bits, data)?;
524        if destination.len() < bytes {
525            return Err(Error::BufferTooSmall);
526        }
527        workspace.account_codec(0)?;
528        work.add(data.len())?;
529        work.add(bytes / 4)?;
530        run(&mut destination[..bytes])
531    })();
532    workspace.finish(&work);
533    result
534}
535
536fn filter_vec(
537    kind: Kind,
538    count: usize,
539    stride: usize,
540    bits: u32,
541    data: &[f32],
542    workspace: &mut Workspace,
543) -> Result<Vec<u8>, Error> {
544    filter_vec_with(kind, count, stride, bits, data, workspace, |out| {
545        filter_run(kind, out, stride, bits, data)
546    })
547}
548
549fn filter_vec_with(
550    kind: Kind,
551    count: usize,
552    stride: usize,
553    bits: u32,
554    data: &[f32],
555    workspace: &mut Workspace,
556    run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
557) -> Result<Vec<u8>, Error> {
558    let mut work = workspace.begin();
559    let check = (|| {
560        let bytes = filter_layout(kind, count, stride, bits, data)?;
561        workspace.account_codec(bytes)?;
562        work.add(data.len())?;
563        work.add(bytes / 4)?;
564        Ok(bytes)
565    })();
566    workspace.finish(&work);
567    let bytes = check?;
568    let mut out = Vec::new();
569    out.try_reserve_exact(bytes)
570        .map_err(|_| Error::AllocationFailed)?;
571    if out.capacity() != bytes {
572        workspace.account_codec(out.capacity())?;
573    }
574    out.resize(bytes, 0);
575    run(&mut out)?;
576    Ok(out)
577}
578
579/// Octahedral filter encoder (meshopt_encodeFilterOct): `data` holds four
580/// floats per vector; stride 4 takes 2..=8 bits, stride 8 takes 2..=16.
581/// Z stores 1.0 and W is quantized to the full byte width.
582///
583/// # Errors
584/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
585/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
586pub fn encode_filter_oct(
587    count: usize,
588    stride: usize,
589    bits: u32,
590    data: &[f32],
591    workspace: &mut Workspace,
592) -> Result<Vec<u8>, Error> {
593    filter_vec_with(Kind::Oct, count, stride, bits, data, workspace, |out| {
594        filter_encode::oct(out, stride, bits, data);
595        Ok(())
596    })
597}
598/// Caller-buffer form of [`encode_filter_oct`]; the destination tail is preserved.
599///
600/// # Errors
601/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
602/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
603/// `BufferTooSmall` if the destination is too short.
604pub fn encode_filter_oct_into(
605    destination: &mut [u8],
606    count: usize,
607    stride: usize,
608    bits: u32,
609    data: &[f32],
610    workspace: &mut Workspace,
611) -> Result<(), Error> {
612    filter_into_with(
613        Kind::Oct,
614        destination,
615        count,
616        stride,
617        bits,
618        data,
619        workspace,
620        |out| {
621            filter_encode::oct(out, stride, bits, data);
622            Ok(())
623        },
624    )
625}
626/// Quaternion filter encoder (meshopt_encodeFilterQuat): four floats per
627/// quaternion, stride 8, 4..=16 bits.
628///
629/// # Errors
630/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
631/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
632pub fn encode_filter_quat(
633    count: usize,
634    stride: usize,
635    bits: u32,
636    data: &[f32],
637    workspace: &mut Workspace,
638) -> Result<Vec<u8>, Error> {
639    filter_vec_with(Kind::Quat, count, stride, bits, data, workspace, |out| {
640        filter_encode::quat(out, bits, data);
641        Ok(())
642    })
643}
644/// Caller-buffer form of [`encode_filter_quat`]; the destination tail is preserved.
645///
646/// # Errors
647/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
648/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
649/// `BufferTooSmall` if the destination is too short.
650pub fn encode_filter_quat_into(
651    destination: &mut [u8],
652    count: usize,
653    stride: usize,
654    bits: u32,
655    data: &[f32],
656    workspace: &mut Workspace,
657) -> Result<(), Error> {
658    filter_into_with(
659        Kind::Quat,
660        destination,
661        count,
662        stride,
663        bits,
664        data,
665        workspace,
666        |out| {
667            filter_encode::quat(out, bits, data);
668            Ok(())
669        },
670    )
671}
672/// Exponential filter encoder (meshopt_encodeFilterExp): stride / 4 floats
673/// per vector, stride a multiple of four up to 256, 1..=24 mantissa bits.
674/// Returns NumericalFailure where the reference conversion is undefined
675/// (non-finite input; one-bit mantissas with exponent 128); the output may
676/// then be partially written.
677///
678/// # Errors
679/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
680/// `NumericalFailure` for non-finite exponential intermediates; `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
681pub fn encode_filter_exp(
682    count: usize,
683    stride: usize,
684    bits: u32,
685    data: &[f32],
686    mode: ExpMode,
687    workspace: &mut Workspace,
688) -> Result<Vec<u8>, Error> {
689    filter_vec(Kind::Exp(mode), count, stride, bits, data, workspace)
690}
691/// Caller-buffer form of [`encode_filter_exp`]; the destination tail is preserved.
692///
693/// # Errors
694/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
695/// `NumericalFailure` for non-finite exponential intermediates; `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
696/// `BufferTooSmall` if the destination is too short.
697pub fn encode_filter_exp_into(
698    destination: &mut [u8],
699    count: usize,
700    stride: usize,
701    bits: u32,
702    data: &[f32],
703    mode: ExpMode,
704    workspace: &mut Workspace,
705) -> Result<(), Error> {
706    filter_into(
707        Kind::Exp(mode),
708        destination,
709        count,
710        stride,
711        bits,
712        data,
713        workspace,
714    )
715}
716/// Color filter encoder (meshopt_encodeFilterColor): four floats per RGBA
717/// color into YCoCg-R plus alpha; stride 4 takes 2..=8 bits, stride 8 2..=16.
718///
719/// # Errors
720/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
721/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
722pub fn encode_filter_color(
723    count: usize,
724    stride: usize,
725    bits: u32,
726    data: &[f32],
727    workspace: &mut Workspace,
728) -> Result<Vec<u8>, Error> {
729    filter_vec(Kind::Color, count, stride, bits, data, workspace)
730}
731/// Caller-buffer form of [`encode_filter_color`]; the destination tail is preserved.
732///
733/// # Errors
734/// `InvalidLayout` for invalid stride/input length; `InvalidParameter` for unsupported precision;
735/// `SizeOverflow` for size overflow or `LimitExceeded` for exhausted limits.
736/// `BufferTooSmall` if the destination is too short.
737pub fn encode_filter_color_into(
738    destination: &mut [u8],
739    count: usize,
740    stride: usize,
741    bits: u32,
742    data: &[f32],
743    workspace: &mut Workspace,
744) -> Result<(), Error> {
745    filter_into(
746        Kind::Color,
747        destination,
748        count,
749        stride,
750        bits,
751        data,
752        workspace,
753    )
754}
755
756/// The source-compatible u32 entry point; see [`encode_index_buffer_generic`] for selectable index widths.
757#[inline]
758pub fn encode_index_buffer(
759    indices: &[u32],
760    encoding: IndexEncoding,
761    workspace: &mut Workspace,
762) -> Result<Vec<u8>, Error> {
763    encode_index_buffer_generic::<u32>(indices, encoding, workspace)
764}
765
766/// The source-compatible u32 entry point; see [`encode_index_buffer_into_generic`] for selectable index widths.
767#[inline]
768pub fn encode_index_buffer_into(
769    destination: &mut [u8],
770    indices: &[u32],
771    encoding: IndexEncoding,
772    workspace: &mut Workspace,
773) -> Result<usize, Error> {
774    encode_index_buffer_into_generic::<u32>(destination, indices, encoding, workspace)
775}
776
777/// The source-compatible u32 entry point; see [`encode_index_sequence_generic`] for selectable index widths.
778#[inline]
779pub fn encode_index_sequence(
780    indices: &[u32],
781    encoding: IndexEncoding,
782    workspace: &mut Workspace,
783) -> Result<Vec<u8>, Error> {
784    encode_index_sequence_generic::<u32>(indices, encoding, workspace)
785}
786
787/// The source-compatible u32 entry point; see [`encode_index_sequence_into_generic`] for selectable index widths.
788#[inline]
789pub fn encode_index_sequence_into(
790    destination: &mut [u8],
791    indices: &[u32],
792    encoding: IndexEncoding,
793    workspace: &mut Workspace,
794) -> Result<usize, Error> {
795    encode_index_sequence_into_generic::<u32>(destination, indices, encoding, workspace)
796}