Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 165 additions & 0 deletions crates/victory-shader/src/emit/mesh_compute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//! Shared buffer layout for the additive mesh-as-compute fallback.
//!
//! Each dispatched workgroup writes one fixed-size slot. The slot starts
//! with a 16-byte header (vertex count, primitive count, two reserved words),
//! followed by vertex records and then primitive records. Every vertex
//! record stores position at byte offset 0, optional clip/cull distances,
//! then one `float4` per parameter location. A primitive record stores
//! three vertex indices, a cull flag, then one `float4` per per-primitive
//! parameter location.

/// Byte offsets and strides for one workgroup's mesh-compute output slot.
///
/// Offsets within a vertex record are relative to the start of that record;
/// `vertices_offset` and `primitives_offset` are relative to the slot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MeshComputeLayout {
/// Bytes in one slot, including the header and all records.
pub slot_bytes: u64,
/// Byte offset of the first vertex record in the slot.
pub vertices_offset: u64,
/// Bytes in each vertex record.
pub vertex_stride: u64,
/// Byte offset of the first primitive record in the slot.
pub primitives_offset: u64,
/// Bytes in each primitive record, including its index/cull header.
pub primitive_stride: u64,
/// Declared maximum vertices for a workgroup.
pub max_vertices: u32,
/// Declared maximum primitives for a workgroup.
pub max_primitives: u32,
/// Number of clip-distance values stored per vertex.
pub clip_count: u32,
/// Offset of clip distances within each vertex record.
pub clip_offset: u64,
/// Number of cull-distance values stored per vertex.
pub cull_count: u32,
/// Offset of cull distances within each vertex record.
pub cull_offset: u64,
/// Parameter locations and their byte offsets within each vertex record,
/// in ascending location order.
pub params: Vec<(u32, u64)>,
/// Per-primitive parameter locations and their byte offsets within each
/// primitive record, in ascending location order.
pub primitive_params: Vec<(u32, u64)>,
}

/// Computes the 16-byte-aligned slot layout used by the mesh-as-compute
/// fallback. Parameter locations are sorted and duplicate rows are stored
/// only once.
///
/// Returns `None` if the resulting offsets or slot size overflow `u64`.
pub fn mesh_compute_layout(
max_vertices: u32,
max_primitives: u32,
clip_count: u32,
cull_count: u32,
parameter_locations: &[u32],
primitive_parameter_locations: &[u32],
) -> Option<MeshComputeLayout> {
const HEADER_BYTES: u64 = 16;
const RECORD_BYTES: u64 = 16;

let vec4s = |count: u32| u64::from(count).div_ceil(4);
let clip_vec4s = vec4s(clip_count);
let cull_vec4s = vec4s(cull_count);
let mut params = parameter_locations.to_vec();
params.sort_unstable();
params.dedup();
let mut primitive_params = primitive_parameter_locations.to_vec();
primitive_params.sort_unstable();
primitive_params.dedup();

let vertex_stride = 1u64
.checked_add(clip_vec4s)?
.checked_add(cull_vec4s)?
.checked_add(u64::try_from(params.len()).ok()?)?
.checked_mul(RECORD_BYTES)?;
let vertices_offset = HEADER_BYTES;
let clip_offset = RECORD_BYTES;
let cull_offset = RECORD_BYTES.checked_add(clip_vec4s.checked_mul(RECORD_BYTES)?)?;
let mut params_with_offsets = Vec::with_capacity(params.len());
for (index, location) in params.into_iter().enumerate() {
let vec4_index = 1u64
.checked_add(clip_vec4s)?
.checked_add(cull_vec4s)?
.checked_add(u64::try_from(index).ok()?)?;
params_with_offsets.push((location, vec4_index.checked_mul(RECORD_BYTES)?));
}
let primitive_stride = 1u64
.checked_add(u64::try_from(primitive_params.len()).ok()?)?
.checked_mul(RECORD_BYTES)?;
let primitive_params_with_offsets = primitive_params
.into_iter()
.enumerate()
.map(|(index, location)| {
Some((
location,
RECORD_BYTES.checked_add(u64::try_from(index).ok()?.checked_mul(RECORD_BYTES)?)?,
))
})
.collect::<Option<Vec<_>>>()?;
let primitives_offset =
vertices_offset.checked_add(u64::from(max_vertices).checked_mul(vertex_stride)?)?;
let slot_bytes =
primitives_offset.checked_add(u64::from(max_primitives).checked_mul(primitive_stride)?)?;

Some(MeshComputeLayout {
slot_bytes,
vertices_offset,
vertex_stride,
primitives_offset,
primitive_stride,
max_vertices,
max_primitives,
clip_count,
clip_offset,
cull_count,
cull_offset,
params: params_with_offsets,
primitive_params: primitive_params_with_offsets,
})
}

#[cfg(test)]
mod tests {
use super::{MeshComputeLayout, mesh_compute_layout};

#[test]
fn aligns_records_and_sorts_unique_parameter_locations() {
assert_eq!(
mesh_compute_layout(2, 3, 5, 1, &[8, 2, 8], &[6, 4, 6]),
Some(MeshComputeLayout {
slot_bytes: 352,
vertices_offset: 16,
vertex_stride: 96,
primitives_offset: 208,
primitive_stride: 48,
max_vertices: 2,
max_primitives: 3,
clip_count: 5,
clip_offset: 16,
cull_count: 1,
cull_offset: 48,
params: vec![(2, 64), (8, 80)],
primitive_params: vec![(4, 16), (6, 32)],
})
);
}

#[test]
fn supports_slots_without_optional_outputs() {
let layout = mesh_compute_layout(3, 4, 0, 0, &[], &[]).unwrap();
assert_eq!(layout.vertex_stride, 16);
assert_eq!(layout.vertices_offset, 16);
assert_eq!(layout.primitives_offset, 64);
assert_eq!(layout.slot_bytes, 128);
assert!(layout.params.is_empty());
assert!(layout.primitive_params.is_empty());
}

#[test]
fn rejects_layouts_whose_size_overflows() {
assert!(mesh_compute_layout(u32::MAX, u32::MAX, u32::MAX, 0, &[], &[]).is_none());
}
}
1 change: 1 addition & 0 deletions crates/victory-shader/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ mod image_atomic_tests;
#[cfg(all(test, windows))]
mod intersect_tests;
mod lower;
pub mod mesh_compute;
#[cfg(test)]
mod mtbuf_tests;
mod own_bits;
Expand Down
Loading