|
| 1 | +//! Index buffer types |
| 2 | +use std::{fmt::Debug, io, marker::PhantomData, mem::size_of}; |
| 3 | + |
| 4 | +/// Trait to read from an index buffer, in a given format. (u16, u32) |
| 5 | +pub trait Format { |
| 6 | + type Item; |
| 7 | + #[must_use] |
| 8 | + fn get(buf: &[u8], index: usize) -> Self::Item; |
| 9 | +} |
| 10 | + |
| 11 | +/// Wrapper around a raw buffer of indices, supporting either u16 or u32 indices. |
| 12 | +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 13 | +pub struct IndexBuffer<F: Format> { |
| 14 | + count: usize, |
| 15 | + |
| 16 | + buffer: Vec<u8>, |
| 17 | + |
| 18 | + _format: PhantomData<F>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<F: Format> Debug for IndexBuffer<F> { |
| 22 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 23 | + f.debug_struct("IndexBuffer") |
| 24 | + .field("count", &self.count) |
| 25 | + .field("stride", &self.stride()) |
| 26 | + .field("buffer (size)", &self.buffer.len()) |
| 27 | + .finish() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl Format for u32 { |
| 32 | + type Item = u32; |
| 33 | + fn get(buf: &[u8], index: usize) -> u32 { |
| 34 | + let off = index * size_of::<u32>(); |
| 35 | + u32::from_le_bytes(buf[off..off + 4].try_into().unwrap()) |
| 36 | + } |
| 37 | +} |
| 38 | +impl Format for u16 { |
| 39 | + type Item = u16; |
| 40 | + fn get(buf: &[u8], index: usize) -> u16 { |
| 41 | + let off = index * size_of::<u16>(); |
| 42 | + u16::from_le_bytes(buf[off..off + 2].try_into().unwrap()) |
| 43 | + } |
| 44 | +} |
| 45 | +impl<F: Format> IndexBuffer<F> { |
| 46 | + #[must_use] |
| 47 | + /// Creates a new index buffer from a buffer |
| 48 | + pub fn new(buffer: Vec<u8>) -> Self { |
| 49 | + let stride = size_of::<F>(); |
| 50 | + if buffer.len() % stride != 0 { |
| 51 | + panic!("Index buffer size must be a multiple of index size!"); |
| 52 | + } |
| 53 | + Self { |
| 54 | + count: buffer.len() / stride, |
| 55 | + buffer, |
| 56 | + _format: PhantomData, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Reads an index buffer from a reader. |
| 61 | + /// |
| 62 | + /// # Arguments |
| 63 | + /// * `reader` - The reader to read from. |
| 64 | + /// * `count` - The number of indices to read. |
| 65 | + pub fn read<R: io::Read>(reader: &mut R, count: usize) -> Result<Self, io::Error> { |
| 66 | + let mut buffer = vec![0u8; size_of::<F>() * count]; |
| 67 | + reader.read_exact(&mut buffer)?; |
| 68 | + Ok(Self::new(buffer)) |
| 69 | + } |
| 70 | + |
| 71 | + #[inline(always)] |
| 72 | + #[must_use] |
| 73 | + /// The size in bytes of a single index. |
| 74 | + pub fn stride(&self) -> usize { |
| 75 | + size_of::<F>() |
| 76 | + } |
| 77 | + |
| 78 | + #[inline(always)] |
| 79 | + #[must_use] |
| 80 | + /// An iterator over the indices in the buffer. |
| 81 | + pub fn iter(&self) -> IndexBufferIter<F> { |
| 82 | + IndexBufferIter { |
| 83 | + buffer: self, |
| 84 | + counter: 0, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[inline] |
| 89 | + #[must_use] |
| 90 | + /// Get an item from the buffer, at a given index. |
| 91 | + pub fn get(&self, index: usize) -> F::Item { |
| 92 | + F::get(&self.buffer, index) |
| 93 | + } |
| 94 | + |
| 95 | + #[inline(always)] |
| 96 | + #[must_use] |
| 97 | + /// The number of indices in the buffer. |
| 98 | + pub fn count(&self) -> usize { |
| 99 | + self.count |
| 100 | + } |
| 101 | + |
| 102 | + #[inline(always)] |
| 103 | + #[must_use] |
| 104 | + /// The raw underlying bytes. |
| 105 | + pub fn as_bytes(&self) -> &[u8] { |
| 106 | + &self.buffer |
| 107 | + } |
| 108 | + #[inline(always)] |
| 109 | + #[must_use] |
| 110 | + /// Take ownership of the underlying bytes. |
| 111 | + pub fn into_inner(self) -> Vec<u8> { |
| 112 | + self.buffer |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +pub struct IndexBufferIter<'a, F: Format> { |
| 117 | + buffer: &'a IndexBuffer<F>, |
| 118 | + counter: usize, |
| 119 | +} |
| 120 | + |
| 121 | +impl<'a, F: Format> Iterator for IndexBufferIter<'a, F> { |
| 122 | + type Item = F::Item; |
| 123 | + |
| 124 | + fn next(&mut self) -> Option<Self::Item> { |
| 125 | + if self.counter >= self.buffer.count { |
| 126 | + return None; |
| 127 | + } |
| 128 | + let item = F::get(self.buffer.as_bytes(), self.counter); |
| 129 | + self.counter += 1; |
| 130 | + Some(item) |
| 131 | + } |
| 132 | +} |
0 commit comments