IndirectDiffuseClipmaps.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Renderer/IndirectDiffuseClipmaps.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/AccelerationStructureBuilder.h>
  9. #include <AnKi/Renderer/Sky.h>
  10. #include <AnKi/Renderer/ShadowMapping.h>
  11. #include <AnKi/Renderer/HistoryLength.h>
  12. #include <AnKi/Renderer/MotionVectors.h>
  13. #include <AnKi/Scene/Components/SkyboxComponent.h>
  14. #include <AnKi/Shaders/Include/MaterialTypes.h>
  15. #include <AnKi/Util/Tracer.h>
  16. #include <AnKi/GpuMemory/UnifiedGeometryBuffer.h>
  17. namespace anki {
  18. ANKI_SVAR(IdcRays, StatCategory::kRenderer, "IDC ray count", StatFlag::kZeroEveryFrame)
  19. class ProbeRange
  20. {
  21. public:
  22. IVec3 m_begin;
  23. IVec3 m_end;
  24. };
  25. /// Given the clipmap's position of this and the previous frame it splits the clipmap into regions that contain new probes (thus they need a full
  26. /// update) or regions of probes that need a less frequent update.
  27. static void findClipmapInUpdateRanges(Vec3 newClipmapMin, Vec3 oldClipmapMin, Vec3 probeSize, UVec3 probeCountsu,
  28. Array<ProbeRange, 3>& fullUpdateProbeRanges, U32& fullUpdateProbeRangeCount,
  29. ProbeRange& partialUpdateProbeRange)
  30. {
  31. fullUpdateProbeRangeCount = 0;
  32. const IVec3 probeCounts(probeCountsu);
  33. const IVec3 delta = IVec3((newClipmapMin - oldClipmapMin) / probeSize);
  34. const IVec3 absDelta = delta.abs();
  35. if(absDelta.x() >= probeCounts.x() || absDelta.y() >= probeCounts.y() || absDelta.z() >= probeCounts.z())
  36. {
  37. // No overlap between the old and new clipmap positions, full update
  38. fullUpdateProbeRanges[fullUpdateProbeRangeCount++] = {IVec3(0), probeCounts};
  39. partialUpdateProbeRange = {IVec3(0), IVec3(0)};
  40. }
  41. else
  42. {
  43. IVec3 partialUpdateProbeRangeBegin(0);
  44. IVec3 partialUpdateProbeRangeEnd = probeCounts;
  45. IVec3 fullUpdateProbeRangeBegin(0);
  46. IVec3 fullUpdateProbeRangeEnd(0);
  47. if(delta.x() > 0)
  48. {
  49. // New AABB on the right of old
  50. fullUpdateProbeRangeBegin =
  51. IVec3(partialUpdateProbeRangeEnd.x() - delta.x(), partialUpdateProbeRangeBegin.y(), partialUpdateProbeRangeBegin.z());
  52. fullUpdateProbeRangeEnd = partialUpdateProbeRangeEnd;
  53. partialUpdateProbeRangeEnd.x() -= delta.x();
  54. }
  55. else if(delta.x() < 0)
  56. {
  57. // New AABB on the left of old
  58. fullUpdateProbeRangeBegin = partialUpdateProbeRangeBegin;
  59. fullUpdateProbeRangeEnd = IVec3(-delta.x(), partialUpdateProbeRangeEnd.y(), partialUpdateProbeRangeEnd.z());
  60. partialUpdateProbeRangeBegin.x() += -delta.x();
  61. }
  62. if(delta.x() != 0)
  63. {
  64. fullUpdateProbeRanges[fullUpdateProbeRangeCount++] = {fullUpdateProbeRangeBegin, fullUpdateProbeRangeEnd};
  65. }
  66. fullUpdateProbeRangeBegin = fullUpdateProbeRangeEnd = IVec3(0);
  67. if(delta.y() > 0)
  68. {
  69. // New AABB on the top of old
  70. fullUpdateProbeRangeBegin =
  71. IVec3(partialUpdateProbeRangeBegin.x(), partialUpdateProbeRangeEnd.y() - delta.y(), partialUpdateProbeRangeBegin.z());
  72. fullUpdateProbeRangeEnd = partialUpdateProbeRangeEnd;
  73. partialUpdateProbeRangeEnd.y() -= delta.y();
  74. }
  75. else if(delta.y() < 0)
  76. {
  77. // New AABB at the bottom of old
  78. fullUpdateProbeRangeBegin = partialUpdateProbeRangeBegin;
  79. fullUpdateProbeRangeEnd = IVec3(partialUpdateProbeRangeEnd.x(), -delta.y(), partialUpdateProbeRangeEnd.z());
  80. partialUpdateProbeRangeBegin.y() += -delta.y();
  81. }
  82. if(delta.y() != 0)
  83. {
  84. fullUpdateProbeRanges[fullUpdateProbeRangeCount++] = {fullUpdateProbeRangeBegin, fullUpdateProbeRangeEnd};
  85. }
  86. fullUpdateProbeRangeBegin = fullUpdateProbeRangeEnd = IVec3(0);
  87. if(delta.z() > 0)
  88. {
  89. // New AABB on the front of old
  90. fullUpdateProbeRangeBegin =
  91. IVec3(partialUpdateProbeRangeBegin.x(), partialUpdateProbeRangeBegin.y(), partialUpdateProbeRangeEnd.z() - delta.z());
  92. fullUpdateProbeRangeEnd = partialUpdateProbeRangeEnd;
  93. partialUpdateProbeRangeEnd.z() -= delta.z();
  94. }
  95. else if(delta.z() < 0)
  96. {
  97. // New AABB on the back of old
  98. fullUpdateProbeRangeBegin = partialUpdateProbeRangeBegin;
  99. fullUpdateProbeRangeEnd = IVec3(partialUpdateProbeRangeEnd.x(), partialUpdateProbeRangeEnd.y(), -delta.z());
  100. partialUpdateProbeRangeBegin.z() += -delta.z();
  101. }
  102. if(delta.z() != 0)
  103. {
  104. fullUpdateProbeRanges[fullUpdateProbeRangeCount++] = {fullUpdateProbeRangeBegin, fullUpdateProbeRangeEnd};
  105. }
  106. partialUpdateProbeRange = {partialUpdateProbeRangeBegin, partialUpdateProbeRangeEnd};
  107. // Validation
  108. [[maybe_unused]] I32 totalProbeCount = 0;
  109. for(U32 i = 0; i < fullUpdateProbeRangeCount; ++i)
  110. {
  111. const IVec3 end = fullUpdateProbeRanges[i].m_end;
  112. const IVec3 begin = fullUpdateProbeRanges[i].m_begin;
  113. const IVec3 diff = end - begin;
  114. ANKI_ASSERT(diff.x() * diff.y() * diff.z() > 0);
  115. totalProbeCount += diff.x() * diff.y() * diff.z();
  116. }
  117. {
  118. const IVec3 end = partialUpdateProbeRange.m_end;
  119. const IVec3 begin = partialUpdateProbeRange.m_begin;
  120. const IVec3 diff = end - begin;
  121. ANKI_ASSERT(diff.x() * diff.y() * diff.z() > 0);
  122. totalProbeCount += diff.x() * diff.y() * diff.z();
  123. }
  124. ANKI_ASSERT(totalProbeCount == probeCounts.x() * probeCounts.y() * probeCounts.z());
  125. }
  126. }
  127. static void computeClipmapBounds(Vec3 cameraPos, Vec3 lookDir, U32 clipmapIdx, IndirectDiffuseClipmapConstants& consts)
  128. {
  129. const Vec3 offset = lookDir * kIndirectDiffuseClipmapForwardBias * F32(clipmapIdx + 1);
  130. cameraPos += offset;
  131. const Vec3 halfSize = consts.m_sizes[clipmapIdx].xyz() * 0.5;
  132. const Vec3 probeSize = consts.m_sizes[clipmapIdx].xyz() / Vec3(consts.m_probeCounts);
  133. const Vec3 roundedPos = (cameraPos / probeSize).round() * probeSize;
  134. consts.m_aabbMins[clipmapIdx] = (roundedPos - halfSize).xyz0();
  135. [[maybe_unused]] const Vec3 aabbMax = roundedPos + halfSize;
  136. ANKI_ASSERT(aabbMax - consts.m_aabbMins[clipmapIdx].xyz() == consts.m_sizes[clipmapIdx].xyz());
  137. }
  138. Error IndirectDiffuseClipmaps::init()
  139. {
  140. ANKI_CHECK(RtMaterialFetchRendererObject::init());
  141. const Bool firstBounceUsesRt = g_cvarRenderIdcFirstBounceRayDistance > 0.0f;
  142. m_lowRezRtDesc = getRenderer().create2DRenderTargetDescription(getRenderer().getInternalResolution().x() / 2,
  143. getRenderer().getInternalResolution().y() / (!g_cvarRenderIdcApplyHighQuality + 1),
  144. getRenderer().getHdrFormat(), "IndirectDiffuseClipmap: Apply rez");
  145. m_lowRezRtDesc.bake();
  146. m_fullRtDesc = getRenderer().create2DRenderTargetDescription(getRenderer().getInternalResolution().x(), getRenderer().getInternalResolution().y(),
  147. getRenderer().getHdrFormat(), "IndirectDiffuseClipmap: Full");
  148. m_fullRtDesc.bake();
  149. if(firstBounceUsesRt)
  150. {
  151. for(U32 i = 0; i < 2; ++i)
  152. {
  153. const TextureInitInfo init = getRenderer().create2DRenderTargetInitInfo(
  154. getRenderer().getInternalResolution().x(), getRenderer().getInternalResolution().y(), Format::kR16G16B16A16_Sfloat,
  155. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Final #%u", i));
  156. m_irradianceRts[i] = getRenderer().createAndClearRenderTarget(init, TextureUsageBit::kSrvCompute);
  157. }
  158. }
  159. m_consts.m_probeCounts = UVec3(g_cvarRenderIdcProbesXZ, g_cvarRenderIdcProbesY, g_cvarRenderIdcProbesXZ);
  160. m_consts.m_totalProbeCount = m_consts.m_probeCounts.x() * m_consts.m_probeCounts.y() * m_consts.m_probeCounts.z();
  161. m_consts.m_sizes[0] = Vec3(g_cvarRenderIdcClipmap0XZSize, g_cvarRenderIdcClipmap0YSize, g_cvarRenderIdcClipmap0XZSize).xyz0();
  162. m_consts.m_sizes[1] = Vec3(g_cvarRenderIdcClipmap1XZSize, g_cvarRenderIdcClipmap1YSize, g_cvarRenderIdcClipmap1XZSize).xyz0();
  163. m_consts.m_sizes[2] = Vec3(g_cvarRenderIdcClipmap2XZSize, g_cvarRenderIdcClipmap2YSize, g_cvarRenderIdcClipmap2XZSize).xyz0();
  164. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  165. {
  166. TextureInitInfo init = getRenderer().create2DRenderTargetInitInfo(m_consts.m_probeCounts.x(), m_consts.m_probeCounts.z(), Format::kR8_Unorm,
  167. TextureUsageBit::kUavCompute | TextureUsageBit::kAllSrv,
  168. generateTempPassName("IndirectDiffuseClipmap: Probe validity #%u", i));
  169. init.m_depth = m_consts.m_probeCounts.y();
  170. init.m_type = TextureType::k3D;
  171. m_probeValidityVolumes[i] = getRenderer().createAndClearRenderTarget(init, TextureUsageBit::kSrvCompute);
  172. }
  173. // Create the RT result texture
  174. const U32 texelsPerProbe = square<U32>(g_cvarRenderIdcRadianceOctMapSize);
  175. m_rtResultRtDesc =
  176. getRenderer().create2DRenderTargetDescription(m_consts.m_totalProbeCount, texelsPerProbe * g_cvarRenderIdcRayCountPerTexelOfNewProbe,
  177. Format::kR16G16B16A16_Sfloat, "IndirectDiffuseClipmap: RT result");
  178. m_rtResultRtDesc.bake();
  179. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  180. {
  181. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  182. m_consts.m_probeCounts.x() * (g_cvarRenderIdcRadianceOctMapSize + 2),
  183. m_consts.m_probeCounts.z() * (g_cvarRenderIdcRadianceOctMapSize + 2), Format::kB10G11R11_Ufloat_Pack32,
  184. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Radiance #%u", clipmap));
  185. volumeInit.m_depth = m_consts.m_probeCounts.y();
  186. volumeInit.m_type = TextureType::k3D;
  187. m_radianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  188. }
  189. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  190. {
  191. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  192. m_consts.m_probeCounts.x() * (g_cvarRenderIdcIrradianceOctMapSize + 2),
  193. m_consts.m_probeCounts.z() * (g_cvarRenderIdcIrradianceOctMapSize + 2), Format::kB10G11R11_Ufloat_Pack32,
  194. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Irradiance #%u", clipmap));
  195. volumeInit.m_depth = m_consts.m_probeCounts.y();
  196. volumeInit.m_type = TextureType::k3D;
  197. m_irradianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  198. }
  199. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  200. {
  201. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  202. m_consts.m_probeCounts.x() * (g_cvarRenderIdcRadianceOctMapSize + 2),
  203. m_consts.m_probeCounts.z() * (g_cvarRenderIdcRadianceOctMapSize + 2), Format::kR16G16_Sfloat, TextureUsageBit::kAllShaderResource,
  204. generateTempPassName("IndirectDiffuseClipmap: Dist moments #%u", clipmap));
  205. volumeInit.m_depth = m_consts.m_probeCounts.y();
  206. volumeInit.m_type = TextureType::k3D;
  207. m_distanceMomentsVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  208. }
  209. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  210. {
  211. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  212. m_consts.m_probeCounts.x(), m_consts.m_probeCounts.z(), Format::kB10G11R11_Ufloat_Pack32, TextureUsageBit::kAllShaderResource,
  213. generateTempPassName("IndirectDiffuseClipmap: Avg light #%u", clipmap));
  214. volumeInit.m_depth = m_consts.m_probeCounts.y();
  215. volumeInit.m_type = TextureType::k3D;
  216. m_avgIrradianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  217. }
  218. const Array<SubMutation, 5> mutation = {{{"GPU_WAVE_SIZE", MutatorValue(GrManager::getSingleton().getDeviceCapabilities().m_maxWaveSize)},
  219. {"RADIANCE_OCTAHEDRON_MAP_SIZE", MutatorValue(g_cvarRenderIdcRadianceOctMapSize)},
  220. {"IRRADIANCE_OCTAHEDRON_MAP_SIZE", MutatorValue(g_cvarRenderIdcIrradianceOctMapSize)},
  221. {"RT_MATERIAL_FETCH_CLIPMAP", 0},
  222. {"SPATIAL_RECONSTRUCT_TYPE", !g_cvarRenderIdcApplyHighQuality}}};
  223. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_applyGiGrProg, "Apply"));
  224. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_visProbesGrProg, "VisualizeProbes"));
  225. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_populateCachesGrProg, "PopulateCaches"));
  226. ANKI_CHECK(
  227. loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_computeIrradianceGrProg, "ComputeIrradiance"));
  228. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_temporalDenoiseGrProg, "TemporalDenoise"));
  229. ANKI_CHECK(
  230. loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_spatialReconstructGrProg, "SpatialReconstruct"));
  231. ANKI_CHECK(
  232. loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_bilateralDenoiseGrProg, "BilateralDenoise"));
  233. for(MutatorValue rtMaterialFetchClipmap = 0; rtMaterialFetchClipmap < 2; ++rtMaterialFetchClipmap)
  234. {
  235. ShaderProgramResourcePtr tmpProg;
  236. ANKI_CHECK(ResourceManager::getSingleton().loadResource("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", tmpProg));
  237. ANKI_ASSERT(tmpProg == m_prog);
  238. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  239. variantInitInfo.requestTechniqueAndTypes(ShaderTypeBit::kRayGen, "RtMaterialFetch");
  240. for(const SubMutation& s : mutation)
  241. {
  242. variantInitInfo.addMutation(s.m_mutatorName, s.m_value);
  243. }
  244. variantInitInfo.addMutation("RT_MATERIAL_FETCH_CLIPMAP", rtMaterialFetchClipmap);
  245. const ShaderProgramResourceVariant* variant;
  246. m_prog->getOrCreateVariant(variantInitInfo, variant);
  247. m_rtLibraryGrProg.reset(&variant->getProgram());
  248. m_rayGenShaderGroupIndices[rtMaterialFetchClipmap] = variant->getShaderGroupHandleIndex();
  249. }
  250. {
  251. ANKI_CHECK(ResourceManager::getSingleton().loadResource("ShaderBinaries/RtMaterialFetchMiss.ankiprogbin", m_missProg));
  252. ShaderProgramResourceVariantInitInfo variantInitInfo(m_missProg);
  253. variantInitInfo.requestTechniqueAndTypes(ShaderTypeBit::kMiss, "RtMaterialFetch");
  254. const ShaderProgramResourceVariant* variant;
  255. m_missProg->getOrCreateVariant(variantInitInfo, variant);
  256. m_missShaderGroupIdx = variant->getShaderGroupHandleIndex();
  257. }
  258. m_sbtRecordSize = getAlignedRoundUp(GrManager::getSingleton().getDeviceCapabilities().m_sbtRecordAlignment,
  259. GrManager::getSingleton().getDeviceCapabilities().m_shaderGroupHandleSize + U32(sizeof(UVec4)));
  260. ANKI_CHECK(ResourceManager::getSingleton().loadResource("EngineAssets/BlueNoise_Rgba8_64x64.png", m_blueNoiseImg));
  261. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  262. {
  263. m_consts.m_textures[i].m_radianceTexture = m_radianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  264. m_consts.m_textures[i].m_irradianceTexture = m_irradianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  265. m_consts.m_textures[i].m_distanceMomentsTexture = m_distanceMomentsVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  266. m_consts.m_textures[i].m_probeValidityTexture = m_probeValidityVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  267. m_consts.m_textures[i].m_averageIrradianceTexture = m_avgIrradianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  268. m_consts.m_textures[i].m_distanceMomentsOctMapSize = (m_distanceMomentsVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  269. m_consts.m_textures[i].m_irradianceOctMapSize = (m_irradianceVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  270. m_consts.m_textures[i].m_radianceOctMapSize = (m_radianceVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  271. m_consts.m_previousFrameAabbMins[i] = 100000.0f * m_consts.m_sizes[i] / Vec4(Vec3(m_consts.m_probeCounts), 1.0f);
  272. m_consts.m_aabbMins[i] = m_consts.m_previousFrameAabbMins[i];
  273. }
  274. return Error::kNone;
  275. }
  276. void IndirectDiffuseClipmaps::populateRenderGraph(RenderingContext& ctx)
  277. {
  278. ANKI_TRACE_SCOPED_EVENT(IndirectDiffuse);
  279. const Bool firstBounceUsesRt = g_cvarRenderIdcFirstBounceRayDistance > 0.0f;
  280. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  281. {
  282. m_consts.m_previousFrameAabbMins[i] = m_consts.m_aabbMins[i];
  283. computeClipmapBounds(ctx.m_matrices.m_cameraTransform.getTranslationPart(),
  284. -ctx.m_matrices.m_cameraTransform.getRotationPart().getZAxis().normalize(), i, m_consts);
  285. }
  286. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  287. const RenderTargetHandle rtResultHandle = rgraph.newRenderTarget(m_rtResultRtDesc);
  288. const RenderTargetHandle lowRezRt = rgraph.newRenderTarget(m_lowRezRtDesc);
  289. const RenderTargetHandle fullRtTmp = rgraph.newRenderTarget(m_fullRtDesc);
  290. Array<RenderTargetHandle, 2> fullRts;
  291. if(firstBounceUsesRt)
  292. {
  293. for(U32 i = 0; i < 2; ++i)
  294. {
  295. if(m_texturesImportedOnce) [[likely]]
  296. {
  297. fullRts[i] = rgraph.importRenderTarget(m_irradianceRts[i].get());
  298. }
  299. else
  300. {
  301. fullRts[i] = rgraph.importRenderTarget(m_irradianceRts[i].get(), TextureUsageBit::kSrvCompute);
  302. }
  303. }
  304. }
  305. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& radianceVolumes = m_runCtx.m_handles.m_radianceVolumes;
  306. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& irradianceVolumes = m_runCtx.m_handles.m_irradianceVolumes;
  307. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& distanceMomentsVolumes = m_runCtx.m_handles.m_distanceMomentsVolumes;
  308. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& probeValidityVolumes = m_runCtx.m_handles.m_probeValidityVolumes;
  309. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& avgIrradianceVolumes = m_runCtx.m_handles.m_avgIrradianceVolumes;
  310. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  311. {
  312. if(m_texturesImportedOnce) [[likely]]
  313. {
  314. radianceVolumes[i] = rgraph.importRenderTarget(m_radianceVolumes[i].get());
  315. irradianceVolumes[i] = rgraph.importRenderTarget(m_irradianceVolumes[i].get());
  316. distanceMomentsVolumes[i] = rgraph.importRenderTarget(m_distanceMomentsVolumes[i].get());
  317. probeValidityVolumes[i] = rgraph.importRenderTarget(m_probeValidityVolumes[i].get());
  318. avgIrradianceVolumes[i] = rgraph.importRenderTarget(m_avgIrradianceVolumes[i].get());
  319. }
  320. else
  321. {
  322. radianceVolumes[i] = rgraph.importRenderTarget(m_radianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  323. irradianceVolumes[i] = rgraph.importRenderTarget(m_irradianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  324. distanceMomentsVolumes[i] = rgraph.importRenderTarget(m_distanceMomentsVolumes[i].get(), TextureUsageBit::kSrvCompute);
  325. probeValidityVolumes[i] = rgraph.importRenderTarget(m_probeValidityVolumes[i].get(), TextureUsageBit::kSrvCompute);
  326. avgIrradianceVolumes[i] = rgraph.importRenderTarget(m_avgIrradianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  327. }
  328. }
  329. m_texturesImportedOnce = true;
  330. // SBT build
  331. BufferHandle sbtHandle;
  332. BufferView sbtBuffer;
  333. buildShaderBindingTablePass("IndirectDiffuseClipmaps: Build SBT", m_rtLibraryGrProg.get(), m_rayGenShaderGroupIndices[1], m_missShaderGroupIdx,
  334. m_sbtRecordSize, rgraph, sbtHandle, sbtBuffer);
  335. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  336. {
  337. // Compute probe ranges and ray budgets and stuff
  338. Array<ProbeRange, 3> fullUpdateRanges;
  339. U32 fullUpdateRangeCount = 0;
  340. ProbeRange partialUpdateRange;
  341. const Vec3 probeSize = m_consts.m_sizes[clipmap].xyz() / Vec3(m_consts.m_probeCounts);
  342. findClipmapInUpdateRanges(m_consts.m_aabbMins[clipmap].xyz(), m_consts.m_previousFrameAabbMins[clipmap].xyz(), probeSize,
  343. UVec3(m_consts.m_probeCounts), fullUpdateRanges, fullUpdateRangeCount, partialUpdateRange);
  344. U32 fullUpdateRayCount = 0;
  345. for(U32 i = 0; i < fullUpdateRangeCount; ++i)
  346. {
  347. const UVec3 counts = UVec3(fullUpdateRanges[i].m_end - fullUpdateRanges[i].m_begin);
  348. const U32 count = counts.x() * counts.y() * counts.z();
  349. fullUpdateRayCount += square<U32>(g_cvarRenderIdcRadianceOctMapSize) * g_cvarRenderIdcRayCountPerTexelOfNewProbe * count;
  350. }
  351. const U32 remainingRayCount = (g_cvarRenderIdcProbeRayBudget / kIndirectDiffuseClipmapCount > fullUpdateRayCount)
  352. ? g_cvarRenderIdcProbeRayBudget / kIndirectDiffuseClipmapCount - fullUpdateRayCount
  353. : 0;
  354. const UVec3 partialUpdateProbeCounts = UVec3(partialUpdateRange.m_end - partialUpdateRange.m_begin);
  355. U32 partialUpdateProbeCount = remainingRayCount / square<U32>(g_cvarRenderIdcRadianceOctMapSize);
  356. partialUpdateProbeCount =
  357. min(partialUpdateProbeCount, partialUpdateProbeCounts.x() * partialUpdateProbeCounts.y() * partialUpdateProbeCounts.z());
  358. g_svarIdcRays.increment(fullUpdateRayCount + partialUpdateProbeCount * square<U32>(g_cvarRenderIdcRadianceOctMapSize));
  359. struct ClipmapRegion
  360. {
  361. UVec3 m_probesBegin;
  362. U32 m_partialUpdate;
  363. UVec3 m_probeCounts;
  364. U32 m_probeCount;
  365. };
  366. struct ProbeUpdateConsts
  367. {
  368. U32 m_clipmapIdx;
  369. U32 m_radianceOctMapSize; // Have it here as well as well as a mutator. Can't use the mutator cause it will create may raygen variants
  370. U32 m_rayCountPerTexel; // Ray count per oct map texel
  371. U32 m_maxProbesToUpdate;
  372. ClipmapRegion m_clipmapRegion;
  373. };
  374. // Do ray tracing around the probes
  375. {
  376. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass(generateTempPassName("IndirectDiffuseClipmaps: RT (clipmap %u)", clipmap));
  377. pass.newTextureDependency(rtResultHandle, TextureUsageBit::kUavCompute);
  378. pass.newBufferDependency(sbtHandle, BufferUsageBit::kShaderBindingTable);
  379. setRgenSpace2Dependencies(pass);
  380. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  381. {
  382. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kSrvCompute);
  383. }
  384. pass.setWork([this, rtResultHandle, &ctx, sbtBuffer, fullUpdateRangeCount, clipmap, fullUpdateRanges, partialUpdateRange,
  385. partialUpdateProbeCount](RenderPassWorkContext& rgraphCtx) {
  386. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  387. cmdb.bindShaderProgram(m_rtLibraryGrProg.get());
  388. // More globals
  389. cmdb.bindSampler(ANKI_MATERIAL_REGISTER_TILINEAR_REPEAT_SAMPLER, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  390. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_GPU_SCENE, 0, GpuSceneBuffer::getSingleton().getBufferView());
  391. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_MESH_LODS, 0, GpuSceneArrays::MeshLod::getSingleton().getBufferView());
  392. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_TRANSFORMS, 0, GpuSceneArrays::Transform::getSingleton().getBufferView());
  393. #define ANKI_UNIFIED_GEOM_FORMAT(fmt, shaderType, reg) \
  394. cmdb.bindSrv( \
  395. reg, 0, \
  396. BufferView(&UnifiedGeometryBuffer::getSingleton().getBuffer(), 0, \
  397. getAlignedRoundDown(getFormatInfo(Format::k##fmt).m_texelSize, UnifiedGeometryBuffer::getSingleton().getBuffer().getSize())), \
  398. Format::k##fmt);
  399. #include <AnKi/Shaders/Include/UnifiedGeometryTypes.def.h>
  400. bindRgenSpace2Resources(ctx, rgraphCtx);
  401. rgraphCtx.bindUav(0, 2, rtResultHandle);
  402. ProbeUpdateConsts consts;
  403. consts.m_clipmapIdx = clipmap;
  404. consts.m_radianceOctMapSize = g_cvarRenderIdcRadianceOctMapSize;
  405. // Do full updates
  406. for(U32 i = 0; i < fullUpdateRangeCount; ++i)
  407. {
  408. cmdb.pushDebugMarker("Full update", Vec3(0.0f, 1.0f, 1.0f));
  409. consts.m_rayCountPerTexel = g_cvarRenderIdcRayCountPerTexelOfNewProbe;
  410. consts.m_clipmapRegion.m_probesBegin = UVec3(fullUpdateRanges[i].m_begin);
  411. consts.m_clipmapRegion.m_probeCounts = UVec3(fullUpdateRanges[i].m_end - fullUpdateRanges[i].m_begin);
  412. consts.m_clipmapRegion.m_probeCount = consts.m_clipmapRegion.m_probeCounts.x() * consts.m_clipmapRegion.m_probeCounts.y()
  413. * consts.m_clipmapRegion.m_probeCounts.z();
  414. consts.m_maxProbesToUpdate = consts.m_clipmapRegion.m_probeCount;
  415. consts.m_clipmapRegion.m_partialUpdate = 0;
  416. cmdb.setFastConstants(&consts, sizeof(consts));
  417. const U32 threadCount =
  418. consts.m_clipmapRegion.m_probeCount * square<U32>(g_cvarRenderIdcRadianceOctMapSize) * consts.m_rayCountPerTexel;
  419. cmdb.dispatchRays(sbtBuffer, m_sbtRecordSize, GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount(), 1,
  420. threadCount, 1, 1);
  421. cmdb.popDebugMarker();
  422. }
  423. // Do partial updates
  424. if(partialUpdateProbeCount)
  425. {
  426. cmdb.pushDebugMarker("Partial update", Vec3(0.0f, 1.0f, 1.0f));
  427. consts.m_rayCountPerTexel = 1;
  428. consts.m_clipmapRegion.m_probesBegin = UVec3(partialUpdateRange.m_begin);
  429. consts.m_clipmapRegion.m_probeCounts = UVec3(partialUpdateRange.m_end - partialUpdateRange.m_begin);
  430. consts.m_clipmapRegion.m_probeCount = consts.m_clipmapRegion.m_probeCounts.x() * consts.m_clipmapRegion.m_probeCounts.y()
  431. * consts.m_clipmapRegion.m_probeCounts.z();
  432. consts.m_maxProbesToUpdate = partialUpdateProbeCount;
  433. consts.m_clipmapRegion.m_partialUpdate = 1;
  434. cmdb.setFastConstants(&consts, sizeof(consts));
  435. const U32 threadCount = partialUpdateProbeCount * square<U32>(g_cvarRenderIdcRadianceOctMapSize);
  436. cmdb.dispatchRays(sbtBuffer, m_sbtRecordSize, GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount(), 1,
  437. threadCount, 1, 1);
  438. cmdb.popDebugMarker();
  439. }
  440. });
  441. }
  442. // Populate caches
  443. {
  444. NonGraphicsRenderPass& pass =
  445. rgraph.newNonGraphicsRenderPass(generateTempPassName("IndirectDiffuseClipmaps: Populate caches (clipmap %u)", clipmap));
  446. pass.newTextureDependency(rtResultHandle, TextureUsageBit::kSrvCompute);
  447. pass.newTextureDependency(radianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  448. pass.newTextureDependency(probeValidityVolumes[clipmap], TextureUsageBit::kUavCompute);
  449. pass.newTextureDependency(distanceMomentsVolumes[clipmap], TextureUsageBit::kUavCompute);
  450. pass.setWork([this, &ctx, rtResultHandle, radianceVolumes, probeValidityVolumes, distanceMomentsVolumes, clipmap, fullUpdateRanges,
  451. partialUpdateRange, partialUpdateProbeCount, fullUpdateRangeCount](RenderPassWorkContext& rgraphCtx) {
  452. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  453. cmdb.bindShaderProgram(m_populateCachesGrProg.get());
  454. rgraphCtx.bindSrv(0, 0, rtResultHandle);
  455. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  456. rgraphCtx.bindUav(0, 0, radianceVolumes[clipmap]);
  457. rgraphCtx.bindUav(1, 0, distanceMomentsVolumes[clipmap]);
  458. rgraphCtx.bindUav(2, 0, probeValidityVolumes[clipmap]);
  459. ProbeUpdateConsts consts;
  460. consts.m_clipmapIdx = clipmap;
  461. consts.m_radianceOctMapSize = g_cvarRenderIdcRadianceOctMapSize;
  462. // Do full updates
  463. const U32 numthreads = 64;
  464. for(U32 i = 0; i < fullUpdateRangeCount; ++i)
  465. {
  466. cmdb.pushDebugMarker("Full update", Vec3(0.0f, 1.0f, 1.0f));
  467. consts.m_rayCountPerTexel = g_cvarRenderIdcRayCountPerTexelOfNewProbe;
  468. consts.m_clipmapRegion.m_probesBegin = UVec3(fullUpdateRanges[i].m_begin);
  469. consts.m_clipmapRegion.m_probeCounts = UVec3(fullUpdateRanges[i].m_end - fullUpdateRanges[i].m_begin);
  470. consts.m_clipmapRegion.m_probeCount = consts.m_clipmapRegion.m_probeCounts.x() * consts.m_clipmapRegion.m_probeCounts.y()
  471. * consts.m_clipmapRegion.m_probeCounts.z();
  472. consts.m_maxProbesToUpdate = consts.m_clipmapRegion.m_probeCount;
  473. consts.m_clipmapRegion.m_partialUpdate = 0;
  474. cmdb.setFastConstants(&consts, sizeof(consts));
  475. U32 threadCount = consts.m_clipmapRegion.m_probeCount * square<U32>(g_cvarRenderIdcRadianceOctMapSize);
  476. threadCount = (threadCount + numthreads - 1) / numthreads;
  477. cmdb.dispatchCompute(threadCount, 1, 1);
  478. cmdb.popDebugMarker();
  479. }
  480. // Do partial updates
  481. if(partialUpdateProbeCount)
  482. {
  483. cmdb.pushDebugMarker("Partial update", Vec3(0.0f, 1.0f, 1.0f));
  484. consts.m_rayCountPerTexel = 1;
  485. consts.m_clipmapRegion.m_probesBegin = UVec3(partialUpdateRange.m_begin);
  486. consts.m_clipmapRegion.m_probeCounts = UVec3(partialUpdateRange.m_end - partialUpdateRange.m_begin);
  487. consts.m_clipmapRegion.m_probeCount = consts.m_clipmapRegion.m_probeCounts.x() * consts.m_clipmapRegion.m_probeCounts.y()
  488. * consts.m_clipmapRegion.m_probeCounts.z();
  489. consts.m_maxProbesToUpdate = partialUpdateProbeCount;
  490. consts.m_clipmapRegion.m_partialUpdate = 1;
  491. cmdb.setFastConstants(&consts, sizeof(consts));
  492. U32 threadCount = consts.m_maxProbesToUpdate * square<U32>(g_cvarRenderIdcRadianceOctMapSize);
  493. threadCount = (threadCount + numthreads - 1) / numthreads;
  494. cmdb.dispatchCompute(threadCount, 1, 1);
  495. cmdb.popDebugMarker();
  496. }
  497. });
  498. }
  499. }
  500. // Compute irradiance
  501. {
  502. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Irradiance");
  503. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  504. {
  505. pass.newTextureDependency(radianceVolumes[clipmap], TextureUsageBit::kSrvCompute);
  506. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  507. pass.newTextureDependency(avgIrradianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  508. }
  509. pass.setWork([this, &ctx, radianceVolumes, irradianceVolumes, avgIrradianceVolumes](RenderPassWorkContext& rgraphCtx) {
  510. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  511. cmdb.bindShaderProgram(m_computeIrradianceGrProg.get());
  512. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  513. U32 uav = 0;
  514. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  515. {
  516. rgraphCtx.bindSrv(clipmap, 0, radianceVolumes[clipmap]);
  517. rgraphCtx.bindUav(uav++, 0, irradianceVolumes[clipmap]);
  518. }
  519. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  520. {
  521. rgraphCtx.bindUav(uav++, 0, avgIrradianceVolumes[clipmap]);
  522. }
  523. cmdb.dispatchCompute(m_consts.m_probeCounts[0] * kIndirectDiffuseClipmapCount, m_consts.m_probeCounts[1], m_consts.m_probeCounts[2]);
  524. });
  525. }
  526. // Apply GI
  527. if(firstBounceUsesRt)
  528. {
  529. patchShaderBindingTablePass("IndirectDiffuseClipmaps: Patch SBT", m_rtLibraryGrProg.get(), m_rayGenShaderGroupIndices[0],
  530. m_missShaderGroupIdx, m_sbtRecordSize, rgraph, sbtHandle, sbtBuffer);
  531. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: RTApply");
  532. pass.newBufferDependency(sbtHandle, BufferUsageBit::kShaderBindingTable);
  533. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  534. {
  535. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kSrvDispatchRays);
  536. pass.newTextureDependency(probeValidityVolumes[clipmap], TextureUsageBit::kSrvDispatchRays);
  537. pass.newTextureDependency(distanceMomentsVolumes[clipmap], TextureUsageBit::kSrvDispatchRays);
  538. }
  539. pass.newTextureDependency(lowRezRt, TextureUsageBit::kUavDispatchRays);
  540. setRgenSpace2Dependencies(pass);
  541. pass.setWork([this, &ctx, sbtBuffer, lowRezRt](RenderPassWorkContext& rgraphCtx) {
  542. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  543. cmdb.bindShaderProgram(m_rtLibraryGrProg.get());
  544. // More globals
  545. cmdb.bindSampler(ANKI_MATERIAL_REGISTER_TILINEAR_REPEAT_SAMPLER, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  546. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_GPU_SCENE, 0, GpuSceneBuffer::getSingleton().getBufferView());
  547. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_MESH_LODS, 0, GpuSceneArrays::MeshLod::getSingleton().getBufferView());
  548. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_TRANSFORMS, 0, GpuSceneArrays::Transform::getSingleton().getBufferView());
  549. #define ANKI_UNIFIED_GEOM_FORMAT(fmt, shaderType, reg) \
  550. cmdb.bindSrv( \
  551. reg, 0, \
  552. BufferView(&UnifiedGeometryBuffer::getSingleton().getBuffer(), 0, \
  553. getAlignedRoundDown(getFormatInfo(Format::k##fmt).m_texelSize, UnifiedGeometryBuffer::getSingleton().getBuffer().getSize())), \
  554. Format::k##fmt);
  555. #include <AnKi/Shaders/Include/UnifiedGeometryTypes.def.h>
  556. bindRgenSpace2Resources(ctx, rgraphCtx);
  557. rgraphCtx.bindUav(0, 2, lowRezRt);
  558. const Array<Vec4, 3> consts = {Vec4(g_cvarRenderIdcFirstBounceRayDistance), {}, {}};
  559. cmdb.setFastConstants(&consts, sizeof(consts));
  560. const U32 width = getRenderer().getInternalResolution().x() / 2;
  561. const U32 height = getRenderer().getInternalResolution().y() / (!g_cvarRenderIdcApplyHighQuality + 1);
  562. cmdb.dispatchRays(sbtBuffer, m_sbtRecordSize, GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount(), 1, width,
  563. height, 1);
  564. g_svarIdcRays.increment(width * height);
  565. });
  566. }
  567. else
  568. {
  569. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Apply irradiance");
  570. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvCompute);
  571. pass.newTextureDependency(getGBuffer().getColorRt(2), TextureUsageBit::kSrvCompute);
  572. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  573. {
  574. pass.newTextureDependency(irradianceVolumes[i], TextureUsageBit::kSrvCompute);
  575. pass.newTextureDependency(probeValidityVolumes[i], TextureUsageBit::kSrvCompute);
  576. pass.newTextureDependency(distanceMomentsVolumes[i], TextureUsageBit::kSrvCompute);
  577. pass.newTextureDependency(avgIrradianceVolumes[i], TextureUsageBit::kSrvCompute);
  578. }
  579. pass.newTextureDependency(lowRezRt, TextureUsageBit::kUavCompute);
  580. pass.setWork([this, &ctx, lowRezRt](RenderPassWorkContext& rgraphCtx) {
  581. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  582. cmdb.bindShaderProgram(m_applyGiGrProg.get());
  583. rgraphCtx.bindSrv(0, 0, getGBuffer().getDepthRt());
  584. rgraphCtx.bindSrv(1, 0, getGBuffer().getColorRt(2));
  585. cmdb.bindSrv(2, 0, TextureView(&m_blueNoiseImg->getTexture(), TextureSubresourceDesc::firstSurface()));
  586. rgraphCtx.bindUav(0, 0, lowRezRt);
  587. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  588. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  589. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x() / 2,
  590. getRenderer().getInternalResolution().y() / (!g_cvarRenderIdcApplyHighQuality + 1));
  591. });
  592. }
  593. // Spatial reconstruct
  594. {
  595. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Spatial reconstruct");
  596. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvCompute);
  597. pass.newTextureDependency(lowRezRt, TextureUsageBit::kSrvCompute);
  598. pass.newTextureDependency(fullRtTmp, TextureUsageBit::kUavCompute);
  599. pass.setWork([this, lowRezRt, fullRtTmp](RenderPassWorkContext& rgraphCtx) {
  600. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  601. cmdb.bindShaderProgram(m_spatialReconstructGrProg.get());
  602. rgraphCtx.bindSrv(0, 0, lowRezRt);
  603. rgraphCtx.bindSrv(1, 0, getGBuffer().getDepthRt());
  604. rgraphCtx.bindUav(0, 0, fullRtTmp);
  605. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x() / 2,
  606. getRenderer().getInternalResolution().y() / (!g_cvarRenderIdcApplyHighQuality + 1));
  607. });
  608. }
  609. if(!firstBounceUsesRt)
  610. {
  611. m_runCtx.m_handles.m_appliedIrradiance = fullRtTmp;
  612. return;
  613. }
  614. const RenderTargetHandle historyRt = fullRts[0];
  615. const RenderTargetHandle outRt = fullRts[1];
  616. // Temporal denoise
  617. {
  618. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Temporal denoise");
  619. pass.newTextureDependency(fullRtTmp, TextureUsageBit::kSrvCompute);
  620. pass.newTextureDependency(historyRt, TextureUsageBit::kSrvCompute);
  621. pass.newTextureDependency(getHistoryLength().getRt(), TextureUsageBit::kSrvCompute);
  622. pass.newTextureDependency(getMotionVectors().getMotionVectorsRt(), TextureUsageBit::kSrvCompute);
  623. pass.newTextureDependency(outRt, TextureUsageBit::kUavCompute);
  624. pass.setWork([this, &ctx, fullRtTmp, historyRt, outRt](RenderPassWorkContext& rgraphCtx) {
  625. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  626. cmdb.bindShaderProgram(m_temporalDenoiseGrProg.get());
  627. rgraphCtx.bindSrv(0, 0, getHistoryLength().getRt());
  628. rgraphCtx.bindSrv(1, 0, getMotionVectors().getMotionVectorsRt());
  629. rgraphCtx.bindSrv(2, 0, historyRt);
  630. rgraphCtx.bindSrv(3, 0, fullRtTmp);
  631. rgraphCtx.bindUav(0, 0, outRt);
  632. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearClamp.get());
  633. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  634. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x(), getRenderer().getInternalResolution().y());
  635. });
  636. }
  637. // Bilateral denoise
  638. {
  639. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Bilateral denoise");
  640. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvCompute);
  641. pass.newTextureDependency(outRt, TextureUsageBit::kSrvCompute);
  642. pass.newTextureDependency(historyRt, TextureUsageBit::kUavCompute);
  643. pass.setWork([this, outRt, historyRt](RenderPassWorkContext& rgraphCtx) {
  644. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  645. cmdb.bindShaderProgram(m_bilateralDenoiseGrProg.get());
  646. rgraphCtx.bindSrv(0, 0, outRt);
  647. rgraphCtx.bindSrv(1, 0, getGBuffer().getDepthRt());
  648. rgraphCtx.bindUav(0, 0, historyRt);
  649. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x(), getRenderer().getInternalResolution().y());
  650. });
  651. }
  652. m_runCtx.m_handles.m_appliedIrradiance = historyRt;
  653. }
  654. void IndirectDiffuseClipmaps::drawDebugProbes(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx) const
  655. {
  656. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  657. const U32 clipmap = 0;
  658. cmdb.bindShaderProgram(m_visProbesGrProg.get());
  659. const UVec4 consts(clipmap);
  660. cmdb.setFastConstants(&consts, sizeof(consts));
  661. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  662. const RenderTargetHandle visVolume = m_runCtx.m_handles.m_avgIrradianceVolumes[clipmap];
  663. rgraphCtx.bindSrv(0, 0, visVolume);
  664. rgraphCtx.bindSrv(1, 0, m_runCtx.m_handles.m_probeValidityVolumes[clipmap]);
  665. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  666. cmdb.draw(PrimitiveTopology::kTriangles, 36, m_consts.m_totalProbeCount);
  667. }
  668. } // end namespace anki