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
13 changes: 12 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ The command-list surface maps to the IR as follows:
| `SetGraphicsRootSignature` / `SetComputeRootSignature` | state; select the pipeline layout and root map |
| `Set*Root32BitConstant(s)` / `Set*Root*View` / `Set*RootDescriptorTable` | update root args; build the push-constant block at the draw/dispatch |
| `SetDescriptorHeaps` | state; scan/populate the bindless arrays and hash table at execute |
| `IASetVertexBuffers` / `IASetIndexBuffer` / `IASetPrimitiveTopology` | state; bind arena buffer + offset, dynamic topology |
| `IASetVertexBuffers` / `IASetIndexBuffer` / `IASetPrimitiveTopology` | state; bind arena buffer + offset, dynamic topology. The console's rectangle list (17, PSO topology type 19) and quad list (19) are drawn as triangles, see *Console rectangle and quad lists* below |
| `RSSetViewports` / `RSSetScissorRects` | state; dynamic viewport/scissor |
| `OMSetRenderTargets` / `OMSetBlendFactor` / `OMSetStencilRef` | state; rendering scope and dynamic state |
| `DrawInstanced` / `DrawIndexedInstanced` | command with a state snapshot |
Expand Down Expand Up @@ -2109,6 +2109,17 @@ The trace will enumerate them.
- **What it doesn't cover:** whether the console flattens a 3D `DispatchMesh` grid the same way. That is an open
question in `docs/design.md`, "Mesh shaders"; the test shader reads the workgroup ID directly.

### Console rectangle and quad lists

The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiveTopology(17)`, with a PSO whose topology type is 19) and quad list (`IASetPrimitiveTopology(19)`). The facts are from xvalkyrie (GPL-3.0, commit `53af7df77c10`; no code taken): a rectangle is three corners and the fourth is v2 + v3 - v1 for the position and every interpolant, after the vertex stage; a quad is four vertices. The layer draws both as triangles (`d3d12_rect_list_test`, RTX 4090: 4096 of 4096 pixels, the colour plane exact, quad list bit-exact).
- **Rectangle list**: PSO type 19 builds a triangle-list pipeline plus a generated geometry stage (`src/rect_list_geometry.h`, written from the vertex shader's own outputs) that takes the three corners and emits the strip v1 v2 v3 v4 (same winding). It has to be post-vertex-stage: the translated vertex shaders fetch by V# themselves, and the fourth corner is a function of what they write. A corner value with the same bits at all three corners (a flat attribute) is copied. Needs `geometryShader` (else `E_NOTIMPL`), no application GS, and a vertex shader with no ClipDistance, CullDistance, Layer or ViewportIndex output.
- **Quad list**: a non-indexed draw uses a shared index buffer containing the triangle-list pattern
`0 1 2, 0 2 3` for each quad. Both triangles start at the first vertex for flat inputs. One draw preserves instance
order and supports up to 16384 quads. The draw's first vertex becomes the vertex offset. Indexed draws still
read the application's index buffer as one four-vertex fan per quad. Vulkan fans use a different provoking
vertex for flat inputs, and overlapping blended instances rasterize quad by quad in this path. The PSO type
is the ordinary triangle type.

## 4. Object model mapping

| D3D12 / Xbox object | Vulkan / layer object | Notes |
Expand Down
15 changes: 15 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,15 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_translated_draw COMMAND d3d12_translated_draw_test)
set_tests_properties(d3d12_translated_draw PROPERTIES SKIP_RETURN_CODE 77)

# The console's rectangle and quad lists (src/rect_list_geometry.h), through the same translated vs_m15 / ps_m15.
add_executable(d3d12_rect_list_test test/d3d12_rect_list_test.cpp shims/d3d12_xs_device.cpp ${LEONIDA_M15_FILES})
target_link_libraries(d3d12_rect_list_test PRIVATE gta6rt_d3d12_com)
target_compile_definitions(d3d12_rect_list_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX
LEONIDA_TEST_M15_DIR="${LEONIDA_M15_DIR}")
target_compile_options(d3d12_rect_list_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_rect_list COMMAND d3d12_rect_list_test)
set_tests_properties(d3d12_rect_list PROPERTIES SKIP_RETURN_CODE 77)

# vk_bindless_test (the bindless set and its table at the Vulkan level, no D3D12) takes the translated vs_ids,
# ps_tex and ps_imgatomic; it was only run by scripts/run_gpu_tests.py and check.py until D3D-314 needed it.
set(LEONIDA_BINDLESS_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/bindless_shaders)
Expand Down Expand Up @@ -3216,6 +3225,12 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(replay_readback_stages_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME replay_readback_stages COMMAND replay_readback_stages_test)
add_executable(replay_quad_list_test test/replay_quad_list_test.cpp)
target_link_libraries(replay_quad_list_test PRIVATE Vulkan::Headers)
if(WIN32)
target_compile_definitions(replay_quad_list_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME replay_quad_list COMMAND replay_quad_list_test)
# emit of an enhanced texture barrier: no heap allocation for one layout piece, one barrier for each piece (no GPU).
add_executable(replay_texture_barrier_test test/replay_texture_barrier_test.cpp)
target_link_libraries(replay_texture_barrier_test PRIVATE Vulkan::Headers)
Expand Down
61 changes: 52 additions & 9 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "../src/arena_size.h"
#include "../src/gfx10_descriptors.h"
#include "../src/depth24_fragment.h"
#include "../src/rect_list_geometry.h"
#include "../src/uav_clear.h"
#include "../src/vulkan_bindless.h"
#include "../src/vulkan_memory.h"
Expand Down Expand Up @@ -65,7 +66,10 @@ namespace {
constexpr UINT kDescriptorSize = descriptor_size;
// The first 64 KiB of the GPU address arena are never handed out: VA 0 stays invalid, and the page is kept zero
// for unbound vertex and index buffers to read (D3D12 reads zeros there).
constexpr VkDeviceSize kArenaReserved = 64 * 1024;
constexpr VkDeviceSize kZeroPage = 64 * 1024; // zeroed: what unbound vertex and index buffers read
constexpr std::uint32_t kQuadIndicesPerQuad = leonida::quad_triangle_indices.size();
constexpr std::uint32_t kQuadIndexCount = 16 * 1024 * kQuadIndicesPerQuad;
constexpr VkDeviceSize kArenaReserved = kZeroPage + VkDeviceSize(kQuadIndexCount) * sizeof(std::uint32_t);
static_assert(sizeof(leonida::TargetView) <= kDescriptorSize);

// The shader model and root signature version this layer can honour. Nothing in the runtime compiles DXIL: the
Expand Down Expand Up @@ -3058,7 +3062,8 @@ leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC&
key.text(e.SemanticName).u32(e.SemanticIndex).u32(e.Format).u32(e.InputSlot).u32(e.AlignedByteOffset);
key.u32(e.InputSlotClass).u32(e.InstanceDataStepRate);
}
key.u32(desc.IBStripCutValue).u32(desc.PrimitiveTopologyType).u32(desc.NumRenderTargets);
key.u32(desc.IBStripCutValue).u32(std::bit_cast<std::uint32_t>(desc.PrimitiveTopologyType))
.u32(desc.NumRenderTargets);
for (const DXGI_FORMAT format : desc.RTVFormats) key.u32(format);
key.u32(desc.DSVFormat).u32(desc.SampleDesc.Count).u32(desc.SampleDesc.Quality).u32(desc.NodeMask);
return leonida::sha256_hex(key.data());
Expand Down Expand Up @@ -3455,8 +3460,18 @@ class PipelineState final
log_once("geometry shaders need the Vulkan geometryShader feature; stage", 0);
return E_NOTIMPL;
}
if (geometry && desc.PrimitiveTopologyType != D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
log_once("a translated geometry shader takes triangles; topology type", desc.PrimitiveTopologyType);
// The console's rectangle topology type (19) draws as triangles through a generated geometry stage that adds
// each rectangle's fourth corner (src/rect_list_geometry.h); it needs the same device feature, and no GS of
// the application's own.
// Decode the ABI field as bits. The console's value is outside the PC enum's range.
const auto topology_type = std::bit_cast<std::uint32_t>(desc.PrimitiveTopologyType);
const bool rect_list = !mesh && topology_type == leonida::topology_type_rect_list;
if (rect_list && (geometry || !device_.info.features.geometryShader)) {
log_once("a rectangle-list pipeline needs the geometryShader feature and no geometry shader; stage", 0);
return geometry ? E_INVALIDARG : E_NOTIMPL;
}
if (geometry && topology_type != D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) {
log_once("a translated geometry shader takes triangles; topology type", topology_type);
return E_INVALIDARG;
}
const UINT samples = desc.SampleDesc.Count;
Expand Down Expand Up @@ -3635,6 +3650,26 @@ class PipelineState final
subgroup_size_for(*stage.entry, required_sizes[stage_infos.size()], info);
stage_infos.push_back(info);
}
if (rect_list) { // the stage that adds each rectangle's fourth corner, from the vertex shader's outputs
std::string why;
const std::vector<std::uint32_t> words = leonida::rect_list_geometry(vs_stage.words, why);
if (words.empty()) {
std::fprintf(stderr, "[rt] d3d12: %s cannot be drawn as a rectangle list: %s\n",
vs_stage.entry->name.c_str(), why.c_str());
return E_NOTIMPL;
}
VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
module_info.codeSize = words.size() * 4;
module_info.pCode = words.data();
VkShaderModule module = VK_NULL_HANDLE;
if (vk.vkCreateShaderModule(dev, &module_info, nullptr, &module) != VK_SUCCESS) return E_FAIL;
modules_.push_back(module);
VkPipelineShaderStageCreateInfo info{VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO};
info.stage = VK_SHADER_STAGE_GEOMETRY_BIT;
info.module = module;
info.pName = "main";
stage_infos.push_back(info);
}
if (depth24_fragment && !ps_stage.entry) { // no pixel shader: a built-in one writes the depth
const std::vector<std::uint32_t> words = leonida::depth24_depth_only_fragment(depth24_bias);
VkShaderModuleCreateInfo module_info{VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO};
Expand Down Expand Up @@ -3709,12 +3744,13 @@ class PipelineState final
}
}
VkPipelineInputAssemblyStateCreateInfo assembly{VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO};
switch (mesh ? D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE : desc.PrimitiveTopologyType) { // mesh: not used
// Type 19 is the console's rectangles, drawn as triangle lists through the geometry stage added above.
switch (mesh || rect_list ? D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE : topology_type) { // mesh: not used
case D3D12_PRIMITIVE_TOPOLOGY_TYPE_POINT: assembly.topology = VK_PRIMITIVE_TOPOLOGY_POINT_LIST; break;
case D3D12_PRIMITIVE_TOPOLOGY_TYPE_LINE: assembly.topology = VK_PRIMITIVE_TOPOLOGY_LINE_LIST; break;
case D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE: assembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; break;
default:
log_once("primitive topology type not supported:", desc.PrimitiveTopologyType);
log_once("primitive topology type not supported:", topology_type);
return E_INVALIDARG;
}
if (!mesh) { // a mesh PSO has no index buffer: its strip cut is ignored
Expand All @@ -3736,7 +3772,7 @@ class PipelineState final
return E_INVALIDARG;
}
if (r.FillMode != D3D12_FILL_MODE_SOLID ||
desc.PrimitiveTopologyType != D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) return E_INVALIDARG;
topology_type != D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE) return E_INVALIDARG;
// Target-independent rasterization is a separate pipeline path; do not silently ignore its count.
if (r.ForcedSampleCount) return E_NOTIMPL;
}
Expand Down Expand Up @@ -5442,7 +5478,13 @@ class Device final
return false;
}
arena.mapped = static_cast<std::uint8_t*>(mapped);
std::memset(arena.mapped, 0, kArenaReserved); // the zero page unbound vertex and index buffers read
std::memset(arena.mapped, 0, kZeroPage); // the zero page unbound vertex and index buffers read
auto* indices = reinterpret_cast<std::uint32_t*>(arena.mapped + kZeroPage);
for (std::uint32_t i = 0; i < kQuadIndexCount; i += kQuadIndicesPerQuad) {
const std::uint32_t base = i / kQuadIndicesPerQuad * 4;
std::transform(leonida::quad_triangle_indices.begin(), leonida::quad_triangle_indices.end(), indices + i,
[base](std::uint32_t index) { return base + index; });
}
VkBufferDeviceAddressInfo address{VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO};
address.buffer = arena.buffer;
arena.address = vk.vkGetBufferDeviceAddress(state_.info.device, &address);
Expand All @@ -5453,7 +5495,8 @@ class Device final
length = 0;
arena.log = getenv_s(&length, log, "LEONIDA_ARENA_LOG") == 0 && length > 1 && log[0] != '0';
state_.context->set_heap_base(arena.address);
state_.context->set_zero_page(arena.buffer, 0, kArenaReserved);
state_.context->set_zero_page(arena.buffer, 0, kZeroPage);
state_.context->set_quad_indices(arena.buffer, kZeroPage, kQuadIndexCount);
return true;
}

Expand Down
Loading
Loading