SsaoPasses.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <PostProcess/PostProcessFeatureProcessor.h>
  9. #include <PostProcess/Ssao/SsaoSettings.h>
  10. #include <PostProcessing/FastDepthAwareBlurPasses.h>
  11. #include <PostProcessing/SsaoPasses.h>
  12. #include <AzCore/Math/MathUtils.h>
  13. #include <Atom/Feature/PostProcess/Ssao/SsaoConstants.h>
  14. #include <Atom/RPI.Public/RenderPipeline.h>
  15. #include <Atom/RPI.Public/Scene.h>
  16. #include <Atom/RPI.Public/View.h>
  17. namespace AZ
  18. {
  19. namespace Render
  20. {
  21. // --- SSAO Parent Pass ---
  22. RPI::Ptr<SsaoParentPass> SsaoParentPass::Create(const RPI::PassDescriptor& descriptor)
  23. {
  24. RPI::Ptr<SsaoParentPass> pass = aznew SsaoParentPass(descriptor);
  25. return AZStd::move(pass);
  26. }
  27. SsaoParentPass::SsaoParentPass(const RPI::PassDescriptor& descriptor)
  28. : RPI::ParentPass(descriptor)
  29. { }
  30. bool SsaoParentPass::IsEnabled() const
  31. {
  32. if (!ParentPass::IsEnabled())
  33. {
  34. return false;
  35. }
  36. const RPI::Scene* scene = GetScene();
  37. if (!scene)
  38. {
  39. return false;
  40. }
  41. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  42. const RPI::ViewPtr view = GetRenderPipeline()->GetFirstView(GetPipelineViewTag());
  43. if (!fp)
  44. {
  45. return true;
  46. }
  47. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  48. if (!postProcessSettings)
  49. {
  50. return true;
  51. }
  52. const SsaoSettings* ssaoSettings = postProcessSettings->GetSsaoSettings();
  53. if (!ssaoSettings)
  54. {
  55. return true;
  56. }
  57. return ssaoSettings->GetEnabled();
  58. }
  59. void SsaoParentPass::InitializeInternal()
  60. {
  61. ParentPass::InitializeInternal();
  62. m_blurParentPass = FindChildPass(Name("SsaoBlur"))->AsParent();
  63. AZ_Assert(m_blurParentPass, "[SsaoParentPass] Could not retrieve parent blur pass.");
  64. m_blurHorizontalPass = azrtti_cast<FastDepthAwareBlurHorPass*>(m_blurParentPass->FindChildPass(Name("HorizontalBlur")).get());
  65. m_blurVerticalPass = azrtti_cast<FastDepthAwareBlurVerPass*>(m_blurParentPass->FindChildPass(Name("VerticalBlur")).get());
  66. m_downsamplePass = FindChildPass(Name("DepthDownsample")).get();
  67. m_upsamplePass = FindChildPass(Name("Upsample")).get();
  68. AZ_Assert(m_blurHorizontalPass, "[SsaoParentPass] Could not retrieve horizontal blur pass.");
  69. AZ_Assert(m_blurVerticalPass, "[SsaoParentPass] Could not retrieve vertical blur pass.");
  70. AZ_Assert(m_downsamplePass, "[SsaoParentPass] Could not retrieve downsample pass.");
  71. AZ_Assert(m_upsamplePass, "[SsaoParentPass] Could not retrieve upsample pass.");
  72. }
  73. void SsaoParentPass::FrameBeginInternal(FramePrepareParams params)
  74. {
  75. RPI::Scene* scene = GetScene();
  76. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  77. AZ::RPI::ViewPtr view = m_pipeline->GetFirstView(GetPipelineViewTag());
  78. if (fp)
  79. {
  80. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  81. if (postProcessSettings)
  82. {
  83. SsaoSettings* ssaoSettings = postProcessSettings->GetSsaoSettings();
  84. if (ssaoSettings)
  85. {
  86. bool ssaoEnabled = ssaoSettings->GetEnabled();
  87. bool blurEnabled = ssaoEnabled && ssaoSettings->GetEnableBlur();
  88. bool downsampleEnabled = ssaoEnabled && ssaoSettings->GetEnableDownsample();
  89. m_blurParentPass->SetEnabled(blurEnabled);
  90. if (blurEnabled)
  91. {
  92. float constFalloff = ssaoSettings->GetBlurConstFalloff();
  93. float depthFalloffThreshold = ssaoSettings->GetBlurDepthFalloffThreshold();
  94. float depthFalloffStrength = ssaoSettings->GetBlurDepthFalloffStrength();
  95. m_blurHorizontalPass->SetConstants(constFalloff, depthFalloffThreshold, depthFalloffStrength);
  96. m_blurVerticalPass->SetConstants(constFalloff, depthFalloffThreshold, depthFalloffStrength);
  97. }
  98. m_downsamplePass->SetEnabled(downsampleEnabled);
  99. m_upsamplePass->SetEnabled(downsampleEnabled);
  100. }
  101. }
  102. }
  103. ParentPass::FrameBeginInternal(params);
  104. }
  105. // --- SSAO Compute Pass ---
  106. RPI::Ptr<SsaoComputePass> SsaoComputePass::Create(const RPI::PassDescriptor& descriptor)
  107. {
  108. RPI::Ptr<SsaoComputePass> pass = aznew SsaoComputePass(descriptor);
  109. return AZStd::move(pass);
  110. }
  111. SsaoComputePass::SsaoComputePass(const RPI::PassDescriptor& descriptor)
  112. : RPI::ComputePass(descriptor)
  113. { }
  114. void SsaoComputePass::FrameBeginInternal(FramePrepareParams params)
  115. {
  116. // Must match the struct in SsaoCompute.azsl
  117. struct SsaoConstants
  118. {
  119. // The texture dimensions of SSAO output
  120. AZStd::array<u32, 2> m_outputSize;
  121. // The size of a pixel relative to screenspace UV
  122. // Calculated by taking the inverse of the texture dimensions
  123. AZStd::array<float, 2> m_pixelSize;
  124. // The size of half a pixel relative to screenspace UV
  125. AZStd::array<float, 2> m_halfPixelSize;
  126. // The strength of the SSAO effect
  127. float m_strength = Ssao::DefaultStrength;
  128. // The sampling radius calculated in screen UV space
  129. float m_samplingRadius = Ssao::DefaultSamplingRadius;
  130. } ssaoConstants{};
  131. RPI::Scene* scene = GetScene();
  132. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  133. AZ::RPI::ViewPtr view = m_pipeline->GetFirstView(GetPipelineViewTag());
  134. if (fp)
  135. {
  136. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  137. if (postProcessSettings)
  138. {
  139. SsaoSettings* ssaoSettings = postProcessSettings->GetSsaoSettings();
  140. if (ssaoSettings)
  141. {
  142. if (ssaoSettings->GetEnabled())
  143. {
  144. ssaoConstants.m_strength = ssaoSettings->GetStrength();
  145. ssaoConstants.m_samplingRadius = ssaoSettings->GetSamplingRadius();
  146. }
  147. else
  148. {
  149. ssaoConstants.m_strength = 0.0f;
  150. }
  151. }
  152. }
  153. }
  154. AZ_Assert(GetOutputCount() > 0, "SsaoComputePass: No output bindings!");
  155. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  156. AZ_Assert(outputAttachment != nullptr, "SsaoComputePass: Output binding has no attachment!");
  157. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  158. ssaoConstants.m_outputSize[0] = size.m_width;
  159. ssaoConstants.m_outputSize[1] = size.m_height;
  160. ssaoConstants.m_pixelSize[0] = 1.0f / float(size.m_width);
  161. ssaoConstants.m_pixelSize[1] = 1.0f / float(size.m_height);
  162. ssaoConstants.m_halfPixelSize[0] = 0.5f * ssaoConstants.m_pixelSize[0];
  163. ssaoConstants.m_halfPixelSize[1] = 0.5f * ssaoConstants.m_pixelSize[1];
  164. m_shaderResourceGroup->SetConstant(m_constantsIndex, ssaoConstants);
  165. RPI::ComputePass::FrameBeginInternal(params);
  166. }
  167. } // namespace Render
  168. } // namespace AZ