Renderer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Renderer/Common.h>
  7. #include <AnKi/Renderer/Utils/Drawer.h>
  8. #include <AnKi/Renderer/Utils/GpuVisibility.h>
  9. #include <AnKi/Renderer/Utils/HzbGenerator.h>
  10. #include <AnKi/Math.h>
  11. #include <AnKi/Gr.h>
  12. #include <AnKi/Resource/Forward.h>
  13. #include <AnKi/Collision/Forward.h>
  14. #include <AnKi/Renderer/Utils/Readback.h>
  15. namespace anki {
  16. // Forward
  17. extern BoolCVar g_vrsCVar;
  18. extern BoolCVar g_vrsLimitTo2x2CVar;
  19. extern BoolCVar g_preferComputeCVar;
  20. extern NumericCVar<F32> g_renderScalingCVar;
  21. extern BoolCVar g_rayTracedShadowsCVar;
  22. /// @addtogroup renderer
  23. /// @{
  24. /// Renderer statistics.
  25. class RendererPrecreatedSamplers
  26. {
  27. public:
  28. SamplerPtr m_nearestNearestClamp;
  29. SamplerPtr m_trilinearClamp;
  30. SamplerPtr m_trilinearRepeat;
  31. SamplerPtr m_trilinearRepeatAniso;
  32. SamplerPtr m_trilinearRepeatAnisoResolutionScalingBias;
  33. SamplerPtr m_trilinearClampShadow;
  34. };
  35. /// Offscreen renderer.
  36. class Renderer
  37. {
  38. public:
  39. Renderer();
  40. ~Renderer();
  41. #define ANKI_RENDERER_OBJECT_DEF(a, b) \
  42. a& get##a() \
  43. { \
  44. return *m_##b; \
  45. }
  46. #include <AnKi/Renderer/RendererObject.def.h>
  47. #undef ANKI_RENDERER_OBJECT_DEF
  48. Bool getRtShadowsEnabled() const
  49. {
  50. return m_rtShadows.isCreated();
  51. }
  52. const UVec2& getInternalResolution() const
  53. {
  54. return m_internalResolution;
  55. }
  56. const UVec2& getPostProcessResolution() const
  57. {
  58. return m_postProcessResolution;
  59. }
  60. F32 getAspectRatio() const
  61. {
  62. return F32(m_internalResolution.x()) / F32(m_internalResolution.y());
  63. }
  64. /// Init the renderer.
  65. Error init(UVec2 swapchainSize, StackMemoryPool* framePool);
  66. /// This function does all the rendering stages and produces a final result.
  67. Error populateRenderGraph(RenderingContext& ctx);
  68. void finalize(const RenderingContext& ctx, Fence* fence);
  69. U64 getFrameCount() const
  70. {
  71. return m_frameCount;
  72. }
  73. const RenderableDrawer& getSceneDrawer() const
  74. {
  75. return m_sceneDrawer;
  76. }
  77. RenderableDrawer& getSceneDrawer()
  78. {
  79. return m_sceneDrawer;
  80. }
  81. GpuVisibility& getGpuVisibility()
  82. {
  83. return m_visibility;
  84. }
  85. GpuVisibilityNonRenderables& getGpuVisibilityNonRenderables()
  86. {
  87. return m_nonRenderablesVisibility;
  88. }
  89. GpuVisibilityAccelerationStructures& getGpuVisibilityAccelerationStructures()
  90. {
  91. return m_asVisibility;
  92. }
  93. const HzbGenerator& getHzbGenerator() const
  94. {
  95. return m_hzbGenerator;
  96. }
  97. ReadbackManager& getReadbackManager()
  98. {
  99. return m_readbaks;
  100. }
  101. /// Create the init info for a 2D texture that will be used as a render target.
  102. [[nodiscard]] TextureInitInfo create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage, CString name = {});
  103. /// Create the init info for a 2D texture that will be used as a render target.
  104. [[nodiscard]] RenderTargetDescription create2DRenderTargetDescription(U32 w, U32 h, Format format, CString name = {});
  105. [[nodiscard]] TexturePtr createAndClearRenderTarget(const TextureInitInfo& inf, TextureUsageBit initialUsage,
  106. const ClearValue& clearVal = ClearValue());
  107. TextureView& getDummyTextureView2d() const
  108. {
  109. return *m_dummyTexView2d;
  110. }
  111. TextureView& getDummyTextureView3d() const
  112. {
  113. return *m_dummyTexView3d;
  114. }
  115. Buffer& getDummyBuffer() const
  116. {
  117. return *m_dummyBuff;
  118. }
  119. const RendererPrecreatedSamplers& getSamplers() const
  120. {
  121. return m_samplers;
  122. }
  123. U32 getTileSize() const
  124. {
  125. return m_tileSize;
  126. }
  127. const UVec2& getTileCounts() const
  128. {
  129. return m_tileCounts;
  130. }
  131. U32 getZSplitCount() const
  132. {
  133. return m_zSplitCount;
  134. }
  135. Format getHdrFormat() const;
  136. Format getDepthNoStencilFormat() const;
  137. BufferHandle getGpuSceneBufferHandle() const
  138. {
  139. return m_runCtx.m_gpuSceneHandle;
  140. }
  141. /// @name Debug render targets
  142. /// @{
  143. /// Register a debug render target.
  144. void registerDebugRenderTarget(RendererObject* obj, CString rtName);
  145. /// Set the render target you want to show.
  146. void setCurrentDebugRenderTarget(CString rtName);
  147. /// Get the render target currently showing.
  148. CString getCurrentDebugRenderTarget() const
  149. {
  150. return m_currentDebugRtName;
  151. }
  152. // Need to call it after the handle is set by the RenderGraph.
  153. Bool getCurrentDebugRenderTarget(Array<RenderTargetHandle, kMaxDebugRenderTargets>& handles, ShaderProgramPtr& optionalShaderProgram);
  154. /// @}
  155. StackMemoryPool& getFrameMemoryPool() const
  156. {
  157. ANKI_ASSERT(m_framePool);
  158. return *m_framePool;
  159. }
  160. private:
  161. /// @name Rendering stages
  162. /// @{
  163. #define ANKI_RENDERER_OBJECT_DEF(a, b) UniquePtr<a, SingletonMemoryPoolDeleter<RendererMemoryPool>> m_##b;
  164. #include <AnKi/Renderer/RendererObject.def.h>
  165. #undef ANKI_RENDERER_OBJECT_DEF
  166. /// @}
  167. U32 m_tileSize = 0;
  168. UVec2 m_tileCounts = UVec2(0u);
  169. U32 m_zSplitCount = 0;
  170. UVec2 m_internalResolution = UVec2(0u); ///< The resolution of all passes up until TAA.
  171. UVec2 m_postProcessResolution = UVec2(0u); ///< The resolution of post processing and following passes.
  172. RenderableDrawer m_sceneDrawer;
  173. GpuVisibility m_visibility;
  174. GpuVisibilityNonRenderables m_nonRenderablesVisibility;
  175. GpuVisibilityAccelerationStructures m_asVisibility;
  176. HzbGenerator m_hzbGenerator;
  177. ReadbackManager m_readbaks;
  178. U64 m_frameCount; ///< Frame number
  179. CommonMatrices m_prevMatrices;
  180. Array<Vec2, 64> m_jitterOffsets;
  181. TextureViewPtr m_dummyTexView2d;
  182. TextureViewPtr m_dummyTexView3d;
  183. BufferPtr m_dummyBuff;
  184. RendererPrecreatedSamplers m_samplers;
  185. ShaderProgramResourcePtr m_clearTexComputeProg;
  186. StackMemoryPool* m_framePool = nullptr;
  187. class DebugRtInfo
  188. {
  189. public:
  190. RendererObject* m_obj;
  191. RendererString m_rtName;
  192. };
  193. RendererDynamicArray<DebugRtInfo> m_debugRts;
  194. RendererString m_currentDebugRtName;
  195. class
  196. {
  197. public:
  198. BufferHandle m_gpuSceneHandle;
  199. } m_runCtx;
  200. Error initInternal(UVec2 swapchainSize);
  201. void gpuSceneCopy(RenderingContext& ctx);
  202. };
  203. /// @}
  204. } // end namespace anki