MotionBlurPass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <PostProcessing/MotionBlurPass.h>
  9. #include <PostProcess/PostProcessFeatureProcessor.h>
  10. #include <Atom/RPI.Public/RenderPipeline.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. RPI::Ptr<MotionBlurPass> MotionBlurPass::Create(const RPI::PassDescriptor& descriptor)
  17. {
  18. RPI::Ptr<MotionBlurPass> pass = aznew MotionBlurPass(descriptor);
  19. return AZStd::move(pass);
  20. }
  21. MotionBlurPass::MotionBlurPass(const RPI::PassDescriptor& descriptor)
  22. : RPI::ComputePass(descriptor)
  23. {
  24. }
  25. bool MotionBlurPass::IsEnabled() const
  26. {
  27. if (!ComputePass::IsEnabled())
  28. {
  29. return false;
  30. }
  31. const RPI::Scene* scene = GetScene();
  32. if (!scene)
  33. {
  34. return false;
  35. }
  36. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  37. const RPI::ViewPtr view = GetRenderPipeline()->GetDefaultView();
  38. if (!fp)
  39. {
  40. return false;
  41. }
  42. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  43. if (!postProcessSettings)
  44. {
  45. return false;
  46. }
  47. const MotionBlurSettings* MotionBlurSettings = postProcessSettings->GetMotionBlurSettings();
  48. if (!MotionBlurSettings)
  49. {
  50. return false;
  51. }
  52. return MotionBlurSettings->GetEnabled();
  53. }
  54. void MotionBlurPass::FrameBeginInternal(FramePrepareParams params)
  55. {
  56. // Must match the struct in MotionBlur.azsl
  57. struct Constants
  58. {
  59. AZ::u32 m_sampleNumber = MotionBlur::DefaultSampleNumber;
  60. float m_strength = MotionBlur::DefaultStrength;
  61. AZStd::array<AZ::u32, 2> m_outputSize;
  62. } constants{};
  63. RPI::Scene* scene = GetScene();
  64. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  65. if (fp)
  66. {
  67. RPI::ViewPtr view = scene->GetDefaultRenderPipeline()->GetDefaultView();
  68. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  69. if (postProcessSettings)
  70. {
  71. MotionBlurSettings* MotionBlurSettings = postProcessSettings->GetMotionBlurSettings();
  72. if (MotionBlurSettings)
  73. {
  74. constants.m_sampleNumber = MotionBlurSettings->GetSampleNumber();
  75. constants.m_strength = MotionBlurSettings->GetStrength();
  76. }
  77. }
  78. }
  79. AZ_Assert(GetOutputCount() > 0, "MotionBlurPass: No output bindings!");
  80. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  81. AZ_Assert(outputAttachment != nullptr, "MotionBlurPass: Output binding has no attachment!");
  82. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  83. constants.m_outputSize[0] = size.m_width;
  84. constants.m_outputSize[1] = size.m_height;
  85. m_shaderResourceGroup->SetConstant(m_constantsIndex, constants);
  86. RPI::ComputePass::FrameBeginInternal(params);
  87. }
  88. } // namespace Render
  89. } // namespace AZ