BsRenderCompositor.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsRenderBeastPrerequisites.h"
  5. namespace bs
  6. {
  7. class RendererExtension;
  8. namespace ct
  9. {
  10. struct SceneInfo;
  11. class RendererViewGroup;
  12. class RenderCompositorNode;
  13. struct PooledStorageBuffer;
  14. struct FrameInfo;
  15. /** @addtogroup RenderBeast
  16. * @{
  17. */
  18. /** Inputs provided to each node in the render compositor hierarchy */
  19. struct RenderCompositorNodeInputs
  20. {
  21. RenderCompositorNodeInputs(const RendererViewGroup& viewGroup, const RendererView& view, const SceneInfo& scene,
  22. const RenderBeastOptions& options, const FrameInfo& frameInfo)
  23. : viewGroup(viewGroup), view(view), scene(scene), options(options), frameInfo(frameInfo)
  24. { }
  25. const RendererViewGroup& viewGroup;
  26. const RendererView& view;
  27. const SceneInfo& scene;
  28. const RenderBeastOptions& options;
  29. const FrameInfo& frameInfo;
  30. // Callbacks to external systems can hook into the compositor
  31. SmallVector<RendererExtension*, 4> extPreBasePass;
  32. SmallVector<RendererExtension*, 4> extPostBasePass;
  33. SmallVector<RendererExtension*, 4> extPostLighting;
  34. SmallVector<RendererExtension*, 4> extOverlay;
  35. SmallVector<RenderCompositorNode*, 4> inputNodes;
  36. };
  37. /**
  38. * Node in the render compositor hierarchy. Nodes can be implemented to perform specific rendering tasks. Each node
  39. * can depend on other nodes in the hierarchy.
  40. *
  41. * @note Implementations must provide a getNodeId() and getDependencies() static method, which are expected to
  42. * return a unique name for the implemented node, as well as a set of nodes it depends on.
  43. */
  44. class RenderCompositorNode
  45. {
  46. public:
  47. virtual ~RenderCompositorNode() { }
  48. protected:
  49. friend class RenderCompositor;
  50. /** Executes the task implemented in the node. */
  51. virtual void render(const RenderCompositorNodeInputs& inputs) = 0;
  52. /**
  53. * Cleans up any temporary resources allocated in a render() call. Any resources lasting longer than one frame
  54. * should be kept alive and released in some other manner.
  55. */
  56. virtual void clear() = 0;
  57. };
  58. /**
  59. * Performs rendering by iterating over a hierarchy of render nodes. Each node in the hierarchy performs a specific
  60. * rendering tasks and passes its output to the dependant node. The system takes care of initializing, rendering and
  61. * cleaning up nodes automatically depending on their dependencies.
  62. */
  63. class RenderCompositor
  64. {
  65. /** Contains internal information about a single render node. */
  66. struct NodeInfo
  67. {
  68. RenderCompositorNode* node;
  69. UINT32 lastUseIdx;
  70. SmallVector<RenderCompositorNode*, 4> inputs;
  71. };
  72. public:
  73. ~RenderCompositor();
  74. /**
  75. * Rebuilds the render node hierarchy. Call this whenever some setting that may influence the render node
  76. * dependencies changes.
  77. *
  78. * @param[in] view Parent view to which this compositor belongs to.
  79. * @param[in] finalNode Identifier of the final node in the node hierarchy. This node is expected to write
  80. * to the views render target. All other nodes will be deduced from this node's
  81. * dependencies.
  82. */
  83. void build(const RendererView& view, const StringID& finalNode);
  84. /** Performs rendering using the current render node hierarchy. This is expected to be called once per frame. */
  85. void execute(RenderCompositorNodeInputs& inputs) const;
  86. private:
  87. /** Clears the render node hierarchy. */
  88. void clear();
  89. Vector<NodeInfo> mNodeInfos;
  90. bool mIsValid = false;
  91. /************************************************************************/
  92. /* NODE TYPES */
  93. /************************************************************************/
  94. public:
  95. /** Contains information about a specific node type. */
  96. struct NodeType
  97. {
  98. virtual ~NodeType() {};
  99. /** Creates a new node of this type. */
  100. virtual RenderCompositorNode* create() const = 0;
  101. /** Returns identifier for all the dependencies of a node of this type. */
  102. virtual SmallVector<StringID, 4> getDependencies(const RendererView& view) const = 0;
  103. StringID id;
  104. };
  105. /** Templated implementation of NodeType. */
  106. template<class T>
  107. struct TNodeType : NodeType
  108. {
  109. /** @copydoc NodeType::create() */
  110. RenderCompositorNode* create() const override { return bs_new<T>(); }
  111. /** @copydoc NodeType::getDependencies() */
  112. SmallVector<StringID, 4> getDependencies(const RendererView& view) const override
  113. {
  114. return T::getDependencies(view);
  115. }
  116. };
  117. /**
  118. * Registers a new type of node with the system. Each node type must first be registered before it can be used
  119. * in the node hierarchy.
  120. */
  121. template<class T>
  122. static void registerNodeType()
  123. {
  124. auto findIter = mNodeTypes.find(T::getNodeId());
  125. if (findIter != mNodeTypes.end())
  126. LOGERR("Found two render compositor nodes with the same name \"" + String(T::getNodeId().cstr()) + "\".");
  127. mNodeTypes[T::getNodeId()] = bs_new<TNodeType<T>>();
  128. }
  129. /** Releases any information about render node types. */
  130. static void cleanUp()
  131. {
  132. for (auto& entry : mNodeTypes)
  133. bs_delete(entry.second);
  134. mNodeTypes.clear();
  135. }
  136. private:
  137. static UnorderedMap<StringID, NodeType*> mNodeTypes;
  138. };
  139. /************************************************************************/
  140. /* BASE PASS NODES */
  141. /************************************************************************/
  142. /** Initializes the scene depth texture. Does not perform any rendering. */
  143. class RCNodeSceneDepth : public RenderCompositorNode
  144. {
  145. public:
  146. // Outputs
  147. SPtr<PooledRenderTexture> depthTex;
  148. static StringID getNodeId() { return "SceneDepth"; }
  149. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  150. protected:
  151. /** @copydoc RenderCompositorNode::render */
  152. void render(const RenderCompositorNodeInputs& inputs) override;
  153. /** @copydoc RenderCompositorNode::clear */
  154. void clear() override;
  155. };
  156. /**
  157. * Initializes the GBuffer textures and renders the base pass into the GBuffer. The base pass includes all the opaque
  158. * objects visible to the view.
  159. */
  160. class RCNodeGBuffer : public RenderCompositorNode
  161. {
  162. public:
  163. // Outputs
  164. SPtr<PooledRenderTexture> albedoTex;
  165. SPtr<PooledRenderTexture> normalTex;
  166. SPtr<PooledRenderTexture> roughMetalTex;
  167. SPtr<RenderTexture> renderTarget;
  168. static StringID getNodeId() { return "GBuffer"; }
  169. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  170. protected:
  171. /** @copydoc RenderCompositorNode::render */
  172. void render(const RenderCompositorNodeInputs& inputs) override;
  173. /** @copydoc RenderCompositorNode::clear */
  174. void clear() override;
  175. };
  176. /** Initializes the scene color texture and/or buffer. Does not perform any rendering. */
  177. class RCNodeSceneColor : public RenderCompositorNode
  178. {
  179. public:
  180. // Outputs
  181. /**
  182. * Contains scene color. If MSAA is used this texture will be null until the flattened data from the buffer is
  183. * first resolved into this texture.
  184. */
  185. SPtr<PooledRenderTexture> sceneColorTex;
  186. /**
  187. * Flattened, buffer version of sceneColorTex. Only available when MSAA is used, since random writes to multisampled
  188. * textures aren't supported on all render backends.
  189. */
  190. SPtr<PooledStorageBuffer> flattenedSceneColorBuffer;
  191. SPtr<RenderTexture> renderTarget;
  192. static StringID getNodeId() { return "SceneColor"; }
  193. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  194. protected:
  195. /** @copydoc RenderCompositorNode::render */
  196. void render(const RenderCompositorNodeInputs& inputs) override;
  197. /** @copydoc RenderCompositorNode::clear */
  198. void clear() override;
  199. };
  200. /**
  201. * Determines which samples in the GBuffer require per-sample shading and outputs a texture with the coverage (for use
  202. * in compute shaders) and populates the primary stencil buffer as well (for use in non-compute shaders). Only relevant
  203. * when rendering with MSAA enabled.
  204. */
  205. class RCNodeMSAACoverage : public RenderCompositorNode
  206. {
  207. public:
  208. // Outputs
  209. SPtr<PooledRenderTexture> output;
  210. static StringID getNodeId() { return "MSAACoverage"; }
  211. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  212. protected:
  213. /** @copydoc RenderCompositorNode::render */
  214. void render(const RenderCompositorNodeInputs& inputs) override;
  215. /** @copydoc RenderCompositorNode::clear */
  216. void clear() override;
  217. };
  218. /************************************************************************/
  219. /* LIGHTING NODES */
  220. /************************************************************************/
  221. /** Initializes the light accumulation texture and/or buffer. Does not perform any rendering. */
  222. class RCNodeLightAccumulation : public RenderCompositorNode
  223. {
  224. public:
  225. // Outputs
  226. /**
  227. * Contains lighting information accumulated from multiple lights. If MSAA is used this texture will be null until
  228. * the flattened data from the buffer is first resolved into this texture.
  229. */
  230. SPtr<PooledRenderTexture> lightAccumulationTex;
  231. /**
  232. * Flattened, buffer version of lightAccumulationTex. Only available when MSAA is used, since random writes to
  233. * multisampled textures aren't supported on all render backends.
  234. */
  235. SPtr<PooledStorageBuffer> flattenedLightAccumBuffer;
  236. SPtr<RenderTexture> renderTarget;
  237. static StringID getNodeId() { return "LightAccumulation"; }
  238. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  239. protected:
  240. /** @copydoc RenderCompositorNode::render */
  241. void render(const RenderCompositorNodeInputs& inputs) override;
  242. /** @copydoc RenderCompositorNode::clear */
  243. void clear() override;
  244. };
  245. /**
  246. * Performs tiled deferred lighting, outputing any lighting information in the light accumulation buffer.
  247. * By default only non-shadowed lights are rendered, as shadowed ones are handled using standard deferred.
  248. */
  249. class RCNodeTiledDeferredLighting : public RenderCompositorNode
  250. {
  251. public:
  252. // Outputs
  253. RCNodeLightAccumulation* output;
  254. static StringID getNodeId() { return "TiledDeferredLighting"; }
  255. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  256. protected:
  257. /** @copydoc RenderCompositorNode::render */
  258. void render(const RenderCompositorNodeInputs& inputs) override;
  259. /** @copydoc RenderCompositorNode::clear */
  260. void clear() override;
  261. };
  262. /**
  263. * Performs standard deferred lighting, outputting any lighting information in the light accumulation buffer.
  264. * Only renders shadowed lights.
  265. */
  266. class RCNodeStandardDeferredLighting : public RenderCompositorNode
  267. {
  268. public:
  269. // Outputs
  270. RCNodeLightAccumulation* output;
  271. static StringID getNodeId() { return "StandardDeferredLighting"; }
  272. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  273. protected:
  274. /** @copydoc RenderCompositorNode::render */
  275. void render(const RenderCompositorNodeInputs& inputs) override;
  276. /** @copydoc RenderCompositorNode::clear */
  277. void clear() override;
  278. SPtr<RenderTexture> mLightOcclusionRT;
  279. };
  280. /**
  281. * In case light accumulation was rendered into a buffer instead of a texture (if MSAA is used), this node will
  282. * unflatten the buffer and write its contents into the light accumulation texture.
  283. */
  284. class RCNodeUnflattenLightAccum : public RenderCompositorNode
  285. {
  286. public:
  287. // Outputs
  288. RCNodeLightAccumulation* output;
  289. static StringID getNodeId() { return "UnflattenLightAccum"; }
  290. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  291. protected:
  292. /** @copydoc RenderCompositorNode::render */
  293. void render(const RenderCompositorNodeInputs& inputs) override;
  294. /** @copydoc RenderCompositorNode::clear */
  295. void clear() override;
  296. };
  297. /** Evaluates indirect lighting from the light probe volume, if available, or the sky irradiance otherwise. */
  298. class RCNodeIndirectLighting : public RenderCompositorNode
  299. {
  300. public:
  301. // Outputs to the unflattened RCNodeLightAccumulation
  302. static StringID getNodeId() { return "IndirectLighting"; }
  303. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  304. protected:
  305. /** @copydoc RenderCompositorNode::render */
  306. void render(const RenderCompositorNodeInputs& inputs) override;
  307. /** @copydoc RenderCompositorNode::clear */
  308. void clear() override;
  309. };
  310. /**
  311. * Performs tiled deferred image based lighting, combines it with direct lighting present in the light accumulation
  312. * buffer and outputs the results to the scene color texture or buffer.
  313. */
  314. class RCNodeTiledDeferredIBL : public RenderCompositorNode
  315. {
  316. public:
  317. // Outputs
  318. RCNodeLightAccumulation* output;
  319. static StringID getNodeId() { return "TiledDeferredIBL"; }
  320. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  321. protected:
  322. /** @copydoc RenderCompositorNode::render */
  323. void render(const RenderCompositorNodeInputs& inputs) override;
  324. /** @copydoc RenderCompositorNode::clear */
  325. void clear() override;
  326. };
  327. /** Renders transparent objects using clustered forward rendering. */
  328. class RCNodeClusteredForward : public RenderCompositorNode
  329. {
  330. public:
  331. RCNodeClusteredForward();
  332. static StringID getNodeId() { return "ClusteredForward"; }
  333. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  334. protected:
  335. /** @copydoc RenderCompositorNode::render */
  336. void render(const RenderCompositorNodeInputs& inputs) override;
  337. /** @copydoc RenderCompositorNode::clear */
  338. void clear() override;
  339. SPtr<SamplerState> mSSRAOSamplerState;
  340. };
  341. /**
  342. * In case scene color was rendered into a buffer instead of a texture (if MSAA is used), this node will
  343. * unflatten the buffer and write its contents into the scene color texture.
  344. */
  345. class RCNodeUnflattenSceneColor : public RenderCompositorNode
  346. {
  347. public:
  348. // Outputs
  349. RCNodeSceneColor* output;
  350. static StringID getNodeId() { return "UnflattenSceneColor"; }
  351. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  352. protected:
  353. /** @copydoc RenderCompositorNode::render */
  354. void render(const RenderCompositorNodeInputs& inputs) override;
  355. /** @copydoc RenderCompositorNode::clear */
  356. void clear() override;
  357. };
  358. /**
  359. * Renders the skybox into the scene color texture. If skybox texture is not available, a solid color will be rendered
  360. * instead.
  361. */
  362. class RCNodeSkybox : public RenderCompositorNode
  363. {
  364. public:
  365. static StringID getNodeId() { return "Skybox"; }
  366. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  367. protected:
  368. /** @copydoc RenderCompositorNode::render */
  369. void render(const RenderCompositorNodeInputs& inputs) override;
  370. /** @copydoc RenderCompositorNode::clear */
  371. void clear() override;
  372. };
  373. /** Moves the contents of the scene color texture into the view's output target. */
  374. class RCNodeFinalResolve : public RenderCompositorNode
  375. {
  376. public:
  377. static StringID getNodeId() { return "FinalResolve"; }
  378. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  379. protected:
  380. /** @copydoc RenderCompositorNode::render */
  381. void render(const RenderCompositorNodeInputs& inputs) override;
  382. /** @copydoc RenderCompositorNode::clear */
  383. void clear() override;
  384. };
  385. /************************************************************************/
  386. /* POST PROCESS NODES */
  387. /************************************************************************/
  388. /**
  389. * Helper node used for post-processing. Takes care of allocating and switching between textures used for post process
  390. * effects.
  391. */
  392. class RCNodePostProcess : public RenderCompositorNode
  393. {
  394. public:
  395. RCNodePostProcess();
  396. /**
  397. * Returns a texture that can be used for rendering a post-process effect, and the result of the previous
  398. * output. Switches these textures so the next call they are returned in the opposite parameters.
  399. */
  400. void getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const;
  401. /** Returns a texture that contains the last rendererd post process output. */
  402. SPtr<Texture> getLastOutput() const;
  403. static StringID getNodeId() { return "PostProcess"; }
  404. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  405. protected:
  406. /** @copydoc RenderCompositorNode::render */
  407. void render(const RenderCompositorNodeInputs& inputs) override;
  408. /** @copydoc RenderCompositorNode::clear */
  409. void clear() override;
  410. mutable SPtr<PooledRenderTexture> mOutput[2];
  411. mutable bool mAllocated[2];
  412. mutable UINT32 mCurrentIdx = 0;
  413. };
  414. /**
  415. * Performs tone mapping on the contents of the scene color texture. At the same time resolves MSAA into a non-MSAA
  416. * scene color texture.
  417. */
  418. class RCNodeTonemapping : public RenderCompositorNode
  419. {
  420. public:
  421. SPtr<PooledRenderTexture> eyeAdaptation;
  422. SPtr<PooledRenderTexture> prevEyeAdaptation;
  423. ~RCNodeTonemapping();
  424. static StringID getNodeId() { return "Tonemapping"; }
  425. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  426. protected:
  427. /** @copydoc RenderCompositorNode::render */
  428. void render(const RenderCompositorNodeInputs& inputs) override;
  429. /** @copydoc RenderCompositorNode::clear */
  430. void clear() override;
  431. SPtr<PooledRenderTexture> mTonemapLUT;
  432. UINT64 mTonemapLastUpdateHash = -1;
  433. };
  434. /** Renders the depth of field effect using Gaussian blurring. */
  435. class RCNodeGaussianDOF : public RenderCompositorNode
  436. {
  437. public:
  438. static StringID getNodeId() { return "GaussianDOF"; }
  439. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  440. protected:
  441. /** @copydoc RenderCompositorNode::render */
  442. void render(const RenderCompositorNodeInputs& inputs) override;
  443. /** @copydoc RenderCompositorNode::clear */
  444. void clear() override;
  445. };
  446. /** Renders FXAA. */
  447. class RCNodeFXAA : public RenderCompositorNode
  448. {
  449. public:
  450. static StringID getNodeId() { return "FXAA"; }
  451. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  452. protected:
  453. /** @copydoc RenderCompositorNode::render */
  454. void render(const RenderCompositorNodeInputs& inputs) override;
  455. /** @copydoc RenderCompositorNode::clear */
  456. void clear() override;
  457. };
  458. /************************************************************************/
  459. /* SCREEN SPACE */
  460. /************************************************************************/
  461. /** Resolves the depth buffer (if multi-sampled). Otherwise just references the original depth buffer. */
  462. class RCNodeResolvedSceneDepth : public RenderCompositorNode
  463. {
  464. public:
  465. SPtr<PooledRenderTexture> output;
  466. static StringID getNodeId() { return "ResolvedSceneDepth"; }
  467. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  468. protected:
  469. /** @copydoc RenderCompositorNode::render */
  470. void render(const RenderCompositorNodeInputs& inputs) override;
  471. /** @copydoc RenderCompositorNode::clear */
  472. void clear() override;
  473. bool mPassThrough = false;
  474. };
  475. /** Builds the hierarchical Z buffer. */
  476. class RCNodeHiZ : public RenderCompositorNode
  477. {
  478. public:
  479. SPtr<PooledRenderTexture> output;
  480. static StringID getNodeId() { return "HiZ"; }
  481. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  482. protected:
  483. /** @copydoc RenderCompositorNode::render */
  484. void render(const RenderCompositorNodeInputs& inputs) override;
  485. /** @copydoc RenderCompositorNode::clear */
  486. void clear() override;
  487. };
  488. /** Renders screen space ambient occlusion. */
  489. class RCNodeSSAO : public RenderCompositorNode
  490. {
  491. public:
  492. SPtr<PooledRenderTexture> output;
  493. static StringID getNodeId() { return "SSAO"; }
  494. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  495. protected:
  496. /** @copydoc RenderCompositorNode::render */
  497. void render(const RenderCompositorNodeInputs& inputs) override;
  498. /** @copydoc RenderCompositorNode::clear */
  499. void clear() override;
  500. };
  501. /** Renders screen space reflections. */
  502. class RCNodeSSR : public RenderCompositorNode
  503. {
  504. public:
  505. SPtr<PooledRenderTexture> output;
  506. ~RCNodeSSR();
  507. static StringID getNodeId() { return "SSR"; }
  508. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  509. protected:
  510. /** @copydoc RenderCompositorNode::render */
  511. void render(const RenderCompositorNodeInputs& inputs) override;
  512. /** @copydoc RenderCompositorNode::clear */
  513. void clear() override;
  514. /** Cleans up any outputs. */
  515. void deallocOutputs();
  516. SPtr<PooledRenderTexture> mPrevFrame;
  517. };
  518. /** @} */
  519. }}