Renderer.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (C) 2009-2022, 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/Core/GpuMemoryPools.h>
  12. #include <AnKi/Collision/Forward.h>
  13. namespace anki {
  14. // Forward
  15. class ConfigSet;
  16. class ResourceManager;
  17. class StagingGpuMemoryPool;
  18. class UiManager;
  19. /// @addtogroup renderer
  20. /// @{
  21. /// Renderer statistics.
  22. class RendererPrecreatedSamplers
  23. {
  24. public:
  25. SamplerPtr m_nearestNearestClamp;
  26. SamplerPtr m_trilinearClamp;
  27. SamplerPtr m_trilinearRepeat;
  28. SamplerPtr m_trilinearRepeatAniso;
  29. SamplerPtr m_trilinearRepeatAnisoResolutionScalingBias;
  30. };
  31. /// Offscreen renderer.
  32. class Renderer
  33. {
  34. public:
  35. Renderer();
  36. ~Renderer();
  37. #define ANKI_RENDERER_OBJECT_DEF(a, b) \
  38. a& get##a() \
  39. { \
  40. return *m_##b; \
  41. }
  42. #include <AnKi/Renderer/RendererObject.defs.h>
  43. #undef ANKI_RENDERER_OBJECT_DEF
  44. Bool getRtShadowsEnabled() const
  45. {
  46. return m_rtShadows.isCreated();
  47. }
  48. const UVec2& getInternalResolution() const
  49. {
  50. return m_internalResolution;
  51. }
  52. const UVec2& getPostProcessResolution() const
  53. {
  54. return m_postProcessResolution;
  55. }
  56. F32 getAspectRatio() const
  57. {
  58. return F32(m_internalResolution.x()) / F32(m_internalResolution.y());
  59. }
  60. /// Init the renderer.
  61. Error init(ThreadHive* hive, ResourceManager* resources, GrManager* gr, StagingGpuMemoryPool* stagingMem,
  62. UiManager* ui, HeapAllocator<U8> alloc, ConfigSet* config, Timestamp* globTimestamp,
  63. UVec2 swapchainSize);
  64. /// This function does all the rendering stages and produces a final result.
  65. Error populateRenderGraph(RenderingContext& ctx);
  66. void finalize(const RenderingContext& ctx);
  67. U64 getFrameCount() const
  68. {
  69. return m_frameCount;
  70. }
  71. const RenderableDrawer& getSceneDrawer() const
  72. {
  73. return m_sceneDrawer;
  74. }
  75. RenderableDrawer& getSceneDrawer()
  76. {
  77. return m_sceneDrawer;
  78. }
  79. UiManager& getUiManager()
  80. {
  81. ANKI_ASSERT(m_ui);
  82. return *m_ui;
  83. }
  84. /// Create the init info for a 2D texture that will be used as a render target.
  85. [[nodiscard]] TextureInitInfo create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage,
  86. CString name = {});
  87. /// Create the init info for a 2D texture that will be used as a render target.
  88. [[nodiscard]] RenderTargetDescription create2DRenderTargetDescription(U32 w, U32 h, Format format,
  89. CString name = {});
  90. [[nodiscard]] TexturePtr createAndClearRenderTarget(const TextureInitInfo& inf, TextureUsageBit initialUsage,
  91. const ClearValue& clearVal = ClearValue());
  92. GrManager& getGrManager()
  93. {
  94. return *m_gr;
  95. }
  96. HeapAllocator<U8> getAllocator() const
  97. {
  98. return m_alloc;
  99. }
  100. ResourceManager& getResourceManager()
  101. {
  102. return *m_resources;
  103. }
  104. const ConfigSet& getConfig() const
  105. {
  106. ANKI_ASSERT(m_config);
  107. return *m_config;
  108. }
  109. Timestamp getGlobalTimestamp() const
  110. {
  111. return *m_globTimestamp;
  112. }
  113. Timestamp* getGlobalTimestampPtr()
  114. {
  115. return m_globTimestamp;
  116. }
  117. /// Returns true if there were resources loaded or loading async tasks that got completed.
  118. Bool resourcesLoaded() const
  119. {
  120. return m_resourcesDirty;
  121. }
  122. TextureViewPtr getDummyTextureView2d() const
  123. {
  124. return m_dummyTexView2d;
  125. }
  126. TextureViewPtr getDummyTextureView3d() const
  127. {
  128. return m_dummyTexView3d;
  129. }
  130. BufferPtr getDummyBuffer() const
  131. {
  132. return m_dummyBuff;
  133. }
  134. const RendererPrecreatedSamplers& getSamplers() const
  135. {
  136. return m_samplers;
  137. }
  138. StagingGpuMemoryPool& getStagingGpuMemory()
  139. {
  140. ANKI_ASSERT(m_stagingMem);
  141. return *m_stagingMem;
  142. }
  143. ThreadHive& getThreadHive()
  144. {
  145. ANKI_ASSERT(m_threadHive);
  146. return *m_threadHive;
  147. }
  148. const ThreadHive& getThreadHive() const
  149. {
  150. ANKI_ASSERT(m_threadHive);
  151. return *m_threadHive;
  152. }
  153. U32 getTileSize() const
  154. {
  155. return m_tileSize;
  156. }
  157. const UVec2& getTileCounts() const
  158. {
  159. return m_tileCounts;
  160. }
  161. U32 getZSplitCount() const
  162. {
  163. return m_zSplitCount;
  164. }
  165. Format getHdrFormat() const;
  166. Format getDepthNoStencilFormat() const;
  167. /// @name Debug render targets
  168. /// @{
  169. /// Register a debug render target.
  170. void registerDebugRenderTarget(RendererObject* obj, CString rtName);
  171. /// Set the render target you want to show.
  172. void setCurrentDebugRenderTarget(CString rtName);
  173. /// Get the render target currently showing.
  174. CString getCurrentDebugRenderTarget() const
  175. {
  176. return m_currentDebugRtName;
  177. }
  178. // Need to call it after the handle is set by the RenderGraph.
  179. void getCurrentDebugRenderTarget(RenderTargetHandle& handle, Bool& handleValid,
  180. ShaderProgramPtr& optionalShaderProgram);
  181. /// @}
  182. private:
  183. ResourceManager* m_resources = nullptr;
  184. ThreadHive* m_threadHive = nullptr;
  185. StagingGpuMemoryPool* m_stagingMem = nullptr;
  186. GrManager* m_gr = nullptr;
  187. UiManager* m_ui = nullptr;
  188. Timestamp* m_globTimestamp = nullptr;
  189. ConfigSet* m_config = nullptr;
  190. HeapAllocator<U8> m_alloc;
  191. /// @name Rendering stages
  192. /// @{
  193. #define ANKI_RENDERER_OBJECT_DEF(a, b) UniquePtr<a> m_##b;
  194. #include <AnKi/Renderer/RendererObject.defs.h>
  195. #undef ANKI_RENDERER_OBJECT_DEF
  196. /// @}
  197. U32 m_tileSize = 0;
  198. UVec2 m_tileCounts = UVec2(0u);
  199. U32 m_zSplitCount = 0;
  200. UVec2 m_internalResolution = UVec2(0u); ///< The resolution of all passes up until TAA.
  201. UVec2 m_postProcessResolution = UVec2(0u); ///< The resolution of post processing and following passes.
  202. RenderableDrawer m_sceneDrawer;
  203. U64 m_frameCount; ///< Frame number
  204. U64 m_prevLoadRequestCount = 0;
  205. U64 m_prevAsyncTasksCompleted = 0;
  206. Bool m_resourcesDirty = true;
  207. CommonMatrices m_prevMatrices;
  208. Array<Vec2, 64> m_jitterOffsets;
  209. TextureViewPtr m_dummyTexView2d;
  210. TextureViewPtr m_dummyTexView3d;
  211. BufferPtr m_dummyBuff;
  212. RendererPrecreatedSamplers m_samplers;
  213. ShaderProgramResourcePtr m_clearTexComputeProg;
  214. class DebugRtInfo
  215. {
  216. public:
  217. RendererObject* m_obj;
  218. String m_rtName;
  219. };
  220. DynamicArray<DebugRtInfo> m_debugRts;
  221. String m_currentDebugRtName;
  222. Error initInternal(UVec2 swapchainSize);
  223. };
  224. /// @}
  225. } // end namespace anki