BsRenderCompositor.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. /* LIGHTING NODES */
  202. /************************************************************************/
  203. /** Initializes the light accumulation texture and/or buffer. Does not perform any rendering. */
  204. class RCNodeLightAccumulation : public RenderCompositorNode
  205. {
  206. public:
  207. // Outputs
  208. /**
  209. * Contains lighting information accumulated from multiple lights. If MSAA is used this texture will be null until
  210. * the flattened data from the buffer is first resolved into this texture.
  211. */
  212. SPtr<PooledRenderTexture> lightAccumulationTex;
  213. /**
  214. * Flattened, buffer version of lightAccumulationTex. Only available when MSAA is used, since random writes to
  215. * multisampled textures aren't supported on all render backends.
  216. */
  217. SPtr<PooledStorageBuffer> flattenedLightAccumBuffer;
  218. SPtr<RenderTexture> renderTarget;
  219. static StringID getNodeId() { return "LightAccumulation"; }
  220. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  221. protected:
  222. /** @copydoc RenderCompositorNode::render */
  223. void render(const RenderCompositorNodeInputs& inputs) override;
  224. /** @copydoc RenderCompositorNode::clear */
  225. void clear() override;
  226. };
  227. /**
  228. * Performs tiled deferred lighting, outputing any lighting information in the light accumulation buffer.
  229. * By default only non-shadowed lights are rendered, as shadowed ones are handled using standard deferred.
  230. */
  231. class RCNodeTiledDeferredLighting : public RenderCompositorNode
  232. {
  233. public:
  234. // Outputs
  235. RCNodeLightAccumulation* output;
  236. static StringID getNodeId() { return "TiledDeferredLighting"; }
  237. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  238. protected:
  239. /** @copydoc RenderCompositorNode::render */
  240. void render(const RenderCompositorNodeInputs& inputs) override;
  241. /** @copydoc RenderCompositorNode::clear */
  242. void clear() override;
  243. };
  244. /**
  245. * Performs standard deferred lighting, outputting any lighting information in the light accumulation buffer.
  246. * Only renders shadowed lights.
  247. */
  248. class RCNodeStandardDeferredLighting : public RenderCompositorNode
  249. {
  250. public:
  251. // Outputs
  252. RCNodeLightAccumulation* output;
  253. static StringID getNodeId() { return "StandardDeferredLighting"; }
  254. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  255. protected:
  256. /** @copydoc RenderCompositorNode::render */
  257. void render(const RenderCompositorNodeInputs& inputs) override;
  258. /** @copydoc RenderCompositorNode::clear */
  259. void clear() override;
  260. SPtr<RenderTexture> mRenderTarget;
  261. };
  262. /**
  263. * In case light accumulation was rendered into a buffer instead of a texture (if MSAA is used), this node will
  264. * unflatten the buffer and write its contents into the light accumulation texture.
  265. */
  266. class RCNodeUnflattenLightAccum : public RenderCompositorNode
  267. {
  268. public:
  269. // Outputs
  270. RCNodeLightAccumulation* output;
  271. static StringID getNodeId() { return "UnflattenLightAccum"; }
  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. };
  279. /** Evaluates indirect lighting from the light probe volume, if available, or the sky irradiance otherwise. */
  280. class RCNodeIndirectLighting : public RenderCompositorNode
  281. {
  282. public:
  283. // Outputs to the unflattened RCNodeLightAccumulation
  284. static StringID getNodeId() { return "IndirectLighting"; }
  285. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  286. protected:
  287. /** @copydoc RenderCompositorNode::render */
  288. void render(const RenderCompositorNodeInputs& inputs) override;
  289. /** @copydoc RenderCompositorNode::clear */
  290. void clear() override;
  291. };
  292. /**
  293. * Performs tiled deferred image based lighting, combines it with direct lighting present in the light accumulation
  294. * buffer and outputs the results to the scene color texture or buffer.
  295. */
  296. class RCNodeTiledDeferredIBL : public RenderCompositorNode
  297. {
  298. public:
  299. // Outputs
  300. RCNodeLightAccumulation* output;
  301. static StringID getNodeId() { return "TiledDeferredIBL"; }
  302. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  303. protected:
  304. /** @copydoc RenderCompositorNode::render */
  305. void render(const RenderCompositorNodeInputs& inputs) override;
  306. /** @copydoc RenderCompositorNode::clear */
  307. void clear() override;
  308. };
  309. /** Renders transparent objects using clustered forward rendering. */
  310. class RCNodeClusteredForward : public RenderCompositorNode
  311. {
  312. public:
  313. static StringID getNodeId() { return "ClusteredForward"; }
  314. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  315. protected:
  316. /** @copydoc RenderCompositorNode::render */
  317. void render(const RenderCompositorNodeInputs& inputs) override;
  318. /** @copydoc RenderCompositorNode::clear */
  319. void clear() override;
  320. };
  321. /**
  322. * In case scene color was rendered into a buffer instead of a texture (if MSAA is used), this node will
  323. * unflatten the buffer and write its contents into the scene color texture.
  324. */
  325. class RCNodeUnflattenSceneColor : public RenderCompositorNode
  326. {
  327. public:
  328. // Outputs
  329. RCNodeSceneColor* output;
  330. static StringID getNodeId() { return "UnflattenSceneColor"; }
  331. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  332. protected:
  333. /** @copydoc RenderCompositorNode::render */
  334. void render(const RenderCompositorNodeInputs& inputs) override;
  335. /** @copydoc RenderCompositorNode::clear */
  336. void clear() override;
  337. };
  338. /**
  339. * Renders the skybox into the scene color texture. If skybox texture is not available, a solid color will be rendered
  340. * instead.
  341. */
  342. class RCNodeSkybox : public RenderCompositorNode
  343. {
  344. public:
  345. static StringID getNodeId() { return "Skybox"; }
  346. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  347. protected:
  348. /** @copydoc RenderCompositorNode::render */
  349. void render(const RenderCompositorNodeInputs& inputs) override;
  350. /** @copydoc RenderCompositorNode::clear */
  351. void clear() override;
  352. };
  353. /** Moves the contents of the scene color texture into the view's output target. */
  354. class RCNodeFinalResolve : public RenderCompositorNode
  355. {
  356. public:
  357. static StringID getNodeId() { return "FinalResolve"; }
  358. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  359. protected:
  360. /** @copydoc RenderCompositorNode::render */
  361. void render(const RenderCompositorNodeInputs& inputs) override;
  362. /** @copydoc RenderCompositorNode::clear */
  363. void clear() override;
  364. };
  365. /************************************************************************/
  366. /* POST PROCESS NODES */
  367. /************************************************************************/
  368. /**
  369. * Helper node used for post-processing. Takes care of allocating and switching between textures used for post process
  370. * effects.
  371. */
  372. class RCNodePostProcess : public RenderCompositorNode
  373. {
  374. public:
  375. RCNodePostProcess();
  376. /**
  377. * Returns a texture that can be used for rendering a post-process effect, and the result of the previous
  378. * output. Switches these textures so the next call they are returned in the opposite parameters.
  379. */
  380. void getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const;
  381. /** Returns a texture that contains the last rendererd post process output. */
  382. SPtr<Texture> getLastOutput() const;
  383. static StringID getNodeId() { return "PostProcess"; }
  384. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  385. protected:
  386. /** @copydoc RenderCompositorNode::render */
  387. void render(const RenderCompositorNodeInputs& inputs) override;
  388. /** @copydoc RenderCompositorNode::clear */
  389. void clear() override;
  390. mutable SPtr<PooledRenderTexture> mOutput[2];
  391. mutable bool mAllocated[2];
  392. mutable UINT32 mCurrentIdx = 0;
  393. };
  394. /**
  395. * Performs tone mapping on the contents of the scene color texture. At the same time resolves MSAA into a non-MSAA
  396. * scene color texture.
  397. */
  398. class RCNodeTonemapping : public RenderCompositorNode
  399. {
  400. public:
  401. SPtr<PooledRenderTexture> eyeAdaptation;
  402. SPtr<PooledRenderTexture> prevEyeAdaptation;
  403. ~RCNodeTonemapping();
  404. static StringID getNodeId() { return "Tonemapping"; }
  405. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  406. protected:
  407. /** @copydoc RenderCompositorNode::render */
  408. void render(const RenderCompositorNodeInputs& inputs) override;
  409. /** @copydoc RenderCompositorNode::clear */
  410. void clear() override;
  411. SPtr<PooledRenderTexture> mTonemapLUT;
  412. UINT64 mTonemapLastUpdateHash = -1;
  413. };
  414. /** Renders the depth of field effect using Gaussian blurring. */
  415. class RCNodeGaussianDOF : public RenderCompositorNode
  416. {
  417. public:
  418. static StringID getNodeId() { return "GaussianDOF"; }
  419. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  420. protected:
  421. /** @copydoc RenderCompositorNode::render */
  422. void render(const RenderCompositorNodeInputs& inputs) override;
  423. /** @copydoc RenderCompositorNode::clear */
  424. void clear() override;
  425. };
  426. /** Renders FXAA. */
  427. class RCNodeFXAA : public RenderCompositorNode
  428. {
  429. public:
  430. static StringID getNodeId() { return "FXAA"; }
  431. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  432. protected:
  433. /** @copydoc RenderCompositorNode::render */
  434. void render(const RenderCompositorNodeInputs& inputs) override;
  435. /** @copydoc RenderCompositorNode::clear */
  436. void clear() override;
  437. };
  438. /************************************************************************/
  439. /* SCREEN SPACE */
  440. /************************************************************************/
  441. /** Resolves the depth buffer (if multi-sampled). Otherwise just references the original depth buffer. */
  442. class RCNodeResolvedSceneDepth : public RenderCompositorNode
  443. {
  444. public:
  445. SPtr<PooledRenderTexture> output;
  446. static StringID getNodeId() { return "ResolvedSceneDepth"; }
  447. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  448. protected:
  449. /** @copydoc RenderCompositorNode::render */
  450. void render(const RenderCompositorNodeInputs& inputs) override;
  451. /** @copydoc RenderCompositorNode::clear */
  452. void clear() override;
  453. bool mPassThrough = false;
  454. };
  455. /** Builds the hierarchical Z buffer. */
  456. class RCNodeHiZ : public RenderCompositorNode
  457. {
  458. public:
  459. SPtr<PooledRenderTexture> output;
  460. static StringID getNodeId() { return "HiZ"; }
  461. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  462. protected:
  463. /** @copydoc RenderCompositorNode::render */
  464. void render(const RenderCompositorNodeInputs& inputs) override;
  465. /** @copydoc RenderCompositorNode::clear */
  466. void clear() override;
  467. };
  468. /** Renders screen space ambient occlusion. */
  469. class RCNodeSSAO : public RenderCompositorNode
  470. {
  471. public:
  472. SPtr<PooledRenderTexture> output;
  473. static StringID getNodeId() { return "SSAO"; }
  474. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  475. protected:
  476. /** @copydoc RenderCompositorNode::render */
  477. void render(const RenderCompositorNodeInputs& inputs) override;
  478. /** @copydoc RenderCompositorNode::clear */
  479. void clear() override;
  480. };
  481. /** Renders screen space reflections. */
  482. class RCNodeSSR : public RenderCompositorNode
  483. {
  484. public:
  485. SPtr<PooledRenderTexture> output;
  486. ~RCNodeSSR();
  487. static StringID getNodeId() { return "SSR"; }
  488. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  489. protected:
  490. /** @copydoc RenderCompositorNode::render */
  491. void render(const RenderCompositorNodeInputs& inputs) override;
  492. /** @copydoc RenderCompositorNode::clear */
  493. void clear() override;
  494. /** Cleans up any outputs. */
  495. void deallocOutputs();
  496. SPtr<PooledRenderTexture> mPrevFrame;
  497. };
  498. /** @} */
  499. }}