Kale
Loading...
Searching...
No Matches
SwapChain.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifdef KALE_VULKAN
18
19#include "SwapChain.hpp"
20
25
26using namespace Kale;
27using namespace Kale::Vulkan;
28
33SwapChain::SwapChain(Device& device) : ChildResource(device), support(device.physicalDevice) {
35}
36
46
51 vk::SurfaceFormatKHR swapformat = support.chooseFormat();
52 vk::PresentModeKHR presentMode = support.choosePresentMode();
53 vk::Extent2D swapextent = support.chooseSwapExtent();
54
55 uint32_t imageCount = support.capabilities.minImageCount + 1;
56 if (support.capabilities.maxImageCount != 0 && imageCount > support.capabilities.maxImageCount)
57 imageCount = support.capabilities.maxImageCount;
58
59 vk::SwapchainCreateInfoKHR createInfo;
60 createInfo.surface = Core::surface.get();
61 createInfo.minImageCount = imageCount;
62 createInfo.imageFormat = swapformat.format;
63 createInfo.imageColorSpace = swapformat.colorSpace;
64 createInfo.imageExtent = swapextent;
65 createInfo.imageArrayLayers = 1;
66 createInfo.imageUsage = vk::ImageUsageFlagBits::eColorAttachment;
67
68 uint32_t queueFamilyIndices[] = {
71 };
72
74 createInfo.imageSharingMode = vk::SharingMode::eExclusive;
75 }
76 else {
77 createInfo.imageSharingMode = vk::SharingMode::eConcurrent;
78 createInfo.queueFamilyIndexCount = 2;
79 createInfo.pQueueFamilyIndices = queueFamilyIndices;
80 }
81
82 createInfo.preTransform = support.capabilities.currentTransform;
83 createInfo.compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
84 createInfo.presentMode = presentMode;
85 createInfo.clipped = VK_TRUE;
86 createInfo.oldSwapchain = nullptr;
87
88 swapchain = parentPtr->logicalDevice->createSwapchainKHRUnique(createInfo);
89
90 // Get swapchain images
91 images = parentPtr->logicalDevice->getSwapchainImagesKHR(swapchain.get());
92 extent = swapextent;
93 format = swapformat.format;
94
96}
97
102 imageViews.reserve(images.size());
103 for (const vk::Image& image : images) {
104 vk::ImageSubresourceRange range(vk::ImageAspectFlagBits::eColor, 0, 1, 0, 1);
105 vk::ImageViewCreateInfo createInfo(vk::ImageViewCreateFlags(), image, vk::ImageViewType::e2D, format,
106 vk::ComponentMapping(), range);
107 imageViews.push_back(parentPtr->logicalDevice->createImageViewUnique(createInfo));
108 }
109}
110
116 // Clear the current frame buffers
117 frameBuffers.clear();
118
119 // Reserve memory for the frame buffers
120 frameBuffers.reserve(imageViews.size());
121
122 // Create the frame buffers from the iamge views
123 for (const vk::UniqueImageView& imageView : imageViews) {
124 vk::FramebufferCreateInfo createInfo(vk::FramebufferCreateFlags(), renderer.renderPass.get(), 1, &imageView.get(),
125 extent.width, extent.height, 1);
126 frameBuffers.push_back(Core::device.logicalDevice->createFramebufferUnique(createInfo));
127 }
128}
129
134 // Empty Body
135}
136
140void SwapChain::freeResources(bool remove) {
143 frameBuffers.clear();
144 imageViews.clear();
145 swapchain.reset();
146}
147
148#endif
virtual void freeResources(bool remove=true)
virtual void init(Parent &parent)
static Device device
Definition Core.hpp:99
static vk::UniqueSurfaceKHR surface
Definition Core.hpp:94
vk::PhysicalDevice physicalDevice
Definition Device.hpp:69
vk::UniqueDevice logicalDevice
Definition Device.hpp:59
QueueFamilyIndices queueIndices
Definition Device.hpp:79
std::optional< uint32_t > graphicsFamilyIndex
std::optional< uint32_t > presentFamilyIndex
vk::UniqueRenderPass renderPass
Definition Renderer.hpp:59
std::vector< vk::UniqueImageView > imageViews
Definition SwapChain.hpp:77
vk::UniqueSwapchainKHR swapchain
Definition SwapChain.hpp:57
SwapChainSupportDetails support
Definition SwapChain.hpp:72
std::vector< vk::Image > images
Definition SwapChain.hpp:82
void freeResources(bool remove=true) override
std::vector< vk::UniqueFramebuffer > frameBuffers
Definition SwapChain.hpp:87
void init(Device &device) override
Definition SwapChain.cpp:41
void createFrameBuffers(Renderer &renderer)