feat(gpu/host): add native F12 backbuffer PNG screenshots (#276) #360

Merged
Androtim merged 2 commits from upstream-pr/360 into main 2026-12-02 19:36:27 +00:00
Androtim commented 2026-12-02 16:17:25 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T16:17:25Z.


Implements full-resolution native swapchain backbuffer screenshots triggered by pressing F12, part of #276.

Summary

  • Edge-triggered F12 capture: Polls VK_F12 on the foreground key thread with edge debouncing so holding F12 triggers one capture per press. Enabled by default; set VICTORY_SCREENSHOTS=0 to disable.
  • Zero-stall native swapchain readback: After the F3/menu/notice overlay draws onto the active swapchain backbuffer in present_planes, issues a CopyTextureRegion into a pooled ID3D12Resource readback staging buffer before transitioning to D3D12_RESOURCE_STATE_PRESENT.
  • Async background PNG encoding: Hands readback buffers off to a dedicated victory-screenshot worker thread, waits for GPU completion via ID3D12Fence without blocking the presenter, converts RGBA8 / BGRA8 / BGRX8 / R10G10B10A2 / RGBA16F backbuffers to 8-bit RGB PNG via zlib-rs, writes collision-safe minivi-YYYYMMDD-HHMMSS.png under <VICTORY_HOME>/screenshots (or %LOCALAPPDATA%/victory/screenshots), and logs one INFO line with filename and pixel dimensions.
  • Unit tests: Added tests in crates/victory-gpu/src/host/screenshot.rs covering format conversions, edge trigger debouncing, env gating, screenshot directory selection, and unique filename generation.

Verification (offline host checks)

  • cargo clippy -p victory-gpu --all-targets -- -D warnings: passed (0 warnings)
  • cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings: passed (0 warnings)
  • cargo test -p xtask: 8 passed, 0 failed
  • cargo xtask policy: 326 files ok
  • cargo test -p victory-gpu --lib -- --test-threads=1: 731 passed, 0 failed, 39 ignored (including all 8 host::screenshot::tests; ignored cases are pre-existing host-GPU/benchmark tests)
<!-- greyfinch-sync:Androtim/MiniVI:issue:5610410497 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/360). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T16:17:25Z. --- Implements full-resolution native swapchain backbuffer screenshots triggered by pressing `F12`, part of #276. ### Summary - **Edge-triggered F12 capture:** Polls `VK_F12` on the foreground key thread with edge debouncing so holding `F12` triggers one capture per press. Enabled by default; set `VICTORY_SCREENSHOTS=0` to disable. - **Zero-stall native swapchain readback:** After the `F3`/menu/notice overlay draws onto the active swapchain backbuffer in `present_planes`, issues a `CopyTextureRegion` into a pooled `ID3D12Resource` readback staging buffer before transitioning to `D3D12_RESOURCE_STATE_PRESENT`. - **Async background PNG encoding:** Hands readback buffers off to a dedicated `victory-screenshot` worker thread, waits for GPU completion via `ID3D12Fence` without blocking the presenter, converts `RGBA8` / `BGRA8` / `BGRX8` / `R10G10B10A2` / `RGBA16F` backbuffers to 8-bit RGB PNG via `zlib-rs`, writes collision-safe `minivi-YYYYMMDD-HHMMSS.png` under `<VICTORY_HOME>/screenshots` (or `%LOCALAPPDATA%/victory/screenshots`), and logs one `INFO` line with filename and pixel dimensions. - **Unit tests:** Added tests in `crates/victory-gpu/src/host/screenshot.rs` covering format conversions, edge trigger debouncing, env gating, screenshot directory selection, and unique filename generation. ### Verification (offline host checks) - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: passed (0 warnings) - `cargo clippy -p victory-gpu --all-targets --all-features -- -D warnings`: passed (0 warnings) - `cargo test -p xtask`: 8 passed, 0 failed - `cargo xtask policy`: 326 files ok - `cargo test -p victory-gpu --lib -- --test-threads=1`: 731 passed, 0 failed, 39 ignored (including all 8 `host::screenshot::tests`; ignored cases are pre-existing host-GPU/benchmark tests)
Androtim commented 2026-12-02 16:34:50 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T16:34:50Z.


Architect Review (CI 46925476715 GREEN, clean-room verified):

  • Clean F12 native backbuffer screenshot implementation (VICTORY_SCREENSHOTS=0 kill-switch, zero overhead when F12 is not pressed, back resource-state transitions COPY_DEST -> COPY_SOURCE -> PRESENT match both composed and non-composed paths in present_planes, RAII EventHandle + device-removed u64::MAX guards + 30s timeout, staging buffer dropped before PNG encode/disk I/O, atomic create_new filenames, 8/8 unit tests).
  • Ready for a quick live-GPU F12 smoke test (confirming full-res PNG with F3 overlay, correct RGB orientation, and zero D3D12 errors) before merge.
<!-- greyfinch-sync:Androtim/MiniVI:comment:7352152604 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/360#issuecomment-7352152604). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T16:34:50Z. --- Architect Review (CI `46925476715` **GREEN**, clean-room verified): - Clean `F12` native backbuffer screenshot implementation (`VICTORY_SCREENSHOTS=0` kill-switch, zero overhead when `F12` is not pressed, `back` resource-state transitions `COPY_DEST -> COPY_SOURCE -> PRESENT` match both composed and non-composed paths in `present_planes`, RAII `EventHandle` + device-removed `u64::MAX` guards + 30s timeout, staging buffer dropped before PNG encode/disk I/O, atomic `create_new` filenames, 8/8 unit tests). - Ready for a quick live-GPU `F12` smoke test (confirming full-res PNG with `F3` overlay, correct RGB orientation, and zero D3D12 errors) before merge.
Androtim commented 2026-12-02 17:05:35 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T17:05:35Z.


Automated triage for the maintainers (not an approval).

Verdict: needs-work · CI (check): passing

Overall this is a clean, well-scoped implementation of native F12 backbuffer PNG screenshots in crates/victory-gpu/src/host.rs and crates/victory-gpu/src/host/screenshot.rs. The resource state transitions in present_planes (COPY_DEST -> COPY_SOURCE -> PRESENT), rising-edge key debouncing, format conversions, collision-safe PNG writing, and unit tests all look solid.

There are two concrete synchronization bugs in wait_for_fence (crates/victory-gpu/src/host/screenshot.rs) that should be fixed before merging:

  1. Premature readback on stale EventHandle signal: worker_loop reuses a single auto-reset EventHandle across jobs. If a previous job returns via WAIT_TIMEOUT (after now >= target) or times out after 30s and the GPU signals event afterward, event remains signaled for the next job (SetEventOnCompletion does not reset it). In wait_for_fence, the WAIT_OBJECT_0 arm returns fence.GetCompletedValue() != u64::MAX without checking >= target, so process_job can Map and read pending.buffer before the GPU copy has finished.
  2. Unreachable device-removed check in the polling fallback: In the let Some(event) = event else loop, while fence.GetCompletedValue() < target immediately exits when GetCompletedValue() == u64::MAX (since u64::MAX < target is false), making the inner == u64::MAX check unreachable and returning true instead of false on device removal.

Minor note

  • In crates/victory-gpu/src/host.rs (present_planes), when slot is Some(_) and execute_own_with_fence returns (0, None) (if submit fell back to synchronous execution), screenshot::submit is skipped and the capture is silently dropped. Consider confirming whether None means synchronous completion vs submission failure, or unifying the match on fence_value.

To change the verdict:

  1. In wait_for_fence (crates/victory-gpu/src/host/screenshot.rs), on WAIT_OBJECT_0, verify let now = fence.GetCompletedValue(); is neither u64::MAX (return false) nor < target (keep waiting / re-arm if stale) before returning true.
  2. In the event == None polling fallback of wait_for_fence, check let now = fence.GetCompletedValue(); if now == u64::MAX { return false; } if now >= target { return true; } inside a loop so device removal returns false.
  3. Once fixed, run a quick local real-GPU smoke test (pressing F12 in-game with and without the F3 overlay/menu, plus rapid presses and VICTORY_SCREENSHOTS=0) to verify the D3D12 backbuffer readback and PNG output end-to-end.

Generated with Antigravity CLI (agy).

<!-- greyfinch-sync:Androtim/MiniVI:comment:7352887718 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/360#issuecomment-7352887718). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T17:05:35Z. --- <!-- minivi-triage v1 head=6a8d28d528f6f9ab5d53e93d7fc591c2765405f9 verdict=needs-work ci=passing at=2026-12-02T17:06:07Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-work` · **CI (`check`):** `passing` Overall this is a clean, well-scoped implementation of native `F12` backbuffer PNG screenshots in `crates/victory-gpu/src/host.rs` and `crates/victory-gpu/src/host/screenshot.rs`. The resource state transitions in `present_planes` (`COPY_DEST -> COPY_SOURCE -> PRESENT`), rising-edge key debouncing, format conversions, collision-safe PNG writing, and unit tests all look solid. There are two concrete synchronization bugs in `wait_for_fence` (`crates/victory-gpu/src/host/screenshot.rs`) that should be fixed before merging: 1. **Premature readback on stale `EventHandle` signal:** `worker_loop` reuses a single auto-reset `EventHandle` across jobs. If a previous job returns via `WAIT_TIMEOUT` (after `now >= target`) or times out after 30s and the GPU signals `event` afterward, `event` remains signaled for the next job (`SetEventOnCompletion` does not reset it). In `wait_for_fence`, the `WAIT_OBJECT_0` arm returns `fence.GetCompletedValue() != u64::MAX` without checking `>= target`, so `process_job` can `Map` and read `pending.buffer` before the GPU copy has finished. 2. **Unreachable device-removed check in the polling fallback:** In the `let Some(event) = event else` loop, `while fence.GetCompletedValue() < target` immediately exits when `GetCompletedValue() == u64::MAX` (since `u64::MAX < target` is `false`), making the inner `== u64::MAX` check unreachable and returning `true` instead of `false` on device removal. ### Minor note - In `crates/victory-gpu/src/host.rs` (`present_planes`), when `slot` is `Some(_)` and `execute_own_with_fence` returns `(0, None)` (if `submit` fell back to synchronous execution), `screenshot::submit` is skipped and the capture is silently dropped. Consider confirming whether `None` means synchronous completion vs submission failure, or unifying the match on `fence_value`. ### To change the verdict: 1. In `wait_for_fence` (`crates/victory-gpu/src/host/screenshot.rs`), on `WAIT_OBJECT_0`, verify `let now = fence.GetCompletedValue();` is neither `u64::MAX` (return `false`) nor `< target` (keep waiting / re-arm if stale) before returning `true`. 2. In the `event == None` polling fallback of `wait_for_fence`, check `let now = fence.GetCompletedValue(); if now == u64::MAX { return false; } if now >= target { return true; }` inside a `loop` so device removal returns `false`. 3. Once fixed, run a quick local real-GPU smoke test (pressing `F12` in-game with and without the `F3` overlay/menu, plus rapid presses and `VICTORY_SCREENSHOTS=0`) to verify the D3D12 backbuffer readback and PNG output end-to-end. --- _Generated with Antigravity CLI (`agy`)._
Androtim commented 2026-12-02 19:36:24 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T19:36:24Z.


Verified 5c67b199 resolves Audit Pass #3 Finding #12 (and both minivi-triage wait_for_fence notes: ResetEvent before SetEventOnCompletion, re-checking GetCompletedValue() >= target + u64::MAX device-removal detection across both event and polling paths, bounded 30s timeout, plus FakeFence unit tests). CI 46952425065 is GREEN, zero runtime overhead when F12 is unused, VICTORY_SCREENSHOTS=0 kill-switch. Merging to main.

<!-- greyfinch-sync:Androtim/MiniVI:comment:7356438896 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/360#issuecomment-7356438896). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T19:36:24Z. --- Verified `5c67b199` resolves Audit Pass #3 Finding `#12` (and both `minivi-triage` `wait_for_fence` notes: `ResetEvent` before `SetEventOnCompletion`, re-checking `GetCompletedValue() >= target` + `u64::MAX` device-removal detection across both event and polling paths, bounded 30s timeout, plus `FakeFence` unit tests). CI `46952425065` is GREEN, zero runtime overhead when `F12` is unused, `VICTORY_SCREENSHOTS=0` kill-switch. Merging to `main`.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Androtim/MiniVI!360
No description provided.