Skip to content

Renderer ​

Renderer interface for drawing a scene with a given camera.

The renderer owns GPU state and draw logic for rendering a Scene with a specified Camera. Construct one alongside your Window, and call Render once per frame.

This class defines the rendering interface only. The actual rendering implementation is provided by a backend, and multiple backends (for example, OpenGL or Vulkan) may exist behind this interface.

cpp
vglx::Renderer renderer({
  .clear_color = 0x444444u
});

auto ok = renderer.Initialize(window);
if (!ok) {
  HandleError(ok.error());
}

Once initialized, the renderer reads the framebuffer size and content scale from the window at the start of every frame, so the viewport follows window resizes without any user code. Call SetViewport only to draw into a sub-region of the framebuffer. The renderer must be destroyed before the window it was initialized with.

Construction ​

Constructors ​


Renderer() ​

Constructs a renderer.

GPU resources are not created until Initialize is called.

cpp
Renderer::Renderer(const Renderer::Parameters& params);
ParameterDescription
paramsInitialization parameters for constructing the renderer.

Types ​

Renderer::ShadowMap enum ​

Shadow mapping method applied to shadow-casting lights.

ValueDescription
NoneShadows disabled.
BasicSingle-sample depth-mapped shadows with hard edges.
PCFPercentage-closer filtered (PCF) shadows with soft edges.

Renderer::ToneMapping enum ​

Tone mapping operator applied to the final HDR frame before display.

ValueDescription
NoneNo tone mapping. HDR values are clamped to [0, 1].
ACESFilmicACES filmic curve. Compresses highlights and preserves shadows.

Renderer::DriverInfo struct ​

Driver and hardware identity strings reported by the rendering backend.

ParameterDescription
vendor std::stringDriver vendor string.
renderer std::stringHardware renderer string.
version std::stringGraphics API version string.
glsl_version std::stringShading language version string.

Renderer::Limits struct ​

Hardware limits reported by the rendering backend.

ParameterDescription
max_anisotropy floatMax anisotropy for texture sampling.
max_samples intMax MSAA sample count.
max_texture_units intMax texture units per shader.
max_texture_size intMax 2D texture dimension.
max_cube_map_size intMax cube map face dimension.
max_renderbuffer_size intMax renderbuffer dimension.

Renderer::Parameters struct ​

Parameters for constructing a Renderer object.

ParameterDescription
sample_count intAntialiasing level (e.g., 4x MSAA).
clear_color ColorClear color used at the start of a frame.
auto_clear boolAutomatic buffer clearing at the start of a frame.
tone_mapping ToneMappingTone mapping operator applied to the final frame.
exposure floatExposure scale applied to HDR values before tone mapping.
shadow_map ShadowMapShadow mapping method applied to the scene.

Renderer::Viewport struct ​

Viewport rectangle in framebuffer pixels.

The origin is the bottom-left corner of the framebuffer.

ParameterDescription
x intLeft edge in pixels.
y intBottom edge in pixels.
width intWidth in pixels.
height intHeight in pixels.

Functions ​

Clear() void ​

Clears the color and depth buffers of the given target.

Intended for compositing flows where automatic clearing is disabled (see SetAutoClear). Call it once at the start of a frame, then render multiple scenes into the same target.

cpp
void Renderer::Clear(RenderTarget* target=nullptr);
ParameterDescription
targetPointer to the render target to clear or nullptr for default framebuffer.

GetDriverInfo() const DriverInfo& ​

Returns driver and hardware identity strings reported by the rendering backend.

Must be called after Initialize has succeeded.

cpp
const DriverInfo& Renderer::GetDriverInfo() const;

GetLimits() const Limits& ​

Returns hardware limits reported by the rendering backend.

Must be called after Initialize has succeeded.

cpp
const Limits& Renderer::GetLimits() const;

Initialize() std::expected<void, std::string> ​

Initializes GPU state and binds the renderer to a window.

The window must be initialized first. From this point on the renderer reads the framebuffer size and content scale from the window at the start of every frame that targets the default framebuffer so resizes are handled automatically. The renderer keeps a reference to the window for its lifetime and must be destroyed before it.

cpp
std::expected<void, std::string> Renderer::Initialize(Window& window);
ParameterDescription
windowThe window whose framebuffer the renderer presents to.

Render() void ​

Renders the given scene from the specified camera.

The scene is expected to be in a consistent state for rendering. If you are using the runtime path, this is handled automatically. In direct initialization flows, call the per-frame update routine Scene::Advance prior to rendering.

When rendering to the default framebuffer, the scene's canvas is drawn on top of the tone-mapped frame in display space. Canvas is skipped when rendering to a render target.

cpp
void Renderer::Render(Scene* scene, Camera* camera, RenderTarget* target=nullptr);
ParameterDescription
scenePointer to the scene to render.
cameraPointer to the active camera.
targetPointer to the render target to clear or nullptr for default framebuffer.

RenderedObjectsPerFrame() size_t ​

Returns the number of renderable objects drawn in the last frame.

Intended for statistics overlays and debugging.

cpp
size_t Renderer::RenderedObjectsPerFrame() const;

SetAutoClear() void ​

Sets whether the color and depth buffers are cleared before drawing.

When enabled (the default), each call to Render clears the color and depth buffers before drawing. Disable it to composite multiple scenes into the same frame, such as rendering a screen-space overlay on top of a previously rendered scene, using Clear to clear explicitly when needed.

cpp
void Renderer::SetAutoClear(bool auto_clear);
ParameterDescription
auto_clearClear the depth and color buffers automatically at the start of each frame.

SetClearColor() void ​

Sets the clear color for subsequent frames.

The color is applied at the start of each frame when the framebuffer is cleared. Typically used to define the background color of the rendering surface.

cpp
void Renderer::SetClearColor(const Color& color);
ParameterDescription
colorClear color in RGB format.

SetExposure() void ​

Sets the exposure scale for subsequent frames.

Multiplies HDR color values before tone mapping is applied. Higher values brighten the image; lower values darken it.

cpp
void Renderer::SetExposure(float exposure);
ParameterDescription
exposureLinear exposure multiplier. Defaults to 1.0.

SetShadowMap() void ​

Sets the shadow mapping method for subsequent frames.

Switching methods may compile new shader variants on the next frame. Selecting ShadowMap::None releases all shadow map resources. Switching back re-creates them on demand.

cpp
void Renderer::SetShadowMap(ShadowMap shadow_map);
ParameterDescription
shadow_mapShadow mapping method to apply.

SetToneMapping() void ​

Sets the tone mapping operator for subsequent frames.

Changes how HDR color values are mapped to the display range. Takes effect immediately on the next call to Render.

cpp
void Renderer::SetToneMapping(ToneMapping tone_mapping);
ParameterDescription
tone_mappingTone mapping operator to apply.

SetViewport() void ​

Pins the viewport to an explicit rectangle.

By default the viewport covers the full framebuffer of the window passed to Initialize and follows it across resizes. Calling this method replaces that behavior with a fixed rectangle.

cpp
void Renderer::SetViewport(const Viewport& viewport);
ParameterDescription
viewportViewport rectangle to draw into, in framebuffer pixels.

Released under the MIT License.