| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525 |
- //! Presented frames written to PNG files, to see what reaches the screen:
- //! with `VICTORY_CAPTURE=<folder>`, every frame whose number is a power of
- //! two (the frames the log reports) is saved as `frame-<n>.png`, at half
- //! size. Only in builds with diagnostics.
- use std::path::PathBuf;
- use windows::Win32::Graphics::Direct3D12::*;
- use windows::Win32::Graphics::Dxgi::Common::*;
- /// The folder frames go to, if capturing.
- pub(crate) fn folder() -> Option<&'static PathBuf> {
- static FOLDER: std::sync::OnceLock<Option<PathBuf>> = std::sync::OnceLock::new();
- FOLDER
- .get_or_init(|| std::env::var_os("VICTORY_CAPTURE").map(PathBuf::from))
- .as_ref()
- }
- /// Frames listed in `VICTORY_CAPTURE_FRAMES` (comma-separated): saved
- /// too, and their draws traced at debug level.
- pub(crate) fn listed(n: u64) -> bool {
- static LIST: std::sync::OnceLock<Vec<u64>> = std::sync::OnceLock::new();
- LIST.get_or_init(|| {
- let list = std::env::var("VICTORY_CAPTURE_FRAMES").unwrap_or_default();
- list.split(',')
- .filter_map(|f| f.trim().parse().ok())
- .collect()
- })
- .contains(&n)
- }
- /// Whether frame `n` is saved.
- pub(crate) fn wanted(n: u64) -> bool {
- folder().is_some() && (n.is_power_of_two() || listed(n))
- }
- /// Saves 8-bit RGBA or BGRA rows (`pitch` bytes apart) as frame `n`.
- pub(crate) fn save(n: u64, width: u32, height: u32, pitch: usize, bgra: bool, rows: &[u8]) {
- save_as(&format!("frame-{n}"), width, height, pitch, bgra, rows);
- }
- /// Saves 8-bit RGBA or BGRA rows as `<name>.png`, at half size.
- fn save_as(name: &str, width: u32, height: u32, pitch: usize, bgra: bool, rows: &[u8]) {
- let Some(folder) = folder() else { return };
- let (w, h) = ((width / 2).max(1), (height / 2).max(1));
- // Filter byte 0 per row, then RGB of every other pixel of every other row.
- let mut raw = Vec::with_capacity((w as usize * 3 + 1) * h as usize);
- for y in 0..h as usize {
- raw.push(0);
- let row = &rows[(2 * y).min(height as usize - 1) * pitch..];
- for x in 0..w as usize {
- let p = &row[(2 * x).min(width as usize - 1) * 4..][..3];
- if bgra {
- raw.extend([p[2], p[1], p[0]]);
- } else {
- raw.extend_from_slice(p);
- }
- }
- }
- let mut png = b"\x89PNG\r\n\x1a\n".to_vec();
- let mut header = Vec::new();
- header.extend(w.to_be_bytes());
- header.extend(h.to_be_bytes());
- header.extend([8, 2, 0, 0, 0]); // 8-bit RGB
- chunk(&mut png, b"IHDR", &header);
- chunk(
- &mut png,
- b"IDAT",
- &miniz_oxide::deflate::compress_to_vec_zlib(&raw, 3),
- );
- chunk(&mut png, b"IEND", &[]);
- let path = folder.join(format!("{name}.png"));
- match std::fs::write(&path, png) {
- Ok(()) => tracing::info!(path = %path.display(), "frame saved"),
- Err(e) => tracing::warn!(error = %e, path = %path.display(), "frame not saved"),
- }
- }
- fn chunk(png: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
- png.extend((data.len() as u32).to_be_bytes());
- let start = png.len();
- png.extend(kind);
- png.extend(data);
- let crc = crc32(&png[start..]);
- png.extend(crc.to_be_bytes());
- }
- fn crc32(bytes: &[u8]) -> u32 {
- let mut crc = !0u32;
- for &b in bytes {
- crc ^= u32::from(b);
- for _ in 0..8 {
- crc = (crc >> 1) ^ (0xedb8_8320 & (crc & 1).wrapping_neg());
- }
- }
- !crc
- }
- /// Records a copy of `texture` (in the copy-source state) into a
- /// readback buffer.
- pub(crate) fn read_back(
- device: &ID3D12Device,
- list: &ID3D12GraphicsCommandList,
- texture: &ID3D12Resource,
- ) -> Option<(ID3D12Resource, D3D12_PLACED_SUBRESOURCE_FOOTPRINT)> {
- let mut footprint = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
- let mut total = 0;
- // SAFETY: a live texture; outputs for one subresource.
- unsafe {
- let desc = texture.GetDesc();
- device.GetCopyableFootprints(
- &desc,
- 0,
- 1,
- 0,
- Some(&mut footprint),
- None,
- None,
- Some(&mut total),
- );
- }
- let heap = D3D12_HEAP_PROPERTIES {
- Type: D3D12_HEAP_TYPE_READBACK,
- ..Default::default()
- };
- let desc = D3D12_RESOURCE_DESC {
- Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
- Width: total,
- Height: 1,
- DepthOrArraySize: 1,
- MipLevels: 1,
- SampleDesc: DXGI_SAMPLE_DESC {
- Count: 1,
- Quality: 0,
- },
- Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
- ..Default::default()
- };
- let mut buffer: Option<ID3D12Resource> = None;
- // SAFETY: plain creation; an open list; locations naming live resources.
- unsafe {
- device
- .CreateCommittedResource(
- &heap,
- D3D12_HEAP_FLAG_NONE,
- &desc,
- D3D12_RESOURCE_STATE_COPY_DEST,
- None,
- &mut buffer,
- )
- .ok()?;
- let buffer = buffer?;
- let to = D3D12_TEXTURE_COPY_LOCATION {
- pResource: std::mem::transmute_copy(&buffer),
- Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
- Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
- PlacedFootprint: footprint,
- },
- };
- let from = D3D12_TEXTURE_COPY_LOCATION {
- pResource: std::mem::transmute_copy(texture),
- Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
- Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
- SubresourceIndex: 0,
- },
- };
- list.CopyTextureRegion(&to, 0, 0, 0, &from, None);
- Some((buffer, footprint))
- }
- }
- /// Saves a frame read back by [`read_back`] (8-bit color only).
- pub(crate) fn save_read_back(
- frame: u64,
- buffer: &ID3D12Resource,
- footprint: &D3D12_PLACED_SUBRESOURCE_FOOTPRINT,
- ) {
- let f = footprint.Footprint;
- let bgra = match f.Format {
- DXGI_FORMAT_R8G8B8A8_UNORM | DXGI_FORMAT_R8G8B8A8_UNORM_SRGB => false,
- DXGI_FORMAT_B8G8R8A8_UNORM | DXGI_FORMAT_B8G8R8A8_UNORM_SRGB => true,
- other => {
- tracing::warn!(format = other.0, "frame not saved: not 8-bit color");
- return;
- }
- };
- let mut data = std::ptr::null_mut();
- // SAFETY: the copy finished (every submission is waited for); the
- // mapping spans the footprint's rows.
- unsafe {
- if buffer.Map(0, None, Some(&mut data)).is_err() {
- return;
- }
- let len = f.RowPitch as usize * f.Height as usize;
- let rows = std::slice::from_raw_parts(data.cast::<u8>(), len);
- save(frame, f.Width, f.Height, f.RowPitch as usize, bgra, rows);
- buffer.Unmap(0, None);
- }
- }
- /// With `VICTORY_DUMP_TARGETS` set: title textures the frames listed in
- /// `VICTORY_CAPTURE_FRAMES` render to or write, in first-use order, saved
- /// at that frame's present as `frame-<n>-<i>-<resource>-<w>x<h>-f<format>.png`
- /// (what each holds at the end of the frame; formats normalized to show
- /// structure: floats scaled by their largest value, depth by its range).
- static TARGETS: std::sync::Mutex<Vec<usize>> = std::sync::Mutex::new(Vec::new());
- fn dump_on() -> bool {
- static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
- *ON.get_or_init(|| folder().is_some() && std::env::var_os("VICTORY_DUMP_TARGETS").is_some())
- }
- /// Notes a title texture the frame being built renders to or writes.
- pub(crate) fn note_target(resource: crate::object::This) {
- if resource.is_null() || !dump_on() || !listed(crate::queue::presented() + 1) {
- return;
- }
- let mut targets = TARGETS.lock().unwrap_or_else(|e| e.into_inner());
- if !targets.contains(&(resource as usize)) {
- targets.push(resource as usize);
- }
- }
- /// At the present of frame `frame`: saves the noted targets.
- pub(crate) fn dump_targets(frame: u64) {
- let targets = std::mem::take(&mut *TARGETS.lock().unwrap_or_else(|e| e.into_inner()));
- let Some(host) = crate::host::host() else { return };
- for (i, &resource) in targets.iter().enumerate() {
- let resource = resource as crate::object::This;
- let Some(texture) = crate::resource::host_resource(resource) else {
- continue;
- };
- // SAFETY: a live resource.
- let d = unsafe { texture.GetDesc() };
- if d.SampleDesc.Count > 1 || d.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D {
- continue;
- }
- const COPY_SOURCE: u32 = 0x800;
- let Some(before) = crate::resource::host_transition(resource, COPY_SOURCE).map(|(_, b)| b)
- else {
- dump_one(host, texture, COPY_SOURCE, frame, i, resource, &d);
- continue;
- };
- dump_one(host, texture, before, frame, i, resource, &d);
- crate::resource::host_transition(resource, before);
- }
- }
- fn dump_one(
- host: &crate::host::Host,
- texture: &ID3D12Resource,
- state: u32,
- frame: u64,
- i: usize,
- resource: crate::object::This,
- d: &D3D12_RESOURCE_DESC,
- ) {
- let Ok(list) = host.new_list() else { return };
- let state = D3D12_RESOURCE_STATES(crate::host::legal_state(texture, state) as i32);
- let copy = D3D12_RESOURCE_STATE_COPY_SOURCE;
- // SAFETY: an open list of ours and a live texture.
- let read = unsafe {
- if state != copy {
- list.list
- .ResourceBarrier(&[crate::host::transition(texture, state, copy)]);
- }
- let read = read_back(&host.device, &list.list, texture);
- if state != copy {
- list.list
- .ResourceBarrier(&[crate::host::transition(texture, copy, state)]);
- }
- read
- };
- list.close();
- host.execute(&[&list]);
- let Some((buffer, footprint)) = read else { return };
- let f = footprint.Footprint;
- let mut data = std::ptr::null_mut();
- // SAFETY: the copy finished; the mapping spans the footprint's rows.
- unsafe {
- if buffer.Map(0, None, Some(&mut data)).is_err() {
- return;
- }
- let pitch = f.RowPitch as usize;
- let rows = std::slice::from_raw_parts(data.cast::<u8>(), pitch * f.Height as usize);
- if let Some(rgba) = to_rgba(f.Format.0 as u32, f.Width as usize, f.Height as usize, pitch, rows) {
- let name = format!(
- "frame-{frame}-{i:02}-{resource:?}-{}x{}-f{}",
- d.Width, d.Height, d.Format.0
- );
- save_as(&name, f.Width, f.Height, f.Width as usize * 4, false, &rgba);
- } else {
- tracing::info!(format = f.Format.0, ?resource, "target not dumped: format");
- }
- buffer.Unmap(0, None);
- }
- }
- fn half(h: u16) -> f32 {
- let (sign, exp, man) = ((h >> 15) as u32, ((h >> 10) & 0x1f) as i32, (h & 0x3ff) as f32);
- let v = match exp {
- 0 => man * 2f32.powi(-24),
- 31 => f32::INFINITY,
- e => (1.0 + man / 1024.0) * 2f32.powi(e - 15),
- };
- if sign == 1 { -v } else { v }
- }
- /// A float packed in `bits` with a 5-bit exponent and `man` mantissa bits
- /// (the 11- and 10-bit floats of R11G11B10).
- fn small_float(bits: u32, man: u32) -> f32 {
- let (exp, m) = ((bits >> man) & 0x1f, (bits & ((1 << man) - 1)) as f32);
- match exp {
- 0 => m / (1 << man) as f32 * 2f32.powi(-14),
- 31 => f32::INFINITY,
- e => (1.0 + m / (1 << man) as f32) * 2f32.powi(e as i32 - 15),
- }
- }
- /// Rows of a host format as 8-bit RGBA: 8-bit color as is, everything else
- /// as channel values scaled so the largest finite one is white (single
- /// channels: lowest black, highest white, as grey). `None` for formats not
- /// handled.
- fn to_rgba(format: u32, w: usize, h: usize, pitch: usize, rows: &[u8]) -> Option<Vec<u8>> {
- let mut out = Vec::with_capacity(w * h * 4);
- let px = |x: usize, y: usize, n: usize| &rows[y * pitch + x * n..][..n];
- let u16_at = |b: &[u8], i: usize| u16::from_le_bytes([b[2 * i], b[2 * i + 1]]);
- let u32_at = |b: &[u8], i: usize| u32::from_le_bytes(b[4 * i..4 * i + 4].try_into().unwrap_or([0; 4]));
- // Channel values as floats, then scaled.
- type Decode = Box<dyn Fn(&[u8]) -> [f32; 3]>;
- let (n, decode): (usize, Decode) = match format {
- 27..=32 => {
- for y in 0..h {
- for x in 0..w {
- let p = px(x, y, 4);
- out.extend([p[0], p[1], p[2], 255]);
- }
- }
- return Some(out);
- }
- 87..=93 => {
- for y in 0..h {
- for x in 0..w {
- let p = px(x, y, 4);
- out.extend([p[2], p[1], p[0], 255]);
- }
- }
- return Some(out);
- }
- 23..=25 => (4, Box::new(move |b| {
- let v = u32_at(b, 0);
- [(v & 0x3ff) as f32, (v >> 10 & 0x3ff) as f32, (v >> 20 & 0x3ff) as f32]
- })),
- 26 => (4, Box::new(move |b| {
- let v = u32_at(b, 0);
- [small_float(v & 0x7ff, 6), small_float(v >> 11 & 0x7ff, 6), small_float(v >> 22, 5)]
- })),
- 9..=11 => (8, Box::new(move |b| [half(u16_at(b, 0)), half(u16_at(b, 1)), half(u16_at(b, 2))])),
- 33..=34 => (4, Box::new(move |b| [half(u16_at(b, 0)), half(u16_at(b, 1)), 0.0])),
- 1..=2 => (16, Box::new(move |b| {
- [0, 1, 2].map(|i| f32::from_bits(u32_at(b, i)))
- })),
- 15..=16 => (8, Box::new(move |b| [f32::from_bits(u32_at(b, 0)), f32::from_bits(u32_at(b, 1)), 0.0])),
- 35..=38 => (4, Box::new(move |b| [f32::from(u16_at(b, 0)), f32::from(u16_at(b, 1)), 0.0])),
- 48..=52 => (2, Box::new(move |b| [f32::from(b[0]), f32::from(b[1]), 0.0])),
- // Single channels: shown as grey.
- 39..=43 => (4, Box::new(move |b| [f32::from_bits(u32_at(b, 0)); 3])),
- 53..=54 => (2, Box::new(move |b| [half(u16_at(b, 0)); 3])),
- 55..=59 => (2, Box::new(move |b| [f32::from(u16_at(b, 0)); 3])),
- 60..=65 => (1, Box::new(move |b| [f32::from(b[0]); 3])),
- _ => return None,
- };
- let values: Vec<[f32; 3]> = (0..h)
- .flat_map(|y| (0..w).map(move |x| (x, y)))
- .map(|(x, y)| decode(px(x, y, n)))
- .collect();
- let grey = matches!(format, 39..=43 | 53..=65);
- let finite = values.iter().flatten().copied().filter(|v| v.is_finite());
- let (low, high) = if grey {
- finite.fold((f32::MAX, f32::MIN), |(l, h), v| (l.min(v), h.max(v)))
- } else {
- (0.0, finite.fold(0.0f32, f32::max))
- };
- let range = (high - low).max(1e-20);
- tracing::info!(format, low, high, "target values");
- for v in values {
- let c = v.map(|c| (((c - low) / range).clamp(0.0, 1.0) * 255.0) as u8);
- out.extend([c[0], c[1], c[2], 255]);
- }
- Some(out)
- }
- /// Pixels per side of the grid [`probe`] reads.
- const GRID: u32 = 4;
- /// Bytes between the grid's pixels in the readback buffer (placement
- /// alignment).
- const SLOT: u64 = 512;
- /// Records copies of a grid of pixels of `texture` (8-bit color, in the
- /// copy-source state) into a readback buffer, to tell black frames.
- pub(crate) fn probe(
- device: &ID3D12Device,
- list: &ID3D12GraphicsCommandList,
- texture: &ID3D12Resource,
- ) -> Option<ID3D12Resource> {
- // SAFETY: a live resource.
- let d = unsafe { texture.GetDesc() };
- if !matches!(d.Format.0, 27..=29 | 87 | 90 | 91) {
- return None;
- }
- let desc = D3D12_RESOURCE_DESC {
- Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
- Width: SLOT * u64::from(GRID * GRID),
- Height: 1,
- DepthOrArraySize: 1,
- MipLevels: 1,
- SampleDesc: DXGI_SAMPLE_DESC {
- Count: 1,
- Quality: 0,
- },
- Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
- ..Default::default()
- };
- let heap = D3D12_HEAP_PROPERTIES {
- Type: D3D12_HEAP_TYPE_READBACK,
- ..Default::default()
- };
- let mut buffer: Option<ID3D12Resource> = None;
- // SAFETY: plain creation; an open list; locations naming live resources.
- unsafe {
- device
- .CreateCommittedResource(
- &heap,
- D3D12_HEAP_FLAG_NONE,
- &desc,
- D3D12_RESOURCE_STATE_COPY_DEST,
- None,
- &mut buffer,
- )
- .ok()?;
- let buffer = buffer?;
- for i in 0..GRID * GRID {
- let (x, y) = (
- (2 * (i % GRID) + 1) * d.Width as u32 / (2 * GRID),
- (2 * (i / GRID) + 1) * d.Height / (2 * GRID),
- );
- let to = D3D12_TEXTURE_COPY_LOCATION {
- pResource: std::mem::transmute_copy(&buffer),
- Type: D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
- Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
- PlacedFootprint: D3D12_PLACED_SUBRESOURCE_FOOTPRINT {
- Offset: SLOT * u64::from(i),
- Footprint: D3D12_SUBRESOURCE_FOOTPRINT {
- Format: d.Format,
- Width: 1,
- Height: 1,
- Depth: 1,
- RowPitch: 256,
- },
- },
- },
- };
- let from = D3D12_TEXTURE_COPY_LOCATION {
- pResource: std::mem::transmute_copy(texture),
- Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
- Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
- SubresourceIndex: 0,
- },
- };
- let pixel = D3D12_BOX {
- left: x,
- top: y,
- front: 0,
- right: x + 1,
- bottom: y + 1,
- back: 1,
- };
- list.CopyTextureRegion(&to, 0, 0, 0, &from, Some(&pixel));
- }
- Some(buffer)
- }
- }
- /// Whether every pixel [`probe`] read is (near) black.
- pub(crate) fn black(buffer: &ID3D12Resource) -> bool {
- let mut data = std::ptr::null_mut();
- // SAFETY: the copies finished (every submission is waited for).
- unsafe {
- if buffer.Map(0, None, Some(&mut data)).is_err() {
- return false;
- }
- let bytes =
- std::slice::from_raw_parts(data.cast::<u8>(), (SLOT * u64::from(GRID * GRID)) as usize);
- let level = bytes
- .chunks(SLOT as usize)
- .flat_map(|pixel| pixel[..3].iter().copied())
- .max()
- .unwrap_or(0);
- buffer.Unmap(0, None);
- let mut levels = LEVELS.lock().unwrap_or_else(|e| e.into_inner());
- if levels.len() == 12 {
- levels.pop_front();
- }
- levels.push_back(level);
- level < 8
- }
- }
- /// The brightest probed channel of the last frames, oldest first: a fade
- /// darkens over several, a glitch drops at once.
- static LEVELS: std::sync::Mutex<std::collections::VecDeque<u8>> =
- std::sync::Mutex::new(std::collections::VecDeque::new());
- pub(crate) fn recent_levels() -> Vec<u8> {
- let levels = LEVELS.lock().unwrap_or_else(|e| e.into_inner());
- levels.iter().copied().collect()
- }
- #[cfg(test)]
- mod tests {
- #[test]
- fn crc_matches_the_png_check_value() {
- assert_eq!(super::crc32(b"IEND"), 0xae42_6082);
- }
- }
|