From edcde681f01451ec8a74b6396035e1aeb0bde400 Mon Sep 17 00:00:00 2001 From: local Date: Wed, 25 Nov 2026 10:52:47 -0600 Subject: [PATCH] The run's numbers on the picture (VICTORY_PANEL=1; off; F3 shows and hides it) White on black in the upper left corner of what is shown, twice a second, from the counters of the frame-rate line, memory and textures; our own 5x7 characters. Copied onto the swap chain's buffer out of a buffer in the card's memory: screenshots carry it, saved frames and the frame recorder do not. Co-Authored-By: Claude Opus 5.5 --- crates/victory-gpu/Cargo.toml | 2 +- crates/victory-gpu/src/copystat.rs | 9 +- crates/victory-gpu/src/draw.rs | 6 + crates/victory-gpu/src/host.rs | 16 + crates/victory-gpu/src/host/panel.rs | 486 ++++++++++++++++++++++++ crates/victory-gpu/src/pipeline.rs | 6 + crates/victory-gpu/src/replay/record.rs | 6 + 7 files changed, 529 insertions(+), 2 deletions(-) create mode 100644 crates/victory-gpu/src/host/panel.rs diff --git a/crates/victory-gpu/Cargo.toml b/crates/victory-gpu/Cargo.toml index 858802a..16c7495 100644 --- a/crates/victory-gpu/Cargo.toml +++ b/crates/victory-gpu/Cargo.toml @@ -30,7 +30,7 @@ diagnostics = [] workspace = true [target.'cfg(windows)'.dependencies] -windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading"] } +windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_Security", "Win32_System_Diagnostics_Debug", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Kernel", "Win32_System_LibraryLoader", "Win32_System_Memory", "Win32_System_Performance", "Win32_System_ProcessStatus", "Win32_System_Registry", "Win32_System_SystemInformation", "Win32_System_Threading", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] } windows = { workspace = true, features = [ "Win32_Graphics_Direct3D", "Win32_Graphics_Direct3D_Fxc", diff --git a/crates/victory-gpu/src/copystat.rs b/crates/victory-gpu/src/copystat.rs index 484c90a..34c214d 100644 --- a/crates/victory-gpu/src/copystat.rs +++ b/crates/victory-gpu/src/copystat.rs @@ -161,6 +161,13 @@ pub(crate) fn add(c: C, n: u64) { COUNT[bank()].0[c as usize].fetch_add(n, Relaxed); } +/// The counter, all sets together, as it is now (it goes back to 0 with +/// [`log`]). +pub(crate) fn peek(c: C) -> u64 { + let i = c as usize; + COUNT.iter().map(|b| b.0[i].load(Relaxed)).sum() +} + /// Counter `i`, all sets together, from now on 0. fn taken(i: usize) -> u64 { COUNT.iter().map(|b| b.0[i].swap(0, Relaxed)).sum() @@ -491,7 +498,7 @@ mod own_tests { /// The process's memory in bytes: in physical memory, and committed /// (private bytes). -fn process_memory() -> (u64, u64) { +pub(crate) fn process_memory() -> (u64, u64) { use windows_sys::Win32::System::ProcessStatus::{ K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS, }; diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index 9889269..ccf8c8e 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -452,6 +452,12 @@ pub(crate) fn take_frame() -> Vec { std::mem::take(&mut *FRAME.lock().unwrap_or_else(|e| e.into_inner())) } +/// The draws dropped since [`take_dropped`]. +#[cfg_attr(not(feature = "diagnostics"), allow(dead_code))] +pub(crate) fn dropped_now() -> u64 { + DROPPED.load(Ordering::Relaxed) +} + /// Draws, mesh dispatches and dispatches recorded but not run on the host /// (no host pipeline) since the last call. pub(crate) fn take_dropped() -> u64 { diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 507a79d..7702f90 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -3467,6 +3467,8 @@ pub(crate) mod aside; pub(crate) mod awake; mod card_steps; pub(crate) mod indirects; +#[cfg(feature = "diagnostics")] +mod panel; mod place; mod send; pub(crate) use send::{goes_as_words, words_under}; @@ -7960,6 +7962,8 @@ struct Presenter { ahead: Vec<(HostList, u64)>, hwnd: HWND, meter: Meter, + #[cfg(feature = "diagnostics")] + panel: panel::Panel, } /// The frame rate, shown in the window's caption every second and logged @@ -7989,6 +7993,12 @@ impl Meter { } } + /// Whether the next frame counted makes the frame-rate line. + #[cfg(feature = "diagnostics")] + fn line_due(&self) -> bool { + self.since.elapsed() >= std::time::Duration::from_secs(1) + } + /// Counts a presented frame. fn frame(&mut self, hwnd: HWND) { #[cfg(feature = "diagnostics")] @@ -8363,6 +8373,8 @@ impl Host { .flatten(); #[cfg(feature = "diagnostics")] let probe = if slot.is_some() { None } else { crate::capture::probe(&self.device, list, texture) }; + #[cfg(feature = "diagnostics")] + p.panel.draw(self, copy, &back, (width, height), format); #[cfg(not(feature = "diagnostics"))] let _ = frame; list.ResourceBarrier(&[ @@ -8390,6 +8402,8 @@ impl Host { let mut showing = crate::timeline::span("the host's Present"); let _ = p.swap_chain.Present(0, DXGI_PRESENT(0)); showing.next("the frame meter"); + #[cfg(feature = "diagnostics")] + p.panel.frame(p.meter.line_due()); p.meter.frame(p.hwnd); showing.end(); #[cfg(feature = "diagnostics")] @@ -8442,6 +8456,8 @@ impl Host { ahead: Vec::new(), hwnd, meter: Meter::new(), + #[cfg(feature = "diagnostics")] + panel: panel::Panel::new(), }) } } diff --git a/crates/victory-gpu/src/host/panel.rs b/crates/victory-gpu/src/host/panel.rs new file mode 100644 index 0000000..586d08f --- /dev/null +++ b/crates/victory-gpu/src/host/panel.rs @@ -0,0 +1,486 @@ +//! Diagnostics: the run's numbers on the picture (`VICTORY_PANEL=1`; off by +//! default; F3 shows and hides it while our window has the focus): white +//! text on a black box in the upper left corner of what is shown, so that a +//! screenshot carries them. Made again twice a second from the counters of +//! the frame-rate line, memory and textures; a frame itself only copies the +//! box out of a buffer in the card's memory. Copied onto the swap chain's +//! buffer, behind the title's picture: saved frames and the frame recorder +//! see the title's own. + +use std::sync::OnceLock; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::{Duration, Instant}; + +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::*; +use windows_sys::Win32::UI::Input::KeyboardAndMouse::{GetAsyncKeyState, VK_F3}; +use windows_sys::Win32::UI::WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId}; + +use super::{Host, HostList, transition}; + +/// How often the numbers change. +const EVERY: Duration = Duration::from_millis(500); + +/// A character's dots, and with the space beside and below it. +const GLYPH: (u32, u32) = (5, 7); +const CELL: (u32, u32) = (6, 9); +/// Characters in a line, lines, and the box's rim in dots. +const COLUMNS: u32 = 43; +const LINES: u32 = 14; +const RIM: u32 = 3; + +/// The texts are put into one of these in turn: the card reads the others, +/// at most half a second old. +const UPLOADS: usize = 4; + +fn asked() -> bool { + static ON: OnceLock = OnceLock::new(); + *ON.get_or_init(|| crate::switch::on("VICTORY_PANEL")) +} + +/// Whether it is shown now. +fn shown() -> &'static AtomicBool { + static SHOWN: OnceLock = OnceLock::new(); + SHOWN.get_or_init(|| AtomicBool::new(asked())) +} + +/// F3 was pressed since the last call, with one of our windows in front. +fn pressed() -> bool { + static DOWN: AtomicBool = AtomicBool::new(false); + // SAFETY: a plain query. + let down = unsafe { GetAsyncKeyState(i32::from(VK_F3)) } < 0; + if DOWN.swap(down, Ordering::Relaxed) || !down { + return false; + } + let mut process = 0; + // SAFETY: plain queries; room for the answer. + unsafe { GetWindowThreadProcessId(GetForegroundWindow(), &mut process) }; + process == std::process::id() +} + +/// Seven rows of five dots, the leftmost the highest bit. +#[rustfmt::skip] +const fn glyph(c: u8) -> [u8; 7] { + match c { + b'0' => [0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110], + b'1' => [0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110], + b'2' => [0b01110, 0b10001, 0b00001, 0b00110, 0b01000, 0b10000, 0b11111], + b'3' => [0b11110, 0b00001, 0b00001, 0b01110, 0b00001, 0b00001, 0b11110], + b'4' => [0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010], + b'5' => [0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110], + b'6' => [0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110], + b'7' => [0b11111, 0b00001, 0b00010, 0b00010, 0b00100, 0b00100, 0b00100], + b'8' => [0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110], + b'9' => [0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100], + b'A' => [0b00100, 0b01010, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001], + b'B' => [0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b11110], + b'C' => [0b01111, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b01111], + b'D' => [0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11110], + b'E' => [0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b11111], + b'F' => [0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000], + b'G' => [0b01111, 0b10000, 0b10000, 0b10011, 0b10001, 0b10001, 0b01111], + b'H' => [0b10001, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001], + b'I' => [0b01110, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110], + b'J' => [0b00111, 0b00010, 0b00010, 0b00010, 0b00010, 0b10010, 0b01100], + b'K' => [0b10001, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001], + b'L' => [0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b11111], + b'M' => [0b10001, 0b11011, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001], + b'N' => [0b10001, 0b11001, 0b11001, 0b10101, 0b10011, 0b10011, 0b10001], + b'O' => [0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110], + b'P' => [0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000], + b'Q' => [0b01110, 0b10001, 0b10001, 0b10001, 0b10101, 0b10010, 0b01101], + b'R' => [0b11110, 0b10001, 0b10001, 0b11110, 0b10100, 0b10010, 0b10001], + b'S' => [0b01111, 0b10000, 0b10000, 0b01110, 0b00001, 0b00001, 0b11110], + b'T' => [0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100], + b'U' => [0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110], + b'V' => [0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100], + b'W' => [0b10001, 0b10001, 0b10001, 0b10101, 0b10101, 0b11011, 0b10001], + b'X' => [0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001], + b'Y' => [0b10001, 0b10001, 0b01010, 0b00100, 0b00100, 0b00100, 0b00100], + b'Z' => [0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111], + b'.' => [0, 0, 0, 0, 0, 0b01100, 0b01100], + b'/' => [0b00001, 0b00010, 0b00010, 0b00100, 0b01000, 0b01000, 0b10000], + b'-' => [0, 0, 0, 0b11111, 0, 0, 0], + b' ' => [0; 7], + _ => [0b11111, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111], + } +} + +/// The box's size in pixels, a dot being `scale` of them each way. +const fn size(scale: u32) -> (u32, u32) { + ((COLUMNS * CELL.0 + 2 * RIM) * scale, (LINES * CELL.1 + 2 * RIM) * scale) +} + +/// Pixels to a dot for a picture that high: a 720-line screenshot of it +/// shows one and a third. +fn scale(height: u32) -> u32 { + (height / 540).max(1) +} + +/// Whether a dot of the picture of `lines` is white. +fn lit(lines: &[String], x: u32, y: u32) -> bool { + let (Some(x), Some(y)) = (x.checked_sub(RIM), y.checked_sub(RIM)) else { + return false; + }; + if x % CELL.0 >= GLYPH.0 || y % CELL.1 >= GLYPH.1 || x / CELL.0 >= COLUMNS { + return false; + } + let Some(&c) = lines.get((y / CELL.1) as usize).and_then(|l| l.as_bytes().get((x / CELL.0) as usize)) else { + return false; + }; + (glyph(c.to_ascii_uppercase())[(y % CELL.1) as usize] >> (GLYPH.0 - 1 - x % CELL.0)) & 1 != 0 +} + +/// The bytes of a black and a white pixel of a swap chain's format. +fn colours(format: DXGI_FORMAT) -> Option<[&'static [u8]; 2]> { + Some(match format { + DXGI_FORMAT_R8G8B8A8_UNORM | DXGI_FORMAT_B8G8R8A8_UNORM => [&[0, 0, 0, 0xFF], &[0xFF; 4]], + DXGI_FORMAT_R10G10B10A2_UNORM => [&[0, 0, 0, 0xC0], &[0xFF; 4]], + // Halves of 0 and 1. + DXGI_FORMAT_R16G16B16A16_FLOAT => [ + &[0, 0, 0, 0, 0, 0, 0, 0x3C], + &[0, 0x3C, 0, 0x3C, 0, 0x3C, 0, 0x3C], + ], + _ => return None, + }) +} + +/// The box as rows of `pitch` bytes. +fn painted(lines: &[String], scale: u32, colours: [&[u8]; 2], pitch: usize) -> Vec { + let (width, height) = size(scale); + let bytes = colours[0].len(); + let mut out = vec![0; pitch * height as usize]; + for y in 0..height / scale { + let top = pitch * (y * scale) as usize; + for x in 0..width / scale { + let colour = colours[usize::from(lit(lines, x, y))]; + for i in 0..scale { + let at = top + (x * scale + i) as usize * bytes; + out[at..at + bytes].copy_from_slice(colour); + } + } + for i in 1..scale as usize { + out.copy_within(top..top + pitch, top + i * pitch); + } + } + out +} + +/// The counters read for it. They grow, and go back to 0 with the frame-rate +/// line (right behind a look of ours). +#[derive(Clone, Copy, Default)] +struct Counters([u64; COUNTERS]); + +const COUNTERS: usize = 16; + +impl Counters { + fn now() -> Counters { + use super::{CARD_CLOCK_US, COPY_BACK, PREPARING, SUBMISSION_US}; + use crate::copystat::{C, peek}; + let get = |a: &std::sync::atomic::AtomicU64| a.load(Ordering::Relaxed); + Counters([ + get(&SUBMISSION_US[0]), + get(&PREPARING[0]), + get(&PREPARING[1]), + get(&PREPARING[2]), + get(&PREPARING[3]), + get(©_BACK[0]), + get(©_BACK[1]), + get(&CARD_CLOCK_US[0]), + get(&CARD_CLOCK_US[1]), + get(&CARD_CLOCK_US[2]), + get(&crate::shadow::CARD_STATS[0]), + crate::draw::dropped_now(), + peek(C::RecordNs), + peek(C::RecordCalls), + peek(C::Submissions), + crate::draw::drawn(), + ]) + } + + /// What came since `before`, per frame. + fn since(self, before: Counters, frames: f64) -> [f64; COUNTERS] { + let mut out = [0.0; COUNTERS]; + for (i, out) in out.iter_mut().enumerate() { + let (was, is) = (before.0[i], self.0[i]); + *out = if is >= was { is - was } else { is } as f64 / frames; + } + out + } +} + +/// The text. `c`: [`Counters`] per frame, `ms` a frame's time, `worst` the +/// longest frame's. +fn text(c: &[f64; COUNTERS], ms: f64, worst: f64, host: &Host) -> Vec { + use crate::draw::{ + HEAP_TEXTURE_BYTES, HEAP_TEXTURES, HEAP_TEXTURES_AWAY, HEAP_TEXTURES_FULL, HEAP_TEXTURES_WAITING, + }; + let gb = |bytes: u64| bytes as f64 / (1u64 << 30) as f64; + let get = |a: &std::sync::atomic::AtomicU64| a.load(Ordering::Relaxed); + let (working, private) = crate::copystat::process_memory(); + let [budget, used, _, shared] = host.video_memory().unwrap_or_default(); + let (hits, misses) = crate::shader_cache::counts(); + let failed = get(&crate::pipeline::FAILED); + let idle = (c[7] / 1e3).min(ms); + vec![ + format!("FPS {:.1} FRAME {ms:.1} MS LONGEST {worst:.0}", 1e3 / ms.max(0.001)), + format!("DRAWS {:.0} DROPPED {:.1} SUBMITS {:.1}", c[15], c[11], c[14]), + format!("CPU PREP {:.1} MS COPY {:.1} TEXTURES {:.1}", c[0] / 1e3, c[1] / 1e3, c[3] / 1e3), + format!("RECORDING {:.0} MS IN {:.0} CALLS", c[12] / 1e6, c[13]), + format!("GPU BUSY {:.1} MS IDLE {idle:.1} BEHIND US {:.1}", ms - idle, c[8] / 1e3), + format!("WAIT FOR GPU {:.1} MS COPY BACK {:.1} MS", c[9] / 1e3, c[5] / 1e3), + format!("MB UP {:.1} ON CARD {:.1} TEX {:.1} BACK {:.1}", c[2] / 1e6, c[10] / 1e6, c[4] / 1e6, c[6] / 1e6), + format!("RAM PRIVATE {:.1} GB IN MEMORY {:.1} GB", gb(private), gb(working)), + format!("VRAM {:.1}/{:.1} GB SHARED {:.1} GB", gb(used), gb(budget), gb(shared)), + format!( + "TEXTURES {} {:.1} GB WAITING {}", + get(&HEAP_TEXTURES), + gb(get(&HEAP_TEXTURE_BYTES)), + get(&HEAP_TEXTURES_WAITING) + ), + format!( + "UNMAPPED {} NO ROOM {} VIEWS {}", + get(&HEAP_TEXTURES_AWAY), + get(&HEAP_TEXTURES_FULL), + get(&crate::descriptor::host::REFUSED_VIEWS) + ), + format!("SHADERS CACHED {hits} NEW {misses} FAILED {failed}"), + format!("PIPELINES QUEUED {}", crate::pipeline::making()), + format!("FRAME NO {}", crate::queue::presented()), + ] +} + +/// The buffers of a box that size and format. +struct Buffers { + format: DXGI_FORMAT, + scale: u32, + pitch: u32, + uploads: Vec<(ID3D12Resource, *mut u8)>, + card: ID3D12Resource, + /// The upload written last. + turn: usize, + /// It is not in the card's buffer yet. + fresh: bool, +} + +// SAFETY: free-threaded resources, and memory only written under the presenter's lock. +unsafe impl Send for Buffers {} + +impl Buffers { + fn new(host: &Host, format: DXGI_FORMAT, scale: u32) -> Option { + let (width, height) = size(scale); + let pitch = (width * colours(format)?[0].len() as u32).next_multiple_of(256); + let bytes = u64::from(pitch) * u64::from(height); + let uploads = (0..UPLOADS).map(|_| host.upload_buffer(bytes)).collect::>>()?; + let heap = D3D12_HEAP_PROPERTIES { + Type: D3D12_HEAP_TYPE_DEFAULT, + ..Default::default() + }; + let desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: bytes, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + ..Default::default() + }; + let mut card: Option = None; + // SAFETY: plain creation of a buffer. + unsafe { + host.device + .CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, D3D12_RESOURCE_STATE_COMMON, None, &mut card) + .ok()?; + } + Some(Buffers { format, scale, pitch, uploads, card: card?, turn: 0, fresh: false }) + } + + fn write(&mut self, lines: &[String]) { + let Some(colours) = colours(self.format) else { return }; + let picture = painted(lines, self.scale, colours, self.pitch as usize); + self.turn = (self.turn + 1) % UPLOADS; + // SAFETY: a mapped upload buffer of the picture's size, which the card read a second or more ago. + unsafe { std::ptr::copy_nonoverlapping(picture.as_ptr(), self.uploads[self.turn].1, picture.len()) }; + self.fresh = true; + } +} + +/// The panel of a presenter. +pub(super) struct Panel { + since: Instant, + frames: u32, + last: Instant, + worst: f64, + before: Counters, + lines: Vec, + changed: bool, + buffers: Option, + failed: bool, +} + +impl Panel { + pub(super) fn new() -> Panel { + let now = Instant::now(); + Panel { + since: now, + frames: 0, + last: now, + worst: 0.0, + before: Counters::default(), + lines: Vec::new(), + changed: false, + buffers: None, + failed: false, + } + } + + /// A frame was presented; `line`: the counters go back to 0 right behind + /// this call. + pub(super) fn frame(&mut self, line: bool) { + if pressed() { + shown().fetch_xor(true, Ordering::Relaxed); + } + let now = Instant::now(); + self.frames += 1; + self.worst = self.worst.max((now - self.last).as_secs_f64() * 1e3); + self.last = now; + let elapsed = now - self.since; + if elapsed < EVERY && !(line && elapsed * 2 >= EVERY) { + return; + } + let (frames, worst) = (f64::from(self.frames), self.worst); + (self.since, self.frames, self.worst) = (now, 0, 0.0); + if !shown().load(Ordering::Relaxed) { + return; + } + let Some(host) = super::host() else { return }; + let counters = Counters::now(); + let c = counters.since(self.before, frames); + self.before = counters; + self.lines = text(&c, elapsed.as_secs_f64() * 1e3 / frames, worst, host); + self.changed = true; + } + + /// Records the copy of the box onto `back`, a buffer of the swap chain of + /// that size and format in the state to be copied into. + pub(super) fn draw( + &mut self, + host: &Host, + list: &HostList, + back: &ID3D12Resource, + (width, height): (u32, u32), + format: DXGI_FORMAT, + ) { + if !shown().load(Ordering::Relaxed) || self.lines.is_empty() || self.failed { + return; + } + // Not the title's: the frame recorder leaves it out. + let _ours = crate::replay::record::unrecorded(); + let scale = scale(height); + if self.buffers.as_ref().is_none_or(|b| (b.format, b.scale) != (format, scale)) { + // (What the card may still read stays: a picture's size changes once or twice in a run.) + std::mem::forget(self.buffers.take()); + self.buffers = Buffers::new(host, format, scale); + self.failed = self.buffers.is_none(); + self.changed = true; + } + let Some(buffers) = &mut self.buffers else { return }; + if std::mem::take(&mut self.changed) { + buffers.write(&self.lines); + } + let (w, h) = size(scale); + let list = &list.list; + // SAFETY: an open list and live resources; the box lies within both. + unsafe { + if std::mem::take(&mut buffers.fresh) { + list.CopyResource(&buffers.card, &buffers.uploads[buffers.turn].0); + list.ResourceBarrier(&[transition( + &buffers.card, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_COPY_SOURCE, + )]); + } + let from = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(&buffers.card), + Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { + PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT { + Offset: 0, + Footprint: D3D12_SUBRESOURCE_FOOTPRINT { + Format: format, + Width: w, + Height: h, + Depth: 1, + RowPitch: buffers.pitch, + }, + }, + }, + }; + let to = D3D12_TEXTURE_COPY_LOCATION { + pResource: std::mem::transmute_copy(back), + Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, + Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 { SubresourceIndex: 0 }, + }; + let part = D3D12_BOX { + left: 0, + top: 0, + front: 0, + right: w.min(width), + bottom: h.min(height), + back: 1, + }; + list.CopyTextureRegion(&to, 0, 0, 0, &from, Some(&part)); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// Every character the lines use has its own picture, and no two look + /// alike. + #[test] + fn characters_differ() { + let used: Vec = (b'0'..=b'9').chain(b'A'..=b'Z').chain(*b"./- ").collect(); + for (i, a) in used.iter().enumerate() { + assert_ne!(glyph(*a), glyph(b'#'), "{} has a picture", *a as char); + assert!(glyph(*a).iter().all(|row| row >> GLYPH.0 == 0)); + for b in &used[i + 1..] { + assert_ne!(glyph(*a), glyph(*b), "{} {}", *a as char, *b as char); + } + } + } + + #[test] + fn a_box_is_painted() { + let lines = vec!["I".to_owned(), " i".to_owned()]; + for scale in [1, 2, 4] { + let (width, height) = size(scale); + let pitch = (width * 4).next_multiple_of(256) as usize; + let colours = colours(DXGI_FORMAT_R10G10B10A2_UNORM).unwrap(); + let picture = painted(&lines, scale, colours, pitch); + assert_eq!(picture.len(), pitch * height as usize); + let at = |x: u32, y: u32| &picture[y as usize * pitch + x as usize * 4..][..4]; + // The rim is black, the first letter's top row lit in its middle. + assert_eq!(at(0, 0), colours[0]); + assert_eq!(at((RIM + 2) * scale, RIM * scale), colours[1]); + assert_eq!(at((RIM + 2) * scale + scale - 1, RIM * scale + scale - 1), colours[1]); + assert_eq!(at(RIM * scale, RIM * scale), colours[0]); + // Small letters are shown as capitals, a line and a cell on. + assert_eq!(at((RIM + CELL.0 + 2) * scale, (RIM + CELL.1) * scale), colours[1]); + // Nothing past a line's end. + assert_eq!(at((RIM + CELL.0 * 2 + 2) * scale, (RIM + CELL.1) * scale), colours[0]); + } + } + + #[test] + fn counters_that_went_back_to_nothing_count_from_there() { + let (mut a, mut b) = (Counters::default(), Counters::default()); + (a.0[0], b.0[0]) = (10, 30); + (a.0[1], b.0[1]) = (50, 20); + let c = b.since(a, 10.0); + assert_eq!((c[0], c[1]), (2.0, 2.0)); + } +} diff --git a/crates/victory-gpu/src/pipeline.rs b/crates/victory-gpu/src/pipeline.rs index 7a00633..0945b4c 100644 --- a/crates/victory-gpu/src/pipeline.rs +++ b/crates/victory-gpu/src/pipeline.rs @@ -78,6 +78,9 @@ static SHADERS: Mutex>>> = Mutex::new(None) /// kept and the bytes of their host code. None of them is ever let go. static KEPT: [std::sync::atomic::AtomicU64; 4] = [const { std::sync::atomic::AtomicU64::new(0) }; 4]; +/// Shaders that did not translate. +pub(crate) static FAILED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + /// What the shader table and the pipeline library keep in memory: /// containers and their bytes, translations and their bytes, the bytes of /// the library's file (kept whole while the library lives) and what its @@ -158,6 +161,9 @@ impl Shader { KEPT[2].fetch_add(1, std::sync::atomic::Ordering::Relaxed); KEPT[3].fetch_add(t.host.dxil.len() as u64, std::sync::atomic::Ordering::Relaxed); } + if done.is_err() { + FAILED.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } match &done { Ok(t) => tracing::info!( stage = self.stage, diff --git a/crates/victory-gpu/src/replay/record.rs b/crates/victory-gpu/src/replay/record.rs index 232f7b7..70a1fa4 100644 --- a/crates/victory-gpu/src/replay/record.rs +++ b/crates/victory-gpu/src/replay/record.rs @@ -393,6 +393,12 @@ impl Drop for Ours { } } +/// While what this returns lives, this thread's D3D12 calls are not +/// recorded. +pub(crate) fn unrecorded() -> impl Drop { + Ours::new() +} + fn ours() -> bool { OURS.with(|o| o.get() > 0) }