BsRenderCompositor.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsRenderCompositor.h"
  4. #include "BsGpuResourcePool.h"
  5. #include "BsRendererView.h"
  6. #include "Renderer/BsRendererUtility.h"
  7. #include "Mesh/BsMesh.h"
  8. #include "RenderAPI/BsGpuBuffer.h"
  9. #include "BsStandardDeferredLighting.h"
  10. #include "BsRenderBeastOptions.h"
  11. #include "Renderer/BsCamera.h"
  12. #include "BsRendererScene.h"
  13. #include "BsRenderBeast.h"
  14. #include "Utility/BsBitwise.h"
  15. #include "BsRendererTextures.h"
  16. #include "BsObjectRendering.h"
  17. #include "Material/BsGpuParamsSet.h"
  18. #include "Renderer/BsRendererExtension.h"
  19. #include "Renderer/BsSkybox.h"
  20. #include "BsLightProbes.h"
  21. namespace bs { namespace ct
  22. {
  23. UnorderedMap<StringID, RenderCompositor::NodeType*> RenderCompositor::mNodeTypes;
  24. RenderCompositor::~RenderCompositor()
  25. {
  26. clear();
  27. }
  28. void RenderCompositor::build(const RendererView& view, const StringID& finalNode)
  29. {
  30. clear();
  31. bs_frame_mark();
  32. {
  33. FrameUnorderedMap<StringID, UINT32> processedNodes;
  34. mIsValid = true;
  35. std::function<bool(const StringID&)> registerNode = [&](const StringID& nodeId)
  36. {
  37. // Find node type
  38. auto iterFind = mNodeTypes.find(nodeId);
  39. if (iterFind == mNodeTypes.end())
  40. {
  41. LOGERR("Cannot find render compositor node of type \"" + String(nodeId.cstr()) + "\".");
  42. return false;
  43. }
  44. NodeType* nodeType = iterFind->second;
  45. // Register current node
  46. auto iterFind2 = processedNodes.find(nodeId);
  47. // New node
  48. if (iterFind2 == processedNodes.end())
  49. {
  50. // Mark it as invalid for now
  51. processedNodes[nodeId] = -1;
  52. }
  53. // Register node dependencies
  54. SmallVector<StringID, 4> depIds = nodeType->getDependencies(view);
  55. for (auto& dep : depIds)
  56. {
  57. if (!registerNode(dep))
  58. return false;
  59. }
  60. // Register current node
  61. UINT32 curIdx;
  62. // New node, properly populate its index
  63. if (iterFind2 == processedNodes.end())
  64. {
  65. iterFind2 = processedNodes.find(nodeId);
  66. curIdx = (UINT32)mNodeInfos.size();
  67. mNodeInfos.push_back(NodeInfo());
  68. processedNodes[nodeId] = curIdx;
  69. NodeInfo& nodeInfo = mNodeInfos.back();
  70. nodeInfo.node = nodeType->create();
  71. nodeInfo.lastUseIdx = -1;
  72. for (auto& depId : depIds)
  73. {
  74. iterFind2 = processedNodes.find(depId);
  75. NodeInfo& depNodeInfo = mNodeInfos[iterFind2->second];
  76. nodeInfo.inputs.push_back(depNodeInfo.node);
  77. }
  78. }
  79. else // Existing node
  80. {
  81. curIdx = iterFind2->second;
  82. // Check if invalid
  83. if (curIdx == -1)
  84. {
  85. LOGERR("Render compositor nodes recursion detected. Node \"" + String(nodeId.cstr()) + "\" " +
  86. "depends on node \"" + String(iterFind->first.cstr()) + "\" which is not available at " +
  87. "this stage.");
  88. return false;
  89. }
  90. }
  91. // Update dependency last use counters
  92. for (auto& dep : depIds)
  93. {
  94. iterFind2 = processedNodes.find(dep);
  95. NodeInfo& depNodeInfo = mNodeInfos[iterFind2->second];
  96. if (depNodeInfo.lastUseIdx == -1)
  97. depNodeInfo.lastUseIdx = curIdx;
  98. else
  99. depNodeInfo.lastUseIdx = std::max(depNodeInfo.lastUseIdx, curIdx);
  100. }
  101. return true;
  102. };
  103. mIsValid = registerNode(finalNode);
  104. if (!mIsValid)
  105. clear();
  106. }
  107. bs_frame_clear();
  108. }
  109. void RenderCompositor::execute(RenderCompositorNodeInputs& inputs) const
  110. {
  111. if (!mIsValid)
  112. return;
  113. bs_frame_mark();
  114. {
  115. FrameVector<const NodeInfo*> activeNodes;
  116. UINT32 idx = 0;
  117. for (auto& entry : mNodeInfos)
  118. {
  119. inputs.inputNodes = entry.inputs;
  120. entry.node->render(inputs);
  121. activeNodes.push_back(&entry);
  122. for (UINT32 i = 0; i < (UINT32)activeNodes.size(); ++i)
  123. {
  124. if (activeNodes[i] == nullptr)
  125. continue;
  126. if (activeNodes[i]->lastUseIdx <= idx)
  127. {
  128. activeNodes[i]->node->clear();
  129. activeNodes[i] = nullptr;
  130. }
  131. }
  132. idx++;
  133. }
  134. }
  135. bs_frame_clear();
  136. if (!mNodeInfos.empty())
  137. mNodeInfos.back().node->clear();
  138. }
  139. void RenderCompositor::clear()
  140. {
  141. for (auto& entry : mNodeInfos)
  142. bs_delete(entry.node);
  143. mNodeInfos.clear();
  144. mIsValid = false;
  145. }
  146. void RCNodeSceneDepth::render(const RenderCompositorNodeInputs& inputs)
  147. {
  148. GpuResourcePool& resPool = GpuResourcePool::instance();
  149. const RendererViewProperties& viewProps = inputs.view.getProperties();
  150. UINT32 width = viewProps.viewRect.width;
  151. UINT32 height = viewProps.viewRect.height;
  152. UINT32 numSamples = viewProps.numSamples;
  153. depthTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D32_S8X24, width, height, TU_DEPTHSTENCIL,
  154. numSamples, false));
  155. }
  156. void RCNodeSceneDepth::clear()
  157. {
  158. GpuResourcePool& resPool = GpuResourcePool::instance();
  159. resPool.release(depthTex);
  160. }
  161. SmallVector<StringID, 4> RCNodeSceneDepth::getDependencies(const RendererView& view)
  162. {
  163. return {};
  164. }
  165. void RCNodeGBuffer::render(const RenderCompositorNodeInputs& inputs)
  166. {
  167. // Allocate necessary textures & targets
  168. GpuResourcePool& resPool = GpuResourcePool::instance();
  169. const RendererViewProperties& viewProps = inputs.view.getProperties();
  170. UINT32 width = viewProps.viewRect.width;
  171. UINT32 height = viewProps.viewRect.height;
  172. UINT32 numSamples = viewProps.numSamples;
  173. // Note: Consider customizable formats. e.g. for testing if quality can be improved with higher precision normals.
  174. albedoTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA8, width, height, TU_RENDERTARGET,
  175. numSamples, true));
  176. normalTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGB10A2, width, height, TU_RENDERTARGET,
  177. numSamples, false));
  178. roughMetalTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RG16F, width, height, TU_RENDERTARGET,
  179. numSamples, false)); // Note: Metal doesn't need 16-bit float
  180. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  181. SPtr<PooledRenderTexture> sceneDepthTex = sceneDepthNode->depthTex;
  182. bool rebuildRT = false;
  183. if (renderTarget != nullptr)
  184. {
  185. rebuildRT |= renderTarget->getColorTexture(0) != albedoTex->texture;
  186. rebuildRT |= renderTarget->getColorTexture(1) != normalTex->texture;
  187. rebuildRT |= renderTarget->getColorTexture(2) != roughMetalTex->texture;
  188. rebuildRT |= renderTarget->getDepthStencilTexture() != sceneDepthTex->texture;
  189. }
  190. else
  191. rebuildRT = true;
  192. if (renderTarget == nullptr || rebuildRT)
  193. {
  194. RENDER_TEXTURE_DESC gbufferDesc;
  195. gbufferDesc.colorSurfaces[0].texture = albedoTex->texture;
  196. gbufferDesc.colorSurfaces[0].face = 0;
  197. gbufferDesc.colorSurfaces[0].numFaces = 1;
  198. gbufferDesc.colorSurfaces[0].mipLevel = 0;
  199. gbufferDesc.colorSurfaces[1].texture = normalTex->texture;
  200. gbufferDesc.colorSurfaces[1].face = 0;
  201. gbufferDesc.colorSurfaces[1].numFaces = 1;
  202. gbufferDesc.colorSurfaces[1].mipLevel = 0;
  203. gbufferDesc.colorSurfaces[2].texture = roughMetalTex->texture;
  204. gbufferDesc.colorSurfaces[2].face = 0;
  205. gbufferDesc.colorSurfaces[2].numFaces = 1;
  206. gbufferDesc.colorSurfaces[2].mipLevel = 0;
  207. gbufferDesc.depthStencilSurface.texture = sceneDepthTex->texture;
  208. gbufferDesc.depthStencilSurface.face = 0;
  209. gbufferDesc.depthStencilSurface.mipLevel = 0;
  210. renderTarget = RenderTexture::create(gbufferDesc);
  211. }
  212. // Prepare all visible objects. Note that this also prepares non-opaque objects.
  213. const VisibilityInfo& visibility = inputs.view.getVisibilityMasks();
  214. UINT32 numRenderables = (UINT32)inputs.scene.renderables.size();
  215. for (UINT32 i = 0; i < numRenderables; i++)
  216. {
  217. if (!visibility.renderables[i])
  218. continue;
  219. RendererObject* rendererObject = inputs.scene.renderables[i];
  220. rendererObject->updatePerCallBuffer(viewProps.viewProjTransform);
  221. for (auto& element : inputs.scene.renderables[i]->elements)
  222. {
  223. if (element.perCameraBindingIdx != -1)
  224. element.params->setParamBlockBuffer(element.perCameraBindingIdx, inputs.view.getPerViewBuffer(), true);
  225. }
  226. }
  227. Camera* sceneCamera = inputs.view.getSceneCamera();
  228. // Trigger pre-base-pass callbacks
  229. if (sceneCamera != nullptr)
  230. {
  231. for(auto& extension : inputs.extPreBasePass)
  232. {
  233. if (extension->check(*sceneCamera))
  234. extension->render(*sceneCamera);
  235. }
  236. }
  237. // Render base pass
  238. RenderAPI& rapi = RenderAPI::instance();
  239. rapi.setRenderTarget(renderTarget);
  240. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  241. rapi.setViewport(area);
  242. // Clear all targets
  243. rapi.clearViewport(FBT_COLOR | FBT_DEPTH | FBT_STENCIL, Color::ZERO, 1.0f, 0);
  244. // Render all visible opaque elements
  245. const Vector<RenderQueueElement>& opaqueElements = inputs.view.getOpaqueQueue()->getSortedElements();
  246. for (auto iter = opaqueElements.begin(); iter != opaqueElements.end(); ++iter)
  247. {
  248. BeastRenderableElement* renderElem = static_cast<BeastRenderableElement*>(iter->renderElem);
  249. SPtr<Material> material = renderElem->material;
  250. if (iter->applyPass)
  251. gRendererUtility().setPass(material, iter->passIdx, renderElem->techniqueIdx);
  252. gRendererUtility().setPassParams(renderElem->params, iter->passIdx);
  253. if(renderElem->morphVertexDeclaration == nullptr)
  254. gRendererUtility().draw(renderElem->mesh, renderElem->subMesh);
  255. else
  256. gRendererUtility().drawMorph(renderElem->mesh, renderElem->subMesh, renderElem->morphShapeBuffer,
  257. renderElem->morphVertexDeclaration);
  258. }
  259. // Trigger post-base-pass callbacks
  260. if (sceneCamera != nullptr)
  261. {
  262. for(auto& extension : inputs.extPostBasePass)
  263. {
  264. if (extension->check(*sceneCamera))
  265. extension->render(*sceneCamera);
  266. }
  267. }
  268. }
  269. void RCNodeGBuffer::clear()
  270. {
  271. GpuResourcePool& resPool = GpuResourcePool::instance();
  272. resPool.release(albedoTex);
  273. resPool.release(normalTex);
  274. resPool.release(roughMetalTex);
  275. }
  276. SmallVector<StringID, 4> RCNodeGBuffer::getDependencies(const RendererView& view)
  277. {
  278. return { RCNodeSceneDepth::getNodeId() };
  279. }
  280. void RCNodeSceneColor::render(const RenderCompositorNodeInputs& inputs)
  281. {
  282. GpuResourcePool& resPool = GpuResourcePool::instance();
  283. const RendererViewProperties& viewProps = inputs.view.getProperties();
  284. UINT32 width = viewProps.viewRect.width;
  285. UINT32 height = viewProps.viewRect.height;
  286. UINT32 numSamples = viewProps.numSamples;
  287. // Note: Consider customizable HDR format via options? e.g. smaller PF_FLOAT_R11G11B10 or larger 32-bit format
  288. sceneColorTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, TU_RENDERTARGET |
  289. TU_LOADSTORE, numSamples, false));
  290. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  291. SPtr<PooledRenderTexture> sceneDepthTex = sceneDepthNode->depthTex;
  292. if (viewProps.numSamples > 1)
  293. {
  294. UINT32 bufferNumElements = width * height * viewProps.numSamples;
  295. flattenedSceneColorBuffer = resPool.get(POOLED_STORAGE_BUFFER_DESC::createStandard(BF_16X4F, bufferNumElements));
  296. }
  297. else
  298. flattenedSceneColorBuffer = nullptr;
  299. bool rebuildRT = false;
  300. if (renderTarget != nullptr)
  301. {
  302. rebuildRT |= renderTarget->getColorTexture(0) != sceneColorTex->texture;
  303. rebuildRT |= renderTarget->getDepthStencilTexture() != sceneDepthTex->texture;
  304. }
  305. else
  306. rebuildRT = true;
  307. if (rebuildRT)
  308. {
  309. RENDER_TEXTURE_DESC sceneColorDesc;
  310. sceneColorDesc.colorSurfaces[0].texture = sceneColorTex->texture;
  311. sceneColorDesc.colorSurfaces[0].face = 0;
  312. sceneColorDesc.colorSurfaces[0].numFaces = 1;
  313. sceneColorDesc.colorSurfaces[0].mipLevel = 0;
  314. sceneColorDesc.depthStencilSurface.texture = sceneDepthTex->texture;
  315. sceneColorDesc.depthStencilSurface.face = 0;
  316. sceneColorDesc.depthStencilSurface.numFaces = 1;
  317. sceneColorDesc.depthStencilSurface.mipLevel = 0;
  318. renderTarget = RenderTexture::create(sceneColorDesc);
  319. }
  320. }
  321. void RCNodeSceneColor::clear()
  322. {
  323. GpuResourcePool& resPool = GpuResourcePool::instance();
  324. resPool.release(sceneColorTex);
  325. if (flattenedSceneColorBuffer != nullptr)
  326. resPool.release(flattenedSceneColorBuffer);
  327. }
  328. SmallVector<StringID, 4> RCNodeSceneColor::getDependencies(const RendererView& view)
  329. {
  330. return { RCNodeSceneDepth::getNodeId() };
  331. }
  332. void RCNodeMSAACoverage::render(const RenderCompositorNodeInputs& inputs)
  333. {
  334. GpuResourcePool& resPool = GpuResourcePool::instance();
  335. const RendererViewProperties& viewProps = inputs.view.getProperties();
  336. UINT32 width = viewProps.viewRect.width;
  337. UINT32 height = viewProps.viewRect.height;
  338. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width, height, TU_RENDERTARGET));
  339. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  340. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  341. GBufferTextures gbuffer;
  342. gbuffer.albedo = gbufferNode->albedoTex->texture;
  343. gbuffer.normals = gbufferNode->normalTex->texture;
  344. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  345. gbuffer.depth = sceneDepthNode->depthTex->texture;
  346. MSAACoverageMat* mat = MSAACoverageMat::getVariation(viewProps.numSamples);
  347. RenderAPI& rapi = RenderAPI::instance();
  348. rapi.setRenderTarget(output->renderTexture);
  349. mat->execute(inputs.view, gbuffer);
  350. MSAACoverageStencilMat* stencilMat = MSAACoverageStencilMat::get();
  351. rapi.setRenderTarget(sceneDepthNode->depthTex->renderTexture);
  352. stencilMat->execute(inputs.view, output->texture);
  353. }
  354. void RCNodeMSAACoverage::clear()
  355. {
  356. GpuResourcePool& resPool = GpuResourcePool::instance();
  357. resPool.release(output);
  358. }
  359. SmallVector<StringID, 4> RCNodeMSAACoverage::getDependencies(const RendererView& view)
  360. {
  361. return { RCNodeGBuffer::getNodeId(), RCNodeSceneDepth::getNodeId() };
  362. }
  363. void RCNodeLightAccumulation::render(const RenderCompositorNodeInputs& inputs)
  364. {
  365. GpuResourcePool& resPool = GpuResourcePool::instance();
  366. const RendererViewProperties& viewProps = inputs.view.getProperties();
  367. RCNodeSceneDepth* depthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  368. UINT32 width = viewProps.viewRect.width;
  369. UINT32 height = viewProps.viewRect.height;
  370. UINT32 numSamples = viewProps.numSamples;
  371. if (numSamples > 1)
  372. {
  373. UINT32 bufferNumElements = width * height * numSamples;
  374. flattenedLightAccumBuffer =
  375. resPool.get(POOLED_STORAGE_BUFFER_DESC::createStandard(BF_16X4F, bufferNumElements));
  376. SPtr<GpuBuffer> buffer = flattenedLightAccumBuffer->buffer;
  377. auto& bufferProps = buffer->getProperties();
  378. UINT32 bufferSize = bufferProps.getElementSize() * bufferProps.getElementCount();
  379. UINT16* data = (UINT16*)buffer->lock(0, bufferSize, GBL_WRITE_ONLY_DISCARD);
  380. {
  381. memset(data, 0, bufferSize);
  382. }
  383. buffer->unlock();
  384. }
  385. else
  386. flattenedLightAccumBuffer = nullptr;
  387. lightAccumulationTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width,
  388. height, TU_LOADSTORE | TU_RENDERTARGET, numSamples, false));
  389. bool rebuildRT;
  390. if (renderTarget != nullptr)
  391. {
  392. rebuildRT = renderTarget->getColorTexture(0) != lightAccumulationTex->texture;
  393. rebuildRT |= renderTarget->getDepthStencilTexture() != depthNode->depthTex->texture;
  394. }
  395. else
  396. rebuildRT = true;
  397. if (rebuildRT)
  398. {
  399. RENDER_TEXTURE_DESC lightAccumulationRTDesc;
  400. lightAccumulationRTDesc.colorSurfaces[0].texture = lightAccumulationTex->texture;
  401. lightAccumulationRTDesc.colorSurfaces[0].face = 0;
  402. lightAccumulationRTDesc.colorSurfaces[0].numFaces = 1;
  403. lightAccumulationRTDesc.colorSurfaces[0].mipLevel = 0;
  404. lightAccumulationRTDesc.depthStencilSurface.texture = depthNode->depthTex->texture;
  405. lightAccumulationRTDesc.depthStencilSurface.face = 0;
  406. lightAccumulationRTDesc.depthStencilSurface.numFaces = 1;
  407. lightAccumulationRTDesc.depthStencilSurface.mipLevel = 0;
  408. renderTarget = RenderTexture::create(lightAccumulationRTDesc);
  409. }
  410. }
  411. void RCNodeLightAccumulation::clear()
  412. {
  413. GpuResourcePool& resPool = GpuResourcePool::instance();
  414. resPool.release(lightAccumulationTex);
  415. if (flattenedLightAccumBuffer)
  416. resPool.release(flattenedLightAccumBuffer);
  417. }
  418. SmallVector<StringID, 4> RCNodeLightAccumulation::getDependencies(const RendererView& view)
  419. {
  420. return { RCNodeSceneDepth::getNodeId() };
  421. }
  422. void RCNodeTiledDeferredLighting::render(const RenderCompositorNodeInputs& inputs)
  423. {
  424. output = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[0]);
  425. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  426. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[2]);
  427. const RendererViewProperties& viewProps = inputs.view.getProperties();
  428. SPtr<Texture> msaaCoverage;
  429. if(viewProps.numSamples > 1)
  430. {
  431. RCNodeMSAACoverage* coverageNode = static_cast<RCNodeMSAACoverage*>(inputs.inputNodes[3]);
  432. msaaCoverage = coverageNode->output->texture;
  433. }
  434. TiledDeferredLightingMat* tiledDeferredMat = TiledDeferredLightingMat::getVariation(viewProps.numSamples);
  435. GBufferTextures gbuffer;
  436. gbuffer.albedo = gbufferNode->albedoTex->texture;
  437. gbuffer.normals = gbufferNode->normalTex->texture;
  438. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  439. gbuffer.depth = sceneDepthNode->depthTex->texture;
  440. const VisibleLightData& lightData = inputs.viewGroup.getVisibleLightData();
  441. SPtr<GpuBuffer> flattenedLightAccumBuffer;
  442. if (output->flattenedLightAccumBuffer)
  443. flattenedLightAccumBuffer = output->flattenedLightAccumBuffer->buffer;
  444. tiledDeferredMat->execute(inputs.view, lightData, gbuffer, output->lightAccumulationTex->texture,
  445. flattenedLightAccumBuffer, msaaCoverage);
  446. }
  447. void RCNodeTiledDeferredLighting::clear()
  448. {
  449. output = nullptr;
  450. }
  451. SmallVector<StringID, 4> RCNodeTiledDeferredLighting::getDependencies(const RendererView& view)
  452. {
  453. SmallVector<StringID, 4> deps;
  454. deps.push_back(RCNodeLightAccumulation::getNodeId());
  455. deps.push_back(RCNodeGBuffer::getNodeId());
  456. deps.push_back(RCNodeSceneDepth::getNodeId());
  457. if(view.getProperties().numSamples > 1)
  458. deps.push_back(RCNodeMSAACoverage::getNodeId());
  459. return deps;
  460. }
  461. void RCNodeStandardDeferredLighting::render(const RenderCompositorNodeInputs& inputs)
  462. {
  463. RCNodeTiledDeferredLighting* tileDeferredNode = static_cast<RCNodeTiledDeferredLighting*>(inputs.inputNodes[0]);
  464. output = tileDeferredNode->output;
  465. // If shadows are disabled we handle all lights through tiled deferred
  466. if (!inputs.view.getRenderSettings().enableShadows)
  467. {
  468. mLightOcclusionRT = nullptr;
  469. return;
  470. }
  471. GpuResourcePool& resPool = GpuResourcePool::instance();
  472. const RendererViewProperties& viewProps = inputs.view.getProperties();
  473. UINT32 width = viewProps.viewRect.width;
  474. UINT32 height = viewProps.viewRect.height;
  475. UINT32 numSamples = viewProps.numSamples;
  476. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  477. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[2]);
  478. // Allocate light occlusion
  479. SPtr<PooledRenderTexture> lightOcclusionTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width,
  480. height, TU_RENDERTARGET, numSamples, false));
  481. bool rebuildRT = false;
  482. if (mLightOcclusionRT != nullptr)
  483. {
  484. rebuildRT |= mLightOcclusionRT->getColorTexture(0) != lightOcclusionTex->texture;
  485. rebuildRT |= mLightOcclusionRT->getDepthStencilTexture() != sceneDepthNode->depthTex->texture;
  486. }
  487. else
  488. rebuildRT = true;
  489. if (rebuildRT)
  490. {
  491. RENDER_TEXTURE_DESC lightOcclusionRTDesc;
  492. lightOcclusionRTDesc.colorSurfaces[0].texture = lightOcclusionTex->texture;
  493. lightOcclusionRTDesc.colorSurfaces[0].face = 0;
  494. lightOcclusionRTDesc.colorSurfaces[0].numFaces = 1;
  495. lightOcclusionRTDesc.colorSurfaces[0].mipLevel = 0;
  496. lightOcclusionRTDesc.depthStencilSurface.texture = sceneDepthNode->depthTex->texture;
  497. lightOcclusionRTDesc.depthStencilSurface.face = 0;
  498. lightOcclusionRTDesc.depthStencilSurface.numFaces = 1;
  499. lightOcclusionRTDesc.depthStencilSurface.mipLevel = 0;
  500. mLightOcclusionRT = RenderTexture::create(lightOcclusionRTDesc);
  501. }
  502. GBufferTextures gbuffer;
  503. gbuffer.albedo = gbufferNode->albedoTex->texture;
  504. gbuffer.normals = gbufferNode->normalTex->texture;
  505. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  506. gbuffer.depth = sceneDepthNode->depthTex->texture;
  507. const VisibleLightData& lightData = inputs.viewGroup.getVisibleLightData();
  508. const ShadowRendering& shadowRenderer = inputs.viewGroup.getShadowRenderer();
  509. RenderAPI& rapi = RenderAPI::instance();
  510. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  511. {
  512. LightType lightType = (LightType)i;
  513. auto& lights = lightData.getLights(lightType);
  514. UINT32 count = lightData.getNumShadowedLights(lightType);
  515. UINT32 offset = lightData.getNumUnshadowedLights(lightType);
  516. for (UINT32 j = 0; j < count; j++)
  517. {
  518. rapi.setRenderTarget(mLightOcclusionRT, FBT_DEPTH, RT_DEPTH_STENCIL);
  519. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  520. rapi.setViewport(area);
  521. rapi.clearViewport(FBT_COLOR, Color::ZERO);
  522. UINT32 lightIdx = offset + j;
  523. const RendererLight& light = *lights[lightIdx];
  524. shadowRenderer.renderShadowOcclusion(inputs.view, inputs.options.shadowFilteringQuality, light, gbuffer);
  525. rapi.setRenderTarget(output->renderTarget, FBT_DEPTH | FBT_STENCIL, RT_COLOR0 | RT_DEPTH_STENCIL);
  526. StandardDeferred::instance().renderLight(lightType, light, inputs.view, gbuffer,
  527. lightOcclusionTex->texture);
  528. }
  529. }
  530. // Makes sure light accumulation can be read by following passes
  531. rapi.setRenderTarget(nullptr);
  532. resPool.release(lightOcclusionTex);
  533. }
  534. void RCNodeStandardDeferredLighting::clear()
  535. {
  536. output = nullptr;
  537. }
  538. SmallVector<StringID, 4> RCNodeStandardDeferredLighting::getDependencies(const RendererView& view)
  539. {
  540. SmallVector<StringID, 4> deps;
  541. deps.push_back(RCNodeTiledDeferredLighting::getNodeId());
  542. deps.push_back(RCNodeGBuffer::getNodeId());
  543. deps.push_back(RCNodeSceneDepth::getNodeId());
  544. if (view.getProperties().numSamples > 1)
  545. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  546. return deps;
  547. }
  548. void RCNodeUnflattenLightAccum::render(const RenderCompositorNodeInputs& inputs)
  549. {
  550. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[0]);
  551. FlatFramebufferToTextureMat* material = FlatFramebufferToTextureMat::get();
  552. RenderAPI& rapi = RenderAPI::instance();
  553. rapi.setRenderTarget(lightAccumNode->renderTarget, FBT_DEPTH | FBT_STENCIL, RT_COLOR0 | RT_DEPTH_STENCIL);
  554. material->execute(lightAccumNode->flattenedLightAccumBuffer->buffer, lightAccumNode->lightAccumulationTex->texture);
  555. }
  556. void RCNodeUnflattenLightAccum::clear()
  557. {
  558. output = nullptr;
  559. }
  560. SmallVector<StringID, 4> RCNodeUnflattenLightAccum::getDependencies(const RendererView& view)
  561. {
  562. return { RCNodeLightAccumulation::getNodeId() };
  563. }
  564. void RCNodeIndirectLighting::render(const RenderCompositorNodeInputs& inputs)
  565. {
  566. if (!inputs.view.getRenderSettings().enableIndirectLighting)
  567. return;
  568. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  569. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  570. RCNodeLightAccumulation* lightAccumNode = static_cast <RCNodeLightAccumulation*>(inputs.inputNodes[2]);
  571. SPtr<Texture> ssao;
  572. if (inputs.view.getRenderSettings().ambientOcclusion.enabled)
  573. {
  574. RCNodeSSAO* ssaoNode = static_cast<RCNodeSSAO*>(inputs.inputNodes[4]);
  575. ssao = ssaoNode->output->texture;
  576. }
  577. else
  578. ssao = Texture::WHITE;
  579. GpuResourcePool& resPool = GpuResourcePool::instance();
  580. const RendererViewProperties& viewProps = inputs.view.getProperties();
  581. const LightProbes& lightProbes = inputs.scene.lightProbes;
  582. LightProbesInfo lpInfo = lightProbes.getInfo();
  583. IrradianceEvaluateMat* evaluateMat;
  584. SPtr<PooledRenderTexture> volumeIndices;
  585. if(lightProbes.hasAnyProbes())
  586. {
  587. POOLED_RENDER_TEXTURE_DESC volumeIndicesDesc;
  588. POOLED_RENDER_TEXTURE_DESC depthDesc;
  589. TetrahedraRenderMat::getOutputDesc(inputs.view, volumeIndicesDesc, depthDesc);
  590. volumeIndices = resPool.get(volumeIndicesDesc);
  591. SPtr<PooledRenderTexture> depthTex = resPool.get(depthDesc);
  592. RENDER_TEXTURE_DESC rtDesc;
  593. rtDesc.colorSurfaces[0].texture = volumeIndices->texture;
  594. rtDesc.depthStencilSurface.texture = depthTex->texture;
  595. SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
  596. RenderAPI& rapi = RenderAPI::instance();
  597. rapi.setRenderTarget(rt);
  598. rapi.clearRenderTarget(FBT_DEPTH);
  599. gRendererUtility().clear(-1);
  600. TetrahedraRenderMat* renderTetrahedra = TetrahedraRenderMat::getVariation(viewProps.numSamples > 1, true);
  601. renderTetrahedra->execute(inputs.view, sceneDepthNode->depthTex->texture, lpInfo.tetrahedraVolume, rt);
  602. rt = nullptr;
  603. resPool.release(depthTex);
  604. evaluateMat = IrradianceEvaluateMat::getVariation(viewProps.numSamples > 1, true, false);
  605. }
  606. else // Sky only
  607. {
  608. evaluateMat = IrradianceEvaluateMat::getVariation(viewProps.numSamples > 1, true, true);
  609. }
  610. GBufferTextures gbuffer;
  611. gbuffer.albedo = gbufferNode->albedoTex->texture;
  612. gbuffer.normals = gbufferNode->normalTex->texture;
  613. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  614. gbuffer.depth = sceneDepthNode->depthTex->texture;
  615. SPtr<Texture> volumeIndicesTex;
  616. if (volumeIndices)
  617. volumeIndicesTex = volumeIndices->texture;
  618. evaluateMat->execute(inputs.view, gbuffer, volumeIndicesTex, lpInfo, inputs.scene.skybox, ssao,
  619. lightAccumNode->renderTarget);
  620. if(volumeIndices)
  621. resPool.release(volumeIndices);
  622. }
  623. void RCNodeIndirectLighting::clear()
  624. {
  625. // Do nothing
  626. }
  627. SmallVector<StringID, 4> RCNodeIndirectLighting::getDependencies(const RendererView& view)
  628. {
  629. SmallVector<StringID, 4> deps;
  630. deps.push_back(RCNodeGBuffer::getNodeId());
  631. deps.push_back(RCNodeSceneDepth::getNodeId());
  632. deps.push_back(RCNodeLightAccumulation::getNodeId());
  633. deps.push_back(RCNodeStandardDeferredLighting::getNodeId());
  634. if(view.getRenderSettings().ambientOcclusion.enabled)
  635. deps.push_back(RCNodeSSAO::getNodeId());
  636. if (view.getProperties().numSamples > 1)
  637. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  638. return deps;
  639. }
  640. void RCNodeTiledDeferredIBL::render(const RenderCompositorNodeInputs& inputs)
  641. {
  642. const RenderSettings& rs = inputs.view.getRenderSettings();
  643. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  644. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  645. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[2]);
  646. RCNodeLightAccumulation* lightAccumNode = static_cast <RCNodeLightAccumulation*>(inputs.inputNodes[3]);
  647. SPtr<Texture> ssr;
  648. if (rs.screenSpaceReflections.enabled)
  649. {
  650. RCNodeSSR* ssrNode = static_cast<RCNodeSSR*>(inputs.inputNodes[5]);
  651. ssr = ssrNode->output->texture;
  652. }
  653. else
  654. ssr = Texture::BLACK;
  655. UINT32 nodeIdx = 6;
  656. SPtr<Texture> ssao;
  657. if (rs.ambientOcclusion.enabled)
  658. {
  659. RCNodeSSAO* ssaoNode = static_cast<RCNodeSSAO*>(inputs.inputNodes[nodeIdx++]);
  660. ssao = ssaoNode->output->texture;
  661. }
  662. else
  663. ssao = Texture::WHITE;
  664. const RendererViewProperties& viewProps = inputs.view.getProperties();
  665. SPtr<Texture> msaaCoverage;
  666. if(viewProps.numSamples > 1)
  667. {
  668. RCNodeMSAACoverage* coverageNode = static_cast<RCNodeMSAACoverage*>(inputs.inputNodes[nodeIdx++]);
  669. msaaCoverage = coverageNode->output->texture;
  670. }
  671. TiledDeferredImageBasedLightingMat* material = TiledDeferredImageBasedLightingMat::getVariation(viewProps.numSamples);
  672. TiledDeferredImageBasedLightingMat::Inputs iblInputs;
  673. iblInputs.gbuffer.albedo = gbufferNode->albedoTex->texture;
  674. iblInputs.gbuffer.normals = gbufferNode->normalTex->texture;
  675. iblInputs.gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  676. iblInputs.gbuffer.depth = sceneDepthNode->depthTex->texture;
  677. iblInputs.sceneColorTex = sceneColorNode->sceneColorTex->texture;
  678. iblInputs.lightAccumulation = lightAccumNode->lightAccumulationTex->texture;
  679. iblInputs.preIntegratedGF = RendererTextures::preintegratedEnvGF;
  680. iblInputs.ambientOcclusion = ssao;
  681. iblInputs.ssr = ssr;
  682. iblInputs.msaaCoverage = msaaCoverage;
  683. if(sceneColorNode->flattenedSceneColorBuffer)
  684. iblInputs.sceneColorBuffer = sceneColorNode->flattenedSceneColorBuffer->buffer;
  685. material->execute(inputs.view, inputs.scene, inputs.viewGroup.getVisibleReflProbeData(), iblInputs);
  686. }
  687. void RCNodeTiledDeferredIBL::clear()
  688. {
  689. output = nullptr;
  690. }
  691. SmallVector<StringID, 4> RCNodeTiledDeferredIBL::getDependencies(const RendererView& view)
  692. {
  693. SmallVector<StringID, 4> deps;
  694. deps.push_back(RCNodeSceneColor::getNodeId());
  695. deps.push_back(RCNodeGBuffer::getNodeId());
  696. deps.push_back(RCNodeSceneDepth::getNodeId());
  697. deps.push_back(RCNodeLightAccumulation::getNodeId());
  698. deps.push_back(RCNodeIndirectLighting::getNodeId());
  699. deps.push_back(RCNodeSSR::getNodeId());
  700. if(view.getRenderSettings().ambientOcclusion.enabled)
  701. deps.push_back(RCNodeSSAO::getNodeId());
  702. if(view.getProperties().numSamples > 1)
  703. deps.push_back(RCNodeMSAACoverage::getNodeId());
  704. return deps;
  705. }
  706. void RCNodeUnflattenSceneColor::render(const RenderCompositorNodeInputs& inputs)
  707. {
  708. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  709. FlatFramebufferToTextureMat* material = FlatFramebufferToTextureMat::get();
  710. int readOnlyFlags = FBT_DEPTH | FBT_STENCIL;
  711. RenderAPI& rapi = RenderAPI::instance();
  712. rapi.setRenderTarget(sceneColorNode->renderTarget, readOnlyFlags, RT_COLOR0 | RT_DEPTH_STENCIL);
  713. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  714. rapi.setViewport(area);
  715. material->execute(sceneColorNode->flattenedSceneColorBuffer->buffer, sceneColorNode->sceneColorTex->texture);
  716. }
  717. void RCNodeUnflattenSceneColor::clear()
  718. {
  719. output = nullptr;
  720. }
  721. SmallVector<StringID, 4> RCNodeUnflattenSceneColor::getDependencies(const RendererView& view)
  722. {
  723. return { RCNodeSceneColor::getNodeId() };
  724. }
  725. RCNodeClusteredForward::RCNodeClusteredForward()
  726. {
  727. SAMPLER_STATE_DESC desc;
  728. desc.minFilter = FO_POINT;
  729. desc.magFilter = FO_POINT;
  730. desc.mipFilter = FO_POINT;
  731. desc.addressMode.u = TAM_CLAMP;
  732. desc.addressMode.v = TAM_CLAMP;
  733. desc.addressMode.w = TAM_CLAMP;
  734. mSSRAOSamplerState = SamplerState::create(desc);
  735. }
  736. void RCNodeClusteredForward::render(const RenderCompositorNodeInputs& inputs)
  737. {
  738. const SceneInfo& sceneInfo = inputs.scene;
  739. const RendererViewProperties& viewProps = inputs.view.getProperties();
  740. const VisibleLightData& visibleLightData = inputs.viewGroup.getVisibleLightData();
  741. const VisibleReflProbeData& visibleReflProbeData = inputs.viewGroup.getVisibleReflProbeData();
  742. const LightGrid& lightGrid = inputs.view.getLightGrid();
  743. SPtr<GpuParamBlockBuffer> gridParams;
  744. SPtr<GpuBuffer> gridLightOffsetsAndSize, gridLightIndices;
  745. SPtr<GpuBuffer> gridProbeOffsetsAndSize, gridProbeIndices;
  746. lightGrid.getOutputs(gridLightOffsetsAndSize, gridLightIndices, gridProbeOffsetsAndSize, gridProbeIndices,
  747. gridParams);
  748. // Prepare refl. probe param buffer
  749. ReflProbeParamBuffer reflProbeParamBuffer;
  750. reflProbeParamBuffer.populate(sceneInfo.skybox, visibleReflProbeData, sceneInfo.reflProbeCubemapsTex,
  751. viewProps.renderingReflections);
  752. SPtr<Texture> skyFilteredRadiance;
  753. if(sceneInfo.skybox)
  754. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  755. // Prepare objects for rendering
  756. const VisibilityInfo& visibility = inputs.view.getVisibilityMasks();
  757. UINT32 numRenderables = (UINT32)sceneInfo.renderables.size();
  758. for (UINT32 i = 0; i < numRenderables; i++)
  759. {
  760. if (!visibility.renderables[i])
  761. continue;
  762. for (auto& element : sceneInfo.renderables[i]->elements)
  763. {
  764. bool isTransparent = (element.material->getShader()->getFlags() & (UINT32)ShaderFlags::Transparent) != 0;
  765. if (!isTransparent)
  766. continue;
  767. // Note: It would be nice to be able to set this once and keep it, only updating if the buffers actually
  768. // change (e.g. when growing). Although technically the internal systems should be smart enough to
  769. // avoid updates unless objects actually changed.
  770. if (element.gridParamsBindingIdx != -1)
  771. element.params->setParamBlockBuffer(element.gridParamsBindingIdx, gridParams, true);
  772. element.gridLightOffsetsAndSizeParam.set(gridLightOffsetsAndSize);
  773. element.gridLightIndicesParam.set(gridLightIndices);
  774. element.lightsBufferParam.set(visibleLightData.getLightBuffer());
  775. // Image based lighting params
  776. ImageBasedLightingParams& iblParams = element.imageBasedParams;
  777. if (iblParams.reflProbeParamsBindingIdx != -1)
  778. element.params->setParamBlockBuffer(iblParams.reflProbeParamsBindingIdx, reflProbeParamBuffer.buffer);
  779. element.gridProbeOffsetsAndSizeParam.set(gridProbeOffsetsAndSize);
  780. iblParams.reflectionProbeIndicesParam.set(gridProbeIndices);
  781. iblParams.reflectionProbesParam.set(visibleReflProbeData.getProbeBuffer());
  782. iblParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  783. iblParams.ambientOcclusionTexParam.set(Texture::WHITE); // Note: Add SSAO here?
  784. iblParams.ssrTexParam.set(Texture::BLACK); // Note: Add SSR here?
  785. iblParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  786. iblParams.preintegratedEnvBRDFParam.set(RendererTextures::preintegratedEnvGF);
  787. iblParams.ssrSampParam.set(mSSRAOSamplerState);
  788. iblParams.ambientOcclusionSampParam.set(mSSRAOSamplerState);
  789. }
  790. }
  791. // TODO: Transparent objects cannot receive shadows. In order to support this I'd have to render the light occlusion
  792. // for all lights affecting this object into a single (or a few) textures. I can likely use texture arrays for this,
  793. // or to avoid sampling many textures, perhaps just jam it all in one or few texture channels.
  794. const Vector<RenderQueueElement>& transparentElements = inputs.view.getTransparentQueue()->getSortedElements();
  795. for (auto iter = transparentElements.begin(); iter != transparentElements.end(); ++iter)
  796. {
  797. BeastRenderableElement* renderElem = static_cast<BeastRenderableElement*>(iter->renderElem);
  798. SPtr<Material> material = renderElem->material;
  799. if (iter->applyPass)
  800. gRendererUtility().setPass(material, iter->passIdx, renderElem->techniqueIdx);
  801. gRendererUtility().setPassParams(renderElem->params, iter->passIdx);
  802. if(renderElem->morphVertexDeclaration == nullptr)
  803. gRendererUtility().draw(renderElem->mesh, renderElem->subMesh);
  804. else
  805. gRendererUtility().drawMorph(renderElem->mesh, renderElem->subMesh, renderElem->morphShapeBuffer,
  806. renderElem->morphVertexDeclaration);
  807. }
  808. // Trigger post-lighting callbacks
  809. Camera* sceneCamera = inputs.view.getSceneCamera();
  810. if (sceneCamera != nullptr)
  811. {
  812. for(auto& extension : inputs.extPostLighting)
  813. {
  814. if (extension->check(*sceneCamera))
  815. extension->render(*sceneCamera);
  816. }
  817. }
  818. }
  819. void RCNodeClusteredForward::clear()
  820. {
  821. // Do nothing
  822. }
  823. SmallVector<StringID, 4> RCNodeClusteredForward::getDependencies(const RendererView& view)
  824. {
  825. return { RCNodeSceneColor::getNodeId(), RCNodeSkybox::getNodeId() };
  826. }
  827. void RCNodeSkybox::render(const RenderCompositorNodeInputs& inputs)
  828. {
  829. Skybox* skybox = inputs.scene.skybox;
  830. SPtr<Texture> radiance = skybox ? skybox->getTexture() : nullptr;
  831. if (radiance != nullptr)
  832. {
  833. SkyboxMat* material = SkyboxMat::getVariation(false);
  834. material->bind(inputs.view.getPerViewBuffer());
  835. material->setParams(radiance, Color::White);
  836. }
  837. else
  838. {
  839. Color clearColor = inputs.view.getProperties().clearColor;
  840. SkyboxMat* material = SkyboxMat::getVariation(true);
  841. material->bind(inputs.view.getPerViewBuffer());
  842. material->setParams(nullptr, clearColor);
  843. }
  844. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[1]);
  845. int readOnlyFlags = FBT_DEPTH | FBT_STENCIL;
  846. RenderAPI& rapi = RenderAPI::instance();
  847. rapi.setRenderTarget(sceneColorNode->renderTarget, readOnlyFlags, RT_COLOR0 | RT_DEPTH_STENCIL);
  848. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  849. rapi.setViewport(area);
  850. SPtr<Mesh> mesh = gRendererUtility().getSkyBoxMesh();
  851. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  852. }
  853. void RCNodeSkybox::clear()
  854. { }
  855. SmallVector<StringID, 4> RCNodeSkybox::getDependencies(const RendererView& view)
  856. {
  857. SmallVector<StringID, 4> deps;
  858. deps.push_back(RCNodeTiledDeferredIBL::getNodeId());
  859. deps.push_back(RCNodeSceneColor::getNodeId());
  860. if (view.getProperties().numSamples > 1)
  861. deps.push_back(RCNodeUnflattenSceneColor::getNodeId());
  862. return deps;
  863. }
  864. void RCNodeFinalResolve::render(const RenderCompositorNodeInputs& inputs)
  865. {
  866. const RendererViewProperties& viewProps = inputs.view.getProperties();
  867. SPtr<Texture> input;
  868. if(viewProps.runPostProcessing)
  869. {
  870. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[0]);
  871. // Note: Ideally the last PP effect could write directly to the final target and we could avoid this copy
  872. input = postProcessNode->getLastOutput();
  873. }
  874. else
  875. {
  876. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  877. input = sceneColorNode->sceneColorTex->texture;
  878. }
  879. SPtr<RenderTarget> target = viewProps.target;
  880. RenderAPI& rapi = RenderAPI::instance();
  881. rapi.setRenderTarget(target);
  882. rapi.setViewport(viewProps.nrmViewRect);
  883. gRendererUtility().blit(input, Rect2I::EMPTY, viewProps.flipView);
  884. if(viewProps.encodeDepth)
  885. {
  886. RCNodeResolvedSceneDepth* resolvedSceneDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  887. EncodeDepthMat* encodeDepthMat = EncodeDepthMat::get();
  888. encodeDepthMat->execute(resolvedSceneDepthNode->output->texture, viewProps.depthEncodeNear,
  889. viewProps.depthEncodeFar, target);
  890. }
  891. // Trigger overlay callbacks
  892. Camera* sceneCamera = inputs.view.getSceneCamera();
  893. if (sceneCamera != nullptr)
  894. {
  895. for(auto& extension : inputs.extOverlay)
  896. {
  897. if (extension->check(*sceneCamera))
  898. extension->render(*sceneCamera);
  899. }
  900. }
  901. }
  902. void RCNodeFinalResolve::clear()
  903. { }
  904. SmallVector<StringID, 4> RCNodeFinalResolve::getDependencies(const RendererView& view)
  905. {
  906. const RendererViewProperties& viewProps = view.getProperties();
  907. SmallVector<StringID, 4> deps;
  908. if(viewProps.runPostProcessing)
  909. {
  910. deps.push_back(RCNodePostProcess::getNodeId());
  911. deps.push_back(RCNodeFXAA::getNodeId());
  912. }
  913. else
  914. {
  915. deps.push_back(RCNodeSceneColor::getNodeId());
  916. deps.push_back(RCNodeClusteredForward::getNodeId());
  917. }
  918. if(viewProps.encodeDepth)
  919. deps.push_back(RCNodeResolvedSceneDepth::getNodeId());
  920. return deps;
  921. }
  922. RCNodePostProcess::RCNodePostProcess()
  923. :mOutput(), mAllocated()
  924. { }
  925. void RCNodePostProcess::getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const
  926. {
  927. GpuResourcePool& resPool = GpuResourcePool::instance();
  928. const RendererViewProperties& viewProps = view.getProperties();
  929. UINT32 width = viewProps.viewRect.width;
  930. UINT32 height = viewProps.viewRect.height;
  931. if(!mAllocated[mCurrentIdx])
  932. {
  933. mOutput[mCurrentIdx] = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA8, width, height,
  934. TU_RENDERTARGET, 1, false));
  935. mAllocated[mCurrentIdx] = true;
  936. }
  937. output = mOutput[mCurrentIdx]->renderTexture;
  938. UINT32 otherIdx = (mCurrentIdx + 1) % 2;
  939. if (mAllocated[otherIdx])
  940. lastFrame = mOutput[otherIdx]->texture;
  941. mCurrentIdx = otherIdx;
  942. }
  943. SPtr<Texture> RCNodePostProcess::getLastOutput() const
  944. {
  945. UINT32 otherIdx = (mCurrentIdx + 1) % 2;
  946. if (mAllocated[otherIdx])
  947. return mOutput[otherIdx]->texture;
  948. return nullptr;
  949. }
  950. void RCNodePostProcess::render(const RenderCompositorNodeInputs& inputs)
  951. {
  952. // Do nothing, this is just a helper node
  953. }
  954. void RCNodePostProcess::clear()
  955. {
  956. GpuResourcePool& resPool = GpuResourcePool::instance();
  957. if (mAllocated[0])
  958. resPool.release(mOutput[0]);
  959. if (mAllocated[1])
  960. resPool.release(mOutput[1]);
  961. mAllocated[0] = false;
  962. mAllocated[1] = false;
  963. mCurrentIdx = 0;
  964. }
  965. SmallVector<StringID, 4> RCNodePostProcess::getDependencies(const RendererView& view)
  966. {
  967. return {};
  968. }
  969. RCNodeTonemapping::~RCNodeTonemapping()
  970. {
  971. GpuResourcePool& resPool = GpuResourcePool::instance();
  972. if (mTonemapLUT)
  973. resPool.release(mTonemapLUT);
  974. if (prevEyeAdaptation)
  975. resPool.release(prevEyeAdaptation);
  976. }
  977. void RCNodeTonemapping::render(const RenderCompositorNodeInputs& inputs)
  978. {
  979. GpuResourcePool& resPool = GpuResourcePool::instance();
  980. const RendererViewProperties& viewProps = inputs.view.getProperties();
  981. const RenderSettings& settings = inputs.view.getRenderSettings();
  982. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  983. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[2]);
  984. SPtr<Texture> sceneColor = sceneColorNode->sceneColorTex->texture;
  985. bool hdr = settings.enableHDR;
  986. bool msaa = viewProps.numSamples > 1;
  987. if(hdr && settings.enableAutoExposure)
  988. {
  989. // Downsample scene
  990. DownsampleMat* downsampleMat = DownsampleMat::getVariation(1, msaa);
  991. SPtr<PooledRenderTexture> downsampledScene = resPool.get(DownsampleMat::getOutputDesc(sceneColor));
  992. downsampleMat->execute(sceneColor, downsampledScene->renderTexture);
  993. // Generate histogram
  994. SPtr<PooledRenderTexture> eyeAdaptHistogram = resPool.get(
  995. EyeAdaptHistogramMat::getOutputDesc(downsampledScene->texture));
  996. EyeAdaptHistogramMat* eyeAdaptHistogramMat = EyeAdaptHistogramMat::get();
  997. eyeAdaptHistogramMat->execute(downsampledScene->texture, eyeAdaptHistogram->texture, settings.autoExposure);
  998. // Reduce histogram
  999. SPtr<PooledRenderTexture> reducedHistogram = resPool.get(EyeAdaptHistogramReduceMat::getOutputDesc());
  1000. SPtr<Texture> prevFrameEyeAdaptation;
  1001. if (prevEyeAdaptation != nullptr)
  1002. prevFrameEyeAdaptation = prevEyeAdaptation->texture;
  1003. EyeAdaptHistogramReduceMat* eyeAdaptHistogramReduce = EyeAdaptHistogramReduceMat::get();
  1004. eyeAdaptHistogramReduce->execute(downsampledScene->texture, eyeAdaptHistogram->texture,
  1005. prevFrameEyeAdaptation, reducedHistogram->renderTexture);
  1006. resPool.release(downsampledScene);
  1007. downsampledScene = nullptr;
  1008. resPool.release(eyeAdaptHistogram);
  1009. eyeAdaptHistogram = nullptr;
  1010. // Generate eye adaptation value
  1011. eyeAdaptation = resPool.get(EyeAdaptationMat::getOutputDesc());
  1012. EyeAdaptationMat* eyeAdaptationMat = EyeAdaptationMat::get();
  1013. eyeAdaptationMat->execute(reducedHistogram->texture, eyeAdaptation->renderTexture, inputs.frameInfo.timeDelta,
  1014. settings.autoExposure, settings.exposureScale);
  1015. resPool.release(reducedHistogram);
  1016. reducedHistogram = nullptr;
  1017. }
  1018. else
  1019. {
  1020. if(prevEyeAdaptation)
  1021. resPool.release(prevEyeAdaptation);
  1022. prevEyeAdaptation = nullptr;
  1023. eyeAdaptation = nullptr;
  1024. }
  1025. bool gammaOnly;
  1026. bool autoExposure;
  1027. if (hdr)
  1028. {
  1029. if (settings.enableTonemapping)
  1030. {
  1031. UINT64 latestHash = inputs.view.getRenderSettingsHash();
  1032. bool tonemapLUTDirty = mTonemapLastUpdateHash != latestHash;
  1033. if (tonemapLUTDirty) // Rebuild LUT if PP settings changed
  1034. {
  1035. if(mTonemapLUT == nullptr)
  1036. mTonemapLUT = resPool.get(CreateTonemapLUTMat::getOutputDesc());
  1037. CreateTonemapLUTMat* createLUT = CreateTonemapLUTMat::get();
  1038. createLUT->execute(mTonemapLUT->texture, settings);
  1039. mTonemapLastUpdateHash = latestHash;
  1040. }
  1041. gammaOnly = false;
  1042. }
  1043. else
  1044. gammaOnly = true;
  1045. autoExposure = settings.enableAutoExposure;
  1046. }
  1047. else
  1048. {
  1049. gammaOnly = true;
  1050. autoExposure = false;
  1051. }
  1052. if(gammaOnly)
  1053. {
  1054. if(mTonemapLUT)
  1055. {
  1056. resPool.release(mTonemapLUT);
  1057. mTonemapLUT = nullptr;
  1058. }
  1059. }
  1060. TonemappingMat* tonemapping = TonemappingMat::getVariation(gammaOnly, autoExposure, msaa);
  1061. SPtr<RenderTexture> ppOutput;
  1062. SPtr<Texture> ppLastFrame;
  1063. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1064. SPtr<Texture> eyeAdaptationTex;
  1065. if (eyeAdaptation)
  1066. eyeAdaptationTex = eyeAdaptation->texture;
  1067. SPtr<Texture> tonemapLUTTex;
  1068. if (mTonemapLUT)
  1069. tonemapLUTTex = mTonemapLUT->texture;
  1070. tonemapping->execute(sceneColor, eyeAdaptationTex, tonemapLUTTex, ppOutput, settings);
  1071. }
  1072. void RCNodeTonemapping::clear()
  1073. {
  1074. GpuResourcePool& resPool = GpuResourcePool::instance();
  1075. // Save eye adaptation for next frame
  1076. if(prevEyeAdaptation)
  1077. resPool.release(prevEyeAdaptation);
  1078. std::swap(eyeAdaptation, prevEyeAdaptation);
  1079. }
  1080. SmallVector<StringID, 4> RCNodeTonemapping::getDependencies(const RendererView& view)
  1081. {
  1082. return{ RCNodeSceneColor::getNodeId(), RCNodeClusteredForward::getNodeId(), RCNodePostProcess::getNodeId() };
  1083. }
  1084. void RCNodeGaussianDOF::render(const RenderCompositorNodeInputs& inputs)
  1085. {
  1086. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  1087. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[2]);
  1088. const DepthOfFieldSettings& settings = inputs.view.getRenderSettings().depthOfField;
  1089. bool near = settings.nearBlurAmount > 0.0f;
  1090. bool far = settings.farBlurAmount > 0.0f;
  1091. bool enabled = settings.enabled && (near || far);
  1092. if(!enabled)
  1093. return;
  1094. GaussianDOFSeparateMat* separateMat = GaussianDOFSeparateMat::getVariation(near, far);
  1095. GaussianDOFCombineMat* combineMat = GaussianDOFCombineMat::getVariation(near, far);
  1096. GaussianBlurMat* blurMat = GaussianBlurMat::get();
  1097. SPtr<RenderTexture> ppOutput;
  1098. SPtr<Texture> ppLastFrame;
  1099. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1100. separateMat->execute(ppLastFrame, sceneDepthNode->depthTex->texture, inputs.view, settings);
  1101. SPtr<PooledRenderTexture> nearTex, farTex;
  1102. if(near && far)
  1103. {
  1104. nearTex = separateMat->getOutput(0);
  1105. farTex = separateMat->getOutput(1);
  1106. }
  1107. else
  1108. {
  1109. if (near)
  1110. nearTex = separateMat->getOutput(0);
  1111. else
  1112. farTex = separateMat->getOutput(0);
  1113. }
  1114. // Blur the out of focus pixels
  1115. // Note: Perhaps set up stencil so I can avoid performing blur on unused parts of the textures?
  1116. const TextureProperties& texProps = nearTex ? nearTex->texture->getProperties() : farTex->texture->getProperties();
  1117. POOLED_RENDER_TEXTURE_DESC tempTexDesc = POOLED_RENDER_TEXTURE_DESC::create2D(texProps.getFormat(),
  1118. texProps.getWidth(), texProps.getHeight(), TU_RENDERTARGET);
  1119. SPtr<PooledRenderTexture> tempTexture = GpuResourcePool::instance().get(tempTexDesc);
  1120. SPtr<Texture> blurredNearTex;
  1121. if(nearTex)
  1122. {
  1123. blurMat->execute(nearTex->texture, settings.nearBlurAmount, tempTexture->renderTexture);
  1124. blurredNearTex = tempTexture->texture;
  1125. }
  1126. SPtr<Texture> blurredFarTex;
  1127. if(farTex)
  1128. {
  1129. // If temporary texture is used up, re-use the original near texture for the blurred result
  1130. if(blurredNearTex)
  1131. {
  1132. blurMat->execute(farTex->texture, settings.farBlurAmount, nearTex->renderTexture);
  1133. blurredFarTex = nearTex->texture;
  1134. }
  1135. else // Otherwise just use the temporary
  1136. {
  1137. blurMat->execute(farTex->texture, settings.farBlurAmount, tempTexture->renderTexture);
  1138. blurredFarTex = tempTexture->texture;
  1139. }
  1140. }
  1141. combineMat->execute(ppLastFrame, blurredNearTex, blurredFarTex,
  1142. sceneDepthNode->depthTex->texture, ppOutput, inputs.view, settings);
  1143. separateMat->release();
  1144. GpuResourcePool::instance().release(tempTexture);
  1145. }
  1146. void RCNodeGaussianDOF::clear()
  1147. {
  1148. // Do nothing
  1149. }
  1150. SmallVector<StringID, 4> RCNodeGaussianDOF::getDependencies(const RendererView& view)
  1151. {
  1152. return { RCNodeTonemapping::getNodeId(), RCNodeSceneDepth::getNodeId(), RCNodePostProcess::getNodeId() };
  1153. }
  1154. void RCNodeFXAA::render(const RenderCompositorNodeInputs& inputs)
  1155. {
  1156. const RenderSettings& settings = inputs.view.getRenderSettings();
  1157. if (!settings.enableFXAA)
  1158. return;
  1159. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[1]);
  1160. SPtr<RenderTexture> ppOutput;
  1161. SPtr<Texture> ppLastFrame;
  1162. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1163. // Note: I could skip executing FXAA over DOF and motion blurred pixels
  1164. FXAAMat* fxaa = FXAAMat::get();
  1165. fxaa->execute(ppLastFrame, ppOutput);
  1166. }
  1167. void RCNodeFXAA::clear()
  1168. {
  1169. // Do nothing
  1170. }
  1171. SmallVector<StringID, 4> RCNodeFXAA::getDependencies(const RendererView& view)
  1172. {
  1173. return { RCNodeGaussianDOF::getNodeId(), RCNodePostProcess::getNodeId() };
  1174. }
  1175. void RCNodeResolvedSceneDepth::render(const RenderCompositorNodeInputs& inputs)
  1176. {
  1177. GpuResourcePool& resPool = GpuResourcePool::instance();
  1178. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1179. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  1180. if (viewProps.numSamples > 1)
  1181. {
  1182. UINT32 width = viewProps.viewRect.width;
  1183. UINT32 height = viewProps.viewRect.height;
  1184. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D32_S8X24, width, height,
  1185. TU_DEPTHSTENCIL, 1, false));
  1186. RenderAPI& rapi = RenderAPI::instance();
  1187. rapi.setRenderTarget(output->renderTexture);
  1188. rapi.clearRenderTarget(FBT_STENCIL);
  1189. gRendererUtility().blit(sceneDepthNode->depthTex->texture, Rect2I::EMPTY, false, true);
  1190. mPassThrough = false;
  1191. }
  1192. else
  1193. {
  1194. output = sceneDepthNode->depthTex;
  1195. mPassThrough = true;
  1196. }
  1197. }
  1198. void RCNodeResolvedSceneDepth::clear()
  1199. {
  1200. GpuResourcePool& resPool = GpuResourcePool::instance();
  1201. if (!mPassThrough)
  1202. resPool.release(output);
  1203. else
  1204. output = nullptr;
  1205. mPassThrough = false;
  1206. }
  1207. SmallVector<StringID, 4> RCNodeResolvedSceneDepth::getDependencies(const RendererView& view)
  1208. {
  1209. // GBuffer require because it renders the base pass (populates the depth buffer)
  1210. return { RCNodeSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1211. }
  1212. void RCNodeHiZ::render(const RenderCompositorNodeInputs& inputs)
  1213. {
  1214. GpuResourcePool& resPool = GpuResourcePool::instance();
  1215. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1216. RCNodeResolvedSceneDepth* resolvedSceneDepth = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  1217. UINT32 width = viewProps.viewRect.width;
  1218. UINT32 height = viewProps.viewRect.height;
  1219. UINT32 size = Bitwise::nextPow2(std::max(width, height));
  1220. UINT32 numMips = PixelUtil::getMaxMipmaps(size, size, 1, PF_R32F);
  1221. size = 1 << numMips;
  1222. // Note: Use the 32-bit buffer here as 16-bit causes too much banding (most of the scene gets assigned 4-5 different
  1223. // depth values).
  1224. // - When I add UNORM 16-bit format I should be able to switch to that
  1225. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R32F, size, size, TU_RENDERTARGET, 1, false, 1,
  1226. numMips));
  1227. Rect2 srcRect = viewProps.nrmViewRect;
  1228. // If viewport size is odd, adjust UV
  1229. srcRect.width += (viewProps.viewRect.width % 2) * (1.0f / viewProps.viewRect.width);
  1230. srcRect.height += (viewProps.viewRect.height % 2) * (1.0f / viewProps.viewRect.height);
  1231. // Generate first mip
  1232. RENDER_TEXTURE_DESC rtDesc;
  1233. rtDesc.colorSurfaces[0].texture = output->texture;
  1234. rtDesc.colorSurfaces[0].mipLevel = 0;
  1235. SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
  1236. Rect2 destRect;
  1237. bool downsampledFirstMip = false; // Not used currently
  1238. if (downsampledFirstMip)
  1239. {
  1240. // Make sure that 1 pixel in HiZ maps to a 2x2 block in source
  1241. destRect = Rect2(0, 0,
  1242. Math::ceilToInt(viewProps.viewRect.width / 2.0f) / (float)size,
  1243. Math::ceilToInt(viewProps.viewRect.height / 2.0f) / (float)size);
  1244. BuildHiZMat* material = BuildHiZMat::get();
  1245. material->execute(resolvedSceneDepth->output->texture, 0, srcRect, destRect, rt);
  1246. }
  1247. else // First level is just a copy of the depth buffer
  1248. {
  1249. destRect = Rect2(0, 0,
  1250. viewProps.viewRect.width / (float)size,
  1251. viewProps.viewRect.height / (float)size);
  1252. RenderAPI& rapi = RenderAPI::instance();
  1253. rapi.setRenderTarget(rt);
  1254. rapi.setViewport(destRect);
  1255. Rect2I srcAreaInt;
  1256. srcAreaInt.x = (INT32)(srcRect.x * viewProps.viewRect.width);
  1257. srcAreaInt.y = (INT32)(srcRect.y * viewProps.viewRect.height);
  1258. srcAreaInt.width = (UINT32)(srcRect.width * viewProps.viewRect.width);
  1259. srcAreaInt.height = (UINT32)(srcRect.height * viewProps.viewRect.height);
  1260. gRendererUtility().blit(resolvedSceneDepth->output->texture, srcAreaInt);
  1261. rapi.setViewport(Rect2(0, 0, 1, 1));
  1262. }
  1263. // Generate remaining mip levels
  1264. for(UINT32 i = 1; i <= numMips; i++)
  1265. {
  1266. rtDesc.colorSurfaces[0].mipLevel = i;
  1267. rt = RenderTexture::create(rtDesc);
  1268. BuildHiZMat* material = BuildHiZMat::get();
  1269. material->execute(output->texture, i - 1, destRect, destRect, rt);
  1270. }
  1271. }
  1272. void RCNodeHiZ::clear()
  1273. {
  1274. GpuResourcePool& resPool = GpuResourcePool::instance();
  1275. resPool.release(output);
  1276. }
  1277. SmallVector<StringID, 4> RCNodeHiZ::getDependencies(const RendererView& view)
  1278. {
  1279. // Note: This doesn't actually use any gbuffer textures, but node is a dependency because it renders to the depth
  1280. // buffer. In order to avoid keeping gbuffer textures alive I could separate out the base pass into its own node
  1281. // perhaps. But at the moment it doesn't matter, as anything using HiZ also needs gbuffer.
  1282. return { RCNodeResolvedSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1283. }
  1284. void RCNodeSSAO::render(const RenderCompositorNodeInputs& inputs)
  1285. {
  1286. /** Maximum valid depth range within samples in a sample set. In meters. */
  1287. static const float DEPTH_RANGE = 1.0f;
  1288. GpuResourcePool& resPool = GpuResourcePool::instance();
  1289. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1290. const AmbientOcclusionSettings& settings = inputs.view.getRenderSettings().ambientOcclusion;
  1291. RCNodeResolvedSceneDepth* resolvedDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  1292. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  1293. SPtr<Texture> sceneDepth = resolvedDepthNode->output->texture;
  1294. SPtr<Texture> sceneNormals = gbufferNode->normalTex->texture;
  1295. const TextureProperties& normalsProps = sceneNormals->getProperties();
  1296. SPtr<PooledRenderTexture> resolvedNormals;
  1297. RenderAPI& rapi = RenderAPI::instance();
  1298. if(sceneNormals->getProperties().getNumSamples() > 1)
  1299. {
  1300. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(normalsProps.getFormat(),
  1301. normalsProps.getWidth(), normalsProps.getHeight(), TU_RENDERTARGET);
  1302. resolvedNormals = resPool.get(desc);
  1303. rapi.setRenderTarget(resolvedNormals->renderTexture);
  1304. gRendererUtility().blit(sceneNormals);
  1305. sceneNormals = resolvedNormals->texture;
  1306. }
  1307. // Multiple downsampled AO levels are used to minimize cache trashing. Downsampled AO targets use larger radius,
  1308. // whose contents are then blended with the higher level.
  1309. UINT32 quality = settings.quality;
  1310. UINT32 numDownsampleLevels = 0;
  1311. if (quality > 1)
  1312. numDownsampleLevels = 1;
  1313. else if (quality > 2)
  1314. numDownsampleLevels = 2;
  1315. SSAODownsampleMat* downsample = SSAODownsampleMat::get();
  1316. SPtr<PooledRenderTexture> setupTex0;
  1317. if(numDownsampleLevels > 0)
  1318. {
  1319. Vector2I downsampledSize(
  1320. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1321. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1322. );
  1323. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, downsampledSize.x,
  1324. downsampledSize.y, TU_RENDERTARGET);
  1325. setupTex0 = GpuResourcePool::instance().get(desc);
  1326. downsample->execute(inputs.view, sceneDepth, sceneNormals, setupTex0->renderTexture, DEPTH_RANGE);
  1327. }
  1328. SPtr<PooledRenderTexture> setupTex1;
  1329. if(numDownsampleLevels > 1)
  1330. {
  1331. Vector2I downsampledSize(
  1332. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1333. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1334. );
  1335. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, downsampledSize.x,
  1336. downsampledSize.y, TU_RENDERTARGET);
  1337. setupTex1 = GpuResourcePool::instance().get(desc);
  1338. downsample->execute(inputs.view, sceneDepth, sceneNormals, setupTex1->renderTexture, DEPTH_RANGE);
  1339. }
  1340. SSAOTextureInputs textures;
  1341. textures.sceneDepth = sceneDepth;
  1342. textures.sceneNormals = sceneNormals;
  1343. textures.randomRotations = RendererTextures::ssaoRandomization4x4;
  1344. SPtr<PooledRenderTexture> downAOTex1;
  1345. if(numDownsampleLevels > 1)
  1346. {
  1347. textures.aoSetup = setupTex1->texture;
  1348. Vector2I downsampledSize(
  1349. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1350. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1351. );
  1352. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1353. downsampledSize.y, TU_RENDERTARGET);
  1354. downAOTex1 = GpuResourcePool::instance().get(desc);
  1355. SSAOMat* ssaoMat = SSAOMat::getVariation(false, false, quality);
  1356. ssaoMat->execute(inputs.view, textures, downAOTex1->renderTexture, settings);
  1357. GpuResourcePool::instance().release(setupTex1);
  1358. setupTex1 = nullptr;
  1359. }
  1360. SPtr<PooledRenderTexture> downAOTex0;
  1361. if(numDownsampleLevels > 0)
  1362. {
  1363. textures.aoSetup = setupTex0->texture;
  1364. if(downAOTex1)
  1365. textures.aoDownsampled = downAOTex1->texture;
  1366. Vector2I downsampledSize(
  1367. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1368. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1369. );
  1370. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1371. downsampledSize.y, TU_RENDERTARGET);
  1372. downAOTex0 = GpuResourcePool::instance().get(desc);
  1373. bool upsample = numDownsampleLevels > 1;
  1374. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, false, quality);
  1375. ssaoMat->execute(inputs.view, textures, downAOTex0->renderTexture, settings);
  1376. if(upsample)
  1377. {
  1378. GpuResourcePool::instance().release(downAOTex1);
  1379. downAOTex1 = nullptr;
  1380. }
  1381. }
  1382. UINT32 width = viewProps.viewRect.width;
  1383. UINT32 height = viewProps.viewRect.height;
  1384. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width, height, TU_RENDERTARGET));
  1385. {
  1386. if(setupTex0)
  1387. textures.aoSetup = setupTex0->texture;
  1388. if(downAOTex0)
  1389. textures.aoDownsampled = downAOTex0->texture;
  1390. bool upsample = numDownsampleLevels > 0;
  1391. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, true, quality);
  1392. ssaoMat->execute(inputs.view, textures, output->renderTexture, settings);
  1393. }
  1394. if(resolvedNormals)
  1395. {
  1396. GpuResourcePool::instance().release(resolvedNormals);
  1397. resolvedNormals = nullptr;
  1398. }
  1399. if(numDownsampleLevels > 0)
  1400. {
  1401. GpuResourcePool::instance().release(setupTex0);
  1402. GpuResourcePool::instance().release(downAOTex0);
  1403. }
  1404. // Blur the output
  1405. // Note: If I implement temporal AA then this can probably be avoided. I can instead jitter the sample offsets
  1406. // each frame, and averaging them out should yield blurred AO.
  1407. if(quality > 1) // On level 0 we don't blur at all, on level 1 we use the ad-hoc blur in shader
  1408. {
  1409. const RenderTargetProperties& rtProps = output->renderTexture->getProperties();
  1410. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, rtProps.getWidth(),
  1411. rtProps.getHeight(), TU_RENDERTARGET);
  1412. SPtr<PooledRenderTexture> blurIntermediateTex = GpuResourcePool::instance().get(desc);
  1413. SSAOBlurMat* blurHorz = SSAOBlurMat::getVariation(true);
  1414. SSAOBlurMat* blurVert = SSAOBlurMat::getVariation(false);
  1415. blurHorz->execute(inputs.view, output->texture, sceneDepth, blurIntermediateTex->renderTexture, DEPTH_RANGE);
  1416. blurVert->execute(inputs.view, blurIntermediateTex->texture, sceneDepth, output->renderTexture, DEPTH_RANGE);
  1417. GpuResourcePool::instance().release(blurIntermediateTex);
  1418. }
  1419. }
  1420. void RCNodeSSAO::clear()
  1421. {
  1422. GpuResourcePool& resPool = GpuResourcePool::instance();
  1423. resPool.release(output);
  1424. }
  1425. SmallVector<StringID, 4> RCNodeSSAO::getDependencies(const RendererView& view)
  1426. {
  1427. return { RCNodeResolvedSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1428. }
  1429. RCNodeSSR::~RCNodeSSR()
  1430. {
  1431. deallocOutputs();
  1432. }
  1433. void RCNodeSSR::render(const RenderCompositorNodeInputs& inputs)
  1434. {
  1435. const ScreenSpaceReflectionsSettings& settings = inputs.view.getRenderSettings().screenSpaceReflections;
  1436. if (settings.enabled)
  1437. {
  1438. RenderAPI& rapi = RenderAPI::instance();
  1439. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  1440. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[1]);
  1441. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[2]);
  1442. RCNodeHiZ* hiZNode = static_cast<RCNodeHiZ*>(inputs.inputNodes[3]);
  1443. RCNodeResolvedSceneDepth* resolvedSceneDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[4]);
  1444. GpuResourcePool& resPool = GpuResourcePool::instance();
  1445. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1446. UINT32 width = viewProps.viewRect.width;
  1447. UINT32 height = viewProps.viewRect.height;
  1448. SPtr<Texture> hiZ = hiZNode->output->texture;
  1449. // This will be executing before scene color is resolved, so get the light accum buffer instead
  1450. SPtr<Texture> sceneColor = lightAccumNode->lightAccumulationTex->texture;
  1451. // Resolve multiple samples if MSAA is used
  1452. SPtr<PooledRenderTexture> resolvedSceneColor;
  1453. if(viewProps.numSamples > 1)
  1454. {
  1455. resolvedSceneColor = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height,
  1456. TU_RENDERTARGET));
  1457. rapi.setRenderTarget(resolvedSceneColor->renderTexture);
  1458. gRendererUtility().blit(sceneColor);
  1459. sceneColor = resolvedSceneColor->texture;
  1460. }
  1461. GBufferTextures gbuffer;
  1462. gbuffer.albedo = gbufferNode->albedoTex->texture;
  1463. gbuffer.normals = gbufferNode->normalTex->texture;
  1464. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  1465. gbuffer.depth = sceneDepthNode->depthTex->texture;
  1466. SSRStencilMat* stencilMat = SSRStencilMat::getVariation(viewProps.numSamples > 1, true);
  1467. // Note: Making the assumption that the stencil buffer is clear at this point
  1468. rapi.setRenderTarget(resolvedSceneDepthNode->output->renderTexture, FBT_DEPTH, RT_DEPTH_STENCIL);
  1469. stencilMat->execute(inputs.view, gbuffer, settings);
  1470. SPtr<PooledRenderTexture> traceOutput = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width,
  1471. height, TU_RENDERTARGET));
  1472. RENDER_TEXTURE_DESC traceRtDesc;
  1473. traceRtDesc.colorSurfaces[0].texture = traceOutput->texture;
  1474. traceRtDesc.depthStencilSurface.texture = resolvedSceneDepthNode->output->texture;
  1475. SPtr<RenderTexture> traceRt = RenderTexture::create(traceRtDesc);
  1476. rapi.setRenderTarget(traceRt, FBT_DEPTH | FBT_STENCIL, RT_DEPTH_STENCIL);
  1477. rapi.clearRenderTarget(FBT_COLOR);
  1478. SSRTraceMat* traceMat = SSRTraceMat::getVariation(settings.quality, viewProps.numSamples > 1, true);
  1479. traceMat->execute(inputs.view, gbuffer, sceneColor, hiZ, settings, traceRt);
  1480. if (resolvedSceneColor)
  1481. {
  1482. resPool.release(resolvedSceneColor);
  1483. resolvedSceneColor = nullptr;
  1484. }
  1485. if (mPrevFrame)
  1486. {
  1487. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, TU_RENDERTARGET));
  1488. rapi.setRenderTarget(output->renderTexture);
  1489. rapi.clearRenderTarget(FBT_COLOR);
  1490. SSRResolveMat* resolveMat = SSRResolveMat::getVariation(viewProps.numSamples > 1);
  1491. resolveMat->execute(inputs.view, mPrevFrame->texture, traceOutput->texture, sceneDepthNode->depthTex->texture,
  1492. output->renderTexture);
  1493. resPool.release(traceOutput);
  1494. }
  1495. else
  1496. output = traceOutput;
  1497. RenderAPI::instance().setRenderTarget(nullptr);
  1498. }
  1499. else
  1500. deallocOutputs();
  1501. }
  1502. void RCNodeSSR::clear()
  1503. {
  1504. GpuResourcePool& resPool = GpuResourcePool::instance();
  1505. if(mPrevFrame)
  1506. resPool.release(mPrevFrame);
  1507. mPrevFrame = output;
  1508. output = nullptr;
  1509. }
  1510. void RCNodeSSR::deallocOutputs()
  1511. {
  1512. GpuResourcePool& resPool = GpuResourcePool::instance();
  1513. if(mPrevFrame)
  1514. {
  1515. resPool.release(mPrevFrame);
  1516. mPrevFrame = nullptr;
  1517. }
  1518. }
  1519. SmallVector<StringID, 4> RCNodeSSR::getDependencies(const RendererView& view)
  1520. {
  1521. SmallVector<StringID, 4> deps;
  1522. if (view.getRenderSettings().screenSpaceReflections.enabled)
  1523. {
  1524. deps.push_back(RCNodeSceneDepth::getNodeId());
  1525. deps.push_back(RCNodeLightAccumulation::getNodeId());
  1526. deps.push_back(RCNodeGBuffer::getNodeId());
  1527. deps.push_back(RCNodeHiZ::getNodeId());
  1528. deps.push_back(RCNodeResolvedSceneDepth::getNodeId());
  1529. if (view.getProperties().numSamples > 1)
  1530. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  1531. }
  1532. return deps;
  1533. }
  1534. }}