Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
VulkanSwapChain.hpp
Go to the documentation of this file.
1#pragma once
2#include <vulkan/vulkan.h>
3#include <vector>
4#include <stdexcept>
5#include <iostream>
6
12public:
16 VulkanSwapchain() = default;
21
29 void initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, uint32_t graphicsQueueFamily);
33 void cleanup();
34
39 VkFormat getImageFormat() const { return imageFormat; }
44 VkExtent2D getExtent() const { return extent; }
49 const std::vector<VkFramebuffer>& getFramebuffers() const { return framebuffers; }
54 VkRenderPass getRenderPass() const { return renderPass; }
59 VkSwapchainKHR getSwapchain() const { return swapchain; }
60
61private:
63 VkDevice device = VK_NULL_HANDLE;
65 VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
67 VkSurfaceKHR surface = VK_NULL_HANDLE;
68
70 VkSwapchainKHR swapchain = VK_NULL_HANDLE;
72 std::vector<VkImage> images;
74 std::vector<VkImageView> imageViews;
76 std::vector<VkFramebuffer> framebuffers;
77
79 VkFormat imageFormat{};
81 VkExtent2D extent{};
83 VkRenderPass renderPass = VK_NULL_HANDLE;
84
86 VkImage depthImage = VK_NULL_HANDLE;
88 VkDeviceMemory depthImageMemory = VK_NULL_HANDLE;
90 VkImageView depthImageView = VK_NULL_HANDLE;
92 VkFormat depthFormat = VK_FORMAT_D32_SFLOAT;
93
94private:
99 void createSwapchain(uint32_t graphicsQueueFamily);
103 void createImageViews();
107 void createRenderPass();
111 void createFramebuffers();
119 VkFormat findDepthFormat();
123 VkFormat findSupportedFormat(const std::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
127 uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
128
134 VkSurfaceFormatKHR chooseSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& formats);
140 VkPresentModeKHR choosePresentMode(const std::vector<VkPresentModeKHR>& modes);
146 VkExtent2D chooseExtent(const VkSurfaceCapabilitiesKHR& capabilities);
147};
VkRenderPass getRenderPass() const
Gets default render pass for swapchain buffers.
Definition VulkanSwapChain.hpp:54
VkImage depthImage
GPU depth image allocation handle.
Definition VulkanSwapChain.hpp:86
VkSwapchainKHR swapchain
Raw swapchain handle.
Definition VulkanSwapChain.hpp:70
void createFramebuffers()
Creates framebuffers linking image views to the render pass.
Definition VulkanSwapChain.cpp:223
std::vector< VkFramebuffer > framebuffers
framebuffers wrapping image views.
Definition VulkanSwapChain.hpp:76
void cleanup()
Safely destroys framebuffers, render pass, image views, and swapchain handles.
Definition VulkanSwapChain.cpp:32
~VulkanSwapchain()
Destroy the Vulkan Swapchain object and release swapchain structures.
Definition VulkanSwapChain.cpp:6
VkPresentModeKHR choosePresentMode(const std::vector< VkPresentModeKHR > &modes)
Selects optimal presentation mode (e.g. Mailbox or FIFO).
Definition VulkanSwapChain.cpp:262
void createRenderPass()
Configures render pass layouts.
Definition VulkanSwapChain.cpp:162
VulkanSwapchain()=default
Construct a new Vulkan Swapchain object.
VkSurfaceKHR surface
Reference to presentation surface.
Definition VulkanSwapChain.hpp:67
VkFormat findDepthFormat()
Helper to query and select best depth format.
Definition VulkanSwapChain.cpp:337
VkDevice device
Reference to logical device.
Definition VulkanSwapChain.hpp:63
void createImageViews()
Creates image views for each swapchain image.
Definition VulkanSwapChain.cpp:135
VkPhysicalDevice physicalDevice
Reference to physical device.
Definition VulkanSwapChain.hpp:65
void createSwapchain(uint32_t graphicsQueueFamily)
Establishes the KHR swapchain handle.
Definition VulkanSwapChain.cpp:73
VkExtent2D getExtent() const
Gets resolution extent details.
Definition VulkanSwapChain.hpp:44
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties)
Helper to query suitable GPU memory index.
Definition VulkanSwapChain.cpp:359
std::vector< VkImageView > imageViews
image views wrapping swapchain images.
Definition VulkanSwapChain.hpp:74
VkFormat depthFormat
Selected depth format.
Definition VulkanSwapChain.hpp:92
VkExtent2D chooseExtent(const VkSurfaceCapabilitiesKHR &capabilities)
Resolves swapchain resolution boundaries against hardware limits.
Definition VulkanSwapChain.cpp:274
VkRenderPass renderPass
Render pass used for frame clearing and drawing.
Definition VulkanSwapChain.hpp:83
const std::vector< VkFramebuffer > & getFramebuffers() const
Gets active framebuffers list.
Definition VulkanSwapChain.hpp:49
VkFormat getImageFormat() const
Gets selected swapchain format.
Definition VulkanSwapChain.hpp:39
VkFormat imageFormat
image format of swapchain buffers.
Definition VulkanSwapChain.hpp:79
VkImageView depthImageView
Depth image view wrapper.
Definition VulkanSwapChain.hpp:90
VkExtent2D extent
image extent (resolution) of swapchain buffers.
Definition VulkanSwapChain.hpp:81
VkFormat findSupportedFormat(const std::vector< VkFormat > &candidates, VkImageTiling tiling, VkFormatFeatureFlags features)
Helper to find supported depth formats.
Definition VulkanSwapChain.cpp:345
VkSwapchainKHR getSwapchain() const
Gets raw KHR swapchain.
Definition VulkanSwapChain.hpp:59
void initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, uint32_t graphicsQueueFamily)
Sets up parameters, selects formats, builds KHR swapchain, views, render pass and framebuffers.
Definition VulkanSwapChain.cpp:17
VkDeviceMemory depthImageMemory
GPU depth memory handle.
Definition VulkanSwapChain.hpp:88
VkSurfaceFormatKHR chooseSurfaceFormat(const std::vector< VkSurfaceFormatKHR > &formats)
Selects optimal color format and color space.
Definition VulkanSwapChain.cpp:250
std::vector< VkImage > images
Retrieved swapchain images.
Definition VulkanSwapChain.hpp:72
void createDepthResources()
Creates depth buffers and views.
Definition VulkanSwapChain.cpp:285