Merge local/next into local/copies

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 22:16:46 -06:00
commit c802da4b67
20 files changed
+5139 -143

No files matched your search

+365 -77
View File
@@ -1527,11 +1527,17 @@ unsafe impl Send for CardWork {}
static CARD_WORK: Mutex<Option<CardWork>> = Mutex::new(None);
/// The compare of a declared range of a copy in the card's memory with
/// its bytes from before, one thread per 64 KiB piece (cut at multiples of
/// its bytes from before, one group per 64 KiB piece (cut at multiples of
/// 64 KiB of title memory, as `Shadow::card_piece` cuts them): `differs`
/// gets a dword per piece, 1 where a byte differs. The offsets are widened
/// to 16 bytes; buffers are multiples of 64 KiB, and a piece flagged for a
/// byte just outside the range costs a fetch that finds nothing.
///
/// A piece's 64 threads walk it side by side, each every 64th 16 bytes, and
/// one of them hashes their 64 hashes: 64 steps after one another. One thread
/// walked the piece before, 4,096 steps; the passes took 24.8 ms of a frame's
/// 82 on the card (capture replay1, 405 of them), most of it waiting for
/// those steps.
pub(crate) const CARD_COMPARE: &str = r#"
#define RS "RootConstants(num32BitConstants=6, b0), SRV(t0), UAV(u0), UAV(u1)"
cbuffer Span : register(b0) {
@@ -1547,22 +1553,31 @@ ByteAddressBuffer now : register(t0);
// last sent up (or hashed whole, the first time).
RWByteAddressBuffer hashes : register(u0);
RWByteAddressBuffer differs : register(u1);
groupshared uint4 parts[64];
[RootSignature(RS)]
[numthreads(64, 1, 1)]
void main(uint3 id : SV_DispatchThreadID) {
if (id.x >= pieces) return;
void main(uint3 id : SV_GroupID, uint thread : SV_GroupIndex) {
uint piece = first + id.x;
uint lo = piece * 65536u;
lo = lo > skew ? lo - skew : 0u;
uint hi = min((piece + 1u) * 65536u - skew, size);
lo = lo & ~15u;
hi = (hi + 15u) & ~15u;
uint4 h = uint4(0x9E3779B9u, 0x85EBCA6Bu, 0xC2B2AE35u, 0x27D4EB2Fu);
uint4 h = uint4(0x9E3779B9u, 0x85EBCA6Bu, 0xC2B2AE35u, 0x27D4EB2Fu) + thread;
[loop]
for (uint at = lo; at < hi; at += 16u) {
for (uint at = lo + thread * 16u; at < hi; at += 1024u) {
h = (h ^ now.Load4(at)) * uint4(0x01000193u, 0x9E3779B1u, 0x85EBCA77u, 0xC2B2AE3Du);
h = h.yzwx ^ (h >> 15);
}
parts[thread] = h;
GroupMemoryBarrierWithGroupSync();
if (thread != 0u) return;
h = parts[0];
[loop]
for (uint i = 1u; i < 64u; ++i) {
h = (h ^ parts[i]) * uint4(0x01000193u, 0x9E3779B1u, 0x85EBCA77u, 0xC2B2AE3Du);
h = h.yzwx ^ (h >> 15);
}
if (compare != 0u) {
differs.Store(out_at + id.x * 4u, any(h != hashes.Load4(piece * 16u)) ? 1u : 0u);
// With the copy-back on its own thread: the next compare of the
@@ -1574,16 +1589,36 @@ void main(uint3 id : SV_DispatchThreadID) {
}
"#;
/// One pass of [`CARD_COMPARE`] over a span's pieces, into `differs` from
/// dword `at` on (`mode` as the shader's `compare`): a group per piece, in
/// parts a dispatch can hold.
///
/// # Safety
/// The list is open with the compare's root signature, pipeline and buffers
/// set, and the pieces lie in the buffer.
unsafe fn card_pass(list: &ID3D12GraphicsCommandList, span: &crate::shadow::CardSpan, size: u32, at: u32, mode: u32) {
const MOST: u32 = D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION;
let mut done = 0;
while done < span.pieces {
let n = (span.pieces - done).min(MOST);
let constants = [span.first + done, span.skew, size, n, (at + done) * 4, mode];
// SAFETY: the caller's.
unsafe {
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.Dispatch(n, 1, 1);
}
done += n;
}
}
/// Whether the pieces hashed again after ranges were sent up to a copy in
/// the card's memory are hashed in runs of pieces that touch, each piece
/// once (`VICTORY_CARD_REHASH_RUNS=1`; off by default), and not in a pass
/// per range sent up.
///
/// These passes are ours, they run on the card before every submission's
/// lists, and no timing so far names them: each walks its 64 KiB pieces
/// 16 bytes at a time, 4,096 steps one after the other, so a pass takes
/// the time of one piece however few its pieces are. Run 102 sent up 514
/// ranges a frame: 514 such passes. The same pieces are hashed either way;
/// lists: a pass takes the time of one piece however few its pieces are.
/// Run 102 sent up 514 ranges a frame: 514 such passes. The same pieces are hashed either way;
/// a piece hashed when it need not be, or not hashed when it should, costs
/// a fetch that finds nothing and loses no shader write.
fn rehash_runs() -> bool {
@@ -1642,16 +1677,19 @@ enum Step {
/// A copy into title memory (destination, source, bytes): the title's
/// buffer copies, which later refreshes must see.
Copy(usize, usize, usize),
/// A copy of title memory into the list's own upload memory.
Fill(usize, usize, usize),
/// A copy of title memory into the list's own upload memory; if a
/// descriptor slot shows that memory and nothing else does (a constant
/// buffer of a table made for the draw), the title heap and the slot.
Fill(usize, usize, usize, Option<(usize, u32)>),
/// Brings a range (address, bytes) of a buffer's host copy up to date:
/// once per submission, whichever lists ask, until a `Copy` or `Run`
/// intervenes.
Refresh(std::sync::Arc<crate::shadow::Shadow>, usize, usize),
/// Work that may write title memory.
Run(Box<dyn Fn() + Send>),
/// Work that writes host memory only.
Host(Box<dyn Fn() + Send>),
/// Work that writes host memory only, and the title memory it reads
/// (from, to; empty for none).
Host(Box<dyn Fn() + Send>, (usize, usize)),
/// Shaders write a range (address, bytes) of a buffer's host copy.
GpuWrite(std::sync::Arc<crate::shadow::Shadow>, usize, usize),
/// Brings a texture's staging buffer up to date: once per submission,
@@ -2019,6 +2057,8 @@ struct Sent {
number: u64,
card: Option<CardPlan>,
back: Vec<Declared>,
/// What was taken off it to be done on the card.
own: Option<card_steps::Taken>,
/// Its bottom-of-pipe writes.
writes: Vec<(usize, crate::queue::Written, usize)>,
}
@@ -2034,6 +2074,8 @@ struct Flight {
order: Vec<usize>,
card: Option<CardPlan>,
back: Vec<Declared>,
/// The list of ours that ran ahead of them ([`card_steps`]).
own: Option<card_steps::Taken>,
}
/// One thing in the line of what waits for the card, in order.
@@ -2067,6 +2109,8 @@ struct Line {
/// With [`relay`]: the bottom-of-pipe writes of the flights being
/// landed (taken out of `line`, not done yet), for [`late_met`].
in_hand: Vec<(usize, crate::queue::Written, usize)>,
/// With [`card_steps`]: what the flights being landed write.
landing: Vec<card_steps::Writer>,
}
// SAFETY: D3D12 objects (free-threaded) and host lists nothing records
@@ -2082,6 +2126,7 @@ static LINE: Mutex<Line> = Mutex::new(Line {
poke: false,
landed: Vec::new(),
in_hand: Vec::new(),
landing: Vec::new(),
});
/// Told when a flight joins [`LINE`] or it is poked.
static LINE_MOVED: std::sync::Condvar = std::sync::Condvar::new();
@@ -2152,14 +2197,14 @@ fn flying() -> bool {
/// runs steps of ours that may read or write it anywhere; what the
/// flights' shaders write is not there yet.
fn late_blocked(runs: &[&HostList]) -> Option<usize> {
let written = {
let (written, copies) = {
let line = line();
if line.written.is_empty() {
return None;
}
line.written.clone()
(line.written.clone(), card_steps::copies(&line))
};
blocked_by(&written, runs, true)
blocked_by(&written, runs, true).or_else(|| card_steps::copied_read(&copies, runs).then_some(WHY_READ))
}
/// Whether a small fill of upload memory, or copy of a title buffer, goes on without waiting for
@@ -2188,48 +2233,131 @@ fn fill_is_titles(src: usize, len: usize) -> bool {
on && len <= SMALL && (crate::watch::written_since_taken(src, len) || !crate::watch::landed_lately(src, len, 600))
}
/// Diagnostics: the reads that made a submission wait for flights: what
/// kind, where, how much, and the declared write it lies in; how often and
/// how long the wait was.
/// Diagnostics: the waits for flights, by what waited, why it was not done
/// on the card ([`card_steps`]), its bytes, and the declared write it lies
/// in (start, bytes).
#[cfg(feature = "diagnostics")]
type Blame = std::collections::HashMap<(&'static str, usize, usize, usize, usize), (u64, u64)>;
type Blamed = (&'static str, &'static str, usize, usize, usize);
/// How often and how long, and the address the last one read.
#[cfg(feature = "diagnostics")]
static BLAME: Mutex<Option<Blame>> = Mutex::new(None);
static BLAME: Mutex<Option<std::collections::HashMap<Blamed, (u64, u64, usize)>>> = Mutex::new(None);
/// What the wait that comes next is for, and why not on the card.
#[cfg(feature = "diagnostics")]
static BLAMED: Mutex<Option<(&'static str, usize, usize, usize, usize)>> = Mutex::new(None);
static BLAMED: Mutex<(Option<(Blamed, usize)>, &'static str)> = Mutex::new((None, ""));
/// Diagnostics: shader writes declared over a megabyte or more: the range
/// (from, to) and what declares it ([`wide_write`]): how often, and its
/// shaders.
#[cfg(feature = "diagnostics")]
static WIDE_WRITES: Mutex<Option<std::collections::HashMap<(usize, usize, usize), (u64, String)>>> = Mutex::new(None);
/// Diagnostics: a draw or dispatch declares that it writes `len` bytes at
/// `address`, through a root view of `pipeline`, a view in a table (0) or a
/// raw address (1).
#[cfg(feature = "diagnostics")]
pub(crate) fn wide_write(address: u64, len: u64, pipeline: usize) {
if len < 1 << 20 {
return;
}
let mut all = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner());
let e = all
.get_or_insert_with(Default::default)
.entry((address as usize, (address + len) as usize, pipeline))
.or_insert_with(|| {
let shaders = match pipeline {
0 => "a view in a table".into(),
1 => "a raw address".into(),
p => crate::pipeline::host_pipeline(p as crate::object::This).map_or_else(String::new, |p| {
p.stages.iter().map(|s| s.shader.hash.to_string()).collect::<Vec<_>>().join("+")
}),
};
(0, shaders)
});
e.0 += 1;
}
#[cfg(feature = "diagnostics")]
fn blame(kind: &'static str, written: &[(usize, usize)], at: usize, len: usize) {
let i = written.partition_point(|r| r.1 <= at);
let (from, to) = written.get(i).copied().unwrap_or((0, 0));
*BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = Some((kind, at, len, from, to - from));
let mut blamed = BLAMED.lock().unwrap_or_else(|e| e.into_inner());
let why = blamed.1;
blamed.0 = Some(((kind, why, len, from, to - from), at));
}
/// The wait for what [`blame`] last named took `us`.
/// Why [`card_steps`] left the submission to the wait that comes next.
#[cfg(feature = "diagnostics")]
fn blamed(us: u64) {
if let Some(key) = BLAMED.lock().unwrap_or_else(|e| e.into_inner()).take() {
let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner());
let e = all.get_or_insert_with(Default::default).entry(key).or_insert((0, 0));
e.0 += 1;
e.1 += us;
}
fn refused(why: &'static str) {
*BLAMED.lock().unwrap_or_else(|e| e.into_inner()) = (None, why);
}
/// The wait for what [`blame`] last named, or a wait of another reason than
/// title memory read, took `us`.
#[cfg(feature = "diagnostics")]
fn blamed(why: usize, us: u64) {
let (named, refused) = std::mem::replace(&mut *BLAMED.lock().unwrap_or_else(|e| e.into_inner()), (None, ""));
let (key, at) = match (why, named) {
(WHY_READ, Some(named)) => named,
(WHY_READ, None) => (("a host copy of what a copy done on the card wrote", refused, 0, 0, 0), 0),
_ => {
let kind = match why {
WHY_SYNC => "a synchronous submission, a present or the sweep",
WHY_VALUE => "a memory value",
WHY_STALE => "title memory changed under shader writes",
WHY_RUN => "a step of ours",
WHY_MOST => "lists in the air",
WHY_FENCE => "a blocking fence wait",
_ => "another reason",
};
((kind, "", 0, 0, 0), 0)
}
};
let mut all = BLAME.lock().unwrap_or_else(|e| e.into_inner());
let e = all.get_or_insert_with(Default::default).entry(key).or_default();
*e = (e.0 + 1, e.1 + us, at);
}
/// For the frame-rate line: the five that waited longest.
#[cfg(feature = "diagnostics")]
fn log_blame(frames: f64) {
let all = BLAME.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default();
let wide = WIDE_WRITES.lock().unwrap_or_else(|e| e.into_inner()).take().unwrap_or_default();
let mut all: Vec<_> = all.into_iter().collect();
all.sort_unstable_by_key(|(_, (_, us))| std::cmp::Reverse(*us));
for ((kind, at, len, from, size), (n, us)) in all.into_iter().take(8) {
all.sort_unstable_by_key(|(_, (_, us, _))| std::cmp::Reverse(*us));
for ((kind, why, len, from, size), (n, us, at)) in all.into_iter().take(5) {
// The passes that declare it written, the ones that do so most.
let mut passes: std::collections::BTreeMap<&str, u64> = std::collections::BTreeMap::new();
for ((a, b, _), (n, shaders)) in &wide {
if *a < at + len && at < *b {
*passes.entry(shaders).or_default() += n;
}
}
let mut passes: Vec<(&str, u64)> = passes.into_iter().collect();
passes.sort_unstable_by_key(|p| std::cmp::Reverse(p.1));
let passes: Vec<String> =
passes.into_iter().take(5).map(|(shaders, n)| format!("{shaders} {:.1}x", n as f64 / frames)).collect();
tracing::info!(
"waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} {at:#x}+{len:#x}, in shader writes declared over {from:#x}+{size:#x}",
"waited for flights, per frame: {:.1} ms in {:.2} waits for {kind} +{len:#x} (last at {at:#x}), in shader writes declared \
over {from:#x}+{size:#x} (declared per frame over a megabyte or more there, by shader: {}){}{why}",
us as f64 / 1e3 / frames,
n as f64 / frames
n as f64 / frames,
passes.join(", "),
if why.is_empty() { "" } else { "; not done on the card: " },
);
}
}
/// Whether flights that write `written` stop a read of `len` bytes at `src`.
fn read_blocked(written: &[(usize, usize)], src: usize, len: usize) -> bool {
touches(written, src, src + len) && !fill_is_titles(src, len)
}
/// Whether they stop a title buffer copy, and if so whether by its source.
fn copy_blocked(written: &[(usize, usize)], dst: usize, src: usize, len: usize) -> Option<bool> {
let read = read_blocked(written, src, len);
(read || touches(written, dst, dst + len)).then_some(read)
}
/// [`late_blocked`] against `written`, the title memory flights' shaders
/// may write (in order, joined); `count` whether the kind of read is
/// counted for the frame line.
@@ -2241,12 +2369,11 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
for step in &list.deferred().copies {
match step {
&Step::Copy(dst, src, len) => {
let read = touches(written, src, src + len) && !fill_is_titles(src, len);
if read || touches(written, dst, dst + len) {
if let Some(_read) = copy_blocked(written, dst, src, len) {
if count {
LATE_STATS[15].fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
if read {
if _read {
blame("a buffer copy's source", written, src, len);
} else {
blame("a buffer copy's destination", written, dst, len);
@@ -2255,8 +2382,8 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
return Some(WHY_READ);
}
}
&Step::Fill(_, src, len) => {
if touches(written, src, src + len) && !fill_is_titles(src, len) {
&Step::Fill(_, src, len, _) => {
if read_blocked(written, src, len) {
if count {
LATE_STATS[16].fetch_add(1, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
@@ -2277,7 +2404,7 @@ fn blocked_by(written: &[(usize, usize)], runs: &[&HostList], count: bool) -> Op
}
}
Step::Run(_) => return Some(WHY_RUN),
Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(_) => {}
Step::Refresh(..) | Step::GpuWrite(..) | Step::Host(..) => {}
}
}
}
@@ -2517,7 +2644,7 @@ fn relay_wait(why: usize, done: impl Fn(&Line) -> bool) {
LATE_WAITED[why.min(10)].fetch_add(us, Ordering::Relaxed);
SUBMISSION_US[1].fetch_add(us, Ordering::Relaxed);
#[cfg(feature = "diagnostics")]
blamed(us);
blamed(why, us);
}
/// What the thread that lands flights fetches card copies' pieces with
@@ -2582,6 +2709,9 @@ fn relay_worker() {
_ => None,
})
.collect();
if card_steps::stage() > 0 {
line.landing = card_steps::writers(&items);
}
break Ok(items);
}
};
@@ -2711,6 +2841,9 @@ fn late_line(frames: f64) {
r[4] / 1000.0,
);
}
card_steps::log(frames);
#[cfg(feature = "diagnostics")]
log_blame(frames);
if late_exact() {
let [pages, runs, bytes] = crate::shadow::merged_stats().map(|n| n as f64 / frames);
tracing::info!(
@@ -2721,6 +2854,8 @@ fn late_line(frames: f64) {
}
}
mod card_steps;
#[cfg(test)]
mod late_tests {
use super::{continues, join_ranges, touches};
@@ -2836,6 +2971,19 @@ fn declared_writes(runs: &[&HostList]) -> Vec<Declared> {
back
}
/// The walls trace (`VICTORY_WALLS_TRACE=1` or `2`; off by default): log
/// lines that say what the title's CPU was given while it decided whether
/// the camera is in a room. The values of the small read-backs
/// ([`read_backs`]), the frame's main pass by scissor rectangle with its
/// culling dispatches (`draw.rs`, `walls_census`), the shader-written
/// buffers merged into title memory (`shadow.rs`, `walls_merged`), host
/// copies found stale after their refresh (`shadow.rs`), and, in the
/// runtime, the read requests that fail (with 2: every request).
pub(crate) fn walls_trace() -> bool {
static ON: OnceLock<bool> = OnceLock::new();
*ON.get_or_init(|| std::env::var_os("VICTORY_WALLS_TRACE").is_some_and(|v| v == "1" || v == "2"))
}
/// After `list` has run: the texels its read-backs copied into host
/// buffers of ours go to the title's memory (`HostList::defer_read_back`).
///
@@ -2855,6 +3003,19 @@ fn read_backs(list: &HostList) {
for line in 0..*rows {
std::ptr::copy_nonoverlapping(from.add(line * row.next_multiple_of(256)), (*at as *mut u8).add(line * pitch), *row);
}
// The walls trace: what the title is handed (one row of at
// most 24 values: its 5x1 and 1x1 float targets).
if walls_trace() && *rows == 1 && *row <= 96 {
let mut values = [0f32; 24];
std::ptr::copy_nonoverlapping(from, values.as_mut_ptr().cast::<u8>(), *row & !3);
tracing::info!(
frame = crate::frame::current(),
to = format_args!("{:#x}", *at),
bytes = *row,
values = ?&values[..*row / 4],
"walls trace: a read-back lands in title memory"
);
}
buffer.Unmap(0, None);
}
}
@@ -3263,8 +3424,12 @@ impl Host {
(|| -> windows::core::Result<()> {
allocator.Reset()?;
list.Reset(allocator, None)?;
// Every copy first, then one barrier call, then every
// pass: a wait between each copy and its pass had the
// card do them one after the other.
let mut sent = Vec::new();
for (shadow, ranges) in &dirty {
let Some((staging, before, size)) = shadow.card_sides() else {
let Some((staging, before, _)) = shadow.card_sides() else {
continue;
};
for &(a, b) in ranges {
@@ -3272,7 +3437,22 @@ impl Host {
crate::shadow::CARD_STATS[0].fetch_add((b - a) as u64, Ordering::Relaxed);
}
crate::shadow::CARD_STATS[1].fetch_add(ranges.len() as u64, Ordering::Relaxed);
let Some(before) = before else {
if before.is_some() && !ranges.is_empty() {
sent.push(transition(
&shadow.buffer,
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
));
}
}
if !sent.is_empty() {
list.ResourceBarrier(&sent);
}
list.SetComputeRootSignature(&work.root);
list.SetPipelineState(&work.pso);
list.SetComputeRootUnorderedAccessView(3, work.differs.0.GetGPUVirtualAddress());
for (shadow, ranges) in &dirty {
let Some((_, Some(before), size)) = shadow.card_sides() else {
continue;
};
// No twin (it cost the card the copy's size again,
@@ -3281,18 +3461,8 @@ impl Host {
// hashed again, all of them the first time; the
// compare after the lists hashes the declared
// pieces and flags those whose hash is another.
if !ranges.is_empty() {
list.ResourceBarrier(&[transition(
&shadow.buffer,
D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE,
)]);
}
list.SetComputeRootSignature(&work.root);
list.SetPipelineState(&work.pso);
list.SetComputeRootShaderResourceView(1, shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(3, work.differs.0.GetGPUVirtualAddress());
let runs: Vec<crate::shadow::CardSpan> = if whole.iter().any(|s| Arc::ptr_eq(s, shadow)) {
vec![shadow.card_run(0, usize::MAX)]
} else {
@@ -3303,9 +3473,7 @@ impl Host {
CARD_PASSES[0].fetch_add(runs.len() as u64, Ordering::Relaxed);
CARD_PASSES[1].fetch_add(runs.iter().map(|r| u64::from(r.pieces)).sum::<u64>(), Ordering::Relaxed);
for run in runs {
let constants = [run.first, run.skew, size as u32, run.pieces, 0, 0];
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.Dispatch(run.pieces.div_ceil(64), 1, 1);
card_pass(list, &run, size as u32, 0, 0);
}
}
list.Close()?;
@@ -3387,11 +3555,9 @@ impl Host {
let Some((_, Some(before), size)) = r.shadow.card_sides() else {
continue;
};
let constants = [r.span.first, r.span.skew, size as u32, r.span.pieces, r.at * 4, if flown { 2 } else { 1 }];
list.SetComputeRoot32BitConstants(0, 6, constants.as_ptr().cast(), 0);
list.SetComputeRootShaderResourceView(1, r.shadow.buffer.GetGPUVirtualAddress());
list.SetComputeRootUnorderedAccessView(2, before.GetGPUVirtualAddress());
list.Dispatch(r.span.pieces.div_ceil(64), 1, 1);
card_pass(list, &r.span, size as u32, r.at, if flown { 2 } else { 1 });
CARD_PASSES[2].fetch_add(1, Ordering::Relaxed);
}
list.Close()?;
@@ -3611,7 +3777,9 @@ impl Host {
// SAFETY: the mapping of the fetched bytes, `total` of them;
// the copy into it has run.
let gpu = unsafe { std::slice::from_raw_parts((data + *at as usize) as *const u8, hi - lo) };
written += shadow.card_merge_relay(*lo, gpu, after, &mut title);
let merged = shadow.card_merge_relay(*lo, gpu, after, &mut title);
shadow.walls_merged(gpu.len(), merged);
written += merged;
}
COPY_BACK[1].fetch_add(written as u64, Ordering::Relaxed);
// Sends up to `after` are of no use to any later merge.
@@ -3691,6 +3859,9 @@ impl Host {
if let Some(card) = flight.card {
self.card_done(card);
}
if let Some(own) = flight.own {
own.landed();
}
landed.push((flight.lists, flight.order));
}
Late::Write(at, what, bytes) => what.write(at as *mut u8, bytes),
@@ -3713,10 +3884,12 @@ impl Host {
poke,
landed: kept,
in_hand,
landing,
..
} = &mut *line;
kept.extend(landed);
in_hand.clear();
landing.clear();
// What is still in the air, from the flights that are.
written.clear();
*lists = 0;
@@ -3840,7 +4013,9 @@ impl Host {
// the copy into it has run, and the caller
// waits for this job.
let gpu = unsafe { std::slice::from_raw_parts((fetched + at as usize) as *const u8, hi - lo) };
counts[i].fetch_add(shadow.card_merge(lo, gpu, &mut title), Ordering::Relaxed);
let merged = shadow.card_merge(lo, gpu, &mut title);
shadow.walls_merged(gpu.len(), merged);
counts[i].fetch_add(merged, Ordering::Relaxed);
}
}) as Box<dyn FnOnce() + Send>
})
@@ -3855,7 +4030,9 @@ impl Host {
// SAFETY: the mapping of the fetched bytes, `total` of
// them; the copy into it has run.
let gpu = unsafe { std::slice::from_raw_parts((*data + at as usize) as *const u8, hi - lo) };
written[i] += plan.ranges[i].shadow.card_merge(lo, gpu, &mut title);
let merged = plan.ranges[i].shadow.card_merge(lo, gpu, &mut title);
plan.ranges[i].shadow.walls_merged(gpu.len(), merged);
written[i] += merged;
}
}
}
@@ -3909,6 +4086,7 @@ impl Host {
order,
card: sent.card,
back: sent.back,
own: sent.own,
}));
items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes)));
*flying >= late_most()
@@ -3953,6 +4131,7 @@ impl Host {
order: Vec::new(),
card: sent.card,
back: sent.back,
own: sent.own,
}));
items.extend(sent.writes.into_iter().map(|(at, what, bytes)| Late::Write(at, what, bytes)));
}
@@ -4133,17 +4312,32 @@ impl Host {
// Flights in the air ([`late`]): a submission that is waited for
// settles them first, one sent ahead only if preparing it reads
// what they write.
// Or the reads are done on the card, behind the flights.
let own = if late { self.card_steps(runs) } else { None };
if !late {
self.settle(&mut submitted, WHY_SYNC);
} else if let Some(why) = late_blocked(runs) {
} else if own.is_none()
&& let Some(why) = late_blocked(runs)
{
if relay_exact() && why == WHY_READ {
// Only until the flights that may write what is read have
// landed: their copy-back's end, not the card's other work.
relay_wait(why, |line| blocked_by(&line.written, runs, false).is_none());
relay_wait(why, |line| {
blocked_by(&line.written, runs, false).is_none() && !card_steps::copied_read(&card_steps::copies(line), runs)
});
} else {
self.settle(&mut submitted, why);
}
}
// That list runs first, and is one of the submission's from here on.
let with: Vec<&HostList>;
let runs = match &own {
Some(own) => {
with = std::iter::once(own.list()).chain(runs.iter().copied()).collect();
&with[..]
}
None => runs,
};
let behind = late && flying();
// Before this submission counts as started: what the sweep lets go
// of is named by no list, and nothing runs now (behind flights the
@@ -4220,7 +4414,7 @@ impl Host {
#[cfg(feature = "diagnostics")]
split.part(1);
for list in runs {
list.run_deferred(&mut batch);
list.run_deferred(&mut batch, |i| own.as_ref().is_some_and(|own| own.takes(list, i)));
firsts.push(lists.len());
if let Some(prelude) = list.prelude() {
lists.push(Some(prelude));
@@ -4282,6 +4476,7 @@ impl Host {
number: this_submission,
card,
back,
own,
writes,
};
working(was_working);
@@ -5166,7 +5361,15 @@ impl HostList {
pub(crate) fn defer_fill(&self, dst: *mut u8, src: *const u8, len: usize) {
self.deferred()
.copies
.push(Step::Fill(dst as usize, src as usize, len));
.push(Step::Fill(dst as usize, src as usize, len, None));
}
/// As [`HostList::defer_fill`], for upload memory that only `slot` (a
/// title heap's address, and the slot) shows.
pub(crate) fn defer_constants(&self, dst: *mut u8, src: *const u8, len: usize, slot: (usize, u32)) {
self.deferred()
.copies
.push(Step::Fill(dst as usize, src as usize, len, Some(slot)));
}
/// Brings `[address, address + len)` of a buffer's host copy up to date
@@ -5286,7 +5489,7 @@ impl HostList {
// list's last run (mesh dispatches: the largest group count
// asked for, the groups asked in all; with the card's copy, as
// the list's end copied them here): taken, then zeroed.
deferred.copies.push(Step::Host(Box::new(move || unsafe {
let notes = Box::new(move || unsafe {
let words = at as *mut u32;
let (largest, all) = (words.add(1).read_volatile(), words.add(2).read_volatile());
MESH_GROUPS[0].fetch_max(u64::from(largest), Ordering::Relaxed);
@@ -5294,7 +5497,8 @@ impl HostList {
for k in 0..3 {
words.add(k).write_volatile(0);
}
})));
});
deferred.copies.push(Step::Host(notes, (0, 0)));
drop(deferred);
if let Some(card) = card {
self.zero_card_budget(card);
@@ -5470,7 +5674,13 @@ impl HostList {
/// As [`HostList::defer`], for work that writes only host memory.
pub(crate) fn defer_host(&self, work: impl Fn() + Send + 'static) {
self.deferred().copies.push(Step::Host(Box::new(work)));
self.deferred().copies.push(Step::Host(Box::new(work), (0, 0)));
}
/// As [`HostList::defer_host`], for work that reads `len` bytes of title
/// memory at `from`.
pub(crate) fn defer_host_read(&self, from: usize, len: usize, work: impl Fn() + Send + 'static) {
self.deferred().copies.push(Step::Host(Box::new(work), (from, from + len)));
}
/// Brings a texture's staging buffer up to date when the list is
@@ -5481,8 +5691,10 @@ impl HostList {
/// Runs the list's submission-time steps; refreshes go into `batch`
/// (the title's big buffers, a 174 MB one among them, are read by most
/// lists of a submission, through many ranges).
fn run_deferred(&self, batch: &mut Batch) {
/// lists of a submission, through many ranges). Copies and fills that
/// `taken` names (by their place) are left out: they are done on the
/// card ([`card_steps`]).
fn run_deferred(&self, batch: &mut Batch, taken: impl Fn(usize) -> bool) {
#[cfg(feature = "diagnostics")]
use crate::copystat::{C, add};
// The time and the bytes of the copies and fills, some 13,000 a
@@ -5490,7 +5702,10 @@ impl HostList {
let (mut us, mut bytes) = (std::time::Duration::ZERO, 0u64);
#[cfg(feature = "diagnostics")]
let (mut copies, mut refreshes, mut refreshed) = (0u64, 0u64, 0u64);
for step in &self.deferred().copies {
for (i, step) in self.deferred().copies.iter().enumerate() {
if matches!(step, Step::Copy(..) | Step::Fill(..)) && taken(i) {
continue;
}
let (started, copied) = match step {
&Step::Copy(dst, src, len) => {
let started = std::time::Instant::now();
@@ -5504,7 +5719,7 @@ impl HostList {
crate::watch::wrote(dst, len);
(started, len)
}
&Step::Fill(dst, src, len) => {
&Step::Fill(dst, src, len, _) => {
let started = std::time::Instant::now();
#[cfg(feature = "diagnostics")]
{
@@ -5522,7 +5737,7 @@ impl HostList {
batch.add(shadow, *address, *len);
continue;
}
Step::Run(work) | Step::Host(work) => {
Step::Run(work) | Step::Host(work, _) => {
#[cfg(feature = "diagnostics")]
let started = std::time::Instant::now();
if matches!(step, Step::Run(_)) {
@@ -6448,7 +6663,6 @@ impl Meter {
);
#[cfg(feature = "diagnostics")]
{
log_blame(frames);
let put = [0, 1, 2, 3].map(|i| crate::shadow::PUT[i].swap(0, Ordering::Relaxed) as f64 / frames);
tracing::info!(
"what shaders wrote put into title memory per frame (VICTORY_EXACT_BACK): {:.0} pieces in {:.0} writes, {:.1} MB; {:.0} pieces joined over gaps",
@@ -6535,7 +6749,17 @@ impl Host {
.ResizeBuffers(BUFFERS, width, height, format, DXGI_SWAP_CHAIN_FLAG(0))
};
match resized {
Ok(()) => (p.width, p.height, p.format) = (width, height, format),
Ok(()) => {
(p.width, p.height, p.format) = (width, height, format);
tracing::info!(
width,
height,
format = format.0,
frame,
"the swap chain's buffers were made again for another size or format"
);
self.log_video_memory("right after the swap chain's buffers were made again");
}
Err(e) => {
tracing::warn!(error = %e, width, height, "swap chain not resized");
return None;
@@ -6631,6 +6855,7 @@ impl Host {
Some(hwnd) => hwnd,
None => window(width, height)?,
};
self.log_video_memory("right before the swap chain is made");
// SAFETY: plain object creation for our window.
unsafe {
let factory: IDXGIFactory2 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0))?;
@@ -6653,6 +6878,7 @@ impl Host {
let copy = self.new_list()?;
copy.close();
tracing::info!(width, height, format = format.0, "presenting to a window");
self.log_video_memory("right after the swap chain is made");
Ok(Presenter {
swap_chain,
width,
@@ -6732,9 +6958,71 @@ impl Host {
.QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &mut shared)
.ok()?;
}
// The card's budget moved by 256 MB or more since the last look:
// said at once, with what was in use at the last look, so the log
// has the moment. (It fell from 11,242 MB to some 10,300 MB with
// the first world frame in runs 179 to 185, and the memory line,
// seconds later, was the first to show it. We reserve nothing
// and make the swap chain once, at the start.)
static LAST: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3];
let now = [own.Budget, own.CurrentUsage, shared.CurrentUsage];
let last = [0, 1, 2].map(|i| LAST[i].swap(now[i], Ordering::Relaxed));
if last[0] != 0 && last[0].abs_diff(own.Budget) >= (256u64 << 20) {
tracing::info!(
"the system changed the budget of the card's memory from {} MB to {} MB (frame {}); at the last look {} MB of the card's and {} MB of the PC's shared memory were in use",
last[0] >> 20,
own.Budget >> 20,
crate::frame::current(),
last[1] >> 20,
last[2] >> 20
);
self.log_video_memory("right after that change");
}
Some([own.Budget, own.CurrentUsage, shared.Budget, shared.CurrentUsage])
}
/// Both of the system's memory groups for this process, in bytes: for
/// the card's own memory and then for the PC's memory shared with the
/// card, the budget, the use, what could still be reserved and what
/// is reserved (the layer reserves none).
pub(crate) fn video_memory_groups(&self) -> Option<[[u64; 4]; 2]> {
let adapter = self.adapter.as_ref()?;
let mut own = DXGI_QUERY_VIDEO_MEMORY_INFO::default();
let mut shared = DXGI_QUERY_VIDEO_MEMORY_INFO::default();
// SAFETY: plain queries that fill our structures.
unsafe {
adapter
.QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_LOCAL, &mut own)
.ok()?;
adapter
.QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &mut shared)
.ok()?;
}
let all = |m: &DXGI_QUERY_VIDEO_MEMORY_INFO| {
[m.Budget, m.CurrentUsage, m.AvailableForReservation, m.CurrentReservation]
};
Some([all(&own), all(&shared)])
}
/// Logs [`Self::video_memory_groups`], with `when` it is said.
pub(crate) fn log_video_memory(&self, when: &str) {
let Some([own, shared]) = self.video_memory_groups() else {
return;
};
tracing::info!(
"video memory as the system counts it, {}: the card's own: budget {} MB, in use {} MB, {} MB could still be reserved, {} MB reserved; the PC's shared with the card: budget {} MB, in use {} MB, {} MB could still be reserved, {} MB reserved (the layer reserves none)",
when,
own[0] >> 20,
own[1] >> 20,
own[2] >> 20,
own[3] >> 20,
shared[0] >> 20,
shared[1] >> 20,
shared[2] >> 20,
shared[3] >> 20
);
}
/// A buffer in system memory that the CPU maps (write-back, so reading
/// it is fast too) and shaders read and write: for host copies of title
/// memory that shaders write, which upload memory cannot be. Starts in