2
0

BsRenderCompositor.cpp 63 KB

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