Program Listing for File swapchain.hpp

Return to documentation for file (inexor/vulkan-renderer/wrapper/swapchains/swapchain.hpp)

#pragma once

#include "inexor/vulkan-renderer/wrapper/synchronization/semaphore.hpp"

#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
#include <span>
#include <vector>

namespace inexor::vulkan_renderer::wrapper::synchronization {
// Forward declaration
class Semaphore;
} // namespace inexor::vulkan_renderer::wrapper::synchronization

namespace inexor::vulkan_renderer::wrapper {
// Forward declaration
class Device;
} // namespace inexor::vulkan_renderer::wrapper

namespace inexor::vulkan_renderer::wrapper::swapchains {

// Using declaration
using synchronization::Semaphore;
using wrapper::Device;

class Swapchain {
private:
    const Device &m_device;
    VkSwapchainKHR m_swapchain{VK_NULL_HANDLE};
    VkSurfaceKHR m_surface{VK_NULL_HANDLE};
    VkSurfaceFormatKHR m_surface_format;
    std::vector<VkImage> m_imgs;
    std::vector<VkImageView> m_img_views;
    VkExtent2D m_current_extent{};
    std::unique_ptr<Semaphore> m_img_available;
    bool m_vsync_enabled{false};

    [[nodiscard]] std::vector<VkImage> get_swapchain_images();

public:
    Swapchain(const Device &device, VkSurfaceKHR surface, std::uint32_t width, std::uint32_t height,
              bool vsync_enabled);

    Swapchain(const Swapchain &) = delete;
    Swapchain(Swapchain &&) noexcept;

    ~Swapchain();

    Swapchain &operator=(const Swapchain &) = delete;
    Swapchain &operator=(Swapchain &&) = delete;

    [[nodiscard]] std::uint32_t
    acquire_next_image_index(std::uint64_t timeout = std::numeric_limits<std::uint64_t>::max());

    [[nodiscard]] VkExtent2D extent() const {
        return m_current_extent;
    }

    [[nodiscard]] const VkSemaphore *image_available_semaphore() const {
        return m_img_available->semaphore();
    }

    [[nodiscard]] std::uint32_t image_count() const {
        return static_cast<std::uint32_t>(m_imgs.size());
    }

    [[nodiscard]] VkFormat image_format() const {
        return m_surface_format.format;
    }

    [[nodiscard]] const std::vector<VkImageView> &image_views() const {
        return m_img_views;
    }

    void present(std::uint32_t img_index);

    void setup_swapchain(VkExtent2D extent, bool vsync_enabled);

    [[nodiscard]] const VkSwapchainKHR *swapchain() const {
        return &m_swapchain;
    }
};

} // namespace inexor::vulkan_renderer::wrapper::swapchains