diff options
| author | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 20:27:12 +0100 |
|---|---|---|
| committer | Vonsek <45567075+Vonsek@users.noreply.github.com> | 2026-11-22 20:27:12 +0100 |
| commit | 3eb80d77e384a8c51bb58076dc881d7d291f8001 (patch) | |
| tree | eb76604305dca9a75845c36bdbbe8fe066051fe1 /runtime/shims/d3d12_com.cpp | |
| parent | b87dc1e9af21b11375c29dfa1046232eccbed2db (diff) | |
| parent | 162821017ea3c286c8bec860f3ff94f037912061 (diff) | |
Merge main into cov-d16fmt (#2039)
Conflicts:
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py
tests/test_shader_checks.py
AI-assisted.
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
| -rw-r--r-- | runtime/shims/d3d12_com.cpp | 313 |
1 files changed, 270 insertions, 43 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 55c6f86a8..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> |
| @@ -24,6 +26,22 @@ | |||
| 24 | 26 | ||
| 25 | namespace rt::d3d12 { | 27 | namespace rt::d3d12 { |
| 26 | 28 | ||
| 29 | static std::uint64_t buffer_tiles(ID3D12Resource* buffer) { | ||
| 30 | if (!buffer) return 0; | ||
| 31 | const auto desc = buffer->GetDesc(); | ||
| 32 | if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || !desc.Width) return 0; | ||
| 33 | return 1 + (desc.Width - 1) / leonida::tile_size_in_bytes; | ||
| 34 | } | ||
| 35 | |||
| 36 | static bool buffer_tile_region(const D3D12_TILED_RESOURCE_COORDINATE* start, const D3D12_TILE_REGION_SIZE* size, | ||
| 37 | std::uint64_t tiles, leonida::BufferTileRegion& region) { | ||
| 38 | if (start && (start->Y || start->Z || start->Subresource)) return false; | ||
| 39 | if (size && size->UseBox && (size->Height != 1 || size->Depth != 1)) return false; | ||
| 40 | region.first = start ? start->X : 0; | ||
| 41 | region.count = size ? (size->UseBox ? size->Width : size->NumTiles) : (start ? 1 : tiles); | ||
| 42 | return region.count && region.first < tiles && region.count <= tiles - region.first; | ||
| 43 | } | ||
| 44 | |||
| 27 | void stub(const char* method) { | 45 | void stub(const char* method) { |
| 28 | // Generated API stubs pass a bare method name; explanatory diagnostics are not separate calls. | 46 | // Generated API stubs pass a bare method name; explanatory diagnostics are not separate calls. |
| 29 | if (!std::strchr(method, '(')) d3d12_trace::record(method, "unsupported"); | 47 | if (!std::strchr(method, '(')) d3d12_trace::record(method, "unsupported"); |
| @@ -190,12 +208,22 @@ public: | |||
| 190 | 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 |
| 191 | record_into(command_allocator); | 209 | record_into(command_allocator); |
| 192 | invalid_ = false; | 210 | invalid_ = false; |
| 211 | calls_.clear(); | ||
| 193 | 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 |
| 194 | reset_state(state); // Reset returns every list state to its default | 213 | reset_state(state); // Reset returns every list state to its default |
| 195 | stencil_ref_set_ = false; | 214 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) set_initial_pipeline(state); // reset_state holds it |
| 196 | return d3d12_trace::result(method, S_OK); | 215 | return d3d12_trace::result(method, S_OK); |
| 197 | } | 216 | } |
| 198 | 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 | |||
| 199 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { | 227 | void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { |
| 200 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); | 228 | d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); |
| 201 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); | 229 | if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); |
| @@ -203,15 +231,17 @@ public: | |||
| 203 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || | 231 | if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || |
| 204 | !bundle->list_.closed()) | 232 | !bundle->list_.closed()) |
| 205 | return check(false); | 233 | return check(false); |
| 206 | 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 |
| 207 | 235 | // 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. | 236 | // 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_; | 237 | // were recorded in the bundle's own list and are copied between them, in order. |
| 210 | if (bundle->stencil_ref_set_) { | 238 | std::size_t copied = 0; |
| 211 | front_stencil_ref_ = bundle->front_stencil_ref_; | 239 | for (const BundleCall& call : bundle->calls_) { |
| 212 | back_stencil_ref_ = bundle->back_stencil_ref_; | 240 | if (!list_.execute_bundle(bundle->list_, copied, call.commands)) return check(false); |
| 213 | stencil_ref_set_ = true; | 241 | copied = call.commands; |
| 242 | call.run(*this); | ||
| 214 | } | 243 | } |
| 244 | check(list_.execute_bundle(bundle->list_, copied)); | ||
| 215 | } | 245 | } |
| 216 | 246 | ||
| 217 | // 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 |
| @@ -226,11 +256,14 @@ public: | |||
| 226 | 256 | ||
| 227 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { | 257 | void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { |
| 228 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); | 258 | d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); |
| 259 | if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); | ||
| 229 | set_pipeline(state); | 260 | set_pipeline(state); |
| 230 | } | 261 | } |
| 231 | 262 | ||
| 232 | // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. | 263 | // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. |
| 233 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override {} | 264 | void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override { |
| 265 | d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps", "ignored"); | ||
| 266 | } | ||
| 234 | 267 | ||
| 235 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { | 268 | void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { |
| 236 | constants(compute_, index, 1, &value, offset); | 269 | constants(compute_, index, 1, &value, offset); |
| @@ -266,12 +299,13 @@ public: | |||
| 266 | address(graphics_, index, va, rt::rootsig::ParamType::Descriptor); | 299 | address(graphics_, index, va, rt::rootsig::ParamType::Descriptor); |
| 267 | } | 300 | } |
| 268 | void STDMETHODCALLTYPE SetGraphicsRootDescriptorTable(UINT index, D3D12_GPU_DESCRIPTOR_HANDLE table) override { | 301 | void STDMETHODCALLTYPE SetGraphicsRootDescriptorTable(UINT index, D3D12_GPU_DESCRIPTOR_HANDLE table) override { |
| 302 | d3d12_trace::record("ID3D12GraphicsCommandList::SetGraphicsRootDescriptorTable"); | ||
| 269 | address(graphics_, index, table.ptr, rt::rootsig::ParamType::Table); | 303 | address(graphics_, index, table.ptr, rt::rootsig::ParamType::Table); |
| 270 | } | 304 | } |
| 271 | 305 | ||
| 272 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { | 306 | void STDMETHODCALLTYPE Dispatch(UINT x, UINT y, UINT z) override { |
| 273 | if (!compute_pipeline_ || !compute_.layout) return check(false); | 307 | if (defer([=](GraphicsCommandList& l) { l.record_dispatch(x, y, z); }, true)) return; |
| 274 | check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); | 308 | record_dispatch(x, y, z); |
| 275 | } | 309 | } |
| 276 | 310 | ||
| 277 | // 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). |
| @@ -279,20 +313,27 @@ public: | |||
| 279 | 313 | ||
| 280 | // 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). |
| 281 | 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; | ||
| 282 | topology_ = u(topology); | 317 | topology_ = u(topology); |
| 283 | } | 318 | } |
| 284 | 319 | ||
| 285 | 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 { |
| 286 | 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); |
| 287 | for (UINT i = 0; i < count; ++i) | 322 | if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) { |
| 288 | vertex_buffers_[start + i] = views ? leonida::VertexBufferView{views[i].BufferLocation, views[i].SizeInBytes, | 323 | std::vector<D3D12_VERTEX_BUFFER_VIEW> copy; |
| 289 | views[i].StrideInBytes} | 324 | if (views) copy.assign(views, views + count); |
| 290 | : 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); | ||
| 291 | } | 331 | } |
| 292 | 332 | ||
| 293 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { | 333 | void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { |
| 294 | 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; |
| 295 | : leonida::IndexBufferView{}; | 335 | if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; |
| 336 | set_index_buffer(view); | ||
| 296 | } | 337 | } |
| 297 | 338 | ||
| 298 | // 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). |
| @@ -326,24 +367,32 @@ public: | |||
| 326 | } | 367 | } |
| 327 | 368 | ||
| 328 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { | 369 | void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { |
| 329 | blend_factor_set_ = true; | 370 | 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]} | 371 | : 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 } | 372 | if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; |
| 373 | blend_factor_ = value; | ||
| 332 | } | 374 | } |
| 333 | 375 | ||
| 334 | 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; | ||
| 335 | front_stencil_ref_ = back_stencil_ref_ = reference; | 378 | front_stencil_ref_ = back_stencil_ref_ = reference; |
| 336 | stencil_ref_set_ = true; // the bundle carry-back in ExecuteBundle needs to see this | ||
| 337 | } | 379 | } |
| 338 | 380 | ||
| 339 | 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 { |
| 340 | 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; | ||
| 341 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); | 386 | record_draw(false, vertices, instances, first_vertex, 0, first_instance); |
| 342 | } | 387 | } |
| 343 | 388 | ||
| 344 | 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, |
| 345 | UINT first_instance) override { | 390 | UINT first_instance) override { |
| 346 | 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; | ||
| 347 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); | 396 | record_draw(true, indices, instances, first_index, base_vertex, first_instance); |
| 348 | } | 397 | } |
| 349 | 398 | ||
| @@ -415,6 +464,25 @@ public: | |||
| 415 | check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes})); | 464 | check(list_.copy_buffer({resource_id(dst), dst_offset, resource_id(src), src_offset, bytes})); |
| 416 | } | 465 | } |
| 417 | 466 | ||
| 467 | void STDMETHODCALLTYPE CopyTiles(ID3D12Resource* tiled, const D3D12_TILED_RESOURCE_COORDINATE* start, | ||
| 468 | const D3D12_TILE_REGION_SIZE* size, ID3D12Resource* linear, | ||
| 469 | UINT64 linear_offset, D3D12_TILE_COPY_FLAGS flags) override { | ||
| 470 | d3d12_trace::record("ID3D12GraphicsCommandList::CopyTiles"); | ||
| 471 | const auto direction = flags & ~D3D12_TILE_COPY_FLAG_NO_HAZARD; | ||
| 472 | const bool to_linear = direction == D3D12_TILE_COPY_FLAG_SWIZZLED_TILED_RESOURCE_TO_LINEAR_BUFFER; | ||
| 473 | leonida::BufferTileRegion region; | ||
| 474 | if (!start || !size || !linear || linear == tiled || | ||
| 475 | !buffer_tile_region(start, size, buffer_tiles(tiled), region) || | ||
| 476 | (!to_linear && direction != D3D12_TILE_COPY_FLAG_LINEAR_BUFFER_TO_SWIZZLED_TILED_RESOURCE)) | ||
| 477 | return check(false); | ||
| 478 | const auto tiled_id = resource_id(tiled), linear_id = resource_id(linear); | ||
| 479 | const auto tiled_offset = region.first * leonida::tile_size_in_bytes; | ||
| 480 | const auto bytes = region.count * leonida::tile_size_in_bytes; | ||
| 481 | check(list_.copy_buffer(to_linear | ||
| 482 | ? leonida::CopyBuffer{linear_id, linear_offset, tiled_id, tiled_offset, bytes, tiled_id} | ||
| 483 | : leonida::CopyBuffer{tiled_id, tiled_offset, linear_id, linear_offset, bytes, tiled_id})); | ||
| 484 | } | ||
| 485 | |||
| 418 | void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z, | 486 | void STDMETHODCALLTYPE CopyTextureRegion(const D3D12_TEXTURE_COPY_LOCATION* dst, UINT x, UINT y, UINT z, |
| 419 | const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override { | 487 | const D3D12_TEXTURE_COPY_LOCATION* src, const D3D12_BOX* box) override { |
| 420 | d3d12_trace::record("ID3D12GraphicsCommandList::CopyTextureRegion"); | 488 | d3d12_trace::record("ID3D12GraphicsCommandList::CopyTextureRegion"); |
| @@ -433,9 +501,19 @@ public: | |||
| 433 | 501 | ||
| 434 | // 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. |
| 435 | void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, | 503 | void STDMETHODCALLTYPE ResolveSubresource(ID3D12Resource* dst, UINT dst_subresource, ID3D12Resource* src, |
| 436 | UINT src_subresource, DXGI_FORMAT) override { | 504 | UINT src_subresource, DXGI_FORMAT format) override { |
| 437 | d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); | 505 | d3d12_trace::record("ID3D12GraphicsCommandList::ResolveSubresource"); |
| 438 | 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 | } | ||
| 439 | 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})); |
| 440 | } | 518 | } |
| 441 | 519 | ||
| @@ -565,9 +643,13 @@ public: | |||
| 565 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { | 643 | void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { |
| 566 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); | 644 | d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); |
| 567 | 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; | ||
| 568 | front_stencil_ref_ = front; | 651 | front_stencil_ref_ = front; |
| 569 | back_stencil_ref_ = back; | 652 | back_stencil_ref_ = back; |
| 570 | stencil_ref_set_ = true; | ||
| 571 | } | 653 | } |
| 572 | 654 | ||
| 573 | // 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 |
| @@ -747,6 +829,7 @@ private: | |||
| 747 | 829 | ||
| 748 | 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, |
| 749 | 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 | ||
| 750 | leonida::UavView target{}; | 833 | leonida::UavView target{}; |
| 751 | 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); |
| 752 | 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))); |
| @@ -815,6 +898,10 @@ private: | |||
| 815 | // 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" |
| 816 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. | 899 | // (D3D12, "Using a Root Signature"), and engines rebind routinely. |
| 817 | 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 | ||
| 818 | const RootArgumentLayout* layout = root_signature_layout(signature); | 905 | const RootArgumentLayout* layout = root_signature_layout(signature); |
| 819 | if (layout && layout == state.layout) return; | 906 | if (layout && layout == state.layout) return; |
| 820 | hold(signature); // state.layout points into it | 907 | hold(signature); // state.layout points into it |
| @@ -842,6 +929,22 @@ private: | |||
| 842 | } | 929 | } |
| 843 | 930 | ||
| 844 | 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 | } | ||
| 845 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); | 948 | const RootArgumentLayout::Parameter* p = parameter(state, index, rt::rootsig::ParamType::Constants); |
| 846 | if (!p) return; | 949 | if (!p) return; |
| 847 | 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); |
| @@ -850,6 +953,10 @@ private: | |||
| 850 | } | 953 | } |
| 851 | 954 | ||
| 852 | 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; | ||
| 853 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); | 960 | const RootArgumentLayout::Parameter* p = parameter(state, index, type); |
| 854 | if (!p) return; | 961 | if (!p) return; |
| 855 | write(state, p->user_offset, std::uint32_t(va)); | 962 | write(state, p->user_offset, std::uint32_t(va)); |
| @@ -862,7 +969,6 @@ private: | |||
| 862 | bool invalid_ = false; | 969 | bool invalid_ = false; |
| 863 | UINT front_stencil_ref_ = 0; | 970 | UINT front_stencil_ref_ = 0; |
| 864 | UINT back_stencil_ref_ = 0; | 971 | UINT back_stencil_ref_ = 0; |
| 865 | bool stencil_ref_set_ = false; | ||
| 866 | RootState compute_; | 972 | RootState compute_; |
| 867 | RootState graphics_; | 973 | RootState graphics_; |
| 868 | std::uintptr_t compute_pipeline_ = 0; | 974 | std::uintptr_t compute_pipeline_ = 0; |
| @@ -875,7 +981,6 @@ private: | |||
| 875 | std::vector<leonida::TargetView> render_targets_; | 981 | std::vector<leonida::TargetView> render_targets_; |
| 876 | leonida::TargetView depth_{}; | 982 | leonida::TargetView depth_{}; |
| 877 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; | 983 | std::array<float, 4> blend_factor_{1, 1, 1, 1}; |
| 878 | bool blend_factor_set_ = false; | ||
| 879 | 984 | ||
| 880 | // 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 |
| 881 | // 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 |
| @@ -883,6 +988,31 @@ private: | |||
| 883 | // runs the submission stays the application's job, as in D3D12.) | 988 | // runs the submission stays the application's job, as in D3D12.) |
| 884 | std::vector<IUnknown*> held_; | 989 | std::vector<IUnknown*> held_; |
| 885 | 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 | |||
| 886 | void hold(IUnknown* object) { | 1016 | void hold(IUnknown* object) { |
| 887 | 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; |
| 888 | object->AddRef(); | 1018 | object->AddRef(); |
| @@ -914,10 +1044,26 @@ private: | |||
| 914 | render_targets_.clear(); | 1044 | render_targets_.clear(); |
| 915 | depth_ = {}; | 1045 | depth_ = {}; |
| 916 | blend_factor_ = {1, 1, 1, 1}; | 1046 | blend_factor_ = {1, 1, 1, 1}; |
| 917 | blend_factor_set_ = false; | ||
| 918 | set_pipeline(state); | 1047 | set_pipeline(state); |
| 919 | } | 1048 | } |
| 920 | 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 | |||
| 921 | // 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. |
| 922 | 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) { |
| 923 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); | 1069 | if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); |
| @@ -1072,22 +1218,36 @@ public: | |||
| 1072 | return true; | 1218 | return true; |
| 1073 | } | 1219 | } |
| 1074 | 1220 | ||
| 1075 | // Reserved resources are fully backed at creation (docs/d3d12_vulkan.md section 4.2), so tile mappings are not | 1221 | void STDMETHODCALLTYPE UpdateTileMappings(ID3D12Resource* resource, UINT region_count, |
| 1076 | // emulated: every tile keeps its own memory. What that loses is reported once each: two resources mapped to the | 1222 | const D3D12_TILED_RESOURCE_COORDINATE* starts, |
| 1077 | // same heap tiles do not alias, and a tile unmapped (or remapped) keeps its contents. | 1223 | const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count, |
| 1078 | void STDMETHODCALLTYPE UpdateTileMappings(ID3D12Resource*, UINT, const D3D12_TILED_RESOURCE_COORDINATE*, | 1224 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, |
| 1079 | const D3D12_TILE_REGION_SIZE*, ID3D12Heap*, UINT, | 1225 | const UINT* counts, D3D12_TILE_MAPPING_FLAGS mapping_flags) override { |
| 1080 | const D3D12_TILE_RANGE_FLAGS*, const UINT*, const UINT*, | 1226 | constexpr auto method = "ID3D12CommandQueue::UpdateTileMappings"; |
| 1081 | D3D12_TILE_MAPPING_FLAGS) override { | 1227 | if (!update_buffer_tiles(resource, region_count, starts, sizes, heap, range_count, flags, offsets, |
| 1082 | d3d12_trace::record("ID3D12CommandQueue::UpdateTileMappings", "ignored"); | 1228 | counts, mapping_flags)) { |
| 1083 | note_once("ID3D12CommandQueue::UpdateTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); | 1229 | d3d12_trace::record(method, "refused"); |
| 1230 | note_once("ID3D12CommandQueue::UpdateTileMappings: invalid mapping or buffer sparse residency unavailable"); | ||
| 1231 | } else { | ||
| 1232 | d3d12_trace::record(method, "ok"); | ||
| 1233 | } | ||
| 1084 | } | 1234 | } |
| 1085 | 1235 | ||
| 1086 | void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource*, const D3D12_TILED_RESOURCE_COORDINATE*, ID3D12Resource*, | 1236 | void STDMETHODCALLTYPE CopyTileMappings(ID3D12Resource* dst, const D3D12_TILED_RESOURCE_COORDINATE* dst_start, |
| 1087 | const D3D12_TILED_RESOURCE_COORDINATE*, const D3D12_TILE_REGION_SIZE*, | 1237 | ID3D12Resource* src, const D3D12_TILED_RESOURCE_COORDINATE* src_start, |
| 1088 | D3D12_TILE_MAPPING_FLAGS) override { | 1238 | const D3D12_TILE_REGION_SIZE* size, D3D12_TILE_MAPPING_FLAGS flags) override { |
| 1089 | d3d12_trace::record("ID3D12CommandQueue::CopyTileMappings", "ignored"); | 1239 | constexpr auto method = "ID3D12CommandQueue::CopyTileMappings"; |
| 1090 | note_once("ID3D12CommandQueue::CopyTileMappings ignored: reserved resources are fully backed, mappings are not emulated"); | 1240 | std::lock_guard lock(mutex_); |
| 1241 | leonida::BufferTileRegion from, to; | ||
| 1242 | if ((flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD) || !dst_start || !src_start || !size || | ||
| 1243 | !buffer_tile_region(src_start, size, buffer_tiles(src), from) || | ||
| 1244 | !buffer_tile_region(dst_start, size, buffer_tiles(dst), to) || | ||
| 1245 | !queue_.copy_tile_mappings(resource_id(dst), to.first, resource_id(src), from)) { | ||
| 1246 | d3d12_trace::record(method, "refused"); | ||
| 1247 | note_once("ID3D12CommandQueue::CopyTileMappings: invalid mapping or buffer sparse residency unavailable"); | ||
| 1248 | } else { | ||
| 1249 | d3d12_trace::record(method, "ok"); | ||
| 1250 | } | ||
| 1091 | } | 1251 | } |
| 1092 | 1252 | ||
| 1093 | HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { | 1253 | HRESULT STDMETHODCALLTYPE Signal(ID3D12Fence* fence, UINT64 value) override { |
| @@ -1243,6 +1403,60 @@ private: | |||
| 1243 | queue_.marker(kind, label.c_str()); | 1403 | queue_.marker(kind, label.c_str()); |
| 1244 | } | 1404 | } |
| 1245 | 1405 | ||
| 1406 | bool update_buffer_tiles(ID3D12Resource* resource, UINT region_count, const D3D12_TILED_RESOURCE_COORDINATE* starts, | ||
| 1407 | const D3D12_TILE_REGION_SIZE* sizes, ID3D12Heap* heap, UINT range_count, | ||
| 1408 | const D3D12_TILE_RANGE_FLAGS* flags, const UINT* offsets, const UINT* counts, | ||
| 1409 | D3D12_TILE_MAPPING_FLAGS mapping_flags) { | ||
| 1410 | if (!region_count || !range_count || (!starts && region_count != 1) || | ||
| 1411 | (mapping_flags & ~D3D12_TILE_MAPPING_FLAG_NO_HAZARD)) return false; | ||
| 1412 | const auto tiles = buffer_tiles(resource); | ||
| 1413 | std::vector<leonida::BufferTileRegion> regions(region_count); | ||
| 1414 | std::uint64_t total = 0; | ||
| 1415 | for (UINT i = 0; i < region_count; ++i) { | ||
| 1416 | if (!buffer_tile_region(starts ? starts + i : nullptr, sizes ? sizes + i : nullptr, tiles, regions[i])) | ||
| 1417 | return false; | ||
| 1418 | if (regions[i].count > UINT64_MAX - total) return false; | ||
| 1419 | total += regions[i].count; | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | std::vector<leonida::BufferTileRange> ranges; | ||
| 1423 | ranges.reserve(range_count); | ||
| 1424 | std::uint64_t remaining = total, heap_tiles_required = 0; | ||
| 1425 | for (UINT i = 0; i < range_count; ++i) { | ||
| 1426 | const auto count = counts ? counts[i] : (range_count == 1 ? total : 1); | ||
| 1427 | if (!count) continue; | ||
| 1428 | if (count > remaining) return false; | ||
| 1429 | remaining -= count; | ||
| 1430 | using leonida::TileRangeMode; | ||
| 1431 | const auto flag = flags ? flags[i] : D3D12_TILE_RANGE_FLAG_NONE; | ||
| 1432 | switch (flag) { | ||
| 1433 | case D3D12_TILE_RANGE_FLAG_NONE: | ||
| 1434 | case D3D12_TILE_RANGE_FLAG_REUSE_SINGLE_TILE: { | ||
| 1435 | if (!offsets) return false; | ||
| 1436 | const auto used = flag == D3D12_TILE_RANGE_FLAG_NONE ? count : 1; | ||
| 1437 | if (used > UINT64_MAX - offsets[i]) return false; | ||
| 1438 | heap_tiles_required = std::max(heap_tiles_required, offsets[i] + used); | ||
| 1439 | ranges.push_back({flag == D3D12_TILE_RANGE_FLAG_NONE ? TileRangeMode::sequential : TileRangeMode::reuse, | ||
| 1440 | offsets[i], count}); | ||
| 1441 | break; | ||
| 1442 | } | ||
| 1443 | case D3D12_TILE_RANGE_FLAG_NULL: | ||
| 1444 | ranges.push_back({TileRangeMode::unmap, 0, count}); | ||
| 1445 | break; | ||
| 1446 | case D3D12_TILE_RANGE_FLAG_SKIP: | ||
| 1447 | ranges.push_back({TileRangeMode::skip, 0, count}); | ||
| 1448 | break; | ||
| 1449 | default: | ||
| 1450 | return false; | ||
| 1451 | } | ||
| 1452 | } | ||
| 1453 | if (remaining || (heap_tiles_required && | ||
| 1454 | (!heap || heap_tiles_required > heap->GetDesc().SizeInBytes / leonida::tile_size_in_bytes))) return false; | ||
| 1455 | std::lock_guard lock(mutex_); | ||
| 1456 | return queue_.update_tiles(resource_id(resource), regions, ranges, | ||
| 1457 | [&] { return heap_tiles_required ? tile_heap(heap, context_) : nullptr; }); | ||
| 1458 | } | ||
| 1459 | |||
| 1246 | D3D12_COMMAND_QUEUE_DESC desc_; | 1460 | D3D12_COMMAND_QUEUE_DESC desc_; |
| 1247 | leonida::VulkanContext& context_; | 1461 | leonida::VulkanContext& context_; |
| 1248 | std::mutex mutex_; | 1462 | std::mutex mutex_; |
| @@ -1290,7 +1504,7 @@ HRESULT create_command_allocator(D3D12_COMMAND_LIST_TYPE type, REFIID riid, void | |||
| 1290 | } | 1504 | } |
| 1291 | 1505 | ||
| 1292 | 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, |
| 1293 | void** out, IUnknown* device) { | 1507 | void** out, IUnknown* device, ID3D12PipelineState* initial_state) { |
| 1294 | if (!out) return E_POINTER; | 1508 | if (!out) return E_POINTER; |
| 1295 | *out = nullptr; | 1509 | *out = nullptr; |
| 1296 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); | 1510 | CommandAllocator* command_allocator = CommandAllocator::from(allocator); |
| @@ -1303,7 +1517,12 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator | |||
| 1303 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does | 1517 | auto* object = new GraphicsCommandList(type); // created open, as D3D12 does |
| 1304 | object->record_into(command_allocator); | 1518 | object->record_into(command_allocator); |
| 1305 | object->attach_device(device); | 1519 | object->attach_device(device); |
| 1306 | 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; | ||
| 1307 | } | 1526 | } |
| 1308 | 1527 | ||
| 1309 | 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, |
| @@ -1384,4 +1603,12 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& | |||
| 1384 | return true; | 1603 | return true; |
| 1385 | } | 1604 | } |
| 1386 | 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 | |||
| 1387 | } // namespace rt::d3d12 | 1614 | } // namespace rt::d3d12 |