Skip to main content

meshoptimizer_rs/
remap.rs

1//! Index generation and fetch processing from upstream indexgenerator.cpp and
2//! vfetchoptimizer.cpp. Hash probing and callback traversal match upstream.
3use crate::budget::Budget;
4use crate::workspace::{checked_bytes, topology, Work};
5use crate::{Error, Positions, Workspace};
6use alloc::vec::Vec;
7
8const NONE: u32 = u32::MAX;
9
10/// Initialized byte records used as binary equality keys. Supplied padding is
11/// part of equality; no typed object is reinterpreted as bytes.
12#[derive(Clone, Copy, Debug)]
13pub struct VertexStream<'a> {
14    data: &'a [u8],
15    count: usize,
16    pub(crate) size: usize,
17    stride: usize,
18    offset: usize,
19}
20impl<'a> VertexStream<'a> {
21    /// Validate a stream with a key size of 1 through 256 bytes. Stride may
22    /// exceed 256; only the key bytes participate in equality.
23    ///
24    /// # Errors
25    /// `InvalidLayout` for invalid record size, stride, offset or length; `SizeOverflow` for overflowing view sizes.
26    pub fn new(
27        data: &'a [u8],
28        count: usize,
29        size: usize,
30        stride: usize,
31        offset: usize,
32    ) -> Result<Self, Error> {
33        if size == 0 || size > 256 || stride < size {
34            return Err(Error::InvalidLayout);
35        }
36        let end = if count == 0 {
37            offset
38        } else {
39            (count - 1)
40                .checked_mul(stride)
41                .and_then(|v| v.checked_add(offset))
42                .and_then(|v| v.checked_add(size))
43                .ok_or(Error::SizeOverflow)?
44        };
45        if end > data.len() {
46            return Err(Error::InvalidLayout);
47        }
48        if count > u32::MAX as usize {
49            return Err(Error::SizeOverflow);
50        }
51        Ok(Self {
52            data,
53            count,
54            size,
55            stride,
56            offset,
57        })
58    }
59    /// Validate tightly packed initialized records.
60    ///
61    /// # Errors
62    /// `InvalidLayout` for invalid record size, stride, offset or length; `SizeOverflow` for overflowing view sizes.
63    pub fn packed(data: &'a [u8], size: usize) -> Result<Self, Error> {
64        if size == 0 || !data.len().is_multiple_of(size) {
65            return Err(Error::InvalidLayout);
66        }
67        Self::new(data, data.len() / size, size, size, 0)
68    }
69    /// Number of records.
70    pub const fn len(self) -> usize {
71        self.count
72    }
73    /// Whether there are no records.
74    pub const fn is_empty(self) -> bool {
75        self.count == 0
76    }
77    #[inline(always)]
78    pub(crate) fn key(self, i: u32) -> &'a [u8] {
79        let start = self.offset + i as usize * self.stride;
80        &self.data[start..start + self.size]
81    }
82    #[inline(always)]
83    fn key_array<const SIZE: usize>(self, i: u32) -> &'a [u8; SIZE] {
84        let start = self.offset + i as usize * self.stride;
85        self.data[start..start + SIZE]
86            .try_into()
87            .expect("validated fixed-width key")
88    }
89}
90
91/// A compact vertex numbering; unused vertices retain `u32::MAX`.
92#[derive(Debug)]
93pub struct VertexRemap {
94    /// Original vertex to compact vertex index.
95    pub remap: Vec<u32>,
96    /// Number of unique referenced vertices.
97    pub vertex_count: usize,
98}
99/// Fetch-ordered initialized vertex bytes and rewritten indices.
100#[derive(Debug)]
101pub struct FetchedMeshGeneric<I: crate::Index = u32> {
102    /// Rewritten topology.
103    pub indices: Vec<I>,
104    /// Used tightly packed vertex records.
105    pub vertices: Vec<u8>,
106    /// Number of used vertices.
107    pub vertex_count: usize,
108}
109/// Unique first-corner indices and source vertices for provoking-vertex rendering.
110#[derive(Debug)]
111pub struct ProvokingMeshGeneric<I: crate::Index = u32> {
112    /// Rotated and rewritten triangle indices.
113    pub indices: Vec<I>,
114    /// Output vertex to original vertex mapping, including clones.
115    pub reorder: Vec<u32>,
116}
117
118fn buckets(n: usize) -> Result<usize, Error> {
119    n.checked_add(n / 4)
120        .ok_or(Error::SizeOverflow)?
121        .max(1)
122        .checked_next_power_of_two()
123        .ok_or(Error::SizeOverflow)
124}
125#[inline(always)]
126fn hash_update(mut h: u32, mut key: &[u8]) -> u32 {
127    const M: u32 = 0x5bd1e995;
128    while key.len() >= 4 {
129        let mut k = u32::from_le_bytes(key[..4].try_into().expect("four bytes"));
130        k = k.wrapping_mul(M);
131        k ^= k >> 24;
132        k = k.wrapping_mul(M);
133        h = h.wrapping_mul(M) ^ k;
134        key = &key[4..];
135    }
136    if !key.is_empty() {
137        h ^= u32::from(key[0])
138            | (u32::from(key[key.len() / 2]) << 8)
139            | (u32::from(key[key.len() - 1]) << 16);
140        h = h.wrapping_mul(M);
141        h ^= h >> 24;
142    }
143    h
144}
145#[inline]
146fn spatial_hash(p: [u32; 3]) -> u32 {
147    p[0].wrapping_mul(73856093) ^ p[1].wrapping_mul(19349663) ^ p[2].wrapping_mul(83492791)
148}
149#[inline(always)]
150fn position_hash(p: Positions<'_>, i: u32) -> u32 {
151    position_value_hash(p.get(i as usize).expect("validated position"))
152}
153#[inline(always)]
154fn position_value_hash(p: [f32; 3]) -> u32 {
155    spatial_hash(p.map(|v| {
156        let mut x = v.to_bits();
157        if x == 0x80000000 {
158            x = 0;
159        }
160        x ^ (x >> 17)
161    }))
162}
163#[inline(always)]
164fn validate_indices(indices: Option<&[u32]>, count: usize, work: &mut Work) -> Result<(), Error> {
165    if count > u32::MAX as usize {
166        return Err(Error::SizeOverflow);
167    }
168    if let Some(indices) = indices {
169        work.indices(indices, count)?;
170    }
171    Ok(())
172}
173fn streams_valid(streams: &[VertexStream<'_>]) -> Result<usize, Error> {
174    if streams.is_empty() || streams.len() > 16 {
175        return Err(Error::InvalidLayout);
176    }
177    let count = streams[0].count;
178    if streams.iter().any(|s| s.count != count) {
179        return Err(Error::InvalidLayout);
180    }
181    Ok(count)
182}
183#[inline]
184fn stream_hash(streams: &[VertexStream<'_>], i: u32) -> u32 {
185    streams.iter().fold(0, |h, s| hash_update(h, s.key(i)))
186}
187#[inline]
188fn stream_equal(streams: &[VertexStream<'_>], a: u32, b: u32) -> bool {
189    streams.iter().all(|s| s.key(a) == s.key(b))
190}
191#[inline(always)]
192fn generate_record<const CANONICAL: bool, const LIMITED: bool, const OUTLINE: bool>(
193    index: u32,
194    remap: &mut [u32],
195    table: &mut [u32],
196    hash: &impl Fn(u32) -> u32,
197    equal: &mut impl FnMut(u32, u32) -> bool,
198    next: &mut u32,
199    budget: (&mut Work, &mut usize),
200) -> Result<(), Error> {
201    let (work, visits) = budget;
202    if LIMITED {
203        work.add(1)?;
204    } else {
205        *visits += 1;
206    }
207    if remap[index as usize] != NONE {
208        return Ok(());
209    }
210    if OUTLINE {
211        generate_unique_outlined::<CANONICAL, LIMITED>(
212            index,
213            remap,
214            table,
215            hash,
216            equal,
217            next,
218            (work, visits),
219        )
220    } else {
221        generate_unique::<CANONICAL, LIMITED>(
222            index,
223            remap,
224            table,
225            hash,
226            equal,
227            next,
228            (work, visits),
229        )
230    }
231}
232// Keep the common already-mapped scan small for callback-heavy custom remaps.
233#[inline(never)]
234fn generate_unique_outlined<const CANONICAL: bool, const LIMITED: bool>(
235    index: u32,
236    remap: &mut [u32],
237    table: &mut [u32],
238    hash: &impl Fn(u32) -> u32,
239    equal: &mut impl FnMut(u32, u32) -> bool,
240    next: &mut u32,
241    budget: (&mut Work, &mut usize),
242) -> Result<(), Error> {
243    generate_unique::<CANONICAL, LIMITED>(index, remap, table, hash, equal, next, budget)
244}
245#[inline(always)]
246fn generate_unique<const CANONICAL: bool, const LIMITED: bool>(
247    index: u32,
248    remap: &mut [u32],
249    table: &mut [u32],
250    hash: &impl Fn(u32) -> u32,
251    equal: &mut impl FnMut(u32, u32) -> bool,
252    next: &mut u32,
253    budget: (&mut Work, &mut usize),
254) -> Result<(), Error> {
255    let (work, visits) = budget;
256    let mask = table.len() - 1;
257    let mut bucket = hash(index) as usize & mask;
258    let mut found = false;
259    for probe in 0..table.len() {
260        if LIMITED {
261            work.add(1)?;
262        } else {
263            *visits += 1;
264        }
265        if table[bucket] == NONE || equal(table[bucket], index) {
266            found = true;
267            break;
268        }
269        bucket = (bucket + probe + 1) & mask;
270    }
271    if !found {
272        return Err(Error::NumericalFailure);
273    }
274    if table[bucket] == NONE {
275        table[bucket] = index;
276        if CANONICAL {
277            remap[index as usize] = index;
278        } else {
279            remap[index as usize] = *next;
280            *next += 1;
281        }
282    } else if CANONICAL {
283        remap[index as usize] = table[bucket];
284    } else {
285        remap[index as usize] = remap[table[bucket] as usize];
286    }
287    Ok(())
288}
289fn generate_kernel<const CANONICAL: bool, const PREPARED: bool, const OUTLINE: bool>(
290    remap: &mut [u32],
291    indices: Option<&[u32]>,
292    table: &mut [u32],
293    hash: impl Fn(u32) -> u32,
294    equal: impl FnMut(u32, u32) -> bool,
295    work: &mut Work,
296) -> Result<usize, Error> {
297    // Select indexed/linear traversal once, outside the bounded hash batches.
298    if let Some(indices) = indices {
299        generate_custom_kernel::<CANONICAL, PREPARED, true>(
300            remap, indices, table, hash, equal, work,
301        )
302    } else {
303        generate_custom_kernel::<CANONICAL, PREPARED, false>(remap, &[], table, hash, equal, work)
304    }
305}
306fn generate_custom_kernel<const CANONICAL: bool, const PREPARED: bool, const INDEXED: bool>(
307    remap: &mut [u32],
308    indices: &[u32],
309    table: &mut [u32],
310    hash: impl Fn(u32) -> u32,
311    mut equal: impl FnMut(u32, u32) -> bool,
312    work: &mut Work,
313) -> Result<usize, Error> {
314    work.add(remap.len())?;
315    if !PREPARED {
316        remap.fill(NONE);
317    }
318    work.add(table.len())?;
319    table.fill(NONE);
320    let n = if INDEXED { indices.len() } else { remap.len() };
321    let bound = table.len().checked_add(1).ok_or(Error::SizeOverflow)?;
322    let mut next = 0;
323    let mut begin = 0;
324    while begin < n {
325        let count = work.batch(n - begin, bound)?;
326        let mut visits = 0;
327        if count == 0 {
328            let index = if INDEXED {
329                indices[begin]
330            } else {
331                begin as u32
332            };
333            generate_record::<CANONICAL, true, false>(
334                index,
335                remap,
336                table,
337                &hash,
338                &mut equal,
339                &mut next,
340                (work, &mut visits),
341            )?;
342            begin += 1;
343        } else {
344            let result = (|| {
345                if INDEXED {
346                    for &index in &indices[begin..begin + count] {
347                        generate_record::<CANONICAL, false, false>(
348                            index,
349                            remap,
350                            table,
351                            &hash,
352                            &mut equal,
353                            &mut next,
354                            (work, &mut visits),
355                        )?;
356                    }
357                } else {
358                    for index in begin..begin + count {
359                        generate_record::<CANONICAL, false, false>(
360                            index as u32,
361                            remap,
362                            table,
363                            &hash,
364                            &mut equal,
365                            &mut next,
366                            (work, &mut visits),
367                        )?;
368                    }
369                }
370                Ok(())
371            })();
372            work.add(visits)?;
373            result?;
374            begin += count;
375        }
376    }
377    Ok(next as usize)
378}
379fn generate<const OUTLINE: bool>(
380    remap: &mut [u32],
381    indices: Option<&[u32]>,
382    table: &mut [u32],
383    hash: impl Fn(u32) -> u32,
384    equal: impl FnMut(u32, u32) -> bool,
385    canonical: bool,
386    work: &mut Work,
387) -> Result<usize, Error> {
388    if canonical {
389        generate_kernel::<true, false, OUTLINE>(remap, indices, table, hash, equal, work)
390    } else {
391        generate_kernel::<false, false, OUTLINE>(remap, indices, table, hash, equal, work)
392    }
393}
394fn generate_streams(
395    remap: &mut [u32],
396    indices: Option<&[u32]>,
397    table: &mut [u32],
398    streams: &[VertexStream<'_>],
399    canonical: bool,
400    work: &mut Work,
401) -> Result<usize, Error> {
402    if let [vertices] = streams {
403        let vertices = *vertices;
404        match vertices.size {
405            4 => return generate_single::<4>(remap, indices, table, vertices, canonical, work),
406            8 => return generate_single::<8>(remap, indices, table, vertices, canonical, work),
407            12 => return generate_single::<12>(remap, indices, table, vertices, canonical, work),
408            16 => return generate_single::<16>(remap, indices, table, vertices, canonical, work),
409            _ => {}
410        }
411        generate::<false>(
412            remap,
413            indices,
414            table,
415            |i| hash_update(0, vertices.key(i)),
416            |a, b| vertices.key(a) == vertices.key(b),
417            canonical,
418            work,
419        )
420    } else {
421        generate::<false>(
422            remap,
423            indices,
424            table,
425            |i| stream_hash(streams, i),
426            |a, b| stream_equal(streams, a, b),
427            canonical,
428            work,
429        )
430    }
431}
432
433fn generate_single<const SIZE: usize>(
434    remap: &mut [u32],
435    indices: Option<&[u32]>,
436    table: &mut [u32],
437    vertices: VertexStream<'_>,
438    canonical: bool,
439    work: &mut Work,
440) -> Result<usize, Error> {
441    generate::<false>(
442        remap,
443        indices,
444        table,
445        |i| hash_update(0, vertices.key_array::<SIZE>(i)),
446        |a, b| vertices.key_array::<SIZE>(a) == vertices.key_array::<SIZE>(b),
447        canonical,
448        work,
449    )
450}
451
452fn shadow_kernel(
453    remap: &mut [u32],
454    indices: &[u32],
455    output: &mut [u32],
456    table: &mut [u32],
457    hash: impl Fn(u32) -> u32,
458    mut equal: impl FnMut(u32, u32) -> bool,
459    work: &mut Work,
460) -> Result<(), Error> {
461    // The shadow workspace prepares both ranges with the sentinel. Keep the
462    // original logical initialization charges and their failure order.
463    work.add(remap.len())?;
464    work.add(table.len())?;
465    let bound = table.len().checked_add(2).ok_or(Error::SizeOverflow)?;
466    let mut next = 0;
467    let mut begin = 0;
468    while begin < indices.len() {
469        let count = work.batch(indices.len() - begin, bound)?;
470        let mut visits = 0;
471        if count == 0 {
472            let index = indices[begin];
473            generate_record::<true, true, false>(
474                index,
475                remap,
476                table,
477                &hash,
478                &mut equal,
479                &mut next,
480                (work, &mut visits),
481            )?;
482            work.add(1)?;
483            output[begin] = remap[index as usize];
484            begin += 1;
485        } else {
486            let result = (|| {
487                for i in begin..begin + count {
488                    let index = indices[i];
489                    generate_record::<true, false, false>(
490                        index,
491                        remap,
492                        table,
493                        &hash,
494                        &mut equal,
495                        &mut next,
496                        (work, &mut visits),
497                    )?;
498                    visits += 1;
499                    output[i] = remap[index as usize];
500                }
501                Ok(())
502            })();
503            work.add(visits)?;
504            result?;
505            begin += count;
506        }
507    }
508    Ok(())
509}
510
511fn shadow_streams(
512    remap: &mut [u32],
513    indices: &[u32],
514    output: &mut [u32],
515    table: &mut [u32],
516    streams: &[VertexStream<'_>],
517    work: &mut Work,
518) -> Result<(), Error> {
519    if let [vertices] = streams {
520        let vertices = *vertices;
521        match vertices.size {
522            4 => return shadow_single::<4>(remap, indices, output, table, vertices, work),
523            8 => return shadow_single::<8>(remap, indices, output, table, vertices, work),
524            12 => return shadow_single::<12>(remap, indices, output, table, vertices, work),
525            16 => return shadow_single::<16>(remap, indices, output, table, vertices, work),
526            _ => {}
527        }
528        shadow_kernel(
529            remap,
530            indices,
531            output,
532            table,
533            |i| hash_update(0, vertices.key(i)),
534            |a, b| vertices.key(a) == vertices.key(b),
535            work,
536        )
537    } else {
538        shadow_kernel(
539            remap,
540            indices,
541            output,
542            table,
543            |i| stream_hash(streams, i),
544            |a, b| stream_equal(streams, a, b),
545            work,
546        )
547    }
548}
549
550fn shadow_single<const SIZE: usize>(
551    remap: &mut [u32],
552    indices: &[u32],
553    output: &mut [u32],
554    table: &mut [u32],
555    vertices: VertexStream<'_>,
556    work: &mut Work,
557) -> Result<(), Error> {
558    shadow_kernel(
559        remap,
560        indices,
561        output,
562        table,
563        |i| hash_update(0, vertices.key_array::<SIZE>(i)),
564        |a, b| vertices.key_array::<SIZE>(a) == vertices.key_array::<SIZE>(b),
565        work,
566    )
567}
568
569/// Generate compact numbering from tightly packed binary keys. `None` visits
570/// every vertex in original order; indexed input need not be a triangle list.
571///
572/// # Errors
573/// `IndexOutOfBounds` for invalid references;
574/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
575#[inline]
576pub fn generate_vertex_remap_generic<I: crate::Index>(
577    indices: Option<&[I]>,
578    vertices: VertexStream<'_>,
579    workspace: &mut Workspace,
580) -> Result<VertexRemap, Error> {
581    I::with_optional(indices, workspace, |indices, workspace| {
582        generate_vertex_remap_u32(indices, vertices, workspace)
583    })
584}
585
586fn generate_vertex_remap_u32(
587    indices: Option<&[u32]>,
588    vertices: VertexStream<'_>,
589    workspace: &mut Workspace,
590) -> Result<VertexRemap, Error> {
591    generate_vertex_remap_multi_generic(indices, &[vertices], workspace)
592}
593/// Generate compact numbering from one through sixteen initialized key streams.
594///
595/// # Errors
596/// `InvalidLayout` for absent or mismatched streams; `IndexOutOfBounds` for invalid references;
597/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
598#[inline]
599pub fn generate_vertex_remap_multi_generic<I: crate::Index>(
600    indices: Option<&[I]>,
601    streams: &[VertexStream<'_>],
602    workspace: &mut Workspace,
603) -> Result<VertexRemap, Error> {
604    I::with_optional(indices, workspace, |indices, workspace| {
605        generate_vertex_remap_multi_u32(indices, streams, workspace)
606    })
607}
608
609fn generate_vertex_remap_multi_u32(
610    indices: Option<&[u32]>,
611    streams: &[VertexStream<'_>],
612    workspace: &mut Workspace,
613) -> Result<VertexRemap, Error> {
614    workspace.begin();
615    let count = streams_valid(streams)?;
616    if let [vertices] = streams {
617        let vertices = *vertices;
618        match vertices.size {
619            4 => return remap_alloc_single::<4>(indices, vertices, workspace),
620            8 => return remap_alloc_single::<8>(indices, vertices, workspace),
621            12 => return remap_alloc_single::<12>(indices, vertices, workspace),
622            16 => return remap_alloc_single::<16>(indices, vertices, workspace),
623            _ => {}
624        }
625        remap_alloc::<false>(
626            indices,
627            count,
628            |i| hash_update(0, vertices.key(i)),
629            |a, b| vertices.key(a) == vertices.key(b),
630            false,
631            workspace,
632        )
633    } else {
634        remap_alloc::<false>(
635            indices,
636            count,
637            |i| stream_hash(streams, i),
638            |a, b| stream_equal(streams, a, b),
639            false,
640            workspace,
641        )
642    }
643}
644fn remap_alloc_single<const SIZE: usize>(
645    indices: Option<&[u32]>,
646    vertices: VertexStream<'_>,
647    workspace: &mut Workspace,
648) -> Result<VertexRemap, Error> {
649    remap_alloc::<false>(
650        indices,
651        vertices.count,
652        |i| hash_update(0, vertices.key_array::<SIZE>(i)),
653        |a, b| vertices.key_array::<SIZE>(a) == vertices.key_array::<SIZE>(b),
654        false,
655        workspace,
656    )
657}
658fn remap_alloc<const OUTLINE: bool>(
659    indices: Option<&[u32]>,
660    count: usize,
661    hash: impl Fn(u32) -> u32,
662    equal: impl FnMut(u32, u32) -> bool,
663    canonical: bool,
664    workspace: &mut Workspace,
665) -> Result<VertexRemap, Error> {
666    let mut work = workspace.begin();
667    let result = (|| {
668        validate_indices(indices, count, &mut work)?;
669        let table_len = buckets(count)?;
670        let output_bytes = checked_bytes(count, 4)?;
671        workspace.prepare([table_len, 0, 0, 0], output_bytes)?;
672        let mut remap = crate::workspace::output_sentinel(count)?;
673        if remap.capacity() != count {
674            workspace.prepare([table_len, 0, 0, 0], checked_bytes(remap.capacity(), 4)?)?;
675        }
676        let vertex_count = if canonical {
677            generate_kernel::<true, true, OUTLINE>(
678                &mut remap,
679                indices,
680                &mut workspace.integers[..table_len],
681                hash,
682                equal,
683                &mut work,
684            )?
685        } else {
686            generate_kernel::<false, true, OUTLINE>(
687                &mut remap,
688                indices,
689                &mut workspace.integers[..table_len],
690                hash,
691                equal,
692                &mut work,
693            )?
694        };
695        Ok(VertexRemap {
696            remap,
697            vertex_count,
698        })
699    })();
700    workspace.finish(&work);
701    result
702}
703// Callback work must remain behind numerical position equality. Keeping the
704// call out of the probe body also avoids spilling its state on unequal keys.
705#[inline(never)]
706fn custom_equal(equal: &mut impl FnMut(u32, u32) -> bool, a: u32, b: u32) -> bool {
707    equal(a, b)
708}
709/// Generate compact numbering with a callback invoked only for numerically
710/// equal positions, in exactly upstream's hash-probe order. Signed zeros weld.
711///
712/// # Errors
713/// `IndexOutOfBounds` for invalid references; `NumericalFailure` if bounded hash probing finds no slot.
714/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
715///
716/// # Panics
717/// Panics if the supplied callback panics.
718pub fn generate_vertex_remap_custom_generic<I: crate::Index>(
719    indices: Option<&[I]>,
720    positions: Positions<'_>,
721    equal: impl FnMut(u32, u32) -> bool,
722    workspace: &mut Workspace,
723) -> Result<VertexRemap, Error> {
724    I::with_optional(indices, workspace, |indices, workspace| {
725        generate_vertex_remap_custom_u32(indices, positions, equal, workspace)
726    })
727}
728
729fn generate_vertex_remap_custom_u32(
730    indices: Option<&[u32]>,
731    positions: Positions<'_>,
732    mut equal: impl FnMut(u32, u32) -> bool,
733    workspace: &mut Workspace,
734) -> Result<VertexRemap, Error> {
735    if let Some(p) = positions.packed_values() {
736        return remap_alloc::<true>(
737            indices,
738            p.len(),
739            |i| position_value_hash(p[i as usize]),
740            |a, b| p[a as usize] == p[b as usize] && custom_equal(&mut equal, a, b),
741            false,
742            workspace,
743        );
744    }
745    remap_alloc::<true>(
746        indices,
747        positions.len(),
748        |i| position_hash(positions, i),
749        |a, b| {
750            positions.get(a as usize) == positions.get(b as usize) && custom_equal(&mut equal, a, b)
751        },
752        false,
753        workspace,
754    )
755}
756/// Map each position to its first numerically equal vertex. Signed zeros weld;
757/// NaNs do not compare equal. The output refers to original indices.
758///
759/// # Errors
760/// `NumericalFailure` if bounded hash probing finds no slot.
761/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
762pub fn generate_position_remap_generic<I: crate::Index>(
763    positions: Positions<'_>,
764    workspace: &mut Workspace,
765) -> Result<Vec<I>, Error> {
766    generate_position_remap_u32(positions, workspace)
767        .and_then(|value| I::from_vec(value, workspace))
768}
769
770fn generate_position_remap_u32(
771    positions: Positions<'_>,
772    workspace: &mut Workspace,
773) -> Result<Vec<u32>, Error> {
774    if let Some(p) = positions.packed_values() {
775        return Ok(remap_alloc::<false>(
776            None,
777            p.len(),
778            |i| position_value_hash(p[i as usize]),
779            |a, b| p[a as usize] == p[b as usize],
780            true,
781            workspace,
782        )?
783        .remap);
784    }
785    Ok(remap_alloc::<false>(
786        None,
787        positions.len(),
788        |i| position_hash(positions, i),
789        |a, b| positions.get(a as usize) == positions.get(b as usize),
790        true,
791        workspace,
792    )?
793    .remap)
794}
795/// Hash table length for [`weld_position`] over `vertices` vertices.
796pub(crate) fn weld_table_len(vertices: usize) -> Result<usize, Error> {
797    buckets(vertices)
798}
799/// Exact-position weld of one referenced vertex for `sanitize`, using the
800/// [`generate_position_remap`] hash and equality: signed zeros weld, NaNs do
801/// not. `remap` and `table` start filled with the unused sentinel, `index` is
802/// in range and `table` has [`weld_table_len`] entries. Returns the first
803/// visited vertex whose position equals this vertex's position. Every
804/// hash-table visit is charged to `work`.
805pub(crate) fn weld_position(
806    index: u32,
807    remap: &mut [u32],
808    table: &mut [u32],
809    positions: Positions<'_>,
810    work: &mut Work,
811) -> Result<u32, Error> {
812    let mut next = 0;
813    let mut visits = 0;
814    if let Some(p) = positions.packed_values() {
815        generate_record::<true, true, false>(
816            index,
817            remap,
818            table,
819            &|i| position_value_hash(p[i as usize]),
820            &mut |a, b| p[a as usize] == p[b as usize],
821            &mut next,
822            (work, &mut visits),
823        )?;
824    } else {
825        generate_record::<true, true, false>(
826            index,
827            remap,
828            table,
829            &|i| position_hash(positions, i),
830            &mut |a, b| positions.get(a as usize) == positions.get(b as usize),
831            &mut next,
832            (work, &mut visits),
833        )?;
834    }
835    Ok(remap[index as usize])
836}
837/// Generate compact numbering into caller storage. The tail is untouched;
838/// work exhaustion may leave the used prefix partially modified.
839///
840/// # Errors
841/// Same errors as [`generate_vertex_remap_multi`]. `BufferTooSmall` if an output buffer is too short.
842#[inline]
843pub fn generate_vertex_remap_multi_into_generic<I: crate::Index>(
844    destination: &mut [u32],
845    indices: Option<&[I]>,
846    streams: &[VertexStream<'_>],
847    workspace: &mut Workspace,
848) -> Result<usize, Error> {
849    I::with_optional(indices, workspace, |indices, workspace| {
850        generate_vertex_remap_multi_into_u32(destination, indices, streams, workspace)
851    })
852}
853
854fn generate_vertex_remap_multi_into_u32(
855    destination: &mut [u32],
856    indices: Option<&[u32]>,
857    streams: &[VertexStream<'_>],
858    workspace: &mut Workspace,
859) -> Result<usize, Error> {
860    let mut work = workspace.begin();
861    let result = (|| {
862        let count = streams_valid(streams)?;
863        validate_indices(indices, count, &mut work)?;
864        if destination.len() < count {
865            return Err(Error::BufferTooSmall);
866        }
867        let table_len = buckets(count)?;
868        workspace.prepare([table_len, 0, 0, 0], 0)?;
869        generate_streams(
870            &mut destination[..count],
871            indices,
872            &mut workspace.integers[..table_len],
873            streams,
874            false,
875            &mut work,
876        )
877    })();
878    workspace.finish(&work);
879    result
880}
881/// Single-stream caller-buffer compact numbering.
882///
883/// # Errors
884/// Same errors as [`generate_vertex_remap`]. `BufferTooSmall` if an output buffer is too short.
885#[inline]
886pub fn generate_vertex_remap_into_generic<I: crate::Index>(
887    destination: &mut [u32],
888    indices: Option<&[I]>,
889    vertices: VertexStream<'_>,
890    workspace: &mut Workspace,
891) -> Result<usize, Error> {
892    I::with_optional(indices, workspace, |indices, workspace| {
893        generate_vertex_remap_into_u32(destination, indices, vertices, workspace)
894    })
895}
896
897fn generate_vertex_remap_into_u32(
898    destination: &mut [u32],
899    indices: Option<&[u32]>,
900    vertices: VertexStream<'_>,
901    workspace: &mut Workspace,
902) -> Result<usize, Error> {
903    generate_vertex_remap_multi_into_generic(destination, indices, &[vertices], workspace)
904}
905
906#[inline(always)]
907fn copy_vertex<const SIZE: usize>(
908    destination: &mut [u8],
909    vertices: VertexStream<'_>,
910    source: u32,
911    target: usize,
912) {
913    let size = if SIZE == 0 { vertices.size } else { SIZE };
914    let input = vertices.offset + source as usize * vertices.stride;
915    let output = target * size;
916    destination[output..output + size].copy_from_slice(&vertices.data[input..input + size]);
917}
918fn remap_vertex_copy<const SIZE: usize>(
919    destination: &mut [u8],
920    vertices: VertexStream<'_>,
921    remap: &[u32],
922    work: &mut Work,
923) -> Result<(), Error> {
924    if work.precharge(remap.len(), 1)? {
925        for (i, &r) in remap.iter().enumerate() {
926            if r != NONE {
927                copy_vertex::<SIZE>(destination, vertices, i as u32, r as usize);
928            }
929        }
930    } else {
931        for (i, &r) in remap.iter().enumerate() {
932            work.add(1)?;
933            if r != NONE {
934                copy_vertex::<SIZE>(destination, vertices, i as u32, r as usize);
935            }
936        }
937    }
938    Ok(())
939}
940// Tightly packed fixed-size records: zip source records with the remap, so
941// only the destination record index needs a bounds check. Same visit order,
942// so repeated targets still keep the last source record.
943fn remap_vertex_packed<const SIZE: usize>(
944    destination: &mut [u8],
945    vertices: VertexStream<'_>,
946    remap: &[u32],
947    work: &mut Work,
948) -> Result<(), Error> {
949    if vertices.stride != SIZE || vertices.offset != 0 || !work.precharge(remap.len(), 1)? {
950        return remap_vertex_copy::<SIZE>(destination, vertices, remap, work);
951    }
952    remap_vertex_records(
953        destination.as_chunks_mut::<SIZE>().0,
954        vertices.data.as_chunks::<SIZE>().0,
955        remap,
956    );
957    Ok(())
958}
959// Keep each packed monomorph independent of stream validation and metering.
960#[inline(never)]
961fn remap_vertex_records<const SIZE: usize>(
962    output: &mut [[u8; SIZE]],
963    input: &[[u8; SIZE]],
964    remap: &[u32],
965) {
966    let (records, tail) = input[..remap.len()].as_chunks::<4>();
967    let (targets, target_tail) = remap.as_chunks::<4>();
968    for (records, targets) in records.iter().zip(targets) {
969        for (record, &r) in records.iter().zip(targets) {
970            if r != NONE {
971                output[r as usize] = *record;
972            }
973        }
974    }
975    for (record, &r) in tail.iter().zip(target_tail) {
976        if r != NONE {
977            output[r as usize] = *record;
978        }
979    }
980}
981fn remap_vertex_dispatch(
982    destination: &mut [u8],
983    vertices: VertexStream<'_>,
984    remap: &[u32],
985    work: &mut Work,
986) -> Result<(), Error> {
987    match vertices.size {
988        4 => remap_vertex_packed::<4>(destination, vertices, remap, work),
989        8 => remap_vertex_packed::<8>(destination, vertices, remap, work),
990        12 => remap_vertex_packed::<12>(destination, vertices, remap, work),
991        16 => remap_vertex_packed::<16>(destination, vertices, remap, work),
992        _ => remap_vertex_copy::<0>(destination, vertices, remap, work),
993    }
994}
995/// Output record count of a vertex remap (largest target + 1), rejecting
996/// targets at or beyond `count`. `NONE + 1` wraps to zero, so one branch-free
997/// maximum covers both the unmapped test and the bound; a failing or
998/// limited reduction falls back to the per-record scan and its charges.
999fn remap_required(remap: &[u32], count: usize, work: &mut Work) -> Result<usize, Error> {
1000    if work.covers(remap.len())? {
1001        let top = remap.iter().fold(0u32, |m, &i| m.max(i.wrapping_add(1)));
1002        if top as usize <= count {
1003            work.add(remap.len())?;
1004            return Ok(top as usize);
1005        }
1006    }
1007    let mut required = 0;
1008    work.scan(remap.iter().copied(), |i| {
1009        if i != NONE {
1010            if i as usize >= count {
1011                return Err(Error::IndexOutOfBounds);
1012            }
1013            required = required.max(i as usize + 1);
1014        }
1015        Ok(())
1016    })?;
1017    Ok(required)
1018}
1019
1020/// Remap initialized vertex bytes into caller storage. Repeated destinations
1021/// use the last source record, matching upstream; unmapped records are untouched.
1022/// Validation precedes writes; work exhaustion may leave partial output.
1023///
1024/// # Errors
1025/// Same errors as [`remap_vertex_buffer`]. `BufferTooSmall` if an output buffer is too short.
1026pub fn remap_vertex_buffer_into(
1027    destination: &mut [u8],
1028    vertices: VertexStream<'_>,
1029    remap: &[u32],
1030    workspace: &mut Workspace,
1031) -> Result<(), Error> {
1032    let mut work = workspace.begin();
1033    let result = (|| {
1034        if remap.len() != vertices.count {
1035            return Err(Error::InvalidLayout);
1036        }
1037        let required = remap_required(remap, vertices.count, &mut work)?;
1038        if destination.len() < checked_bytes(required, vertices.size)? {
1039            return Err(Error::BufferTooSmall);
1040        }
1041        workspace.charge_retained()?;
1042        remap_vertex_dispatch(destination, vertices, remap, &mut work)?;
1043        Ok(())
1044    })();
1045    workspace.finish(&work);
1046    result
1047}
1048/// Allocate remapped tightly packed bytes, zero-initializing unmapped records.
1049///
1050/// # Errors
1051/// `InvalidLayout` for a mismatched remap length; `IndexOutOfBounds` for invalid remap values;
1052/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1053pub fn remap_vertex_buffer(
1054    vertices: VertexStream<'_>,
1055    remap: &[u32],
1056    workspace: &mut Workspace,
1057) -> Result<Vec<u8>, Error> {
1058    let mut work = workspace.begin();
1059    let result = (|| {
1060        if remap.len() != vertices.count {
1061            return Err(Error::InvalidLayout);
1062        }
1063        let required = remap_required(remap, vertices.count, &mut work)?;
1064        let bytes = checked_bytes(required, vertices.size)?;
1065        let mut budget = Budget::new(workspace);
1066        let mut out = budget.filled(bytes, 0u8)?;
1067        remap_vertex_dispatch(&mut out, vertices, remap, &mut work)?;
1068        Ok(out)
1069    })();
1070    workspace.finish(&work);
1071    result
1072}
1073/// Remap arbitrary index sequences. `None` remaps all vertices in order;
1074/// references to the unused sentinel are rejected before writes.
1075///
1076/// # Errors
1077/// Same errors as [`remap_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
1078#[inline]
1079pub fn remap_index_buffer_into_generic<I: crate::Index>(
1080    destination: &mut [I],
1081    indices: Option<&[I]>,
1082    remap: &[u32],
1083    workspace: &mut Workspace,
1084) -> Result<(), Error> {
1085    I::with_optional(indices, workspace, |indices, workspace| {
1086        I::with_mut(destination, workspace, |destination, workspace| {
1087            remap_index_buffer_into_u32(destination, indices, remap, workspace)
1088        })
1089    })
1090}
1091
1092fn remap_index_buffer_into_u32(
1093    destination: &mut [u32],
1094    indices: Option<&[u32]>,
1095    remap: &[u32],
1096    workspace: &mut Workspace,
1097) -> Result<(), Error> {
1098    let mut work = workspace.begin();
1099    let result = (|| {
1100        let count = indices.map_or(remap.len(), <[u32]>::len);
1101        if let Some(input) = indices.filter(|_| {
1102            count
1103                .checked_mul(2)
1104                .is_some_and(|n| work.covers(n).unwrap_or(false))
1105        }) {
1106            if remap.len() > u32::MAX as usize {
1107                return Err(Error::SizeOverflow);
1108            }
1109            let mut missing = None;
1110            for (at, &index) in input.iter().enumerate() {
1111                let Some(&value) = remap.get(index as usize) else {
1112                    work.add(at + 1)?;
1113                    return Err(Error::IndexOutOfBounds);
1114                };
1115                if value == NONE && missing.is_none() {
1116                    missing = Some(at);
1117                }
1118            }
1119            work.add(count)?;
1120            if destination.len() < count {
1121                return Err(Error::BufferTooSmall);
1122            }
1123            work.add(missing.map_or(count, |at| at + 1))?;
1124            if missing.is_some() {
1125                return Err(Error::InvalidParameter);
1126            }
1127        } else {
1128            validate_indices(indices, remap.len(), &mut work)?;
1129            if destination.len() < count {
1130                return Err(Error::BufferTooSmall);
1131            }
1132            work.scan(0..count, |i| {
1133                let index = indices.map_or(i as u32, |v| v[i]);
1134                if remap[index as usize] == NONE {
1135                    return Err(Error::InvalidParameter);
1136                }
1137                Ok(())
1138            })?;
1139        }
1140        workspace.charge_retained()?;
1141        if work.precharge(count, 1)? {
1142            if let Some(input) = indices {
1143                for (out, &index) in destination[..count].iter_mut().zip(input) {
1144                    *out = remap[index as usize];
1145                }
1146            } else {
1147                destination[..count].copy_from_slice(remap);
1148            }
1149        } else {
1150            for (i, out) in destination[..count].iter_mut().enumerate() {
1151                work.add(1)?;
1152                *out = remap[indices.map_or(i as u32, |v| v[i]) as usize];
1153            }
1154        }
1155        Ok(())
1156    })();
1157    workspace.finish(&work);
1158    result
1159}
1160#[inline(always)]
1161fn remap_index_copy(
1162    output: &mut [u32],
1163    input: Option<&[u32]>,
1164    remap: &[u32],
1165    work: &mut Work,
1166) -> Result<(), Error> {
1167    let count = output.len();
1168    if work.covers(count)? {
1169        // Copy without a per-index exit (four lookups per step), then test for
1170        // unmapped values in one branch-free pass. The owned output is dropped
1171        // on error, so only the charge through the first unmapped index is
1172        // observable. Unindexed input is the remap prefix itself.
1173        if let Some(input) = input {
1174            let (input4, input_tail) = input.as_chunks::<4>();
1175            let (output4, output_tail) = output.as_chunks_mut::<4>();
1176            for (out, index) in output4.iter_mut().zip(input4) {
1177                *out = index.map(|i| remap[i as usize]);
1178            }
1179            for (out, &index) in output_tail.iter_mut().zip(input_tail) {
1180                *out = remap[index as usize];
1181            }
1182        } else {
1183            output.copy_from_slice(&remap[..count]);
1184        }
1185        let unmapped = output
1186            .iter()
1187            .fold(0u32, |any, &v| any | u32::from(v == NONE));
1188        if unmapped != 0 {
1189            let at = output.iter().position(|&v| v == NONE).unwrap_or(count);
1190            work.add(at + 1)?;
1191            return Err(Error::InvalidParameter);
1192        }
1193        work.add(count)?;
1194    } else {
1195        for (i, out) in output.iter_mut().enumerate() {
1196            work.add(1)?;
1197            *out = remap[input.map_or(i, |v| v[i] as usize)];
1198            if *out == NONE {
1199                return Err(Error::InvalidParameter);
1200            }
1201        }
1202    }
1203    Ok(())
1204}
1205
1206// Combine checked gathers with validation, without per-index metering or
1207// sentinel selection. A failed gather is replayed by the original validator.
1208#[inline(always)]
1209fn remap_index_gather(output: &mut [u32], input: &[u32], remap: &[u32]) -> bool {
1210    let (input4, input_tail) = input.as_chunks::<4>();
1211    let (output4, output_tail) = output.as_chunks_mut::<4>();
1212    for (out, index) in output4.iter_mut().zip(input4) {
1213        let Some(&a) = remap.get(index[0] as usize) else {
1214            return false;
1215        };
1216        let Some(&b) = remap.get(index[1] as usize) else {
1217            return false;
1218        };
1219        let Some(&c) = remap.get(index[2] as usize) else {
1220            return false;
1221        };
1222        let Some(&d) = remap.get(index[3] as usize) else {
1223            return false;
1224        };
1225        *out = [a, b, c, d];
1226    }
1227    for (out, &index) in output_tail.iter_mut().zip(input_tail) {
1228        let Some(&value) = remap.get(index as usize) else {
1229            return false;
1230        };
1231        *out = value;
1232    }
1233    true
1234}
1235
1236/// Allocate remapped indices.
1237///
1238/// # Errors
1239/// `InvalidParameter` if unindexed remapping exceeds the remap length; `IndexOutOfBounds` for invalid references;
1240/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1241#[inline]
1242pub fn remap_index_buffer_generic<I: crate::Index>(
1243    indices: Option<&[I]>,
1244    remap: &[u32],
1245    workspace: &mut Workspace,
1246) -> Result<Vec<I>, Error> {
1247    I::with_optional(indices, workspace, |indices, workspace| {
1248        remap_index_buffer_u32(indices, remap, workspace)
1249            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1250    })
1251}
1252
1253fn remap_index_buffer_u32(
1254    indices: Option<&[u32]>,
1255    remap: &[u32],
1256    workspace: &mut Workspace,
1257) -> Result<Vec<u32>, Error> {
1258    let mut work = workspace.begin();
1259    let result = (|| {
1260        // Speculate only when the complete logical work and allocation fit.
1261        // No workspace state is changed until all indices have been validated.
1262        if let Some(input) = indices.filter(|input| {
1263            remap.len() <= u32::MAX as usize
1264                && input
1265                    .len()
1266                    .checked_mul(2)
1267                    .is_some_and(|n| work.covers(n).unwrap_or(false))
1268                && checked_bytes(input.len(), 4).ok().is_some_and(|bytes| {
1269                    workspace
1270                        .retained_bytes()
1271                        .ok()
1272                        .and_then(|n| n.checked_add(bytes))
1273                        .is_some_and(|n| n <= workspace.limits().max_bytes)
1274                })
1275        }) {
1276            let mut out = Vec::new();
1277            // Reservation failure must still give index validation precedence.
1278            if out.try_reserve_exact(input.len()).is_ok() {
1279                out.resize_with(input.len(), || 0u32);
1280                if !remap_index_gather(&mut out, input, remap) {
1281                    // Replay only the failed validation; owned writes are unobservable.
1282                    validate_indices(Some(input), remap.len(), &mut work)?;
1283                }
1284                work.add(input.len())?;
1285                workspace.charge_owned(checked_bytes(input.len(), 4)?)?;
1286                if out.capacity() != input.len() {
1287                    workspace.charge_owned(checked_bytes(out.capacity(), 4)?)?;
1288                }
1289                let missing = out.iter().fold(0u32, |any, &v| any | u32::from(v == NONE));
1290                if missing != 0 {
1291                    let first = out
1292                        .iter()
1293                        .position(|&v| v == NONE)
1294                        .map_or(out.len(), |i| i + 1);
1295                    work.add(first)?;
1296                    return Err(Error::InvalidParameter);
1297                }
1298                work.add(input.len())?;
1299                return Ok(out);
1300            }
1301        }
1302        validate_indices(indices, remap.len(), &mut work)?;
1303        let count = indices.map_or(remap.len(), <[u32]>::len);
1304        let mut budget = Budget::new(workspace);
1305        let mut out = budget.filled(count, 0u32)?;
1306        remap_index_copy(&mut out, indices, remap, &mut work)?;
1307        Ok(out)
1308    })();
1309    workspace.finish(&work);
1310    result
1311}
1312
1313#[inline(always)]
1314fn filter_triangle<const LIMITED: bool>(
1315    source: &[u32; 3],
1316    remap: &[u32],
1317    table: &mut [[u32; 3]],
1318    out: &mut [u32],
1319    write_generic: &mut usize,
1320    budget: (&mut Work, &mut usize),
1321) -> Result<(), Error> {
1322    let (work, visits) = budget;
1323    if LIMITED {
1324        work.add(1)?;
1325    } else {
1326        *visits += 1;
1327    }
1328    let mut key = [
1329        remap[source[0] as usize],
1330        remap[source[1] as usize],
1331        remap[source[2] as usize],
1332    ];
1333    if key[0] == key[1] || key[0] == key[2] || key[1] == key[2] {
1334        return Ok(());
1335    }
1336    if key[1] < key[0] && key[1] < key[2] {
1337        key = [key[1], key[2], key[0]];
1338    } else if key[2] < key[0] && key[2] < key[1] {
1339        key = [key[2], key[0], key[1]];
1340    }
1341    let mask = table.len() - 1;
1342    let mut bucket = spatial_hash(key) as usize & mask;
1343    for probe in 0..table.len() {
1344        if LIMITED {
1345            work.add(1)?;
1346        } else {
1347            *visits += 1;
1348        }
1349        if table[bucket] == key {
1350            return Ok(());
1351        }
1352        if table[bucket] == [NONE; 3] {
1353            table[bucket] = key;
1354            out[*write_generic..*write_generic + 3].copy_from_slice(source);
1355            *write_generic += 3;
1356            return Ok(());
1357        }
1358        bucket = (bucket + probe + 1) & mask;
1359    }
1360    Err(Error::NumericalFailure)
1361}
1362
1363fn filter_triangles(
1364    out: &mut [u32],
1365    indices: &[u32],
1366    remap: &[u32],
1367    table: &mut [[u32; 3]],
1368    work: &mut Work,
1369) -> Result<usize, Error> {
1370    work.add(table.len())?;
1371    table.fill([NONE; 3]);
1372    let source = indices.as_chunks::<3>().0;
1373    let bound = table.len().checked_add(1).ok_or(Error::SizeOverflow)?;
1374    let mut write_generic = 0;
1375    let mut begin = 0;
1376    while begin < source.len() {
1377        let count = work.batch(source.len() - begin, bound)?;
1378        let mut visits = 0;
1379        if count == 0 {
1380            filter_triangle::<true>(
1381                &source[begin],
1382                remap,
1383                table,
1384                out,
1385                &mut write_generic,
1386                (work, &mut visits),
1387            )?;
1388            begin += 1;
1389        } else {
1390            let result = (|| {
1391                for triangle in &source[begin..begin + count] {
1392                    filter_triangle::<false>(
1393                        triangle,
1394                        remap,
1395                        table,
1396                        out,
1397                        &mut write_generic,
1398                        (work, &mut visits),
1399                    )?;
1400                }
1401                Ok(())
1402            })();
1403            work.add(visits)?;
1404            result?;
1405            begin += count;
1406        }
1407    }
1408    Ok(write_generic)
1409}
1410
1411fn shadow_filter<const FILTER: bool>(
1412    indices: &[u32],
1413    streams: &[VertexStream<'_>],
1414    destination: Option<&mut [u32]>,
1415    workspace: &mut Workspace,
1416) -> Result<(Vec<u32>, usize), Error> {
1417    let mut work = workspace.begin();
1418    let result = (|| {
1419        let count = streams_valid(streams)?;
1420        topology(indices, count, &mut work)?;
1421        if destination
1422            .as_ref()
1423            .is_some_and(|out| out.len() < indices.len())
1424        {
1425            return Err(Error::BufferTooSmall);
1426        }
1427        let table_len = buckets(count)?;
1428        let triangle_len = if FILTER {
1429            buckets(indices.len() / 3)?
1430        } else {
1431            0
1432        };
1433        let integer_len = triangle_len
1434            .checked_mul(3)
1435            .and_then(|v| v.checked_add(table_len))
1436            .and_then(|v| v.checked_add(count))
1437            .ok_or(Error::SizeOverflow)?;
1438        let output_bytes = if destination.is_none() {
1439            checked_bytes(indices.len(), 4)?
1440        } else {
1441            0
1442        };
1443        if FILTER {
1444            workspace.prepare([integer_len, 0, 0, 0], output_bytes)?;
1445        } else {
1446            workspace.prepare_remap(integer_len, output_bytes)?;
1447        }
1448        let mut owned = if destination.is_none() {
1449            crate::workspace::output(indices.len())?
1450        } else {
1451            Vec::new()
1452        };
1453        if destination.is_none() && owned.capacity() != indices.len() {
1454            let actual_output = checked_bytes(owned.capacity(), 4)?;
1455            if FILTER {
1456                workspace.prepare([integer_len, 0, 0, 0], actual_output)?;
1457            } else {
1458                workspace.prepare_remap(integer_len, actual_output)?;
1459            }
1460        }
1461        let out = destination.unwrap_or(&mut owned);
1462        let out = &mut out[..indices.len()];
1463        let (remap, rest) = workspace.integers[..integer_len].split_at_mut(count);
1464        let (table, triangles) = rest.split_at_mut(table_len);
1465        let triangles = triangles.as_chunks_mut::<3>().0;
1466        if !FILTER {
1467            shadow_streams(remap, indices, out, table, streams, &mut work)?;
1468            return Ok((owned, indices.len()));
1469        }
1470        generate_streams(remap, Some(indices), table, streams, false, &mut work)?;
1471        let write_generic = filter_triangles(out, indices, remap, triangles, &mut work)?;
1472        owned.truncate(write_generic);
1473        Ok((owned, write_generic))
1474    })();
1475    workspace.finish(&work);
1476    result
1477}
1478/// Replace triangle references with first referenced binary-equal vertices.
1479///
1480/// # Errors
1481/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1482/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1483#[inline]
1484pub fn generate_shadow_index_buffer_generic<I: crate::Index>(
1485    indices: &[I],
1486    vertices: VertexStream<'_>,
1487    workspace: &mut Workspace,
1488) -> Result<Vec<I>, Error> {
1489    I::with_input(indices, workspace, |indices, workspace| {
1490        generate_shadow_index_buffer_u32(indices, vertices, workspace)
1491            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1492    })
1493}
1494
1495fn generate_shadow_index_buffer_u32(
1496    indices: &[u32],
1497    vertices: VertexStream<'_>,
1498    workspace: &mut Workspace,
1499) -> Result<Vec<u32>, Error> {
1500    generate_shadow_index_buffer_multi_generic(indices, &[vertices], workspace)
1501}
1502/// Multi-stream shadow indices, preserving original vertex numbering.
1503///
1504/// # Errors
1505/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1506/// `InvalidLayout` for absent or mismatched streams;
1507/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1508#[inline]
1509pub fn generate_shadow_index_buffer_multi_generic<I: crate::Index>(
1510    indices: &[I],
1511    streams: &[VertexStream<'_>],
1512    workspace: &mut Workspace,
1513) -> Result<Vec<I>, Error> {
1514    I::with_input(indices, workspace, |indices, workspace| {
1515        generate_shadow_index_buffer_multi_u32(indices, streams, workspace)
1516            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1517    })
1518}
1519
1520fn generate_shadow_index_buffer_multi_u32(
1521    indices: &[u32],
1522    streams: &[VertexStream<'_>],
1523    workspace: &mut Workspace,
1524) -> Result<Vec<u32>, Error> {
1525    Ok(shadow_filter::<false>(indices, streams, None, workspace)?.0)
1526}
1527/// Remove binary-degenerate and duplicate triangles; preserve surviving source
1528/// corners and winding. Rotated duplicates are removed; reversed winding stays.
1529///
1530/// # Errors
1531/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1532/// `NumericalFailure` if bounded hash probing finds no slot.
1533/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1534#[inline]
1535pub fn filter_index_buffer_generic<I: crate::Index>(
1536    indices: &[I],
1537    vertices: VertexStream<'_>,
1538    workspace: &mut Workspace,
1539) -> Result<Vec<I>, Error> {
1540    I::with_input(indices, workspace, |indices, workspace| {
1541        filter_index_buffer_u32(indices, vertices, workspace)
1542            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1543    })
1544}
1545
1546fn filter_index_buffer_u32(
1547    indices: &[u32],
1548    vertices: VertexStream<'_>,
1549    workspace: &mut Workspace,
1550) -> Result<Vec<u32>, Error> {
1551    filter_index_buffer_multi_generic(indices, &[vertices], workspace)
1552}
1553/// Multi-stream triangle filtering.
1554///
1555/// # Errors
1556/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1557/// `InvalidLayout` for absent or mismatched streams; `NumericalFailure` if bounded hash probing finds no slot.
1558/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1559#[inline]
1560pub fn filter_index_buffer_multi_generic<I: crate::Index>(
1561    indices: &[I],
1562    streams: &[VertexStream<'_>],
1563    workspace: &mut Workspace,
1564) -> Result<Vec<I>, Error> {
1565    I::with_input(indices, workspace, |indices, workspace| {
1566        filter_index_buffer_multi_u32(indices, streams, workspace)
1567            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1568    })
1569}
1570
1571fn filter_index_buffer_multi_u32(
1572    indices: &[u32],
1573    streams: &[VertexStream<'_>],
1574    workspace: &mut Workspace,
1575) -> Result<Vec<u32>, Error> {
1576    Ok(shadow_filter::<true>(indices, streams, None, workspace)?.0)
1577}
1578
1579#[inline(always)]
1580fn edge_hash(edge: u64, remap: &[u32]) -> u32 {
1581    edge_value_hash(remap[(edge >> 32) as usize], remap[edge as u32 as usize])
1582}
1583#[inline(always)]
1584fn edge_value_hash(mut h1: u32, mut h2: u32) -> u32 {
1585    const M: u32 = 0x5bd1e995;
1586    h1 ^= h2 >> 18;
1587    h1 = h1.wrapping_mul(M);
1588    h2 ^= h1 >> 22;
1589    h2 = h2.wrapping_mul(M);
1590    h1 ^= h2 >> 17;
1591    h1 = h1.wrapping_mul(M);
1592    h2 ^= h1 >> 19;
1593    h2.wrapping_mul(M)
1594}
1595fn patches<const TESS: bool>(
1596    indices: &[u32],
1597    positions: Positions<'_>,
1598    destination: Option<&mut [u32]>,
1599    workspace: &mut Workspace,
1600) -> Result<Vec<u32>, Error> {
1601    if let Some(p) = positions.packed_values() {
1602        patches_kernel::<TESS>(indices, p.len(), |i| p[i as usize], destination, workspace)
1603    } else {
1604        patches_kernel::<TESS>(
1605            indices,
1606            positions.len(),
1607            |i| positions.get(i as usize).expect("checked position"),
1608            destination,
1609            workspace,
1610        )
1611    }
1612}
1613fn patches_kernel<const TESS: bool>(
1614    indices: &[u32],
1615    vertex_count: usize,
1616    position: impl Fn(u32) -> [f32; 3],
1617    destination: Option<&mut [u32]>,
1618    workspace: &mut Workspace,
1619) -> Result<Vec<u32>, Error> {
1620    let mut work = workspace.begin();
1621    let result = (|| {
1622        topology(indices, vertex_count, &mut work)?;
1623        let mut budget = Budget::new(workspace);
1624        let mut remap = budget.filled(vertex_count, NONE)?;
1625        let mut table = budget.filled(buckets(vertex_count)?, NONE)?;
1626        // Adjacency uses binary position equality (including signed zeros),
1627        // unlike the public numerical generatePositionRemap operation.
1628        generate::<false>(
1629            &mut remap,
1630            None,
1631            &mut table,
1632            |i| {
1633                let mut h = 0;
1634                for v in position(i) {
1635                    h = hash_update(h, &v.to_bits().to_le_bytes());
1636                }
1637                h
1638            },
1639            |a, b| position(a).map(f32::to_bits) == position(b).map(f32::to_bits),
1640            true,
1641            &mut work,
1642        )?;
1643        budget.release(table);
1644        let mut edges = budget.filled(buckets(indices.len())?, u64::MAX)?;
1645        let mut opposite = budget.filled(if TESS { 0 } else { edges.len() }, NONE)?;
1646        let factor = if TESS { 4 } else { 2 };
1647        let size = indices
1648            .len()
1649            .checked_mul(factor)
1650            .ok_or(Error::SizeOverflow)?;
1651        let mut owned = if destination.is_none() {
1652            budget.filled(size, 0u32)?
1653        } else {
1654            Vec::new()
1655        };
1656        let out = destination.unwrap_or(&mut owned);
1657        if out.len() < size {
1658            return Err(Error::BufferTooSmall);
1659        }
1660        let out = &mut out[..size];
1661        if !TESS {
1662            adjacency_pass::<true>(indices, out, &mut edges, &mut opposite, &remap, &mut work)?;
1663            adjacency_pass::<false>(indices, out, &mut edges, &mut opposite, &remap, &mut work)?;
1664            return Ok(owned);
1665        }
1666        for tri in indices.as_chunks::<3>().0 {
1667            for e in 0..3 {
1668                work.add(1)?;
1669                let edge = (u64::from(tri[e]) << 32) | u64::from(tri[(e + 1) % 3]);
1670                let slot = edge_lookup(&edges, &remap, edge, &mut work)?;
1671                if edges[slot] == u64::MAX {
1672                    edges[slot] = edge;
1673                    if !TESS {
1674                        opposite[slot] = tri[(e + 2) % 3];
1675                    }
1676                }
1677            }
1678        }
1679        for (tri, patch) in indices
1680            .as_chunks::<3>()
1681            .0
1682            .iter()
1683            .zip(out.chunks_exact_mut(factor * 3))
1684        {
1685            for e in 0..3 {
1686                work.add(1)?;
1687                let a = tri[e];
1688                let b = tri[(e + 1) % 3];
1689                let edge = (u64::from(b) << 32) | u64::from(a);
1690                let slot = edge_lookup(&edges, &remap, edge, &mut work)?;
1691                if TESS {
1692                    let opp = if edges[slot] == u64::MAX {
1693                        edge
1694                    } else {
1695                        edges[slot]
1696                    };
1697                    patch[e] = a;
1698                    patch[3 + e * 2] = opp as u32;
1699                    patch[4 + e * 2] = (opp >> 32) as u32;
1700                    patch[9 + e] = remap[a as usize];
1701                } else {
1702                    patch[e * 2] = a;
1703                    patch[e * 2 + 1] = if edges[slot] == u64::MAX {
1704                        a
1705                    } else {
1706                        opposite[slot]
1707                    };
1708                }
1709            }
1710        }
1711        Ok(owned)
1712    })();
1713    workspace.finish(&work);
1714    result
1715}
1716
1717// A triangle visits three edges; each lookup probes at most the entire table.
1718// Charge completed batches once when that conservative bound fits. Near the
1719// limit, replay the original per-edge/per-probe sequence before any writes.
1720fn adjacency_pass<const BUILD: bool>(
1721    indices: &[u32],
1722    out: &mut [u32],
1723    edges: &mut [u64],
1724    opposite: &mut [u32],
1725    remap: &[u32],
1726    work: &mut Work,
1727) -> Result<(), Error> {
1728    let triangles = indices.as_chunks::<3>().0;
1729    let bound = edges.len().checked_add(1).and_then(|n| n.checked_mul(3));
1730    let mut begin = 0;
1731    while begin < triangles.len() {
1732        let count = match bound {
1733            Some(bound) => work.batch(triangles.len() - begin, bound)?,
1734            None => 0,
1735        };
1736        let mut visits = 0;
1737        for (offset, tri) in triangles[begin..begin + count.max(1)].iter().enumerate() {
1738            for e in 0..3 {
1739                let a = tri[e];
1740                let b = tri[(e + 1) % 3];
1741                let edge = if BUILD {
1742                    (u64::from(remap[a as usize]) << 32) | u64::from(remap[b as usize])
1743                } else {
1744                    (u64::from(remap[b as usize]) << 32) | u64::from(remap[a as usize])
1745                };
1746                let slot = if count == 0 {
1747                    work.add(1)?;
1748                    adjacency_lookup(edges, edge, work)?
1749                } else {
1750                    visits += 1;
1751                    let (slot, probes) = edge_probe(edges, edge);
1752                    visits += probes;
1753                    let Some(slot) = slot else {
1754                        work.add(visits)?;
1755                        return Err(Error::NumericalFailure);
1756                    };
1757                    slot
1758                };
1759                if BUILD {
1760                    if edges[slot] == u64::MAX {
1761                        edges[slot] = edge;
1762                        opposite[slot] = tri[(e + 2) % 3];
1763                    }
1764                } else {
1765                    let patch = &mut out[(begin + offset) * 6..][..6];
1766                    patch[e * 2] = a;
1767                    patch[e * 2 + 1] = if edges[slot] == u64::MAX {
1768                        a
1769                    } else {
1770                        opposite[slot]
1771                    };
1772                }
1773            }
1774        }
1775        work.add(visits)?;
1776        begin += count.max(1);
1777    }
1778    Ok(())
1779}
1780
1781#[inline(always)]
1782fn edge_probe(table: &[u64], edge: u64) -> (Option<usize>, usize) {
1783    let mask = table.len() - 1;
1784    let mut bucket = edge_value_hash((edge >> 32) as u32, edge as u32) as usize & mask;
1785    for probe in 0..table.len() {
1786        let old = table[bucket];
1787        if old == u64::MAX || old == edge {
1788            return (Some(bucket), probe + 1);
1789        }
1790        bucket = (bucket + probe + 1) & mask;
1791    }
1792    (None, table.len())
1793}
1794
1795#[inline(always)]
1796fn adjacency_lookup(table: &[u64], edge: u64, work: &mut Work) -> Result<usize, Error> {
1797    if work.covers(table.len())? {
1798        let (slot, probes) = edge_probe(table, edge);
1799        work.add(probes)?;
1800        slot.ok_or(Error::NumericalFailure)
1801    } else {
1802        let mask = table.len() - 1;
1803        let mut bucket = edge_value_hash((edge >> 32) as u32, edge as u32) as usize & mask;
1804        for probe in 0..table.len() {
1805            work.add(1)?;
1806            if table[bucket] == u64::MAX || table[bucket] == edge {
1807                return Ok(bucket);
1808            }
1809            bucket = (bucket + probe + 1) & mask;
1810        }
1811        Err(Error::NumericalFailure)
1812    }
1813}
1814#[inline(always)]
1815fn edge_lookup(table: &[u64], remap: &[u32], edge: u64, work: &mut Work) -> Result<usize, Error> {
1816    if work.covers(table.len())? {
1817        edge_lookup_kernel::<false>(table, remap, edge, work)
1818    } else {
1819        edge_lookup_kernel::<true>(table, remap, edge, work)
1820    }
1821}
1822#[inline(always)]
1823fn edge_lookup_kernel<const LIMITED: bool>(
1824    table: &[u64],
1825    remap: &[u32],
1826    edge: u64,
1827    work: &mut Work,
1828) -> Result<usize, Error> {
1829    let mask = table.len() - 1;
1830    let mut bucket = edge_hash(edge, remap) as usize & mask;
1831    let a = remap[(edge >> 32) as usize];
1832    let b = remap[edge as u32 as usize];
1833    for probe in 0..table.len() {
1834        if LIMITED {
1835            work.add(1)?;
1836        }
1837        let old = table[bucket];
1838        if old == u64::MAX || (remap[(old >> 32) as usize] == a && remap[old as u32 as usize] == b)
1839        {
1840            if !LIMITED {
1841                work.add(probe + 1)?;
1842            }
1843            return Ok(bucket);
1844        }
1845        bucket = (bucket + probe + 1) & mask;
1846    }
1847    if !LIMITED {
1848        work.add(table.len())?;
1849    }
1850    Err(Error::NumericalFailure)
1851}
1852/// Generate six adjacency indices per triangle using binary position welding.
1853///
1854/// # Errors
1855/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1856/// `NumericalFailure` if bounded hash probing finds no slot.
1857/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1858#[inline]
1859pub fn generate_adjacency_index_buffer_generic<I: crate::Index>(
1860    indices: &[I],
1861    positions: Positions<'_>,
1862    workspace: &mut Workspace,
1863) -> Result<Vec<I>, Error> {
1864    I::with_input(indices, workspace, |indices, workspace| {
1865        generate_adjacency_index_buffer_u32(indices, positions, workspace)
1866            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1867    })
1868}
1869
1870fn generate_adjacency_index_buffer_u32(
1871    indices: &[u32],
1872    positions: Positions<'_>,
1873    workspace: &mut Workspace,
1874) -> Result<Vec<u32>, Error> {
1875    patches::<false>(indices, positions, None, workspace)
1876}
1877/// Generate twelve tessellation indices per triangle using binary position welding.
1878///
1879/// # Errors
1880/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1881/// `NumericalFailure` if bounded hash probing finds no slot.
1882/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1883#[inline]
1884pub fn generate_tessellation_index_buffer_generic<I: crate::Index>(
1885    indices: &[I],
1886    positions: Positions<'_>,
1887    workspace: &mut Workspace,
1888) -> Result<Vec<I>, Error> {
1889    I::with_input(indices, workspace, |indices, workspace| {
1890        generate_tessellation_index_buffer_u32(indices, positions, workspace)
1891            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1892    })
1893}
1894
1895fn generate_tessellation_index_buffer_u32(
1896    indices: &[u32],
1897    positions: Positions<'_>,
1898    workspace: &mut Workspace,
1899) -> Result<Vec<u32>, Error> {
1900    patches::<true>(indices, positions, None, workspace)
1901}
1902/// Generate a unique first-corner vertex for each triangle, with the same
1903/// wrapping eight-bit valence heuristic and cyclic rotations as upstream.
1904///
1905/// # Errors
1906/// `InvalidTopology` for incomplete triangles or `IndexOutOfBounds` for invalid vertex references.
1907/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
1908#[inline]
1909pub fn generate_provoking_index_buffer_generic<I: crate::Index>(
1910    indices: &[I],
1911    vertex_count: usize,
1912    workspace: &mut Workspace,
1913) -> Result<ProvokingMeshGeneric<I>, Error> {
1914    I::with_input(indices, workspace, |indices, workspace| {
1915        generate_provoking_index_buffer_u32(indices, vertex_count, workspace)
1916            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
1917    })
1918}
1919
1920fn generate_provoking_index_buffer_u32(
1921    indices: &[u32],
1922    vertex_count: usize,
1923    workspace: &mut Workspace,
1924) -> Result<ProvokingMeshGeneric, Error> {
1925    Ok(provoking(indices, vertex_count, None, workspace)?.0)
1926}
1927#[inline(always)]
1928fn provoking_triangle(
1929    tri: [u32; 3],
1930    destination: &mut [u32; 3],
1931    remap: &mut [u32],
1932    valence: &mut [u8],
1933    reorder: &mut [u32],
1934    write_generic: &mut usize,
1935) {
1936    let mut tri = tri;
1937    let rank = |i: u32| {
1938        if remap[i as usize] == NONE {
1939            u32::from(valence[i as usize])
1940        } else {
1941            NONE
1942        }
1943    };
1944    let v = [rank(tri[0]), rank(tri[1]), rank(tri[2])];
1945    if v[1] != NONE && v[1] <= v[0] && v[1] <= v[2] {
1946        tri = [tri[1], tri[2], tri[0]];
1947    } else if v[2] != NONE && v[2] <= v[0] && v[2] <= v[1] {
1948        tri = [tri[2], tri[0], tri[1]];
1949    }
1950    if remap[tri[0] as usize] == NONE {
1951        remap[tri[0] as usize] = *write_generic as u32;
1952    }
1953    reorder[*write_generic] = tri[0];
1954    *destination = [*write_generic as u32, tri[1], tri[2]];
1955    *write_generic += 1;
1956    for i in tri {
1957        valence[i as usize] = valence[i as usize].wrapping_sub(1);
1958    }
1959}
1960#[inline(always)]
1961fn provoking_corner(
1962    index: &mut u32,
1963    remap: &mut [u32],
1964    reorder: &mut [u32],
1965    write_generic: &mut usize,
1966) {
1967    let r = &mut remap[*index as usize];
1968    if *r == NONE {
1969        *r = *write_generic as u32;
1970        reorder[*write_generic] = *index;
1971        *write_generic += 1;
1972    }
1973    *index = *r;
1974}
1975fn provoking(
1976    indices: &[u32],
1977    vertex_count: usize,
1978    destination: Option<(&mut [u32], &mut [u32])>,
1979    workspace: &mut Workspace,
1980) -> Result<(ProvokingMeshGeneric, usize), Error> {
1981    let mut work = workspace.begin();
1982    let result = (|| {
1983        topology(indices, vertex_count, &mut work)?;
1984        let count = vertex_count
1985            .checked_add(indices.len() / 3)
1986            .ok_or(Error::SizeOverflow)?;
1987        if count > u32::MAX as usize {
1988            return Err(Error::SizeOverflow);
1989        }
1990        workspace.prepare([vertex_count, 0, vertex_count, 0], 0)?;
1991        let mut budget = Budget::new(workspace);
1992        let caller = destination.is_some();
1993        let mut owned = if caller {
1994            Vec::new()
1995        } else {
1996            budget.filled(indices.len(), 0u32)?
1997        };
1998        let mut owned_reorder = if caller {
1999            Vec::new()
2000        } else {
2001            budget.filled(count, 0u32)?
2002        };
2003        let (out, reorder) = destination.unwrap_or((&mut owned, &mut owned_reorder));
2004        if out.len() < indices.len() || reorder.len() < count {
2005            return Err(Error::BufferTooSmall);
2006        }
2007        let out = &mut out[..indices.len()];
2008        let remap = &mut budget.ws.integers[..vertex_count];
2009        let valence = &mut budget.ws.flags[..vertex_count];
2010        remap.fill(NONE);
2011        valence.fill(0);
2012        if work.precharge(indices.len(), 1)? {
2013            for &i in indices {
2014                valence[i as usize] = valence[i as usize].wrapping_add(1);
2015            }
2016        } else {
2017            for &i in indices {
2018                work.add(1)?;
2019                valence[i as usize] = valence[i as usize].wrapping_add(1);
2020            }
2021        }
2022        let mut write_generic = 0;
2023        if work.precharge(indices.len() / 3, 1)? {
2024            for (&tri, dest) in indices
2025                .as_chunks::<3>()
2026                .0
2027                .iter()
2028                .zip(out.as_chunks_mut::<3>().0)
2029            {
2030                provoking_triangle(tri, dest, remap, valence, reorder, &mut write_generic);
2031            }
2032        } else {
2033            for (&tri, dest) in indices
2034                .as_chunks::<3>()
2035                .0
2036                .iter()
2037                .zip(out.as_chunks_mut::<3>().0)
2038            {
2039                work.add(1)?;
2040                provoking_triangle(tri, dest, remap, valence, reorder, &mut write_generic);
2041            }
2042        }
2043        let corners = indices.len() / 3 * 2;
2044        if work.precharge(corners, 1)? {
2045            for tri in out.as_chunks_mut::<3>().0 {
2046                provoking_corner(&mut tri[1], remap, reorder, &mut write_generic);
2047                provoking_corner(&mut tri[2], remap, reorder, &mut write_generic);
2048            }
2049        } else {
2050            for tri in out.as_chunks_mut::<3>().0 {
2051                for i in &mut tri[1..] {
2052                    work.add(1)?;
2053                    provoking_corner(i, remap, reorder, &mut write_generic);
2054                }
2055            }
2056        }
2057        owned_reorder.truncate(write_generic);
2058        Ok((
2059            ProvokingMeshGeneric {
2060                indices: owned,
2061                reorder: owned_reorder,
2062            },
2063            write_generic,
2064        ))
2065    })();
2066    workspace.finish(&work);
2067    result
2068}
2069
2070/// Borrowed fetch-remap input with its index-validation result cached once.
2071///
2072/// Construction scans at most `indices.len()` entries, without allocating.
2073/// Invalid input is retained so operations can preserve error precedence,
2074/// logical work usage and caller-buffer writes, including on short budgets.
2075#[derive(Clone, Copy, Debug)]
2076pub struct FetchIndicesGeneric<'a, I: crate::Index = u32> {
2077    indices: &'a [I],
2078    vertex_count: usize,
2079    visits: usize,
2080    error: Option<Error>,
2081}
2082impl<'a, I: crate::Index> FetchIndicesGeneric<'a, I> {
2083    /// Cache validation of an arbitrary index sequence and its vertex count.
2084    ///
2085    /// Errors are deferred to the fetch-remap operation, where the original
2086    /// validation visits are charged to its [`Workspace`] in constant time
2087    /// when they fit. Short budgets replay the original charged prefix.
2088    pub fn new_generic(indices: &'a [I], vertex_count: usize) -> Self {
2089        let (visits, error) = if vertex_count > u32::MAX as usize {
2090            (0, Some(Error::SizeOverflow))
2091        } else if let Some(at) = indices
2092            .iter()
2093            .position(|&i| i.to_u32() as usize >= vertex_count)
2094        {
2095            (at + 1, Some(Error::IndexOutOfBounds))
2096        } else {
2097            (indices.len(), None)
2098        };
2099        Self {
2100            indices,
2101            vertex_count,
2102            visits,
2103            error,
2104        }
2105    }
2106    fn apply(self, work: &mut Work) -> Result<(), Error> {
2107        if work.covers(self.visits)? {
2108            work.add(self.visits)?;
2109        } else {
2110            work.scan(self.indices[..self.visits].iter(), |_| Ok(()))?;
2111        }
2112        self.error.map_or(Ok(()), Err)
2113    }
2114}
2115
2116/// Generate first-use fetch numbering using cached index validation.
2117///
2118/// Output, errors, error precedence and logical workspace usage match
2119/// [`optimize_vertex_fetch_remap`]. Construct [`FetchIndices`] once to avoid
2120/// rescanning the sequence for validity on repeated calls. Numbering and
2121/// output initialization still take O(indices.len() + vertex_count) time.
2122///
2123/// # Errors
2124/// `IndexOutOfBounds` for invalid references;
2125/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
2126pub fn optimize_vertex_fetch_remap_with_indices_generic<I: crate::Index>(
2127    source: FetchIndicesGeneric<'_, I>,
2128    workspace: &mut Workspace,
2129) -> Result<VertexRemap, Error> {
2130    I::with_input(source.indices, workspace, |indices, workspace| {
2131        let source = FetchIndicesGeneric {
2132            indices,
2133            vertex_count: source.vertex_count,
2134            visits: source.visits,
2135            error: source.error,
2136        };
2137        optimize_vertex_fetch_remap_with_indices_u32(source, workspace)
2138    })
2139}
2140
2141fn optimize_vertex_fetch_remap_with_indices_u32(
2142    source: FetchIndicesGeneric<'_>,
2143    workspace: &mut Workspace,
2144) -> Result<VertexRemap, Error> {
2145    let mut work = workspace.begin();
2146    let result = (|| {
2147        source.apply(&mut work)?;
2148        let mut budget = Budget::new(workspace);
2149        let mut remap = budget.filled(source.vertex_count, NONE)?;
2150        let vertex_count = fetch_remap_kernel(&mut remap, source.indices, &mut work)?;
2151        Ok(VertexRemap {
2152            remap,
2153            vertex_count,
2154        })
2155    })();
2156    workspace.finish(&work);
2157    result
2158}
2159
2160/// Generate fetch numbering into caller storage using cached validation.
2161///
2162/// Matches [`optimize_vertex_fetch_remap_into`], including error precedence,
2163/// unused-vertex sentinels, preserved tails and partially written prefixes on
2164/// work exhaustion. Reuse [`FetchIndices`] to avoid repeated validity scans;
2165/// output initialization and numbering remain O(vertex_count + indices.len()).
2166///
2167/// # Errors
2168/// Same errors as [`optimize_vertex_fetch_remap_with_indices`]. `BufferTooSmall` if an output buffer is too short.
2169pub fn optimize_vertex_fetch_remap_with_indices_into_generic<I: crate::Index>(
2170    destination: &mut [u32],
2171    source: FetchIndicesGeneric<'_, I>,
2172    workspace: &mut Workspace,
2173) -> Result<usize, Error> {
2174    I::with_input(source.indices, workspace, |indices, workspace| {
2175        let source = FetchIndicesGeneric {
2176            indices,
2177            vertex_count: source.vertex_count,
2178            visits: source.visits,
2179            error: source.error,
2180        };
2181        optimize_vertex_fetch_remap_with_indices_into_u32(destination, source, workspace)
2182    })
2183}
2184
2185fn optimize_vertex_fetch_remap_with_indices_into_u32(
2186    destination: &mut [u32],
2187    source: FetchIndicesGeneric<'_>,
2188    workspace: &mut Workspace,
2189) -> Result<usize, Error> {
2190    let mut work = workspace.begin();
2191    let result = (|| {
2192        source.apply(&mut work)?;
2193        if destination.len() < source.vertex_count {
2194            return Err(Error::BufferTooSmall);
2195        }
2196        workspace.prepare([0; 4], 0)?;
2197        work.add(source.vertex_count)?;
2198        destination[..source.vertex_count].fill(NONE);
2199        fetch_remap_kernel(
2200            &mut destination[..source.vertex_count],
2201            source.indices,
2202            &mut work,
2203        )
2204    })();
2205    workspace.finish(&work);
2206    result
2207}
2208
2209/// Generate first-use fetch numbering for an arbitrary index sequence.
2210///
2211/// Validates the full sequence on every call. Repeated queries can cache this
2212/// O(indices.len()) validation with [`FetchIndices`] and
2213/// [`optimize_vertex_fetch_remap_with_indices`].
2214///
2215/// # Errors
2216/// `IndexOutOfBounds` for invalid references;
2217/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
2218#[inline]
2219pub fn optimize_vertex_fetch_remap_generic<I: crate::Index>(
2220    indices: &[I],
2221    vertex_count: usize,
2222    workspace: &mut Workspace,
2223) -> Result<VertexRemap, Error> {
2224    I::with_input(indices, workspace, |indices, workspace| {
2225        optimize_vertex_fetch_remap_u32(indices, vertex_count, workspace)
2226    })
2227}
2228
2229fn optimize_vertex_fetch_remap_u32(
2230    indices: &[u32],
2231    vertex_count: usize,
2232    workspace: &mut Workspace,
2233) -> Result<VertexRemap, Error> {
2234    let mut work = workspace.begin();
2235    let result = (|| {
2236        if vertex_count > u32::MAX as usize {
2237            return Err(Error::SizeOverflow);
2238        }
2239        work.indices(indices, vertex_count)?;
2240        let mut budget = Budget::new(workspace);
2241        let mut remap = budget.filled(vertex_count, NONE)?;
2242        let next = fetch_remap_kernel(&mut remap, indices, &mut work)?;
2243        Ok(VertexRemap {
2244            remap,
2245            vertex_count: next,
2246        })
2247    })();
2248    workspace.finish(&work);
2249    result
2250}
2251/// Fetch numbering into caller storage. Unused vertices are `u32::MAX`;
2252/// work exhaustion may partially modify the used prefix.
2253///
2254/// Validates the full sequence on every call. Repeated queries can cache this
2255/// O(indices.len()) validation with [`FetchIndices`] and
2256/// [`optimize_vertex_fetch_remap_with_indices_into`].
2257///
2258/// # Errors
2259/// Same errors as [`optimize_vertex_fetch_remap`]. `BufferTooSmall` if an output buffer is too short.
2260#[inline]
2261pub fn optimize_vertex_fetch_remap_into_generic<I: crate::Index>(
2262    destination: &mut [u32],
2263    indices: &[I],
2264    vertex_count: usize,
2265    workspace: &mut Workspace,
2266) -> Result<usize, Error> {
2267    I::with_input(indices, workspace, |indices, workspace| {
2268        optimize_vertex_fetch_remap_into_u32(destination, indices, vertex_count, workspace)
2269    })
2270}
2271
2272fn optimize_vertex_fetch_remap_into_u32(
2273    destination: &mut [u32],
2274    indices: &[u32],
2275    vertex_count: usize,
2276    workspace: &mut Workspace,
2277) -> Result<usize, Error> {
2278    let mut work = workspace.begin();
2279    let result = (|| {
2280        if vertex_count > u32::MAX as usize {
2281            return Err(Error::SizeOverflow);
2282        }
2283        work.indices(indices, vertex_count)?;
2284        if destination.len() < vertex_count {
2285            return Err(Error::BufferTooSmall);
2286        }
2287        workspace.prepare([0; 4], 0)?;
2288        work.add(vertex_count)?;
2289        destination[..vertex_count].fill(NONE);
2290        fetch_remap_kernel(&mut destination[..vertex_count], indices, &mut work)
2291    })();
2292    workspace.finish(&work);
2293    result
2294}
2295#[inline]
2296fn fetch_remap_kernel(remap: &mut [u32], indices: &[u32], work: &mut Work) -> Result<usize, Error> {
2297    let mut next = 0;
2298    if work.precharge(indices.len(), 1)? {
2299        for &i in indices {
2300            let slot = &mut remap[i as usize];
2301            if *slot == NONE {
2302                *slot = next;
2303                next += 1;
2304            }
2305        }
2306    } else {
2307        for &i in indices {
2308            work.add(1)?;
2309            let slot = &mut remap[i as usize];
2310            if *slot == NONE {
2311                *slot = next;
2312                next += 1;
2313            }
2314        }
2315    }
2316    Ok(next as usize)
2317}
2318#[inline(always)]
2319fn fetch_visit<const SIZE: usize>(
2320    destination: &mut [u8],
2321    vertices: VertexStream<'_>,
2322    remap: &mut [u32],
2323    next: &mut usize,
2324    index: u32,
2325    output: &mut u32,
2326) {
2327    let r = &mut remap[index as usize];
2328    if *r == NONE {
2329        copy_vertex::<SIZE>(destination, vertices, index, *next);
2330        *r = *next as u32;
2331        *next += 1;
2332    }
2333    *output = *r;
2334}
2335fn fetch_kernel<'a, const SIZE: usize>(
2336    destination: &mut [u8],
2337    vertices: VertexStream<'_>,
2338    remap: &mut [u32],
2339    indices: impl ExactSizeIterator<Item = (u32, &'a mut u32)>,
2340    work: &mut Work,
2341) -> Result<usize, Error> {
2342    let mut next = 0;
2343    if work.precharge(indices.len(), 1)? {
2344        for (index, output) in indices {
2345            fetch_visit::<SIZE>(destination, vertices, remap, &mut next, index, output);
2346        }
2347    } else {
2348        for (index, output) in indices {
2349            work.add(1)?;
2350            fetch_visit::<SIZE>(destination, vertices, remap, &mut next, index, output);
2351        }
2352    }
2353    Ok(next)
2354}
2355fn fetch_dispatch<'a>(
2356    destination: &mut [u8],
2357    vertices: VertexStream<'_>,
2358    remap: &mut [u32],
2359    indices: impl ExactSizeIterator<Item = (u32, &'a mut u32)>,
2360    work: &mut Work,
2361) -> Result<usize, Error> {
2362    match vertices.size {
2363        4 => fetch_kernel::<4>(destination, vertices, remap, indices, work),
2364        8 => fetch_kernel::<8>(destination, vertices, remap, indices, work),
2365        12 => fetch_kernel::<12>(destination, vertices, remap, indices, work),
2366        16 => fetch_kernel::<16>(destination, vertices, remap, indices, work),
2367        _ => fetch_kernel::<0>(destination, vertices, remap, indices, work),
2368    }
2369}
2370
2371#[inline(always)]
2372fn fetch_owned_record<const SIZE: usize, const PACKED: bool>(
2373    vertices: VertexStream<'_>,
2374    source: &[[u8; SIZE]],
2375    remap: &mut [u32],
2376    index: &mut u32,
2377    next: &mut usize,
2378) -> Option<[u8; SIZE]> {
2379    let entry = &mut remap[*index as usize];
2380    if *entry == NONE {
2381        let record = if PACKED {
2382            source[*index as usize]
2383        } else {
2384            *vertices.key_array::<SIZE>(*index)
2385        };
2386        *entry = *next as u32;
2387        *next += 1;
2388        *index = *entry;
2389        Some(record)
2390    } else {
2391        *index = *entry;
2392        None
2393    }
2394}
2395
2396fn fetch_owned<const SIZE: usize>(
2397    indices: &[u32],
2398    vertices: VertexStream<'_>,
2399    workspace: &mut Workspace,
2400    work: &mut Work,
2401) -> Result<FetchedMeshGeneric, Error> {
2402    let (mut out, mut records, owned) = {
2403        let mut budget = Budget::new(workspace);
2404        let out = budget.copied(indices)?;
2405        let records = budget.reserved::<[u8; SIZE]>(vertices.count)?;
2406        (out, records, budget.bytes())
2407    };
2408    workspace.prepare_remap(vertices.count, owned)?;
2409    let remap = &mut workspace.integers[..vertices.count];
2410    let vertex_count = if vertices.stride == SIZE {
2411        let source = &vertices.data[vertices.offset..].as_chunks::<SIZE>().0[..vertices.count];
2412        fetch_owned_kernel::<SIZE, true>(&mut records, vertices, source, remap, &mut out, work)?
2413    } else {
2414        fetch_owned_kernel::<SIZE, false>(&mut records, vertices, &[], remap, &mut out, work)?
2415    };
2416    debug_assert_eq!(records.len(), vertex_count);
2417    Ok(FetchedMeshGeneric {
2418        indices: out,
2419        vertices: records.into_flattened(),
2420        vertex_count,
2421    })
2422}
2423
2424fn fetch_owned_kernel<const SIZE: usize, const PACKED: bool>(
2425    records: &mut Vec<[u8; SIZE]>,
2426    vertices: VertexStream<'_>,
2427    source: &[[u8; SIZE]],
2428    remap: &mut [u32],
2429    out: &mut [u32],
2430    work: &mut Work,
2431) -> Result<usize, Error> {
2432    let mut next = 0;
2433    if work.precharge(out.len(), 1)? {
2434        for index in out {
2435            if let Some(record) =
2436                fetch_owned_record::<SIZE, PACKED>(vertices, source, remap, index, &mut next)
2437            {
2438                records.push(record);
2439            }
2440        }
2441    } else {
2442        for index in out {
2443            work.add(1)?;
2444            if let Some(record) =
2445                fetch_owned_record::<SIZE, PACKED>(vertices, source, remap, index, &mut next)
2446            {
2447                records.push(record);
2448            }
2449        }
2450    }
2451    Ok(next)
2452}
2453
2454/// Allocate first-use vertex bytes and rewritten arbitrary indices.
2455///
2456/// # Errors
2457/// `IndexOutOfBounds` for invalid references;
2458/// `SizeOverflow` for size overflow, `LimitExceeded` for exhausted limits, or `AllocationFailed` for failed reservation.
2459#[inline]
2460pub fn optimize_vertex_fetch_generic<I: crate::Index>(
2461    indices: &[I],
2462    vertices: VertexStream<'_>,
2463    workspace: &mut Workspace,
2464) -> Result<FetchedMeshGeneric<I>, Error> {
2465    I::with_input(indices, workspace, |indices, workspace| {
2466        optimize_vertex_fetch_u32(indices, vertices, workspace)
2467            .and_then(|value| crate::index::Convert::<I>::convert(value, workspace))
2468    })
2469}
2470
2471fn optimize_vertex_fetch_u32(
2472    indices: &[u32],
2473    vertices: VertexStream<'_>,
2474    workspace: &mut Workspace,
2475) -> Result<FetchedMeshGeneric, Error> {
2476    let mut work = workspace.begin();
2477    let result = (|| {
2478        validate_indices(Some(indices), vertices.count, &mut work)?;
2479        work.add(indices.len())?;
2480        if vertices.count >= 1024 && vertices.stride != vertices.size {
2481            match vertices.size {
2482                4 => return fetch_owned::<4>(indices, vertices, workspace, &mut work),
2483                8 => return fetch_owned::<8>(indices, vertices, workspace, &mut work),
2484                12 => return fetch_owned::<12>(indices, vertices, workspace, &mut work),
2485                16 => return fetch_owned::<16>(indices, vertices, workspace, &mut work),
2486                _ => {}
2487            }
2488        }
2489        let (mut out, mut bytes, owned) = {
2490            let mut budget = Budget::new(workspace);
2491            let out = budget.copied(indices)?;
2492            let bytes = budget.filled(checked_bytes(vertices.count, vertices.size)?, 0u8)?;
2493            (out, bytes, budget.bytes())
2494        };
2495        workspace.prepare_remap(vertices.count, owned)?;
2496        let remap = &mut workspace.integers[..vertices.count];
2497        let next = fetch_dispatch(
2498            &mut bytes,
2499            vertices,
2500            remap,
2501            out.iter_mut().map(|i| (*i, i)),
2502            &mut work,
2503        )?;
2504        bytes.truncate(next * vertices.size);
2505        Ok(FetchedMeshGeneric {
2506            indices: out,
2507            vertices: bytes,
2508            vertex_count: next,
2509        })
2510    })();
2511    workspace.finish(&work);
2512    result
2513}
2514/// Write fetch-ordered bytes while rewriting indices in place. Validation and
2515/// allocation precede writes; counted-work exhaustion may modify both buffers.
2516///
2517/// # Errors
2518/// Same errors as [`optimize_vertex_fetch`]. `BufferTooSmall` if destination storage is insufficient.
2519#[inline]
2520pub fn optimize_vertex_fetch_into_generic<I: crate::Index>(
2521    destination: &mut [u8],
2522    indices: &mut [I],
2523    vertices: VertexStream<'_>,
2524    workspace: &mut Workspace,
2525) -> Result<usize, Error> {
2526    I::with_mut(indices, workspace, |indices, workspace| {
2527        optimize_vertex_fetch_into_u32(destination, indices, vertices, workspace)
2528    })
2529}
2530
2531fn optimize_vertex_fetch_into_u32(
2532    destination: &mut [u8],
2533    indices: &mut [u32],
2534    vertices: VertexStream<'_>,
2535    workspace: &mut Workspace,
2536) -> Result<usize, Error> {
2537    let mut work = workspace.begin();
2538    let result = (|| {
2539        validate_indices(Some(indices), vertices.count, &mut work)?;
2540        if destination.len() < checked_bytes(vertices.count, vertices.size)? {
2541            return Err(Error::BufferTooSmall);
2542        }
2543        workspace.prepare_remap(vertices.count, 0)?;
2544        let remap = &mut workspace.integers[..vertices.count];
2545        let next = fetch_dispatch(
2546            destination,
2547            vertices,
2548            remap,
2549            indices.iter_mut().map(|i| (*i, i)),
2550            &mut work,
2551        )?;
2552        Ok(next)
2553    })();
2554    workspace.finish(&work);
2555    result
2556}
2557
2558/// Caller-buffer filtering. The tail is preserved; work exhaustion can leave partial output.
2559///
2560/// # Errors
2561/// Same errors as [`filter_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
2562#[inline]
2563pub fn filter_index_buffer_into_generic<I: crate::Index>(
2564    destination: &mut [I],
2565    indices: &[I],
2566    vertices: VertexStream<'_>,
2567    workspace: &mut Workspace,
2568) -> Result<usize, Error> {
2569    I::with_input(indices, workspace, |indices, workspace| {
2570        I::with_mut(destination, workspace, |destination, workspace| {
2571            filter_index_buffer_into_u32(destination, indices, vertices, workspace)
2572        })
2573    })
2574}
2575
2576fn filter_index_buffer_into_u32(
2577    destination: &mut [u32],
2578    indices: &[u32],
2579    vertices: VertexStream<'_>,
2580    workspace: &mut Workspace,
2581) -> Result<usize, Error> {
2582    filter_index_buffer_multi_into_generic(destination, indices, &[vertices], workspace)
2583}
2584/// Caller-buffer multi-stream filtering; late work exhaustion can leave partial output.
2585///
2586/// # Errors
2587/// Same errors as [`filter_index_buffer_multi`]. `BufferTooSmall` if an output buffer is too short.
2588#[inline]
2589pub fn filter_index_buffer_multi_into_generic<I: crate::Index>(
2590    destination: &mut [I],
2591    indices: &[I],
2592    streams: &[VertexStream<'_>],
2593    workspace: &mut Workspace,
2594) -> Result<usize, Error> {
2595    I::with_input(indices, workspace, |indices, workspace| {
2596        I::with_mut(destination, workspace, |destination, workspace| {
2597            filter_index_buffer_multi_into_u32(destination, indices, streams, workspace)
2598        })
2599    })
2600}
2601
2602fn filter_index_buffer_multi_into_u32(
2603    destination: &mut [u32],
2604    indices: &[u32],
2605    streams: &[VertexStream<'_>],
2606    workspace: &mut Workspace,
2607) -> Result<usize, Error> {
2608    Ok(shadow_filter::<true>(indices, streams, Some(destination), workspace)?.1)
2609}
2610/// Caller-buffer shadow indices; late work exhaustion can leave partial output.
2611///
2612/// # Errors
2613/// Same errors as [`generate_shadow_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
2614#[inline]
2615pub fn generate_shadow_index_buffer_into_generic<I: crate::Index>(
2616    destination: &mut [I],
2617    indices: &[I],
2618    vertices: VertexStream<'_>,
2619    workspace: &mut Workspace,
2620) -> Result<usize, Error> {
2621    I::with_input(indices, workspace, |indices, workspace| {
2622        I::with_mut(destination, workspace, |destination, workspace| {
2623            generate_shadow_index_buffer_into_u32(destination, indices, vertices, workspace)
2624        })
2625    })
2626}
2627
2628fn generate_shadow_index_buffer_into_u32(
2629    destination: &mut [u32],
2630    indices: &[u32],
2631    vertices: VertexStream<'_>,
2632    workspace: &mut Workspace,
2633) -> Result<usize, Error> {
2634    generate_shadow_index_buffer_multi_into_generic(destination, indices, &[vertices], workspace)
2635}
2636/// Caller-buffer multi-stream shadow indices; late work exhaustion can leave partial output.
2637///
2638/// # Errors
2639/// Same errors as [`generate_shadow_index_buffer_multi`]. `BufferTooSmall` if an output buffer is too short.
2640#[inline]
2641pub fn generate_shadow_index_buffer_multi_into_generic<I: crate::Index>(
2642    destination: &mut [I],
2643    indices: &[I],
2644    streams: &[VertexStream<'_>],
2645    workspace: &mut Workspace,
2646) -> Result<usize, Error> {
2647    I::with_input(indices, workspace, |indices, workspace| {
2648        I::with_mut(destination, workspace, |destination, workspace| {
2649            generate_shadow_index_buffer_multi_into_u32(destination, indices, streams, workspace)
2650        })
2651    })
2652}
2653
2654fn generate_shadow_index_buffer_multi_into_u32(
2655    destination: &mut [u32],
2656    indices: &[u32],
2657    streams: &[VertexStream<'_>],
2658    workspace: &mut Workspace,
2659) -> Result<usize, Error> {
2660    Ok(shadow_filter::<false>(indices, streams, Some(destination), workspace)?.1)
2661}
2662/// Caller-buffer adjacency; requires twice input-length capacity. Late work exhaustion can leave partial output.
2663///
2664/// # Errors
2665/// Same errors as [`generate_adjacency_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
2666#[inline]
2667pub fn generate_adjacency_index_buffer_into_generic<I: crate::Index>(
2668    destination: &mut [I],
2669    indices: &[I],
2670    positions: Positions<'_>,
2671    workspace: &mut Workspace,
2672) -> Result<(), Error> {
2673    I::with_input(indices, workspace, |indices, workspace| {
2674        I::with_mut(destination, workspace, |destination, workspace| {
2675            generate_adjacency_index_buffer_into_u32(destination, indices, positions, workspace)
2676        })
2677    })
2678}
2679
2680fn generate_adjacency_index_buffer_into_u32(
2681    destination: &mut [u32],
2682    indices: &[u32],
2683    positions: Positions<'_>,
2684    workspace: &mut Workspace,
2685) -> Result<(), Error> {
2686    patches::<false>(indices, positions, Some(destination), workspace)?;
2687    Ok(())
2688}
2689/// Caller-buffer tessellation; requires four times input-length capacity. Late work exhaustion can leave partial output.
2690///
2691/// # Errors
2692/// Same errors as [`generate_tessellation_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
2693#[inline]
2694pub fn generate_tessellation_index_buffer_into_generic<I: crate::Index>(
2695    destination: &mut [I],
2696    indices: &[I],
2697    positions: Positions<'_>,
2698    workspace: &mut Workspace,
2699) -> Result<(), Error> {
2700    I::with_input(indices, workspace, |indices, workspace| {
2701        I::with_mut(destination, workspace, |destination, workspace| {
2702            generate_tessellation_index_buffer_into_u32(destination, indices, positions, workspace)
2703        })
2704    })
2705}
2706
2707fn generate_tessellation_index_buffer_into_u32(
2708    destination: &mut [u32],
2709    indices: &[u32],
2710    positions: Positions<'_>,
2711    workspace: &mut Workspace,
2712) -> Result<(), Error> {
2713    patches::<true>(indices, positions, Some(destination), workspace)?;
2714    Ok(())
2715}
2716fn remap_into<const OUTLINE: bool>(
2717    destination: &mut [u32],
2718    indices: Option<&[u32]>,
2719    count: usize,
2720    hash: impl Fn(u32) -> u32,
2721    equal: impl FnMut(u32, u32) -> bool,
2722    canonical: bool,
2723    workspace: &mut Workspace,
2724) -> Result<usize, Error> {
2725    let mut work = workspace.begin();
2726    let result = (|| {
2727        validate_indices(indices, count, &mut work)?;
2728        if destination.len() < count {
2729            return Err(Error::BufferTooSmall);
2730        }
2731        let table_len = buckets(count)?;
2732        workspace.prepare([table_len, 0, 0, 0], 0)?;
2733        generate::<OUTLINE>(
2734            &mut destination[..count],
2735            indices,
2736            &mut workspace.integers[..table_len],
2737            hash,
2738            equal,
2739            canonical,
2740            &mut work,
2741        )
2742    })();
2743    workspace.finish(&work);
2744    result
2745}
2746/// Caller-buffer numerical position remap. The tail is preserved; work exhaustion can leave partial output.
2747///
2748/// # Errors
2749/// Same errors as [`generate_position_remap`]. `BufferTooSmall` if an output buffer is too short.
2750pub fn generate_position_remap_into_generic<I: crate::Index>(
2751    destination: &mut [I],
2752    positions: Positions<'_>,
2753    workspace: &mut Workspace,
2754) -> Result<(), Error> {
2755    I::with_mut(destination, workspace, |destination, workspace| {
2756        generate_position_remap_into_u32(destination, positions, workspace)
2757    })
2758}
2759
2760fn generate_position_remap_into_u32(
2761    destination: &mut [u32],
2762    positions: Positions<'_>,
2763    workspace: &mut Workspace,
2764) -> Result<(), Error> {
2765    if let Some(p) = positions.packed_values() {
2766        return remap_into::<false>(
2767            destination,
2768            None,
2769            p.len(),
2770            |i| position_value_hash(p[i as usize]),
2771            |a, b| p[a as usize] == p[b as usize],
2772            true,
2773            workspace,
2774        )
2775        .map(|_| ());
2776    }
2777    remap_into::<false>(
2778        destination,
2779        None,
2780        positions.len(),
2781        |i| position_hash(positions, i),
2782        |a, b| positions.get(a as usize) == positions.get(b as usize),
2783        true,
2784        workspace,
2785    )?;
2786    Ok(())
2787}
2788/// Caller-buffer custom numbering with exact callback traversal. Work exhaustion can leave partial output.
2789///
2790/// # Errors
2791/// Same errors as [`generate_vertex_remap_custom`]. `BufferTooSmall` if an output buffer is too short.
2792///
2793/// # Panics
2794/// Panics if the supplied callback panics.
2795pub fn generate_vertex_remap_custom_into_generic<I: crate::Index>(
2796    destination: &mut [u32],
2797    indices: Option<&[I]>,
2798    positions: Positions<'_>,
2799    equal: impl FnMut(u32, u32) -> bool,
2800    workspace: &mut Workspace,
2801) -> Result<usize, Error> {
2802    I::with_optional(indices, workspace, |indices, workspace| {
2803        generate_vertex_remap_custom_into_u32(destination, indices, positions, equal, workspace)
2804    })
2805}
2806
2807fn generate_vertex_remap_custom_into_u32(
2808    destination: &mut [u32],
2809    indices: Option<&[u32]>,
2810    positions: Positions<'_>,
2811    mut equal: impl FnMut(u32, u32) -> bool,
2812    workspace: &mut Workspace,
2813) -> Result<usize, Error> {
2814    if let Some(p) = positions.packed_values() {
2815        return remap_into::<true>(
2816            destination,
2817            indices,
2818            p.len(),
2819            |i| position_value_hash(p[i as usize]),
2820            |a, b| p[a as usize] == p[b as usize] && custom_equal(&mut equal, a, b),
2821            false,
2822            workspace,
2823        );
2824    }
2825    remap_into::<true>(
2826        destination,
2827        indices,
2828        positions.len(),
2829        |i| position_hash(positions, i),
2830        |a, b| {
2831            positions.get(a as usize) == positions.get(b as usize) && custom_equal(&mut equal, a, b)
2832        },
2833        false,
2834        workspace,
2835    )
2836}
2837/// Write provoking topology and used reorder prefix. Requires vertex-count plus triangle-count reorder capacity; work exhaustion can partially modify both outputs.
2838///
2839/// # Errors
2840/// Same errors as [`generate_provoking_index_buffer`]. `BufferTooSmall` if an output buffer is too short.
2841#[inline]
2842pub fn generate_provoking_index_buffer_into_generic<I: crate::Index>(
2843    destination: &mut [I],
2844    reorder: &mut [u32],
2845    indices: &[I],
2846    vertex_count: usize,
2847    workspace: &mut Workspace,
2848) -> Result<usize, Error> {
2849    I::with_input(indices, workspace, |indices, workspace| {
2850        I::with_mut(destination, workspace, |destination, workspace| {
2851            generate_provoking_index_buffer_into_u32(
2852                destination,
2853                reorder,
2854                indices,
2855                vertex_count,
2856                workspace,
2857            )
2858        })
2859    })
2860}
2861
2862fn generate_provoking_index_buffer_into_u32(
2863    destination: &mut [u32],
2864    reorder: &mut [u32],
2865    indices: &[u32],
2866    vertex_count: usize,
2867    workspace: &mut Workspace,
2868) -> Result<usize, Error> {
2869    Ok(provoking(
2870        indices,
2871        vertex_count,
2872        Some((destination, reorder)),
2873        workspace,
2874    )?
2875    .1)
2876}
2877
2878/// The source-compatible u32 entry point; see [`generate_vertex_remap_generic`] for selectable index widths.
2879#[inline]
2880pub fn generate_vertex_remap(
2881    indices: Option<&[u32]>,
2882    vertices: VertexStream<'_>,
2883    workspace: &mut Workspace,
2884) -> Result<VertexRemap, Error> {
2885    generate_vertex_remap_generic::<u32>(indices, vertices, workspace)
2886}
2887
2888/// The source-compatible u32 entry point; see [`generate_vertex_remap_multi_generic`] for selectable index widths.
2889#[inline]
2890pub fn generate_vertex_remap_multi(
2891    indices: Option<&[u32]>,
2892    streams: &[VertexStream<'_>],
2893    workspace: &mut Workspace,
2894) -> Result<VertexRemap, Error> {
2895    generate_vertex_remap_multi_generic::<u32>(indices, streams, workspace)
2896}
2897
2898/// The source-compatible u32 entry point; see [`generate_vertex_remap_custom_generic`] for selectable index widths.
2899#[inline]
2900pub fn generate_vertex_remap_custom(
2901    indices: Option<&[u32]>,
2902    positions: Positions<'_>,
2903    equal: impl FnMut(u32, u32) -> bool,
2904    workspace: &mut Workspace,
2905) -> Result<VertexRemap, Error> {
2906    generate_vertex_remap_custom_generic::<u32>(indices, positions, equal, workspace)
2907}
2908
2909/// The source-compatible u32 entry point; see [`generate_position_remap_generic`] for selectable index widths.
2910#[inline]
2911pub fn generate_position_remap(
2912    positions: Positions<'_>,
2913    workspace: &mut Workspace,
2914) -> Result<Vec<u32>, Error> {
2915    generate_position_remap_generic::<u32>(positions, workspace)
2916}
2917
2918/// The source-compatible u32 entry point; see [`generate_vertex_remap_multi_into_generic`] for selectable index widths.
2919#[inline]
2920pub fn generate_vertex_remap_multi_into(
2921    destination: &mut [u32],
2922    indices: Option<&[u32]>,
2923    streams: &[VertexStream<'_>],
2924    workspace: &mut Workspace,
2925) -> Result<usize, Error> {
2926    generate_vertex_remap_multi_into_generic::<u32>(destination, indices, streams, workspace)
2927}
2928
2929/// The source-compatible u32 entry point; see [`generate_vertex_remap_into_generic`] for selectable index widths.
2930#[inline]
2931pub fn generate_vertex_remap_into(
2932    destination: &mut [u32],
2933    indices: Option<&[u32]>,
2934    vertices: VertexStream<'_>,
2935    workspace: &mut Workspace,
2936) -> Result<usize, Error> {
2937    generate_vertex_remap_into_generic::<u32>(destination, indices, vertices, workspace)
2938}
2939
2940/// The source-compatible u32 entry point; see [`remap_index_buffer_into_generic`] for selectable index widths.
2941#[inline]
2942pub fn remap_index_buffer_into(
2943    destination: &mut [u32],
2944    indices: Option<&[u32]>,
2945    remap: &[u32],
2946    workspace: &mut Workspace,
2947) -> Result<(), Error> {
2948    remap_index_buffer_into_generic::<u32>(destination, indices, remap, workspace)
2949}
2950
2951/// The source-compatible u32 entry point; see [`remap_index_buffer_generic`] for selectable index widths.
2952#[inline]
2953pub fn remap_index_buffer(
2954    indices: Option<&[u32]>,
2955    remap: &[u32],
2956    workspace: &mut Workspace,
2957) -> Result<Vec<u32>, Error> {
2958    remap_index_buffer_generic::<u32>(indices, remap, workspace)
2959}
2960
2961/// The source-compatible u32 entry point; see [`generate_shadow_index_buffer_generic`] for selectable index widths.
2962#[inline]
2963pub fn generate_shadow_index_buffer(
2964    indices: &[u32],
2965    vertices: VertexStream<'_>,
2966    workspace: &mut Workspace,
2967) -> Result<Vec<u32>, Error> {
2968    generate_shadow_index_buffer_generic::<u32>(indices, vertices, workspace)
2969}
2970
2971/// The source-compatible u32 entry point; see [`generate_shadow_index_buffer_multi_generic`] for selectable index widths.
2972#[inline]
2973pub fn generate_shadow_index_buffer_multi(
2974    indices: &[u32],
2975    streams: &[VertexStream<'_>],
2976    workspace: &mut Workspace,
2977) -> Result<Vec<u32>, Error> {
2978    generate_shadow_index_buffer_multi_generic::<u32>(indices, streams, workspace)
2979}
2980
2981/// The source-compatible u32 entry point; see [`filter_index_buffer_generic`] for selectable index widths.
2982#[inline]
2983pub fn filter_index_buffer(
2984    indices: &[u32],
2985    vertices: VertexStream<'_>,
2986    workspace: &mut Workspace,
2987) -> Result<Vec<u32>, Error> {
2988    filter_index_buffer_generic::<u32>(indices, vertices, workspace)
2989}
2990
2991/// The source-compatible u32 entry point; see [`filter_index_buffer_multi_generic`] for selectable index widths.
2992#[inline]
2993pub fn filter_index_buffer_multi(
2994    indices: &[u32],
2995    streams: &[VertexStream<'_>],
2996    workspace: &mut Workspace,
2997) -> Result<Vec<u32>, Error> {
2998    filter_index_buffer_multi_generic::<u32>(indices, streams, workspace)
2999}
3000
3001/// The source-compatible u32 entry point; see [`generate_adjacency_index_buffer_generic`] for selectable index widths.
3002#[inline]
3003pub fn generate_adjacency_index_buffer(
3004    indices: &[u32],
3005    positions: Positions<'_>,
3006    workspace: &mut Workspace,
3007) -> Result<Vec<u32>, Error> {
3008    generate_adjacency_index_buffer_generic::<u32>(indices, positions, workspace)
3009}
3010
3011/// The source-compatible u32 entry point; see [`generate_tessellation_index_buffer_generic`] for selectable index widths.
3012#[inline]
3013pub fn generate_tessellation_index_buffer(
3014    indices: &[u32],
3015    positions: Positions<'_>,
3016    workspace: &mut Workspace,
3017) -> Result<Vec<u32>, Error> {
3018    generate_tessellation_index_buffer_generic::<u32>(indices, positions, workspace)
3019}
3020
3021/// The source-compatible u32 entry point; see [`generate_provoking_index_buffer_generic`] for selectable index widths.
3022#[inline]
3023pub fn generate_provoking_index_buffer(
3024    indices: &[u32],
3025    vertex_count: usize,
3026    workspace: &mut Workspace,
3027) -> Result<ProvokingMesh, Error> {
3028    generate_provoking_index_buffer_generic::<u32>(indices, vertex_count, workspace).map(Into::into)
3029}
3030
3031/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_remap_with_indices_generic`] for selectable index widths.
3032#[inline]
3033pub fn optimize_vertex_fetch_remap_with_indices(
3034    source: FetchIndices<'_>,
3035    workspace: &mut Workspace,
3036) -> Result<VertexRemap, Error> {
3037    optimize_vertex_fetch_remap_with_indices_generic::<u32>(source.into(), workspace)
3038}
3039
3040/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_remap_with_indices_into_generic`] for selectable index widths.
3041#[inline]
3042pub fn optimize_vertex_fetch_remap_with_indices_into(
3043    destination: &mut [u32],
3044    source: FetchIndices<'_>,
3045    workspace: &mut Workspace,
3046) -> Result<usize, Error> {
3047    optimize_vertex_fetch_remap_with_indices_into_generic::<u32>(
3048        destination,
3049        source.into(),
3050        workspace,
3051    )
3052}
3053
3054/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_remap_generic`] for selectable index widths.
3055#[inline]
3056pub fn optimize_vertex_fetch_remap(
3057    indices: &[u32],
3058    vertex_count: usize,
3059    workspace: &mut Workspace,
3060) -> Result<VertexRemap, Error> {
3061    optimize_vertex_fetch_remap_generic::<u32>(indices, vertex_count, workspace)
3062}
3063
3064/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_remap_into_generic`] for selectable index widths.
3065#[inline]
3066pub fn optimize_vertex_fetch_remap_into(
3067    destination: &mut [u32],
3068    indices: &[u32],
3069    vertex_count: usize,
3070    workspace: &mut Workspace,
3071) -> Result<usize, Error> {
3072    optimize_vertex_fetch_remap_into_generic::<u32>(destination, indices, vertex_count, workspace)
3073}
3074
3075/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_generic`] for selectable index widths.
3076#[inline]
3077pub fn optimize_vertex_fetch(
3078    indices: &[u32],
3079    vertices: VertexStream<'_>,
3080    workspace: &mut Workspace,
3081) -> Result<FetchedMesh, Error> {
3082    optimize_vertex_fetch_generic::<u32>(indices, vertices, workspace).map(Into::into)
3083}
3084
3085/// The source-compatible u32 entry point; see [`optimize_vertex_fetch_into_generic`] for selectable index widths.
3086#[inline]
3087pub fn optimize_vertex_fetch_into(
3088    destination: &mut [u8],
3089    indices: &mut [u32],
3090    vertices: VertexStream<'_>,
3091    workspace: &mut Workspace,
3092) -> Result<usize, Error> {
3093    optimize_vertex_fetch_into_generic::<u32>(destination, indices, vertices, workspace)
3094}
3095
3096/// The source-compatible u32 entry point; see [`filter_index_buffer_into_generic`] for selectable index widths.
3097#[inline]
3098pub fn filter_index_buffer_into(
3099    destination: &mut [u32],
3100    indices: &[u32],
3101    vertices: VertexStream<'_>,
3102    workspace: &mut Workspace,
3103) -> Result<usize, Error> {
3104    filter_index_buffer_into_generic::<u32>(destination, indices, vertices, workspace)
3105}
3106
3107/// The source-compatible u32 entry point; see [`filter_index_buffer_multi_into_generic`] for selectable index widths.
3108#[inline]
3109pub fn filter_index_buffer_multi_into(
3110    destination: &mut [u32],
3111    indices: &[u32],
3112    streams: &[VertexStream<'_>],
3113    workspace: &mut Workspace,
3114) -> Result<usize, Error> {
3115    filter_index_buffer_multi_into_generic::<u32>(destination, indices, streams, workspace)
3116}
3117
3118/// The source-compatible u32 entry point; see [`generate_shadow_index_buffer_into_generic`] for selectable index widths.
3119#[inline]
3120pub fn generate_shadow_index_buffer_into(
3121    destination: &mut [u32],
3122    indices: &[u32],
3123    vertices: VertexStream<'_>,
3124    workspace: &mut Workspace,
3125) -> Result<usize, Error> {
3126    generate_shadow_index_buffer_into_generic::<u32>(destination, indices, vertices, workspace)
3127}
3128
3129/// The source-compatible u32 entry point; see [`generate_shadow_index_buffer_multi_into_generic`] for selectable index widths.
3130#[inline]
3131pub fn generate_shadow_index_buffer_multi_into(
3132    destination: &mut [u32],
3133    indices: &[u32],
3134    streams: &[VertexStream<'_>],
3135    workspace: &mut Workspace,
3136) -> Result<usize, Error> {
3137    generate_shadow_index_buffer_multi_into_generic::<u32>(destination, indices, streams, workspace)
3138}
3139
3140/// The source-compatible u32 entry point; see [`generate_adjacency_index_buffer_into_generic`] for selectable index widths.
3141#[inline]
3142pub fn generate_adjacency_index_buffer_into(
3143    destination: &mut [u32],
3144    indices: &[u32],
3145    positions: Positions<'_>,
3146    workspace: &mut Workspace,
3147) -> Result<(), Error> {
3148    generate_adjacency_index_buffer_into_generic::<u32>(destination, indices, positions, workspace)
3149}
3150
3151/// The source-compatible u32 entry point; see [`generate_tessellation_index_buffer_into_generic`] for selectable index widths.
3152#[inline]
3153pub fn generate_tessellation_index_buffer_into(
3154    destination: &mut [u32],
3155    indices: &[u32],
3156    positions: Positions<'_>,
3157    workspace: &mut Workspace,
3158) -> Result<(), Error> {
3159    generate_tessellation_index_buffer_into_generic::<u32>(
3160        destination,
3161        indices,
3162        positions,
3163        workspace,
3164    )
3165}
3166
3167/// The source-compatible u32 entry point; see [`generate_position_remap_into_generic`] for selectable index widths.
3168#[inline]
3169pub fn generate_position_remap_into(
3170    destination: &mut [u32],
3171    positions: Positions<'_>,
3172    workspace: &mut Workspace,
3173) -> Result<(), Error> {
3174    generate_position_remap_into_generic::<u32>(destination, positions, workspace)
3175}
3176
3177/// The source-compatible u32 entry point; see [`generate_vertex_remap_custom_into_generic`] for selectable index widths.
3178#[inline]
3179pub fn generate_vertex_remap_custom_into(
3180    destination: &mut [u32],
3181    indices: Option<&[u32]>,
3182    positions: Positions<'_>,
3183    equal: impl FnMut(u32, u32) -> bool,
3184    workspace: &mut Workspace,
3185) -> Result<usize, Error> {
3186    generate_vertex_remap_custom_into_generic::<u32>(
3187        destination,
3188        indices,
3189        positions,
3190        equal,
3191        workspace,
3192    )
3193}
3194
3195/// The source-compatible u32 entry point; see [`generate_provoking_index_buffer_into_generic`] for selectable index widths.
3196#[inline]
3197pub fn generate_provoking_index_buffer_into(
3198    destination: &mut [u32],
3199    reorder: &mut [u32],
3200    indices: &[u32],
3201    vertex_count: usize,
3202    workspace: &mut Workspace,
3203) -> Result<usize, Error> {
3204    generate_provoking_index_buffer_into_generic::<u32>(
3205        destination,
3206        reorder,
3207        indices,
3208        vertex_count,
3209        workspace,
3210    )
3211}
3212
3213impl<'a> FetchIndices<'a> {
3214    /// Source-compatible u32 operation.
3215    #[inline]
3216    pub fn new(indices: &'a [u32], vertex_count: usize) -> Self {
3217        FetchIndicesGeneric::new_generic(indices, vertex_count).into()
3218    }
3219}
3220
3221/// The source-compatible u32 form of [`FetchedMeshGeneric`].
3222/// Fetch-ordered initialized vertex bytes and rewritten indices.
3223#[derive(Debug)]
3224pub struct FetchedMesh {
3225    /// Rewritten topology.
3226    pub indices: Vec<u32>,
3227    /// Used tightly packed vertex records.
3228    pub vertices: Vec<u8>,
3229    /// Number of used vertices.
3230    pub vertex_count: usize,
3231}
3232impl From<FetchedMeshGeneric<u32>> for FetchedMesh {
3233    #[inline]
3234    fn from(value: FetchedMeshGeneric<u32>) -> Self {
3235        Self {
3236            indices: value.indices,
3237            vertices: value.vertices,
3238            vertex_count: value.vertex_count,
3239        }
3240    }
3241}
3242
3243impl From<FetchedMesh> for FetchedMeshGeneric<u32> {
3244    #[inline]
3245    fn from(value: FetchedMesh) -> Self {
3246        Self {
3247            indices: value.indices,
3248            vertices: value.vertices,
3249            vertex_count: value.vertex_count,
3250        }
3251    }
3252}
3253
3254/// The source-compatible u32 form of [`ProvokingMeshGeneric`].
3255/// Unique first-corner indices and source vertices for provoking-vertex rendering.
3256#[derive(Debug)]
3257pub struct ProvokingMesh {
3258    /// Rotated and rewritten triangle indices.
3259    pub indices: Vec<u32>,
3260    /// Output vertex to original vertex mapping, including clones.
3261    pub reorder: Vec<u32>,
3262}
3263impl From<ProvokingMeshGeneric<u32>> for ProvokingMesh {
3264    #[inline]
3265    fn from(value: ProvokingMeshGeneric<u32>) -> Self {
3266        Self {
3267            indices: value.indices,
3268            reorder: value.reorder,
3269        }
3270    }
3271}
3272
3273impl From<ProvokingMesh> for ProvokingMeshGeneric<u32> {
3274    #[inline]
3275    fn from(value: ProvokingMesh) -> Self {
3276        Self {
3277            indices: value.indices,
3278            reorder: value.reorder,
3279        }
3280    }
3281}
3282
3283/// The source-compatible u32 form of [`FetchIndicesGeneric`].
3284/// Borrowed fetch-remap input with its index-validation result cached once.
3285///
3286/// Construction scans at most `indices.len()` entries, without allocating.
3287/// Invalid input is retained so operations can preserve error precedence,
3288/// logical work usage and caller-buffer writes, including on short budgets.
3289#[derive(Clone, Copy, Debug)]
3290pub struct FetchIndices<'a> {
3291    indices: &'a [u32],
3292    vertex_count: usize,
3293    visits: usize,
3294    error: Option<Error>,
3295}
3296impl<'a> From<FetchIndicesGeneric<'a, u32>> for FetchIndices<'a> {
3297    #[inline]
3298    fn from(value: FetchIndicesGeneric<'a, u32>) -> Self {
3299        Self {
3300            indices: value.indices,
3301            vertex_count: value.vertex_count,
3302            visits: value.visits,
3303            error: value.error,
3304        }
3305    }
3306}
3307
3308impl<'a> From<FetchIndices<'a>> for FetchIndicesGeneric<'a, u32> {
3309    #[inline]
3310    fn from(value: FetchIndices<'a>) -> Self {
3311        Self {
3312            indices: value.indices,
3313            vertex_count: value.vertex_count,
3314            visits: value.visits,
3315            error: value.error,
3316        }
3317    }
3318}