From: Mikko Rasa Date: Tue, 13 Aug 2024 12:14:49 +0000 (+0300) Subject: Adjust the logic for discarding fraembuffer contents in Vulkan X-Git-Url: https://git.tdb.fi/?a=commitdiff_plain;h=f69f21937213d8ffedaa35a2a3da6cbfaab5daab;p=libs%2Fgl.git Adjust the logic for discarding fraembuffer contents in Vulkan 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. --- diff --git a/source/backends/vulkan/renderpass.cpp b/source/backends/vulkan/renderpass.cpp index 1c2af612..b6c09c23 100644 --- a/source/backends/vulkan/renderpass.cpp +++ b/source/backends/vulkan/renderpass.cpp @@ -53,18 +53,26 @@ void RenderPass::fill_creation_info(vector &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(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(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 &buffer) const ++i; }; - bool discard = (clear && !clear_values && discard_fb_contents); VkSampleCountFlagBits vk_samples = static_cast(get_vulkan_samples(format.get_samples())); for(FrameAttachment a: format) - fill_attachment(a, vk_samples, discard); + fill_attachment(a, vk_samples, false); if(resolve) {