BsRenderCompositor.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. static StringID getNodeId() { return "ClusteredForward"; }
  332. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  333. protected:
  334. /** @copydoc RenderCompositorNode::render */
  335. void render(const RenderCompositorNodeInputs& inputs) override;
  336. /** @copydoc RenderCompositorNode::clear */
  337. void clear() override;
  338. };
  339. /**
  340. * In case scene color was rendered into a buffer instead of a texture (if MSAA is used), this node will
  341. * unflatten the buffer and write its contents into the scene color texture.
  342. */
  343. class RCNodeUnflattenSceneColor : public RenderCompositorNode
  344. {
  345. public:
  346. // Outputs
  347. RCNodeSceneColor* output;
  348. static StringID getNodeId() { return "UnflattenSceneColor"; }
  349. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  350. protected:
  351. /** @copydoc RenderCompositorNode::render */
  352. void render(const RenderCompositorNodeInputs& inputs) override;
  353. /** @copydoc RenderCompositorNode::clear */
  354. void clear() override;
  355. };
  356. /**
  357. * Renders the skybox into the scene color texture. If skybox texture is not available, a solid color will be rendered
  358. * instead.
  359. */
  360. class RCNodeSkybox : public RenderCompositorNode
  361. {
  362. public:
  363. static StringID getNodeId() { return "Skybox"; }
  364. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  365. protected:
  366. /** @copydoc RenderCompositorNode::render */
  367. void render(const RenderCompositorNodeInputs& inputs) override;
  368. /** @copydoc RenderCompositorNode::clear */
  369. void clear() override;
  370. };
  371. /** Moves the contents of the scene color texture into the view's output target. */
  372. class RCNodeFinalResolve : public RenderCompositorNode
  373. {
  374. public:
  375. static StringID getNodeId() { return "FinalResolve"; }
  376. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  377. protected:
  378. /** @copydoc RenderCompositorNode::render */
  379. void render(const RenderCompositorNodeInputs& inputs) override;
  380. /** @copydoc RenderCompositorNode::clear */
  381. void clear() override;
  382. };
  383. /************************************************************************/
  384. /* POST PROCESS NODES */
  385. /************************************************************************/
  386. /**
  387. * Helper node used for post-processing. Takes care of allocating and switching between textures used for post process
  388. * effects.
  389. */
  390. class RCNodePostProcess : public RenderCompositorNode
  391. {
  392. public:
  393. RCNodePostProcess();
  394. /**
  395. * Returns a texture that can be used for rendering a post-process effect, and the result of the previous
  396. * output. Switches these textures so the next call they are returned in the opposite parameters.
  397. */
  398. void getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const;
  399. /** Returns a texture that contains the last rendererd post process output. */
  400. SPtr<Texture> getLastOutput() const;
  401. static StringID getNodeId() { return "PostProcess"; }
  402. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  403. protected:
  404. /** @copydoc RenderCompositorNode::render */
  405. void render(const RenderCompositorNodeInputs& inputs) override;
  406. /** @copydoc RenderCompositorNode::clear */
  407. void clear() override;
  408. mutable SPtr<PooledRenderTexture> mOutput[2];
  409. mutable bool mAllocated[2];
  410. mutable UINT32 mCurrentIdx = 0;
  411. };
  412. /**
  413. * Performs tone mapping on the contents of the scene color texture. At the same time resolves MSAA into a non-MSAA
  414. * scene color texture.
  415. */
  416. class RCNodeTonemapping : public RenderCompositorNode
  417. {
  418. public:
  419. SPtr<PooledRenderTexture> eyeAdaptation;
  420. SPtr<PooledRenderTexture> prevEyeAdaptation;
  421. ~RCNodeTonemapping();
  422. static StringID getNodeId() { return "Tonemapping"; }
  423. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  424. protected:
  425. /** @copydoc RenderCompositorNode::render */
  426. void render(const RenderCompositorNodeInputs& inputs) override;
  427. /** @copydoc RenderCompositorNode::clear */
  428. void clear() override;
  429. SPtr<PooledRenderTexture> mTonemapLUT;
  430. UINT64 mTonemapLastUpdateHash = -1;
  431. };
  432. /** Renders the depth of field effect using Gaussian blurring. */
  433. class RCNodeGaussianDOF : public RenderCompositorNode
  434. {
  435. public:
  436. static StringID getNodeId() { return "GaussianDOF"; }
  437. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  438. protected:
  439. /** @copydoc RenderCompositorNode::render */
  440. void render(const RenderCompositorNodeInputs& inputs) override;
  441. /** @copydoc RenderCompositorNode::clear */
  442. void clear() override;
  443. };
  444. /** Renders FXAA. */
  445. class RCNodeFXAA : public RenderCompositorNode
  446. {
  447. public:
  448. static StringID getNodeId() { return "FXAA"; }
  449. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  450. protected:
  451. /** @copydoc RenderCompositorNode::render */
  452. void render(const RenderCompositorNodeInputs& inputs) override;
  453. /** @copydoc RenderCompositorNode::clear */
  454. void clear() override;
  455. };
  456. /************************************************************************/
  457. /* SCREEN SPACE */
  458. /************************************************************************/
  459. /** Resolves the depth buffer (if multi-sampled). Otherwise just references the original depth buffer. */
  460. class RCNodeResolvedSceneDepth : public RenderCompositorNode
  461. {
  462. public:
  463. SPtr<PooledRenderTexture> output;
  464. static StringID getNodeId() { return "ResolvedSceneDepth"; }
  465. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  466. protected:
  467. /** @copydoc RenderCompositorNode::render */
  468. void render(const RenderCompositorNodeInputs& inputs) override;
  469. /** @copydoc RenderCompositorNode::clear */
  470. void clear() override;
  471. bool mPassThrough = false;
  472. };
  473. /** Builds the hierarchical Z buffer. */
  474. class RCNodeHiZ : public RenderCompositorNode
  475. {
  476. public:
  477. SPtr<PooledRenderTexture> output;
  478. static StringID getNodeId() { return "HiZ"; }
  479. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  480. protected:
  481. /** @copydoc RenderCompositorNode::render */
  482. void render(const RenderCompositorNodeInputs& inputs) override;
  483. /** @copydoc RenderCompositorNode::clear */
  484. void clear() override;
  485. };
  486. /** Renders screen space ambient occlusion. */
  487. class RCNodeSSAO : public RenderCompositorNode
  488. {
  489. public:
  490. SPtr<PooledRenderTexture> output;
  491. static StringID getNodeId() { return "SSAO"; }
  492. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  493. protected:
  494. /** @copydoc RenderCompositorNode::render */
  495. void render(const RenderCompositorNodeInputs& inputs) override;
  496. /** @copydoc RenderCompositorNode::clear */
  497. void clear() override;
  498. };
  499. /** Renders screen space reflections. */
  500. class RCNodeSSR : public RenderCompositorNode
  501. {
  502. public:
  503. SPtr<PooledRenderTexture> output;
  504. ~RCNodeSSR();
  505. static StringID getNodeId() { return "SSR"; }
  506. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  507. protected:
  508. /** @copydoc RenderCompositorNode::render */
  509. void render(const RenderCompositorNodeInputs& inputs) override;
  510. /** @copydoc RenderCompositorNode::clear */
  511. void clear() override;
  512. /** Cleans up any outputs. */
  513. void deallocOutputs();
  514. SPtr<PooledRenderTexture> mPrevFrame;
  515. };
  516. /** @} */
  517. }}