Skip to content

Commit 361b347

Browse files
committed
finished porting tests to gltf-on-the-slab
1 parent 9ffd823 commit 361b347

File tree

14 files changed

+886
-934
lines changed

14 files changed

+886
-934
lines changed

crates/renderling-derive/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ pub fn derive_from_slab(input: proc_macro::TokenStream) -> proc_macro::TokenStre
129129
FieldName::Ident(field) => Ident::new(&format!("offset_of_{}", field), field.span()),
130130
};
131131
offsets.push(quote! {
132-
pub fn #ident() -> usize {
133-
#(<#offset_tys as renderling_shader::slab::Slabbed>::slab_size()+)*
134-
0
132+
pub fn #ident() -> renderling_shader::id::Offset<#ty> {
133+
renderling_shader::id::Offset::new(
134+
#(<#offset_tys as renderling_shader::slab::Slabbed>::slab_size()+)*
135+
0
136+
)
135137
}
136138
});
137139
offset_tys.push(ty.clone());

crates/renderling-shader/src/bits.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,4 @@ mod test {
340340
[a, b, c, d, e, f, g]
341341
);
342342
}
343-
344-
#[test]
345-
fn indices_sanity() {
346-
let slab: [u32; 20] = [
347-
65536, 2, 0, 0, 0, 1065353216, 0, 0, 0, 1065353216, 0, 0, 0, 1065353216, 0, 0,
348-
1065353216, 0, 0, 1065353216,
349-
];
350-
let u32_index = 9usize;
351-
let byte_offset = 0usize;
352-
let (a, u32_index, byte_offset) = extract_u32(u32_index, byte_offset, &slab);
353-
let (b, u32_index, byte_offset) = extract_u32(u32_index, byte_offset, &slab);
354-
let (c, u32_index, byte_offset) = extract_u32(u32_index, byte_offset, &slab);
355-
}
356343
}

crates/renderling-shader/src/id.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,66 @@ impl<T> Id<T> {
141141
}
142142
}
143143

144+
/// The offset of a field relative a parent's `Id`.
145+
///
146+
/// Offset functions are automatically derived for `Slabbed` structs.
147+
///
148+
/// ```rust
149+
/// use renderling_shader::{id::{Id, Offset}, slab::{Slab, Slabbed}};
150+
///
151+
/// #[derive(Debug, Default, PartialEq, Slabbed)]
152+
/// pub struct Parent {
153+
/// pub child_a: u32,
154+
/// pub child_b: u32,
155+
/// }
156+
///
157+
/// let mut slab = [0u32; 10];
158+
///
159+
/// let parent_id = Id::new(3);
160+
/// let parent = Parent{ child_a: 0, child_b: 1 };
161+
/// slab.write(parent_id, &parent);
162+
/// assert_eq!(parent, slab.read(parent_id));
163+
///
164+
/// slab.write(parent_id + Parent::offset_of_child_a(), &42);
165+
/// let a = slab.read(parent_id + Parent::offset_of_child_a());
166+
/// assert_eq!(42, a);
167+
/// ```
168+
pub struct Offset<T> {
169+
pub offset: u32,
170+
_phantom: PhantomData<T>,
171+
}
172+
173+
impl<F, T> core::ops::Add<Id<T>> for Offset<F> {
174+
type Output = Id<F>;
175+
176+
fn add(self, rhs: Id<T>) -> Self::Output {
177+
Id::new(self.offset + rhs.0)
178+
}
179+
}
180+
181+
impl<F, T> core::ops::Add<Offset<F>> for Id<T> {
182+
type Output = Id<F>;
183+
184+
fn add(self, rhs: Offset<F>) -> Self::Output {
185+
Id::new(self.0 + rhs.offset)
186+
}
187+
}
188+
189+
impl<T> From<Offset<T>> for Id<T> {
190+
fn from(value: Offset<T>) -> Self {
191+
Id::new(value.offset)
192+
}
193+
}
194+
195+
impl<T> Offset<T> {
196+
pub const fn new(offset: usize) -> Self {
197+
Self {
198+
offset: offset as u32,
199+
_phantom: PhantomData,
200+
}
201+
}
202+
}
203+
144204
#[cfg(test)]
145205
mod test {
146206
use crate::stage::GpuEntity;

crates/renderling-shader/src/slab.rs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
//! Slab storage and ops used for storing on CPU and extracting on GPU.
1+
//! Slab storage and operations.
22
use core::marker::PhantomData;
33

44
pub use renderling_derive::Slabbed;
55

6-
use crate::id::Id;
6+
use crate::{array::Array, id::Id};
77

88
/// Determines the "size" of a type when stored in a slab of `&[u32]`,
99
/// and how to read/write it from/to the slab.
@@ -341,6 +341,7 @@ impl<T: core::any::Any> Slabbed for PhantomData<T> {
341341
}
342342
}
343343

344+
/// Trait for slabs of `u32`s that can store many types.
344345
pub trait Slab {
345346
/// Return the number of u32 elements in the slab.
346347
fn len(&self) -> usize;
@@ -366,12 +367,32 @@ pub trait Slab {
366367
/// Write the type into the slab at the index.
367368
///
368369
/// Return the next index, or the same index if writing would overlap the slab.
369-
fn write<T: Slabbed + Default>(&mut self, t: &T, index: usize) -> usize;
370+
fn write_indexed<T: Slabbed>(&mut self, t: &T, index: usize) -> usize;
370371

371372
/// Write a slice of the type into the slab at the index.
372373
///
373374
/// Return the next index, or the same index if writing would overlap the slab.
374-
fn write_slice<T: Slabbed + Default>(&mut self, t: &[T], index: usize) -> usize;
375+
fn write_indexed_slice<T: Slabbed>(&mut self, t: &[T], index: usize) -> usize;
376+
377+
/// Write the type into the slab at the position of the given `Id`.
378+
///
379+
/// This likely performs a partial write if the given `Id` is out of bounds.
380+
fn write<T: Slabbed>(&mut self, id: Id<T>, t: &T) {
381+
let _ = self.write_indexed(t, id.index());
382+
}
383+
384+
/// Write contiguous elements into the slab at the position of the given `Array`.
385+
///
386+
/// ## NOTE
387+
/// This does nothing if the length of `Array` is greater than the length of `data`.
388+
fn write_array<T: Slabbed>(&mut self, array: Array<T>, data: &[T]) {
389+
if array.len() > data.len() {
390+
return;
391+
}
392+
for i in 0..array.len() {
393+
let _ = self.write(array.at(i), &data[i]);
394+
}
395+
}
375396
}
376397

377398
impl Slab for [u32] {
@@ -385,11 +406,11 @@ impl Slab for [u32] {
385406
t
386407
}
387408

388-
fn write<T: Slabbed + Default>(&mut self, t: &T, index: usize) -> usize {
409+
fn write_indexed<T: Slabbed>(&mut self, t: &T, index: usize) -> usize {
389410
t.write_slab(index, self)
390411
}
391412

392-
fn write_slice<T: Slabbed + Default>(&mut self, t: &[T], index: usize) -> usize {
413+
fn write_indexed_slice<T: Slabbed>(&mut self, t: &[T], index: usize) -> usize {
393414
let mut index = index;
394415
for item in t {
395416
index = item.write_slab(index, self);
@@ -408,12 +429,12 @@ impl Slab for Vec<u32> {
408429
self.as_slice().read(id)
409430
}
410431

411-
fn write<T: Slabbed + Default>(&mut self, t: &T, index: usize) -> usize {
412-
self.as_mut_slice().write(t, index)
432+
fn write_indexed<T: Slabbed>(&mut self, t: &T, index: usize) -> usize {
433+
self.as_mut_slice().write_indexed(t, index)
413434
}
414435

415-
fn write_slice<T: Slabbed + Default>(&mut self, t: &[T], index: usize) -> usize {
416-
self.as_mut_slice().write_slice(t, index)
436+
fn write_indexed_slice<T: Slabbed>(&mut self, t: &[T], index: usize) -> usize {
437+
self.as_mut_slice().write_indexed_slice(t, index)
417438
}
418439
}
419440

@@ -428,16 +449,16 @@ mod test {
428449
#[test]
429450
fn slab_array_readwrite() {
430451
let mut slab = [0u32; 16];
431-
slab.write(&42, 0);
432-
slab.write(&666, 1);
452+
slab.write_indexed(&42, 0);
453+
slab.write_indexed(&666, 1);
433454
let t = slab.read(Id::<[u32; 2]>::new(0));
434455
assert_eq!([42, 666], t);
435456
let t: Vec<u32> = slab.read_vec(Array::new(0, 2));
436457
assert_eq!([42, 666], t[..]);
437-
slab.write_slice(&[1, 2, 3, 4], 2);
458+
slab.write_indexed_slice(&[1, 2, 3, 4], 2);
438459
let t: Vec<u32> = slab.read_vec(Array::new(2, 4));
439460
assert_eq!([1, 2, 3, 4], t[..]);
440-
slab.write_slice(&[[1.0, 2.0, 3.0, 4.0], [5.5, 6.5, 7.5, 8.5]], 0);
461+
slab.write_indexed_slice(&[[1.0, 2.0, 3.0, 4.0], [5.5, 6.5, 7.5, 8.5]], 0);
441462

442463
let arr = Array::<[f32; 4]>::new(0, 2);
443464
assert_eq!(Id::new(0), arr.at(0));
@@ -481,10 +502,10 @@ mod test {
481502
let mut slab = vec![0u32; geometry_slab_size + Array::<Vertex>::slab_size()];
482503
let index = 0usize;
483504
let vertices = Array::<Vertex>::new(index as u32, geometry.len() as u32);
484-
let index = slab.write_slice(&geometry, index);
505+
let index = slab.write_indexed_slice(&geometry, index);
485506
assert_eq!(geometry_slab_size, index);
486507
let vertices_id = Id::<Array<Vertex>>::from(index);
487-
let index = slab.write(&vertices, index);
508+
let index = slab.write_indexed(&vertices, index);
488509
assert_eq!(geometry_slab_size + Array::<Vertex>::slab_size(), index);
489510
assert_eq!(Vertex::slab_size() * 6, vertices_id.index());
490511
assert!(slab.contains(vertices_id),);

crates/renderling-shader/src/stage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ mod test {
15791579
b: glam::Vec2::new(3.0, 4.0),
15801580
c: glam::Vec4::new(5.0, 6.0, 7.0, 8.0),
15811581
};
1582-
let index = slab.write(&the, 0);
1582+
let index = slab.write_indexed(&the, 0);
15831583
assert_eq!(9, index);
15841584
let the2 = slab.read(Id::<TheType>::new(0));
15851585
assert_eq!(the, the2);

0 commit comments

Comments
 (0)