Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
renderer.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4
5#include "flint/camera/camera.h"
6#include "flint/core/api.h"
7#include "flint/core/asset-loader.h"
8#include "flint/graphics/drawable.h"
9#include "flint/graphics/pipelines/batch-pipeline.h"
10#include "flint/graphics/pipelines/primitives-pipeline.h"
11#include "flint/graphics/pipelines/ui-pipeline.h"
12#include "flint/graphics/types/color.h"
13#include "flint/window/IWindow.h"
14
15#include <SDL3/SDL_gpu.h>
16#include <SDL3/SDL_render.h>
17
18#include <functional>
19#include <memory>
20#include <string>
21#include <vector>
22
23namespace Flint {
24 struct FrameData;
25
41 public:
49 FLINT_API explicit GPURenderer(IWindow *window, IAssetLoader &assetLoader,
50 ShaderFormat shaderFormat);
51 FLINT_API virtual ~GPURenderer();
52
60 FLINT_API bool beginFrame();
61
73 FLINT_API void draw(const std::function<void(RenderQueue &)> &overlayEmit = {});
79 FLINT_API void endFrame();
80
87 FLINT_API void submit(std::shared_ptr<const Drawable> drawable,
88 DrawLayer layer = DrawLayer::Midground);
89
99 [[deprecated("Use submit(std::shared_ptr<const Drawable>, DrawLayer) instead.")]]
100 FLINT_API void submit(const Drawable *drawable, DrawLayer layer = DrawLayer::Midground);
101
106 FLINT_API void setClearColor(Color color);
107
112 FLINT_API void setViewport(const SDL_GPUViewport &viewport);
113
118 FLINT_API void setCamera(const Camera &camera);
119
124 [[nodiscard]] std::unique_ptr<Shader> createShader() const {
125 return std::make_unique<Shader>(m_gpuDevice, m_shaderFormat);
126 }
127
133 [[nodiscard]] FLINT_API std::vector<char> loadShader(const std::string &basePath) const;
134
140 [[nodiscard]] FLINT_API std::string getShaderPath(const std::string &basePath) const;
141
143 [[nodiscard]] ShaderFormat getShaderFormat() const {
144 return m_shaderFormat;
145 }
146
154 [[deprecated("Do not query getRenderPass() during emission. Use RenderQueue::custom() for "
155 "custom GPU draw passes.")]] [[nodiscard]] SDL_GPURenderPass *
157 return m_renderPass;
158 }
159
164 void setGPUPresentMode(SDL_GPUPresentMode mode) {
165 if (mode != SDL_GPU_PRESENTMODE_VSYNC &&
166 !SDL_WindowSupportsGPUPresentMode(m_gpuDevice, m_sdlWindow, mode)) {
167 FLINT_ENGINE_WARN("Present mode {} unsupported, falling back to VSYNC",
168 static_cast<int>(mode));
169 mode = SDL_GPU_PRESENTMODE_VSYNC;
170 }
171 if (!SDL_SetGPUSwapchainParameters(m_gpuDevice, m_sdlWindow,
172 SDL_GPU_SWAPCHAINCOMPOSITION_SDR, mode)) {
173 FLINT_ENGINE_ERROR("SDL_SetGPUSwapchainParameters failed: {}", SDL_GetError());
174 }
175 }
176
184 [[deprecated(
185 "Do not query getCommandBuffer() during emission. Use RenderQueue::deferUpload() or "
186 "RenderQueue::custom().")]] [[nodiscard]] SDL_GPUCommandBuffer *
188 return m_commandBuffer;
189 }
190
192 [[nodiscard]] const FloatRect &getCurrentCameraView() const {
193 return m_currentCameraView;
194 }
195
197 [[nodiscard]] SDL_GPUDevice *getGPUDevice() const {
198 return m_gpuDevice;
199 }
200
202 [[nodiscard]] IAssetLoader &getAssetLoader() const {
203 return m_assetLoader;
204 }
205
207 [[nodiscard]] const SDL_GPUViewport &getDefaultViewport() const {
208 return m_defaultViewport;
209 }
210
213 [[nodiscard]] FLINT_API SDL_Renderer *getSDLRenderer() const;
214
216 [[nodiscard]] UIPipeline &getUIPipeline() {
217 return m_uiPipeline;
218 }
219
222 return m_primitivePipeline;
223 }
224
226 [[nodiscard]] SDL_GPUTexture *getWhiteTexture() {
227 return m_defaultWhiteTexture;
228 }
229
230 private:
231 IWindow *m_iWindow = nullptr;
232 SDL_Window *m_sdlWindow = nullptr;
233 ShaderFormat m_shaderFormat;
234 SDL_GPURenderPass *m_renderPass = nullptr;
235 SDL_GPUDevice *m_gpuDevice = nullptr;
236 SDL_GPUCommandBuffer *m_commandBuffer = nullptr;
237 SDL_GPUTexture *m_swapTexture = nullptr;
238 Uint32 m_swapWidth = 0, m_swapHeight = 0;
239
240 std::vector<DrawCommand> m_commands;
241 FloatRect m_currentCameraView = FloatRect({0, 0}, {320, 180});
242
243 UIPipeline m_uiPipeline;
244 BatchPipeline m_batchPipeline;
245 PrimitivePipeline m_primitivePipeline;
246
247 Color m_clearColor = {0, 0, 0, 255};
248
249 SDL_GPUTexture *m_defaultWhiteTexture = nullptr;
250 SDL_GPUSampler *m_defaultSampler = nullptr;
251 SDL_GPUViewport m_defaultViewport{};
252
253 IAssetLoader &m_assetLoader;
254
255 std::unique_ptr<FrameData>
256 m_frame;
257
262 bool uploadFrameBuffers();
263
274 void ensureBuffer(SDL_GPUBuffer *&buffer, Uint32 &capacity, Uint32 neededBytes,
275 SDL_GPUBufferUsageFlags usage, const char *debugName);
276
281 void resetFrameData();
282
286 void destroyFrameBuffers();
287
288 void createDefaultViewport(SDL_GPURenderPass *renderPass);
289 bool createDefaultTexture();
290 void initializePipelines();
291 void cleanupPipelines();
292 };
293
294}
Interface for objects that contribute ordered work to a rendered frame.
Definition drawable.h:21
High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.
Definition renderer.h:40
FLINT_API std::vector< char > loadShader(const std::string &basePath) const
Load raw shader bytecode bytes from the asset loader.
ShaderFormat getShaderFormat() const
Get the active GPU shader format.
Definition renderer.h:143
FLINT_API void submit(std::shared_ptr< const Drawable > drawable, DrawLayer layer=DrawLayer::Midground)
Queue a shared Drawable for rendering in the current frame.
FLINT_API void setCamera(const Camera &camera)
Update the active camera view transform used for batch coordinate projection.
PrimitivePipeline & getPrimitivePipeline()
Return reference to the primitive point/line pipeline wrapper.
Definition renderer.h:221
SDL_GPURenderPass * getRenderPass() const
Return the current frame's active SDL GPU render pass handle.
Definition renderer.h:156
IAssetLoader & getAssetLoader() const
Return reference to the core asset loader.
Definition renderer.h:202
SDL_GPUCommandBuffer * getCommandBuffer() const
Return the current frame's active SDL GPU command buffer handle.
Definition renderer.h:187
SDL_GPUTexture * getWhiteTexture()
Return shared 1x1 fallback white GPU texture handle.
Definition renderer.h:226
std::unique_ptr< Shader > createShader() const
Factory method creating a new Shader instance bound to this GPU device.
Definition renderer.h:124
FLINT_API void submit(const Drawable *drawable, DrawLayer layer=DrawLayer::Midground)
Queue a raw Drawable pointer for rendering in the current frame.
FLINT_API void setClearColor(Color color)
Set the RGBA clear color applied at the start of each frame.
UIPipeline & getUIPipeline()
Return reference to the UI pipeline wrapper.
Definition renderer.h:216
FLINT_API bool beginFrame()
Acquire the next swapchain texture and prepare the initial GPU command buffer.
FLINT_API GPURenderer(IWindow *window, IAssetLoader &assetLoader, ShaderFormat shaderFormat)
Construct the renderer and initialize the underlying GPU device for the window.
FLINT_API void setViewport(const SDL_GPUViewport &viewport)
Set or override the default rendering viewport.
FLINT_API std::string getShaderPath(const std::string &basePath) const
Get the full asset path for a shader base name including system format extension.
FLINT_API void endFrame()
Close active render passes, submit the command buffer to the GPU, and present.
const FloatRect & getCurrentCameraView() const
Return the camera view rectangle used for the current frame projection.
Definition renderer.h:192
FLINT_API void draw(const std::function< void(RenderQueue &)> &overlayEmit={})
Execute all queued draw commands in layer and submission order.
void setGPUPresentMode(SDL_GPUPresentMode mode)
Configure GPU swapchain presentation mode for the window.
Definition renderer.h:164
const SDL_GPUViewport & getDefaultViewport() const
Return default full-screen viewport bounds.
Definition renderer.h:207
SDL_GPUDevice * getGPUDevice() const
Return the native SDL GPU device owned by this renderer.
Definition renderer.h:197
FLINT_API SDL_Renderer * getSDLRenderer() const
Return legacy SDL_Renderer handle if running alongside an SDL software fallback pass.
Abstract interface for loading raw asset data by path.
Abstract interface for a platform window.
Definition IWindow.h:27
Batches drawables into sorted geometry, primitive, and custom execution passes for GPURenderer.
Alpha-blended pipeline for rendering UI elements (text, panels, icons).
Definition ui-pipeline.h:36
RGBA color with 8-bit unsigned components.
Definition color.h:13