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