radv: fix optimizing needed states if some are marked as dynamic

From the Vulkan spec 1.2.157:

    "VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT specifies that the
     stencilTestEnable state in VkPipelineDepthStencilStateCreateInfo
     will be ignored and must be set dynamically with
     vkCmdSetStencilTestEnableEXT before any draw call."

So, stencilTestEnable should be ignored if dynamic. While we are
at it, fix depthBoundsTestEnable too.

Cc: 20.2
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3633
Signed-off-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Bas Nieuwenhuizen <bas@basnieuwenhuizen.nl>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/7112>
diff --git a/src/amd/vulkan/radv_pipeline.c b/src/amd/vulkan/radv_pipeline.c
index 1061225..50233f5 100644
--- a/src/amd/vulkan/radv_pipeline.c
+++ b/src/amd/vulkan/radv_pipeline.c
@@ -912,6 +912,21 @@
 }
 
 static bool
+radv_is_state_dynamic(const VkGraphicsPipelineCreateInfo *pCreateInfo,
+		      VkDynamicState state)
+{
+	if (pCreateInfo->pDynamicState) {
+		uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
+		for (uint32_t i = 0; i < count; i++) {
+			if (pCreateInfo->pDynamicState->pDynamicStates[i] == state)
+				return true;
+		}
+	}
+
+	return false;
+}
+
+static bool
 radv_pipeline_has_dynamic_ds_states(const VkGraphicsPipelineCreateInfo *pCreateInfo)
 {
 	VkDynamicState ds_states[] = {
@@ -922,14 +937,9 @@
 		VK_DYNAMIC_STATE_STENCIL_OP_EXT,
 	};
 
-	if (pCreateInfo->pDynamicState) {
-		uint32_t count = pCreateInfo->pDynamicState->dynamicStateCount;
-		for (uint32_t i = 0; i < count; i++) {
-			for (uint32_t j = 0; j < ARRAY_SIZE(ds_states); j++) {
-				if (pCreateInfo->pDynamicState->pDynamicStates[i] == ds_states[j])
-					return true;
-			}
-		}
+	for (uint32_t i = 0; i < ARRAY_SIZE(ds_states); i++) {
+		if (radv_is_state_dynamic(pCreateInfo, ds_states[i]))
+			return true;
 	}
 
 	return false;
@@ -1329,11 +1339,13 @@
 		states &= ~RADV_DYNAMIC_DEPTH_BIAS;
 
 	if (!pCreateInfo->pDepthStencilState ||
-	    !pCreateInfo->pDepthStencilState->depthBoundsTestEnable)
+	    (!pCreateInfo->pDepthStencilState->depthBoundsTestEnable &&
+	     !radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_DEPTH_BOUNDS_TEST_ENABLE_EXT)))
 		states &= ~RADV_DYNAMIC_DEPTH_BOUNDS;
 
 	if (!pCreateInfo->pDepthStencilState ||
-	    !pCreateInfo->pDepthStencilState->stencilTestEnable)
+	    (!pCreateInfo->pDepthStencilState->stencilTestEnable &&
+	     !radv_is_state_dynamic(pCreateInfo, VK_DYNAMIC_STATE_STENCIL_TEST_ENABLE_EXT)))
 		states &= ~(RADV_DYNAMIC_STENCIL_COMPARE_MASK |
 		            RADV_DYNAMIC_STENCIL_WRITE_MASK |
 		            RADV_DYNAMIC_STENCIL_REFERENCE);