victory-gta6: this fork's changes on top of VIctory main (ffe310e)

World rendering, lighting, frame-rate and memory work for Grand Theft Auto VI
(Xbox Series X profile) on one Windows PC with an NVIDIA RTX 4070. The README
describes the state, the changes and the credits. Based on VIctory by Marvisak
and contributors (GPL-3.0-or-later); XCL by ivall and contributors was used as
a reference. No game files are included.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
fxcelessandClaude Fable 5.2 committed 2026-11-24 13:02:18 -05:00
1 parent 45c6e80601
commit 6fa814fba5
61 files changed
+32259 -1274

No files matched your search

+196 -5
View File
@@ -13,13 +13,73 @@ pub const SAMPLER_SPACE: u32 = 0;
/// `t<root parameter index>` in this space.
pub const ROOT_CBV_SPACE: u32 = 1;
/// The root SRV `t<SLOT_STRIDES>` in [`ROOT_CBV_SPACE`] of a shader that
/// reads buffers through descriptors it picks from the view heap at an
/// index it computes (SM 6.6 dynamic resources): a raw buffer of
/// [`SLOT_STRIDES_COUNT`] dwords the host keeps, dword `i` the bytes per
/// element of the buffer view at heap slot `i` (0: none or not a buffer).
/// The console's descriptor holds that stride and its hardware multiplies
/// the element index by it; the program's code does not say it, and the
/// used-resource list only names what tables hold. A shader that has this
/// root SRV also tells the host that the buffer views of the whole heap
/// must be ready when it runs, not only those its tables name.
pub const SLOT_STRIDES: u8 = 255;
/// Dwords of the [`SLOT_STRIDES`] buffer: heap indices are clamped to it.
pub const SLOT_STRIDES_COUNT: u32 = 1 << 21;
/// Title memory by address (`resource::MEMORY`): console code reads and
/// writes through raw 40-bit addresses it takes from root constants,
/// descriptors and other memory (the ray-tracing structure builder and the
/// programs that walk its structures). The host keeps copies of the title
/// memory shaders use; a shader with such an access finds the copy by the
/// address:
///
/// - the root SRV `t<SLOT_MEMORY>` in [`ROOT_CBV_SPACE`] is a directory of
/// [`MEMORY_PAGES`] entries of two dwords, one per 64 KiB of title
/// address space: the number of the host copy holding that page (0: none
/// yet) and the page's byte offset in that copy;
/// - copy `n` is the raw buffer view at view-heap slot `base + n`, `base`
/// the user-data dword of kind [`USER_MEMORY_BASE`]; slot `base` itself
/// reads zeros and drops writes;
/// - the root UAV `u<SLOT_FAULTS>` takes the page numbers of accesses that
/// found no copy ([`MEMORY_FAULTS`] dwords, dword 0 a sink), which the
/// host reads afterwards to make those copies for the frames to come.
pub const SLOT_MEMORY: u8 = 254;
/// See [`SLOT_MEMORY`].
pub const SLOT_FAULTS: u8 = 253;
/// Entries of the [`SLOT_MEMORY`] directory: title addresses below 128 GiB.
pub const MEMORY_PAGES: u32 = 1 << 21;
/// Dwords of the [`SLOT_FAULTS`] buffer.
pub const MEMORY_FAULTS: u32 = 4096;
/// `RootType::Other` code of a user-data dword the host computes: the view
/// heap slot of the memory copies' views ([`SLOT_MEMORY`]).
pub const USER_MEMORY_BASE: u32 = 0x100;
/// `RootType::Other` code of a user-data dword the host computes: the low
/// 32 bits of the title address of the buffer view at dword `dword` of the
/// descriptor table of root parameter `slot`; [`USER_VIEW_ADDRESS`] + 1:
/// its bits from 32 up. (Console code reads the address out of a buffer
/// descriptor and uses it as a pointer.)
pub const USER_VIEW_ADDRESS: u32 = 0x101;
/// `RootType::Other` code of a user-data dword the host computes: the
/// bytes (0 to 15) by which the raw view the host binds for the root view
/// of root parameter `slot` begins before the title's address. A raw view
/// begins on 16 bytes of its host buffer; the console's root views begin
/// anywhere (the structure builder's counters sit at addresses ending in
/// 4 and c). The shader adds the dword to every byte offset through that
/// view. Asked for every root UAV, and for the root SRVs of programs with
/// a spilled user-data table.
pub const USER_VIEW_BIAS: u32 = 0x103;
/// `ShaderFlags` bits (metadata) and their `SFI0` feature bits.
pub(crate) mod flags {
pub const RAW_BUFFERS: u64 = 1 << 4;
pub const STENCIL_REF: u64 = 1 << 11;
pub const TYPED_UAV_LOADS: u64 = 1 << 13;
pub const WAVE_OPS: u64 = 1 << 19;
pub const BARYCENTRICS: u64 = 1 << 22;
pub const INT64: u64 = 1 << 20;
pub const ATOMIC_TYPED64: u64 = 1 << 27;
pub const ATOMIC_HEAP64: u64 = 1 << 32;
pub const RESOURCE_HEAP: u64 = 1 << 30;
pub const SAMPLER_HEAP: u64 = 1 << 31;
}
@@ -45,6 +105,25 @@ pub(crate) struct Mesh {
pub topology: u8,
}
/// What a geometry shader declares: triangles in, triangle strips out on
/// stream 0, one instance.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Geometry {
pub max_vertices: u32,
}
/// The null slot a host view heap keeps at its end when the title's heap did not fit in it
/// (0: no limit). Shaders translated while it is set read that slot for any view index at or
/// past it: a host that caps its heap (NVIDIA at 1 000 000 views; a retail game's heap is
/// larger) hung its GPU on reads past the end (GPU-based validation: "descriptor heap index
/// out of bounds"), which AMD's driver forgives.
static VIEW_LIMIT: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
/// See [`VIEW_LIMIT`]: `null_slot` is the last slot of the host's view heap, kept null.
pub fn set_view_heap_limit(null_slot: u32) {
VIEW_LIMIT.store(null_slot, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) struct Shader {
pub m: Module,
pub t: Types,
@@ -55,6 +134,8 @@ pub(crate) struct Shader {
pub prims: Vec<SigElement>,
/// A mesh shader (`threads` is its thread-group size).
pub mesh: Option<Mesh>,
/// A geometry shader (the rectangle stage, `rect.rs`); `pixel` is false.
pub geometry: Option<Geometry>,
/// Dwords of user data the shader reads (0: no user-data buffer).
pub user_data: u32,
/// Immediate samplers, bound as `s0..` in [`SAMPLER_SPACE`].
@@ -65,6 +146,8 @@ pub(crate) struct Shader {
pub root_cbvs: Vec<(u8, V)>,
/// Root SRVs (raw buffers) read, likewise.
pub root_srvs: Vec<(u8, V)>,
/// Root UAVs (raw buffers) read or written, likewise.
pub root_uavs: Vec<(u8, V)>,
pub flags: u64,
/// Heap handles made with a non-uniform index.
pub nonuniform_handles: u32,
@@ -115,11 +198,13 @@ impl Shader {
outputs: Vec::new(),
prims: Vec::new(),
mesh: None,
geometry: None,
user_data: 0,
samplers: 0,
cmp_samplers: 0,
root_cbvs: Vec::new(),
root_srvs: Vec::new(),
root_uavs: Vec::new(),
flags: 0,
nonuniform_handles: 0,
threads: None,
@@ -236,6 +321,31 @@ impl Shader {
h
}
/// The handle of the root UAV (a raw buffer) of root parameter `slot`:
/// `u<slot>` in [`ROOT_CBV_SPACE`].
pub fn root_uav_handle(&mut self, slot: u8) -> V {
if let Some(&(_, h)) = self.root_uavs.iter().find(|r| r.0 == slot) {
return h;
}
let r = self.m.i32(u32::from(slot));
let bind = [r, r, self.m.i32(ROOT_CBV_SPACE), self.m.i8(1)];
let bind = self.m.aggregate(self.t.bind, &bind);
let f = self.m.bool(false);
let block = self.entry;
let h = self.op(
block,
"createHandleFromBinding",
Attr::ReadNone,
self.t.handle,
217,
&[bind, r, f],
);
let h = self.annotate(block, h, 11 | 0x1000, 0);
self.flags |= flags::RAW_BUFFERS;
self.root_uavs.push((slot, h));
h
}
/// The handle of immediate sampler `n` (a comparison sampler when its
/// bit in `cmp_samplers` is set).
pub fn sampler_handle(&mut self, block: usize, n: u32) -> V {
@@ -286,6 +396,25 @@ impl Shader {
nonuniform: bool,
props: (u32, u32),
) -> V {
// a view index past the host heap (`set_view_heap_limit`) reads its null slot instead
let limit = VIEW_LIMIT.load(std::sync::atomic::Ordering::Relaxed);
// (as `dx.op.binary` UMin: a compare and select emitted here made the bitcode invalid)
let index = if !sampler && limit != 0 {
let null = self.m.i32(limit);
let i32t = self.t.i32;
self.op(block, "binary.i32", Attr::ReadNone, i32t, 40, &[index, null])
} else if sampler {
// A sampler heap has at most 2,048 samplers on any host: an
// index past that (a number read from memory the host has not
// got yet) stays inside the largest heap there is.
// shortcut: not the bound heap's own count, which a title
// heap smaller than 2,048 would need.
let last = self.m.i32(2047);
let i32t = self.t.i32;
self.op(block, "binary.i32", Attr::ReadNone, i32t, 40, &[index, last])
} else {
index
};
self.flags |= if sampler {
flags::SAMPLER_HEAP
} else {
@@ -318,6 +447,7 @@ impl Shader {
|| self.samplers > 0
|| !self.root_cbvs.is_empty()
|| !self.root_srvs.is_empty()
|| !self.root_uavs.is_empty()
{
let empty = self.m.md_str("");
let mut cbvs = None;
@@ -401,6 +531,43 @@ impl Shader {
}
srvs = Some(self.m.md_node(list));
}
let mut uavs = None;
if !self.root_uavs.is_empty() {
let ty = self
.m
.named_struct("struct.RWByteAddressBuffer", vec![self.t.i32]);
let p = self.m.ptr(ty);
let u = self.m.undef(p);
let no = self.m.bool(false);
let no = self.m.md_value(no);
let mut list = Vec::new();
for (n, (slot, _)) in self.root_uavs.clone().into_iter().enumerate() {
// Id, variable, name, space, lower bound, range size,
// shape (11: raw buffer), globally coherent, counter,
// rasterizer ordered, no extra properties.
let ops = vec![
Some(i32c(&mut self, n as u32)),
Some(self.m.md_value(u)),
Some(empty),
Some(i32c(&mut self, ROOT_CBV_SPACE)),
Some(i32c(&mut self, u32::from(slot))),
Some(i32c(&mut self, 1)),
Some(i32c(&mut self, 11)),
Some(no),
Some(no),
Some(no),
None,
];
list.push(Some(self.m.md_node(ops)));
bindings.push(Binding {
kind: 7,
space: ROOT_CBV_SPACE,
register: u32::from(slot),
dxil_kind: 11,
});
}
uavs = Some(self.m.md_node(list));
}
let mut samplers = None;
if self.samplers > 0 {
let ty = self.m.named_struct("struct.SamplerState", vec![self.t.i32]);
@@ -430,11 +597,12 @@ impl Shader {
}
samplers = Some(self.m.md_node(list));
}
resources = Some(self.m.md_node(vec![srvs, None, cbvs, samplers]));
// PSV0 lists them as the module does: CBVs, samplers, SRVs.
resources = Some(self.m.md_node(vec![srvs, uavs, cbvs, samplers]));
// PSV0 lists them as the module does: CBVs, samplers, SRVs, UAVs.
bindings.sort_by_key(|b| match b.kind {
2 => 0,
1 => 1,
7 => 3,
_ => 2,
});
}
@@ -468,6 +636,11 @@ impl Shader {
});
}
}
// A geometry shader lists four streams: the other three have
// no outputs.
if self.geometry.is_some() {
view.extend([0, 0, 0]);
}
let arr = self.m.array(view.len() as u32, self.t.i32);
let elems: Vec<V> = view.iter().map(|&v| self.m.i32(v)).collect();
let arr = self.m.aggregate(arr, &elems);
@@ -488,8 +661,8 @@ impl Shader {
if self.pixel && self.outputs.iter().any(|o| o.kind == 20) {
self.flags |= flags::STENCIL_REF;
}
// Entry properties: shader flags (tag 0), thread-group size (4),
// wave size (11).
// Entry properties: shader flags (tag 0), geometry state (1),
// thread-group size (4), wave size (11).
let mut props = Vec::new();
if self.flags != 0 {
props.push(i32c(&mut self, 0));
@@ -497,6 +670,13 @@ impl Shader {
let f = self.m.int_const(i64t, self.flags as i64);
props.push(self.m.md_value(f));
}
if let Some(g) = self.geometry {
// Input primitive (3: triangles), maximum vertices, stream
// mask, output topology (5: triangle strips), instances.
props.push(i32c(&mut self, 1));
let state = [3, g.max_vertices, 1, 5, 1].map(|n| Some(i32c(&mut self, n)));
props.push(self.m.md_node(state.to_vec()));
}
if let Some(threads) = self.threads {
let t = threads.map(|n| Some(i32c(&mut self, n)));
let t = self.m.md_node(t.to_vec());
@@ -543,9 +723,10 @@ impl Shader {
let version = self.m.md_node(vec![Some(one), Some(six)]);
self.m.named_md("dx.version", vec![version]);
self.m.named_md("dx.valver", vec![version]);
// Program kinds: 0 pixel, 1 vertex, 5 compute, 13 mesh.
// Program kinds: 0 pixel, 1 vertex, 2 geometry, 5 compute, 13 mesh.
let (name, kind) = match (self.threads, self.pixel) {
_ if self.mesh.is_some() => ("ms", 13u32),
_ if self.geometry.is_some() => ("gs", 2),
(Some(_), _) => ("cs", 5u32),
(None, true) => ("ps", 0),
(None, false) => ("vs", 1),
@@ -578,12 +759,21 @@ impl Shader {
if self.flags & flags::INT64 != 0 {
features |= 0x8000;
}
if self.flags & flags::BARYCENTRICS != 0 {
features |= 0x2_0000;
}
if self.flags & flags::RESOURCE_HEAP != 0 {
features |= 0x200_0000;
}
if self.flags & flags::SAMPLER_HEAP != 0 {
features |= 0x400_0000;
}
if self.flags & flags::ATOMIC_TYPED64 != 0 {
features |= 0x40_0000;
}
if self.flags & flags::ATOMIC_HEAP64 != 0 {
features |= 0x1000_0000;
}
let sfi0 = features.to_le_bytes();
let isg1 = container::signature(&self.inputs, true);
let osg1 = container::signature(&self.outputs, false);
@@ -599,6 +789,7 @@ impl Shader {
outputs: &self.outputs,
prims: &self.prims,
mesh: self.mesh.map(|m| (m, self.lds_dwords * 4)),
geometry: self.geometry.map(|g| g.max_vertices),
bindings: &bindings,
threads: self.threads.unwrap_or_default(),
wave_size: self.wave_size,