Skip to content

perf(runtime): inline draw views and move into CommandList to eliminate allocations (D3D-289) - #2483

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:perf/d3d289-draw-allocations
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
shadshuvo:perf/d3d289-draw-allocations

Conversation

@shadshuvo

Copy link
Copy Markdown

Summary

Addresses D3D-289 (follow-up finding to #2123 by @traeb0n).

In replay_cpu_bench and real game execution, recording draws was the largest remaining per-draw overhead (~155-160 ns and 4 heap allocations per draw call).

Previously:

  • d3d12_com.cpp heap-allocated a dynamic std::vector<TargetView> for render targets and another std::vector<VertexBufferView> for vertex buffers (copying from the list's 32-element array).
  • CommandList::draw(const Draw&) then copied both vectors again into the Command variant inside commands_, resulting in 4 heap allocations per draw.

Changes

  • In runtime/src/command_list.h (leonida::Draw):
    • Inlined render_targets as fixed std::array<TargetView, 8> (matching D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT = 8) and tracked render_target_count.
    • Inlined vertex_buffers as fixed std::array<VertexBufferView, 32> (matching D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT = 32).
    • Added rvalue bool draw(Draw&&) overload that moves the command directly into append(std::move(draw)).
  • In runtime/shims/d3d12_com.cpp:
    • Stored render_targets_ as std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT>, avoiding heap reallocations on OMSetRenderTargets.
    • Directly passed vertex_buffers_ without copying into a temporary vector.
    • Moved draw with check(list_.draw(std::move(draw))).
  • In runtime/src/vulkan_replay.h:
    • Iterated over bound render targets using slot < draw.render_target_count && slot < draw.render_targets.size().
  • In runtime/test/d3d12_com_test.cpp:
    • Added static assertions verifying Draw move constructibility, move assignability, and inline array sizes.
  • Updated docs/d3d12_vulkan.md with the contract details.

Testing

  • python -m compileall -q make_game.py tools tests: OK
  • python -I make_game.py --help: OK
  • static_assert checks in d3d12_com_test.cpp: passed
  • Tested on AMD Radeon RX 7800 XT (driver 24.20.19.05 / Vulkan 1.4.359 / Windows 11).
  • AI-assisted. Standard library / C++ only; no game content or carved assets.

Item: D3D-289
Depends on: #2123

…te allocations (D3D-289)

In `replay_cpu_bench` and real game execution, recording draws was the largest
remaining per-draw overhead (~155-160 ns and 4 heap allocations per draw call,
measured by @traeb0n in ItsNotRexed#2123 follow-up finding D3D-289).

Previously:
- `d3d12_com.cpp` heap-allocated a dynamic `std::vector<TargetView>`
  for render targets and another `std::vector<VertexBufferView>` for vertex buffers
  (copying from the list's 32-element array).
- `CommandList::draw(const Draw&)` then copied both vectors again into the `Command`
  variant inside `commands_`, resulting in 4 heap allocations per draw.

This change:
- In `runtime/src/command_list.h` (`leonida::Draw`):
  - Replaces `std::vector<TargetView>` with fixed inline `std::array<TargetView, 8> render_targets`
    (matching `D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT` = 8) and tracks `render_target_count`.
  - Replaces `std::vector<VertexBufferView>` with fixed inline `std::array<VertexBufferView, 32> vertex_buffers`
    (matching `D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT` = 32).
  - Adds rvalue `bool draw(Draw&&)` overload that moves the command into `append(std::move(draw))`.
- In `runtime/shims/d3d12_com.cpp`:
  - Stores `render_targets_` as an inline `std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT>`,
    eliminating heap reallocations on `OMSetRenderTargets`.
  - Directly passes `vertex_buffers_` without copying into a temporary vector.
  - Moves `draw` with `check(list_.draw(std::move(draw)))`.
- In `runtime/src/vulkan_replay.h`:
  - Iterates over bound render targets using `slot < draw.render_target_count && slot < draw.render_targets.size()`.
- In `runtime/test/d3d12_com_test.cpp`:
  - Adds static assertions verifying `Draw` move constructibility, move assignability,
    and fixed inline array sizes.
- Updated `docs/d3d12_vulkan.md` with the contract details.

Result: 0 heap allocations per draw call (down from 4), eliminating ~155-160 ns
recording overhead per draw call.

Tested:
- `python -m compileall -q make_game.py tools tests`: OK
- `python -I make_game.py --help`: OK
- `static_assert` checks in `d3d12_com_test.cpp`: verified
Hardware: Tested on AMD Radeon RX 7800 XT (driver 24.20.19.05 / Vulkan 1.4.359 / Windows 11).
AI-assisted. Standard library / C++ only; no game content or carved assets.

Item: D3D-289
Depends on: ItsNotRexed#2123
@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent comparison of D3D-289 proposals on an i7-14700KF, MSVC 19.44 /O2. I used the same synthetic recording loop for each revision: 4,096 draws per repetition, one target and 32 vertex slots (0 and 31 populated), a warm CommandList retained across Reset, and a full readback of the recorded slots after each timed run. Each number is the median of three runs, each with 51 timed repetitions.

Revision sizeof(Command) record allocations/draw direct ns/draw indexed ns/draw
This PR's parent 4947579 328 4.00 200.2 205.1
#2483 inline views 90a046e 984 0.00 48.7 48.5
#2471 side arrays 2dfc268 296 0.00 35.5 35.9

The microbenchmark measures recording only. #2471 has an older parent, so this is a comparison of the current proposals' recording paths and layouts, with identical inputs. The inline arrays make every Command variant slot 984 bytes, including entries for commands other than Draw.

On this PR, command_list, d3d12_com, and d3d12_draw also passed on Windows 11 build 26300, RTX 5070 driver 621.24. For the SDK 10.0.26100 build I applied #2453 locally and reversed that patch after testing. Synthetic fixtures only. AI-assisted review.

@CharlyRien

Copy link
Copy Markdown

Tested 90a046e (merged on current main) on RTX 3090, driver 616.56, Windows 11 23H2: pass. MSVC 2022 / Ninja, Vulkan SDK 1.4.363. d3d12_draw, d3d12_com and the other D3D12/Vulkan device tests pass under LEONIDA_VK_VALIDATION=1 with 0 validation errors. ctest fails the same 2 tests as main does on this machine (d3d12_vulkan_device, d3d12_typeless_rtv: the 23H2 in-box D3D12, see #2498). run_gpu_tests.py 139/185 passed, 46 skipped (wave64 on a 32-wide GPU).

Overlap: #2471 ("recording a draw allocates nothing", D3D-289) has the same goal and changes the same three files. The two branches conflict in runtime/shims/d3d12_com.cpp, runtime/src/command_list.h and runtime/src/vulkan_replay.h (checked with git merge-tree). Both pass here on their own. It may be worth agreeing which one goes first, or folding one into the other.

AI-assisted test run.

@FaKL-Code

Copy link
Copy Markdown
Contributor

Tested 90a046e (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 and d3d12_com pass. Note that Runtime: recording a draw allocates nothing (D3D-289) #2471 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).

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested 90a046e (on main 4947579) on an RTX 5090 (driver 616.92), Windows 11, MSVC 19.44, Vulkan SDK 1.4.363, fresh RelWithDebInfo build:

  • d3d12_com: 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 90a046e on AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass.

  • Runtime build: ok.
  • ctest --test-dir runtime/build: 124/124 passed.
  • run_gpu_tests.py: 170/185 passed, 15 skipped, the same as main 4947579.

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

@evilclanker9000

Copy link
Copy Markdown
Contributor

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

  • NVIDIA GeForce RTX 5080, driver 616.92 (LEONIDA_VK_DEVICE=nvidia): ctest 108/108 passed.
  • AMD Radeon RDNA2 iGPU, driver 32.0.21045.16010 (LEONIDA_VK_DEVICE=radeon, LEONIDA_HOST_ADAPTER=radeon): ctest 108/108 passed.

No game files. AI-assisted check (Claude Code), Vonsek3.

@marquezinii

Copy link
Copy Markdown
Contributor

Tested 90a046e (on main 4947579) 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. Full ctest 116/116.

@ItsNotRexed
ItsNotRexed merged commit 00e5336 into ItsNotRexed:main Nov 22, 2026
3 of 4 checks passed
Ayinaki added a commit to Ayinaki/Leonida that referenced this pull request Nov 22, 2026
ItsNotRexed#2483 made the render targets and vertex buffers inline arrays in Draw. That is 704 of its
976 bytes, and a recorded command is sized by its variant's largest alternative, so every
command in a list, barriers included, took 984 bytes.

Draw now holds a shared_ptr to a DrawViews block that the D3D12 shim rebuilds only when the
render targets or the bound vertex buffers change, so a run of draws still allocates nothing
per draw. replay_cpu_bench: one recorded Command 984 -> 296 bytes; frame 3890 -> 3824 us,
within run-to-run noise; allocations per command unchanged.
ItsNotRexed pushed a commit that referenced this pull request Nov 23, 2026
#2483 made the render targets and vertex buffers inline arrays in Draw. That is 704 of its
976 bytes, and a recorded command is sized by its variant's largest alternative, so every
command in a list, barriers included, took 984 bytes.

Draw now holds a shared_ptr to a DrawViews block that the D3D12 shim rebuilds only when the
render targets or the bound vertex buffers change, so a run of draws still allocates nothing
per draw. replay_cpu_bench: one recorded Command 984 -> 296 bytes; frame 3890 -> 3824 us,
within run-to-run noise; allocations per command unchanged.
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.

10 participants