VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
renderer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 11.09.2025.
5//
6#pragma once
7
8#include <memory>
9
11#include "shadowMap.h"
12#include "shadowRenderer.h"
14#include "shadowRendererLocal.h"
15#include "scene/scene.h"
18
19namespace visutwin::canvas
20{
21 class Camera;
22 class RenderTarget;
23 class Layer;
24
25 /*
26 * The base renderer functionality to allow implementation of specialized renderers
27 */
29 {
30 public:
31 Renderer(const std::shared_ptr<GraphicsDevice>& device, const std::shared_ptr<Scene>& scene);
32
33 void renderForwardLayer(Camera* camera, RenderTarget* renderTarget, Layer* layer, bool transparent);
34
35 // Collects directional shadow-casting lights for a camera, allocates shadow maps,
36 // and calls ShadowRendererDirectional::cull() to position shadow cameras.
37 // Must be called once per frame before buildFrameGraph().
38 void cullShadowmaps(Camera* camera);
39
40 protected:
41 std::shared_ptr<GraphicsDevice> _device;
42
43 std::shared_ptr<Scene> _scene;
44
45 std::shared_ptr<RenderPassUpdateClustered> _renderPassUpdateClustered;
46
47 std::unique_ptr<ShadowRendererLocal> _shadowRendererLocal;
48
49 // A list of all unique lights in the layer composition
50 std::vector<Light*> _lights;
51
52 // A list of all unique local lights (spot & omni) in the layer composition
53 std::vector<Light*> _localLights;
54
55 // A list of unique directional shadow casting lights for each enabled camera.
56 // Generated each frame during light culling.
57 std::unordered_map<Camera*, std::vector<Light*>> _cameraDirShadowLights;
58
59 ShadowRendererDirectional* shadowRendererDirectional() const { return _shadowRendererDirectional.get(); }
60
61 // ShadowMaps owned by the renderer, kept alive for the lifetime of the lights that use them.
62 std::vector<std::unique_ptr<ShadowMap>> _ownedShadowMaps;
63
64 private:
65 friend class Engine;
66 friend class ShadowRenderer;
67
68 std::unique_ptr<ShadowRenderer> _shadowRenderer;
69 std::unique_ptr<ShadowRendererDirectional> _shadowRendererDirectional;
70
71 std::unique_ptr<LightTextureAtlas> _lightTextureAtlas;
72
73 // Clustered lighting: CPU-side 3D grid that indexes local lights.
74 // Created lazily when Scene::clusteredLightingEnabled() is true.
75 std::unique_ptr<WorldClusters> _worldClusters;
76
77 int _forwardDrawCalls = 0;
78 int _materialSwitches = 0;
79 int _depthMapTime = 0;
80 int _forwardTime = 0;
81 int _sortTime = 0;
82
83 // timing
84 int _skinTime = 0;
85 int _morphTime = 0;
86 int _cullTime = 0;
87 int _shadowMapTime = 0;
88 int _lightClustersTime = 0;
89 int _layerCompositionUpdateTime = 0;
90
91 int _shadowMapUpdates = 0;
92 int _shadowDrawCalls = 0;
93 int _skinDrawCalls = 0;
94 int _instancedDrawCalls = 0;
95 int _numDrawCallsCulled = 0;
96 int _camerasRendered = 0;
97 int _lightClusters = 0;
98 int _gsplatCount = 0;
99
100 std::array<int, PRIMITIVE_TRIFAN - PRIMITIVE_POINTS + 1> _primsPerFrame;
101 };
102}
Perspective or orthographic camera with projection matrix, jitter (TAA), and render target binding.
Definition camera.h:40
std::shared_ptr< Scene > _scene
Definition renderer.h:43
std::vector< Light * > _lights
Definition renderer.h:50
ShadowRendererDirectional * shadowRendererDirectional() const
Definition renderer.h:59
friend class ShadowRenderer
Definition renderer.h:66
std::shared_ptr< RenderPassUpdateClustered > _renderPassUpdateClustered
Definition renderer.h:45
void renderForwardLayer(Camera *camera, RenderTarget *renderTarget, Layer *layer, bool transparent)
Definition renderer.cpp:218
std::unordered_map< Camera *, std::vector< Light * > > _cameraDirShadowLights
Definition renderer.h:57
std::unique_ptr< ShadowRendererLocal > _shadowRendererLocal
Definition renderer.h:47
std::shared_ptr< GraphicsDevice > _device
Definition renderer.h:41
std::vector< Light * > _localLights
Definition renderer.h:53
void cullShadowmaps(Camera *camera)
Definition renderer.cpp:170
std::vector< std::unique_ptr< ShadowMap > > _ownedShadowMaps
Definition renderer.h:62
Renderer(const std::shared_ptr< GraphicsDevice > &device, const std::shared_ptr< Scene > &scene)
Definition renderer.cpp:149
@ PRIMITIVE_POINTS
Definition mesh.h:19
@ PRIMITIVE_TRIFAN
Definition mesh.h:25