1use super::{filter_encode, index_encode, vertex_encode};
4use crate::{workspace::checked_bytes, Error, Workspace};
5use alloc::vec::Vec;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct VertexEncoding {
11 version: u8,
12 level: u8,
13}
14impl VertexEncoding {
15 pub const DEFAULT: Self = Self {
17 version: 1,
18 level: 2,
19 };
20 pub const fn new(version: u8, level: u8) -> Result<Self, Error> {
27 if version > 1 {
28 return Err(Error::UnsupportedVersion);
29 }
30 if level > 9 {
31 return Err(Error::InvalidParameter);
32 }
33 Ok(Self { version, level })
34 }
35 pub const fn version(self) -> u8 {
37 self.version
38 }
39 pub const fn level(self) -> u8 {
41 self.level
42 }
43}
44impl Default for VertexEncoding {
45 fn default() -> Self {
46 Self::DEFAULT
47 }
48}
49
50#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
53pub struct IndexEncoding {
54 version: u8,
55}
56impl IndexEncoding {
57 pub const DEFAULT: Self = Self { version: 1 };
59 pub const fn new(version: u8) -> Result<Self, Error> {
64 if version > 1 {
65 return Err(Error::UnsupportedVersion);
66 }
67 Ok(Self { version })
68 }
69 pub const fn version(self) -> u8 {
71 self.version
72 }
73}
74impl Default for IndexEncoding {
75 fn default() -> Self {
76 Self::DEFAULT
77 }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
82pub enum ExpMode {
83 Separate,
85 SharedVector,
87 SharedComponent,
89 Clamped,
91}
92
93#[inline]
94fn vertex_layout(count: usize, stride: usize) -> Result<usize, Error> {
95 if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
96 return Err(Error::InvalidLayout);
97 }
98 checked_bytes(count, stride)
99}
100
101#[inline]
107pub fn encode_vertex_buffer_bound(count: usize, stride: usize) -> Result<usize, Error> {
108 if stride == 0 || stride > 256 || !stride.is_multiple_of(4) {
109 return Err(Error::InvalidLayout);
110 }
111 vertex_encode::bound(count, stride)
112}
113
114#[inline]
120pub fn encode_index_buffer_bound(index_count: usize, vertex_count: usize) -> Result<usize, Error> {
121 if !index_count.is_multiple_of(3) {
122 return Err(Error::InvalidTopology);
123 }
124 index_encode::index_bound(index_count, vertex_count as u64)
125}
126
127#[inline]
132pub fn encode_index_sequence_bound(
133 index_count: usize,
134 vertex_count: usize,
135) -> Result<usize, Error> {
136 index_encode::sequence_bound(index_count, vertex_count as u64)
137}
138
139fn charge(bound: usize, input: usize, workspace: &mut Workspace) -> Result<(), Error> {
141 let mut work = workspace.begin();
142 let check = (|| {
143 workspace.account_codec(bound)?;
144 work.add(input)?;
145 work.add(bound)
146 })();
147 workspace.finish(&work);
148 check
149}
150
151#[cfg(feature = "parallel")]
154pub(crate) fn encode_vertex_buffer_parallel(
155 vertices: &[u8],
156 count: usize,
157 stride: usize,
158 encoding: VertexEncoding,
159 workspace: &mut Workspace,
160) -> Result<Vec<u8>, Error> {
161 let valid = stride != 0 && stride <= 256 && stride.is_multiple_of(4);
162 if !valid
163 || rayon::current_num_threads() < 2
164 || count.div_ceil(vertex_encode::block_size(stride)) < 2 * vertex_encode::MIN_CHUNK_BLOCKS
165 {
166 return encode_vertex_buffer(vertices, count, stride, encoding, workspace);
167 }
168 let _ = workspace.begin();
169 if vertices.len() != vertex_layout(count, stride)? {
170 return Err(Error::InvalidLayout);
171 }
172 let bound = vertex_encode::bound(count, stride)?;
173 charge(bound, vertices.len(), workspace)?;
174 vertex_encode::encode_parallel(vertices, count, stride, encoding.version, encoding.level)
175}
176
177fn allocate(
180 bound: usize,
181 input: usize,
182 workspace: &mut Workspace,
183 encode: impl FnOnce(&mut [u8]) -> Result<usize, Error>,
184) -> Result<Vec<u8>, Error> {
185 charge(bound, input, workspace)?;
186 let mut out = Vec::new();
187 out.try_reserve_exact(bound)
188 .map_err(|_| Error::AllocationFailed)?;
189 if out.capacity() != bound {
190 workspace.account_codec(out.capacity())?;
191 }
192 out.resize(bound, 0);
193 let used = encode(&mut out)?;
194 out.truncate(used);
195 Ok(out)
196}
197
198fn into(
201 bound: usize,
202 capacity: usize,
203 input: usize,
204 workspace: &mut Workspace,
205 encode: impl FnOnce() -> Result<usize, Error>,
206) -> Result<usize, Error> {
207 let mut work = workspace.begin();
208 let result = (|| {
209 workspace.account_codec(0)?;
210 work.add(input)?;
211 work.add(bound.min(capacity))?;
212 encode()
213 })();
214 workspace.finish(&work);
215 result
216}
217
218pub fn encode_vertex_buffer(
227 vertices: &[u8],
228 count: usize,
229 stride: usize,
230 encoding: VertexEncoding,
231 workspace: &mut Workspace,
232) -> Result<Vec<u8>, Error> {
233 let _ = workspace.begin();
234 if vertices.len() != vertex_layout(count, stride)? {
235 return Err(Error::InvalidLayout);
236 }
237 let bound = vertex_encode::bound(count, stride)?;
238 allocate(bound, vertices.len(), workspace, |out| {
239 vertex_encode::encode(
240 out,
241 vertices,
242 count,
243 stride,
244 encoding.version,
245 encoding.level,
246 )
247 })
248}
249
250pub fn encode_vertex_buffer_into(
260 destination: &mut [u8],
261 vertices: &[u8],
262 count: usize,
263 stride: usize,
264 encoding: VertexEncoding,
265 workspace: &mut Workspace,
266) -> Result<usize, Error> {
267 let _ = workspace.begin();
268 if vertices.len() != vertex_layout(count, stride)? {
269 return Err(Error::InvalidLayout);
270 }
271 let bound = vertex_encode::bound(count, stride)?;
272 let capacity = destination.len();
273 into(bound, capacity, vertices.len(), workspace, || {
274 vertex_encode::encode(
275 destination,
276 vertices,
277 count,
278 stride,
279 encoding.version,
280 encoding.level,
281 )
282 })
283}
284
285fn max_vertices(indices: &[u32]) -> u64 {
289 if indices.is_empty() {
290 return 0;
291 }
292 u64::from(indices.iter().fold(0, |a, &b| a | b)) + 1
293}
294
295#[inline]
303pub fn encode_index_buffer_generic<I: crate::Index>(
304 indices: &[I],
305 encoding: IndexEncoding,
306 workspace: &mut Workspace,
307) -> Result<Vec<u8>, Error> {
308 I::with_input(indices, workspace, |indices, workspace| {
309 encode_index_buffer_u32(indices, encoding, workspace)
310 })
311}
312
313fn encode_index_buffer_u32(
314 indices: &[u32],
315 encoding: IndexEncoding,
316 workspace: &mut Workspace,
317) -> Result<Vec<u8>, Error> {
318 let _ = workspace.begin();
319 if !indices.len().is_multiple_of(3) {
320 return Err(Error::InvalidTopology);
321 }
322 let bound = index_encode::index_bound(indices.len(), max_vertices(indices))?;
323 let input = checked_bytes(indices.len(), 4)?;
324 allocate(bound, input, workspace, |out| {
325 index_encode::encode_index_buffer(out, indices, encoding.version)
326 })
327}
328
329#[inline]
338pub fn encode_index_buffer_into_generic<I: crate::Index>(
339 destination: &mut [u8],
340 indices: &[I],
341 encoding: IndexEncoding,
342 workspace: &mut Workspace,
343) -> Result<usize, Error> {
344 I::with_input(indices, workspace, |indices, workspace| {
345 encode_index_buffer_into_u32(destination, indices, encoding, workspace)
346 })
347}
348
349fn encode_index_buffer_into_u32(
350 destination: &mut [u8],
351 indices: &[u32],
352 encoding: IndexEncoding,
353 workspace: &mut Workspace,
354) -> Result<usize, Error> {
355 let _ = workspace.begin();
356 if !indices.len().is_multiple_of(3) {
357 return Err(Error::InvalidTopology);
358 }
359 let bound = index_encode::index_bound(indices.len(), 1 << 32)?;
360 let input = checked_bytes(indices.len(), 4)?;
361 let capacity = destination.len();
362 into(bound, capacity, input, workspace, || {
363 index_encode::encode_index_buffer(destination, indices, encoding.version)
364 })
365}
366
367#[inline]
374pub fn encode_index_sequence_generic<I: crate::Index>(
375 indices: &[I],
376 encoding: IndexEncoding,
377 workspace: &mut Workspace,
378) -> Result<Vec<u8>, Error> {
379 I::with_input(indices, workspace, |indices, workspace| {
380 encode_index_sequence_u32(indices, encoding, workspace)
381 })
382}
383
384fn encode_index_sequence_u32(
385 indices: &[u32],
386 encoding: IndexEncoding,
387 workspace: &mut Workspace,
388) -> Result<Vec<u8>, Error> {
389 let _ = workspace.begin();
390 let bound = index_encode::sequence_bound(indices.len(), max_vertices(indices))?;
391 let input = checked_bytes(indices.len(), 4)?;
392 allocate(bound, input, workspace, |out| {
393 index_encode::encode_index_sequence(out, indices, encoding.version)
394 })
395}
396
397#[inline]
406pub fn encode_index_sequence_into_generic<I: crate::Index>(
407 destination: &mut [u8],
408 indices: &[I],
409 encoding: IndexEncoding,
410 workspace: &mut Workspace,
411) -> Result<usize, Error> {
412 I::with_input(indices, workspace, |indices, workspace| {
413 encode_index_sequence_into_u32(destination, indices, encoding, workspace)
414 })
415}
416
417fn encode_index_sequence_into_u32(
418 destination: &mut [u8],
419 indices: &[u32],
420 encoding: IndexEncoding,
421 workspace: &mut Workspace,
422) -> Result<usize, Error> {
423 let _ = workspace.begin();
424 let bound = index_encode::sequence_bound(indices.len(), 1 << 32)?;
425 let input = checked_bytes(indices.len(), 4)?;
426 let capacity = destination.len();
427 into(bound, capacity, input, workspace, || {
428 index_encode::encode_index_sequence(destination, indices, encoding.version)
429 })
430}
431
432#[derive(Clone, Copy, PartialEq, Eq)]
433enum Kind {
434 Oct,
435 Quat,
436 Exp(ExpMode),
437 Color,
438}
439
440fn filter_layout(
442 kind: Kind,
443 count: usize,
444 stride: usize,
445 bits: u32,
446 data: &[f32],
447) -> Result<usize, Error> {
448 let (layout, floats, range) = match kind {
449 Kind::Oct | Kind::Color => (stride == 4 || stride == 8, 4, 2..=16),
450 Kind::Quat => (stride == 8, 4, 4..=16),
451 Kind::Exp(_) => (
452 stride > 0 && stride <= 256 && stride.is_multiple_of(4),
453 stride / 4,
454 1..=24,
455 ),
456 };
457 if !layout {
458 return Err(Error::InvalidLayout);
459 }
460 if !range.contains(&bits)
461 || (stride == 4 && bits > 8 && matches!(kind, Kind::Oct | Kind::Color))
462 {
463 return Err(Error::InvalidParameter);
464 }
465 if data.len() != checked_bytes(count, floats)? {
466 return Err(Error::InvalidLayout);
467 }
468 checked_bytes(count, stride)
469}
470
471fn filter_run(
472 kind: Kind,
473 out: &mut [u8],
474 stride: usize,
475 bits: u32,
476 data: &[f32],
477) -> Result<(), Error> {
478 match kind {
479 Kind::Oct => filter_encode::oct(out, stride, bits, data),
480 Kind::Quat => filter_encode::quat(out, bits, data),
481 Kind::Color => filter_encode::color(out, stride, bits, data),
482 Kind::Exp(mode) => return filter_encode::exp(out, stride, bits, data, mode),
483 }
484 Ok(())
485}
486
487fn filter_into(
488 kind: Kind,
489 destination: &mut [u8],
490 count: usize,
491 stride: usize,
492 bits: u32,
493 data: &[f32],
494 workspace: &mut Workspace,
495) -> Result<(), Error> {
496 filter_into_with(
497 kind,
498 destination,
499 count,
500 stride,
501 bits,
502 data,
503 workspace,
504 |out| filter_run(kind, out, stride, bits, data),
505 )
506}
507
508#[allow(clippy::too_many_arguments)]
511fn filter_into_with(
512 kind: Kind,
513 destination: &mut [u8],
514 count: usize,
515 stride: usize,
516 bits: u32,
517 data: &[f32],
518 workspace: &mut Workspace,
519 run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
520) -> Result<(), Error> {
521 let mut work = workspace.begin();
522 let result = (|| {
523 let bytes = filter_layout(kind, count, stride, bits, data)?;
524 if destination.len() < bytes {
525 return Err(Error::BufferTooSmall);
526 }
527 workspace.account_codec(0)?;
528 work.add(data.len())?;
529 work.add(bytes / 4)?;
530 run(&mut destination[..bytes])
531 })();
532 workspace.finish(&work);
533 result
534}
535
536fn filter_vec(
537 kind: Kind,
538 count: usize,
539 stride: usize,
540 bits: u32,
541 data: &[f32],
542 workspace: &mut Workspace,
543) -> Result<Vec<u8>, Error> {
544 filter_vec_with(kind, count, stride, bits, data, workspace, |out| {
545 filter_run(kind, out, stride, bits, data)
546 })
547}
548
549fn filter_vec_with(
550 kind: Kind,
551 count: usize,
552 stride: usize,
553 bits: u32,
554 data: &[f32],
555 workspace: &mut Workspace,
556 run: impl FnOnce(&mut [u8]) -> Result<(), Error>,
557) -> Result<Vec<u8>, Error> {
558 let mut work = workspace.begin();
559 let check = (|| {
560 let bytes = filter_layout(kind, count, stride, bits, data)?;
561 workspace.account_codec(bytes)?;
562 work.add(data.len())?;
563 work.add(bytes / 4)?;
564 Ok(bytes)
565 })();
566 workspace.finish(&work);
567 let bytes = check?;
568 let mut out = Vec::new();
569 out.try_reserve_exact(bytes)
570 .map_err(|_| Error::AllocationFailed)?;
571 if out.capacity() != bytes {
572 workspace.account_codec(out.capacity())?;
573 }
574 out.resize(bytes, 0);
575 run(&mut out)?;
576 Ok(out)
577}
578
579pub fn encode_filter_oct(
587 count: usize,
588 stride: usize,
589 bits: u32,
590 data: &[f32],
591 workspace: &mut Workspace,
592) -> Result<Vec<u8>, Error> {
593 filter_vec_with(Kind::Oct, count, stride, bits, data, workspace, |out| {
594 filter_encode::oct(out, stride, bits, data);
595 Ok(())
596 })
597}
598pub fn encode_filter_oct_into(
605 destination: &mut [u8],
606 count: usize,
607 stride: usize,
608 bits: u32,
609 data: &[f32],
610 workspace: &mut Workspace,
611) -> Result<(), Error> {
612 filter_into_with(
613 Kind::Oct,
614 destination,
615 count,
616 stride,
617 bits,
618 data,
619 workspace,
620 |out| {
621 filter_encode::oct(out, stride, bits, data);
622 Ok(())
623 },
624 )
625}
626pub fn encode_filter_quat(
633 count: usize,
634 stride: usize,
635 bits: u32,
636 data: &[f32],
637 workspace: &mut Workspace,
638) -> Result<Vec<u8>, Error> {
639 filter_vec_with(Kind::Quat, count, stride, bits, data, workspace, |out| {
640 filter_encode::quat(out, bits, data);
641 Ok(())
642 })
643}
644pub fn encode_filter_quat_into(
651 destination: &mut [u8],
652 count: usize,
653 stride: usize,
654 bits: u32,
655 data: &[f32],
656 workspace: &mut Workspace,
657) -> Result<(), Error> {
658 filter_into_with(
659 Kind::Quat,
660 destination,
661 count,
662 stride,
663 bits,
664 data,
665 workspace,
666 |out| {
667 filter_encode::quat(out, bits, data);
668 Ok(())
669 },
670 )
671}
672pub fn encode_filter_exp(
682 count: usize,
683 stride: usize,
684 bits: u32,
685 data: &[f32],
686 mode: ExpMode,
687 workspace: &mut Workspace,
688) -> Result<Vec<u8>, Error> {
689 filter_vec(Kind::Exp(mode), count, stride, bits, data, workspace)
690}
691pub fn encode_filter_exp_into(
698 destination: &mut [u8],
699 count: usize,
700 stride: usize,
701 bits: u32,
702 data: &[f32],
703 mode: ExpMode,
704 workspace: &mut Workspace,
705) -> Result<(), Error> {
706 filter_into(
707 Kind::Exp(mode),
708 destination,
709 count,
710 stride,
711 bits,
712 data,
713 workspace,
714 )
715}
716pub fn encode_filter_color(
723 count: usize,
724 stride: usize,
725 bits: u32,
726 data: &[f32],
727 workspace: &mut Workspace,
728) -> Result<Vec<u8>, Error> {
729 filter_vec(Kind::Color, count, stride, bits, data, workspace)
730}
731pub fn encode_filter_color_into(
738 destination: &mut [u8],
739 count: usize,
740 stride: usize,
741 bits: u32,
742 data: &[f32],
743 workspace: &mut Workspace,
744) -> Result<(), Error> {
745 filter_into(
746 Kind::Color,
747 destination,
748 count,
749 stride,
750 bits,
751 data,
752 workspace,
753 )
754}
755
756#[inline]
758pub fn encode_index_buffer(
759 indices: &[u32],
760 encoding: IndexEncoding,
761 workspace: &mut Workspace,
762) -> Result<Vec<u8>, Error> {
763 encode_index_buffer_generic::<u32>(indices, encoding, workspace)
764}
765
766#[inline]
768pub fn encode_index_buffer_into(
769 destination: &mut [u8],
770 indices: &[u32],
771 encoding: IndexEncoding,
772 workspace: &mut Workspace,
773) -> Result<usize, Error> {
774 encode_index_buffer_into_generic::<u32>(destination, indices, encoding, workspace)
775}
776
777#[inline]
779pub fn encode_index_sequence(
780 indices: &[u32],
781 encoding: IndexEncoding,
782 workspace: &mut Workspace,
783) -> Result<Vec<u8>, Error> {
784 encode_index_sequence_generic::<u32>(indices, encoding, workspace)
785}
786
787#[inline]
789pub fn encode_index_sequence_into(
790 destination: &mut [u8],
791 indices: &[u32],
792 encoding: IndexEncoding,
793 workspace: &mut Workspace,
794) -> Result<usize, Error> {
795 encode_index_sequence_into_generic::<u32>(destination, indices, encoding, workspace)
796}