ChromaticAberrationPass.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/ChromaticAberrationPass.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<ChromaticAberrationPass> ChromaticAberrationPass::Create(const RPI::PassDescriptor& descriptor)
  17. {
  18. RPI::Ptr<ChromaticAberrationPass> pass = aznew ChromaticAberrationPass(descriptor);
  19. return AZStd::move(pass);
  20. }
  21. ChromaticAberrationPass::ChromaticAberrationPass(const RPI::PassDescriptor& descriptor)
  22. : RPI::ComputePass(descriptor)
  23. {
  24. }
  25. bool ChromaticAberrationPass::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. if (!fp)
  38. {
  39. return false;
  40. }
  41. const RPI::ViewPtr view = GetRenderPipeline()->GetFirstView(GetPipelineViewTag());
  42. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  43. if (!postProcessSettings)
  44. {
  45. return false;
  46. }
  47. const ChromaticAberrationSettings* chromaticAberrationSettings = postProcessSettings->GetChromaticAberrationSettings();
  48. if (!chromaticAberrationSettings)
  49. {
  50. return false;
  51. }
  52. return chromaticAberrationSettings->GetEnabled();
  53. }
  54. void ChromaticAberrationPass::FrameBeginInternal(FramePrepareParams params)
  55. {
  56. // Must match the struct in ChromaticAberration.azsl
  57. struct Constants
  58. {
  59. AZStd::array<u32, 2> m_outputSize;
  60. AZStd::array<float, 2> m_outputCenter;
  61. float m_strength = ChromaticAberration::DefaultStrength;
  62. float m_blend = ChromaticAberration::DefaultBlend;
  63. } constants{};
  64. RPI::Scene* scene = GetScene();
  65. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  66. if (fp)
  67. {
  68. RPI::ViewPtr view = m_pipeline->GetFirstView(GetPipelineViewTag());
  69. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  70. if (postProcessSettings)
  71. {
  72. ChromaticAberrationSettings* chromaticAberrationSettings = postProcessSettings->GetChromaticAberrationSettings();
  73. if (chromaticAberrationSettings)
  74. {
  75. constants.m_strength = chromaticAberrationSettings->GetStrength();
  76. constants.m_blend = chromaticAberrationSettings->GetBlend();
  77. }
  78. }
  79. }
  80. AZ_Assert(GetOutputCount() > 0, "ChromaticAberrationPass: No output bindings!");
  81. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  82. AZ_Assert(outputAttachment != nullptr, "ChromaticAberrationPass: Output binding has no attachment!");
  83. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  84. constants.m_outputSize[0] = size.m_width;
  85. constants.m_outputSize[1] = size.m_height;
  86. constants.m_outputCenter[0] = (size.m_width - 1) * 0.5f;
  87. constants.m_outputCenter[1] = (size.m_height -1) * 0.5f;
  88. m_shaderResourceGroup->SetConstant(m_constantsIndex, constants);
  89. RPI::ComputePass::FrameBeginInternal(params);
  90. }
  91. } // namespace Render
  92. } // namespace AZ