BsRenderCompositor.cpp 65 KB

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