Pack and write capture pictures on a thread of their own #310

Open
github-bot wants to merge 3 commits from github-bot/gh-310 into main AGit
Owner

@⁠wondasahnet on GitHub · 2026-11-27 15:37 UTC · original
wondasahnet:perf/capture-off-submit-thread → main · +364 −9 · 5 files · 3 commits · open

With VICTORY_CAPTURE_WORLD every 8th frame's picture in the world is saved as a PNG. The picture was packed (deflate, and a CRC-32 made a bit at a time) and written by the thread that prepares the submissions, the one a frame waits for. The pictures are there to judge the very runs they slowed down.

Measured

RTX 5080, Ryzen 7 9800X3D, 64 GB, Windows 11. Series S build, prison intro (cell, corridor, common room), windowed, diagnostics build, -Machine 4070, captures on. Self-driven runs; fps is the median of the frame lines with at least 1,000 draws, p10 the 10th percentile.

A 25 s sampling profile of the submitting thread before the change: 23% of its samples in saving capture PNGs. After it that share is gone from the thread.

fps median (two runs) p10
Before 22.0 / 23.4 20.4 / 19.0
After 23.85 / 24.4 19.4 / 21.5

Runs of this scene differ by about 1 fps from one to the next, so the table says "somewhat more", not a ratio; the profile is the firmer evidence. A run without captures is not affected at all.

What changes

  • The caller still takes the rows from the read-back buffer (it is reused). Packing and writing go to a victory-capture thread through a bounded line of 16 pictures.
  • When the line is full (the disk is slower than the frames) or the thread could not be started, the caller packs and writes itself, as before. No picture is dropped while the process runs.
  • The CRC-32 goes by a table of 256 values made at compile time. The encoder is otherwise unchanged.

Tests

  • crc_matches_the_png_check_value gains the standard check value ("123456789" gives 0xcbf43926) beside the PNG IEND one.

  • Run on this branch at 708e0fa (base c44e2ce), Windows 11, RTX 5080, Ryzen 7 9800X3D, 64 GB:

    Command Result
    cargo check --workspace --all-targets --locked clean
    cargo test --locked -p victory-gpu --features diagnostics --lib -- capture:: 3 passed, 0 failed
    cargo build --locked --profile diag -p victory --features diagnostics (before the rebase onto c44e2ce, which was clean) builds
    git diff --check clean

    The four CI commands were run once on a local merge of this branch with five other branches of mine, on base fe8a7a2 before the rebase: both clippy commands clean, cargo test --workspace --locked passed, and cargo test --workspace --all-features --locked -- --test-threads=1 passed except modules::lifecycle::tests::direct_close_suspends_and_exits in victory-hle, a window test in a crate this branch does not touch; it passed in the default job of the same run and when run again alone. After the rebase the merge was linted again with both clippy commands: clean.

Not covered

  • Pictures still waiting in the line when the process ends are lost (16 at most). Before, a picture was on disk when the save returned.
  • No test reads back a file written by the thread.
  • The frame rates are two runs each on one PC.

Since the runs above the branch was rebased onto main at 16edfec (no conflicts; cargo check -p victory-gpu --all-targets --locked clean); its tip is 2312bbd now.

**@⁠wondasahnet** on GitHub · 2026-11-27 15:37 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310) `wondasahnet:perf/capture-off-submit-thread` → `main` · +364 −9 · 5 files · 3 commits · open With `VICTORY_CAPTURE_WORLD` every 8th frame's picture in the world is saved as a PNG. The picture was packed (deflate, and a CRC-32 made a bit at a time) and written by the thread that prepares the submissions, the one a frame waits for. The pictures are there to judge the very runs they slowed down. ## Measured RTX 5080, Ryzen 7 9800X3D, 64 GB, Windows 11. Series S build, prison intro (cell, corridor, common room), windowed, diagnostics build, `-Machine 4070`, captures on. Self-driven runs; fps is the median of the frame lines with at least 1,000 draws, p10 the 10th percentile. A 25 s sampling profile of the submitting thread before the change: 23% of its samples in saving capture PNGs. After it that share is gone from the thread. | | fps median (two runs) | p10 | |---|---:|---:| | Before | 22.0 / 23.4 | 20.4 / 19.0 | | After | 23.85 / 24.4 | 19.4 / 21.5 | Runs of this scene differ by about 1 fps from one to the next, so the table says "somewhat more", not a ratio; the profile is the firmer evidence. A run without captures is not affected at all. ## What changes - The caller still takes the rows from the read-back buffer (it is reused). Packing and writing go to a `victory-capture` thread through a bounded line of 16 pictures. - When the line is full (the disk is slower than the frames) or the thread could not be started, the caller packs and writes itself, as before. No picture is dropped while the process runs. - The CRC-32 goes by a table of 256 values made at compile time. The encoder is otherwise unchanged. ## Tests - `crc_matches_the_png_check_value` gains the standard check value (`"123456789"` gives `0xcbf43926`) beside the PNG `IEND` one. - Run on this branch at `708e0fa` (base `c44e2ce`), Windows 11, RTX 5080, Ryzen 7 9800X3D, 64 GB: | Command | Result | |---|---| | `cargo check --workspace --all-targets --locked` | clean | | `cargo test --locked -p victory-gpu --features diagnostics --lib -- capture::` | 3 passed, 0 failed | | `cargo build --locked --profile diag -p victory --features diagnostics` (before the rebase onto `c44e2ce`, which was clean) | builds | | `git diff --check` | clean | The four CI commands were run once on a local merge of this branch with five other branches of mine, on base `fe8a7a2` before the rebase: both clippy commands clean, `cargo test --workspace --locked` passed, and `cargo test --workspace --all-features --locked -- --test-threads=1` passed except `modules::lifecycle::tests::direct_close_suspends_and_exits` in `victory-hle`, a window test in a crate this branch does not touch; it passed in the default job of the same run and when run again alone. After the rebase the merge was linted again with both clippy commands: clean. ## Not covered - Pictures still waiting in the line when the process ends are lost (16 at most). Before, a picture was on disk when the save returned. - No test reads back a file written by the thread. - The frame rates are two runs each on one PC. --- Since the runs above the branch was rebased onto `main` at `16edfec` (no conflicts; `cargo check -p victory-gpu --all-targets --locked` clean); its tip is `2312bbd` now.
With VICTORY_CAPTURE_WORLD every 8th frame's picture was packed (deflate,
and a CRC-32 made a bit at a time) by the thread that prepares the
submissions, the one a frame waits for: 23% of its samples in a profile of
the Series S prison scene (RTX 5080, 4070 profile, 22 fps). The pictures
are there to judge those very runs.

The rows are still taken from the read-back buffer by the caller; packing
and writing go to a "victory-capture" thread through a bounded line, and
fall back to the caller when the line is full. The CRC-32 goes by a table.

Two runs each: 22.0 / 23.4 fps median before, 23.85 / 24.4 after.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 15:43:33 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 15:54 UTC · original

@⁠wondasahnet The drop in submit-thread samples spent writing PNGs is worth keeping. The open case is a normal close with pictures still queued. Drain the worker on that path and verify a PNG written by the worker before calling the capture complete.

**@⁠TaynanGT** on GitHub · 2026-11-27 15:54 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310#issuecomment-7222619506) @⁠wondasahnet The drop in submit-thread samples spent writing PNGs is worth keeping. The open case is a normal close with pictures still queued. Drain the worker on that path and verify a PNG written by the worker before calling the capture complete.
Author
Owner

@⁠UncommonDLL on GitHub · 2026-11-27 16:30 UTC · original

@⁠wondasahnet @⁠TaynanGT We implemented the normal-close drain on 2312bbd3. The Windows regression held a worker write pending, filled the queue and exercised the synchronous fallback. Before the drain returned, the worker finished 17 writes. All 18 PNGs decoded to 11×7 RGB8 and matched every expected pixel. With the drain disabled, the same test caught close returning with zero worker writes completed.

The four capture tests passed. The existing WM_CLOSE process-exit test passed separately. The patch credits Cuebert’s queued-frame regression in fxceless #142. OpenAI Codex implemented and tested the change on Windows. Claude Sonnet 5.5 Extra reviewed it.

Capture-drain-aabfb888.patch

Apply with git am Capture-drain-aabfb888.patch.

**@⁠UncommonDLL** on GitHub · 2026-11-27 16:30 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310#issuecomment-7223346701) @⁠wondasahnet @⁠TaynanGT We implemented the normal-close drain on `2312bbd3`. The Windows regression held a worker write pending, filled the queue and exercised the synchronous fallback. Before the drain returned, the worker finished 17 writes. All 18 PNGs decoded to 11×7 RGB8 and matched every expected pixel. With the drain disabled, the same test caught close returning with zero worker writes completed. The four capture tests passed. The existing `WM_CLOSE` process-exit test passed separately. The patch credits Cuebert’s queued-frame regression in fxceless #142. OpenAI Codex implemented and tested the change on Windows. Claude Sonnet 5.5 Extra reviewed it. [Capture-drain-aabfb888.patch](https://github.com/user-attachments/files/35521793/Capture-drain-aabfb888.patch) Apply with `git am Capture-drain-aabfb888.patch`.
The thread that packs and writes capture pictures has a line of up to 16.
What was in it when the process ended was lost: a normal close could leave
the last pictures of a run unwritten.

The line can now be closed. capture::finish closes it and waits until the
thread has written every picture that was in it, three seconds at most; if
that runs out, the number left is logged and the process goes on ending
(a disk that does not answer must not keep it). It waits for the thread's
last picture, not for the thread's own end, which needs the loader's lock.
A picture saved after that is written by its caller.

It is called where the pipeline books are saved already:
- the window is closed or the title is ended as the console ends it
  (lifecycle's end, before TerminateProcess);
- the title calls the CRT's exit (the loader's hook that runs before the
  other threads are ended, inside its five-second deadline);
- the title's main thread returns.

Not covered: the title calling TerminateProcess or abort, the exit after
a device loss, a crash, the loader's deadline running out, and a kill
from outside. A picture whose rows are being taken from the read-back
buffer at that moment is not in the line yet and is not waited for.

Tests: eight pictures given to the process's own line are read back after
finish returns and checked as a decoder would (signature, every chunk's
check value by a CRC made a bit at a time, the header's fields, the rows
unpacked and compared byte for byte, the end chunk last), and a picture
saved afterwards is written by its caller; a closed line is worked off in
order before the wait ends; a thread that does not come back holds the
wait for its bound and the pictures left are counted.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 16:37 UTC · original

@⁠UncommonDLL The 18 pixel-exact files and the disabled-drain control cover the normal-close case. Please put the drain and its regression on the PR branch so the Windows checks run both together on the new head.

**@⁠TaynanGT** on GitHub · 2026-11-27 16:37 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310#issuecomment-7223484923) @⁠UncommonDLL The 18 pixel-exact files and the disabled-drain control cover the normal-close case. Please put the drain and its regression on the PR branch so the Windows checks run both together on the new head.
Author
Owner

@⁠wondasahnet on GitHub · 2026-11-27 16:38 UTC · original

Done in db2a446, on top of the reviewed tip.

The drain. The capture thread's line can now be closed. At an orderly end of the process the line is closed and the thread is waited for until every picture that was in it is written. The wait is bounded at three seconds; if it runs out, the number of pictures left is logged and the process goes on ending, so a disk that does not answer cannot hold the exit. It waits for the thread's last picture rather than for the thread's own end, because a thread ends under the loader's lock, which the exiting thread may hold. A picture saved after the drain is written by its caller.

It runs where the pipeline books are already saved:

  • the window is closed, or the title is ended as the console ends it (lifecycle's end, before TerminateProcess);
  • the title calls the CRT's exit (the loader's hook that runs before the other threads are ended, inside its five-second deadline);
  • the title's main thread returns.

The PNG written by the worker. pictures_in_the_line_are_valid_png_files_when_finish_returns sends eight pictures through the process's own line into a temporary folder, waits as the exit does, then reads each file back and checks it as a decoder would: signature; every chunk's length and check value, computed with a bit-at-a-time CRC-32 rather than the writer's table; the header's width, height, bit depth, colour type, compression, filter and interlace fields; the packed rows inflated (with the zlib crate that is already a dependency, no new one) and compared byte for byte with the rows handed in; filter type 0 in every row; the end chunk last and nothing behind it. The test asserts that each picture really went to the thread and not to the caller's fallback.

Two more tests on the line itself: a_closed_line_is_worked_off_before_the_wait_ends (nine of ten pictures still queued when the line is closed; all ten are worked off, in order) and the_wait_for_the_line_is_bounded_and_counts_what_is_left (a thread that never comes back holds the wait for its bound and no longer; the three pictures it had are reported as left).

Checks. cargo check --workspace --all-targets --locked, and cargo clippy --workspace --all-targets --locked -- -D warnings with and without --all-features: clean. cargo test --locked -p victory-gpu --features diagnostics --lib -- capture::: 6 passed, five runs. cargo test --locked -p victory-hle --all-features --lib -- lifecycle --test-threads=1: 19 passed; that includes the close test whose child process now runs the drain (with an empty line).

Not covered.

  • Ends that do not drain: the title calling TerminateProcess or abort, the exit after a device loss, a crash, the loader's deadline running out, a kill from outside. Up to 16 pictures can still be lost there.
  • A picture whose rows are being taken from the read-back buffer when the process ends is not in the line yet and is not waited for.
  • I have not watched the drain in a game run. My self-driven runs end by a kill, so they do not reach it; the evidence is the tests above.
**@⁠wondasahnet** on GitHub · 2026-11-27 16:38 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310#issuecomment-7223503522) Done in `db2a446`, on top of the reviewed tip. **The drain.** The capture thread's line can now be closed. At an orderly end of the process the line is closed and the thread is waited for until every picture that was in it is written. The wait is bounded at three seconds; if it runs out, the number of pictures left is logged and the process goes on ending, so a disk that does not answer cannot hold the exit. It waits for the thread's last picture rather than for the thread's own end, because a thread ends under the loader's lock, which the exiting thread may hold. A picture saved after the drain is written by its caller. It runs where the pipeline books are already saved: - the window is closed, or the title is ended as the console ends it (`lifecycle`'s `end`, before `TerminateProcess`); - the title calls the CRT's `exit` (the loader's hook that runs before the other threads are ended, inside its five-second deadline); - the title's main thread returns. **The PNG written by the worker.** `pictures_in_the_line_are_valid_png_files_when_finish_returns` sends eight pictures through the process's own line into a temporary folder, waits as the exit does, then reads each file back and checks it as a decoder would: signature; every chunk's length and check value, computed with a bit-at-a-time CRC-32 rather than the writer's table; the header's width, height, bit depth, colour type, compression, filter and interlace fields; the packed rows inflated (with the zlib crate that is already a dependency, no new one) and compared byte for byte with the rows handed in; filter type 0 in every row; the end chunk last and nothing behind it. The test asserts that each picture really went to the thread and not to the caller's fallback. Two more tests on the line itself: `a_closed_line_is_worked_off_before_the_wait_ends` (nine of ten pictures still queued when the line is closed; all ten are worked off, in order) and `the_wait_for_the_line_is_bounded_and_counts_what_is_left` (a thread that never comes back holds the wait for its bound and no longer; the three pictures it had are reported as left). **Checks.** `cargo check --workspace --all-targets --locked`, and `cargo clippy --workspace --all-targets --locked -- -D warnings` with and without `--all-features`: clean. `cargo test --locked -p victory-gpu --features diagnostics --lib -- capture::`: 6 passed, five runs. `cargo test --locked -p victory-hle --all-features --lib -- lifecycle --test-threads=1`: 19 passed; that includes the close test whose child process now runs the drain (with an empty line). **Not covered.** - Ends that do not drain: the title calling `TerminateProcess` or `abort`, the exit after a device loss, a crash, the loader's deadline running out, a kill from outside. Up to 16 pictures can still be lost there. - A picture whose rows are being taken from the read-back buffer when the process ends is not in the line yet and is not waited for. - I have not watched the drain in a game run. My self-driven runs end by a kill, so they do not reach it; the evidence is the tests above.
Make overlapping window close and title exit wait for the same bounded drain. Hold the writer in the PNG regression and make sure the second exit cannot bypass it. The held-writer setup follows the tests proposed by UncommonDLL and Cuebert.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 16:45 UTC · original · edited 2026-11-27 16:50 UTC

@⁠wondasahnet The three-second cap and the completion signal before thread teardown address the slow-disk and loader-lock cases. I’ve started the Windows checks on db2a446. There’s one overlap to cover: a window close and CRT exit can both reach finish(). Once the first caller changes the line to Caller, the second can end the process while the first still waits. The shared wait is up in 926102e. The isolated held-writer test reproduced the early return on db2a446 and passes with both callers waiting. The Windows checks are on the new head.

**@⁠TaynanGT** on GitHub · 2026-11-27 16:45 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/310#issuecomment-7223637016) · edited 2026-11-27 16:50 UTC @⁠wondasahnet The three-second cap and the completion signal before thread teardown address the slow-disk and loader-lock cases. I’ve started the Windows checks on `db2a446`. There’s one overlap to cover: a window close and CRT exit can both reach `finish()`. Once the first caller changes the line to `Caller`, the second can end the process while the first still waits. The shared wait is up in `926102e`. The isolated held-writer test reproduced the early return on `db2a446` and passes with both callers waiting. The [Windows checks](https://github.com/TaynanGT/Vice-Tay/actions/runs/46036792755) are on the new head.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/310/head:github-bot/gh-310'
git switch 'github-bot/gh-310'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-310'
git switch 'github-bot/gh-310'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-310'
git switch 'github-bot/gh-310'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-310'
git switch 'main'
git merge --squash 'github-bot/gh-310'
git switch 'main'
git merge --ff-only 'github-bot/gh-310'
git switch 'main'
git merge 'github-bot/gh-310'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.