DepthOfFieldMaskPass.cpp 5.0 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 <Atom/RPI.Public/RenderPipeline.h>
  9. #include <Atom/RPI.Public/Scene.h>
  10. #include <Atom/RPI.Public/View.h>
  11. #include <PostProcess/PostProcessFeatureProcessor.h>
  12. #include <PostProcess/DepthOfField/DepthOfFieldSettings.h>
  13. #include <PostProcessing/DepthOfFieldMaskPass.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. RPI::Ptr<DepthOfFieldMaskPass> DepthOfFieldMaskPass::Create(const RPI::PassDescriptor& descriptor)
  19. {
  20. RPI::Ptr<DepthOfFieldMaskPass> pass = aznew DepthOfFieldMaskPass(descriptor);
  21. return pass;
  22. }
  23. DepthOfFieldMaskPass::DepthOfFieldMaskPass(const RPI::PassDescriptor& descriptor)
  24. : RPI::FullscreenTrianglePass(descriptor)
  25. {
  26. }
  27. void DepthOfFieldMaskPass::InitializeInternal()
  28. {
  29. FullscreenTrianglePass::InitializeInternal();
  30. m_blendFactorIndex.Reset();
  31. m_inputResolutionInverseIndex.Reset();
  32. m_radiusMinIndex.Reset();
  33. m_radiusMaxIndex.Reset();
  34. }
  35. void DepthOfFieldMaskPass::FrameBeginInternal(FramePrepareParams params)
  36. {
  37. RPI::Scene* scene = GetScene();
  38. PostProcessFeatureProcessor* fp = scene->GetFeatureProcessor<PostProcessFeatureProcessor>();
  39. AZ::RPI::ViewPtr view = GetRenderPipeline()->GetFirstView(GetPipelineViewTag());
  40. if (fp)
  41. {
  42. PostProcessSettings* postProcessSettings = fp->GetLevelSettingsFromView(view);
  43. if (postProcessSettings)
  44. {
  45. DepthOfFieldSettings* dofSettings = postProcessSettings->GetDepthOfFieldSettings();
  46. if (dofSettings)
  47. {
  48. AZ::RHI::Handle<uint32_t> splitSize = dofSettings->GetSplitSizeForPass(GetName());
  49. if (splitSize.IsValid())
  50. {
  51. switch (splitSize.GetIndex())
  52. {
  53. case 2:
  54. SetBlendFactor(dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision2);
  55. SetRadiusMinMax(dofSettings->m_minBokehRadiusDivision2, dofSettings->m_maxBokehRadiusDivision2);
  56. break;
  57. case 4:
  58. SetBlendFactor(dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision4);
  59. SetRadiusMinMax(dofSettings->m_minBokehRadiusDivision4, dofSettings->m_maxBokehRadiusDivision4);
  60. break;
  61. case 8:
  62. SetBlendFactor(dofSettings->m_configurationToViewSRG.m_backBlendFactorDivision8);
  63. SetRadiusMinMax(dofSettings->m_minBokehRadiusDivision8, dofSettings->m_maxBokehRadiusDivision8);
  64. break;
  65. default:
  66. AZ_Assert(false, "DepthOfFieldMaskPass : Failed to get the division number from pass request name for mask.");
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. RPI::FullscreenTrianglePass::FrameBeginInternal(params);
  74. }
  75. void DepthOfFieldMaskPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  76. {
  77. // Update resolution size
  78. const auto attachmentBindings = GetAttachmentBindings();
  79. const RPI::PassAttachmentBinding& attachmentBinding = attachmentBindings[0];
  80. if (attachmentBinding.GetAttachment())
  81. {
  82. RHI::Size size = attachmentBinding.GetAttachment()->m_descriptor.m_image.m_size;
  83. m_inputResolutionInverse[0] = 1.0f / size.m_width;
  84. m_inputResolutionInverse[1] = 1.0f / size.m_height;
  85. }
  86. AZ_Assert(m_shaderResourceGroup, "DepthOfFieldMaskPass %s has a null shader resource group when calling FrameBeginInternal.", GetPathName().GetCStr());
  87. m_shaderResourceGroup->SetConstant(m_blendFactorIndex, m_blendFactor);
  88. m_shaderResourceGroup->SetConstant(m_inputResolutionInverseIndex, m_inputResolutionInverse);
  89. m_shaderResourceGroup->SetConstant(m_radiusMinIndex, m_radiusMin);
  90. m_shaderResourceGroup->SetConstant(m_radiusMaxIndex, m_radiusMax);
  91. BindPassSrg(context, m_shaderResourceGroup);
  92. m_shaderResourceGroup->Compile();
  93. }
  94. void DepthOfFieldMaskPass::SetBlendFactor(const AZStd::array<float, 2>& blendFactor)
  95. {
  96. m_blendFactor = blendFactor;
  97. }
  98. void DepthOfFieldMaskPass::SetRadiusMinMax(float min, float max)
  99. {
  100. m_radiusMin = min;
  101. m_radiusMax = max;
  102. }
  103. } // namespace Render
  104. } // namespace AZ