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
15 changes: 11 additions & 4 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ hand-written RDNA2 run through the translator in `d3d12_translated_draw_test`, a
`d3d12_m2_textured_test`. `D3D12XboxCreateDevice` uses it when `LEONIDA_D3D12=vulkan` is
set and the host PC device otherwise. Root signatures (packed into the 32 user-data dwords), graphics and compute
PSOs, draws, dispatches, `ExecuteIndirect`, and presentation through `PresentX` or a DXGI swapchain are implemented
and have GPU tests; which present path the game uses is still open (question 6). Shader-resource views of buffers,
pipeline-state streams (`CreatePipelineState`), tessellation and stream-output pipelines are not implemented, and
mesh shaders are reported as not supported (stream PSOs and mesh shaders: D3D-29, #1712). The device reports
and have GPU tests; which present path the game uses is still open (question 6). Buffer SRVs and pipeline-state
streams (`CreatePipelineState`) are implemented. Mesh PSOs, `DispatchMesh`, and indirect mesh dispatches
are supported with `VK_EXT_mesh_shader`; GPU count buffers have additional requirements (section 4.7). The device reports
`RaytracingTier` as `NOT_SUPPORTED`, yet `BuildRaytracingAccelerationStructure` builds XDXR BVH4 structures on the
GPU (section 4.8).** This is the plan for
replacing the console's `d3d12_xs.dll` with an implementation that records D3D12 commands and replays them on
Expand Down Expand Up @@ -3764,7 +3764,14 @@ Not implemented yet:
- `D3D12_COMMAND_SIGNATURE_DESC` describes the argument stride and a list of argument types. Simple draw,
draw-indexed, dispatch and mesh-dispatch signatures map to `vkCmdDraw*Indirect`, `vkCmdDispatchIndirect` and
`vkCmdDrawMeshTasksIndirectEXT`; use the `*Count` variants (`VK_KHR_draw_indirect_count`, core 1.2) when the
`ExecuteIndirect` call supplies a count buffer.
`ExecuteIndirect` call supplies a count buffer for draw or draw-indexed signatures.
- A single `DISPATCH_MESH` argument is supported on direct lists. Recording captures
the graphics PSO, root arguments and render targets as `DispatchMesh` does. Vulkan reads the three group counts
from the argument buffer at execution, using the signature's byte stride. With a count buffer, the same GPU helper
as compute indirect dispatch prepares compact mesh records and zeroes those beyond the clamped count before
rendering. This requires buffer device addresses and keeps the helper dispatch outside pipeline statistics queries.
Fixed counts work on mesh-capable devices without that path.
Mesh signatures with additional arguments are still unsupported.
- Signatures that change root constants, vertex/index buffers or descriptors per argument cannot be expressed by
one Vulkan indirect call. Use a compute expansion pass that writes a Vulkan-shaped indirect buffer, or
`VK_NV_device_generated_commands` on NVIDIA. Start with the simple signatures; the trace will show which
Expand Down
23 changes: 16 additions & 7 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const char* name(leonida::ReplayResult result) {
case leonida::ReplayResult::invalid_query: return "invalid query";
case leonida::ReplayResult::invalid_indirect_arguments: return "invalid indirect argument or count buffer range";
case leonida::ReplayResult::invalid_draw: return "draw kind does not match the pipeline (mesh or vertex)";
case leonida::ReplayResult::unsupported_mesh_indirect_count:
return "GPU mesh indirect count could not be prepared (requires buffer device addresses and compute scratch)";
case leonida::ReplayResult::unsupported: return "unsupported";
}
return "?";
Expand Down Expand Up @@ -992,13 +994,20 @@ class GraphicsCommandList final
return check(false);
}
if (kind == leonida::IndirectKind::dispatch_mesh) {
// Not replayed yet (#3777 replays it as vkCmdDrawMeshTasksIndirectEXT): nothing is recorded, and the
// first such call says so. The list stays valid.
static std::atomic<bool> logged{false};
if (!logged.exchange(true, std::memory_order_relaxed))
std::fprintf(stderr, "[rt] d3d12: ExecuteIndirect with a DISPATCH_MESH signature is not replayed yet; "
"nothing was recorded\n");
return;
if (type_ != D3D12_COMMAND_LIST_TYPE_DIRECT) return check(false);
if (!max_command_count) return;
if (!graphics_pipeline_ || !graphics_.layout) return check(false);
if (!any_nonempty_viewport()) return;
const auto arguments = resource_id(argument_buffer);
const auto count = count_buffer ? resource_id(count_buffer) : 0;
if (!arguments || (count_buffer && !count)) return check(false);
leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_,
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_};
draw.mesh = true;
draw.viewports = draw_viewports();
draw.mesh_indirect = leonida::MeshIndirectArguments{arguments, argument_buffer_offset, stride,
max_command_count, count, count_buffer_offset};
return check(list_.draw(std::move(draw)));
}
leonida::ExecuteIndirect cmd{};
cmd.kind = kind;
Expand Down
4 changes: 2 additions & 2 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11709,8 +11709,8 @@ class Device final
case D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED: kind = leonida::IndirectKind::draw_indexed; break;
case D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH: kind = leonida::IndirectKind::dispatch; break;
case D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH:
// As DispatchMesh: only a device with mesh shaders (MeshShaderTier 1) takes it.
if (!state_.info.mesh_shader) return E_NOTIMPL;
if (desc->NumArgumentDescs != 1 || !state_.info.mesh_shader ||
!state_.context->vk.vkCmdDrawMeshTasksIndirectEXT) return E_NOTIMPL;
kind = leonida::IndirectKind::dispatch_mesh;
break;
default: return E_NOTIMPL;
Expand Down
21 changes: 17 additions & 4 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ struct DrawViewports {
// and index buffers and the topology.
// Render targets and vertex buffer views are inlined (8 simultaneous targets, 32 input slots), eliminating heap allocations
// during command list recording (D3D-289).
struct MeshIndirectArguments {
ResourceId buffer;
std::uint64_t offset;
std::uint32_t stride;
std::uint32_t count;
ResourceId count_buffer = 0;
std::uint64_t count_offset = 0;
};

struct Draw {
std::uintptr_t pipeline = 0;
std::array<std::uint32_t, 32> user_data{};
Expand All @@ -451,6 +460,7 @@ struct Draw {
std::uint32_t first_instance = 0;
bool mesh = false;
std::shared_ptr<const DrawViewports> viewports; // non-null when >1 viewport or >1 scissor was bound
std::optional<MeshIndirectArguments> mesh_indirect;
};

// D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.
Expand Down Expand Up @@ -599,8 +609,6 @@ enum class IndirectKind : std::uint8_t {
draw = 0,
draw_indexed = 1,
dispatch = 2,
// D3D12_DISPATCH_MESH_ARGUMENTS. Created on a device with mesh shaders; ExecuteIndirect with it records nothing
// yet (the replay is #3777's), so a title that only creates such a signature at startup goes on.
dispatch_mesh = 3,
};

Expand Down Expand Up @@ -762,15 +770,20 @@ class CommandList {
// not recorded.
bool draw(const Draw& draw) {
if (!draw.pipeline) return false;
if (!draw.count || !draw.instances || (draw.mesh && !draw.first)) return !closed_;
if (!has_draw_work(draw)) return !closed_;
return append(draw);
}
bool draw(Draw&& draw) {
if (!draw.pipeline) return false;
if (!draw.count || !draw.instances) return !closed_;
if (!has_draw_work(draw)) return !closed_;
return append(std::move(draw));
}

static bool has_draw_work(const Draw& draw) {
if (draw.mesh_indirect) return draw.mesh_indirect->count != 0;
return draw.count && draw.instances && (!draw.mesh || draw.first);
}

// ClearRenderTargetView; the resource must be in render_target state at execution. No rects: the whole view.
bool clear_render_target(const TargetView& target, const ClearColor& color, std::span<const Rect> rects = {}) {
if (!valid(target)) return false;
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct VulkanDeviceFunctions {
X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \
X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \
X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \
X(vkCmdDrawMeshTasksIndirectEXT) \
X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \
X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass)
#define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr;
Expand Down Expand Up @@ -1046,6 +1047,7 @@ class VulkanContext {
replay_.set_blend_constants = vk.vkCmdSetBlendConstants;
replay_.draw = vk.vkCmdDraw;
replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled
replay_.draw_mesh_tasks_indirect = vk.vkCmdDrawMeshTasksIndirectEXT;
replay_.draw_indexed = vk.vkCmdDrawIndexed;
replay_.graphics_pipeline = [this](std::uintptr_t id) -> const GraphicsPipeline* {
const auto it = graphics_pipelines_.find(id);
Expand Down
40 changes: 38 additions & 2 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ enum class ReplayResult {
invalid_query,
invalid_indirect_arguments,
invalid_draw, // DispatchMesh with a pipeline that has no mesh shader, or a vertex draw with one that has
unsupported_mesh_indirect_count,
unsupported
};

Expand Down Expand Up @@ -977,6 +978,7 @@ struct ReplayFunctions {
PFN_vkCmdDraw draw = nullptr;
unsigned* mesh_draw_count = nullptr; // opt-in count for this submission
PFN_vkCmdDrawMeshTasksEXT draw_mesh_tasks = nullptr; // VK_EXT_mesh_shader; null: mesh draws are unsupported
PFN_vkCmdDrawMeshTasksIndirectEXT draw_mesh_tasks_indirect = nullptr;
PFN_vkCmdDrawIndexed draw_indexed = nullptr;
std::function<const GraphicsPipeline*(std::uintptr_t)> graphics_pipeline;
// The buffer and offset a console VA range starts in (from ResourceBinding::va), false when none does. `size` is
Expand Down Expand Up @@ -1751,6 +1753,7 @@ inline VkDeviceSize indirect_record_size(IndirectKind kind) {
case IndirectKind::draw: return sizeof(VkDrawIndirectCommand);
case IndirectKind::draw_indexed: return sizeof(VkDrawIndexedIndirectCommand);
case IndirectKind::dispatch: return sizeof(VkDispatchIndirectCommand);
case IndirectKind::dispatch_mesh: return 0; // Mesh dispatches use Draw to capture graphics state.
}
return 0;
}
Expand Down Expand Up @@ -2388,7 +2391,8 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
// A null pipeline with a variant builder is one built at its first draw: variant 0 (all slots bound).
if (!pipeline || (!pipeline->pipeline && !pipeline->variant)) return unresolved_at(__LINE__);
if (pipeline->mesh != draw.mesh) return ReplayResult::invalid_draw;
if (draw.mesh && !vk.draw_mesh_tasks) return unsupported_at(__LINE__);
if (draw.mesh_indirect && !vk.draw_mesh_tasks_indirect) return unsupported_at(__LINE__);
if (draw.mesh && !draw.mesh_indirect && !vk.draw_mesh_tasks) return unsupported_at(__LINE__);
if (pipeline->viewport_with_count && (!vk.set_viewport_with_count || !vk.set_scissor_with_count))
return unsupported_at(__LINE__);
if (pipeline->bindless_set && !vk.bind_descriptor_sets) return unsupported_at(__LINE__);
Expand Down Expand Up @@ -3685,6 +3689,27 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
}
if (pipeline->bindless_set && !vk.bind_descriptor_sets) return unsupported_at(__LINE__);
} else if (const auto* draw = std::get_if<Draw>(&command)) {
if (draw->mesh_indirect) {
const auto& args = *draw->mesh_indirect;
if (!bind(args.buffer) || !p.binding.buffer) return ReplayResult::unresolved_resource;
constexpr VkDeviceSize record_size = sizeof(VkDrawMeshTasksIndirectCommandEXT);
if (!draw->mesh || !args.count || args.stride < record_size || args.stride % 4 ||
!detail::indirect_range(p.binding, args.offset, VkDeviceSize(args.count - 1) * args.stride + record_size))
return ReplayResult::invalid_indirect_arguments;
if (args.count_buffer) {
const ResourceBinding* count = resolve(args.count_buffer);
if (!count || !count->buffer) return ReplayResult::unresolved_resource;
if (!detail::indirect_range(*count, args.count_offset, sizeof(std::uint32_t)))
return ReplayResult::invalid_indirect_arguments;
// Clamp the GPU count before executing any draw, including when the count is UINT32_MAX.
if (!vk.prepare_counted_dispatch || !vk.emit_counted_dispatch ||
(statistics_active && !statistics_helpers) ||
!vk.prepare_counted_dispatch(p.binding.buffer, p.binding.buffer_offset + args.offset,
count->buffer, count->buffer_offset + args.count_offset,
args.stride, args.count, p.counted_dispatch))
return ReplayResult::unsupported_mesh_indirect_count;
}
}
if (out.draw_count == out.draws.size()) out.draws.emplace_back();
auto& prepared_draw = out.draws[out.draw_count];
prepared_draw.reset();
Expand Down Expand Up @@ -4023,6 +4048,8 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
end_statistics();
if (std::holds_alternative<Draw>(command) && prepared.draws[p.draw].quad_indices.buffer)
vk.emit_quad_indices(command_buffer, prepared.draws[p.draw].quad_indices);
if (std::holds_alternative<Draw>(command) && p.counted_dispatch.indirect)
vk.emit_counted_dispatch(command_buffer, p.counted_dispatch);
begin_statistics();
}
if (const auto* predication = std::get_if<Predication>(&command)) {
Expand Down Expand Up @@ -4892,7 +4919,16 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_
vk.bind_vertex_buffers2(command_buffer, slot, 1, &d.vertex_buffers[slot], &d.vertex_offsets[slot],
&d.vertex_sizes[slot], &d.vertex_strides[slot]);
}
if (draw->mesh) {
if (draw->mesh_indirect) {
const auto& args = *draw->mesh_indirect;
const bool counted = p.counted_dispatch.indirect != VK_NULL_HANDLE;
const VkBuffer buffer = counted ? p.counted_dispatch.indirect : p.binding.buffer;
const VkDeviceSize offset = counted ? p.counted_dispatch.offset : p.binding.buffer_offset + args.offset;
const VkDeviceSize stride = counted ? sizeof(VkDrawMeshTasksIndirectCommandEXT) : args.stride;
// A single record per call also works without Vulkan's multiDrawIndirect feature.
for (std::uint32_t record = 0; record < args.count; ++record)
vk.draw_mesh_tasks_indirect(command_buffer, buffer, offset + record * stride, 1, 0);
} else if (draw->mesh) {
vk.draw_mesh_tasks(command_buffer, draw->count, draw->instances, draw->first);
if (vk.mesh_draw_count) ++*vk.mesh_draw_count;
} else if (draw->topology == topology_quad_list) {
Expand Down
Loading
Loading