BsRenderCompositor.cpp 76 KB

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