[PR #1877] perf(runtime): reuse rendering scopes across compatible draws #1889

Open
opened 2026-11-26 21:19:18 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1877
Author: @cheltyi
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: perf/replay-draw-scopes


📝 Commits (7)

  • 5b6f721 perf(runtime): reuse rendering scopes for compatible draws
  • 47e177d test(runtime): avoid Windows far macro in rendering test
  • 44c65f0 Merge #1877 rendering-scope reuse for diagnostics follow-up
  • 0355263 runtime: measure rendering-scope reuse and benchmark mixed draws
  • 12b3386 Merge current main for rendering-scope metrics follow-up
  • 5d58dde Merge current main before completing rendering-scope PR
  • 5009bea Merge branch 'main' into perf/replay-draw-scopes

📊 Changes

7 files changed (+1429 additions, -50 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+49 -4)
📝 runtime/CMakeLists.txt (+12 -2)
📝 runtime/README.md (+1 -0)
➕ runtime/src/rendering_scope_stats.h (+87 -0)
📝 runtime/src/vulkan_replay.h (+143 -44)
➕ runtime/test/replay_rendering_scope_test.cpp (+594 -0)
➕ runtime/test/vk_rendering_scope_test.cpp (+543 -0)

📄 Description

Claimed in the coordination issue as: D3D-REPLAY-DRAW-SCOPE-RUNS — claim; D3D-REPLAY-SCOPE-METRICS — claim.

Depends on: #1062 — the documentation edits overlap in section 4.3; the runtime code has no dependency on its render-pass API implementation.

What and why

The Vulkan replay currently starts and ends dynamic rendering for every resolved draw, with attachment dependencies between those scopes. For 10,000 consecutive draws sharing a color/depth target, this emits 10,000 begins, 10,000 ends and 20,000 attachment barriers.

Keep one scope across consecutive compatible direct draws. Compatibility requires the same prepared graphics pipeline, attachment slots/images/views/subresource ranges/layouts, render area and layer count. Close before all other commands, skipped or indirect draws, statistics endpoints and GPU helpers, and before list cleanup. Each draw still emits its own state, descriptors, push constants and arguments. Fixed-function depth/stencil/blend ordering remains Vulkan rasterization order within the scope. The compatible 10,000-draw case now has one begin/end and two attachment dependencies.

The scope-lifetime optimization is independent of #1062's COM render-pass API; its section 4.3 documentation overlap is declared above.

LEONIDA_RENDERING_SCOPE_STATS=1 now reports aggregate emitted/eligible/indirect/skipped draw commands, application scopes, shared draws, maximum run, a run-length histogram and scope-end reasons. The cached opt-in selects a template once per list; the default specialization has no per-draw counter updates. The instrumented path collects locally and merges once per list, including with device-fault checkpoints enabled. Reports contain fixed labels and totals only, at powers of two emitted lists and normal exit. Counts describe CPU replay attempts, including repeated lists or work that may not complete on the GPU.

The existing GPU fixture gains --bench-mixed: runs of 2/3 draws separated by UAV barriers, actual pipeline changes, target changes or single indirect draws, plus a repeating mixed sequence. Every pass checks both targets' pixels/depth and rendering scope counts. The default correctness run includes the mixed sequence. The renderer implementation is not duplicated.

Measured frame cost

Ryzen 5 5600 + Radeon RX 6700 XT, RADV Mesa 26.2.5-arch3.1, Arch Linux. The new hand-authored fixture draws 10,000 scissored triangles into a 32×32 RGBA32F/D32 target, with changing push constants, depth testing/writes and alpha blending. Global UAV barriers split the compatible runs. Every color/depth texel is independently calculated on the CPU and checked after every frame.

Compatible run length Before frame (ms) After frame (ms) Change
4 43.67 20.12 −54%
16 43.29 12.90 −70%
64 42.83 9.88 −77%
10,000 42.94 8.10 −81%

These are medians of two alternating before/after process medians, each with three warmups and seven measured frames, without validation. In the full compatible case, CPU emit was 10.97 → 3.29 ms and the GPU replay interval 31.62 → 4.53 ms. Pipeline creation and preparation are outside timing; serialized frame cost includes command recording, submission, readback and waiting for completion. This is synthetic runtime frame time; retail-game FPS and the distribution of compatible run lengths have not been measured.

The singleton control (a UAV barrier between every draw) used a longer check: CPU affinity, 30 warmups + 30 samples, five quiet alternating pairs. Median 10,000-draw frame time was 48.72 → 48.86 ms (+0.29%); CPU emit 13.043 → 13.023 ms. A separate cheap-stub CPU benchmark found about 5% added untracked singleton overhead (roughly 0.10 ms/10,000 draws), without reproducing a frame regression on the GPU. This optimization benefits compatible runs; it retains the existing work at boundaries.

Additional mixed-workload measurements

GCC 16.2.1, -O3 -DNDEBUG, RADV NAVI22, Mesa 26.2.5. Five alternating before/after process pairs pinned to CPU 4; each process uses three warm-up and seven measured passes per case. Validation and scope diagnostics are disabled for timings. Values below are medians of the five process medians at 10,000 draw commands. The baseline uses the main replay header at db1a22d29; all other runtime sources and the synthetic workload match. Its only fixture change is the expected per-draw scope count. Both versions pass pixel/depth/blend checks.

Boundary / direct run Scopes after (before: 10,000) Before frame ms After frame ms Change
UAV barrier / 2 5,000 46.95 30.18 -35.7%
UAV barrier / 3 3,334 45.74 23.49 -48.6%
Pipeline / 2 5,000 47.41 31.39 -33.8%
Pipeline / 3 3,334 46.16 25.60 -44.6%
Targets / 2 5,000 45.16 29.82 -34.0%
Targets / 3 3,334 46.90 25.56 -45.5%
Single indirect / 2 6,667 49.64 36.65 -26.2%
Single indirect / 3 5,000 47.05 30.60 -35.0%
Mixed / variable 5,000 52.11 34.22 -34.3%

These are serialized synthetic frame costs, including recording/submission/readback/waiting. They do not establish a retail FPS gain or fix the black screen. The unsuccessful retail FPS comparison is recorded below.

A separate five alternating process pairs compare #1877 without this follow-up against this follow-up with diagnostics disabled. The 10,000-command mixed case changes from 32.5534 to 32.7262 ms frame (+0.53%), and 8.7039 to 8.7131 ms CPU emit (+0.11%). Across the nine 10,000-command cases, frame medians range from -3.44% to +7.12%, CPU emit from -5.98% to +6.00%; these runs do not establish zero overhead in every workload.

Tested

  • Full portable Release runtime build and CTest: 100 passed, 0 failed, 2 skipped (retail shader corpus unset; sync-validation self-test requires explicit opt-in).
  • Explicit validation + scope statistics: vk_rendering_scope, vk_sync_validation, replay_rendering_scope, replay_rendering_scope_stats all pass. The sync-validation self-test detects its deliberately missing barrier; rendering-scope fixture reports zero validation errors. Baseline fixture also passes validation.
  • CPU fixture covers every histogram edge, boundary reasons, skipped/indirect/helper/statistics draws, exclusion of internal clear scopes, repeated/empty lists and per-draw device-fault checkpoints. Exact environment opt-in checked with unset/0/1/true/10.
  • All 20 benchmark processes pass pixel/depth/blend and scope checks, at both 1,000 and 10,000 commands.
  • Translator suite: 491 files, 6,312 tests, no failures. compileall, make_game.py --help, text line-ending tests, no-game-data check and git diff --check pass.

GPUs tested: AMD Radeon RX 6700 XT (RADV NAVI22), Mesa/vulkan-radeon 26.2.5, CachyOS kernel 7.3.1-1-cachyos. Vulkan validation layer 1.4.363. Windows/MSVC was not run locally for the added diagnostics/mixed cases. Independent Windows reviews in this PR apply to the original 47e177df6 revision.

AI-assisted: yes.

After merging current main (a224b5d14) for final HEAD 5d58dde1c, CMake confirms the portable build is up to date, and the seven focused rendering/query/indirect tests pass with validation and scope diagnostics enabled. Runtime replay sources and semantic tests are unchanged by that final main merge.

Retail boot limit (2026-11-25)

Two offline, network-isolated retail boot checks on CachyOS 7.3.1, RADV/Mesa 26.2.5 did not yield a verified first world frame. HEAD 47e177df6 ran for 12 minutes through setup/loading before its time limit. Baseline 77baf9062 ran for 6 minutes 43 seconds before an AMD GPU fault and successful GPU reset.

The baseline used a copy of the completed first-run settings from the HEAD run, so these are not a matched frame-time/FPS pair. No retail FPS gain is established. The baseline fault shows that a GPU reset can occur without this optimization; it does not establish the cause of the earlier desktop freeze or show that this PR fixes it. The synthetic results above remain synthetic frame costs.

Checklist

  • Based on current main (a224b5d14) and merges cleanly.
  • One topic: rendering-scope reuse, its diagnostics and validation.
  • No game data, keys or decryption, including quoted logs/reports; shaders are hand-authored synthetic fixtures.
  • No new translator rule; the runtime replay behavior is documented in docs/d3d12_vulkan.md.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1877 **Author:** [@cheltyi](https://github.com/cheltyi) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `perf/replay-draw-scopes` --- ### 📝 Commits (7) - [`5b6f721`](https://github.com/ubiquitarianism/leonida/commit/5b6f721f5ffa4e787a749b3082d643f9f17baa7c) perf(runtime): reuse rendering scopes for compatible draws - [`47e177d`](https://github.com/ubiquitarianism/leonida/commit/47e177df6a6f07ea036c60c36aace6ba489e47f9) test(runtime): avoid Windows far macro in rendering test - [`44c65f0`](https://github.com/ubiquitarianism/leonida/commit/44c65f0772ae51820abb6d8dadff2d5d9d0547b2) Merge #1877 rendering-scope reuse for diagnostics follow-up - [`0355263`](https://github.com/ubiquitarianism/leonida/commit/03552636de82dbc1b6b5f4df12f0c25f198d3100) runtime: measure rendering-scope reuse and benchmark mixed draws - [`12b3386`](https://github.com/ubiquitarianism/leonida/commit/12b33866e1ee1e56f3b6243aaa3733d7696dbaea) Merge current main for rendering-scope metrics follow-up - [`5d58dde`](https://github.com/ubiquitarianism/leonida/commit/5d58dde1c8e003aa98bd94812438c8ff3de42d30) Merge current main before completing rendering-scope PR - [`5009bea`](https://github.com/ubiquitarianism/leonida/commit/5009bea2e8c2c6249c18c93dc1984a2dcc87ea5a) Merge branch 'main' into perf/replay-draw-scopes ### 📊 Changes **7 files changed** (+1429 additions, -50 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+49 -4) 📝 `runtime/CMakeLists.txt` (+12 -2) 📝 `runtime/README.md` (+1 -0) ➕ `runtime/src/rendering_scope_stats.h` (+87 -0) 📝 `runtime/src/vulkan_replay.h` (+143 -44) ➕ `runtime/test/replay_rendering_scope_test.cpp` (+594 -0) ➕ `runtime/test/vk_rendering_scope_test.cpp` (+543 -0) </details> ### 📄 Description Claimed in the coordination issue as: **D3D-REPLAY-DRAW-SCOPE-RUNS** — [claim](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7163814583); **D3D-REPLAY-SCOPE-METRICS** — [claim](https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7174267235). Depends on: #1062 — the documentation edits overlap in section 4.3; the runtime code has no dependency on its render-pass API implementation. ### What and why The Vulkan replay currently starts and ends dynamic rendering for every resolved draw, with attachment dependencies between those scopes. For 10,000 consecutive draws sharing a color/depth target, this emits 10,000 begins, 10,000 ends and 20,000 attachment barriers. Keep one scope across consecutive compatible direct draws. Compatibility requires the same prepared graphics pipeline, attachment slots/images/views/subresource ranges/layouts, render area and layer count. Close before all other commands, skipped or indirect draws, statistics endpoints and GPU helpers, and before list cleanup. Each draw still emits its own state, descriptors, push constants and arguments. Fixed-function depth/stencil/blend ordering remains Vulkan rasterization order within the scope. The compatible 10,000-draw case now has one begin/end and two attachment dependencies. The scope-lifetime optimization is independent of #1062's COM render-pass API; its section 4.3 documentation overlap is declared above. `LEONIDA_RENDERING_SCOPE_STATS=1` now reports aggregate emitted/eligible/indirect/skipped draw commands, application scopes, shared draws, maximum run, a run-length histogram and scope-end reasons. The cached opt-in selects a template once per list; the default specialization has no per-draw counter updates. The instrumented path collects locally and merges once per list, including with device-fault checkpoints enabled. Reports contain fixed labels and totals only, at powers of two emitted lists and normal exit. Counts describe CPU replay attempts, including repeated lists or work that may not complete on the GPU. The existing GPU fixture gains `--bench-mixed`: runs of 2/3 draws separated by UAV barriers, actual pipeline changes, target changes or single indirect draws, plus a repeating mixed sequence. Every pass checks both targets' pixels/depth and rendering scope counts. The default correctness run includes the mixed sequence. The renderer implementation is not duplicated. ### Measured frame cost Ryzen 5 5600 + Radeon RX 6700 XT, RADV Mesa 26.2.5-arch3.1, Arch Linux. The new hand-authored fixture draws 10,000 scissored triangles into a 32×32 RGBA32F/D32 target, with changing push constants, depth testing/writes and alpha blending. Global UAV barriers split the compatible runs. Every color/depth texel is independently calculated on the CPU and checked after every frame. | Compatible run length | Before frame (ms) | After frame (ms) | Change | |---|---:|---:|---:| | 4 | 43.67 | 20.12 | −54% | | 16 | 43.29 | 12.90 | −70% | | 64 | 42.83 | 9.88 | −77% | | 10,000 | 42.94 | 8.10 | −81% | These are medians of two alternating before/after process medians, each with three warmups and seven measured frames, without validation. In the full compatible case, CPU emit was 10.97 → 3.29 ms and the GPU replay interval 31.62 → 4.53 ms. Pipeline creation and preparation are outside timing; serialized frame cost includes command recording, submission, readback and waiting for completion. This is synthetic runtime frame time; retail-game FPS and the distribution of compatible run lengths have not been measured. The singleton control (a UAV barrier between every draw) used a longer check: CPU affinity, 30 warmups + 30 samples, five quiet alternating pairs. Median 10,000-draw frame time was 48.72 → 48.86 ms (+0.29%); CPU emit 13.043 → 13.023 ms. A separate cheap-stub CPU benchmark found about 5% added untracked singleton overhead (roughly 0.10 ms/10,000 draws), without reproducing a frame regression on the GPU. This optimization benefits compatible runs; it retains the existing work at boundaries. ### Additional mixed-workload measurements GCC 16.2.1, `-O3 -DNDEBUG`, RADV NAVI22, Mesa 26.2.5. Five alternating before/after process pairs pinned to CPU 4; each process uses three warm-up and seven measured passes per case. Validation and scope diagnostics are disabled for timings. Values below are medians of the five process medians at 10,000 draw commands. The baseline uses the `main` replay header at `db1a22d29`; all other runtime sources and the synthetic workload match. Its only fixture change is the expected per-draw scope count. Both versions pass pixel/depth/blend checks. | Boundary / direct run | Scopes after (before: 10,000) | Before frame ms | After frame ms | Change | |---|---:|---:|---:|---:| | UAV barrier / 2 | 5,000 | 46.95 | 30.18 | -35.7% | | UAV barrier / 3 | 3,334 | 45.74 | 23.49 | -48.6% | | Pipeline / 2 | 5,000 | 47.41 | 31.39 | -33.8% | | Pipeline / 3 | 3,334 | 46.16 | 25.60 | -44.6% | | Targets / 2 | 5,000 | 45.16 | 29.82 | -34.0% | | Targets / 3 | 3,334 | 46.90 | 25.56 | -45.5% | | Single indirect / 2 | 6,667 | 49.64 | 36.65 | -26.2% | | Single indirect / 3 | 5,000 | 47.05 | 30.60 | -35.0% | | Mixed / variable | 5,000 | 52.11 | 34.22 | -34.3% | These are serialized synthetic frame costs, including recording/submission/readback/waiting. They do not establish a retail FPS gain or fix the black screen. The unsuccessful retail FPS comparison is recorded below. A separate five alternating process pairs compare #1877 without this follow-up against this follow-up with diagnostics disabled. The 10,000-command mixed case changes from 32.5534 to 32.7262 ms frame (+0.53%), and 8.7039 to 8.7131 ms CPU emit (+0.11%). Across the nine 10,000-command cases, frame medians range from -3.44% to +7.12%, CPU emit from -5.98% to +6.00%; these runs do not establish zero overhead in every workload. ### Tested - Full portable Release runtime build and CTest: 100 passed, 0 failed, 2 skipped (retail shader corpus unset; sync-validation self-test requires explicit opt-in). - Explicit validation + scope statistics: `vk_rendering_scope`, `vk_sync_validation`, `replay_rendering_scope`, `replay_rendering_scope_stats` all pass. The sync-validation self-test detects its deliberately missing barrier; rendering-scope fixture reports zero validation errors. Baseline fixture also passes validation. - CPU fixture covers every histogram edge, boundary reasons, skipped/indirect/helper/statistics draws, exclusion of internal clear scopes, repeated/empty lists and per-draw device-fault checkpoints. Exact environment opt-in checked with unset/0/1/true/10. - All 20 benchmark processes pass pixel/depth/blend and scope checks, at both 1,000 and 10,000 commands. - Translator suite: 491 files, 6,312 tests, no failures. `compileall`, `make_game.py --help`, text line-ending tests, no-game-data check and `git diff --check` pass. GPUs tested: AMD Radeon RX 6700 XT (RADV NAVI22), Mesa/vulkan-radeon 26.2.5, CachyOS kernel 7.3.1-1-cachyos. Vulkan validation layer 1.4.363. Windows/MSVC was not run locally for the added diagnostics/mixed cases. Independent Windows reviews in this PR apply to the original `47e177df6` revision. AI-assisted: yes. After merging current `main` (`a224b5d14`) for final HEAD `5d58dde1c`, CMake confirms the portable build is up to date, and the seven focused rendering/query/indirect tests pass with validation and scope diagnostics enabled. Runtime replay sources and semantic tests are unchanged by that final main merge. ### Retail boot limit (2026-11-25) Two offline, network-isolated retail boot checks on CachyOS 7.3.1, RADV/Mesa 26.2.5 did not yield a verified first world frame. HEAD `47e177df6` ran for 12 minutes through setup/loading before its time limit. Baseline `77baf9062` ran for 6 minutes 43 seconds before an AMD GPU fault and successful GPU reset. The baseline used a copy of the completed first-run settings from the HEAD run, so these are not a matched frame-time/FPS pair. No retail FPS gain is established. The baseline fault shows that a GPU reset can occur without this optimization; it does not establish the cause of the earlier desktop freeze or show that this PR fixes it. The synthetic results above remain synthetic frame costs. ### Checklist - [x] Based on current `main` (`a224b5d14`) and merges cleanly. - [x] One topic: rendering-scope reuse, its diagnostics and validation. - [x] No game data, keys or decryption, including quoted logs/reports; shaders are hand-authored synthetic fixtures. - [x] No new translator rule; the runtime replay behavior is documented in `docs/d3d12_vulkan.md`. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1889
No description provided.