Skip to content
Merged
2 changes: 1 addition & 1 deletion crates/victory-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ publish.workspace = true
blake3.workspace = true
sha2.workspace = true
miniz_oxide.workspace = true
smallvec = { version = "1.16", features = ["union"] }
smallvec = { version = "1.16", features = ["const_new", "union"] }
tracing.workspace = true
anyhow.workspace = true
victory-formats.workspace = true
Expand Down
28 changes: 28 additions & 0 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,34 @@ impl HostHeap {
self.note_address(index, Some(view));
}

/// Makes slot `index` (a dynamic one) again from `record`, which the
/// title wrote at the slot's place in its own table after the slot was
/// made, and returns the view recorded. As [`HostHeap::mirror`] makes a
/// title slot when it is written and no draw reads it yet: a view of a
/// texture the title placed itself stays null, and nothing the view
/// needs is made for it. `half`: the shader the slot was made for moves
/// halves through it, so a 16-bit float or signed buffer view is made
/// as its unsigned twin ([`half_view`]), as when the table was made.
pub(crate) fn remake_at(&self, index: u32, record: [u32; 3], half: bool) -> Option<ViewPin> {
let [tag, i, generation] = record;
let Some(view) = (tag == VIEW_TAG).then(|| view_at(i, generation)).flatten() else {
self.make_null(index);
return None;
};
if is_placed_texture(&view) {
null_for(&view, self.cpu(index));
self.keep(index, None);
self.set_stride(index, Some(&view));
self.set_mark(index, None);
self.note_address(index, Some(&view));
} else if let Some(twin) = half.then(|| half_view(&view)).flatten() {
self.make_at(index, &twin);
} else {
self.make_at(index, &view);
}
Some(view)
}

/// Makes slot `index` (a dynamic one) a raw view, shader resource or
/// unordered access (`uav`), of `bytes` of `buffer` from byte `first`
/// (both multiples of 4), which it holds; a null one for no buffer, or
Expand Down
1,610 changes: 1,534 additions & 76 deletions crates/victory-gpu/src/draw.rs

Large diffs are not rendered by default.

192 changes: 192 additions & 0 deletions crates/victory-gpu/src/fresh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
//! Tables made afresh: why, and what tells them apart.
//!
//! A shader reads the title's descriptor tables in place, from the host
//! slots that mirror the title's own, unless something has to differ for
//! that shader ([`Why`]): the table reaches past a capped host heap; the
//! shader moves a 16-bit float or signed buffer view as integers, and is
//! shown the unsigned view of the same layout, its twin
//! (`descriptor::host::half_view`); a constant view reads a snapshot of its
//! bytes (`crate::rewritten`). Such a table is made afresh in dynamic slots
//! (`crate::draw`, `State::tables`), once for all its reasons, and once
//! within a list for all the passes it serves alike.
//!
//! "Alike" is the [`key`]: each slot's record, whether the shader moves
//! halves there (the slot then holds a twin where another shader's holds
//! the title's view), the snapshot a constant view reads there (a title
//! copy ends it, and the passes after the copy read another), and whether
//! the table's slots are made again at submission where the title rewrote
//! them. A table that needs a twin and a snapshot is so one table holding
//! both, and no table with only one of the two, or with another snapshot,
//! stands for it.
//!
//! Plain std: the decisions are tested on any host.

use std::hash::{Hash, Hasher};

/// Why a table is made afresh for a shader. Any of the three, together.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Why {
/// The shader moves halves through a descriptor at which the title has
/// a 16-bit float or signed buffer view: the slot shows its twin.
pub(crate) twin: bool,
/// The table reaches past a host heap smaller than the title's: the
/// title's slots there have no host slot of their own.
pub(crate) capped: bool,
/// The table holds a constant view of bytes a title copy rewrites under
/// a pass: the slot shows a snapshot of them.
pub(crate) snapshot: bool,
}

impl Why {
/// Whether the shader reads the title's own slots when no dynamic slots
/// are left for the table: only if it was wanted afresh for nothing but
/// its snapshots, which the pass then does without, as before snapshots.
/// In place a shader that moves halves would read them through the
/// title's format, and past a capped heap there are no slots to read:
/// such a draw is dropped instead.
pub(crate) fn in_place_without_slots(self) -> bool {
self.snapshot && !self.twin && !self.capped
}

/// Whether the table's slots are made again at submission where the
/// title rewrote them after the pass was recorded (`crate::draw`,
/// `remake_table`): every table that holds a snapshot, as its slots in
/// place would show the title's records of then, a twin among its
/// slots made again as a twin. Without a snapshot a table made afresh
/// stays as recorded, and so does one past a capped heap, with or
/// without a snapshot: both as they were before snapshots.
pub(crate) fn followed(self) -> bool {
self.snapshot && !self.capped
}
}

/// One slot of a table made afresh, as far as it tells tables apart.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct Slot {
/// The title's record at the slot's place in its own table.
pub(crate) record: [u32; 3],
/// The shader moves 16-bit channels as integers through the view there
/// (`HostShader::half_views`): a 16-bit float or signed buffer view is
/// shown as its unsigned twin.
pub(crate) half: bool,
/// The snapshot a constant view there reads: its scratch buffer (by
/// address) and the offset in it.
pub(crate) snapshot: Option<(usize, u64)>,
}

/// The key of the table holding `slots`, among the tables a list makes
/// afresh from one title table. `followed`: its slots are made again at
/// submission where the title rewrote them.
pub(crate) fn key(slots: &[Slot], followed: bool) -> u64 {
let mut h = std::collections::hash_map::DefaultHasher::new();
slots.hash(&mut h);
followed.hash(&mut h);
h.finish()
}

#[cfg(test)]
mod tests {
use super::*;

/// A table that needs a twin and a snapshot keeps what each gives it:
/// it is never read in place (the shader would read the 16-bit view in
/// the title's format), and it follows the slots the title rewrites, as
/// a table made for a snapshot alone does.
#[test]
fn a_table_with_a_twin_and_a_snapshot_keeps_both() {
let why = |twin, capped, snapshot| Why {
twin,
capped,
snapshot,
};
let both = why(true, false, true);
assert!(
!both.in_place_without_slots(),
"its draw is dropped without slots"
);
assert!(both.followed(), "made again where the title rewrites it");
// Each reason alone.
let snapshot = why(false, false, true);
assert!(snapshot.in_place_without_slots() && snapshot.followed());
let twin = why(true, false, false);
assert!(
!twin.in_place_without_slots() && !twin.followed(),
"as recorded"
);
let capped = why(false, true, false);
assert!(
!capped.in_place_without_slots() && !capped.followed(),
"as recorded"
);
// Past a capped heap a snapshot changes nothing but the constants.
for twin in [false, true] {
let all = why(twin, true, true);
assert!(!all.in_place_without_slots() && !all.followed());
}
}

/// A table of three slots: constants, a 16-bit buffer view, a target.
fn table(half: bool, snapshot: Option<(usize, u64)>) -> [Slot; 3] {
let slot = |index, half, snapshot| Slot {
record: [0x5649, index, 1],
half,
snapshot,
};
[
slot(7, false, snapshot),
slot(8, half, None),
slot(9, false, None),
]
}

/// A table that needs a twin and a snapshot is one table: the passes
/// between two title copies share it, and the tables with one of the two
/// only, with another snapshot or with other records are other tables.
#[test]
fn a_table_with_a_twin_and_a_snapshot_is_a_table_of_its_own() {
let taken = Some((0x7000_1000, 256));
let both = key(&table(true, taken), true);
assert_eq!(
both,
key(&table(true, taken), true),
"made once for the passes that read it alike"
);
let others = [
// Before the constants were known, or with snapshots off.
("the twin alone", key(&table(true, None), false)),
// A shader that reads the 16-bit view in the title's format.
("the snapshot alone", key(&table(false, taken), true)),
// The title's own slots, in dynamic ones (a capped heap).
("neither", key(&table(false, None), false)),
// The same slots past a capped heap, which stay as recorded.
("both, as recorded", key(&table(true, taken), false)),
// A title copy ended the snapshot.
(
"a later snapshot",
key(&table(true, Some((0x7000_1000, 512))), true),
),
(
"a snapshot in another chunk",
key(&table(true, Some((0x7000_2000, 256))), true),
),
];
for (k, (what, other)) in others.iter().enumerate() {
assert_ne!(both, *other, "{what}");
for (earlier, same) in &others[..k] {
assert_ne!(other, same, "{what} and {earlier}");
}
}
// The halves at another slot, the snapshot at another slot, another
// record at a slot, and a shorter table.
let mut moved = table(true, taken);
(moved[1].half, moved[2].half) = (false, true);
assert_ne!(both, key(&moved, true));
let mut moved = table(true, taken);
(moved[0].snapshot, moved[2].snapshot) = (None, taken);
assert_ne!(both, key(&moved, true));
let mut rewritten = table(true, taken);
rewritten[2].record[2] += 1;
assert_ne!(both, key(&rewritten, true));
assert_ne!(both, key(&table(true, taken)[..2], true));
}
}
Loading