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:
authorVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 19:09:24 +0100
committerVonsek <45567075+Vonsek@users.noreply.github.com>2026-11-22 19:09:24 +0100
commit8d43271bcd727cb9732301bcc8cb9ac2b3d071f8 (patch)
treed329715c2c79a5f19c4c4081911702aad5738e27 /runtime/shims/d3d12_com.cpp
parent538a9fd77d7c1632125da01f59f0cbdd3102c4e4 (diff)
parentec8ec4c9a46c6623f1a9c125afd3566da97f1e0c (diff)
Merge upstream main into codex/d3d12-compute-trace
Trace at the Dispatch entry point while preserving the newly merged bundle replay path.
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp177
1 files changed, 149 insertions, 28 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index af14c6b9b..f9b4d15a3 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>
@@ -206,12 +208,22 @@ public:
206 if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists 208 if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists
207 record_into(command_allocator); 209 record_into(command_allocator);
208 invalid_ = false; 210 invalid_ = false;
211 calls_.clear();
209 release_held(); // what the old recording used: it was replayed at ExecuteCommandLists 212 release_held(); // what the old recording used: it was replayed at ExecuteCommandLists
210 reset_state(state); // Reset returns every list state to its default 213 reset_state(state); // Reset returns every list state to its default
211 stencil_ref_set_ = false; 214 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it
212 return d3d12_trace::result(method, S_OK); 215 return d3d12_trace::result(method, S_OK);
213 } 216 }
214 217
218 // CreateCommandList's pipeline state. As SetPipelineState on a direct list; on a bundle it is the first deferred
219 // call, which the application did not make, so it is not traced.
220 void set_initial_pipeline(ID3D12PipelineState* state) {
221 if (!state) return;
222 if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return SetPipelineState(state);
223 defer([=](GraphicsCommandList& l) { l.set_pipeline(state); });
224 hold(state);
225 }
226
215 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { 227 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override {
216 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); 228 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle");
217 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 229 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
@@ -219,15 +231,17 @@ public:
219 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || 231 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ ||
220 !bundle->list_.closed()) 232 !bundle->list_.closed())
221 return check(false); 233 return check(false);
222 if (!list_.execute_bundle(bundle->list_)) return check(false); 234 // The bundle's state calls and draws play on this list through the members the application would have
223 235 // called, so a draw sees this list's state plus what the bundle set, and what the bundle set stays here
224 // Only the sticky state currently recorded by this COM layer is carried back from the bundle. 236 // afterwards (measured on the PC runtime, docs/d3d12_vulkan.md section 3.2). Commands that need no state
225 if (bundle->blend_factor_set_) blend_factor_ = bundle->blend_factor_; 237 // were recorded in the bundle's own list and are copied between them, in order.
226 if (bundle->stencil_ref_set_) { 238 std::size_t copied = 0;
227 front_stencil_ref_ = bundle->front_stencil_ref_; 239 for (const BundleCall& call : bundle->calls_) {
228 back_stencil_ref_ = bundle->back_stencil_ref_; 240 if (!list_.execute_bundle(bundle->list_, copied, call.commands)) return check(false);
229 stencil_ref_set_ = true; 241 copied = call.commands;
242 call.run(*this);
230 } 243 }
244 check(list_.execute_bundle(bundle->list_, copied));
231 } 245 }
232 246
233 // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords 247 // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords
@@ -243,6 +257,7 @@ public:
243 257
244 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { 258 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override {
245 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); 259 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState");
260 if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state);
246 set_pipeline(state); 261 set_pipeline(state);
247 } 262 }
248 263
@@ -291,8 +306,8 @@ public:
291 306
292 void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { 307 void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override {
293 d3d12_trace::record("ID3D12GraphicsCommandList::Dispatch"); 308 d3d12_trace::record("ID3D12GraphicsCommandList::Dispatch");
294 if (!compute_pipeline_ || !compute_.layout) return check(false); 309 if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return;
295 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); 310 record_dispatch(x, y, z);
296 } 311 }
297 312
298 // ClearState returns every piece of list state to its default, as Reset does (stencil references included). 313 // ClearState returns every piece of list state to its default, as Reset does (stencil references included).
@@ -300,20 +315,27 @@ public:
300 315
301 // Graphics state. Draws record a copy of it (command_list.h, Draw). 316 // Graphics state. Draws record a copy of it (command_list.h, Draw).
302 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { 317 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override {
318 if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return;
303 topology_ = u(topology); 319 topology_ = u(topology);
304 } 320 }
305 321
306 void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { 322 void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override {
307 if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); 323 if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false);
308 for (UINT i = 0; i < count; ++i) 324 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
309 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, 325 std::vector<D3D12_VERTEX_BUFFER_VIEW> copy;
310 views[i].StrideInBytes} 326 if (views) copy.assign(views, views + count);
311 : leonida::VertexBufferView{}; 327 defer([=, copy = std::move(copy), bound = views != nullptr](GraphicsCommandList& l) {
328 l.set_vertex_buffers(start, count, bound ? copy.data() : nullptr);
329 });
330 return;
331 }
332 set_vertex_buffers(start, count, views);
312 } 333 }
313 334
314 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { 335 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override {
315 index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} 336 const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt;
316 : leonida::IndexBufferView{}; 337 if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return;
338 set_index_buffer(view);
317 } 339 }
318 340
319 // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). 341 // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw).
@@ -347,24 +369,32 @@ public:
347 } 369 }
348 370
349 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { 371 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override {
350 blend_factor_set_ = true; 372 const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]}
351 blend_factor_ = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} 373 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 }
352 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } 374 if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return;
375 blend_factor_ = value;
353 } 376 }
354 377
355 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { 378 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override {
379 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return;
356 front_stencil_ref_ = back_stencil_ref_ = reference; 380 front_stencil_ref_ = back_stencil_ref_ = reference;
357 stencil_ref_set_ = true; // the bundle carry-back in ExecuteBundle needs to see this
358 } 381 }
359 382
360 void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { 383 void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override {
361 d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); 384 d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced");
385 if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); },
386 true))
387 return;
362 record_draw(false, vertices, instances, first_vertex, 0, first_instance); 388 record_draw(false, vertices, instances, first_vertex, 0, first_instance);
363 } 389 }
364 390
365 void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, 391 void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex,
366 UINT first_instance) override { 392 UINT first_instance) override {
367 d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); 393 d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced");
394 if (defer([=](GraphicsCommandList& l) {
395 l.record_draw(true, indices, instances, first_index, base_vertex, first_instance);
396 }, true))
397 return;
368 record_draw(true, indices, instances, first_index, base_vertex, first_instance); 398 record_draw(true, indices, instances, first_index, base_vertex, first_instance);
369 } 399 }
370 400
@@ -473,9 +503,19 @@ public:
473 503
474 // The format picks the member of a typeless family to resolve as; the textures' own format is used. 504 // The format picks the member of a typeless family to resolve as; the textures' own format is used.
475 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, 505 void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src,
476 UINT src_subresource, DXGI_FORMAT) override { 506 UINT src_subresource, DXGI_FORMAT format) override {
477 d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); 507 d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource");
478 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 508 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
509 const auto integer_color = [](ID3D12Resource* resource) {
510 if (!resource) return false;
511 const auto desc = resource->GetDesc();
512 return !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) &&
513 color_resolve_format_is_integer(desc.Format);
514 };
515 if (color_resolve_format_is_integer(format) || integer_color(dst) || integer_color(src)) {
516 note_once("ResolveSubresource refused: integer colour formats cannot be resolved");
517 return check(false);
518 }
479 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource})); 519 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource}));
480 } 520 }
481 521
@@ -605,9 +645,13 @@ public:
605 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { 645 void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override {
606 d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); 646 d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef");
607 if (list_.closed()) return check(false); 647 if (list_.closed()) return check(false);
648 if (defer([=](GraphicsCommandList& l) {
649 l.front_stencil_ref_ = front;
650 l.back_stencil_ref_ = back;
651 }))
652 return;
608 front_stencil_ref_ = front; 653 front_stencil_ref_ = front;
609 back_stencil_ref_ = back; 654 back_stencil_ref_ = back;
610 stencil_ref_set_ = true;
611 } 655 }
612 656
613 // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace 657 // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace
@@ -787,6 +831,7 @@ private:
787 831
788 void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, 832 void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float,
789 const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { 833 const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) {
834 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no clear is allowed in a bundle
790 leonida::UavView target{}; 835 leonida::UavView target{};
791 if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); 836 if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false);
792 check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); 837 check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects)));
@@ -855,6 +900,10 @@ private:
855 // signature that is already bound changes nothing: "existing root signature bindings do not become stale" 900 // signature that is already bound changes nothing: "existing root signature bindings do not become stale"
856 // (D3D12, "Using a Root Signature"), and engines rebind routinely. 901 // (D3D12, "Using a Root Signature"), and engines rebind routinely.
857 void bind_root(RootState& state, ID3D12RootSignature* signature) { 902 void bind_root(RootState& state, ID3D12RootSignature* signature) {
903 if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) {
904 l.bind_root(compute ? l.compute_ : l.graphics_, signature);
905 }))
906 return hold(signature); // the bundle keeps it for as long as it can be executed
858 const RootArgumentLayout* layout = root_signature_layout(signature); 907 const RootArgumentLayout* layout = root_signature_layout(signature);
859 if (layout && layout == state.layout) return; 908 if (layout && layout == state.layout) return;
860 hold(signature); // state.layout points into it 909 hold(signature); // state.layout points into it
@@ -882,6 +931,22 @@ private:
882 } 931 }
883 932
884 void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { 933 void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) {
934 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) {
935 // The layout is the executing list's, so the range is checked when it runs; no signature takes more
936 // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory.
937 if (count > D3D12_MAX_ROOT_COST) return check(false);
938 const auto* words = static_cast<const std::uint32_t*>(data);
939 std::vector<std::uint32_t> copy;
940 if (data) copy.assign(words, words + count);
941 // A count of 0 with a non-null pointer is a valid call; an empty vector's data() may be null.
942 static constexpr std::uint32_t none = 0;
943 defer([=, compute = &state == &compute_, copy = std::move(copy),
944 given = data != nullptr](GraphicsCommandList& l) {
945 l.constants(compute ? l.compute_ : l.graphics_, index, count,
946 !given ? nullptr : copy.empty() ? &none : copy.data(), offset);
947 });
948 return;
949 }
885 const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); 950 const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants);
886 if (!p) return; 951 if (!p) return;
887 if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); 952 if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false);
@@ -890,6 +955,10 @@ private:
890 } 955 }
891 956
892 void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { 957 void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) {
958 if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) {
959 l.address(compute ? l.compute_ : l.graphics_, index, va, type);
960 }))
961 return;
893 const RootArgumentLayout::Parameter* p = parameter(state, index, type); 962 const RootArgumentLayout::Parameter* p = parameter(state, index, type);
894 if (!p) return; 963 if (!p) return;
895 write(state, p->user_offset, std::uint32_t(va)); 964 write(state, p->user_offset, std::uint32_t(va));
@@ -902,7 +971,6 @@ private:
902 bool invalid_ = false; 971 bool invalid_ = false;
903 UINT front_stencil_ref_ = 0; 972 UINT front_stencil_ref_ = 0;
904 UINT back_stencil_ref_ = 0; 973 UINT back_stencil_ref_ = 0;
905 bool stencil_ref_set_ = false;
906 RootState compute_; 974 RootState compute_;
907 RootState graphics_; 975 RootState graphics_;
908 std::uintptr_t compute_pipeline_ = 0; 976 std::uintptr_t compute_pipeline_ = 0;
@@ -915,7 +983,6 @@ private:
915 std::vector<leonida::TargetView> render_targets_; 983 std::vector<leonida::TargetView> render_targets_;
916 leonida::TargetView depth_{}; 984 leonida::TargetView depth_{};
917 std::array<float, 4> blend_factor_{1, 1, 1, 1}; 985 std::array<float, 4> blend_factor_{1, 1, 1, 1};
918 bool blend_factor_set_ = false;
919 986
920 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right 987 // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right
921 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root 988 // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root
@@ -923,6 +990,31 @@ private:
923 // runs the submission stays the application's job, as in D3D12.) 990 // runs the submission stays the application's job, as in D3D12.)
924 std::vector<IUnknown*> held_; 991 std::vector<IUnknown*> held_;
925 992
993 // A bundle keeps its state calls and draws instead of applying them: it has no render targets, viewport or root
994 // signature of its own to resolve them against, and what it sets belongs to the list that executes it.
995 // `commands` is how much of the bundle's own list was recorded before the call, so ExecuteBundle can replay the
996 // calls and copy the state-free commands (markers, immediate writes) in their original order. Returns whether the
997 // call was kept; on any other list it is false and the caller does the work.
998 struct BundleCall {
999 std::size_t commands;
1000 std::function<void(GraphicsCommandList&)> run;
1001 };
1002 std::vector<BundleCall> calls_;
1003
1004 // Every deferred call replays through a private member that does not trace: the trace line is written once, when
1005 // the application makes the call, and not again at each ExecuteBundle. After Close nothing is appended, since
1006 // another list may be executing the bundle: a state call is ignored, and a draw or dispatch (`command`) makes
1007 // the list invalid.
1008 bool defer(std::function<void(GraphicsCommandList&)> run, bool command = false) {
1009 if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return false;
1010 if (list_.closed()) {
1011 if (command) check(false);
1012 return true;
1013 }
1014 calls_.push_back({list_.commands().size(), std::move(run)});
1015 return true;
1016 }
1017
926 void hold(IUnknown* object) { 1018 void hold(IUnknown* object) {
927 if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; 1019 if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return;
928 object->AddRef(); 1020 object->AddRef();
@@ -954,10 +1046,26 @@ private:
954 render_targets_.clear(); 1046 render_targets_.clear();
955 depth_ = {}; 1047 depth_ = {};
956 blend_factor_ = {1, 1, 1, 1}; 1048 blend_factor_ = {1, 1, 1, 1};
957 blend_factor_set_ = false;
958 set_pipeline(state); 1049 set_pipeline(state);
959 } 1050 }
960 1051
1052 void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) {
1053 for (UINT i = 0; i < count; ++i)
1054 vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes,
1055 views[i].StrideInBytes}
1056 : leonida::VertexBufferView{};
1057 }
1058
1059 void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) {
1060 index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)}
1061 : leonida::IndexBufferView{};
1062 }
1063
1064 void record_dispatch(UINT x, UINT y, UINT z) {
1065 if (!compute_pipeline_ || !compute_.layout) return check(false);
1066 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z}));
1067 }
1068
961 // A draw needs a graphics pipeline, a graphics root signature and a topology. 1069 // A draw needs a graphics pipeline, a graphics root signature and a topology.
962 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { 1070 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
963 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); 1071 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
@@ -1398,7 +1506,7 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void
1398} 1506}
1399 1507
1400HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, 1508HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid,
1401 void** out, IUnknown* device) { 1509 void** out, IUnknown* device, ID3D12PipelineState* initial_state) {
1402 if (!out) return E_POINTER; 1510 if (!out) return E_POINTER;
1403 *out = nullptr; 1511 *out = nullptr;
1404 CommandAllocator* command_allocator = CommandAllocator::from(allocator); 1512 CommandAllocator* command_allocator = CommandAllocator::from(allocator);
@@ -1411,7 +1519,12 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator
1411 auto* object = new GraphicsCommandList(type); // created open, as D3D12 does 1519 auto* object = new GraphicsCommandList(type); // created open, as D3D12 does
1412 object->record_into(command_allocator); 1520 object->record_into(command_allocator);
1413 object->attach_device(device); 1521 object->attach_device(device);
1414 return hand_out(object, riid, out); 1522 const HRESULT hr = hand_out(object, riid, out); // releases the creation reference, so a failure frees the list
1523 // The initial pipeline state only once the list exists for the caller, as SetPipelineState on it would be.
1524 if (SUCCEEDED(hr) && initial_state)
1525 if (GraphicsCommandList* list = GraphicsCommandList::from(static_cast<IUnknown*>(*out)))
1526 list->set_initial_pipeline(initial_state);
1527 return hr;
1415} 1528}
1416 1529
1417HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, 1530HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid,
@@ -1492,4 +1605,12 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT&
1492 return true; 1605 return true;
1493} 1606}
1494 1607
1608bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) {
1609 const GraphicsCommandList* l = GraphicsCommandList::from(list);
1610 if (!l) return false;
1611 const auto recorded = l->commands().commands();
1612 commands.assign(recorded.begin(), recorded.end());
1613 return true;
1614}
1615
1495} // namespace rt::d3d12 1616} // namespace rt::d3d12