gpu/launcher: direct in-place CBVs, CBV dedup, coalesced fills, and cutscene texture-pass optimizations
CI / check (push) Canceled after 0s

This commit is contained in:
theme613 committed 2026-11-29 01:02:30 +08:00
1 parent b78c91f127
commit ebd07c1dbb
5 files changed
+245 -34

No files matched your search

+157 -6
View File
@@ -2715,6 +2715,9 @@ struct Deferred {
/// Scratch memory [`HostList::fill_scratch`] fills since the list last
/// wrote title memory (source, bytes): its host address.
filled: std::collections::HashMap<(usize, usize), u64, std::hash::BuildHasherDefault<AddressHash>>,
/// Constant-buffer upload copies [`HostList::constant_scratch`] made since
/// the list last wrote title memory (`(src, len)` -> `(buffer, gpu, step)`).
constants: std::collections::HashMap<(usize, usize), (ID3D12Resource, u64, usize), std::hash::BuildHasherDefault<AddressHash>>,
/// Textures whose contents the list brings up to date.
textures: std::collections::HashSet<usize, std::hash::BuildHasherDefault<AddressHash>>,
/// Whether `copies` contains at least one `Step::GpuWrite` (lets
@@ -2759,6 +2762,7 @@ impl Deferred {
self.refreshed.clear();
self.gpu_written.clear();
self.filled.clear();
self.constants.clear();
}
}
@@ -7689,8 +7693,50 @@ impl HostList {
Some(gpu)
}
/// Upload memory for a table's constant buffer (`len` bytes at `src`,
/// viewed by `slot`), deduplicated within this list until the list next
/// writes title memory: repeated binds of the same `(src, len)` share one
/// upload copy and one `Step::Fill`, attaching every slot so `card_steps`
/// retargets all of them.
pub(crate) fn constant_scratch(
&self,
src: *const u8,
len: u32,
slot: (usize, u32),
) -> Option<(ID3D12Resource, u64)> {
let key = (src as usize, len as usize);
{
let mut deferred = self.deferred();
if let Some((buffer, gpu, step_idx)) = deferred.constants.get(&key).cloned() {
if let Some(Step::Fill(_, _, _, Some(to))) = deferred.copies.get_mut(step_idx) {
match to {
card_steps::FillTo::Slot(h0, i0) => {
let first = (*h0, *i0);
*to = card_steps::FillTo::Slots(vec![first, slot]);
}
card_steps::FillTo::Slots(slots) => slots.push(slot),
card_steps::FillTo::Buffer(_) => {}
}
}
return Some((buffer, gpu));
}
}
let (buffer, gpu, to) = self.scratch_held(u64::from(len))?;
let mut deferred = self.deferred();
let step_idx = deferred.copies.len();
deferred.copies.push(Step::Fill(
to as usize,
src as usize,
len as usize,
Some(card_steps::FillTo::Slot(slot.0, slot.1)),
));
deferred.constants.insert(key, (buffer.clone(), gpu, step_idx));
Some((buffer, gpu))
}
/// As [`HostList::defer_fill`], for upload memory that only `slot` (a
/// title heap's address, and the slot) shows.
#[allow(dead_code)]
pub(crate) fn defer_constants(&self, dst: *mut u8, src: *const u8, len: usize, slot: (usize, u32)) {
self.deferred()
.copies
@@ -8248,21 +8294,22 @@ impl HostList {
continue;
}
Step::Run(work) | Step::Host(work, _) => {
batch.fill();
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
if matches!(step, Step::Run(_)) {
let is_run = matches!(step, Step::Run(_));
if is_run {
batch.fill();
batch.title_written();
// (It may write anywhere, a heap's records too.)
if crate::heap_marks::on() {
crate::heap_marks::prep_wrote();
}
}
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
let _step = crate::timeline::span("a step of ours");
work();
// It wrote title memory, where is not known here: the
// submission's one asking ends, later refreshes ask anew.
if matches!(step, Step::Run(_)) {
if is_run {
crate::watch::ask_once_end();
}
#[cfg(feature = "diagnostics")]
@@ -9202,11 +9249,33 @@ impl Batch {
// Below this many the threads cost more than they save.
const FEW: usize = 96;
const PART: usize = 64;
const RUN_PART: usize = 16;
const MAX_RUN: usize = 256 << 10;
if self.fills.is_empty() {
return;
}
let started = std::time::Instant::now();
if self.fills.len() < FEW {
if fill_batch() && self.fills.len() >= 2 {
let mut fills = std::mem::take(&mut self.fills);
fills.sort_unstable_by(|a, b| a.1.cmp(&b.1).then(a.2.cmp(&b.2)));
// With userspace `safecopy`, only coalesce overlapping/duplicate `src`
// ranges (avoids double-copying non-overlapping adjacent fills through
// scratch while still keeping sorted-address cache locality). With
// kernel `ReadProcessMemory`, coalesce within a 4 KiB page to save syscalls.
let max_gap = if crate::safecopy::on() { None } else { Some(4096) };
let runs = coalesce_fill_runs(&fills, max_gap, MAX_RUN);
if fills.len() < FEW || runs.len() < RUN_PART || !wide_fills() {
for run in runs {
run_fill_chunk(&fills, run);
}
} else {
crate::shadow::wide_each(runs.len().div_ceil(RUN_PART), move |i| {
for &run in runs.iter().skip(i * RUN_PART).take(RUN_PART) {
run_fill_chunk(&fills, run);
}
});
}
} else if self.fills.len() < FEW || !wide_fills() {
for (dst, src, len) in self.fills.drain(..) {
copy_mapped(dst as *mut u8, src as *const u8, len, false);
}
@@ -9359,6 +9428,88 @@ impl Batch {
}
}
/// Whether [`Batch::fill`] sorts fills by source address and coalesces
/// duplicate/adjacent source ranges into one staging read (`VICTORY_FILL_BATCH=0`
/// disables; on by default).
fn fill_batch() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_FILL_BATCH").is_none_or(|v| v != "0"))
}
/// Groups `fills` (sorted by `(src, len)`) into coalesced runs
/// `(idx_start, idx_end, run_src_start, run_src_end)` with total span `<= max_run`.
/// When `max_gap` is `None`, only strictly overlapping/duplicate ranges (`src < last.3`)
/// are merged; when `Some(g)`, ranges separated by `<= g` bytes are also merged.
fn coalesce_fill_runs(
fills: &[(usize, usize, usize)],
max_gap: Option<usize>,
max_run: usize,
) -> Vec<(usize, usize, usize, usize)> {
let mut runs: Vec<(usize, usize, usize, usize)> = Vec::new();
for (idx, &(_, src, len)) in fills.iter().enumerate() {
if len == 0 {
continue;
}
let end = src.saturating_add(len);
if let Some(last) = runs.last_mut() {
let new_end = last.3.max(end);
let close = match max_gap {
None => src < last.3,
Some(g) => src <= last.3.saturating_add(g),
};
if len <= max_run
&& close
&& new_end.saturating_sub(last.2) <= max_run
&& idx == last.1
{
last.1 = idx + 1;
last.3 = new_end;
continue;
}
}
runs.push((idx, idx + 1, src, end));
}
runs
}
/// Executes one coalesced run of fills using a thread-local staging buffer
/// when multiple fills share or border the same source memory.
fn run_fill_chunk(fills: &[(usize, usize, usize)], run: (usize, usize, usize, usize)) {
thread_local! {
static SCRATCH: std::cell::RefCell<Vec<u8>> = const { std::cell::RefCell::new(Vec::new()) };
}
let slice = &fills[run.0..run.1];
if slice.len() == 1 {
let (dst, src, len) = slice[0];
copy_mapped(dst as *mut u8, src as *const u8, len, false);
return;
}
let (run_start, run_end) = (run.2, run.3);
let run_len = run_end - run_start;
let ok = SCRATCH.with_borrow_mut(|buf| {
if buf.len() < run_len {
buf.resize(run_len, 0);
}
if !read(run_start as *const u8, buf.as_mut_ptr(), run_len) {
return false;
}
for &(dst, src, len) in slice {
let off = src - run_start;
// SAFETY: `dst` is writable upload scratch of `len` bytes;
// `off + len <= run_len <= buf.len()`; non-overlapping.
unsafe {
std::ptr::copy_nonoverlapping(buf.as_ptr().add(off), dst as *mut u8, len);
}
}
true
});
if !ok {
for &(dst, src, len) in slice {
copy_mapped(dst as *mut u8, src as *const u8, len, false);
}
}
}
/// Copies title memory at submission. The ranges were checked against their
/// resources when recorded, but the title may since have unmapped pages of
/// them (tiled memory, page mappings), which read as zeros here; a title