diff options
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 177 |
1 files changed, 149 insertions, 28 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 663f5f3a5..e1c8deca1 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 |
| @@ -242,6 +256,7 @@ public: | |||
| 242 | 256 | ||
| 243 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { | 257 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { |
| 244 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); | 258 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); |
| 259 | if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); | ||
| 245 | set_pipeline(state); | 260 | set_pipeline(state); |
| 246 | } | 261 | } |
| 247 | 262 | ||
| @@ -289,8 +304,8 @@ public: | |||
| 289 | } | 304 | } |
| 290 | 305 | ||
| 291 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { | 306 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { |
| 292 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 307 | if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return; |
| 293 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 308 | record_dispatch(x, y, z); |
| 294 | } | 309 | } |
| 295 | 310 | ||
| 296 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). | 311 | // ClearState returns every piece of list state to its default, as Reset does (stencil references included). |
| @@ -298,20 +313,27 @@ public: | |||
| 298 | 313 | ||
| 299 | // Graphics state. Draws record a copy of it (command_list.h, Draw). | 314 | // Graphics state. Draws record a copy of it (command_list.h, Draw). |
| 300 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { | 315 | void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { |
| 316 | if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return; | ||
| 301 | topology_ = u(topology); | 317 | topology_ = u(topology); |
| 302 | } | 318 | } |
| 303 | 319 | ||
| 304 | void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { | 320 | void STDMETHODCALLTYPE IASetVertexBuffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) override { |
| 305 | if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); | 321 | if (std::uint64_t(start) + count > vertex_buffers_.size()) return check(false); |
| 306 | for (UINT i = 0; i < count; ++i) | 322 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { |
| 307 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 323 | std::vector<D3D12_VERTEX_BUFFER_VIEW> copy; |
| 308 | views[i].StrideInBytes} | 324 | if (views) copy.assign(views, views + count); |
| 309 | : leonida::VertexBufferView{}; | 325 | defer([=, copy = std::move(copy), bound = views != nullptr](GraphicsCommandList& l) { |
| 326 | l.set_vertex_buffers(start, count, bound ? copy.data() : nullptr); | ||
| 327 | }); | ||
| 328 | return; | ||
| 329 | } | ||
| 330 | set_vertex_buffers(start, count, views); | ||
| 310 | } | 331 | } |
| 311 | 332 | ||
| 312 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { | 333 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { |
| 313 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | 334 | const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt; |
| 314 | : leonida::IndexBufferView{}; | 335 | if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; |
| 336 | set_index_buffer(view); | ||
| 315 | } | 337 | } |
| 316 | 338 | ||
| 317 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). | 339 | // A count of 0 unbinds the viewports; draws then rasterize nothing (record_draw). |
| @@ -345,24 +367,32 @@ public: | |||
| 345 | } | 367 | } |
| 346 | 368 | ||
| 347 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { | 369 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { |
| 348 | blend_factor_set_ = true; | 370 | const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} |
| 349 | blend_factor_ = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} | 371 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } |
| 350 | : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } | 372 | if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; |
| 373 | blend_factor_ = value; | ||
| 351 | } | 374 | } |
| 352 | 375 | ||
| 353 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { | 376 | void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { |
| 377 | if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; | ||
| 354 | front_stencil_ref_ = back_stencil_ref_ = reference; | 378 | front_stencil_ref_ = back_stencil_ref_ = reference; |
| 355 | stencil_ref_set_ = true; // the bundle carry-back in ExecuteBundle needs to see this | ||
| 356 | } | 379 | } |
| 357 | 380 | ||
| 358 | void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { | 381 | void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { |
| 359 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); | 382 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); |
| 383 | if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); }, | ||
| 384 | true)) | ||
| 385 | return; | ||
| 360 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); | 386 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); |
| 361 | } | 387 | } |
| 362 | 388 | ||
| 363 | void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, | 389 | void STDMETHODCALLTYPE DrawIndexedInstanced(UINT indices, UINT instances, UINT first_index, INT base_vertex, |
| 364 | UINT first_instance) override { | 390 | UINT first_instance) override { |
| 365 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); | 391 | d3d12_trace::record("ID3D12GraphicsCommandList::DrawIndexedInstanced"); |
| 392 | if (defer([=](GraphicsCommandList& l) { | ||
| 393 | l.record_draw(true, indices, instances, first_index, base_vertex, first_instance); | ||
| 394 | }, true)) | ||
| 395 | return; | ||
| 366 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); | 396 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); |
| 367 | } | 397 | } |
| 368 | 398 | ||
| @@ -471,9 +501,19 @@ public: | |||
| 471 | 501 | ||
| 472 | // The format picks the member of a typeless family to resolve as; the textures' own format is used. | 502 | // The format picks the member of a typeless family to resolve as; the textures' own format is used. |
| 473 | void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, | 503 | void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, |
| 474 | UINT src_subresource, DXGI_FORMAT) override { | 504 | UINT src_subresource, DXGI_FORMAT format) override { |
| 475 | d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); | 505 | d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); |
| 476 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); | 506 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); |
| 507 | const auto integer_color = [](ID3D12Resource* resource) { | ||
| 508 | if (!resource) return false; | ||
| 509 | const auto desc = resource->GetDesc(); | ||
| 510 | return !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) && | ||
| 511 | color_resolve_format_is_integer(desc.Format); | ||
| 512 | }; | ||
| 513 | if (color_resolve_format_is_integer(format) || integer_color(dst) || integer_color(src)) { | ||
| 514 | note_once("ResolveSubresource refused: integer colour formats cannot be resolved"); | ||
| 515 | return check(false); | ||
| 516 | } | ||
| 477 | check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource})); | 517 | check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource})); |
| 478 | } | 518 | } |
| 479 | 519 | ||
| @@ -603,9 +643,13 @@ public: | |||
| 603 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { | 643 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { |
| 604 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); | 644 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); |
| 605 | if (list_.closed()) return check(false); | 645 | if (list_.closed()) return check(false); |
| 646 | if (defer([=](GraphicsCommandList& l) { | ||
| 647 | l.front_stencil_ref_ = front; | ||
| 648 | l.back_stencil_ref_ = back; | ||
| 649 | })) | ||
| 650 | return; | ||
| 606 | front_stencil_ref_ = front; | 651 | front_stencil_ref_ = front; |
| 607 | back_stencil_ref_ = back; | 652 | back_stencil_ref_ = back; |
| 608 | stencil_ref_set_ = true; | ||
| 609 | } | 653 | } |
| 610 | 654 | ||
| 611 | // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace | 655 | // Debug markers, recorded in order with the other commands and replayed as VK_EXT_debug_utils labels. The trace |
| @@ -785,6 +829,7 @@ private: | |||
| 785 | 829 | ||
| 786 | void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, | 830 | void clear_unordered_access(D3D12_CPU_DESCRIPTOR_HANDLE view, ID3D12Resource* resource, bool is_float, |
| 787 | const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { | 831 | const std::array<std::uint32_t, 4>& values, UINT count, const D3D12_RECT* rects) { |
| 832 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); // no clear is allowed in a bundle | ||
| 788 | leonida::UavView target{}; | 833 | leonida::UavView target{}; |
| 789 | if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); | 834 | if (!view.ptr || !resource || !unordered_access_view(view, resource, target)) return check(false); |
| 790 | check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); | 835 | check(list_.clear_unordered_access(target, is_float, values, to_rects(count, rects))); |
| @@ -853,6 +898,10 @@ private: | |||
| 853 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" | 898 | // signature that is already bound changes nothing: "existing root signature bindings do not become stale" |
| 854 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. | 899 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. |
| 855 | void bind_root(RootState& state, ID3D12RootSignature* signature) { | 900 | void bind_root(RootState& state, ID3D12RootSignature* signature) { |
| 901 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | ||
| 902 | l.bind_root(compute ? l.compute_ : l.graphics_, signature); | ||
| 903 | })) | ||
| 904 | return hold(signature); // the bundle keeps it for as long as it can be executed | ||
| 856 | const RootArgumentLayout* layout = root_signature_layout(signature); | 905 | const RootArgumentLayout* layout = root_signature_layout(signature); |
| 857 | if (layout && layout == state.layout) return; | 906 | if (layout && layout == state.layout) return; |
| 858 | hold(signature); // state.layout points into it | 907 | hold(signature); // state.layout points into it |
| @@ -880,6 +929,22 @@ private: | |||
| 880 | } | 929 | } |
| 881 | 930 | ||
| 882 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { | 931 | void constants(RootState& state, UINT index, UINT count, const void* data, UINT offset) { |
| 932 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { | ||
| 933 | // The layout is the executing list's, so the range is checked when it runs; no signature takes more | ||
| 934 | // than D3D12_MAX_ROOT_COST dwords, which bounds what is copied from the caller's memory. | ||
| 935 | if (count > D3D12_MAX_ROOT_COST) return check(false); | ||
| 936 | const auto* words = static_cast<const std::uint32_t*>(data); | ||
| 937 | std::vector<std::uint32_t> copy; | ||
| 938 | if (data) copy.assign(words, words + count); | ||
| 939 | // A count of 0 with a non-null pointer is a valid call; an empty vector's data() may be null. | ||
| 940 | static constexpr std::uint32_t none = 0; | ||
| 941 | defer([=, compute = &state == &compute_, copy = std::move(copy), | ||
| 942 | given = data != nullptr](GraphicsCommandList& l) { | ||
| 943 | l.constants(compute ? l.compute_ : l.graphics_, index, count, | ||
| 944 | !given ? nullptr : copy.empty() ? &none : copy.data(), offset); | ||
| 945 | }); | ||
| 946 | return; | ||
| 947 | } | ||
| 883 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); | 948 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); |
| 884 | if (!p) return; | 949 | if (!p) return; |
| 885 | if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); | 950 | if (!data || std::uint64_t(offset) + count > p->constant_count) return check(false); |
| @@ -888,6 +953,10 @@ private: | |||
| 888 | } | 953 | } |
| 889 | 954 | ||
| 890 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { | 955 | void address(RootState& state, UINT index, std::uint64_t va, rt::rootsig::ParamType type) { |
| 956 | if (defer([=, compute = &state == &compute_](GraphicsCommandList& l) { | ||
| 957 | l.address(compute ? l.compute_ : l.graphics_, index, va, type); | ||
| 958 | })) | ||
| 959 | return; | ||
| 891 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 960 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 892 | if (!p) return; | 961 | if (!p) return; |
| 893 | write(state, p->user_offset, std::uint32_t(va)); | 962 | write(state, p->user_offset, std::uint32_t(va)); |
| @@ -900,7 +969,6 @@ private: | |||
| 900 | bool invalid_ = false; | 969 | bool invalid_ = false; |
| 901 | UINT front_stencil_ref_ = 0; | 970 | UINT front_stencil_ref_ = 0; |
| 902 | UINT back_stencil_ref_ = 0; | 971 | UINT back_stencil_ref_ = 0; |
| 903 | bool stencil_ref_set_ = false; | ||
| 904 | RootState compute_; | 972 | RootState compute_; |
| 905 | RootState graphics_; | 973 | RootState graphics_; |
| 906 | std::uintptr_t compute_pipeline_ = 0; | 974 | std::uintptr_t compute_pipeline_ = 0; |
| @@ -913,7 +981,6 @@ private: | |||
| 913 | std::vector<leonida::TargetView> render_targets_; | 981 | std::vector<leonida::TargetView> render_targets_; |
| 914 | leonida::TargetView depth_{}; | 982 | leonida::TargetView depth_{}; |
| 915 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; | 983 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; |
| 916 | bool blend_factor_set_ = false; | ||
| 917 | 984 | ||
| 918 | // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right | 985 | // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right |
| 919 | // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root | 986 | // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root |
| @@ -921,6 +988,31 @@ private: | |||
| 921 | // runs the submission stays the application's job, as in D3D12.) | 988 | // runs the submission stays the application's job, as in D3D12.) |
| 922 | std::vector<IUnknown*> held_; | 989 | std::vector<IUnknown*> held_; |
| 923 | 990 | ||
| 991 | // A bundle keeps its state calls and draws instead of applying them: it has no render targets, viewport or root | ||
| 992 | // signature of its own to resolve them against, and what it sets belongs to the list that executes it. | ||
| 993 | // `commands` is how much of the bundle's own list was recorded before the call, so ExecuteBundle can replay the | ||
| 994 | // calls and copy the state-free commands (markers, immediate writes) in their original order. Returns whether the | ||
| 995 | // call was kept; on any other list it is false and the caller does the work. | ||
| 996 | struct BundleCall { | ||
| 997 | std::size_t commands; | ||
| 998 | std::function<void(GraphicsCommandList&)> run; | ||
| 999 | }; | ||
| 1000 | std::vector<BundleCall> calls_; | ||
| 1001 | |||
| 1002 | // Every deferred call replays through a private member that does not trace: the trace line is written once, when | ||
| 1003 | // the application makes the call, and not again at each ExecuteBundle. After Close nothing is appended, since | ||
| 1004 | // another list may be executing the bundle: a state call is ignored, and a draw or dispatch (`command`) makes | ||
| 1005 | // the list invalid. | ||
| 1006 | bool defer(std::function<void(GraphicsCommandList&)> run, bool command = false) { | ||
| 1007 | if (type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE) return false; | ||
| 1008 | if (list_.closed()) { | ||
| 1009 | if (command) check(false); | ||
| 1010 | return true; | ||
| 1011 | } | ||
| 1012 | calls_.push_back({list_.commands().size(), std::move(run)}); | ||
| 1013 | return true; | ||
| 1014 | } | ||
| 1015 | |||
| 924 | void hold(IUnknown* object) { | 1016 | void hold(IUnknown* object) { |
| 925 | if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; | 1017 | if (!object || std::find(held_.begin(), held_.end(), object) != held_.end()) return; |
| 926 | object->AddRef(); | 1018 | object->AddRef(); |
| @@ -952,10 +1044,26 @@ private: | |||
| 952 | render_targets_.clear(); | 1044 | render_targets_.clear(); |
| 953 | depth_ = {}; | 1045 | depth_ = {}; |
| 954 | blend_factor_ = {1, 1, 1, 1}; | 1046 | blend_factor_ = {1, 1, 1, 1}; |
| 955 | blend_factor_set_ = false; | ||
| 956 | set_pipeline(state); | 1047 | set_pipeline(state); |
| 957 | } | 1048 | } |
| 958 | 1049 | ||
| 1050 | void set_vertex_buffers(UINT start, UINT count, const D3D12_VERTEX_BUFFER_VIEW* views) { | ||
| 1051 | for (UINT i = 0; i < count; ++i) | ||
| 1052 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | ||
| 1053 | views[i].StrideInBytes} | ||
| 1054 | : leonida::VertexBufferView{}; | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | void set_index_buffer(const D3D12_INDEX_BUFFER_VIEW* view) { | ||
| 1058 | index_ = view ? leonida::IndexBufferView{view->BufferLocation, view->SizeInBytes, u(view->Format)} | ||
| 1059 | : leonida::IndexBufferView{}; | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | void record_dispatch(UINT x, UINT y, UINT z) { | ||
| 1063 | if (!compute_pipeline_ || !compute_.layout) return check(false); | ||
| 1064 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | ||
| 1065 | } | ||
| 1066 | |||
| 959 | // A draw needs a graphics pipeline, a graphics root signature and a topology. | 1067 | // A draw needs a graphics pipeline, a graphics root signature and a topology. |
| 960 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { | 1068 | void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { |
| 961 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1069 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| @@ -1396,7 +1504,7 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void | |||
| 1396 | } | 1504 | } |
| 1397 | 1505 | ||
| 1398 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, | 1506 | HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator* allocator, REFIID riid, |
| 1399 | void** out, IUnknown* device) { | 1507 | void** out, IUnknown* device, ID3D12PipelineState* initial_state) { |
| 1400 | if (!out) return E_POINTER; | 1508 | if (!out) return E_POINTER; |
| 1401 | *out = nullptr; | 1509 | *out = nullptr; |
| 1402 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); | 1510 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); |
| @@ -1409,7 +1517,12 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator | |||
| 1409 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does | 1517 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does |
| 1410 | object->record_into(command_allocator); | 1518 | object->record_into(command_allocator); |
| 1411 | object->attach_device(device); | 1519 | object->attach_device(device); |
| 1412 | return hand_out(object, riid, out); | 1520 | const HRESULT hr = hand_out(object, riid, out); // releases the creation reference, so a failure frees the list |
| 1521 | // The initial pipeline state only once the list exists for the caller, as SetPipelineState on it would be. | ||
| 1522 | if (SUCCEEDED(hr) && initial_state) | ||
| 1523 | if (GraphicsCommandList* list = GraphicsCommandList::from(static_cast<IUnknown*>(*out))) | ||
| 1524 | list->set_initial_pipeline(initial_state); | ||
| 1525 | return hr; | ||
| 1413 | } | 1526 | } |
| 1414 | 1527 | ||
| 1415 | HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, | 1528 | HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, |
| @@ -1490,4 +1603,12 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& | |||
| 1490 | return true; | 1603 | return true; |
| 1491 | } | 1604 | } |
| 1492 | 1605 | ||
| 1606 | bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Command>& commands) { | ||
| 1607 | const GraphicsCommandList* l = GraphicsCommandList::from(list); | ||
| 1608 | if (!l) return false; | ||
| 1609 | const auto recorded = l->commands().commands(); | ||
| 1610 | commands.assign(recorded.begin(), recorded.end()); | ||
| 1611 | return true; | ||
| 1612 | } | ||
| 1613 | |||
| 1493 | } // namespace rt::d3d12 | 1614 | } // namespace rt::d3d12 |