Skip to content
Merged
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
1 change: 1 addition & 0 deletions docs/ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ The launcher sets these for a Proton or Wine launch. A value you set yourself is
| `TUXBOX_REFERENCE_CBV` | `1` applies the constant-buffer layer to a provider TuxBox didn't build |
| `TUXBOX_RASTER_ROOT_FALLBACK` | Try an unsupported raster root signature unchanged |
| `TUXBOX_DESCRIPTOR_HEAP_LIMIT` | Cap shader-visible descriptor heaps to force paging. Without it a heap over 1,000,000 descriptors gets what the driver allows, at most the 1,048,575 slots the heap bitmaps name |
| `TUXBOX_CONSOLE_BUILT` | With `TUXBOX_CONSOLE_STRUCTURES`, which of the structures the title builds while it runs are written for the walks. By default those in packed positions, which are final in title memory when the build is called. `0` writes none. `late` also writes those in float positions, at the call and once more when the GPU is past that frame, for a host whose GPU works in the title's own memory. `all` also writes those in float positions as the CPU reads them at the call, which is wrong for a mesh the GPU writes afterwards. For comparing pictures |
| `TUXBOX_CONSOLE_WALKS` | With `TUXBOX_CONSOLE_STRUCTURES`, give the buffer only to the walk programs whose source digest begins with one of these comma-separated prefixes. The others keep their empty scenes. For finding which walk a fault or a wrong picture comes from, and for a title whose walks cannot all be given the buffer yet: in ProjectScarlett only the four distant-shadow walks can (`a902033a,bd054a51,65f82768,3b375136`); with the others the hit-resolve programs they feed do not return and the device is lost |
| `TUXBOX_PLACEMENT_LAZY_FLUSH` | `1` looks for written placed textures only on the first `D3DFlushEntireCpuCache` after a submission. Later flushes leave their uploads to the next submission, which a batch already waiting behind a label can then precede |
| `TUXBOX_PLACEMENT_WATCH` | `0` checks every placed texture against the title's memory before each batch, instead of only those XMem's write watch saw written or cannot track |
Expand Down
2 changes: 2 additions & 0 deletions runtime/d3d12/src/commands/build_raytracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::System::{Diagnostics::Debug::ReadProcessMemory, Threading::GetCurrentProcess};
use windows::core::{GUID, IUnknown, Result};

mod box_tree;
mod console;
mod console_structure;
pub(super) mod guest_addresses;
mod meshlet_indices;
mod packed_positions;
Expand Down
123 changes: 123 additions & 0 deletions runtime/d3d12/src/commands/build_raytracing/box_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
//! A four-way tree of boxes over things that each have a box.
//!
//! The console's structures are such trees: a box node holds up to four
//! children with the box of each. This builds one over leaves given by their
//! boxes, for the structures the provider writes itself (`console_scene`,
//! `console_structure`). It is not the console builder's tree: any tree whose
//! boxes hold their children gives a walk the same hits. A node's leaves are
//! halved at the median of their box centres along the axis the centres
//! spread widest on, and each half again.
// Not called outside the tests until the structures are placed for the walks.
#![cfg_attr(not(test), allow(dead_code))]

#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct Bounds {
pub(super) min: [f32; 3],
pub(super) max: [f32; 3],
}

impl Bounds {
/// The box nothing is inside, as the console keeps an empty slot.
pub(super) const NONE: Self = Self { min: [f32::MAX; 3], max: [f32::MIN; 3] };

pub(super) fn add(&mut self, other: &Bounds) {
for axis in 0..3 {
self.min[axis] = self.min[axis].min(other.min[axis]);
self.max[axis] = self.max[axis].max(other.max[axis]);
}
}

pub(super) fn of(points: &[[f32; 3]]) -> Self {
let mut bounds = Self::NONE;
for point in points {
bounds.add(&Bounds { min: *point, max: *point });
}
bounds
}

/// Whether the box holds something a ray can reach: every bound is a
/// finite number and none is missing.
pub(super) fn finite(&self) -> bool {
(0..3).all(|axis| self.min[axis].is_finite() && self.max[axis].is_finite() && self.min[axis] <= self.max[axis])
}

/// The six floats a box node keeps: the low corner, then the high one.
pub(super) fn words(&self) -> [u32; 6] {
core::array::from_fn(|at| if at < 3 { self.min[at] } else { self.max[at - 3] }.to_bits())
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Child {
Leaf(usize),
Node(usize),
}

/// The nodes, the root first and every node before the nodes below it, and
/// the box of each.
pub(super) struct Tree {
pub(super) nodes: Vec<Vec<Child>>,
pub(super) bounds: Vec<Bounds>,
}

fn halve<'a>(leaves: &'a mut [usize], boxes: &[Bounds]) -> (&'a mut [usize], &'a mut [usize]) {
let centre = |leaf: usize, axis: usize| boxes[leaf].min[axis] + boxes[leaf].max[axis];
let spread = |axis: usize| {
let (low, high) = leaves.iter().fold((f32::MAX, f32::MIN), |(low, high), &leaf| {
(low.min(centre(leaf, axis)), high.max(centre(leaf, axis)))
});
high - low
};
let axis = (0..3).max_by(|&a, &b| spread(a).total_cmp(&spread(b))).unwrap_or(0);
leaves.sort_unstable_by(|&a, &b| centre(a, axis).total_cmp(&centre(b, axis)).then(a.cmp(&b)));
leaves.split_at_mut(leaves.len() / 2)
}

fn node(leaves: &mut [usize], boxes: &[Bounds], nodes: &mut Vec<Vec<Child>>) -> usize {
let me = nodes.len();
nodes.push(Vec::new());
let groups: Vec<&mut [usize]> = if leaves.len() <= 4 {
leaves.chunks_mut(1).collect()
} else {
let (low, high) = halve(leaves, boxes);
let (a, b) = halve(low, boxes);
let (c, d) = halve(high, boxes);
vec![a, b, c, d]
};
let mut children = Vec::with_capacity(4);
for group in groups {
match group.len() {
0 => {}
1 => children.push(Child::Leaf(group[0])),
_ => children.push(Child::Node(node(group, boxes, nodes))),
}
}
nodes[me] = children;
me
}

/// The tree over leaves with these boxes. With no leaf it has no node; with
/// any, its root is a node, of one child for a single leaf.
pub(super) fn tree(boxes: &[Bounds]) -> Tree {
let mut nodes = Vec::new();
if !boxes.is_empty() {
let mut leaves: Vec<usize> = (0..boxes.len()).collect();
node(&mut leaves, boxes, &mut nodes);
}
// A node's children come after it, so the last node's box is known first.
let mut bounds = vec![Bounds::NONE; nodes.len()];
for (index, children) in nodes.iter().enumerate().rev() {
for child in children {
let below = match *child {
Child::Leaf(leaf) => boxes[leaf],
Child::Node(below) => bounds[below],
};
bounds[index].add(&below);
}
}
Tree { nodes, bounds }
}

#[cfg(test)]
#[path = "../../tests/commands/box_tree.rs"]
mod tests;
Loading
Loading