Water light on, PS4 pads, F1 menu saves at once, long-session memory, memory switches

Rendering and memory (GPL-3.0 code ported in; credits in the source):
- Water: 64-lane compute programs on 32-lane waves keep each wave
  together through loops and set up shared memory from the first
  thread, so the water's tiled light no longer leaves dark blocks. It
  runs by default now (VICTORY_WATER_LIGHT=0 leaves it out; an empty
  value, an earlier version's saved OFF, runs it). Also cloud tiles and
  the screen probes' ray activation.
- Placed views: their padding cleared as before; folding a buffer
  view's first element into its address is VICTORY_PLACED_FOLD=1, off:
  with it on, clothing and caps swapped to other variants for single
  frames in the boat cutscene.
- Texture sweep: textures whose copy was let go leave the looks until
  used again. Static samplers made first, in one order. Small buffers
  made ahead of the first 3D frame.
- play mode: VICTORY_TEXSPK_PASS_MS=8, VICTORY_PASSES_HOT=1,
  VICTORY_HELPER_PRIORITY=1.
- Memory switches, all off: VICTORY_SLOT_EVICT, VICTORY_ALLOCATOR_TRIM,
  VICTORY_BUDGET_PACK (and VICTORY_LIST_POOL_IDLE_S).

Input: DualShock 4 (0x05C4, 0x09CC, 0x0BA0 adapter) read over HID, USB
and Bluetooth, beside the DualSense; PlayStation pads rumble, and stop
when the game ends (issue #352).

F1 menu: every change is saved at once; it saves to
victory-settings.txt in VICTORY_HOME or %LOCALAPPDATA%\victory however
the game was started (no more "NO SETTINGS FILE").

Warm-up start: its 30 s count from the first frame of 100 draws or
more, not from the start: waiting at the title screen restarted it too
early and the next run still sat at 14 fps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
trustno1-coderandClaude Opus 5.5 committed 2026-11-27 15:03:27 -08:00
1 parent df2daafeb1
commit 64d4931b3d
44 files changed
+4664 -952

No files matched your search

+264 -58
View File
@@ -1210,6 +1210,39 @@ fn shape_of(unordered: bool, resource: &[u32; 14], desc: &[u32; 16]) -> (Shape,
)
}
/// Diagnostics: which words of placed views' shapes vary. Each new shape's
/// 29 words (unordered, the resource's 14, the view's 14 without its
/// address) go into a set per word, of at most 4,096 values; at 1,000 shapes
/// and then every 50,000 the distinct values per word are logged, with the
/// last shape's words. (Shapes were meant to be as many as the title has
/// kinds of textures and buffers; run leak1 filled all 4,194,304.)
#[cfg(all(windows, feature = "diagnostics"))]
fn shape_census(shape: &Shape, count: usize) {
use std::collections::HashSet;
static SEEN: std::sync::Mutex<Vec<HashSet<u32>>> = std::sync::Mutex::new(Vec::new());
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
if seen.is_empty() {
seen.resize_with(29, HashSet::new);
}
let (unordered, resource, rest) = shape;
let words: Vec<u32> = std::iter::once(u32::from(*unordered)).chain(resource.iter().copied()).chain(rest.iter().copied()).collect();
for (set, &w) in seen.iter_mut().zip(&words) {
if set.len() < 4096 {
set.insert(w);
}
}
if count == 1000 || (count > 0 && count % 50_000 == 0) {
let distinct: Vec<String> = seen.iter().map(|s| s.len().to_string()).collect();
let last: Vec<String> = words.iter().map(|w| format!("{w:x}")).collect();
tracing::info!(
shapes = count,
"placed view shapes, distinct values per word (0 unordered, 1-14 the resource, 15-24 the view's words 0-9, 25-28 its words 12-15): {}; the last: {}",
distinct.join(" "),
last.join(" ")
);
}
}
/// 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] {
@@ -1226,6 +1259,8 @@ impl Views {
if self.shapes.len() >= SHAPES {
return None;
}
#[cfg(all(windows, feature = "diagnostics"))]
shape_census(&shape, self.shapes.len());
let at = self.shapes.len() as u32;
self.shapes.push(shape);
self.shape_index.insert(shape, at);
@@ -1522,6 +1557,88 @@ pub(crate) fn make_waiting() {
}
}
// Slot eviction (`VICTORY_SLOT_EVICT=1`, `crate::resource::slot_evict`): memory stack by
// skornuta (skornuta/victory-memory-stack 57fb136, GPL-3.0), patch 06 and its base.
/// For `crate::resource::slot_evict`: of the host textures `raws` (raw
/// pointer to title texture), those shown only by title slots a draw makes
/// again by itself (`HostHeap::slots_showing`), with those slots as (heap,
/// index). A texture any other slot shows is left out (the third result
/// counts them). So is one any of whose slots a draw's table showed after
/// frame `cutoff` (the second result counts them): a texture drawn through
/// a table every frame never asks for its host copy again, so its last use
/// is old although it is on screen.
#[cfg(windows)]
#[allow(clippy::type_complexity)]
pub(crate) fn evictable_slots(
raws: &std::collections::HashMap<usize, usize>,
frame: u64,
cutoff: u64,
) -> (std::collections::HashMap<usize, Vec<(usize, u32)>>, u64, u64) {
let set: std::collections::HashSet<usize> = raws.keys().copied().collect();
let mut by_texture: std::collections::HashMap<usize, Vec<(usize, u32)>> = std::collections::HashMap::new();
let mut blocked = std::collections::HashSet::new();
let mut seen = std::collections::HashSet::new();
{
// (The heaps stay while the lock is held: `mirror`.)
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
for &(_, _, at) in heaps.iter() {
// SAFETY: a registered heap, live while the lock is held.
let Some(Some(host)) = unsafe { &*(at as *const Heap) }.host.get() else {
continue;
};
for (index, raw, ok, lately) in host.slots_showing(&set, frame, cutoff) {
if lately {
seen.insert(raw);
}
if ok {
by_texture.entry(raw).or_default().push((at, index));
} else {
blocked.insert(raw);
}
}
}
}
let kept = seen.iter().filter(|raw| !blocked.contains(raw)).count() as u64;
by_texture.retain(|raw, _| !blocked.contains(raw) && !seen.contains(raw));
(
by_texture.into_iter().filter_map(|(raw, slots)| Some((*raws.get(&raw)?, slots))).collect(),
kept,
blocked.len() as u64,
)
}
/// Puts the slots of title texture `resource` back to waiting for its host
/// copy ([`evictable_slots`]): null now, made again when the texture is
/// ([`make_waiting`]) or after the draw that reads them through a table.
/// Returns how many were made. Called before the host copy is let go.
#[cfg(windows)]
pub(crate) fn evict_slots(resource: usize, slots: &[(usize, u32)]) -> u64 {
let mut n = 0;
for &(at, index) in slots {
// (The heap stays while the lock is held: `mirror`.)
let heaps = SHADER_HEAPS.read().unwrap_or_else(|e| e.into_inner());
if !heaps.iter().any(|&(_, _, h)| h == at) {
continue;
}
// SAFETY: a registered heap, live while the lock is held.
let heap = unsafe { &*(at as *const Heap) };
let (Some(handle), Some(Some(host))) = (heap.handle(index), heap.host.get()) else {
continue;
};
if host.texture_slot_evicted(index) {
drop(heaps);
let mut waiting = WAITING.lock().unwrap_or_else(|e| e.into_inner());
let handles = waiting.entry(resource).or_default();
if !handles.contains(&handle) {
handles.push(handle);
}
n += 1;
}
}
n
}
fn record(handle: u64, view: View) {
if handle == 0 {
return;
@@ -1634,56 +1751,6 @@ pub(crate) extern "system" fn create_uav(
record(handle, view);
}
/// Copy only the fields of a placed resource description. Padding can differ
/// between two views of the same texture and the caller may leave it unwritten.
fn placed_resource_words(desc: *const [u32; 14]) -> Option<[u32; 14]> {
if desc.is_null() {
return None;
}
let src = desc.cast::<u32>();
let mut fields = [0; 14];
// SAFETY: the caller supplies a 56-byte ResourceDesc, possibly unaligned.
// Dimension occupies word 0. Alignment through Flags occupy words 2..13.
unsafe {
fields[0] = src.read_unaligned();
fields[2..13].copy_from_slice(&src.add(2).cast::<[u32; 11]>().read_unaligned());
}
Some(fields)
}
/// The words of a placed view's description with those its kind does not
/// use set to zero. The first ten words are the PC's view description: for a
/// shader resource view, the format, the dimension, the component mapping,
/// a word of padding (the union that follows starts on 8 bytes) and the
/// union's six words; for an unordered access view, the format, the
/// dimension and the union's eight words. Each dimension uses only the head
/// of the union, and the title leaves the rest unwritten: run vt-shapes saw
/// 131,392 values in word 3 and 36,413 in word 9 of a million shapes, each
/// a new shape for the same view. Words 10 and on (the address and the
/// console's own fields) are kept; an unknown dimension keeps all.
fn placed_view_words(desc: *const [u32; 16], unordered: bool) -> Option<[u32; 16]> {
let mut d = words(desc)?;
// Words of the union each dimension uses (D3D12_BUFFER_SRV and the like).
let (start, used) = if unordered {
// D3D12_UAV_DIMENSION: buffer, 1D, 1D array, 2D, 2D array, 2D MS,
// 2D MS array, 3D.
const USED: [usize; 9] = [0, 7, 1, 3, 2, 4, 0, 2, 3];
(2, USED.get(d[1] as usize).copied())
} else {
// D3D12_SRV_DIMENSION: buffer, 1D, 1D array, 2D, 2D array, 2D MS,
// 2D MS array, 3D, cube, cube array, acceleration structure.
const USED: [usize; 12] = [0, 5, 3, 5, 4, 6, 0, 2, 3, 3, 5, 2];
(4, USED.get(d[1] as usize).copied())
};
if let Some(used) = used.filter(|_| d[1] != 0) {
if !unordered {
d[3] = 0;
}
d[start + used..10].fill(0);
}
Some(d)
}
/// Device methods 0x230 and 0x238: shader resource and unordered access
/// views of memory the title placed itself, `(resource desc, view desc,
/// handle)`.
@@ -1693,7 +1760,8 @@ pub(crate) extern "system" fn create_placed_srv(
desc: *const [u32; 16],
handle: u64,
) {
if let (Some(resource), Some(desc)) = (placed_resource_words(resource), placed_view_words(desc, false)) {
if let (Some(resource), Some(desc)) = (words(resource), words(desc)) {
let (resource, desc) = normal_placed(false, resource, desc);
record(handle, View::Placed(false, resource, desc));
}
}
@@ -1704,11 +1772,103 @@ pub(crate) extern "system" fn create_placed_uav(
desc: *const [u32; 16],
handle: u64,
) {
if let (Some(resource), Some(desc)) = (placed_resource_words(resource), placed_view_words(desc, true)) {
if let (Some(resource), Some(desc)) = (words(resource), words(desc)) {
let (resource, desc) = normal_placed(true, resource, desc);
record(handle, View::Placed(true, resource, desc));
}
}
/// A placed view as the title gives it carries bytes no view reads: the
/// padding of the resource description (words 1 and 13), of a shader
/// resource view's description (word 3), and the union's words past the
/// member of the view's dimension. The title leaves them as its stack held
/// them, so nearly every view of placed memory was a shape of its own: run
/// leak1 filled the 4,194,304 shapes the records can name in 20 minutes on
/// the water, then made 5.6 million table entries that nothing lets go
/// (3.3 GB), and the frame time doubled; the reported slow-down, white
/// picture and stuttering sound after long play. Those bytes are cleared.
/// With `VICTORY_PLACED_FOLD=1`, a buffer view's first element is folded
/// into its address and its buffer's width made the view's own length, so
/// views of one ring of buffers at many offsets are one shape (off: it
/// swapped clothing for single frames).
/// `VICTORY_PLACED_NORMAL=0`: kept as the title gave them, as before.
/// `VICTORY_PLACED_FOLD=1`: [`normal_placed`] also folds a buffer view's
/// first element into its address.
fn placed_fold() -> bool {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_PLACED_FOLD").is_some_and(|v| v == "1"))
}
fn normal_placed(unordered: bool, mut resource: [u32; 14], mut desc: [u32; 16]) -> ([u32; 14], [u32; 16]) {
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_PLACED_NORMAL").is_none_or(|v| v != "0")) {
return (resource, desc);
}
resource[1] = 0;
resource[13] = 0;
// The union's first word, and the words of the dimension's member
// (unknown dimensions keep all of them).
let (start, used) = if unordered {
(2, match desc[1] {
1 => 7,
2 => 1,
3 => 3,
4 => 2,
5 => 4,
6 => 1,
7 => 2,
8 => 3,
_ => 8,
})
} else {
desc[3] = 0;
(4, match desc[1] {
1 => 5,
2 => 3,
3 => 5,
4 => 4,
5 => 6,
6 => 1,
7 => 2,
8 => 3,
9 => 3,
10 => 5,
11 => 2,
_ => 6,
})
};
for w in &mut desc[(start + used).min(10)..10] {
*w = 0;
}
// A buffer view of a buffer: its first element into its address
// (`VICTORY_PLACED_FOLD=1`; off by default). With it on, characters'
// clothing and caps changed to other variants of theirs for single frames
// in cutscenes (the boat ride, RTX 5070, 2026-11-27): views the title
// keeps apart were taken for one. `VICTORY_PLACED_NORMAL=0` (no
// clearing either) had it gone too; the clearing above alone was our
// own before this merge, and keeps the shapes from leaking.
if resource[0] == 1 && desc[1] == 1 && placed_fold() {
let (count, stride, flags) = if unordered { (desc[4], desc[5], desc[8]) } else { (desc[6], desc[7], desc[8]) };
// (Raw: a flag of 1 in both kinds of view.)
let element = if stride > 0 {
u64::from(stride)
} else if flags & 1 != 0 {
4
} else {
crate::resource::element(desc[0]).map_or(0, |(bytes, _)| bytes)
};
if element > 0 {
let first = u64::from(desc[start]) | u64::from(desc[start + 1]) << 32;
let address = (u64::from(desc[10]) | u64::from(desc[11]) << 32).wrapping_add(first.wrapping_mul(element));
let len = u64::from(count) * element;
(desc[start], desc[start + 1]) = (0, 0);
(desc[10], desc[11]) = (address as u32, (address >> 32) as u32);
(resource[4], resource[5]) = (len as u32, (len >> 32) as u32);
}
}
(resource, desc)
}
/// Device method.
pub(crate) extern "system" fn create_sampler(_: This, desc: *const [u32; 13], handle: u64) {
if let Some(desc) = words(desc) {
@@ -2050,7 +2210,9 @@ mod tests {
] {
create(null, &resource, &desc, handle);
let first = slot;
assert_eq!(view(handle), Some(View::Placed(unordered, resource, desc)));
// Kept as `normal_placed` makes it (a buffer view's first element folded into its address).
let (r, d) = normal_placed(unordered, resource, desc);
assert_eq!(view(handle), Some(View::Placed(unordered, r, d)));
let mut noisy = desc;
for w in unused {
noisy[w] = 0xdead_beef;
@@ -2202,6 +2364,45 @@ mod tests {
/// 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 buffer_views_differing_in_padding_are_one_shape_and_offsets_stay_apart() {
if !inline() {
return;
}
let null = std::ptr::null();
let slots = [0u64; 4];
let at = slots.as_ptr() as u64;
// A raw buffer view of 17 dwords (format 0x27, raw flag), its
// resource 0x1_0000 bytes wide; padding as a stack might leave it.
let resource = |junk: u32, width: u32| -> [u32; 14] {
[1, junk, 0, 0, width, 0, 1, 0x10001, 0, 1, 0, 1, 0, junk ^ 0x5a5a]
};
let view_desc = |junk: u32, first: u32, address: u64| -> [u32; 16] {
let mut d = [0u32; 16];
d[..10].copy_from_slice(&[0x27, 1, 0x1688, junk, first, 0, 17, 0, 1, junk.rotate_left(7)]);
(d[10], d[11]) = (address as u32, (address >> 32) as u32);
d[12] = 0x16;
d
};
let base = 0xd_7771_0000u64;
let kept: [u32; 14] = [1, 0, 0, 0, 0x1_0000, 0, 1, 0x10001, 0, 1, 0, 1, 0, 0];
let mut seen = Vec::new();
for n in 0..500u32 {
// Other padding every time, seven offsets into one ring.
create_placed_srv(null, &resource(n.wrapping_mul(0x9e37_79b9), 0x1_0000), &view_desc(n.wrapping_mul(0x85eb_ca6b), n % 7, base), at);
let Some(View::Placed(false, r, d)) = view(at) else { panic!("a placed view") };
// Padding cleared; the first element and the address kept as given
// (folding them is VICTORY_PLACED_FOLD=1, off: it swapped clothing).
assert_eq!(u64::from(d[10]) | u64::from(d[11]) << 32, base);
assert_eq!((d[4], d[5], d[6], r), (n % 7, 0, 17, kept));
assert_eq!((r[1], r[13], d[3], d[9]), (0, 0, 0, 0));
seen.push((r, d[..10].to_vec()));
}
seen.sort();
seen.dedup();
assert_eq!(seen.len(), 7, "one shape for each offset, whatever the padding");
}
#[test]
fn views_at_new_addresses_add_no_entries() {
if !inline() {
@@ -2214,13 +2415,15 @@ mod tests {
// 13 are the description's padding, which placed views keep as zero.
let resource: [u32; 14] = std::array::from_fn(|k| if k == 1 || k == 13 { 0 } else { 0x5ee0_0000 + k as u32 });
let mut desc: [u32; 16] = std::array::from_fn(|k| 0x7a70_0000 + k as u32);
// (As views are kept: their padding cleared, `normal_placed`.)
let kept = normal_placed(false, resource, desc).0;
let mine = |v: &View| match v {
View::Constant(address, _) => (0x6_0000_0000..0x6_1000_0000).contains(address),
View::Placed(_, r, _) => *r == resource,
View::Placed(_, r, _) => *r == kept,
_ => false,
};
let entries = || VIEWS.read().unwrap().list.iter().filter(|(v, _)| mine(v)).count();
let shapes = || VIEWS.read().unwrap().shapes.iter().filter(|s| s.1 == resource).count();
let shapes = || VIEWS.read().unwrap().shapes.iter().filter(|s| s.1 == kept).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);
@@ -2228,18 +2431,21 @@ mod tests {
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)));
let (r, d) = normal_placed(false, resource, desc);
assert_eq!(view(at), Some(View::Placed(false, r, d)));
}
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)));
let (r, d) = normal_placed(true, resource, desc);
assert_eq!(view(at), Some(View::Placed(true, r, d)));
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)));
let (r, d) = normal_placed(false, resource, desc);
assert_eq!(view(at), Some(View::Placed(false, r, d)));
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.