ShadowMapping.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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/ShadowMapping.h>
  6. #include <AnKi/Renderer/Renderer.h>
  7. #include <AnKi/Renderer/GBuffer.h>
  8. #include <AnKi/Renderer/PrimaryNonRenderableVisibility.h>
  9. #include <AnKi/Renderer/Utils/GpuVisibility.h>
  10. #include <AnKi/Renderer/Utils/Drawer.h>
  11. #include <AnKi/Renderer/Utils/HzbGenerator.h>
  12. #include <AnKi/Core/App.h>
  13. #include <AnKi/Core/StatsSet.h>
  14. #include <AnKi/Core/GpuMemory/GpuVisibleTransientMemoryPool.h>
  15. #include <AnKi/Util/Tracer.h>
  16. #include <AnKi/Scene/Components/LightComponent.h>
  17. #include <AnKi/Scene/Components/CameraComponent.h>
  18. #include <AnKi/Scene/RenderStateBucket.h>
  19. namespace anki {
  20. static StatCounter g_tilesAllocatedStatVar(StatCategory::kRenderer, "Shadow tiles (re)allocated", StatFlag::kMainThreadUpdates);
  21. static StatCounter g_shadowLightsProcessedStatVar(StatCategory::kRenderer, "Lights processed by shadows",
  22. StatFlag::kMainThreadUpdates | StatFlag::kZeroEveryFrame);
  23. class LightHash
  24. {
  25. public:
  26. class Unpacked
  27. {
  28. public:
  29. U64 m_uuid : 31;
  30. U64 m_componentIndex : 30;
  31. U64 m_faceIdx : 3;
  32. };
  33. union
  34. {
  35. Unpacked m_unpacked;
  36. U64 m_packed;
  37. };
  38. };
  39. static U64 encodeTileHash(U32 lightUuid, U32 componentIndex, U32 faceIdx)
  40. {
  41. ANKI_ASSERT(faceIdx < 6);
  42. LightHash c;
  43. c.m_unpacked.m_uuid = lightUuid;
  44. c.m_unpacked.m_componentIndex = componentIndex;
  45. c.m_unpacked.m_faceIdx = faceIdx;
  46. return c.m_packed;
  47. }
  48. static LightHash decodeTileHash(U64 hash)
  49. {
  50. LightHash c;
  51. c.m_packed = hash;
  52. return c;
  53. }
  54. Error ShadowMapping::init()
  55. {
  56. // Init RT
  57. {
  58. m_tileResolution = g_shadowMappingTileResolutionCVar;
  59. m_tileCountBothAxis = g_shadowMappingTileCountPerRowOrColumnCVar;
  60. const TextureUsageBit usage =
  61. TextureUsageBit::kSrvPixel | TextureUsageBit::kSrvCompute | TextureUsageBit::kSrvTraceRays | TextureUsageBit::kAllRtvDsv;
  62. TextureInitInfo texinit = getRenderer().create2DRenderTargetInitInfo(
  63. m_tileResolution * m_tileCountBothAxis, m_tileResolution * m_tileCountBothAxis, Format::kD32_Sfloat, usage, "ShadowAtlas");
  64. ClearValue clearVal;
  65. clearVal.m_colorf[0] = 1.0f;
  66. m_atlasTex = getRenderer().createAndClearRenderTarget(texinit, TextureUsageBit::kSrvPixel, clearVal);
  67. }
  68. // Tiles
  69. m_tileAlloc.init(m_tileCountBothAxis, m_tileCountBothAxis, kTileAllocHierarchyCount, true);
  70. ANKI_CHECK(loadShaderProgram("ShaderBinaries/ShadowMappingClearDepth.ankiprogbin", m_clearDepthProg, m_clearDepthGrProg));
  71. ANKI_CHECK(loadShaderProgram("ShaderBinaries/ShadowMappingVetVisibility.ankiprogbin", m_vetVisibilityProg, m_vetVisibilityGrProg));
  72. for(U32 i = 0; i < kMaxShadowCascades; ++i)
  73. {
  74. RendererString name;
  75. name.sprintf("DirLight HZB #%d", i);
  76. const U32 cascadeResolution = (m_tileResolution * (1 << (kTileAllocHierarchyCount - 1))) >> chooseDirectionalLightShadowCascadeDetail(i);
  77. UVec2 size(min(cascadeResolution, 1024u));
  78. size /= 2;
  79. m_cascadeHzbRtDescrs[i] = getRenderer().create2DRenderTargetDescription(size.x(), size.y(), Format::kR32_Sfloat, name);
  80. m_cascadeHzbRtDescrs[i].m_mipmapCount = U8(computeMaxMipmapCount2d(m_cascadeHzbRtDescrs[i].m_width, m_cascadeHzbRtDescrs[i].m_height));
  81. m_cascadeHzbRtDescrs[i].bake();
  82. }
  83. return Error::kNone;
  84. }
  85. Mat4 ShadowMapping::createSpotLightTextureMatrix(const UVec4& viewport) const
  86. {
  87. const F32 atlasSize = F32(m_tileResolution * m_tileCountBothAxis);
  88. #if ANKI_COMPILER_GCC_COMPATIBLE
  89. # pragma GCC diagnostic push
  90. # pragma GCC diagnostic ignored "-Wpedantic" // Because GCC and clang throw an incorrect warning
  91. #endif
  92. const Vec2 uv(F32(viewport[0]) / atlasSize, F32(viewport[1]) / atlasSize);
  93. #if ANKI_COMPILER_GCC_COMPATIBLE
  94. # pragma GCC diagnostic pop
  95. #endif
  96. ANKI_ASSERT(uv >= Vec2(0.0f) && uv <= Vec2(1.0f));
  97. ANKI_ASSERT(viewport[2] == viewport[3]);
  98. const F32 sizeTextureSpace = F32(viewport[2]) / atlasSize;
  99. const Mat4 biasMat4(0.5f, 0.0f, 0.0f, 0.5f, 0.0f, -0.5f, 0.0f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
  100. return Mat4(sizeTextureSpace, 0.0f, 0.0f, uv.x(), 0.0f, sizeTextureSpace, 0.0f, uv.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
  101. * biasMat4;
  102. }
  103. void ShadowMapping::populateRenderGraph(RenderingContext& ctx)
  104. {
  105. ANKI_TRACE_SCOPED_EVENT(ShadowMapping);
  106. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  107. // Import
  108. if(m_rtImportedOnce) [[likely]]
  109. {
  110. m_runCtx.m_rt = rgraph.importRenderTarget(m_atlasTex.get());
  111. }
  112. else
  113. {
  114. m_runCtx.m_rt = rgraph.importRenderTarget(m_atlasTex.get(), TextureUsageBit::kSrvPixel);
  115. m_rtImportedOnce = true;
  116. }
  117. // First process the lights
  118. processLights(ctx);
  119. }
  120. void ShadowMapping::chooseDetail(const Vec3& cameraOrigin, const LightComponent& lightc, Vec2 lodDistances, U32& tileAllocatorHierarchy) const
  121. {
  122. if(lightc.getLightComponentType() == LightComponentType::kPoint)
  123. {
  124. const F32 distFromTheCamera = (cameraOrigin - lightc.getWorldPosition()).length() - lightc.getRadius();
  125. if(distFromTheCamera < lodDistances[0])
  126. {
  127. tileAllocatorHierarchy = kPointLightMaxTileAllocHierarchy;
  128. }
  129. else
  130. {
  131. tileAllocatorHierarchy = max(kPointLightMaxTileAllocHierarchy, 1u) - 1;
  132. }
  133. }
  134. else
  135. {
  136. ANKI_ASSERT(lightc.getLightComponentType() == LightComponentType::kSpot);
  137. // Get some data
  138. const Vec3 coneOrigin = lightc.getWorldPosition();
  139. const Vec3 coneDir = lightc.getDirection();
  140. const F32 coneAngle = lightc.getOuterAngle();
  141. // Compute the distance from the camera to the light cone
  142. const Vec3 V = cameraOrigin - coneOrigin;
  143. const F32 VlenSq = V.dot(V);
  144. const F32 V1len = V.dot(coneDir);
  145. const F32 distFromTheCamera = cos(coneAngle) * sqrt(VlenSq - V1len * V1len) - V1len * sin(coneAngle);
  146. if(distFromTheCamera < lodDistances[0])
  147. {
  148. tileAllocatorHierarchy = kSpotLightMaxTileAllocHierarchy;
  149. }
  150. else if(distFromTheCamera < lodDistances[1])
  151. {
  152. tileAllocatorHierarchy = max(kSpotLightMaxTileAllocHierarchy, 1u) - 1;
  153. }
  154. else
  155. {
  156. tileAllocatorHierarchy = max(kSpotLightMaxTileAllocHierarchy, 2u) - 2;
  157. }
  158. }
  159. }
  160. TileAllocatorResult2 ShadowMapping::allocateAtlasTiles(U32 lightUuid, U32 componentIndex, U32 faceCount, const U32* hierarchies,
  161. UVec4* atlasTileViewports)
  162. {
  163. ANKI_ASSERT(lightUuid > 0);
  164. ANKI_ASSERT(faceCount > 0);
  165. ANKI_ASSERT(hierarchies);
  166. TileAllocatorResult2 goodResult = TileAllocatorResult2::kAllocationSucceded | TileAllocatorResult2::kTileCached;
  167. for(U32 i = 0; i < faceCount; ++i)
  168. {
  169. TileAllocator::ArrayOfLightUuids kickedOutLights(&getRenderer().getFrameMemoryPool());
  170. Array<U32, 4> tileViewport;
  171. const TileAllocatorResult2 result = m_tileAlloc.allocate(
  172. GlobalFrameIndex::getSingleton().m_value, encodeTileHash(lightUuid, componentIndex, i), hierarchies[i], tileViewport, kickedOutLights);
  173. for(U64 kickedLightHash : kickedOutLights)
  174. {
  175. const LightHash hash = decodeTileHash(kickedLightHash);
  176. const Bool found = SceneGraph::getSingleton().getComponentArrays().getLights().indexExists(hash.m_unpacked.m_componentIndex);
  177. if(found)
  178. {
  179. LightComponent& lightc = SceneGraph::getSingleton().getComponentArrays().getLights()[hash.m_unpacked.m_componentIndex];
  180. if(lightc.getUuid() == hash.m_unpacked.m_uuid)
  181. {
  182. lightc.setShadowAtlasUvViewports({});
  183. }
  184. }
  185. }
  186. if(!!(result & TileAllocatorResult2::kAllocationFailed))
  187. {
  188. ANKI_R_LOGW("There is not enough space in the shadow atlas for more shadow maps. Increase the %s or decrease the scene's shadow casters",
  189. g_shadowMappingTileCountPerRowOrColumnCVar.getFullName().cstr());
  190. // Invalidate cache entries for what we already allocated
  191. for(U32 j = 0; j < i; ++j)
  192. {
  193. m_tileAlloc.invalidateCache(encodeTileHash(lightUuid, componentIndex, j));
  194. }
  195. return TileAllocatorResult2::kAllocationFailed;
  196. }
  197. if(!(result & TileAllocatorResult2::kTileCached))
  198. {
  199. g_tilesAllocatedStatVar.increment(1);
  200. }
  201. goodResult &= result;
  202. // Set viewport
  203. const UVec4 viewport = UVec4(tileViewport) * m_tileResolution;
  204. atlasTileViewports[i] = viewport;
  205. }
  206. return goodResult;
  207. }
  208. void ShadowMapping::processLights(RenderingContext& ctx)
  209. {
  210. // First allocate tiles for the dir light and then build passes for points and spot lights. Then passes for the dir light. The dir light has many
  211. // passes and it will push the other types of lights further into the future. So do those first.
  212. // Vars
  213. const Vec3 cameraOrigin = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
  214. RenderGraphBuilder& rgraph = ctx.m_renderGraphDescr;
  215. const CameraComponent& mainCam = SceneGraph::getSingleton().getActiveCameraNode().getFirstComponentOfType<CameraComponent>();
  216. // Allocate tiles for the dir light first but don't build any passes
  217. const LightComponent* dirLight = SceneGraph::getSingleton().getDirectionalLight();
  218. if(dirLight && (!dirLight->getShadowEnabled() || !g_shadowCascadeCountCVar))
  219. {
  220. dirLight = nullptr; // Skip dir light
  221. }
  222. Array<UVec4, kMaxShadowCascades> dirLightAtlasViewports;
  223. if(dirLight)
  224. {
  225. const U32 cascadeCount = g_shadowCascadeCountCVar;
  226. Array<U32, kMaxShadowCascades> hierarchies;
  227. for(U32 cascade = 0; cascade < cascadeCount; ++cascade)
  228. {
  229. // Change the quality per cascade
  230. hierarchies[cascade] = kTileAllocHierarchyCount - 1 - chooseDirectionalLightShadowCascadeDetail(cascade);
  231. }
  232. [[maybe_unused]] const TileAllocatorResult2 res = allocateAtlasTiles(kMaxU32, 0, cascadeCount, &hierarchies[0], &dirLightAtlasViewports[0]);
  233. ANKI_ASSERT(!!(res & TileAllocatorResult2::kAllocationSucceded) && "Dir light should never fail");
  234. }
  235. // Process the point lights first
  236. WeakArray<LightComponent*> lights = getRenderer().getPrimaryNonRenderableVisibility().getInterestingVisibleComponents().m_shadowLights;
  237. g_shadowLightsProcessedStatVar.increment(lights.getSize() + (dirLight != 0));
  238. for(LightComponent* lightc : lights)
  239. {
  240. if(lightc->getLightComponentType() != LightComponentType::kPoint || !lightc->getShadowEnabled())
  241. {
  242. continue;
  243. }
  244. // Prepare data to allocate tiles and allocate
  245. U32 hierarchy;
  246. chooseDetail(cameraOrigin, *lightc, {g_lod0MaxDistanceCVar, g_lod1MaxDistanceCVar}, hierarchy);
  247. Array<U32, 6> hierarchies;
  248. hierarchies.fill(hierarchy);
  249. Array<UVec4, 6> atlasViewports;
  250. const TileAllocatorResult2 result = allocateAtlasTiles(lightc->getUuid(), lightc->getArrayIndex(), 6, &hierarchies[0], &atlasViewports[0]);
  251. if(!!(result & TileAllocatorResult2::kAllocationSucceded))
  252. {
  253. // All good, update the light
  254. // Remove a few texels to avoid bilinear filtering bleeding
  255. F32 texelsBorder;
  256. if(g_shadowMappingPcfCVar || g_shadowMappingPcssCVar)
  257. {
  258. texelsBorder = 2.0f; // 2 texels
  259. }
  260. else
  261. {
  262. texelsBorder = 0.5f; // Half texel
  263. }
  264. const F32 atlasResolution = F32(m_tileResolution * m_tileCountBothAxis);
  265. F32 superTileSize = F32(atlasViewports[0][2]); // Should be the same for all tiles and faces
  266. superTileSize -= texelsBorder * 2.0f; // Remove from both sides
  267. Array<Vec4, 6> uvViewports;
  268. for(U face = 0; face < 6; ++face)
  269. {
  270. // Add a half texel to the viewport's start to avoid bilinear filtering bleeding
  271. const Vec2 uvViewportXY = (Vec2(atlasViewports[face].xy()) + texelsBorder) / atlasResolution;
  272. uvViewports[face] = Vec4(uvViewportXY, Vec2(superTileSize / atlasResolution));
  273. }
  274. if(!(result & TileAllocatorResult2::kTileCached))
  275. {
  276. lightc->setShadowAtlasUvViewports(uvViewports);
  277. }
  278. // Vis testing
  279. const Array<F32, kMaxLodCount - 1> lodDistances = {g_lod0MaxDistanceCVar, g_lod1MaxDistanceCVar};
  280. DistanceGpuVisibilityInput visIn;
  281. visIn.m_passesName = generateTempPassName("Shadows point light light UUID:%u", lightc->getUuid());
  282. visIn.m_technique = RenderingTechnique::kDepth;
  283. visIn.m_lodReferencePoint = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
  284. visIn.m_lodDistances = lodDistances;
  285. visIn.m_rgraph = &rgraph;
  286. visIn.m_pointOfTest = lightc->getWorldPosition();
  287. visIn.m_testRadius = lightc->getRadius();
  288. visIn.m_hashVisibles = true;
  289. GpuVisibilityOutput visOut;
  290. getRenderer().getGpuVisibility().populateRenderGraph(visIn, visOut);
  291. // Vet visibility
  292. const Bool renderAllways = !(result & TileAllocatorResult2::kTileCached);
  293. BufferView clearTileIndirectArgs;
  294. if(!renderAllways)
  295. {
  296. clearTileIndirectArgs = createVetVisibilityPass(generateTempPassName("Shadows: Vet point light light UUID:%u", lightc->getUuid()),
  297. *lightc, visOut, rgraph);
  298. }
  299. // Draw
  300. Array<ShadowSubpassInfo, 6> subpasses;
  301. for(U32 face = 0; face < 6; ++face)
  302. {
  303. Frustum frustum;
  304. frustum.init(FrustumType::kPerspective);
  305. frustum.setPerspective(kClusterObjectFrustumNearPlane, lightc->getRadius(), kPi / 2.0f, kPi / 2.0f);
  306. frustum.setWorldTransform(
  307. Transform(lightc->getWorldPosition().xyz0(), Frustum::getOmnidirectionalFrustumRotations()[face], Vec4(1.0f, 1.0f, 1.0f, 0.0f)));
  308. frustum.update();
  309. subpasses[face].m_clearTileIndirectArgs = clearTileIndirectArgs;
  310. subpasses[face].m_viewMat = frustum.getViewMatrix();
  311. subpasses[face].m_viewport = atlasViewports[face];
  312. subpasses[face].m_viewProjMat = frustum.getViewProjectionMatrix();
  313. }
  314. createDrawShadowsPass(subpasses, visOut, generateTempPassName("Shadows: Point light UUID:%u", lightc->getUuid()), rgraph);
  315. }
  316. else
  317. {
  318. // Can't be a caster from now on
  319. lightc->setShadowAtlasUvViewports({});
  320. }
  321. }
  322. // Process the spot lights 2nd
  323. for(LightComponent* lightc : lights)
  324. {
  325. if(lightc->getLightComponentType() != LightComponentType::kSpot || !lightc->getShadowEnabled())
  326. {
  327. continue;
  328. }
  329. // Allocate tile
  330. U32 hierarchy;
  331. chooseDetail(cameraOrigin, *lightc, {g_lod0MaxDistanceCVar, g_lod1MaxDistanceCVar}, hierarchy);
  332. UVec4 atlasViewport;
  333. const TileAllocatorResult2 result = allocateAtlasTiles(lightc->getUuid(), lightc->getArrayIndex(), 1, &hierarchy, &atlasViewport);
  334. if(!!(result & TileAllocatorResult2::kAllocationSucceded))
  335. {
  336. // All good, update the light
  337. if(!(result & TileAllocatorResult2::kTileCached))
  338. {
  339. const F32 atlasResolution = F32(m_tileResolution * m_tileCountBothAxis);
  340. const Vec4 uvViewport = Vec4(atlasViewport) / atlasResolution;
  341. lightc->setShadowAtlasUvViewports({&uvViewport, 1});
  342. }
  343. // Vis testing
  344. const Array<F32, kMaxLodCount - 1> lodDistances = {g_lod0MaxDistanceCVar, g_lod1MaxDistanceCVar};
  345. FrustumGpuVisibilityInput visIn;
  346. visIn.m_passesName = generateTempPassName("Shadows spot light UUID:%u", lightc->getUuid());
  347. visIn.m_technique = RenderingTechnique::kDepth;
  348. visIn.m_lodReferencePoint = cameraOrigin;
  349. visIn.m_lodDistances = lodDistances;
  350. visIn.m_rgraph = &rgraph;
  351. visIn.m_viewProjectionMatrix = lightc->getSpotLightViewProjectionMatrix();
  352. visIn.m_hashVisibles = true;
  353. visIn.m_viewportSize = atlasViewport.zw();
  354. GpuVisibilityOutput visOut;
  355. getRenderer().getGpuVisibility().populateRenderGraph(visIn, visOut);
  356. // Vet visibility
  357. const Bool renderAllways = !(result & TileAllocatorResult2::kTileCached);
  358. BufferView clearTileIndirectArgs;
  359. if(!renderAllways)
  360. {
  361. clearTileIndirectArgs =
  362. createVetVisibilityPass(generateTempPassName("Shadows: Vet spot light UUID:%u", lightc->getUuid()), *lightc, visOut, rgraph);
  363. }
  364. // Add draw pass
  365. createDrawShadowsPass(atlasViewport, lightc->getSpotLightViewProjectionMatrix(), lightc->getSpotLightViewMatrix(), visOut,
  366. clearTileIndirectArgs, {}, generateTempPassName("Shadows: Spot light UUID:%u", lightc->getUuid()), rgraph);
  367. }
  368. else
  369. {
  370. // Doesn't have renderables or the allocation failed, won't be a shadow caster
  371. lightc->setShadowAtlasUvViewports({});
  372. }
  373. }
  374. // Process the directional light last
  375. if(dirLight)
  376. {
  377. const U32 cascadeCount = g_shadowCascadeCountCVar;
  378. // Compute the view projection matrices
  379. Array<F32, kMaxShadowCascades> cascadeDistances;
  380. static_assert(kMaxShadowCascades == 4);
  381. cascadeDistances[0] = g_shadowCascade0DistanceCVar;
  382. cascadeDistances[1] = g_shadowCascade1DistanceCVar;
  383. cascadeDistances[2] = g_shadowCascade2DistanceCVar;
  384. cascadeDistances[3] = g_shadowCascade3DistanceCVar;
  385. Array<Mat4, kMaxShadowCascades> cascadeViewProjMats;
  386. Array<Mat3x4, kMaxShadowCascades> cascadeViewMats;
  387. Array<Mat4, kMaxShadowCascades> cascadeProjMats;
  388. Array<Array<F32, U32(FrustumPlaneType::kCount)>, kMaxShadowCascades> cascadePlanes;
  389. dirLight->computeCascadeFrustums(mainCam.getFrustum(), {&cascadeDistances[0], cascadeCount}, {&cascadeProjMats[0], cascadeCount},
  390. {&cascadeViewMats[0], cascadeCount}, {cascadePlanes.getBegin(), cascadeCount});
  391. for(U cascade = 0; cascade < cascadeCount; ++cascade)
  392. {
  393. cascadeViewProjMats[cascade] = cascadeProjMats[cascade] * Mat4(cascadeViewMats[cascade], Vec4(0.0f, 0.0f, 0.0f, 1.0f));
  394. }
  395. // HZB generation
  396. HzbDirectionalLightInput hzbGenIn;
  397. hzbGenIn.m_cascadeCount = cascadeCount;
  398. hzbGenIn.m_depthBufferRt = getRenderer().getGBuffer().getDepthRt();
  399. hzbGenIn.m_depthBufferRtSize = getRenderer().getInternalResolution();
  400. hzbGenIn.m_cameraProjectionMatrix = ctx.m_matrices.m_projection;
  401. hzbGenIn.m_cameraInverseViewProjectionMatrix = ctx.m_matrices.m_invertedViewProjection;
  402. for(U cascade = 0; cascade < cascadeCount; ++cascade)
  403. {
  404. hzbGenIn.m_cascades[cascade].m_hzbRt = rgraph.newRenderTarget(m_cascadeHzbRtDescrs[cascade]);
  405. hzbGenIn.m_cascades[cascade].m_hzbRtSize = UVec2(m_cascadeHzbRtDescrs[cascade].m_width, m_cascadeHzbRtDescrs[cascade].m_height);
  406. hzbGenIn.m_cascades[cascade].m_viewMatrix = cascadeViewMats[cascade];
  407. hzbGenIn.m_cascades[cascade].m_projectionMatrix = cascadeProjMats[cascade];
  408. hzbGenIn.m_cascades[cascade].m_cascadeMaxDistance = cascadeDistances[cascade];
  409. }
  410. getRenderer().getHzbGenerator().populateRenderGraphDirectionalLight(hzbGenIn, rgraph);
  411. // Create passes per cascade
  412. for(U32 cascade = 0; cascade < cascadeCount; ++cascade)
  413. {
  414. // Vis testing
  415. const Array<F32, kMaxLodCount - 1> lodDistances = {g_lod0MaxDistanceCVar, g_lod1MaxDistanceCVar};
  416. FrustumGpuVisibilityInput visIn;
  417. visIn.m_passesName = generateTempPassName("Shadows: Dir light cascade cascade:%u", cascade);
  418. visIn.m_technique = RenderingTechnique::kDepth;
  419. visIn.m_viewProjectionMatrix = cascadeViewProjMats[cascade];
  420. visIn.m_lodReferencePoint = ctx.m_matrices.m_cameraTransform.getTranslationPart().xyz();
  421. visIn.m_lodDistances = lodDistances;
  422. visIn.m_hzbRt = &hzbGenIn.m_cascades[cascade].m_hzbRt;
  423. visIn.m_rgraph = &rgraph;
  424. visIn.m_viewportSize = dirLightAtlasViewports[cascade].zw();
  425. GpuVisibilityOutput visOut;
  426. getRenderer().getGpuVisibility().populateRenderGraph(visIn, visOut);
  427. // Draw
  428. createDrawShadowsPass(dirLightAtlasViewports[cascade], cascadeViewProjMats[cascade], cascadeViewMats[cascade], visOut, {},
  429. hzbGenIn.m_cascades[cascade].m_hzbRt, generateTempPassName("Shadows: Dir light cascade:%u", cascade), rgraph);
  430. // Update the texture matrix to point to the correct region in the atlas
  431. ctx.m_dirLightTextureMatrices[cascade] = createSpotLightTextureMatrix(dirLightAtlasViewports[cascade]) * cascadeViewProjMats[cascade];
  432. ctx.m_dirLightFarPlanes[cascade] = cascadePlanes[cascade][FrustumPlaneType::kFar];
  433. const F32 texelSize = 1.0f / F32(m_atlasTex->getWidth());
  434. if(cascade == 0)
  435. {
  436. ctx.m_dirLightPcfTexelRadius[cascade] = kPcfTexelRadius * texelSize;
  437. }
  438. else
  439. {
  440. // Make the PCF radius proportional to the 1st cascade to make PCF blurring for all cascades look somewhat similar
  441. const F32 cascade0Meters = cascadePlanes[0][FrustumPlaneType::kRight] - cascadePlanes[0][FrustumPlaneType::kLeft];
  442. const F32 cascadeXMeters = cascadePlanes[cascade][FrustumPlaneType::kRight] - cascadePlanes[cascade][FrustumPlaneType::kLeft];
  443. ctx.m_dirLightPcfTexelRadius[cascade] = ctx.m_dirLightPcfTexelRadius[0] * cascade0Meters / cascadeXMeters;
  444. if(ctx.m_dirLightPcfTexelRadius[cascade] < texelSize * 0.5)
  445. {
  446. // Too small, don't bother
  447. ctx.m_dirLightPcfTexelRadius[cascade] = 0.0f;
  448. }
  449. }
  450. }
  451. }
  452. }
  453. BufferView ShadowMapping::createVetVisibilityPass(CString passName, const LightComponent& lightc, const GpuVisibilityOutput& visOut,
  454. RenderGraphBuilder& rgraph) const
  455. {
  456. BufferView clearTileIndirectArgs;
  457. clearTileIndirectArgs = GpuVisibleTransientMemoryPool::getSingleton().allocateStructuredBuffer<DrawIndirectArgs>(1);
  458. NonGraphicsRenderPass& pass = rgraph.newNonGraphicsRenderPass(passName);
  459. // The shader doesn't actually write to the handle but have it as a write dependency for the drawer to correctly wait for this pass
  460. pass.newBufferDependency(visOut.m_dependency, BufferUsageBit::kUavCompute);
  461. pass.setWork([this, &lightc, hashBuff = visOut.m_visiblesHashBuffer, mdiBuff = visOut.m_legacy.m_mdiDrawCountsBuffer, clearTileIndirectArgs,
  462. dispatchMeshIndirectArgs = visOut.m_mesh.m_dispatchMeshIndirectArgsBuffer,
  463. drawIndirectArgs = visOut.m_mesh.m_drawIndirectArgs](RenderPassWorkContext& rpass) {
  464. ANKI_TRACE_SCOPED_EVENT(ShadowmappingVet);
  465. CommandBuffer& cmdb = *rpass.m_commandBuffer;
  466. cmdb.bindShaderProgram(m_vetVisibilityGrProg.get());
  467. const UVec4 lightIndex(lightc.getGpuSceneLightAllocation().getIndex());
  468. cmdb.setFastConstants(&lightIndex, sizeof(lightIndex));
  469. cmdb.bindSrv(0, 0, hashBuff);
  470. cmdb.bindUav(0, 0, mdiBuff.isValid() ? mdiBuff : BufferView(&getRenderer().getDummyBuffer()).setRange(sizeof(U32)));
  471. cmdb.bindUav(1, 0, GpuSceneArrays::Light::getSingleton().getBufferView());
  472. cmdb.bindUav(2, 0, GpuSceneArrays::LightVisibleRenderablesHash::getSingleton().getBufferView());
  473. cmdb.bindUav(3, 0, clearTileIndirectArgs);
  474. cmdb.bindUav(4, 0,
  475. dispatchMeshIndirectArgs.isValid() ? dispatchMeshIndirectArgs
  476. : BufferView(&getRenderer().getDummyBuffer()).setRange(sizeof(DispatchIndirectArgs)));
  477. cmdb.bindUav(5, 0,
  478. drawIndirectArgs.isValid() ? drawIndirectArgs : BufferView(&getRenderer().getDummyBuffer()).setRange(sizeof(DrawIndirectArgs)));
  479. ANKI_ASSERT(RenderStateBucketContainer::getSingleton().getBucketCount(RenderingTechnique::kDepth) <= 64 && "TODO");
  480. cmdb.dispatchCompute(1, 1, 1);
  481. });
  482. return clearTileIndirectArgs;
  483. }
  484. void ShadowMapping::createDrawShadowsPass(const UVec4& viewport, const Mat4& viewProjMat, const Mat3x4& viewMat, const GpuVisibilityOutput& visOut,
  485. const BufferView& clearTileIndirectArgs, const RenderTargetHandle hzbRt, CString passName,
  486. RenderGraphBuilder& rgraph)
  487. {
  488. ShadowSubpassInfo spass;
  489. spass.m_clearTileIndirectArgs = clearTileIndirectArgs;
  490. spass.m_hzbRt = hzbRt;
  491. spass.m_viewMat = viewMat;
  492. spass.m_viewport = viewport;
  493. spass.m_viewProjMat = viewProjMat;
  494. createDrawShadowsPass({&spass, 1}, visOut, passName, rgraph);
  495. }
  496. void ShadowMapping::createDrawShadowsPass(ConstWeakArray<ShadowSubpassInfo> subpasses_, const GpuVisibilityOutput& visOut, CString passName,
  497. RenderGraphBuilder& rgraph)
  498. {
  499. WeakArray<ShadowSubpassInfo> subpasses;
  500. newArray<ShadowSubpassInfo>(getRenderer().getFrameMemoryPool(), subpasses_.getSize(), subpasses);
  501. memcpy(subpasses.getBegin(), subpasses_.getBegin(), subpasses.getSizeInBytes());
  502. // Create the pass
  503. GraphicsRenderPass& pass = rgraph.newGraphicsRenderPass(passName);
  504. GraphicsRenderPassTargetDesc smRti(m_runCtx.m_rt);
  505. smRti.m_loadOperation = RenderTargetLoadOperation::kLoad;
  506. smRti.m_clearValue.m_depthStencil.m_depth = 1.0f;
  507. smRti.m_subresource.m_depthStencilAspect = DepthStencilAspectBit::kDepth;
  508. pass.setRenderpassInfo({}, &smRti);
  509. pass.newBufferDependency(visOut.m_dependency, BufferUsageBit::kIndirectDraw);
  510. pass.newTextureDependency(m_runCtx.m_rt, TextureUsageBit::kRtvDsvWrite);
  511. pass.setWork([this, visOut, subpasses](RenderPassWorkContext& rgraphCtx) {
  512. ANKI_TRACE_SCOPED_EVENT(ShadowMapping);
  513. CommandBuffer& cmdb = *rgraphCtx.m_commandBuffer;
  514. for(U32 i = 0; i < subpasses.getSize(); ++i)
  515. {
  516. const ShadowSubpassInfo& spass = subpasses[i];
  517. cmdb.setViewport(spass.m_viewport[0], spass.m_viewport[1], spass.m_viewport[2], spass.m_viewport[3]);
  518. // Clear the tile
  519. {
  520. cmdb.bindShaderProgram(m_clearDepthGrProg.get());
  521. cmdb.setDepthCompareOperation(CompareOperation::kAlways);
  522. if(spass.m_clearTileIndirectArgs.isValid())
  523. {
  524. cmdb.drawIndirect(PrimitiveTopology::kTriangles, spass.m_clearTileIndirectArgs);
  525. }
  526. else
  527. {
  528. cmdb.draw(PrimitiveTopology::kTriangles, 3);
  529. }
  530. cmdb.setDepthCompareOperation(CompareOperation::kLess);
  531. }
  532. // Set state
  533. cmdb.setPolygonOffset(kShadowsPolygonOffsetFactor, kShadowsPolygonOffsetUnits);
  534. RenderableDrawerArguments args;
  535. args.m_renderingTechinuqe = RenderingTechnique::kDepth;
  536. args.m_viewMatrix = spass.m_viewMat;
  537. args.m_cameraTransform = spass.m_viewMat.invertTransformation();
  538. args.m_viewProjectionMatrix = spass.m_viewProjMat;
  539. args.m_previousViewProjectionMatrix = Mat4::getIdentity(); // Don't care
  540. args.m_sampler = getRenderer().getSamplers().m_trilinearRepeat.get();
  541. args.m_viewport = UVec4(spass.m_viewport[0], spass.m_viewport[1], spass.m_viewport[2], spass.m_viewport[3]);
  542. args.fill(visOut);
  543. getRenderer().getRenderableDrawer().drawMdi(args, cmdb);
  544. cmdb.setPolygonOffset(0.0, 0.0);
  545. }
  546. });
  547. }
  548. } // end namespace anki