move the dimensioned indexing out of spirv_backend and into coremem_types

this allows us to use it from the CPU implementation.
This commit is contained in:
2022-07-26 18:03:21 -07:00
parent d93d14d260
commit dbd666d272
4 changed files with 116 additions and 133 deletions

View File

@@ -1,8 +1,9 @@
use core::ops::{Index, IndexMut};
use spirv_std::RuntimeArray;
// TODO: remove this re-export
pub use coremem_types::dim::DimensionedSlice;
use coremem_types::vec::Vec3u;
use spirv_std::RuntimeArray;
pub struct SizedArray<T> {
items: T,
@@ -45,62 +46,3 @@ impl<'a, T> IndexMut<usize> for SizedArray<&'a mut RuntimeArray<T>> {
}
}
pub struct DimensionedSlice<T> {
dim: Vec3u,
items: T,
}
impl<T> DimensionedSlice<T> {
pub fn new(dim: Vec3u, items: T) -> Self {
Self { dim, items }
}
}
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a T> {
type Output=T::Output;
fn index(&self, idx: Vec3u) -> &Self::Output {
let idx = index(idx, self.dim);
&self.items[idx]
}
}
impl<'a, T: Index<usize> + ?Sized> Index<Vec3u> for DimensionedSlice<&'a mut T> {
type Output=T::Output;
fn index(&self, idx: Vec3u) -> &Self::Output {
let idx = index(idx, self.dim);
&self.items[idx]
}
}
impl<'a, T: IndexMut<usize> + ?Sized> IndexMut<Vec3u> for DimensionedSlice<&'a mut T> {
fn index_mut(&mut self, idx: Vec3u) -> &mut Self::Output {
let idx = index(idx, self.dim);
&mut self.items[idx]
}
}
fn index(loc: Vec3u, dim: Vec3u) -> usize {
((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_index() {
let dim = Vec3u::new(2, 3, 7);
assert_eq!(index(Vec3u::new(0, 0, 0), dim), 0);
assert_eq!(index(Vec3u::new(1, 0, 0), dim), 1);
assert_eq!(index(Vec3u::new(0, 1, 0), dim), 2);
assert_eq!(index(Vec3u::new(1, 1, 0), dim), 3);
assert_eq!(index(Vec3u::new(0, 2, 0), dim), 4);
assert_eq!(index(Vec3u::new(0, 0, 1), dim), 6);
assert_eq!(index(Vec3u::new(1, 0, 1), dim), 7);
assert_eq!(index(Vec3u::new(0, 1, 1), dim), 8);
assert_eq!(index(Vec3u::new(1, 2, 1), dim), 11);
assert_eq!(index(Vec3u::new(1, 2, 2), dim), 17);
}
}