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 // Fills a CameraFrustum from the camera's current matrices. Used by the
94 // cull pass and by World::IsSeenByCamera.
95 static void BuildCameraFrustum(Camera3D* camera, CameraFrustum& outFrustum);
96
97 // Baked occlusion culling (see OcclusionData). Applies to game cameras;
98 // the editor camera only culls when the preview toggle is on.
99 void EnableOcclusionCulling(bool enable);
100 bool IsOcclusionCullingEnabled() const;
101 void EnableOcclusionPreview(bool enable);
102 bool IsOcclusionPreviewEnabled() const;
103 int32_t GetNumFrustumCulled() const;
104 int32_t GetNumOcclusionCulled() const;
105
106 void Enable3dRendering(bool enable);
107 bool Is3dRenderingEnabled() const;
108 void Enable2dRendering(bool enable);
109 bool Is2dRenderingEnabled() const;
110
111 void EnablePathTracing(bool enable);
112 bool IsPathTracingEnabled() const;
113
116
117 void LoadDefaultTextures();
119 void LoadDefaultMeshes();
120 void LoadDefaultFonts();
121
122 void AddDebugDraw(const DebugDraw& draw);
123 void RemoveDebugDrawsForNode(Node* node);
124 const std::vector<DebugDraw>& GetDebugDraws() const;
125
126 // ===== Custom Render Pass (for native addons) =====
127 //
128 // Lets an addon submit GFX_* draw calls during the engine's main forward
129 // pass — i.e. inside an active BeginRenderPass/EndRenderPass and after the
130 // camera's view/projection have been bound. Without this hook addons can
131 // only submit Gizmos:: draws, which are #if EDITOR only.
132 //
133 // The callback is invoked after opaque + translucent + debug draws and
134 // immediately before the engine draws the world's debug-line buffer, so
135 // alpha-blended geometry composites correctly against the rendered scene.
136 // Invoked once per frame per world Render() call. Order across multiple
137 // registered passes is registration order.
138 //
139 // Hot-reload safe: addons should Unregister in their OnUnload.
140 typedef void (*CustomRenderPassFn)(void* userData);
141 POLYPHASE_API uint64_t RegisterCustomRenderPass(CustomRenderPassFn fn, void* userData);
143
144 // Engine-internal: run all registered callbacks. Called from Renderer::Render.
146
147 const std::vector<LightData>& GetLightData() const;
148
149 void SetClearColor(const glm::vec4& color);
150 glm::vec4 GetClearColor() const;
151
152 void BeginLightBake();
153 void EndLightBake();
154 bool IsLightBakeInProgress() const;
155 float GetLightBakeProgress() const;
156
157 bool IsLightFadeEnabled() const;
158 void EnableLightFade(bool enable);
159 void SetLightFadeLimit(uint32_t limit);
160 uint32_t GetLightFadeLimit() const;
161 void SetLightFadeSpeed(float speed);
162 float GetLightFadeSpeed() const;
163
164 void SetColorScale(float colorScale);
165 float GetColorScale() const;
166 float GetColorScaleInverse() const;
167
168 void SetSelectedColor(const glm::vec4& color);
169 glm::vec4 GetSelectedColor() const;
170 void SetSelectedCheckerSize(float size);
171 float GetSelectedCheckerSize() const;
172
174 void EnablePostProcessPass(PostProcessPassId passId, bool enable);
175
176 void SetResolutionScale(float scale);
177 float GetResolutionScale() const;
178
179 uint32_t GetViewportX(int32_t screenIdx = -1);
180 uint32_t GetViewportY(int32_t screenIdx = -1);
181 uint32_t GetViewportWidth(int32_t screenIdx = -1);
182 uint32_t GetViewportHeight(int32_t screenIdx = -1);
183 glm::uvec4 GetViewport(int32_t screenIdx = -1);
184 glm::uvec4 GetSceneViewport(int32_t screenIdx = -1);
185
186 // Property Getters
187 uint32_t GetRaysPerPixel() const;
188 uint32_t GetMaxBounces() const;
190 uint32_t GetBakeRaysPerVertex() const;
191 uint32_t GetBakeMaxBounces() const;
192 float GetBakeShadowBias() const;
193 uint32_t GetBakeIndirectIterations() const;
194 uint32_t GetBakeDiffuseDirectPasses() const;
195 uint32_t GetBakeDiffuseIndirectPasses() const;
196 glm::vec4 GetSkyZenithColor() const;
197 glm::vec4 GetSkyHorizonColor() const;
198 glm::vec4 GetGroundColor() const;
199
200 // Default Textures
207
208 // Environment / IBL
210
211 // Default Materials
213
214 // Default Meshes
222
223 // Default Fonts
228
229private:
230
231 static Renderer* sInstance;
232 Renderer();
233
234 void BeginFrame();
235 void EndFrame();
236
237 void GatherDrawData(World* world);
238 void GatherLightData(World* world);
239 bool PassesTargetScreenFilter(Node* node, World* world) const;
240 void RenderDraws(const std::vector<DrawData>& drawData);
241 void RenderDraws(const std::vector<DrawData>& drawData, PipelineConfig pipelineConfig);
242 void RenderDebugDraws(const std::vector<DebugDraw>& draws, PipelineConfig pipelineConfig = PipelineConfig::Count);
243 void FrustumCull(Camera3D* camera);
244 void OcclusionCull(World* world, Camera3D* camera);
245 int32_t OcclusionCullDraws(const class OcclusionData& data, int32_t cell, const uint32_t* visibleSet, std::vector<DrawData>& drawData);
246 bool IsDrawOcclusionVisible(const class OcclusionData& data, int32_t cell, const uint32_t* visibleSet, const DrawData& draw) const;
247 void DrawOcclusionDebug(World* world, const class OcclusionData* data, int32_t cell, const uint32_t* visibleSet);
248 int32_t FrustumCullDraws(const CameraFrustum& frustum, std::vector<DrawData>& drawData);
249 int32_t FrustumCullDraws(const CameraFrustum& frustum, std::vector<DebugDraw>& drawData);
250 int32_t FrustumCullLights(const CameraFrustum& frustum, std::vector<LightData>& lightData);
251
252 void RenderShadowCasters(World* world);
253 void RenderSelectedGeometry(World* world);
254
255 void UpdateDebugDraws();
256
257 SharedPtr<StatsOverlay> mStatsWidget;
258 SharedPtr<Console> mConsoleWidget;
259
260 EngineState* mEngineState = nullptr;
261
262 bool mInitialized = false;
263
264 std::vector<DrawData> mShadowDraws;
265 std::vector<DrawData> mOpaqueDraws;
266 std::vector<DrawData> mSimpleShadowDraws;
267 std::vector<DrawData> mPostShadowOpaqueDraws; // (post-simple-shadow opaques. not talking about shadow mapping)
268 std::vector<DrawData> mTranslucentDraws;
269 std::vector<DrawData> mWireframeDraws;
270 std::vector<DrawData> mWidgetDraws;
271
272 std::vector<LightData> mLightData;
273
274 std::vector<DebugDraw> mDebugDraws;
275 std::vector<DebugDraw> mCollisionDraws;
276
277 // Custom render passes registered by native addons. See RegisterCustomRenderPass.
278 struct CustomRenderPassEntry
279 {
280 uint64_t mId;
282 void* mUserData;
283 };
284 std::vector<CustomRenderPassEntry> mCustomRenderPasses;
285 uint64_t mNextCustomRenderPassId = 1;
286
287 World* mCurrentWorld = nullptr;
288 uint32_t mFrameIndex = 0;
289 uint32_t mScreenIndex = 0;
290 int32_t mTargetScreenFilter = -1; // -1 = no filter, 0 = top screen, 1 = bottom screen
291 uint32_t mFrameNumber = 0;
292 float mGlobalUiScale = 1.0f;
293 DebugMode mDebugMode = DEBUG_NONE;
294 BoundsDebugMode mBoundsDebugMode = BoundsDebugMode::Off;
295 bool mFrustumCulling = true;
296 bool mOcclusionCulling = true;
297 bool mOcclusionPreview = false;
298 int32_t mNumFrustumCulled = 0;
299 int32_t mNumOcclusionCulled = 0;
300 bool mEnableProxyRendering = false;
301 bool mEnable3dRendering = true;
302 bool mEnable2dRendering = true;
303 bool mEnablePathTracing = false;
304 bool mEnableLightFade = false;
305 uint32_t mLightFadeLimit = 4;
306 float mLightFadeSpeed = 1.0f;
307 glm::vec4 mClearColor = {};
308 float mColorScale = 1.0f;
309 glm::vec4 mSelectedColor = { 0.2f, 0.1f, 1.0f, 0.6f };
310 float mSelectedCheckerSize = 8.0f;
311
312 // Path tracing
313 uint32_t mRaysPerPixel = 4;
314 uint32_t mMaxBounces = 4;
315 bool mPathTraceAccumulate = true;
316 glm::vec4 mSkyZenithColor = { 0.0f, 0.0f, 0.0f, 1.0f };
317 glm::vec4 mSkyHorizonColor = { 0.0f, 0.0f, 0.0f, 1.0f };
318 glm::vec4 mGroundColor = { 0.0f, 0.0f, 0.0f, 1.0f };
319
320 // Light Bake
321 uint32_t mBakeRaysPerVertex = 4;
322 uint32_t mBakeMaxBounces = 4;
323 float mBakeShadowBias = 0.001f;
324 uint32_t mBakeIndirectIterations = 20;
325 uint32_t mBakeDiffuseDirectPasses = 2;
326 uint32_t mBakeDiffuseIndirectPasses = 2;
327
328 // Post Process
329 bool mPostProcessEnables[(uint32_t)PostProcessPassId::Count] = { };
330
331};
PostProcessPassId
Definition EngineTypes.h:185
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 OcclusionData.h:30
Definition Renderer.h:38
FontRef mRoboto16
Definition Renderer.h:225
void RunCustomRenderPasses()
Definition Renderer.cpp:575
FontRef mRobotoMono16
Definition Renderer.h:226
MaterialRef mDefaultMaterial
Definition Renderer.h:212
void EnablePathTracing(bool enable)
Definition Renderer.cpp:276
bool IsRenderingLastScreen() const
Definition Renderer.cpp:321
void LoadDefaultMeshes()
Definition Renderer.cpp:484
static void Destroy()
Definition Renderer.cpp:79
void BeginLightBake()
Definition Renderer.cpp:2270
void SetSelectedColor(const glm::vec4 &color)
Definition Renderer.cpp:2250
bool IsRenderingFirstScreen() const
Definition Renderer.cpp:316
bool IsOcclusionPreviewEnabled() const
Definition Renderer.cpp:241
void SetLightFadeSpeed(float speed)
Definition Renderer.cpp:2310
void Initialize()
Definition Renderer.cpp:108
void EnableStatsOverlay(bool enable)
Definition Renderer.cpp:392
void LoadDefaultTextures()
Definition Renderer.cpp:469
int32_t GetNumOcclusionCulled() const
Definition Renderer.cpp:251
void SetDebugMode(DebugMode mode)
Definition Renderer.cpp:183
uint32_t GetRaysPerPixel() const
Definition Renderer.cpp:2395
glm::uvec4 GetSceneViewport(int32_t screenIdx=-1)
Definition Renderer.cpp:2547
glm::vec4 GetSkyZenithColor() const
Definition Renderer.cpp:2440
void EnableOcclusionCulling(bool enable)
Definition Renderer.cpp:226
static void BuildCameraFrustum(Camera3D *camera, CameraFrustum &outFrustum)
Definition Renderer.cpp:1177
static POLYPHASE_API Renderer * Get()
Definition Renderer.cpp:88
uint32_t GetBakeIndirectIterations() const
Definition Renderer.cpp:2425
uint32_t GetViewportWidth(int32_t screenIdx=-1)
Definition Renderer.cpp:2496
glm::vec4 GetGroundColor() const
Definition Renderer.cpp:2450
uint32_t GetScreenIndex() const
Definition Renderer.cpp:311
bool IsConsoleEnabled()
Definition Renderer.cpp:430
FontRef mRobotoMono8
Definition Renderer.h:227
void EnableConsole(bool enable)
Definition Renderer.cpp:422
bool IsOcclusionCullingEnabled() const
Definition Renderer.cpp:231
bool Is2dRenderingEnabled() const
Definition Renderer.cpp:271
bool IsProxyRenderingEnabled() const
Definition Renderer.cpp:201
glm::vec4 GetSelectedColor() const
Definition Renderer.cpp:2255
void Render(World *world, int32_t screenIndex)
Definition Renderer.cpp:1578
void EnableLightFade(bool enable)
Definition Renderer.cpp:2295
void RemoveDebugDrawsForNode(Node *node)
Definition Renderer.cpp:533
glm::vec4 GetClearColor() const
Definition Renderer.cpp:2245
bool IsPathTraceAccumulationEnabled() const
Definition Renderer.cpp:2405
POLYPHASE_API uint64_t RegisterCustomRenderPass(CustomRenderPassFn fn, void *userData)
Definition Renderer.cpp:551
bool IsStatsOverlayEnabled() const
Definition Renderer.cpp:413
uint32_t GetViewportX(int32_t screenIdx=-1)
Definition Renderer.cpp:2456
glm::vec4 GetSkyHorizonColor() const
Definition Renderer.cpp:2445
TextureRef mDefaultORMTexture
Definition Renderer.h:206
Texture * GetBlackTexture()
Definition Renderer.cpp:286
void Enable2dRendering(bool enable)
Definition Renderer.cpp:266
void SetLightFadeLimit(uint32_t limit)
Definition Renderer.cpp:2300
static void Create()
Definition Renderer.cpp:72
int32_t GetNumFrustumCulled() const
Definition Renderer.cpp:246
float GetLightFadeSpeed() const
Definition Renderer.cpp:2315
bool IsLightFadeEnabled() const
Definition Renderer.cpp:2290
void AddDebugDraw(const DebugDraw &draw)
Definition Renderer.cpp:526
Node3D * ProcessHitCheck(World *world, int32_t x, int32_t y, uint32_t *outInstance=nullptr)
Definition Renderer.cpp:178
bool IsPostProcessPassEnabled(PostProcessPassId passId) const
Definition Renderer.cpp:2355
void LoadDefaultFonts()
Definition Renderer.cpp:516
uint32_t GetBakeDiffuseDirectPasses() const
Definition Renderer.cpp:2430
float GetResolutionScale() const
Definition Renderer.cpp:2390
float GetBakeShadowBias() const
Definition Renderer.cpp:2420
float GetLightBakeProgress() const
Definition Renderer.cpp:2285
StaticMeshRef mCylinderMesh
Definition Renderer.h:217
uint32_t GetViewportY(int32_t screenIdx=-1)
Definition Renderer.cpp:2476
void Enable3dRendering(bool enable)
Definition Renderer.cpp:256
void SetSelectedCheckerSize(float size)
Definition Renderer.cpp:2260
const std::vector< DebugDraw > & GetDebugDraws() const
Definition Renderer.cpp:546
void SetEngineState(EngineState *engineState)
Definition Renderer.cpp:296
uint32_t GetBakeMaxBounces() const
Definition Renderer.cpp:2415
const std::vector< LightData > & GetLightData() const
Definition Renderer.cpp:2235
void EnableProxyRendering(bool enable)
Definition Renderer.cpp:196
void SetBoundsDebugMode(BoundsDebugMode mode)
Definition Renderer.cpp:206
void GatherProperties(std::vector< Property > &props)
Definition Renderer.cpp:142
StaticMeshRef mSphere112Mesh
Definition Renderer.h:220
void SetColorScale(float colorScale)
Definition Renderer.cpp:2320
void(* CustomRenderPassFn)(void *userData)
Definition Renderer.h:140
void EnableOcclusionPreview(bool enable)
Definition Renderer.cpp:236
TextureRef mDefaultColorTexture
Definition Renderer.h:203
bool IsPathTracingEnabled() const
Definition Renderer.cpp:281
Console * GetConsoleWidget()
Definition Renderer.cpp:450
StatsOverlay * GetStatsWidget()
Definition Renderer.cpp:455
float GetColorScale() const
Definition Renderer.cpp:2339
glm::vec2 GetActiveScreenResolution()
Definition Renderer.cpp:359
void SetClearColor(const glm::vec4 &color)
Definition Renderer.cpp:2240
DebugMode GetDebugMode() const
Definition Renderer.cpp:191
TextureRef mDefaultColorAlphaTexture
Definition Renderer.h:204
StaticMeshRef mSphereMesh
Definition Renderer.h:219
uint32_t GetBakeDiffuseIndirectPasses() const
Definition Renderer.cpp:2435
void SetGlobalUiScale(float scale)
Definition Renderer.cpp:369
uint32_t GetLightFadeLimit() const
Definition Renderer.cpp:2305
uint32_t GetFrameIndex() const
Definition Renderer.cpp:306
void DirtyAllWidgets()
Definition Renderer.cpp:435
StaticMeshRef mCubeMesh
Definition Renderer.h:216
BoundsDebugMode GetBoundsDebugMode() const
Definition Renderer.cpp:211
glm::vec2 GetScreenResolution(int32_t screen=-1)
Definition Renderer.cpp:335
uint32_t GetFrameNumber() const
Definition Renderer.cpp:301
TextureRef mDefaultNormalTexture
Definition Renderer.h:205
void SetResolutionScale(float scale)
Definition Renderer.cpp:2384
bool Is3dRenderingEnabled() const
Definition Renderer.cpp:261
void LoadDefaultMaterials()
Definition Renderer.cpp:479
void EnablePostProcessPass(PostProcessPassId passId, bool enable)
Definition Renderer.cpp:2373
void EndLightBake()
Definition Renderer.cpp:2275
StaticMeshRef mConeMesh
Definition Renderer.h:215
StaticMeshRef mPlaneMesh
Definition Renderer.h:218
TextureRef mBlackTexture
Definition Renderer.h:202
MaterialLite * GetDefaultMaterial()
Definition Renderer.cpp:291
TextureRef mWhiteTexture
Definition Renderer.h:201
float GetSelectedCheckerSize() const
Definition Renderer.cpp:2265
TextureRef mEnvironmentMap
Definition Renderer.h:209
World * GetCurrentWorld()
Definition Renderer.cpp:330
uint32_t GetMaxBounces() const
Definition Renderer.cpp:2400
uint32_t GetViewportHeight(int32_t screenIdx=-1)
Definition Renderer.cpp:2519
void EnableFrustumCulling(bool enable)
Definition Renderer.cpp:216
bool IsFrustumCullingEnabled() const
Definition Renderer.cpp:221
bool IsLightBakeInProgress() const
Definition Renderer.cpp:2280
POLYPHASE_API void UnregisterCustomRenderPass(uint64_t id)
Definition Renderer.cpp:562
float GetGlobalUiScale() const
Definition Renderer.cpp:364
float GetColorScaleInverse() const
Definition Renderer.cpp:2345
FontRef mRoboto32
Definition Renderer.h:224
StaticMeshRef mTorusMesh
Definition Renderer.h:221
glm::uvec4 GetViewport(int32_t screenIdx=-1)
Definition Renderer.cpp:2542
~Renderer()
Definition Renderer.cpp:93
uint32_t GetBakeRaysPerVertex() const
Definition Renderer.cpp:2410
Definition SmartPointer.h:33
Definition StatsOverlay.h:27
Definition Texture.h:10
Definition Widget.h:57
Definition World.h:26
Definition EngineTypes.h:365
Definition EngineTypes.h:333
Definition EngineTypes.h:503
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