From 650880105e6d2d6f5b507a9353cc1247f910b0df Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Thu, 29 Sep 2022 11:55:03 -0500 Subject: [PATCH] vulkan,lavapipe: Use a tri-state enum for depth clip enable This should make it a lot more clear how depth clip enables work. Annoyingly, because of the way they originally worked in Vulkan 1.0, it's dependent on the depth clamp if the state isn't set in the pipeline and isn't declared dynamic. The enum is explicitly set up so that drivers don't need to be aware of this change unless they already implement VK_EXT_extended_dynamic_state3. If depth clamp/clamp are not dynamic, depth clip will be either TRUE or FALSE which map to 1/0 so the field can still be treated as a boolean. Reviewed-by: Lionel Landwerlin Reviewed-By: Mike Blumenkrantz Reviewed-by: Roland Scheidegger Part-of: --- src/gallium/frontends/lavapipe/lvp_execute.c | 9 +++---- src/intel/vulkan/anv_pipeline.c | 3 ++- src/intel/vulkan_hasvk/anv_pipeline.c | 3 ++- src/vulkan/runtime/vk_graphics_state.c | 20 ++++++++++----- src/vulkan/runtime/vk_graphics_state.h | 27 +++++++++++++++++--- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/gallium/frontends/lavapipe/lvp_execute.c b/src/gallium/frontends/lavapipe/lvp_execute.c index 0c03f2d0203..8aadb827678 100644 --- a/src/gallium/frontends/lavapipe/lvp_execute.c +++ b/src/gallium/frontends/lavapipe/lvp_execute.c @@ -741,11 +741,10 @@ static void handle_graphics_pipeline(struct vk_cmd_queue_entry *cmd, if (BITSET_TEST(ps->dynamic, MESA_VK_DYNAMIC_RS_DEPTH_CLIP_ENABLE)) { state->depth_clamp_sets_clip = false; } else { - if (!ps->rs->depth_clip_present) - state->rs_state.depth_clip_near = state->rs_state.depth_clip_far = !state->rs_state.depth_clamp; - else - state->rs_state.depth_clip_near = state->rs_state.depth_clip_far = ps->rs->depth_clip_enable; - state->depth_clamp_sets_clip = !ps->rs->depth_clip_present; + state->rs_state.depth_clip_near = state->rs_state.depth_clip_far = + vk_rasterization_state_depth_clip_enable(ps->rs); + state->depth_clamp_sets_clip = + ps->rs->depth_clip_enable == VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP; } if (!BITSET_TEST(ps->dynamic, MESA_VK_DYNAMIC_RS_RASTERIZER_DISCARD_ENABLE)) diff --git a/src/intel/vulkan/anv_pipeline.c b/src/intel/vulkan/anv_pipeline.c index 8e58c5ff47e..997b4cda4b3 100644 --- a/src/intel/vulkan/anv_pipeline.c +++ b/src/intel/vulkan/anv_pipeline.c @@ -2253,7 +2253,8 @@ anv_graphics_pipeline_init(struct anv_graphics_pipeline *pipeline, vk_dynamic_graphics_state_fill(&pipeline->dynamic_state, state); pipeline->depth_clamp_enable = state->rs->depth_clamp_enable; - pipeline->depth_clip_enable = state->rs->depth_clip_enable; + pipeline->depth_clip_enable = + vk_rasterization_state_depth_clip_enable(state->rs); pipeline->view_mask = state->rp->view_mask; result = anv_graphics_pipeline_compile(pipeline, cache, pCreateInfo, state); diff --git a/src/intel/vulkan_hasvk/anv_pipeline.c b/src/intel/vulkan_hasvk/anv_pipeline.c index 1a65fabbe29..4d5aaddf1ba 100644 --- a/src/intel/vulkan_hasvk/anv_pipeline.c +++ b/src/intel/vulkan_hasvk/anv_pipeline.c @@ -1892,7 +1892,8 @@ anv_graphics_pipeline_init(struct anv_graphics_pipeline *pipeline, vk_dynamic_graphics_state_fill(&pipeline->dynamic_state, state); pipeline->depth_clamp_enable = state->rs->depth_clamp_enable; - pipeline->depth_clip_enable = state->rs->depth_clip_enable; + pipeline->depth_clip_enable = + vk_rasterization_state_depth_clip_enable(state->rs); pipeline->view_mask = state->rp->view_mask; result = anv_graphics_pipeline_compile(pipeline, cache, pCreateInfo, state); diff --git a/src/vulkan/runtime/vk_graphics_state.c b/src/vulkan/runtime/vk_graphics_state.c index 4bf35851720..19257ab3dcb 100644 --- a/src/vulkan/runtime/vk_graphics_state.c +++ b/src/vulkan/runtime/vk_graphics_state.c @@ -507,8 +507,14 @@ vk_rasterization_state_init(struct vk_rasterization_state *rs, * depth clipping is disabled when * VkPipelineRasterizationStateCreateInfo::depthClampEnable is VK_TRUE. */ - rs->depth_clamp_enable = rs_info->depthClampEnable; - rs->depth_clip_enable = !rs_info->depthClampEnable; + if (IS_DYNAMIC(RS_DEPTH_CLAMP_ENABLE)) { + rs->depth_clip_enable = VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP; + } else { + rs->depth_clamp_enable = rs_info->depthClampEnable; + rs->depth_clip_enable = rs_info->depthClampEnable ? + VK_MESA_DEPTH_CLIP_ENABLE_FALSE : + VK_MESA_DEPTH_CLIP_ENABLE_TRUE; + } rs->polygon_mode = rs_info->polygonMode; @@ -535,8 +541,9 @@ vk_rasterization_state_init(struct vk_rasterization_state *rs, case VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT: { const VkPipelineRasterizationDepthClipStateCreateInfoEXT *rdc_info = (const VkPipelineRasterizationDepthClipStateCreateInfoEXT *)ext; - rs->depth_clip_enable = rdc_info->depthClipEnable; - rs->depth_clip_present = true; + rs->depth_clip_enable = rdc_info->depthClipEnable ? + VK_MESA_DEPTH_CLIP_ENABLE_TRUE : + VK_MESA_DEPTH_CLIP_ENABLE_FALSE; break; } @@ -1988,8 +1995,9 @@ vk_common_CmdSetDepthClipEnableEXT(VkCommandBuffer commandBuffer, VK_FROM_HANDLE(vk_command_buffer, cmd, commandBuffer); struct vk_dynamic_graphics_state *dyn = &cmd->dynamic_graphics_state; - SET_DYN_BOOL(dyn, RS_DEPTH_CLIP_ENABLE, - rs.depth_clip_enable, depthClipEnable); + SET_DYN_VALUE(dyn, RS_DEPTH_CLIP_ENABLE, rs.depth_clip_enable, + depthClipEnable ? VK_MESA_DEPTH_CLIP_ENABLE_TRUE : + VK_MESA_DEPTH_CLIP_ENABLE_FALSE); } VKAPI_ATTR void VKAPI_CALL diff --git a/src/vulkan/runtime/vk_graphics_state.h b/src/vulkan/runtime/vk_graphics_state.h index eef181f5ca0..186b00cb38b 100644 --- a/src/vulkan/runtime/vk_graphics_state.h +++ b/src/vulkan/runtime/vk_graphics_state.h @@ -215,6 +215,17 @@ struct vk_discard_rectangles_state { VkRect2D rectangles[MESA_VK_MAX_DISCARD_RECTANGLES]; }; +enum PACKED vk_mesa_depth_clip_enable { + /** Depth clipping should be disabled */ + VK_MESA_DEPTH_CLIP_ENABLE_FALSE = 0, + + /** Depth clipping should be enabled */ + VK_MESA_DEPTH_CLIP_ENABLE_TRUE = 1, + + /** Depth clipping should be enabled iff depth clamping is disabled */ + VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP, +}; + struct vk_rasterization_state { /** VkPipelineRasterizationStateCreateInfo::rasterizerDiscardEnable * @@ -234,10 +245,7 @@ struct vk_rasterization_state { * * MESA_VK_DYNAMIC_RS_DEPTH_CLIP_ENABLE */ - bool depth_clip_enable; - - /** denotes the presence of VkPipelineRasterizationDepthClipStateCreateInfoEXT */ - bool depth_clip_present; + enum vk_mesa_depth_clip_enable depth_clip_enable; /** VkPipelineRasterizationStateCreateInfo::polygonMode * @@ -342,6 +350,17 @@ struct vk_rasterization_state { } line; }; +static inline bool +vk_rasterization_state_depth_clip_enable(const struct vk_rasterization_state *rs) +{ + switch (rs->depth_clip_enable) { + case VK_MESA_DEPTH_CLIP_ENABLE_FALSE: return false; + case VK_MESA_DEPTH_CLIP_ENABLE_TRUE: return true; + case VK_MESA_DEPTH_CLIP_ENABLE_NOT_CLAMP: return !rs->depth_clamp_enable; + } + unreachable("Invalid depth clip enable"); +} + struct vk_fragment_shading_rate_state { /** VkPipelineFragmentShadingRateStateCreateInfoKHR::fragmentSize *