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
12 changes: 8 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 fixed-count indirect mesh dispatches
are supported with `VK_EXT_mesh_shader`. 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,11 @@ 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 with a fixed command count. 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. Mesh signatures with additional
arguments and mesh execution with a GPU count buffer 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
20 changes: 13 additions & 7 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,13 +992,19 @@ 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 || count_buffer) 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);
if (!arguments) 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};
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
19 changes: 15 additions & 4 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ 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;
};

struct Draw {
std::uintptr_t pipeline = 0;
std::array<std::uint32_t, 32> user_data{};
Expand All @@ -451,6 +458,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 +607,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 +768,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
22 changes: 20 additions & 2 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,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 +1752,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 +2390,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 +3688,14 @@ 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 (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 @@ -4892,7 +4903,14 @@ 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 VkDeviceSize offset = p.binding.buffer_offset + args.offset;
// 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, p.binding.buffer,
offset + VkDeviceSize(record) * args.stride, 1, args.stride);
} 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
20 changes: 17 additions & 3 deletions runtime/test/d3d12_mesh_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
// refuses (MS with VS, CS with PS, a subobject twice or in two versions, an unknown type, a truncated stream) are E_INVALIDARG, and
// state not built yet (an amplification shader, a depth-bounds test) is E_NOTIMPL.
// - Draws: DispatchMesh(2, 2, 1) with root constants (0, 0) and a red tint fills the top-left quadrant;
// DispatchMesh(1, 1, 2) moved by (1, -1) with a green tint fills the top half of the bottom-right one, so the
// ExecuteIndirect with a padded DISPATCH_MESH signature runs an empty grid and then a (1, 1, 2) grid. The grid
// moved by (1, -1) with a green tint fills the top half of the bottom-right quadrant, so the
// Y and Z counts reach the shader; the stream's VS PSO draws a full-screen triangle through a viewport on the
// bottom-left quadrant (yellow). Each pixel is read back and compared.
// - A vertex draw with the mesh PSO, and DispatchMesh with the VS one, fail at execution (ReplayResult::
Expand Down Expand Up @@ -394,7 +395,19 @@ int main(int argc, char** argv) {
std::uint8_t* up = nullptr;
EXPECT(SUCCEEDED(upload->Map(0, nullptr, reinterpret_cast<void**>(&up))));
for (int i = 0; i < 3; ++i) std::memcpy(up + 256 * i, tints[i], sizeof(tints[i]));
constexpr UINT mesh_args_offset = 1024;
struct MeshRecord {
D3D12_DISPATCH_MESH_ARGUMENTS grid;
UINT padding;
};
const MeshRecord mesh_args[] = {{{0, 0, 0}, 0}, {{1, 1, 2}, 0}};
std::memcpy(up + mesh_args_offset, mesh_args, sizeof(mesh_args));
const D3D12_GPU_VIRTUAL_ADDRESS upload_va = upload->GetGPUVirtualAddress();
const D3D12_INDIRECT_ARGUMENT_DESC mesh_argument{D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH_MESH};
const D3D12_COMMAND_SIGNATURE_DESC mesh_signature_desc{sizeof(MeshRecord), 1, &mesh_argument, 0};
Ref<ID3D12CommandSignature> mesh_signature;
EXPECT(SUCCEEDED(device->CreateCommandSignature(&mesh_signature_desc, nullptr,
IID_PPV_ARGS(&mesh_signature.p))));

const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
Ref<ID3D12DescriptorHeap> rtv_heap;
Expand Down Expand Up @@ -439,7 +452,7 @@ int main(int argc, char** argv) {
// of the bottom-right quadrant.
list->SetGraphicsRoot32BitConstants(0, 2, down_right, 0);
list->SetGraphicsRootConstantBufferView(1, upload_va + 256);
list6->DispatchMesh(1, 1, 2);
list->ExecuteIndirect(mesh_signature.p, 2, upload.p, mesh_args_offset, nullptr, 0);
// The stream's VS PSO: a full-screen triangle through a viewport on the bottom-left quadrant, yellow.
const D3D12_VIEWPORT bottom_left{0, float(size / 2), float(size / 2), float(size / 2), 0, 1};
list->RSSetViewports(1, &bottom_left);
Expand Down Expand Up @@ -714,6 +727,7 @@ int main(int argc, char** argv) {
for (auto* p : {&mesh_pso, &vs_pso, &refused}) p->reset();
root.reset();
rtv_heap.reset();
mesh_signature.reset();
list6.reset();
list.reset();
allocator.reset();
Expand Down Expand Up @@ -745,7 +759,7 @@ int main(int argc, char** argv) {
return 1;
}
std::printf("D3D-29: mesh tier 1; mesh and VS PSOs from pipeline state streams (9 refused as invalid, 2 as not "
"implemented); DispatchMesh over X, Y and Z and a stream VS draw matched all %u pixels; wrong-kind "
"implemented); direct and indirect mesh dispatches and a stream VS draw matched all %u pixels; wrong-kind "
"draws fail at execution, oversized grids at Close; statistics queries around mesh draws resolve\n",
size * size);
return 0;
Expand Down
Loading