72 lines
1.7 KiB
C++
72 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <memory>
|
|
#include <sophia/types.hpp>
|
|
#include <vector>
|
|
#include <vulkan/vulkan.hpp>
|
|
|
|
#include "device.hpp"
|
|
#include "swapchain.hpp"
|
|
#include "window.hpp"
|
|
|
|
namespace sophia {
|
|
|
|
struct FrameData {
|
|
vk::CommandBuffer commandBuffer;
|
|
vk::UniqueSemaphore imageAvailable;
|
|
vk::UniqueFence inFlight;
|
|
};
|
|
|
|
class RenderContext {
|
|
public:
|
|
static constexpr u32 MAX_FRAMES_IN_FLIGHT = 2;
|
|
|
|
RenderContext(const RenderContext&) = delete;
|
|
RenderContext& operator=(const RenderContext&) = delete;
|
|
|
|
RenderContext(Window& window, Device& device);
|
|
~RenderContext();
|
|
|
|
vk::CommandBuffer beginFrame();
|
|
void endFrame();
|
|
|
|
void transitionCurrentImage(vk::CommandBuffer commandBuffer,
|
|
vk::ImageLayout newLayout) const {
|
|
this->swapchain->getImage(static_cast<int>(this->currentImageIndex))
|
|
.transitionLayout(commandBuffer, newLayout);
|
|
}
|
|
vk::ImageView getCurrentImageView() const {
|
|
return this->swapchain->getImageView(
|
|
static_cast<int>(this->currentImageIndex));
|
|
}
|
|
|
|
vk::Extent2D getExtent() const { return this->swapchain->getExtent(); }
|
|
vk::Format getImageFormat() const {
|
|
return this->swapchain->getImageFormat();
|
|
}
|
|
u32 getImageCount() const {
|
|
return static_cast<u32>(this->swapchain->imageCount());
|
|
}
|
|
|
|
private:
|
|
void recreateSwapchain();
|
|
void createFrames();
|
|
|
|
Window& window;
|
|
Device& device;
|
|
|
|
std::unique_ptr<Swapchain> swapchain;
|
|
|
|
std::array<FrameData, MAX_FRAMES_IN_FLIGHT> frames;
|
|
|
|
std::vector<vk::UniqueSemaphore> renderFinishedSemaphores;
|
|
std::vector<vk::Fence> imagesInFlight;
|
|
|
|
u32 currentFrameIndex = 0;
|
|
u32 currentImageIndex = 0;
|
|
bool isFrameStarted = false;
|
|
};
|
|
|
|
} // namespace sophia
|