upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/d3d12_com.cpp
diff options
context:
space:
mode:
authorShadman PC <iamanthonytony@gmail.com>2026-11-22 23:11:32 +0600
committerShadman PC <iamanthonytony@gmail.com>2026-11-22 23:11:32 +0600
commit90a046eaa49062e8c6c8e9b431b7ef5fa302ae45 (patch)
treeeae817137740042956bcb828a0d247c138772b99 /runtime/shims/d3d12_com.cpp
parent49475796c226c8511b9470bf97a4daf57b8af323 (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/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp15
1 files changed, 9 insertions, 6 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