Renderer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/Drawer.h>
  8. #include <AnKi/Math.h>
  9. #include <AnKi/Gr.h>
  10. #include <AnKi/Resource/Forward.h>
  11. #include <AnKi/Collision/Forward.h>
  12. namespace anki {
  13. /// @addtogroup renderer
  14. /// @{
  15. /// Renderer statistics.
  16. class RendererPrecreatedSamplers
  17. {
  18. public:
  19. SamplerPtr m_nearestNearestClamp;
  20. SamplerPtr m_trilinearClamp;
  21. SamplerPtr m_trilinearRepeat;
  22. SamplerPtr m_trilinearRepeatAniso;
  23. SamplerPtr m_trilinearRepeatAnisoResolutionScalingBias;
  24. SamplerPtr m_trilinearClampShadow;
  25. };
  26. /// Offscreen renderer.
  27. class Renderer
  28. {
  29. public:
  30. Renderer();
  31. ~Renderer();
  32. #define ANKI_RENDERER_OBJECT_DEF(a, b) \
  33. a& get##a() \
  34. { \
  35. return *m_##b; \
  36. }
  37. #include <AnKi/Renderer/RendererObject.defs.h>
  38. #undef ANKI_RENDERER_OBJECT_DEF
  39. Bool getRtShadowsEnabled() const
  40. {
  41. return m_rtShadows.isCreated();
  42. }
  43. const UVec2& getInternalResolution() const
  44. {
  45. return m_internalResolution;
  46. }
  47. const UVec2& getPostProcessResolution() const
  48. {
  49. return m_postProcessResolution;
  50. }
  51. F32 getAspectRatio() const
  52. {
  53. return F32(m_internalResolution.x()) / F32(m_internalResolution.y());
  54. }
  55. /// Init the renderer.
  56. Error init(UVec2 swapchainSize);
  57. /// This function does all the rendering stages and produces a final result.
  58. Error populateRenderGraph(RenderingContext& ctx);
  59. void finalize(const RenderingContext& ctx);
  60. U64 getFrameCount() const
  61. {
  62. return m_frameCount;
  63. }
  64. const RenderableDrawer& getSceneDrawer() const
  65. {
  66. return m_sceneDrawer;
  67. }
  68. RenderableDrawer& getSceneDrawer()
  69. {
  70. return m_sceneDrawer;
  71. }
  72. /// Create the init info for a 2D texture that will be used as a render target.
  73. [[nodiscard]] TextureInitInfo create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage, CString name = {});
  74. /// Create the init info for a 2D texture that will be used as a render target.
  75. [[nodiscard]] RenderTargetDescription create2DRenderTargetDescription(U32 w, U32 h, Format format, CString name = {});
  76. [[nodiscard]] TexturePtr createAndClearRenderTarget(const TextureInitInfo& inf, TextureUsageBit initialUsage,
  77. const ClearValue& clearVal = ClearValue());
  78. /// Returns true if there were resources loaded or loading async tasks that got completed.
  79. Bool resourcesLoaded() const
  80. {
  81. return m_resourcesDirty;
  82. }
  83. TextureView& getDummyTextureView2d() const
  84. {
  85. return *m_dummyTexView2d;
  86. }
  87. TextureView& getDummyTextureView3d() const
  88. {
  89. return *m_dummyTexView3d;
  90. }
  91. Buffer& getDummyBuffer() const
  92. {
  93. return *m_dummyBuff;
  94. }
  95. const RendererPrecreatedSamplers& getSamplers() const
  96. {
  97. return m_samplers;
  98. }
  99. U32 getTileSize() const
  100. {
  101. return m_tileSize;
  102. }
  103. const UVec2& getTileCounts() const
  104. {
  105. return m_tileCounts;
  106. }
  107. U32 getZSplitCount() const
  108. {
  109. return m_zSplitCount;
  110. }
  111. Format getHdrFormat() const;
  112. Format getDepthNoStencilFormat() const;
  113. BufferHandle getGpuSceneBufferHandle() const
  114. {
  115. return m_runCtx.m_gpuSceneHandle;
  116. }
  117. /// @name Debug render targets
  118. /// @{
  119. /// Register a debug render target.
  120. void registerDebugRenderTarget(RendererObject* obj, CString rtName);
  121. /// Set the render target you want to show.
  122. void setCurrentDebugRenderTarget(CString rtName);
  123. /// Get the render target currently showing.
  124. CString getCurrentDebugRenderTarget() const
  125. {
  126. return m_currentDebugRtName;
  127. }
  128. // Need to call it after the handle is set by the RenderGraph.
  129. Bool getCurrentDebugRenderTarget(Array<RenderTargetHandle, kMaxDebugRenderTargets>& handles, ShaderProgramPtr& optionalShaderProgram);
  130. /// @}
  131. private:
  132. /// @name Rendering stages
  133. /// @{
  134. #define ANKI_RENDERER_OBJECT_DEF(a, b) UniquePtr<a, SingletonMemoryPoolDeleter<RendererMemoryPool>> m_##b;
  135. #include <AnKi/Renderer/RendererObject.defs.h>
  136. #undef ANKI_RENDERER_OBJECT_DEF
  137. /// @}
  138. U32 m_tileSize = 0;
  139. UVec2 m_tileCounts = UVec2(0u);
  140. U32 m_zSplitCount = 0;
  141. UVec2 m_internalResolution = UVec2(0u); ///< The resolution of all passes up until TAA.
  142. UVec2 m_postProcessResolution = UVec2(0u); ///< The resolution of post processing and following passes.
  143. RenderableDrawer m_sceneDrawer;
  144. U64 m_frameCount; ///< Frame number
  145. U64 m_prevLoadRequestCount = 0;
  146. U64 m_prevAsyncTasksCompleted = 0;
  147. Bool m_resourcesDirty = true;
  148. CommonMatrices m_prevMatrices;
  149. Array<Vec2, 64> m_jitterOffsets;
  150. TextureViewPtr m_dummyTexView2d;
  151. TextureViewPtr m_dummyTexView3d;
  152. BufferPtr m_dummyBuff;
  153. RendererPrecreatedSamplers m_samplers;
  154. ShaderProgramResourcePtr m_clearTexComputeProg;
  155. class DebugRtInfo
  156. {
  157. public:
  158. RendererObject* m_obj;
  159. RendererString m_rtName;
  160. };
  161. RendererDynamicArray<DebugRtInfo> m_debugRts;
  162. RendererString m_currentDebugRtName;
  163. class
  164. {
  165. public:
  166. BufferHandle m_gpuSceneHandle;
  167. } m_runCtx;
  168. Error initInternal(UVec2 swapchainSize);
  169. void gpuSceneCopy(RenderingContext& ctx);
  170. };
  171. /// @}
  172. } // end namespace anki