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:
authorgabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com>2026-11-22 17:03:14 -0300
committergabrielsteglich <63128571+gabrielsteglich@users.noreply.github.com>2026-11-22 17:03:14 -0300
commit90f10662acba93bdf1aa9a84b11b733209495c31 (patch)
tree9298428b3dfbfc6bda26ed2b363e8ef9813abcda /runtime/shims/d3d12_com.cpp
parentf8e89767f66de8f2a375615ca96a10459a87fef7 (diff)
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
Merge upstream main into reserved texture tile mappings
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp192
1 files changed, 158 insertions, 34 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 502fb1143..49d47d623 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -12,7 +12,9 @@
12#include <atomic> 12#include <atomic>
13#include <bit> 13#include <bit>
14#include <condition_variable> 14#include <condition_variable>
15#include <functional>
15#include <memory> 16#include <memory>
17#include <optional>
16#include <cstdio> 18#include <cstdio>
17#include <cstring> 19#include <cstring>
18#include <mutex> 20#include <mutex>
@@ -225,12 +227,22 @@ public:
225 if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists 227 if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists
226 record_into(command_allocator); 228 record_into(command_allocator);
227 invalid_ = false; 229 invalid_ = false;
230 calls_.clear();
228 release_held(); // what the old recording used: it was replayed at ExecuteCommandLists 231 release_held(); // what the old recording used: it was replayed at ExecuteCommandLists
229 reset_state(state); // Reset returns every list state to its default 232 reset_state(state); // Reset returns every list state to its default
230 stencil_ref_set_ = false; 233 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it
231 return d3d12_trace::result(method, S_OK); 234 return d3d12_trace::result(method, S_OK);
232 } 235 }
233 236
237 // CreateCommandList's pipeline state. As SetPipelineState on a direct list; on a bundle it is the first deferred
238 // call, which the application did not make, so it is not traced.
239 void set_initial_pipeline(ID3D12PipelineState* state) {
240 if (!state) return;
241 if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return SetPipelineState(state);
242 defer([=](GraphicsCommandList& l) { l.set_pipeline(state); });
243 hold(state);
244 }
245
234 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { 246 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override {
235 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); 247 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle");
236 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 248 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
@@ -238,15 +250,17 @@ public:
238 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || 250 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ ||
239 !bundle->list_.closed()) 251 !bundle->list_.closed())
240 return check(false); 252 return check(false);
241 if (!list_.execute_bundle(bundle->list_)) return check(false); 253 // The bundle's state calls and draws play on this list through the members the application would have
242 254 // called, so a draw sees this list's state plus what the bundle set, and what the bundle set stays here
243 // Only the sticky state currently recorded by this COM layer is carried back from the bundle. 255 // afterwards (measured on the PC runtime, docs/d3d12_vulkan.md section 3.2). Commands that need no state
244 if (bundle->blend_factor_set_) blend_factor_ = bundle->blend_factor_; 256 // were recorded in the bundle's own list and are copied between them, in order.
245 if (bundle->stencil_ref_set_) { 257 std::size_t copied = 0;
246 front_stencil_ref_ = bundle->front_stencil_ref_; 258 for (const BundleCall& call : bundle->calls_) {
247 back_stencil_ref_ = bundle->back_stencil_ref_; 259 if (!list_.execute_bundle(bundle->list_, copied, call.commands)) return check(false);
248 stencil_ref_set_ = true; 260 copied = call.commands;
261 call.run(*this);
249 } 262 }
263 check(list_.execute_bundle(bundle->list_, copied));
250 } 264 }
251 265
252 // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords 266 // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords
@@ -261,6 +275,7 @@ public:
261 275
262 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { 276 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override {
263 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); 277 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState");
278 if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state);
264 set_pipeline(state); 279 set_pipeline(state);
265 } 280 }
266 281
@@ -308,8 +323,8 @@ public:
308 } 323 }
309 324
310 void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { 325 void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override {
311 if (!compute_pipeline_ || !compute_.layout) return check(false); 326 if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return;
312 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); 327 record_dispatch(x, y, z);
313 } 328 }
314 329
315 // ClearState returns every piece of list state to its default, as Reset does (stencil references included). 330 // ClearState returns every piece of list state to its default, as Reset does (stencil references included).
@@ -317,20 +332,27 @@ public:
317 332
318 // Graphics state. Draws record a copy of it (command_list.h, Draw). 333 // Graphics state. Draws record a copy of it (command_list.h, Draw).
319 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { 334 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override {
335 if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return;
320 topology_ = u(topology); 336 topology_ = u(topology);
321 } 337 }
322 338
323 void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { 339 void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override {
324 if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); 340 if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false);
325 for (UINT i = 0; i < count; ++i) 341 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
326 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, 342 std::vector<D3D12_VERTEX_BUFFER_VIEW> copy;
327 views[i].StrideInBytes} 343 if (views) copy.assign(views, views + count);
328 : leonida::VertexBufferView{}; 344 defer([=, copy = std::move(copy), bound = views != nullptr](GraphicsCommandList& l) {
345 l.set_vertex_buffers(start, count, bound ? copy.data() : nullptr);
346 });
347 return;
348 }
349 set_vertex_buffers(start, count, views);
329 } 350 }
330 351
331 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { 352 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override {
332 index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} 353 const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt;
333 : leonida::IndexBufferView{}; 354 if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return;
355 set_index_buffer(view);
334 } 356 }
335 357
336 // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). 358 // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw).
@@ -355,7 +377,8 @@ public:
355 void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range, 377 void STDMETHODCALLTYPE OMSetRenderTargets(UINT count, const D3D12_CPU_DESCRIPTOR_HANDLE* targets, BOOL single_range,
356 const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override { 378 const D3D12_CPU_DESCRIPTOR_HANDLE* depth) override {
357 if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false); 379 if (count > D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT) return check(false);
358 render_targets_.assign(count, {}); 380 render_targets_.fill({});
381 render_target_count_ = count;
359 for (UINT i = 0; i < count && targets; ++i) { 382 for (UINT i = 0; i < count && targets; ++i) {
360 const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr; 383 const SIZE_T handle = single_range ? targets[0].ptr + SIZE_T(i) * descriptor_size : targets[i].ptr;
361 if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle); 384 if (handle) render_targets_[i] = *reinterpret_cast<const leonida::TargetView*>(handle);
@@ -364,24 +387,32 @@ public:
364 } 387 }
365 388
366 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { 389 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override {
367 blend_factor_set_ = true; 390 const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]}
368 blend_factor_ = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} 391 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 }
369 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } 392 if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return;
393 blend_factor_ = value;
370 } 394 }
371 395
372 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { 396 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override {
397 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return;
373 front_stencil_ref_ = back_stencil_ref_ = reference; 398 front_stencil_ref_ = back_stencil_ref_ = reference;
374 stencil_ref_set_ = true; // the bundle carry-back in ExecuteBundle needs to see this
375 } 399 }
376 400
377 void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { 401 void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override {
378 d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); 402 d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced");
403 if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); },
404 true))
405 return;
379 record_draw(false, vertices, instances, first_vertex, 0, first_instance); 406 record_draw(false, vertices, instances, first_vertex, 0, first_instance);
380 } 407 }
381 408
382 void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, 409 void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex,
383 UINT first_instance) override { 410 UINT first_instance) override {
384 d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); 411 d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced");
412 if (defer([=](GraphicsCommandList& l) {
413 l.record_draw(true, indices, instances, first_index, base_vertex, first_instance);
414 }, true))
415 return;
385 record_draw(true, indices, instances, first_index, base_vertex, first_instance); 416 record_draw(true, indices, instances, first_index, base_vertex, first_instance);
386 } 417 }
387 418
@@ -519,9 +550,19 @@ public:
519 550
520 // The format picks the member of a typeless family to resolve as; the textures' own format is used. 551 // The format picks the member of a typeless family to resolve as; the textures' own format is used.
521 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, 552 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src,
522 UINT src_subresource, DXGI_FORMAT) override { 553 UINT src_subresource, DXGI_FORMAT format) override {
523 d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); 554 d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource");
524 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 555 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
556 const auto integer_color = [](ID3D12Resource* resource) {
557 if (!resource) return false;
558 const auto desc = resource->GetDesc();
559 return !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) &&
560 color_resolve_format_is_integer(desc.Format);
561 };
562 if (color_resolve_format_is_integer(format) || integer_color(dst) || integer_color(src)) {
563 note_once("ResolveSubresource refused: integer colour formats cannot be resolved");
564 return check(false);
565 }
525 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource})); 566 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource}));
526 } 567 }
527 568
@@ -651,9 +692,13 @@ public:
651 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { 692 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override {
652 d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); 693 d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef");
653 if (list_.closed()) return check(false); 694 if (list_.closed()) return check(false);
695 if (defer([=](GraphicsCommandList& l) {
696 l.front_stencil_ref_ = front;
697 l.back_stencil_ref_ = back;
698 }))
699 return;
654 front_stencil_ref_ = front; 700 front_stencil_ref_ = front;
655 back_stencil_ref_ = back; 701 back_stencil_ref_ = back;
656 stencil_ref_set_ = true;
657 } 702 }
658 703
659 // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace 704 // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace
@@ -833,6 +878,7 @@ private:
833 878
834 void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, 879 void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float,
835 const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { 880 const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) {
881 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no clear is allowed in a bundle
836 leonida::UavView target{}; 882 leonida::UavView target{};
837 if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); 883 if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false);
838 check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); 884 check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects)));
@@ -901,6 +947,10 @@ private:
901 // signature that is already bound changes nothing: "existing root signature bindings do not become stale" 947 // signature that is already bound changes nothing: "existing root signature bindings do not become stale"
902 // (D3D12, "Using a Root Signature"), and engines rebind routinely. 948 // (D3D12, "Using a Root Signature"), and engines rebind routinely.
903 void bind_root(RootState& state, ID3D12RootSignature* signature) { 949 void bind_root(RootState& state, ID3D12RootSignature* signature) {
950 if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) {
951 l.bind_root(compute ? l.compute_ : l.graphics_, signature);
952 }))
953 return hold(signature); // the bundle keeps it for as long as it can be executed
904 const RootArgumentLayout* layout = root_signature_layout(signature); 954 const RootArgumentLayout* layout = root_signature_layout(signature);
905 if (layout && layout == state.layout) return; 955 if (layout && layout == state.layout) return;
906 hold(signature); // state.layout points into it 956 hold(signature); // state.layout points into it
@@ -928,6 +978,22 @@ private:
928 } 978 }
929 979
930 void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { 980 void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) {
981 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
982 // The layout is the executing list's, so the range is checked when it runs; no signature takes more
983 // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory.
984 if (count > D3D12_MAX_ROOT_COST) return check(false);
985 const auto* words = static_cast<const std::uint32_t*>(data);
986 std::vector<std::uint32_t> copy;
987 if (data) copy.assign(words, words + count);
988 // A count of 0 with a non-null pointer is a valid call; an empty vector's data() may be null.
989 static constexpr std::uint32_t none = 0;
990 defer([=, compute = &state == &compute_, copy = std::move(copy),
991 given = data != nullptr](GraphicsCommandList& l) {
992 l.constants(compute ? l.compute_ : l.graphics_, index, count,
993 !given ? nullptr : copy.empty() ? &none : copy.data(), offset);
994 });
995 return;
996 }
931 const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); 997 const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants);
932 if (!p) return; 998 if (!p) return;
933 if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); 999 if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false);
@@ -936,6 +1002,10 @@ private:
936 } 1002 }
937 1003
938 void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { 1004 void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) {
1005 if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) {
1006 l.address(compute ? l.compute_ : l.graphics_, index, va, type);
1007 }))
1008 return;
939 const RootArgumentLayout::Parameter* p = parameter(state, index, type); 1009 const RootArgumentLayout::Parameter* p = parameter(state, index, type);
940 if (!p) return; 1010 if (!p) return;
941 write(state, p->user_offset, std::uint32_t(va)); 1011 write(state, p->user_offset, std::uint32_t(va));
@@ -948,7 +1018,6 @@ private:
948 bool invalid_ = false; 1018 bool invalid_ = false;
949 UINT front_stencil_ref_ = 0; 1019 UINT front_stencil_ref_ = 0;
950 UINT back_stencil_ref_ = 0; 1020 UINT back_stencil_ref_ = 0;
951 bool stencil_ref_set_ = false;
952 RootState compute_; 1021 RootState compute_;
953 RootState graphics_; 1022 RootState graphics_;
954 std::uintptr_t compute_pipeline_ = 0; 1023 std::uintptr_t compute_pipeline_ = 0;
@@ -958,10 +1027,10 @@ private:
958 leonida::IndexBufferView index_{}; 1027 leonida::IndexBufferView index_{};
959 leonida::Viewport viewport_{}; 1028 leonida::Viewport viewport_{};
960 leonida::Rect scissor_{}; 1029 leonida::Rect scissor_{};
961 std::vector<leonida::TargetView> render_targets_; 1030 std::array<leonida::TargetView, D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT> render_targets_{};
1031 UINT render_target_count_ = 0;
962 leonida::TargetView depth_{}; 1032 leonida::TargetView depth_{};
963 std::array<float, 4> blend_factor_{1, 1, 1, 1}; 1033 std::array<float, 4> blend_factor_{1, 1, 1, 1};
964 bool blend_factor_set_ = false;
965 1034
966 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right 1035 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right
967 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root 1036 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root
@@ -969,6 +1038,31 @@ private:
969 // runs the submission stays the application's job, as in D3D12.) 1038 // runs the submission stays the application's job, as in D3D12.)
970 std::vector<IUnknown*> held_; 1039 std::vector<IUnknown*> held_;
971 1040
1041 // A bundle keeps its state calls and draws instead of applying them: it has no render targets, viewport or root
1042 // signature of its own to resolve them against, and what it sets belongs to the list that executes it.
1043 // `commands` is how much of the bundle's own list was recorded before the call, so ExecuteBundle can replay the
1044 // calls and copy the state-free commands (markers, immediate writes) in their original order. Returns whether the
1045 // call was kept; on any other list it is false and the caller does the work.
1046 struct BundleCall {
1047 std::size_t commands;
1048 std::function<void(GraphicsCommandList&)> run;
1049 };
1050 std::vector<BundleCall> calls_;
1051
1052 // Every deferred call replays through a private member that does not trace: the trace line is written once, when
1053 // the application makes the call, and not again at each ExecuteBundle. After Close nothing is appended, since
1054 // another list may be executing the bundle: a state call is ignored, and a draw or dispatch (`command`) makes
1055 // the list invalid.
1056 bool defer(std::function<void(GraphicsCommandList&)> run, bool command = false) {
1057 if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return false;
1058 if (list_.closed()) {
1059 if (command) check(false);
1060 return true;
1061 }
1062 calls_.push_back({list_.commands().size(), std::move(run)});
1063 return true;
1064 }
1065
972 void hold(IUnknown* object) { 1066 void hold(IUnknown* object) {
973 if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; 1067 if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return;
974 object->AddRef(); 1068 object->AddRef();
@@ -997,13 +1091,30 @@ private:
997 index_ = {}; 1091 index_ = {};
998 viewport_ = {}; 1092 viewport_ = {};
999 scissor_ = {}; 1093 scissor_ = {};
1000 render_targets_.clear(); 1094 render_targets_.fill({});
1095 render_target_count_ = 0;
1001 depth_ = {}; 1096 depth_ = {};
1002 blend_factor_ = {1, 1, 1, 1}; 1097 blend_factor_ = {1, 1, 1, 1};
1003 blend_factor_set_ = false;
1004 set_pipeline(state); 1098 set_pipeline(state);
1005 } 1099 }
1006 1100
1101 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) {
1102 for (UINT i = 0; i < count; ++i)
1103 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes,
1104 views[i].StrideInBytes}
1105 : leonida::VertexBufferView{};
1106 }
1107
1108 void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) {
1109 index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)}
1110 : leonida::IndexBufferView{};
1111 }
1112
1113 void record_dispatch(UINT x, UINT y, UINT z) {
1114 if (!compute_pipeline_ || !compute_.layout) return check(false);
1115 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z}));
1116 }
1117
1007 // A draw needs a graphics pipeline, a graphics root signature and a topology. 1118 // A draw needs a graphics pipeline, a graphics root signature and a topology.
1008 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { 1119 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
1009 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); 1120 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
@@ -1011,10 +1122,10 @@ private:
1011 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. 1122 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
1012 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; 1123 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return;
1013 leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_, 1124 leonida::Draw draw{graphics_pipeline_, graphics_.data, render_targets_, depth_,
1014 {vertex_buffers_.begin(), vertex_buffers_.end()}, index_, topology_, viewport_, scissor_, 1125 vertex_buffers_, index_, topology_, viewport_, scissor_,
1015 blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, 1126 blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first,
1016 base_vertex, first_instance}; 1127 base_vertex, first_instance, render_target_count_};
1017 check(list_.draw(draw)); 1128 check(list_.draw(std::move(draw)));
1018 } 1129 }
1019}; 1130};
1020 1131
@@ -1449,7 +1560,7 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void
1449} 1560}
1450 1561
1451HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, 1562HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid,
1452 void** out, IUnknown* device) { 1563 void** out, IUnknown* device, ID3D12PipelineState* initial_state) {
1453 if (!out) return E_POINTER; 1564 if (!out) return E_POINTER;
1454 *out = nullptr; 1565 *out = nullptr;
1455 CommandAllocator* command_allocator = CommandAllocator::from(allocator); 1566 CommandAllocator* command_allocator = CommandAllocator::from(allocator);
@@ -1462,7 +1573,12 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator
1462 auto* object = new GraphicsCommandList(type); // created open, as D3D12 does 1573 auto* object = new GraphicsCommandList(type); // created open, as D3D12 does
1463 object->record_into(command_allocator); 1574 object->record_into(command_allocator);
1464 object->attach_device(device); 1575 object->attach_device(device);
1465 return hand_out(object, riid, out); 1576 const HRESULT hr = hand_out(object, riid, out); // releases the creation reference, so a failure frees the list
1577 // The initial pipeline state only once the list exists for the caller, as SetPipelineState on it would be.
1578 if (SUCCEEDED(hr) && initial_state)
1579 if (GraphicsCommandList* list = GraphicsCommandList::from(static_cast<IUnknown*>(*out)))
1580 list->set_initial_pipeline(initial_state);
1581 return hr;
1466} 1582}
1467 1583
1468HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, 1584HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid,
@@ -1543,4 +1659,12 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT&
1543 return true; 1659 return true;
1544} 1660}
1545 1661
1662bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) {
1663 const GraphicsCommandList* l = GraphicsCommandList::from(list);
1664 if (!l) return false;
1665 const auto recorded = l->commands().commands();
1666 commands.assign(recorded.begin(), recorded.end());
1667 return true;
1668}
1669
1546} // namespace rt::d3d12 1670} // namespace rt::d3d12