Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<D3D12_INDIRECT_ARGUMENT_TYPE> 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<D3D12_INDIRECT_ARGUMENT_TYPE>& argument_types() const { return argument_types_; }
ID3D12RootSignature* root_signature() const { return root_signature_; }
static CommandSignature* from(IUnknown* object) { return Child::from<CommandSignature>(object); }

private:
leonida::IndirectKind kind_;
UINT stride_;
std::vector<D3D12_INDIRECT_ARGUMENT_TYPE> argument_types_;
ID3D12RootSignature* root_signature_ = nullptr;
};

class CommandAllocator final
Expand Down Expand Up @@ -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<D3D12_INDIRECT_ARGUMENT_TYPE> 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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<const D3D12_INDIRECT_ARGUMENT_TYPE> 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<D3D12_INDIRECT_ARGUMENT_TYPE> 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<D3D12_INDIRECT_ARGUMENT_TYPE>* 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;
}

Expand Down
8 changes: 6 additions & 2 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const D3D12_INDIRECT_ARGUMENT_TYPE> 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<D3D12_INDIRECT_ARGUMENT_TYPE>* 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.
Expand Down
9 changes: 8 additions & 1 deletion runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<D3D12_INDIRECT_ARGUMENT_TYPE> 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;
Expand All @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uint32_t> 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.
Expand Down
58 changes: 58 additions & 0 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const std::uint64_t*>(host);
std::uint64_t size = *reinterpret_cast<const std::uint32_t*>(host + sizeof(std::uint64_t));
const std::uint32_t stride = *reinterpret_cast<const std::uint32_t*>(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<const std::uint64_t*>(host);
std::uint64_t size = *reinterpret_cast<const std::uint32_t*>(host + sizeof(std::uint64_t));
const std::uint32_t format = *reinterpret_cast<const std::uint32_t*>(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) {
Expand Down