IndirectDiffuseClipmaps.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/Scene/Components/SkyboxComponent.h>
  12. #include <AnKi/Shaders/Include/MaterialTypes.h>
  13. #include <AnKi/Util/Tracer.h>
  14. #include <AnKi/GpuMemory/UnifiedGeometryBuffer.h>
  15. namespace anki {
  16. static void computeClipmapBounds(Vec3 cameraPos, Vec3 lookDir, U32 clipmapIdx, IndirectDiffuseClipmapConstants& consts)
  17. {
  18. const Vec3 offset = lookDir * kIndirectDiffuseClipmapForwardBias * F32(clipmapIdx + 1);
  19. cameraPos += offset;
  20. const Vec3 halfSize = consts.m_sizes[clipmapIdx].xyz() * 0.5;
  21. const Vec3 probeSize = consts.m_sizes[clipmapIdx].xyz() / Vec3(consts.m_probeCounts);
  22. const Vec3 roundedPos = (cameraPos / probeSize).round() * probeSize;
  23. consts.m_aabbMins[clipmapIdx] = (roundedPos - halfSize).xyz0();
  24. [[maybe_unused]] const Vec3 aabbMax = roundedPos + halfSize;
  25. ANKI_ASSERT(aabbMax - consts.m_aabbMins[clipmapIdx].xyz() == consts.m_sizes[clipmapIdx].xyz());
  26. }
  27. Error IndirectDiffuseClipmaps::init()
  28. {
  29. ANKI_CHECK(RtMaterialFetchRendererObject::init());
  30. m_lowRezRtDesc = getRenderer().create2DRenderTargetDescription(
  31. getRenderer().getInternalResolution().x() / 2, getRenderer().getInternalResolution().y() / (!g_indirectDiffuseClipmapApplyHighQuality + 1),
  32. getRenderer().getHdrFormat(), "IndirectDiffuseClipmap: Half");
  33. m_lowRezRtDesc.bake();
  34. m_fullRtDesc = getRenderer().create2DRenderTargetDescription(getRenderer().getInternalResolution().x(), getRenderer().getInternalResolution().y(),
  35. getRenderer().getHdrFormat(), "IndirectDiffuseClipmap: Full");
  36. m_fullRtDesc.bake();
  37. m_consts.m_probeCounts = UVec3(g_indirectDiffuseClipmapProbesXZCVar, g_indirectDiffuseClipmapProbesYCVar, g_indirectDiffuseClipmapProbesXZCVar);
  38. m_consts.m_totalProbeCount = m_consts.m_probeCounts.x() * m_consts.m_probeCounts.y() * m_consts.m_probeCounts.z();
  39. m_consts.m_sizes[0] = Vec3(g_indirectDiffuseClipmap0XZSizeCVar, g_indirectDiffuseClipmap0YSizeCVar, g_indirectDiffuseClipmap0XZSizeCVar).xyz0();
  40. m_consts.m_sizes[1] = Vec3(g_indirectDiffuseClipmap1XZSizeCVar, g_indirectDiffuseClipmap1YSizeCVar, g_indirectDiffuseClipmap1XZSizeCVar).xyz0();
  41. m_consts.m_sizes[2] = Vec3(g_indirectDiffuseClipmap2XZSizeCVar, g_indirectDiffuseClipmap2YSizeCVar, g_indirectDiffuseClipmap2XZSizeCVar).xyz0();
  42. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  43. {
  44. TextureInitInfo init = getRenderer().create2DRenderTargetInitInfo(m_consts.m_probeCounts.x(), m_consts.m_probeCounts.z(), Format::kR8_Unorm,
  45. TextureUsageBit::kUavCompute | TextureUsageBit::kAllSrv,
  46. generateTempPassName("IndirectDiffuseClipmap: Probe validity #%u", i));
  47. init.m_depth = m_consts.m_probeCounts.y();
  48. init.m_type = TextureType::k3D;
  49. m_probeValidityVolumes[i] = getRenderer().createAndClearRenderTarget(init, TextureUsageBit::kSrvCompute);
  50. }
  51. // Create the RT result texture
  52. const U32 raysPerProbePerFrame = square<U32>(g_indirectDiffuseClipmapRadianceOctMapSize);
  53. m_rtResultRtDesc = getRenderer().create2DRenderTargetDescription(m_consts.m_totalProbeCount, raysPerProbePerFrame * kIndirectDiffuseClipmapCount,
  54. Format::kR16G16B16A16_Sfloat, "IndirectDiffuseClipmap: RT result");
  55. m_rtResultRtDesc.bake();
  56. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  57. {
  58. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  59. m_consts.m_probeCounts.x() * (g_indirectDiffuseClipmapRadianceOctMapSize + 2),
  60. m_consts.m_probeCounts.z() * (g_indirectDiffuseClipmapRadianceOctMapSize + 2), Format::kB10G11R11_Ufloat_Pack32,
  61. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Radiance #%u", clipmap));
  62. volumeInit.m_depth = m_consts.m_probeCounts.y();
  63. volumeInit.m_type = TextureType::k3D;
  64. m_radianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  65. }
  66. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  67. {
  68. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  69. m_consts.m_probeCounts.x() * (g_indirectDiffuseClipmapIrradianceOctMapSize + 2),
  70. m_consts.m_probeCounts.z() * (g_indirectDiffuseClipmapIrradianceOctMapSize + 2), Format::kB10G11R11_Ufloat_Pack32,
  71. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Irradiance #%u", clipmap));
  72. volumeInit.m_depth = m_consts.m_probeCounts.y();
  73. volumeInit.m_type = TextureType::k3D;
  74. m_irradianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  75. }
  76. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  77. {
  78. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  79. m_consts.m_probeCounts.x() * (g_indirectDiffuseClipmapRadianceOctMapSize + 2),
  80. m_consts.m_probeCounts.z() * (g_indirectDiffuseClipmapRadianceOctMapSize + 2), Format::kR16G16_Sfloat,
  81. TextureUsageBit::kAllShaderResource, generateTempPassName("IndirectDiffuseClipmap: Dist moments #%u", clipmap));
  82. volumeInit.m_depth = m_consts.m_probeCounts.y();
  83. volumeInit.m_type = TextureType::k3D;
  84. m_distanceMomentsVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  85. }
  86. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  87. {
  88. TextureInitInfo volumeInit = getRenderer().create2DRenderTargetInitInfo(
  89. m_consts.m_probeCounts.x(), m_consts.m_probeCounts.z(), Format::kB10G11R11_Ufloat_Pack32, TextureUsageBit::kAllShaderResource,
  90. generateTempPassName("IndirectDiffuseClipmap: Avg light #%u", clipmap));
  91. volumeInit.m_depth = m_consts.m_probeCounts.y();
  92. volumeInit.m_type = TextureType::k3D;
  93. m_avgIrradianceVolumes[clipmap] = getRenderer().createAndClearRenderTarget(volumeInit, TextureUsageBit::kSrvCompute);
  94. }
  95. const Array<SubMutation, 5> mutation = {{{"GPU_WAVE_SIZE", MutatorValue(GrManager::getSingleton().getDeviceCapabilities().m_maxWaveSize)},
  96. {"RADIANCE_OCTAHEDRON_MAP_SIZE", MutatorValue(g_indirectDiffuseClipmapRadianceOctMapSize)},
  97. {"IRRADIANCE_OCTAHEDRON_MAP_SIZE", MutatorValue(g_indirectDiffuseClipmapIrradianceOctMapSize)},
  98. {"RT_MATERIAL_FETCH_CLIPMAP", 0},
  99. {"SPATIAL_RECONSTRUCT_TYPE", !g_indirectDiffuseClipmapApplyHighQuality}}};
  100. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_applyGiGrProg, "Apply"));
  101. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_visProbesGrProg, "VisualizeProbes"));
  102. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_populateCachesGrProg, "PopulateCaches"));
  103. ANKI_CHECK(
  104. loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_computeIrradianceGrProg, "ComputeIrradiance"));
  105. ANKI_CHECK(loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_temporalDenoiseGrProg, "TemporalDenoise"));
  106. ANKI_CHECK(
  107. loadShaderProgram("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", mutation, m_prog, m_spatialReconstructGrProg, "SpatialReconstruct"));
  108. for(MutatorValue rtMaterialFetchClipmap = 0; rtMaterialFetchClipmap < 2; ++rtMaterialFetchClipmap)
  109. {
  110. ShaderProgramResourcePtr tmpProg;
  111. ANKI_CHECK(ResourceManager::getSingleton().loadResource("ShaderBinaries/IndirectDiffuseClipmaps.ankiprogbin", tmpProg));
  112. ANKI_ASSERT(tmpProg == m_prog);
  113. ShaderProgramResourceVariantInitInfo variantInitInfo(m_prog);
  114. variantInitInfo.requestTechniqueAndTypes(ShaderTypeBit::kRayGen, "RtMaterialFetch");
  115. for(const SubMutation& s : mutation)
  116. {
  117. variantInitInfo.addMutation(s.m_mutatorName, s.m_value);
  118. }
  119. variantInitInfo.addMutation("RT_MATERIAL_FETCH_CLIPMAP", rtMaterialFetchClipmap);
  120. const ShaderProgramResourceVariant* variant;
  121. m_prog->getOrCreateVariant(variantInitInfo, variant);
  122. m_rtLibraryGrProg.reset(&variant->getProgram());
  123. m_rayGenShaderGroupIndices[rtMaterialFetchClipmap] = variant->getShaderGroupHandleIndex();
  124. }
  125. {
  126. ANKI_CHECK(ResourceManager::getSingleton().loadResource("ShaderBinaries/RtMaterialFetchMiss.ankiprogbin", m_missProg));
  127. ShaderProgramResourceVariantInitInfo variantInitInfo(m_missProg);
  128. variantInitInfo.requestTechniqueAndTypes(ShaderTypeBit::kMiss, "RtMaterialFetch");
  129. const ShaderProgramResourceVariant* variant;
  130. m_missProg->getOrCreateVariant(variantInitInfo, variant);
  131. m_missShaderGroupIdx = variant->getShaderGroupHandleIndex();
  132. }
  133. m_sbtRecordSize = getAlignedRoundUp(GrManager::getSingleton().getDeviceCapabilities().m_sbtRecordAlignment,
  134. GrManager::getSingleton().getDeviceCapabilities().m_shaderGroupHandleSize + U32(sizeof(UVec4)));
  135. ANKI_CHECK(ResourceManager::getSingleton().loadResource("EngineAssets/BlueNoise_Rgba8_64x64.png", m_blueNoiseImg));
  136. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  137. {
  138. m_consts.m_textures[i].m_radianceTexture = m_radianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  139. m_consts.m_textures[i].m_irradianceTexture = m_irradianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  140. m_consts.m_textures[i].m_distanceMomentsTexture = m_distanceMomentsVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  141. m_consts.m_textures[i].m_probeValidityTexture = m_probeValidityVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  142. m_consts.m_textures[i].m_averageIrradianceTexture = m_avgIrradianceVolumes[i]->getOrCreateBindlessTextureIndex(TextureSubresourceDesc::all());
  143. m_consts.m_textures[i].m_distanceMomentsOctMapSize = (m_distanceMomentsVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  144. m_consts.m_textures[i].m_irradianceOctMapSize = (m_irradianceVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  145. m_consts.m_textures[i].m_radianceOctMapSize = (m_radianceVolumes[i]->getWidth() / m_consts.m_probeCounts.x()) - 2;
  146. }
  147. return Error::kNone;
  148. }
  149. void IndirectDiffuseClipmaps::populateRenderGraph(RenderingContext& ctx)
  150. {
  151. ANKI_TRACE_SCOPED_EVENT(IndirectDiffuse);
  152. const Bool firstBounceRt = g_indirectDiffuseClipmapFirstBounceRayDistance > 0.0f;
  153. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  154. {
  155. m_consts.m_previousFrameAabbMins[i] = m_consts.m_aabbMins[i];
  156. computeClipmapBounds(ctx.m_matrices.m_cameraTransform.getTranslationPart(),
  157. -ctx.m_matrices.m_cameraTransform.getRotationPart().getZAxis().normalize(), i, m_consts);
  158. }
  159. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  160. const RenderTargetHandle rtResultHandle = rgraph.newRenderTarget(m_rtResultRtDesc);
  161. const RenderTargetHandle lowRezRt = rgraph.newRenderTarget(m_lowRezRtDesc);
  162. const RenderTargetHandle fullHandle = rgraph.newRenderTarget(m_fullRtDesc);
  163. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& radianceVolumes = m_runCtx.m_handles.m_radianceVolumes;
  164. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& irradianceVolumes = m_runCtx.m_handles.m_irradianceVolumes;
  165. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& distanceMomentsVolumes = m_runCtx.m_handles.m_distanceMomentsVolumes;
  166. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& probeValidityVolumes = m_runCtx.m_handles.m_probeValidityVolumes;
  167. Array<RenderTargetHandle, kIndirectDiffuseClipmapCount>& avgIrradianceVolumes = m_runCtx.m_handles.m_avgIrradianceVolumes;
  168. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  169. {
  170. if(m_texturesImportedOnce) [[likely]]
  171. {
  172. radianceVolumes[i] = rgraph.importRenderTarget(m_radianceVolumes[i].get());
  173. irradianceVolumes[i] = rgraph.importRenderTarget(m_irradianceVolumes[i].get());
  174. distanceMomentsVolumes[i] = rgraph.importRenderTarget(m_distanceMomentsVolumes[i].get());
  175. probeValidityVolumes[i] = rgraph.importRenderTarget(m_probeValidityVolumes[i].get());
  176. avgIrradianceVolumes[i] = rgraph.importRenderTarget(m_avgIrradianceVolumes[i].get());
  177. }
  178. else
  179. {
  180. radianceVolumes[i] = rgraph.importRenderTarget(m_radianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  181. irradianceVolumes[i] = rgraph.importRenderTarget(m_irradianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  182. distanceMomentsVolumes[i] = rgraph.importRenderTarget(m_distanceMomentsVolumes[i].get(), TextureUsageBit::kSrvCompute);
  183. probeValidityVolumes[i] = rgraph.importRenderTarget(m_probeValidityVolumes[i].get(), TextureUsageBit::kSrvCompute);
  184. avgIrradianceVolumes[i] = rgraph.importRenderTarget(m_avgIrradianceVolumes[i].get(), TextureUsageBit::kSrvCompute);
  185. }
  186. }
  187. m_texturesImportedOnce = true;
  188. // SBT build
  189. BufferHandle sbtHandle;
  190. BufferView sbtBuffer;
  191. buildShaderBindingTablePass("IndirectDiffuseClipmaps: Build SBT", m_rtLibraryGrProg.get(), m_rayGenShaderGroupIndices[1], m_missShaderGroupIdx,
  192. m_sbtRecordSize, rgraph, sbtHandle, sbtBuffer);
  193. // Do ray tracing around the probes
  194. {
  195. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: RT");
  196. pass.newTextureDependency(rtResultHandle, TextureUsageBit::kUavCompute);
  197. pass.newBufferDependency(sbtHandle, BufferUsageBit::kShaderBindingTable);
  198. if(getRenderer().getGeneratedSky().isEnabled())
  199. {
  200. pass.newTextureDependency(getRenderer().getGeneratedSky().getEnvironmentMapRt(), TextureUsageBit::kSrvTraceRays);
  201. }
  202. pass.newTextureDependency(getShadowMapping().getShadowmapRt(), TextureUsageBit::kSrvTraceRays);
  203. pass.newAccelerationStructureDependency(getRenderer().getAccelerationStructureBuilder().getAccelerationStructureHandle(),
  204. AccelerationStructureUsageBit::kTraceRaysSrv);
  205. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  206. {
  207. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kSrvCompute);
  208. }
  209. pass.setWork([this, rtResultHandle, &ctx, sbtBuffer](RenderPassWorkContext& rgraphCtx) {
  210. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  211. cmdb.bindShaderProgram(m_rtLibraryGrProg.get());
  212. // More globals
  213. cmdb.bindSampler(ANKI_MATERIAL_REGISTER_TILINEAR_REPEAT_SAMPLER, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  214. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_GPU_SCENE, 0, GpuSceneBuffer::getSingleton().getBufferView());
  215. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_MESH_LODS, 0, GpuSceneArrays::MeshLod::getSingleton().getBufferView());
  216. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_TRANSFORMS, 0, GpuSceneArrays::Transform::getSingleton().getBufferView());
  217. #define ANKI_UNIFIED_GEOM_FORMAT(fmt, shaderType, reg) \
  218. cmdb.bindSrv( \
  219. reg, 0, \
  220. BufferView(&UnifiedGeometryBuffer::getSingleton().getBuffer(), 0, \
  221. getAlignedRoundDown(getFormatInfo(Format::k##fmt).m_texelSize, UnifiedGeometryBuffer::getSingleton().getBuffer().getSize())), \
  222. Format::k##fmt);
  223. #include <AnKi/Shaders/Include/UnifiedGeometryTypes.def.h>
  224. cmdb.bindConstantBuffer(0, 2, ctx.m_globalRenderingConstantsBuffer);
  225. U32 srv = 0;
  226. rgraphCtx.bindSrv(srv++, 2, getRenderer().getAccelerationStructureBuilder().getAccelerationStructureHandle());
  227. const LightComponent* dirLight = SceneGraph::getSingleton().getDirectionalLight();
  228. const SkyboxComponent* sky = SceneGraph::getSingleton().getSkybox();
  229. const Bool bSkySolidColor =
  230. (!sky || sky->getSkyboxType() == SkyboxType::kSolidColor || (!dirLight && sky->getSkyboxType() == SkyboxType::kGenerated));
  231. if(bSkySolidColor)
  232. {
  233. cmdb.bindSrv(srv++, 2, TextureView(getDummyGpuResources().m_texture2DSrv.get(), TextureSubresourceDesc::all()));
  234. }
  235. else if(sky->getSkyboxType() == SkyboxType::kImage2D)
  236. {
  237. cmdb.bindSrv(srv++, 2, TextureView(&sky->getImageResource().getTexture(), TextureSubresourceDesc::all()));
  238. }
  239. else
  240. {
  241. rgraphCtx.bindSrv(srv++, 2, getRenderer().getGeneratedSky().getEnvironmentMapRt());
  242. }
  243. rgraphCtx.bindSrv(srv++, 2, getShadowMapping().getShadowmapRt());
  244. cmdb.bindSrv(srv++, 2, BufferView(getDummyGpuResources().m_buffer.get(), 0, sizeof(U32)));
  245. cmdb.bindSrv(srv++, 2, BufferView(getDummyGpuResources().m_buffer.get(), 0, sizeof(U32)));
  246. for(U32 i = 0; i < 3; ++i)
  247. {
  248. cmdb.bindSrv(srv++, 2, TextureView(getDummyGpuResources().m_texture2DSrv.get(), TextureSubresourceDesc::all()));
  249. }
  250. cmdb.bindSampler(0, 2, getRenderer().getSamplers().m_trilinearClamp.get());
  251. cmdb.bindSampler(1, 2, getRenderer().getSamplers().m_trilinearClampShadow.get());
  252. cmdb.bindSampler(2, 2, getRenderer().getSamplers().m_trilinearRepeat.get());
  253. rgraphCtx.bindUav(0, 2, rtResultHandle);
  254. cmdb.bindUav(1, 2, TextureView(getDummyGpuResources().m_texture2DUav.get(), TextureSubresourceDesc::firstSurface()));
  255. const U32 raysPerProbePerFrame = square<U32>(g_indirectDiffuseClipmapRadianceOctMapSize);
  256. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  257. {
  258. const UVec4 consts(clipmap, g_indirectDiffuseClipmapRadianceOctMapSize, 0, 0);
  259. cmdb.setFastConstants(&consts, sizeof(consts));
  260. cmdb.traceRays(sbtBuffer, m_sbtRecordSize, GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount(), 1,
  261. m_consts.m_totalProbeCount * raysPerProbePerFrame, 1, 1);
  262. }
  263. });
  264. }
  265. // Populate caches
  266. {
  267. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Populate caches");
  268. pass.newTextureDependency(rtResultHandle, TextureUsageBit::kSrvCompute);
  269. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  270. {
  271. pass.newTextureDependency(radianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  272. pass.newTextureDependency(probeValidityVolumes[clipmap], TextureUsageBit::kUavCompute);
  273. pass.newTextureDependency(distanceMomentsVolumes[clipmap], TextureUsageBit::kUavCompute);
  274. }
  275. pass.setWork([this, &ctx, rtResultHandle, radianceVolumes, probeValidityVolumes, distanceMomentsVolumes](RenderPassWorkContext& rgraphCtx) {
  276. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  277. cmdb.bindShaderProgram(m_populateCachesGrProg.get());
  278. rgraphCtx.bindSrv(0, 0, rtResultHandle);
  279. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  280. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  281. {
  282. rgraphCtx.bindUav(0, 0, radianceVolumes[clipmap]);
  283. rgraphCtx.bindUav(1, 0, distanceMomentsVolumes[clipmap]);
  284. rgraphCtx.bindUav(2, 0, probeValidityVolumes[clipmap]);
  285. const UVec4 consts(clipmap);
  286. cmdb.setFastConstants(&consts, sizeof(consts));
  287. const U32 raysPerProbePerFrame = square<U32>(g_indirectDiffuseClipmapRadianceOctMapSize);
  288. const U32 threadCount = 64;
  289. cmdb.dispatchCompute((raysPerProbePerFrame * m_consts.m_totalProbeCount + threadCount - 1) / threadCount, 1, 1);
  290. }
  291. });
  292. }
  293. // Compute irradiance
  294. {
  295. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Irradiance");
  296. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  297. {
  298. pass.newTextureDependency(radianceVolumes[clipmap], TextureUsageBit::kSrvCompute);
  299. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  300. pass.newTextureDependency(avgIrradianceVolumes[clipmap], TextureUsageBit::kUavCompute);
  301. }
  302. pass.setWork([this, &ctx, radianceVolumes, irradianceVolumes, avgIrradianceVolumes](RenderPassWorkContext& rgraphCtx) {
  303. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  304. cmdb.bindShaderProgram(m_computeIrradianceGrProg.get());
  305. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  306. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  307. U32 uav = 0;
  308. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  309. {
  310. rgraphCtx.bindSrv(clipmap, 0, radianceVolumes[clipmap]);
  311. rgraphCtx.bindUav(uav++, 0, irradianceVolumes[clipmap]);
  312. }
  313. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  314. {
  315. rgraphCtx.bindUav(uav++, 0, avgIrradianceVolumes[clipmap]);
  316. }
  317. cmdb.dispatchCompute(m_consts.m_probeCounts[0] * kIndirectDiffuseClipmapCount, m_consts.m_probeCounts[1], m_consts.m_probeCounts[2]);
  318. });
  319. }
  320. // Apply GI
  321. if(firstBounceRt)
  322. {
  323. patchShaderBindingTablePass("IndirectDiffuseClipmaps: Patch SBT", m_rtLibraryGrProg.get(), m_rayGenShaderGroupIndices[0],
  324. m_missShaderGroupIdx, m_sbtRecordSize, rgraph, sbtHandle, sbtBuffer);
  325. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: RTApply");
  326. pass.newBufferDependency(sbtHandle, BufferUsageBit::kShaderBindingTable);
  327. if(getRenderer().getGeneratedSky().isEnabled())
  328. {
  329. pass.newTextureDependency(getRenderer().getGeneratedSky().getEnvironmentMapRt(), TextureUsageBit::kSrvTraceRays);
  330. }
  331. pass.newTextureDependency(getShadowMapping().getShadowmapRt(), TextureUsageBit::kSrvTraceRays);
  332. pass.newAccelerationStructureDependency(getRenderer().getAccelerationStructureBuilder().getAccelerationStructureHandle(),
  333. AccelerationStructureUsageBit::kTraceRaysSrv);
  334. pass.newTextureDependency(getGBuffer().getColorRt(2), TextureUsageBit::kSrvTraceRays);
  335. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvTraceRays);
  336. for(U32 clipmap = 0; clipmap < kIndirectDiffuseClipmapCount; ++clipmap)
  337. {
  338. pass.newTextureDependency(irradianceVolumes[clipmap], TextureUsageBit::kSrvTraceRays);
  339. pass.newTextureDependency(probeValidityVolumes[clipmap], TextureUsageBit::kSrvTraceRays);
  340. pass.newTextureDependency(distanceMomentsVolumes[clipmap], TextureUsageBit::kSrvTraceRays);
  341. }
  342. pass.newTextureDependency(lowRezRt, TextureUsageBit::kUavTraceRays);
  343. pass.setWork([this, &ctx, sbtBuffer, lowRezRt](RenderPassWorkContext& rgraphCtx) {
  344. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  345. cmdb.bindShaderProgram(m_rtLibraryGrProg.get());
  346. // More globals
  347. cmdb.bindSampler(ANKI_MATERIAL_REGISTER_TILINEAR_REPEAT_SAMPLER, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  348. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_GPU_SCENE, 0, GpuSceneBuffer::getSingleton().getBufferView());
  349. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_MESH_LODS, 0, GpuSceneArrays::MeshLod::getSingleton().getBufferView());
  350. cmdb.bindSrv(ANKI_MATERIAL_REGISTER_TRANSFORMS, 0, GpuSceneArrays::Transform::getSingleton().getBufferView());
  351. #define ANKI_UNIFIED_GEOM_FORMAT(fmt, shaderType, reg) \
  352. cmdb.bindSrv( \
  353. reg, 0, \
  354. BufferView(&UnifiedGeometryBuffer::getSingleton().getBuffer(), 0, \
  355. getAlignedRoundDown(getFormatInfo(Format::k##fmt).m_texelSize, UnifiedGeometryBuffer::getSingleton().getBuffer().getSize())), \
  356. Format::k##fmt);
  357. #include <AnKi/Shaders/Include/UnifiedGeometryTypes.def.h>
  358. cmdb.bindConstantBuffer(0, 2, ctx.m_globalRenderingConstantsBuffer);
  359. U32 srv = 0;
  360. rgraphCtx.bindSrv(srv++, 2, getRenderer().getAccelerationStructureBuilder().getAccelerationStructureHandle());
  361. const LightComponent* dirLight = SceneGraph::getSingleton().getDirectionalLight();
  362. const SkyboxComponent* sky = SceneGraph::getSingleton().getSkybox();
  363. const Bool bSkySolidColor =
  364. (!sky || sky->getSkyboxType() == SkyboxType::kSolidColor || (!dirLight && sky->getSkyboxType() == SkyboxType::kGenerated));
  365. if(bSkySolidColor)
  366. {
  367. cmdb.bindSrv(srv++, 2, TextureView(getDummyGpuResources().m_texture2DSrv.get(), TextureSubresourceDesc::all()));
  368. }
  369. else if(sky->getSkyboxType() == SkyboxType::kImage2D)
  370. {
  371. cmdb.bindSrv(srv++, 2, TextureView(&sky->getImageResource().getTexture(), TextureSubresourceDesc::all()));
  372. }
  373. else
  374. {
  375. rgraphCtx.bindSrv(srv++, 2, getRenderer().getGeneratedSky().getEnvironmentMapRt());
  376. }
  377. rgraphCtx.bindSrv(srv++, 2, getShadowMapping().getShadowmapRt());
  378. cmdb.bindSrv(srv++, 2, BufferView(getDummyGpuResources().m_buffer.get(), 0, sizeof(U32)));
  379. cmdb.bindSrv(srv++, 2, BufferView(getDummyGpuResources().m_buffer.get(), 0, sizeof(U32)));
  380. rgraphCtx.bindSrv(srv++, 2, getGBuffer().getDepthRt());
  381. cmdb.bindSrv(srv++, 2, TextureView(getDummyGpuResources().m_texture2DSrv.get(), TextureSubresourceDesc::all()));
  382. rgraphCtx.bindSrv(srv++, 2, getGBuffer().getColorRt(2));
  383. cmdb.bindSampler(0, 2, getRenderer().getSamplers().m_trilinearClamp.get());
  384. cmdb.bindSampler(1, 2, getRenderer().getSamplers().m_trilinearClampShadow.get());
  385. cmdb.bindSampler(2, 2, getRenderer().getSamplers().m_trilinearRepeat.get());
  386. rgraphCtx.bindUav(0, 2, lowRezRt);
  387. cmdb.bindUav(1, 2, TextureView(getDummyGpuResources().m_texture2DUav.get(), TextureSubresourceDesc::firstSurface()));
  388. const Vec4 consts(g_indirectDiffuseClipmapFirstBounceRayDistance);
  389. cmdb.setFastConstants(&consts, sizeof(consts));
  390. cmdb.traceRays(sbtBuffer, m_sbtRecordSize, GpuSceneArrays::RenderableBoundingVolumeRt::getSingleton().getElementCount(), 1,
  391. getRenderer().getInternalResolution().x() / 2,
  392. getRenderer().getInternalResolution().y() / (!g_indirectDiffuseClipmapApplyHighQuality + 1), 1);
  393. });
  394. }
  395. else
  396. {
  397. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Apply irradiance");
  398. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvCompute);
  399. pass.newTextureDependency(getGBuffer().getColorRt(2), TextureUsageBit::kSrvCompute);
  400. for(U32 i = 0; i < kIndirectDiffuseClipmapCount; ++i)
  401. {
  402. pass.newTextureDependency(irradianceVolumes[i], TextureUsageBit::kSrvCompute);
  403. pass.newTextureDependency(probeValidityVolumes[i], TextureUsageBit::kSrvCompute);
  404. pass.newTextureDependency(distanceMomentsVolumes[i], TextureUsageBit::kSrvCompute);
  405. pass.newTextureDependency(avgIrradianceVolumes[i], TextureUsageBit::kSrvCompute);
  406. }
  407. pass.newTextureDependency(lowRezRt, TextureUsageBit::kUavCompute);
  408. pass.setWork([this, &ctx, lowRezRt](RenderPassWorkContext& rgraphCtx) {
  409. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  410. cmdb.bindShaderProgram(m_applyGiGrProg.get());
  411. rgraphCtx.bindSrv(0, 0, getGBuffer().getDepthRt());
  412. rgraphCtx.bindSrv(1, 0, getGBuffer().getColorRt(2));
  413. cmdb.bindSrv(2, 0, TextureView(&m_blueNoiseImg->getTexture(), TextureSubresourceDesc::firstSurface()));
  414. rgraphCtx.bindUav(0, 0, lowRezRt);
  415. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  416. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  417. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x() / 2,
  418. getRenderer().getInternalResolution().y() / (!g_indirectDiffuseClipmapApplyHighQuality + 1));
  419. });
  420. }
  421. // Spatial reconstruct
  422. {
  423. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass("IndirectDiffuseClipmaps: Spatial reconstruct");
  424. pass.newTextureDependency(getGBuffer().getDepthRt(), TextureUsageBit::kSrvCompute);
  425. pass.newTextureDependency(lowRezRt, TextureUsageBit::kSrvCompute);
  426. pass.newTextureDependency(fullHandle, TextureUsageBit::kUavCompute);
  427. pass.setWork([this, lowRezRt, fullHandle](RenderPassWorkContext& rgraphCtx) {
  428. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  429. cmdb.bindShaderProgram(m_spatialReconstructGrProg.get());
  430. rgraphCtx.bindSrv(0, 0, lowRezRt);
  431. rgraphCtx.bindSrv(1, 0, getGBuffer().getDepthRt());
  432. rgraphCtx.bindUav(0, 0, fullHandle);
  433. dispatchPPCompute(cmdb, 8, 8, getRenderer().getInternalResolution().x() / 2,
  434. getRenderer().getInternalResolution().y() / (!g_indirectDiffuseClipmapApplyHighQuality + 1));
  435. });
  436. }
  437. m_runCtx.m_handles.m_appliedIrradiance = fullHandle;
  438. }
  439. void IndirectDiffuseClipmaps::drawDebugProbes(const RenderingContext& ctx, RenderPassWorkContext& rgraphCtx) const
  440. {
  441. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  442. const U32 clipmap = 0;
  443. cmdb.bindShaderProgram(m_visProbesGrProg.get());
  444. const UVec4 consts(clipmap);
  445. cmdb.setFastConstants(&consts, sizeof(consts));
  446. cmdb.bindConstantBuffer(0, 0, ctx.m_globalRenderingConstantsBuffer);
  447. const RenderTargetHandle visVolume = m_runCtx.m_handles.m_radianceVolumes[clipmap];
  448. rgraphCtx.bindSrv(0, 0, visVolume);
  449. rgraphCtx.bindSrv(1, 0, m_runCtx.m_handles.m_probeValidityVolumes[clipmap]);
  450. cmdb.bindSampler(0, 0, getRenderer().getSamplers().m_trilinearRepeat.get());
  451. cmdb.draw(PrimitiveTopology::kTriangles, 36, m_consts.m_totalProbeCount);
  452. }
  453. } // end namespace anki