Merge fork main a26859f (the first room renders right)

Descriptor records the title copies as bytes are mirrored before each
submission. Views off their copy's elements: our rule (raw views too, and
now typed views on 16 bytes), the fork's copies. Wave sums of halves: ours
(the fork's test passes with it). Sizes of descriptors nothing uses: ours,
then the fork's for 2D images. The fork's count of cut indirect groups is
left out: up to WIDEST groups run as asked here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-25 01:00:56 -06:00
commit f1650b7e45
13 files changed
+964 -109

No files matched your search

+201
View File
@@ -59,6 +59,10 @@ pub(crate) struct Heap {
host: std::sync::OnceLock<Option<host::HostHeap>>,
#[cfg(windows)]
pending: std::sync::Mutex<Vec<u32>>,
/// Changed pages of a placed shader-visible heap. Some titles copy
/// descriptor records into it as bytes, without a descriptor API call.
#[cfg(windows)]
written: Option<crate::watch::Tracked>,
}
pub(crate) fn heap<'a>(this: This) -> &'a Heap {
@@ -155,6 +159,119 @@ fn mirror(handle: u64) {
}
}
/// Mirrors records copied directly into placed shader-visible heaps. Calls
/// to `Create*View` are already mirrored by [`mirror`]; raw CPU copies leave
/// only write-watch pages to reveal which slots changed.
///
/// Upstream VIctory 86a5ed6 does the same and counts 1,300 to 14,400 such
/// slots a frame in the world of the title this fork works on, against a
/// few hundred views made by calls; without it those host slots stayed
/// null, or went on showing what they showed before.
/// `VICTORY_HEAP_SYNC=0`: not done, as before. [`SYNCED`] counts.
#[cfg(windows)]
pub(crate) fn sync_heaps() {
use std::sync::atomic::Ordering::Relaxed;
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
if !*ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_SYNC").is_none_or(|v| v != "0")) {
return;
}
let started = std::time::Instant::now();
let (mut pages, mut looked, mut mirrored) = (0u64, 0u64, 0u64);
let mut page_bytes: Vec<u8> = Vec::new();
let heaps = SHADER_HEAPS.lock().unwrap_or_else(|e| e.into_inner());
for &(start, end, at) in heaps.iter() {
// SAFETY: registered heaps are live (removed when dropped).
let heap = unsafe { &*(at as *const Heap) };
let (Some(Some(host)), Some(written), Some(step)) =
(heap.host.get(), &heap.written, increment(heap.desc.kind))
else {
continue;
};
let step = u64::from(step);
// Capped host heaps have no counterparts for the title slots reserved
// for per-draw tables; raw writes there must not be mirrored.
let mirrored_count = heap.desc.count.min(host.count());
for (range_from, range_end) in written.take(start as usize, end as usize) {
let mut page = range_from & !(crate::watch::PAGE - 1);
while page < range_end {
let from = page.max(range_from);
let to = (page + crate::watch::PAGE).min(range_end);
// The page's records are read through the kernel, into a
// copy: a page the title unmaps meanwhile fails the read
// where 2,048 reads in place would fault.
page_bytes.resize(to - from, 0);
if crate::host::read_into(from as *const u8, &mut page_bytes) {
pages += 1;
let slots = changed_slots(from, to, start as usize, step, mirrored_count);
for index in slots {
let handle = start + index * step;
// A record that straddles the range's ends is left
// to the range that holds its first byte.
let Some(at) = (handle as usize).checked_sub(from) else {
continue;
};
let Some(record) = page_bytes.get(at..at + step as usize) else {
continue;
};
looked += 1;
// Only a record that is not the one last seen there
// (`HostHeap::mirror_copied`).
if host.mirror_copied(index as u32, handle, record) {
mirrored += 1;
}
}
}
page += crate::watch::PAGE;
}
}
}
drop(heaps);
for (count, n) in SYNCED.iter().zip([pages, looked, mirrored, started.elapsed().as_micros() as u64]) {
count.fetch_add(n, Relaxed);
}
}
/// Since the last frame line, of [`sync_heaps`]: changed pages of placed
/// shader-visible heaps looked at, slots on them, slots whose record was
/// not the one last seen (mirrored), and the microseconds it took.
#[cfg(windows)]
pub(crate) static SYNCED: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4];
/// With the frame line: what [`sync_heaps`] did per frame over `frames`.
/// Nothing while no heap page changed.
#[cfg(windows)]
pub(crate) fn sync_line(frames: f64) {
let [pages, looked, mirrored, micros] = [0, 1, 2, 3].map(|i| SYNCED[i].swap(0, std::sync::atomic::Ordering::Relaxed));
if pages == 0 {
return;
}
tracing::info!(
"descriptor records the title copied into its heap as bytes (VICTORY_HEAP_SYNC=0: not looked for): per frame \
{:.1} changed heap pages, {:.0} slots looked at, {:.1} whose record was new and was mirrored, {:.2} ms",
pages as f64 / frames,
looked as f64 / frames,
mirrored as f64 / frames,
micros as f64 / 1000.0 / frames
);
}
/// Title descriptor slots in a changed byte range that have host counterparts.
/// `range_end` is exclusive.
#[cfg(windows)]
fn changed_slots(
range_from: usize,
range_end: usize,
heap_start: usize,
step: u64,
mirrored_count: u32,
) -> std::ops::Range<u64> {
let from = (range_from - heap_start) as u64;
let to = (range_end - heap_start) as u64;
let first = from / step;
let last = to.div_ceil(step).min(u64::from(mirrored_count));
first.min(last)..last
}
/// Gives back dynamic slots of the host heap behind the title heap at
/// `heap`, if that heap still exists.
#[cfg(windows)]
@@ -984,6 +1101,10 @@ pub(crate) extern "system" fn create_heap(
host: std::sync::OnceLock::new(),
#[cfg(windows)]
pending: Default::default(),
#[cfg(windows)]
written: (placed && visible)
.then(|| crate::watch::track(start as usize, size as usize, true))
.flatten(),
});
#[cfg(windows)]
if visible {
@@ -1041,6 +1162,34 @@ mod tests {
create_heap(std::ptr::null(), &extended.0, &HEAP_IIDS[0], out)
}
#[cfg(windows)]
#[test]
fn raw_heap_sync_stops_before_dynamic_host_slots() {
let start = 0x1000usize;
let step = 32;
let capped = 5;
assert_eq!(
changed_slots(
start + 3 * step + 4,
start + 4 * step + 8,
start,
step as u64,
capped
),
3..5
);
assert_eq!(
changed_slots(
start + 6 * step,
start + 9 * step,
start,
step as u64,
capped
),
5..5
);
}
#[test]
fn heaps_hold_count_descriptors_from_their_start() {
let null = std::ptr::null();
@@ -1067,6 +1216,58 @@ mod tests {
assert_eq!(create(bad, none, &mut h), E_INVALIDARG);
}
/// A descriptor copied into a placed heap as bytes is mirrored before
/// the next submission, even though no descriptor method saw the copy.
#[cfg(windows)]
#[test]
fn records_copied_as_bytes_are_made_at_the_next_sync() {
use windows_sys::Win32::System::Memory::{
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree,
};
const MEM_WRITE_WATCH: u32 = 0x20_0000;
let memory = unsafe {
VirtualAlloc(
std::ptr::null(),
crate::watch::PAGE,
MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH,
PAGE_READWRITE,
)
};
assert!(!memory.is_null());
let desc = HeapDesc {
kind: SAMPLERS,
count: 64,
flags: SHADER_VISIBLE,
nodes: 1,
};
let placement = Placement {
address: memory as u64,
size: 64 * 16,
};
let mut h: This = std::ptr::null();
assert_eq!(create(desc, placement, &mut h), S_OK);
if let Some(host) = heap(h).host() {
let mut elsewhere = [0u64; 2];
let source = elsewhere.as_mut_ptr() as u64;
let sampler = [21, 3, 3, 3, 0, 1, 0, 0, 0, 0, 0, 0x7f7f_ffff, 0];
create_sampler(std::ptr::null(), &sampler, source);
// SAFETY: the heap slot and source both hold 16 descriptor bytes.
unsafe {
std::ptr::copy_nonoverlapping(
source as *const u8,
memory.cast::<u8>().add(5 * 16),
16,
)
};
assert!(!host.made(5));
sync_heaps();
assert!(host.made(5));
}
slot::<extern "system" fn(This) -> u32>(h, 0x10)(h);
// SAFETY: this allocation belongs to the test.
unsafe { VirtualFree(memory, 0, MEM_RELEASE) };
}
#[test]
fn views_survive_copies() {
let null = std::ptr::null();