BsRenderCompositor.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272
  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 == (UINT32)-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 == (UINT32)-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. SPtr<GpuParams> gpuParams = element.params->getGpuParams();
  224. for(UINT32 j = 0; j < GPT_COUNT; j++)
  225. {
  226. const GpuParamBinding& binding = element.perCameraBindings[j];
  227. if(binding.slot != (UINT32)-1)
  228. gpuParams->setParamBlockBuffer(binding.set, binding.slot, inputs.view.getPerViewBuffer());
  229. }
  230. }
  231. }
  232. Camera* sceneCamera = inputs.view.getSceneCamera();
  233. // Trigger pre-base-pass callbacks
  234. if (sceneCamera != nullptr)
  235. {
  236. for(auto& extension : inputs.extPreBasePass)
  237. {
  238. if (extension->check(*sceneCamera))
  239. extension->render(*sceneCamera);
  240. }
  241. }
  242. // Render base pass
  243. RenderAPI& rapi = RenderAPI::instance();
  244. rapi.setRenderTarget(renderTarget);
  245. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  246. rapi.setViewport(area);
  247. // Clear all targets
  248. rapi.clearViewport(FBT_COLOR | FBT_DEPTH | FBT_STENCIL, Color::ZERO, 1.0f, 0);
  249. // Render all visible opaque elements
  250. const Vector<RenderQueueElement>& opaqueElements = inputs.view.getOpaqueQueue()->getSortedElements();
  251. for (auto iter = opaqueElements.begin(); iter != opaqueElements.end(); ++iter)
  252. {
  253. BeastRenderableElement* renderElem = static_cast<BeastRenderableElement*>(iter->renderElem);
  254. SPtr<Material> material = renderElem->material;
  255. if (iter->applyPass)
  256. gRendererUtility().setPass(material, iter->passIdx, renderElem->techniqueIdx);
  257. gRendererUtility().setPassParams(renderElem->params, iter->passIdx);
  258. if(renderElem->morphVertexDeclaration == nullptr)
  259. gRendererUtility().draw(renderElem->mesh, renderElem->subMesh);
  260. else
  261. gRendererUtility().drawMorph(renderElem->mesh, renderElem->subMesh, renderElem->morphShapeBuffer,
  262. renderElem->morphVertexDeclaration);
  263. }
  264. // Make sure that any compute shaders are able to read g-buffer by unbinding it
  265. rapi.setRenderTarget(nullptr);
  266. // Trigger post-base-pass callbacks
  267. if (sceneCamera != nullptr)
  268. {
  269. for(auto& extension : inputs.extPostBasePass)
  270. {
  271. if (extension->check(*sceneCamera))
  272. extension->render(*sceneCamera);
  273. }
  274. }
  275. }
  276. void RCNodeGBuffer::clear()
  277. {
  278. GpuResourcePool& resPool = GpuResourcePool::instance();
  279. resPool.release(albedoTex);
  280. resPool.release(normalTex);
  281. resPool.release(roughMetalTex);
  282. }
  283. SmallVector<StringID, 4> RCNodeGBuffer::getDependencies(const RendererView& view)
  284. {
  285. return { RCNodeSceneDepth::getNodeId() };
  286. }
  287. void RCNodeSceneColor::render(const RenderCompositorNodeInputs& inputs)
  288. {
  289. GpuResourcePool& resPool = GpuResourcePool::instance();
  290. const RendererViewProperties& viewProps = inputs.view.getProperties();
  291. UINT32 width = viewProps.viewRect.width;
  292. UINT32 height = viewProps.viewRect.height;
  293. UINT32 numSamples = viewProps.numSamples;
  294. UINT32 usageFlags = TU_RENDERTARGET;
  295. bool tiledDeferredSupported = inputs.featureSet != RenderBeastFeatureSet::DesktopMacOS;
  296. if(tiledDeferredSupported)
  297. usageFlags |= TU_LOADSTORE;
  298. // Note: Consider customizable HDR format via options? e.g. smaller PF_FLOAT_R11G11B10 or larger 32-bit format
  299. sceneColorTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, usageFlags,
  300. numSamples, false));
  301. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  302. SPtr<PooledRenderTexture> sceneDepthTex = sceneDepthNode->depthTex;
  303. if (tiledDeferredSupported && viewProps.numSamples > 1)
  304. {
  305. UINT32 bufferNumElements = width * height * viewProps.numSamples;
  306. flattenedSceneColorBuffer = resPool.get(POOLED_STORAGE_BUFFER_DESC::createStandard(BF_16X4F, bufferNumElements));
  307. }
  308. else
  309. flattenedSceneColorBuffer = nullptr;
  310. bool rebuildRT = false;
  311. if (renderTarget != nullptr)
  312. {
  313. rebuildRT |= renderTarget->getColorTexture(0) != sceneColorTex->texture;
  314. rebuildRT |= renderTarget->getDepthStencilTexture() != sceneDepthTex->texture;
  315. }
  316. else
  317. rebuildRT = true;
  318. if (rebuildRT)
  319. {
  320. RENDER_TEXTURE_DESC sceneColorDesc;
  321. sceneColorDesc.colorSurfaces[0].texture = sceneColorTex->texture;
  322. sceneColorDesc.colorSurfaces[0].face = 0;
  323. sceneColorDesc.colorSurfaces[0].numFaces = 1;
  324. sceneColorDesc.colorSurfaces[0].mipLevel = 0;
  325. sceneColorDesc.depthStencilSurface.texture = sceneDepthTex->texture;
  326. sceneColorDesc.depthStencilSurface.face = 0;
  327. sceneColorDesc.depthStencilSurface.numFaces = 1;
  328. sceneColorDesc.depthStencilSurface.mipLevel = 0;
  329. renderTarget = RenderTexture::create(sceneColorDesc);
  330. }
  331. }
  332. void RCNodeSceneColor::clear()
  333. {
  334. GpuResourcePool& resPool = GpuResourcePool::instance();
  335. resPool.release(sceneColorTex);
  336. if (flattenedSceneColorBuffer != nullptr)
  337. resPool.release(flattenedSceneColorBuffer);
  338. }
  339. SmallVector<StringID, 4> RCNodeSceneColor::getDependencies(const RendererView& view)
  340. {
  341. return { RCNodeSceneDepth::getNodeId() };
  342. }
  343. void RCNodeMSAACoverage::render(const RenderCompositorNodeInputs& inputs)
  344. {
  345. GpuResourcePool& resPool = GpuResourcePool::instance();
  346. const RendererViewProperties& viewProps = inputs.view.getProperties();
  347. UINT32 width = viewProps.viewRect.width;
  348. UINT32 height = viewProps.viewRect.height;
  349. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width, height, TU_RENDERTARGET));
  350. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  351. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  352. GBufferTextures gbuffer;
  353. gbuffer.albedo = gbufferNode->albedoTex->texture;
  354. gbuffer.normals = gbufferNode->normalTex->texture;
  355. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  356. gbuffer.depth = sceneDepthNode->depthTex->texture;
  357. MSAACoverageMat* mat = MSAACoverageMat::getVariation(viewProps.numSamples);
  358. RenderAPI& rapi = RenderAPI::instance();
  359. rapi.setRenderTarget(output->renderTexture);
  360. mat->execute(inputs.view, gbuffer);
  361. MSAACoverageStencilMat* stencilMat = MSAACoverageStencilMat::get();
  362. rapi.setRenderTarget(sceneDepthNode->depthTex->renderTexture);
  363. stencilMat->execute(inputs.view, output->texture);
  364. rapi.setRenderTarget(nullptr);
  365. }
  366. void RCNodeMSAACoverage::clear()
  367. {
  368. GpuResourcePool& resPool = GpuResourcePool::instance();
  369. resPool.release(output);
  370. }
  371. SmallVector<StringID, 4> RCNodeMSAACoverage::getDependencies(const RendererView& view)
  372. {
  373. return { RCNodeGBuffer::getNodeId(), RCNodeSceneDepth::getNodeId() };
  374. }
  375. void RCNodeLightAccumulation::render(const RenderCompositorNodeInputs& inputs)
  376. {
  377. bool supportsTiledDeferred = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  378. if(!supportsTiledDeferred)
  379. {
  380. // If tiled deferred is not supported, we don't need a separate texture for light accumulation, instead we
  381. // use scene color directly
  382. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  383. lightAccumulationTex = sceneColorNode->sceneColorTex;
  384. renderTarget = sceneColorNode->renderTarget;
  385. mOwnsTexture = false;
  386. return;
  387. }
  388. GpuResourcePool& resPool = GpuResourcePool::instance();
  389. const RendererViewProperties& viewProps = inputs.view.getProperties();
  390. RCNodeSceneDepth* depthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  391. UINT32 width = viewProps.viewRect.width;
  392. UINT32 height = viewProps.viewRect.height;
  393. UINT32 numSamples = viewProps.numSamples;
  394. if (numSamples > 1)
  395. {
  396. UINT32 bufferNumElements = width * height * numSamples;
  397. flattenedLightAccumBuffer =
  398. resPool.get(POOLED_STORAGE_BUFFER_DESC::createStandard(BF_16X4F, bufferNumElements));
  399. SPtr<GpuBuffer> buffer = flattenedLightAccumBuffer->buffer;
  400. auto& bufferProps = buffer->getProperties();
  401. UINT32 bufferSize = bufferProps.getElementSize() * bufferProps.getElementCount();
  402. UINT16* data = (UINT16*)buffer->lock(0, bufferSize, GBL_WRITE_ONLY_DISCARD);
  403. {
  404. memset(data, 0, bufferSize);
  405. }
  406. buffer->unlock();
  407. }
  408. else
  409. flattenedLightAccumBuffer = nullptr;
  410. lightAccumulationTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width,
  411. height, TU_LOADSTORE | TU_RENDERTARGET, numSamples, false));
  412. bool rebuildRT;
  413. if (renderTarget != nullptr)
  414. {
  415. rebuildRT = renderTarget->getColorTexture(0) != lightAccumulationTex->texture;
  416. rebuildRT |= renderTarget->getDepthStencilTexture() != depthNode->depthTex->texture;
  417. }
  418. else
  419. rebuildRT = true;
  420. if (rebuildRT)
  421. {
  422. RENDER_TEXTURE_DESC lightAccumulationRTDesc;
  423. lightAccumulationRTDesc.colorSurfaces[0].texture = lightAccumulationTex->texture;
  424. lightAccumulationRTDesc.colorSurfaces[0].face = 0;
  425. lightAccumulationRTDesc.colorSurfaces[0].numFaces = 1;
  426. lightAccumulationRTDesc.colorSurfaces[0].mipLevel = 0;
  427. lightAccumulationRTDesc.depthStencilSurface.texture = depthNode->depthTex->texture;
  428. lightAccumulationRTDesc.depthStencilSurface.face = 0;
  429. lightAccumulationRTDesc.depthStencilSurface.numFaces = 1;
  430. lightAccumulationRTDesc.depthStencilSurface.mipLevel = 0;
  431. renderTarget = RenderTexture::create(lightAccumulationRTDesc);
  432. }
  433. mOwnsTexture = true;
  434. }
  435. void RCNodeLightAccumulation::clear()
  436. {
  437. GpuResourcePool& resPool = GpuResourcePool::instance();
  438. if(mOwnsTexture)
  439. resPool.release(lightAccumulationTex);
  440. else
  441. {
  442. lightAccumulationTex = nullptr;
  443. renderTarget = nullptr;
  444. }
  445. if (flattenedLightAccumBuffer)
  446. resPool.release(flattenedLightAccumBuffer);
  447. }
  448. SmallVector<StringID, 4> RCNodeLightAccumulation::getDependencies(const RendererView& view)
  449. {
  450. SmallVector<StringID, 4> deps;
  451. bool supportsTiledDeferred = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  452. if(!supportsTiledDeferred)
  453. deps.push_back(RCNodeSceneColor::getNodeId());
  454. else
  455. deps.push_back(RCNodeSceneDepth::getNodeId());
  456. return deps;
  457. }
  458. void RCNodeTiledDeferredLighting::render(const RenderCompositorNodeInputs& inputs)
  459. {
  460. output = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[0]);
  461. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  462. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[2]);
  463. const RendererViewProperties& viewProps = inputs.view.getProperties();
  464. SPtr<Texture> msaaCoverage;
  465. if(viewProps.numSamples > 1)
  466. {
  467. RCNodeMSAACoverage* coverageNode = static_cast<RCNodeMSAACoverage*>(inputs.inputNodes[3]);
  468. msaaCoverage = coverageNode->output->texture;
  469. }
  470. TiledDeferredLightingMat* tiledDeferredMat = TiledDeferredLightingMat::getVariation(viewProps.numSamples);
  471. GBufferTextures gbuffer;
  472. gbuffer.albedo = gbufferNode->albedoTex->texture;
  473. gbuffer.normals = gbufferNode->normalTex->texture;
  474. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  475. gbuffer.depth = sceneDepthNode->depthTex->texture;
  476. const VisibleLightData& lightData = inputs.viewGroup.getVisibleLightData();
  477. SPtr<GpuBuffer> flattenedLightAccumBuffer;
  478. if (output->flattenedLightAccumBuffer)
  479. flattenedLightAccumBuffer = output->flattenedLightAccumBuffer->buffer;
  480. tiledDeferredMat->execute(inputs.view, lightData, gbuffer, output->lightAccumulationTex->texture,
  481. flattenedLightAccumBuffer, msaaCoverage);
  482. }
  483. void RCNodeTiledDeferredLighting::clear()
  484. {
  485. output = nullptr;
  486. }
  487. SmallVector<StringID, 4> RCNodeTiledDeferredLighting::getDependencies(const RendererView& view)
  488. {
  489. SmallVector<StringID, 4> deps;
  490. deps.push_back(RCNodeLightAccumulation::getNodeId());
  491. deps.push_back(RCNodeGBuffer::getNodeId());
  492. deps.push_back(RCNodeSceneDepth::getNodeId());
  493. if(view.getProperties().numSamples > 1)
  494. deps.push_back(RCNodeMSAACoverage::getNodeId());
  495. return deps;
  496. }
  497. void RCNodeStandardDeferredLighting::render(const RenderCompositorNodeInputs& inputs)
  498. {
  499. SPtr<RenderTexture> outputRT;
  500. bool tiledDeferredSupported = inputs.featureSet != RenderBeastFeatureSet::DesktopMacOS;
  501. if(tiledDeferredSupported)
  502. {
  503. RCNodeTiledDeferredLighting* tileDeferredNode = static_cast<RCNodeTiledDeferredLighting*>(inputs.inputNodes[2]);
  504. outputRT = tileDeferredNode->output->renderTarget;
  505. // If shadows are disabled we handle all lights through tiled deferred, except when tiled deferred isn't available
  506. if (!inputs.view.getRenderSettings().enableShadows)
  507. {
  508. mLightOcclusionRT = nullptr;
  509. return;
  510. }
  511. }
  512. else
  513. {
  514. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[2]);
  515. outputRT = lightAccumNode->renderTarget;
  516. mLightOcclusionRT = nullptr;
  517. }
  518. GpuResourcePool& resPool = GpuResourcePool::instance();
  519. const RendererViewProperties& viewProps = inputs.view.getProperties();
  520. UINT32 width = viewProps.viewRect.width;
  521. UINT32 height = viewProps.viewRect.height;
  522. UINT32 numSamples = viewProps.numSamples;
  523. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  524. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  525. GBufferTextures gbuffer;
  526. gbuffer.albedo = gbufferNode->albedoTex->texture;
  527. gbuffer.normals = gbufferNode->normalTex->texture;
  528. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  529. gbuffer.depth = sceneDepthNode->depthTex->texture;
  530. const VisibleLightData& lightData = inputs.viewGroup.getVisibleLightData();
  531. RenderAPI& rapi = RenderAPI::instance();
  532. // Render unshadowed lights
  533. if(!tiledDeferredSupported)
  534. {
  535. rapi.setRenderTarget(outputRT, FBT_DEPTH | FBT_STENCIL, RT_DEPTH_STENCIL);
  536. rapi.clearRenderTarget(FBT_COLOR, Color::ZERO);
  537. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  538. {
  539. LightType lightType = (LightType)i;
  540. auto& lights = lightData.getLights(lightType);
  541. UINT32 count = lightData.getNumUnshadowedLights(lightType);
  542. for (UINT32 j = 0; j < count; j++)
  543. {
  544. UINT32 lightIdx = j;
  545. const RendererLight& light = *lights[lightIdx];
  546. StandardDeferred::instance().renderLight(lightType, light, inputs.view, gbuffer, Texture::BLACK);
  547. }
  548. }
  549. }
  550. // Allocate light occlusion
  551. SPtr<PooledRenderTexture> lightOcclusionTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width,
  552. height, TU_RENDERTARGET, numSamples, false));
  553. bool rebuildRT = false;
  554. if (mLightOcclusionRT != nullptr)
  555. {
  556. rebuildRT |= mLightOcclusionRT->getColorTexture(0) != lightOcclusionTex->texture;
  557. rebuildRT |= mLightOcclusionRT->getDepthStencilTexture() != sceneDepthNode->depthTex->texture;
  558. }
  559. else
  560. rebuildRT = true;
  561. if (rebuildRT)
  562. {
  563. RENDER_TEXTURE_DESC lightOcclusionRTDesc;
  564. lightOcclusionRTDesc.colorSurfaces[0].texture = lightOcclusionTex->texture;
  565. lightOcclusionRTDesc.colorSurfaces[0].face = 0;
  566. lightOcclusionRTDesc.colorSurfaces[0].numFaces = 1;
  567. lightOcclusionRTDesc.colorSurfaces[0].mipLevel = 0;
  568. lightOcclusionRTDesc.depthStencilSurface.texture = sceneDepthNode->depthTex->texture;
  569. lightOcclusionRTDesc.depthStencilSurface.face = 0;
  570. lightOcclusionRTDesc.depthStencilSurface.numFaces = 1;
  571. lightOcclusionRTDesc.depthStencilSurface.mipLevel = 0;
  572. mLightOcclusionRT = RenderTexture::create(lightOcclusionRTDesc);
  573. }
  574. // Render shadowed lights
  575. const ShadowRendering& shadowRenderer = inputs.viewGroup.getShadowRenderer();
  576. for (UINT32 i = 0; i < (UINT32)LightType::Count; i++)
  577. {
  578. LightType lightType = (LightType)i;
  579. auto& lights = lightData.getLights(lightType);
  580. UINT32 count = lightData.getNumShadowedLights(lightType);
  581. UINT32 offset = lightData.getNumUnshadowedLights(lightType);
  582. for (UINT32 j = 0; j < count; j++)
  583. {
  584. rapi.setRenderTarget(mLightOcclusionRT, FBT_DEPTH, RT_DEPTH_STENCIL);
  585. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  586. rapi.setViewport(area);
  587. rapi.clearViewport(FBT_COLOR, Color::ZERO);
  588. UINT32 lightIdx = offset + j;
  589. const RendererLight& light = *lights[lightIdx];
  590. shadowRenderer.renderShadowOcclusion(inputs.view, inputs.options.shadowFilteringQuality, light, gbuffer);
  591. rapi.setRenderTarget(outputRT, FBT_DEPTH | FBT_STENCIL, RT_COLOR0 | RT_DEPTH_STENCIL);
  592. StandardDeferred::instance().renderLight(lightType, light, inputs.view, gbuffer,
  593. lightOcclusionTex->texture);
  594. }
  595. }
  596. // Makes sure light accumulation can be read by following passes
  597. rapi.setRenderTarget(nullptr);
  598. resPool.release(lightOcclusionTex);
  599. }
  600. void RCNodeStandardDeferredLighting::clear()
  601. {
  602. // Do nothing
  603. }
  604. SmallVector<StringID, 4> RCNodeStandardDeferredLighting::getDependencies(const RendererView& view)
  605. {
  606. SmallVector<StringID, 4> deps;
  607. deps.push_back(RCNodeGBuffer::getNodeId());
  608. deps.push_back(RCNodeSceneDepth::getNodeId());
  609. bool tiledDeferredSupported = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  610. if(!tiledDeferredSupported)
  611. {
  612. deps.push_back(RCNodeLightAccumulation::getNodeId());
  613. }
  614. else
  615. {
  616. deps.push_back(RCNodeTiledDeferredLighting::getNodeId());
  617. if (view.getProperties().numSamples > 1)
  618. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  619. }
  620. return deps;
  621. }
  622. void RCNodeStandardDeferredIBL::render(const RenderCompositorNodeInputs& inputs)
  623. {
  624. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[2]);
  625. SPtr<RenderTexture> outputRT = lightAccumNode->renderTarget;
  626. GpuResourcePool& resPool = GpuResourcePool::instance();
  627. const RendererViewProperties& viewProps = inputs.view.getProperties();
  628. UINT32 width = viewProps.viewRect.width;
  629. UINT32 height = viewProps.viewRect.height;
  630. UINT32 numSamples = viewProps.numSamples;
  631. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  632. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  633. GBufferTextures gbuffer;
  634. gbuffer.albedo = gbufferNode->albedoTex->texture;
  635. gbuffer.normals = gbufferNode->normalTex->texture;
  636. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  637. gbuffer.depth = sceneDepthNode->depthTex->texture;
  638. RenderAPI& rapi = RenderAPI::instance();
  639. const RenderSettings& rs = inputs.view.getRenderSettings();
  640. bool isMSAA = viewProps.numSamples > 1;
  641. SPtr<PooledRenderTexture> iblRadianceTex = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width,
  642. height, TU_RENDERTARGET, numSamples, false));
  643. RENDER_TEXTURE_DESC rtDesc;
  644. rtDesc.colorSurfaces[0].texture = iblRadianceTex->texture;
  645. rtDesc.depthStencilSurface.texture = sceneDepthNode->depthTex->texture;
  646. SPtr<GpuParamBlockBuffer> perViewBuffer = inputs.view.getPerViewBuffer();
  647. SPtr<RenderTexture> iblRadianceRT = RenderTexture::create(rtDesc);
  648. rapi.setRenderTarget(iblRadianceRT, FBT_DEPTH | FBT_STENCIL, RT_DEPTH_STENCIL);
  649. const VisibleReflProbeData& probeData = inputs.viewGroup.getVisibleReflProbeData();
  650. ReflProbeParamBuffer reflProbeParams;
  651. reflProbeParams.populate(inputs.scene.skybox, probeData.getNumProbes(), inputs.scene.reflProbeCubemapsTex,
  652. viewProps.capturingReflections);
  653. // Prepare the texture for refl. probe and skybox rendering
  654. {
  655. SPtr<Texture> ssr;
  656. if (rs.screenSpaceReflections.enabled)
  657. {
  658. RCNodeSSR* ssrNode = static_cast<RCNodeSSR*>(inputs.inputNodes[3]);
  659. ssr = ssrNode->output->texture;
  660. }
  661. else
  662. ssr = Texture::BLACK;
  663. UINT32 nodeIdx = 4;
  664. SPtr<Texture> ssao;
  665. if (rs.ambientOcclusion.enabled)
  666. {
  667. RCNodeSSAO* ssaoNode = static_cast<RCNodeSSAO*>(inputs.inputNodes[nodeIdx++]);
  668. ssao = ssaoNode->output->texture;
  669. }
  670. else
  671. ssao = Texture::WHITE;
  672. DeferredIBLSetupMat* mat = DeferredIBLSetupMat::getVariation(isMSAA, true);
  673. mat->bind(gbuffer, perViewBuffer, ssr, ssao, reflProbeParams.buffer);
  674. gRendererUtility().drawScreenQuad();
  675. // Draw pixels requiring per-sample evaluation
  676. if (isMSAA)
  677. {
  678. DeferredIBLSetupMat* msaaMat = DeferredIBLSetupMat::getVariation(true, false);
  679. msaaMat->bind(gbuffer, perViewBuffer, ssr, ssao, reflProbeParams.buffer);
  680. gRendererUtility().drawScreenQuad();
  681. }
  682. }
  683. if (!viewProps.capturingReflections)
  684. {
  685. // Render refl. probes
  686. UINT32 numProbes = probeData.getNumProbes();
  687. for (UINT32 i = 0; i < numProbes; i++)
  688. {
  689. const ReflProbeData& probe = probeData.getProbeData(i);
  690. StandardDeferred::instance().renderReflProbe(probe, inputs.view, gbuffer, inputs.scene,
  691. reflProbeParams.buffer);
  692. }
  693. // Render sky
  694. SPtr<Texture> skyFilteredRadiance;
  695. if (inputs.scene.skybox)
  696. skyFilteredRadiance = inputs.scene.skybox->getFilteredRadiance();
  697. if (skyFilteredRadiance)
  698. {
  699. DeferredIBLSkyMat* skymat = DeferredIBLSkyMat::getVariation(isMSAA, true);
  700. skymat->bind(gbuffer, perViewBuffer, inputs.scene.skybox, reflProbeParams.buffer);
  701. gRendererUtility().drawScreenQuad();
  702. // Draw pixels requiring per-sample evaluation
  703. if (isMSAA)
  704. {
  705. DeferredIBLSkyMat* msaaMat = DeferredIBLSkyMat::getVariation(true, false);
  706. msaaMat->bind(gbuffer, perViewBuffer, inputs.scene.skybox, reflProbeParams.buffer);
  707. gRendererUtility().drawScreenQuad();
  708. }
  709. }
  710. }
  711. // Finalize rendered reflections and output them to main render target
  712. {
  713. rapi.setRenderTarget(outputRT, FBT_DEPTH | FBT_STENCIL, RT_COLOR0 | RT_DEPTH_STENCIL);
  714. DeferredIBLFinalizeMat* mat = DeferredIBLFinalizeMat::getVariation(isMSAA, true);
  715. mat->bind(gbuffer, perViewBuffer, iblRadianceTex->texture, RendererTextures::preintegratedEnvGF,
  716. reflProbeParams.buffer);
  717. gRendererUtility().drawScreenQuad();
  718. // Draw pixels requiring per-sample evaluation
  719. if (isMSAA)
  720. {
  721. DeferredIBLFinalizeMat* msaaMat = DeferredIBLFinalizeMat::getVariation(true, false);
  722. msaaMat->bind(gbuffer, perViewBuffer, iblRadianceTex->texture, RendererTextures::preintegratedEnvGF,
  723. reflProbeParams.buffer);
  724. gRendererUtility().drawScreenQuad();
  725. }
  726. }
  727. // Makes sure light accumulation can be read by following passes
  728. rapi.setRenderTarget(nullptr);
  729. }
  730. void RCNodeStandardDeferredIBL::clear()
  731. {
  732. // Do nothing
  733. }
  734. SmallVector<StringID, 4> RCNodeStandardDeferredIBL::getDependencies(const RendererView& view)
  735. {
  736. SmallVector<StringID, 4> deps;
  737. deps.push_back(RCNodeGBuffer::getNodeId());
  738. deps.push_back(RCNodeSceneDepth::getNodeId());
  739. deps.push_back(RCNodeLightAccumulation::getNodeId());
  740. deps.push_back(RCNodeSSR::getNodeId());
  741. if (view.getRenderSettings().ambientOcclusion.enabled)
  742. deps.push_back(RCNodeSSAO::getNodeId());
  743. deps.push_back(RCNodeStandardDeferredLighting::getNodeId());
  744. return deps;
  745. }
  746. void RCNodeUnflattenLightAccum::render(const RenderCompositorNodeInputs& inputs)
  747. {
  748. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[0]);
  749. FlatFramebufferToTextureMat* material = FlatFramebufferToTextureMat::get();
  750. RenderAPI& rapi = RenderAPI::instance();
  751. rapi.setRenderTarget(lightAccumNode->renderTarget, FBT_DEPTH | FBT_STENCIL, RT_DEPTH_STENCIL);
  752. material->execute(lightAccumNode->flattenedLightAccumBuffer->buffer, lightAccumNode->lightAccumulationTex->texture);
  753. }
  754. void RCNodeUnflattenLightAccum::clear()
  755. {
  756. output = nullptr;
  757. }
  758. SmallVector<StringID, 4> RCNodeUnflattenLightAccum::getDependencies(const RendererView& view)
  759. {
  760. return { RCNodeLightAccumulation::getNodeId() };
  761. }
  762. void RCNodeIndirectLighting::render(const RenderCompositorNodeInputs& inputs)
  763. {
  764. if (!inputs.view.getRenderSettings().enableIndirectLighting)
  765. return;
  766. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[0]);
  767. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  768. RCNodeLightAccumulation* lightAccumNode = static_cast <RCNodeLightAccumulation*>(inputs.inputNodes[2]);
  769. SPtr<Texture> ssao;
  770. if (inputs.view.getRenderSettings().ambientOcclusion.enabled)
  771. {
  772. RCNodeSSAO* ssaoNode = static_cast<RCNodeSSAO*>(inputs.inputNodes[4]);
  773. ssao = ssaoNode->output->texture;
  774. }
  775. else
  776. ssao = Texture::WHITE;
  777. GpuResourcePool& resPool = GpuResourcePool::instance();
  778. const RendererViewProperties& viewProps = inputs.view.getProperties();
  779. const LightProbes& lightProbes = inputs.scene.lightProbes;
  780. LightProbesInfo lpInfo = lightProbes.getInfo();
  781. IrradianceEvaluateMat* evaluateMat;
  782. SPtr<PooledRenderTexture> volumeIndices;
  783. if(lightProbes.hasAnyProbes())
  784. {
  785. POOLED_RENDER_TEXTURE_DESC volumeIndicesDesc;
  786. POOLED_RENDER_TEXTURE_DESC depthDesc;
  787. TetrahedraRenderMat::getOutputDesc(inputs.view, volumeIndicesDesc, depthDesc);
  788. volumeIndices = resPool.get(volumeIndicesDesc);
  789. SPtr<PooledRenderTexture> depthTex = resPool.get(depthDesc);
  790. RENDER_TEXTURE_DESC rtDesc;
  791. rtDesc.colorSurfaces[0].texture = volumeIndices->texture;
  792. rtDesc.depthStencilSurface.texture = depthTex->texture;
  793. SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
  794. RenderAPI& rapi = RenderAPI::instance();
  795. rapi.setRenderTarget(rt);
  796. rapi.clearRenderTarget(FBT_DEPTH);
  797. gRendererUtility().clear(-1);
  798. TetrahedraRenderMat* renderTetrahedra = TetrahedraRenderMat::getVariation(viewProps.numSamples > 1, true);
  799. renderTetrahedra->execute(inputs.view, sceneDepthNode->depthTex->texture, lpInfo.tetrahedraVolume, rt);
  800. rt = nullptr;
  801. resPool.release(depthTex);
  802. evaluateMat = IrradianceEvaluateMat::getVariation(viewProps.numSamples > 1, true, false);
  803. }
  804. else // Sky only
  805. {
  806. evaluateMat = IrradianceEvaluateMat::getVariation(viewProps.numSamples > 1, true, true);
  807. }
  808. GBufferTextures gbuffer;
  809. gbuffer.albedo = gbufferNode->albedoTex->texture;
  810. gbuffer.normals = gbufferNode->normalTex->texture;
  811. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  812. gbuffer.depth = sceneDepthNode->depthTex->texture;
  813. SPtr<Texture> volumeIndicesTex;
  814. if (volumeIndices)
  815. volumeIndicesTex = volumeIndices->texture;
  816. evaluateMat->execute(inputs.view, gbuffer, volumeIndicesTex, lpInfo, inputs.scene.skybox, ssao,
  817. lightAccumNode->renderTarget);
  818. if(volumeIndices)
  819. resPool.release(volumeIndices);
  820. }
  821. void RCNodeIndirectLighting::clear()
  822. {
  823. // Do nothing
  824. }
  825. SmallVector<StringID, 4> RCNodeIndirectLighting::getDependencies(const RendererView& view)
  826. {
  827. SmallVector<StringID, 4> deps;
  828. deps.push_back(RCNodeGBuffer::getNodeId());
  829. deps.push_back(RCNodeSceneDepth::getNodeId());
  830. deps.push_back(RCNodeLightAccumulation::getNodeId());
  831. bool supportsTiledDeferred = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  832. if(supportsTiledDeferred)
  833. deps.push_back(RCNodeStandardDeferredLighting::getNodeId());
  834. else
  835. deps.push_back(RCNodeStandardDeferredIBL::getNodeId());
  836. if(view.getRenderSettings().ambientOcclusion.enabled)
  837. deps.push_back(RCNodeSSAO::getNodeId());
  838. if(supportsTiledDeferred)
  839. {
  840. if (view.getProperties().numSamples > 1)
  841. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  842. }
  843. return deps;
  844. }
  845. void RCNodeTiledDeferredIBL::render(const RenderCompositorNodeInputs& inputs)
  846. {
  847. const RenderSettings& rs = inputs.view.getRenderSettings();
  848. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  849. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  850. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[2]);
  851. RCNodeLightAccumulation* lightAccumNode = static_cast <RCNodeLightAccumulation*>(inputs.inputNodes[3]);
  852. SPtr<Texture> ssr;
  853. if (rs.screenSpaceReflections.enabled)
  854. {
  855. RCNodeSSR* ssrNode = static_cast<RCNodeSSR*>(inputs.inputNodes[5]);
  856. ssr = ssrNode->output->texture;
  857. }
  858. else
  859. ssr = Texture::BLACK;
  860. UINT32 nodeIdx = 6;
  861. SPtr<Texture> ssao;
  862. if (rs.ambientOcclusion.enabled)
  863. {
  864. RCNodeSSAO* ssaoNode = static_cast<RCNodeSSAO*>(inputs.inputNodes[nodeIdx++]);
  865. ssao = ssaoNode->output->texture;
  866. }
  867. else
  868. ssao = Texture::WHITE;
  869. const RendererViewProperties& viewProps = inputs.view.getProperties();
  870. SPtr<Texture> msaaCoverage;
  871. if(viewProps.numSamples > 1)
  872. {
  873. RCNodeMSAACoverage* coverageNode = static_cast<RCNodeMSAACoverage*>(inputs.inputNodes[nodeIdx++]);
  874. msaaCoverage = coverageNode->output->texture;
  875. }
  876. TiledDeferredImageBasedLightingMat* material = TiledDeferredImageBasedLightingMat::getVariation(viewProps.numSamples);
  877. TiledDeferredImageBasedLightingMat::Inputs iblInputs;
  878. iblInputs.gbuffer.albedo = gbufferNode->albedoTex->texture;
  879. iblInputs.gbuffer.normals = gbufferNode->normalTex->texture;
  880. iblInputs.gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  881. iblInputs.gbuffer.depth = sceneDepthNode->depthTex->texture;
  882. iblInputs.sceneColorTex = sceneColorNode->sceneColorTex->texture;
  883. iblInputs.lightAccumulation = lightAccumNode->lightAccumulationTex->texture;
  884. iblInputs.preIntegratedGF = RendererTextures::preintegratedEnvGF;
  885. iblInputs.ambientOcclusion = ssao;
  886. iblInputs.ssr = ssr;
  887. iblInputs.msaaCoverage = msaaCoverage;
  888. if(sceneColorNode->flattenedSceneColorBuffer)
  889. iblInputs.sceneColorBuffer = sceneColorNode->flattenedSceneColorBuffer->buffer;
  890. material->execute(inputs.view, inputs.scene, inputs.viewGroup.getVisibleReflProbeData(), iblInputs);
  891. }
  892. void RCNodeTiledDeferredIBL::clear()
  893. {
  894. output = nullptr;
  895. }
  896. SmallVector<StringID, 4> RCNodeTiledDeferredIBL::getDependencies(const RendererView& view)
  897. {
  898. SmallVector<StringID, 4> deps;
  899. deps.push_back(RCNodeSceneColor::getNodeId());
  900. deps.push_back(RCNodeGBuffer::getNodeId());
  901. deps.push_back(RCNodeSceneDepth::getNodeId());
  902. deps.push_back(RCNodeLightAccumulation::getNodeId());
  903. deps.push_back(RCNodeIndirectLighting::getNodeId());
  904. deps.push_back(RCNodeSSR::getNodeId());
  905. if(view.getRenderSettings().ambientOcclusion.enabled)
  906. deps.push_back(RCNodeSSAO::getNodeId());
  907. if(view.getProperties().numSamples > 1)
  908. deps.push_back(RCNodeMSAACoverage::getNodeId());
  909. return deps;
  910. }
  911. void RCNodeUnflattenSceneColor::render(const RenderCompositorNodeInputs& inputs)
  912. {
  913. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  914. FlatFramebufferToTextureMat* material = FlatFramebufferToTextureMat::get();
  915. int readOnlyFlags = FBT_DEPTH | FBT_STENCIL;
  916. RenderAPI& rapi = RenderAPI::instance();
  917. rapi.setRenderTarget(sceneColorNode->renderTarget, readOnlyFlags, RT_DEPTH_STENCIL);
  918. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  919. rapi.setViewport(area);
  920. material->execute(sceneColorNode->flattenedSceneColorBuffer->buffer, sceneColorNode->sceneColorTex->texture);
  921. }
  922. void RCNodeUnflattenSceneColor::clear()
  923. {
  924. output = nullptr;
  925. }
  926. SmallVector<StringID, 4> RCNodeUnflattenSceneColor::getDependencies(const RendererView& view)
  927. {
  928. return { RCNodeSceneColor::getNodeId() };
  929. }
  930. RCNodeClusteredForward::RCNodeClusteredForward()
  931. { }
  932. void RCNodeClusteredForward::render(const RenderCompositorNodeInputs& inputs)
  933. {
  934. const SceneInfo& sceneInfo = inputs.scene;
  935. const RendererViewProperties& viewProps = inputs.view.getProperties();
  936. const VisibleLightData& visibleLightData = inputs.viewGroup.getVisibleLightData();
  937. const VisibleReflProbeData& visibleReflProbeData = inputs.viewGroup.getVisibleReflProbeData();
  938. const LightGrid& lightGrid = inputs.view.getLightGrid();
  939. SPtr<GpuParamBlockBuffer> gridParams;
  940. SPtr<GpuBuffer> gridLightOffsetsAndSize, gridLightIndices;
  941. SPtr<GpuBuffer> gridProbeOffsetsAndSize, gridProbeIndices;
  942. lightGrid.getOutputs(gridLightOffsetsAndSize, gridLightIndices, gridProbeOffsetsAndSize, gridProbeIndices,
  943. gridParams);
  944. // Prepare refl. probe param buffer
  945. ReflProbeParamBuffer reflProbeParamBuffer;
  946. reflProbeParamBuffer.populate(sceneInfo.skybox, visibleReflProbeData.getNumProbes(), sceneInfo.reflProbeCubemapsTex,
  947. viewProps.capturingReflections);
  948. SPtr<Texture> skyFilteredRadiance;
  949. if(sceneInfo.skybox)
  950. skyFilteredRadiance = sceneInfo.skybox->getFilteredRadiance();
  951. // Prepare objects for rendering
  952. const VisibilityInfo& visibility = inputs.view.getVisibilityMasks();
  953. UINT32 numRenderables = (UINT32)sceneInfo.renderables.size();
  954. for (UINT32 i = 0; i < numRenderables; i++)
  955. {
  956. if (!visibility.renderables[i])
  957. continue;
  958. for (auto& element : sceneInfo.renderables[i]->elements)
  959. {
  960. bool isTransparent = (element.material->getShader()->getFlags() & (UINT32)ShaderFlags::Transparent) != 0;
  961. if (!isTransparent)
  962. continue;
  963. // Note: It would be nice to be able to set this once and keep it, only updating if the buffers actually
  964. // change (e.g. when growing).
  965. SPtr<GpuParams> gpuParams = element.params->getGpuParams();
  966. for(UINT32 j = 0; j < GPT_COUNT; j++)
  967. {
  968. const GpuParamBinding& binding = element.gridParamsBindings[j];
  969. if (binding.slot != (UINT32)-1)
  970. gpuParams->setParamBlockBuffer(binding.set, binding.slot, gridParams);
  971. }
  972. element.gridLightOffsetsAndSizeParam.set(gridLightOffsetsAndSize);
  973. element.gridLightIndicesParam.set(gridLightIndices);
  974. element.lightsBufferParam.set(visibleLightData.getLightBuffer());
  975. // Image based lighting params
  976. ImageBasedLightingParams& iblParams = element.imageBasedParams;
  977. if (iblParams.reflProbeParamBindings.set != (UINT32)-1)
  978. {
  979. gpuParams->setParamBlockBuffer(
  980. iblParams.reflProbeParamBindings.set,
  981. iblParams.reflProbeParamBindings.slot,
  982. reflProbeParamBuffer.buffer);
  983. }
  984. element.gridProbeOffsetsAndSizeParam.set(gridProbeOffsetsAndSize);
  985. iblParams.reflectionProbeIndicesParam.set(gridProbeIndices);
  986. iblParams.reflectionProbesParam.set(visibleReflProbeData.getProbeBuffer());
  987. iblParams.skyReflectionsTexParam.set(skyFilteredRadiance);
  988. iblParams.ambientOcclusionTexParam.set(Texture::WHITE); // Note: Add SSAO here?
  989. iblParams.ssrTexParam.set(Texture::BLACK); // Note: Add SSR here?
  990. iblParams.reflectionProbeCubemapsTexParam.set(sceneInfo.reflProbeCubemapsTex);
  991. iblParams.preintegratedEnvBRDFParam.set(RendererTextures::preintegratedEnvGF);
  992. }
  993. }
  994. // TODO: Transparent objects cannot receive shadows. In order to support this I'd have to render the light occlusion
  995. // for all lights affecting this object into a single (or a few) textures. I can likely use texture arrays for this,
  996. // or to avoid sampling many textures, perhaps just jam it all in one or few texture channels.
  997. const Vector<RenderQueueElement>& transparentElements = inputs.view.getTransparentQueue()->getSortedElements();
  998. for (auto iter = transparentElements.begin(); iter != transparentElements.end(); ++iter)
  999. {
  1000. BeastRenderableElement* renderElem = static_cast<BeastRenderableElement*>(iter->renderElem);
  1001. SPtr<Material> material = renderElem->material;
  1002. if (iter->applyPass)
  1003. gRendererUtility().setPass(material, iter->passIdx, renderElem->techniqueIdx);
  1004. gRendererUtility().setPassParams(renderElem->params, iter->passIdx);
  1005. if(renderElem->morphVertexDeclaration == nullptr)
  1006. gRendererUtility().draw(renderElem->mesh, renderElem->subMesh);
  1007. else
  1008. gRendererUtility().drawMorph(renderElem->mesh, renderElem->subMesh, renderElem->morphShapeBuffer,
  1009. renderElem->morphVertexDeclaration);
  1010. }
  1011. // Trigger post-lighting callbacks
  1012. Camera* sceneCamera = inputs.view.getSceneCamera();
  1013. if (sceneCamera != nullptr)
  1014. {
  1015. for(auto& extension : inputs.extPostLighting)
  1016. {
  1017. if (extension->check(*sceneCamera))
  1018. extension->render(*sceneCamera);
  1019. }
  1020. }
  1021. }
  1022. void RCNodeClusteredForward::clear()
  1023. {
  1024. // Do nothing
  1025. }
  1026. SmallVector<StringID, 4> RCNodeClusteredForward::getDependencies(const RendererView& view)
  1027. {
  1028. return { RCNodeSceneColor::getNodeId(), RCNodeSkybox::getNodeId() };
  1029. }
  1030. void RCNodeSkybox::render(const RenderCompositorNodeInputs& inputs)
  1031. {
  1032. Skybox* skybox = inputs.scene.skybox;
  1033. SPtr<Texture> radiance = skybox ? skybox->getTexture() : nullptr;
  1034. if (radiance != nullptr)
  1035. {
  1036. SkyboxMat* material = SkyboxMat::getVariation(false);
  1037. material->bind(inputs.view.getPerViewBuffer(), radiance, Color::White);
  1038. }
  1039. else
  1040. {
  1041. Color clearColor = inputs.view.getProperties().clearColor;
  1042. SkyboxMat* material = SkyboxMat::getVariation(true);
  1043. material->bind(inputs.view.getPerViewBuffer(), nullptr, clearColor);
  1044. }
  1045. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  1046. int readOnlyFlags = FBT_DEPTH | FBT_STENCIL;
  1047. RenderAPI& rapi = RenderAPI::instance();
  1048. rapi.setRenderTarget(sceneColorNode->renderTarget, readOnlyFlags, RT_COLOR0 | RT_DEPTH_STENCIL);
  1049. Rect2 area(0.0f, 0.0f, 1.0f, 1.0f);
  1050. rapi.setViewport(area);
  1051. SPtr<Mesh> mesh = gRendererUtility().getSkyBoxMesh();
  1052. gRendererUtility().draw(mesh, mesh->getProperties().getSubMesh(0));
  1053. }
  1054. void RCNodeSkybox::clear()
  1055. { }
  1056. SmallVector<StringID, 4> RCNodeSkybox::getDependencies(const RendererView& view)
  1057. {
  1058. bool supportsTiledDeferred = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  1059. SmallVector<StringID, 4> deps;
  1060. deps.push_back(RCNodeSceneColor::getNodeId());
  1061. if(supportsTiledDeferred)
  1062. {
  1063. deps.push_back(RCNodeTiledDeferredIBL::getNodeId());
  1064. if (view.getProperties().numSamples > 1)
  1065. deps.push_back(RCNodeUnflattenSceneColor::getNodeId());
  1066. }
  1067. else
  1068. deps.push_back(RCNodeIndirectLighting::getNodeId());
  1069. return deps;
  1070. }
  1071. void RCNodeFinalResolve::render(const RenderCompositorNodeInputs& inputs)
  1072. {
  1073. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1074. SPtr<Texture> input;
  1075. if(viewProps.runPostProcessing)
  1076. {
  1077. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[0]);
  1078. // Note: Ideally the last PP effect could write directly to the final target and we could avoid this copy
  1079. input = postProcessNode->getLastOutput();
  1080. }
  1081. else
  1082. {
  1083. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  1084. input = sceneColorNode->sceneColorTex->texture;
  1085. }
  1086. SPtr<RenderTarget> target = viewProps.target;
  1087. RenderAPI& rapi = RenderAPI::instance();
  1088. rapi.setRenderTarget(target);
  1089. rapi.setViewport(viewProps.nrmViewRect);
  1090. gRendererUtility().blit(input, Rect2I::EMPTY, viewProps.flipView);
  1091. if(viewProps.encodeDepth)
  1092. {
  1093. RCNodeResolvedSceneDepth* resolvedSceneDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  1094. EncodeDepthMat* encodeDepthMat = EncodeDepthMat::get();
  1095. encodeDepthMat->execute(resolvedSceneDepthNode->output->texture, viewProps.depthEncodeNear,
  1096. viewProps.depthEncodeFar, target);
  1097. }
  1098. // Trigger overlay callbacks
  1099. Camera* sceneCamera = inputs.view.getSceneCamera();
  1100. if (sceneCamera != nullptr)
  1101. {
  1102. for(auto& extension : inputs.extOverlay)
  1103. {
  1104. if (extension->check(*sceneCamera))
  1105. extension->render(*sceneCamera);
  1106. }
  1107. }
  1108. }
  1109. void RCNodeFinalResolve::clear()
  1110. { }
  1111. SmallVector<StringID, 4> RCNodeFinalResolve::getDependencies(const RendererView& view)
  1112. {
  1113. const RendererViewProperties& viewProps = view.getProperties();
  1114. SmallVector<StringID, 4> deps;
  1115. if(viewProps.runPostProcessing)
  1116. {
  1117. deps.push_back(RCNodePostProcess::getNodeId());
  1118. deps.push_back(RCNodeFXAA::getNodeId());
  1119. }
  1120. else
  1121. {
  1122. deps.push_back(RCNodeSceneColor::getNodeId());
  1123. deps.push_back(RCNodeClusteredForward::getNodeId());
  1124. }
  1125. if(viewProps.encodeDepth)
  1126. deps.push_back(RCNodeResolvedSceneDepth::getNodeId());
  1127. return deps;
  1128. }
  1129. RCNodePostProcess::RCNodePostProcess()
  1130. :mOutput(), mAllocated()
  1131. { }
  1132. void RCNodePostProcess::getAndSwitch(const RendererView& view, SPtr<RenderTexture>& output, SPtr<Texture>& lastFrame) const
  1133. {
  1134. GpuResourcePool& resPool = GpuResourcePool::instance();
  1135. const RendererViewProperties& viewProps = view.getProperties();
  1136. UINT32 width = viewProps.viewRect.width;
  1137. UINT32 height = viewProps.viewRect.height;
  1138. if(!mAllocated[mCurrentIdx])
  1139. {
  1140. mOutput[mCurrentIdx] = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA8, width, height,
  1141. TU_RENDERTARGET, 1, false));
  1142. mAllocated[mCurrentIdx] = true;
  1143. }
  1144. output = mOutput[mCurrentIdx]->renderTexture;
  1145. UINT32 otherIdx = (mCurrentIdx + 1) % 2;
  1146. if (mAllocated[otherIdx])
  1147. lastFrame = mOutput[otherIdx]->texture;
  1148. mCurrentIdx = otherIdx;
  1149. }
  1150. SPtr<Texture> RCNodePostProcess::getLastOutput() const
  1151. {
  1152. UINT32 otherIdx = (mCurrentIdx + 1) % 2;
  1153. if (mAllocated[otherIdx])
  1154. return mOutput[otherIdx]->texture;
  1155. return nullptr;
  1156. }
  1157. void RCNodePostProcess::render(const RenderCompositorNodeInputs& inputs)
  1158. {
  1159. // Do nothing, this is just a helper node
  1160. }
  1161. void RCNodePostProcess::clear()
  1162. {
  1163. GpuResourcePool& resPool = GpuResourcePool::instance();
  1164. if (mAllocated[0])
  1165. resPool.release(mOutput[0]);
  1166. if (mAllocated[1])
  1167. resPool.release(mOutput[1]);
  1168. mAllocated[0] = false;
  1169. mAllocated[1] = false;
  1170. mCurrentIdx = 0;
  1171. }
  1172. SmallVector<StringID, 4> RCNodePostProcess::getDependencies(const RendererView& view)
  1173. {
  1174. return {};
  1175. }
  1176. RCNodeTonemapping::~RCNodeTonemapping()
  1177. {
  1178. GpuResourcePool& resPool = GpuResourcePool::instance();
  1179. if (mTonemapLUT)
  1180. resPool.release(mTonemapLUT);
  1181. if (prevEyeAdaptation)
  1182. resPool.release(prevEyeAdaptation);
  1183. }
  1184. void RCNodeTonemapping::render(const RenderCompositorNodeInputs& inputs)
  1185. {
  1186. GpuResourcePool& resPool = GpuResourcePool::instance();
  1187. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1188. const RenderSettings& settings = inputs.view.getRenderSettings();
  1189. RCNodeSceneColor* sceneColorNode = static_cast<RCNodeSceneColor*>(inputs.inputNodes[0]);
  1190. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[2]);
  1191. SPtr<Texture> sceneColor = sceneColorNode->sceneColorTex->texture;
  1192. bool hdr = settings.enableHDR;
  1193. bool msaa = viewProps.numSamples > 1;
  1194. if(hdr && settings.enableAutoExposure)
  1195. {
  1196. // Downsample scene
  1197. DownsampleMat* downsampleMat = DownsampleMat::getVariation(1, msaa);
  1198. SPtr<PooledRenderTexture> downsampledScene = resPool.get(DownsampleMat::getOutputDesc(sceneColor));
  1199. downsampleMat->execute(sceneColor, downsampledScene->renderTexture);
  1200. if(useHistogramEyeAdapatation(inputs))
  1201. {
  1202. // Generate histogram
  1203. SPtr<PooledRenderTexture> eyeAdaptHistogram =
  1204. resPool.get(EyeAdaptHistogramMat::getOutputDesc(downsampledScene->texture));
  1205. EyeAdaptHistogramMat* eyeAdaptHistogramMat = EyeAdaptHistogramMat::get();
  1206. eyeAdaptHistogramMat->execute(downsampledScene->texture, eyeAdaptHistogram->texture, settings.autoExposure);
  1207. // Reduce histogram
  1208. SPtr<PooledRenderTexture> reducedHistogram = resPool.get(EyeAdaptHistogramReduceMat::getOutputDesc());
  1209. SPtr<Texture> prevFrameEyeAdaptation;
  1210. if (prevEyeAdaptation != nullptr)
  1211. prevFrameEyeAdaptation = prevEyeAdaptation->texture;
  1212. EyeAdaptHistogramReduceMat* eyeAdaptHistogramReduce = EyeAdaptHistogramReduceMat::get();
  1213. eyeAdaptHistogramReduce->execute(
  1214. downsampledScene->texture,
  1215. eyeAdaptHistogram->texture,
  1216. prevFrameEyeAdaptation,
  1217. reducedHistogram->renderTexture);
  1218. resPool.release(downsampledScene);
  1219. downsampledScene = nullptr;
  1220. resPool.release(eyeAdaptHistogram);
  1221. eyeAdaptHistogram = nullptr;
  1222. // Generate eye adaptation value
  1223. eyeAdaptation = resPool.get(EyeAdaptationMat::getOutputDesc());
  1224. EyeAdaptationMat* eyeAdaptationMat = EyeAdaptationMat::get();
  1225. eyeAdaptationMat->execute(
  1226. reducedHistogram->texture,
  1227. eyeAdaptation->renderTexture,
  1228. inputs.frameInfo.timeDelta,
  1229. settings.autoExposure,
  1230. settings.exposureScale);
  1231. resPool.release(reducedHistogram);
  1232. reducedHistogram = nullptr;
  1233. }
  1234. else
  1235. {
  1236. // Populate alpha values of the downsampled texture with luminance
  1237. SPtr<PooledRenderTexture> luminanceTex =
  1238. resPool.get(EyeAdaptationBasicSetupMat::getOutputDesc(downsampledScene->texture));
  1239. EyeAdaptationBasicSetupMat* setupMat = EyeAdaptationBasicSetupMat::get();
  1240. setupMat->execute(
  1241. downsampledScene->texture,
  1242. luminanceTex->renderTexture,
  1243. inputs.frameInfo.timeDelta,
  1244. settings.autoExposure,
  1245. settings.exposureScale);
  1246. SPtr<Texture> downsampleInput = luminanceTex->texture;
  1247. luminanceTex = nullptr;
  1248. // Downsample some more
  1249. for(UINT32 i = 0; i < 5; i++)
  1250. {
  1251. downsampleMat = DownsampleMat::getVariation(1, false);
  1252. SPtr<PooledRenderTexture> downsampledLuminance =
  1253. resPool.get(DownsampleMat::getOutputDesc(downsampleInput));
  1254. downsampleMat->execute(downsampleInput, downsampledLuminance->renderTexture);
  1255. downsampleInput = downsampledLuminance->texture;
  1256. }
  1257. // Generate eye adaptation value
  1258. EyeAdaptationBasicMat* eyeAdaptationMat = EyeAdaptationBasicMat::get();
  1259. SPtr<Texture> prevFrameEyeAdaptation;
  1260. if (prevEyeAdaptation != nullptr)
  1261. prevFrameEyeAdaptation = prevEyeAdaptation->texture;
  1262. eyeAdaptation = resPool.get(EyeAdaptationBasicMat::getOutputDesc());
  1263. eyeAdaptationMat->execute(
  1264. downsampleInput,
  1265. prevFrameEyeAdaptation,
  1266. eyeAdaptation->renderTexture,
  1267. inputs.frameInfo.timeDelta,
  1268. settings.autoExposure,
  1269. settings.exposureScale);
  1270. }
  1271. }
  1272. else
  1273. {
  1274. if(prevEyeAdaptation)
  1275. resPool.release(prevEyeAdaptation);
  1276. prevEyeAdaptation = nullptr;
  1277. eyeAdaptation = nullptr;
  1278. }
  1279. bool volumeLUT = inputs.featureSet == RenderBeastFeatureSet::Desktop;
  1280. bool gammaOnly;
  1281. bool autoExposure;
  1282. if (hdr)
  1283. {
  1284. if (settings.enableTonemapping)
  1285. {
  1286. UINT64 latestHash = inputs.view.getRenderSettingsHash();
  1287. bool tonemapLUTDirty = mTonemapLastUpdateHash != latestHash;
  1288. if (tonemapLUTDirty) // Rebuild LUT if PP settings changed
  1289. {
  1290. CreateTonemapLUTMat* createLUT = CreateTonemapLUTMat::getVariation(volumeLUT);
  1291. if(mTonemapLUT == nullptr)
  1292. mTonemapLUT = resPool.get(createLUT->getOutputDesc());
  1293. if(volumeLUT)
  1294. createLUT->execute3D(mTonemapLUT->texture, settings);
  1295. else
  1296. createLUT->execute2D(mTonemapLUT->renderTexture, settings);
  1297. mTonemapLastUpdateHash = latestHash;
  1298. }
  1299. gammaOnly = false;
  1300. }
  1301. else
  1302. gammaOnly = true;
  1303. autoExposure = settings.enableAutoExposure;
  1304. }
  1305. else
  1306. {
  1307. gammaOnly = true;
  1308. autoExposure = false;
  1309. }
  1310. if(gammaOnly)
  1311. {
  1312. if(mTonemapLUT)
  1313. {
  1314. resPool.release(mTonemapLUT);
  1315. mTonemapLUT = nullptr;
  1316. }
  1317. }
  1318. TonemappingMat* tonemapping = TonemappingMat::getVariation(volumeLUT, gammaOnly, autoExposure, msaa);
  1319. SPtr<RenderTexture> ppOutput;
  1320. SPtr<Texture> ppLastFrame;
  1321. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1322. SPtr<Texture> eyeAdaptationTex;
  1323. if (eyeAdaptation)
  1324. eyeAdaptationTex = eyeAdaptation->texture;
  1325. SPtr<Texture> tonemapLUTTex;
  1326. if (mTonemapLUT)
  1327. tonemapLUTTex = mTonemapLUT->texture;
  1328. tonemapping->execute(sceneColor, eyeAdaptationTex, tonemapLUTTex, ppOutput, settings);
  1329. }
  1330. void RCNodeTonemapping::clear()
  1331. {
  1332. GpuResourcePool& resPool = GpuResourcePool::instance();
  1333. // Save eye adaptation for next frame
  1334. if(prevEyeAdaptation)
  1335. resPool.release(prevEyeAdaptation);
  1336. std::swap(eyeAdaptation, prevEyeAdaptation);
  1337. }
  1338. bool RCNodeTonemapping::useHistogramEyeAdapatation(const RenderCompositorNodeInputs& inputs)
  1339. {
  1340. return inputs.featureSet == RenderBeastFeatureSet::Desktop;
  1341. }
  1342. SmallVector<StringID, 4> RCNodeTonemapping::getDependencies(const RendererView& view)
  1343. {
  1344. return{ RCNodeSceneColor::getNodeId(), RCNodeClusteredForward::getNodeId(), RCNodePostProcess::getNodeId() };
  1345. }
  1346. void RCNodeGaussianDOF::render(const RenderCompositorNodeInputs& inputs)
  1347. {
  1348. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[1]);
  1349. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[2]);
  1350. const DepthOfFieldSettings& settings = inputs.view.getRenderSettings().depthOfField;
  1351. bool near = settings.nearBlurAmount > 0.0f;
  1352. bool far = settings.farBlurAmount > 0.0f;
  1353. bool enabled = settings.enabled && (near || far);
  1354. if(!enabled)
  1355. return;
  1356. GaussianDOFSeparateMat* separateMat = GaussianDOFSeparateMat::getVariation(near, far);
  1357. GaussianDOFCombineMat* combineMat = GaussianDOFCombineMat::getVariation(near, far);
  1358. GaussianBlurMat* blurMat = GaussianBlurMat::get();
  1359. SPtr<RenderTexture> ppOutput;
  1360. SPtr<Texture> ppLastFrame;
  1361. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1362. separateMat->execute(ppLastFrame, sceneDepthNode->depthTex->texture, inputs.view, settings);
  1363. SPtr<PooledRenderTexture> nearTex, farTex;
  1364. if(near && far)
  1365. {
  1366. nearTex = separateMat->getOutput(0);
  1367. farTex = separateMat->getOutput(1);
  1368. }
  1369. else
  1370. {
  1371. if (near)
  1372. nearTex = separateMat->getOutput(0);
  1373. else
  1374. farTex = separateMat->getOutput(0);
  1375. }
  1376. // Blur the out of focus pixels
  1377. // Note: Perhaps set up stencil so I can avoid performing blur on unused parts of the textures?
  1378. const TextureProperties& texProps = nearTex ? nearTex->texture->getProperties() : farTex->texture->getProperties();
  1379. POOLED_RENDER_TEXTURE_DESC tempTexDesc = POOLED_RENDER_TEXTURE_DESC::create2D(texProps.getFormat(),
  1380. texProps.getWidth(), texProps.getHeight(), TU_RENDERTARGET);
  1381. SPtr<PooledRenderTexture> tempTexture = GpuResourcePool::instance().get(tempTexDesc);
  1382. SPtr<Texture> blurredNearTex;
  1383. if(nearTex)
  1384. {
  1385. blurMat->execute(nearTex->texture, settings.nearBlurAmount, tempTexture->renderTexture);
  1386. blurredNearTex = tempTexture->texture;
  1387. }
  1388. SPtr<Texture> blurredFarTex;
  1389. if(farTex)
  1390. {
  1391. // If temporary texture is used up, re-use the original near texture for the blurred result
  1392. if(blurredNearTex)
  1393. {
  1394. blurMat->execute(farTex->texture, settings.farBlurAmount, nearTex->renderTexture);
  1395. blurredFarTex = nearTex->texture;
  1396. }
  1397. else // Otherwise just use the temporary
  1398. {
  1399. blurMat->execute(farTex->texture, settings.farBlurAmount, tempTexture->renderTexture);
  1400. blurredFarTex = tempTexture->texture;
  1401. }
  1402. }
  1403. combineMat->execute(ppLastFrame, blurredNearTex, blurredFarTex,
  1404. sceneDepthNode->depthTex->texture, ppOutput, inputs.view, settings);
  1405. separateMat->release();
  1406. GpuResourcePool::instance().release(tempTexture);
  1407. }
  1408. void RCNodeGaussianDOF::clear()
  1409. {
  1410. // Do nothing
  1411. }
  1412. SmallVector<StringID, 4> RCNodeGaussianDOF::getDependencies(const RendererView& view)
  1413. {
  1414. return { RCNodeTonemapping::getNodeId(), RCNodeSceneDepth::getNodeId(), RCNodePostProcess::getNodeId() };
  1415. }
  1416. void RCNodeFXAA::render(const RenderCompositorNodeInputs& inputs)
  1417. {
  1418. const RenderSettings& settings = inputs.view.getRenderSettings();
  1419. if (!settings.enableFXAA)
  1420. return;
  1421. RCNodePostProcess* postProcessNode = static_cast<RCNodePostProcess*>(inputs.inputNodes[1]);
  1422. SPtr<RenderTexture> ppOutput;
  1423. SPtr<Texture> ppLastFrame;
  1424. postProcessNode->getAndSwitch(inputs.view, ppOutput, ppLastFrame);
  1425. // Note: I could skip executing FXAA over DOF and motion blurred pixels
  1426. FXAAMat* fxaa = FXAAMat::get();
  1427. fxaa->execute(ppLastFrame, ppOutput);
  1428. }
  1429. void RCNodeFXAA::clear()
  1430. {
  1431. // Do nothing
  1432. }
  1433. SmallVector<StringID, 4> RCNodeFXAA::getDependencies(const RendererView& view)
  1434. {
  1435. return { RCNodeGaussianDOF::getNodeId(), RCNodePostProcess::getNodeId() };
  1436. }
  1437. void RCNodeResolvedSceneDepth::render(const RenderCompositorNodeInputs& inputs)
  1438. {
  1439. GpuResourcePool& resPool = GpuResourcePool::instance();
  1440. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1441. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  1442. if (viewProps.numSamples > 1)
  1443. {
  1444. UINT32 width = viewProps.viewRect.width;
  1445. UINT32 height = viewProps.viewRect.height;
  1446. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_D32_S8X24, width, height,
  1447. TU_DEPTHSTENCIL, 1, false));
  1448. RenderAPI& rapi = RenderAPI::instance();
  1449. rapi.setRenderTarget(output->renderTexture);
  1450. rapi.clearRenderTarget(FBT_STENCIL);
  1451. gRendererUtility().blit(sceneDepthNode->depthTex->texture, Rect2I::EMPTY, false, true);
  1452. mPassThrough = false;
  1453. }
  1454. else
  1455. {
  1456. output = sceneDepthNode->depthTex;
  1457. mPassThrough = true;
  1458. }
  1459. }
  1460. void RCNodeResolvedSceneDepth::clear()
  1461. {
  1462. GpuResourcePool& resPool = GpuResourcePool::instance();
  1463. if (!mPassThrough)
  1464. resPool.release(output);
  1465. else
  1466. output = nullptr;
  1467. mPassThrough = false;
  1468. }
  1469. SmallVector<StringID, 4> RCNodeResolvedSceneDepth::getDependencies(const RendererView& view)
  1470. {
  1471. // GBuffer require because it renders the base pass (populates the depth buffer)
  1472. return { RCNodeSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1473. }
  1474. void RCNodeHiZ::render(const RenderCompositorNodeInputs& inputs)
  1475. {
  1476. GpuResourcePool& resPool = GpuResourcePool::instance();
  1477. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1478. RCNodeResolvedSceneDepth* resolvedSceneDepth = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  1479. UINT32 width = viewProps.viewRect.width;
  1480. UINT32 height = viewProps.viewRect.height;
  1481. UINT32 size = Bitwise::nextPow2(std::max(width, height));
  1482. UINT32 numMips = PixelUtil::getMaxMipmaps(size, size, 1, PF_R32F);
  1483. size = 1 << numMips;
  1484. // Note: Use the 32-bit buffer here as 16-bit causes too much banding (most of the scene gets assigned 4-5 different
  1485. // depth values).
  1486. // - When I add UNORM 16-bit format I should be able to switch to that
  1487. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R32F, size, size, TU_RENDERTARGET, 1, false, 1,
  1488. numMips));
  1489. Rect2 srcRect = viewProps.nrmViewRect;
  1490. // If viewport size is odd, adjust UV
  1491. srcRect.width += (viewProps.viewRect.width % 2) * (1.0f / viewProps.viewRect.width);
  1492. srcRect.height += (viewProps.viewRect.height % 2) * (1.0f / viewProps.viewRect.height);
  1493. // Generate first mip
  1494. RENDER_TEXTURE_DESC rtDesc;
  1495. rtDesc.colorSurfaces[0].texture = output->texture;
  1496. rtDesc.colorSurfaces[0].mipLevel = 0;
  1497. SPtr<RenderTexture> rt = RenderTexture::create(rtDesc);
  1498. Rect2 destRect;
  1499. bool downsampledFirstMip = false; // Not used currently
  1500. if (downsampledFirstMip)
  1501. {
  1502. // Make sure that 1 pixel in HiZ maps to a 2x2 block in source
  1503. destRect = Rect2(0, 0,
  1504. Math::ceilToInt(viewProps.viewRect.width / 2.0f) / (float)size,
  1505. Math::ceilToInt(viewProps.viewRect.height / 2.0f) / (float)size);
  1506. BuildHiZMat* material = BuildHiZMat::get();
  1507. material->execute(resolvedSceneDepth->output->texture, 0, srcRect, destRect, rt);
  1508. }
  1509. else // First level is just a copy of the depth buffer
  1510. {
  1511. destRect = Rect2(0, 0,
  1512. viewProps.viewRect.width / (float)size,
  1513. viewProps.viewRect.height / (float)size);
  1514. RenderAPI& rapi = RenderAPI::instance();
  1515. rapi.setRenderTarget(rt);
  1516. rapi.setViewport(destRect);
  1517. Rect2I srcAreaInt;
  1518. srcAreaInt.x = (INT32)(srcRect.x * viewProps.viewRect.width);
  1519. srcAreaInt.y = (INT32)(srcRect.y * viewProps.viewRect.height);
  1520. srcAreaInt.width = (UINT32)(srcRect.width * viewProps.viewRect.width);
  1521. srcAreaInt.height = (UINT32)(srcRect.height * viewProps.viewRect.height);
  1522. gRendererUtility().blit(resolvedSceneDepth->output->texture, srcAreaInt);
  1523. rapi.setViewport(Rect2(0, 0, 1, 1));
  1524. }
  1525. // Generate remaining mip levels
  1526. for(UINT32 i = 1; i <= numMips; i++)
  1527. {
  1528. rtDesc.colorSurfaces[0].mipLevel = i;
  1529. rt = RenderTexture::create(rtDesc);
  1530. BuildHiZMat* material = BuildHiZMat::get();
  1531. material->execute(output->texture, i - 1, destRect, destRect, rt);
  1532. }
  1533. }
  1534. void RCNodeHiZ::clear()
  1535. {
  1536. GpuResourcePool& resPool = GpuResourcePool::instance();
  1537. resPool.release(output);
  1538. }
  1539. SmallVector<StringID, 4> RCNodeHiZ::getDependencies(const RendererView& view)
  1540. {
  1541. // Note: This doesn't actually use any gbuffer textures, but node is a dependency because it renders to the depth
  1542. // buffer. In order to avoid keeping gbuffer textures alive I could separate out the base pass into its own node
  1543. // perhaps. But at the moment it doesn't matter, as anything using HiZ also needs gbuffer.
  1544. return { RCNodeResolvedSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1545. }
  1546. void RCNodeSSAO::render(const RenderCompositorNodeInputs& inputs)
  1547. {
  1548. /** Maximum valid depth range within samples in a sample set. In meters. */
  1549. static const float DEPTH_RANGE = 1.0f;
  1550. GpuResourcePool& resPool = GpuResourcePool::instance();
  1551. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1552. const AmbientOcclusionSettings& settings = inputs.view.getRenderSettings().ambientOcclusion;
  1553. RCNodeResolvedSceneDepth* resolvedDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[0]);
  1554. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[1]);
  1555. SPtr<Texture> sceneDepth = resolvedDepthNode->output->texture;
  1556. SPtr<Texture> sceneNormals = gbufferNode->normalTex->texture;
  1557. const TextureProperties& normalsProps = sceneNormals->getProperties();
  1558. SPtr<PooledRenderTexture> resolvedNormals;
  1559. RenderAPI& rapi = RenderAPI::instance();
  1560. if(sceneNormals->getProperties().getNumSamples() > 1)
  1561. {
  1562. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(normalsProps.getFormat(),
  1563. normalsProps.getWidth(), normalsProps.getHeight(), TU_RENDERTARGET);
  1564. resolvedNormals = resPool.get(desc);
  1565. rapi.setRenderTarget(resolvedNormals->renderTexture);
  1566. gRendererUtility().blit(sceneNormals);
  1567. sceneNormals = resolvedNormals->texture;
  1568. }
  1569. // Multiple downsampled AO levels are used to minimize cache trashing. Downsampled AO targets use larger radius,
  1570. // whose contents are then blended with the higher level.
  1571. UINT32 quality = settings.quality;
  1572. UINT32 numDownsampleLevels = 0;
  1573. if (quality > 1)
  1574. numDownsampleLevels = 1;
  1575. else if (quality > 2)
  1576. numDownsampleLevels = 2;
  1577. SSAODownsampleMat* downsample = SSAODownsampleMat::get();
  1578. SPtr<PooledRenderTexture> setupTex0;
  1579. if(numDownsampleLevels > 0)
  1580. {
  1581. Vector2I downsampledSize(
  1582. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1583. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1584. );
  1585. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, downsampledSize.x,
  1586. downsampledSize.y, TU_RENDERTARGET);
  1587. setupTex0 = GpuResourcePool::instance().get(desc);
  1588. downsample->execute(inputs.view, sceneDepth, sceneNormals, setupTex0->renderTexture, DEPTH_RANGE);
  1589. }
  1590. SPtr<PooledRenderTexture> setupTex1;
  1591. if(numDownsampleLevels > 1)
  1592. {
  1593. Vector2I downsampledSize(
  1594. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1595. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1596. );
  1597. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, downsampledSize.x,
  1598. downsampledSize.y, TU_RENDERTARGET);
  1599. setupTex1 = GpuResourcePool::instance().get(desc);
  1600. downsample->execute(inputs.view, sceneDepth, sceneNormals, setupTex1->renderTexture, DEPTH_RANGE);
  1601. }
  1602. SSAOTextureInputs textures;
  1603. textures.sceneDepth = sceneDepth;
  1604. textures.sceneNormals = sceneNormals;
  1605. textures.randomRotations = RendererTextures::ssaoRandomization4x4;
  1606. SPtr<PooledRenderTexture> downAOTex1;
  1607. if(numDownsampleLevels > 1)
  1608. {
  1609. textures.aoSetup = setupTex1->texture;
  1610. Vector2I downsampledSize(
  1611. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 4)),
  1612. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 4))
  1613. );
  1614. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1615. downsampledSize.y, TU_RENDERTARGET);
  1616. downAOTex1 = GpuResourcePool::instance().get(desc);
  1617. SSAOMat* ssaoMat = SSAOMat::getVariation(false, false, quality);
  1618. ssaoMat->execute(inputs.view, textures, downAOTex1->renderTexture, settings);
  1619. GpuResourcePool::instance().release(setupTex1);
  1620. setupTex1 = nullptr;
  1621. }
  1622. SPtr<PooledRenderTexture> downAOTex0;
  1623. if(numDownsampleLevels > 0)
  1624. {
  1625. textures.aoSetup = setupTex0->texture;
  1626. if(downAOTex1)
  1627. textures.aoDownsampled = downAOTex1->texture;
  1628. Vector2I downsampledSize(
  1629. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.width, 2)),
  1630. std::max(1, Math::divideAndRoundUp((INT32)viewProps.viewRect.height, 2))
  1631. );
  1632. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, downsampledSize.x,
  1633. downsampledSize.y, TU_RENDERTARGET);
  1634. downAOTex0 = GpuResourcePool::instance().get(desc);
  1635. bool upsample = numDownsampleLevels > 1;
  1636. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, false, quality);
  1637. ssaoMat->execute(inputs.view, textures, downAOTex0->renderTexture, settings);
  1638. if(upsample)
  1639. {
  1640. GpuResourcePool::instance().release(downAOTex1);
  1641. downAOTex1 = nullptr;
  1642. }
  1643. }
  1644. UINT32 width = viewProps.viewRect.width;
  1645. UINT32 height = viewProps.viewRect.height;
  1646. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, width, height, TU_RENDERTARGET));
  1647. {
  1648. if(setupTex0)
  1649. textures.aoSetup = setupTex0->texture;
  1650. if(downAOTex0)
  1651. textures.aoDownsampled = downAOTex0->texture;
  1652. bool upsample = numDownsampleLevels > 0;
  1653. SSAOMat* ssaoMat = SSAOMat::getVariation(upsample, true, quality);
  1654. ssaoMat->execute(inputs.view, textures, output->renderTexture, settings);
  1655. }
  1656. if(resolvedNormals)
  1657. {
  1658. GpuResourcePool::instance().release(resolvedNormals);
  1659. resolvedNormals = nullptr;
  1660. }
  1661. if(numDownsampleLevels > 0)
  1662. {
  1663. GpuResourcePool::instance().release(setupTex0);
  1664. GpuResourcePool::instance().release(downAOTex0);
  1665. }
  1666. // Blur the output
  1667. // Note: If I implement temporal AA then this can probably be avoided. I can instead jitter the sample offsets
  1668. // each frame, and averaging them out should yield blurred AO.
  1669. if(quality > 1) // On level 0 we don't blur at all, on level 1 we use the ad-hoc blur in shader
  1670. {
  1671. const RenderTargetProperties& rtProps = output->renderTexture->getProperties();
  1672. POOLED_RENDER_TEXTURE_DESC desc = POOLED_RENDER_TEXTURE_DESC::create2D(PF_R8, rtProps.width,
  1673. rtProps.height, TU_RENDERTARGET);
  1674. SPtr<PooledRenderTexture> blurIntermediateTex = GpuResourcePool::instance().get(desc);
  1675. SSAOBlurMat* blurHorz = SSAOBlurMat::getVariation(true);
  1676. SSAOBlurMat* blurVert = SSAOBlurMat::getVariation(false);
  1677. blurHorz->execute(inputs.view, output->texture, sceneDepth, blurIntermediateTex->renderTexture, DEPTH_RANGE);
  1678. blurVert->execute(inputs.view, blurIntermediateTex->texture, sceneDepth, output->renderTexture, DEPTH_RANGE);
  1679. GpuResourcePool::instance().release(blurIntermediateTex);
  1680. }
  1681. RenderAPI::instance().setRenderTarget(nullptr);
  1682. }
  1683. void RCNodeSSAO::clear()
  1684. {
  1685. GpuResourcePool& resPool = GpuResourcePool::instance();
  1686. resPool.release(output);
  1687. }
  1688. SmallVector<StringID, 4> RCNodeSSAO::getDependencies(const RendererView& view)
  1689. {
  1690. return { RCNodeResolvedSceneDepth::getNodeId(), RCNodeGBuffer::getNodeId() };
  1691. }
  1692. RCNodeSSR::~RCNodeSSR()
  1693. {
  1694. deallocOutputs();
  1695. }
  1696. void RCNodeSSR::render(const RenderCompositorNodeInputs& inputs)
  1697. {
  1698. const ScreenSpaceReflectionsSettings& settings = inputs.view.getRenderSettings().screenSpaceReflections;
  1699. if (settings.enabled)
  1700. {
  1701. RenderAPI& rapi = RenderAPI::instance();
  1702. RCNodeSceneDepth* sceneDepthNode = static_cast<RCNodeSceneDepth*>(inputs.inputNodes[0]);
  1703. RCNodeLightAccumulation* lightAccumNode = static_cast<RCNodeLightAccumulation*>(inputs.inputNodes[1]);
  1704. RCNodeGBuffer* gbufferNode = static_cast<RCNodeGBuffer*>(inputs.inputNodes[2]);
  1705. RCNodeHiZ* hiZNode = static_cast<RCNodeHiZ*>(inputs.inputNodes[3]);
  1706. RCNodeResolvedSceneDepth* resolvedSceneDepthNode = static_cast<RCNodeResolvedSceneDepth*>(inputs.inputNodes[4]);
  1707. GpuResourcePool& resPool = GpuResourcePool::instance();
  1708. const RendererViewProperties& viewProps = inputs.view.getProperties();
  1709. UINT32 width = viewProps.viewRect.width;
  1710. UINT32 height = viewProps.viewRect.height;
  1711. SPtr<Texture> hiZ = hiZNode->output->texture;
  1712. // This will be executing before scene color is resolved, so get the light accum buffer instead
  1713. SPtr<Texture> sceneColor = lightAccumNode->lightAccumulationTex->texture;
  1714. // Resolve multiple samples if MSAA is used
  1715. SPtr<PooledRenderTexture> resolvedSceneColor;
  1716. if(viewProps.numSamples > 1)
  1717. {
  1718. resolvedSceneColor = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height,
  1719. TU_RENDERTARGET));
  1720. rapi.setRenderTarget(resolvedSceneColor->renderTexture);
  1721. gRendererUtility().blit(sceneColor);
  1722. sceneColor = resolvedSceneColor->texture;
  1723. }
  1724. GBufferTextures gbuffer;
  1725. gbuffer.albedo = gbufferNode->albedoTex->texture;
  1726. gbuffer.normals = gbufferNode->normalTex->texture;
  1727. gbuffer.roughMetal = gbufferNode->roughMetalTex->texture;
  1728. gbuffer.depth = sceneDepthNode->depthTex->texture;
  1729. SSRStencilMat* stencilMat = SSRStencilMat::getVariation(viewProps.numSamples > 1, true);
  1730. // Note: Making the assumption that the stencil buffer is clear at this point
  1731. rapi.setRenderTarget(resolvedSceneDepthNode->output->renderTexture, FBT_DEPTH, RT_DEPTH_STENCIL);
  1732. stencilMat->execute(inputs.view, gbuffer, settings);
  1733. SPtr<PooledRenderTexture> traceOutput = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width,
  1734. height, TU_RENDERTARGET));
  1735. RENDER_TEXTURE_DESC traceRtDesc;
  1736. traceRtDesc.colorSurfaces[0].texture = traceOutput->texture;
  1737. traceRtDesc.depthStencilSurface.texture = resolvedSceneDepthNode->output->texture;
  1738. SPtr<RenderTexture> traceRt = RenderTexture::create(traceRtDesc);
  1739. rapi.setRenderTarget(traceRt, FBT_DEPTH | FBT_STENCIL, RT_DEPTH_STENCIL);
  1740. rapi.clearRenderTarget(FBT_COLOR, Color::ZERO);
  1741. SSRTraceMat* traceMat = SSRTraceMat::getVariation(settings.quality, viewProps.numSamples > 1, true);
  1742. traceMat->execute(inputs.view, gbuffer, sceneColor, hiZ, settings, traceRt);
  1743. if (resolvedSceneColor)
  1744. {
  1745. resPool.release(resolvedSceneColor);
  1746. resolvedSceneColor = nullptr;
  1747. }
  1748. if (mPrevFrame)
  1749. {
  1750. output = resPool.get(POOLED_RENDER_TEXTURE_DESC::create2D(PF_RGBA16F, width, height, TU_RENDERTARGET));
  1751. rapi.setRenderTarget(output->renderTexture);
  1752. rapi.clearRenderTarget(FBT_COLOR);
  1753. SSRResolveMat* resolveMat = SSRResolveMat::getVariation(viewProps.numSamples > 1);
  1754. resolveMat->execute(inputs.view, mPrevFrame->texture, traceOutput->texture, sceneDepthNode->depthTex->texture,
  1755. output->renderTexture);
  1756. resPool.release(traceOutput);
  1757. }
  1758. else
  1759. output = traceOutput;
  1760. RenderAPI::instance().setRenderTarget(nullptr);
  1761. }
  1762. else
  1763. deallocOutputs();
  1764. }
  1765. void RCNodeSSR::clear()
  1766. {
  1767. GpuResourcePool& resPool = GpuResourcePool::instance();
  1768. if(mPrevFrame)
  1769. resPool.release(mPrevFrame);
  1770. mPrevFrame = output;
  1771. output = nullptr;
  1772. }
  1773. void RCNodeSSR::deallocOutputs()
  1774. {
  1775. GpuResourcePool& resPool = GpuResourcePool::instance();
  1776. if(mPrevFrame)
  1777. {
  1778. resPool.release(mPrevFrame);
  1779. mPrevFrame = nullptr;
  1780. }
  1781. }
  1782. SmallVector<StringID, 4> RCNodeSSR::getDependencies(const RendererView& view)
  1783. {
  1784. SmallVector<StringID, 4> deps;
  1785. if (view.getRenderSettings().screenSpaceReflections.enabled)
  1786. {
  1787. deps.push_back(RCNodeSceneDepth::getNodeId());
  1788. deps.push_back(RCNodeLightAccumulation::getNodeId());
  1789. deps.push_back(RCNodeGBuffer::getNodeId());
  1790. deps.push_back(RCNodeHiZ::getNodeId());
  1791. deps.push_back(RCNodeResolvedSceneDepth::getNodeId());
  1792. bool supportsTiledDeferred = gRenderBeast()->getFeatureSet() != RenderBeastFeatureSet::DesktopMacOS;
  1793. if(supportsTiledDeferred)
  1794. {
  1795. if (view.getProperties().numSamples > 1)
  1796. deps.push_back(RCNodeUnflattenLightAccum::getNodeId());
  1797. }
  1798. }
  1799. return deps;
  1800. }
  1801. }}