Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
Flint::GPURenderer Class Reference

High-level SDL GPU renderer managing pipeline state, batching, and frame presentation. More...

#include <flint/graphics/renderer.h>

+ Collaboration diagram for Flint::GPURenderer:

Public Member Functions

FLINT_API GPURenderer (IWindow *window, IAssetLoader &assetLoader, ShaderFormat shaderFormat)
 Construct the renderer and initialize the underlying GPU device for the window.
 
FLINT_API bool beginFrame ()
 Acquire the next swapchain texture and prepare the initial GPU command buffer.
 
FLINT_API void draw (const std::function< void(RenderQueue &)> &overlayEmit={})
 Execute all queued draw commands in layer and submission order.
 
FLINT_API void endFrame ()
 Close active render passes, submit the command buffer to the GPU, and present.
 
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 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.
 
FLINT_API void setViewport (const SDL_GPUViewport &viewport)
 Set or override the default rendering viewport.
 
FLINT_API void setCamera (const Camera &camera)
 Update the active camera view transform used for batch coordinate projection.
 
std::unique_ptr< Shader > createShader () const
 Factory method creating a new Shader instance bound to this GPU device.
 
FLINT_API std::vector< char > loadShader (const std::string &basePath) const
 Load raw shader bytecode bytes from the asset loader.
 
FLINT_API std::string getShaderPath (const std::string &basePath) const
 Get the full asset path for a shader base name including system format extension.
 
ShaderFormat getShaderFormat () const
 Get the active GPU shader format.
 
SDL_GPURenderPass * getRenderPass () const
 Return the current frame's active SDL GPU render pass handle.
 
void setGPUPresentMode (SDL_GPUPresentMode mode)
 Configure GPU swapchain presentation mode for the window.
 
SDL_GPUCommandBuffer * getCommandBuffer () const
 Return the current frame's active SDL GPU command buffer handle.
 
const FloatRect & getCurrentCameraView () const
 Return the camera view rectangle used for the current frame projection.
 
SDL_GPUDevice * getGPUDevice () const
 Return the native SDL GPU device owned by this renderer.
 
IAssetLoader & getAssetLoader () const
 Return reference to the core asset loader.
 
const SDL_GPUViewport & getDefaultViewport () const
 Return default full-screen viewport bounds.
 
FLINT_API SDL_Renderer * getSDLRenderer () const
 Return legacy SDL_Renderer handle if running alongside an SDL software fallback pass.
 
UIPipeline & getUIPipeline ()
 Return reference to the UI pipeline wrapper.
 
PrimitivePipeline & getPrimitivePipeline ()
 Return reference to the primitive point/line pipeline wrapper.
 
SDL_GPUTexture * getWhiteTexture ()
 Return shared 1x1 fallback white GPU texture handle.
 

Detailed Description

High-level SDL GPU renderer managing pipeline state, batching, and frame presentation.

Owns the SDL GPU device, swapchain lifecycle, and built-in pipeline wrappers (Batch, Primitive, UI).

Standard Frame Execution Lifecycle:

  1. beginFrame() — Acquire swapchain texture and initialize GPU command pass.
  2. submit() — Queue drawables sorted by DrawLayer.
  3. draw() — Construct RenderQueue, emit draw geometry, drain upload callbacks, and execute batched passes.
  4. endFrame() — Close render pass, submit command buffer to GPU, and present.

Definition at line 40 of file renderer.h.

Constructor & Destructor Documentation

◆ GPURenderer()

FLINT_API Flint::GPURenderer::GPURenderer ( IWindow * window,
IAssetLoader & assetLoader,
ShaderFormat shaderFormat )
explicit

Construct the renderer and initialize the underlying GPU device for the window.

Parameters
windowTarget window receiving rendered frames.
assetLoaderAsset loader used to load built-in shader bytecode.
shaderFormatActive GPU shader format (SPIR-V, MSL, or DXIL).

Member Function Documentation

◆ beginFrame()

FLINT_API bool Flint::GPURenderer::beginFrame ( )

Acquire the next swapchain texture and prepare the initial GPU command buffer.

Must be called once per frame prior to any submit() or draw() operations.

Returns
true if swapchain texture acquisition succeeded, false otherwise.

◆ createShader()

std::unique_ptr< Shader > Flint::GPURenderer::createShader ( ) const
inlinenodiscard

Factory method creating a new Shader instance bound to this GPU device.

Returns
std::unique_ptr<Shader> Initialized shader wrapper.

Definition at line 124 of file renderer.h.

◆ draw()

FLINT_API void Flint::GPURenderer::draw ( const std::function< void(RenderQueue &)> & overlayEmit = {})

Execute all queued draw commands in layer and submission order.

Performs the multi-stage draw execution:

  1. Sorts submitted drawables by DrawLayer.
  2. Constructs a transient RenderQueue bound to FrameData and emits draw geometry.
  3. Closes the initial pass and uploads geometry/line VBOs and IBOs to GPU memory.
  4. Drains all deferred resource upload callbacks (deferUpload).
  5. Re-opens the render pass and dispatches batched geometry, line, and custom draw runs.
  6. Clears commands and resets transient frame data storage.

◆ endFrame()

FLINT_API void Flint::GPURenderer::endFrame ( )

Close active render passes, submit the command buffer to the GPU, and present.

Must be called once per frame after draw().

◆ getAssetLoader()

IAssetLoader & Flint::GPURenderer::getAssetLoader ( ) const
inlinenodiscard

Return reference to the core asset loader.

Definition at line 202 of file renderer.h.

◆ getCommandBuffer()

SDL_GPUCommandBuffer * Flint::GPURenderer::getCommandBuffer ( ) const
inlinenodiscard

Return the current frame's active SDL GPU command buffer handle.

Deprecated
During emission (Drawable::emit), command buffer access is restricted. Use RenderQueue::deferUpload() for pre-render GPU uploads or RenderQueue::custom() for inline GPU execution callbacks.

Definition at line 187 of file renderer.h.

◆ getCurrentCameraView()

const FloatRect & Flint::GPURenderer::getCurrentCameraView ( ) const
inlinenodiscard

Return the camera view rectangle used for the current frame projection.

Definition at line 192 of file renderer.h.

◆ getDefaultViewport()

const SDL_GPUViewport & Flint::GPURenderer::getDefaultViewport ( ) const
inlinenodiscard

Return default full-screen viewport bounds.

Definition at line 207 of file renderer.h.

◆ getGPUDevice()

SDL_GPUDevice * Flint::GPURenderer::getGPUDevice ( ) const
inlinenodiscard

Return the native SDL GPU device owned by this renderer.

Definition at line 197 of file renderer.h.

◆ getPrimitivePipeline()

PrimitivePipeline & Flint::GPURenderer::getPrimitivePipeline ( )
inlinenodiscard

Return reference to the primitive point/line pipeline wrapper.

Definition at line 221 of file renderer.h.

◆ getRenderPass()

SDL_GPURenderPass * Flint::GPURenderer::getRenderPass ( ) const
inlinenodiscard

Return the current frame's active SDL GPU render pass handle.

Deprecated
During emission (Drawable::emit), no render pass is active because geometry is batched into CPU streams first. Do not query or store this handle during submit/emit passes. Use RenderQueue::custom() to enqueue raw GPU render commands instead.

Definition at line 156 of file renderer.h.

◆ getShaderFormat()

ShaderFormat Flint::GPURenderer::getShaderFormat ( ) const
inlinenodiscard

Get the active GPU shader format.

Definition at line 143 of file renderer.h.

◆ getShaderPath()

FLINT_API std::string Flint::GPURenderer::getShaderPath ( const std::string & basePath) const
nodiscard

Get the full asset path for a shader base name including system format extension.

Parameters
basePathBase shader path.
Returns
std::string Resolved shader asset path.

◆ getUIPipeline()

UIPipeline & Flint::GPURenderer::getUIPipeline ( )
inlinenodiscard

Return reference to the UI pipeline wrapper.

Definition at line 216 of file renderer.h.

◆ getWhiteTexture()

SDL_GPUTexture * Flint::GPURenderer::getWhiteTexture ( )
inlinenodiscard

Return shared 1x1 fallback white GPU texture handle.

Definition at line 226 of file renderer.h.

◆ loadShader()

FLINT_API std::vector< char > Flint::GPURenderer::loadShader ( const std::string & basePath) const
nodiscard

Load raw shader bytecode bytes from the asset loader.

Parameters
basePathBase path to shader asset without file extension.
Returns
std::vector<char> Shader binary data buffer.

◆ setCamera()

FLINT_API void Flint::GPURenderer::setCamera ( const Camera & camera)

Update the active camera view transform used for batch coordinate projection.

Parameters
cameraActive world camera.

◆ setClearColor()

FLINT_API void Flint::GPURenderer::setClearColor ( Color color)

Set the RGBA clear color applied at the start of each frame.

Parameters
colorClear color values.

◆ setGPUPresentMode()

void Flint::GPURenderer::setGPUPresentMode ( SDL_GPUPresentMode mode)
inline

Configure GPU swapchain presentation mode for the window.

Parameters
modeDesired present mode (VSYNC, IMMEDIATE, or MAILBOX).

Definition at line 164 of file renderer.h.

◆ setViewport()

FLINT_API void Flint::GPURenderer::setViewport ( const SDL_GPUViewport & viewport)

Set or override the default rendering viewport.

Parameters
viewportTarget GPU viewport bounds.

◆ submit() [1/2]

FLINT_API void Flint::GPURenderer::submit ( const Drawable * drawable,
DrawLayer layer = DrawLayer::Midground )

Queue a raw Drawable pointer for rendering in the current frame.

Deprecated
Use submit(std::shared_ptr<const Drawable>, DrawLayer) instead to prevent dangling references.
Parameters
drawableNon-owning pointer to the drawable. Must remain valid through draw().
layerRender layer sorting priority; defaults to DrawLayer::Midground.

◆ submit() [2/2]

FLINT_API void Flint::GPURenderer::submit ( std::shared_ptr< const Drawable > drawable,
DrawLayer layer = DrawLayer::Midground )

Queue a shared Drawable for rendering in the current frame.

Parameters
drawableDrawable to render (shared ownership).
layerRender layer sorting priority; defaults to DrawLayer::Midground.

The documentation for this class was generated from the following file: