spirv_backend: strip out the glam stuff, except for the lib.rs interface

This commit is contained in:
2022-07-18 01:48:19 -07:00
parent d92089ae23
commit 4378f33eb9
4 changed files with 67 additions and 83 deletions

View File

@@ -1,4 +1,3 @@
use spirv_std::glam::{self, UVec3};
use coremem_types::vec;
use coremem_types::vecu;
@@ -13,25 +12,6 @@ pub struct XYZStd<T>(T, T, T);
pub type Vec3Std = vec::Vec3<f32>;
pub type UVec3Std = vecu::Vec3u;
pub trait IntoGlam {
type Output;
fn into_glam(self) -> Self::Output;
}
impl IntoGlam for Vec3Std {
type Output = glam::Vec3;
fn into_glam(self) -> Self::Output {
Self::Output::new(self.x(), self.y(), self.z())
}
}
impl IntoGlam for UVec3Std {
type Output = glam::UVec3;
fn into_glam(self) -> Self::Output {
Self::Output::new(self.x(), self.y(), self.z())
}
}
// const NULL_VEC3STD: *const Vec3Std = core::ptr::null();
// const NULL_VEC3STD_MUT: *mut Vec3Std = core::ptr::null_mut();
// impl Nullable for Vec3Std {
@@ -297,15 +277,15 @@ impl<'a, T: Copy> ArrayHandleMut<'a, T> {
#[derive(Clone, Copy)]
pub struct Array3<'a, T> {
data: &'a UnsizedArray<T>,
dim: UVec3,
dim: UVec3Std,
}
fn index(loc: UVec3, dim: UVec3) -> usize {
((loc.z*dim.y + loc.y)*dim.x + loc.x) as usize
fn index(loc: UVec3Std, dim: UVec3Std) -> usize {
((loc.z()*dim.y() + loc.y())*dim.x() + loc.x()) as usize
}
fn checked_index(idx: UVec3, dim: UVec3) -> Optional<usize> {
if idx.x < dim.x && idx.y < dim.y && idx.z < dim.z {
fn checked_index(idx: UVec3Std, dim: UVec3Std) -> Optional<usize> {
if idx.x() < dim.x() && idx.y() < dim.y() && idx.z() < dim.z() {
let flat_idx = index(idx, dim);
Optional::some(flat_idx)
} else {
@@ -314,18 +294,18 @@ fn checked_index(idx: UVec3, dim: UVec3) -> Optional<usize> {
}
impl<'a, T> Array3<'a, T> {
pub fn new(data: &'a UnsizedArray<T>, dim: UVec3) -> Self {
pub fn new(data: &'a UnsizedArray<T>, dim: UVec3Std) -> Self {
Self {
data,
dim,
}
}
pub fn index(self, idx: UVec3) -> Optional<usize> {
pub fn index(self, idx: UVec3Std) -> Optional<usize> {
checked_index(idx, self.dim)
}
pub fn into_handle(self, idx: UVec3) -> ArrayHandle<'a, T> {
pub fn into_handle(self, idx: UVec3Std) -> ArrayHandle<'a, T> {
let idx = checked_index(idx, self.dim).unwrap();
unsafe {
self.data.get_handle(idx)
@@ -334,7 +314,7 @@ impl<'a, T> Array3<'a, T> {
}
impl<'a, T: Copy + Default> Array3<'a, T> {
pub fn get(&self, idx: UVec3) -> Optional<T> {
pub fn get(&self, idx: UVec3Std) -> Optional<T> {
let idx = self.index(idx);
if idx.is_some() {
Optional::some(unsafe {
@@ -347,7 +327,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
}
// impl<'a, T> Array3<'a, T> {
// pub fn get_ref(&self, idx: UVec3) -> OptionalRef<'a, T> {
// pub fn get_ref(&self, idx: UVec3Std) -> OptionalRef<'a, T> {
// OptionalRef::some(unsafe {
// self.data.index(0)
// })
@@ -364,7 +344,7 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
// }
// impl<'a> Array3<'a, Vec3Std> {
// pub fn get(&self, idx: UVec3) -> Vec3Std {
// pub fn get(&self, idx: UVec3Std) -> Vec3Std {
// let flat_idx = self.index(idx);
// (self.data[0].0, 0.0, 0.0)
// // let idx = self.index(idx);
@@ -380,11 +360,11 @@ impl<'a, T: Copy + Default> Array3<'a, T> {
/// 3d dynamically-sized array backed by a mutably borrowed buffer.
pub struct Array3Mut<'a, T> {
data: &'a mut UnsizedArray<T>,
dim: UVec3,
dim: UVec3Std,
}
impl<'a, T> Array3Mut<'a, T> {
pub fn new(data: &'a mut UnsizedArray<T>, dim: UVec3) -> Self {
pub fn new(data: &'a mut UnsizedArray<T>, dim: UVec3Std) -> Self {
Self {
data,
dim,
@@ -395,8 +375,8 @@ impl<'a, T> Array3Mut<'a, T> {
// Array3::new(self.data, self.dim)
// }
pub fn index(&self, idx: UVec3) -> Optional<usize> {
if idx.x < self.dim.x && idx.y < self.dim.y && idx.z < self.dim.z {
pub fn index(&self, idx: UVec3Std) -> Optional<usize> {
if idx.x() < self.dim.x() && idx.y() < self.dim.y() && idx.z() < self.dim.z() {
let flat_idx = index(idx, self.dim);
Optional::some(flat_idx)
} else {
@@ -406,19 +386,19 @@ impl<'a, T> Array3Mut<'a, T> {
}
impl<'a, T: Copy> Array3Mut<'a, T> {
pub fn into_mut_handle(self, idx: UVec3) -> ArrayHandleMut<'a, T> {
pub fn into_mut_handle(self, idx: UVec3Std) -> ArrayHandleMut<'a, T> {
let idx = self.index(idx).unwrap();
unsafe {
self.data.get_handle_mut(idx)
}
}
// fn index(&self, idx: UVec3) -> Optional<usize> {
// fn index(&self, idx: UVec3Std) -> Optional<usize> {
// self.as_array3().index(idx)
// }
// // pub fn get(&self, idx: UVec3) -> Option<T> {
// // pub fn get(&self, idx: UVec3Std) -> Option<T> {
// // self.get_ref(idx).cloned()
// // }
// pub fn get_ref<'b>(&'b self, idx: UVec3) -> OptionalRef<'b, T> {
// pub fn get_ref<'b>(&'b self, idx: UVec3Std) -> OptionalRef<'b, T> {
// let idx = self.index(idx);
// if idx.is_some() {
// OptionalRef::some(&self.data[idx.unwrap()])
@@ -426,15 +406,15 @@ impl<'a, T: Copy> Array3Mut<'a, T> {
// OptionalRef::none()
// }
// }
// pub fn get_mut(&mut self, idx: UVec3) -> Option<T> {
// pub fn get_mut(&mut self, idx: UVec3Std) -> Option<T> {
// self.get_ref_mut(idx).cloned()
// }
// pub fn get_ref_mut<'b>(&'b mut self, idx: UVec3) -> Option<&'b mut T> {
// pub fn get_ref_mut<'b>(&'b mut self, idx: UVec3Std) -> Option<&'b mut T> {
// self.data.get_mut(index(idx, self.dim))
// }
// /// Like `get_ref_mut`, but imposes fewer lifetime requirements by consuming `self`.
// pub fn into_ref_mut(self, idx: UVec3) -> Option<&'a mut T> {
// pub fn into_ref_mut(self, idx: UVec3Std) -> Option<&'a mut T> {
// self.data.get_mut(index(idx, self.dim))
// }
}
@@ -445,16 +425,16 @@ mod test {
use super::*;
#[test]
fn test_index() {
let dim = UVec3::new(2, 3, 7);
assert_eq!(index(UVec3::new(0, 0, 0), dim), 0);
assert_eq!(index(UVec3::new(1, 0, 0), dim), 1);
assert_eq!(index(UVec3::new(0, 1, 0), dim), 2);
assert_eq!(index(UVec3::new(1, 1, 0), dim), 3);
assert_eq!(index(UVec3::new(0, 2, 0), dim), 4);
assert_eq!(index(UVec3::new(0, 0, 1), dim), 6);
assert_eq!(index(UVec3::new(1, 0, 1), dim), 7);
assert_eq!(index(UVec3::new(0, 1, 1), dim), 8);
assert_eq!(index(UVec3::new(1, 2, 1), dim), 11);
assert_eq!(index(UVec3::new(1, 2, 2), dim), 17);
let dim = UVec3Std::new(2, 3, 7);
assert_eq!(index(UVec3Std::new(0, 0, 0), dim), 0);
assert_eq!(index(UVec3Std::new(1, 0, 0), dim), 1);
assert_eq!(index(UVec3Std::new(0, 1, 0), dim), 2);
assert_eq!(index(UVec3Std::new(1, 1, 0), dim), 3);
assert_eq!(index(UVec3Std::new(0, 2, 0), dim), 4);
assert_eq!(index(UVec3Std::new(0, 0, 1), dim), 6);
assert_eq!(index(UVec3Std::new(1, 0, 1), dim), 7);
assert_eq!(index(UVec3Std::new(0, 1, 1), dim), 8);
assert_eq!(index(UVec3Std::new(1, 2, 1), dim), 11);
assert_eq!(index(UVec3Std::new(1, 2, 2), dim), 17);
}
}