Skip to content

Runtime: emit makes no heap allocation for an enhanced texture barrier (RT-290) - #4468

Closed
shdann wants to merge 2 commits into
ItsNotRexed:mainfrom
shdann:shdann/emit-texture-barrier-no-alloc
Closed

shdann wants to merge 2 commits into
ItsNotRexed:mainfrom
shdann:shdann/emit-texture-barrier-no-alloc

Conversation

@shdann

@shdann shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-290 (claim). Finding: item 4 of the runtime audit.

What and why

emit in runtime/src/vulkan_replay.h made a std::vector<VkImageMemoryBarrier2> for each TextureBarrier. Thus each enhanced texture barrier made one heap allocation. #901 removed the allocations of the other barriers, clears and copies, but not this one. replay_cpu_bench had no enhanced texture barrier row, thus the allocation column did not show it.

This change:

  • emit keeps the image barriers in a stack array of 2. A range has one barrier for each layout piece. The usual range has one or two pieces (LayoutPieces). A vector holds the barriers only when a range has more than two pieces.
  • The Vulkan call does not change. emit continues to make one vkCmdPipelineBarrier2 call with all the barriers of the range.
  • replay_cpu_bench has a new row, texture barrier pair (enh.). Its self-test fails when emit allocates for this row.
  • replay_texture_barrier_test is new. It records the barriers of the vkCmdPipelineBarrier2 call. Case 1: a range in one layout gives one barrier and no heap allocation in emit. Case 2: three mips in three layouts give three barriers in one call, each with the old layout of its mip.

The diff in vulkan_replay.h is 10 added lines and 5 removed lines. I examined the open pull requests #1872, #2471, #2928, #2969, #3693 and #3784. They do not change these lines.

Tested

Windows 11, MSVC 14.51, CMake + Ninja, RelWithDebInfo. NVIDIA GeForce RTX 4090, driver 32.0.16.2101. No game files.

The tests that fail before the change and pass after it:

Test main (03b09a08) This branch
replay_texture_barrier_test FAIL: emit allocates nothing for a barrier with one piece (1 allocation) OK
replay_cpu_bench --self-test FAIL texture barrier pair (enh.): emit makes 1.00 heap allocations per command ok

replay_cpu_bench --reps 101, row texture barrier pair (enh.), 4096 commands, 5 runs for each build, in turns:

emit, ns per command (median of 5 runs, range) emit allocations per command Vulkan calls per command
main 124.9 (118.7 to 226.1) 1.00 1.00
This branch 88.1 (87.5 to 94.1) 0.00 1.00

Other tests:

  • Full ctest on this branch: 134 of 135 tests pass. xbox_present fails. Full ctest on main: 132 of 134 tests pass. d3d12_present and xbox_present fail. These two tests fail on main on this computer because of the window manager. d3d12_present passes in some runs and fails in others, on main also. 10 tests do not run on the two builds (no game files, no SDL3, no validation layer).
  • vk_command_replay_test on the RTX 4090: OK (validation off, because the Vulkan SDK is not installed).
  • vk_queue_test on the RTX 4090: OK.
  • command_list_test and the other replay_* tests: OK.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (Not applicable: no translator rule.)

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings November 23, 2026 01:36

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran replay_texture_barrier_test at 7b1e5b40 on Windows 11: replay_texture_barrier_test: OK, exit 0.

An enhanced texture barrier's image barriers now stay on the stack during emit (no heap allocation), and the new test plus the existing replay behaviour pass. No blocking issue.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 7b1e5b4: emit uses two stack slots for the common one- or two-piece layout case and allocates a vector only for larger piece counts. The synthetic test checks both single-piece allocation behavior and per-mip barriers for mixed layouts; Windows test report passes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

2 open issues
What changed in this PR

Removes per-command heap allocation when emitting enhanced texture barriers in Vulkan replay, and adds tests/bench coverage to prevent regressions.

Changes:

  • Update emit to use a small stack buffer for the common 1–2 image barrier case, heap-allocating only when needed.
  • Add a dedicated replay test that validates barrier emission shape (count/layouts) and asserts no heap allocations for the 1-piece case.
  • Extend CPU replay bench with a new enhanced texture barrier row plus a self-test allocation check.
File Description
runtime/​src/​vulkan_replay.h Switch texture barrier barrier storage from always-heap std::vector to stack-first storage.
runtime/​test/​replay_texture_barrier_test.cpp New test validating barrier recording and ensuring emit does not allocate in the 1-piece case.
runtime/​test/​replay_cpu_bench.cpp Add benchmark kind for enhanced texture barrier pairs and enforce zero emit allocations in self-test.
runtime/​CMakeLists.txt Wire the new replay texture barrier test into the build and CTest.

Review effort: Lite

Comment on lines +578 to +581
// One image barrier for each piece: emit keeps the usual one or two on the stack.
if (kind.name == texture_barrier_kind && r.emit.allocations != 0) {
std::fprintf(stderr, "FAIL %s: emit makes %.2f heap allocations per command\n", kind.name,
r.emit.allocations);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c448630. The self-test now compares the row name by content: std::strcmp(kind.name, texture_barrier_kind) == 0. replay_cpu_bench --self-test passes on Windows 11 with MSVC 14.51. AI-assisted.

Comment on lines +19 to +30

void* operator new(std::size_t size) {
++g_allocations;
if (void* p = std::malloc(size ? size : 1)) return p;
throw std::bad_alloc();
}
void* operator new[](std::size_t size) { return operator new(size); }
void operator delete(void* p) noexcept { std::free(p); }
void operator delete[](void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }
void operator delete[](void* p, std::size_t) noexcept { std::free(p); }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c448630. replay_texture_barrier_test now also replaces the aligned forms: operator new and operator new[] with std::align_val_t, and the four aligned operator delete forms. Each aligned allocation adds 1 to the counter. The nothrow forms of the standard library call the replaced forms, thus the counter includes them. There was no link error before this change, because the standard library supplies the aligned forms when a program does not replace them. replay_texture_barrier_test passes, and ctest -R "replay_|command_list" passes 10 of 10 on Windows 11 with MSVC 14.51. The Linux build runs in CI. AI-assisted.

@mrbean56

Copy link
Copy Markdown

Tested 7b1e5b40bf on Linux (Debian 13, GCC): pass, negative control red.

  • Built and ran replay_texture_barrier_test: OK. The test stubs vkCmdPipelineBarrier2 and counts allocations with its own operator new, so it is CPU-only and deterministic.
  • Negative control: restored main's runtime/src/vulkan_replay.h, rebuilt the same test: "emit made 1 heap allocations for a barrier with one piece" and FAIL: emit allocates nothing for a barrier with one piece. So the zero-allocation claim is measured, not assumed.
  • Read the change: few is a stack array for ≤2 pieces, many is resized only when pieces.size() > 2, each used slot is assigned a full zero-initialized VkImageMemoryBarrier2 before its fields are set, and barrier_count cannot exceed the storage. The zero-piece case passes few.data() with count 0, which is valid.
  • Also ran the real vk_command_replay_test part G (a texture barrier with several pieces) on an RX 7800 XT/RADV with the validation layer: no validation errors, so the replaced array still records what it did.

No game data. AI-assisted.

shdann added a commit to shdann/Leonida that referenced this pull request Nov 23, 2026
…locations

Review of ItsNotRexed#4468: replay_cpu_bench compares the row name with std::strcmp
instead of by pointer, and replay_texture_barrier_test also replaces the
aligned forms of operator new and delete, so that an allocation of an
over-aligned type is counted.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@kapitche

Copy link
Copy Markdown
Contributor

Tested c448630 merged with main d0835dd (merged tree fd523a0ebd) on Windows 11 with two GPUs (MSVC 14.51): built replay_texture_barrier_test and replay_cpu_bench.

  • replay_texture_barrier_test: OK, exit 0, on the NVIDIA RTX 4080 SUPER and on the Intel UHD 770 (driver 32.0.101.6129). replay_cpu_bench --self-test: exit 0, 15 kinds and a 296-command frame recorded and replayed, ok (run once). The tests don't use a GPU (they record the vkCmdPipelineBarrier2 call), so the two runs differ only by VK_LOADER_DRIVERS_DISABLE; I did not run the bench's timing rows.

Negative control: runtime/src/vulkan_replay.h restored from main with the PR's tests kept: replay_texture_barrier_test exits 1 (emit made 1 heap allocations for a barrier with one piece) and replay_cpu_bench --self-test exits 1 (FAIL texture barrier pair (enh.): emit makes 1.00 heap allocations per command).

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested c448630 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran ctest -R "^replay_texture_barrier$" (the test this PR changes): pass on both GPUs, 1/1 each. No game files.

AI-assisted.

shdann and others added 2 commits November 23, 2026 10:24
…e stack

emit made a std::vector<VkImageMemoryBarrier2> for each TextureBarrier: one
heap allocation for each enhanced texture barrier. The barriers of the usual
one or two layout pieces are now in a stack array; a vector holds them only
when a range has more than two pieces.

replay_cpu_bench gets an enhanced texture barrier row, and its self-test
fails when emit allocates for that row. replay_texture_barrier_test checks
the barriers of a one-piece range (no allocation) and of a three-piece range.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…locations

Review of ItsNotRexed#4468: replay_cpu_bench compares the row name with std::strcmp
instead of by pointer, and replay_texture_barrier_test also replaces the
aligned forms of operator new and delete, so that an allocation of an
over-aligned type is counted.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran both at 7b1e5b40b on Windows 11, RTX 5070 Ti:

  • replay_texture_barrier_test -> OK, exit 0.
  • replay_cpu_bench -> exit 0; it reports the layer-only cost (37,888 entries: 50.3 us storage-kept vs 176.2 us made-fresh; 10,600.3 us per ExecuteCommandLists of that size; 888 B per prepared entry, 984 B per recorded command).
  • git diff --check clean.

The test covers the texture barrier emitted for a read-after-write on the same texture in the replay path, and the bench records the reuse metric the change is meant to show (a kept prepared list is ~3.5x cheaper than rebuilding it). Both compile and run against the current main. No blocking issue.

@JamieHartley308

Copy link
Copy Markdown

Tested ea648bc (based on main 3faac55) on Windows 11 (10.0.26300), MSVC 19.44 (VS 2022 generator), RelWithDebInfo, Vulkan SDK 1.4.363, Python 3.13, RTX 3090 / NVIDIA 621.24: pass.

  • Build: no errors and no warnings in the changed files.
  • ctest -E loader_test: 170/170 pass. Main with the same setup: 169/169, so the only difference is the new replay_texture_barrier. Not run on both: 9 skipped (no game package or shader corpus, LEONIDA_VK_VALIDATION / LEONIDA_FIRST_LIGHT_DIR unset) and gameinput disabled (no SDL3).
  • replay_texture_barrier 50 times (--repeat until-fail:50): 50/50 pass. replay_cpu_bench_selftest passes, with its new no-allocation check.
  • Negative control: built against main's vulkan_replay.h, the new test fails only on "emit allocates nothing for a barrier with one piece" (1 allocation). All other checks pass, so the emitted barriers are unchanged.
  • Both tests run on the CPU (a stub records vkCmdPipelineBarrier2), so the GPU is not exercised. loader_test was not run.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested ea648bc merged onto main 3faac55 on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, RelWithDebInfo:

  • Full ctest: 179/179.
  • replay_cpu_bench_selftest and replay_texture_barrier: pass 20 of 20 runs each, and pass under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Warnings: none new in the files this PR changes.

AI-assisted (Claude Code).

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vibryxcx

Copy link
Copy Markdown

Tested ea648bc on AMD Radeon RX 6800 (RDNA2, discrete, gfx1030), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200), MSVC 14.44, RelWithDebInfo: pass.

  • New replay_texture_barrier passes; the rest of ctest and run_gpu_tests.py (201/217, 16 skipped) match main here (d3d12_sparse_queue_family_contract fails on main on this card too).
  • Context: the test stubs vkCmdPipelineBarrier2 and counts allocations, so it's CPU-deterministic; this run only adds a discrete RDNA2 on AMD's Windows driver next to the RDNA2 iGPU, NVIDIA and Intel runs above, and nothing differed.

No game files. AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified on Linux at ea648bc, clean merge against current main 1cc6e3c. compileall is clean and the Python guards around runtime/CMakeLists pass (test_ctest_registration, test_build_runtime_fallback, test_cmake_vulkan_missing, test_runtime_paths — all OK). C++ and cmake were not built on this box, so replay_texture_barrier_test and the bench row are static review: the stack array's capacity 2 matches LayoutPieces' inline capacity (vulkan_replay.h:601), is already included, an empty piece list gives barrier count 0 with the array never read, >2 pieces still take the vector path, and the allocation counter reserves the recording vector outside the measured region, so the zero-allocation assertion is a real regression guard. Nothing touches game data. CI: 4 checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@ramenbellic

ramenbellic commented Nov 23, 2026 •

Copy link
Copy Markdown

This was merged into main as 53a5a7df8 ("Merge pull request #4468"), and the commits pushed after that are on main as well: merging the current head ea648bc50 onto main 867d922f0 changes no file. There's nothing left to merge, so it can be closed.

(Checked with git merge-tree --write-tree main <head>: the result is identical to main's tree.)

(PR cleanup: #8919.) AI-assisted (Claude Code).

@ne1i

ne1i commented Nov 23, 2026

Copy link
Copy Markdown

Checked ea648bc5 against main 867d922f on Linux: merging this head produces a tree identical to main

  • CTest target replay_texture_barrier: 100% tests passed out of 1 with the merge, and 100% with the PR's runtime files reverted, in a build that recompiled nothing (warm build 8 ms).
  • The agent reports the whole-tree diff against main is empty for every file the PR touches (runtime/CMakeLists.txt, runtime/src/vulkan_replay.h, replay_cpu_bench.cpp, replay_texture_barrier_test.cpp), which matches: there is nothing left to merge, so the behaviour is already on main.

If that is right, the PR can probably be closed as superseded.

Independent check (not the author). Run by an AI agent (DeepSeek V4.1 Flash) and re-run by the orchestrating session (Claude Code) before posting.

@S1m0ne1997

Copy link
Copy Markdown
Contributor

Thanks for this, @shdann. Both commits of this PR are already on main: "replay: emit keeps an enhanced texture barrier's image barriers on the stack" as 7b1e5b40b, and the bench-test fix as c4486309c (the diff is fully present on main 867d922f0). Could you close this PR when you have a moment?

(PR cleanup, #8919; AI-assisted, Claude Code)

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

A merge of this head (ea648bc) into main gives the same tree as main. All the changes are already on main (53a5a7d, as ramenbellic said). I close this pull request. AI-assisted.

@shdann shdann closed this Nov 23, 2026
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.