Skip to content

display: take the screenshot the title asks for - #106

Open
xiaotu030202 wants to merge 1 commit into
Zettrox:mainfrom
xiaotu030202:screenshot-from-the-title
Open

xiaotu030202 wants to merge 1 commit into
Zettrox:mainfrom
xiaotu030202:screenshot-from-the-title

Conversation

@xiaotu030202

@xiaotu030202 xiaotu030202 commented Nov 25, 2026 •

Copy link
Copy Markdown

take_screenshot answered NOT_SUPPORTED with tracing::info!("screenshot not taken"), so the title's own screenshot path could never take one. The comment beside it described exactly what was missing:

// shortcut: screenshots fail as if capture were unavailable. Taking one needs
// a hook in the GPU side: the last presented frame read back as BGRA8 (size,
// row pitch, bytes) on request, e.g. `victory_gpu::last_frame() ->
// Option<Frame>`, copied from the back buffer after `PresentX`.

That is now there.

What is in it

victory-gpu/src/screenshot.rs (new) — the ask queue, the read-back, and the fill:

  • ask() queues a placeholder; wanted() says one is outstanding; a third ask before any frame is presented drops the oldest, so a title asking in a loop cannot grow the queue.
  • read_back() copies the frame into a readback buffer on the copy list the present already runs.
  • fill() maps it after the submission has run and converts RGBA to BGRA when the swap format is RGBA.

host.rs — present treats an outstanding ask like a saved frame: it runs on its own list rather than being sent ahead, and fills the ask before the swap chain's Present.

display.rs — writes the bytes where the title asked, and fills in width/height/pitch.

Why not share capture.rs

It already reads a texture back for its frame saves, but it is #[cfg(all(windows, feature = "diagnostics"))] and its job is writing PNGs into a folder. The title asks for a screenshot in every build, and what it needs is the bytes in its own buffer, not a file. Sharing it would mean compiling capture without its switches and splitting what remains; the read-back is about forty lines, so it lives beside it rather than inside it. Nothing in capture.rs changed.

The shape the title sees

CAPTURE_VTABLE's 0x28, (this, user, result):

#[repr(C)]
struct Shot {
    width: u32, height: u32, pitch: u32, pad: u32,
    bytes: *mut u8, capacity: usize,
}

The call fills width, height and pitch before returning and copies the frame into bytes, which the title sized by capacity. A null result, null buffer, or a capacity below pitch * height is refused — E_INVALIDARG, E_NOT_SUFFICIENT_BUFFER — rather than written past.

If that layout is not what the title passes, the failure is an immediate refusal, not a bad write, and the log names which check failed. A wrong guess here is safe to be wrong about.

Formats other than 8-bit colour drop the ask rather than leaving it outstanding (so later frames are not read back for nothing) and report NOT_SUPPORTED after the timeout, which is the old behaviour.

Waiting

take_blocking polls every 2 ms for up to two seconds. A condition variable would need the presenting thread to take the same mutex while it holds the swap chain's — a lock order I did not want to introduce for one feature. The poll only runs while a title is waiting.

Verification

Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK 10.0.26100.0. No game files — so this is not evidence that the title's screenshot works end to end; it is evidence that nothing else broke and the queue behaves.

$ cargo test --workspace -- --test-threads=1
victory-shader 210 passed; victory-gpu 110 passed; … 0 failures anywhere

$ cargo clippy -p victory-gpu -p victory-hle --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok

One new test, screenshot::tests::asks_queue_and_are_taken_oldest_first, covers what needs no device: an ask is wanted until a frame fills it, an unfilled ask is not a frame, take gives the oldest filled one, and a third ask drops the oldest. The read-back and the mapping need a real device and are left to the title's calls.

Not done

0x18 (diagnostic screenshot) and 0x20 (diagnostic clip) are still missing. Those are the developer-capture interfaces — they write files rather than take a frame, and capture.rs already has most of what they need behind its switches. A different change.

If someone can run the title and report what the log says at a screenshot (screenshot taken with the size, or not taken: no frame presented, or a refusal), that would settle the layout question above without needing to guess.


Added after the first push: the conversion is tested on its own

380 lines of new module for one call is a lot to review, so the part that can be held without a device is now a pure function with tests beside it. ill maps the readback and hands the rows to o_bgra, which either copies (the swap chain already presents BGRA) or swaps red and blue four bytes at a time.

screenshot::tests::rgba_rows_arrive_as_bgra pins:

  • one RGBA texel (1,2,3,4) becomes (3,2,1,4);
  • two texels, so the chunks line up across a row and a misaligned iterator would show;
  • BGRA in stays BGRA (not swapped back — the mistake that produces blue faces);
  • a trailing partial texel comes back as a zero texel rather than shifting the row;
  • empty in, empty out.

cargo test -p victory-gpu --lib is 112 passed, 0 failed; cargo clippy -p victory-gpu -p victory-hle --all-targets is clean; cargo xtask policy is 172 files ok.

`take_screenshot` answered `NOT_SUPPORTED` with `tracing::info!("screenshot not
taken")`, so the title's own screenshot path could never take one. The
comment beside it described what was missing: "the last presented frame read
back as BGRA8 (size, row pitch, bytes) on request … copied from the back
buffer after `PresentX`".

That is now there. `victory-gpu/src/screenshot.rs` holds the ask queue and the
read-back; `host::present` notices an ask and reads the frame on the copy list
it already runs, before the swap chain's `Present`; `display.rs` writes the
bytes where the title asked.

## Why a module of its own

`capture.rs` already reads a texture back for its frame saves, but it is
`#[cfg(all(windows, feature = "diagnostics"))]` and its job is writing PNGs
into a folder. The title asks for a screenshot in **every** build, and what it
needs is the bytes in its own buffer, not a file. Sharing `capture` would
mean compiling it without its switches and splitting what is left; the
read-back is about forty lines, so it lives beside it instead of inside it.

The queue it needs is small and worth its own comment: an ask is a placeholder
with no rows, a present fills the oldest one in, and `take` hands back the
oldest filled one. A third ask before any frame is presented drops the oldest,
so a title asking in a loop cannot grow it.

## The shape the title sees

`CAPTURE_VTABLE`'s `0x28`, `(this, user, result)`:

```rust
#[repr(C)]
struct Shot {
    width: u32, height: u32, pitch: u32, pad: u32,
    bytes: *mut u8, capacity: usize,
}
```

The call fills in `width`, `height` and `pitch` before returning and copies
the frame into `bytes`, which the title sized by `capacity`. A null result,
null buffer, or a capacity below `pitch * height` is refused rather than
written past — `E_INVALIDARG` and `E_NOT_SUFFICIENT_BUFFER`.

Formats other than 8-bit colour cannot be read back as a screenshot: the ask
is dropped rather than left waiting for every later frame, and the call
reports `NOT_SUPPORTED` after its timeout, which is what it did before.

## Waiting

`take_blocking` polls for up to two seconds. A condition variable would need
the presenting thread to take the same mutex while it holds the swap chain's,
which is a lock order I did not want to introduce for this; the poll is every
2 ms and only runs while a title is waiting for a screenshot.

## Verification

Windows 11, RTX 5070 Ti, Rust 1.98.1 (pinned), MSVC 14.44.35207, Windows SDK
10.0.26100.0. **No game files**, so this is not evidence that the title's
screenshot now works end to end — it is evidence that nothing else broke and
that the queue behaves.

```
$ cargo test --workspace -- --test-threads=1
... every suite ok; victory-shader 210 passed, victory-gpu 110 passed ...
test result: ok. N passed; 0 failed          (0 failures anywhere)

$ cargo clippy -p victory-gpu -p victory-hle --all-targets
clean

$ cargo xtask policy
asset policy: 172 files ok
```

One new test, `screenshot::tests::asks_queue_and_are_taken_oldest_first`,
covers what does not need a device: an ask is wanted until a frame fills it,
an unfilled ask is not a frame, `take` gives the oldest filled one, and a
third ask drops the oldest. The read-back and the mapping need a real device
and are left to the title's own calls.

## Not done

`0x18` (`diagnostic screenshot`) and `0x20` (`diagnostic clip`) are still
`missing`. Those are the developer-capture interfaces, not what the title
calls for a user screenshot, and they write files rather than take a frame —
a different shape, and `capture.rs` already has most of it behind its
switches.

If this is not the layout the title actually passes at `0x28`, the refusal is
immediate (`E_NOT_SUFFICIENT_BUFFER` or `E_INVALIDARG`) rather than a bad
write, so a wrong guess here fails safely and the log says which.
@xiaotu030202
xiaotu030202 force-pushed the screenshot-from-the-title branch from e3a0ac7 to 888d818 Compare November 25, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant