Renderer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. {
  15. // Forward
  16. class ConfigSet;
  17. class ResourceManager;
  18. class StagingGpuMemoryManager;
  19. class UiManager;
  20. /// @addtogroup renderer
  21. /// @{
  22. /// Renderer statistics.
  23. class RendererPrecreatedSamplers
  24. {
  25. public:
  26. SamplerPtr m_nearestNearestClamp;
  27. SamplerPtr m_trilinearClamp;
  28. SamplerPtr m_trilinearRepeat;
  29. SamplerPtr m_trilinearRepeatAniso;
  30. };
  31. /// Offscreen renderer.
  32. class Renderer
  33. {
  34. public:
  35. Renderer();
  36. ~Renderer();
  37. ProbeReflections& getProbeReflections()
  38. {
  39. return *m_probeReflections;
  40. }
  41. VolumetricLightingAccumulation& getVolumetricLightingAccumulation()
  42. {
  43. return *m_volLighting;
  44. }
  45. ShadowMapping& getShadowMapping()
  46. {
  47. return *m_shadowMapping;
  48. }
  49. GBuffer& getGBuffer()
  50. {
  51. return *m_gbuffer;
  52. }
  53. LightShading& getLightShading()
  54. {
  55. return *m_lightShading;
  56. }
  57. DepthDownscale& getDepthDownscale()
  58. {
  59. return *m_depth;
  60. }
  61. ForwardShading& getForwardShading()
  62. {
  63. return *m_forwardShading;
  64. }
  65. VolumetricFog& getVolumetricFog()
  66. {
  67. return *m_volFog;
  68. }
  69. Tonemapping& getTonemapping()
  70. {
  71. return *m_tonemapping;
  72. }
  73. Ssao& getSsao()
  74. {
  75. return *m_ssao;
  76. }
  77. Bloom& getBloom()
  78. {
  79. return *m_bloom;
  80. }
  81. FinalComposite& getFinalComposite()
  82. {
  83. return *m_finalComposite;
  84. }
  85. Dbg& getDbg()
  86. {
  87. return *m_dbg;
  88. }
  89. TemporalAA& getTemporalAA()
  90. {
  91. return *m_temporalAA;
  92. }
  93. DownscaleBlur& getDownscaleBlur()
  94. {
  95. return *m_downscale;
  96. }
  97. LensFlare& getLensFlare()
  98. {
  99. return *m_lensFlare;
  100. }
  101. const LensFlare& getLensFlare() const
  102. {
  103. return *m_lensFlare;
  104. }
  105. const GlobalIllumination& getGlobalIllumination() const
  106. {
  107. return *m_gi;
  108. }
  109. UiStage& getUiStage()
  110. {
  111. return *m_uiStage;
  112. }
  113. ShadowmapsResolve& getShadowmapsResolve()
  114. {
  115. return *m_smResolve;
  116. }
  117. AccelerationStructureBuilder& getAccelerationStructureBuilder()
  118. {
  119. return *m_accelerationStructureBuilder;
  120. }
  121. RtShadows& getRtShadows()
  122. {
  123. return *m_rtShadows;
  124. }
  125. MotionVectors& getMotionVectors()
  126. {
  127. return *m_motionVectors;
  128. }
  129. Bool getRtShadowsEnabled() const
  130. {
  131. return m_rtShadows.isCreated();
  132. }
  133. Ssr& getSsr()
  134. {
  135. return *m_ssr;
  136. }
  137. Ssgi& getSsgi()
  138. {
  139. return *m_ssgi;
  140. }
  141. U32 getWidth() const
  142. {
  143. return m_width;
  144. }
  145. U32 getHeight() const
  146. {
  147. return m_height;
  148. }
  149. F32 getAspectRatio() const
  150. {
  151. return F32(m_width) / F32(m_height);
  152. }
  153. /// Init the renderer.
  154. ANKI_USE_RESULT Error init(ThreadHive* hive, ResourceManager* resources, GrManager* gr,
  155. StagingGpuMemoryManager* stagingMem, UiManager* ui, HeapAllocator<U8> alloc,
  156. const ConfigSet& config, Timestamp* globTimestamp);
  157. /// This function does all the rendering stages and produces a final result.
  158. ANKI_USE_RESULT Error populateRenderGraph(RenderingContext& ctx);
  159. void finalize(const RenderingContext& ctx);
  160. U64 getFrameCount() const
  161. {
  162. return m_frameCount;
  163. }
  164. const RenderableDrawer& getSceneDrawer() const
  165. {
  166. return m_sceneDrawer;
  167. }
  168. RenderableDrawer& getSceneDrawer()
  169. {
  170. return m_sceneDrawer;
  171. }
  172. UiManager& getUiManager()
  173. {
  174. ANKI_ASSERT(m_ui);
  175. return *m_ui;
  176. }
  177. /// Create the init info for a 2D texture that will be used as a render target.
  178. ANKI_USE_RESULT TextureInitInfo create2DRenderTargetInitInfo(U32 w, U32 h, Format format, TextureUsageBit usage,
  179. CString name = {});
  180. /// Create the init info for a 2D texture that will be used as a render target.
  181. ANKI_USE_RESULT RenderTargetDescription create2DRenderTargetDescription(U32 w, U32 h, Format format,
  182. CString name = {});
  183. ANKI_USE_RESULT TexturePtr createAndClearRenderTarget(const TextureInitInfo& inf,
  184. const ClearValue& clearVal = ClearValue());
  185. GrManager& getGrManager()
  186. {
  187. return *m_gr;
  188. }
  189. HeapAllocator<U8> getAllocator() const
  190. {
  191. return m_alloc;
  192. }
  193. ResourceManager& getResourceManager()
  194. {
  195. return *m_resources;
  196. }
  197. Timestamp getGlobalTimestamp() const
  198. {
  199. return *m_globTimestamp;
  200. }
  201. Timestamp* getGlobalTimestampPtr()
  202. {
  203. return m_globTimestamp;
  204. }
  205. /// Returns true if there were resources loaded or loading async tasks that got completed.
  206. Bool resourcesLoaded() const
  207. {
  208. return m_resourcesDirty;
  209. }
  210. TextureViewPtr getDummyTextureView2d() const
  211. {
  212. return m_dummyTexView2d;
  213. }
  214. TextureViewPtr getDummyTextureView3d() const
  215. {
  216. return m_dummyTexView3d;
  217. }
  218. BufferPtr getDummyBuffer() const
  219. {
  220. return m_dummyBuff;
  221. }
  222. const RendererPrecreatedSamplers& getSamplers() const
  223. {
  224. return m_samplers;
  225. }
  226. StagingGpuMemoryManager& getStagingGpuMemoryManager()
  227. {
  228. ANKI_ASSERT(m_stagingMem);
  229. return *m_stagingMem;
  230. }
  231. ThreadHive& getThreadHive()
  232. {
  233. ANKI_ASSERT(m_threadHive);
  234. return *m_threadHive;
  235. }
  236. const ThreadHive& getThreadHive() const
  237. {
  238. ANKI_ASSERT(m_threadHive);
  239. return *m_threadHive;
  240. }
  241. U32 getTileSize() const
  242. {
  243. return m_tileSize;
  244. }
  245. const UVec2& getTileCounts() const
  246. {
  247. return m_tileCounts;
  248. }
  249. U32 getZSplitCount() const
  250. {
  251. return m_zSplitCount;
  252. }
  253. /// @name Debug render targets
  254. /// @{
  255. /// Register a debug render target.
  256. void registerDebugRenderTarget(RendererObject* obj, CString rtName);
  257. /// Set the render target you want to show.
  258. void setCurrentDebugRenderTarget(CString rtName);
  259. /// Get the render target currently showing.
  260. CString getCurrentDebugRenderTarget() const
  261. {
  262. return m_currentDebugRtName;
  263. }
  264. // Need to call it after the handle is set by the RenderGraph.
  265. void getCurrentDebugRenderTarget(RenderTargetHandle& handle, Bool& handleValid,
  266. ShaderProgramPtr& optionalShaderProgram);
  267. /// @}
  268. private:
  269. ResourceManager* m_resources = nullptr;
  270. ThreadHive* m_threadHive = nullptr;
  271. StagingGpuMemoryManager* m_stagingMem = nullptr;
  272. GrManager* m_gr = nullptr;
  273. UiManager* m_ui = nullptr;
  274. Timestamp* m_globTimestamp;
  275. HeapAllocator<U8> m_alloc;
  276. /// @name Rendering stages
  277. /// @{
  278. UniquePtr<VolumetricLightingAccumulation> m_volLighting;
  279. UniquePtr<GlobalIllumination> m_gi;
  280. UniquePtr<ProbeReflections> m_probeReflections;
  281. UniquePtr<ShadowMapping> m_shadowMapping; ///< Shadow mapping.
  282. UniquePtr<GBuffer> m_gbuffer; ///< Material rendering stage
  283. UniquePtr<GBufferPost> m_gbufferPost;
  284. UniquePtr<Ssr> m_ssr;
  285. UniquePtr<Ssgi> m_ssgi;
  286. UniquePtr<LightShading> m_lightShading; ///< Illumination rendering stage
  287. UniquePtr<DepthDownscale> m_depth;
  288. UniquePtr<ForwardShading> m_forwardShading; ///< Forward shading.
  289. UniquePtr<VolumetricFog> m_volFog; ///< Volumetric fog.
  290. UniquePtr<LensFlare> m_lensFlare; ///< Forward shading lens flares.
  291. UniquePtr<DownscaleBlur> m_downscale;
  292. UniquePtr<TemporalAA> m_temporalAA;
  293. UniquePtr<Tonemapping> m_tonemapping;
  294. UniquePtr<Ssao> m_ssao;
  295. UniquePtr<Bloom> m_bloom;
  296. UniquePtr<FinalComposite> m_finalComposite; ///< Postprocessing rendering stage
  297. UniquePtr<Dbg> m_dbg; ///< Debug stage.
  298. UniquePtr<UiStage> m_uiStage;
  299. UniquePtr<GenericCompute> m_genericCompute;
  300. UniquePtr<ShadowmapsResolve> m_smResolve;
  301. UniquePtr<AccelerationStructureBuilder> m_accelerationStructureBuilder;
  302. UniquePtr<RtShadows> m_rtShadows;
  303. UniquePtr<MotionVectors> m_motionVectors;
  304. UniquePtr<ClusterBinning> m_clusterBinning;
  305. /// @}
  306. U32 m_tileSize = 0;
  307. UVec2 m_tileCounts = UVec2(0u);
  308. U32 m_zSplitCount = 0;
  309. U32 m_width;
  310. U32 m_height;
  311. RenderableDrawer m_sceneDrawer;
  312. U64 m_frameCount; ///< Frame number
  313. U64 m_prevLoadRequestCount = 0;
  314. U64 m_prevAsyncTasksCompleted = 0;
  315. Bool m_resourcesDirty = true;
  316. CommonMatrices m_prevMatrices;
  317. Array<Mat4, 16> m_jitteredMats16x;
  318. Array<Mat4, 8> m_jitteredMats8x;
  319. TextureViewPtr m_dummyTexView2d;
  320. TextureViewPtr m_dummyTexView3d;
  321. BufferPtr m_dummyBuff;
  322. RendererPrecreatedSamplers m_samplers;
  323. ShaderProgramResourcePtr m_clearTexComputeProg;
  324. class DebugRtInfo
  325. {
  326. public:
  327. RendererObject* m_obj;
  328. String m_rtName;
  329. };
  330. DynamicArray<DebugRtInfo> m_debugRts;
  331. String m_currentDebugRtName;
  332. ANKI_USE_RESULT Error initInternal(const ConfigSet& initializer);
  333. void initJitteredMats();
  334. };
  335. /// @}
  336. } // end namespace anki