List of evicted pages

This commit is contained in:
2026-10-13 21:17:24 +02:00
parent 2a85708b3a
commit a9785252c7
5 changed files with 535 additions and 125 deletions
+109 -36
View File
@@ -1,8 +1,8 @@
use std::num::NonZero;
use bytemuck::Zeroable;
use bytemuck::{Zeroable, bytes_of};
use tiff::encoder;
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoder, ComputePipeline, Device, Queue, ShaderStages, naga::back::spv::Capability::Shader, util::DeviceExt};
use wgpu::{BindGroup, BindGroupLayout, Buffer, BufferUsages, CommandEncoder, ComputePassDescriptor, ComputePipeline, Device, Queue, ShaderStages, naga::back::spv::Capability::Shader, util::DeviceExt};
use crate::{gpu::radix_sort::{self, RadixSorter}, gpu_profiler::{self, ProfilingEncoder, ScopeEncoder, begin_scope}, voxel_cache::{data::{CacheNodeRequest, CacheResponse, ColorPoolElement, LocationPoolElement, StructurePoolElement}, indirect_buffer::NonZeroBufferCompactor, layout::VoxelCacheLayout, producer_interface::CacheProducerInterface, request_buffer::RequestBuffer, structure_table::StructureTable, usage_buffer::UsageBuffer}};
@@ -56,6 +56,9 @@ pub struct VoxelCache<const N: usize>
// Scratch buffer of u32 spanning whole cache for operations
cache_scratch_buffer: Buffer,
// To store the amount of pages that have been written in this insertion
written_pages: PageList,
structure_bind_group: BindGroup,
management_bind_group: BindGroup,
attributes_bind_group: BindGroup,
@@ -179,6 +182,8 @@ where
mapped_at_creation: false,
});
let written_pages = PageList::new(&device, cache_size);
// Attributes
let (attributes_bind_group, attributes_bind_group_layout) = {
let attributes_bind_group_layout = device.create_bind_group_layout(
@@ -428,6 +433,7 @@ where
Some(&attributes_bind_group_layout),
Some(&structure_table_bind_group_layout),
Some(&cache_interface_bind_group_layout),
Some(written_pages.bind_group_layout()),
],
immediate_size: size_of::<CachingPipelineImmediates>() as u32, // Current frame timestamp
});
@@ -436,24 +442,13 @@ where
let mut copy_loops = String::new();
for (attribute_index, attribute) in layout.attributes().iter().enumerate()
{
let node_size = attribute.word_count * children_count as u32;
let word_count = attribute.word_count;
copy_loops += format!("
// Iterate on every children
for(var i = 0u; i < {children_count}; i++){{
// Iterate on every word of the attribute
for(var j = 0u; j < {word_count}; j++){{
// Offset within child
let offset = i * {word_count} + j;
attributes_pools[{attribute_index}].values[overwritten_element * {node_size} + offset]
= attributes_producer[{attribute_index}].values[producer_index * {node_size} + offset];
}}
}}
").as_str();
copy_loops += &attribute.wgsl_copy_loop(
"attributes_pools",
"attributes_producer",
"overwritten_element",
"producer_index",
children_count,
attribute_index);
}
let children_count = N * N * N;
@@ -503,6 +498,9 @@ where
@group(4) @binding(4) var<storage, read_write> attributes_producer: binding_array<AttributeArray, {attribute_count}>;
@group(4) @binding(5) var<storage, read_write> locations: array<LocationPoolElement>;
@group(5) @binding(0) var<storage, read_write> written_pages_count: u32;
@group(5) @binding(1) var<storage, read_write> written_pages_indices: array<u32>;
@compute
@workgroup_size(64)
fn main(
@@ -518,9 +516,14 @@ where
return;
}}
written_pages_count = total;
// Get index from lru eviction list
let overwritten_element = lru_list[index];
// Write into the written pages list
written_pages_indices[index] = overwritten_element;
if(parameters.write_pointers == 0 && usage_buffer[overwritten_element] != parameters.frame_timestamp)
{{
// ~~ Phase 1 ~~
@@ -637,6 +640,7 @@ where
let total_pool = arrayLength(&structure_pool);
let total_table = arrayLength(&structure_table_pointers);
if(index < total_table)
{{
// Invocation falls in structure table
@@ -848,6 +852,7 @@ where
usage_buffer,
lru_list,
cache_scratch_buffer,
written_pages: PageList::new(&device, cache_size),
structure_bind_group,
management_bind_group,
@@ -926,7 +931,7 @@ where
], Some("VoxelCache::structure_table_bind_group"))
}
pub fn cache_post_render(&mut self, encoder: &mut ScopeEncoder, request_interface: &CacheProducerInterface<N>)
pub fn cache_post_render(&mut self, encoder: &mut ScopeEncoder<'_, '_, CommandEncoder>, request_interface: &CacheProducerInterface<N>)
{
gpu_profiler::begin_scope!(encoder, "cache_post_render");
// Sort request buffer
@@ -989,11 +994,7 @@ where
}
{
gpu_profiler::begin_scope!(encoder, "indirect_copy");
let mut indirect_usage_copy_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("VoxelCache::indirect_usage_copy_pass"),
timestamp_writes: None,
});
let mut indirect_usage_copy_pass = encoder.begin_profiled_compute_pass("indrect_compute_pass");
indirect_usage_copy_pass.set_bind_group(0, Some(&self.structure_bind_group), &[]);
indirect_usage_copy_pass.set_bind_group(1, Some(&self.management_bind_group), &[]);
@@ -1031,11 +1032,8 @@ where
}
// Sorted requests are now in the group
gpu_profiler::begin_scope!(encoder, "request_pass");
let mut request_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("VoxelCache::write_requests_pass"),
timestamp_writes: None,
});
//gpu_profiler::begin_scope!(encoder, "request_pass");
let mut request_pass = encoder.begin_profiled_compute_pass("VoxelCache::write_requests_pass");
request_pass.set_bind_group(0, Some(&self.structure_bind_group), &[]);
request_pass.set_bind_group(1, Some(&self.management_bind_group), &[]);
@@ -1061,13 +1059,15 @@ where
self.timestamp
}
pub fn cache_insert(&mut self, encoder: &mut CommandEncoder, cache_interface: &CacheProducerInterface<N>)
pub fn cache_insert(&mut self, encoder: &mut ScopeEncoder<'_, '_, CommandEncoder>, cache_interface: &CacheProducerInterface<N>)
{
// Each of the entry of the cache insertion, is matched with the usage list to evict
let mut cache_insertion_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
label: Some("VoxelCache::cache_insertion_pass"),
timestamp_writes: None,
});
let mut cache_insertion_pass = encoder.begin_profiled_compute_pass("VoxelCache::cache_insertion_pass");
// let mut cache_insertion_pass = encoder.begin_compute_pass(&ComputePassDescriptor
// {
// label: Some("mozeifj"),
// timestamp_writes: None,
// });
// Copy insertions to evicted lru
// Point parents to children
@@ -1075,6 +1075,7 @@ where
// ~~~ Write data + Dirty flagging/timestamp update ~~~
{
gpu_profiler::begin_scope!(cache_insertion_pass, "Write pages");
cache_insertion_pass.set_bind_group(0, Some(&self.structure_bind_group), &[]);
cache_insertion_pass.set_bind_group(1, Some(&self.management_bind_group), &[]);
cache_insertion_pass.set_bind_group(2, Some(&self.attributes_bind_group), &[]);
@@ -1099,6 +1100,7 @@ where
// ~~~ Invalidate pointers ~~~
{
gpu_profiler::begin_scope!(cache_insertion_pass, "Invalidation");
cache_insertion_pass.set_pipeline(&self.invalidation_pass);
cache_insertion_pass.set_bind_group(4, None, &[]);
cache_insertion_pass
@@ -1113,6 +1115,7 @@ where
// ~~~ Write new pointers ~~~
{
gpu_profiler::begin_scope!(cache_insertion_pass, "Pointer write");
cache_insertion_pass.set_pipeline(&self.caching_pass);
cache_insertion_pass.set_bind_group(4, Some(&cache_interface.request_side_bind_group), &[]);
cache_insertion_pass.set_immediates(
@@ -1132,6 +1135,7 @@ where
// Reset state
{
gpu_profiler::begin_scope!(cache_insertion_pass, "Reset state");
cache_insertion_pass.set_pipeline(&self.reset_pass);
cache_insertion_pass.set_bind_group(4, None, &[]);
let shader_invocation_count = self.size;
@@ -1146,6 +1150,75 @@ where
self.timestamp += 1;
}
pub fn written_page_list(&self) -> &PageList
{
&self.written_pages
}
}
pub struct PageList
{
max_size: usize,
// u32: page_count
count_buffer: Buffer,
// Buffer of u32 indices into pages
list_buffer: Buffer,
bind_group_layout: BindGroupLayout,
bind_group: BindGroup,
}
impl PageList
{
pub fn new(device: &Device, max_size: usize) -> Self
{
let count_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor
{
label: Some("PageList::count_buffer"),
contents: bytes_of(&0),
usage: BufferUsages::STORAGE,
});
let list_buffer = device.create_buffer(&wgpu::BufferDescriptor
{
label: Some("PageList::list_buffer"),
usage: BufferUsages::STORAGE,
size: (size_of::<u32>() * max_size) as u64,
mapped_at_creation: false,
});
let (bind_group_layout, bind_group) = create_multi_buffers_bg_bg_layout(device, &[&count_buffer, &list_buffer], Some("PageList::bind_group"));
PageList
{
max_size,
count_buffer,
list_buffer,
bind_group_layout,
bind_group,
}
}
pub fn max_size(&self) -> usize {
self.max_size
}
pub fn count_buffer(&self) -> &Buffer {
&self.count_buffer
}
pub fn list_buffer(&self) -> &Buffer {
&self.list_buffer
}
pub fn bind_group_layout(&self) -> &BindGroupLayout {
&self.bind_group_layout
}
pub fn bind_group(&self) -> &BindGroup {
&self.bind_group
}
}
// Utilities