Polyphase Game Engine
Loading...
Searching...
No Matches
Renderer.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include "glm/glm.hpp"
5#include <array>
6
7#include "EngineTypes.h"
8#include "Assets/Texture.h"
9#include "Assets/StaticMesh.h"
10#include "Assets/MaterialLite.h"
11#include "Vertex.h"
12#include "World.h"
13#include "Constants.h"
14#include "Log.h"
15#include "Profiler.h"
16
17class Widget;
18class Console;
19class StatsOverlay;
20class CameraFrustum;
21
22struct EngineState;
23
25{
26 Light3D* mComponent = nullptr;
27 float mDistance2 = 0.0f;
28
29 LightDistance2(Light3D* comp, float dist2) : mComponent(comp), mDistance2(dist2) {}
30};
31
32// Per-method POLYPHASE_API on Get() only. A class-level export forces MSVC to
33// emit Renderer's implicit ctor/dtor bodies in every TU that includes this
34// header, which in turn instantiates ~SharedPtr<Console> / ~SharedPtr<StatsOverlay>
35// against the forward declarations above → C4150 ("deletion of pointer to
36// incomplete type"). Addons only need the singleton accessor anyway.
38{
39public:
40
41 static void Create();
42 static void Destroy();
43 POLYPHASE_API static Renderer* Get();
44
45 void Render(World* world, int32_t screenIndex);
46#if EDITOR
47 void RenderSecondScreen(World* world, class Image* colorTarget, class Image* depthTarget,
48 uint32_t width, uint32_t height, class Camera3D* cameraOverride = nullptr,
49 int32_t targetScreen = -1,
50 bool drawAccumulatedDebugDraws = true);
51#endif
52 ~Renderer();
53 void Initialize();
54
55 void GatherProperties(std::vector<Property>& props);
56
57 void EnableStatsOverlay(bool enable);
58 bool IsStatsOverlayEnabled() const;
59 void EnableConsole(bool enable);
60 bool IsConsoleEnabled();
61
62 void DirtyAllWidgets();
63
66
67 void SetEngineState(EngineState* engineState);
68 uint32_t GetFrameNumber() const;
69 uint32_t GetFrameIndex() const;
70 uint32_t GetScreenIndex() const;
71 bool IsRenderingFirstScreen() const;
72 bool IsRenderingLastScreen() const;
74
75 glm::vec2 GetScreenResolution(int32_t screen = -1);
76 glm::vec2 GetActiveScreenResolution();
77
78 float GetGlobalUiScale() const;
79 void SetGlobalUiScale(float scale);
80
81 Node3D* ProcessHitCheck(World* world, int32_t x, int32_t y, uint32_t* outInstance = nullptr);
82
83 void SetDebugMode(DebugMode mode);
84 DebugMode GetDebugMode() const;
85 void EnableProxyRendering(bool enable);
86 bool IsProxyRenderingEnabled() const;
89
90 void EnableFrustumCulling(bool enable);
91 bool IsFrustumCullingEnabled() const;
92
93 void Enable3dRendering(bool enable);
94 bool Is3dRenderingEnabled() const;
95 void Enable2dRendering(bool enable);
96 bool Is2dRenderingEnabled() const;
97
98 void EnablePathTracing(bool enable);
99 bool IsPathTracingEnabled() const;
100
103
104 void LoadDefaultTextures();
106 void LoadDefaultMeshes();
107 void LoadDefaultFonts();
108
109 void AddDebugDraw(const DebugDraw& draw);
110 void RemoveDebugDrawsForNode(Node* node);
111 const std::vector<DebugDraw>& GetDebugDraws() const;
112
113 // ===== Custom Render Pass (for native addons) =====
114 //
115 // Lets an addon submit GFX_* draw calls during the engine's main forward
116 // pass — i.e. inside an active BeginRenderPass/EndRenderPass and after the
117 // camera's view/projection have been bound. Without this hook addons can
118 // only submit Gizmos:: draws, which are #if EDITOR only.
119 //
120 // The callback is invoked after opaque + translucent + debug draws and
121 // immediately before the engine draws the world's debug-line buffer, so
122 // alpha-blended geometry composites correctly against the rendered scene.
123 // Invoked once per frame per world Render() call. Order across multiple
124 // registered passes is registration order.
125 //
126 // Hot-reload safe: addons should Unregister in their OnUnload.
127 typedef void (*CustomRenderPassFn)(void* userData);
128 POLYPHASE_API uint64_t RegisterCustomRenderPass(CustomRenderPassFn fn, void* userData);
130
131 // Engine-internal: run all registered callbacks. Called from Renderer::Render.
133
134 const std::vector<LightData>& GetLightData() const;
135
136 void SetClearColor(const glm::vec4& color);
137 glm::vec4 GetClearColor() const;
138
139 void BeginLightBake();
140 void EndLightBake();
141 bool IsLightBakeInProgress() const;
142 float GetLightBakeProgress() const;
143
144 bool IsLightFadeEnabled() const;
145 void EnableLightFade(bool enable);
146 void SetLightFadeLimit(uint32_t limit);
147 uint32_t GetLightFadeLimit() const;
148 void SetLightFadeSpeed(float speed);
149 float GetLightFadeSpeed() const;
150
151 void SetColorScale(float colorScale);
152 float GetColorScale() const;
153 float GetColorScaleInverse() const;
154
155 void SetSelectedColor(const glm::vec4& color);
156 glm::vec4 GetSelectedColor() const;
157 void SetSelectedCheckerSize(float size);
158 float GetSelectedCheckerSize() const;
159
161 void EnablePostProcessPass(PostProcessPassId passId, bool enable);
162
163 void SetResolutionScale(float scale);
164 float GetResolutionScale() const;
165
166 uint32_t GetViewportX(int32_t screenIdx = -1);
167 uint32_t GetViewportY(int32_t screenIdx = -1);
168 uint32_t GetViewportWidth(int32_t screenIdx = -1);
169 uint32_t GetViewportHeight(int32_t screenIdx = -1);
170 glm::uvec4 GetViewport(int32_t screenIdx = -1);
171 glm::uvec4 GetSceneViewport(int32_t screenIdx = -1);
172
173 // Property Getters
174 uint32_t GetRaysPerPixel() const;
175 uint32_t GetMaxBounces() const;
177 uint32_t GetBakeRaysPerVertex() const;
178 uint32_t GetBakeMaxBounces() const;
179 float GetBakeShadowBias() const;
180 uint32_t GetBakeIndirectIterations() const;
181 uint32_t GetBakeDiffuseDirectPasses() const;
182 uint32_t GetBakeDiffuseIndirectPasses() const;
183 glm::vec4 GetSkyZenithColor() const;
184 glm::vec4 GetSkyHorizonColor() const;
185 glm::vec4 GetGroundColor() const;
186
187 // Default Textures
194
195 // Environment / IBL
197
198 // Default Materials
200
201 // Default Meshes
209
210 // Default Fonts
215
216private:
217
218 static Renderer* sInstance;
219 Renderer();
220
221 void BeginFrame();
222 void EndFrame();
223
224 void GatherDrawData(World* world);
225 void GatherLightData(World* world);
226 void RenderDraws(const std::vector<DrawData>& drawData);
227 void RenderDraws(const std::vector<DrawData>& drawData, PipelineConfig pipelineConfig);
228 void RenderDebugDraws(const std::vector<DebugDraw>& draws, PipelineConfig pipelineConfig = PipelineConfig::Count);
229 void FrustumCull(Camera3D* camera);
230 int32_t FrustumCullDraws(const CameraFrustum& frustum, std::vector<DrawData>& drawData);
231 int32_t FrustumCullDraws(const CameraFrustum& frustum, std::vector<DebugDraw>& drawData);
232 int32_t FrustumCullLights(const CameraFrustum& frustum, std::vector<LightData>& lightData);
233
234 void RenderShadowCasters(World* world);
235 void RenderSelectedGeometry(World* world);
236
237 void UpdateDebugDraws();
238
239 SharedPtr<StatsOverlay> mStatsWidget;
240 SharedPtr<Console> mConsoleWidget;
241
242 EngineState* mEngineState = nullptr;
243
244 bool mInitialized = false;
245
246 std::vector<DrawData> mShadowDraws;
247 std::vector<DrawData> mOpaqueDraws;
248 std::vector<DrawData> mSimpleShadowDraws;
249 std::vector<DrawData> mPostShadowOpaqueDraws; // (post-simple-shadow opaques. not talking about shadow mapping)
250 std::vector<DrawData> mTranslucentDraws;
251 std::vector<DrawData> mWireframeDraws;
252 std::vector<DrawData> mWidgetDraws;
253
254 std::vector<LightData> mLightData;
255
256 std::vector<DebugDraw> mDebugDraws;
257 std::vector<DebugDraw> mCollisionDraws;
258
259 // Custom render passes registered by native addons. See RegisterCustomRenderPass.
260 struct CustomRenderPassEntry
261 {
262 uint64_t mId;
264 void* mUserData;
265 };
266 std::vector<CustomRenderPassEntry> mCustomRenderPasses;
267 uint64_t mNextCustomRenderPassId = 1;
268
269 World* mCurrentWorld = nullptr;
270 uint32_t mFrameIndex = 0;
271 uint32_t mScreenIndex = 0;
272 int32_t mTargetScreenFilter = -1; // -1 = no filter, 0 = top screen, 1 = bottom screen
273 uint32_t mFrameNumber = 0;
274 float mGlobalUiScale = 1.0f;
275 DebugMode mDebugMode = DEBUG_NONE;
276 BoundsDebugMode mBoundsDebugMode = BoundsDebugMode::Off;
277 bool mFrustumCulling = true;
278 bool mEnableProxyRendering = false;
279 bool mEnable3dRendering = true;
280 bool mEnable2dRendering = true;
281 bool mEnablePathTracing = false;
282 bool mEnableLightFade = false;
283 uint32_t mLightFadeLimit = 4;
284 float mLightFadeSpeed = 1.0f;
285 glm::vec4 mClearColor = {};
286 float mColorScale = 1.0f;
287 glm::vec4 mSelectedColor = { 0.2f, 0.1f, 1.0f, 0.6f };
288 float mSelectedCheckerSize = 8.0f;
289
290 // Path tracing
291 uint32_t mRaysPerPixel = 4;
292 uint32_t mMaxBounces = 4;
293 bool mPathTraceAccumulate = true;
294 glm::vec4 mSkyZenithColor = { 0.0f, 0.0f, 0.0f, 1.0f };
295 glm::vec4 mSkyHorizonColor = { 0.0f, 0.0f, 0.0f, 1.0f };
296 glm::vec4 mGroundColor = { 0.0f, 0.0f, 0.0f, 1.0f };
297
298 // Light Bake
299 uint32_t mBakeRaysPerVertex = 4;
300 uint32_t mBakeMaxBounces = 4;
301 float mBakeShadowBias = 0.001f;
302 uint32_t mBakeIndirectIterations = 20;
303 uint32_t mBakeDiffuseDirectPasses = 2;
304 uint32_t mBakeDiffuseIndirectPasses = 2;
305
306 // Post Process
307 bool mPostProcessEnables[(uint32_t)PostProcessPassId::Count] = { };
308
309};
PostProcessPassId
Definition EngineTypes.h:181
DebugMode
Definition Enums.h:33
@ DEBUG_NONE
Definition Enums.h:34
BoundsDebugMode
Definition Enums.h:41
PipelineConfig
Definition GraphicsTypes.h:66
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition AssetRef.h:18
Definition Camera3d.h:12
Definition CameraFrustum.h:8
Definition Console.h:14
Definition Light3d.h:6
Definition MaterialLite.h:51
Definition Node3d.h:14
Definition Node.h:67
Definition Renderer.h:38
FontRef mRoboto16
Definition Renderer.h:212
void RunCustomRenderPasses()
Definition Renderer.cpp:540
FontRef mRobotoMono16
Definition Renderer.h:213
MaterialRef mDefaultMaterial
Definition Renderer.h:199
void EnablePathTracing(bool enable)
Definition Renderer.cpp:241
bool IsRenderingLastScreen() const
Definition Renderer.cpp:286
void LoadDefaultMeshes()
Definition Renderer.cpp:449
static void Destroy()
Definition Renderer.cpp:74
void BeginLightBake()
Definition Renderer.cpp:1943
void SetSelectedColor(const glm::vec4 &color)
Definition Renderer.cpp:1923
bool IsRenderingFirstScreen() const
Definition Renderer.cpp:281
void SetLightFadeSpeed(float speed)
Definition Renderer.cpp:1983
void Initialize()
Definition Renderer.cpp:103
void EnableStatsOverlay(bool enable)
Definition Renderer.cpp:357
void LoadDefaultTextures()
Definition Renderer.cpp:434
void SetDebugMode(DebugMode mode)
Definition Renderer.cpp:178
uint32_t GetRaysPerPixel() const
Definition Renderer.cpp:2068
glm::uvec4 GetSceneViewport(int32_t screenIdx=-1)
Definition Renderer.cpp:2220
glm::vec4 GetSkyZenithColor() const
Definition Renderer.cpp:2113
static POLYPHASE_API Renderer * Get()
Definition Renderer.cpp:83
uint32_t GetBakeIndirectIterations() const
Definition Renderer.cpp:2098
uint32_t GetViewportWidth(int32_t screenIdx=-1)
Definition Renderer.cpp:2169
glm::vec4 GetGroundColor() const
Definition Renderer.cpp:2123
uint32_t GetScreenIndex() const
Definition Renderer.cpp:276
bool IsConsoleEnabled()
Definition Renderer.cpp:395
FontRef mRobotoMono8
Definition Renderer.h:214
void EnableConsole(bool enable)
Definition Renderer.cpp:387
bool Is2dRenderingEnabled() const
Definition Renderer.cpp:236
bool IsProxyRenderingEnabled() const
Definition Renderer.cpp:196
glm::vec4 GetSelectedColor() const
Definition Renderer.cpp:1928
void Render(World *world, int32_t screenIndex)
Definition Renderer.cpp:1316
void EnableLightFade(bool enable)
Definition Renderer.cpp:1968
void RemoveDebugDrawsForNode(Node *node)
Definition Renderer.cpp:498
glm::vec4 GetClearColor() const
Definition Renderer.cpp:1918
bool IsPathTraceAccumulationEnabled() const
Definition Renderer.cpp:2078
POLYPHASE_API uint64_t RegisterCustomRenderPass(CustomRenderPassFn fn, void *userData)
Definition Renderer.cpp:516
bool IsStatsOverlayEnabled() const
Definition Renderer.cpp:378
uint32_t GetViewportX(int32_t screenIdx=-1)
Definition Renderer.cpp:2129
glm::vec4 GetSkyHorizonColor() const
Definition Renderer.cpp:2118
TextureRef mDefaultORMTexture
Definition Renderer.h:193
Texture * GetBlackTexture()
Definition Renderer.cpp:251
void Enable2dRendering(bool enable)
Definition Renderer.cpp:231
void SetLightFadeLimit(uint32_t limit)
Definition Renderer.cpp:1973
static void Create()
Definition Renderer.cpp:67
float GetLightFadeSpeed() const
Definition Renderer.cpp:1988
bool IsLightFadeEnabled() const
Definition Renderer.cpp:1963
void AddDebugDraw(const DebugDraw &draw)
Definition Renderer.cpp:491
Node3D * ProcessHitCheck(World *world, int32_t x, int32_t y, uint32_t *outInstance=nullptr)
Definition Renderer.cpp:173
bool IsPostProcessPassEnabled(PostProcessPassId passId) const
Definition Renderer.cpp:2028
void LoadDefaultFonts()
Definition Renderer.cpp:481
uint32_t GetBakeDiffuseDirectPasses() const
Definition Renderer.cpp:2103
float GetResolutionScale() const
Definition Renderer.cpp:2063
float GetBakeShadowBias() const
Definition Renderer.cpp:2093
float GetLightBakeProgress() const
Definition Renderer.cpp:1958
StaticMeshRef mCylinderMesh
Definition Renderer.h:204
uint32_t GetViewportY(int32_t screenIdx=-1)
Definition Renderer.cpp:2149
void Enable3dRendering(bool enable)
Definition Renderer.cpp:221
void SetSelectedCheckerSize(float size)
Definition Renderer.cpp:1933
const std::vector< DebugDraw > & GetDebugDraws() const
Definition Renderer.cpp:511
void SetEngineState(EngineState *engineState)
Definition Renderer.cpp:261
uint32_t GetBakeMaxBounces() const
Definition Renderer.cpp:2088
const std::vector< LightData > & GetLightData() const
Definition Renderer.cpp:1908
void EnableProxyRendering(bool enable)
Definition Renderer.cpp:191
void SetBoundsDebugMode(BoundsDebugMode mode)
Definition Renderer.cpp:201
void GatherProperties(std::vector< Property > &props)
Definition Renderer.cpp:137
StaticMeshRef mSphere112Mesh
Definition Renderer.h:207
void SetColorScale(float colorScale)
Definition Renderer.cpp:1993
void(* CustomRenderPassFn)(void *userData)
Definition Renderer.h:127
TextureRef mDefaultColorTexture
Definition Renderer.h:190
bool IsPathTracingEnabled() const
Definition Renderer.cpp:246
Console * GetConsoleWidget()
Definition Renderer.cpp:415
StatsOverlay * GetStatsWidget()
Definition Renderer.cpp:420
float GetColorScale() const
Definition Renderer.cpp:2012
glm::vec2 GetActiveScreenResolution()
Definition Renderer.cpp:324
void SetClearColor(const glm::vec4 &color)
Definition Renderer.cpp:1913
DebugMode GetDebugMode() const
Definition Renderer.cpp:186
TextureRef mDefaultColorAlphaTexture
Definition Renderer.h:191
StaticMeshRef mSphereMesh
Definition Renderer.h:206
uint32_t GetBakeDiffuseIndirectPasses() const
Definition Renderer.cpp:2108
void SetGlobalUiScale(float scale)
Definition Renderer.cpp:334
uint32_t GetLightFadeLimit() const
Definition Renderer.cpp:1978
uint32_t GetFrameIndex() const
Definition Renderer.cpp:271
void DirtyAllWidgets()
Definition Renderer.cpp:400
StaticMeshRef mCubeMesh
Definition Renderer.h:203
BoundsDebugMode GetBoundsDebugMode() const
Definition Renderer.cpp:206
glm::vec2 GetScreenResolution(int32_t screen=-1)
Definition Renderer.cpp:300
uint32_t GetFrameNumber() const
Definition Renderer.cpp:266
TextureRef mDefaultNormalTexture
Definition Renderer.h:192
void SetResolutionScale(float scale)
Definition Renderer.cpp:2057
bool Is3dRenderingEnabled() const
Definition Renderer.cpp:226
void LoadDefaultMaterials()
Definition Renderer.cpp:444
void EnablePostProcessPass(PostProcessPassId passId, bool enable)
Definition Renderer.cpp:2046
void EndLightBake()
Definition Renderer.cpp:1948
StaticMeshRef mConeMesh
Definition Renderer.h:202
StaticMeshRef mPlaneMesh
Definition Renderer.h:205
TextureRef mBlackTexture
Definition Renderer.h:189
MaterialLite * GetDefaultMaterial()
Definition Renderer.cpp:256
TextureRef mWhiteTexture
Definition Renderer.h:188
float GetSelectedCheckerSize() const
Definition Renderer.cpp:1938
TextureRef mEnvironmentMap
Definition Renderer.h:196
World * GetCurrentWorld()
Definition Renderer.cpp:295
uint32_t GetMaxBounces() const
Definition Renderer.cpp:2073
uint32_t GetViewportHeight(int32_t screenIdx=-1)
Definition Renderer.cpp:2192
void EnableFrustumCulling(bool enable)
Definition Renderer.cpp:211
bool IsFrustumCullingEnabled() const
Definition Renderer.cpp:216
bool IsLightBakeInProgress() const
Definition Renderer.cpp:1953
POLYPHASE_API void UnregisterCustomRenderPass(uint64_t id)
Definition Renderer.cpp:527
float GetGlobalUiScale() const
Definition Renderer.cpp:329
float GetColorScaleInverse() const
Definition Renderer.cpp:2018
FontRef mRoboto32
Definition Renderer.h:211
StaticMeshRef mTorusMesh
Definition Renderer.h:208
glm::uvec4 GetViewport(int32_t screenIdx=-1)
Definition Renderer.cpp:2215
~Renderer()
Definition Renderer.cpp:88
uint32_t GetBakeRaysPerVertex() const
Definition Renderer.cpp:2083
Definition SmartPointer.h:33
Definition StatsOverlay.h:26
Definition Texture.h:10
Definition Widget.h:53
Definition World.h:24
Definition EngineTypes.h:231
Definition EngineTypes.h:359
Definition Renderer.h:25
float mDistance2
Definition Renderer.h:27
LightDistance2(Light3D *comp, float dist2)
Definition Renderer.h:29
Light3D * mComponent
Definition Renderer.h:26