BsRenderCompositor.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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<PooledRenderTexture> mLightOcclusionTex;
  261. SPtr<RenderTexture> mRenderTarget;
  262. };
  263. /**
  264. * In case light accumulation was rendered into a buffer instead of a texture (if MSAA is used), this node will
  265. * unflatten the buffer and write its contents into the light accumulation texture.
  266. */
  267. class RCNodeUnflattenLightAccum : public RenderCompositorNode
  268. {
  269. public:
  270. // Outputs
  271. RCNodeLightAccumulation* output;
  272. static StringID getNodeId() { return "UnflattenLightAccum"; }
  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. };
  280. /**
  281. * Perform tiled deferred image based lighting, combines it with direct lighting present in the light accumulation
  282. * buffer and outputs the results to the scene color texture or buffer.
  283. */
  284. class RCNodeTiledDeferredIBL : public RenderCompositorNode
  285. {
  286. public:
  287. // Outputs
  288. RCNodeLightAccumulation* output;
  289. static StringID getNodeId() { return "TiledDeferredIBL"; }
  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. /** Renders transparent objects using clustered forward rendering. */
  298. class RCNodeClusteredForward : public RenderCompositorNode
  299. {
  300. public:
  301. static StringID getNodeId() { return "ClusteredForward"; }
  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. /**
  310. * In case scene color was rendered into a buffer instead of a texture (if MSAA is used), this node will
  311. * unflatten the buffer and write its contents into the scene color texture.
  312. */
  313. class RCNodeUnflattenSceneColor : public RenderCompositorNode
  314. {
  315. public:
  316. // Outputs
  317. RCNodeSceneColor* output;
  318. static StringID getNodeId() { return "UnflattenSceneColor"; }
  319. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  320. protected:
  321. /** @copydoc RenderCompositorNode::render */
  322. void render(const RenderCompositorNodeInputs& inputs) override;
  323. /** @copydoc RenderCompositorNode::clear */
  324. void clear() override;
  325. };
  326. /**
  327. * Renders the skybox into the scene color texture. If skybox texture is not available, a solid color will be rendered
  328. * instead.
  329. */
  330. class RCNodeSkybox : public RenderCompositorNode
  331. {
  332. public:
  333. static StringID getNodeId() { return "Skybox"; }
  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. };
  341. /** Moves the contents of the scene color texture into the view's output target. */
  342. class RCNodeFinalResolve : public RenderCompositorNode
  343. {
  344. public:
  345. static StringID getNodeId() { return "FinalResolve"; }
  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. /************************************************************************/
  354. /* POST PROCESS NODES */
  355. /************************************************************************/
  356. /**
  357. * Helper node used for post-processing. Takes care of allocating and switching between textures used for post process
  358. * effects.
  359. */
  360. class RCNodePostProcess : public RenderCompositorNode
  361. {
  362. public:
  363. RCNodePostProcess();
  364. /**
  365. * Returns a texture that can be used for rendering a post-process effect, and the result of the previous
  366. * output. Switches these textures so the next call they are returned in the opposite parameters.
  367. */
  368. void getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const;
  369. /** Returns a texture that contains the last rendererd post process output. */
  370. SPtr<Texture> getLastOutput() const;
  371. static StringID getNodeId() { return "PostProcess"; }
  372. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  373. protected:
  374. /** @copydoc RenderCompositorNode::render */
  375. void render(const RenderCompositorNodeInputs& inputs) override;
  376. /** @copydoc RenderCompositorNode::clear */
  377. void clear() override;
  378. mutable SPtr<PooledRenderTexture> mOutput[2];
  379. mutable bool mAllocated[2];
  380. mutable UINT32 mCurrentIdx = 0;
  381. };
  382. /**
  383. * Performs tone mapping on the contents of the scene color texture. At the same time resolves MSAA into a non-MSAA
  384. * scene color texture.
  385. */
  386. class RCNodeTonemapping : public RenderCompositorNode
  387. {
  388. public:
  389. SPtr<PooledRenderTexture> eyeAdaptation;
  390. SPtr<PooledRenderTexture> prevEyeAdaptation;
  391. ~RCNodeTonemapping();
  392. static StringID getNodeId() { return "Tonemapping"; }
  393. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  394. protected:
  395. /** @copydoc RenderCompositorNode::render */
  396. void render(const RenderCompositorNodeInputs& inputs) override;
  397. /** @copydoc RenderCompositorNode::clear */
  398. void clear() override;
  399. SPtr<PooledRenderTexture> mTonemapLUT;
  400. UINT64 mTonemapLastUpdateHash = -1;
  401. };
  402. /** Renders the depth of field effect using Gaussian blurring. */
  403. class RCNodeGaussianDOF : public RenderCompositorNode
  404. {
  405. public:
  406. static StringID getNodeId() { return "GaussianDOF"; }
  407. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  408. protected:
  409. /** @copydoc RenderCompositorNode::render */
  410. void render(const RenderCompositorNodeInputs& inputs) override;
  411. /** @copydoc RenderCompositorNode::clear */
  412. void clear() override;
  413. };
  414. /** Renders FXAA. */
  415. class RCNodeFXAA : public RenderCompositorNode
  416. {
  417. public:
  418. static StringID getNodeId() { return "FXAA"; }
  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. /************************************************************************/
  427. /* SCREEN SPACE */
  428. /************************************************************************/
  429. /** Resolves the depth buffer (if multi-sampled). Otherwise just references the original depth buffer. */
  430. class RCNodeResolvedSceneDepth : public RenderCompositorNode
  431. {
  432. public:
  433. SPtr<PooledRenderTexture> output;
  434. static StringID getNodeId() { return "ResolvedSceneDepth"; }
  435. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  436. protected:
  437. /** @copydoc RenderCompositorNode::render */
  438. void render(const RenderCompositorNodeInputs& inputs) override;
  439. /** @copydoc RenderCompositorNode::clear */
  440. void clear() override;
  441. bool mPassThrough = false;
  442. };
  443. /** Builds the hierarchical Z buffer. */
  444. class RCNodeHiZ : public RenderCompositorNode
  445. {
  446. public:
  447. SPtr<PooledRenderTexture> output;
  448. static StringID getNodeId() { return "HiZ"; }
  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. /** Renders screen space ambient occlusion. */
  457. class RCNodeSSAO : public RenderCompositorNode
  458. {
  459. public:
  460. SPtr<PooledRenderTexture> output;
  461. static StringID getNodeId() { return "SSAO"; }
  462. static SmallVector<StringID, 4> getDependencies(const RendererView& view);
  463. protected:
  464. /** @copydoc RenderCompositorNode::render */
  465. void render(const RenderCompositorNodeInputs& inputs) override;
  466. /** @copydoc RenderCompositorNode::clear */
  467. void clear() override;
  468. };
  469. /** @} */
  470. }}