SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Renderer.hpp
Go to the documentation of this file.
1#pragma once
2
3
4#include "RenderContext.hpp"
5#include <Core/OSDef.hpp>
6#include <Core/Timer.hpp>
7
8namespace Sleak {
9
10namespace RenderEngine {
11
12/// Selects which graphics API a Renderer instance targets.
19
20/// Per-backend feature capability bits, queried to gate optional pipeline stages.
21enum GraphicsCaps : uint32_t {
22 CapDeferred = 1 << 0,
23 CapSSAO = 1 << 1,
24 CapSSR = 1 << 2,
25 CapTAA = 1 << 3,
26 CapBloom = 1 << 4,
27 CapIBL = 1 << 5,
29 CapShadows = 1 << 7,
30 CapHDRTarget = 1 << 8,
31 CapVelocity = 1 << 9,
32 CapLightShaft = 1 << 10,
34};
35
36/// Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
37class ENGINE_API Renderer {
38public:
39 virtual ~Renderer() = 0; // Pure virtual destructor
40
41 /// Creates the device, swapchain, and backend resources.
42 virtual bool Initialize() = 0;
43 /// Acquires the next frame and prepares it for command recording.
44 virtual void BeginRender() = 0;
45 /// Submits recorded commands and presents the frame.
46 virtual void EndRender() = 0;
47 virtual void Cleanup() = 0;
48 /// Blocks until the GPU has finished all outstanding work.
49 virtual void WaitIdle() {}
50 /// Flushes any buffered upload/staging transfers still in flight.
51 virtual void FlushPendingTransfers() {}
52
53 // GPU memory tracking (overridden by VulkanRenderer)
54 virtual size_t GetGPUMemoryUsed() const { return 0; }
55 virtual size_t GetGPUMemoryBudget() const { return 0; }
56
57 /// Recreates swapchain-dependent resources for a new surface size.
58 virtual void Resize(uint32_t width, uint32_t height) = 0;
59
60 /// Initializes ImGui integration for this backend.
61 virtual bool CreateImGUI() = 0;
62
63 /// Returns the backend's command-recording interface.
64 virtual RenderContext* GetContext() = 0;
65
66 inline RendererType GetType() const
67 {
68 return Type;
69 }
70
71 inline const char* GetTypeStr() const {
72 switch(GetType())
73 {
74 case RendererType::DirectX12: return "DirectX 12";
75 case RendererType::DirectX11: return "DirectX 11";
76 case RendererType::Vulkan: return "Vulkan";
77 case RendererType::OpenGL: return "OpenGL";
78 default: return "Unknown!";
79 }
80 }
81
82 /// Feature capability mask; backends override with the audited truth.
83 virtual uint32_t GetFeatureCaps() const { return CapShadows; }
84
85 inline RenderMode GetRenderMode() const {
86 return Mode;
87 }
88
89 inline void SetRenderDrawMode(RenderMode mode) {
90 Mode = mode;
92 }
93
94 inline void SetRenderCullFace(RenderFace face) {
95 Face = face;
97 }
98
99 inline int GetFrameRate() const {
100 return frameRate;
101 }
102
103 inline float GetFrameTime() const {
104 return frameTime;
105 }
106
107 inline float GetRamUsage() const {
108 return UsedRam;
109 }
110
111 inline float GetCPUUsage() const {
112 return UsedCPU;
113 }
114
115 inline int GetVertices() const {
116 return DisplayVertices;
117 }
118
119 inline int GetTriangles() const {
120 return DisplayTriangles;
121 }
122
125 }
126
127 inline void SetPerformanceCounter(bool value) {
129 }
130
131 inline void SetImGUI(bool bEnable) {bImInitialized = bEnable;}
132 inline bool GetImGUIEnabled() { return bImInitialized; }
133
134 // Shadow mapping support (overridden by VulkanRenderer)
135 virtual void UpdateShadowLightUBO(const void* data, uint32_t size) { (void)data; (void)size; }
136 virtual void SetLightVP(const float* lightVP) { (void)lightVP; }
137 void SetShadowPassEnabled(bool enabled) { m_shadowPassEnabled = enabled; }
139
140 // VSync
141 virtual void SetVSync(bool enabled) {
142 if (enabled == m_vsync) return;
143 m_vsync = enabled;
145 }
146 bool GetVSync() const { return m_vsync; }
147 virtual void ApplyVSyncChange() {}
148
149 // Post-process: ACES Tonemapping
150 void SetTonemappingEnabled(bool enabled) { m_tonemapEnabled = enabled; }
151 bool IsTonemappingEnabled() const { return m_tonemapEnabled; }
152 void SetExposure(float exposure) { m_exposure = exposure; }
153 float GetExposure() const { return m_exposure; }
154 void SetGamma(float gamma) { m_gamma = gamma; }
155 float GetGamma() const { return m_gamma; }
156
157 // Post-process: SSAO
158 void SetSSAOEnabled(bool enabled) { m_ssaoEnabled = enabled; }
159 bool IsSSAOEnabled() const { return m_ssaoEnabled; }
160 void SetSSAORadius(float radius) { m_ssaoRadius = radius; }
161 float GetSSAORadius() const { return m_ssaoRadius; }
162 void SetSSAOBias(float bias) { m_ssaoBias = bias; }
163 float GetSSAOBias() const { return m_ssaoBias; }
164 void SetSSAOPower(float power) { m_ssaoPower = power; }
165 float GetSSAOPower() const { return m_ssaoPower; }
166
167 // IBL (Image Based Lighting)
168 void SetIBLEnabled(bool enabled) { m_iblEnabled = enabled; }
169 bool IsIBLEnabled() const { return m_iblEnabled; }
170 void SetIBLIntensity(float intensity) { m_iblIntensity = intensity; }
171 float GetIBLIntensity() const { return m_iblIntensity; }
172
173 // Post-process: SSR (Screen-Space Reflections)
174 void SetSSREnabled(bool enabled) { m_ssrEnabled = enabled; }
175 bool IsSSREnabled() const { return m_ssrEnabled; }
176
177 // Post-process: TAA (Temporal Anti-Aliasing)
178 void SetTAAEnabled(bool enabled) { m_taaEnabled = enabled; }
179 bool IsTAAEnabled() const { return m_taaEnabled; }
180
181 // Post-process: Bloom
182 void SetBloomEnabled(bool enabled) { m_bloomEnabled = enabled; }
183 bool IsBloomEnabled() const { return m_bloomEnabled; }
184
185 // PCSS Soft Shadows
186 void SetPCSSEnabled(bool enabled) { m_pcssEnabled = enabled; }
187 bool IsPCSSEnabled() const { return m_pcssEnabled; }
188
189 // Shadow map resolution. Takes effect at shadow-resource creation; after
190 // that it is queued until the backend recreates shadow resources, so the
191 // getter always reflects the LIVE map size (LightManager texel math
192 // depends on this).
193 /// Requests a new shadow map size, clamped to [256, 8192] and queued if resources already exist.
194 void SetShadowMapResolution(uint32_t res) {
195 if (res < 256 || res > 8192) return;
196 if (res == m_shadowMapResolution) return;
200 return;
201 }
203 }
206
207 // Deferred rendering mode (default: enabled)
208 void SetDeferredEnabled(bool enabled) { m_deferredEnabled = enabled; }
209 bool GetDeferredEnabled() const { return m_deferredEnabled; }
210
211 // Anti-aliasing (MSAA)
212 /// Requests a new MSAA sample count; rejects invalid values and samples above 1 while deferred shading is on.
213 virtual void SetMSAASampleCount(uint32_t samples) {
214 // Validate: must be 1, 2, 4, or 8
215 if (samples != 1 && samples != 2 && samples != 4 && samples != 8)
216 return;
217 if (samples > m_maxMsaaSampleCount)
218 samples = m_maxMsaaSampleCount;
219 if (samples == m_msaaSampleCount)
220 return;
221 // MSAA is incompatible with the deferred GBuffer resolve model:
222 // GBuffer color/depth attachments are 1-sample, so a render pass
223 // built with >1 samples produces attachment/pipeline sample
224 // mismatches. Silently reject — keep the current sample count.
225 if (m_deferredEnabled && samples > 1)
226 return;
227 m_pendingMsaaSampleCount = samples;
229 }
230 uint32_t GetMSAASampleCount() const { return m_msaaSampleCount; }
231 uint32_t GetMaxMSAASampleCount() const { return m_maxMsaaSampleCount; }
232 virtual void ApplyMSAAChange() {}
233
234 protected:
235 /// Applies the current RenderMode (fill/wireframe/points) to backend state.
236 virtual void ConfigureRenderMode() = 0;
237 /// Applies the current RenderFace (cull mode) to backend state.
238 virtual void ConfigureRenderFace() = 0;
239
240 /// Rolls up frame count into rate/time/triangle stats once per MetricUpdateInterval.
242 if (!bEnabledPerformanceCounter) return;
243 m_frameCount++;
244 float elapsed = m_frameTimer.Elapsed(); // seconds since last reset
245 if (elapsed >= MetricUpdateInterval) {
246 frameRate = static_cast<int>(m_frameCount / elapsed);
247 frameTime = (elapsed / m_frameCount) * 1000.0f; // ms per frame
250 m_frameCount = 0;
251 m_frameTimer.Reset();
252 DrawnVertices = 0;
253 DrawnTriangles = 0;
254 }
255 }
256
257 // Performance counter
259 int frameRate = 0;
260 float frameTime = 0;
261 float UsedRam = 0;
262 float UsedCPU = 0;
268 uint32_t m_frameCount = 0;
269
270 // ImGUI
271 bool bImInitialized = false;
272 bool bImFrameActive = false; // true when ImGui::NewFrame was called this frame
273
274 // Renderer
278
279 // Shadow pass
281
282 // Post-process: Tonemapping
283 // Disabled by default — game renders in sRGB space, not linear HDR.
284 // Enable only when the lighting pipeline explicitly works in linear space.
285 bool m_tonemapEnabled = false;
286 float m_exposure = 1.0f;
287 float m_gamma = 2.2f;
288
289 // Post-process: SSAO
290 bool m_ssaoEnabled = false;
291 float m_ssaoRadius = 0.8f;
292 float m_ssaoBias = 0.025f;
293 float m_ssaoPower = 2.0f;
294
295 // IBL
296 bool m_iblEnabled = false;
297 // 0.5 is balanced for LDR sky panoramas — additive on top of hemisphere
298 // ambient gives noticeable IBL character without blowing out brightness.
299 // Crank toward 1.5+ once you swap in an HDR environment map.
300 float m_iblIntensity = 0.5f;
301
302 // Post-process: SSR / TAA / Bloom
303 bool m_ssrEnabled = false;
304 bool m_taaEnabled = false;
305 bool m_bloomEnabled = true;
306
307 // PCSS
308 bool m_pcssEnabled = true;
309
310 // Shadow map resolution
311 uint32_t m_shadowMapResolution = 2048;
315
316 // Deferred rendering
317 bool m_deferredEnabled = true;
318
319 // VSync state
320 bool m_vsync = false;
322
323 // MSAA state
324 uint32_t m_msaaSampleCount = 1;
328
329 static constexpr float MetricUpdateInterval = 0.5f; // seconds
330};
331
332inline Renderer::~Renderer() {} // Provide definition for pure virtual destructor
333
334}
335}
336
int width
int height
Abstract interface for high-level graphics command execution and resource management.
Abstract render backend: swapchain, frame lifecycle, and post-effect toggles.
Definition Renderer.hpp:37
virtual void ConfigureRenderMode()=0
Applies the current RenderMode (fill/wireframe/points) to backend state.
RendererType GetType() const
Definition Renderer.hpp:66
void SetShadowPassEnabled(bool enabled)
Definition Renderer.hpp:137
virtual size_t GetGPUMemoryBudget() const
Definition Renderer.hpp:55
const char * GetTypeStr() const
Definition Renderer.hpp:71
virtual bool Initialize()=0
Creates the device, swapchain, and backend resources.
static constexpr float MetricUpdateInterval
Definition Renderer.hpp:329
void SetSSAOEnabled(bool enabled)
Definition Renderer.hpp:158
void SetSSAOBias(float bias)
Definition Renderer.hpp:162
void SetImGUI(bool bEnable)
Definition Renderer.hpp:131
void SetIBLEnabled(bool enabled)
Definition Renderer.hpp:168
virtual void ConfigureRenderFace()=0
Applies the current RenderFace (cull mode) to backend state.
void SetPCSSEnabled(bool enabled)
Definition Renderer.hpp:186
void UpdateFrameMetrics()
Rolls up frame count into rate/time/triangle stats once per MetricUpdateInterval.
Definition Renderer.hpp:241
virtual void EndRender()=0
Submits recorded commands and presents the frame.
virtual size_t GetGPUMemoryUsed() const
Definition Renderer.hpp:54
virtual void ApplyShadowResolutionChange()
Definition Renderer.hpp:205
void SetTonemappingEnabled(bool enabled)
Definition Renderer.hpp:150
uint32_t GetMSAASampleCount() const
Definition Renderer.hpp:230
virtual void ApplyVSyncChange()
Definition Renderer.hpp:147
void SetSSREnabled(bool enabled)
Definition Renderer.hpp:174
void SetRenderDrawMode(RenderMode mode)
Definition Renderer.hpp:89
uint32_t GetMaxMSAASampleCount() const
Definition Renderer.hpp:231
void SetTAAEnabled(bool enabled)
Definition Renderer.hpp:178
virtual void WaitIdle()
Blocks until the GPU has finished all outstanding work.
Definition Renderer.hpp:49
void SetPerformanceCounter(bool value)
Definition Renderer.hpp:127
void SetShadowMapResolution(uint32_t res)
Requests a new shadow map size, clamped to [256, 8192] and queued if resources already exist.
Definition Renderer.hpp:194
RenderMode GetRenderMode() const
Definition Renderer.hpp:85
virtual void SetLightVP(const float *lightVP)
Definition Renderer.hpp:136
virtual void BeginRender()=0
Acquires the next frame and prepares it for command recording.
virtual bool CreateImGUI()=0
Initializes ImGui integration for this backend.
void SetBloomEnabled(bool enabled)
Definition Renderer.hpp:182
virtual RenderContext * GetContext()=0
Returns the backend's command-recording interface.
void SetSSAORadius(float radius)
Definition Renderer.hpp:160
void SetExposure(float exposure)
Definition Renderer.hpp:152
virtual void SetVSync(bool enabled)
Definition Renderer.hpp:141
void SetIBLIntensity(float intensity)
Definition Renderer.hpp:170
virtual void FlushPendingTransfers()
Flushes any buffered upload/staging transfers still in flight.
Definition Renderer.hpp:51
void SetRenderCullFace(RenderFace face)
Definition Renderer.hpp:94
virtual void SetMSAASampleCount(uint32_t samples)
Requests a new MSAA sample count; rejects invalid values and samples above 1 while deferred shading i...
Definition Renderer.hpp:213
void SetSSAOPower(float power)
Definition Renderer.hpp:164
void SetGamma(float gamma)
Definition Renderer.hpp:154
uint32_t GetShadowMapResolution() const
Definition Renderer.hpp:204
virtual void UpdateShadowLightUBO(const void *data, uint32_t size)
Definition Renderer.hpp:135
void SetDeferredEnabled(bool enabled)
Definition Renderer.hpp:208
virtual void Resize(uint32_t width, uint32_t height)=0
Recreates swapchain-dependent resources for a new surface size.
virtual uint32_t GetFeatureCaps() const
Feature capability mask; backends override with the audited truth.
Definition Renderer.hpp:83
Backend-facing rendering layer shared by the four graphics backends.
RendererType
Selects which graphics API a Renderer instance targets.
Definition Renderer.hpp:13
RenderMode
Rasterizer fill style for a draw.
RenderFace
Which triangle winding gets culled.
GraphicsCaps
Per-backend feature capability bits, queried to gate optional pipeline stages.
Definition Renderer.hpp:21
Root namespace for everything the engine exposes.
Definition Camera.hpp:10