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 | |
| 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')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 15 | ||||
| -rw-r--r-- | runtime/src/command_list.h | 44 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 2 | ||||
| -rw-r--r-- | runtime/test/d3d12_com_test.cpp | 5 |
4 files changed, 41 insertions, 25 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 663f5f3a5..3ccf90a2d 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp | |||
| @@ -336,7 +336,8 @@ public: | |||
| 336 | void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range, | 336 | void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range, |
| 337 | const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override { | 337 | const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override { |
| 338 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); | 338 | if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); |
| 339 | render_targets_.assign(count, {}); | 339 | render_targets_.fill({}); |
| 340 | render_target_count_ = count; | ||
| 340 | for (UINT i = 0; i < count && targets; ++i) { | 341 | for (UINT i = 0; i < count && targets; ++i) { |
| 341 | const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; | 342 | const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; |
| 342 | if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); | 343 | if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); |
| @@ -910,7 +911,8 @@ private: | |||
| 910 | leonida::IndexBufferView index_{}; | 911 | leonida::IndexBufferView index_{}; |
| 911 | leonida::Viewport viewport_{}; | 912 | leonida::Viewport viewport_{}; |
| 912 | leonida::Rect scissor_{}; | 913 | leonida::Rect scissor_{}; |
| 913 | std::vector<leonida::TargetView> render_targets_; | 914 | std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; |
| 915 | UINT render_target_count_ = 0; | ||
| 914 | leonida::TargetView depth_{}; | 916 | leonida::TargetView depth_{}; |
| 915 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; | 917 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; |
| 916 | bool blend_factor_set_ = false; | 918 | bool blend_factor_set_ = false; |
| @@ -949,7 +951,8 @@ private: | |||
| 949 | index_ = {}; | 951 | index_ = {}; |
| 950 | viewport_ = {}; | 952 | viewport_ = {}; |
| 951 | scissor_ = {}; | 953 | scissor_ = {}; |
| 952 | render_targets_.clear(); | 954 | render_targets_.fill({}); |
| 955 | render_target_count_ = 0; | ||
| 953 | depth_ = {}; | 956 | depth_ = {}; |
| 954 | blend_factor_ = {1, 1, 1, 1}; | 957 | blend_factor_ = {1, 1, 1, 1}; |
| 955 | blend_factor_set_ = false; | 958 | blend_factor_set_ = false; |
| @@ -963,10 +966,10 @@ private: | |||
| 963 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. | 966 | // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. |
| 964 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; | 967 | if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; |
| 965 | leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_, | 968 | leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_, |
| 966 | {vertex_buffers_.begin(), vertex_buffers_.end()}, index_, topology_, viewport_, scissor_, | 969 | vertex_buffers_, index_, topology_, viewport_, scissor_, |
| 967 | blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, | 970 | blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, |
| 968 | base_vertex, first_instance}; | 971 | base_vertex, first_instance, render_target_count_}; |
| 969 | check(list_.draw(draw)); | 972 | check(list_.draw(std::move(draw))); |
| 970 | } | 973 | } |
| 971 | }; | 974 | }; |
| 972 | 975 | ||
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 = {}) { |
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 8ece08d1e..bd8ae2574 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h | |||
| @@ -1798,7 +1798,7 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct | |||
| 1798 | }; | 1798 | }; |
| 1799 | 1799 | ||
| 1800 | p.colors.assign(pipeline->color_formats.size(), {}); | 1800 | p.colors.assign(pipeline->color_formats.size(), {}); |
| 1801 | for (std::size_t slot = 0; slot < draw.render_targets.size() && slot < p.colors.size(); ++slot) { | 1801 | for (std::size_t slot = 0; slot < draw.render_target_count && slot < draw.render_targets.size() && slot < p.colors.size(); ++slot) { |
| 1802 | const TargetView& target = draw.render_targets[slot]; | 1802 | const TargetView& target = draw.render_targets[slot]; |
| 1803 | if (!target.resource) continue; | 1803 | if (!target.resource) continue; |
| 1804 | const ResourceBinding* binding = resolve(target.resource); | 1804 | const ResourceBinding* binding = resolve(target.resource); |
diff --git a/runtime/test/d3d12_com_test.cpp b/runtime/test/d3d12_com_test.cpp index 134617ad2..d157b9d36 100644 --- a/runtime/test/d3d12_com_test.cpp +++ b/runtime/test/d3d12_com_test.cpp | |||
| @@ -28,6 +28,11 @@ struct Ref { | |||
| 28 | 28 | ||
| 29 | } // namespace | 29 | } // namespace |
| 30 | 30 | ||
| 31 | static_assert(std::is_move_constructible_v<leonida::Draw>); | ||
| 32 | static_assert(std::is_move_assignable_v<leonida::Draw>); | ||
| 33 | static_assert(sizeof(leonida::Draw::render_targets) == sizeof(leonida::TargetView) * 8); | ||
| 34 | static_assert(sizeof(leonida::Draw::vertex_buffers) == sizeof(leonida::VertexBufferView) * 32); | ||
| 35 | |||
| 31 | int main(int argc, char** argv) { | 36 | int main(int argc, char** argv) { |
| 32 | if (argc == 2 && std::strcmp(argv[1], "--trace-d3d12") == 0) rt::d3d12_trace::set_enabled(true); | 37 | if (argc == 2 && std::strcmp(argv[1], "--trace-d3d12") == 0) rt::d3d12_trace::set_enabled(true); |
| 33 | else if (argc != 1) return 2; | 38 | else if (argc != 1) return 2; |