formatting: Using canonical formatting on whole project

This commit is contained in:
2026-11-22 17:16:32 +01:00
parent 2910314df3
commit dd0e102348
44 changed files with 1526 additions and 3376 deletions
+9 -18
View File
@@ -6,22 +6,19 @@ use wgpu::Buffer;
use vxls_profiler::memory_profiler;
/// Contains instanciated gpu buffers to hold data for cache attributes
pub struct CacheAttributes<const N: usize>
{
pub struct CacheAttributes<const N: usize> {
attribute_buffers: Vec<wgpu::Buffer>,
bind_group_layout: wgpu::BindGroupLayout,
bind_group: wgpu::BindGroup,
}
impl<const N: usize> CacheAttributes<N>
{
impl<const N: usize> CacheAttributes<N> {
pub fn from_attribute_layout(
device: &mut memory_profiler::Device,
layout: &AttributesLayout,
page_count: usize,
) -> Self
{
) -> Self {
let attribute_buffers = layout
.attributes
.iter()
@@ -61,8 +58,7 @@ impl<const N: usize> CacheAttributes<N>
pub fn create_bind_group_layout(
device: &wgpu::Device,
layout: &AttributesLayout,
) -> BindGroupLayout
{
) -> BindGroupLayout {
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("CacheAttributes::bind_group_layout"),
entries: &[wgpu::BindGroupLayoutEntry {
@@ -81,18 +77,15 @@ impl<const N: usize> CacheAttributes<N>
})
}
pub fn bind_group_layout(&self) -> &wgpu::BindGroupLayout
{
pub fn bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.bind_group_layout
}
pub fn bind_group(&self) -> &wgpu::BindGroup
{
pub fn bind_group(&self) -> &wgpu::BindGroup {
&self.bind_group
}
pub fn attribute_buffers(&self) -> &[Buffer]
{
pub fn attribute_buffers(&self) -> &[Buffer] {
&self.attribute_buffers
}
}
@@ -102,8 +95,7 @@ pub fn create_attributes_buffer_profiled<const N: usize>(
attribute_descriptor: &AttributeDescriptor,
page_count: usize,
usage: wgpu::BufferUsages,
) -> wgpu::Buffer
{
) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(
format!(
@@ -124,8 +116,7 @@ pub fn create_attributes_buffer<const N: usize>(
attribute_descriptor: &AttributeDescriptor,
page_count: usize,
usage: wgpu::BufferUsages,
) -> wgpu::Buffer
{
) -> wgpu::Buffer {
device.create_buffer(&wgpu::BufferDescriptor {
label: Some(
format!(
+6 -12
View File
@@ -20,8 +20,7 @@ use vxls_profiler::{self};
/// broken by depth, deepest first. This works because the timestamp pass is a
/// stable radix sort applied after a descending depth sort: pages that share a
/// timestamp keep the deepest-first order established by the depth pass.
pub struct CacheEvictor
{
pub struct CacheEvictor {
page_count: usize,
/// Contains a u32 timestamp per page, so that we know when a page was last used
@@ -48,14 +47,12 @@ pub struct CacheEvictor
bind_group: BindGroup,
}
impl CacheEvictor
{
impl CacheEvictor {
pub fn new(
device: &mut memory_profiler::Device,
page_count: usize,
initial_timestamp: u32,
) -> Self
{
) -> Self {
let timestamp_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("CacheUsage::timestamp_pages"),
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
@@ -213,18 +210,15 @@ impl CacheEvictor
}
}
pub fn bind_group(&self) -> &BindGroup
{
pub fn bind_group(&self) -> &BindGroup {
&self.bind_group
}
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
{
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout {
create_multi_buffers_bind_group_layout(device, 3, Some("CacheEvictor::bind_group_layout"))
}
pub fn timestamp_pages_buffer(&self) -> &wgpu::Buffer
{
pub fn timestamp_pages_buffer(&self) -> &wgpu::Buffer {
&self.timestamp_pages
}
}
+50 -111
View File
@@ -56,57 +56,38 @@ where
/// Errors reported while uploading a producer attribute slice into
/// [`CacheInsertionData`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AttributeUploadError
{
pub enum AttributeUploadError {
/// The slice is not a whole number of pages.
NotWholePages
{
voxel_count: usize, children: usize
},
NotWholePages { voxel_count: usize, children: usize },
/// The slice does not fit in this insertion batch.
TooManyVoxels
{
voxel_count: usize, capacity: usize
},
TooManyVoxels { voxel_count: usize, capacity: usize },
/// The producer declares more attributes than the cache layout has.
MissingCacheAttribute
{
index: usize
},
MissingCacheAttribute { index: usize },
/// The producer attribute index is out of bounds.
AttributeIndexOutOfBounds
{
index: usize
},
AttributeIndexOutOfBounds { index: usize },
/// The cache attribute is not `External`, so insertion will not copy it.
NotExternal
{
index: usize
},
NotExternal { index: usize },
/// Declared word counts disagree.
WordCountMismatch
{
WordCountMismatch {
index: usize,
producer: u32,
cache: u32,
},
/// Declared labels disagree (only checked when the cache label is present).
LabelMismatch
{
LabelMismatch {
index: usize,
producer: &'static str,
cache: Option<&'static str>,
},
/// A `get_attr` implementation returned the wrong number of bytes.
BadAttributeBytes
{
BadAttributeBytes {
index: usize,
expected: usize,
actual: usize,
@@ -118,18 +99,15 @@ pub fn validate_voxel_count(
voxel_count: usize,
children: usize,
capacity: usize,
) -> Result<(), AttributeUploadError>
{
if !voxel_count.is_multiple_of(children)
{
) -> Result<(), AttributeUploadError> {
if !voxel_count.is_multiple_of(children) {
return Err(AttributeUploadError::NotWholePages {
voxel_count,
children,
});
}
if voxel_count > capacity
{
if voxel_count > capacity {
return Err(AttributeUploadError::TooManyVoxels {
voxel_count,
capacity,
@@ -146,23 +124,18 @@ pub fn validate_voxel_count(
pub fn collect_attribute_bytes<A: Attributes>(
voxels: &[A],
index: usize,
) -> Result<Vec<u8>, AttributeUploadError>
{
let Some(descriptor) = A::LAYOUT.attributes.get(index)
else
{
) -> Result<Vec<u8>, AttributeUploadError> {
let Some(descriptor) = A::LAYOUT.attributes.get(index) else {
return Err(AttributeUploadError::AttributeIndexOutOfBounds { index });
};
let expected = descriptor.word_count as usize * size_of::<u32>();
let mut bytes = Vec::with_capacity(voxels.len() * expected);
for voxel in voxels
{
for voxel in voxels {
let attribute = voxel.get_attr(index);
if attribute.len() != expected
{
if attribute.len() != expected {
return Err(AttributeUploadError::BadAttributeBytes {
index,
expected,
@@ -180,8 +153,7 @@ impl<const N: usize> CacheInsertionData<N>
where
[(); N * N * N]:,
{
pub fn new(device: &wgpu::Device, max_page_count: usize, layout: &AttributesLayout) -> Self
{
pub fn new(device: &wgpu::Device, max_page_count: usize, layout: &AttributesLayout) -> Self {
let page_count_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("CacheInsertionData::page_count_buffer"),
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
@@ -330,8 +302,7 @@ where
pub fn create_bind_group_layout(
device: &wgpu::Device,
layout: Option<&AttributesLayout>,
) -> BindGroupLayout
{
) -> BindGroupLayout {
let storage_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
binding,
visibility: ShaderStages::COMPUTE,
@@ -353,8 +324,7 @@ where
storage_entry(6), // valid count
];
if let Some(layout) = layout
{
if let Some(layout) = layout {
entries.push(wgpu::BindGroupLayoutEntry {
binding: 7,
visibility: ShaderStages::COMPUTE,
@@ -373,82 +343,67 @@ where
})
}
pub fn bind_group(&self) -> &wgpu::BindGroup
{
pub fn bind_group(&self) -> &wgpu::BindGroup {
&self.bind_group
}
pub fn bind_group_no_attributes(&self) -> &wgpu::BindGroup
{
pub fn bind_group_no_attributes(&self) -> &wgpu::BindGroup {
&self.bind_group_no_attributes
}
pub fn page_count(&self) -> usize
{
pub fn page_count(&self) -> usize {
self.max_page_count
}
pub fn page_count_buffer(&self) -> &wgpu::Buffer
{
pub fn page_count_buffer(&self) -> &wgpu::Buffer {
&self.page_count_buffer
}
pub fn insertion_information_buffer(&self) -> &wgpu::Buffer
{
pub fn insertion_information_buffer(&self) -> &wgpu::Buffer {
&self.insertion_information
}
pub fn location_pages_buffer(&self) -> &wgpu::Buffer
{
pub fn location_pages_buffer(&self) -> &wgpu::Buffer {
&self.location_pages
}
pub fn destination_indices_buffer(&self) -> &wgpu::Buffer
{
pub fn destination_indices_buffer(&self) -> &wgpu::Buffer {
&self.destination_indices
}
pub fn insertion_parents_buffer(&self) -> &wgpu::Buffer
{
pub fn insertion_parents_buffer(&self) -> &wgpu::Buffer {
&self.insertion_parents
}
pub fn valid_indices_buffer(&self) -> &wgpu::Buffer
{
pub fn valid_indices_buffer(&self) -> &wgpu::Buffer {
&self.valid_indices
}
pub fn valid_count_buffer(&self) -> &wgpu::Buffer
{
pub fn valid_count_buffer(&self) -> &wgpu::Buffer {
&self.valid_count
}
pub fn attributes_buffers(&self) -> &[wgpu::Buffer]
{
pub fn attributes_buffers(&self) -> &[wgpu::Buffer] {
&self.attributes
}
/// The layout this insertion data was created from.
pub fn layout(&self) -> &AttributesLayout
{
pub fn layout(&self) -> &AttributesLayout {
&self.layout
}
/// Number of attribute buffers in this insertion batch.
pub fn attribute_count(&self) -> usize
{
pub fn attribute_count(&self) -> usize {
self.attributes.len()
}
/// The attribute buffer for `index`, if it exists.
pub fn attribute_buffer(&self, index: usize) -> Option<&wgpu::Buffer>
{
pub fn attribute_buffer(&self, index: usize) -> Option<&wgpu::Buffer> {
self.attributes.get(index)
}
/// Maximum number of voxels this insertion batch can hold.
pub fn attribute_capacity(&self) -> usize
{
pub fn attribute_capacity(&self) -> usize {
self.max_page_count * N * N * N
}
@@ -464,10 +419,8 @@ where
&self,
queue: &wgpu::Queue,
voxels: &[A],
) -> Result<usize, AttributeUploadError>
{
if A::LAYOUT != self.layout
{
) -> Result<usize, AttributeUploadError> {
if A::LAYOUT != self.layout {
return Err(AttributeUploadError::LabelMismatch {
index: 0,
producer: "..",
@@ -476,13 +429,11 @@ where
}
validate_voxel_count(voxels.len(), N * N * N, self.attribute_capacity())?;
if voxels.is_empty()
{
if voxels.is_empty() {
return Ok(0);
}
for index in 0..A::LAYOUT.len()
{
for index in 0..A::LAYOUT.len() {
let bytes = collect_attribute_bytes(voxels, index)?;
queue.write_buffer(&self.attributes[index], 0, &bytes);
}
@@ -492,8 +443,7 @@ where
}
#[cfg(test)]
mod tests
{
mod tests {
use super::*;
use vxls_attributes::Attribute;
@@ -502,18 +452,14 @@ mod tests
#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
#[repr(C)]
struct TestAttributes
{
struct TestAttributes {
color: u32,
normal: [f32; 3],
}
impl Attribute for TestAttributes
{
fn average(attributes: &[Self]) -> Self
{
if attributes.is_empty()
{
impl Attribute for TestAttributes {
fn average(attributes: &[Self]) -> Self {
if attributes.is_empty() {
return Self::default();
}
@@ -536,27 +482,22 @@ mod tests
}
}
impl Attributes for TestAttributes
{
impl Attributes for TestAttributes {
const LAYOUT: AttributesLayout = AttributesLayout::new(&[
AttributeDescriptor::new("color", 1),
AttributeDescriptor::new("normal", 3),
]);
fn get_attr(&self, index: usize) -> &[u8]
{
match index
{
fn get_attr(&self, index: usize) -> &[u8] {
match index {
0 => bytemuck::bytes_of(&self.color),
1 => bytemuck::bytes_of(&self.normal),
_ => panic!("attribute index out of bounds"),
}
}
fn get_attr_mut(&mut self, index: usize) -> &mut [u8]
{
match index
{
fn get_attr_mut(&mut self, index: usize) -> &mut [u8] {
match index {
0 => bytemuck::bytes_of_mut(&mut self.color),
1 => bytemuck::bytes_of_mut(&mut self.normal),
_ => panic!("attribute index out of bounds"),
@@ -565,8 +506,7 @@ mod tests
}
#[test]
fn collect_attribute_bytes_orders_by_voxel()
{
fn collect_attribute_bytes_orders_by_voxel() {
let voxels = [
TestAttributes {
color: 1,
@@ -594,8 +534,7 @@ mod tests
}
#[test]
fn voxel_counts_must_be_whole_pages_within_capacity()
{
fn voxel_counts_must_be_whole_pages_within_capacity() {
assert_eq!(validate_voxel_count(64, 64, 128), Ok(()));
assert_eq!(
validate_voxel_count(65, 64, 128),
+25 -50
View File
@@ -35,17 +35,14 @@ pub mod structure;
pub mod tables;
/// Stores some ressources that can be reused throughout the modules of the cache
struct CacheRessources<const N: usize>
{
struct CacheRessources<const N: usize> {
scratch_buffer: wgpu::Buffer,
scratch_buffer_bind_group: wgpu::BindGroup,
radix_sorter: RadixSorter,
}
impl<const N: usize> CacheRessources<N>
{
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self
{
impl<const N: usize> CacheRessources<N> {
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self {
let scratch_buffer = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("CacheRessources::scratch_buffer"),
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
@@ -67,8 +64,7 @@ impl<const N: usize> CacheRessources<N>
}
}
pub fn create_scratch_buffer_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout
{
pub fn create_scratch_buffer_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
create_multi_buffers_bind_group_layout(
device,
1,
@@ -76,8 +72,7 @@ impl<const N: usize> CacheRessources<N>
)
}
pub fn scratch_buffer_bind_group(&self) -> &wgpu::BindGroup
{
pub fn scratch_buffer_bind_group(&self) -> &wgpu::BindGroup {
&self.scratch_buffer_bind_group
}
}
@@ -145,8 +140,7 @@ where
page_count: usize,
max_requests: usize,
attributes_layout: &AttributesLayout,
) -> Self
{
) -> Self {
let mut cache_memory_profile = MemoryProfile::new("VoxelCache");
let mut mem_device = cache_memory_profile.begin(device);
@@ -208,8 +202,7 @@ where
device: &Device,
encoder: &mut ScopeEncoder<CommandEncoder>,
request_buffer: &CacheRequestBuffer,
)
{
) {
vxls_profiler::begin_scope!(encoder, "gather_requests");
self.request_handler.sort_requests(
@@ -242,8 +235,7 @@ where
device: &Device,
encoder: &mut ScopeEncoder<CommandEncoder>,
insertions: &mut [&mut VoxelCacheInsertion<N>],
)
{
) {
vxls_profiler::begin_scope!(encoder, "insertion");
let new_timestamp = self.frame_timestamp + 1;
@@ -256,16 +248,12 @@ where
// Validate everything before allocating any page, so stale insertions
// can never consume a recycled page.
for insertion in insertions.iter_mut()
{
match insertion
{
VoxelCacheInsertion::ExternalRequest(cache_insertion_data, invalidable) =>
{
for insertion in insertions.iter_mut() {
match insertion {
VoxelCacheInsertion::ExternalRequest(cache_insertion_data, invalidable) => {
invalidable.validate_insertion(device, &mut encoder, cache_insertion_data);
}
VoxelCacheInsertion::InternalRequest(cache_insertion_data) =>
{
VoxelCacheInsertion::InternalRequest(cache_insertion_data) => {
self.cache_insertion_pass.encode_validation(
device,
&mut encoder,
@@ -278,13 +266,10 @@ where
}
}
for insertion in insertions.iter()
{
match insertion
{
for insertion in insertions.iter() {
match insertion {
VoxelCacheInsertion::ExternalRequest(cache_insertion_data, _)
| VoxelCacheInsertion::InternalRequest(cache_insertion_data) =>
{
| VoxelCacheInsertion::InternalRequest(cache_insertion_data) => {
self.cache_insertion_pass.encode(
device,
&mut encoder,
@@ -299,10 +284,8 @@ where
}
// Invalidate external structures pointing inside the cache
for insertion in insertions.iter_mut()
{
if let VoxelCacheInsertion::ExternalRequest(_, invalidable) = insertion
{
for insertion in insertions.iter_mut() {
if let VoxelCacheInsertion::ExternalRequest(_, invalidable) = insertion {
invalidable.invalidate(&mut encoder, &self.evictor, new_timestamp);
}
}
@@ -316,10 +299,8 @@ where
);
// Write internal requests pointers
for insertion in insertions.iter_mut()
{
if let VoxelCacheInsertion::InternalRequest(insertion) = insertion
{
for insertion in insertions.iter_mut() {
if let VoxelCacheInsertion::InternalRequest(insertion) = insertion {
self.write_pointers_pass
.encode(&mut encoder, device, &self.structure, insertion);
}
@@ -329,8 +310,7 @@ where
self.frame_timestamp = new_timestamp;
}
pub fn current_timestamp(&self) -> u32
{
pub fn current_timestamp(&self) -> u32 {
self.frame_timestamp
}
@@ -340,8 +320,7 @@ where
attributes: &CacheAttributes<N>,
request_handler: &CacheRequestHandler<N>,
evictor: &CacheEvictor,
) -> (wgpu::BindGroupLayout, wgpu::BindGroup)
{
) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
let attribute_count = attributes.attribute_buffers().len();
let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("VoxelCache::raytrace_bind_group_layout"),
@@ -427,24 +406,20 @@ where
(bind_group_layout, bind_group)
}
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
{
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.raytrace_bind_group_layout
}
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
{
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup {
&self.raytrace_bind_group
}
// For debugging
pub fn usage_timestamps(&self) -> &wgpu::Buffer
{
pub fn usage_timestamps(&self) -> &wgpu::Buffer {
self.evictor.timestamp_pages_buffer()
}
pub fn size(&self) -> usize
{
pub fn size(&self) -> usize {
self.page_count
}
}
+9 -18
View File
@@ -67,8 +67,7 @@ impl<const N: usize> WriteRequestsPass<N>
where
[(); N * N * N]:,
{
pub fn new(device: &Device, page_count: usize, cache_layout: &AttributesLayout) -> Self
{
pub fn new(device: &Device, page_count: usize, cache_layout: &AttributesLayout) -> Self {
// Binds
// - structure
// - request buffer
@@ -117,8 +116,7 @@ where
cache_structure: &CacheStructure<N>,
cache_request_handler: &CacheRequestHandler<N>,
target_request_buffer: &CacheRequestBuffer,
)
{
) {
vxls_profiler::begin_scope!(encoder, "WriteRequests");
let indirect_dispatch_buffer = self.indirect_buffer.workgroup_count(
@@ -177,8 +175,7 @@ impl<const N: usize> CacheInsertionPass<N>
where
[(); N * N * N]:,
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self {
// Binds
// - structure
// - request buffer
@@ -279,8 +276,7 @@ where
cache_structure: &CacheStructure<N>,
insertion_data: &CacheInsertionData<N>,
cache_evictor: &CacheEvictor,
)
{
) {
vxls_profiler::begin_scope!(encoder, "PageInsertionValidation");
let indirect_write_dispatch = self.indirect_dispatcher.workgroup_count(
@@ -315,8 +311,7 @@ where
insertion_data: &CacheInsertionData<N>,
cache_evictor: &CacheEvictor,
timestamp: u32,
)
{
) {
vxls_profiler::begin_scope!(encoder, "PageInsertion");
let indirect_write_dispatch = self.indirect_dispatcher.workgroup_count(
@@ -362,8 +357,7 @@ impl<const N: usize> CacheInvalidationPass<N>
where
[(); N * N * N]:,
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self {
// Binds
// - structure
// - request buffer
@@ -411,8 +405,7 @@ where
cache_structure: &CacheStructure<N>,
cache_evictor: &CacheEvictor,
timestamp: u32,
)
{
) {
vxls_profiler::begin_scope!(encoder, "CacheInvalidation");
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
@@ -443,8 +436,7 @@ impl<const N: usize> WritePointersPass<N>
where
[(); N * N * N]:,
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self
{
pub fn new(device: &Device, page_count: usize, layout: &AttributesLayout) -> Self {
let bindings = [
// To find where pages are pointing
Some(&CacheStructure::<N>::create_bind_group_layout(device)),
@@ -489,8 +481,7 @@ where
device: &Device,
cache_structure: &CacheStructure<N>,
cache_insertion_data: &CacheInsertionData<N>,
)
{
) {
vxls_profiler::begin_scope!(encoder, "WritePointers");
let indirect_buffer = self.indirect_buffer.workgroup_count(
+23 -46
View File
@@ -23,8 +23,7 @@ use vxls_profiler::memory_profiler;
use vxls_profiler::{self};
/// Records, handles, and produces a list of requested data inside the cache
pub struct CacheRequestHandler<const N: usize>
{
pub struct CacheRequestHandler<const N: usize> {
/// Contains a u32 per pointer of the cache, that records an amount of request by the cache user.
/// This can then be used to prioritize request based on demand
request_counts_pages: wgpu::Buffer,
@@ -58,8 +57,7 @@ pub struct CacheRequestHandler<const N: usize>
}
/// Records requests made by the cache
pub struct CacheRequestBuffer
{
pub struct CacheRequestBuffer {
pub max_requests: usize,
/// Buffer containing the amount of requests made by the cache, under max requsets
@@ -76,10 +74,8 @@ pub struct CacheRequestBuffer
bind_group: wgpu::BindGroup,
}
impl<const N: usize> CacheRequestHandler<N>
{
pub fn new(device: &mut memory_profiler::Device, page_count: usize, max_selected: u32) -> Self
{
impl<const N: usize> CacheRequestHandler<N> {
pub fn new(device: &mut memory_profiler::Device, page_count: usize, max_selected: u32) -> Self {
let element_count = page_count * N * N * N;
assert!(
element_count <= u32::MAX as usize,
@@ -175,8 +171,7 @@ impl<const N: usize> CacheRequestHandler<N>
}
}
fn create_gather_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
{
fn create_gather_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout {
let entry = |binding: u32, read_only: bool| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
@@ -211,8 +206,7 @@ impl<const N: usize> CacheRequestHandler<N>
ressources: &CacheRessources<N>,
encoder: &mut ScopeEncoder<CommandEncoder>,
k: u32,
)
{
) {
vxls_profiler::begin_scope!(encoder, "RequestHandler::sort_requests");
// Select the most requested pages.
@@ -297,8 +291,7 @@ impl<const N: usize> CacheRequestHandler<N>
);
}
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
{
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout {
create_multi_buffers_bind_group_layout(
device,
3,
@@ -306,24 +299,20 @@ impl<const N: usize> CacheRequestHandler<N>
)
}
pub fn bind_group(&self) -> &BindGroup
{
pub fn bind_group(&self) -> &BindGroup {
&self.bind_group
}
pub fn request_count_buffer(&self) -> &wgpu::Buffer
{
pub fn request_count_buffer(&self) -> &wgpu::Buffer {
&self.request_count
}
pub fn request_pages_buffer(&self) -> &wgpu::Buffer
{
pub fn request_pages_buffer(&self) -> &wgpu::Buffer {
&self.request_counts_pages
}
/// Resets the RequestHandlers request buffer for a next cache usage.
pub fn reset(&self, encoder: &mut ScopeEncoder<CommandEncoder>)
{
pub fn reset(&self, encoder: &mut ScopeEncoder<CommandEncoder>) {
let mut compute_pass = encoder.begin_profiled_compute_pass("CacheRequestHandler::reset");
compute_pass.set_pipeline(&self.reset_pass);
@@ -332,11 +321,9 @@ impl<const N: usize> CacheRequestHandler<N>
}
}
impl CacheRequestBuffer
{
impl CacheRequestBuffer {
/// Creates a request buffer that can contain at most `max_amount` requests
pub fn new(device: &Device, max_requests: usize) -> Self
{
pub fn new(device: &Device, max_requests: usize) -> Self {
let request_count = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("CacheRequestBuffer::request_count"),
usage: wgpu::BufferUsages::STORAGE
@@ -378,8 +365,7 @@ impl CacheRequestBuffer
}
}
pub fn create_bind_group_layout(device: &Device) -> BindGroupLayout
{
pub fn create_bind_group_layout(device: &Device) -> BindGroupLayout {
create_multi_buffers_bind_group_layout(
device,
3,
@@ -387,8 +373,7 @@ impl CacheRequestBuffer
)
}
pub fn bind_group(&self) -> &BindGroup
{
pub fn bind_group(&self) -> &BindGroup {
&self.bind_group
}
@@ -399,8 +384,7 @@ impl CacheRequestBuffer
device: &Device,
queue: &wgpu::Queue,
callback: F,
)
{
) {
// Once the three required buffers have been mapped, the requestMapState will automatically
// fire the callback
let request_map_state = Arc::new(Mutex::new(RequestMapState::new(callback)));
@@ -456,18 +440,15 @@ impl CacheRequestBuffer
}
}
struct RequestMapState<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static>
{
struct RequestMapState<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static> {
request_count: Option<usize>,
request_infos: Option<Vec<RequestInformation>>,
request_metas: Option<Vec<RequestMeta>>,
callback: F,
}
impl<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static> RequestMapState<F>
{
pub fn new(callback: F) -> Self
{
impl<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static> RequestMapState<F> {
pub fn new(callback: F) -> Self {
Self {
request_count: None,
request_infos: None,
@@ -476,8 +457,7 @@ impl<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static> Re
}
}
fn try_callback(&mut self)
{
fn try_callback(&mut self) {
if let (Some(request_count), Some(request_infos), Some(request_metas)) =
(self.request_count, &self.request_infos, &self.request_metas)
{
@@ -488,20 +468,17 @@ impl<F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static> Re
}
}
pub fn set_request_count(&mut self, request_count: usize)
{
pub fn set_request_count(&mut self, request_count: usize) {
self.request_count = Some(request_count);
self.try_callback();
}
pub fn set_request_infos(&mut self, request_infos: Vec<RequestInformation>)
{
pub fn set_request_infos(&mut self, request_infos: Vec<RequestInformation>) {
self.request_infos = Some(request_infos);
self.try_callback();
}
pub fn set_request_metas(&mut self, request_metas: Vec<RequestMeta>)
{
pub fn set_request_metas(&mut self, request_metas: Vec<RequestMeta>) {
self.request_metas = Some(request_metas);
self.try_callback();
}
+35 -70
View File
@@ -26,22 +26,17 @@ use bytemuck::Zeroable;
#[repr(transparent)]
pub struct CachePointer(u32);
impl CachePointer
{
impl CachePointer {
/// First option represents subdivisability
/// Second option represents availability
///
/// None -> unsubdivisable
/// Some(None) -> subdivisable not available
/// Some(Some(ptr)) -> subdivisable, available a pointer ptr
pub fn new(value: Option<Option<u32>>) -> Self
{
CachePointer(match value
{
Some(available) => match available
{
Some(pointer) =>
{
pub fn new(value: Option<Option<u32>>) -> Self {
CachePointer(match value {
Some(available) => match available {
Some(pointer) => {
assert_eq!(
pointer >> 30,
0,
@@ -55,17 +50,14 @@ impl CachePointer
})
}
pub fn as_option(&self) -> Option<Option<u32>>
{
pub fn as_option(&self) -> Option<Option<u32>> {
let subdivisable = (self.0 >> 31) != 0;
let available = ((self.0 >> 30) & 1) != 0;
let pointer = self.0 & 0x3FFFFFFF;
match (subdivisable, available)
{
match (subdivisable, available) {
(false, true) => panic!("Invalid cache pointer. (not subdivisable and available)"),
(false, _) if pointer != 0 =>
{
(false, _) if pointer != 0 => {
panic!("Invalid cache pointer. (non null unsubdivisable pointer)")
}
(false, _) => None,
@@ -75,10 +67,8 @@ impl CachePointer
}
}
impl From<u32> for CachePointer
{
fn from(value: u32) -> Self
{
impl From<u32> for CachePointer {
fn from(value: u32) -> Self {
CachePointer(value)
}
}
@@ -116,13 +106,11 @@ pub const NO_TABLE_SLOT: u32 = u32::MAX;
/// insertions can be rejected against the current table state.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct StructureAnchor
{
pub struct StructureAnchor {
pub words: [u32; ANCHOR_WORDS],
}
impl StructureAnchor
{
impl StructureAnchor {
/// Convenience anchor used by tests and by tables that do not need a
/// distinguishable identity at startup.
#[allow(dead_code)]
@@ -131,15 +119,13 @@ impl StructureAnchor
};
/// Table-defined helper for world-position anchors: `(x, y, z, version)`.
pub fn from_world(x: i32, y: i32, z: i32, version: u32) -> Self
{
pub fn from_world(x: i32, y: i32, z: i32, version: u32) -> Self {
Self {
words: [x as u32, y as u32, z as u32, version],
}
}
pub fn world(self) -> (i32, i32, i32, u32)
{
pub fn world(self) -> (i32, i32, i32, u32) {
(
self.words[0] as i32,
self.words[1] as i32,
@@ -165,8 +151,7 @@ pub struct StructureLocator(pub u32);
/// particular request
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct LocationPage
{
pub struct LocationPage {
/// Identifier for locating this particular node within the structure
pub node_id: StructureLocator,
@@ -195,8 +180,7 @@ pub struct LocationPage
#[repr(C)]
pub struct DepthPage(pub u32);
impl DepthPage
{
impl DepthPage {
pub const PAGE_UNUSED: Self = DepthPage(u32::MAX);
}
@@ -205,8 +189,7 @@ impl DepthPage
/// children is needed
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct RequestInformation
{
pub struct RequestInformation {
/// Identifier for locating this particular node within the structure
pub node_id: StructureLocator,
@@ -226,8 +209,7 @@ pub struct RequestInformation
/// Metainformation kept along each request, to know which node made the demand
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(C)]
pub struct RequestMeta
{
pub struct RequestMeta {
/// The page on which the request was issued, with the special value 0xFFFFFFFF if the request
/// is external
pub page_index: u32,
@@ -252,16 +234,14 @@ pub const NO_PARENT_PAGE: u32 = u32::MAX;
#[repr(transparent)]
pub struct InsertionParentKind(u32);
impl InsertionParentKind
{
impl InsertionParentKind {
/// The new page is a child of a resident cache page.
pub const INTERNAL: Self = Self(0);
/// The new page is the root of a structure owned by a structure table.
pub const EXTERNAL: Self = Self(1);
pub fn raw(self) -> u32
{
pub fn raw(self) -> u32 {
self.0
}
}
@@ -272,8 +252,7 @@ impl InsertionParentKind
/// the node the request was issued on, and link the new page without reading the request buffer.
#[derive(Clone, Copy, Debug, Zeroable, Pod)]
#[repr(C)]
pub struct InsertionParent
{
pub struct InsertionParent {
/// [`InsertionParentKind`].
pub kind: u32,
@@ -300,16 +279,14 @@ pub struct InsertionParent
pub table_slot: u32,
}
impl InsertionParent
{
impl InsertionParent {
/// Parent information for a child of a resident cache page.
pub fn internal(
meta: &RequestMeta,
node_id: StructureLocator,
producer_id: ProducerId,
anchor: StructureAnchor,
) -> Self
{
) -> Self {
Self {
kind: InsertionParentKind::INTERNAL.raw(),
page_index: meta.page_index,
@@ -323,8 +300,7 @@ impl InsertionParent
}
/// Parent information for the root of an external structure.
pub fn external(producer_id: ProducerId, anchor: StructureAnchor, table_slot: u32) -> Self
{
pub fn external(producer_id: ProducerId, anchor: StructureAnchor, table_slot: u32) -> Self {
Self {
kind: InsertionParentKind::EXTERNAL.raw(),
page_index: NO_PARENT_PAGE,
@@ -337,13 +313,11 @@ impl InsertionParent
}
}
pub fn is_external(&self) -> bool
{
pub fn is_external(&self) -> bool {
self.kind == InsertionParentKind::EXTERNAL.raw()
}
pub fn is_internal(&self) -> bool
{
pub fn is_internal(&self) -> bool {
self.kind == InsertionParentKind::INTERNAL.raw()
}
}
@@ -361,22 +335,18 @@ impl InsertionParent
#[repr(transparent)]
pub struct ChildInformation(u32);
impl ChildInformation
{
impl ChildInformation {
pub const NONE: Self = Self(0);
pub const SUBDIVISABLE: Self = Self(1 << 31);
pub const OCCUPIED: Self = Self(1 << 30);
pub fn new(subdivisable: bool, occupied: bool) -> Self
{
pub fn new(subdivisable: bool, occupied: bool) -> Self {
let mut n = Self::NONE;
if subdivisable
{
if subdivisable {
n = n | Self::SUBDIVISABLE;
}
if occupied
{
if occupied {
n = n | Self::OCCUPIED;
}
@@ -384,12 +354,10 @@ impl ChildInformation
}
}
impl BitOr for ChildInformation
{
impl BitOr for ChildInformation {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output
{
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
@@ -409,13 +377,11 @@ where
unsafe impl<const N: usize> Pod for InsertionInformation<N> where [(); N * N * N]: {}
#[cfg(test)]
mod tests
{
mod tests {
use super::*;
#[test]
fn wire_layout_sizes_are_stable()
{
fn wire_layout_sizes_are_stable() {
assert_eq!(size_of::<StructureAnchor>(), 16);
assert_eq!(size_of::<RequestInformation>(), 32);
assert_eq!(size_of::<LocationPage>(), 24);
@@ -424,8 +390,7 @@ mod tests
}
#[test]
fn anchor_world_round_trips_negative_coordinates()
{
fn anchor_world_round_trips_negative_coordinates() {
let anchor = StructureAnchor::from_world(-17, 42, -3, 7);
assert_eq!(anchor.world(), (-17, 42, -3, 7));
}
+6 -12
View File
@@ -31,8 +31,7 @@ where
pub const LOCATION_PAGES_BINDING: u32 = 1;
pub const DEPTH_PAGES_BINDING: u32 = 2;
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self
{
pub fn new(device: &mut memory_profiler::Device, page_count: usize) -> Self {
let pointer_pages = device.create_buffer(&wgpu::BufferDescriptor {
label: Some("CacheStructure::pointer_pages"),
size: (size_of::<StructurePage<N>>() * page_count) as u64,
@@ -74,28 +73,23 @@ where
}
}
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
{
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout {
create_multi_buffers_bind_group_layout(device, 3, Some("CacheStructure::bind_group_layout"))
}
pub fn bind_group_layout(&self) -> &BindGroupLayout
{
pub fn bind_group_layout(&self) -> &BindGroupLayout {
&self.bind_group_layout
}
pub fn bind_group(&self) -> &BindGroup
{
pub fn bind_group(&self) -> &BindGroup {
&self.bind_group
}
pub fn pointer_pages(&self) -> &Buffer
{
pub fn pointer_pages(&self) -> &Buffer {
&self.pointer_pages
}
pub fn depth_pages(&self) -> &Buffer
{
pub fn depth_pages(&self) -> &Buffer {
&self.depth_pages
}
}
+83 -166
View File
@@ -46,8 +46,7 @@ macro_rules! include_table_wgsl {
fn retired_anchors(
old_anchors: &[StructureAnchor],
new_anchors: &[StructureAnchor],
) -> Vec<StructureAnchor>
{
) -> Vec<StructureAnchor> {
use std::collections::HashSet;
assert!(
@@ -74,8 +73,7 @@ fn retired_anchors(
/// keeps its stale anchor on the GPU until it is reused; the empty pointer keeps
/// the tracer from ever treating it as live.
#[allow(dead_code)]
struct SlotAllocator
{
struct SlotAllocator {
/// Slot-indexed liveness. `None` means the slot is free.
slot_anchors: Vec<Option<StructureAnchor>>,
@@ -89,10 +87,8 @@ struct SlotAllocator
}
#[allow(dead_code)]
impl SlotAllocator
{
fn new(capacity: usize) -> Self
{
impl SlotAllocator {
fn new(capacity: usize) -> Self {
assert!(
capacity <= u32::MAX as usize,
"structure table capacity exceeds the u32 slot index space"
@@ -107,11 +103,9 @@ impl SlotAllocator
}
}
fn allocate(&mut self, anchor: StructureAnchor) -> Option<u32>
{
fn allocate(&mut self, anchor: StructureAnchor) -> Option<u32> {
// Allocating an anchor that is already live is idempotent.
if let Some(&slot) = self.anchor_slot.get(&anchor)
{
if let Some(&slot) = self.anchor_slot.get(&anchor) {
return Some(slot);
}
@@ -122,8 +116,7 @@ impl SlotAllocator
Some(slot)
}
fn release(&mut self, slot: u32) -> Option<StructureAnchor>
{
fn release(&mut self, slot: u32) -> Option<StructureAnchor> {
let anchor = self.slot_anchors.get_mut(slot as usize)?.take()?;
self.anchor_slot.remove(&anchor);
self.live -= 1;
@@ -132,11 +125,9 @@ impl SlotAllocator
}
/// Extends the slot space. Existing allocations keep their slot indices.
fn grow(&mut self, new_capacity: usize)
{
fn grow(&mut self, new_capacity: usize) {
let old_capacity = self.slot_anchors.len();
if new_capacity <= old_capacity
{
if new_capacity <= old_capacity {
return;
}
@@ -152,35 +143,29 @@ impl SlotAllocator
.extend((old_capacity..new_capacity).rev().map(|slot| slot as u32));
}
fn capacity(&self) -> usize
{
fn capacity(&self) -> usize {
self.slot_anchors.len()
}
fn live_count(&self) -> usize
{
fn live_count(&self) -> usize {
self.live
}
fn is_full(&self) -> bool
{
fn is_full(&self) -> bool {
self.free.is_empty()
}
fn is_live(&self, slot: u32) -> bool
{
fn is_live(&self, slot: u32) -> bool {
self.slot_anchors
.get(slot as usize)
.is_some_and(|anchor| anchor.is_some())
}
fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor>
{
fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor> {
self.slot_anchors.get(slot as usize).copied().flatten()
}
fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32>
{
fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32> {
self.anchor_slot.get(anchor).copied()
}
}
@@ -196,8 +181,7 @@ impl SlotAllocator
/// pipelines, indirect buffer) and size-dependent ones (buffers, bind groups,
/// request handler, scratch resources). [`Self::resize`] rebuilds only the
/// latter so external pipeline layouts stay valid.
struct StructureTableCore
{
struct StructureTableCore {
size: usize,
#[allow(dead_code)]
max_requests: u32,
@@ -231,8 +215,7 @@ struct StructureTableCore
raytrace_bind_group: wgpu::BindGroup,
}
impl StructureTableCore
{
impl StructureTableCore {
fn new(
device: &wgpu::Device,
size: usize,
@@ -240,8 +223,7 @@ impl StructureTableCore
producer_id: u32,
initial_anchors: &[StructureAnchor],
initial_pointers: &[CachePointer],
) -> Self
{
) -> Self {
assert_eq!(initial_anchors.len(), size);
assert_eq!(initial_pointers.len(), size);
@@ -422,10 +404,8 @@ impl StructureTableCore
/// outside this type remain compatible. Any bind group obtained before the
/// resize must be re-fetched.
#[allow(dead_code)]
fn resize(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, new_size: usize)
{
if new_size <= self.size
{
fn resize(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, new_size: usize) {
if new_size <= self.size {
return;
}
@@ -516,8 +496,7 @@ impl StructureTableCore
self.raytrace_bind_group = raytrace_bind_group;
}
fn write_anchor(&self, queue: &wgpu::Queue, slot: u32, anchor: StructureAnchor)
{
fn write_anchor(&self, queue: &wgpu::Queue, slot: u32, anchor: StructureAnchor) {
queue.write_buffer(
&self.anchors,
slot as u64 * size_of::<StructureAnchor>() as u64,
@@ -526,8 +505,7 @@ impl StructureTableCore
}
#[allow(dead_code)]
fn write_pointer(&self, queue: &wgpu::Queue, slot: u32, pointer: CachePointer)
{
fn write_pointer(&self, queue: &wgpu::Queue, slot: u32, pointer: CachePointer) {
queue.write_buffer(
&self.pointers,
slot as u64 * size_of::<CachePointer>() as u64,
@@ -540,8 +518,7 @@ impl StructureTableCore
device: &wgpu::Device,
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
request_buffer: &CacheRequestBuffer,
)
{
) {
self.request_handler.sort_requests(
device,
&self.common_ressources,
@@ -652,8 +629,7 @@ impl StructureTableCore
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
cache_evictor: &CacheEvictor,
invalid_timestamp: u32,
)
{
) {
let mut invalidate_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("StructureTableCore::invalidate_pass"),
timestamp_writes: None,
@@ -667,29 +643,24 @@ impl StructureTableCore
invalidate_pass.dispatch_workgroups(self.size.div_ceil(256) as u32, 1, 1);
}
fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
{
fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.raytrace_bind_group_layout
}
fn raytrace_bind_group(&self) -> &wgpu::BindGroup
{
fn raytrace_bind_group(&self) -> &wgpu::BindGroup {
&self.raytrace_bind_group
}
#[allow(dead_code)]
fn pointers_buffer(&self) -> &wgpu::Buffer
{
fn pointers_buffer(&self) -> &wgpu::Buffer {
&self.pointers
}
fn request_pages_buffer(&self) -> &wgpu::Buffer
{
fn request_pages_buffer(&self) -> &wgpu::Buffer {
self.request_handler.request_pages_buffer()
}
fn pointers_bind_group(&self) -> &wgpu::BindGroup
{
fn pointers_bind_group(&self) -> &wgpu::BindGroup {
&self.pointers_bind_group
}
}
@@ -697,23 +668,20 @@ impl StructureTableCore
/// A structure table with a fixed length.
///
/// Every slot is live and is kept in sync with [`Self::reassign_slots`].
pub struct FixedStructureTable
{
pub struct FixedStructureTable {
core: StructureTableCore,
/// Host mirror used to preserve identity across slot moves.
slot_anchors: Vec<StructureAnchor>,
}
impl FixedStructureTable
{
impl FixedStructureTable {
/// Creates a static table with one distinct anchor per slot.
///
/// Retained for callers that do not move slots; [`Self::new_with_anchors`]
/// is the general constructor.
#[allow(dead_code)]
pub fn new(device: &wgpu::Device, size: usize, max_requests: usize, producer_id: u32) -> Self
{
pub fn new(device: &wgpu::Device, size: usize, max_requests: usize, producer_id: u32) -> Self {
let anchors = (0..size)
.map(|slot| StructureAnchor {
words: [slot as u32, 0, 0, 0],
@@ -729,8 +697,7 @@ impl FixedStructureTable
max_requests: usize,
producer_id: u32,
anchors: Vec<StructureAnchor>,
) -> Self
{
) -> Self {
assert_eq!(anchors.len(), size);
let pointers = vec![CachePointer::new(Some(None)); size];
@@ -753,18 +720,15 @@ impl FixedStructureTable
&mut self,
queue: &wgpu::Queue,
new_anchors: &[StructureAnchor],
) -> Vec<StructureAnchor>
{
) -> Vec<StructureAnchor> {
assert_eq!(new_anchors.len(), self.core.size);
let retired = retired_anchors(&self.slot_anchors, new_anchors);
#[allow(clippy::needless_range_loop)]
for slot in 0..self.core.size
{
for slot in 0..self.core.size {
let anchor = new_anchors[slot];
if self.slot_anchors[slot] == anchor
{
if self.slot_anchors[slot] == anchor {
continue;
}
@@ -777,8 +741,7 @@ impl FixedStructureTable
/// Returns the current slot for an anchor, if it is in the table.
#[allow(dead_code)]
pub fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32>
{
pub fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32> {
self.slot_anchors
.iter()
.position(|candidate| candidate == anchor)
@@ -786,8 +749,7 @@ impl FixedStructureTable
}
#[allow(dead_code)]
pub fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor>
{
pub fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor> {
self.slot_anchors.get(slot as usize).copied()
}
@@ -796,8 +758,7 @@ impl FixedStructureTable
device: &wgpu::Device,
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
request_buffer: &CacheRequestBuffer,
)
{
) {
self.core.gather_requests(device, encoder, request_buffer);
}
@@ -813,24 +774,20 @@ impl FixedStructureTable
self.core.insert(device, encoder, insertion);
}
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
{
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
self.core.raytrace_bind_group_layout()
}
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
{
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup {
self.core.raytrace_bind_group()
}
#[allow(dead_code)]
pub fn pointers_buffer(&self) -> &wgpu::Buffer
{
pub fn pointers_buffer(&self) -> &wgpu::Buffer {
self.core.pointers_buffer()
}
pub fn pointers_bind_group(&self) -> &wgpu::BindGroup
{
pub fn pointers_bind_group(&self) -> &wgpu::BindGroup {
self.core.pointers_bind_group()
}
}
@@ -847,23 +804,20 @@ impl FixedStructureTable
/// size-dependent GPU resources, so it must happen between frames; any bind
/// group obtained from the table before growth must be re-fetched.
#[allow(dead_code)]
pub struct DynamicStructureTable
{
pub struct DynamicStructureTable {
core: StructureTableCore,
allocator: SlotAllocator,
}
#[allow(dead_code)]
impl DynamicStructureTable
{
impl DynamicStructureTable {
/// Creates a table with `capacity` free slots.
pub fn new(
device: &wgpu::Device,
capacity: usize,
max_requests: usize,
producer_id: u32,
) -> Self
{
) -> Self {
// Free slots keep a stale (here zero) anchor; only the empty pointer is
// meaningful for liveness.
let anchors = vec![StructureAnchor::ZERO; capacity];
@@ -888,8 +842,7 @@ impl DynamicStructureTable
/// Returns `None` if the table is full. Allocating an anchor that is already
/// live returns its existing slot. The slot is marked subdivisible but not
/// available, so the cache emits a root request for it.
pub fn allocate(&mut self, queue: &wgpu::Queue, anchor: StructureAnchor) -> Option<u32>
{
pub fn allocate(&mut self, queue: &wgpu::Queue, anchor: StructureAnchor) -> Option<u32> {
let slot = self.allocator.allocate(anchor)?;
self.core.write_anchor(queue, slot, anchor);
@@ -908,8 +861,7 @@ impl DynamicStructureTable
///
/// This does not free cache pages: the root becomes unreachable and its
/// pages are reclaimed by the normal LRU eviction.
pub fn release(&mut self, queue: &wgpu::Queue, slot: u32) -> Option<StructureAnchor>
{
pub fn release(&mut self, queue: &wgpu::Queue, slot: u32) -> Option<StructureAnchor> {
let anchor = self.allocator.release(slot)?;
self.core
.write_pointer(queue, slot, CachePointer::new(None));
@@ -920,16 +872,14 @@ impl DynamicStructureTable
///
/// Capacity doubles from the current value until it reaches `min_capacity`.
/// Must be called between frames.
pub fn reserve(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, min_capacity: usize)
{
pub fn reserve(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, min_capacity: usize) {
assert!(
min_capacity <= u32::MAX as usize,
"structure table capacity exceeds the u32 slot index space"
);
let new_capacity = self.grown_capacity(min_capacity);
if new_capacity <= self.capacity()
{
if new_capacity <= self.capacity() {
return;
}
@@ -938,8 +888,7 @@ impl DynamicStructureTable
}
/// Doubles the table capacity. Must be called between frames.
pub fn grow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue)
{
pub fn grow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) {
let max = u32::MAX as usize;
let min_capacity = self.capacity().saturating_mul(2).min(max).max(1);
self.reserve(device, queue, min_capacity);
@@ -951,54 +900,44 @@ impl DynamicStructureTable
device: &wgpu::Device,
queue: &wgpu::Queue,
anchor: StructureAnchor,
) -> Option<u32>
{
if let Some(slot) = self.allocator.slot_for_anchor(&anchor)
{
) -> Option<u32> {
if let Some(slot) = self.allocator.slot_for_anchor(&anchor) {
return Some(slot);
}
if self.allocator.is_full()
{
if self.allocator.is_full() {
self.grow(device, queue);
}
self.allocate(queue, anchor)
}
fn grown_capacity(&self, min_capacity: usize) -> usize
{
fn grown_capacity(&self, min_capacity: usize) -> usize {
let max = u32::MAX as usize;
let mut capacity = self.capacity().max(1);
while capacity < min_capacity
{
while capacity < min_capacity {
capacity = capacity.saturating_mul(2).min(max);
}
capacity
}
pub fn capacity(&self) -> usize
{
pub fn capacity(&self) -> usize {
self.allocator.capacity()
}
pub fn live_count(&self) -> usize
{
pub fn live_count(&self) -> usize {
self.allocator.live_count()
}
pub fn is_live(&self, slot: u32) -> bool
{
pub fn is_live(&self, slot: u32) -> bool {
self.allocator.is_live(slot)
}
pub fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor>
{
pub fn slot_anchor(&self, slot: u32) -> Option<StructureAnchor> {
self.allocator.slot_anchor(slot)
}
pub fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32>
{
pub fn slot_for_anchor(&self, anchor: &StructureAnchor) -> Option<u32> {
self.allocator.slot_for_anchor(anchor)
}
@@ -1007,8 +946,7 @@ impl DynamicStructureTable
device: &wgpu::Device,
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
request_buffer: &CacheRequestBuffer,
)
{
) {
self.core.gather_requests(device, encoder, request_buffer);
}
@@ -1024,29 +962,24 @@ impl DynamicStructureTable
self.core.insert(device, encoder, insertion);
}
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
{
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout {
self.core.raytrace_bind_group_layout()
}
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
{
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup {
self.core.raytrace_bind_group()
}
#[allow(dead_code)]
pub fn pointers_buffer(&self) -> &wgpu::Buffer
{
pub fn pointers_buffer(&self) -> &wgpu::Buffer {
self.core.pointers_buffer()
}
pub fn request_pages_buffer(&self) -> &wgpu::Buffer
{
pub fn request_pages_buffer(&self) -> &wgpu::Buffer {
self.core.request_pages_buffer()
}
pub fn pointers_bind_group(&self) -> &wgpu::BindGroup
{
pub fn pointers_bind_group(&self) -> &wgpu::BindGroup {
self.core.pointers_bind_group()
}
}
@@ -1060,8 +993,7 @@ where
device: &wgpu::Device,
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
insertion: &CacheInsertionData<N>,
)
{
) {
self.core.validate_insertion(device, encoder, insertion);
}
@@ -1070,8 +1002,7 @@ where
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
cache_evictor: &CacheEvictor,
invalid_timestamp: u32,
)
{
) {
self.core
.invalidate(encoder, cache_evictor, invalid_timestamp);
}
@@ -1086,8 +1017,7 @@ where
device: &wgpu::Device,
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
insertion: &CacheInsertionData<N>,
)
{
) {
self.core.validate_insertion(device, encoder, insertion);
}
@@ -1096,26 +1026,22 @@ where
encoder: &mut ScopeEncoder<wgpu::CommandEncoder>,
cache_evictor: &CacheEvictor,
invalid_timestamp: u32,
)
{
) {
self.core
.invalidate(encoder, cache_evictor, invalid_timestamp);
}
}
#[cfg(test)]
mod tests
{
mod tests {
use super::*;
fn anchor(x: i32) -> StructureAnchor
{
fn anchor(x: i32) -> StructureAnchor {
StructureAnchor::from_world(x, 0, 0, 0)
}
#[test]
fn permuted_anchors_retire_nothing()
{
fn permuted_anchors_retire_nothing() {
let old_anchors = [anchor(0), anchor(1), anchor(2)];
let new_anchors = [anchor(2), anchor(0), anchor(1)];
@@ -1125,8 +1051,7 @@ mod tests
}
#[test]
fn leaving_anchor_retires()
{
fn leaving_anchor_retires() {
let old_anchors = [anchor(0), anchor(1)];
let new_anchors = [anchor(1), anchor(2)];
@@ -1136,8 +1061,7 @@ mod tests
}
#[test]
fn reentering_anchor_is_the_same_identity()
{
fn reentering_anchor_is_the_same_identity() {
let first = retired_anchors(&[anchor(0)], &[anchor(5)]);
assert_eq!(first, vec![anchor(0)]);
@@ -1147,14 +1071,12 @@ mod tests
#[test]
#[should_panic(expected = "a structure table must not expose two live slots")]
fn duplicate_live_anchors_are_rejected()
{
fn duplicate_live_anchors_are_rejected() {
let _ = retired_anchors(&[], &[anchor(0), anchor(0)]);
}
#[test]
fn allocator_allocates_low_indices_first()
{
fn allocator_allocates_low_indices_first() {
let mut allocator = SlotAllocator::new(4);
assert_eq!(allocator.allocate(anchor(0)), Some(0));
@@ -1165,8 +1087,7 @@ mod tests
}
#[test]
fn allocator_reuses_released_slots()
{
fn allocator_reuses_released_slots() {
let mut allocator = SlotAllocator::new(2);
let a = allocator.allocate(anchor(0)).unwrap();
let b = allocator.allocate(anchor(1)).unwrap();
@@ -1180,8 +1101,7 @@ mod tests
}
#[test]
fn allocator_is_idempotent_for_live_anchors()
{
fn allocator_is_idempotent_for_live_anchors() {
let mut allocator = SlotAllocator::new(2);
let slot = allocator.allocate(anchor(0)).unwrap();
@@ -1190,8 +1110,7 @@ mod tests
}
#[test]
fn allocator_release_unknown_slot_is_none()
{
fn allocator_release_unknown_slot_is_none() {
let mut allocator = SlotAllocator::new(2);
assert_eq!(allocator.release(0), None);
@@ -1199,8 +1118,7 @@ mod tests
}
#[test]
fn allocator_tracks_liveness()
{
fn allocator_tracks_liveness() {
let mut allocator = SlotAllocator::new(2);
let slot = allocator.allocate(anchor(0)).unwrap();
@@ -1216,8 +1134,7 @@ mod tests
}
#[test]
fn allocator_grow_preserves_live_slots()
{
fn allocator_grow_preserves_live_slots() {
let mut allocator = SlotAllocator::new(2);
let a = allocator.allocate(anchor(0)).unwrap();
let b = allocator.allocate(anchor(1)).unwrap();