BsRenderCompositor.cpp 73 KB

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