Swapchain code cleanup
Use references instead of pointers
This commit is contained in:
parent
372cab5779
commit
dcec337fa9
12 changed files with 65 additions and 85 deletions
|
|
@ -111,10 +111,9 @@ void VulkanRaytracingSample::setupFrameBuffer()
|
|||
frameBufferCreateInfo.layers = 1;
|
||||
|
||||
// Create frame buffers for every swap chain image
|
||||
frameBuffers.resize(swapChain.imageCount);
|
||||
for (uint32_t i = 0; i < frameBuffers.size(); i++)
|
||||
{
|
||||
attachments[0] = swapChain.buffers[i].view;
|
||||
frameBuffers.resize(swapChain.images.size());
|
||||
for (uint32_t i = 0; i < frameBuffers.size(); i++) {
|
||||
attachments[0] = swapChain.imageViews[i];
|
||||
VK_CHECK_RESULT(vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &frameBuffers[i]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ void VulkanSwapChain::setContext(VkInstance instance, VkPhysicalDevice physicalD
|
|||
this->device = device;
|
||||
}
|
||||
|
||||
void VulkanSwapChain::create(uint32_t *width, uint32_t *height, bool vsync, bool fullscreen)
|
||||
void VulkanSwapChain::create(uint32_t& width, uint32_t& height, bool vsync, bool fullscreen)
|
||||
{
|
||||
assert(physicalDevice);
|
||||
assert(device);
|
||||
|
|
@ -221,7 +221,24 @@ void VulkanSwapChain::create(uint32_t *width, uint32_t *height, bool vsync, bool
|
|||
VkSurfaceCapabilitiesKHR surfCaps;
|
||||
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfCaps));
|
||||
|
||||
// Get available present modes
|
||||
VkExtent2D swapchainExtent = {};
|
||||
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
|
||||
if (surfCaps.currentExtent.width == (uint32_t)-1)
|
||||
{
|
||||
// If the surface size is undefined, the size is set to the size of the images requested
|
||||
swapchainExtent.width = width;
|
||||
swapchainExtent.height = height;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the surface size is defined, the swap chain size must match
|
||||
swapchainExtent = surfCaps.currentExtent;
|
||||
width = surfCaps.currentExtent.width;
|
||||
height = surfCaps.currentExtent.height;
|
||||
}
|
||||
|
||||
|
||||
// Select a present mode for the swapchain
|
||||
uint32_t presentModeCount;
|
||||
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, NULL));
|
||||
assert(presentModeCount > 0);
|
||||
|
|
@ -229,26 +246,6 @@ void VulkanSwapChain::create(uint32_t *width, uint32_t *height, bool vsync, bool
|
|||
std::vector<VkPresentModeKHR> presentModes(presentModeCount);
|
||||
VK_CHECK_RESULT(vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &presentModeCount, presentModes.data()));
|
||||
|
||||
VkExtent2D swapchainExtent = {};
|
||||
// If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
|
||||
if (surfCaps.currentExtent.width == (uint32_t)-1)
|
||||
{
|
||||
// If the surface size is undefined, the size is set to
|
||||
// the size of the images requested.
|
||||
swapchainExtent.width = *width;
|
||||
swapchainExtent.height = *height;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the surface size is defined, the swap chain size must match
|
||||
swapchainExtent = surfCaps.currentExtent;
|
||||
*width = surfCaps.currentExtent.width;
|
||||
*height = surfCaps.currentExtent.height;
|
||||
}
|
||||
|
||||
|
||||
// Select a present mode for the swapchain
|
||||
|
||||
// The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec
|
||||
// This mode waits for the vertical blank ("v-sync")
|
||||
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
|
|
@ -348,25 +345,23 @@ void VulkanSwapChain::create(uint32_t *width, uint32_t *height, bool vsync, bool
|
|||
|
||||
VK_CHECK_RESULT(vkCreateSwapchainKHR(device, &swapchainCI, nullptr, &swapChain));
|
||||
|
||||
// If an existing swap chain is re-created, destroy the old swap chain
|
||||
// This also cleans up all the presentable images
|
||||
if (oldSwapchain != VK_NULL_HANDLE)
|
||||
{
|
||||
for (uint32_t i = 0; i < imageCount; i++)
|
||||
{
|
||||
vkDestroyImageView(device, buffers[i].view, nullptr);
|
||||
// If an existing swap chain is re-created, destroy the old swap chain and the ressources owned by the application (image views, images are owned by the swap chain)
|
||||
if (oldSwapchain != VK_NULL_HANDLE) {
|
||||
for (auto i = 0; i < images.size(); i++) {
|
||||
vkDestroyImageView(device, imageViews[i], nullptr);
|
||||
}
|
||||
vkDestroySwapchainKHR(device, oldSwapchain, nullptr);
|
||||
}
|
||||
VK_CHECK_RESULT(vkGetSwapchainImagesKHR(device, swapChain, &imageCount, NULL));
|
||||
uint32_t imageCount{ 0 };
|
||||
VK_CHECK_RESULT(vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr));
|
||||
|
||||
// Get the swap chain images
|
||||
images.resize(imageCount);
|
||||
VK_CHECK_RESULT(vkGetSwapchainImagesKHR(device, swapChain, &imageCount, images.data()));
|
||||
|
||||
// Get the swap chain buffers containing the image and imageview
|
||||
buffers.resize(imageCount);
|
||||
for (uint32_t i = 0; i < imageCount; i++)
|
||||
imageViews.resize(imageCount);
|
||||
for (auto i = 0; i < images.size(); i++)
|
||||
{
|
||||
VkImageViewCreateInfo colorAttachmentView = {};
|
||||
colorAttachmentView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
|
|
@ -385,20 +380,16 @@ void VulkanSwapChain::create(uint32_t *width, uint32_t *height, bool vsync, bool
|
|||
colorAttachmentView.subresourceRange.layerCount = 1;
|
||||
colorAttachmentView.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
colorAttachmentView.flags = 0;
|
||||
|
||||
buffers[i].image = images[i];
|
||||
|
||||
colorAttachmentView.image = buffers[i].image;
|
||||
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &colorAttachmentView, nullptr, &buffers[i].view));
|
||||
colorAttachmentView.image = images[i];
|
||||
VK_CHECK_RESULT(vkCreateImageView(device, &colorAttachmentView, nullptr, &imageViews[i]));
|
||||
}
|
||||
}
|
||||
|
||||
VkResult VulkanSwapChain::acquireNextImage(VkSemaphore presentCompleteSemaphore, uint32_t *imageIndex)
|
||||
VkResult VulkanSwapChain::acquireNextImage(VkSemaphore presentCompleteSemaphore, uint32_t& imageIndex)
|
||||
{
|
||||
// By setting timeout to UINT64_MAX we will always wait until the next image has been acquired or an actual error is thrown
|
||||
// With that we don't have to handle VK_NOT_READY
|
||||
return vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, presentCompleteSemaphore, (VkFence)nullptr, imageIndex);
|
||||
return vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, presentCompleteSemaphore, (VkFence)nullptr, &imageIndex);
|
||||
}
|
||||
|
||||
VkResult VulkanSwapChain::queuePresent(VkQueue queue, uint32_t imageIndex, VkSemaphore waitSemaphore)
|
||||
|
|
@ -421,16 +412,13 @@ VkResult VulkanSwapChain::queuePresent(VkQueue queue, uint32_t imageIndex, VkSem
|
|||
|
||||
void VulkanSwapChain::cleanup()
|
||||
{
|
||||
if (swapChain != VK_NULL_HANDLE)
|
||||
{
|
||||
for (uint32_t i = 0; i < imageCount; i++)
|
||||
{
|
||||
vkDestroyImageView(device, buffers[i].view, nullptr);
|
||||
if (swapChain != VK_NULL_HANDLE) {
|
||||
for (auto i = 0; i < images.size(); i++) {
|
||||
vkDestroyImageView(device, imageViews[i], nullptr);
|
||||
}
|
||||
}
|
||||
if (surface != VK_NULL_HANDLE)
|
||||
{
|
||||
vkDestroySwapchainKHR(device, swapChain, nullptr);
|
||||
}
|
||||
if (surface != VK_NULL_HANDLE) {
|
||||
vkDestroySurfaceKHR(instance, surface, nullptr);
|
||||
}
|
||||
surface = VK_NULL_HANDLE;
|
||||
|
|
|
|||
|
|
@ -27,11 +27,6 @@
|
|||
#include <sys/utsname.h>
|
||||
#endif
|
||||
|
||||
typedef struct _SwapChainBuffers {
|
||||
VkImage image{ VK_NULL_HANDLE };
|
||||
VkImageView view{ VK_NULL_HANDLE };
|
||||
} SwapChainBuffer;
|
||||
|
||||
class VulkanSwapChain
|
||||
{
|
||||
private:
|
||||
|
|
@ -43,9 +38,8 @@ public:
|
|||
VkFormat colorFormat{};
|
||||
VkColorSpaceKHR colorSpace{};
|
||||
VkSwapchainKHR swapChain{ VK_NULL_HANDLE };
|
||||
uint32_t imageCount;
|
||||
std::vector<VkImage> images{};
|
||||
std::vector<SwapChainBuffer> buffers{};
|
||||
std::vector<VkImageView> imageViews{};
|
||||
uint32_t queueNodeIndex{ UINT32_MAX };
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
|
|
@ -79,7 +73,7 @@ public:
|
|||
* @param height Pointer to the height of the swapchain (may be adjusted to fit the requirements of the swapchain)
|
||||
* @param vsync (Optional, default = false) Can be used to force vsync-ed rendering (by using VK_PRESENT_MODE_FIFO_KHR as presentation mode)
|
||||
*/
|
||||
void create(uint32_t* width, uint32_t* height, bool vsync = false, bool fullscreen = false);
|
||||
void create(uint32_t& width, uint32_t& height, bool vsync = false, bool fullscreen = false);
|
||||
/**
|
||||
* Acquires the next image in the swap chain
|
||||
*
|
||||
|
|
@ -90,7 +84,7 @@ public:
|
|||
*
|
||||
* @return VkResult of the image acquisition
|
||||
*/
|
||||
VkResult acquireNextImage(VkSemaphore presentCompleteSemaphore, uint32_t* imageIndex);
|
||||
VkResult acquireNextImage(VkSemaphore presentCompleteSemaphore, uint32_t& imageIndex);
|
||||
/**
|
||||
* Queue an image for presentation
|
||||
*
|
||||
|
|
|
|||
|
|
@ -179,7 +179,7 @@ std::string VulkanExampleBase::getWindowTitle() const
|
|||
void VulkanExampleBase::createCommandBuffers()
|
||||
{
|
||||
// Create one command buffer for each swap chain image
|
||||
drawCmdBuffers.resize(swapChain.imageCount);
|
||||
drawCmdBuffers.resize(swapChain.images.size());
|
||||
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, static_cast<uint32_t>(drawCmdBuffers.size()));
|
||||
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, drawCmdBuffers.data()));
|
||||
}
|
||||
|
|
@ -751,7 +751,7 @@ void VulkanExampleBase::drawUI(const VkCommandBuffer commandBuffer)
|
|||
void VulkanExampleBase::prepareFrame()
|
||||
{
|
||||
// Acquire the next image from the swap chain
|
||||
VkResult result = swapChain.acquireNextImage(semaphores.presentComplete, ¤tBuffer);
|
||||
VkResult result = swapChain.acquireNextImage(semaphores.presentComplete, currentBuffer);
|
||||
// Recreate the swapchain if it's no longer compatible with the surface (OUT_OF_DATE)
|
||||
// SRS - If no longer optimal (VK_SUBOPTIMAL_KHR), wait until submitFrame() in case number of swapchain images will change on resize
|
||||
if ((result == VK_ERROR_OUT_OF_DATE_KHR) || (result == VK_SUBOPTIMAL_KHR)) {
|
||||
|
|
@ -3077,11 +3077,11 @@ void VulkanExampleBase::setupDepthStencil()
|
|||
void VulkanExampleBase::setupFrameBuffer()
|
||||
{
|
||||
// Create frame buffers for every swap chain image
|
||||
frameBuffers.resize(swapChain.imageCount);
|
||||
frameBuffers.resize(swapChain.images.size());
|
||||
for (uint32_t i = 0; i < frameBuffers.size(); i++)
|
||||
{
|
||||
const VkImageView attachments[2] = {
|
||||
swapChain.buffers[i].view,
|
||||
swapChain.imageViews[i],
|
||||
// Depth/Stencil attachment is the same for all frame buffers
|
||||
depthStencil.view
|
||||
};
|
||||
|
|
@ -3290,7 +3290,7 @@ void VulkanExampleBase::createSurface()
|
|||
|
||||
void VulkanExampleBase::createSwapChain()
|
||||
{
|
||||
swapChain.create(&width, &height, settings.vsync, settings.fullscreen);
|
||||
swapChain.create(width, height, settings.vsync, settings.fullscreen);
|
||||
}
|
||||
|
||||
void VulkanExampleBase::OnUpdateUIOverlay(vks::UIOverlay *overlay) {}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue