Repository navigation
perf(runtime): inline draw views and move into CommandList to eliminate allocations (D3D-289) - #2483
Conversation
…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
|
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.
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, |
|
Tested 90a046e (merged on current main) on RTX 3090, driver 616.56, Windows 11 23H2: pass. MSVC 2022 / Ninja, Vulkan SDK 1.4.363. Overlap: #2471 ("recording a draw allocates nothing", D3D-289) has the same goal and changes the same three files. The two branches conflict in AI-assisted test run. |
|
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
AI-assisted (Claude). |
|
Tested 90a046e (on
AI-assisted (Claude Code). |
|
Tested 90a046e on AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2, Vulkan 1.4.359, Windows 11: pass.
Tools: MSVC 14.44 ( |
|
Tested 90a046e on Windows 11 (26200), MSVC 19.4x (VS 2022 Build Tools), Ninja RelWithDebInfo: pass on both GPUs of a two-GPU PC.
No game files. AI-assisted check (Claude Code), Vonsek3. |
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.
#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.
Summary
Addresses D3D-289 (follow-up finding to #2123 by @traeb0n).
In
replay_cpu_benchand 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.cppheap-allocated a dynamicstd::vector<TargetView>for render targets and anotherstd::vector<VertexBufferView>for vertex buffers (copying from the list's 32-element array).CommandList::draw(const Draw&)then copied both vectors again into theCommandvariant insidecommands_, resulting in 4 heap allocations per draw.Changes
runtime/src/command_list.h(leonida::Draw):render_targetsas fixedstd::array<TargetView, 8>(matchingD3D12_SIMULTANEOUS_RENDER_TARGET_COUNT= 8) and trackedrender_target_count.vertex_buffersas fixedstd::array<VertexBufferView, 32>(matchingD3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT= 32).bool draw(Draw&&)overload that moves the command directly intoappend(std::move(draw)).runtime/shims/d3d12_com.cpp:render_targets_asstd::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT>, avoiding heap reallocations onOMSetRenderTargets.vertex_buffers_without copying into a temporary vector.drawwithcheck(list_.draw(std::move(draw))).runtime/src/vulkan_replay.h:slot < draw.render_target_count && slot < draw.render_targets.size().runtime/test/d3d12_com_test.cpp:Drawmove constructibility, move assignability, and inline array sizes.docs/d3d12_vulkan.mdwith the contract details.Testing
python -m compileall -q make_game.py tools tests: OKpython -I make_game.py --help: OKstatic_assertchecks ind3d12_com_test.cpp: passedItem: D3D-289
Depends on: #2123