Nameless Engine
Loading...
Searching...
No Matches
Renderer.h
1#pragma once
2
3// Standard.
4#include <variant>
5#include <vector>
6#include <string>
7#include <filesystem>
8#include <memory>
9
10// Custom.
11#include "misc/Error.h"
12#include "render/general/resource/frame/FrameResourceManager.h"
13#include "render/RenderSettings.h"
14#include "game/camera/CameraProperties.h"
15#include "render/RenderStatistics.h"
16#include "render/general/resource/MeshData.h"
17#include "shader/ComputeShaderInterface.h"
18
19namespace ne {
20 class GameManager;
21 class Window;
22 class PipelineManager;
23 class RenderSettings;
24 class EnvironmentNode;
25 class MeshNode;
26 class Material;
27 class Pipeline;
28 class ShadowMapHandle;
29 class DirectionalLightNode;
30 class SpotlightNode;
31 class PointLightNode;
32 class GpuResourceManager;
33 class LightingShaderResourceManager;
34 class ShaderCpuWriteResourceBindingManager;
35 class ShaderTextureResourceBindingManager;
36 class ShaderLightArray;
37 class ShaderManager;
38 class GlobalShaderResourceBindingManager;
39 struct ShaderLightsInFrustumSingleType;
40 struct GraphicsPipelineRegistry;
41
43 class Renderer {
44 // Only window should be able to request new frames to be drawn.
45 friend class Window;
46
47 // Settings will modify renderer's state.
48 friend class RenderSettings;
49
50 // This node sets itself to the renderer when spawned so that its parameters will be used
51 // in the rendering (also removes itself when despawned).
52 friend class EnvironmentNode;
53
54 // Notifies when active camera changes.
55 friend class CameraManager;
56
57 public:
58 Renderer() = delete;
59 Renderer(const Renderer&) = delete;
60 Renderer& operator=(const Renderer&) = delete;
61
62 virtual ~Renderer();
63
69 static constexpr float getMinDepth() { return minDepth; }
70
76 static constexpr float getMaxDepth() { return maxDepth; }
77
86 static std::variant<std::unique_ptr<Renderer>, Error>
87 create(GameManager* pGameManager, std::optional<RendererType> preferredRenderer);
88
97 virtual std::vector<std::string> getSupportedGpuNames() const = 0;
98
104 virtual std::variant<std::set<std::pair<unsigned int, unsigned int>>, Error>
106
115 virtual std::variant<std::set<std::pair<unsigned int, unsigned int>>, Error>
117
123 virtual RendererType getType() const = 0;
124
133 virtual std::string getUsedApiVersion() const = 0;
134
142 std::pair<std::recursive_mutex*, RenderSettings*> getRenderSettings();
143
152
158 virtual std::string getCurrentlyUsedGpuName() const = 0;
159
168 size_t getTotalVideoMemoryInMb() const;
169
178 size_t getUsedVideoMemoryInMb() const;
179
187
193 virtual std::pair<unsigned int, unsigned int> getRenderTargetSize() const = 0;
194
202 Window* getWindow() const;
203
212
221
230
239
248
257
266
275
284
298 std::recursive_mutex* getRenderResourcesMutex();
299
318
319 protected:
325 MeshNode* pMeshNode = nullptr;
326
328 std::vector<MeshIndexBufferInfo> vIndexBuffers;
329 };
330
334 Material* pMaterial = nullptr;
335
337 std::vector<MeshInFrustumInfo> vMeshes;
338 };
339
343 Pipeline* pPipeline = nullptr;
344
346 std::vector<MaterialInFrustumInfo> vMaterials;
347 };
348
350 std::vector<PipelineInFrustumInfo> vOpaquePipelines;
351
353 std::vector<PipelineInFrustumInfo> vTransparentPipelines;
354 };
355
359 std::pair<std::recursive_mutex*, ShaderLightsInFrustumSingleType*> mtxPointLightsInFrustum;
360
362 std::pair<std::recursive_mutex*, ShaderLightsInFrustumSingleType*> mtxSpotlightsInFrustum;
363
364 // No directional lights here because directional lights can't be culled.
365 };
366
382 static consteval unsigned int getRecommendedSwapChainBufferCount() {
384 }
385
395 ShadowMapHandle*& pShadowMapHandle,
396 unsigned int& iShadowPassLightInfoArrayIndex);
397
406 SpotlightNode* pNode,
407 ShadowMapHandle*& pShadowMapHandle,
408 unsigned int& iShadowPassLightInfoArrayIndex);
409
418 static unsigned int
419 getPointLightShadowPassLightInfoArrayIndex(PointLightNode* pNode, size_t iCubemapFaceIndex);
420
429
436
445 [[nodiscard]] std::optional<Error> compileEngineShaders() const;
446
457 void
458 updateFrameConstantsBuffer(FrameResource* pCurrentFrameResource, CameraProperties* pCameraProperties);
459
467
475
483
491
501 virtual std::variant<AntialiasingQuality, Error> getMaxSupportedAntialiasingQuality() const = 0;
502
509 void onFramebufferSizeChanged(int iWidth, int iHeight);
510
517 virtual void onFramebufferSizeChangedDerived(int iWidth, int iHeight) {}
518
520 void drawNextFrame();
521
529
542 CameraProperties* pCameraProperties, FrameResource* pCurrentFrameResource) = 0;
543
556 FrameResource* pCurrentFrameResource,
557 size_t iCurrentFrameResourceIndex,
558 GraphicsPipelineRegistry* pGraphicsPipelines) = 0;
559
568 FrameResource* pCurrentFrameResource,
569 size_t iCurrentFrameResourceIndex,
570 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vOpaquePipelines) = 0;
571
582 FrameResource* pCurrentFrameResource,
583 size_t iCurrentFrameResourceIndex,
584 ComputeExecutionStage stage) = 0;
585
594 virtual void drawMeshesMainPass(
595 FrameResource* pCurrentFrameResource,
596 size_t iCurrentFrameResourceIndex,
597 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vOpaquePipelines,
598 const std::vector<Renderer::MeshesInFrustum::PipelineInFrustumInfo>& vTransparentPipelines) = 0;
599
606 virtual void present(FrameResource* pCurrentFrameResource, size_t iCurrentFrameResourceIndex) = 0;
607
616 [[nodiscard]] std::optional<Error> onRenderSettingsChanged(bool bShadowMapSizeChanged = false);
617
624 [[nodiscard]] virtual std::optional<Error> onRenderSettingsChangedDerived() = 0;
625
635 virtual void waitForGpuToFinishUsingFrameResource(FrameResource* pFrameResource) = 0;
636
645 virtual bool isInitialized() const = 0;
646
654 [[nodiscard]] std::optional<Error> initializeRenderer();
655
664 [[nodiscard]] std::optional<Error> initializeResourceManagers();
665
676 [[nodiscard]] std::optional<Error> clampSettingsToMaxSupported();
677
684 [[nodiscard]] std::optional<Error> recalculateLightTileFrustums();
685
700 CameraProperties* pActiveCameraProperties, GraphicsPipelineRegistry* pGraphicsPipelines);
701
711 CameraProperties* pActiveCameraProperties, size_t iCurrentFrameResourceIndex);
712
718 inline std::pair<std::mutex, FrameConstants>* getFrameConstants() { return &mtxFrameConstants; }
719
729 inline std::atomic<size_t>* getDrawCallCounter() {
731 }
732
733 private:
745 static std::unique_ptr<Renderer>
746 createRenderer(GameManager* pGameManager, std::optional<RendererType> preferredRenderer);
747
763 static std::variant<std::unique_ptr<Renderer>, std::pair<Error, std::string>> createRenderer(
764 RendererType type,
766 const std::vector<std::string>& vBlacklistedGpuNames);
767
768#if defined(WIN32)
774 static void nanosleep(long long iNanoseconds);
775#endif
782
795 unsigned int iRenderTargetWidth,
796 unsigned int iRenderTargetHeight,
797 CameraProperties* pCameraProperties);
798
801
807
809 void setupRenderStats();
810
816 std::optional<Error> initializeRenderSettings();
817
822 std::recursive_mutex mtxRwRenderResources;
823
825 std::unique_ptr<GpuResourceManager> pResourceManager;
826
828 std::unique_ptr<ShaderManager> pShaderManager;
829
831 std::unique_ptr<PipelineManager> pPipelineManager;
832
834 std::unique_ptr<FrameResourceManager> pFrameResourceManager;
835
837 std::unique_ptr<ShaderCpuWriteResourceBindingManager> pShaderCpuWriteResourceManager;
838
840 std::unique_ptr<ShaderTextureResourceBindingManager> pShaderTextureResourceManager;
841
843 std::unique_ptr<LightingShaderResourceManager> pLightingShaderResourceManager;
844
846 std::unique_ptr<GlobalShaderResourceBindingManager> pGlobalShaderResourceBindingManager;
847
852 std::pair<std::recursive_mutex, std::unique_ptr<RenderSettings>> mtxRenderSettings;
853
856
858 bool bIsWindowMinimized = false;
859
861 std::pair<std::mutex, EnvironmentNode*> mtxSpawnedEnvironmentNode;
862
864 std::pair<std::mutex, FrameConstants> mtxFrameConstants;
865
868
871
873 static constexpr unsigned int iRecommendedSwapChainBufferCount = 2;
874
876 static constexpr float minDepth = 0.0F;
877
879 static constexpr float maxDepth = 1.0F;
880 };
881} // namespace ne
Definition: CameraManager.h:16
Definition: CameraProperties.h:22
Definition: DirectionalLightNode.h:14
Definition: EnvironmentNode.h:11
Definition: Error.h:27
Definition: FrameResourceManager.h:46
Definition: GameManager.h:34
Definition: GlobalShaderResourceBindingManager.h:21
Definition: GpuResourceManager.h:43
Definition: GpuResource.h:16
Definition: LightingShaderResourceManager.h:50
Definition: Material.h:63
Definition: MeshNode.h:28
Definition: PipelineManager.h:98
Definition: Pipeline.h:20
Definition: PointLightNode.h:15
Definition: RenderSettings.h:45
Definition: RenderStatistics.h:11
FrameTemporaryStatistics frameTemporaryStatistics
Definition: RenderStatistics.h:200
Definition: Renderer.h:43
std::optional< Error > initializeResourceManagers()
Definition: Renderer.cpp:824
std::optional< Error > compileEngineShaders() const
Definition: Renderer.cpp:74
std::recursive_mutex mtxRwRenderResources
Definition: Renderer.h:822
virtual std::string getUsedApiVersion() const =0
LightingShaderResourceManager * getLightingShaderResourceManager() const
Definition: Renderer.cpp:721
ShaderTextureResourceBindingManager * getShaderTextureResourceManager() const
Definition: Renderer.cpp:717
void resetLightingShaderResourceManager()
Definition: Renderer.cpp:284
std::unique_ptr< GpuResourceManager > pResourceManager
Definition: Renderer.h:825
void drawNextFrame()
Definition: Renderer.cpp:307
virtual void prepareRenderTargetForNextFrame()
Definition: Renderer.h:528
std::unique_ptr< ShaderManager > pShaderManager
Definition: Renderer.h:828
std::pair< std::recursive_mutex *, RenderSettings * > getRenderSettings()
Definition: Renderer.cpp:689
size_t getUsedVideoMemoryInMb() const
Definition: Renderer.cpp:699
virtual void executeComputeShadersOnGraphicsQueue(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, ComputeExecutionStage stage)=0
std::pair< std::mutex, FrameConstants > mtxFrameConstants
Definition: Renderer.h:864
virtual RendererType getType() const =0
virtual std::variant< AntialiasingQuality, Error > getMaxSupportedAntialiasingQuality() const =0
static void getDirectionalLightNodeShadowMappingInfo(DirectionalLightNode *pNode, ShadowMapHandle *&pShadowMapHandle, unsigned int &iShadowPassLightInfoArrayIndex)
Definition: Renderer.cpp:1176
GameManager * pGameManager
Definition: Renderer.h:870
virtual std::pair< unsigned int, unsigned int > getRenderTargetSize() const =0
PipelineManager * getPipelineManager() const
Definition: Renderer.cpp:707
static std::variant< std::unique_ptr< Renderer >, Error > create(GameManager *pGameManager, std::optional< RendererType > preferredRenderer)
Definition: Renderer.cpp:616
std::unique_ptr< ShaderTextureResourceBindingManager > pShaderTextureResourceManager
Definition: Renderer.h:840
virtual void present(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex)=0
void resetFrameResourceManager()
Definition: Renderer.cpp:274
virtual std::optional< Error > onRenderSettingsChangedDerived()=0
void setupRenderStats()
Definition: Renderer.cpp:731
virtual void drawShadowMappingPass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, GraphicsPipelineRegistry *pGraphicsPipelines)=0
void updateFrameConstantsBuffer(FrameResource *pCurrentFrameResource, CameraProperties *pCameraProperties)
Definition: Renderer.cpp:164
Window * getWindow() const
Definition: Renderer.cpp:701
void cullLightsOutsideCameraFrustum(CameraProperties *pActiveCameraProperties, size_t iCurrentFrameResourceIndex)
Definition: Renderer.cpp:1055
std::atomic< size_t > * getDrawCallCounter()
Definition: Renderer.h:729
MeshesInFrustum meshesInFrustumLastFrame
Definition: Renderer.h:855
void updateResourcesForNextFrame(unsigned int iRenderTargetWidth, unsigned int iRenderTargetHeight, CameraProperties *pCameraProperties)
Definition: Renderer.cpp:870
FrameResourceManager * getFrameResourceManager() const
Definition: Renderer.cpp:711
void onActiveCameraChanged()
Definition: Renderer.cpp:469
std::optional< Error > initializeRenderer()
Definition: Renderer.cpp:814
virtual void onFramebufferSizeChangedDerived(int iWidth, int iHeight)
Definition: Renderer.h:517
virtual void waitForGpuToFinishUsingFrameResource(FrameResource *pFrameResource)=0
std::pair< std::mutex, FrameConstants > * getFrameConstants()
Definition: Renderer.h:718
size_t getTotalVideoMemoryInMb() const
Definition: Renderer.cpp:695
static std::unique_ptr< Renderer > createRenderer(GameManager *pGameManager, std::optional< RendererType > preferredRenderer)
Definition: Renderer.cpp:481
GpuResourceManager * getResourceManager() const
Definition: Renderer.cpp:709
std::recursive_mutex * getRenderResourcesMutex()
Definition: Renderer.cpp:729
void calculateFrameStatistics()
Definition: Renderer.cpp:187
static consteval unsigned int getRecommendedSwapChainBufferCount()
Definition: Renderer.h:382
virtual bool isInitialized() const =0
std::optional< Error > initializeRenderSettings()
Definition: Renderer.cpp:772
std::unique_ptr< FrameResourceManager > pFrameResourceManager
Definition: Renderer.h:834
std::optional< Error > recalculateLightTileFrustums()
Definition: Renderer.cpp:431
virtual void waitForGpuToFinishWorkUpToThisPoint()=0
std::unique_ptr< GlobalShaderResourceBindingManager > pGlobalShaderResourceBindingManager
Definition: Renderer.h:846
static ShadowMapHandle * getPointLightNodeShadowMapHandle(PointLightNode *pNode)
Definition: Renderer.cpp:1197
std::unique_ptr< ShaderCpuWriteResourceBindingManager > pShaderCpuWriteResourceManager
Definition: Renderer.h:837
GlobalShaderResourceBindingManager * getGlobalShaderResourceBindingManager() const
Definition: Renderer.cpp:725
ShaderManager * getShaderManager() const
Definition: Renderer.cpp:705
std::pair< std::recursive_mutex, std::unique_ptr< RenderSettings > > mtxRenderSettings
Definition: Renderer.h:852
static constexpr float getMaxDepth()
Definition: Renderer.h:76
virtual std::variant< std::set< std::pair< unsigned int, unsigned int > >, Error > getSupportedRefreshRates() const =0
GameManager * getGameManager() const
Definition: Renderer.cpp:703
void resetGpuResourceManager()
Definition: Renderer.cpp:254
virtual std::vector< std::string > getSupportedGpuNames() const =0
static constexpr float minDepth
Definition: Renderer.h:876
RenderStatistics * getRenderStatistics()
Definition: Renderer.cpp:693
std::optional< Error > onRenderSettingsChanged(bool bShadowMapSizeChanged=false)
Definition: Renderer.cpp:399
virtual std::string getCurrentlyUsedGpuName() const =0
void recalculateTargetFrameTimeFromFpsLimitSetting()
Definition: Renderer.cpp:600
RenderStatistics renderStats
Definition: Renderer.h:867
std::optional< Error > clampSettingsToMaxSupported()
Definition: Renderer.cpp:858
static void getSpotlightNodeShadowMappingInfo(SpotlightNode *pNode, ShadowMapHandle *&pShadowMapHandle, unsigned int &iShadowPassLightInfoArrayIndex)
Definition: Renderer.cpp:1184
virtual GpuResource * getDepthTextureNoMultisampling()=0
void onFramebufferSizeChanged(int iWidth, int iHeight)
Definition: Renderer.cpp:294
std::unique_ptr< PipelineManager > pPipelineManager
Definition: Renderer.h:831
static constexpr float maxDepth
Definition: Renderer.h:879
virtual void prepareForDrawingNextFrame(CameraProperties *pCameraProperties, FrameResource *pCurrentFrameResource)=0
virtual void drawMeshesDepthPrepass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vOpaquePipelines)=0
static constexpr unsigned int iRecommendedSwapChainBufferCount
Definition: Renderer.h:873
static unsigned int getPointLightShadowPassLightInfoArrayIndex(PointLightNode *pNode, size_t iCubemapFaceIndex)
Definition: Renderer.cpp:1193
void resetPipelineManager()
Definition: Renderer.cpp:264
virtual std::variant< std::set< std::pair< unsigned int, unsigned int > >, Error > getSupportedRenderResolutions() const =0
virtual void drawMeshesMainPass(FrameResource *pCurrentFrameResource, size_t iCurrentFrameResourceIndex, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vOpaquePipelines, const std::vector< Renderer::MeshesInFrustum::PipelineInFrustumInfo > &vTransparentPipelines)=0
std::unique_ptr< LightingShaderResourceManager > pLightingShaderResourceManager
Definition: Renderer.h:843
MeshesInFrustum * getMeshesInCameraFrustum(CameraProperties *pActiveCameraProperties, GraphicsPipelineRegistry *pGraphicsPipelines)
Definition: Renderer.cpp:942
bool bIsWindowMinimized
Definition: Renderer.h:858
ShaderCpuWriteResourceBindingManager * getShaderCpuWriteResourceManager() const
Definition: Renderer.cpp:713
static constexpr float getMinDepth()
Definition: Renderer.h:69
std::pair< std::mutex, EnvironmentNode * > mtxSpawnedEnvironmentNode
Definition: Renderer.h:861
Definition: ShaderCpuWriteResourceBindingManager.h:21
Definition: ShaderManager.h:25
Definition: ShaderTextureResourceBindingManager.h:27
Definition: ShadowMapHandle.h:23
Definition: SpotlightNode.h:15
Definition: Window.h:198
Definition: FrameResource.h:15
Definition: PipelineRegistry.hpp:31
std::atomic< size_t > iDrawCallCount
Definition: RenderStatistics.h:178
Definition: Renderer.h:357
std::pair< std::recursive_mutex *, ShaderLightsInFrustumSingleType * > mtxPointLightsInFrustum
Definition: Renderer.h:359
std::pair< std::recursive_mutex *, ShaderLightsInFrustumSingleType * > mtxSpotlightsInFrustum
Definition: Renderer.h:362
Material * pMaterial
Definition: Renderer.h:334
std::vector< MeshInFrustumInfo > vMeshes
Definition: Renderer.h:337
std::vector< MeshIndexBufferInfo > vIndexBuffers
Definition: Renderer.h:328
MeshNode * pMeshNode
Definition: Renderer.h:325
Pipeline * pPipeline
Definition: Renderer.h:343
std::vector< MaterialInFrustumInfo > vMaterials
Definition: Renderer.h:346
Definition: Renderer.h:321
std::vector< PipelineInFrustumInfo > vOpaquePipelines
Definition: Renderer.h:350
std::vector< PipelineInFrustumInfo > vTransparentPipelines
Definition: Renderer.h:353