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:
authorAyinaki <ayinakidev@gmail.com>2026-11-22 21:23:04 +0000
committerAyinaki <ayinakidev@gmail.com>2026-11-22 21:23:04 +0000
commitf506039c95aef2af2f0fb1be809c12fc5eda431c (patch)
tree65fec380beb9a29ef45178fe48163edcfd8aecc7 /runtime/shims/d3d12_com.cpp
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
perf(runtime): move a draw's views out of the command variant (D3D-391)
#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.
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp27
1 files changed, 23 insertions, 4 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 38b21d55f..2b46af12d 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -360,6 +360,7 @@ public:
360 if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); 360 if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false);
361 render_targets_.fill({}); 361 render_targets_.fill({});
362 render_target_count_ = count; 362 render_target_count_ = count;
363 draw_views_stale_ = true;
363 for (UINT i = 0; i < count && targets; ++i) { 364 for (UINT i = 0; i < count && targets; ++i) {
364 const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; 365 const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr;
365 if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); 366 if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle);
@@ -981,6 +982,10 @@ private:
981 leonida::Rect scissor_{}; 982 leonida::Rect scissor_{};
982 std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{}; 983 std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{};
983 UINT render_target_count_ = 0; 984 UINT render_target_count_ = 0;
985 // One block shared by every draw that keeps the same targets and buffers, rebuilt when either changes, so a
986 // run of draws does not allocate per draw.
987 std::shared_ptr<const leonida::DrawViews> draw_views_;
988 bool draw_views_stale_ = true;
984 leonida::TargetView depth_{}; 989 leonida::TargetView depth_{};
985 std::array<float, 4> blend_factor_{1, 1, 1, 1}; 990 std::array<float, 4> blend_factor_{1, 1, 1, 1};
986 991
@@ -1045,12 +1050,14 @@ private:
1045 scissor_ = {}; 1050 scissor_ = {};
1046 render_targets_.fill({}); 1051 render_targets_.fill({});
1047 render_target_count_ = 0; 1052 render_target_count_ = 0;
1053 draw_views_stale_ = true;
1048 depth_ = {}; 1054 depth_ = {};
1049 blend_factor_ = {1, 1, 1, 1}; 1055 blend_factor_ = {1, 1, 1, 1};
1050 set_pipeline(state); 1056 set_pipeline(state);
1051 } 1057 }
1052 1058
1053 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { 1059 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) {
1060 if (count) draw_views_stale_ = true;
1054 for (UINT i = 0; i < count; ++i) 1061 for (UINT i = 0; i < count; ++i)
1055 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, 1062 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes,
1056 views[i].StrideInBytes} 1063 views[i].StrideInBytes}
@@ -1067,16 +1074,28 @@ private:
1067 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); 1074 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z}));
1068 } 1075 }
1069 1076
1077 // The views block a draw records. Rebuilt only when the render targets or the bound vertex buffers change.
1078 const std::shared_ptr<const leonida::DrawViews>& draw_views() {
1079 if (draw_views_stale_ || !draw_views_) {
1080 auto views = std::make_shared<leonida::DrawViews>();
1081 views->render_targets = render_targets_;
1082 views->vertex_buffers = vertex_buffers_;
1083 views->render_target_count = render_target_count_;
1084 draw_views_ = std::move(views);
1085 draw_views_stale_ = false;
1086 }
1087 return draw_views_;
1088 }
1089
1070 // A draw needs a graphics pipeline, a graphics root signature and a topology. 1090 // A draw needs a graphics pipeline, a graphics root signature and a topology.
1071 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { 1091 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
1072 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); 1092 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
1073 // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, 1093 // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing,
1074 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. 1094 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
1075 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; 1095 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return;
1076 leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_, 1096 leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_,
1077 vertex_buffers_, index_, topology_, viewport_, scissor_, 1097 scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
1078 blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, 1098 first, base_vertex, first_instance};
1079 base_vertex, first_instance, render_target_count_};
1080 check(list_.draw(std::move(draw))); 1099 check(list_.draw(std::move(draw)));
1081 } 1100 }
1082}; 1101};