BsRenderCompositor.h 19 KB

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