]> git.tdb.fi Git - libs/gl.git/commitdiff
Adjust the logic for discarding fraembuffer contents in Vulkan
authorMikko Rasa <tdb@tdb.fi>
Tue, 13 Aug 2024 12:14:49 +0000 (15:14 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 13 Aug 2024 12:14:49 +0000 (15:14 +0300)
It was unnecessarily transitioning from a known layout when the
framebuffer is being cleraed.  This triggered a validation error on the
first frame when rendering directly to the swapchain.

source/backends/vulkan/renderpass.cpp

index 1c2af61250d74707360e24fb75124c541063ab6c..b6c09c230fac2effbea20ff105c22e761ef8be83 100644 (file)
@@ -53,18 +53,26 @@ void RenderPass::fill_creation_info(vector<char> &buffer) const
        VkAttachmentReference2 *color_ptr = color_refs;
        VkAttachmentReference2 *ds_ptr = depth_stencil_ref;
        unsigned i = 0;
-       auto fill_attachment = [=, &color_ptr, &ds_ptr, &i](FrameAttachment a, VkSampleCountFlagBits samples, bool discard){
+       auto fill_attachment = [=, &color_ptr, &ds_ptr, &i](FrameAttachment a, VkSampleCountFlagBits samples, bool force_discard){
                VkImageLayout subpass_layout = static_cast<VkImageLayout>(get_vulkan_attachment_layout(get_components(get_attachment_pixelformat(a))));
                VkImageLayout external_layout = (to_present ? VK_IMAGE_LAYOUT_PRESENT_SRC_KHR : subpass_layout);
 
                attachments[i].sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
                attachments[i].format = static_cast<VkFormat>(get_vulkan_pixelformat(get_attachment_pixelformat(a)));
                attachments[i].samples = samples;
-               attachments[i].loadOp = (discard ? VK_ATTACHMENT_LOAD_OP_DONT_CARE : clear ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
+               if(force_discard || (clear && !clear_values))
+                       attachments[i].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
+               else if(clear && clear_values)
+                       attachments[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
+               else
+                       attachments[i].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
                attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
                attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
                attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
-               attachments[i].initialLayout = (discard ? VK_IMAGE_LAYOUT_UNDEFINED : external_layout);
+               if(force_discard || discard_fb_contents)
+                       attachments[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
+               else
+                       attachments[i].initialLayout = external_layout;
                attachments[i].finalLayout = external_layout;
 
                if(subpass_layout==VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)
@@ -84,10 +92,9 @@ void RenderPass::fill_creation_info(vector<char> &buffer) const
                ++i;
        };
 
-       bool discard = (clear && !clear_values && discard_fb_contents);
        VkSampleCountFlagBits vk_samples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
        for(FrameAttachment a: format)
-               fill_attachment(a, vk_samples, discard);
+               fill_attachment(a, vk_samples, false);
 
        if(resolve)
        {