BsRenderCompositor.h 20 KB

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