refactor: Splitting into crates
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::Buffer;
|
||||
use wgpu::BufferUsages;
|
||||
|
||||
use vxls_profiler::memory_profiler;
|
||||
use crate::layout::CacheLayout;
|
||||
|
||||
/// Contains instanciated gpu buffers to hold data for cache attributes
|
||||
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>
|
||||
{
|
||||
pub fn from_cache_layout(
|
||||
device: &mut memory_profiler::Device,
|
||||
layout: &CacheLayout,
|
||||
page_count: usize,
|
||||
) -> Self
|
||||
{
|
||||
let attribute_buffers = layout
|
||||
.attributes
|
||||
.iter()
|
||||
.map(|attribute| {
|
||||
attribute.create_buffer_profiled(
|
||||
device,
|
||||
N * N * N,
|
||||
page_count,
|
||||
BufferUsages::STORAGE,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device, layout);
|
||||
|
||||
let bind_group_bindings = attribute_buffers
|
||||
.iter()
|
||||
.map(|buffer| buffer.as_entire_buffer_binding())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("CacheAttributes::bind_group"),
|
||||
layout: &bind_group_layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::BufferArray(&bind_group_bindings),
|
||||
}],
|
||||
});
|
||||
|
||||
CacheAttributes {
|
||||
attribute_buffers,
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_bind_group_layout(device: &wgpu::Device, layout: &CacheLayout)
|
||||
-> BindGroupLayout
|
||||
{
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("CacheAttributes::bind_group_layout"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: Some(
|
||||
std::num::NonZero::new(layout.attributes.len() as u32)
|
||||
.expect("Cannot create cache attributes with no attributes"),
|
||||
),
|
||||
}],
|
||||
})
|
||||
}
|
||||
|
||||
pub fn bind_group_layout(&self) -> &wgpu::BindGroupLayout
|
||||
{
|
||||
&self.bind_group_layout
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
pub fn attribute_buffers(&self) -> &[Buffer]
|
||||
{
|
||||
&self.attribute_buffers
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
/// Contains structure and logic related to eviction logic in the cache.
|
||||
use wgpu::BindGroup;
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::ComputePipeline;
|
||||
use wgpu::include_wgsl;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use vxls_gpu::create_multi_buffers_bind_group;
|
||||
use vxls_gpu::radix_sort;
|
||||
use vxls_profiler::ScopeEncoder;
|
||||
use vxls_profiler::memory_profiler;
|
||||
use vxls_profiler::{self};
|
||||
use crate::CacheRessources;
|
||||
use crate::create_multi_buffers_bind_group_layout;
|
||||
use crate::structure::CacheStructure;
|
||||
|
||||
/// Retains usage information about pages in the cache under the form of timestamps.
|
||||
/// Produces an eviction order from which the cache can pull pages to recycle.
|
||||
///
|
||||
/// The list is ordered by timestamp first (least recently used first). Ties are
|
||||
/// 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
|
||||
{
|
||||
page_count: usize,
|
||||
|
||||
/// Contains a u32 timestamp per page, so that we know when a page was last used
|
||||
timestamp_pages: wgpu::Buffer,
|
||||
|
||||
/// Contains page indices in eviction order: least recently used first.
|
||||
/// Among pages with equal timestamps, deeper pages come first.
|
||||
lru_list: wgpu::Buffer,
|
||||
|
||||
/// When reusing pages, count of lru pages consumed
|
||||
consumed_count: wgpu::Buffer,
|
||||
|
||||
/// To reset lru indices between insertions
|
||||
reset_lru_pass: wgpu::ComputePipeline,
|
||||
|
||||
/// To copy corresponding timestamp values after the descending depth sort
|
||||
indirect_copy_pass: wgpu::ComputePipeline,
|
||||
pipelines_dispatch_count: u32,
|
||||
|
||||
// For external passes
|
||||
bind_group_layout: BindGroupLayout,
|
||||
bind_group: BindGroup,
|
||||
}
|
||||
|
||||
impl CacheEvictor
|
||||
{
|
||||
pub fn new(
|
||||
device: &mut memory_profiler::Device,
|
||||
page_count: usize,
|
||||
initial_timestamp: u32,
|
||||
) -> Self
|
||||
{
|
||||
let timestamp_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheUsage::timestamp_pages"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
contents: bytemuck::cast_slice(&vec![initial_timestamp; page_count]),
|
||||
});
|
||||
|
||||
let lru_list = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheUsage::lru_list"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
size: (page_count * size_of::<u32>()) as u64,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let consumed_count = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheUsage::consumed_count"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
contents: bytemuck::bytes_of(&0u32),
|
||||
});
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device);
|
||||
let bind_group = create_multi_buffers_bind_group(
|
||||
device,
|
||||
&bind_group_layout,
|
||||
&[×tamp_pages, &lru_list, &consumed_count],
|
||||
Some("CacheEvictor::bind_group"),
|
||||
);
|
||||
|
||||
let pipelines_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("CacheEvictor::reset_lru_list_pipeline_layout"),
|
||||
bind_group_layouts: &[
|
||||
Some(&bind_group_layout),
|
||||
Some(&CacheStructure::<1>::create_bind_group_layout(device)),
|
||||
Some(&CacheRessources::<1>::create_scratch_buffer_bind_group_layout(device)),
|
||||
],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let shader_module = device.create_shader_module(include_wgsl!("./shaders/eviction.wgsl"));
|
||||
|
||||
let reset_lru_pass = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheEvictor::reset_lru_pass"),
|
||||
layout: Some(&pipelines_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("reset_lru_list"),
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let indirect_copy_pass = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheEvictor::indirect_copy_pass"),
|
||||
layout: Some(&pipelines_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("indirect_copy"),
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
page_count,
|
||||
timestamp_pages,
|
||||
lru_list,
|
||||
consumed_count,
|
||||
|
||||
reset_lru_pass,
|
||||
indirect_copy_pass,
|
||||
pipelines_dispatch_count: page_count.div_ceil(256) as u32,
|
||||
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepare_eviction_list<const N: usize>(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
ressources: &CacheRessources<N>,
|
||||
structure: &CacheStructure<N>,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
) where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "CacheEvictor::prepare_eviction_list");
|
||||
// Reset lru list
|
||||
{
|
||||
let mut reset_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("CacheEvictor::reset_pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
reset_pass.set_bind_group(0, Some(&self.bind_group), &[]);
|
||||
reset_pass.set_bind_group(1, Some(structure.bind_group()), &[]);
|
||||
reset_pass.set_bind_group(2, Some(ressources.scratch_buffer_bind_group()), &[]);
|
||||
reset_pass.set_pipeline(&self.reset_lru_pass);
|
||||
reset_pass.dispatch_workgroups(self.pipelines_dispatch_count, 1, 1);
|
||||
}
|
||||
|
||||
// First sort nodes by depth, deepest first. This is the tie-breaker:
|
||||
// the timestamp sort below is stable, so it preserves this order for
|
||||
// pages that share a timestamp.
|
||||
//
|
||||
// Consuming the list in order then reuses the deepest pages first among
|
||||
// equal timestamps. Since a page's resident children are deeper, they
|
||||
// appear earlier in the list and are reused first (provided their
|
||||
// timestamp is equal or older), instead of being orphaned when the
|
||||
// parent is recycled.
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "depth radix");
|
||||
encoder.copy_buffer_to_buffer(
|
||||
structure.depth_pages(),
|
||||
0,
|
||||
&ressources.scratch_buffer,
|
||||
0,
|
||||
structure.depth_pages().size(),
|
||||
);
|
||||
|
||||
ressources.radix_sorter.sort(
|
||||
device,
|
||||
&ressources.scratch_buffer,
|
||||
&self.lru_list,
|
||||
None,
|
||||
Some(self.page_count),
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Descending,
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "indirect copy");
|
||||
let mut indirect_copy_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("CacheEvictor::indirect_copy_pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
indirect_copy_pass.set_bind_group(0, Some(&self.bind_group), &[]);
|
||||
indirect_copy_pass.set_bind_group(1, Some(structure.bind_group()), &[]);
|
||||
indirect_copy_pass.set_bind_group(2, Some(ressources.scratch_buffer_bind_group()), &[]);
|
||||
indirect_copy_pass.set_pipeline(&self.indirect_copy_pass);
|
||||
indirect_copy_pass.dispatch_workgroups(self.pipelines_dispatch_count, 1, 1);
|
||||
}
|
||||
|
||||
// Then sort by timestamp, oldest first. The radix sort is stable, so
|
||||
// equal timestamps retain the deepest-first order from the depth sort.
|
||||
// The resulting list is LRU-first with deepest-first ties.
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "timestamp radix");
|
||||
ressources.radix_sorter.sort(
|
||||
device,
|
||||
&ressources.scratch_buffer,
|
||||
&self.lru_list,
|
||||
None,
|
||||
Some(self.page_count),
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Ascending,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
&self.timestamp_pages
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
use std::num::NonZero;
|
||||
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::BufferUsages;
|
||||
use wgpu::ShaderStages;
|
||||
|
||||
use crate::layout::CacheLayout;
|
||||
use crate::schema::InsertionInformation;
|
||||
use crate::schema::InsertionParent;
|
||||
use crate::schema::LocationPage;
|
||||
|
||||
/// Data to be inserted in a cache insertion operation
|
||||
pub struct CacheInsertionData<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
/// The amount of pages to insert this structure can hold
|
||||
max_page_count: usize,
|
||||
|
||||
/// Amount of pages that the structure actually contains
|
||||
page_count_buffer: wgpu::Buffer,
|
||||
|
||||
/// Information about inserted pages : dense array of [`InsertionInformation<N>`] structs
|
||||
insertion_information: wgpu::Buffer,
|
||||
|
||||
/// Location of the new nodes : dense array of [`LocationPage`] structs
|
||||
location_pages: wgpu::Buffer,
|
||||
|
||||
/// Contains the attributes to import in the cache.
|
||||
attributes: Vec<wgpu::Buffer>,
|
||||
|
||||
/// Contains the indicies of the page in which the data was written
|
||||
destination_indices: wgpu::Buffer,
|
||||
|
||||
/// Parent/attachment information for every entry : dense array of [`InsertionParent`]
|
||||
insertion_parents: wgpu::Buffer,
|
||||
|
||||
/// Indices of the entries that passed validation, written by the validation pass
|
||||
valid_indices: wgpu::Buffer,
|
||||
|
||||
/// Amount of entries written to [`Self::valid_indices`]
|
||||
valid_count: wgpu::Buffer,
|
||||
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
bind_group: wgpu::BindGroup,
|
||||
bind_group_no_attributes: wgpu::BindGroup,
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheInsertionData<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(device: &wgpu::Device, max_page_count: usize, layout: &CacheLayout) -> Self
|
||||
{
|
||||
let page_count_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::page_count_buffer"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: size_of::<u32>() as u64,
|
||||
});
|
||||
|
||||
let insertion_information = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::insertion_information"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: (size_of::<InsertionInformation<N>>() * max_page_count) as u64,
|
||||
});
|
||||
|
||||
let location_pages = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::location_pages"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: (size_of::<LocationPage>() * max_page_count) as u64,
|
||||
});
|
||||
|
||||
let destination_indices = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::destination_indices"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: (size_of::<u32>() * max_page_count) as u64,
|
||||
});
|
||||
|
||||
let insertion_parents = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::insertion_parents"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: (size_of::<InsertionParent>() * max_page_count) as u64,
|
||||
});
|
||||
|
||||
let valid_indices = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::valid_indices"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
size: (size_of::<u32>() * max_page_count) as u64,
|
||||
});
|
||||
|
||||
let valid_count = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheInsertionData::valid_count"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
mapped_at_creation: false,
|
||||
size: size_of::<u32>() as u64,
|
||||
});
|
||||
|
||||
let attributes = layout
|
||||
.attributes
|
||||
.iter()
|
||||
.map(|attribute| {
|
||||
attribute.create_buffer(
|
||||
device,
|
||||
N * N * N,
|
||||
max_page_count,
|
||||
BufferUsages::STORAGE | BufferUsages::COPY_DST,
|
||||
)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let bind_group_bindings = attributes
|
||||
.iter()
|
||||
.map(|buffer| buffer.as_entire_buffer_binding())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut entries = vec![
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: page_count_buffer.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: insertion_information.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: location_pages.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: destination_indices.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 4,
|
||||
resource: insertion_parents.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 5,
|
||||
resource: valid_indices.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 6,
|
||||
resource: valid_count.as_entire_binding(),
|
||||
},
|
||||
];
|
||||
|
||||
let bind_group_layout_no_attributes = Self::create_bind_group_layout(device, None);
|
||||
let bind_group_no_attributes = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("CacheInsertionData::bind_group_no_attributes"),
|
||||
layout: &bind_group_layout_no_attributes,
|
||||
entries: &entries,
|
||||
});
|
||||
entries.push(wgpu::BindGroupEntry {
|
||||
binding: 7,
|
||||
resource: wgpu::BindingResource::BufferArray(&bind_group_bindings),
|
||||
});
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device, Some(layout));
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("CacheInsertionData::bind_group"),
|
||||
layout: &bind_group_layout,
|
||||
entries: &entries,
|
||||
});
|
||||
|
||||
Self {
|
||||
page_count_buffer,
|
||||
max_page_count,
|
||||
insertion_information,
|
||||
location_pages,
|
||||
destination_indices,
|
||||
insertion_parents,
|
||||
valid_indices,
|
||||
valid_count,
|
||||
attributes,
|
||||
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
bind_group_no_attributes,
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates a bindgroup layout to bind the insertion data :
|
||||
/// - Page count buffer (u32)
|
||||
/// - Insertion information
|
||||
/// - Location pages
|
||||
/// - Destination indicies
|
||||
/// - (Optional) attributes
|
||||
///
|
||||
/// An applcation that does not need the the attributes can pass `None` to layout.
|
||||
/// It can then use [`CacheInsertionData::<_>::bind_group_no_attributes(...)`] to get the
|
||||
/// corresponding bind group, otherwise use [`CacheInsertionData::<_>::bind_group(...)`]
|
||||
///
|
||||
pub fn create_bind_group_layout(
|
||||
device: &wgpu::Device,
|
||||
layout: Option<&CacheLayout>,
|
||||
) -> BindGroupLayout
|
||||
{
|
||||
let storage_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
|
||||
binding,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
};
|
||||
|
||||
let mut entries = vec![
|
||||
storage_entry(0), // page count
|
||||
storage_entry(1), // insertion information
|
||||
storage_entry(2), // location pages
|
||||
storage_entry(3), // destination indices
|
||||
storage_entry(4), // insertion parents
|
||||
storage_entry(5), // valid indices
|
||||
storage_entry(6), // valid count
|
||||
];
|
||||
|
||||
if let Some(layout) = layout
|
||||
{
|
||||
entries.push(wgpu::BindGroupLayoutEntry {
|
||||
binding: 7,
|
||||
visibility: ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: Some(NonZero::new(layout.attributes.len() as u32).unwrap()),
|
||||
});
|
||||
}
|
||||
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("CacheInsertionData::bind_group_layout"),
|
||||
entries: &entries,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
pub fn bind_group_no_attributes(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.bind_group_no_attributes
|
||||
}
|
||||
|
||||
pub fn page_count(&self) -> usize
|
||||
{
|
||||
self.max_page_count
|
||||
}
|
||||
|
||||
pub fn page_count_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.page_count_buffer
|
||||
}
|
||||
|
||||
pub fn insertion_information_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.insertion_information
|
||||
}
|
||||
|
||||
pub fn location_pages_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.location_pages
|
||||
}
|
||||
|
||||
pub fn destination_indices_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.destination_indices
|
||||
}
|
||||
|
||||
pub fn insertion_parents_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.insertion_parents
|
||||
}
|
||||
|
||||
pub fn valid_indices_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.valid_indices
|
||||
}
|
||||
|
||||
pub fn valid_count_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.valid_count
|
||||
}
|
||||
|
||||
pub fn attributes_buffers(&self) -> &[wgpu::Buffer]
|
||||
{
|
||||
&self.attributes
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
use wgpu::BufferUsages;
|
||||
|
||||
use vxls_profiler::memory_profiler;
|
||||
|
||||
/// Defines how a [`VoxelCache`]'s attribute is populated upon populating a page
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum CacheAttributeInit
|
||||
{
|
||||
/// Attribute is external : it is copied from the insertion operation
|
||||
External,
|
||||
|
||||
/// Attribute will not be touched by the cache mechanism. It is left to the discrecy of the
|
||||
/// cache user.
|
||||
///
|
||||
/// Some applications requires pieces of data into the resident data of the cache that are
|
||||
/// computed on the fly : e.g. global illumination
|
||||
DontTouch,
|
||||
}
|
||||
|
||||
/// Describes an attribute of a [`VoxelCache`]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct CacheAttribute
|
||||
{
|
||||
pub label: Option<&'static str>,
|
||||
pub word_count: u32,
|
||||
pub init: CacheAttributeInit,
|
||||
}
|
||||
|
||||
impl CacheAttribute
|
||||
{
|
||||
pub fn create_buffer_profiled(
|
||||
&self,
|
||||
device: &mut memory_profiler::Device,
|
||||
child_count: usize,
|
||||
page_count: usize,
|
||||
usage: wgpu::BufferUsages,
|
||||
) -> wgpu::Buffer
|
||||
{
|
||||
device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some(
|
||||
format!(
|
||||
"VoxelCacheAttribute ({}) {} word(s)",
|
||||
self.label.unwrap_or("no-name"),
|
||||
self.word_count
|
||||
)
|
||||
.as_str(),
|
||||
),
|
||||
size: (self.word_count as usize * page_count * child_count * size_of::<u32>()) as u64,
|
||||
usage,
|
||||
mapped_at_creation: false,
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates the wgpu buffer necessary to store this attribute in the cache
|
||||
pub fn create_buffer(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
child_count: usize,
|
||||
page_count: usize,
|
||||
usage: wgpu::BufferUsages,
|
||||
) -> wgpu::Buffer
|
||||
{
|
||||
device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some(
|
||||
format!(
|
||||
"VoxelCacheAttribute ({}) {} word(s)",
|
||||
self.label.unwrap_or("no-name"),
|
||||
self.word_count
|
||||
)
|
||||
.as_str(),
|
||||
),
|
||||
size: (self.word_count as usize * page_count * child_count * size_of::<u32>()) as u64,
|
||||
usage,
|
||||
mapped_at_creation: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct CacheLayout
|
||||
{
|
||||
pub(super) attributes: Vec<CacheAttribute>,
|
||||
}
|
||||
|
||||
impl CacheLayout
|
||||
{
|
||||
/// Adds a new [`VoxelCacheAttribute`] in the layout
|
||||
///
|
||||
/// Attributes are added starting from 0 counting up
|
||||
///
|
||||
/// Returns the index of the new added attirbute
|
||||
pub fn add_attribute(&mut self, attribute: CacheAttribute) -> usize
|
||||
{
|
||||
let index = self.attributes.len();
|
||||
self.attributes.push(attribute);
|
||||
index
|
||||
}
|
||||
|
||||
/// Produces a wgsl declaration describing this layout :
|
||||
///
|
||||
/// ```wgsl
|
||||
///
|
||||
/// const ATTRIBUTE_COUNT: u32 = /* attribute count */;
|
||||
/// const ATTRIBUTE_WORD_COUNTS: array<u32, ...> = {
|
||||
/// /* word count for attribute 0 */,
|
||||
/// /* word count for attribute 1 */,
|
||||
/// ...
|
||||
/// };
|
||||
///
|
||||
/// const ATTRIBUTE_COPY: array<bool, ...> = {
|
||||
/// /* external? for attribute 0 */,
|
||||
/// /* external? for attribute 1 */,
|
||||
/// ...
|
||||
/// };
|
||||
///
|
||||
/// ```
|
||||
pub fn wgsl_layout_decl(&self) -> String
|
||||
{
|
||||
let mut decl = String::new();
|
||||
|
||||
decl += &format!("const ATTRIBUTE_COUNT: u32 = {};", self.attributes.len());
|
||||
|
||||
// Word counts
|
||||
decl += &format!(
|
||||
"const ATTRIBUTE_WORD_COUNTS: array<u32, {}> = array<u32, {}>(",
|
||||
self.attributes.len(),
|
||||
self.attributes.len()
|
||||
);
|
||||
|
||||
for attribute in self.attributes.iter()
|
||||
{
|
||||
decl += &format!(" {},", attribute.word_count);
|
||||
}
|
||||
|
||||
decl += ");";
|
||||
|
||||
// Copy
|
||||
decl += &format!(
|
||||
"const ATTRIBUTE_COPY: array<bool, {}> = array<bool, {}>(",
|
||||
self.attributes.len(),
|
||||
self.attributes.len()
|
||||
);
|
||||
|
||||
for attribute in self.attributes.iter()
|
||||
{
|
||||
decl += &format!(" {},", attribute.init == CacheAttributeInit::External);
|
||||
}
|
||||
|
||||
decl += ");";
|
||||
|
||||
decl
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
#![feature(generic_const_exprs)]
|
||||
|
||||
use std::num::NonZero;
|
||||
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::Device;
|
||||
|
||||
use vxls_gpu::create_multi_buffers_bg_bg_layout;
|
||||
use vxls_gpu::create_multi_buffers_bind_group_layout;
|
||||
use vxls_gpu::radix_sort::RadixSorter;
|
||||
use vxls_profiler;
|
||||
use vxls_profiler::ScopeEncoder;
|
||||
use vxls_profiler::memory_profiler;
|
||||
use vxls_profiler::memory_profiler::MemoryProfile;
|
||||
use crate::attributes::CacheAttributes;
|
||||
use crate::eviction::CacheEvictor;
|
||||
use crate::insertion::CacheInsertionData;
|
||||
use crate::pipelines::CacheInsertionPass;
|
||||
use crate::pipelines::CacheInvalidationPass;
|
||||
use crate::pipelines::WritePointersPass;
|
||||
use crate::pipelines::WriteRequestsPass;
|
||||
use crate::requests::CacheRequestBuffer;
|
||||
use crate::requests::CacheRequestHandler;
|
||||
use crate::structure::CacheStructure;
|
||||
|
||||
pub mod attributes;
|
||||
pub mod eviction;
|
||||
pub mod insertion;
|
||||
pub mod layout;
|
||||
pub mod pipelines;
|
||||
pub mod requests;
|
||||
pub mod schema;
|
||||
pub mod structure;
|
||||
pub mod tables;
|
||||
|
||||
pub use layout::CacheAttribute;
|
||||
pub use layout::CacheAttributeInit;
|
||||
pub use layout::CacheLayout;
|
||||
|
||||
/// Stores some ressources that can be reused throughout the modules of the cache
|
||||
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
|
||||
{
|
||||
let scratch_buffer = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheRessources::scratch_buffer"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
|
||||
size: (size_of::<u32>() * page_count * N * N * N) as u64,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let radix_sorter = RadixSorter::new(device, page_count * N * N * N);
|
||||
|
||||
let (_, scratch_buffer_bind_group) = create_multi_buffers_bg_bg_layout(
|
||||
device,
|
||||
&[&scratch_buffer],
|
||||
Some("CacheRessources::scratch_buffer_bind_group"),
|
||||
);
|
||||
Self {
|
||||
scratch_buffer,
|
||||
scratch_buffer_bind_group,
|
||||
radix_sorter,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_scratch_buffer_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout
|
||||
{
|
||||
create_multi_buffers_bind_group_layout(
|
||||
device,
|
||||
1,
|
||||
Some("CacheRessources::scratch_buffer_bind_group_layout"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn scratch_buffer_bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.scratch_buffer_bind_group
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VoxelCache<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
page_count: usize,
|
||||
structure: CacheStructure<N>,
|
||||
attributes: CacheAttributes<N>,
|
||||
request_handler: CacheRequestHandler<N>,
|
||||
evictor: CacheEvictor,
|
||||
|
||||
common_ressources: CacheRessources<N>,
|
||||
|
||||
// Operations
|
||||
write_requests_pass: WriteRequestsPass<N>,
|
||||
cache_insertion_pass: CacheInsertionPass<N>,
|
||||
cache_invalidation_pass: CacheInvalidationPass<N>,
|
||||
write_pointers_pass: WritePointersPass<N>,
|
||||
|
||||
// Raytracing
|
||||
raytrace_bind_group_layout: wgpu::BindGroupLayout,
|
||||
raytrace_bind_group: wgpu::BindGroup,
|
||||
|
||||
frame_timestamp: u32,
|
||||
}
|
||||
|
||||
pub trait CacheInvalidable<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
fn invalidate(
|
||||
&mut self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
invalid_timestamp: u32,
|
||||
);
|
||||
|
||||
/// Validates an external insertion batch against the structure table and
|
||||
/// compacts the valid entries before the cache allocates pages for them.
|
||||
fn validate_insertion(
|
||||
&mut self,
|
||||
device: &Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
insertion: &CacheInsertionData<N>,
|
||||
);
|
||||
}
|
||||
|
||||
pub enum VoxelCacheInsertion<'a, const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
ExternalRequest(&'a CacheInsertionData<N>, &'a mut dyn CacheInvalidable<N>),
|
||||
InternalRequest(&'a CacheInsertionData<N>),
|
||||
}
|
||||
|
||||
impl<const N: usize> VoxelCache<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(
|
||||
device: &Device,
|
||||
page_count: usize,
|
||||
max_requests: usize,
|
||||
layout: &CacheLayout,
|
||||
) -> Self
|
||||
{
|
||||
let mut cache_memory_profile = MemoryProfile::new("VoxelCache");
|
||||
let mut mem_device = cache_memory_profile.begin(device);
|
||||
|
||||
let structure = CacheStructure::new(&mut mem_device.group("CacheStructure"), page_count);
|
||||
let attributes = CacheAttributes::from_cache_layout(
|
||||
&mut mem_device.group("CacheAttributes"),
|
||||
layout,
|
||||
page_count,
|
||||
);
|
||||
let request_handler = CacheRequestHandler::new(
|
||||
&mut mem_device.group("CacheRequestHandler"),
|
||||
page_count,
|
||||
max_requests as u32,
|
||||
);
|
||||
let evictor = CacheEvictor::new(&mut mem_device.group("CacheEvictor"), page_count, 0u32);
|
||||
let common_ressources =
|
||||
CacheRessources::new(&mut mem_device.group("CacheRessources"), page_count);
|
||||
|
||||
let write_requests_pass = WriteRequestsPass::new(device, page_count, layout);
|
||||
let cache_insertion_pass = CacheInsertionPass::new(device, page_count, layout);
|
||||
let cache_invalidation_pass = CacheInvalidationPass::new(device, page_count, layout);
|
||||
let write_pointers_pass = WritePointersPass::new(device, page_count, layout);
|
||||
|
||||
let (raytrace_bind_group_layout, raytrace_bind_group) = Self::create_raytrace_bind_group(
|
||||
device,
|
||||
&structure,
|
||||
&attributes,
|
||||
&request_handler,
|
||||
&evictor,
|
||||
);
|
||||
|
||||
drop(mem_device);
|
||||
let mut root_group = cache_memory_profile.terminate();
|
||||
println!("{}", root_group);
|
||||
|
||||
Self {
|
||||
page_count,
|
||||
structure,
|
||||
attributes,
|
||||
request_handler,
|
||||
evictor,
|
||||
common_ressources,
|
||||
|
||||
write_requests_pass,
|
||||
cache_insertion_pass,
|
||||
cache_invalidation_pass,
|
||||
write_pointers_pass,
|
||||
|
||||
raytrace_bind_group_layout,
|
||||
raytrace_bind_group,
|
||||
|
||||
frame_timestamp: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn gather_requests(
|
||||
&self,
|
||||
device: &Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
request_buffer: &CacheRequestBuffer,
|
||||
)
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "gather_requests");
|
||||
|
||||
self.request_handler.sort_requests(
|
||||
device,
|
||||
&self.common_ressources,
|
||||
&mut encoder,
|
||||
request_buffer.max_requests as u32,
|
||||
);
|
||||
|
||||
self.write_requests_pass.encode(
|
||||
&mut encoder,
|
||||
device,
|
||||
&self.structure,
|
||||
&self.request_handler,
|
||||
request_buffer,
|
||||
);
|
||||
|
||||
self.request_handler.reset(&mut encoder);
|
||||
}
|
||||
|
||||
/// Inserts new pages into the cache. Two insertion modes are available:
|
||||
///
|
||||
/// External: Inserts brand new pages, regardless of the misses in the cache. This can be used
|
||||
/// to bring external requests into the cache
|
||||
///
|
||||
/// Internal: This is the response to requests emited inside the cache (gotten through the
|
||||
/// gather requests method)
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
device: &Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
insertions: &mut [&mut VoxelCacheInsertion<N>],
|
||||
)
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "insertion");
|
||||
let new_timestamp = self.frame_timestamp + 1;
|
||||
|
||||
self.evictor.prepare_eviction_list(
|
||||
device,
|
||||
&self.common_ressources,
|
||||
&self.structure,
|
||||
&mut encoder,
|
||||
);
|
||||
|
||||
// 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) =>
|
||||
{
|
||||
invalidable.validate_insertion(device, &mut encoder, cache_insertion_data);
|
||||
}
|
||||
VoxelCacheInsertion::InternalRequest(cache_insertion_data) =>
|
||||
{
|
||||
self.cache_insertion_pass.encode_validation(
|
||||
device,
|
||||
&mut encoder,
|
||||
&self.attributes,
|
||||
&self.structure,
|
||||
cache_insertion_data,
|
||||
&self.evictor,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for insertion in insertions.iter()
|
||||
{
|
||||
match insertion
|
||||
{
|
||||
VoxelCacheInsertion::ExternalRequest(cache_insertion_data, _)
|
||||
| VoxelCacheInsertion::InternalRequest(cache_insertion_data) =>
|
||||
{
|
||||
self.cache_insertion_pass.encode(
|
||||
device,
|
||||
&mut encoder,
|
||||
&self.attributes,
|
||||
&self.structure,
|
||||
cache_insertion_data,
|
||||
&self.evictor,
|
||||
new_timestamp,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate external structures pointing inside the cache
|
||||
for insertion in insertions.iter_mut()
|
||||
{
|
||||
if let VoxelCacheInsertion::ExternalRequest(_, invalidable) = insertion
|
||||
{
|
||||
invalidable.invalidate(&mut encoder, &self.evictor, new_timestamp);
|
||||
}
|
||||
}
|
||||
|
||||
// Invalidate cache pages
|
||||
self.cache_invalidation_pass.encode(
|
||||
&mut encoder,
|
||||
&self.structure,
|
||||
&self.evictor,
|
||||
new_timestamp,
|
||||
);
|
||||
|
||||
// Write internal requests pointers
|
||||
for insertion in insertions.iter_mut()
|
||||
{
|
||||
if let VoxelCacheInsertion::InternalRequest(insertion) = insertion
|
||||
{
|
||||
self.write_pointers_pass.encode(
|
||||
&mut encoder,
|
||||
device,
|
||||
&self.structure,
|
||||
insertion,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Proceed to new frame
|
||||
self.frame_timestamp = new_timestamp;
|
||||
}
|
||||
|
||||
pub fn current_timestamp(&self) -> u32
|
||||
{
|
||||
self.frame_timestamp
|
||||
}
|
||||
|
||||
fn create_raytrace_bind_group(
|
||||
device: &wgpu::Device,
|
||||
structure: &CacheStructure<N>,
|
||||
attributes: &CacheAttributes<N>,
|
||||
request_handler: &CacheRequestHandler<N>,
|
||||
evictor: &CacheEvictor,
|
||||
) -> (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"),
|
||||
entries: &[
|
||||
// Structure pointers
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: true },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Request pages
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Usage pages
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 2,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
// Attributes
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 3,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only: false },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: Some(
|
||||
NonZero::new(attribute_count as u32)
|
||||
.expect("Null amount of cache attributes."),
|
||||
),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let attributes_bindings = attributes
|
||||
.attribute_buffers()
|
||||
.iter()
|
||||
.map(|buffer| buffer.as_entire_buffer_binding())
|
||||
.collect::<Vec<_>>();
|
||||
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("VoxelCache::raytrace_bind_group"),
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: structure.pointer_pages().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: request_handler.request_pages_buffer().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: evictor.timestamp_pages_buffer().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: wgpu::BindingResource::BufferArray(&attributes_bindings),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
(bind_group_layout, bind_group)
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group_layout(&self) -> &wgpu::BindGroupLayout
|
||||
{
|
||||
&self.raytrace_bind_group_layout
|
||||
}
|
||||
|
||||
pub fn raytrace_bind_group(&self) -> &wgpu::BindGroup
|
||||
{
|
||||
&self.raytrace_bind_group
|
||||
}
|
||||
|
||||
// For debugging
|
||||
pub fn usage_timestamps(&self) -> &wgpu::Buffer
|
||||
{
|
||||
self.evictor.timestamp_pages_buffer()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,515 @@
|
||||
use wgpu::BufferUsages;
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::ComputePipeline;
|
||||
use wgpu::Device;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use vxls_gpu::indirect_buffer::IndirectBuffer1D;
|
||||
use vxls_profiler;
|
||||
use vxls_profiler::ScopeEncoder;
|
||||
use crate::attributes::CacheAttributes;
|
||||
use crate::create_multi_buffers_bg_bg_layout;
|
||||
use crate::eviction::CacheEvictor;
|
||||
use crate::insertion::CacheInsertionData;
|
||||
use crate::layout::CacheLayout;
|
||||
use crate::requests::CacheRequestBuffer;
|
||||
use crate::requests::CacheRequestHandler;
|
||||
use crate::structure::CacheStructure;
|
||||
|
||||
macro_rules! programmatic_common_wgsl {
|
||||
($children_count:ident, $page_count:ident, $cache_layout:ident) => {
|
||||
format!(
|
||||
"
|
||||
const CHILDREN_COUNT: u32 = u32({});
|
||||
const PAGE_COUNT: u32 = u32({});
|
||||
|
||||
// Cache layout
|
||||
{}
|
||||
",
|
||||
$children_count * $children_count * $children_count,
|
||||
$page_count,
|
||||
$cache_layout.wgsl_layout_decl()
|
||||
)
|
||||
.as_str()
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! include_cache_wgsl {
|
||||
($path:literal, $children_count:ident, $page_count:ident, $cache_layout:ident) => {{
|
||||
let mut full_shader = String::new();
|
||||
|
||||
full_shader.push_str(include_str!("./shaders/common.wgsl"));
|
||||
full_shader.push_str(programmatic_common_wgsl!(
|
||||
$children_count,
|
||||
$page_count,
|
||||
$cache_layout
|
||||
));
|
||||
full_shader.push_str(include_str!("./shaders/cache_common.wgsl"));
|
||||
full_shader.push_str(include_str!($path));
|
||||
|
||||
wgpu::ShaderModuleDescriptor {
|
||||
label: Some($path),
|
||||
source: wgpu::ShaderSource::Wgsl(full_shader.into()),
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub(super) use include_cache_wgsl;
|
||||
|
||||
pub struct WriteRequestsPass<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pipeline: ComputePipeline,
|
||||
// To compute indirect dispatch count from request count
|
||||
indirect_buffer: IndirectBuffer1D,
|
||||
}
|
||||
|
||||
impl<const N: usize> WriteRequestsPass<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(device: &Device, page_count: usize, cache_layout: &CacheLayout) -> Self
|
||||
{
|
||||
// Binds
|
||||
// - structure
|
||||
// - request buffer
|
||||
|
||||
let bindings = [
|
||||
// To get the location information
|
||||
Some(&CacheStructure::<N>::create_bind_group_layout(device)),
|
||||
// To get which pages have had a request issued on them
|
||||
Some(&CacheRequestHandler::<N>::create_bind_group_layout(device)),
|
||||
// To write the request information to target
|
||||
Some(&CacheRequestBuffer::create_bind_group_layout(device)),
|
||||
];
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("WriteRequestsPass::pipeline_layout"),
|
||||
bind_group_layouts: &bindings,
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let shader_module = device.create_shader_module(include_cache_wgsl!(
|
||||
"./shaders/write_requests.wgsl",
|
||||
N,
|
||||
page_count,
|
||||
cache_layout
|
||||
));
|
||||
|
||||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("WriteRequestsPass::pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("write_requests"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline,
|
||||
indirect_buffer: IndirectBuffer1D::new(device),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode(
|
||||
&self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
device: &Device,
|
||||
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(
|
||||
device,
|
||||
&mut encoder,
|
||||
cache_request_handler.request_count_buffer(),
|
||||
256,
|
||||
);
|
||||
|
||||
encoder.copy_buffer_to_buffer(
|
||||
cache_request_handler.request_count_buffer(),
|
||||
0,
|
||||
&target_request_buffer.request_count,
|
||||
0,
|
||||
cache_request_handler.request_count_buffer().size(),
|
||||
);
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("WriteRequestsPass::pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, cache_structure.bind_group(), &[]);
|
||||
pass.set_bind_group(1, cache_request_handler.bind_group(), &[]);
|
||||
pass.set_bind_group(2, target_request_buffer.bind_group(), &[]);
|
||||
|
||||
pass.dispatch_workgroups_indirect(indirect_dispatch_buffer, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts new pages in the cache
|
||||
pub struct CacheInsertionPass<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
/// Copies new pages into cache using LRU order
|
||||
write_pipeline: ComputePipeline,
|
||||
|
||||
/// Validates internal insertions and compacts their indices
|
||||
validate_internal_pipeline: ComputePipeline,
|
||||
|
||||
/// u32 buffer containing amount of inserted data of current pass
|
||||
inserted_count: wgpu::Buffer,
|
||||
inserted_count_bind_group: wgpu::BindGroup,
|
||||
|
||||
/// Reports the consumed pages to the evictor by incrementing the consumed pages count
|
||||
consume_pipeline: ComputePipeline,
|
||||
|
||||
/// To dispatch the correct amount of writters depending on the amout of pages to insert
|
||||
indirect_dispatcher: IndirectBuffer1D,
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheInsertionPass<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(device: &Device, page_count: usize, layout: &CacheLayout) -> Self
|
||||
{
|
||||
// Binds
|
||||
// - structure
|
||||
// - request buffer
|
||||
|
||||
let inserted_count = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheInsertionPass::inserted_count"),
|
||||
contents: bytemuck::bytes_of(&0u32),
|
||||
usage: BufferUsages::STORAGE,
|
||||
});
|
||||
|
||||
let (inserted_count_bind_group_layout, inserted_count_bind_group) =
|
||||
create_multi_buffers_bg_bg_layout(
|
||||
device,
|
||||
&[&inserted_count],
|
||||
Some("CacheInsertionPass::inserted_count_bind_group"),
|
||||
);
|
||||
|
||||
let bindings = [
|
||||
// To write the new attributes
|
||||
Some(&CacheAttributes::<N>::create_bind_group_layout(
|
||||
device, layout,
|
||||
)),
|
||||
// To write the new locations, pointers
|
||||
Some(&CacheStructure::<N>::create_bind_group_layout(device)),
|
||||
// New incoming attributes
|
||||
Some(&CacheInsertionData::<N>::create_bind_group_layout(
|
||||
device,
|
||||
Some(layout),
|
||||
)),
|
||||
// To get recyclable pages, and mark pages dirty
|
||||
Some(&CacheEvictor::create_bind_group_layout(device)),
|
||||
Some(&inserted_count_bind_group_layout),
|
||||
];
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("CacheInsertionPass::pipeline_layout"),
|
||||
bind_group_layouts: &bindings,
|
||||
|
||||
// To pass the new timestamp
|
||||
immediate_size: size_of::<u32>() as u32,
|
||||
});
|
||||
|
||||
let shader_module = device.create_shader_module(include_cache_wgsl!(
|
||||
"./shaders/insertion.wgsl",
|
||||
N,
|
||||
page_count,
|
||||
layout
|
||||
));
|
||||
|
||||
let write_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheInsertionPass::write_pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("insertion"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let consume_pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheInsertionPass::consume_pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("consume"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let validate_internal_pipeline =
|
||||
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheInsertionPass::validate_internal_pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("validate_internal"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
write_pipeline,
|
||||
validate_internal_pipeline,
|
||||
consume_pipeline,
|
||||
inserted_count,
|
||||
inserted_count_bind_group,
|
||||
indirect_dispatcher: IndirectBuffer1D::new(device),
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates internal insertions and compacts them into the insertion data's
|
||||
/// `valid_indices` buffer.
|
||||
///
|
||||
/// External roots are validated by the structure table that requested them.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn encode_validation(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
cache_attributes: &CacheAttributes<N>,
|
||||
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(
|
||||
device,
|
||||
&mut encoder,
|
||||
insertion_data.page_count_buffer(),
|
||||
256,
|
||||
);
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("CacheInsertionPass::validation_pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
pass.set_bind_group(0, cache_attributes.bind_group(), &[]);
|
||||
pass.set_bind_group(1, cache_structure.bind_group(), &[]);
|
||||
pass.set_bind_group(2, insertion_data.bind_group(), &[]);
|
||||
pass.set_bind_group(3, cache_evictor.bind_group(), &[]);
|
||||
pass.set_bind_group(4, &self.inserted_count_bind_group, &[]);
|
||||
|
||||
pass.set_pipeline(&self.validate_internal_pipeline);
|
||||
pass.dispatch_workgroups_indirect(indirect_write_dispatch, 0);
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn encode(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
cache_attributes: &CacheAttributes<N>,
|
||||
cache_structure: &CacheStructure<N>,
|
||||
insertion_data: &CacheInsertionData<N>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
timestamp: u32,
|
||||
)
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "PageInsertion");
|
||||
|
||||
let indirect_write_dispatch = self.indirect_dispatcher.workgroup_count(
|
||||
device,
|
||||
&mut encoder,
|
||||
insertion_data.page_count_buffer(),
|
||||
256,
|
||||
);
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("CacheInsertionPass::pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
pass.set_bind_group(0, cache_attributes.bind_group(), &[]);
|
||||
pass.set_bind_group(1, cache_structure.bind_group(), &[]);
|
||||
pass.set_bind_group(2, insertion_data.bind_group(), &[]);
|
||||
pass.set_bind_group(3, cache_evictor.bind_group(), &[]);
|
||||
pass.set_bind_group(4, &self.inserted_count_bind_group, &[]);
|
||||
|
||||
// Write pass
|
||||
pass.set_pipeline(&self.write_pipeline);
|
||||
pass.set_immediates(0, bytemuck::bytes_of(×tamp));
|
||||
pass.dispatch_workgroups_indirect(indirect_write_dispatch, 0);
|
||||
|
||||
// Consume pass
|
||||
pass.set_pipeline(&self.consume_pipeline);
|
||||
pass.set_immediates(0, bytemuck::bytes_of(&0u32)); // Unused
|
||||
pass.dispatch_workgroups(1, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Invalidates pointers that are pointing to recycled pages
|
||||
pub struct CacheInvalidationPass<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
workgroup_count: u32,
|
||||
pipeline: ComputePipeline,
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheInvalidationPass<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(device: &Device, page_count: usize, layout: &CacheLayout) -> Self
|
||||
{
|
||||
// Binds
|
||||
// - structure
|
||||
// - request buffer
|
||||
|
||||
let bindings = [
|
||||
// To find where pages are pointing
|
||||
Some(&CacheStructure::<N>::create_bind_group_layout(device)),
|
||||
// To read timestamp on which invalidation is marked
|
||||
Some(&CacheEvictor::create_bind_group_layout(device)),
|
||||
];
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("CacheInvalidationPass::pipeline_layout"),
|
||||
bind_group_layouts: &bindings,
|
||||
|
||||
// For setting the correct timestamp after invalidation
|
||||
immediate_size: size_of::<u32>() as u32,
|
||||
});
|
||||
|
||||
let shader_module = device.create_shader_module(include_cache_wgsl!(
|
||||
"./shaders/invalidation.wgsl",
|
||||
N,
|
||||
page_count,
|
||||
layout
|
||||
));
|
||||
|
||||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheInvalidationPass::pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("invalidation"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
workgroup_count: page_count.div_ceil(256) as u32,
|
||||
pipeline,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode(
|
||||
&self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
cache_structure: &CacheStructure<N>,
|
||||
cache_evictor: &CacheEvictor,
|
||||
timestamp: u32,
|
||||
)
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "CacheInvalidation");
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("CacheInvalidationPass::pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, cache_structure.bind_group(), &[]);
|
||||
pass.set_bind_group(1, cache_evictor.bind_group(), &[]);
|
||||
|
||||
pass.set_immediates(0, bytemuck::bytes_of(×tamp));
|
||||
|
||||
pass.dispatch_workgroups(self.workgroup_count, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// Points the pages to the newly cached pages
|
||||
pub struct WritePointersPass<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pipeline: ComputePipeline,
|
||||
indirect_buffer: IndirectBuffer1D,
|
||||
}
|
||||
|
||||
impl<const N: usize> WritePointersPass<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub fn new(device: &Device, page_count: usize, layout: &CacheLayout) -> Self
|
||||
{
|
||||
let bindings = [
|
||||
// To find where pages are pointing
|
||||
Some(&CacheStructure::<N>::create_bind_group_layout(device)),
|
||||
// To find where the request's response went
|
||||
Some(&CacheInsertionData::<N>::create_bind_group_layout(
|
||||
device,
|
||||
Some(layout),
|
||||
)),
|
||||
];
|
||||
|
||||
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("WritePointersPass::pipeline_layout"),
|
||||
bind_group_layouts: &bindings,
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let shader_module = device.create_shader_module(include_cache_wgsl!(
|
||||
"./shaders/write_pointers.wgsl",
|
||||
N,
|
||||
page_count,
|
||||
layout
|
||||
));
|
||||
|
||||
let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("WritePointersPass::pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader_module,
|
||||
entry_point: Some("write_pointers"),
|
||||
compilation_options: Default::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline,
|
||||
indirect_buffer: IndirectBuffer1D::new(device),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn encode(
|
||||
&self,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
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(
|
||||
device,
|
||||
&mut encoder,
|
||||
cache_insertion_data.page_count_buffer(),
|
||||
256,
|
||||
);
|
||||
|
||||
let mut pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("WritePointersPass::pass"),
|
||||
timestamp_writes: None,
|
||||
});
|
||||
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, cache_structure.bind_group(), &[]);
|
||||
pass.set_bind_group(1, cache_insertion_data.bind_group(), &[]);
|
||||
|
||||
pass.dispatch_workgroups_indirect(indirect_buffer, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use wgpu::BindGroup;
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::CommandEncoder;
|
||||
use wgpu::ComputePipeline;
|
||||
use wgpu::Device;
|
||||
use wgpu::include_wgsl;
|
||||
use wgpu::util::DeviceExt;
|
||||
|
||||
use vxls_gpu::create_multi_buffers_bind_group;
|
||||
use vxls_gpu::indirect_buffer::IndirectBuffer1D;
|
||||
use vxls_gpu::radix_sort::{self};
|
||||
use vxls_gpu::top_k::TopKSelector;
|
||||
use vxls_profiler::ScopeEncoder;
|
||||
use vxls_profiler::memory_profiler;
|
||||
use vxls_profiler::{self};
|
||||
use crate::CacheRessources;
|
||||
use crate::create_multi_buffers_bg_bg_layout;
|
||||
use crate::create_multi_buffers_bind_group_layout;
|
||||
use crate::schema::RequestInformation;
|
||||
use crate::schema::RequestMeta;
|
||||
|
||||
/// Records, handles, and produces a list of requested data inside the cache
|
||||
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,
|
||||
|
||||
/// Ultimately contains sorted requests
|
||||
sorted_requests: wgpu::Buffer,
|
||||
|
||||
/// Number of requests
|
||||
request_count: wgpu::Buffer,
|
||||
|
||||
/// Number of request counts in `request_counts_pages`
|
||||
element_count: u32,
|
||||
|
||||
/// Selects the most requested pages
|
||||
top_k: TopKSelector,
|
||||
|
||||
/// Gathers the selected counts and indices into contiguous sort buffers
|
||||
gather_pass: ComputePipeline,
|
||||
gather_pass_bind_group_layout: BindGroupLayout,
|
||||
gather_indirect_buffer: IndirectBuffer1D,
|
||||
|
||||
// Pass to reset the requests
|
||||
reset_pass_bind_group: BindGroup,
|
||||
reset_pass: ComputePipeline,
|
||||
reset_pass_dispatch: u32,
|
||||
|
||||
// External bind group
|
||||
bind_group_layout: BindGroupLayout,
|
||||
bind_group: BindGroup,
|
||||
}
|
||||
|
||||
/// Records requests made by the cache
|
||||
pub struct CacheRequestBuffer
|
||||
{
|
||||
pub max_requests: usize,
|
||||
|
||||
/// Buffer containing the amount of requests made by the cache, under max requsets
|
||||
pub request_count: wgpu::Buffer,
|
||||
|
||||
/// Buffer containing the requsets made by the cache, a dense array of [`voxel_cache::schema::RequestInformation`]
|
||||
pub request_informations: wgpu::Buffer,
|
||||
|
||||
/// Buffer containing the meta information about the requests, a dense array of [`voxel_cache::schema::RequestMeta`]
|
||||
request_meta: wgpu::Buffer,
|
||||
|
||||
bind_group_layout: wgpu::BindGroupLayout,
|
||||
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
|
||||
{
|
||||
let element_count = page_count * N * N * N;
|
||||
assert!(
|
||||
element_count <= u32::MAX as usize,
|
||||
"CacheRequestHandler: too many request pages ({element_count})"
|
||||
);
|
||||
let element_count = element_count as u32;
|
||||
|
||||
let request_counts_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheRequestHandler::request_counts_pages"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
contents: bytemuck::cast_slice(&vec![0u32; element_count as usize]),
|
||||
});
|
||||
|
||||
let sorted_requests = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheRequestHandler::sorted_requests"),
|
||||
usage: wgpu::BufferUsages::STORAGE,
|
||||
contents: bytemuck::cast_slice(&vec![0u32; element_count as usize]),
|
||||
});
|
||||
|
||||
let request_count = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheRequestHandler::request_count"),
|
||||
usage: wgpu::BufferUsages::STORAGE
|
||||
| wgpu::BufferUsages::COPY_DST
|
||||
| wgpu::BufferUsages::COPY_SRC,
|
||||
contents: bytemuck::bytes_of(&0u32),
|
||||
});
|
||||
|
||||
let top_k = TopKSelector::new(device, max_selected);
|
||||
|
||||
let gather_pass_bind_group_layout = Self::create_gather_bind_group_layout(device);
|
||||
let gather_pass_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("CacheRequestHandler::gather_pass_pipeline_layout"),
|
||||
bind_group_layouts: &[Some(&gather_pass_bind_group_layout)],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let gather_pass = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheRequestHandler::gather_pass"),
|
||||
layout: Some(&gather_pass_pipeline_layout),
|
||||
module: &device.create_shader_module(include_wgsl!("./shaders/gather_requests.wgsl")),
|
||||
entry_point: Some("gather_requests"),
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let (reset_pass_bg_layout, reset_pass_bind_group) = create_multi_buffers_bg_bg_layout(
|
||||
device,
|
||||
&[&request_counts_pages],
|
||||
Some("CacheRequestHandler::reset_pass_bind_group"),
|
||||
);
|
||||
|
||||
let reset_pass_pipeline_layout =
|
||||
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||
label: Some("CacheRequestHandler::reset_pass_pipeline_layout"),
|
||||
bind_group_layouts: &[Some(&reset_pass_bg_layout)],
|
||||
immediate_size: 0,
|
||||
});
|
||||
|
||||
let reset_pass = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
label: Some("CacheRequestHandler::reset_pass"),
|
||||
layout: Some(&reset_pass_pipeline_layout),
|
||||
module: &device.create_shader_module(include_wgsl!("./shaders/requests.wgsl")),
|
||||
entry_point: Some("reset_requests"),
|
||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||
cache: None,
|
||||
});
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device);
|
||||
let bind_group = create_multi_buffers_bind_group(
|
||||
device,
|
||||
&bind_group_layout,
|
||||
&[&request_count, &sorted_requests, &request_counts_pages],
|
||||
Some("CacheRequestHandler::bind_group"),
|
||||
);
|
||||
|
||||
Self {
|
||||
request_counts_pages,
|
||||
sorted_requests,
|
||||
request_count,
|
||||
element_count,
|
||||
top_k,
|
||||
gather_pass,
|
||||
gather_pass_bind_group_layout,
|
||||
gather_indirect_buffer: IndirectBuffer1D::new(device),
|
||||
|
||||
reset_pass_bind_group,
|
||||
reset_pass,
|
||||
reset_pass_dispatch: element_count.div_ceil(256),
|
||||
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
fn create_gather_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
|
||||
{
|
||||
let entry = |binding: u32, read_only: bool| wgpu::BindGroupLayoutEntry {
|
||||
binding,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::Buffer {
|
||||
ty: wgpu::BufferBindingType::Storage { read_only },
|
||||
has_dynamic_offset: false,
|
||||
min_binding_size: None,
|
||||
},
|
||||
count: None,
|
||||
};
|
||||
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("CacheRequestHandler::gather_pass_bind_group_layout"),
|
||||
entries: &[
|
||||
entry(0, true), // requests
|
||||
entry(1, true), // selected_indices
|
||||
entry(2, true), // selected_count
|
||||
entry(3, false), // keys
|
||||
entry(4, false), // values
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
/// Sorts the requests currently held by the request handler in order to process them.
|
||||
///
|
||||
/// Uses both the sorter and the scratch buffer in the ressources
|
||||
///
|
||||
/// The `request_count` buffer will contain the amount of request produced this time.
|
||||
pub fn sort_requests(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
ressources: &CacheRessources<N>,
|
||||
encoder: &mut ScopeEncoder<CommandEncoder>,
|
||||
k: u32,
|
||||
)
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "RequestHandler::sort_requests");
|
||||
|
||||
// Select the most requested pages.
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "top_k");
|
||||
self.top_k.select(
|
||||
device,
|
||||
&mut encoder,
|
||||
&self.request_counts_pages,
|
||||
self.element_count,
|
||||
k,
|
||||
);
|
||||
}
|
||||
|
||||
// Gather the selected counts and indices into contiguous sort buffers.
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "gather");
|
||||
|
||||
let gather_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("CacheRequestHandler::gather_bind_group"),
|
||||
layout: &self.gather_pass_bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: self.request_counts_pages.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: self.top_k.selected_indices_buffer().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 2,
|
||||
resource: self.top_k.selected_count_buffer().as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 3,
|
||||
resource: ressources.scratch_buffer.as_entire_binding(),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 4,
|
||||
resource: self.sorted_requests.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let gather_dispatch = self.gather_indirect_buffer.workgroup_count(
|
||||
device,
|
||||
&mut encoder,
|
||||
self.top_k.selected_count_buffer(),
|
||||
256,
|
||||
);
|
||||
|
||||
let mut compute_pass = encoder.begin_profiled_compute_pass("gather");
|
||||
compute_pass.set_pipeline(&self.gather_pass);
|
||||
compute_pass.set_bind_group(0, &gather_bind_group, &[]);
|
||||
compute_pass.dispatch_workgroups_indirect(gather_dispatch, 0);
|
||||
}
|
||||
|
||||
// Sort the selected requests by descending request count. The scratch
|
||||
// buffer holds the keys (counts) and `sorted_requests` the values
|
||||
// (indices). `selected_count` is the indirect element count.
|
||||
{
|
||||
vxls_profiler::begin_scope!(encoder, "radix");
|
||||
ressources.radix_sorter.sort(
|
||||
device,
|
||||
&ressources.scratch_buffer,
|
||||
&self.sorted_requests,
|
||||
Some(self.top_k.selected_count_buffer()),
|
||||
None,
|
||||
&mut encoder,
|
||||
radix_sort::RadixSortDirection::Descending,
|
||||
);
|
||||
}
|
||||
|
||||
// Publish the amount of selected requests for downstream passes.
|
||||
encoder.copy_buffer_to_buffer(
|
||||
self.top_k.selected_count_buffer(),
|
||||
0,
|
||||
&self.request_count,
|
||||
0,
|
||||
size_of::<u32>() as u64,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn create_bind_group_layout(device: &wgpu::Device) -> BindGroupLayout
|
||||
{
|
||||
create_multi_buffers_bind_group_layout(
|
||||
device,
|
||||
3,
|
||||
Some("CacheRequestHandler::bind_group_layout"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
pub fn request_count_buffer(&self) -> &wgpu::Buffer
|
||||
{
|
||||
&self.request_count
|
||||
}
|
||||
|
||||
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>)
|
||||
{
|
||||
let mut compute_pass = encoder.begin_profiled_compute_pass("CacheRequestHandler::reset");
|
||||
|
||||
compute_pass.set_pipeline(&self.reset_pass);
|
||||
compute_pass.set_bind_group(0, Some(&self.reset_pass_bind_group), &[]);
|
||||
compute_pass.dispatch_workgroups(self.reset_pass_dispatch, 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
impl CacheRequestBuffer
|
||||
{
|
||||
/// Creates a request buffer that can contain at most `max_amount` requests
|
||||
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
|
||||
| wgpu::BufferUsages::COPY_SRC
|
||||
| wgpu::BufferUsages::COPY_DST,
|
||||
contents: bytemuck::bytes_of(&0u32),
|
||||
});
|
||||
|
||||
let request_informations = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheRequestBuffer::request_informations"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
size: (size_of::<RequestInformation>() * max_requests) as u64,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let request_meta = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheRequestBuffer::request_meta"),
|
||||
usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
|
||||
size: (size_of::<RequestMeta>() * max_requests) as u64,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device);
|
||||
let bind_group = create_multi_buffers_bind_group(
|
||||
device,
|
||||
&bind_group_layout,
|
||||
&[&request_count, &request_informations, &request_meta],
|
||||
Some("CacheRequestBuffer::bind_group"),
|
||||
);
|
||||
|
||||
Self {
|
||||
max_requests,
|
||||
request_count,
|
||||
request_informations,
|
||||
request_meta,
|
||||
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_bind_group_layout(device: &Device) -> BindGroupLayout
|
||||
{
|
||||
create_multi_buffers_bind_group_layout(
|
||||
device,
|
||||
3,
|
||||
Some("CacheRequestBuffer::bind_group_layout"),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
pub fn host_download_requests<
|
||||
F: FnMut(&[RequestInformation], &[RequestMeta]) + Send + Sync + 'static,
|
||||
>(
|
||||
&self,
|
||||
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)));
|
||||
|
||||
let request_map_state_1 = request_map_state.clone();
|
||||
wgpu::util::DownloadBuffer::read_buffer(
|
||||
device,
|
||||
queue,
|
||||
&self.request_count.slice(..),
|
||||
move |buffer| {
|
||||
let mapped_buffer = buffer.expect("Could not map request buffer (count).");
|
||||
let request_count: u32 = bytemuck::pod_read_unaligned(&mapped_buffer);
|
||||
|
||||
request_map_state_1
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_request_count(request_count as usize);
|
||||
},
|
||||
);
|
||||
|
||||
let request_map_state_2 = request_map_state.clone();
|
||||
wgpu::util::DownloadBuffer::read_buffer(
|
||||
device,
|
||||
queue,
|
||||
&self.request_informations.slice(..),
|
||||
move |buffer| {
|
||||
let mapped_buffer = buffer.expect("Could not map request buffer (informations).");
|
||||
let request_infos: Vec<RequestInformation> =
|
||||
bytemuck::pod_collect_to_vec(&mapped_buffer);
|
||||
|
||||
request_map_state_2
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_request_infos(request_infos);
|
||||
},
|
||||
);
|
||||
|
||||
let request_map_state_3 = request_map_state.clone();
|
||||
wgpu::util::DownloadBuffer::read_buffer(
|
||||
device,
|
||||
queue,
|
||||
&self.request_meta.slice(..),
|
||||
move |buffer| {
|
||||
let mapped_buffer = buffer.expect("Could not map request buffer (meta).");
|
||||
let request_metas: Vec<RequestMeta> =
|
||||
bytemuck::pod_collect_to_vec(&mapped_buffer);
|
||||
|
||||
request_map_state_3
|
||||
.lock()
|
||||
.unwrap()
|
||||
.set_request_metas(request_metas);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
Self {
|
||||
request_count: None,
|
||||
request_infos: None,
|
||||
request_metas: None,
|
||||
callback,
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
)
|
||||
{
|
||||
(self.callback)(
|
||||
&request_infos[0..request_count],
|
||||
&request_metas[0..request_count],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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>)
|
||||
{
|
||||
self.request_infos = Some(request_infos);
|
||||
self.try_callback();
|
||||
}
|
||||
|
||||
pub fn set_request_metas(&mut self, request_metas: Vec<RequestMeta>)
|
||||
{
|
||||
self.request_metas = Some(request_metas);
|
||||
self.try_callback();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,420 @@
|
||||
/// Contains definitions of the data layouts for the cache
|
||||
use std::ops::BitOr;
|
||||
|
||||
use bytemuck::Pod;
|
||||
use bytemuck::Zeroable;
|
||||
|
||||
/// CachePointer represents a pointer in the [`VoxelCache`]
|
||||
///
|
||||
/// Pointers are used in the structure pool of the cache and encodes the N-tree itself
|
||||
///
|
||||
/// Three states are possible :
|
||||
///
|
||||
/// - unsubdivisable: This children of the node cannot be subdivided further
|
||||
/// - subdivisable: This children of the node can be subdivided further ...
|
||||
/// - unavailable: ... but is not currently available in the cache
|
||||
/// - available: ... is currently available in the cache and the pointer is valid
|
||||
///
|
||||
///
|
||||
/// The layout is the following
|
||||
///
|
||||
/// - bit 31: subdivisable?
|
||||
/// - bit 30: available?
|
||||
/// - bit 0..29: pointer (only relevant if available is 1)
|
||||
///
|
||||
#[derive(Clone, Copy, Debug, Zeroable, Pod)]
|
||||
#[repr(transparent)]
|
||||
pub struct CachePointer(u32);
|
||||
|
||||
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) =>
|
||||
{
|
||||
assert_eq!(
|
||||
pointer >> 30,
|
||||
0,
|
||||
"Provided pointer is to big and does not fit in CachePointer. Last two bits must be null."
|
||||
);
|
||||
(1 << 31) | (1 << 30) | pointer
|
||||
}
|
||||
None => 1 << 31,
|
||||
},
|
||||
None => 0,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
(false, true) => panic!("Invalid cache pointer. (not subdivisable and available)"),
|
||||
(false, _) if pointer != 0 =>
|
||||
{
|
||||
panic!("Invalid cache pointer. (non null unsubdivisable pointer)")
|
||||
}
|
||||
(false, _) => None,
|
||||
(true, false) => Some(None),
|
||||
(true, true) => Some(Some(pointer)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for CachePointer
|
||||
{
|
||||
fn from(value: u32) -> Self
|
||||
{
|
||||
CachePointer(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// Pages encoding the structure of the trees in the cache.
|
||||
/// Occupancy mask is a mask containing a bit per children, based on whether it can be skipped
|
||||
/// during raytracing. This can help faster tracing, and coalesce skips.
|
||||
#[derive(Debug, Clone, Copy, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct StructurePage<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub occupancy_mask: u64,
|
||||
pub pointers: [CachePointer; N * N * N],
|
||||
}
|
||||
|
||||
// SAFETY: u64, then packed u32s ([`CachePointer`]s), so no padding, all bit patterns are allowed
|
||||
unsafe impl<const N: usize> Pod for StructurePage<N> where [(); N * N * N]: {}
|
||||
|
||||
/// Number of `u32` words in a [`StructureAnchor`].
|
||||
pub const ANCHOR_WORDS: usize = 4;
|
||||
|
||||
/// Sentinel used in [`RequestMeta::table_slot`] and
|
||||
/// [`InsertionParent::table_slot`] to mark an insertion that is not attached to
|
||||
/// a structure-table slot (internal requests).
|
||||
pub const NO_TABLE_SLOT: u32 = u32::MAX;
|
||||
|
||||
/// Opaque, table-defined identity of a structure's *content*.
|
||||
///
|
||||
/// The cache and the request queue never interpret these words. A table decides
|
||||
/// what an anchor means (for example a world position plus a version) and is
|
||||
/// responsible for never exposing two live slots with the same anchor. The
|
||||
/// anchor travels on the wire so producers stay stateless and so stale
|
||||
/// 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 words: [u32; ANCHOR_WORDS],
|
||||
}
|
||||
|
||||
impl StructureAnchor
|
||||
{
|
||||
/// Convenience anchor used by tests and by tables that do not need a
|
||||
/// distinguishable identity at startup.
|
||||
#[allow(dead_code)]
|
||||
pub const ZERO: Self = Self {
|
||||
words: [0; ANCHOR_WORDS],
|
||||
};
|
||||
|
||||
/// Table-defined helper for world-position anchors: `(x, y, z, version)`.
|
||||
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)
|
||||
{
|
||||
(
|
||||
self.words[0] as i32,
|
||||
self.words[1] as i32,
|
||||
self.words[2] as i32,
|
||||
self.words[3],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Data stored alongside cache structure so that application can locate a
|
||||
/// particular request
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
pub struct LocationPage
|
||||
{
|
||||
/// Identifier for locating this particular node within the structure
|
||||
pub node_id: u32,
|
||||
|
||||
/// Identifier of the producer that owns this node
|
||||
pub producer_id: u32,
|
||||
|
||||
/// Opaque content identity inherited from the root request.
|
||||
pub anchor: StructureAnchor,
|
||||
}
|
||||
|
||||
/// Page storing the depth of the current node related to its root.
|
||||
/// - 0xFFFFFFFF: Page unused
|
||||
/// - 0..0xFFFFFFFF(excl): Depth
|
||||
///
|
||||
/// An external insertion gets the 0 depth.
|
||||
/// Internal insertions get the parent's depth plus one.
|
||||
///
|
||||
/// As such, when inserting pages in the cache, we first sort the lru pages by
|
||||
/// depth, deepest first, then apply a stable radix sort by timestamp. The final
|
||||
/// order is least-recently-used first, with deepest pages first among equal
|
||||
/// timestamps. Reusing deepest pages first means that a page's resident children
|
||||
/// are deeper, so they appear earlier in the list and are reused first (provided
|
||||
/// their timestamp is equal or older) instead of being orphaned when their
|
||||
/// parent is recycled.
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
pub struct DepthPage(pub u32);
|
||||
|
||||
impl DepthPage
|
||||
{
|
||||
pub const PAGE_UNUSED: Self = DepthPage(u32::MAX);
|
||||
}
|
||||
|
||||
/// When the cache requests some information, it produces this structure,
|
||||
/// which contains the node on which a subdivision is requested, as well which particular
|
||||
/// children is needed
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
pub struct RequestInformation
|
||||
{
|
||||
/// Identifier for locating this particular node within the structure
|
||||
pub node_id: u32,
|
||||
|
||||
/// The index of the children on which the request was issued
|
||||
pub children_index: u32,
|
||||
|
||||
/// Identifier of the producer that owns the requesting node
|
||||
pub producer_id: u32,
|
||||
|
||||
/// The amount of times this request was emitted this frame. Higher means more urgent.
|
||||
pub demand: u32,
|
||||
|
||||
/// Opaque content identity of the structure this request belongs to.
|
||||
pub anchor: StructureAnchor,
|
||||
}
|
||||
|
||||
/// Metainformation kept along each request, to know which node made the demand
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
|
||||
#[repr(C)]
|
||||
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,
|
||||
|
||||
/// The children on which the request was issued
|
||||
pub children_index: u32,
|
||||
|
||||
/// The depth on which the request was issued
|
||||
pub depth: u32,
|
||||
|
||||
/// Table slot for an external root request; [`NO_TABLE_SLOT`] for internal
|
||||
/// requests.
|
||||
pub table_slot: u32,
|
||||
}
|
||||
|
||||
/// Sentinel used in [`RequestMeta::page_index`] and [`InsertionParent::page_index`] to mark a
|
||||
/// request/insertion that has no cache parent (an external root requested by a structure table).
|
||||
pub const NO_PARENT_PAGE: u32 = u32::MAX;
|
||||
|
||||
/// Kind of parent an insertion is attached to.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Zeroable, Pod)]
|
||||
#[repr(transparent)]
|
||||
pub struct InsertionParentKind(u32);
|
||||
|
||||
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
|
||||
{
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Identity and attachment point of the page being inserted.
|
||||
///
|
||||
/// This makes an insertion batch self-describing: the GPU can validate that the parent is still
|
||||
/// 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
|
||||
{
|
||||
/// [`InsertionParentKind`].
|
||||
pub kind: u32,
|
||||
|
||||
/// Cache page of the parent. [`NO_PARENT_PAGE`] for external roots.
|
||||
pub page_index: u32,
|
||||
|
||||
/// Child slot inside the parent. `0` for external roots.
|
||||
pub children_index: u32,
|
||||
|
||||
/// Depth of the parent. [`NO_PARENT_PAGE`] for external roots.
|
||||
pub depth: u32,
|
||||
|
||||
/// Locator of the parent inside the structure. `0` for external roots.
|
||||
pub node_id: u32,
|
||||
|
||||
/// Producer that owns the parent.
|
||||
pub producer_id: u32,
|
||||
|
||||
/// Opaque content identity of the structure the parent belongs to.
|
||||
pub anchor: StructureAnchor,
|
||||
|
||||
/// Table slot of an external root; [`NO_TABLE_SLOT`] for internal
|
||||
/// insertions.
|
||||
pub table_slot: u32,
|
||||
}
|
||||
|
||||
impl InsertionParent
|
||||
{
|
||||
/// Parent information for a child of a resident cache page.
|
||||
pub fn internal(
|
||||
meta: &RequestMeta,
|
||||
node_id: u32,
|
||||
producer_id: u32,
|
||||
anchor: StructureAnchor,
|
||||
) -> Self
|
||||
{
|
||||
Self {
|
||||
kind: InsertionParentKind::INTERNAL.raw(),
|
||||
page_index: meta.page_index,
|
||||
children_index: meta.children_index,
|
||||
depth: meta.depth,
|
||||
node_id,
|
||||
producer_id,
|
||||
anchor,
|
||||
table_slot: NO_TABLE_SLOT,
|
||||
}
|
||||
}
|
||||
|
||||
/// Parent information for the root of an external structure.
|
||||
pub fn external(producer_id: u32, anchor: StructureAnchor, table_slot: u32) -> Self
|
||||
{
|
||||
Self {
|
||||
kind: InsertionParentKind::EXTERNAL.raw(),
|
||||
page_index: NO_PARENT_PAGE,
|
||||
children_index: 0,
|
||||
depth: NO_PARENT_PAGE,
|
||||
node_id: 0,
|
||||
producer_id,
|
||||
anchor,
|
||||
table_slot,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_external(&self) -> bool
|
||||
{
|
||||
self.kind == InsertionParentKind::EXTERNAL.raw()
|
||||
}
|
||||
|
||||
pub fn is_internal(&self) -> bool
|
||||
{
|
||||
self.kind == InsertionParentKind::INTERNAL.raw()
|
||||
}
|
||||
}
|
||||
|
||||
/// Structure information about the children of a node.
|
||||
/// Used when inserting data in the cache, to setup structure pointers
|
||||
///
|
||||
/// A specific child of a node being inserted can be in four state by
|
||||
///
|
||||
/// Whether it can be subdivided further or not
|
||||
/// Whether it is considered occupied or not
|
||||
/// 31st bit : subdivisable?
|
||||
/// 30th bit : occupied?
|
||||
#[derive(Clone, Copy, Debug, Zeroable, Pod)]
|
||||
#[repr(transparent)]
|
||||
pub struct ChildInformation(u32);
|
||||
|
||||
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
|
||||
{
|
||||
let mut n = Self::NONE;
|
||||
if subdivisable
|
||||
{
|
||||
n = n | Self::SUBDIVISABLE;
|
||||
}
|
||||
|
||||
if occupied
|
||||
{
|
||||
n = n | Self::OCCUPIED;
|
||||
}
|
||||
|
||||
n
|
||||
}
|
||||
}
|
||||
|
||||
impl BitOr for ChildInformation
|
||||
{
|
||||
type Output = Self;
|
||||
|
||||
fn bitor(self, rhs: Self) -> Self::Output
|
||||
{
|
||||
Self(self.0 | rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
/// Information given for an page insertion operation
|
||||
#[derive(Debug, Clone, Copy, Zeroable)]
|
||||
#[repr(C)]
|
||||
pub struct InsertionInformation<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
/// Information to setup structure pointers
|
||||
pub children: [ChildInformation; N * N * N],
|
||||
}
|
||||
|
||||
// SAFETY: Packed array of u32, no padding, all bit patterns allowed
|
||||
unsafe impl<const N: usize> Pod for InsertionInformation<N> where [(); N * N * N]: {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests
|
||||
{
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn wire_layout_sizes_are_stable()
|
||||
{
|
||||
assert_eq!(size_of::<StructureAnchor>(), 16);
|
||||
assert_eq!(size_of::<RequestInformation>(), 32);
|
||||
assert_eq!(size_of::<LocationPage>(), 24);
|
||||
assert_eq!(size_of::<InsertionParent>(), 44);
|
||||
assert_eq!(size_of::<RequestMeta>(), 16);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anchor_world_round_trips_negative_coordinates()
|
||||
{
|
||||
let anchor = StructureAnchor::from_world(-17, 42, -3, 7);
|
||||
assert_eq!(anchor.world(), (-17, 42, -3, 7));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// For things that depend upon the cache layout/children count
|
||||
|
||||
struct PointerPage
|
||||
{
|
||||
occupancy_low: u32,
|
||||
occupancy_high: u32,
|
||||
pointers: array<u32, CHILDREN_COUNT>,
|
||||
}
|
||||
|
||||
struct InsertionInformation
|
||||
{
|
||||
/// [`ChildInformation`] in schema.rs
|
||||
children: array<u32, CHILDREN_COUNT>
|
||||
}
|
||||
|
||||
// Takes index of pointer and gives cache index
|
||||
fn pointer_index_to_cache_index(request_index: u32) -> CacheIndex
|
||||
{
|
||||
var cache_index: CacheIndex;
|
||||
cache_index.page_index = request_index / CHILDREN_COUNT;
|
||||
cache_index.children_index = request_index % CHILDREN_COUNT;
|
||||
return cache_index;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
enable wgpu_binding_array;
|
||||
|
||||
struct CacheIndex
|
||||
{
|
||||
page_index: u32,
|
||||
children_index: u32,
|
||||
}
|
||||
|
||||
const ANCHOR_WORDS: u32 = 4u;
|
||||
|
||||
struct StructureAnchor
|
||||
{
|
||||
words: array<u32, 4>
|
||||
}
|
||||
|
||||
fn anchor_eq(a: StructureAnchor, b: StructureAnchor) -> bool
|
||||
{
|
||||
for(var i = 0u; i < ANCHOR_WORDS; i++)
|
||||
{
|
||||
if(a.words[i] != b.words[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
struct RequestInformation
|
||||
{
|
||||
node_id: u32,
|
||||
children_index: u32,
|
||||
producer_id: u32,
|
||||
demand: u32,
|
||||
anchor: StructureAnchor
|
||||
}
|
||||
|
||||
struct RequestMeta
|
||||
{
|
||||
page_index: u32,
|
||||
children_index: u32,
|
||||
depth: u32,
|
||||
table_slot: u32
|
||||
}
|
||||
|
||||
// No cache parent (external root insertion).
|
||||
const NO_PARENT_PAGE: u32 = 0xFFFFFFFFu;
|
||||
const INSERTION_PARENT_INTERNAL: u32 = 0u;
|
||||
const INSERTION_PARENT_EXTERNAL: u32 = 1u;
|
||||
// No structure-table slot (internal request/insertion).
|
||||
const NO_TABLE_SLOT: u32 = 0xFFFFFFFFu;
|
||||
|
||||
struct InsertionParent
|
||||
{
|
||||
kind: u32,
|
||||
page_index: u32,
|
||||
children_index: u32,
|
||||
depth: u32,
|
||||
node_id: u32,
|
||||
producer_id: u32,
|
||||
anchor: StructureAnchor,
|
||||
table_slot: u32
|
||||
}
|
||||
|
||||
struct AttributeArray
|
||||
{
|
||||
values: array<u32>
|
||||
}
|
||||
|
||||
struct LocationPage
|
||||
{
|
||||
node_id: u32,
|
||||
producer_id: u32,
|
||||
anchor: StructureAnchor
|
||||
}
|
||||
|
||||
fn child_information_occupied(info: u32) -> bool
|
||||
{
|
||||
return ((info >> 30) & 1) != 0;
|
||||
}
|
||||
|
||||
fn child_information_subdivided(info: u32) -> bool
|
||||
{
|
||||
return ((info >> 31) & 1) != 0;
|
||||
}
|
||||
|
||||
// Makes an empty child pointer, which can be subdivisible or not
|
||||
// but not available
|
||||
fn child_pointer_empty(subdivisible: bool) -> u32
|
||||
{
|
||||
return select(u32(0), u32(1), subdivisible) << 31;
|
||||
}
|
||||
|
||||
fn child_pointer_subdivided(pointer: u32) -> bool
|
||||
{
|
||||
return ((pointer >> 31) & 1) != 0;
|
||||
}
|
||||
|
||||
fn child_pointer_available(pointer: u32) -> bool
|
||||
{
|
||||
// both bits set ?
|
||||
return pointer >> 30 == u32(3);
|
||||
}
|
||||
|
||||
fn child_pointer_pointer(pointer: u32) -> u32
|
||||
{
|
||||
return pointer & u32(0x3FFFFFFF);
|
||||
}
|
||||
|
||||
fn child_pointer_make_available(pointer: u32) -> u32
|
||||
{
|
||||
return pointer | 0xC0000000;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/// Group 0: Lru list
|
||||
@group(0) @binding(0) var<storage, read_write> timestamp_pages: array<u32>;
|
||||
@group(0) @binding(1) var<storage, read_write> lru_list: array<u32>;
|
||||
@group(0) @binding(2) var<storage, read_write> consumed_count: u32;
|
||||
|
||||
/// Group 1: Cache structure
|
||||
@group(1) @binding(0) var<storage, read_write> structure_pointer_pages: array<u32>; // UNUSED
|
||||
@group(1) @binding(1) var<storage, read_write> structure_location_pages: array<u32>; // UNUSED
|
||||
@group(1) @binding(2) var<storage, read_write> structure_depth_pages: array<u32>;
|
||||
|
||||
/// Group 2: Scratch buffer
|
||||
@group(2) @binding(0) var<storage, read_write> scratch_buffer: array<u32>;
|
||||
|
||||
/// Resets the lru list to identity order before the depth sort, and clears the
|
||||
/// number of pages consumed by previous insertions.
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn reset_lru_list(@builtin(global_invocation_id) global_invocation_id: vec3<u32>)
|
||||
{
|
||||
let index = global_invocation_id.x;
|
||||
if(index >= arrayLength(&lru_list))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Indicies must be reset to id, so that the sort operation can use them as values,
|
||||
// and the timestamps as keys
|
||||
lru_list[index] = index;
|
||||
consumed_count = 0;
|
||||
}
|
||||
|
||||
/// Rearranges timestamps into depth order, in between the descending depth sort
|
||||
/// and the stable timestamp sort.
|
||||
///
|
||||
/// `lru_list` currently holds page indices sorted deepest first. Writing each
|
||||
/// page's timestamp to the same position gives the next (stable) radix sort an
|
||||
/// input where equal timestamps preserve the deepest-first depth order.
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn indirect_copy(@builtin(global_invocation_id) global_invocation_id: vec3<u32>)
|
||||
{
|
||||
let index = global_invocation_id.x;
|
||||
if(index >= arrayLength(&lru_list))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Index sorted by depth
|
||||
let gather_index = lru_list[index];
|
||||
|
||||
// Fetch corresponding timestamp
|
||||
let ts = timestamp_pages[gather_index];
|
||||
|
||||
// Copy to corresponding locatoin
|
||||
scratch_buffer[index] = ts;
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Group 0: Table pointers
|
||||
@group(0) @binding(0) var<storage, read_write> fixed_table_pointers: array<u32>; // Request count
|
||||
|
||||
// Group 1: Cache insertion data
|
||||
@group(1) @binding(0) var<storage, read_write> insertion_data_page_count_buffer: u32;
|
||||
@group(1) @binding(1) var<storage, read_write> insertion_data_information: array<u32>; // UNUSED
|
||||
@group(1) @binding(2) var<storage, read_write> insertion_data_location_pages: array<LocationPage>;
|
||||
@group(1) @binding(3) var<storage, read_write> insertion_data_destination_indices: array<u32>;
|
||||
@group(1) @binding(4) var<storage, read_write> insertion_data_parents: array<InsertionParent>;
|
||||
@group(1) @binding(5) var<storage, read_write> insertion_data_valid_indices: array<u32>;
|
||||
@group(1) @binding(6) var<storage, read_write> insertion_data_valid_count: atomic<u32>;
|
||||
|
||||
// Group 2: Slot metadata
|
||||
@group(2) @binding(0) var<storage, read_write> fixed_table_anchors: array<StructureAnchor>;
|
||||
|
||||
const DESTINATION_NONE: u32 = 0xFFFFFFFFu;
|
||||
|
||||
fn slot_matches(parent: InsertionParent) -> bool
|
||||
{
|
||||
let slot = parent.table_slot;
|
||||
if(slot == NO_TABLE_SLOT || slot >= arrayLength(&fixed_table_anchors))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return anchor_eq(parent.anchor, fixed_table_anchors[slot]);
|
||||
}
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn insertion(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
let entry_count = min(insertion_data_page_count_buffer, arrayLength(&insertion_data_parents));
|
||||
if(index >= entry_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let parent = insertion_data_parents[index];
|
||||
if(parent.kind != INSERTION_PARENT_EXTERNAL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let destination = insertion_data_destination_indices[index];
|
||||
if(destination == DESTINATION_NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Re-check identity and pointer state after allocation. A slot move between
|
||||
// validation and insertion must not link the wrong tree.
|
||||
if(!slot_matches(parent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let slot = parent.table_slot;
|
||||
if(slot >= arrayLength(&fixed_table_pointers))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Re-check that the slot is still waiting for this root.
|
||||
let pointer = fixed_table_pointers[slot];
|
||||
if(!child_pointer_subdivided(pointer) || child_pointer_available(pointer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
fixed_table_pointers[slot] = child_pointer_make_available(destination);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/// Group 0: Table pointers
|
||||
@group(0) @binding(0) var<storage, read_write> fixed_table_pointers: array<u32>; // Request count
|
||||
|
||||
// Group 1: Cache evictor
|
||||
@group(1) @binding(0) var<storage, read_write> cache_evictor_timestamps: array<u32>;
|
||||
@group(1) @binding(1) var<storage, read_write> cache_evictor_lru_pages: array<u32>;
|
||||
@group(1) @binding(2) var<storage, read_write> cache_evictor_consumed_count: u32;
|
||||
|
||||
var<immediate> new_timestamp: u32;
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn invalidation(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
let table_length = arrayLength(&fixed_table_pointers);
|
||||
if(index >= table_length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let pointer = fixed_table_pointers[index];
|
||||
if(pointer_invalid(pointer))
|
||||
{
|
||||
// Invalidate
|
||||
fixed_table_pointers[index] = child_pointer_empty(child_pointer_subdivided(pointer));
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer_invalid(pointer: u32) -> bool
|
||||
{
|
||||
if(child_pointer_available(pointer))
|
||||
{
|
||||
let pointer = child_pointer_pointer(pointer);
|
||||
let pointed_timestamp = cache_evictor_timestamps[pointer];
|
||||
|
||||
// Is dirty marker ?
|
||||
return pointed_timestamp == new_timestamp;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Group 0: Table pointers
|
||||
@group(0) @binding(0) var<storage, read_write> fixed_table_pointers: array<u32>;
|
||||
|
||||
// Group 1: Cache insertion data
|
||||
@group(1) @binding(0) var<storage, read_write> insertion_data_page_count_buffer: u32;
|
||||
@group(1) @binding(1) var<storage, read_write> insertion_data_information: array<u32>; // UNUSED
|
||||
@group(1) @binding(2) var<storage, read_write> insertion_data_location_pages: array<LocationPage>;
|
||||
@group(1) @binding(3) var<storage, read_write> insertion_data_destination_indices: array<u32>;
|
||||
@group(1) @binding(4) var<storage, read_write> insertion_data_parents: array<InsertionParent>;
|
||||
@group(1) @binding(5) var<storage, read_write> insertion_data_valid_indices: array<u32>;
|
||||
@group(1) @binding(6) var<storage, read_write> insertion_data_valid_count: atomic<u32>;
|
||||
|
||||
// Group 2: Slot metadata
|
||||
@group(2) @binding(0) var<storage, read_write> fixed_table_anchors: array<StructureAnchor>;
|
||||
|
||||
const DESTINATION_NONE: u32 = 0xFFFFFFFFu;
|
||||
|
||||
/// The slot named by the parent still holds the same anchor. This is what makes
|
||||
/// queue invalidation optional: a stale root response for a retired or reused
|
||||
/// slot can no longer win.
|
||||
fn slot_matches(parent: InsertionParent) -> bool
|
||||
{
|
||||
let slot = parent.table_slot;
|
||||
if(slot == NO_TABLE_SLOT || slot >= arrayLength(&fixed_table_anchors))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return anchor_eq(parent.anchor, fixed_table_anchors[slot]);
|
||||
}
|
||||
|
||||
/// Validates external root insertions against the structure table.
|
||||
///
|
||||
/// A root is still wanted while its table slot is subdivisible but not yet
|
||||
/// available. Slots that were already satisfied (or whose structure was
|
||||
/// invalidated before the request was dispatched) are dropped here, before the
|
||||
/// cache allocates any page for them.
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn validate_external(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
let entry_count = min(insertion_data_page_count_buffer, arrayLength(&insertion_data_parents));
|
||||
if(index >= entry_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let parent = insertion_data_parents[index];
|
||||
|
||||
// Reset every destination; only validated entries that are allocated get a
|
||||
// page written back by the cache insertion pass.
|
||||
insertion_data_destination_indices[index] = DESTINATION_NONE;
|
||||
|
||||
if(parent.kind != INSERTION_PARENT_EXTERNAL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(!slot_matches(parent))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let slot = parent.table_slot;
|
||||
if(slot >= arrayLength(&fixed_table_pointers))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let pointer = fixed_table_pointers[slot];
|
||||
if(!child_pointer_subdivided(pointer) || child_pointer_available(pointer))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let valid_slot = atomicAdd(&insertion_data_valid_count, 1u);
|
||||
insertion_data_valid_indices[valid_slot] = index;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/// Group 0: Cache request handler
|
||||
@group(0) @binding(0) var<storage, read_write> request_handler_count: u32; // Request count
|
||||
@group(0) @binding(1) var<storage, read_write> request_handler_requests: array<u32>;
|
||||
@group(0) @binding(2) var<storage, read_write> request_handler_counts: array<u32>;
|
||||
|
||||
/// Group 1: Request buffer
|
||||
@group(1) @binding(0) var<storage, read_write> request_buffer_count: u32; // Request count
|
||||
@group(1) @binding(1) var<storage, read_write> request_buffer_informations: array<RequestInformation>;
|
||||
@group(1) @binding(2) var<storage, read_write> request_buffer_meta: array<RequestMeta>;
|
||||
|
||||
/// Group 2: Slot metadata (current anchor assignment)
|
||||
@group(2) @binding(0) var<storage, read_write> fixed_table_anchors: array<StructureAnchor>;
|
||||
|
||||
var<immediate> table_producer_id: u32;
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn write_requests(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
request_buffer_count = min(request_handler_count, arrayLength(&request_buffer_informations));
|
||||
if(index >= request_handler_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// The request handler emits a slot index; resolve its current assignment so
|
||||
// the request carries stable identity plus the current placement.
|
||||
let slot = request_handler_requests[index];
|
||||
if(slot >= arrayLength(&fixed_table_anchors))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var request_info: RequestInformation;
|
||||
request_info.node_id = 0;
|
||||
request_info.children_index = 0;
|
||||
request_info.producer_id = table_producer_id;
|
||||
request_info.demand = request_handler_counts[slot];
|
||||
request_info.anchor = fixed_table_anchors[slot];
|
||||
|
||||
var request_meta: RequestMeta;
|
||||
// External requests have no parent page.
|
||||
request_meta.page_index = 0xFFFFFFFF;
|
||||
request_meta.children_index = 0;
|
||||
// u32s wrap arround
|
||||
request_meta.depth = 0xFFFFFFFF;
|
||||
request_meta.table_slot = slot;
|
||||
|
||||
request_buffer_informations[index] = request_info;
|
||||
request_buffer_meta[index] = request_meta;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// Gathers the selected (count, index) pairs into contiguous buffers so they can
|
||||
// be sorted by the radix sorter.
|
||||
//
|
||||
// `keys` receives the request counts and `values` receives the request indices.
|
||||
|
||||
@group(0) @binding(0) var<storage, read> requests: array<u32>;
|
||||
@group(0) @binding(1) var<storage, read> selected_indices: array<u32>;
|
||||
@group(0) @binding(2) var<storage, read> selected_count: u32;
|
||||
@group(0) @binding(3) var<storage, read_write> keys: array<u32>;
|
||||
@group(0) @binding(4) var<storage, read_write> values: array<u32>;
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn gather_requests(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let i = gid.x;
|
||||
if (i >= selected_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let index = selected_indices[i];
|
||||
keys[i] = requests[index];
|
||||
values[i] = index;
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
|
||||
// Group 0: cache_attributes
|
||||
@group(0) @binding(0) var<storage, read_write> cache_attributes: binding_array<AttributeArray>;
|
||||
|
||||
/// Group 1: Cache structure
|
||||
@group(1) @binding(0) var<storage, read_write> structure_pointer_pages: array<PointerPage>;
|
||||
@group(1) @binding(1) var<storage, read_write> structure_location_pages: array<LocationPage>;
|
||||
@group(1) @binding(2) var<storage, read_write> structure_depth_pages: array<u32>;
|
||||
|
||||
// Group 2: Cache insertion data
|
||||
@group(2) @binding(0) var<storage, read_write> insertion_data_page_count_buffer: u32;
|
||||
@group(2) @binding(1) var<storage, read_write> insertion_data_information: array<InsertionInformation>;
|
||||
@group(2) @binding(2) var<storage, read_write> insertion_data_location_pages: array<LocationPage>;
|
||||
@group(2) @binding(3) var<storage, read_write> insertion_data_destination_indices: array<u32>;
|
||||
@group(2) @binding(4) var<storage, read_write> insertion_data_parents: array<InsertionParent>;
|
||||
@group(2) @binding(5) var<storage, read_write> insertion_data_valid_indices: array<u32>;
|
||||
@group(2) @binding(6) var<storage, read_write> insertion_data_valid_count: atomic<u32>;
|
||||
@group(2) @binding(7) var<storage, read_write> insertion_data_attributes: binding_array<AttributeArray>;
|
||||
|
||||
// Group 3: Cache evictor
|
||||
@group(3) @binding(0) var<storage, read_write> cache_evictor_timestamps: array<u32>;
|
||||
@group(3) @binding(1) var<storage, read_write> cache_evictor_lru_pages: array<u32>;
|
||||
@group(3) @binding(2) var<storage, read_write> cache_evictor_consumed_count: u32;
|
||||
|
||||
// Group 4: Inserted count
|
||||
@group(4) @binding(0) var<storage, read_write> pass_inserted_count: atomic<u32>;
|
||||
|
||||
var<immediate> new_timestamp: u32;
|
||||
|
||||
const DESTINATION_NONE: u32 = 0xFFFFFFFFu;
|
||||
|
||||
fn insertion_entry_count() -> u32
|
||||
{
|
||||
return min(insertion_data_page_count_buffer, arrayLength(&insertion_data_parents));
|
||||
}
|
||||
|
||||
/// Checks that the parent page still holds the node the request was issued on,
|
||||
/// and that the requested child slot is still waiting for a page.
|
||||
fn internal_parent_is_valid(parent: InsertionParent) -> bool
|
||||
{
|
||||
if(parent.page_index >= PAGE_COUNT)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let location = structure_location_pages[parent.page_index];
|
||||
if(location.node_id != parent.node_id ||
|
||||
location.producer_id != parent.producer_id ||
|
||||
!anchor_eq(location.anchor, parent.anchor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(structure_depth_pages[parent.page_index] != parent.depth)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let pointer = structure_pointer_pages[parent.page_index].pointers[parent.children_index];
|
||||
return child_pointer_subdivided(pointer) && !child_pointer_available(pointer);
|
||||
}
|
||||
|
||||
/// Validates a batch of internal insertions and compacts the valid ones into
|
||||
/// `insertion_data_valid_indices`.
|
||||
///
|
||||
/// This must run before page allocation so that stale insertions never consume
|
||||
/// an evicted page. External roots are validated by the structure table that
|
||||
/// requested them (see `fixed_table/validate_insertion.wgsl`).
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn validate_internal(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
if(index >= insertion_entry_count())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let parent = insertion_data_parents[index];
|
||||
|
||||
// Reset the destination of every entry. Only entries compacted into
|
||||
// `valid_indices` and actually allocated by the insertion pass get a page;
|
||||
// this keeps invalid/stale insertions from reusing a previous batch's
|
||||
// destination if the host did not pre-fill the buffer.
|
||||
insertion_data_destination_indices[index] = DESTINATION_NONE;
|
||||
|
||||
if(parent.kind != INSERTION_PARENT_INTERNAL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(internal_parent_is_valid(parent))
|
||||
{
|
||||
let slot = atomicAdd(&insertion_data_valid_count, 1u);
|
||||
insertion_data_valid_indices[slot] = index;
|
||||
}
|
||||
}
|
||||
|
||||
fn available_amount(valid_count: u32) -> u32
|
||||
{
|
||||
let lru_remaining = arrayLength(&cache_evictor_timestamps) - cache_evictor_consumed_count;
|
||||
return min(lru_remaining, valid_count);
|
||||
}
|
||||
|
||||
@compute
|
||||
@workgroup_size(1, 1, 1)
|
||||
fn consume(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
// Report consumed amount of LRU pages to cache evictor
|
||||
// So that subsequent insertions can can continue to use new pages
|
||||
cache_evictor_consumed_count += atomicLoad(&pass_inserted_count);
|
||||
atomicStore(&pass_inserted_count, 0);
|
||||
}
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn insertion(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let slot = gid.x;
|
||||
let entry_count = insertion_entry_count();
|
||||
if(slot >= entry_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let valid_count = atomicLoad(&insertion_data_valid_count);
|
||||
if(slot >= valid_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Compacted entry index in the host-provided arrays.
|
||||
let index = insertion_data_valid_indices[slot];
|
||||
let insertion_count = available_amount(valid_count);
|
||||
|
||||
if(slot >= insertion_count)
|
||||
{
|
||||
// Report that page was not written. The host also pre-fills the whole
|
||||
// destination array with DESTINATION_NONE, so this is defensive.
|
||||
insertion_data_destination_indices[index] = DESTINATION_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
// Get a page to evict
|
||||
let page_index = cache_evictor_lru_pages[slot + cache_evictor_consumed_count];
|
||||
let page_timestamp = cache_evictor_timestamps[page_index];
|
||||
|
||||
if(page_timestamp >= new_timestamp - 1)
|
||||
{
|
||||
// Do not insert page
|
||||
insertion_data_destination_indices[index] = DESTINATION_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
atomicAdd(&pass_inserted_count, 1);
|
||||
insertion_data_destination_indices[index] = page_index;
|
||||
|
||||
// Copy new location
|
||||
structure_location_pages[page_index] = insertion_data_location_pages[index];
|
||||
|
||||
// Copy new pointers
|
||||
let new_pointers = insertion_info_to_pointer_page(insertion_data_information[index]);
|
||||
structure_pointer_pages[page_index] = new_pointers;
|
||||
|
||||
// Mark page as dirty for invalidation
|
||||
cache_evictor_timestamps[page_index] = new_timestamp;
|
||||
|
||||
// Set default depth. `write_pointers` refines it for internal insertions.
|
||||
structure_depth_pages[page_index] = 0;
|
||||
|
||||
// Copy attributes
|
||||
for(var attribute_index = u32(0); attribute_index < ATTRIBUTE_COUNT; attribute_index ++)
|
||||
{
|
||||
if(!ATTRIBUTE_COPY[attribute_index])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var page_stride = CHILDREN_COUNT * ATTRIBUTE_WORD_COUNTS[attribute_index];
|
||||
var dest_offset = page_stride * page_index;
|
||||
var src_offset = page_stride * index;
|
||||
|
||||
// Copy all words of the attribute page
|
||||
for(var word = u32(0); word < page_stride; word ++)
|
||||
{
|
||||
cache_attributes[attribute_index].values[dest_offset + word] = insertion_data_attributes[attribute_index].values[src_offset + word];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn insertion_info_to_pointer_page(info: InsertionInformation) -> PointerPage
|
||||
{
|
||||
var page: PointerPage;
|
||||
|
||||
var occupancy_low = u32(0);
|
||||
var occupancy_high = u32(0);
|
||||
for(var i = u32(0); i < CHILDREN_COUNT; i++)
|
||||
{
|
||||
let insert_info = info.children[i];
|
||||
|
||||
// Pointer
|
||||
page.pointers[i] = child_pointer_empty(child_information_subdivided(insert_info));
|
||||
|
||||
// Occupancy
|
||||
var bit = select(u32(0), u32(1), child_information_occupied(insert_info));
|
||||
|
||||
// Dispatch set bit on low or high word
|
||||
bit = bit << (i % u32(32));
|
||||
if(i < 32)
|
||||
{
|
||||
occupancy_low |= bit;
|
||||
}
|
||||
else
|
||||
{
|
||||
occupancy_high |= bit;
|
||||
}
|
||||
}
|
||||
|
||||
page.occupancy_low = occupancy_low;
|
||||
page.occupancy_high = occupancy_high;
|
||||
|
||||
return page;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/// Group 0: Cache structure
|
||||
@group(0) @binding(0) var<storage, read_write> structure_pointer_pages: array<PointerPage>;
|
||||
@group(0) @binding(1) var<storage, read_write> structure_location_pages: array<LocationPage>;
|
||||
@group(0) @binding(2) var<storage, read_write> structure_depth_pages: array<u32>;
|
||||
|
||||
// Group 1: Cache evictor
|
||||
@group(1) @binding(0) var<storage, read_write> cache_evictor_timestamps: array<u32>;
|
||||
@group(1) @binding(1) var<storage, read_write> cache_evictor_lru_pages: array<u32>;
|
||||
@group(1) @binding(2) var<storage, read_write> cache_evictor_consumed_count: u32;
|
||||
|
||||
var<immediate> new_timestamp: u32;
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn invalidation(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
if(index >= arrayLength(&structure_pointer_pages)) { return; }
|
||||
|
||||
for(var i = u32(0); i < CHILDREN_COUNT; i++)
|
||||
{
|
||||
let pointer = structure_pointer_pages[index].pointers[i];
|
||||
if(child_pointer_available(pointer) && cache_evictor_timestamps[child_pointer_pointer(pointer)] == new_timestamp)
|
||||
{
|
||||
structure_pointer_pages[index].pointers[i] = child_pointer_empty(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pointer_invalid(pointer: u32) -> bool
|
||||
{
|
||||
if(child_pointer_available(pointer))
|
||||
{
|
||||
let pointer = child_pointer_pointer(pointer);
|
||||
let pointed_timestamp = cache_evictor_timestamps[pointer];
|
||||
|
||||
// Is dirty marker ?
|
||||
return pointed_timestamp == new_timestamp;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn pointer_invalidate(page_index: u32, child_index: u32)
|
||||
{
|
||||
let pointer = structure_pointer_pages[page_index].pointers[child_index];
|
||||
let pointed_page = child_pointer_pointer(pointer);
|
||||
|
||||
let new_pointer = child_pointer_empty(child_pointer_subdivided(pointer));
|
||||
structure_pointer_pages[page_index].pointers[child_index] = new_pointer;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
@group(0) @binding(0) var<storage, read_write> request_count_pages: array<u32>;
|
||||
|
||||
/// Resets the requests
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn reset_requests(@builtin(global_invocation_id) global_invocation_id: vec3<u32>)
|
||||
{
|
||||
let index = global_invocation_id.x;
|
||||
if(index >= arrayLength(&request_count_pages))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
request_count_pages[index] = 0;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/// Group 0: Cache structure
|
||||
@group(0) @binding(0) var<storage, read_write> structure_pointer_pages: array<PointerPage>;
|
||||
@group(0) @binding(1) var<storage, read_write> structure_location_pages: array<LocationPage>;
|
||||
@group(0) @binding(2) var<storage, read_write> structure_depth_pages: array<u32>;
|
||||
|
||||
// Group 1: Cache insertion data
|
||||
@group(1) @binding(0) var<storage, read_write> insertion_data_page_count_buffer: u32;
|
||||
@group(1) @binding(1) var<storage, read_write> insertion_data_information: array<InsertionInformation>;
|
||||
@group(1) @binding(2) var<storage, read_write> insertion_data_location_pages: array<LocationPage>;
|
||||
@group(1) @binding(3) var<storage, read_write> insertion_data_destination_indices: array<u32>;
|
||||
@group(1) @binding(4) var<storage, read_write> insertion_data_parents: array<InsertionParent>;
|
||||
@group(1) @binding(5) var<storage, read_write> insertion_data_valid_indices: array<u32>;
|
||||
@group(1) @binding(6) var<storage, read_write> insertion_data_valid_count: atomic<u32>;
|
||||
@group(1) @binding(7) var<storage, read_write> insertion_data_attributes: binding_array<AttributeArray>;
|
||||
|
||||
const DESTINATION_NONE: u32 = 0xFFFFFFFFu;
|
||||
|
||||
/// Links inserted pages into their parent, using only the insertion batch's own
|
||||
/// parent description. No request buffer is involved, so a batch may be
|
||||
/// produced on a different frame than the one that emitted the requests.
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn write_pointers(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
let entry_count = min(insertion_data_page_count_buffer, arrayLength(&insertion_data_parents));
|
||||
if(index >= entry_count)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let parent = insertion_data_parents[index];
|
||||
if(parent.kind != INSERTION_PARENT_INTERNAL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let page_index = insertion_data_destination_indices[index];
|
||||
if(page_index == DESTINATION_NONE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// The parent page may have been recycled while this insertion batch was in
|
||||
// flight. Only link if it still describes the same node, and never link a
|
||||
// page to itself.
|
||||
if(parent.page_index >= PAGE_COUNT || parent.page_index == page_index)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let location = structure_location_pages[parent.page_index];
|
||||
if(location.node_id != parent.node_id ||
|
||||
location.producer_id != parent.producer_id ||
|
||||
!anchor_eq(location.anchor, parent.anchor))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
structure_pointer_pages[parent.page_index].pointers[parent.children_index]
|
||||
= child_pointer_make_available(page_index);
|
||||
structure_depth_pages[page_index] = parent.depth + 1;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/// Pipeline to write the requests out in to the request buffer
|
||||
|
||||
/// Group 0: Cache structure
|
||||
@group(0) @binding(0) var<storage, read_write> structure_pointer_pages: array<PointerPage>;
|
||||
@group(0) @binding(1) var<storage, read_write> structure_location_pages: array<LocationPage>;
|
||||
@group(0) @binding(2) var<storage, read_write> structure_depth_pages: array<u32>;
|
||||
|
||||
/// Group 1: Cache request handler
|
||||
@group(1) @binding(0) var<storage, read_write> request_handler_count: u32; // Request count
|
||||
@group(1) @binding(1) var<storage, read_write> request_handler_requests: array<u32>;
|
||||
@group(1) @binding(2) var<storage, read_write> request_handler_counts: array<u32>;
|
||||
|
||||
/// Group 3: Request buffer
|
||||
@group(2) @binding(0) var<storage, read_write> request_buffer_count: u32; // Request count
|
||||
@group(2) @binding(1) var<storage, read_write> request_buffer_informations: array<RequestInformation>;
|
||||
@group(2) @binding(2) var<storage, read_write> request_buffer_meta: array<RequestMeta>;
|
||||
|
||||
@compute
|
||||
@workgroup_size(256, 1, 1)
|
||||
fn write_requests(@builtin(global_invocation_id) gid: vec3<u32>)
|
||||
{
|
||||
let index = gid.x;
|
||||
|
||||
// Max size of request buffer and request count
|
||||
let max_requests =
|
||||
min(arrayLength(&request_buffer_informations), request_handler_count);
|
||||
if(index >= max_requests) { return; }
|
||||
|
||||
|
||||
let request_index = request_handler_requests[index];
|
||||
let request_location = pointer_index_to_cache_index(request_index);
|
||||
let page_index = request_location.page_index;
|
||||
|
||||
// Fill out request information
|
||||
var request_information: RequestInformation;
|
||||
request_information.node_id = structure_location_pages[page_index].node_id;
|
||||
request_information.children_index = request_location.children_index;
|
||||
request_information.producer_id = structure_location_pages[page_index].producer_id;
|
||||
request_information.demand = request_handler_counts[request_index];
|
||||
request_information.anchor = structure_location_pages[page_index].anchor;
|
||||
|
||||
// Fill out request meta information
|
||||
var request_meta: RequestMeta;
|
||||
request_meta.page_index = page_index;
|
||||
request_meta.children_index = request_location.children_index;
|
||||
request_meta.depth = structure_depth_pages[page_index];
|
||||
request_meta.table_slot = NO_TABLE_SLOT;
|
||||
|
||||
// Write them to request buffer
|
||||
request_buffer_informations[index] = request_information;
|
||||
request_buffer_meta[index] = request_meta;
|
||||
|
||||
// Write request count
|
||||
request_buffer_count = max_requests;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
use wgpu::BindGroup;
|
||||
use wgpu::BindGroupLayout;
|
||||
use wgpu::Buffer;
|
||||
use wgpu::BufferUsages;
|
||||
|
||||
use vxls_gpu::create_multi_buffers_bind_group;
|
||||
use vxls_gpu::create_multi_buffers_bind_group_layout;
|
||||
use vxls_profiler::memory_profiler;
|
||||
use crate::schema::DepthPage;
|
||||
use crate::schema::StructurePage;
|
||||
|
||||
/// Contains the instanciated buffers relevant to the structure of the trees
|
||||
pub struct CacheStructure<const N: usize>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pointer_pages: Buffer,
|
||||
location_pages: Buffer,
|
||||
depth_pages: Buffer,
|
||||
|
||||
bind_group_layout: BindGroupLayout,
|
||||
bind_group: BindGroup,
|
||||
}
|
||||
|
||||
impl<const N: usize> CacheStructure<N>
|
||||
where
|
||||
[(); N * N * N]:,
|
||||
{
|
||||
pub const POINTER_PAGES_BINDING: u32 = 0;
|
||||
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
|
||||
{
|
||||
let pointer_pages = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheStructure::pointer_pages"),
|
||||
size: (size_of::<StructurePage<N>>() * page_count) as u64,
|
||||
usage: BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
let location_pages = device.create_buffer(&wgpu::BufferDescriptor {
|
||||
label: Some("CacheStructure::location_pages"),
|
||||
size: (size_of::<StructurePage<N>>() * page_count) as u64,
|
||||
usage: BufferUsages::STORAGE,
|
||||
mapped_at_creation: false,
|
||||
});
|
||||
|
||||
// Unused pages get the maximum depth so they lead the deepest-first
|
||||
// depth sort used by the eviction pass. Combined with their initial
|
||||
// timestamp of 0, this makes them the first pages reused.
|
||||
let depth_pages = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("CacheStructure::depth_pages"),
|
||||
usage: BufferUsages::STORAGE | BufferUsages::COPY_SRC,
|
||||
contents: bytemuck::cast_slice(&vec![DepthPage::PAGE_UNUSED; page_count]),
|
||||
});
|
||||
|
||||
let bind_group_layout = Self::create_bind_group_layout(device);
|
||||
let bind_group = create_multi_buffers_bind_group(
|
||||
device,
|
||||
&bind_group_layout,
|
||||
&[&pointer_pages, &location_pages, &depth_pages],
|
||||
Some("CacheStructure::bind_group"),
|
||||
);
|
||||
|
||||
CacheStructure {
|
||||
pointer_pages,
|
||||
location_pages,
|
||||
depth_pages,
|
||||
|
||||
bind_group_layout,
|
||||
bind_group,
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
&self.bind_group_layout
|
||||
}
|
||||
|
||||
pub fn bind_group(&self) -> &BindGroup
|
||||
{
|
||||
&self.bind_group
|
||||
}
|
||||
|
||||
pub fn pointer_pages(&self) -> &Buffer
|
||||
{
|
||||
&self.pointer_pages
|
||||
}
|
||||
|
||||
pub fn depth_pages(&self) -> &Buffer
|
||||
{
|
||||
&self.depth_pages
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user