refactor: cleaner request path + raytracing code

This commit is contained in:
2026-11-23 11:32:16 +01:00
parent dd0e102348
commit dd5fdc13b8
20 changed files with 1427 additions and 618 deletions
+145
View File
@@ -53,6 +53,74 @@ where
bind_group_no_attributes: wgpu::BindGroup,
}
/// One entry of an insertion batch.
///
/// The cache owns page identity, so the host supplies the structure of the
/// produced page together with the parent it attaches to and the location it
/// should be recorded at. The cache does not depend on the producer crate, so
/// this is the primitive form produced by `ReadyItem::insertion_entry`.
#[derive(Clone, Copy, Debug)]
pub struct InsertionEntry<const N: usize>
where
[(); N * N * N]:,
{
/// Structure of the produced page.
pub information: InsertionInformation<N>,
/// Attachment point of the produced page.
pub parent: InsertionParent,
/// Identity recorded for the produced page.
pub location: LocationPage,
}
/// Errors reported while uploading a full insertion batch.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InsertionUploadError {
/// The batch has more entries than the insertion data can hold.
TooManyEntries { provided: usize, capacity: usize },
/// The voxel slice does not contain exactly one page per entry.
VoxelCountMismatch { provided: usize, expected: usize },
/// The producer attribute layout does not match the cache layout.
LayoutMismatch {
producer: AttributesLayout,
cache: AttributesLayout,
},
/// The attribute payload was rejected by [`CacheInsertionData::write_attributes`].
Attributes(AttributeUploadError),
}
impl From<AttributeUploadError> for InsertionUploadError {
fn from(error: AttributeUploadError) -> Self {
Self::Attributes(error)
}
}
impl std::fmt::Display for InsertionUploadError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TooManyEntries { provided, capacity } => write!(
formatter,
"insertion batch has {provided} entries but capacity is {capacity}"
),
Self::VoxelCountMismatch { provided, expected } => write!(
formatter,
"insertion batch has {provided} voxels but expected {expected}"
),
Self::LayoutMismatch { producer, cache } => write!(
formatter,
"producer attribute layout {producer:?} does not match cache layout {cache:?}"
),
Self::Attributes(error) => write!(formatter, "attribute upload failed: {error:?}"),
}
}
}
impl std::error::Error for InsertionUploadError {}
/// Errors reported while uploading a producer attribute slice into
/// [`CacheInsertionData`].
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -355,6 +423,11 @@ where
self.max_page_count
}
/// Maximum number of entries a batch can hold.
pub fn capacity(&self) -> usize {
self.max_page_count
}
pub fn page_count_buffer(&self) -> &wgpu::Buffer {
&self.page_count_buffer
}
@@ -440,6 +513,78 @@ where
Ok(voxels.len() / (N * N * N))
}
/// Uploads a complete insertion batch.
///
/// `entries` and `voxels` must agree: `voxels` contains one page of
/// [`Attributes`] per entry, in entry-major order. This writes the
/// structure, location and parent arrays, resets `valid_count`, sets
/// `page_count` to the number of entries, and uploads the attributes.
///
/// `destination_indices` is intentionally not written here: the internal
/// and external validation passes reset every entry they process, so a
/// stale destination can never survive a batch.
///
/// Returns the number of entries written.
pub fn upload<A: Attributes>(
&self,
queue: &wgpu::Queue,
entries: &[InsertionEntry<N>],
voxels: &[A],
) -> Result<usize, InsertionUploadError> {
if A::LAYOUT != self.layout {
return Err(InsertionUploadError::LayoutMismatch {
producer: A::LAYOUT,
cache: self.layout,
});
}
if entries.len() > self.capacity() {
return Err(InsertionUploadError::TooManyEntries {
provided: entries.len(),
capacity: self.capacity(),
});
}
let expected_voxels = entries.len() * N * N * N;
if voxels.len() != expected_voxels {
return Err(InsertionUploadError::VoxelCountMismatch {
provided: voxels.len(),
expected: expected_voxels,
});
}
// Disable the batch until every buffer has been written successfully.
// This prevents a partially-uploaded batch from being encoded if an
// attribute upload fails after the structure buffers were written.
queue.write_buffer(&self.page_count_buffer, 0, bytemuck::bytes_of(&0u32));
queue.write_buffer(&self.valid_count, 0, bytemuck::bytes_of(&0u32));
if entries.is_empty() {
return Ok(0);
}
let informations: Vec<InsertionInformation<N>> =
entries.iter().map(|entry| entry.information).collect();
let locations: Vec<LocationPage> = entries.iter().map(|entry| entry.location).collect();
let parents: Vec<InsertionParent> = entries.iter().map(|entry| entry.parent).collect();
queue.write_buffer(
&self.insertion_information,
0,
bytemuck::cast_slice(&informations),
);
queue.write_buffer(&self.location_pages, 0, bytemuck::cast_slice(&locations));
queue.write_buffer(&self.insertion_parents, 0, bytemuck::cast_slice(&parents));
self.write_attributes(queue, voxels)?;
// Only now is the batch complete and safe to process.
let entry_count = entries.len() as u32;
queue.write_buffer(&self.page_count_buffer, 0, bytemuck::bytes_of(&entry_count));
Ok(entries.len())
}
}
#[cfg(test)]