Skip to content

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

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

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

…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>
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.

…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).

@Vonsek

Vonsek commented Nov 23, 2026

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.

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.

7 participants