radv: fix separate depth/stencil layout in render pass
authorSamuel Pitoiset <samuel.pitoiset@gmail.com>
Mon, 18 Jan 2021 07:59:29 +0000 (08:59 +0100)
committerMarge Bot <eric+marge@anholt.net>
Fri, 22 Jan 2021 15:54:18 +0000 (15:54 +0000)
We used to select the stencil layout even if we should have selected
the depth/stencil one.

Fixes: e4c8491bdf8 ("radv: implement VK_KHR_separate_depth_stencil_layouts")
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/8552>

src/amd/vulkan/radv_pass.c

index e3606ad..63cfaa0 100644 (file)
@@ -497,6 +497,69 @@ radv_num_subpass_attachments2(const VkSubpassDescription2 *desc)
               (ds_resolve && ds_resolve->pDepthStencilResolveAttachment);
 }
 
+static bool
+vk_image_layout_depth_only(VkImageLayout layout)
+{
+       switch (layout) {
+       case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL:
+       case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL:
+               return true;
+       default:
+               return false;
+       }
+}
+
+/* From the Vulkan Specification 1.2.166 - VkAttachmentReference2:
+ *
+ * "If layout only specifies the layout of the depth aspect of the attachment,
+ *  the layout of the stencil aspect is specified by the stencilLayout member
+ *  of a VkAttachmentReferenceStencilLayout structure included in the pNext
+ *  chain. Otherwise, layout describes the layout for all relevant image
+ *  aspects."
+ */
+static VkImageLayout
+stencil_ref_layout(const VkAttachmentReference2 *att_ref)
+{
+       if (!vk_image_layout_depth_only(att_ref->layout))
+               return att_ref->layout;
+
+       const VkAttachmentReferenceStencilLayoutKHR *stencil_ref =
+               vk_find_struct_const(att_ref->pNext,
+                                    ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
+       if (!stencil_ref)
+               return VK_IMAGE_LAYOUT_UNDEFINED;
+
+       return stencil_ref->stencilLayout;
+}
+
+/* From the Vulkan Specification 1.2.166 - VkAttachmentDescription2:
+ *
+ * "If format is a depth/stencil format, and initialLayout only specifies the
+ *  initial layout of the depth aspect of the attachment, the initial layout of
+ *  the stencil aspect is specified by the stencilInitialLayout member of a
+ *  VkAttachmentDescriptionStencilLayout structure included in the pNext chain.
+ *  Otherwise, initialLayout describes the initial layout for all relevant
+ *  image aspects."
+ */
+static VkImageLayout
+stencil_desc_layout(const VkAttachmentDescription2KHR *att_desc, bool final)
+{
+       const struct vk_format_description *desc = vk_format_description(att_desc->format);
+       if (!vk_format_has_stencil(desc))
+               return VK_IMAGE_LAYOUT_UNDEFINED;
+
+       const VkImageLayout main_layout =
+               final ? att_desc->finalLayout : att_desc->initialLayout;
+       if (!vk_image_layout_depth_only(main_layout))
+               return main_layout;
+
+       const VkAttachmentDescriptionStencilLayoutKHR *stencil_desc =
+               vk_find_struct_const(att_desc->pNext,
+                                    ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
+       assert(stencil_desc);
+       return final ? stencil_desc->stencilFinalLayout : stencil_desc->stencilInitialLayout;
+}
+
 VkResult radv_CreateRenderPass2(
     VkDevice                                    _device,
     const VkRenderPassCreateInfo2*              pCreateInfo,
@@ -531,9 +594,6 @@ VkResult radv_CreateRenderPass2(
 
        for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
                struct radv_render_pass_attachment *att = &pass->attachments[i];
-               const VkAttachmentDescriptionStencilLayoutKHR *stencil_layout =
-                       vk_find_struct_const(pCreateInfo->pAttachments[i].pNext,
-                                            ATTACHMENT_DESCRIPTION_STENCIL_LAYOUT_KHR);
 
                att->format = pCreateInfo->pAttachments[i].format;
                att->samples = pCreateInfo->pAttachments[i].samples;
@@ -541,12 +601,8 @@ VkResult radv_CreateRenderPass2(
                att->stencil_load_op = pCreateInfo->pAttachments[i].stencilLoadOp;
                att->initial_layout =  pCreateInfo->pAttachments[i].initialLayout;
                att->final_layout =  pCreateInfo->pAttachments[i].finalLayout;
-               att->stencil_initial_layout = (stencil_layout ?
-                                              stencil_layout->stencilInitialLayout :
-                                              pCreateInfo->pAttachments[i].initialLayout);
-               att->stencil_final_layout = (stencil_layout ?
-                                            stencil_layout->stencilFinalLayout :
-                                            pCreateInfo->pAttachments[i].finalLayout);
+               att->stencil_initial_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i], false);
+               att->stencil_final_layout = stencil_desc_layout(&pCreateInfo->pAttachments[i], true);
                // att->store_op = pCreateInfo->pAttachments[i].storeOp;
                // att->stencil_store_op = pCreateInfo->pAttachments[i].stencilStoreOp;
        }
@@ -585,16 +641,10 @@ VkResult radv_CreateRenderPass2(
                        p += desc->inputAttachmentCount;
 
                        for (uint32_t j = 0; j < desc->inputAttachmentCount; j++) {
-                               const VkAttachmentReferenceStencilLayoutKHR *stencil_attachment =
-                                   vk_find_struct_const(desc->pInputAttachments[j].pNext,
-                                                        ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
-
                                subpass->input_attachments[j] = (struct radv_subpass_attachment) {
                                        .attachment = desc->pInputAttachments[j].attachment,
                                        .layout = desc->pInputAttachments[j].layout,
-                                       .stencil_layout = (stencil_attachment ?
-                                                          stencil_attachment->stencilLayout :
-                                                          desc->pInputAttachments[j].layout),
+                                       .stencil_layout = stencil_ref_layout(&desc->pInputAttachments[j]),
                                };
                        }
                }
@@ -626,16 +676,10 @@ VkResult radv_CreateRenderPass2(
                if (desc->pDepthStencilAttachment) {
                        subpass->depth_stencil_attachment = p++;
 
-                       const VkAttachmentReferenceStencilLayoutKHR *stencil_attachment =
-                           vk_find_struct_const(desc->pDepthStencilAttachment->pNext,
-                                                ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
-
                        *subpass->depth_stencil_attachment = (struct radv_subpass_attachment) {
                                .attachment = desc->pDepthStencilAttachment->attachment,
                                .layout = desc->pDepthStencilAttachment->layout,
-                               .stencil_layout = (stencil_attachment ?
-                                                  stencil_attachment->stencilLayout :
-                                                  desc->pDepthStencilAttachment->layout),
+                               .stencil_layout = stencil_ref_layout(desc->pDepthStencilAttachment),
                        };
                }
 
@@ -646,16 +690,10 @@ VkResult radv_CreateRenderPass2(
                if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment) {
                        subpass->ds_resolve_attachment = p++;
 
-                       const VkAttachmentReferenceStencilLayoutKHR *stencil_resolve_attachment =
-                           vk_find_struct_const(ds_resolve->pDepthStencilResolveAttachment->pNext,
-                                                ATTACHMENT_REFERENCE_STENCIL_LAYOUT_KHR);
-
                        *subpass->ds_resolve_attachment = (struct radv_subpass_attachment) {
                                .attachment =  ds_resolve->pDepthStencilResolveAttachment->attachment,
                                .layout =      ds_resolve->pDepthStencilResolveAttachment->layout,
-                               .stencil_layout = (stencil_resolve_attachment ?
-                                                  stencil_resolve_attachment->stencilLayout :
-                                                  ds_resolve->pDepthStencilResolveAttachment->layout),
+                               .stencil_layout = stencil_ref_layout(ds_resolve->pDepthStencilResolveAttachment),
                        };
 
                        subpass->depth_resolve_mode = ds_resolve->depthResolveMode;