Files
Destrum/destrum/include/destrum/Graphics/Swapchain.h
T

98 lines
2.7 KiB
C++

#ifndef SWAPCHAIN_H
#define SWAPCHAIN_H
#include <array>
#include <cstdint>
#include <vector>
#include <vulkan/vulkan.h>
#include <VkBootstrap.h>
#include <destrum/Graphics/FrameConfig.h>
class Swapchain {
public:
struct AcquireResult {
VkResult result{VK_SUCCESS};
VkImage image{VK_NULL_HANDLE};
std::uint32_t imageIndex{0};
[[nodiscard]] bool hasImage() const {
return image != VK_NULL_HANDLE &&
(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR);
}
};
void initSync(VkDevice device);
//Ye ye, passing a pointer is bad bla bla @Kobe
void createSwapchain(
VkDevice device,
vkb::Device vkbDevice,
VkSurfaceKHR surface,
VkFormat format,
std::uint32_t width,
std::uint32_t height,
bool vSync);
[[nodiscard]] bool recreateSwapchain(
VkDevice device,
vkb::Device vkbDevice,
VkSurfaceKHR surface,
VkFormat format,
std::uint32_t width,
std::uint32_t height,
bool vSync);
void cleanup(VkDevice device);
[[nodiscard]] VkExtent2D getExtent() const { return extent; }
[[nodiscard]] const std::vector<VkImage>& getImages() const { return images; }
[[nodiscard]] std::uint32_t getImageCount() const { return static_cast<std::uint32_t>(images.size()); }
void beginFrame(VkDevice device, int index) const;
void resetFences(VkDevice device, int index) const;
AcquireResult acquireNextImage(VkDevice device, std::uint32_t index);
void submitAndPresent(
VkDevice device,
VkCommandBuffer cmd,
VkQueue graphicsQueue,
VkQueue presentQueue,
std::uint32_t imageIndex,
std::uint32_t frameIndex);
[[nodiscard]] bool isDirty() const { return dirty; }
[[nodiscard]] VkImageView getImageView(int index) const { return imageViews[index]; }
[[nodiscard]] VkImageView getImageView(std::uint32_t imageIndex) const {
return imageViews.at(imageIndex);
}
[[nodiscard]] VkSurfaceKHR getSurface() const { return surface; }
[[nodiscard]] vkb::Swapchain getVkbSwapchain() const { return m_swapchain; }
[[nodiscard]] VkFormat getFormat() const { return m_swapchain.image_format; }
private:
void nameSwapchainObjects(VkDevice device) const;
struct FrameData {
VkSemaphore swapchainSemaphore{VK_NULL_HANDLE};
VkFence renderFence{VK_NULL_HANDLE};
};
std::vector<VkSemaphore> imageRenderSemaphores;
std::array<FrameData, FRAMES_IN_FLIGHT> frames;
vkb::Swapchain m_swapchain;
std::vector<VkImage> images;
std::vector<VkImageView> imageViews;
bool dirty{false};
VkSurfaceKHR surface{VK_NULL_HANDLE};
VkExtent2D extent{};
};
#endif