renderer_vulkan: Properly implement depth clamp and clip

This commit is contained in:
IndecisiveTurtle 2025-07-14 02:42:41 +03:00
parent 4cc2220f2f
commit 20ab2cabc9
8 changed files with 168 additions and 46 deletions

View File

@ -516,6 +516,11 @@ struct Liverpool {
BitField<24, 1, u32> dx_linear_attr_clip_enable; BitField<24, 1, u32> dx_linear_attr_clip_enable;
BitField<26, 1, u32> zclip_near_disable; BitField<26, 1, u32> zclip_near_disable;
BitField<27, 1, u32> zclip_far_disable; BitField<27, 1, u32> zclip_far_disable;
bool ZclipEnable() const {
ASSERT(zclip_near_disable == zclip_far_disable);
return !zclip_near_disable;
}
}; };
enum class PolygonMode : u32 { enum class PolygonMode : u32 {

View File

@ -109,18 +109,28 @@ GraphicsPipeline::GraphicsPipeline(
.patchControlPoints = is_rect_list ? 3U : (is_quad_list ? 4U : key.patch_control_points), .patchControlPoints = is_rect_list ? 3U : (is_quad_list ? 4U : key.patch_control_points),
}; };
const vk::PipelineRasterizationProvokingVertexStateCreateInfoEXT provoking_vertex_state = { vk::StructureChain raster_chain = {
.provokingVertexMode = key.provoking_vtx_last == Liverpool::ProvokingVtxLast::First vk::PipelineRasterizationStateCreateInfo{
? vk::ProvokingVertexModeEXT::eFirstVertex .depthClampEnable = key.depth_clamp_enable,
: vk::ProvokingVertexModeEXT::eLastVertex}; .rasterizerDiscardEnable = false,
.polygonMode = LiverpoolToVK::PolygonMode(key.polygon_mode),
.lineWidth = 1.0f,
},
vk::PipelineRasterizationProvokingVertexStateCreateInfoEXT{
.provokingVertexMode = key.provoking_vtx_last == Liverpool::ProvokingVtxLast::First
? vk::ProvokingVertexModeEXT::eFirstVertex
: vk::ProvokingVertexModeEXT::eLastVertex,
},
vk::PipelineRasterizationDepthClipStateCreateInfoEXT{
.depthClipEnable = key.depth_clip_enable,
}};
const vk::PipelineRasterizationStateCreateInfo raster_state = { if (!instance.IsProvokingVertexSupported()) {
.pNext = instance.IsProvokingVertexSupported() ? &provoking_vertex_state : nullptr, raster_chain.unlink<vk::PipelineRasterizationProvokingVertexStateCreateInfoEXT>();
.depthClampEnable = false, }
.rasterizerDiscardEnable = false, if (!instance.IsDepthClipEnableSupported()) {
.polygonMode = LiverpoolToVK::PolygonMode(key.polygon_mode), raster_chain.unlink<vk::PipelineRasterizationDepthClipStateCreateInfoEXT>();
.lineWidth = 1.0f, }
};
const vk::PipelineMultisampleStateCreateInfo multisampling = { const vk::PipelineMultisampleStateCreateInfo multisampling = {
.rasterizationSamples = .rasterizationSamples =
@ -128,15 +138,32 @@ GraphicsPipeline::GraphicsPipeline(
.sampleShadingEnable = false, .sampleShadingEnable = false,
}; };
const vk::PipelineViewportDepthClipControlCreateInfoEXT clip_control = { const vk::DepthClampRangeEXT depth_clamp_range = {
.negativeOneToOne = key.clip_space == Liverpool::ClipSpace::MinusWToW, .minDepthClamp = key.min_depth_clamp,
.maxDepthClamp = key.max_depth_clamp,
}; };
const vk::PipelineViewportStateCreateInfo viewport_info = { vk::StructureChain viewport_chain = {
.pNext = instance.IsDepthClipControlSupported() ? &clip_control : nullptr, vk::PipelineViewportStateCreateInfo{},
vk::PipelineViewportDepthClipControlCreateInfoEXT{
.negativeOneToOne = key.clip_space == Liverpool::ClipSpace::MinusWToW,
},
vk::PipelineViewportDepthClampControlCreateInfoEXT{
.depthClampMode = key.depth_clamp_user_defined_range
? vk::DepthClampModeEXT::eUserDefinedRange
: vk::DepthClampModeEXT::eViewportRange,
.pDepthClampRange = &depth_clamp_range,
},
}; };
boost::container::static_vector<vk::DynamicState, 20> dynamic_states = { if (!instance.IsDepthClampControlSupported()) {
viewport_chain.unlink<vk::PipelineViewportDepthClampControlCreateInfoEXT>();
}
if (!instance.IsDepthClipControlSupported()) {
viewport_chain.unlink<vk::PipelineViewportDepthClipControlCreateInfoEXT>();
}
boost::container::static_vector<vk::DynamicState, 32> dynamic_states = {
vk::DynamicState::eViewportWithCount, vk::DynamicState::eScissorWithCount, vk::DynamicState::eViewportWithCount, vk::DynamicState::eScissorWithCount,
vk::DynamicState::eBlendConstants, vk::DynamicState::eDepthTestEnable, vk::DynamicState::eBlendConstants, vk::DynamicState::eDepthTestEnable,
vk::DynamicState::eDepthWriteEnable, vk::DynamicState::eDepthCompareOp, vk::DynamicState::eDepthWriteEnable, vk::DynamicState::eDepthCompareOp,
@ -227,11 +254,19 @@ GraphicsPipeline::GraphicsPipeline(
}); });
} }
const auto depth_format =
instance.GetSupportedFormat(LiverpoolToVK::DepthFormat(key.z_format, key.stencil_format),
vk::FormatFeatureFlagBits2::eDepthStencilAttachment);
const vk::PipelineRenderingCreateInfo pipeline_rendering_ci = { const vk::PipelineRenderingCreateInfo pipeline_rendering_ci = {
.colorAttachmentCount = key.num_color_attachments, .colorAttachmentCount = key.num_color_attachments,
.pColorAttachmentFormats = key.color_formats.data(), .pColorAttachmentFormats = key.color_formats.data(),
.depthAttachmentFormat = key.depth_format, .depthAttachmentFormat = key.z_format != Liverpool::DepthBuffer::ZFormat::Invalid
.stencilAttachmentFormat = key.stencil_format, ? depth_format
: vk::Format::eUndefined,
.stencilAttachmentFormat =
key.stencil_format != Liverpool::DepthBuffer::StencilFormat::Invalid
? depth_format
: vk::Format::eUndefined,
}; };
std::array<vk::PipelineColorBlendAttachmentState, Liverpool::NumColorBuffers> attachments; std::array<vk::PipelineColorBlendAttachmentState, Liverpool::NumColorBuffers> attachments;
@ -301,8 +336,8 @@ GraphicsPipeline::GraphicsPipeline(
.pVertexInputState = !instance.IsVertexInputDynamicState() ? &vertex_input_info : nullptr, .pVertexInputState = !instance.IsVertexInputDynamicState() ? &vertex_input_info : nullptr,
.pInputAssemblyState = &input_assembly, .pInputAssemblyState = &input_assembly,
.pTessellationState = &tessellation_state, .pTessellationState = &tessellation_state,
.pViewportState = &viewport_info, .pViewportState = &viewport_chain.get(),
.pRasterizationState = &raster_state, .pRasterizationState = &raster_chain.get(),
.pMultisampleState = &multisampling, .pMultisampleState = &multisampling,
.pColorBlendState = &color_blending, .pColorBlendState = &color_blending,
.pDynamicState = &dynamic_info, .pDynamicState = &dynamic_info,

View File

@ -44,13 +44,20 @@ struct GraphicsPipelineKey {
Liverpool::ColorControl::LogicOp logic_op; Liverpool::ColorControl::LogicOp logic_op;
u32 num_samples; u32 num_samples;
u32 mrt_mask; u32 mrt_mask;
vk::Format depth_format; struct {
vk::Format stencil_format; Liverpool::DepthBuffer::ZFormat z_format : 2;
Liverpool::DepthBuffer::StencilFormat stencil_format : 1;
u32 depth_clamp_enable : 1;
u32 depth_clamp_user_defined_range : 1;
float min_depth_clamp;
float max_depth_clamp;
};
struct { struct {
AmdGpu::PrimitiveType prim_type : 5; AmdGpu::PrimitiveType prim_type : 5;
Liverpool::PolygonMode polygon_mode : 2; Liverpool::PolygonMode polygon_mode : 2;
Liverpool::ClipSpace clip_space : 1; Liverpool::ClipSpace clip_space : 1;
Liverpool::ProvokingVtxLast provoking_vtx_last : 1; Liverpool::ProvokingVtxLast provoking_vtx_last : 1;
u32 depth_clip_enable : 1;
}; };
bool operator==(const GraphicsPipelineKey& key) const noexcept { bool operator==(const GraphicsPipelineKey& key) const noexcept {

View File

@ -270,6 +270,8 @@ bool Instance::CreateDevice() {
} }
custom_border_color = add_extension(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME); custom_border_color = add_extension(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
depth_clip_control = add_extension(VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME); depth_clip_control = add_extension(VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME);
depth_clip_enable = add_extension(VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME);
depth_clamp_control = add_extension(VK_EXT_DEPTH_CLAMP_CONTROL_EXTENSION_NAME);
vertex_input_dynamic_state = add_extension(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME); vertex_input_dynamic_state = add_extension(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
list_restart = add_extension(VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME); list_restart = add_extension(VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME);
fragment_shader_barycentric = add_extension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME); fragment_shader_barycentric = add_extension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME);
@ -363,6 +365,7 @@ bool Instance::CreateDevice() {
.dualSrcBlend = features.dualSrcBlend, .dualSrcBlend = features.dualSrcBlend,
.logicOp = features.logicOp, .logicOp = features.logicOp,
.multiDrawIndirect = features.multiDrawIndirect, .multiDrawIndirect = features.multiDrawIndirect,
.depthClamp = features.depthClamp,
.depthBiasClamp = features.depthBiasClamp, .depthBiasClamp = features.depthBiasClamp,
.fillModeNonSolid = features.fillModeNonSolid, .fillModeNonSolid = features.fillModeNonSolid,
.depthBounds = features.depthBounds, .depthBounds = features.depthBounds,
@ -419,6 +422,12 @@ bool Instance::CreateDevice() {
vk::PhysicalDeviceDepthClipControlFeaturesEXT{ vk::PhysicalDeviceDepthClipControlFeaturesEXT{
.depthClipControl = true, .depthClipControl = true,
}, },
vk::PhysicalDeviceDepthClipEnableFeaturesEXT{
.depthClipEnable = true,
},
vk::PhysicalDeviceDepthClampControlFeaturesEXT{
.depthClampControl = true,
},
vk::PhysicalDeviceRobustness2FeaturesEXT{ vk::PhysicalDeviceRobustness2FeaturesEXT{
.robustBufferAccess2 = robustness2_features.robustBufferAccess2, .robustBufferAccess2 = robustness2_features.robustBufferAccess2,
.robustImageAccess2 = robustness2_features.robustImageAccess2, .robustImageAccess2 = robustness2_features.robustImageAccess2,
@ -491,6 +500,12 @@ bool Instance::CreateDevice() {
if (!depth_clip_control) { if (!depth_clip_control) {
device_chain.unlink<vk::PhysicalDeviceDepthClipControlFeaturesEXT>(); device_chain.unlink<vk::PhysicalDeviceDepthClipControlFeaturesEXT>();
} }
if (!depth_clip_enable) {
device_chain.unlink<vk::PhysicalDeviceDepthClipEnableFeaturesEXT>();
}
if (!depth_clamp_control) {
device_chain.unlink<vk::PhysicalDeviceDepthClampControlFeaturesEXT>();
}
if (!robustness2) { if (!robustness2) {
device_chain.unlink<vk::PhysicalDeviceRobustness2FeaturesEXT>(); device_chain.unlink<vk::PhysicalDeviceRobustness2FeaturesEXT>();
} }

View File

@ -109,6 +109,16 @@ public:
return depth_clip_control; return depth_clip_control;
} }
/// Returns true when VK_EXT_depth_clip_enable is supported
bool IsDepthClipEnableSupported() const {
return depth_clip_enable;
}
/// Returns true when VK_EXT_depth_clamp_control is supported
bool IsDepthClampControlSupported() const {
return depth_clamp_control;
}
/// Returns true when VK_EXT_depth_range_unrestricted is supported /// Returns true when VK_EXT_depth_range_unrestricted is supported
bool IsDepthRangeUnrestrictedSupported() const { bool IsDepthRangeUnrestrictedSupported() const {
return depth_range_unrestricted; return depth_range_unrestricted;
@ -409,6 +419,8 @@ private:
bool custom_border_color{}; bool custom_border_color{};
bool fragment_shader_barycentric{}; bool fragment_shader_barycentric{};
bool depth_clip_control{}; bool depth_clip_control{};
bool depth_clip_enable{};
bool depth_clamp_control{};
bool depth_range_unrestricted{}; bool depth_range_unrestricted{};
bool dynamic_state_3{}; bool dynamic_state_3{};
bool vertex_input_dynamic_state{}; bool vertex_input_dynamic_state{};

View File

@ -285,28 +285,21 @@ bool PipelineCache::RefreshGraphicsKey() {
auto& regs = liverpool->regs; auto& regs = liverpool->regs;
auto& key = graphics_key; auto& key = graphics_key;
const auto depth_format = instance.GetSupportedFormat( key.z_format = regs.depth_buffer.DepthValid() ? regs.depth_buffer.z_info.format
LiverpoolToVK::DepthFormat(regs.depth_buffer.z_info.format, : Liverpool::DepthBuffer::ZFormat::Invalid;
regs.depth_buffer.stencil_info.format), key.stencil_format = regs.depth_buffer.StencilValid()
vk::FormatFeatureFlagBits2::eDepthStencilAttachment); ? regs.depth_buffer.stencil_info.format
if (regs.depth_buffer.DepthValid()) { : Liverpool::DepthBuffer::StencilFormat::Invalid;
key.depth_format = depth_format; key.depth_clip_enable = regs.clipper_control.ZclipEnable();
} else {
key.depth_format = vk::Format::eUndefined;
}
if (regs.depth_buffer.StencilValid()) {
key.stencil_format = depth_format;
} else {
key.stencil_format = vk::Format::eUndefined;
}
key.prim_type = regs.primitive_type;
key.polygon_mode = regs.polygon_control.PolyMode();
key.clip_space = regs.clipper_control.clip_space; key.clip_space = regs.clipper_control.clip_space;
key.provoking_vtx_last = regs.polygon_control.provoking_vtx_last; key.provoking_vtx_last = regs.polygon_control.provoking_vtx_last;
key.prim_type = regs.primitive_type;
key.polygon_mode = regs.polygon_control.PolyMode();
key.logic_op = regs.color_control.rop3; key.logic_op = regs.color_control.rop3;
key.num_samples = regs.NumSamples(); key.num_samples = regs.NumSamples();
RefreshDepthClampRange();
const bool skip_cb_binding = const bool skip_cb_binding =
regs.color_control.mode == AmdGpu::Liverpool::ColorControl::OperationMode::Disable; regs.color_control.mode == AmdGpu::Liverpool::ColorControl::OperationMode::Disable;
@ -493,7 +486,66 @@ bool PipelineCache::RefreshGraphicsKey() {
} }
return true; return true;
} // namespace Vulkan }
void PipelineCache::RefreshDepthClampRange() {
auto& regs = liverpool->regs;
auto& key = graphics_key;
bool depth_clamp_can_use_viewport_range = true;
bool depth_clamp_is_same_on_all_viewports = true;
float zmin = std::numeric_limits<float>::max();
float zmax = std::numeric_limits<float>::max();
const auto& vp_ctl = regs.viewport_control;
for (u32 i = 0; i < Liverpool::NumViewports; i++) {
const auto& vp = regs.viewports[i];
const auto& vp_d = regs.viewport_depths[i];
if (vp.xscale == 0) {
continue;
}
const auto zoffset = vp_ctl.zoffset_enable ? vp.zoffset : 0.f;
const auto zscale = vp_ctl.zscale_enable ? vp.zscale : 1.f;
float min_depth;
float max_depth;
if (regs.clipper_control.clip_space == AmdGpu::Liverpool::ClipSpace::MinusWToW) {
min_depth = zoffset - zscale;
max_depth = zoffset + zscale;
} else {
min_depth = zoffset;
max_depth = zoffset + zscale;
}
if (zmin == std::numeric_limits<float>::max()) {
zmin = vp_d.zmin;
zmax = vp_d.zmax;
}
depth_clamp_is_same_on_all_viewports &= (zmin == vp_d.zmin && zmax == vp_d.zmax);
depth_clamp_can_use_viewport_range &= (min_depth == vp_d.zmin && max_depth == vp_d.zmax);
}
if (zmin == std::numeric_limits<float>::max()) {
return;
}
if (!depth_clamp_can_use_viewport_range && !depth_clamp_is_same_on_all_viewports) {
LOG_ERROR(Render_Vulkan,
"Viewport depth clamping configuration cannot be accurately emulated");
} else if (!depth_clamp_can_use_viewport_range && !instance.IsDepthClampControlSupported()) {
LOG_WARNING(
Render_Vulkan,
"Viewport uses custom clamp range zmin={}, zmax={} which cannot be accurately emulated",
zmin, zmax);
}
key.depth_clamp_enable = !regs.depth_render_override.disable_viewport_clamp;
if (key.depth_clamp_enable) {
key.depth_clamp_user_defined_range = !depth_clamp_can_use_viewport_range;
if (key.depth_clamp_user_defined_range) {
key.min_depth_clamp = zmin;
key.max_depth_clamp = zmax;
}
}
}
bool PipelineCache::RefreshComputeKey() { bool PipelineCache::RefreshComputeKey() {
Shader::Backend::Bindings binding{}; Shader::Backend::Bindings binding{};

View File

@ -76,6 +76,8 @@ private:
bool RefreshGraphicsKey(); bool RefreshGraphicsKey();
bool RefreshComputeKey(); bool RefreshComputeKey();
void RefreshDepthClampRange();
void DumpShader(std::span<const u32> code, u64 hash, Shader::Stage stage, size_t perm_idx, void DumpShader(std::span<const u32> code, u64 hash, Shader::Stage stage, size_t perm_idx,
std::string_view ext); std::string_view ext);
std::optional<std::vector<u32>> GetShaderPatch(u64 hash, Shader::Stage stage, size_t perm_idx, std::optional<std::vector<u32>> GetShaderPatch(u64 hash, Shader::Stage stage, size_t perm_idx,

View File

@ -1086,12 +1086,6 @@ void Rasterizer::UpdateViewportScissorState() const {
viewport.maxDepth = zoffset + zscale; viewport.maxDepth = zoffset + zscale;
} }
if (!regs.depth_render_override.disable_viewport_clamp) {
// Apply depth clamp.
viewport.minDepth = std::max(viewport.minDepth, vp_d.zmin);
viewport.maxDepth = std::min(viewport.maxDepth, vp_d.zmax);
}
if (!instance.IsDepthRangeUnrestrictedSupported()) { if (!instance.IsDepthRangeUnrestrictedSupported()) {
// Unrestricted depth range not supported by device. Restrict to valid range. // Unrestricted depth range not supported by device. Restrict to valid range.
viewport.minDepth = std::max(viewport.minDepth, 0.f); viewport.minDepth = std::max(viewport.minDepth, 0.f);