Adding documentation for agents

This commit is contained in:
2026-11-08 15:02:48 +01:00
parent 6dedbbdb75
commit 82c952c6d8
10 changed files with 2139 additions and 12 deletions
+88
View File
@@ -0,0 +1,88 @@
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use crate::production::ProducerId;
use crate::production::StructureId;
use crate::production::StructureLocator;
use crate::voxel_cache::schema::RequestInformation;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct RequestKey
{
producer_id: ProducerId,
structure_id: StructureId,
structure_locator: StructureLocator,
children_index: u32,
}
pub enum JobState
{
Queued,
InFlight,
Ready,
}
pub struct Job
{
state: JobState,
priority: u32,
emit_timestamp: u32,
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct RequestPriority
{
priority: u32,
key: RequestKey,
}
pub struct RequestQueue
{
cap: usize,
count: usize,
jobs: HashMap<RequestKey, RequestKey>,
priorities: BTreeSet<RequestPriority>,
}
impl RequestQueue
{
pub fn new(cap: usize) -> Self
{
Self {
cap,
count: 0,
jobs: HashMap::new(),
priorities: BTreeMap::new(),
}
}
pub fn ingest(&mut self, timestamp: u32, requests: &[RequestInformation])
{
let mut new_requests = vec![];
for (i, request) in requests.iter().enumerate()
{
let key = RequestKey {
producer_id: 0u32.into(), // TODO: Support produer ids
structure_id: request.structure_id.into(),
structure_locator: request.node_id.into(),
children_index: request.children_index,
};
// Check for exitence in the queue
if self.jobs.contains_key(&key)
{
// Update priority
self.priorities.remove(&key);
}
else
{
// Brand new request, to add
new_requests.push(i);
}
// Insert new job
}
}
}
+22 -5
View File
@@ -16,7 +16,12 @@ use crate::voxel_cache::create_multi_buffers_bind_group_layout;
use crate::voxel_cache::structure::CacheStructure;
/// Retains usage information about pages in the cache under the form of timestamps.
/// Produces a list of LRU eviction order, from which the cache can pull pages to recycle
/// 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,
@@ -24,7 +29,8 @@ pub struct CacheEvictor
/// Contains a u32 timestamp per page, so that we know when a page was last used
timestamp_pages: wgpu::Buffer,
/// Contains indices in LRU order, after sorting
/// 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
@@ -33,7 +39,7 @@ pub struct CacheEvictor
/// To reset lru indices between insertions
reset_lru_pass: wgpu::ComputePipeline,
/// To copy corresponding timestamp values after first depth sort
/// To copy corresponding timestamp values after the descending depth sort
indirect_copy_pass: wgpu::ComputePipeline,
pipelines_dispatch_count: u32,
@@ -146,7 +152,15 @@ impl CacheEvictor
reset_pass.dispatch_workgroups(self.pipelines_dispatch_count, 1, 1);
}
// First sort nodes per depth
// 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.
{
gpu_profiler::begin_scope!(encoder, "depth radix");
encoder.copy_buffer_to_buffer(
@@ -164,7 +178,7 @@ impl CacheEvictor
None,
Some(self.page_count),
&mut encoder,
radix_sort::RadixSortDirection::Ascending,
radix_sort::RadixSortDirection::Descending,
);
}
@@ -182,6 +196,9 @@ impl CacheEvictor
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.
{
gpu_profiler::begin_scope!(encoder, "timestamp radix");
ressources.radix_sorter.sort(
+7 -3
View File
@@ -119,9 +119,13 @@ pub struct LocationPage
/// 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. This prevents replacing a high page that pointed to a deeper tree strucutre
/// effectively invalidating the minimum amount of data.
/// 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);
+8 -2
View File
@@ -11,7 +11,8 @@
/// Group 2: Scratch buffer
@group(2) @binding(0) var<storage, read_write> scratch_buffer: array<u32>;
/// Resets the lru list
/// 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>)
@@ -28,7 +29,12 @@ fn reset_lru_list(@builtin(global_invocation_id) global_invocation_id: vec3<u32>
consumed_count = 0;
}
/// Indirect copy, in between the depth sort and the timestamp sort
/// 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>)
+3 -2
View File
@@ -46,8 +46,9 @@ where
mapped_at_creation: false,
});
// Depth pages need to be initialized to unused, this prioritizes them in the
// depth sort used in the eviction pass
// 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,