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
20 changes: 15 additions & 5 deletions crates/victory-gpu/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,22 @@ fn mirror(handle: u64) {
/// heap at indices in their data, where a slot never mirrored reads nothing.
#[cfg(windows)]
pub(crate) fn sync_heaps() {
#[cfg(feature = "diagnostics")]
host::stats::tick();
// Every host heap, also those the title did not write: descriptors
// made by calls leave views to settle too.
let hosts = std::cell::RefCell::new(Vec::new());
sync_heaps_with(
|heap| heap.host.get()?.as_ref().map(std::sync::Arc::clone),
|heap| {
let host = heap.host.get()?.as_ref().map(std::sync::Arc::clone)?;
hosts.borrow_mut().push(std::sync::Arc::clone(&host));
Some(host)
},
|host, first, last, step, bytes| host.mirror_bytes(first, last, step, bytes),
);
// Views that only moved between the title's slots kept their host
// slots; those no slot holds any more give theirs back.
for host in hosts.into_inner() {
host.settle();
}
}

/// Collects dirty ranges under `SHADER_HEAPS`, drops the lock, safely copies
Expand Down Expand Up @@ -304,8 +314,8 @@ pub(crate) fn with_host<R>(heap: usize, f: impl FnOnce(&host::HostHeap) -> R) ->
Some(f(host))
}

/// Diagnostics: how the host view heaps' title blocks and dynamic slots
/// are used, over the last `frames` frames.
/// Diagnostics: how the host view heaps' slots for the title's views and
/// their dynamic slots are used, over the last `frames` frames.
#[cfg(all(windows, feature = "diagnostics"))]
pub(crate) fn log_heaps(frames: f64) {
let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
Expand Down
1,290 changes: 613 additions & 677 deletions crates/victory-gpu/src/descriptor/host.rs

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,17 +685,17 @@ impl State {
}
for (views, kind) in [(&stage.root_cbvs, 0), (&stage.root_srvs, 1), (&stage.root_uavs, 2)] {
for &(slot, param) in views {
let blocks = slot == victory_shader::emit::VIEW_BLOCKS;
if kind == 1 && (blocks || slot == victory_shader::emit::VIEW_STRIDES) {
// The bound view heap's stride table, or its block
let slots = slot == victory_shader::emit::VIEW_SLOTS;
if kind == 1 && (slots || slot == victory_shader::emit::VIEW_STRIDES) {
// The bound view heap's stride table, or its slot
// table; without one the shader would read an
// unbound root view.
let heap = (self.heaps[0] != 0)
.then(|| crate::descriptor::heap(self.heaps[0] as This).host())
.flatten();
let table = heap.and_then(|h| if blocks { h.blocks() } else { h.strides() });
let table = heap.and_then(|h| if slots { h.slots() } else { h.strides() });
let Some(table) = table else {
once!("a shader reads view strides or blocks without a view heap: dropped");
once!("a shader reads view strides or slots without a view heap: dropped");
not_run("view strides without a view heap");
return false;
};
Expand Down Expand Up @@ -1143,10 +1143,10 @@ impl State {
}
// Tables whose slots the host heap does not hold in one run are
// made afresh: those reaching past a host heap smaller than the
// title's, and where the host holds the title's slots by blocks
// (its limit, 1,000,000 views on NVIDIA) those reaching into a
// block without host slots or across blocks that lie apart. A
// table the host holds elsewhere than the title is read there.
// title's. (Where the host caps its view heaps, 1,000,000 views on
// NVIDIA, shaders find each entry through the view-slot table:
// every table is read where the title has it.) A table the host
// holds elsewhere than the title is read there.
let mut made = Vec::new();
for (slot, table, first, last) in spans {
let Some(heap) = self.heap_of(table) else {
Expand Down
10 changes: 6 additions & 4 deletions crates/victory-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,17 +229,19 @@ fn create() -> windows::core::Result<Host> {
let shader_host = victory_shader::emit::host::Host {
device: device.clone(),
};
// Translated shaders index view heaps of `HEAP_VIEWS` slots unless
// the host makes none that large (NVIDIA): said before the first
// translation (`crate::shader_cache`) and the first heap.
crate::descriptor::host::host_views(shader_host.view_heap_limit());
match shader_host.fuses_fmad() {
Ok(true) => {}
Ok(false) => tracing::warn!(
"the host GPU does not fuse multiply-adds: results of the console's fused v_fma and v_fmac can differ in kind here"
),
Err(e) => tracing::debug!(error = %e, "multiply-add fusing not checked"),
}
// Translated shaders index view heaps of `HEAP_VIEWS` slots unless
// the host makes none that large (NVIDIA): said before the first
// translation (`crate::shader_cache`) and the first heap, but after
// the probe above, which binds no view-slot table (translated for a
// capped heap, its table entry would be looked up there).
crate::descriptor::host::host_views(shader_host.view_heap_limit());
Ok(Host {
device,
queue,
Expand Down
2 changes: 1 addition & 1 deletion crates/victory-gpu/src/pipeline/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn open(device: &ID3D12Device1, blob: Vec<u8>) -> Option<Library> {
/// host descriptions).
fn tag(blob: &[u8]) -> String {
let limit = victory_shader::emit::loop_limit().to_le_bytes();
let views = [victory_shader::emit::heap_views(), u32::from(victory_shader::emit::view_blocks())].map(u32::to_le_bytes).concat();
let views = [victory_shader::emit::heap_views(), u32::from(victory_shader::emit::view_slots())].map(u32::to_le_bytes).concat();
let translator = victory_shader::SOURCE_HASH.as_bytes();
let mut h = blake3::Hasher::new();
for part in [
Expand Down
23 changes: 12 additions & 11 deletions crates/victory-gpu/src/shader_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,20 @@ fn key(translator: &str, encoding: &[u8], limit: u32) -> [u8; 32] {

/// [`build`] as this host's entries are keyed: a translation also depends
/// on the host's view heaps where they are smaller than translations
/// assume (`victory_shader::emit::set_view_heap`: `views` slots, which hold
/// the title's by `blocks`). Elsewhere the build itself.
fn host_build(build: &[u8; 32], views: u32, blocks: bool) -> [u8; 32] {
if (views, blocks) == (victory_shader::emit::HEAP_VIEWS, false) {
/// assume (`victory_shader::emit::set_view_heap`: `views` slots, which the
/// title's lead to through the view-slot table with `slots`). Elsewhere
/// the build itself.
fn host_build(build: &[u8; 32], views: u32, slots: bool) -> [u8; 32] {
if (views, slots) == (victory_shader::emit::HEAP_VIEWS, false) {
return *build;
}
let mut h = blake3::Hasher::new();
h.update(build);
h.update(&views.to_le_bytes());
h.update(&[
u8::from(blocks),
victory_shader::emit::VIEW_BLOCK_SHIFT as u8,
]);
// Not the keys of the translations that found the title's slots by
// blocks of 1,024 (a flag and the shift, 10).
h.update(&[u8::from(slots)]);
h.update(b"view slots");
*h.finalize().as_bytes()
}

Expand Down Expand Up @@ -122,14 +123,14 @@ pub fn host_shader(container: &[u8], target: Target) -> (Result<HostShader, Stri
// The host first: making it says what its view heaps are like, which
// translations depend on. Without a host they are as for most hosts.
let _ = crate::host::host();
let (views, blocks) = (
let (views, slots) = (
victory_shader::emit::heap_views(),
victory_shader::emit::view_blocks(),
victory_shader::emit::view_slots(),
);
match CACHE.get() {
Some(cache) => cached(
cache,
&target_key(&host_build(build(), views, blocks), target),
&target_key(&host_build(build(), views, slots), target),
container,
translate,
),
Expand Down
31 changes: 13 additions & 18 deletions crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use super::UserData;
use super::bitcode::{Attr, Bin, Cast, Term, V, pred};
use super::container::SigElement;
use super::shader::{Shader, VIEW_BLOCK_SHIFT, VIEW_BLOCKS, VIEW_SKIP_SHIFT, VIEW_STRIDES, flags};
use super::shader::{Shader, VIEW_SKIP_SHIFT, VIEW_SLOTS, VIEW_STRIDES, flags};
use crate::ShaderError;
use crate::gfx10::{Dpp, Fields, NULL, Operand};
use crate::io::{
Expand Down Expand Up @@ -5848,31 +5848,26 @@ impl<'a> Lower<'a> {

/// The host's heap index for the view heap index `index` that a load
/// of a descriptor from a root parameter of type `root` gives, computed
/// in the current block. The same index, except for a descriptor from
/// the resource heap itself on a host whose view heaps hold the title's
/// slots by blocks (module notes, "Heap indices"): there the index is
/// the title's, and the host's is the view-block table's entry for its
/// block (root SRV [`VIEW_BLOCKS`]) plus the index within the block. A
/// table's slots are the host's already: the host passes where the
/// table lies.
/// in the current block. The same index, except on a host whose view
/// heaps hold the title's views wherever a slot was free (module notes,
/// "Heap indices"): there the index is the title's, from the resource
/// heap itself or through a table (the host passes where the title has
/// the table), and the host's is the view-slot table's entry for it
/// (root SRV [`VIEW_SLOTS`]).
fn host_index(&mut self, root: Option<RootType>, index: V, sampler: bool) -> V {
if sampler || root != Some(RootType::ResourceHeap) || !super::view_blocks() {
let views = matches!(root, Some(RootType::ResourceHeap | RootType::Table));
if sampler || !views || !super::view_slots() {
return index;
}
let c = self.cur;
// The table has an entry for each block below `HEAP_VIEWS`.
// The table has an entry for each index below `HEAP_VIEWS`.
let last = self.s.m.i32(super::HEAP_VIEWS - 1);
let over = self.s.m.cmp(c, pred::IUGT, index, last);
let index = self.s.m.select(c, over, last, index);
let shift = self.s.m.i32(VIEW_BLOCK_SHIFT);
let block = self.s.m.bin(c, Bin::LShr, index, shift);
let two = self.s.m.i32(2);
let at = self.s.m.bin(c, Bin::Shl, block, two);
let table = self.s.root_srv_handle(VIEW_BLOCKS);
let first = self.raw_dword(table, at);
let within = self.s.m.i32((1 << VIEW_BLOCK_SHIFT) - 1);
let within = self.s.m.bin(c, Bin::And, index, within);
self.s.m.bin(c, Bin::Add, first, within)
let at = self.s.m.bin(c, Bin::Shl, index, two);
let table = self.s.root_srv_handle(VIEW_SLOTS);
self.raw_dword(table, at)
}

/// Whether a descriptor comes from the resource heap itself (root type
Expand Down
32 changes: 17 additions & 15 deletions crates/victory-shader/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
//! host copy (and kind) serves every draw of a list. In programs that
//! use title memory (below) root SRVs and UAVs are title memory instead;
//! elsewhere [`HostShader::root_srvs`] and [`HostShader::root_uavs`] now
//! hold only the host's own tables (`VIEW_STRIDES`, `VIEW_BLOCKS`).
//! hold only the host's own tables (`VIEW_STRIDES`, `VIEW_SLOTS`).
//! * **Vertex fetch** (added 2026-11-23). Grand Theft Auto VI's mesh and
//! vertex programs fetch vertices themselves with typed-format loads
//! (`tbuffer_load_format_*`): the format (8_8_8_8 unorm, 16_16 snorm,
Expand Down Expand Up @@ -107,18 +107,20 @@
//! view heap that large (1,000,000 slots at most on NVIDIA) says how
//! large its view heaps are ([`set_view_heap`]): view indices are then
//! clamped below that ([`heap_views`]). Such a heap cannot hold every
//! slot of a title heap larger than itself, so it holds the title's
//! slots by blocks of 2^[`VIEW_BLOCK_SHIFT`], each wherever the host put
//! it ([`view_blocks`]): an index a shader takes from the resource heap
//! itself is the title's, clamped below [`HEAP_VIEWS`], and becomes the
//! host's through the view-block table (root SRV `t`[`VIEW_BLOCKS`] in
//! [`ROOT_CBV_SPACE`], listed in [`HostShader::root_srvs`]): dword
//! `index >> VIEW_BLOCK_SHIFT` plus the index within the block; a block
//! the heap does not hold reads null slots. Descriptor tables need
//! nothing: the host passes where a table lies in its own heap, as it
//! does for tables it makes afresh. The view-stride table and the view
//! addresses are read at the host's index. Translations differ by these
//! two values and by nothing else of the host.
//! slot of a title heap larger than itself, so it holds each distinct
//! view of the title's once, wherever a slot was free ([`view_slots`];
//! a title's slots repeat their views many times over): a view heap
//! index a shader computes, from the resource heap itself or through a
//! descriptor table, is the title's, clamped below [`HEAP_VIEWS`], and
//! becomes the host's through the view-slot table (root SRV
//! `t`[`VIEW_SLOTS`] in [`ROOT_CBV_SPACE`], listed in
//! [`HostShader::root_srvs`]): dword `index` is the host's index, that
//! of a null view for a slot without one. The host passes where the
//! title has a table, and makes none afresh (a table's views do not
//! follow each other in such a heap; its own slots lead to themselves).
//! The view-stride table and the view addresses are read at the host's
//! index. Translations differ by these two values and by nothing else
//! of the host.
//! * **Buffer views at computed heap indices** (added 2026-11-24): a
//! buffer read through a descriptor the shader takes from the resource
//! heap itself first looks up the view-stride table (root SRV
Expand Down Expand Up @@ -267,8 +269,8 @@ use crate::{ShaderError, io, ir, resource, structure};
pub use sampler::StaticSampler;
pub use shader::{
HEAP_SAMPLERS, HEAP_VIEWS, MEMORY_SPACE, ROOT_CBV_SPACE, SAMPLER_SPACE, USER_DATA_SPACE,
VIEW_BLOCK_SHIFT, VIEW_BLOCKS, VIEW_MARK_NONE, VIEW_MARKS, VIEW_SKIP_SHIFT, VIEW_STRIDES,
VIEW_WRITE_BITS, VIEW_WRITES, heap_views, null_buffer_view, set_view_heap, view_blocks,
VIEW_MARK_NONE, VIEW_MARKS, VIEW_SKIP_SHIFT, VIEW_SLOTS, VIEW_STRIDES, VIEW_WRITE_BITS,
VIEW_WRITES, heap_views, null_buffer_view, set_view_heap, view_slots,
};

/// The loop limit without an override: 2^20.
Expand Down
39 changes: 19 additions & 20 deletions crates/victory-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,12 @@ pub const ROOT_CBV_SPACE: u32 = 1;
/// the buffer view at view-heap index `i`, and in its top bits
/// ([`VIEW_SKIP_SHIFT`]) the bytes the host's view starts before it.
pub const VIEW_STRIDES: u8 = 255;
/// The root SRV slot of the host's view-block table, in [`ROOT_CBV_SPACE`]:
/// read only by shaders translated for a host that holds the title's view
/// slots by blocks ([`set_view_heap`]). Its dword `b` is the heap index of
/// the first of the title's slots `b << VIEW_BLOCK_SHIFT ..`, for every `b`
/// below [`HEAP_VIEWS`]` >> VIEW_BLOCK_SHIFT`.
pub const VIEW_BLOCKS: u8 = 254;
/// Title view slots come in blocks of `1 << VIEW_BLOCK_SHIFT` on such a
/// host: a block's slots follow each other in the host's heap, in the
/// title's order, wherever the block lies.
pub const VIEW_BLOCK_SHIFT: u32 = 10;
/// The root SRV slot of the host's view-slot table, in [`ROOT_CBV_SPACE`]:
/// read only by shaders translated for a host whose view heaps hold the
/// title's views wherever a slot was free ([`set_view_heap`]). Its dword
/// `i` is the heap index of the view the title's slot `i` holds (of a null
/// view for a slot without one), for every `i` below [`HEAP_VIEWS`].
pub const VIEW_SLOTS: u8 = 254;
/// Where a view-stride table dword keeps the bytes (0, 4, 8 or 12) the
/// host's raw view of a structured buffer view starts before the view's
/// first byte; the stride is in the bits below. A raw view starts on 16
Expand All @@ -49,7 +45,8 @@ pub const HEAP_VIEWS: u32 = 1 << 21;
pub const HEAP_SAMPLERS: u32 = 2048;

/// What translations assume of the host's view heaps: their slots, and
/// whether they hold the title's slots by blocks ([`set_view_heap`]).
/// whether the title's slots lead to them through the view-slot table
/// ([`set_view_heap`]).
static VIEW_HEAP: [std::sync::atomic::AtomicU32; 2] = [
std::sync::atomic::AtomicU32::new(HEAP_VIEWS),
std::sync::atomic::AtomicU32::new(0),
Expand Down Expand Up @@ -78,11 +75,12 @@ pub fn heap_views() -> u32 {
view_heap().0
}

/// Whether the host's view heaps hold the title's slots by blocks: an
/// index a shader takes from the resource heap itself is the title's, and
/// goes through the view-block table ([`VIEW_BLOCKS`]) to the host's.
/// False unless the host said otherwise ([`set_view_heap`]).
pub fn view_blocks() -> bool {
/// Whether the host's view heaps hold the title's views wherever a slot
/// was free: a view heap index a shader computes (from the resource heap
/// itself or through a table) is the title's, and goes through the
/// view-slot table ([`VIEW_SLOTS`]) to the host's. False unless the host
/// said otherwise ([`set_view_heap`]).
pub fn view_slots() -> bool {
view_heap().1
}

Expand All @@ -91,14 +89,15 @@ pub fn view_blocks() -> bool {
/// undefined there, a removed device in practice): every view heap has
/// `views` slots, the last three null as below. Such a heap has no room for
/// every slot of a title heap that large beside the host's own, so with
/// `blocks` it holds the title's slots by blocks, each wherever it fits
/// ([`VIEW_BLOCKS`]). Set before the first translation. The values are part
/// `slots` it holds each distinct view of the title's once, wherever a slot
/// was free, and the title's slots lead there through a table
/// ([`VIEW_SLOTS`]). Set before the first translation. The values are part
/// of the translation, so a shader cache must key on them (`victory-gpu`'s
/// does).
pub fn set_view_heap(views: u32, blocks: bool) {
pub fn set_view_heap(views: u32, slots: bool) {
let relaxed = std::sync::atomic::Ordering::Relaxed;
VIEW_HEAP[0].store(views.clamp(4, HEAP_VIEWS), relaxed);
VIEW_HEAP[1].store(u32::from(blocks), relaxed);
VIEW_HEAP[1].store(u32::from(slots), relaxed);
}

/// The heap index of the host's null raw buffer view (the null raw
Expand Down
Loading