SkyAtmosphereFeatureProcessor.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_renderPipelineToSkyAtmosphereParentPasses.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& [_, skyAtmosphereParentPasses] : m_renderPipelineToSkyAtmosphereParentPasses)
  55. for (auto pass : skyAtmosphereParentPasses)
  56. {
  57. pass->ReleaseAtmospherePass(id);
  58. }
  59. }
  60. void SkyAtmosphereFeatureProcessor::SetAtmosphereParams(AtmosphereId id, const SkyAtmosphereParams& params)
  61. {
  62. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  63. atmosphere.m_params = params;
  64. atmosphere.m_passNeedsUpdate = true;
  65. }
  66. void SkyAtmosphereFeatureProcessor::SetAtmosphereEnabled(AtmosphereId id, bool enabled)
  67. {
  68. if (id.IsValid())
  69. {
  70. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  71. atmosphere.m_enabled = enabled;
  72. }
  73. }
  74. bool SkyAtmosphereFeatureProcessor::GetAtmosphereEnabled(AtmosphereId id)
  75. {
  76. if (id.IsValid())
  77. {
  78. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  79. return atmosphere.m_enabled;
  80. }
  81. return false;
  82. }
  83. void SkyAtmosphereFeatureProcessor::InitializeAtmosphere(AtmosphereId id)
  84. {
  85. auto& atmosphere = m_atmospheres.GetElement(id.GetIndex());
  86. atmosphere.m_id = id;
  87. atmosphere.m_passNeedsUpdate = true;
  88. atmosphere.m_enabled = true;
  89. for (auto& [_, skyAtmosphereParentPasses] : m_renderPipelineToSkyAtmosphereParentPasses)
  90. {
  91. for (auto pass : skyAtmosphereParentPasses)
  92. {
  93. pass->CreateAtmospherePass(id);
  94. }
  95. }
  96. }
  97. void SkyAtmosphereFeatureProcessor::AddRenderPasses(RPI::RenderPipeline* renderPipeline)
  98. {
  99. if (m_renderPipelineToSkyAtmosphereParentPasses.find(renderPipeline) != m_renderPipelineToSkyAtmosphereParentPasses.end())
  100. {
  101. m_renderPipelineToSkyAtmosphereParentPasses.erase(renderPipeline);
  102. }
  103. m_renderPipelineToSkyAtmosphereParentPasses[renderPipeline] = {};
  104. auto& skyAtmosphereParentPasses = m_renderPipelineToSkyAtmosphereParentPasses[renderPipeline];
  105. RPI::PassFilter passFilter = RPI::PassFilter::CreateWithTemplateName(Name("SkyAtmosphereParentTemplate"), renderPipeline);
  106. RPI::PassSystemInterface::Get()->ForEachPass(
  107. passFilter,
  108. [&skyAtmosphereParentPasses](RPI::Pass* pass) -> RPI::PassFilterExecutionFlow
  109. {
  110. SkyAtmosphereParentPass* parentPass = static_cast<SkyAtmosphereParentPass*>(pass);
  111. skyAtmosphereParentPasses.emplace_back(parentPass);
  112. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses;
  113. });
  114. // make sure atmospheres are created if needed
  115. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  116. {
  117. auto& atmosphere = m_atmospheres.GetElement(i);
  118. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled)
  119. {
  120. InitializeAtmosphere(atmosphere.m_id);
  121. }
  122. }
  123. }
  124. void SkyAtmosphereFeatureProcessor::OnRenderPipelineChanged([[maybe_unused]] RPI::RenderPipeline* pipeline,
  125. RPI::SceneNotification::RenderPipelineChangeType changeType)
  126. {
  127. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Added
  128. || changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  129. {
  130. UpdateBackgroundClearColor();
  131. }
  132. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::Removed)
  133. {
  134. m_renderPipelineToSkyAtmosphereParentPasses.erase(pipeline);
  135. }
  136. }
  137. void SkyAtmosphereFeatureProcessor::Render([[maybe_unused]] const FeatureProcessor::RenderPacket& packet)
  138. {
  139. AZ_PROFILE_SCOPE(RPI, "SkyAtmosphereFeatureProcessor: Render");
  140. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  141. {
  142. auto& atmosphere = m_atmospheres.GetElement(i);
  143. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled && atmosphere.m_passNeedsUpdate)
  144. {
  145. // update every atmosphere parent pass (per-pipeline)
  146. for (auto& [_, skyAtmosphereParentPasses] : m_renderPipelineToSkyAtmosphereParentPasses)
  147. {
  148. for (auto pass : skyAtmosphereParentPasses)
  149. {
  150. pass->UpdateAtmospherePassSRG(atmosphere.m_id, atmosphere.m_params);
  151. }
  152. }
  153. atmosphere.m_passNeedsUpdate = false;
  154. }
  155. }
  156. }
  157. bool SkyAtmosphereFeatureProcessor::HasValidAtmosphere()
  158. {
  159. for (size_t i = 0; i < m_atmospheres.GetSize(); ++i)
  160. {
  161. const auto& atmosphere = m_atmospheres.GetElement(i);
  162. if (atmosphere.m_id.IsValid() && atmosphere.m_enabled)
  163. {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. void SkyAtmosphereFeatureProcessor::UpdateBackgroundClearColor()
  170. {
  171. // don't update the background unless we have valid atmospheres
  172. if (!HasValidAtmosphere())
  173. {
  174. return;
  175. }
  176. // This function is only necessary for now because the default clear value
  177. // color is not black, and is set in various .pass files in places a user
  178. // is unlikely to find. Unfortunately, the viewport will revert to the
  179. // grey color when resizing momentarily.
  180. const RHI::ClearValue blackClearValue = RHI::ClearValue::CreateVector4Float(0.f, 0.f, 0.f, 0.f);
  181. RPI::PassFilter passFilter;
  182. AZStd::string slot;
  183. auto setClearValue = [&](RPI::Pass* pass)-> RPI::PassFilterExecutionFlow
  184. {
  185. Name slotName = Name::FromStringLiteral(slot, AZ::Interface<AZ::NameDictionary>::Get());
  186. if (auto binding = pass->FindAttachmentBinding(slotName))
  187. {
  188. binding->m_unifiedScopeDesc.m_loadStoreAction.m_clearValue = blackClearValue;
  189. }
  190. return RPI::PassFilterExecutionFlow::ContinueVisitingPasses;
  191. };
  192. slot = "SpecularOutput";
  193. passFilter= RPI::PassFilter::CreateWithTemplateName(Name("ForwardPassTemplate"), GetParentScene());
  194. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  195. passFilter = RPI::PassFilter::CreateWithTemplateName(Name("ForwardMSAAPassTemplate"), GetParentScene());
  196. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  197. slot = "ReflectionOutput";
  198. passFilter = RPI::PassFilter::CreateWithTemplateName(Name("ReflectionGlobalFullscreenPassTemplate"), GetParentScene());
  199. RPI::PassSystemInterface::Get()->ForEachPass(passFilter, setClearValue);
  200. }
  201. }