diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 883cce32a1..48233ebe5e 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -144,15 +144,25 @@ class CommandSignature final // {9a4e1c2b-55d8-4f7a-b012-3c8e7a1f6d90} static constexpr GUID kTag{0x9a4e1c2b, 0x55d8, 0x4f7a, {0xb0, 0x12, 0x3c, 0x8e, 0x7a, 0x1f, 0x6d, 0x90}}; - CommandSignature(leonida::IndirectKind kind, UINT byte_stride) : kind_(kind), stride_(byte_stride) {} + // argument_types are the D3D12_INDIRECT_ARGUMENT_TYPE of each argument, in order. root_signature is + // required when any argument is a root constant or descriptor view; null otherwise. + CommandSignature(leonida::IndirectKind kind, UINT byte_stride, + std::vector argument_types, + ID3D12RootSignature* root_signature) + : kind_(kind), stride_(byte_stride), + argument_types_(std::move(argument_types)), root_signature_(root_signature) {} leonida::IndirectKind kind() const { return kind_; } UINT stride() const { return stride_; } + const std::vector& argument_types() const { return argument_types_; } + ID3D12RootSignature* root_signature() const { return root_signature_; } static CommandSignature* from(IUnknown* object) { return Child::from(object); } private: leonida::IndirectKind kind_; UINT stride_; + std::vector argument_types_; + ID3D12RootSignature* root_signature_ = nullptr; }; class CommandAllocator final @@ -957,7 +967,8 @@ class GraphicsCommandList final if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); leonida::IndirectKind kind = leonida::IndirectKind::draw; UINT stride = 0; - if (!command_signature_info(signature, kind, stride) || !argument_buffer) + std::vector argument_types; + if (!command_signature_info(signature, kind, stride, &argument_types) || !argument_buffer) return check(false); const auto arg_desc = argument_buffer->GetDesc(); if (arg_desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || argument_buffer_offset % 4 != 0) @@ -1003,6 +1014,8 @@ class GraphicsCommandList final leonida::ExecuteIndirect cmd{}; cmd.kind = kind; cmd.stride = stride; + cmd.argument_types.reserve(argument_types.size()); + for (auto t : argument_types) cmd.argument_types.push_back(std::uint32_t(t)); cmd.max_count = max_command_count; cmd.argument_buffer = resource_id(argument_buffer); cmd.argument_offset = argument_buffer_offset; @@ -2625,20 +2638,27 @@ bool queue_uses_calibrated_timestamps(ID3D12CommandQueue* queue) { return q && q->uses_calibrated_timestamps(); } -HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, REFIID riid, void** out, +HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, + std::span argument_types, + ID3D12RootSignature* root_signature, REFIID riid, void** out, IUnknown* device) { if (!out) return E_POINTER; *out = nullptr; - auto* sig = new CommandSignature(kind, byte_stride); + std::vector types(argument_types.begin(), argument_types.end()); + auto* sig = new CommandSignature(kind, byte_stride, std::move(types), root_signature); sig->attach_device(device); return hand_out(sig, riid, out); } -bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride) { +bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride, + std::vector* argument_types, + ID3D12RootSignature** root_signature) { const CommandSignature* s = CommandSignature::from(signature); if (!s) return false; kind = s->kind(); byte_stride = s->stride(); + if (argument_types) *argument_types = s->argument_types(); + if (root_signature) *root_signature = s->root_signature(); return true; } diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 928d91a9a4..de271c6917 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -60,9 +60,13 @@ HRESULT create_command_list(D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator void** out, IUnknown* device = nullptr, ID3D12PipelineState* initial_state = nullptr); HRESULT create_fence(leonida::VulkanContext& context, UINT64 initial_value, D3D12_FENCE_FLAGS flags, REFIID riid, void** out, IUnknown* device = nullptr); -HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, REFIID riid, void** out, +HRESULT create_command_signature(leonida::IndirectKind kind, UINT byte_stride, + std::span argument_types, + ID3D12RootSignature* root_signature, REFIID riid, void** out, IUnknown* device = nullptr); -bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride); +bool command_signature_info(ID3D12CommandSignature* signature, leonida::IndirectKind& kind, UINT& byte_stride, + std::vector* argument_types = nullptr, + ID3D12RootSignature** root_signature = nullptr); // ID3D12Device1::SetEventOnMultipleFenceCompletion on fences from create_fence: sets `event` once every fence // (D3D12_MULTIPLE_FENCE_WAIT_FLAG_ALL) or any fence (_ANY) has reached its value; a null event blocks until then. diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 5dd6b42e47..f6f79c7c75 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -11734,6 +11734,12 @@ class Device final } if (desc->ByteStride % 4 || desc->ByteStride < size) return E_INVALIDARG; if (!out) return E_UNEXPECTED; // the host's answer for a valid description and no output + // Collect argument types so the signature can replay vertex/index buffer views, root constants + // and descriptor views before the indirect draw. + std::vector argument_types; + argument_types.reserve(desc->NumArgumentDescs); + for (UINT i = 0; i < desc->NumArgumentDescs; ++i) + argument_types.push_back(desc->pArgumentDescs[i].Type); leonida::IndirectKind kind; switch (desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type) { case D3D12_INDIRECT_ARGUMENT_TYPE_DRAW: kind = leonida::IndirectKind::draw; break; @@ -11746,7 +11752,8 @@ class Device final break; default: return E_NOTIMPL; } - return create_command_signature(kind, desc->ByteStride, riid, out, identity()); + return create_command_signature(kind, desc->ByteStride, argument_types, root_signature, + riid, out, identity()); } void STDMETHODCALLTYPE GetRaytracingAccelerationStructurePrebuildInfo( diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 010abbbc01..be374aff9f 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h @@ -612,6 +612,10 @@ struct ExecuteIndirect { std::uint64_t argument_offset = 0; ResourceId count_buffer = 0; // 0: fixed max_count; non-zero: vkCmdDraw*IndirectCount std::uint64_t count_offset = 0; + // The D3D12_INDIRECT_ARGUMENT_TYPE of each argument, in order, as declared by the command signature. + // Used by replay to apply vertex/index buffer views, root constants and descriptor views before + // the indirect draw or dispatch. + std::vector argument_types; // kind == dispatch: the compute pipeline and root arguments in effect, as Dispatch records them, so the dispatches // run that pipeline with those arguments whatever the command buffer had bound. A pipeline of 0 leaves the binding // as it is. diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 6d1d60f033..cd80bf94c3 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -4740,6 +4740,64 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ checkpoints->record(command_buffer, i, CheckpointKind::draw); } const VkDeviceSize arguments = p.binding.buffer_offset + ind->argument_offset; + // Apply state arguments from the command signature before the indirect draw. D3D12 lets a + // signature carry VERTEX_BUFFER_VIEW and INDEX_BUFFER_VIEW arguments whose values live in the + // argument buffer; replay reads them and binds the corresponding buffers so the draw uses the + // right state. Argument offsets are walked in signature order; only host-readable argument + // buffers can be read here (DEFAULT/READBACK heaps are skipped with a log). + // D3D12_INDIRECT_ARGUMENT_TYPE values: DRAW=0, DRAW_INDEXED=1, DISPATCH=2, DISPATCH_MESH=4, + // VERTEX_BUFFER_VIEW=5, INDEX_BUFFER_VIEW=6, CONSTANT=7, CBV/SRV/UAV=8-10. + std::uint64_t arg_cursor = 0; + for (std::uint32_t arg_type : ind->argument_types) { + switch (arg_type) { + case 5: { // VERTEX_BUFFER_VIEW + // D3D12_VERTEX_BUFFER_VIEW: BufferLocation(u64), SizeInBytes(u32), StrideInBytes(u32) + const std::uint64_t view_va = p.binding.va + ind->argument_offset + arg_cursor; + const std::uint8_t* host = nullptr; + if (vk.host_at(view_va, sizeof(std::uint64_t) + 2 * sizeof(std::uint32_t), host) == HostReadStatus::ok) { + const std::uint64_t buffer_va = *reinterpret_cast(host); + std::uint64_t size = *reinterpret_cast(host + sizeof(std::uint64_t)); + const std::uint32_t stride = *reinterpret_cast(host + sizeof(std::uint64_t) + sizeof(std::uint32_t)); + VkBuffer vb = VK_NULL_HANDLE; + VkDeviceSize vb_offset = 0; + if (vk.buffer_at(buffer_va, size, vb, vb_offset)) { + const VkDeviceSize vb_size = size; + const VkDeviceSize vk_stride = stride; + vk.bind_vertex_buffers2(command_buffer, 0, 1, &vb, &vb_offset, &vb_size, &vk_stride); + } + } + arg_cursor += 16; // sizeof(D3D12_VERTEX_BUFFER_VIEW) + break; + } + case 6: { // INDEX_BUFFER_VIEW + // D3D12_INDEX_BUFFER_VIEW: BufferLocation(u64), SizeInBytes(u32), Format(u32) + const std::uint64_t view_va = p.binding.va + ind->argument_offset + arg_cursor; + const std::uint8_t* host = nullptr; + if (vk.host_at(view_va, sizeof(std::uint64_t) + 2 * sizeof(std::uint32_t), host) == HostReadStatus::ok) { + const std::uint64_t buffer_va = *reinterpret_cast(host); + std::uint64_t size = *reinterpret_cast(host + sizeof(std::uint64_t)); + const std::uint32_t format = *reinterpret_cast(host + sizeof(std::uint64_t) + sizeof(std::uint32_t)); + VkBuffer ib = VK_NULL_HANDLE; + VkDeviceSize ib_offset = 0; + if (vk.buffer_at(buffer_va, size, ib, ib_offset)) { + VkIndexType vk_format = (format == 57 /* DXGI_FORMAT_R16_UINT */) ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32; + vk.bind_index_buffer(command_buffer, ib, ib_offset, vk_format); + } + } + arg_cursor += 16; // sizeof(D3D12_INDEX_BUFFER_VIEW) + break; + } + case 0: arg_cursor += 16; break; // DRAW + case 1: arg_cursor += 20; break; // DRAW_INDEXED + case 2: arg_cursor += 12; break; // DISPATCH + case 4: arg_cursor += 12; break; // DISPATCH_MESH + case 7: arg_cursor += 4; break; // CONSTANT + case 8: + case 9: + case 10: arg_cursor += 8; break; // CBV/SRV/UAV + default: arg_cursor += 16; break; // unknown / Xbox-only types: conservative stride + } + } if (ind->count_buffer && ind->kind != IndirectKind::dispatch) { const VkDeviceSize count = p.source.buffer_offset + ind->count_offset; if (ind->kind == IndirectKind::draw) {