diff options
| author | Shadman PC <iamanthonytony@gmail.com> | 2026-11-22 23:11:32 +0600 |
|---|---|---|
| committer | Shadman PC <iamanthonytony@gmail.com> | 2026-11-22 23:11:32 +0600 |
| commit | 90a046eaa49062e8c6c8e9b431b7ef5fa302ae45 (patch) | |
| tree | eae817137740042956bcb828a0d247c138772b99 /runtime/src/command_list.h | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
perf(runtime): inline draw views and move into CommandList to eliminate 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 #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: #2123
Diffstat (limited to 'runtime/src/command_list.h')
| -rw-r--r-- | runtime/src/command_list.h | 44 |
1 files changed, 26 insertions, 18 deletions
diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 47a0561b3..c546b0612 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h | |||
| @@ -392,25 +392,28 @@ struct IndexBufferView { | |||
| 392 | // DrawInstanced / DrawIndexedInstanced, with everything they use captured when they are recorded: D3D12 reads | 392 | // DrawInstanced / DrawIndexedInstanced, with everything they use captured when they are recorded: D3D12 reads |
| 393 | // render-target and depth descriptors at OMSetRenderTargets, and every other piece of state is the list's state at | 393 | // render-target and depth descriptors at OMSetRenderTargets, and every other piece of state is the list's state at |
| 394 | // the draw. resource 0 marks an unbound render-target slot or no depth buffer. | 394 | // the draw. resource 0 marks an unbound render-target slot or no depth buffer. |
| 395 | // Render targets and vertex buffer views are inlined (8 simultaneous targets, 32 input slots), eliminating heap allocations | ||
| 396 | // during command list recording (D3D-289). | ||
| 395 | struct Draw { | 397 | struct Draw { |
| 396 | std::uintptr_t pipeline; | 398 | std::uintptr_t pipeline = 0; |
| 397 | std::array<std::uint32_t, 32> user_data; | 399 | std::array<std::uint32_t, 32> user_data{}; |
| 398 | std::vector<TargetView> render_targets; | 400 | std::array<TargetView, 8> render_targets{}; |
| 399 | TargetView depth; | 401 | TargetView depth{}; |
| 400 | std::vector<VertexBufferView> vertex_buffers; // by input slot | 402 | std::array<VertexBufferView, 32> vertex_buffers{}; // by input slot |
| 401 | IndexBufferView index; | 403 | IndexBufferView index{}; |
| 402 | std::uint32_t topology; // D3D_PRIMITIVE_TOPOLOGY | 404 | std::uint32_t topology = 0; // D3D_PRIMITIVE_TOPOLOGY |
| 403 | Viewport viewport; // the first; more viewports are not modelled yet | 405 | Viewport viewport{}; // the first; more viewports are not modelled yet |
| 404 | Rect scissor; // the first; D3D12 always scissors | 406 | Rect scissor{}; // the first; D3D12 always scissors |
| 405 | std::array<float, 4> blend_factor; | 407 | std::array<float, 4> blend_factor{1, 1, 1, 1}; |
| 406 | std::uint32_t stencil_front; | 408 | std::uint32_t stencil_front = 0; |
| 407 | std::uint32_t stencil_back; | 409 | std::uint32_t stencil_back = 0; |
| 408 | bool indexed; | 410 | bool indexed = false; |
| 409 | std::uint32_t count; // vertices or indices per instance | 411 | std::uint32_t count = 0; // vertices or indices per instance |
| 410 | std::uint32_t instances; | 412 | std::uint32_t instances = 0; |
| 411 | std::uint32_t first; // first vertex or first index | 413 | std::uint32_t first = 0; // first vertex or first index |
| 412 | std::int32_t base_vertex; | 414 | std::int32_t base_vertex = 0; |
| 413 | std::uint32_t first_instance; | 415 | std::uint32_t first_instance = 0; |
| 416 | std::uint32_t render_target_count = 8; | ||
| 414 | }; | 417 | }; |
| 415 | 418 | ||
| 416 | // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT. | 419 | // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT. |
| @@ -712,6 +715,11 @@ public: | |||
| 712 | if (!draw.count || !draw.instances) return !closed_; | 715 | if (!draw.count || !draw.instances) return !closed_; |
| 713 | return append(draw); | 716 | return append(draw); |
| 714 | } | 717 | } |
| 718 | bool draw(Draw&& draw) { | ||
| 719 | if (!draw.pipeline) return false; | ||
| 720 | if (!draw.count || !draw.instances) return !closed_; | ||
| 721 | return append(std::move(draw)); | ||
| 722 | } | ||
| 715 | 723 | ||
| 716 | // ClearRenderTargetView; the resource must be in render_target state at execution. No rects: the whole view. | 724 | // ClearRenderTargetView; the resource must be in render_target state at execution. No rects: the whole view. |
| 717 | bool clear_render_target(const TargetView& target, const ClearColor& color, std::span<const Rect> rects = {}) { | 725 | bool clear_render_target(const TargetView& target, const ClearColor& color, std::span<const Rect> rects = {}) { |