3
0

ProjectedShadowFeatureProcessor.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Shadows/ProjectedShadowFeatureProcessor.h>
  9. #include <AzCore/Math/MatrixUtils.h>
  10. #include <AzCore/Name/NameDictionary.h>
  11. #include <Math/GaussianMathFilter.h>
  12. #include <Atom/RHI/MultiDeviceDrawPacketBuilder.h>
  13. #include <Atom/RHI/RHISystemInterface.h>
  14. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  15. #include <Atom/RPI.Public/RenderPipeline.h>
  16. #include <Atom/RPI.Public/RPISystemInterface.h>
  17. #include <Atom/RPI.Public/Scene.h>
  18. #include <Atom/RPI.Public/View.h>
  19. #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
  20. #include <Atom/RPI.Public/Pass/PassSystem.h>
  21. #include <Atom/RPI.Public/Pass/PassFilter.h>
  22. #include <Atom/RPI.Public/Shader/Shader.h>
  23. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  24. #include <Atom/Feature/Mesh/MeshCommon.h>
  25. #include <CoreLights/Shadow.h>
  26. namespace AZ::Render
  27. {
  28. namespace
  29. {
  30. AZ_CVAR(
  31. bool,
  32. r_cullShadowmapOutsideViewFrustum,
  33. true,
  34. nullptr,
  35. AZ::ConsoleFunctorFlags::DontReplicate | AZ::ConsoleFunctorFlags::DontDuplicate,
  36. "If set, enables filtering of shadow maps that are outside of the view frustum.");
  37. bool IsShadowmapCullingEnabled()
  38. {
  39. bool cullShadowmapOutsideViewFrustum = true;
  40. if (auto* console = AZ::Interface<AZ::IConsole>::Get())
  41. {
  42. console->GetCvarValue("r_cullShadowmapOutsideViewFrustum", cullShadowmapOutsideViewFrustum);
  43. }
  44. return cullShadowmapOutsideViewFrustum;
  45. }
  46. bool IsLightInsideAnyViewFrustum(AZStd::span<AZ::Frustum> viewFrustums, const AZ::Vector3& lightPosition, float attenuationRadius)
  47. {
  48. return std::any_of(
  49. viewFrustums.begin(),
  50. viewFrustums.end(),
  51. [lightPosition, attenuationRadius](const AZ::Frustum& viewFrustum)
  52. {
  53. return viewFrustum.IntersectSphere(lightPosition, attenuationRadius) != AZ::IntersectResult::Exterior;
  54. });
  55. }
  56. } // namespace
  57. void ProjectedShadowFeatureProcessor::Reflect(ReflectContext* context)
  58. {
  59. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  60. {
  61. serializeContext
  62. ->Class<ProjectedShadowFeatureProcessor, FeatureProcessor>()
  63. ->Version(0);
  64. }
  65. }
  66. void ProjectedShadowFeatureProcessor::Activate()
  67. {
  68. const RHI::ShaderResourceGroupLayout* viewSrgLayout = RPI::RPISystemInterface::Get()->GetViewSrgLayout().get();
  69. GpuBufferHandler::Descriptor desc;
  70. desc.m_bufferName = "ProjectedShadowBuffer";
  71. desc.m_bufferSrgName = "m_projectedShadows";
  72. desc.m_elementCountSrgName = "";
  73. desc.m_elementSize = sizeof(ShadowData);
  74. desc.m_srgLayout = viewSrgLayout;
  75. m_shadowBufferHandler = GpuBufferHandler(desc);
  76. desc.m_bufferName = "ProjectedFilterParamsBuffer";
  77. desc.m_bufferSrgName = "m_projectedFilterParams";
  78. desc.m_elementCountSrgName = "";
  79. desc.m_elementSize = sizeof(EsmShadowmapsPass::FilterParameter);
  80. desc.m_srgLayout = viewSrgLayout;
  81. m_filterParamBufferHandler = GpuBufferHandler(desc);
  82. EnableSceneNotification();
  83. }
  84. void ProjectedShadowFeatureProcessor::Deactivate()
  85. {
  86. DisableSceneNotification();
  87. m_shadowData.Clear();
  88. m_shadowBufferHandler.Release();
  89. m_filterParamBufferHandler.Release();
  90. m_shadowProperties.Clear();
  91. m_projectedShadowmapsPasses.clear();
  92. m_esmShadowmapsPasses.clear();
  93. m_primaryProjectedShadowmapsPass = nullptr;
  94. if (m_primaryEsmShadowmapsPass)
  95. {
  96. m_primaryEsmShadowmapsPass->SetEnabledComputation(false);
  97. m_primaryEsmShadowmapsPass = nullptr;
  98. }
  99. }
  100. ProjectedShadowFeatureProcessor::ShadowId ProjectedShadowFeatureProcessor::AcquireShadow()
  101. {
  102. // Reserve a new slot in m_shadowData
  103. size_t index = m_shadowData.Reserve();
  104. if (index >= std::numeric_limits<ShadowId::IndexType>::max())
  105. {
  106. m_shadowData.Release(index);
  107. return ShadowId::Null;
  108. }
  109. ShadowId id = ShadowId(aznumeric_cast<ShadowId::IndexType>(index));
  110. InitializeShadow(id);
  111. return id;
  112. }
  113. void ProjectedShadowFeatureProcessor::ReleaseShadow(ShadowId id)
  114. {
  115. if (id.IsValid())
  116. {
  117. auto& shadowProperty = GetShadowPropertyFromShadowId(id);
  118. if (m_primaryProjectedShadowmapsPass)
  119. {
  120. m_primaryProjectedShadowmapsPass->QueueRemoveChild(shadowProperty.m_shadowmapPass);
  121. }
  122. m_shadowProperties.RemoveData(&shadowProperty);
  123. m_shadowData.Release(id.GetIndex());
  124. }
  125. m_filterParameterNeedsUpdate = true;
  126. m_shadowmapPassNeedsUpdate = true;
  127. }
  128. void ProjectedShadowFeatureProcessor::SetShadowTransform(ShadowId id, Transform transform)
  129. {
  130. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  131. shadowProperty.m_desc.m_transform = transform;
  132. UpdateShadowView(shadowProperty);
  133. }
  134. void ProjectedShadowFeatureProcessor::SetNearFarPlanes(ShadowId id, float nearPlaneDistance, float farPlaneDistance)
  135. {
  136. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetFrontBackPlanes().");
  137. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  138. shadowProperty.m_desc.m_nearPlaneDistance = GetMax(nearPlaneDistance, 0.0001f);
  139. shadowProperty.m_desc.m_farPlaneDistance = GetMax(farPlaneDistance, nearPlaneDistance + 0.0001f);
  140. UpdateShadowView(shadowProperty);
  141. }
  142. void ProjectedShadowFeatureProcessor::SetAspectRatio(ShadowId id, float aspectRatio)
  143. {
  144. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetAspectRatio().");
  145. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  146. shadowProperty.m_desc.m_aspectRatio = aspectRatio;
  147. UpdateShadowView(shadowProperty);
  148. }
  149. void ProjectedShadowFeatureProcessor::SetFieldOfViewY(ShadowId id, float fieldOfViewYRadians)
  150. {
  151. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetFieldOfViewY().");
  152. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  153. shadowProperty.m_desc.m_fieldOfViewYRadians = fieldOfViewYRadians;
  154. UpdateShadowView(shadowProperty);
  155. }
  156. void ProjectedShadowFeatureProcessor::SetShadowBias(ShadowId id, float bias)
  157. {
  158. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetShadowBias().");
  159. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  160. shadowProperty.m_bias = bias;
  161. }
  162. void ProjectedShadowFeatureProcessor::SetNormalShadowBias(ShadowId id, float normalShadowBias)
  163. {
  164. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetNormalShadowBias().");
  165. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(id.GetIndex());
  166. shadowData.m_normalShadowBias = normalShadowBias;
  167. m_deviceBufferNeedsUpdate = true;
  168. }
  169. void ProjectedShadowFeatureProcessor::SetShadowmapMaxResolution(ShadowId id, ShadowmapSize size)
  170. {
  171. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetShadowmapMaxResolution().");
  172. AZ_Assert(size != ShadowmapSize::None, "Shadowmap size cannot be set to None, remove the shadow instead.");
  173. FilterParameter& esmData = m_shadowData.GetElement<FilterParamIndex>(id.GetIndex());
  174. esmData.m_shadowmapSize = aznumeric_cast<uint32_t>(size);
  175. m_deviceBufferNeedsUpdate = true;
  176. m_shadowmapPassNeedsUpdate = true;
  177. m_filterParameterNeedsUpdate = true;
  178. }
  179. void ProjectedShadowFeatureProcessor::SetEsmExponent(ShadowId id, float exponent)
  180. {
  181. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetEsmExponent().");
  182. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(id.GetIndex());
  183. shadowData.m_esmExponent = exponent;
  184. m_deviceBufferNeedsUpdate = true;
  185. }
  186. void ProjectedShadowFeatureProcessor::SetShadowFilterMethod(ShadowId id, ShadowFilterMethod method)
  187. {
  188. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetShadowFilterMethod().");
  189. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  190. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(id.GetIndex());
  191. shadowData.m_shadowFilterMethod = aznumeric_cast<uint32_t>(method);
  192. UpdateShadowView(shadowProperty);
  193. m_shadowmapPassNeedsUpdate = true;
  194. m_filterParameterNeedsUpdate = true;
  195. }
  196. void ProjectedShadowFeatureProcessor::SetFilteringSampleCount(ShadowId id, uint16_t count)
  197. {
  198. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetFilteringSampleCount().");
  199. AZ_Warning("ProjectedShadowFeatureProcessor", count <= Shadow::MaxPcfSamplingCount, "Sampling count exceed the limit.");
  200. count = GetMin(count, Shadow::MaxPcfSamplingCount);
  201. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(id.GetIndex());
  202. shadowData.m_filteringSampleCount = count;
  203. m_deviceBufferNeedsUpdate = true;
  204. }
  205. void ProjectedShadowFeatureProcessor::SetUseCachedShadows(ShadowId id, bool useCachedShadows)
  206. {
  207. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetUseCachedShadows().");
  208. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  209. shadowProperty.m_useCachedShadows = useCachedShadows;
  210. m_shadowmapPassNeedsUpdate = true;
  211. }
  212. void ProjectedShadowFeatureProcessor::SetShadowProperties(ShadowId id, const ProjectedShadowDescriptor& descriptor)
  213. {
  214. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::SetShadowProperties().");
  215. ShadowProperty& shadowProperty = GetShadowPropertyFromShadowId(id);
  216. if (shadowProperty.m_desc != descriptor)
  217. {
  218. shadowProperty.m_desc = descriptor;
  219. UpdateShadowView(shadowProperty);
  220. // Don't set m_shadowmapPassNeedsUpdate=true here because that would cause the pass to rebuild every time a light moves
  221. // Don't set m_filterParameterNeedsUpdate=true here because that's handled by UpdateShadowView(), and only when filtering is relevant
  222. }
  223. }
  224. auto ProjectedShadowFeatureProcessor::GetShadowProperties(ShadowId id) -> const ProjectedShadowDescriptor&
  225. {
  226. AZ_Assert(id.IsValid(), "Invalid ShadowId passed to ProjectedShadowFeatureProcessor::GetShadowProperties().");
  227. return GetShadowPropertyFromShadowId(id).m_desc;
  228. }
  229. void ProjectedShadowFeatureProcessor::UpdateShadowView(ShadowProperty& shadowProperty)
  230. {
  231. const ProjectedShadowDescriptor& desc = shadowProperty.m_desc;
  232. float nearDist = desc.m_nearPlaneDistance;
  233. float farDist = desc.m_farPlaneDistance;
  234. // Adjust the near plane if it's too close to ensure accuracy.
  235. constexpr float NearFarRatio = 1000.0f;
  236. const float minDist = desc.m_farPlaneDistance / NearFarRatio;
  237. nearDist = GetMax(minDist, nearDist);
  238. Matrix4x4 viewToClipMatrix;
  239. MakePerspectiveFovMatrixRH(
  240. viewToClipMatrix,
  241. GetMax(desc.m_fieldOfViewYRadians, MinimumFieldOfView),
  242. desc.m_aspectRatio,
  243. nearDist,
  244. farDist);
  245. RPI::ViewPtr view = shadowProperty.m_shadowmapView;
  246. view->SetViewToClipMatrix(viewToClipMatrix);
  247. view->SetCameraTransform(Matrix3x4::CreateFromTransform(desc.m_transform));
  248. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(shadowProperty.m_shadowId.GetIndex());
  249. // Adjust the manually set bias to a more appropriate range for the shader. Scale the bias by the
  250. // near plane so that the bias appears consistent as other light properties change.
  251. shadowData.m_bias = nearDist * shadowProperty.m_bias * 0.01f;
  252. FilterParameter& esmData = m_shadowData.GetElement<FilterParamIndex>(shadowProperty.m_shadowId.GetIndex());
  253. // Set parameters to calculate linear depth if ESM is used.
  254. esmData.m_n_f_n = nearDist / (farDist - nearDist);
  255. esmData.m_n_f = nearDist - farDist;
  256. esmData.m_f = farDist;
  257. esmData.m_isEnabled = FilterMethodIsEsm(shadowData);
  258. m_filterParameterNeedsUpdate = m_filterParameterNeedsUpdate || esmData.m_isEnabled;
  259. // Set depth bias matrix.
  260. const Matrix4x4& worldToLightClipMatrix = view->GetWorldToClipMatrix();
  261. const Matrix4x4 depthBiasMatrix = Shadow::GetClipToShadowmapTextureMatrix() * worldToLightClipMatrix;
  262. shadowData.m_depthBiasMatrix = depthBiasMatrix;
  263. shadowData.m_unprojectConstants[0] = view->GetViewToClipMatrix().GetRow(2).GetElement(2);
  264. shadowData.m_unprojectConstants[1] = view->GetViewToClipMatrix().GetRow(2).GetElement(3);
  265. if (shadowProperty.m_useCachedShadows && m_primaryProjectedShadowmapsPass)
  266. {
  267. shadowProperty.m_shadowmapPass->ForceRenderNextFrame();
  268. }
  269. m_deviceBufferNeedsUpdate = true;
  270. }
  271. void ProjectedShadowFeatureProcessor::InitializeShadow(ShadowId shadowId)
  272. {
  273. m_deviceBufferNeedsUpdate = true;
  274. m_shadowmapPassNeedsUpdate = true;
  275. // Reserve a slot in m_shadowProperties, and store that index in m_shadowData's second vector
  276. uint16_t shadowPropertyIndex = m_shadowProperties.GetFreeSlotIndex();
  277. m_shadowData.GetElement<ShadowPropertyIdIndex>(shadowId.GetIndex()) = shadowPropertyIndex;
  278. ShadowProperty& shadowProperty = m_shadowProperties.GetData(shadowPropertyIndex);
  279. shadowProperty.m_shadowId = shadowId;
  280. Name viewName(AZStd::string::format("ProjectedShadowView (shadowId:%d)", shadowId.GetIndex()));
  281. shadowProperty.m_shadowmapView = RPI::View::CreateView(viewName, RPI::View::UsageShadow);
  282. UpdateShadowView(shadowProperty);
  283. if (m_primaryProjectedShadowmapsPass)
  284. {
  285. shadowProperty.m_shadowmapPass = CreateShadowmapPass(shadowId.GetIndex());
  286. m_primaryProjectedShadowmapsPass->QueueAddChild(shadowProperty.m_shadowmapPass);
  287. }
  288. }
  289. void ProjectedShadowFeatureProcessor::OnRenderPipelineChanged([[maybe_unused]] RPI::RenderPipeline* renderPipeline,
  290. [[maybe_unused]] RPI::SceneNotification::RenderPipelineChangeType changeType)
  291. {
  292. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Removed)
  293. {
  294. // Check for cases where the pipeline containing the primary render passes has been removed, which means the pointers
  295. // to those passes are no longer valid.
  296. CheckRemovePrimaryPasses(renderPipeline);
  297. }
  298. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Removed || changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  299. {
  300. RemoveCachedPasses(renderPipeline);
  301. }
  302. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Added || changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  303. {
  304. CachePasses(renderPipeline);
  305. }
  306. // Check to see if the primary passes have changed, and if so removes the children from the old primary pass and creates them
  307. // on the new primary pass. This is necessary if an earlier render pipeline adds or removes references to the shadow map
  308. // passes, forcing this feature processor to change which pipeline it uses to render shadows for all pipelines in the scene.
  309. UpdatePrimaryPasses();
  310. }
  311. void ProjectedShadowFeatureProcessor::CheckRemovePrimaryPasses(RPI::RenderPipeline* renderPipeline)
  312. {
  313. auto projItr = m_projectedShadowmapsPasses.find(renderPipeline);
  314. if (projItr != m_projectedShadowmapsPasses.end() && projItr->second == m_primaryProjectedShadowmapsPass)
  315. {
  316. m_primaryProjectedShadowmapsPass = nullptr;
  317. }
  318. auto esmItr = m_esmShadowmapsPasses.find(renderPipeline);
  319. if (esmItr != m_esmShadowmapsPasses.end() && esmItr->second == m_primaryEsmShadowmapsPass)
  320. {
  321. m_primaryEsmShadowmapsPass = nullptr;
  322. }
  323. }
  324. void ProjectedShadowFeatureProcessor::RemoveCachedPasses(RPI::RenderPipeline* renderPipeline)
  325. {
  326. m_projectedShadowmapsPasses.erase(renderPipeline);
  327. m_esmShadowmapsPasses.erase(renderPipeline);
  328. // Handle the case where the render pipeline containing the primary projected shadow pass is changed, and the
  329. // projected shadow pass was altered or removed as part of that change.
  330. if (renderPipeline == m_primaryShadowPipeline && m_primaryProjectedShadowmapsPass != nullptr)
  331. {
  332. RPI::PassFilter projectedPassFilter = RPI::PassFilter::CreateWithTemplateName(AZ_NAME_LITERAL("ProjectedShadowmapsTemplate"), renderPipeline);
  333. bool primaryPassChanged = true;
  334. RPI::PassSystemInterface::Get()->ForEachPass(projectedPassFilter,
  335. [&](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  336. {
  337. primaryPassChanged = m_primaryProjectedShadowmapsPass != pass;
  338. return RPI::PassFilterExecutionFlow::StopVisitingPasses;
  339. }
  340. );
  341. if (primaryPassChanged)
  342. {
  343. m_primaryProjectedShadowmapsPass = nullptr;
  344. // Check to see if the esm pass still exists on this pipeline. If so, turn it off before setting the pointer to null.
  345. RPI::PassFilter esmPassFilter = RPI::PassFilter::CreateWithTemplateName(AZ_NAME_LITERAL("EsmShadowmapsTemplate"), renderPipeline);
  346. RPI::PassSystemInterface::Get()->ForEachPass(esmPassFilter,
  347. [&](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  348. {
  349. if (pass == m_primaryEsmShadowmapsPass)
  350. {
  351. m_primaryEsmShadowmapsPass->SetEnabledComputation(false);
  352. }
  353. return RPI::PassFilterExecutionFlow::StopVisitingPasses;
  354. }
  355. );
  356. m_primaryEsmShadowmapsPass = nullptr;
  357. }
  358. }
  359. }
  360. void ProjectedShadowFeatureProcessor::CachePasses(RPI::RenderPipeline* renderPipeline)
  361. {
  362. // Find the Projected Shadow pass in a given render pipeline and update it.
  363. RPI::PassFilter projectedPassFilter = RPI::PassFilter::CreateWithTemplateName(AZ_NAME_LITERAL("ProjectedShadowmapsTemplate"), renderPipeline);
  364. RPI::PassSystemInterface::Get()->ForEachPass(projectedPassFilter,
  365. [&](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  366. {
  367. if (m_projectedShadowmapsPasses.contains(renderPipeline))
  368. {
  369. AZ_Error("ProjectedShadowFeatureProcessor", false, "Found multiple projected shadowmap passes in pipeline.");
  370. return RPI::PassFilterExecutionFlow::StopVisitingPasses;
  371. }
  372. ProjectedShadowmapsPass* shadowmapPass = static_cast<ProjectedShadowmapsPass*>(pass);
  373. shadowmapPass->SetAtlasAttachmentImage(m_atlasImage);
  374. m_projectedShadowmapsPasses[renderPipeline] = shadowmapPass;
  375. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses; // continue to check for multiple (error case)
  376. }
  377. );
  378. // Find the ESM shadow pass in a given render pipeline and update it.
  379. RPI::PassFilter esmPassFilter = RPI::PassFilter::CreateWithTemplateName(AZ_NAME_LITERAL("EsmShadowmapsTemplate"), renderPipeline);
  380. RPI::PassSystemInterface::Get()->ForEachPass(esmPassFilter,
  381. [&](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  382. {
  383. EsmShadowmapsPass* esmShadowmapsPass = static_cast<EsmShadowmapsPass*>(pass);
  384. if (esmShadowmapsPass->GetLightTypeName() == Name("projected"))
  385. {
  386. if (m_esmShadowmapsPasses.contains(renderPipeline))
  387. {
  388. AZ_Error("ProjectedShadowFeatureProcessor", false, "Found multiple esm shadowmap passes for projected shadows in pipeline.");
  389. return RPI::PassFilterExecutionFlow::StopVisitingPasses;
  390. }
  391. m_esmShadowmapsPasses[renderPipeline] = esmShadowmapsPass;
  392. if (esmShadowmapsPass != m_primaryEsmShadowmapsPass)
  393. {
  394. esmShadowmapsPass->SetEnabledComputation(false);
  395. }
  396. esmShadowmapsPass->SetAtlasAttachmentImage(m_esmAtlasImage);
  397. }
  398. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses; // continue to check for multiple (error case)
  399. }
  400. );
  401. }
  402. void ProjectedShadowFeatureProcessor::UpdatePrimaryPasses()
  403. {
  404. // Find a new m_primaryProjectedShadowmapsPass. This needs to be the first ProjectedShadowmapsPass
  405. // in the list of pipelines to ensure it calculates the shadows before any other pipelines need it.
  406. bool found = false;
  407. for (RPI::RenderPipelinePtr pipeline : GetParentScene()->GetRenderPipelines())
  408. {
  409. auto itr = m_projectedShadowmapsPasses.find(pipeline.get());
  410. if (itr != m_projectedShadowmapsPasses.end())
  411. {
  412. ProjectedShadowmapsPass* pass = itr->second;
  413. if (m_primaryProjectedShadowmapsPass != pass)
  414. {
  415. if (m_primaryProjectedShadowmapsPass != nullptr)
  416. {
  417. for (RPI::Ptr<RPI::Pass> child : m_primaryProjectedShadowmapsPass->GetChildren())
  418. {
  419. child->QueueForRemoval();
  420. }
  421. }
  422. m_primaryProjectedShadowmapsPass = pass;
  423. for (auto& shadowProperty : m_shadowProperties.GetDataVector())
  424. {
  425. size_t shadowIndex = shadowProperty.m_shadowId.GetIndex();
  426. shadowProperty.m_shadowmapPass = CreateShadowmapPass(shadowIndex);
  427. m_primaryProjectedShadowmapsPass->QueueAddChild(shadowProperty.m_shadowmapPass);
  428. }
  429. }
  430. m_primaryShadowPipeline = pipeline.get();
  431. found = true;
  432. break;
  433. }
  434. }
  435. if (!found)
  436. {
  437. m_primaryProjectedShadowmapsPass = nullptr;
  438. m_primaryShadowPipeline = nullptr;
  439. }
  440. if (found && m_esmShadowmapsPasses.contains(m_primaryProjectedShadowmapsPass->GetRenderPipeline()))
  441. {
  442. // Update the primary esm pass to be the one that's on the same pipeline as the primary projected shadowmaps pass.
  443. EsmShadowmapsPass* firstEsmShadomapsPass = m_esmShadowmapsPasses.at(m_primaryProjectedShadowmapsPass->GetRenderPipeline());
  444. if (firstEsmShadomapsPass != m_primaryEsmShadowmapsPass)
  445. {
  446. if (m_primaryEsmShadowmapsPass != nullptr)
  447. {
  448. m_primaryEsmShadowmapsPass->SetEnabledComputation(false);
  449. }
  450. m_primaryEsmShadowmapsPass = firstEsmShadomapsPass;
  451. // This will enable computation of the primary esm shadow pass later if necessary.
  452. m_filterParameterNeedsUpdate = m_shadowProperties.GetDataCount() > 0;
  453. }
  454. }
  455. else if (m_primaryEsmShadowmapsPass != nullptr)
  456. {
  457. // Either there's no primary projected shadowmaps pass, or there is but there's no esm pass on the same pipeline, so disable
  458. // the primary esm pass if necessary.
  459. RPI::PassFilter esmPassFilter = RPI::PassFilter::CreateWithTemplateName(AZ_NAME_LITERAL("EsmShadowmapsTemplate"), m_primaryShadowPipeline);
  460. RPI::PassSystemInterface::Get()->ForEachPass(esmPassFilter,
  461. [&](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  462. {
  463. if (pass == m_primaryEsmShadowmapsPass)
  464. {
  465. m_primaryEsmShadowmapsPass->SetEnabledComputation(false);
  466. }
  467. return RPI::PassFilterExecutionFlow::StopVisitingPasses;
  468. }
  469. );
  470. m_primaryEsmShadowmapsPass = nullptr;
  471. }
  472. if (m_primaryProjectedShadowmapsPass && !m_clearShadowDrawPacket)
  473. {
  474. CreateClearShadowDrawPacket();
  475. }
  476. m_shadowmapPassNeedsUpdate = true;
  477. }
  478. void ProjectedShadowFeatureProcessor::UpdateFilterParameters()
  479. {
  480. if (m_filterParameterNeedsUpdate)
  481. {
  482. UpdateEsmPassEnabled();
  483. SetFilterParameterToPass();
  484. m_filterParameterNeedsUpdate = false;
  485. }
  486. }
  487. void ProjectedShadowFeatureProcessor::UpdateEsmPassEnabled()
  488. {
  489. if (m_primaryEsmShadowmapsPass == nullptr)
  490. {
  491. return;
  492. }
  493. bool anyShadowsUseEsm = false;
  494. for (const auto& shadowProperty : m_shadowProperties.GetDataVector())
  495. {
  496. FilterParameter& esmData = m_shadowData.GetElement<FilterParamIndex>(shadowProperty.m_shadowId.GetIndex());
  497. if (esmData.m_isEnabled)
  498. {
  499. anyShadowsUseEsm = true;
  500. break;
  501. }
  502. }
  503. m_primaryEsmShadowmapsPass->SetEnabledComputation(anyShadowsUseEsm);
  504. }
  505. void ProjectedShadowFeatureProcessor::SetFilterParameterToPass()
  506. {
  507. static uint32_t nameIndex = 0;
  508. // Create index table buffer.
  509. // [GFX TODO ATOM-14851] Should not be creating a new buffer here, just map the data or orphan with new data.
  510. const AZStd::string indexTableBufferName = AZStd::string::format("IndexTableBuffer(Projected) %d", nameIndex++);
  511. const Data::Instance<RPI::Buffer> indexTableBuffer = m_atlas.CreateShadowmapIndexTableBuffer(indexTableBufferName);
  512. m_filterParamBufferHandler.UpdateBuffer(m_shadowData.GetRawData<FilterParamIndex>(), static_cast<uint32_t>(m_shadowData.GetSize()));
  513. if (m_primaryEsmShadowmapsPass)
  514. {
  515. m_primaryEsmShadowmapsPass->SetShadowmapIndexTableBuffer(indexTableBuffer);
  516. m_primaryEsmShadowmapsPass->SetFilterParameterBuffer(m_filterParamBufferHandler.GetBuffer());
  517. }
  518. }
  519. void ProjectedShadowFeatureProcessor::Simulate(const FeatureProcessor::SimulatePacket& /*packet*/)
  520. {
  521. AZ_PROFILE_SCOPE(RPI, "ProjectedShadowFeatureProcessor: Simulate");
  522. if (m_shadowmapPassNeedsUpdate && m_primaryProjectedShadowmapsPass)
  523. {
  524. UpdateAtlas();
  525. UpdateShadowPasses();
  526. auto& shadowProperties = m_shadowProperties.GetDataVector();
  527. for (const auto& shadowProperty : shadowProperties)
  528. {
  529. const int16_t shadowIndexInSrg = shadowProperty.m_shadowId.GetIndex();
  530. ShadowData& shadowData = m_shadowData.GetElement<ShadowDataIndex>(shadowIndexInSrg);
  531. FilterParameter& filterData = m_shadowData.GetElement<FilterParamIndex>(shadowIndexInSrg);
  532. const ShadowmapAtlas::Origin origin = m_atlas.GetOrigin(shadowIndexInSrg);
  533. shadowData.m_shadowmapArraySlice = origin.m_arraySlice;
  534. filterData.m_shadowmapOriginInSlice = origin.m_originInSlice;
  535. m_deviceBufferNeedsUpdate = true;
  536. }
  537. if (m_primaryEsmShadowmapsPass != nullptr)
  538. {
  539. m_primaryEsmShadowmapsPass->QueueForBuildAndInitialization();
  540. }
  541. m_shadowmapPassNeedsUpdate = false;
  542. }
  543. // This has to be called after UpdateShadowmapSizes().
  544. UpdateFilterParameters();
  545. if (m_deviceBufferNeedsUpdate)
  546. {
  547. m_shadowBufferHandler.UpdateBuffer(m_shadowData.GetRawData<ShadowDataIndex>(), static_cast<uint32_t>(m_shadowData.GetSize()));
  548. m_deviceBufferNeedsUpdate = false;
  549. }
  550. // Turn off cached esm shadow maps for next frame
  551. for (const auto& shadowProperty : m_shadowProperties.GetDataVector())
  552. {
  553. if (shadowProperty.m_useCachedShadows)
  554. {
  555. FilterParameter& esmData = m_shadowData.GetElement<FilterParamIndex>(shadowProperty.m_shadowId.GetIndex());
  556. if (esmData.m_isEnabled != 0)
  557. {
  558. esmData.m_isEnabled = false;
  559. m_filterParameterNeedsUpdate = true;
  560. }
  561. }
  562. }
  563. }
  564. void ProjectedShadowFeatureProcessor::PrepareViews(
  565. const PrepareViewsPacket& prepareViewsPacket, AZStd::vector<AZStd::pair<RPI::PipelineViewTag, RPI::ViewPtr>>& outViews)
  566. {
  567. if (m_primaryProjectedShadowmapsPass != nullptr)
  568. {
  569. RPI::RenderPipeline* renderPipeline = m_primaryProjectedShadowmapsPass->GetRenderPipeline();
  570. if (renderPipeline)
  571. {
  572. AZStd::vector<AZ::Frustum> mainViewFrustums;
  573. for (const auto& [view, viewTag] : prepareViewsPacket.m_persistentViews)
  574. {
  575. AZ::Frustum viewFrustum = AZ::Frustum::CreateFromMatrixColumnMajor(view->GetWorldToClipMatrix());
  576. mainViewFrustums.push_back(viewFrustum);
  577. }
  578. bool cullShadowmapOutsideViewFrustum = IsShadowmapCullingEnabled();
  579. auto& shadowProperties = m_shadowProperties.GetDataVector();
  580. for (ShadowProperty& shadowProperty : shadowProperties)
  581. {
  582. uint16_t shadowIndex = shadowProperty.m_shadowId.GetIndex();
  583. const FilterParameter& filterData = m_shadowData.GetElement<FilterParamIndex>(shadowIndex);
  584. if (filterData.m_shadowmapSize == aznumeric_cast<uint32_t>(ShadowmapSize::None))
  585. {
  586. continue;
  587. }
  588. auto lightPosition = shadowProperty.m_desc.m_transform.GetTranslation();
  589. if (cullShadowmapOutsideViewFrustum &&
  590. !IsLightInsideAnyViewFrustum(mainViewFrustums, lightPosition, shadowProperty.m_desc.m_farPlaneDistance))
  591. {
  592. continue;
  593. }
  594. const RPI::PipelineViewTag& viewTag = shadowProperty.m_shadowmapPass->GetPipelineViewTag();
  595. const RHI::DrawListMask drawListMask = renderPipeline->GetDrawListMask(viewTag);
  596. if (shadowProperty.m_shadowmapView->GetDrawListMask() != drawListMask)
  597. {
  598. shadowProperty.m_shadowmapView->Reset();
  599. shadowProperty.m_shadowmapView->SetDrawListMask(drawListMask);
  600. }
  601. outViews.emplace_back(AZStd::make_pair(viewTag, shadowProperty.m_shadowmapView));
  602. }
  603. }
  604. }
  605. }
  606. void ProjectedShadowFeatureProcessor::Render(const FeatureProcessor::RenderPacket& packet)
  607. {
  608. AZ_PROFILE_SCOPE(RPI, "ProjectedShadowFeatureProcessor: Render");
  609. if (m_primaryProjectedShadowmapsPass != nullptr)
  610. {
  611. for (const RPI::ViewPtr& view : packet.m_views)
  612. {
  613. if (view->GetUsageFlags() & RPI::View::UsageFlags::UsageCamera)
  614. {
  615. RPI::ShaderResourceGroup* srg = view->GetShaderResourceGroup().get();
  616. float shadowMapAtlasSize = static_cast<float>(m_atlas.GetBaseShadowmapSize());
  617. srg->SetConstant(m_shadowmapAtlasSizeIndex, shadowMapAtlasSize);
  618. const float invShadowmapSize = 1.0f / shadowMapAtlasSize;
  619. srg->SetConstant(m_invShadowmapAtlasSizeIndex, invShadowmapSize);
  620. m_shadowBufferHandler.UpdateSrg(srg);
  621. m_filterParamBufferHandler.UpdateSrg(srg);
  622. }
  623. }
  624. }
  625. }
  626. bool ProjectedShadowFeatureProcessor::FilterMethodIsEsm(const ShadowData& shadowData) const
  627. {
  628. return
  629. aznumeric_cast<ShadowFilterMethod>(shadowData.m_shadowFilterMethod) == ShadowFilterMethod::Esm ||
  630. aznumeric_cast<ShadowFilterMethod>(shadowData.m_shadowFilterMethod) == ShadowFilterMethod::EsmPcf;
  631. }
  632. auto ProjectedShadowFeatureProcessor::GetShadowPropertyFromShadowId(ShadowId id) -> ShadowProperty&
  633. {
  634. AZ_Assert(id.IsValid(), "Error: Invalid ShadowId");
  635. uint16_t shadowPropertyId = m_shadowData.GetElement<ShadowPropertyIdIndex>(id.GetIndex());
  636. return m_shadowProperties.GetData(shadowPropertyId);
  637. }
  638. void ProjectedShadowFeatureProcessor::CreateClearShadowDrawPacket()
  639. {
  640. // Force load of shader to clear shadow maps.
  641. const AZStd::string clearShadowShaderFilePath = "Shaders/Shadow/ClearShadow.azshader";
  642. Data::Asset<RPI::ShaderAsset> shaderAsset = RPI::AssetUtils::LoadCriticalAsset<RPI::ShaderAsset>
  643. (clearShadowShaderFilePath, RPI::AssetUtils::TraceLevel::Assert);
  644. m_clearShadowShader = RPI::Shader::FindOrCreate(shaderAsset);
  645. const RPI::ShaderVariant& variant = m_clearShadowShader->GetRootVariant();
  646. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  647. variant.ConfigurePipelineState(pipelineStateDescriptor);
  648. [[maybe_unused]] bool foundPipelineState = GetParentScene()->ConfigurePipelineState(m_clearShadowShader->GetDrawListTag(), pipelineStateDescriptor);
  649. AZ_Assert(foundPipelineState, "Could not find pipeline state for ClearShadow shader's draw list '%s'", shaderAsset->GetDrawListName().GetCStr())
  650. RHI::InputStreamLayoutBuilder layoutBuilder;
  651. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  652. const RHI::MultiDevicePipelineState* pipelineState = m_clearShadowShader->AcquirePipelineState(pipelineStateDescriptor);
  653. if (!pipelineState)
  654. {
  655. AZ_Assert(false, "Shader '%s'. Failed to acquire default pipeline state", shaderAsset->GetName().GetCStr());
  656. return;
  657. }
  658. RHI::MultiDeviceDrawPacketBuilder drawPacketBuilder{RHI::MultiDevice::AllDevices};
  659. drawPacketBuilder.Begin(nullptr);
  660. drawPacketBuilder.SetDrawArguments(RHI::DrawLinear(1, 0, 3, 0));
  661. RHI::MultiDeviceDrawPacketBuilder::MultiDeviceDrawRequest drawRequest;
  662. drawRequest.m_listTag = m_clearShadowShader->GetDrawListTag();
  663. drawRequest.m_pipelineState = pipelineState;
  664. drawRequest.m_sortKey = AZStd::numeric_limits<RHI::DrawItemSortKey>::min();
  665. drawPacketBuilder.AddDrawItem(drawRequest);
  666. m_clearShadowDrawPacket = drawPacketBuilder.End();
  667. }
  668. void ProjectedShadowFeatureProcessor::UpdateAtlas()
  669. {
  670. // Currently when something changes, the atlas is completely reset. This is ok when most shadows are dynamic,
  671. // but isn't ideal for cached shadows which will need to re-render on the next frame.
  672. m_atlas.Initialize();
  673. auto& shadowProperties = m_shadowProperties.GetDataVector();
  674. bool needsEsm = false;
  675. for (const auto& shadowProperty : shadowProperties)
  676. {
  677. uint16_t shadowIndex = shadowProperty.m_shadowId.GetIndex();
  678. FilterParameter& filterData = m_shadowData.GetElement<FilterParamIndex>(shadowIndex);
  679. needsEsm = needsEsm || filterData.m_isEnabled;
  680. m_atlas.SetShadowmapSize(shadowIndex, static_cast<ShadowmapSize>(filterData.m_shadowmapSize));
  681. }
  682. m_atlas.Finalize();
  683. auto createAtlas = [&](RHI::Format format, RHI::ImageBindFlags bindFlags, RHI::ImageAspectFlags aspectFlags, AZStd::string name)
  684. ->Data::Instance<RPI::AttachmentImage>
  685. {
  686. RHI::ImageDescriptor imageDescriptor;
  687. const uint32_t shadowmapSize = static_cast<uint32_t>(m_atlas.GetBaseShadowmapSize());
  688. imageDescriptor.m_size = RHI::Size(shadowmapSize, shadowmapSize, 1);
  689. imageDescriptor.m_format = format;
  690. imageDescriptor.m_arraySize = m_atlas.GetArraySliceCount();
  691. imageDescriptor.m_bindFlags |= bindFlags;
  692. imageDescriptor.m_sharedQueueMask = RHI::HardwareQueueClassMask::Graphics;
  693. // The ImageViewDescriptor must be specified to make sure the frame graph compiler doesn't treat this as a transient image.
  694. RHI::ImageViewDescriptor viewDesc = RHI::ImageViewDescriptor::Create(imageDescriptor.m_format, 0, 0);
  695. viewDesc.m_aspectFlags = aspectFlags;
  696. RPI::CreateAttachmentImageRequest createImageRequest;
  697. createImageRequest.m_imagePool = RPI::ImageSystemInterface::Get()->GetSystemAttachmentPool().get();
  698. createImageRequest.m_imageDescriptor = imageDescriptor;
  699. createImageRequest.m_imageName = AZStd::string::format("%s.%s", name.c_str(), GetParentScene()->GetName().GetCStr());
  700. createImageRequest.m_imageViewDescriptor = &viewDesc;
  701. return RPI::AttachmentImage::Create(createImageRequest);
  702. };
  703. m_atlasImage = createAtlas(RHI::Format::D32_FLOAT, RHI::ImageBindFlags::Depth, RHI::ImageAspectFlags::Depth, "ProjectedShadowAtlas");
  704. for (auto& [key, projectedShadowmapsPass] : m_projectedShadowmapsPasses)
  705. {
  706. projectedShadowmapsPass->SetAtlasAttachmentImage(m_atlasImage);
  707. projectedShadowmapsPass->QueueForBuildAndInitialization();
  708. }
  709. if (needsEsm)
  710. {
  711. m_esmAtlasImage = createAtlas(RHI::Format::R16_FLOAT, RHI::ImageBindFlags::ShaderReadWrite, RHI::ImageAspectFlags::Color, "ProjectedShadowAtlasESM");
  712. for (auto& [key, esmShadowmapsPass] : m_esmShadowmapsPasses)
  713. {
  714. esmShadowmapsPass->SetAtlasAttachmentImage(m_esmAtlasImage);
  715. esmShadowmapsPass->QueueForBuildAndInitialization();
  716. }
  717. }
  718. else
  719. {
  720. m_esmAtlasImage = {};
  721. }
  722. }
  723. RPI::Ptr<ShadowmapPass> ProjectedShadowFeatureProcessor::CreateShadowmapPass(size_t childIndex)
  724. {
  725. const Name passName{ AZStd::string::format("ProjectedShadowmapPass.%zu", childIndex) };
  726. RHI::RHISystemInterface* rhiSystem = RHI::RHISystemInterface::Get();
  727. auto passData = AZStd::make_shared<RPI::RasterPassData>();
  728. passData->m_drawListTag = rhiSystem->GetDrawListTagRegistry()->GetName(m_primaryProjectedShadowmapsPass->GetDrawListTag());
  729. passData->m_pipelineViewTag = AZStd::string::format("%s.%zu", m_primaryProjectedShadowmapsPass->GetPipelineViewTag().GetCStr(), childIndex);
  730. return ShadowmapPass::CreateWithPassRequest(passName, passData);
  731. }
  732. void ProjectedShadowFeatureProcessor::UpdateShadowPasses()
  733. {
  734. struct SliceInfo
  735. {
  736. bool m_hasStaticShadows = false;
  737. AZStd::vector<ShadowmapPass*> m_shadowPasses;
  738. };
  739. AZStd::vector<SliceInfo> sliceInfo(m_atlas.GetArraySliceCount());
  740. for (const auto& it : m_shadowProperties.GetDataVector())
  741. {
  742. // This index indicates the execution order of the passes.
  743. // The first pass to render a slice should clear the slice.
  744. size_t shadowIndex = it.m_shadowId.GetIndex();
  745. auto* pass = it.m_shadowmapPass.get();
  746. const ShadowmapAtlas::Origin origin = m_atlas.GetOrigin(shadowIndex);
  747. pass->SetArraySlice(origin.m_arraySlice);
  748. pass->SetIsStatic(it.m_useCachedShadows);
  749. pass->ForceRenderNextFrame();
  750. const auto& filterData = m_shadowData.GetElement<FilterParamIndex>(shadowIndex);
  751. if (filterData.m_shadowmapSize != static_cast<uint32_t>(ShadowmapSize::None))
  752. {
  753. const RHI::Viewport viewport(
  754. origin.m_originInSlice[0] * 1.f,
  755. (origin.m_originInSlice[0] + filterData.m_shadowmapSize) * 1.f,
  756. origin.m_originInSlice[1] * 1.f,
  757. (origin.m_originInSlice[1] + filterData.m_shadowmapSize) * 1.f);
  758. const RHI::Scissor scissor(
  759. origin.m_originInSlice[0],
  760. origin.m_originInSlice[1],
  761. origin.m_originInSlice[0] + filterData.m_shadowmapSize,
  762. origin.m_originInSlice[1] + filterData.m_shadowmapSize);
  763. pass->SetViewportScissor(viewport, scissor);
  764. pass->SetClearEnabled(false);
  765. SliceInfo& sliceInfoItem = sliceInfo.at(origin.m_arraySlice);
  766. sliceInfoItem.m_shadowPasses.push_back(pass);
  767. sliceInfoItem.m_hasStaticShadows = sliceInfoItem.m_hasStaticShadows || it.m_useCachedShadows;
  768. }
  769. }
  770. RHI::Handle<uint32_t> casterMovedBit = GetParentScene()->GetViewTagBitRegistry().FindTag(MeshCommon::MeshMovedName);
  771. for (const auto& it : sliceInfo)
  772. {
  773. if (!it.m_hasStaticShadows)
  774. {
  775. if (!it.m_shadowPasses.empty())
  776. {
  777. // no static shadows in this slice, so have the first pass clear the atlas on load.
  778. it.m_shadowPasses.at(0)->SetClearEnabled(true);
  779. }
  780. }
  781. else
  782. {
  783. // There's at least one static shadow in this slice, so passes need to clear themselves using a draw.
  784. for (auto* pass : it.m_shadowPasses)
  785. {
  786. pass->SetClearShadowDrawPacket(m_clearShadowDrawPacket);
  787. pass->SetCasterMovedBit(casterMovedBit);
  788. }
  789. }
  790. }
  791. }
  792. }