Skip to content
Open
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
13 changes: 12 additions & 1 deletion crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ impl HostHeap {
));
let strides = (!sampler)
.then(|| {
let bytes = u64::from(victory_shader::emit::SLOT_STRIDES_COUNT) * 4;
let bytes = u64::from(victory_shader::emit::SLOT_STRIDES_DWORDS) * 4;
let (buffer, data) = host.upload_buffer(bytes)?;
// SAFETY: the buffer's whole mapping; a live buffer.
let gpu = unsafe {
Expand Down Expand Up @@ -1125,6 +1125,17 @@ impl HostHeap {
// SAFETY: inside the mapping; shaders read it while we write (a
// dword, either value is one the slot had).
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0)) };
// The title address of the buffer view, after the strides
// (`victory_shader::emit::SLOT_ADDRESSES`): low dword, then high.
let address = view
.and_then(buffer_range)
.map_or(0, |range| range.0);
// SAFETY: as above, two dwords inside the mapping.
unsafe {
let at = (*data as *mut u32).add((victory_shader::emit::SLOT_ADDRESSES + 2 * index) as usize);
at.write_volatile(address as u32);
at.add(1).write_volatile((address >> 32) as u32);
}
}
let mut buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
let was = if stride.is_some() {
Expand Down
57 changes: 48 additions & 9 deletions crates/victory-shader/src/bvh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
//! Four child pointers at bytes 0-15 (`0xffff_ffff`: none), then four
//! boxes, each minimum x y z, maximum x y z: 16-bit floats from byte 16
//! (12 bytes a box), or 32-bit floats from byte 16 (24 bytes a box; bytes
//! 112-127 are not read). The result is the pointers of the children whose
//! box the ray enters before its extent, nearest entry first when the
//! descriptor enables box sorting (bit 63), and `0xffff_ffff` for the rest.
//! 112-127 are not read). The result is the pointers of the children that
//! are there and whose box the ray enters before its extent, nearest entry
//! first when the descriptor enables box sorting (bit 63), and
//! `0xffff_ffff` for the rest.
//!
//! # Triangle nodes
//!
Expand Down Expand Up @@ -64,9 +65,10 @@
//! JCGT 2013), and agree with what the title's own structure
//! builder writes and its trace programs read. Not known from either: how
//! the console rounds, which of two boxes entered at the same distance it
//! returns first, and what it returns for a ray exactly through an edge
//! (here: a hit in both triangles of the edge) or for kinds 6 and 7 (here:
//! a triangle miss; the title's programs do not read it).
//! returns first, what it returns for a ray exactly through an edge (here:
//! a hit in both triangles of the edge), for a child that is not there
//! whose box the ray enters (here: a miss) or for kinds 6 and 7 (here: a
//! triangle miss; the title's programs do not read it).

/// The result dword for a box the ray misses, or a child that is not there.
pub const NONE: u32 = 0xffff_ffff;
Expand Down Expand Up @@ -196,8 +198,10 @@ pub fn boxes(children: &[u32; 4], boxes: &[[f32; 6]; 4], ray: &Ray, sort: bool)
}
let enter = near[0].max(near[1]).max(near[2]);
let leave = far[0].min(far[1]).min(far[2]);
// A box whose minimum x is NaN is switched off.
if !b[0].is_nan() && leave >= enter.max(0.0) && enter < ray.extent {
// A child that is not there is a miss, whatever bytes its box
// holds; a box whose minimum x is NaN is switched off.
let there = children[n] != NONE && !b[0].is_nan();
if there && leave >= enter.max(0.0) && enter < ray.extent {
ids[n] = children[n];
distances[n] = enter;
}
Expand Down Expand Up @@ -473,10 +477,45 @@ pub(crate) mod tests {
[UNIT, UNIT, [f32::NAN, -1.0, -1.0, 1.0, 1.0, 1.0], OFF],
);
let ray = Ray::new([-5.0, 0.0, 0.0], [1.0, 0.0, 0.0], 100.0);
// Child 1 is hit but is not there: its pointer is what "none" is.
// Child 1's box is entered, but child 1 is not there: a miss.
assert_eq!(intersect(&node, 5, &ray, true), [8, NONE, NONE, NONE]);
}

/// A node whose children that are not there have boxes the ray
/// enters: the first before the one child's, the other two at the
/// ray's origin. (The card tests run it too.)
pub(crate) const MISSING_FIRST: ([u32; 4], [[f32; 6]; 4]) = (
[NONE, 8, NONE, NONE],
[
[-1.0, -1.0, 1.0, 1.0, 1.0, 2.0],
[-1.0, -1.0, 3.0, 1.0, 1.0, 4.0],
[0.0; 6],
[0.0; 6],
],
);

/// The ray of [`MISSING_FIRST`]: from the origin along +z.
pub(crate) fn missing_first() -> Ray {
Ray::new([0.0; 3], [0.0, 0.0, 1.0], 10.0)
}

#[test]
fn a_child_that_is_not_there_is_a_miss_whatever_its_box() {
let (children, boxes) = MISSING_FIRST;
let ray = missing_first();
for (node, kind) in [(box32(children, boxes), 5), (box16(children, boxes), 4)] {
// Sorted: the child that is there (entered at 3), then the
// three that are not, their boxes entered at 0 and 1.
assert_eq!(
intersect(&node, kind, &ray, true),
[8, NONE, NONE, NONE],
"kind {kind}"
);
// Not sorted: child order.
assert_eq!(intersect(&node, kind, &ray, false), children, "kind {kind}");
}
}

#[test]
fn equal_distances_keep_child_order() {
let node = box32([8, 16, 24, 32], [UNIT; 4]);
Expand Down
4 changes: 4 additions & 0 deletions crates/victory-shader/src/emit/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,10 @@ impl<'a> Lower<'a> {
let on = self.s.m.bin(c, Bin::Xor, off, on);
let hit = self.s.m.bin(c, Bin::And, through, before);
let hit = self.s.m.bin(c, Bin::And, hit, on);
// A child that is not there is a miss, whatever bytes its box
// holds: it must not sort before a child that is.
let there = self.s.m.cmp(c, pred::IULT, children[n], none);
let hit = self.s.m.bin(c, Bin::And, hit, there);
ids[n] = self.s.m.select(c, hit, children[n], none);
distances[n] = self.s.m.select(c, hit, enter, infinity);
}
Expand Down
11 changes: 11 additions & 0 deletions crates/victory-shader/src/emit/intersect_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ fn scene() -> (Scene, Vec<Vec<Case>>) {
let ids2 = [8, bvh::NONE, 24, 0x1fff_fffc];
box_nodes.push((s.add(nodes::box32(ids2, around), 32), 5));
box_nodes.push((s.add(nodes::box16(ids2, around), 16), 4));
// A child that is not there whose box the ray enters first: the
// reference's own test of it, in both widths, with its ray alone.
let (missing, missing_boxes) = nodes::MISSING_FIRST;
let missing_nodes = [
(s.add(nodes::box32(missing, missing_boxes), 32), 5),
(s.add(nodes::box16(missing, missing_boxes), 16), 4),
];
// Random boxes.
for n in 0..6 {
let boxes: [[f32; 6]; 4] = std::array::from_fn(|_| {
Expand Down Expand Up @@ -357,6 +364,10 @@ fn scene() -> (Scene, Vec<Vec<Case>>) {
cases.push(Some((n, kind, Ray::new(origin, direction, extent))));
}
}
// The reference's test of a child that is not there, at its ray.
for &(n, kind) in &missing_nodes {
cases.push(Some((n, kind, nodes::missing_first())));
}
// Random rays at every box node, half of them aimed at a box.
for &(n, kind) in &box_nodes {
let words = s.node(n);
Expand Down
Loading