Proper resize handling

This commit is contained in:
Sascha Willems 2023-10-14 16:35:41 +02:00
parent b481b63c78
commit 60321d465a
2 changed files with 20 additions and 16 deletions

View file

@ -47,21 +47,33 @@ void VulkanExample::getEnabledFeatures()
} }
/* /*
If the window has been resized, we need to recreate the shading rate image If the window has been resized, we need to recreate the shading rate image and the render pass. That's because the render pass holds information on the fragment shading rate image resolution
*/ */
void VulkanExample::handleResize() void VulkanExample::handleResize()
{ {
// Delete allocated resources vkDeviceWaitIdle(device);
// Invalidate the shading rate image, will be recreated in the renderpass setup
vkDestroyImageView(device, shadingRateImage.view, nullptr); vkDestroyImageView(device, shadingRateImage.view, nullptr);
vkDestroyImage(device, shadingRateImage.image, nullptr); vkDestroyImage(device, shadingRateImage.image, nullptr);
vkFreeMemory(device, shadingRateImage.memory, nullptr); vkFreeMemory(device, shadingRateImage.memory, nullptr);
// Recreate image
prepareShadingRateImage(); prepareShadingRateImage();
// Recreate the render pass and update it with the new fragment shading rate image resolution
vkDestroyRenderPass(device, renderPass, nullptr);
setupRenderPass();
resized = false; resized = false;
} }
void VulkanExample::setupFrameBuffer() void VulkanExample::setupFrameBuffer()
{ {
if (resized) {
handleResize();
}
if (shadingRateImage.image == VK_NULL_HANDLE) {
prepareShadingRateImage();
}
VkImageView attachments[3]; VkImageView attachments[3];
// Depth/Stencil attachment is the same for all frame buffers // Depth/Stencil attachment is the same for all frame buffers
@ -94,8 +106,9 @@ void VulkanExample::setupRenderPass()
vkCreateRenderPass2KHR = reinterpret_cast<PFN_vkCreateRenderPass2KHR>(vkGetInstanceProcAddr(instance, "vkCreateRenderPass2KHR")); vkCreateRenderPass2KHR = reinterpret_cast<PFN_vkCreateRenderPass2KHR>(vkGetInstanceProcAddr(instance, "vkCreateRenderPass2KHR"));
} }
// Create an image with the shading rates to be used during rendering if (shadingRateImage.image == VK_NULL_HANDLE) {
prepareShadingRateImage(); prepareShadingRateImage();
}
std::array<VkAttachmentDescription2KHR, 3> attachments = {}; std::array<VkAttachmentDescription2KHR, 3> attachments = {};
// Color attachment // Color attachment
@ -202,11 +215,6 @@ void VulkanExample::setupRenderPass()
void VulkanExample::buildCommandBuffers() void VulkanExample::buildCommandBuffers()
{ {
if (resized)
{
handleResize();
}
// As this is an extension, we need to manually load the extension pointers // As this is an extension, we need to manually load the extension pointers
if (!vkCmdSetFragmentShadingRateKHR) { if (!vkCmdSetFragmentShadingRateKHR) {
vkCmdSetFragmentShadingRateKHR = reinterpret_cast<PFN_vkCmdSetFragmentShadingRateKHR>(vkGetDeviceProcAddr(device, "vkCmdSetFragmentShadingRateKHR")); vkCmdSetFragmentShadingRateKHR = reinterpret_cast<PFN_vkCmdSetFragmentShadingRateKHR>(vkGetDeviceProcAddr(device, "vkCmdSetFragmentShadingRateKHR"));
@ -538,14 +546,11 @@ void VulkanExample::preparePipelines()
}; };
VkSpecializationInfo specializationInfo = vks::initializers::specializationInfo(specializationMapEntries, sizeof(specializationData), &specializationData); VkSpecializationInfo specializationInfo = vks::initializers::specializationInfo(specializationMapEntries, sizeof(specializationData), &specializationData);
shaderStages[1].pSpecializationInfo = &specializationInfo; shaderStages[1].pSpecializationInfo = &specializationInfo;
// Create pipeline without shading rate
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.opaque)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.opaque));
specializationData.alphaMask = true; specializationData.alphaMask = true;
rasterizationStateCI.cullMode = VK_CULL_MODE_NONE; rasterizationStateCI.cullMode = VK_CULL_MODE_NONE;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.masked)); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.masked));
rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT;
specializationData.alphaMask = false;
} }
void VulkanExample::prepareUniformBuffers() void VulkanExample::prepareUniformBuffers()

View file

@ -17,7 +17,7 @@ public:
vkglTF::Model scene; vkglTF::Model scene;
struct ShadingRateImage { struct ShadingRateImage {
VkImage image; VkImage image{ VK_NULL_HANDLE };
VkDeviceMemory memory; VkDeviceMemory memory;
VkImageView view; VkImageView view;
} shadingRateImage; } shadingRateImage;
@ -58,7 +58,6 @@ public:
virtual void getEnabledFeatures(); virtual void getEnabledFeatures();
void handleResize(); void handleResize();
void buildCommandBuffers(); void buildCommandBuffers();
void loadglTFFile(std::string filename);
void loadAssets(); void loadAssets();
void prepareShadingRateImage(); void prepareShadingRateImage();
void setupDescriptors(); void setupDescriptors();