SkyAtmosphereFeatureProcessor.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <SkyAtmosphere/SkyAtmosphereFeatureProcessor.h>
  9. #include <SkyAtmosphere/SkyAtmosphereParentPass.h>
  10. #include <AzCore/Name/NameDictionary.h>
  11. #include <Atom/RPI.Public/RenderPipeline.h>
  12. #include <Atom/RPI.Public/RPISystemInterface.h>
  13. #include <Atom/RPI.Public/Pass/PassSystem.h>
  14. #include <Atom/RPI.Public/Pass/PassFilter.h>
  15. namespace AZ::Render
  16. {
  17. void SkyAtmosphereFeatureProcessor::Reflect(ReflectContext* context)
  18. {
  19. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  20. {
  21. serializeContext
  22. ->Class<SkyAtmosphereFeatureProcessor, FeatureProcessor>()
  23. ->Version(0);
  24. }
  25. }
  26. void SkyAtmosphereFeatureProcessor::Activate()
  27. {
  28. EnableSceneNotification();
  29. }
  30. void SkyAtmosphereFeatureProcessor::Deactivate()
  31. {
  32. DisableSceneNotification();
  33. m_atmospheres.Clear();
  34. m_skyAtmosphereParentPasses.clear();
  35. }
  36. SkyAtmosphereFeatureProcessor::AtmosphereId SkyAtmosphereFeatureProcessor::CreateAtmosphere()
  37. {
  38. size_t index = m_atmospheres.Reserve();
  39. if (index >= std::numeric_limits<AtmosphereId::IndexType>::max())
  40. {
  41. m_atmospheres.Release(index);
  42. return AtmosphereId::Null;
  43. }
  44. AtmosphereId id = AtmosphereId(aznumeric_cast<AtmosphereId::IndexType>(index));
  45. InitializeAtmosphere(id);
  46. return id;
  47. }
  48. void SkyAtmosphereFeatureProcessor::ReleaseAtmosphere(AtmosphereId id)
  49. {
  50. if (id.IsValid())
  51. {
  52. m_atmospheres.Release(id.GetIndex());
  53. }
  54. for (auto pass : m_skyAtmosphereParentPasses )
  55. {
  56. pass->ReleaseAtmospherePass(id);
  57. }
  58. }
  59. void SkyAtmosphereFeatureProcessor::SetAtmosphereParams(AtmosphereId id, const SkyAtmosphereParams& params)
  60. {
  61. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  62. atmosphere.m_params = params;
  63. atmosphere.m_passNeedsUpdate = true;
  64. }
  65. void SkyAtmosphereFeatureProcessor::SetAtmosphereEnabled(AtmosphereId id, bool enabled)
  66. {
  67. if (id.IsValid())
  68. {
  69. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  70. atmosphere.m_enabled = enabled;
  71. }
  72. }
  73. bool SkyAtmosphereFeatureProcessor::GetAtmosphereEnabled(AtmosphereId id)
  74. {
  75. if (id.IsValid())
  76. {
  77. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  78. return atmosphere.m_enabled;
  79. }
  80. return false;
  81. }
  82. void SkyAtmosphereFeatureProcessor::InitializeAtmosphere(AtmosphereId id)
  83. {
  84. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  85. atmosphere.m_id = id;
  86. atmosphere.m_passNeedsUpdate = true;
  87. atmosphere.m_enabled = true;
  88. for (auto pass : m_skyAtmosphereParentPasses )
  89. {
  90. pass->CreateAtmospherePass(id);
  91. }
  92. }
  93. void SkyAtmosphereFeatureProcessor::AddRenderPasses(RPI::RenderPipeline* renderPipeline)
  94. {
  95. m_skyAtmosphereParentPasses.clear();
  96. RPI::PassFilter passFilter = RPI::PassFilter::CreateWithTemplateName(Name("SkyAtmosphereParentTemplate"), renderPipeline);
  97. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, [this](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  98. {
  99. SkyAtmosphereParentPass* parentPass = static_cast<SkyAtmosphereParentPass*>(pass);
  100. m_skyAtmosphereParentPasses.emplace_back(parentPass);
  101. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses;
  102. });
  103. // make sure atmospheres are created if needed
  104. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  105. {
  106. auto& atmosphere = m_atmospheres.GetElement(i);
  107. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled)
  108. {
  109. InitializeAtmosphere(atmosphere.m_id);
  110. }
  111. }
  112. }
  113. void SkyAtmosphereFeatureProcessor::OnRenderPipelineChanged([[maybe_unused]] RPI::RenderPipeline* pipeline,
  114. RPI::SceneNotification::RenderPipelineChangeType changeType)
  115. {
  116. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Added
  117. || changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  118. {
  119. UpdateBackgroundClearColor();
  120. }
  121. }
  122. void SkyAtmosphereFeatureProcessor::Render([[maybe_unused]] const FeatureProcessor::RenderPacket& packet)
  123. {
  124. AZ_PROFILE_SCOPE(RPI, "SkyAtmosphereFeatureProcessor: Render");
  125. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  126. {
  127. auto& atmosphere = m_atmospheres.GetElement(i);
  128. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled && atmosphere.m_passNeedsUpdate)
  129. {
  130. // update every atmosphere parent pass (per-pipeline)
  131. for (auto pass : m_skyAtmosphereParentPasses)
  132. {
  133. pass->UpdateAtmospherePassSRG(atmosphere.m_id, atmosphere.m_params);
  134. }
  135. atmosphere.m_passNeedsUpdate = false;
  136. }
  137. }
  138. }
  139. bool SkyAtmosphereFeatureProcessor::HasValidAtmosphere()
  140. {
  141. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  142. {
  143. const auto& atmosphere = m_atmospheres.GetElement(i);
  144. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled)
  145. {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. void SkyAtmosphereFeatureProcessor::UpdateBackgroundClearColor()
  152. {
  153. // don't update the background unless we have valid atmospheres
  154. if (!HasValidAtmosphere())
  155. {
  156. return;
  157. }
  158. // This function is only necessary for now because the default clear value
  159. // color is not black, and is set in various .pass files in places a user
  160. // is unlikely to find. Unfortunately, the viewport will revert to the
  161. // grey color when resizing momentarily.
  162. const RHI::ClearValue blackClearValue = RHI::ClearValue::CreateVector4Float(0.f, 0.f, 0.f, 0.f);
  163. RPI::PassFilter passFilter;
  164. AZStd::string slot;
  165. auto setClearValue = [&](RPI::Pass* pass)-> RPI::PassFilterExecutionFlow
  166. {
  167. Name slotName = Name::FromStringLiteral(slot, AZ::Interface<AZ::NameDictionary>::Get());
  168. if (auto binding = pass->FindAttachmentBinding(slotName))
  169. {
  170. binding->m_unifiedScopeDesc.m_loadStoreAction.m_clearValue = blackClearValue;
  171. }
  172. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses;
  173. };
  174. slot = "SpecularOutput";
  175. passFilter= RPI::PassFilter::CreateWithTemplateName(Name("ForwardPassTemplate"), GetParentScene());
  176. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  177. passFilter = RPI::PassFilter::CreateWithTemplateName(Name("ForwardMSAAPassTemplate"), GetParentScene());
  178. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  179. slot = "ReflectionOutput";
  180. passFilter = RPI::PassFilter::CreateWithTemplateName(Name("ReflectionGlobalFullscreenPassTemplate"), GetParentScene());
  181. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  182. }
  183. }