Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed May 14, 2024
1 parent a002c7a commit 0025d69
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 47 deletions.
18 changes: 18 additions & 0 deletions DEVLOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# devlog

## Tue May 14, 2024

### Website!

Part of the NLnet work is setting up a website to host this devlog, docs, guides etc.
So yesterday I bought a domain - [renderling.xyz](https://renderling.xyz)!
I figured since renderling is a 3d renderer `.xyz` was a good choice. It was either that
or `.rs`, but I do my domains through AWS Route53 which doesn't support `.rs` as a TLD.
Also I hope that this library gets used by a wider audience than just the Rust community.
I have plans to write bindings at some point, afterall.

### `naga` SPIR-V atomics support

I opened my first PR into `wgpu` [to add support for atomics in the SPIR-V frontend](https://github.com/gfx-rs/wgpu/pull/5702).
This is the first of many PRs and this is the main focus of the NLnet work.
The PR itself is more of a sanity check that I'm "doing things right". I figured I'd
open it early since I'm unfamiliar with the way `naga` does things.

## Sun May 12, 2024

### More Fox Skinning
Expand Down
22 changes: 22 additions & 0 deletions crates/renderling/src/linkage/test_atomic_i_increment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(dead_code)]
//! Automatically generated with `cd shaders && cargo run --release`.
//!
//! Provides the shader linkage for
//! [stage::test_atomic_i_increment](crate::stage::test_atomic_i_increment).
//!
//! **source path**:
//! `crates/renderling/src/linkage/stage-test_atomic_i_increment.spv`
use super::ShaderLinkage;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
pub const ENTRY_POINT: &str = "stage::test_atomic_i_increment";
#[cfg(target_arch = "wasm32")]
pub const ENTRY_POINT: &str = "stagetest_atomic_i_increment";
pub fn linkage(device: &wgpu::Device) -> ShaderLinkage {
ShaderLinkage {
module: Arc::new(
device.create_shader_module(wgpu::include_spirv!("stage-test_atomic_i_increment.spv")),
),
entry_point: ENTRY_POINT,
}
}
40 changes: 34 additions & 6 deletions crates/renderling/src/slab/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ impl Range {
pub fn intersects(&self, other: &Range) -> bool {
!(self.first_index > other.last_index || self.last_index < other.first_index)
}

pub fn contiguous(&self, range: &Range) -> bool {
self.last_index + 1 == range.first_index || self.first_index == range.last_index + 1
}
}

trait IsRange {
Expand All @@ -61,7 +57,7 @@ impl IsRange for Range {
"{self:?} intersects existing {other:?}, should never happen with Range"
);

self.contiguous(&other)
self.last_index + 1 == other.first_index || self.first_index == other.last_index + 1
}

fn union(&mut self, other: Self) {
Expand Down Expand Up @@ -462,6 +458,15 @@ impl SlabAllocator {
new_buffer
}

/// Defragments the internal "recycle" buffer.
pub fn defrag(&self) {
// UNWRAP: panic on purpose
let mut recycle_guard = self.recycles.write().unwrap();
for range in std::mem::take(&mut recycle_guard.ranges) {
recycle_guard.add_range(range);
}
}

/// Read the range of data from the slab.
///
/// This is primarily used for debugging.
Expand Down Expand Up @@ -972,10 +977,13 @@ mod test {
fn slab_manager_sanity() {
let r = Context::headless(1, 1);
let mut m = SlabAllocator::default();
log::info!("allocating 4 unused u32 slots");
let _ = m.allocate::<u32>();
let _ = m.allocate::<u32>();
let _ = m.allocate::<u32>();
let _ = m.allocate::<u32>();

log::info!("creating 4 update sources");
let h4 = m.new_value(0u32);
let h5 = m.new_value(0u32);
let h6 = m.new_value(0u32);
Expand All @@ -987,7 +995,11 @@ mod test {
wgpu::BufferUsages::empty(),
);
assert!(m.recycles.read().unwrap().ranges.is_empty());
assert_eq!(4, m.update_sources.read().unwrap().len());
let k = m.update_k.load(Ordering::Relaxed);
assert_eq!(4, k);

log::info!("dropping 4 update sources");
drop(h4);
drop(h5);
drop(h6);
Expand All @@ -999,13 +1011,20 @@ mod test {
wgpu::BufferUsages::empty(),
);
assert_eq!(1, m.recycles.read().unwrap().ranges.len());
assert!(m.update_sources.read().unwrap().is_empty());

log::info!("creating 4 update sources, round two");
let h4 = m.new_value(0u32);
let h5 = m.new_value(0u32);
let h6 = m.new_value(0u32);
let h7 = m.new_value(0u32);
assert!(m.recycles.read().unwrap().ranges.is_empty());
assert_eq!(4, m.update_sources.read().unwrap().len());
let k = m.update_k.load(Ordering::Relaxed);
// MAYBE_TODO: recycle "update_k"s instead of incrementing for each new source
assert_eq!(8, k);

log::info!("creating one more update source, immediately dropping it and two others");
let h8 = m.new_value(0u32);
drop(h8);
drop(h4);
Expand All @@ -1017,6 +1036,9 @@ mod test {
wgpu::BufferUsages::empty(),
);
assert_eq!(3, m.recycles.read().unwrap().ranges.len());
assert_eq!(2, m.update_sources.read().unwrap().len());
assert_eq!(9, m.update_k.load(Ordering::Relaxed));

drop(h7);
drop(h5);
let _ = m.upkeep(
Expand All @@ -1025,6 +1047,12 @@ mod test {
None,
wgpu::BufferUsages::empty(),
);
assert_eq!(1, m.recycles.read().unwrap().ranges.len());
m.defrag();
assert_eq!(
1,
m.recycles.read().unwrap().ranges.len(),
"ranges: {:#?}",
m.recycles.read().unwrap().ranges
);
}
}
54 changes: 13 additions & 41 deletions crates/renderling/src/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,47 +401,19 @@ pub fn renderlet_fragment(
log.write(debug);
}

//#[spirv(compute(threads(32)))]
///// Compute the draw calls for this frame.
/////
///// This should be called with `groupcount = (entities.len() / threads) + 1`.
//pub fn compute_cull_entities(
// #[spirv(storage_buffer, descriptor_set = 0, binding = 2)] entities:
// &[GpuEntity], #[spirv(storage_buffer, descriptor_set = 1, binding = 0)]
// draws: &mut [DrawIndirect], #[spirv(global_invocation_id)] global_id:
// UVec3,
//) {
// let i = global_id.x as usize;
//
// if i > entities.len() {
// return;
// }
//
// // when the vertex count and/or instance count is 0, it effectively
// filters // the draw call
// let mut call = DrawIndirect {
// vertex_count: 0,
// instance_count: 0,
// base_vertex: 0,
// base_instance: i as u32,
// };
// let entity = &entities[i];
// let is_visible = entity.visible != 0;
// if entity.is_alive() && is_visible {
// //// once naga supports atomics we can use this to compact the array
// // let index = unsafe {
// // spirv_std::arch::atomic_i_increment::<
// // u32,
// // { spirv_std::memory::Scope::Device as u32 },
// // { spirv_std::memory::Semantics::NONE.bits() as u32 },
// // >(count)
// //};
// call.instance_count = 1;
// call.base_vertex = entity.mesh_first_vertex;
// call.vertex_count = entity.mesh_vertex_count;
// }
// draws[i] = call;
//}
#[cfg(feature = "test_atomic_i_increment")]
#[spirv(compute(threads(32)))]
pub fn test_atomic_i_increment(
#[spirv(storage_buffer, descriptor_set = 0, binding = 0)] global_index: &mut u32,
) {
let _ = unsafe {
spirv_std::arch::atomic_i_increment::<
u32,
{ spirv_std::memory::Scope::Workgroup as u32 },
{ spirv_std::memory::Semantics::NONE.bits() as u32 },
>(global_index)
};
}

#[cfg(feature = "test_i8_16_extraction")]
#[spirv(compute(threads(32)))]
Expand Down

0 comments on commit 0025d69

Please sign in to comment.