Skip to content

Runtime: recording a draw allocates nothing (D3D-289) - #2471

Closed
requiemsnkrp wants to merge 2 commits into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-289-draw-alloc
Closed

requiemsnkrp wants to merge 2 commits into
ItsNotRexed:mainfrom
requiemsnkrp:d3d-289-draw-alloc

Conversation

@requiemsnkrp

Copy link
Copy Markdown

What and why

D3D-289 (claim; @traeb0n had first refusal and was asked, no answer). Recording a draw allocated on the heap on every call: leonida::Draw owned two vectors (render targets, the 32 vertex-buffer slots), built at d3d12_com.cpp:927-928, copied into a temporary Command by draw(const Draw&) → append(const Command&), and copied again by commands_.push_back. 6 allocations per draw on main (the finding's 4 was measured on top of #901, which already moves in append).

  • Draw holds two DrawRange {first, count} instead of vectors; the CommandList keeps one array for all its draws' render targets and one for their vertex buffers. Reset clears them and keeps their storage, so once grown, recording a draw allocates nothing.
  • CommandList::draw(const Draw&, span<const TargetView>, span<const VertexBufferView>) copies the spans in and builds the Command in place; render_targets(draw) / vertex_buffers(draw) read them back; execute_bundle copies a bundle's arrays into the parent and shifts its draw ranges.
  • record_draw (D3D12 layer) passes render_targets_ and the 32-slot array as spans; prepare_draw reads the spans. What is recorded and replayed is unchanged (all 32 slots still stored). One Command shrinks from 328 to 296 bytes.
  • docs/d3d12_vulkan.md: where a draw's arrays live, and that the bench checks it.

Bench

replay_cpu_bench on main has no draw row yet (#2123 adds it), so the bench gets "draw" and "draw indexed" rows here, with #2123's draw scene setup. Ryzen 5 5600X, median of 5 runs (each a median of 25 reps):

row record ns record allocations prepare / emit allocations
draw 315.1 → 47.5 6 → 0 10 / 3 (unchanged)
draw indexed 312.1 → 48.4 6 → 0 10 / 3 (unchanged)

No other row changed its allocation counts; times within noise. The prepare/emit allocations are replay-side (#893, #2123), left alone. The two draw rows now fail the bench (and replay_cpu_bench --self-test, the replay_cpu_bench_selftest ctest) if recording a draw allocates.

Tested

  • command_list_test: new block — a draw's arrays are copies, ExecuteBundle shifts a bundle's draw ranges, an empty draw adds nothing.
  • Full ctest without the window tests (RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51): failures only d3d12_com (segfault), d3d12_debug_markers, d3d12_indirect_rt, d3d12_draw, all failing the same way on main 4eb6155 without this PR (d3d12_draw: "bundle blend pixel (0, 0): got 127,0,127,255, expected red 64", also on main).

Conflicts expected with #2123 (replay_cpu_bench.cpp draw scene, prepare_draw in vulkan_replay.h); happy to rebase whichever lands second.

No game files. AI-assisted (Claude).

- command_list.h: a draw's render targets and vertex-buffer views move out of the recorded Draw into two arrays
  the CommandList keeps for all its draws (DrawRange: first, count). CommandList::draw takes them as spans and
  emplaces the Draw directly, so it no longer copies two vectors into a temporary Command and again into the
  list; CommandList::render_targets / vertex_buffers read them back, ExecuteBundle moves a bundle's draws to the
  parent's copies and Reset keeps the arrays' storage. One recorded Command: 328 -> 296 bytes.
- d3d12_com.cpp: record_draw passes the list's render targets and 32 vertex slots as spans instead of building
  two vectors per draw. vulkan_replay.h: prepare_draw reads the two spans; what is replayed is unchanged.
- replay_cpu_bench: "draw" and "draw indexed" rows (scene pipeline and geometry arena as in ItsNotRexed#2123), checked on
  every run to record with no heap allocation. Ryzen 5 5600X, median of 5 runs: draw record 315.1 -> 47.5 ns and
  6 -> 0 allocations, draw indexed 312.1 -> 48.4 ns and 6 -> 0; prepare and emit unchanged.
- command_list_test: draw arrays are copies, ExecuteBundle rebases them, an empty draw adds nothing.
- d3d12_vulkan.md: where a draw's arrays live.

AI-assisted.
@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent check of 2dfc268 on Windows 11 build 26300, MSVC 19.44, SDK 10.0.26100.0, i7-14700KF, RTX 5070 driver 621.24.

  • command_list and replay_cpu_bench_selftest passed as written. The release benchmark measured 0.00 allocations per recorded draw for both 4,096-command rows; 27.1 ns (draw) and 27.5 ns (indexed) per command during this run. It printed 296 bytes per recorded Command.
  • Stock d3d12_draw failed identically on the PR and its exact parent 4eb6155. Both logged “fence signal 21 is not above 30” before reading the same stale RGBA (127,0,127,255) for the bundle-blend check.
  • Current main already uses fence 31 in 49475796. With only the same 21-to-31 change in the local PR test, the entire d3d12_draw passed, including the bundle blend, indexed strip-cut controls, and 4× MSAA readback. The runtime under test was the stock PR and I restored the test source afterward.

Synthetic fixtures only. AI-assisted review.

@CharlyRien

Copy link
Copy Markdown

Tested 2dfc268 (merged on current main) on RTX 3090, driver 616.56, Windows 11 23H2: pass. MSVC 2022 / Ninja, Vulkan SDK 1.4.363. d3d12_draw and the other D3D12/Vulkan device tests pass under LEONIDA_VK_VALIDATION=1 with 0 validation errors. ctest fails 3 tests, all of which fail without this PR on this machine too: loader (security software scanning fresh PE files), and d3d12_vulkan_device / d3d12_typeless_rtv (the 23H2 in-box D3D12 refuses cases the comparison expects; see #2498). run_gpu_tests.py 139/185 passed, 46 skipped (wave64 on a 32-wide GPU).

AI-assisted test run.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 2dfc268 (merged onto main 4947579) on NVIDIA GeForce RTX 4060 Ti, driver 610.88 (32.0.16.1088), Windows 11 26200, MSVC 14.51.36231 (VS 2026), Vulkan SDK 1.4.363.0: pass

  • Clean configure + build of runtime/ (Visual Studio generator, RelWithDebInfo): no new warnings (same C4201/C4456/C4996 counts as main).
  • ctest -C RelWithDebInfo -j 4: 116/116 passed (loader run on its own; 7 skipped for missing inputs or optional components, gameinput disabled, as on main).
  • d3d12_vulkan_device, command_list, d3d12_com and d3d12_msaa_draw also pass with LEONIDA_VK_VALIDATION=1.
  • command_list passes. Note that perf(runtime): inline draw views and move into CommandList to eliminate allocations (D3D-289) #2483 targets the same item (D3D-289) and touches the same files (command_list.h, vulkan_replay.h, d3d12_com.cpp); both pass here, and they will conflict with each other.

AI-assisted (Claude).

@yagoviccari1-spec

Copy link
Copy Markdown

Tested the merge of PR #2471 head 2dfc268 with origin/main 4947579 (tree 60142531) on Windows 11 build 26200.9974, NVIDIA RTX 5060 Ti / driver 621.24 (Vulkan 1.4.357), MSVC 19.44.35207, CMake 4.5.1 + Ninja 1.13.2, llvm-mc 21.1.8 and SPIRV-Tools v2026.4 on PATH:

  • Full ctest --test-dir runtime/build -C RelWithDebInfo --output-on-failure: 124 ran, 0 failed (~104 s), same as main 4947579 on this machine. d3d12_draw included and passing (0.25 s).
  • At the bare PR head (based on 4eb6155) d3d12_draw fails with the stale-fence bug (fence 21 after an earlier signal 30); that is the test bug already fixed on main by ed25f93, not a regression from this PR. I excluded it only by merging main, not by patching the test.
  • Scope: this is a functional no-regression check of the draw-recording changes. I did not measure the "allocates nothing" claim.

No game files used. Ran with an AI agent on my machine.

@Gabzinnn12

Copy link
Copy Markdown

Tested 2dfc268 on AMD Radeon RX 7600 (RDNA3), AMD 26.11.2 (LLPC), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8: command_list_test passes.

  • command_list_test: exit 0 with and without LEONIDA_VK_VALIDATION=1 (+ VK_LAYER_VALIDATE_SYNC=1).
  • d3d12_com_test, d3d12_m2_textured, d3d12_m3_frame, d3d12_m4_compute, d3d12_translated_draw, d3d12_buffer_uav, d3d12_compute, d3d12_vulkan_device: exit 0 in both modes, no validation errors reported.
  • ctest: 1 failed out of 116 (109 passed, 6 skipped, gameinput disabled): d3d12_draw (bundle blend pixel 128,0,128,255). Run alone it passes without validation and reports VUID-vkDestroyPipeline-pipeline-00765 with validation. That is the fence-21 issue in this PR's base 4eb6155: in my earlier run of main 4947579, which has Tests: wait for the bundle blend draws in d3d12_draw (fence 31, not 21) #1995's fix, d3d12_draw passed.
  • Not run: replay_cpu_bench, so no numbers on the allocation savings.

AI-assisted (Claude Code, reviewed with Codex). No game files.

@gkissel

gkissel commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested 2dfc2686 (merged onto main 49475796, clean) on a Ryzen 7 5800H with an NVIDIA GeForce GTX 1650 Mobile, NVIDIA 620.70.06, CachyOS Linux: pass, with one bench row worth a look.

Windows runtime under Wine 11.21 (clang-cl 23.1.4 + xwin, SDK 10.0.26100): builds clean. Full ctest gives 121 of 124, the same as main plus a log-only change in the same environment. The three failures are environmental: device_proxy (the host comparison runs on Wine's vkd3d) and d3d12_present/xbox_present (no EGL screen under Wine).

Linux (GCC 16.2.1): command_list_test passes, and replay_cpu_bench --self-test gives "14 kinds and a 168-command frame recorded and replayed, ok".

replay_cpu_bench --csv, median of 5 runs, PR vs main 49475796 (main has no draw row):

row record ns total ns allocations record/prepare/emit
draw 32.2 366.3 0 / 0 / 1
draw indexed 32.1 364.7 0 / 0 / 1
every other row within ±2 ns of main within noise, except below unchanged
prepared_entries_fresh 0 105.5 → 129.2 (+22%) 0 / 0 / 0 both

The prepared_entries_fresh difference is not noise here. Over 10 interleaved runs, main gave 103.6–108.1 ns (one outlier at 124.0) and this PR gave 126.7–132.1 ns. It is ~24 ns per command, small next to what the draw rows gain. Since the PR says no other row changed, it may be the smaller Command or the span read-back on the fresh-entry path; I haven't profiled it.

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

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Windows + GPU check: AMD Radeon RX 9070 (RDNA4), Windows 11, MSVC Release, Vulkan SDK 1.4.363. Tested head 2dfc268 merged onto main 4947579 (clean merge), full build, full ctest -C Release -j 4: 100% tests passed out of 116 | new failures vs main: none. The main baseline on the same machine passes 116/116, so nothing regresses here.

AI-assisted (Claude Code); runs on my machine.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 2dfc268, merged onto main 4947579 (no conflicts), on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, fresh RelWithDebInfo build:

  • command_list: passes 20 of 20 runs, and passes under LEONIDA_VK_VALIDATION=1 with 0 validation messages.
  • Full ctest: 125/125. No new /W4 warnings.

AI-assisted (Claude Code).

@bvtterbroot

Copy link
Copy Markdown

Tested 2dfc268 on AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass apart from a failure inherited from the base.

  • Runtime build: ok.
  • ctest --test-dir runtime/build: 115/116; d3d12_draw fails, but not because of this PR: it fails the same way on the PR's base 4eb6155 (bundle blend pixel (0, 0): got 128,0,128,255, expected red 64, 3 of 3 runs on the base and on this PR) and passes on main 4947579. A rebase on main should clear it.
  • run_gpu_tests.py: 167/180 passed, 13 skipped, no failures.

Tools: MSVC 14.44 (scripts/build_runtime.ps1), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (WSL).

@requiemsnkrp

Copy link
Copy Markdown
Author

@gkissel thanks for the careful numbers. I looked into prepared_entries_fresh and it isn't this PR's code; it's where the row's 27.5 MB buffer lands.

  • That row doesn't touch the change: it only builds and frees a std::vector<detail::Prepared> of 21,504 entries. It never calls CommandList, render_targets(draw) or vertex_buffers(draw) (only prepare_draw reads those). Prepared is identical in both builds (1280 bytes under GCC); in the disassembly its copy constructor and _M_fill_assign differ only in address offsets and cold exception paths.
  • Windows/MSVC (Ryzen 5 5600X), 12 interleaved runs of main 4947579 vs GitHub's merge 1e4678e: fresh +0.5%, kept +1.2%, frame total −3.4% (load noise).
  • Linux/GCC 15 -O2 (WSL2, same CPU): I do see it: +5.8% over 30 quiet interleaved runs, and with ASLR off ~97 → 118 ns, close to your 105.5 → 129.2. kept (the path VulkanQueue::execute uses) doesn't move.
  • Cause: the two new draw rows run before this measurement and change the heap's allocation history, so the buffer lands at another offset (…67eaa0 on main, …718610 with the PR, logged via malloc). Removing only the two draw rows from the PR's bench puts it back at …67fbe0 and the row returns to main's level (92.2 vs 89.8). With a preloaded malloc wrapper placing the buffer at the same address, main and the PR are equal at every offset I tried (offset 0x610 is the slowest for both: 107.0 vs 107.4). The row also swings 87–130 ns for main alone just by changing an environment variable's size (stack address). The mechanism is probably an aliasing penalty between the stack temporary and the buffer at some offsets; not confirmed with counters.
  • Not changed here: measuring the two prepared_entries rows before the per-kind rows would stop future rows from moving them, but that changes the bench layout from Runtime: VulkanQueue keeps its PreparedList storage between executions (layer CPU cost per command halved) #893/Runtime: a draw's prepared data lives beside the per-command entries (Depends on #901) #2123, so I'd leave it to you or @traeb0n if you want it.

AI-assisted (Claude).

@traeb0n

traeb0n commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Code review of 2dfc268 (read only, no runs): looks right to me, nothing blocking.

What I checked:

  • The ranges can't dangle. append is private and commands() is const, so a Draw gets into a list only through draw() (which overwrites whatever ranges the caller passed) or execute_bundle (which shifts the bundle's ranges by the parent's sizes before any read). Ranges are indices, not pointers, so copying or moving a CommandList keeps them valid. execute_bundle rejects this == &bundle before inserting from the bundle's vectors, so the self-insert case can't happen.
  • reset() clears both arrays, and draw() keeps the old return values: false when closed or without a pipeline, true for an empty draw that isn't recorded.
  • Nothing else reads a Draw's views. The only other place that handles Draw is emit (vulkan_replay.h:3247), and it works from the prepared entry, not from the views.

One heads-up: this overlaps my #2123 in the same two files.

The two combine cleanly: prepare_draw(draw, list.render_targets(draw), list.vertex_buffers(draw), resolve, vk, prepared_draw). This PR has more test runs, so if it lands first I'll rebase #2123 onto it and fold my bench additions into its Scene and draw() rather than keeping two copies.

AI-assisted (Claude Code).

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 2dfc268 on Windows 11 (26200), MSVC 19.4x (VS 2022 Build Tools), Ninja RelWithDebInfo, on both GPUs of a two-GPU PC:

  • AMD Radeon RDNA2 iGPU, driver 32.0.21045.16010 (LEONIDA_VK_DEVICE=radeon, LEONIDA_HOST_ADAPTER=radeon): ctest 104/104 passed.
  • NVIDIA GeForce RTX 5080, driver 616.92 (LEONIDA_VK_DEVICE=nvidia): 103/104. The failure is vk_pipeline_check_cli: lds_bytes=-1: expected a malformed index, got 1 (MODULE FAIL example).

That failure doesn't look like this PR's. The PR doesn't touch vk_pipeline_check or its CLI test. Since this branch's base (4eb6155), main has changed both (2a1bd03, #2111, #2226). A PR built on a newer main (#2483, 108 tests) passes vk_pipeline_check_cli on the same RTX 5080. A rebase should clear it; I'm not claiming main fails. No game files. AI-assisted check (Claude Code), Vonsek3.

@gkissel

gkissel commented Nov 22, 2026

Copy link
Copy Markdown

@requiemsnkrp thanks, that explains my +22%. My run was Linux/GCC on a 5800H, so it matches what you saw under GCC. The row never calls the changed code, and the effect disappears when the buffer is pinned to the same address, so it's heap placement and not this PR. I withdraw the concern about prepared_entries_fresh. The rows that do exercise the change (draw record 32 ns, 0 allocations) were the point, and they hold. I agree that moving the two prepared_entries rows ahead of the per-kind rows belongs in a separate bench PR. AI-assisted.

Conflict in CommandList::execute_bundle: main now copies a stretch [first, last) of the bundle's commands (bundle draws are deferred calls played on the parent). The draws in the copied stretch get their render targets and vertex buffers copied from the bundle's arrays into the parent's, so a stretch copies only its own draws' runs; command_list_test checks two stretches of a bundle.
@requiemsnkrp

Copy link
Copy Markdown
Author

Merged main ec8ec4c into this branch (1291ae1). One conflict, in CommandList::execute_bundle: main now copies a stretch [first, last) of a bundle's commands (#2103 plays the bundle's state calls and draws on the parent). Resolved in main's way. Each draw in the copied stretch gets its render targets and vertex buffers copied from the bundle's arrays into the parent's, so a stretch copies only its own draws' runs. command_list_test now also executes two stretches of a bundle.

Retested on RTX 3060 with LEONIDA_VK_VALIDATION=1 (0 validation messages):

  • command_list_test: exit 0
  • replay_cpu_bench --self-test: 14 kinds and a 168-command frame recorded and replayed, ok. Bench rows draw and draw indexed: alloc:r 0.00
  • d3d12_draw_test: exit 0, M1: five draws and a culled triangle matched all 4096 pixels ...; d3d12_bundle passes

Full ctest (-E "present|swapchain|frame_pacer"): 103/116. The only failures are the 13 d3d12_* tests that segfault at startup (multithread, d24_support, debug_markers, fence_timing, fence_events, method_variants, rgb96, depth_resolve, resource_desc, node_mask, indirect_zero, stencil_copy, query_rules). main ec8ec4c without this PR fails the same 13.

AI-assisted (Claude).

@marquezinii

Copy link
Copy Markdown
Contributor

Tested 2dfc268 on RTX 5070, driver 621.24, Windows 11 26H2 (26300), MSVC 14.52 (VS Build Tools 18), RelWithDebInfo, with VK_LAYER_KHRONOS_validation on (LEONIDA_VK_VALIDATION=1): pass once merged with current main. This branch is based on 4eb6155, where d3d12_draw already fails on this machine without the PR. Merged locally with main 4947579 (no conflicts, 5488b9d6), full ctest is 116/116.

@ddm-j

ddm-j commented Nov 22, 2026 •

Copy link
Copy Markdown

Tested 1291ae1 under Wine 11.21 (Fedora 45) on AMD Radeon 610M (RDNA2 iGPU, RADV Mesa 26.2.6) and NVIDIA RTX 4070 Ti (615.78.08): pass

This adds AMD and Linux to the Windows NVIDIA checks above. The Windows runtime was cross-built with clang-cl 23.1.5 and xwin (MSVC 14.44, SDK 10.0.26100), and its ctest suite was run under Wine and Xvfb, one GPU at a time with LEONIDA_VK_DEVICE.

command_list, replay_cpu_bench_selftest, d3d12_draw and d3d12_bundle pass on both GPUs, as does the rest of the suite. It's the same on the PR's base:

Wine ctest base ec8ec4c9 this PR
RADV 109 passed, 18 skipped, 0 failed 109 passed, 18 skipped, 0 failed
NVIDIA 109 passed, 18 skipped, 0 failed 109 passed, 18 skipped, 0 failed

Not run, because they fail in this environment on every commit:

  • device_proxy: Fedora's Wine has no host D3D12 device.
  • d3d12_present: it times out on a real GPU under Xvfb.

#2483, which targets the same item (D3D-289), was merged at 20:00 UTC, after this run. This head is based on ec8ec4c9, before that merge, so it may now conflict with #2483 or be superseded by it.

AI-assisted (Claude Code); numbers copied from the run logs.

@lunisahdidouch

Copy link
Copy Markdown

Tested 1291ae1 on an RX 9070 (RDNA4), AMD 26.11.1 (LLPC), Vulkan 1.4.359, Windows 11: pass

Full ctest run: 130/130 passed.

Two things I noticed along the way, in case they're useful:

  • The earlier head 2dfc268 (before the main merge) failed two tests here: d3d12_draw with "bundle blend pixel (0, 0): got 128,0,128,255, expected red 64", and vk_pipeline_check_cli with "--count : expected usage error, got Process terminated due to timeout". Both pass on 1291ae1 and on main ec8ec4c, so it looks like the merge took care of it, but the bundle one is in the area this PR touches.
  • d3d12_draw took 16.4 s on 1291ae1 vs 1.1 s on main ec8ec4c on the same machine. Only one run each, so could be noise, but it's a big gap.

AI-assisted

@requiemsnkrp

Copy link
Copy Markdown
Author

@lunisahdidouch thanks. On the d3d12_draw time: measured here, 7 interleaved runs each (RTX 3060, low priority, machine busy): main ec8ec4c median 636 ms, this PR 1291ae1 median 624 ms; with validation 571–904 vs 604–753 ms. Same output apart from pointers. The change only touches CPU-side recording, so a 15 s gap is most likely a one-off (e.g. a cold driver shader cache) rather than the PR. (#2483 has since landed for D3D-289; the command-size follow-up is D3D-391.)

AI-assisted (Claude).

@requiemsnkrp

Copy link
Copy Markdown
Author

Closing: D3D-289 landed with #2483, and its command-size follow-up D3D-391 with #3693. Thanks to everyone who tested this one. AI-assisted (Claude).

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.