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