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
16 changes: 16 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -3193,6 +3193,22 @@ Not implemented yet:
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
signatures the game creates.
- `CreateCommandSignature` refuses with `E_INVALIDARG` what the host runtime refuses, with or without a root
signature, and a refused call leaves the output as it was. `d3d12_command_signature_root_test` compares 177
values (RTX 3060, driver 610.88) beyond `compare_invalid_arguments`' signatures without one. The rules it
measured:
- A root signature is passed only with at least one root argument (constants, CBV, SRV or UAV); with none it
is refused.
- A root argument names a parameter of its type (root constants for `CONSTANT`, the same kind of root
descriptor for a view), and constants fit their parameter, at least one value.
- Root arguments come in increasing parameter order. Constants into one parameter come in increasing ranges
that don't overlap.
- An incrementing constant is one root constant: a root-constants parameter, an offset inside it, and no overlap
with the other constants of that parameter.
- A vertex buffer view uses a slot below 32, each slot once, and not with a compute dispatch (a mesh dispatch takes
one). An index buffer view comes once and only with an indexed draw. Both rules hold without a root signature too.
- The host takes mesh-dispatch signatures; the layer still answers `E_NOTIMPL` for them, so the test doesn't compare
those (indirect mesh dispatch is #3777).
- Predication (`SetPredication`) maps to `VK_EXT_conditional_rendering` (D3D-18). Done: `GraphicsCommandList::
SetPredication` records a `Predication` command (`runtime/src/command_list.h`; offset 8-byte aligned, op
`EQUAL_ZERO` or `NOT_EQUAL_ZERO`, a null buffer turns it off). The replay (`runtime/src/vulkan_replay.h`) begins
Expand Down
8 changes: 8 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,14 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_pipeline_library_host COMMAND d3d12_pipeline_library_host_test)
set_tests_properties(d3d12_pipeline_library_host PROPERTIES SKIP_RETURN_CODE 77)

# CreateCommandSignature with a root signature (root constants, CBV/SRV/UAV, VB/IB views), compared with the host.
add_executable(d3d12_command_signature_root_test test/d3d12_command_signature_root_test.cpp)
target_link_libraries(d3d12_command_signature_root_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
target_compile_definitions(d3d12_command_signature_root_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_command_signature_root_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_command_signature_root COMMAND d3d12_command_signature_root_test)
set_tests_properties(d3d12_command_signature_root PROPERTIES SKIP_RETURN_CODE 77)

# Milestone M0: a window, 60 cleared frames presented through the proxy on Vulkan.
add_executable(d3d12_present_test test/d3d12_present_test.cpp)
target_link_libraries(d3d12_present_test PRIVATE gta6rt_d3d12_com user32 gdi32 dxgi)
Expand Down
2 changes: 2 additions & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ struct RootArgumentLayout {
std::uint32_t user_offset; // graphics: first dword; unused for a parameter with no dwords
std::uint32_t constant_count; // constants only
std::uint32_t compute_offset = rt::rootsig::kNoSgpr; // compute: first user SGPR
// D3D12_ROOT_PARAMETER_TYPE (tells a root CBV, SRV and UAV apart), or UINT32_MAX when it isn't known.
std::uint32_t d3d_type = 0xFFFFFFFFu;
};
std::vector<Parameter> parameters;
bool graphics_ok = true;
Expand Down
68 changes: 65 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5211,12 +5211,25 @@ class Device final
HRESULT STDMETHODCALLTYPE CreateCommandSignature(const D3D12_COMMAND_SIGNATURE_DESC* desc,
ID3D12RootSignature* root_signature, REFIID riid,
void** out) override {
if (out) *out = nullptr;
// A refused call leaves *out as it was, as the host does (d3d12_command_signature_root_test).
// The host refuses these with E_INVALIDARG (compare_invalid_arguments, "command signature:"): one node, a
// known type per argument, exactly one draw or dispatch and it comes last, a root signature for root
// arguments, and a stride that is a multiple of 4 and holds every argument.
if (!desc || desc->NumArgumentDescs == 0 || !desc->pArgumentDescs || (desc->NodeMask & ~1u))
return E_INVALIDARG;
// With a root signature (d3d12_command_signature_root_test, RTX 3060 / 610.88), the host also wants: at
// least one root argument (a root signature with none is refused), each naming a parameter of the right
// type (root constants for CONSTANT, the same root CBV/SRV/UAV for a view), constants that fit their
// parameter (at least one value), and root arguments in increasing parameter order, constants into one
// parameter in increasing ranges that don't overlap. Vertex buffer views need a slot below 32, each slot
// at most once, and no compute dispatch; an index buffer view comes at most once and only with an indexed
// draw. The vertex and index buffer rules hold without a root signature too. An incrementing constant is one
// root constant.
const RootArgumentLayout* layout = root_signature ? root_signature_layout(root_signature) : nullptr;
bool any_root_argument = false;
std::uint64_t root_position = 0; // where the last root argument ended: parameter << 33 | end of its range
std::uint32_t vertex_slots = 0;
UINT index_buffers = 0;
UINT64 size = 0; // 64-bit: a large Num32BitValuesToSet must not wrap past ByteStride
for (UINT i = 0; i < desc->NumArgumentDescs; ++i) {
const D3D12_INDIRECT_ARGUMENT_DESC& arg = desc->pArgumentDescs[i];
Expand All @@ -5241,9 +5254,57 @@ class Device final
const bool root_argument = !draw && arg.Type != D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW &&
arg.Type != D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW;
if (draw != (i + 1 == desc->NumArgumentDescs) || (root_argument && !root_signature)) return E_INVALIDARG;
}
if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_VERTEX_BUFFER_VIEW) {
const UINT slot = arg.VertexBuffer.Slot;
if (slot >= D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT || (vertex_slots >> slot & 1u))
return E_INVALIDARG;
vertex_slots |= 1u << slot;
}
if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_INDEX_BUFFER_VIEW && ++index_buffers > 1) return E_INVALIDARG;
if (!root_argument) continue;
any_root_argument = true;
if (!layout) continue; // a root signature from another device: nothing to check against
// Every root argument type keeps its parameter index in the same place (the union's first member).
const UINT index = arg.Constant.RootParameterIndex;
if (index >= layout->parameters.size()) return E_INVALIDARG;
const RootArgumentLayout::Parameter& parameter = layout->parameters[index];
std::uint64_t first = 0, end = 1; // a view takes its whole parameter
if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT ||
arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_INCREMENTING_CONSTANT) {
// An incrementing constant is one root constant the runtime counts up, with the same rules.
first = arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT
? arg.Constant.DestOffsetIn32BitValues
: arg.IncrementingConstant.DestOffsetIn32BitValues;
end = first + (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT ? arg.Constant.Num32BitValuesToSet
: 1);
if (parameter.type != rt::rootsig::ParamType::Constants || end == first ||
end > parameter.constant_count)
return E_INVALIDARG;
} else if (arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW ||
arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_SHADER_RESOURCE_VIEW ||
arg.Type == D3D12_INDIRECT_ARGUMENT_TYPE_UNORDERED_ACCESS_VIEW) {
// D3D12_ROOT_PARAMETER_TYPE_CBV/SRV/UAV are 2/3/4, in the order of these argument types.
const std::uint32_t wanted =
D3D12_ROOT_PARAMETER_TYPE_CBV + (arg.Type - D3D12_INDIRECT_ARGUMENT_TYPE_CONSTANT_BUFFER_VIEW);
if (parameter.type != rt::rootsig::ParamType::Descriptor ||
(parameter.d3d_type != 0xFFFFFFFFu && parameter.d3d_type != wanted))
return E_INVALIDARG;
}
// Parameter order, then the constants' ranges: each root argument starts where the last one ended or
// later (end values fit in 33 bits: a 32-bit offset plus a 32-bit count).
const std::uint64_t start = std::uint64_t(index) << 33 | first;
if (start < root_position) return E_INVALIDARG;
root_position = std::uint64_t(index) << 33 | end;
}
if (root_signature && !any_root_argument) return E_INVALIDARG;
const D3D12_INDIRECT_ARGUMENT_TYPE last = desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type;
// Measured against DRAW, DISPATCH and DISPATCH_MESH: the host takes a vertex buffer view with a mesh dispatch
// (E_NOTIMPL below here), an index buffer view only with an indexed draw.
if (vertex_slots && last == D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH) return E_INVALIDARG;
if (index_buffers && last != D3D12_INDIRECT_ARGUMENT_TYPE_DRAW_INDEXED) return E_INVALIDARG;
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
*out = nullptr;
leonida::IndirectKind kind;
switch (desc->pArgumentDescs[desc->NumArgumentDescs - 1].Type) {
case D3D12_INDIRECT_ARGUMENT_TYPE_DRAW: kind = leonida::IndirectKind::draw; break;
Expand Down Expand Up @@ -5449,7 +5510,8 @@ class Device final
break;
}
layout.parameters.push_back({parsed.sig.params[i].type, first, parsed.sig.params[i].num_values,
compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr});
compute.ok ? compute.first_sgpr[i] : rt::rootsig::kNoSgpr,
parsed.sig.params[i].d3d_type});
}
register_static_samplers(parsed.sig);
auto* signature = new RootSignature(
Expand Down
1 change: 1 addition & 0 deletions runtime/src/root_signature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ static DecodeResult parse_rts0_payload(const uint8_t* data, size_t size) {
}
RootParameter p;
p.visibility = static_cast<Visibility>(vis);
p.d3d_type = dtype;
if (dtype == 1) {
p.type = ParamType::Constants;
if (!u32_at(poff, p.shader_register) || !u32_at(poff + 4, p.register_space) ||
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/root_signature.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ struct RootParameter {
uint32_t descriptor_space = 0;
// Descriptor table:
std::vector<DescriptorRange> ranges;
// D3D12_ROOT_PARAMETER_TYPE as the blob stores it (0 table, 1 constants, 2/3/4 CBV/SRV/UAV), which `type`
// folds into three kinds; set by parse_rts0, kUnknownD3dType for a parameter built another way.
static constexpr uint32_t kUnknownD3dType = 0xFFFFFFFFu;
uint32_t d3d_type = kUnknownD3dType;
};

struct RootSignature {
Expand Down
Loading
Loading