IndirectDiffuseClipmaps.cpp 31 KB

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