FilmGrainPass.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/FilmGrainPass.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<FilmGrainPass> FilmGrainPass::Create(const RPI::PassDescriptor& descriptor)
  17. {
  18. RPI::Ptr<FilmGrainPass> pass = aznew FilmGrainPass(descriptor);
  19. return AZStd::move(pass);
  20. }
  21. FilmGrainPass::FilmGrainPass(const RPI::PassDescriptor& descriptor)
  22. : RPI::ComputePass(descriptor)
  23. {
  24. }
  25. bool FilmGrainPass::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 FilmGrainSettings* filmGrainSettings = postProcessSettings->GetFilmGrainSettings();
  48. if (!filmGrainSettings)
  49. {
  50. return false;
  51. }
  52. return filmGrainSettings->GetEnabled();
  53. }
  54. void FilmGrainPass::FrameBeginInternal(FramePrepareParams params)
  55. {
  56. // Must match the struct in FilmGrain.azsl
  57. struct Constants
  58. {
  59. AZStd::array<u32, 2> m_outputSize;
  60. AZStd::array<u32, 2> m_grainTextureSize;
  61. float m_intensity = FilmGrain::DefaultIntensity;
  62. float m_luminanceDampening = FilmGrain::DefaultLuminanceDampening;
  63. float m_tilingScale = FilmGrain::DefaultTilingScale;
  64. float m_pad;
  65. } constants{};
  66. RPI::Scene* scene = GetScene();
  67. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  68. if (fp)
  69. {
  70. RPI::ViewPtr view = scene->GetDefaultRenderPipeline()->GetDefaultView();
  71. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  72. if (postProcessSettings)
  73. {
  74. FilmGrainSettings* filmGrainSettings = postProcessSettings->GetFilmGrainSettings();
  75. if (filmGrainSettings)
  76. {
  77. constants.m_intensity = filmGrainSettings->GetIntensity();
  78. constants.m_luminanceDampening = filmGrainSettings->GetLuminanceDampening();
  79. constants.m_tilingScale = filmGrainSettings->GetTilingScale();
  80. AZStd::string settingsGrainPath = filmGrainSettings->GetGrainPath();
  81. if (m_currentGrainPath != settingsGrainPath)
  82. {
  83. m_currentGrainPath = settingsGrainPath;
  84. m_grainImage = filmGrainSettings->LoadStreamingImage(settingsGrainPath.c_str(), "FilmGrain");
  85. }
  86. }
  87. }
  88. }
  89. m_shaderResourceGroup->SetImage(m_grainIndex, m_grainImage);
  90. RHI::Size grainTextureSize = m_grainImage->GetDescriptor().m_size;
  91. constants.m_grainTextureSize[0] = grainTextureSize.m_width;
  92. constants.m_grainTextureSize[1] = grainTextureSize.m_height;
  93. AZ_Assert(GetOutputCount() > 0, "FilmGrainPass: No output bindings!");
  94. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  95. AZ_Assert(outputAttachment != nullptr, "FilmGrainPass: Output binding has no attachment!");
  96. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  97. constants.m_outputSize[0] = size.m_width;
  98. constants.m_outputSize[1] = size.m_height;
  99. m_shaderResourceGroup->SetConstant(m_constantsIndex, constants);
  100. RPI::ComputePass::FrameBeginInternal(params);
  101. }
  102. } // namespace Render
  103. } // namespace AZ