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 ffe310eb20
commit 578d459c34
61 files changed
+32259 -1274

No files matched your search

+433 -36
View File
@@ -99,6 +99,13 @@ impl Heap {
.as_ref()
}
/// The handle of descriptor `index` of this heap.
#[cfg(windows)]
pub(crate) fn handle(&self, index: u32) -> Option<u64> {
let step = u64::from(increment(self.desc.kind)?);
(index < self.desc.count).then(|| self.start + u64::from(index) * step)
}
/// The descriptor index of `handle` in this heap, if it lies in it.
pub(crate) fn index(&self, handle: u64) -> Option<u32> {
let step = u64::from(increment(self.desc.kind)?);
@@ -231,7 +238,8 @@ impl RtvRecord {
0,
]
};
words[0] = crate::resource::pc_format(words[0]);
// A target or depth view: of a target, in its stand-in format.
words[0] = crate::resource::target_format(crate::resource::pc_format(words[0]));
// SAFETY: both host view descriptions are 24 bytes in this layout.
Some(unsafe { (&raw const words).cast::<T>().read_unaligned() })
}
@@ -318,9 +326,21 @@ impl View {
/// generation, so descriptors (and host slots made from them) naming the
/// old one go stale instead of naming whatever is made at the same address
/// next.
/// shortcut: views without a resource object (constant buffers, memory
/// views, samplers) are kept for the run; they are bounded by the title's
/// memory and sampler states.
///
/// Views without a resource object have nothing that releases them, and a
/// title copies descriptors as bytes, so nothing says when the last
/// descriptor naming an entry is gone: such entries were kept for the run.
/// Grand Theft Auto VI writes some 14,000 constant buffer views a frame,
/// each at the address its constants have that frame: 2,450,139 entries
/// after four minutes of play, 1,047 MB with the index, and 11,000 more
/// every second (run 65). So the two kinds that carry an address are not
/// entries any more: their records name them whole ([`constant_words`],
/// [`placed_words`]), and the table holds only what is bounded: views of
/// resource objects (freed with the resource), samplers (as many as there
/// are sampler states), and the shapes of placed views (their descriptions
/// without the address: as many as the title has kinds of textures and
/// buffers, however often it moves them).
/// `VICTORY_VIEW_INLINE=0`: every view is an entry, as before.
struct Views {
/// Each entry's view and generation.
list: Vec<(View, u32)>,
@@ -328,6 +348,9 @@ struct Views {
/// Entries naming each resource.
by_resource: std::collections::BTreeMap<usize, Vec<u32>>,
free: Vec<u32>,
/// The shapes of views of placed memory, and where each is.
shapes: Vec<Shape>,
shape_index: std::collections::BTreeMap<Shape, u32>,
}
static VIEWS: std::sync::Mutex<Views> = std::sync::Mutex::new(Views {
@@ -335,9 +358,220 @@ static VIEWS: std::sync::Mutex<Views> = std::sync::Mutex::new(Views {
index: std::collections::BTreeMap::new(),
by_resource: std::collections::BTreeMap::new(),
free: Vec::new(),
shapes: Vec::new(),
shape_index: std::collections::BTreeMap::new(),
});
const VIEW_TAG: u32 = u32::from_le_bytes(*b"vVIE");
/// A record is the tag and two words, `i` and `generation` (the host keeps
/// them as one 64-bit key per slot). The top two bits of `generation` say
/// what the other 62 bits are: 0, entry `i` of the table at that
/// generation; [`CONSTANT`], a constant buffer view, whole; [`PLACED`], a
/// view of placed memory: its address and the number of its shape.
const KIND_SHIFT: u32 = 30;
/// Table generations stay below the kind bits (and above 0).
const GENERATIONS: u32 = (1 << KIND_SHIFT) - 1;
const CONSTANT: u32 = 1;
const PLACED: u32 = 2;
/// Bits of a constant buffer view's address in its record: any address a
/// program can have. The 15 bits above them are its size.
const CONSTANT_ADDRESS_BITS: u32 = 47;
/// Bits of a placed view's address in its record: placed memory is title
/// memory, which lies below 1 TiB. The 22 bits above them number its shape.
const PLACED_ADDRESS_BITS: u32 = 40;
const SHAPES: usize = 1 << (62 - PLACED_ADDRESS_BITS);
/// A view of placed memory without its address: whether it is unordered
/// access, the resource description, and the view's words but the two that
/// hold the address (10 and 11).
type Shape = (bool, [u32; 14], [u32; 14]);
/// Whether constant buffer views and views of placed memory are named whole
/// in their records ([`Views`]). On by default; `VICTORY_VIEW_INLINE=0`
/// makes every view a table entry, as before.
fn inline() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_VIEW_INLINE").is_none_or(|v| v != "0"))
}
/// Views named whole in their records since the start: constant buffer
/// views, views of placed memory (for the memory line).
static NAMED_WHOLE: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2];
/// The two record words of the 62 bits `payload` of kind `kind`.
fn kind_words(kind: u32, payload: u64) -> [u32; 2] {
[payload as u32, kind << KIND_SHIFT | (payload >> 32) as u32]
}
/// The 62 bits two record words carry beside their kind.
fn payload(i: u32, generation: u32) -> u64 {
u64::from(generation & GENERATIONS) << 32 | u64::from(i)
}
/// The record words that name the constant buffer view of `size` bytes at
/// `address` whole; `None` if they cannot: an address past 47 bits, or a
/// size of 16 KiB or more that is no multiple of 256 or reaches 4 MiB (the
/// size has 15 bits: bytes below 16 KiB, else units of 256 bytes). Such a
/// view is a table entry.
fn constant_words(address: u64, size: u32) -> Option<[u32; 2]> {
if address >> CONSTANT_ADDRESS_BITS != 0 {
return None;
}
let code = if size < 1 << 14 {
u64::from(size)
} else if size.is_multiple_of(256) && size >> 8 < 1 << 14 {
1 << 14 | u64::from(size >> 8)
} else {
return None;
};
Some(kind_words(CONSTANT, code << CONSTANT_ADDRESS_BITS | address))
}
/// The constant buffer view [`constant_words`] named.
fn constant_of(i: u32, generation: u32) -> View {
let payload = payload(i, generation);
let code = (payload >> CONSTANT_ADDRESS_BITS) as u32;
let size = if code >> 14 == 0 { code } else { (code & 0x3fff) << 8 };
View::Constant(payload & ((1 << CONSTANT_ADDRESS_BITS) - 1), size)
}
/// A placed view's shape and its address.
fn shape_of(unordered: bool, resource: &[u32; 14], desc: &[u32; 16]) -> (Shape, u64) {
let mut rest = [0u32; 14];
rest[..10].copy_from_slice(&desc[..10]);
rest[10..].copy_from_slice(&desc[12..]);
(
(unordered, *resource, rest),
u64::from(desc[10]) | u64::from(desc[11]) << 32,
)
}
/// The record words that name the view of placed memory at `address` with
/// shape number `shape`.
fn placed_words(shape: u32, address: u64) -> [u32; 2] {
kind_words(PLACED, u64::from(shape) << PLACED_ADDRESS_BITS | address)
}
impl Views {
/// The number of `shape`, a new one if it is not there yet; `None` when
/// the shapes are full (the view is then a table entry).
fn shape(&mut self, shape: Shape) -> Option<u32> {
if let Some(&at) = self.shape_index.get(&shape) {
return Some(at);
}
if self.shapes.len() >= SHAPES {
return None;
}
let at = self.shapes.len() as u32;
self.shapes.push(shape);
self.shape_index.insert(shape, at);
Some(at)
}
/// The view of placed memory [`placed_words`] named.
fn placed_of(&self, i: u32, generation: u32) -> Option<View> {
let payload = payload(i, generation);
let address = payload & ((1 << PLACED_ADDRESS_BITS) - 1);
let &(unordered, resource, rest) = self.shapes.get((payload >> PLACED_ADDRESS_BITS) as usize)?;
let mut desc = [0u32; 16];
desc[..10].copy_from_slice(&rest[..10]);
(desc[10], desc[11]) = (address as u32, (address >> 32) as u32);
desc[12..].copy_from_slice(&rest[10..]);
Some(View::Placed(unordered, resource, desc))
}
/// The record words of `view` as a table entry: its entry (a new or a
/// freed one if the view is not there yet) and that entry's generation.
fn entry(&mut self, view: View) -> [u32; 2] {
let i = match self.index.get(&view) {
Some(&i) => i,
None => {
let i = match self.free.pop() {
Some(i) => {
self.list[i as usize].0 = view.clone();
i
}
None => {
self.list.push((view.clone(), 1));
self.list.len() as u32 - 1
}
};
for r in view.resources().into_iter().filter(|&r| r != 0) {
self.by_resource.entry(r).or_default().push(i);
}
self.index.insert(view, i);
i
}
};
[i, self.list[i as usize].1]
}
}
/// The two words that name `view` in a record: the view itself where its
/// kind allows ([`Views`]), else its table entry.
fn words_for(view: View) -> [u32; 2] {
use std::sync::atomic::Ordering::Relaxed;
if inline() {
match &view {
View::Constant(address, size) => {
if let Some(words) = constant_words(*address, *size) {
NAMED_WHOLE[0].fetch_add(1, Relaxed);
return words;
}
}
View::Placed(unordered, resource, desc) => {
let (shape, address) = shape_of(*unordered, resource, desc);
if address >> PLACED_ADDRESS_BITS == 0 {
let mut views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
if let Some(shape) = views.shape(shape) {
NAMED_WHOLE[1].fetch_add(1, Relaxed);
return placed_words(shape, address);
}
}
}
_ => {}
}
}
VIEWS.lock().unwrap_or_else(|e| e.into_inner()).entry(view)
}
/// For the memory line: the distinct views kept ([`Views`]), those of them
/// free for reuse, and the bytes the table and its index hold for them (an
/// estimate: an entry of the table, and one of the index with its share of
/// a tree node; the shapes likewise); then the entries that are constant
/// buffer views and views of placed memory (those that did not fit a
/// record, or all of them with `VICTORY_VIEW_INLINE=0`), the shapes of
/// placed views kept, and the views named whole in their records since the
/// start: constant buffer views, views of placed memory.
#[cfg(all(windows, feature = "diagnostics"))]
pub(crate) fn views_kept() -> [u64; 8] {
use std::sync::atomic::Ordering::Relaxed;
let views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let each = size_of::<(View, u32)>() as u64;
let shape = size_of::<Shape>() as u64;
let (mut constants, mut placed) = (0, 0);
for (view, _) in &views.list {
match view {
View::Constant(..) => constants += 1,
View::Placed(..) => placed += 1,
_ => {}
}
}
[
views.list.len() as u64,
views.free.len() as u64,
views.list.capacity() as u64 * each
+ views.index.len() as u64 * each * 3 / 2
+ views.shapes.capacity() as u64 * shape
+ views.shape_index.len() as u64 * shape * 3 / 2,
constants,
placed,
views.shapes.len() as u64,
NAMED_WHOLE[0].load(Relaxed),
NAMED_WHOLE[1].load(Relaxed),
]
}
impl View {
/// The resource objects the view names.
fn resources(&self) -> [usize; 2] {
@@ -347,43 +581,122 @@ impl View {
_ => [0, 0],
}
}
/// The texture object the view is of, if that texture has no host copy
/// yet (`crate::resource::host_waits`): a slot written with such a
/// view is left null until the texture is first used.
#[cfg(windows)]
fn waiting_texture(&self) -> Option<usize> {
match *self {
View::Shader(r, _) | View::Unordered(r, _, _)
if crate::resource::host_waits(r as This) =>
{
Some(r)
}
_ => None,
}
}
}
/// Title slots (their handles) written with a view of a texture object
/// that had no host copy then, by texture. A title texture gets its host
/// copy on first use (`resource.rs` `Hosted`), and Grand Theft Auto VI
/// writes each texture's views into its view heap when it creates the
/// texture: making those host slots would make every host copy at once.
/// They are made when the texture is: translated shaders index the heap
/// directly, so a slot is not always made again by the draw that reads it.
#[cfg(windows)]
static WAITING: std::sync::Mutex<std::collections::BTreeMap<usize, Vec<u64>>> =
std::sync::Mutex::new(std::collections::BTreeMap::new());
/// Textures given their host copy since [`make_waiting`] last ran.
#[cfg(windows)]
static MADE_LATE: std::sync::Mutex<Vec<usize>> = std::sync::Mutex::new(Vec::new());
/// Title texture `resource` was given its host copy (called under that
/// texture's own lock: nothing else is locked here).
#[cfg(windows)]
pub(crate) fn texture_made(resource: usize) {
MADE_LATE
.lock()
.unwrap_or_else(|e| e.into_inner())
.push(resource);
}
/// The slot at `handle` was written with a view of texture `resource`,
/// which has no host copy: it waits for one.
#[cfg(windows)]
fn wait_for(resource: usize, handle: u64) {
/// A texture with more slots waiting than this is in use for all we
/// know (its views are being copied about): its host copy is made.
const MOST: usize = 64;
let many = {
let mut waiting = WAITING.lock().unwrap_or_else(|e| e.into_inner());
let slots = waiting.entry(resource).or_default();
if !slots.contains(&handle) {
slots.push(handle);
}
slots.len() > MOST
};
if many {
// Makes it, and notes it made.
let _ = crate::resource::host_copy(resource as This);
} else if !crate::resource::host_waits(resource as This) {
// Made by another thread since the slot was looked at: that making
// may have been taken by `make_waiting` before this slot was noted.
texture_made(resource);
}
}
/// Makes the host slots that waited for a texture's host copy, for the
/// textures made since the last call. Called before a submission runs, by
/// the thread that submits: a slot is read when a list runs.
#[cfg(windows)]
pub(crate) fn make_waiting() {
let made = std::mem::take(&mut *MADE_LATE.lock().unwrap_or_else(|e| e.into_inner()));
for resource in made {
let handles = WAITING
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&resource);
for handle in handles.into_iter().flatten() {
// Only if the slot still holds a view of that texture: the
// title may have written it since (and a view of another
// texture without a host copy must go on waiting).
// (Looked up first: the handle is read only while its heap is
// there.)
let Some((heap, index)) = shader_slot(handle) else {
continue;
};
let same = match view(handle) {
Some(View::Shader(r, _) | View::Unordered(r, _, _)) => r == resource,
_ => false,
};
if same && let Some(Some(host)) = heap.host.get() {
host.mirror(index, handle, true);
}
}
}
}
fn record(handle: u64, view: View) {
if handle == 0 {
return;
}
let mut views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let i = match views.index.get(&view) {
Some(&i) => i,
None => {
let i = match views.free.pop() {
Some(i) => {
views.list[i as usize].0 = view.clone();
i
}
None => {
views.list.push((view.clone(), 1));
views.list.len() as u32 - 1
}
};
for r in view.resources().into_iter().filter(|&r| r != 0) {
views.by_resource.entry(r).or_default().push(i);
}
views.index.insert(view, i);
i
}
};
let generation = views.list[i as usize].1;
let [i, generation] = words_for(view);
// SAFETY: the handle is a descriptor slot, at least 16 bytes.
unsafe { (handle as *mut [u32; 3]).write_unaligned([VIEW_TAG, i, generation]) };
drop(views);
#[cfg(windows)]
mirror(handle);
}
/// The title released `resource`: the views naming it go stale.
pub(crate) fn forget(resource: usize) {
#[cfg(windows)]
WAITING
.lock()
.unwrap_or_else(|e| e.into_inner())
.remove(&resource);
let mut views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let Some(entries) = views.by_resource.remove(&resource) else {
return;
@@ -396,19 +709,27 @@ pub(crate) fn forget(resource: usize) {
continue;
}
views.index.remove(&view);
views.list[i as usize].1 = generation.wrapping_add(1).max(1);
// (Below the bits that say a record is no table entry.)
views.list[i as usize].1 = ((generation + 1) & GENERATIONS).max(1);
// The view's other resource (a counter) keeps a stale entry, which
// `forget` of that resource skips.
views.free.push(i);
}
}
/// The view of entry `i` if `generation` is still its own.
#[cfg(windows)]
/// The view the record words `i` and `generation` name: the one they hold
/// whole, or the view of entry `i` if `generation` is still its own.
fn view_at(i: u32, generation: u32) -> Option<View> {
let views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let (view, current) = views.list.get(i as usize)?;
(*current == generation).then(|| view.clone())
match generation >> KIND_SHIFT {
0 => {
let views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let (view, current) = views.list.get(i as usize)?;
(*current == generation).then(|| view.clone())
}
CONSTANT => Some(constant_of(i, generation)),
PLACED => VIEWS.lock().unwrap_or_else(|e| e.into_inner()).placed_of(i, generation),
_ => None,
}
}
/// The view recorded at `handle`, if one is (and its resource is alive).
@@ -418,9 +739,10 @@ pub(crate) fn view(handle: u64) -> Option<View> {
}
// SAFETY: the handle is a descriptor slot the title passes back.
let [tag, i, generation] = unsafe { (handle as *const [u32; 3]).read_unaligned() };
let views = VIEWS.lock().unwrap_or_else(|e| e.into_inner());
let (view, current) = views.list.get(i as usize)?;
(tag == VIEW_TAG && *current == generation).then(|| view.clone())
if tag != VIEW_TAG {
return None;
}
view_at(i, generation)
}
/// A description of `N` words, if the title passes one.
@@ -741,6 +1063,81 @@ mod tests {
heap[0] = 0;
}
/// Constant buffer views are named whole in their records: any address
/// a program can have, any size below 16 KiB, multiples of 256 bytes
/// up to 4 MiB; what does not fit says so.
#[test]
fn constant_views_fit_their_records() {
let top = (1u64 << CONSTANT_ADDRESS_BITS) - 1;
for address in [0u64, 0x100, 0x2602_0000, 0xd_6b60_0000, 0x7ffd_a34f_0000, top] {
for size in [0u32, 1, 255, 256, 4000, (1 << 14) - 1, 1 << 14, 65536, (1 << 22) - 256] {
let [i, generation] = constant_words(address, size).expect("fits");
assert_eq!(generation >> KIND_SHIFT, CONSTANT);
assert_eq!(view_at(i, generation), Some(View::Constant(address, size)), "{address:#x} {size}");
}
}
assert_eq!(constant_words(top + 1, 256), None, "an address past 47 bits");
assert_eq!(constant_words(0x1000, (1 << 14) + 1), None, "16 KiB or more, no multiple of 256");
assert_eq!(constant_words(0x1000, 1 << 22), None, "4 MiB");
// Different views never share their words.
assert_ne!(constant_words(0x1000, 256), constant_words(0x1000, 512));
assert_ne!(constant_words(0x1000, 256), constant_words(0x1100, 256));
assert_ne!(constant_words(0x1000, 1 << 14), constant_words(0x1000, 64));
}
/// The leak of run 65: a title that writes constant buffer views and
/// views of placed memory at ever new addresses adds nothing to the
/// table; one shape stands for all the placed views alike.
#[test]
fn views_at_new_addresses_add_no_entries() {
if !inline() {
return;
}
let null = std::ptr::null();
let slots = [0u64; 4];
let at = slots.as_ptr() as u64;
// A resource description and a view no other test uses.
let resource: [u32; 14] = std::array::from_fn(|k| 0x5ee0_0000 + k as u32);
let mut desc: [u32; 16] = std::array::from_fn(|k| 0x7a70_0000 + k as u32);
let mine = |v: &View| match v {
View::Constant(address, _) => (0x6_0000_0000..0x6_1000_0000).contains(address),
View::Placed(_, r, _) => *r == resource,
_ => false,
};
let entries = || VIEWS.lock().unwrap().list.iter().filter(|(v, _)| mine(v)).count();
let shapes = || VIEWS.lock().unwrap().shapes.iter().filter(|s| s.1 == resource).count();
for n in 0..20_000u64 {
let address = 0x6_0000_0000 + n * 0x100;
create_cbv(null, &[address as u32, (address >> 32) as u32, 512, 0], at);
assert_eq!(view(at), Some(View::Constant(address, 512)));
let placed = 0xd_4940_0000 + n * 0x1_0000;
(desc[10], desc[11]) = (placed as u32, (placed >> 32) as u32);
create_placed_srv(null, &resource, &desc, at);
assert_eq!(view(at), Some(View::Placed(false, resource, desc)));
}
assert_eq!(entries(), 0, "no table entries");
assert_eq!(shapes(), 1, "one shape for 20,000 placed views");
// The same shape, unordered: another shape.
create_placed_uav(null, &resource, &desc, at);
assert_eq!(view(at), Some(View::Placed(true, resource, desc)));
assert_eq!(shapes(), 2);
// Placed memory past 1 TiB (not title memory) is a table entry.
(desc[10], desc[11]) = (0, 0x100);
create_placed_srv(null, &resource, &desc, at);
assert_eq!(view(at), Some(View::Placed(false, resource, desc)));
assert_eq!(entries(), 1);
// A record's words are the same for the same view (the host knows
// a slot's view by them), and differ with the address.
// SAFETY: both handles are slots of 32 bytes above.
let words = |handle: u64| unsafe { *(handle as *const [u32; 3]) };
let other = [0u64; 4];
create_cbv(null, &[0x40, 6, 512, 0], at);
create_cbv(null, &[0x40, 6, 512, 0], other.as_ptr() as u64);
assert_eq!(words(at), words(other.as_ptr() as u64));
create_cbv(null, &[0x140, 6, 512, 0], other.as_ptr() as u64);
assert_ne!(words(at), words(other.as_ptr() as u64));
}
/// A released resource's views go stale; the same view of a new
/// resource at the same address is a new record.
#[test]