diff options
| author | KanameS <nk23a4@gmail.com> | 2026-11-22 23:06:44 +0700 |
|---|---|---|
| committer | KanameS <nk23a4@gmail.com> | 2026-11-22 23:06:44 +0700 |
| commit | b76c78acdb825ec65be5164a5ce89210e806ba60 (patch) | |
| tree | 5ed18443908cc87c7b3009d4781753defd31c7c2 /runtime/shims/d3d12_com.cpp | |
| parent | 4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff) | |
Runtime: a bundle's draws run with the parent's state, and what the bundle set stays on the parent
A draw recorded in a bundle took the bundle's own viewport, targets, root
arguments and buffers. A bundle cannot set a viewport, so every bundle draw
was dropped without an error, and nothing of the parent's state reached it.
Only the stencil reference and the blend factor were handed back.
Inside a bundle, state calls, draws and Dispatch are now kept as deferred
calls, and ExecuteBundle plays them on the parent through the members a
direct call uses, between the bundle's state-free commands, in order. A
draw sees the parent's state plus what the bundle set, and what the bundle
set stays on the parent, as measured on the PC runtime. This replaces the
stencil-reference and blend-factor carry-back fields. Trace lines are
written once, when the bundle records the call.
ClearUnorderedAccessView is refused in a bundle, like the other clears.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 165 |
1 files changed, 138 insertions, 27 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 55c6f86a8..c61abf24d 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> |
| @@ -190,12 +192,22 @@ public: | |||
| 190 | if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists | 192 | if (!list_.reset()) return d3d12_trace::result(method, E_FAIL); // D3D12 resets only closed lists |
| 191 | record_into(command_allocator); | 193 | record_into(command_allocator); |
| 192 | invalid_ = false; | 194 | invalid_ = false; |
| 195 | calls_.clear(); | ||
| 193 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists | 196 | release_held(); // what the old recording used: it was replayed at ExecuteCommandLists |
| 194 | reset_state(state); // Reset returns every list state to its default | 197 | reset_state(state); // Reset returns every list state to its default |
| 195 | stencil_ref_set_ = false; | 198 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it |
| 196 | return d3d12_trace::result(method, S_OK); | 199 | return d3d12_trace::result(method, S_OK); |
| 197 | } | 200 | } |
| 198 | 201 | ||
| 202 | // CreateCommandList's pipeline state. As SetPipelineState on a direct list; on a bundle it is the first deferred | ||
| 203 | // call, which the application did not make, so it is not traced. | ||
| 204 | void set_initial_pipeline(ID3D12PipelineState* state) { | ||
| 205 | if (!state) return; | ||
| 206 | if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return SetPipelineState(state); | ||
| 207 | defer([=](GraphicsCommandList& l) { l.set_pipeline(state); }); | ||
| 208 | hold(state); | ||
| 209 | } | ||
| 210 | |||
| 199 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { | 211 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { |
| 200 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); | 212 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); |
| 201 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); | 213 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); |
| @@ -203,15 +215,17 @@ public: | |||
| 203 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || | 215 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || |
| 204 | !bundle->list_.closed()) | 216 | !bundle->list_.closed()) |
| 205 | return check(false); | 217 | return check(false); |
| 206 | if (!list_.execute_bundle(bundle->list_)) return check(false); | 218 | // The bundle's state calls and draws play on this list through the members the application would have |
| 207 | 219 | // called, so a draw sees this list's state plus what the bundle set, and what the bundle set stays here | |
| 208 | // Only the sticky state currently recorded by this COM layer is carried back from the bundle. | 220 | // afterwards (measured on the PC runtime, docs/d3d12_vulkan.md section 3.2). Commands that need no state |
| 209 | if (bundle->blend_factor_set_) blend_factor_ = bundle->blend_factor_; | 221 | // were recorded in the bundle's own list and are copied between them, in order. |
| 210 | if (bundle->stencil_ref_set_) { | 222 | std::size_t copied = 0; |
| 211 | front_stencil_ref_ = bundle->front_stencil_ref_; | 223 | for (const BundleCall& call : bundle->calls_) { |
| 212 | back_stencil_ref_ = bundle->back_stencil_ref_; | 224 | if (!list_.execute_bundle(bundle->list_, copied, call.commands)) return check(false); |
| 213 | stencil_ref_set_ = true; | 225 | copied = call.commands; |
| 226 | call.run(*this); | ||
| 214 | } | 227 | } |
| 228 | check(list_.execute_bundle(bundle->list_, copied)); | ||
| 215 | } | 229 | } |
| 216 | 230 | ||
| 217 | // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords | 231 | // Root signatures, pipeline state and root arguments. The arguments are packed into the 32 user-data dwords |
| @@ -226,6 +240,7 @@ public: | |||
| 226 | 240 | ||
| 227 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { | 241 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { |
| 228 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); | 242 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); |
| 243 | if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); | ||
| 229 | set_pipeline(state); | 244 | set_pipeline(state); |
| 230 | } | 245 | } |
| 231 | 246 | ||
| @@ -270,8 +285,8 @@ public: | |||
| 270 | } | 285 | } |
| 271 | 286 | ||
| 272 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { | 287 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { |
| 273 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 288 | if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return; |
| 274 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 289 | record_dispatch(x, y, z); |
| 275 | } | 290 | } |
| 276 | 291 | ||
| 277 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). | 292 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). |
| @@ -279,20 +294,27 @@ public: | |||
| 279 | 294 | ||
| 280 | // Graphics state. Draws record a copy of it (command_list.h, Draw). | 295 | // Graphics state. Draws record a copy of it (command_list.h, Draw). |
| 281 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { | 296 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { |
| 297 | if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return; | ||
| 282 | topology_ = u(topology); | 298 | topology_ = u(topology); |
| 283 | } | 299 | } |
| 284 | 300 | ||
| 285 | void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { | 301 | void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { |
| 286 | if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); | 302 | if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); |
| 287 | for (UINT i = 0; i < count; ++i) | 303 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { |
| 288 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 304 | std::vector<D3D12_VERTEX_BUFFER_VIEW> copy; |
| 289 | views[i].StrideInBytes} | 305 | if (views) copy.assign(views, views + count); |
| 290 | : leonida::VertexBufferView{}; | 306 | defer([=, copy = std::move(copy), bound = views != nullptr](GraphicsCommandList& l) { |
| 307 | l.set_vertex_buffers(start, count, bound ? copy.data() : nullptr); | ||
| 308 | }); | ||
| 309 | return; | ||
| 310 | } | ||
| 311 | set_vertex_buffers(start, count, views); | ||
| 291 | } | 312 | } |
| 292 | 313 | ||
| 293 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { | 314 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { |
| 294 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | 315 | const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt; |
| 295 | : leonida::IndexBufferView{}; | 316 | if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; |
| 317 | set_index_buffer(view); | ||
| 296 | } | 318 | } |
| 297 | 319 | ||
| 298 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). | 320 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). |
| @@ -326,24 +348,32 @@ public: | |||
| 326 | } | 348 | } |
| 327 | 349 | ||
| 328 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { | 350 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { |
| 329 | blend_factor_set_ = true; | 351 | const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} |
| 330 | blend_factor_ = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} | 352 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } |
| 331 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } | 353 | if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; |
| 354 | blend_factor_ = value; | ||
| 332 | } | 355 | } |
| 333 | 356 | ||
| 334 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { | 357 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { |
| 358 | if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; | ||
| 335 | front_stencil_ref_ = back_stencil_ref_ = reference; | 359 | front_stencil_ref_ = back_stencil_ref_ = reference; |
| 336 | stencil_ref_set_ = true; // the bundle carry-back in ExecuteBundle needs to see this | ||
| 337 | } | 360 | } |
| 338 | 361 | ||
| 339 | void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { | 362 | void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { |
| 340 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); | 363 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); |
| 364 | if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); }, | ||
| 365 | true)) | ||
| 366 | return; | ||
| 341 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); | 367 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); |
| 342 | } | 368 | } |
| 343 | 369 | ||
| 344 | void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, | 370 | void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, |
| 345 | UINT first_instance) override { | 371 | UINT first_instance) override { |
| 346 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); | 372 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); |
| 373 | if (defer([=](GraphicsCommandList& l) { | ||
| 374 | l.record_draw(true, indices, instances, first_index, base_vertex, first_instance); | ||
| 375 | }, true)) | ||
| 376 | return; | ||
| 347 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); | 377 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); |
| 348 | } | 378 | } |
| 349 | 379 | ||
| @@ -565,9 +595,13 @@ public: | |||
| 565 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { | 595 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { |
| 566 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); | 596 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); |
| 567 | if (list_.closed()) return check(false); | 597 | if (list_.closed()) return check(false); |
| 598 | if (defer([=](GraphicsCommandList& l) { | ||
| 599 | l.front_stencil_ref_ = front; | ||
| 600 | l.back_stencil_ref_ = back; | ||
| 601 | })) | ||
| 602 | return; | ||
| 568 | front_stencil_ref_ = front; | 603 | front_stencil_ref_ = front; |
| 569 | back_stencil_ref_ = back; | 604 | back_stencil_ref_ = back; |
| 570 | stencil_ref_set_ = true; | ||
| 571 | } | 605 | } |
| 572 | 606 | ||
| 573 | // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace | 607 | // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace |
| @@ -747,6 +781,7 @@ private: | |||
| 747 | 781 | ||
| 748 | void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, | 782 | void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, |
| 749 | const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { | 783 | const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { |
| 784 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no clear is allowed in a bundle | ||
| 750 | leonida::UavView target{}; | 785 | leonida::UavView target{}; |
| 751 | if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); | 786 | if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); |
| 752 | check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); | 787 | check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); |
| @@ -815,6 +850,10 @@ private: | |||
| 815 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" | 850 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" |
| 816 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. | 851 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. |
| 817 | void bind_root(RootState& state, ID3D12RootSignature* signature) { | 852 | void bind_root(RootState& state, ID3D12RootSignature* signature) { |
| 853 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | ||
| 854 | l.bind_root(compute ? l.compute_ : l.graphics_, signature); | ||
| 855 | })) | ||
| 856 | return hold(signature); // the bundle keeps it for as long as it can be executed | ||
| 818 | const RootArgumentLayout* layout = root_signature_layout(signature); | 857 | const RootArgumentLayout* layout = root_signature_layout(signature); |
| 819 | if (layout && layout == state.layout) return; | 858 | if (layout && layout == state.layout) return; |
| 820 | hold(signature); // state.layout points into it | 859 | hold(signature); // state.layout points into it |
| @@ -842,6 +881,22 @@ private: | |||
| 842 | } | 881 | } |
| 843 | 882 | ||
| 844 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { | 883 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { |
| 884 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { | ||
| 885 | // The layout is the executing list's, so the range is checked when it runs; no signature takes more | ||
| 886 | // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory. | ||
| 887 | if (count > D3D12_MAX_ROOT_COST) return check(false); | ||
| 888 | const auto* words = static_cast<const std::uint32_t*>(data); | ||
| 889 | std::vector<std::uint32_t> copy; | ||
| 890 | if (data) copy.assign(words, words + count); | ||
| 891 | // A count of 0 with a non-null pointer is a valid call; an empty vector's data() may be null. | ||
| 892 | static constexpr std::uint32_t none = 0; | ||
| 893 | defer([=, compute = &state == &compute_, copy = std::move(copy), | ||
| 894 | given = data != nullptr](GraphicsCommandList& l) { | ||
| 895 | l.constants(compute ? l.compute_ : l.graphics_, index, count, | ||
| 896 | !given ? nullptr : copy.empty() ? &none : copy.data(), offset); | ||
| 897 | }); | ||
| 898 | return; | ||
| 899 | } | ||
| 845 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); | 900 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); |
| 846 | if (!p) return; | 901 | if (!p) return; |
| 847 | if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); | 902 | if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); |
| @@ -850,6 +905,10 @@ private: | |||
| 850 | } | 905 | } |
| 851 | 906 | ||
| 852 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { | 907 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { |
| 908 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | ||
| 909 | l.address(compute ? l.compute_ : l.graphics_, index, va, type); | ||
| 910 | })) | ||
| 911 | return; | ||
| 853 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 912 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 854 | if (!p) return; | 913 | if (!p) return; |
| 855 | write(state, p->user_offset, std::uint32_t(va)); | 914 | write(state, p->user_offset, std::uint32_t(va)); |
| @@ -862,7 +921,6 @@ private: | |||
| 862 | bool invalid_ = false; | 921 | bool invalid_ = false; |
| 863 | UINT front_stencil_ref_ = 0; | 922 | UINT front_stencil_ref_ = 0; |
| 864 | UINT back_stencil_ref_ = 0; | 923 | UINT back_stencil_ref_ = 0; |
| 865 | bool stencil_ref_set_ = false; | ||
| 866 | RootState compute_; | 924 | RootState compute_; |
| 867 | RootState graphics_; | 925 | RootState graphics_; |
| 868 | std::uintptr_t compute_pipeline_ = 0; | 926 | std::uintptr_t compute_pipeline_ = 0; |
| @@ -875,7 +933,6 @@ private: | |||
| 875 | std::vector<leonida::TargetView> render_targets_; | 933 | std::vector<leonida::TargetView> render_targets_; |
| 876 | leonida::TargetView depth_{}; | 934 | leonida::TargetView depth_{}; |
| 877 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; | 935 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; |
| 878 | bool blend_factor_set_ = false; | ||
| 879 | 936 | ||
| 880 | // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right | 937 | // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right |
| 881 | // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root | 938 | // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root |
| @@ -883,6 +940,31 @@ private: | |||
| 883 | // runs the submission stays the application's job, as in D3D12.) | 940 | // runs the submission stays the application's job, as in D3D12.) |
| 884 | std::vector<IUnknown*> held_; | 941 | std::vector<IUnknown*> held_; |
| 885 | 942 | ||
| 943 | // A bundle keeps its state calls and draws instead of applying them: it has no render targets, viewport or root | ||
| 944 | // signature of its own to resolve them against, and what it sets belongs to the list that executes it. | ||
| 945 | // `commands` is how much of the bundle's own list was recorded before the call, so ExecuteBundle can replay the | ||
| 946 | // calls and copy the state-free commands (markers, immediate writes) in their original order. Returns whether the | ||
| 947 | // call was kept; on any other list it is false and the caller does the work. | ||
| 948 | struct BundleCall { | ||
| 949 | std::size_t commands; | ||
| 950 | std::function<void(GraphicsCommandList&)> run; | ||
| 951 | }; | ||
| 952 | std::vector<BundleCall> calls_; | ||
| 953 | |||
| 954 | // Every deferred call replays through a private member that does not trace: the trace line is written once, when | ||
| 955 | // the application makes the call, and not again at each ExecuteBundle. After Close nothing is appended, since | ||
| 956 | // another list may be executing the bundle: a state call is ignored, and a draw or dispatch (`command`) makes | ||
| 957 | // the list invalid. | ||
| 958 | bool defer(std::function<void(GraphicsCommandList&)> run, bool command = false) { | ||
| 959 | if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return false; | ||
| 960 | if (list_.closed()) { | ||
| 961 | if (command) check(false); | ||
| 962 | return true; | ||
| 963 | } | ||
| 964 | calls_.push_back({list_.commands().size(), std::move(run)}); | ||
| 965 | return true; | ||
| 966 | } | ||
| 967 | |||
| 886 | void hold(IUnknown* object) { | 968 | void hold(IUnknown* object) { |
| 887 | if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; | 969 | if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; |
| 888 | object->AddRef(); | 970 | object->AddRef(); |
| @@ -914,10 +996,26 @@ private: | |||
| 914 | render_targets_.clear(); | 996 | render_targets_.clear(); |
| 915 | depth_ = {}; | 997 | depth_ = {}; |
| 916 | blend_factor_ = {1, 1, 1, 1}; | 998 | blend_factor_ = {1, 1, 1, 1}; |
| 917 | blend_factor_set_ = false; | ||
| 918 | set_pipeline(state); | 999 | set_pipeline(state); |
| 919 | } | 1000 | } |
| 920 | 1001 | ||
| 1002 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | ||
| 1003 | for (UINT i = 0; i < count; ++i) | ||
| 1004 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | ||
| 1005 | views[i].StrideInBytes} | ||
| 1006 | : leonida::VertexBufferView{}; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { | ||
| 1010 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | ||
| 1011 | : leonida::IndexBufferView{}; | ||
| 1012 | } | ||
| 1013 | |||
| 1014 | void record_dispatch(UINT x, UINT y, UINT z) { | ||
| 1015 | if (!compute_pipeline_ || !compute_.layout) return check(false); | ||
| 1016 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | ||
| 1017 | } | ||
| 1018 | |||
| 921 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1019 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 922 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1020 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 923 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1021 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| @@ -1290,7 +1388,7 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void | |||
| 1290 | } | 1388 | } |
| 1291 | 1389 | ||
| 1292 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, | 1390 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, |
| 1293 | void** out, IUnknown* device) { | 1391 | void** out, IUnknown* device, ID3D12PipelineState* initial_state) { |
| 1294 | if (!out) return E_POINTER; | 1392 | if (!out) return E_POINTER; |
| 1295 | *out = nullptr; | 1393 | *out = nullptr; |
| 1296 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); | 1394 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); |
| @@ -1303,7 +1401,12 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator | |||
| 1303 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does | 1401 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does |
| 1304 | object->record_into(command_allocator); | 1402 | object->record_into(command_allocator); |
| 1305 | object->attach_device(device); | 1403 | object->attach_device(device); |
| 1306 | return hand_out(object, riid, out); | 1404 | const HRESULT hr = hand_out(object, riid, out); // releases the creation reference, so a failure frees the list |
| 1405 | // The initial pipeline state only once the list exists for the caller, as SetPipelineState on it would be. | ||
| 1406 | if (SUCCEEDED(hr) && initial_state) | ||
| 1407 | if (GraphicsCommandList* list = GraphicsCommandList::from(static_cast<IUnknown*>(*out))) | ||
| 1408 | list->set_initial_pipeline(initial_state); | ||
| 1409 | return hr; | ||
| 1307 | } | 1410 | } |
| 1308 | 1411 | ||
| 1309 | HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, | 1412 | HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, |
| @@ -1384,4 +1487,12 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& | |||
| 1384 | return true; | 1487 | return true; |
| 1385 | } | 1488 | } |
| 1386 | 1489 | ||
| 1490 | bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) { | ||
| 1491 | const GraphicsCommandList* l = GraphicsCommandList::from(list); | ||
| 1492 | if (!l) return false; | ||
| 1493 | const auto recorded = l->commands().commands(); | ||
| 1494 | commands.assign(recorded.begin(), recorded.end()); | ||
| 1495 | return true; | ||
| 1496 | } | ||
| 1497 | |||
| 1387 | } // namespace rt::d3d12 | 1498 | } // namespace rt::d3d12 |