26 assert(surface != VK_NULL_HANDLE);
27 assert(wrapper !=
nullptr);
29 this->
wraps = wrapper;
33 vk::SurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.
formats);
34 vk::PresentModeKHR presentMode = chooseSwapPresentMode(swapChainSupport.
presentModes);
35 vk::Extent2D extent = chooseSwapExtent(swapChainSupport.
capabilities);
37 uint32_t imageCount = swapChainSupport.
capabilities.minImageCount + 1;
39 imageCount = swapChainSupport.
capabilities.maxImageCount;
45 int queueFamilyIndexCount = 0;
46 auto imageSharingMode = vk::SharingMode::eExclusive;
48 imageSharingMode = vk::SharingMode::eConcurrent;
49 queueFamilyIndexCount = 2;
53 vk::CompositeAlphaFlagBitsKHR compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eInherit;
55 vk::CompositeAlphaFlagBitsKHR compositeAlpha = vk::CompositeAlphaFlagBitsKHR::eOpaque;
58 auto preTransform = swapChainSupport.
capabilities.currentTransform;
60 preTransform = vk::SurfaceTransformFlagBitsKHR::eIdentity;
63 vk::SwapchainCreateInfoKHR swapChainCreateInfo(
64 vk::SwapchainCreateFlagsKHR(),
65 static_cast<vk::SurfaceKHR
>(surface),
68 surfaceFormat.colorSpace,
71 vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eInputAttachment | vk::ImageUsageFlagBits::eTransferDst,
73 queueFamilyIndexCount,
74 queueFamilyIndexCount > 0 ? queueFamilyIndices :
nullptr,
93 createSwapchainImageView();
94 createAcquireSemaphores();
100 vk::Result acquireResult;
102 std::tie(acquireResult, imageIndex) =
wraps->
context.
device.acquireNextImageKHR(swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame],
nullptr);
103 if (acquireResult != vk::Result::eSuccess && acquireResult != vk::Result::eSuboptimalKHR) {
104 throw std::runtime_error(
"failed to acquire swap chain image!");
106 std::optional<vk::Semaphore> sem = imageAvailableSemaphores[currentFrame];
107 return std::tuple<uint32_t, std::optional<vk::Semaphore> >(imageIndex, sem );
112 vk::PresentInfoKHR presentInfo{ 1,
129 else if (resultPresent != vk::Result::eSuccess) {
130 throw std::runtime_error(
"failed to present swap chain image!");
133 catch (vk::OutOfDateKHRError) {
143 assert(fence != (vk::Fence) VK_NULL_HANDLE);
144 const uint32_t timeoutNs = 1 * 1000 * 1000 * 1000;
145 for (
int i = 0; i < 5; ++i) {
147 if (res == vk::Result::eSuccess) {
151 PRINT(
"%s",
"Waiting for CmdBuffer fence timed out, retrying...");
154 vk::PresentInfoKHR presentInfo{ 0,
170 else if (resultPresent != vk::Result::eSuccess) {
171 throw std::runtime_error(
"failed to present swap chain image!");
174 catch (vk::OutOfDateKHRError) {
183void SwapchainCommon::createSwapchainImageView()
185 int swapSize = swapChainImages.size();
187 for (uint32_t i = 0; i < swapSize; i++) {
194void SwapchainCommon::createAcquireSemaphores()
197 vk::SemaphoreCreateInfo semaphoreInfo;
198 for (
auto& sem : imageAvailableSemaphores) {
211 assert(index < swapChainImages.size());
212 return swapChainImages[index];
223 for (
auto & sem : imageAvailableSemaphores) {
226 imageAvailableSemaphores.clear();
239 return swapChainImages[currentFrame];
253vk::SurfaceFormatKHR SwapchainCommon::chooseSwapSurfaceFormat(
const std::vector<vk::SurfaceFormatKHR>& availableFormats)
255 for (
const auto& availableFormat : availableFormats) {
261 if (availableFormat.format == vk::Format::eB8G8R8Unorm && availableFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
262 return availableFormat;
264 else if (availableFormat.format == vk::Format::eB8G8R8A8Unorm && availableFormat.colorSpace == vk::ColorSpaceKHR::eSrgbNonlinear) {
265 return availableFormat;
268 return availableFormats[0];
271vk::PresentModeKHR SwapchainCommon::chooseSwapPresentMode(
const std::vector<vk::PresentModeKHR>& availablePresentModes)
274 for (
const auto& availablePresentMode : availablePresentModes) {
275 if (availablePresentMode == vk::PresentModeKHR::eMailbox) {
276 return availablePresentMode;
280 return vk::PresentModeKHR::eFifo;
283vk::Extent2D SwapchainCommon::chooseSwapExtent(
const vk::SurfaceCapabilitiesKHR& capabilities)
285 if (capabilities.currentExtent.width != UINT32_MAX) {
286 return capabilities.currentExtent;
294 PRINT(
"swap size w= %d, h = %d", width, height);
297 vk::Extent2D actualExtent = {
298 static_cast<uint32_t
>(width),
299 static_cast<uint32_t
>(height)
303 actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
304 actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));
Encapsulate the vkSwapchainKHR object and the operations linked to it. Inherit from SwapchainAbstract...
Class that contains helper functions for Vulkan.
file that contains the VulkanWrapper class that manages the classes related to Vulkan code and ease t...
Contain the class WindowAbstract.
std::vector< vk::ImageView > swapchainImageViews
vk::Extent2D swapchainExtent
std::variant< vk::SwapchainKHR > handle
vk::Format swapchainFormat
vk::Image getSwapchainImage(int index) override
SwapchainCommon(VkSurfaceKHR &surface, WindowAbstract *window, VulkanWrapper *wrapper)
vk::Image getCurrentImage() override
void presentImage(uint32_t imageIndex, vk::Semaphore &renderingFinnished) override
int getAttachmentSize() override
std::tuple< uint32_t, std::optional< vk::Semaphore > > acquireImage() override
vk::ImageView getSwapchainImageView(int elem) override
vk::PhysicalDevice physicalDevice
QueueFamilyIndices findQueueFamilies(vk::PhysicalDevice device)
Class that manages the classes related to Vulkan code and act as a wrapper around them.
Abstraction of the way of the result is displayed (screen or HMD).
virtual void getFrameBufferSize(int *w, int *h, vk::PhysicalDevice &pDevice)=0
virtual SwapChainSupportDetails querySwapChainSupport(vk::PhysicalDevice device)=0
Struct to encapsulate the indice of the queues families.
std::optional< uint32_t > graphicsFamily
std::optional< uint32_t > presentFamily
Struct that contains the capability for the sapchain, the formats and the present mode supported.
vk::SurfaceCapabilitiesKHR capabilities
std::vector< vk::SurfaceFormatKHR > formats
std::vector< vk::PresentModeKHR > presentModes