Files
sophia/engine/src/render_context.hpp
T
2026-09-21 00:26:50 -07:00

66 lines
1.4 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();
Image& getCurrentImage() const {
return this->swapchain->getImage(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