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
9 changes: 9 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,15 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(d3d12_depth_resolve_test PRIVATE LEONIDA_DEPTH_EDGE_VS_SPV="${binary}")
endif()

# Hardware depth-bounds testing (DEPTH_STENCIL1/2 DepthBoundsTestEnable + OMSetDepthBounds) against host D3D12.
add_executable(d3d12_depth_bounds_test test/d3d12_depth_bounds_test.cpp)
target_link_libraries(d3d12_depth_bounds_test PRIVATE gta6rt_d3d12_com d3d12 dxgi dxguid)
target_compile_definitions(d3d12_depth_bounds_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_depth_bounds_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_depth_bounds COMMAND d3d12_depth_bounds_test)
set_tests_properties(d3d12_depth_bounds PROPERTIES SKIP_RETURN_CODE 77
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")

# ID3D12Resource::GetDesc against the host runtime and WARP; run explicitly on a Vulkan GPU.
add_executable(d3d12_resource_desc_test test/d3d12_resource_desc_test.cpp)
target_link_libraries(d3d12_resource_desc_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
Expand Down
18 changes: 18 additions & 0 deletions runtime/shims/d3d12_com.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ class GraphicsCommandList final
front_stencil_ref_ = back_stencil_ref_ = reference;
}

// ID3D12GraphicsCommandList1: dynamic depth-bounds range for PSOs created with DepthBoundsTestEnable = TRUE.
// Not allowed in a bundle (D3D12 "Recording command lists and bundles").
void STDMETHODCALLTYPE OMSetDepthBounds(FLOAT min_depth, FLOAT max_depth) override {
d3d12_trace::record("ID3D12GraphicsCommandList1::OMSetDepthBounds");
if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false);
if (list_.closed()) return;
depth_bounds_min_ = min_depth;
depth_bounds_max_ = max_depth;
}

void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override {
d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced");
if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); },
Expand Down Expand Up @@ -490,6 +500,8 @@ class GraphicsCommandList final
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_,
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0};
draw.mesh = true;
draw.depth_bounds_min = depth_bounds_min_;
draw.depth_bounds_max = depth_bounds_max_;
check(list_.draw(std::move(draw)));
}

Expand Down Expand Up @@ -1274,6 +1286,8 @@ class GraphicsCommandList final
bool draw_views_stale_ = true;
leonida::TargetView depth_{};
std::array<float, 4> blend_factor_{1, 1, 1, 1};
float depth_bounds_min_ = 0.0f;
float depth_bounds_max_ = 1.0f;

// The root signatures and pipeline states this recording uses. D3D12 lets an application release them right
// after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root
Expand Down Expand Up @@ -1373,6 +1387,8 @@ class GraphicsCommandList final
draw_views_stale_ = true;
depth_ = {};
blend_factor_ = {1, 1, 1, 1};
depth_bounds_min_ = 0.0f;
depth_bounds_max_ = 1.0f;
set_pipeline(state);
}

Expand Down Expand Up @@ -1418,6 +1434,8 @@ class GraphicsCommandList final
leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_,
scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances,
first, base_vertex, first_instance};
draw.depth_bounds_min = depth_bounds_min_;
draw.depth_bounds_max = depth_bounds_max_;
check(list_.draw(std::move(draw)));
}
};
Expand Down
2 changes: 1 addition & 1 deletion runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct VulkanDeviceInfo {
// D3D12_MESH_SHADER_TIER_1; without it mesh PSOs are refused and the tier is NOT_SUPPORTED.
bool mesh_shader = false;
// The core features enabled on `device`. Graphics PSOs that need independentBlend, fillModeNonSolid,
// depthBiasClamp or dualSrcBlend are refused unless it is set here.
// depthBiasClamp, dualSrcBlend, logicOp, depthClamp or depthBounds are refused unless it is set here.
VkPhysicalDeviceFeatures features{};
// Vulkan 1.2 shaderOutputLayer / shaderOutputViewportIndex enabled on `device`: a translated vertex shader that
// writes the layer or viewport declares ShaderLayer / ShaderViewportIndex (the index's `features`).
Expand Down
61 changes: 31 additions & 30 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3111,6 +3111,7 @@ struct PipelineStream {
D3D12_SHADER_BYTECODE as{};
bool has_cs = false;
bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS
bool depth_bounds_test = false;
};

HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, PipelineStream& out) {
Expand Down Expand Up @@ -3220,10 +3221,7 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe
case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: {
D3D12_DEPTH_STENCIL_DESC1 d{};
take(d);
if (ok && d.DepthBoundsTestEnable) {
log_once("depth-bounds tests are not supported yet; stream subobject", int(type));
return E_NOTIMPL;
}
if (ok) out.depth_bounds_test = d.DepthBoundsTestEnable != FALSE;
g.DepthStencilState = {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, d.StencilReadMask,
d.StencilWriteMask, d.FrontFace, d.BackFace};
break;
Expand All @@ -3232,10 +3230,7 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe
D3D12_DEPTH_STENCIL_DESC2 d{};
take(d);
if (!ok) break;
if (d.DepthBoundsTestEnable) {
log_once("depth-bounds tests are not supported yet; stream subobject", int(type));
return E_NOTIMPL;
}
out.depth_bounds_test = d.DepthBoundsTestEnable != FALSE;
// Separate front and back stencil masks are not modelled (one read and one write mask per PSO).
if (d.StencilEnable && (d.FrontFace.StencilReadMask != d.BackFace.StencilReadMask ||
d.FrontFace.StencilWriteMask != d.BackFace.StencilWriteMask)) {
Expand Down Expand Up @@ -3389,7 +3384,7 @@ class PipelineState final
// no VS, no vertex input or input assembly (the input layout, topology type and strip cut are ignored, as D3D12
// does for mesh PSOs), drawn by DispatchMesh. An AS (amplification shader) is refused: the game has none.
HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr,
const D3D12_SHADER_BYTECODE* as = nullptr) {
const D3D12_SHADER_BYTECODE* as = nullptr, bool depth_bounds_test = false) {
auto& vk = device_.context->vk;
const VkDevice dev = device_.info.device;
const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength;
Expand Down Expand Up @@ -3776,6 +3771,9 @@ class PipelineState final
depth.depthTestEnable = ds.DepthEnable;
depth.depthWriteEnable = ds.DepthEnable && ds.DepthWriteMask == D3D12_DEPTH_WRITE_MASK_ALL;
depth.depthCompareOp = compare_op(ds.DepthFunc);
depth.depthBoundsTestEnable = depth_bounds_test ? VK_TRUE : VK_FALSE;
depth.minDepthBounds = 0.0f;
depth.maxDepthBounds = 1.0f;
depth.stencilTestEnable = ds.StencilEnable;
depth.front = stencil_face(ds.FrontFace, ds.StencilReadMask, ds.StencilWriteMask);
depth.back = stencil_face(ds.BackFace, ds.StencilReadMask, ds.StencilWriteMask);
Expand Down Expand Up @@ -3849,6 +3847,9 @@ class PipelineState final
return refuse("logic ops need logicOp, which the device lacks; op", int(first.LogicOp));
if (raster.depthClampEnable && !features.depthClamp)
return refuse("DepthClipEnable = FALSE needs depthClamp, which the device lacks; flag", 0);
if (depth.depthBoundsTestEnable && !features.depthBounds)
return refuse("DepthBoundsTestEnable = TRUE needs depthBounds, which the device lacks; flag", 1);
graphics_.depth_bounds = depth_bounds_test;

graphics_.color_formats.assign(desc.NumRenderTargets, VK_FORMAT_UNDEFINED);
for (UINT i = 0; i < desc.NumRenderTargets; ++i) {
Expand Down Expand Up @@ -4103,14 +4104,6 @@ class PipelineState final

// The graphics pipeline with the formats of the `unbound` slots (leonida::GraphicsPipeline::variant) undefined.
VkPipeline build(std::uint32_t unbound) const {
// The stride state is last: a pipeline without vertex bindings (a full-screen pass that builds its vertices
// from SV_VertexID) leaves it out, since no vkCmdBindVertexBuffers2 would ever set it
// (VUID-vkCmdDraw-pStrides-04913).
static constexpr VkDynamicState dynamic_states[] = {
VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY, VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE,
VK_DYNAMIC_STATE_STENCIL_REFERENCE, VK_DYNAMIC_STATE_BLEND_CONSTANTS,
VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE};
const Recipe& r = recipe_;
std::vector<VkFormat> colors = graphics_.color_formats;
for (std::size_t slot = 0; slot < colors.size(); ++slot)
Expand Down Expand Up @@ -4155,17 +4148,23 @@ class PipelineState final
blend.pAttachments = r.blends.data();
// A mesh pipeline takes neither the topology nor the strides as dynamic state
// (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066).
static constexpr VkDynamicState mesh_dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR,
VK_DYNAMIC_STATE_STENCIL_REFERENCE,
VK_DYNAMIC_STATE_BLEND_CONSTANTS};
// A pipeline without vertex bindings leaves out VERTEX_INPUT_BINDING_STRIDE (VUID-vkCmdDraw-pStrides-04913).
// DEPTH_BOUNDS is added only when depthBoundsTestEnable is set so non-bounds pipelines need not set it.
VkDynamicState dyn_list[8]{};
std::uint32_t dyn_count = 0;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VIEWPORT;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_SCISSOR;
if (!r.mesh) {
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE;
}
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE;
dyn_list[dyn_count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
if (!r.mesh && !r.bindings.empty()) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE;
if (r.depth.depthBoundsTestEnable) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS;
VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO};
if (r.mesh) {
dynamic.dynamicStateCount = std::uint32_t(std::size(mesh_dynamic_states));
dynamic.pDynamicStates = mesh_dynamic_states;
} else {
dynamic.dynamicStateCount = std::uint32_t(std::size(dynamic_states)) - (r.bindings.empty() ? 1 : 0);
dynamic.pDynamicStates = dynamic_states;
}
dynamic.dynamicStateCount = dyn_count;
dynamic.pDynamicStates = dyn_list;

VkGraphicsPipelineCreateInfo info{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &rendering};
info.stageCount = std::uint32_t(r.stages.size());
Expand Down Expand Up @@ -6183,8 +6182,9 @@ class Device final
if (const HRESULT hr = parse_pipeline_stream(*desc, stream); FAILED(hr)) return d3d12_trace::result(method, hr);
auto* pipeline = new PipelineState(state_);
pipeline->attach_device(identity());
const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute)
: pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as);
const HRESULT hr =
stream.has_cs ? pipeline->create_compute(stream.compute)
: pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, stream.depth_bounds_test);
if (FAILED(hr)) {
pipeline->Release();
return d3d12_trace::result(method, hr);
Expand Down Expand Up @@ -7645,7 +7645,7 @@ class Device final
if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS2)) return d3d12_trace::result(method, E_INVALIDARG);
auto* opts = static_cast<D3D12_FEATURE_DATA_D3D12_OPTIONS2*>(data);
*opts = {};
opts->DepthBoundsTestSupported = FALSE;
opts->DepthBoundsTestSupported = state_.info.features.depthBounds ? TRUE : FALSE;
opts->ProgrammableSamplePositionsTier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED;
return d3d12_trace::result(method, S_OK);
}
Expand Down Expand Up @@ -9796,6 +9796,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
enable.dualSrcBlend = chosen_features.dualSrcBlend;
enable.logicOp = chosen_features.logicOp;
enable.depthClamp = chosen_features.depthClamp;
enable.depthBounds = chosen_features.depthBounds;
enable.geometryShader = chosen_features.geometryShader; // translated geometry shaders (195 in the corpus)
// What translated shaders declare (the index's `features`): 64-bit integers in most of the corpus (25,803 of
// 26,503 entries), doubles, 16-bit integers, clip and cull distances in a few vertex shaders, image stores
Expand Down
2 changes: 2 additions & 0 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ struct Draw {
std::int32_t base_vertex = 0;
std::uint32_t first_instance = 0;
bool mesh = false;
float depth_bounds_min = 0.0f;
float depth_bounds_max = 1.0f;
};

// D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT.
Expand Down
7 changes: 4 additions & 3 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct VulkanDeviceFunctions {
X(vkCmdPushConstants) X(vkCmdDispatch) \
X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \
X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \
X(vkCmdSetBlendConstants) X(vkCmdDraw) \
X(vkCmdSetBlendConstants) X(vkCmdSetDepthBounds) X(vkCmdDraw) \
X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \
X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \
X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \
Expand Down Expand Up @@ -133,8 +133,8 @@ struct VulkanDeviceFunctions {
vkCreateShaderModule && vkDestroyShaderModule && vkCreatePipelineLayout &&
vkDestroyPipelineLayout && vkCreateComputePipelines && vkDestroyPipeline &&
vkGetBufferDeviceAddress && vkCmdBindIndexBuffer && vkCmdSetViewport && vkCmdSetScissor &&
vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdDraw && vkCmdDrawIndexed &&
vkCreateGraphicsPipelines;
vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdSetDepthBounds && vkCmdDraw &&
vkCmdDrawIndexed && vkCreateGraphicsPipelines;
}

// Graphics pipelines also need these Vulkan 1.3 entry points (dynamic rendering, extended dynamic state).
Expand Down Expand Up @@ -974,6 +974,7 @@ class VulkanContext {
replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable;
replay_.set_stencil_reference = vk.vkCmdSetStencilReference;
replay_.set_blend_constants = vk.vkCmdSetBlendConstants;
replay_.set_depth_bounds = vk.vkCmdSetDepthBounds;
replay_.draw = vk.vkCmdDraw;
replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled
replay_.draw_indexed = vk.vkCmdDrawIndexed;
Expand Down
12 changes: 12 additions & 0 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ struct GraphicsPipeline {
std::uint32_t zero_step_slots = 0; // per-instance slots with InstanceDataStepRate 0: stride 0 at draw time
std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE (0: off, 1: 0xFFFF, 2: 0xFFFFFFFF)
bool mesh = false; // a mesh shader instead of a vertex shader: drawn by DispatchMesh only
bool depth_bounds = false; // VK_DYNAMIC_STATE_DEPTH_BOUNDS enabled; draw applies OMSetDepthBounds
// The same pipeline with some attachment formats VK_FORMAT_UNDEFINED, for a draw that leaves those slots
// unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth
// and stencil. Null when it cannot be built.
Expand Down Expand Up @@ -854,6 +855,7 @@ struct ReplayFunctions {
PFN_vkCmdSetPrimitiveRestartEnable set_primitive_restart_enable = nullptr;
PFN_vkCmdSetStencilReference set_stencil_reference = nullptr;
PFN_vkCmdSetBlendConstants set_blend_constants = nullptr;
PFN_vkCmdSetDepthBounds set_depth_bounds = nullptr;
PFN_vkCmdDraw draw = nullptr;
PFN_vkCmdDrawMeshTasksEXT draw_mesh_tasks = nullptr; // VK_EXT_mesh_shader; null: mesh draws are unsupported
PFN_vkCmdDrawIndexed draw_indexed = nullptr;
Expand Down Expand Up @@ -2103,6 +2105,7 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct
if (!pipeline || !pipeline->pipeline) return ReplayResult::unresolved_resource;
if (pipeline->mesh != draw.mesh) return ReplayResult::invalid_draw;
if (draw.mesh && !vk.draw_mesh_tasks) return ReplayResult::unsupported;
if (pipeline->depth_bounds && !vk.set_depth_bounds) return ReplayResult::unsupported;
if (pipeline->bindless_set && !vk.bind_descriptor_sets) return ReplayResult::unsupported;
if (pipeline->color_formats.size() > max_render_targets) return ReplayResult::unsupported;
p.graphics = pipeline;
Expand Down Expand Up @@ -4200,6 +4203,15 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_FRONT_BIT, draw->stencil_front);
vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_BACK_BIT, draw->stencil_back);
vk.set_blend_constants(command_buffer, draw->blend_factor.data());
if (graphics.depth_bounds) {
const float lo = (draw->depth_bounds_min == draw->depth_bounds_min)
? std::clamp(draw->depth_bounds_min, 0.0f, 1.0f)
: 0.0f;
const float hi = (draw->depth_bounds_max == draw->depth_bounds_max)
? std::clamp(draw->depth_bounds_max, 0.0f, 1.0f)
: 1.0f;
vk.set_depth_bounds(command_buffer, lo, hi);
}
for (std::uint32_t slot = 0; slot < d.vertex_buffers.size(); ++slot) {
if (!d.vertex_buffers[slot]) continue;
vk.bind_vertex_buffers2(command_buffer, slot, 1, &d.vertex_buffers[slot], &d.vertex_offsets[slot],
Expand Down
Loading