Skip to content

Commit

Permalink
Proper resize handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SaschaWillems committed Oct 14, 2023
1 parent b481b63 commit 60321d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
33 changes: 19 additions & 14 deletions examples/variablerateshading/variablerateshading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
// Delete allocated resources
vkDeviceWaitIdle(device);
// Invalidate the shading rate image, will be recreated in the renderpass setup
vkDestroyImageView(device, shadingRateImage.view, nullptr);
vkDestroyImage(device, shadingRateImage.image, nullptr);
vkFreeMemory(device, shadingRateImage.memory, nullptr);
// Recreate image
prepareShadingRateImage();
// Recreate the render pass and update it with the new fragment shading rate image resolution
vkDestroyRenderPass(device, renderPass, nullptr);
setupRenderPass();
resized = false;
}

void VulkanExample::setupFrameBuffer()
{
if (resized) {
handleResize();
}

if (shadingRateImage.image == VK_NULL_HANDLE) {
prepareShadingRateImage();
}

VkImageView attachments[3];

// Depth/Stencil attachment is the same for all frame buffers
Expand Down Expand Up @@ -94,8 +106,9 @@ void VulkanExample::setupRenderPass()
vkCreateRenderPass2KHR = reinterpret_cast<PFN_vkCreateRenderPass2KHR>(vkGetInstanceProcAddr(instance, "vkCreateRenderPass2KHR"));
}

// Create an image with the shading rates to be used during rendering
prepareShadingRateImage();
if (shadingRateImage.image == VK_NULL_HANDLE) {
prepareShadingRateImage();
}

std::array<VkAttachmentDescription2KHR, 3> attachments = {};
// Color attachment
Expand Down Expand Up @@ -202,11 +215,6 @@ void VulkanExample::setupRenderPass()

void VulkanExample::buildCommandBuffers()
{
if (resized)
{
handleResize();
}

// As this is an extension, we need to manually load the extension pointers
if (!vkCmdSetFragmentShadingRateKHR) {
vkCmdSetFragmentShadingRateKHR = reinterpret_cast<PFN_vkCmdSetFragmentShadingRateKHR>(vkGetDeviceProcAddr(device, "vkCmdSetFragmentShadingRateKHR"));
Expand Down Expand Up @@ -538,14 +546,11 @@ void VulkanExample::preparePipelines()
};
VkSpecializationInfo specializationInfo = vks::initializers::specializationInfo(specializationMapEntries, sizeof(specializationData), &specializationData);
shaderStages[1].pSpecializationInfo = &specializationInfo;

// Create pipeline without shading rate
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.opaque));

specializationData.alphaMask = true;
rasterizationStateCI.cullMode = VK_CULL_MODE_NONE;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipelines.masked));
rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT;
specializationData.alphaMask = false;
}

void VulkanExample::prepareUniformBuffers()
Expand Down
3 changes: 1 addition & 2 deletions examples/variablerateshading/variablerateshading.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class VulkanExample : public VulkanExampleBase
vkglTF::Model scene;

struct ShadingRateImage {
VkImage image;
VkImage image{ VK_NULL_HANDLE };
VkDeviceMemory memory;
VkImageView view;
} shadingRateImage;
Expand Down Expand Up @@ -58,7 +58,6 @@ class VulkanExample : public VulkanExampleBase
virtual void getEnabledFeatures();
void handleResize();
void buildCommandBuffers();
void loadglTFFile(std::string filename);
void loadAssets();
void prepareShadingRateImage();
void setupDescriptors();
Expand Down

0 comments on commit 60321d4

Please sign in to comment.