upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/d3d12_com.cpp
diff options
context:
space:
mode:
authorKrashgamer <64923888+Krashgamer@users.noreply.github.com>2026-11-23 19:35:24 +0100
committerKrashgamer <64923888+Krashgamer@users.noreply.github.com>2026-11-23 19:35:24 +0100
commit39b757a6d283fedd32715f84107356b2250d7db9 (patch)
tree5b408821e7262eadc8afe3c45ba59cfe70ec2f60 /runtime/shims/d3d12_com.cpp
parent978d525d99413cce232efde07efbf392d1e782a5 (diff)
parent6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff)
Merge main into runtime/emulate-zen2-insns (keep both the cpu_emulation and xg_xs test targets)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/d3d12_com.cpp')
-rw-r--r--runtime/shims/d3d12_com.cpp251
1 files changed, 242 insertions, 9 deletions
diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp
index 987dd2d64..f25168a2a 100644
--- a/runtime/shims/d3d12_com.cpp
+++ b/runtime/shims/d3d12_com.cpp
@@ -202,12 +202,15 @@ public:
202 const std::vector<IUnknown*>& pinned() const { return held_; } 202 const std::vector<IUnknown*>& pinned() const { return held_; }
203 UINT front_stencil_ref() const { return front_stencil_ref_; } 203 UINT front_stencil_ref() const { return front_stencil_ref_; }
204 UINT back_stencil_ref() const { return back_stencil_ref_; } 204 UINT back_stencil_ref() const { return back_stencil_ref_; }
205 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; }
206 D3D12_GPU_VIRTUAL_ADDRESS sampler_base() const { return sampler_base_; }
205 207
206 D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override { 208 D3D12_COMMAND_LIST_TYPE STDMETHODCALLTYPE GetType() override {
207 d3d12_trace::record("ID3D12CommandList::GetType"); 209 d3d12_trace::record("ID3D12CommandList::GetType");
208 return type_; 210 return type_;
209 } 211 }
210 D3D12_COMMAND_LIST_TYPE type() const { return type_; } 212 D3D12_COMMAND_LIST_TYPE type() const { return type_; }
213 bool invalid() const { return invalid_; }
211 214
212 // E_INVALIDARG when a call during recording was invalid, or a query was begun and not ended, as D3D12's Close 215 // E_INVALIDARG when a call during recording was invalid, or a query was begun and not ended, as D3D12's Close
213 // reports it. 216 // reports it.
@@ -248,6 +251,7 @@ public:
248 251
249 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override { 252 void STDMETHODCALLTYPE ExecuteBundle(ID3D12GraphicsCommandList* command_list) override {
250 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle"); 253 d3d12_trace::record("ID3D12GraphicsCommandList::ExecuteBundle");
254 if (list_.closed()) return;
251 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false); 255 if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
252 GraphicsCommandList* bundle = from(command_list); 256 GraphicsCommandList* bundle = from(command_list);
253 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ || 257 if (!bundle || bundle->type_ != D3D12_COMMAND_LIST_TYPE_BUNDLE || bundle->invalid_ ||
@@ -279,13 +283,47 @@ public:
279 283
280 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override { 284 void STDMETHODCALLTYPE SetPipelineState(ID3D12PipelineState* state) override {
281 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState"); 285 d3d12_trace::record("ID3D12GraphicsCommandList::SetPipelineState");
286 if (list_.closed()) return;
282 if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state); 287 if (defer([=](GraphicsCommandList& l) { l.set_pipeline(state); })) return hold(state);
283 set_pipeline(state); 288 set_pipeline(state);
284 } 289 }
285 290
286 // The bound heaps matter to direct heap indexing, which is still open (section 3.4); tables carry their VAs. 291 // Validates shader-visible descriptor heaps and tracks bound heap identities/bases (docs/d3d12_vulkan.md §3.2/§3.4).
287 void STDMETHODCALLTYPE SetDescriptorHeaps(UINT, ID3D12DescriptorHeap* const*) override { 292 void STDMETHODCALLTYPE SetDescriptorHeaps(UINT num, ID3D12DescriptorHeap* const* heaps) override {
288 d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps", "ignored"); 293 d3d12_trace::record("ID3D12GraphicsCommandList::SetDescriptorHeaps");
294 if (type_ == D3D12_COMMAND_LIST_TYPE_COPY) return check(false);
295 if (num > 2 || (num && !heaps)) return check(false);
296
297 ID3D12DescriptorHeap* cbv = nullptr;
298 ID3D12DescriptorHeap* smp = nullptr;
299 D3D12_GPU_VIRTUAL_ADDRESS cbv_base = 0;
300 D3D12_GPU_VIRTUAL_ADDRESS smp_base = 0;
301 IUnknown* const owner = this->attached_device();
302 for (UINT i = 0; i < num; ++i) {
303 ID3D12DescriptorHeap* raw = heaps[i];
304 if (!raw) return check(false);
305 if (ID3D12DescriptorHeap* real = d3d12_xs::heap_proxy_real_heap(raw)) raw = real;
306 D3D12_DESCRIPTOR_HEAP_DESC desc{};
307 D3D12_GPU_VIRTUAL_ADDRESS base = 0;
308 IUnknown* const heap_owner = descriptor_heap_info(raw, desc, base);
309 if (!heap_owner || (owner && heap_owner != owner)) return check(false);
310 if (!(desc.Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) return check(false);
311 if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) {
312 if (cbv) return check(false);
313 cbv = raw;
314 cbv_base = base;
315 } else if (desc.Type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER) {
316 if (smp) return check(false);
317 smp = raw;
318 smp_base = base;
319 } else {
320 return check(false);
321 }
322 }
323 hold(cbv);
324 hold(smp);
325 if (defer([=](GraphicsCommandList& l) { l.verify_bundle_descriptor_heaps(cbv, smp); })) return;
326 set_descriptor_heaps(cbv, cbv_base, smp, smp_base);
289 } 327 }
290 328
291 void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override { 329 void STDMETHODCALLTYPE SetComputeRoot32BitConstant(UINT index, UINT value, UINT offset) override {
@@ -332,14 +370,18 @@ public:
332 record_dispatch(x, y, z); 370 record_dispatch(x, y, z);
333 } 371 }
334 372
335 // ClearState returns every piece of list state to its default, as Reset does (stencil references included). 373 // ClearState returns every piece of list state to its default, as Reset does (stencil references and predication
374 // included).
336 void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override { 375 void STDMETHODCALLTYPE ClearState(ID3D12PipelineState* state) override {
376 if (list_.closed()) return;
337 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 377 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
378 if (!list_.closed()) check(list_.set_predication({0, 0, 0}));
338 reset_state(state); 379 reset_state(state);
339 } 380 }
340 381
341 // Graphics state. Draws record a copy of it (command_list.h, Draw). 382 // Graphics state. Draws record a copy of it (command_list.h, Draw).
342 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override { 383 void STDMETHODCALLTYPE IASetPrimitiveTopology(D3D12_PRIMITIVE_TOPOLOGY topology) override {
384 if (list_.closed()) return;
343 if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return; 385 if (defer([=](GraphicsCommandList& l) { l.topology_ = u(topology); })) return;
344 topology_ = u(topology); 386 topology_ = u(topology);
345 } 387 }
@@ -358,6 +400,7 @@ public:
358 } 400 }
359 401
360 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override { 402 void STDMETHODCALLTYPE IASetIndexBuffer(const D3D12_INDEX_BUFFER_VIEW* view) override {
403 if (list_.closed()) return;
361 const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt; 404 const std::optional<D3D12_INDEX_BUFFER_VIEW> copy = view ? std::optional(*view) : std::nullopt;
362 if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return; 405 if (defer([=](GraphicsCommandList& l) { l.set_index_buffer(copy ? &*copy : nullptr); })) return;
363 set_index_buffer(view); 406 set_index_buffer(view);
@@ -405,6 +448,7 @@ public:
405 } 448 }
406 449
407 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override { 450 void STDMETHODCALLTYPE OMSetBlendFactor(const FLOAT factor[4]) override {
451 if (list_.closed()) return;
408 const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]} 452 const std::array<float, 4> value = factor ? std::array<float, 4>{factor[0], factor[1], factor[2], factor[3]}
409 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 } 453 : std::array<float, 4>{1, 1, 1, 1}; // null means { 1, 1, 1, 1 }
410 if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return; 454 if (defer([=](GraphicsCommandList& l) { l.blend_factor_ = value; })) return;
@@ -412,6 +456,7 @@ public:
412 } 456 }
413 457
414 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override { 458 void STDMETHODCALLTYPE OMSetStencilRef(UINT reference) override {
459 if (list_.closed()) return;
415 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return; 460 if (defer([=](GraphicsCommandList& l) { l.front_stencil_ref_ = l.back_stencil_ref_ = reference; })) return;
416 front_stencil_ref_ = back_stencil_ref_ = reference; 461 front_stencil_ref_ = back_stencil_ref_ = reference;
417 } 462 }
@@ -627,10 +672,24 @@ public:
627 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource, region})); 672 check(list_.resolve_subresource({resource_id(dst), dst_subresource, resource_id(src), src_subresource, region}));
628 } 673 }
629 674
630 // A null buffer turns predication off (docs/d3d12_vulkan.md section 4.7). Bundles may not set it. 675 // A null buffer turns predication off (docs/d3d12_vulkan.md section 4.7). Bundles and copy lists may not set it.
676 // Recording-time checks match live Windows 11 WARP and RTX 5090 D3D12: null buffer requires offset 0 and
677 // EQUAL_ZERO; a non-null resource must be a non-READBACK buffer with an 8-byte aligned in-bounds range.
631 void STDMETHODCALLTYPE SetPredication(ID3D12Resource* buffer, UINT64 offset, D3D12_PREDICATION_OP op) override { 678 void STDMETHODCALLTYPE SetPredication(ID3D12Resource* buffer, UINT64 offset, D3D12_PREDICATION_OP op) override {
632 d3d12_trace::record("ID3D12GraphicsCommandList::SetPredication"); 679 d3d12_trace::record("ID3D12GraphicsCommandList::SetPredication");
633 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || type_ == D3D12_COMMAND_LIST_TYPE_COPY) return check(false); 680 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || type_ == D3D12_COMMAND_LIST_TYPE_COPY) return check(false);
681 if (!buffer) {
682 if (offset != 0 || op != D3D12_PREDICATION_OP_EQUAL_ZERO) return check(false);
683 return check(list_.set_predication({0, 0, 0}));
684 }
685 const auto desc = buffer->GetDesc();
686 if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER) return check(false);
687 if (offset % leonida::predication_alignment != 0 || offset > desc.Width ||
688 desc.Width - offset < leonida::predication_alignment)
689 return check(false);
690 D3D12_HEAP_PROPERTIES heap{};
691 if (SUCCEEDED(buffer->GetHeapProperties(&heap, nullptr)) && heap.Type == D3D12_HEAP_TYPE_READBACK)
692 return check(false);
634 check(list_.set_predication({resource_id(buffer), offset, u(op)})); 693 check(list_.set_predication({resource_id(buffer), offset, u(op)}));
635 } 694 }
636 695
@@ -764,6 +823,29 @@ public:
764 UINT stride = 0; 823 UINT stride = 0;
765 if (!command_signature_info(signature, kind, stride) || !argument_buffer) 824 if (!command_signature_info(signature, kind, stride) || !argument_buffer)
766 return check(false); 825 return check(false);
826 const auto arg_desc = argument_buffer->GetDesc();
827 if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0)
828 return check(false);
829 if (count_buffer) {
830 const auto count_desc = count_buffer->GetDesc();
831 if (count_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || count_buffer_offset % 4 != 0 ||
832 count_desc.Width < sizeof(UINT) || count_buffer_offset > count_desc.Width - sizeof(UINT))
833 return check(false);
834 } else if (count_buffer_offset != 0) {
835 return check(false);
836 }
837 if (max_command_count) {
838 const UINT64 record_size =
839 kind == leonida::IndirectKind::draw ? sizeof(D3D12_DRAW_ARGUMENTS)
840 : kind == leonida::IndirectKind::draw_indexed ? sizeof(D3D12_DRAW_INDEXED_ARGUMENTS)
841 : kind == leonida::IndirectKind::dispatch ? sizeof(D3D12_DISPATCH_ARGUMENTS)
842 : 0;
843 if (!record_size || stride < record_size || stride % 4 != 0)
844 return check(false);
845 const UINT64 required = UINT64(max_command_count - 1) * stride + record_size;
846 if (argument_buffer_offset > arg_desc.Width || arg_desc.Width - argument_buffer_offset < required)
847 return check(false);
848 }
767 leonida::ExecuteIndirect cmd{}; 849 leonida::ExecuteIndirect cmd{};
768 cmd.kind = kind; 850 cmd.kind = kind;
769 cmd.stride = stride; 851 cmd.stride = stride;
@@ -784,7 +866,17 @@ public:
784 const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC* desc, UINT postbuild_count, 866 const D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC* desc, UINT postbuild_count,
785 const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC* postbuild) override { 867 const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC* postbuild) override {
786 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || !desc) return check(false); 868 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || !desc) return check(false);
869 if (postbuild_count && !postbuild) return check(false);
870 for (UINT i = 0; i < postbuild_count; ++i) {
871 if (!valid_postbuild_info_type(postbuild[i].InfoType)) return check(false);
872 }
787 const auto& in = desc->Inputs; 873 const auto& in = desc->Inputs;
874 if (in.Type != D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL &&
875 in.Type != D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL)
876 return check(false);
877 if (in.DescsLayout != D3D12_ELEMENTS_LAYOUT_ARRAY &&
878 in.DescsLayout != D3D12_ELEMENTS_LAYOUT_ARRAY_OF_POINTERS)
879 return check(false);
788 leonida::BuildAccelerationStructure cmd{}; 880 leonida::BuildAccelerationStructure cmd{};
789 cmd.is_tlas = (in.Type == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL); 881 cmd.is_tlas = (in.Type == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL);
790 cmd.update = (in.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE) != 0; 882 cmd.update = (in.Flags & D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PERFORM_UPDATE) != 0;
@@ -859,6 +951,7 @@ public:
859 const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC* desc, UINT count, 951 const D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_DESC* desc, UINT count,
860 const D3D12_GPU_VIRTUAL_ADDRESS* sources) override { 952 const D3D12_GPU_VIRTUAL_ADDRESS* sources) override {
861 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || !desc || !count || !sources) return check(false); 953 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || !desc || !count || !sources) return check(false);
954 if (!valid_postbuild_info_type(desc->InfoType)) return check(false);
862 leonida::AccelerationStructurePostbuild info{}; 955 leonida::AccelerationStructurePostbuild info{};
863 info.dest_va = desc->DestBuffer; 956 info.dest_va = desc->DestBuffer;
864 info.type = static_cast<std::uint32_t>(desc->InfoType); 957 info.type = static_cast<std::uint32_t>(desc->InfoType);
@@ -870,12 +963,37 @@ public:
870 D3D12_GPU_VIRTUAL_ADDRESS source, 963 D3D12_GPU_VIRTUAL_ADDRESS source,
871 D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE mode) override { 964 D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE mode) override {
872 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); 965 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
966 if (!valid_copy_mode(mode)) return check(false);
873 check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)})); 967 check(list_.copy_acceleration_structure({dest, source, static_cast<std::uint32_t>(mode)}));
874 } 968 }
875 969
876 static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); } 970 static GraphicsCommandList* from(IUnknown* object) { return Child::from<GraphicsCommandList>(object); }
877 971
878private: 972private:
973 static bool valid_postbuild_info_type(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TYPE type) {
974 switch (type) {
975 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_COMPACTED_SIZE:
976 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_TOOLS_VISUALIZATION:
977 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_SERIALIZATION:
978 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_POSTBUILD_INFO_CURRENT_SIZE:
979 return true;
980 default:
981 return false;
982 }
983 }
984
985 static bool valid_copy_mode(D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE mode) {
986 switch (mode) {
987 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE:
988 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT:
989 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_VISUALIZATION_DECODE_FOR_TOOLS:
990 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_SERIALIZE:
991 case D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_DESERIALIZE:
992 return true;
993 default:
994 return false;
995 }
996 }
879 template<class E> 997 template<class E>
880 static std::uint32_t u(E value) { 998 static std::uint32_t u(E value) {
881 return static_cast<std::uint32_t>(value); 999 return static_cast<std::uint32_t>(value);
@@ -908,14 +1026,67 @@ private:
908 allocator_ = nullptr; 1026 allocator_ = nullptr;
909 } 1027 }
910 1028
1029 static UINT subresource_count(const D3D12_RESOURCE_DESC& desc) {
1030 if (desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) return 1;
1031 const UINT mips = desc.MipLevels ? desc.MipLevels : 1u;
1032 const UINT layers = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D
1033 ? 1u
1034 : (desc.DepthOrArraySize ? desc.DepthOrArraySize : 1u);
1035 UINT planes = 1;
1036 switch (desc.Format) {
1037 case DXGI_FORMAT_D24_UNORM_S8_UINT:
1038 case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
1039 case DXGI_FORMAT_R24G8_TYPELESS:
1040 case DXGI_FORMAT_R32G8X24_TYPELESS:
1041 case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
1042 case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
1043 case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
1044 case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
1045 case DXGI_FORMAT_NV12:
1046 case DXGI_FORMAT_P010:
1047 case DXGI_FORMAT_P016:
1048 planes = 2;
1049 break;
1050 default:
1051 break;
1052 }
1053 return mips * layers * planes;
1054 }
1055
911 void atomic_copy(std::uint32_t bytes, ID3D12Resource* dst, UINT64 dst_offset, ID3D12Resource* src, 1056 void atomic_copy(std::uint32_t bytes, ID3D12Resource* dst, UINT64 dst_offset, ID3D12Resource* src,
912 UINT64 src_offset, UINT count, ID3D12Resource* const* resources, 1057 UINT64 src_offset, UINT count, ID3D12Resource* const* resources,
913 const D3D12_SUBRESOURCE_RANGE_UINT64* ranges) { 1058 const D3D12_SUBRESOURCE_RANGE_UINT64* ranges) {
914 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || (count && (!resources || !ranges))) return check(false); 1059 if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE || !dst || !src || (count && (!resources || !ranges)))
1060 return check(false);
1061 const auto dst_desc = dst->GetDesc();
1062 const auto src_desc = src->GetDesc();
1063 if (dst_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
1064 src_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER)
1065 return check(false);
1066 if (dst_offset % bytes != 0 || src_offset % bytes != 0 || dst_offset > dst_desc.Width ||
1067 dst_desc.Width - dst_offset < bytes || src_offset > src_desc.Width || src_desc.Width - src_offset < bytes)
1068 return check(false);
915 leonida::AtomicCopyBuffer copy{resource_id(dst), dst_offset, resource_id(src), src_offset, bytes, {}}; 1069 leonida::AtomicCopyBuffer copy{resource_id(dst), dst_offset, resource_id(src), src_offset, bytes, {}};
916 for (UINT i = 0; i < count; ++i) 1070 copy.dependencies.reserve(count);
917 copy.dependencies.push_back( 1071 for (UINT i = 0; i < count; ++i) {
918 {resource_id(resources[i]), ranges[i].Subresource, ranges[i].Range.Begin, ranges[i].Range.End}); 1072 ID3D12Resource* dep = resources[i];
1073 if (!dep) return check(false);
1074 const auto dep_desc = dep->GetDesc();
1075 const auto& r = ranges[i];
1076 UINT64 begin = r.Range.Begin;
1077 UINT64 end = r.Range.End;
1078 if (dep_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) {
1079 if (r.Subresource != 0) return check(false);
1080 if (begin >= end || (end != UINT64_MAX && end > dep_desc.Width))
1081 return check(false);
1082 } else {
1083 const UINT total_subresources = subresource_count(dep_desc);
1084 if (r.Subresource != D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES && r.Subresource >= total_subresources)
1085 return check(false);
1086 if (begin != 0 || end != UINT64_MAX) return check(false);
1087 }
1088 copy.dependencies.push_back({resource_id(dep), r.Subresource, begin, end});
1089 }
919 check(list_.atomic_copy(copy)); 1090 check(list_.atomic_copy(copy));
920 } 1091 }
921 1092
@@ -1082,6 +1253,10 @@ private:
1082 RootState graphics_; 1253 RootState graphics_;
1083 std::uintptr_t compute_pipeline_ = 0; 1254 std::uintptr_t compute_pipeline_ = 0;
1084 std::uintptr_t graphics_pipeline_ = 0; 1255 std::uintptr_t graphics_pipeline_ = 0;
1256 ID3D12DescriptorHeap* cbv_srv_uav_heap_ = nullptr;
1257 ID3D12DescriptorHeap* sampler_heap_ = nullptr;
1258 D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base_ = 0;
1259 D3D12_GPU_VIRTUAL_ADDRESS sampler_base_ = 0;
1085 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED 1260 std::uint32_t topology_ = 0; // D3D_PRIMITIVE_TOPOLOGY_UNDEFINED
1086 std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{}; 1261 std::array<leonida::VertexBufferView, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> vertex_buffers_{};
1087 leonida::IndexBufferView index_{}; 1262 leonida::IndexBufferView index_{};
@@ -1146,10 +1321,44 @@ private:
1146 stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)"); 1321 stub("ID3D12GraphicsCommandList::SetPipelineState(pipeline from another device)");
1147 } 1322 }
1148 1323
1324 void set_descriptor_heaps(ID3D12DescriptorHeap* cbv, D3D12_GPU_VIRTUAL_ADDRESS cbv_base,
1325 ID3D12DescriptorHeap* smp, D3D12_GPU_VIRTUAL_ADDRESS smp_base) {
1326 cbv_srv_uav_heap_ = cbv;
1327 cbv_srv_uav_base_ = cbv_base;
1328 sampler_heap_ = smp;
1329 sampler_base_ = smp_base;
1330 }
1331
1332 // D3D12 bundle rule: every non-null heap bound by a bundle's SetDescriptorHeaps must match the parent list's
1333 // currently bound heap of that type, without clearing any omitted heap slot on the parent.
1334 void verify_bundle_descriptor_heaps(ID3D12DescriptorHeap* cbv, ID3D12DescriptorHeap* smp) {
1335 if ((cbv && cbv != cbv_srv_uav_heap_) || (smp && smp != sampler_heap_)) check(false);
1336 }
1337
1338 void note_direct_heap_indexing(const RootArgumentLayout* layout) const {
1339 if (!layout) return;
1340 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED) {
1341 if (!cbv_srv_uav_heap_)
1342 note_once("draw/dispatch uses CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED with no CBV/SRV/UAV heap bound");
1343 else
1344 note_once("CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)");
1345 }
1346 if (layout->flags & D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED) {
1347 if (!sampler_heap_)
1348 note_once("draw/dispatch uses SAMPLER_HEAP_DIRECTLY_INDEXED with no sampler heap bound");
1349 else
1350 note_once("SAMPLER_HEAP_DIRECTLY_INDEXED heap base tracked on list (SGPR slot open, section 3.4)");
1351 }
1352 }
1353
1149 void reset_state(ID3D12PipelineState* state) { 1354 void reset_state(ID3D12PipelineState* state) {
1150 front_stencil_ref_ = back_stencil_ref_ = 0; 1355 front_stencil_ref_ = back_stencil_ref_ = 0;
1151 compute_ = {}; 1356 compute_ = {};
1152 graphics_ = {}; 1357 graphics_ = {};
1358 cbv_srv_uav_heap_ = nullptr;
1359 sampler_heap_ = nullptr;
1360 cbv_srv_uav_base_ = 0;
1361 sampler_base_ = 0;
1153 topology_ = 0; 1362 topology_ = 0;
1154 vertex_buffers_ = {}; 1363 vertex_buffers_ = {};
1155 index_ = {}; 1364 index_ = {};
@@ -1178,6 +1387,7 @@ private:
1178 1387
1179 void record_dispatch(UINT x, UINT y, UINT z) { 1388 void record_dispatch(UINT x, UINT y, UINT z) {
1180 if (!compute_pipeline_ || !compute_.layout) return check(false); 1389 if (!compute_pipeline_ || !compute_.layout) return check(false);
1390 note_direct_heap_indexing(compute_.layout);
1181 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z})); 1391 check(list_.dispatch({compute_pipeline_, compute_.data, x, y, z}));
1182 } 1392 }
1183 1393
@@ -1197,6 +1407,7 @@ private:
1197 // A draw needs a graphics pipeline, a graphics root signature and a topology. 1407 // A draw needs a graphics pipeline, a graphics root signature and a topology.
1198 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) { 1408 void record_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance) {
1199 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false); 1409 if (!graphics_pipeline_ || !graphics_.layout || !topology_) return check(false);
1410 note_direct_heap_indexing(graphics_.layout);
1200 // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing, 1411 // No viewport (never set, or unbound with RSSetViewports(0)) or an empty one: D3D12 rasterizes nothing,
1201 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded. 1412 // and Vulkan forbids a 0x0 viewport, so the draw is not recorded.
1202 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return; 1413 if (!(viewport_.width > 0) || !(viewport_.height > 0)) return;
@@ -1379,6 +1590,12 @@ public:
1379 void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override { 1590 void STDMETHODCALLTYPE ExecuteCommandLists(UINT count, ID3D12CommandList* const* lists) override {
1380 constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists"; 1591 constexpr auto method = "ID3D12CommandQueue::ExecuteCommandLists";
1381 std::lock_guard lock(mutex_); 1592 std::lock_guard lock(mutex_);
1593 if (count > 0 && !lists) {
1594 note_once("ExecuteCommandLists: null command list array with non-zero count");
1595 last_result_ = leonida::ReplayResult::unsupported;
1596 d3d12_trace::record(method, "failed");
1597 return;
1598 }
1382 std::vector<const leonida::CommandList*> recorded; 1599 std::vector<const leonida::CommandList*> recorded;
1383 std::vector<const GraphicsCommandList*> graphics; 1600 std::vector<const GraphicsCommandList*> graphics;
1384 for (UINT i = 0; i < count; ++i) { 1601 for (UINT i = 0; i < count; ++i) {
@@ -1399,6 +1616,13 @@ public:
1399 d3d12_trace::record(method, "failed"); 1616 d3d12_trace::record(method, "failed");
1400 return; 1617 return;
1401 } 1618 }
1619 // A list whose recording failed (Close returned E_INVALIDARG) must not replay its earlier commands.
1620 if (list->invalid()) {
1621 std::fprintf(stderr, "[rt] d3d12: ExecuteCommandLists: list %u is invalid; refusing batch\n", i);
1622 last_result_ = leonida::ReplayResult::unsupported;
1623 d3d12_trace::record(method, "failed");
1624 return;
1625 }
1402 if (!timestamps_fit(*list)) { 1626 if (!timestamps_fit(*list)) {
1403 last_result_ = leonida::ReplayResult::invalid_query; 1627 last_result_ = leonida::ReplayResult::invalid_query;
1404 d3d12_trace::record(method, "failed"); 1628 d3d12_trace::record(method, "failed");
@@ -1833,4 +2057,13 @@ bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector<leonida::Com
1833 return true; 2057 return true;
1834} 2058}
1835 2059
2060bool bound_descriptor_heap_bases(ID3D12GraphicsCommandList* list, D3D12_GPU_VIRTUAL_ADDRESS& cbv_srv_uav,
2061 D3D12_GPU_VIRTUAL_ADDRESS& sampler) {
2062 const GraphicsCommandList* l = GraphicsCommandList::from(list);
2063 if (!l) return false;
2064 cbv_srv_uav = l->cbv_srv_uav_base();
2065 sampler = l->sampler_base();
2066 return true;
2067}
2068
1836} // namespace rt::d3d12 2069} // namespace rt::d3d12