Renderer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright (C) 2009-2021, 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/StagingGpuMemoryManager.h>
  12. #include <AnKi/Collision/Forward.h>
  13. namespace anki {
  14. // Forward
  15. class ConfigSet;
  16. class ResourceManager;
  17. class StagingGpuMemoryManager;
  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/RendererObjectDefs.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. ANKI_USE_RESULT Error init(ThreadHive* hive, ResourceManager* resources, GrManager* gr,
  62. StagingGpuMemoryManager* stagingMem, UiManager* ui, HeapAllocator<U8> alloc,
  63. const ConfigSet& config, Timestamp* globTimestamp);
  64. /// This function does all the rendering stages and produces a final result.
  65. ANKI_USE_RESULT 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. ANKI_USE_RESULT 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. ANKI_USE_RESULT RenderTargetDescription create2DRenderTargetDescription(U32 w, U32 h, Format format,
  89. CString name = {});
  90. ANKI_USE_RESULT TexturePtr createAndClearRenderTarget(const TextureInitInfo& inf,
  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. Timestamp getGlobalTimestamp() const
  105. {
  106. return *m_globTimestamp;
  107. }
  108. Timestamp* getGlobalTimestampPtr()
  109. {
  110. return m_globTimestamp;
  111. }
  112. /// Returns true if there were resources loaded or loading async tasks that got completed.
  113. Bool resourcesLoaded() const
  114. {
  115. return m_resourcesDirty;
  116. }
  117. TextureViewPtr getDummyTextureView2d() const
  118. {
  119. return m_dummyTexView2d;
  120. }
  121. TextureViewPtr getDummyTextureView3d() const
  122. {
  123. return m_dummyTexView3d;
  124. }
  125. BufferPtr getDummyBuffer() const
  126. {
  127. return m_dummyBuff;
  128. }
  129. const RendererPrecreatedSamplers& getSamplers() const
  130. {
  131. return m_samplers;
  132. }
  133. StagingGpuMemoryManager& getStagingGpuMemoryManager()
  134. {
  135. ANKI_ASSERT(m_stagingMem);
  136. return *m_stagingMem;
  137. }
  138. ThreadHive& getThreadHive()
  139. {
  140. ANKI_ASSERT(m_threadHive);
  141. return *m_threadHive;
  142. }
  143. const ThreadHive& getThreadHive() const
  144. {
  145. ANKI_ASSERT(m_threadHive);
  146. return *m_threadHive;
  147. }
  148. U32 getTileSize() const
  149. {
  150. return m_tileSize;
  151. }
  152. const UVec2& getTileCounts() const
  153. {
  154. return m_tileCounts;
  155. }
  156. U32 getZSplitCount() const
  157. {
  158. return m_zSplitCount;
  159. }
  160. /// @name Debug render targets
  161. /// @{
  162. /// Register a debug render target.
  163. void registerDebugRenderTarget(RendererObject* obj, CString rtName);
  164. /// Set the render target you want to show.
  165. void setCurrentDebugRenderTarget(CString rtName);
  166. /// Get the render target currently showing.
  167. CString getCurrentDebugRenderTarget() const
  168. {
  169. return m_currentDebugRtName;
  170. }
  171. // Need to call it after the handle is set by the RenderGraph.
  172. void getCurrentDebugRenderTarget(RenderTargetHandle& handle, Bool& handleValid,
  173. ShaderProgramPtr& optionalShaderProgram);
  174. /// @}
  175. private:
  176. ResourceManager* m_resources = nullptr;
  177. ThreadHive* m_threadHive = nullptr;
  178. StagingGpuMemoryManager* m_stagingMem = nullptr;
  179. GrManager* m_gr = nullptr;
  180. UiManager* m_ui = nullptr;
  181. Timestamp* m_globTimestamp;
  182. HeapAllocator<U8> m_alloc;
  183. /// @name Rendering stages
  184. /// @{
  185. #define ANKI_RENDERER_OBJECT_DEF(a, b) UniquePtr<a> m_##b;
  186. #include <AnKi/Renderer/RendererObjectDefs.h>
  187. #undef ANKI_RENDERER_OBJECT_DEF
  188. /// @}
  189. U32 m_tileSize = 0;
  190. UVec2 m_tileCounts = UVec2(0u);
  191. U32 m_zSplitCount = 0;
  192. UVec2 m_internalResolution = UVec2(0u); ///< The resolution of all passes up until TAA.
  193. UVec2 m_postProcessResolution = UVec2(0u); ///< The resolution of post processing and following passes.
  194. RenderableDrawer m_sceneDrawer;
  195. U64 m_frameCount; ///< Frame number
  196. U64 m_prevLoadRequestCount = 0;
  197. U64 m_prevAsyncTasksCompleted = 0;
  198. Bool m_resourcesDirty = true;
  199. CommonMatrices m_prevMatrices;
  200. Array<Mat4, 16> m_jitteredMats16x;
  201. Array<Mat4, 8> m_jitteredMats8x;
  202. TextureViewPtr m_dummyTexView2d;
  203. TextureViewPtr m_dummyTexView3d;
  204. BufferPtr m_dummyBuff;
  205. RendererPrecreatedSamplers m_samplers;
  206. ShaderProgramResourcePtr m_clearTexComputeProg;
  207. class DebugRtInfo
  208. {
  209. public:
  210. RendererObject* m_obj;
  211. String m_rtName;
  212. };
  213. DynamicArray<DebugRtInfo> m_debugRts;
  214. String m_currentDebugRtName;
  215. ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
  216. void initJitteredMats();
  217. }; // namespace anki
  218. /// @}
  219. } // end namespace anki