FastDepthAwareBlurPasses.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/FastDepthAwareBlurPasses.h>
  9. #include <AzCore/Math/MathUtils.h>
  10. #include <Atom/RPI.Public/RenderPipeline.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. #include <Atom/RPI.Public/View.h>
  13. namespace AZ
  14. {
  15. namespace Render
  16. {
  17. // --- COMMON ---
  18. void FastDepthAwareBlurPassConstants::InitializeFromSize(RHI::Size outputTextureSize)
  19. {
  20. m_outputSize[0] = outputTextureSize.m_width;
  21. m_outputSize[1] = outputTextureSize.m_height;
  22. m_pixelSize[0] = 1.0f / float(outputTextureSize.m_width);
  23. m_pixelSize[1] = 1.0f / float(outputTextureSize.m_height);
  24. m_halfPixelSize[0] = 0.5f * m_pixelSize[0];
  25. m_halfPixelSize[1] = 0.5f * m_pixelSize[1];
  26. }
  27. void FastDepthAwareBlurPassConstants::SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength)
  28. {
  29. m_constFalloff = constFalloff;
  30. m_depthFalloffThreshold = depthFalloffThreshold;
  31. m_depthFalloffStrength = depthFalloffStrength;
  32. }
  33. // --- HORIZONTAL BLUR ---
  34. RPI::Ptr<FastDepthAwareBlurHorPass> FastDepthAwareBlurHorPass::Create(const RPI::PassDescriptor& descriptor)
  35. {
  36. RPI::Ptr<FastDepthAwareBlurHorPass> pass = aznew FastDepthAwareBlurHorPass(descriptor);
  37. return AZStd::move(pass);
  38. }
  39. FastDepthAwareBlurHorPass::FastDepthAwareBlurHorPass(const RPI::PassDescriptor& descriptor)
  40. : RPI::ComputePass(descriptor)
  41. {
  42. // Though this is a fullscreen pass, the algorithm used makes each thread output 3 blurred pixels, so
  43. // it's not a 1-to-1 ratio and requires custom calculation of target thread counts
  44. m_fullscreenDispatch = false;
  45. }
  46. void FastDepthAwareBlurHorPass::SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength)
  47. {
  48. m_passConstants.m_constFalloff = constFalloff;
  49. m_passConstants.m_depthFalloffThreshold = depthFalloffThreshold;
  50. m_passConstants.m_depthFalloffStrength = depthFalloffStrength;
  51. }
  52. void FastDepthAwareBlurHorPass::FrameBeginInternal(FramePrepareParams params)
  53. {
  54. AZ_Assert(GetOutputCount() > 0, "FastDepthAwareBlurHorPass: No output bindings!");
  55. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  56. AZ_Assert(outputAttachment != nullptr, "FastDepthAwareBlurHorPass: Output binding has no attachment!");
  57. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  58. m_passConstants.InitializeFromSize(size);
  59. m_shaderResourceGroup->SetConstant(m_constantsIndex, m_passConstants);
  60. // The algorithm has each thread output three pixels in the blur direction
  61. u32 targetThreadCountX = (size.m_width + 2) / 3;
  62. SetTargetThreadCounts(targetThreadCountX, size.m_height, 1);
  63. RPI::ComputePass::FrameBeginInternal(params);
  64. }
  65. // --- VERTICAL BLUR ---
  66. RPI::Ptr<FastDepthAwareBlurVerPass> FastDepthAwareBlurVerPass::Create(const RPI::PassDescriptor& descriptor)
  67. {
  68. RPI::Ptr<FastDepthAwareBlurVerPass> pass = aznew FastDepthAwareBlurVerPass(descriptor);
  69. return AZStd::move(pass);
  70. }
  71. FastDepthAwareBlurVerPass::FastDepthAwareBlurVerPass(const RPI::PassDescriptor& descriptor)
  72. : RPI::ComputePass(descriptor)
  73. {
  74. // Though this is a fullscreen pass, the algorithm used makes each thread output 3 blurred pixels, so
  75. // it's not a 1-to-1 ratio and requires custom calculation of target thread counts
  76. m_fullscreenDispatch = false;
  77. }
  78. void FastDepthAwareBlurVerPass::SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength)
  79. {
  80. m_passConstants.m_constFalloff = constFalloff;
  81. m_passConstants.m_depthFalloffThreshold = depthFalloffThreshold;
  82. m_passConstants.m_depthFalloffStrength = depthFalloffStrength;
  83. }
  84. void FastDepthAwareBlurVerPass::FrameBeginInternal(FramePrepareParams params)
  85. {
  86. AZ_Assert(GetOutputCount() > 0, "FastDepthAwareBlurVerPass: No output bindings!");
  87. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  88. AZ_Assert(outputAttachment != nullptr, "FastDepthAwareBlurVerPass: Output binding has no attachment!");
  89. RHI::Size size = outputAttachment->m_descriptor.m_image.m_size;
  90. m_passConstants.InitializeFromSize(size);
  91. m_shaderResourceGroup->SetConstant(m_constantsIndex, m_passConstants);
  92. // The algorithm has each thread output three pixels in the blur direction
  93. u32 targetThreadCountY = (size.m_height + 2) / 3;
  94. SetTargetThreadCounts(size.m_width, targetThreadCountY, 1);
  95. RPI::ComputePass::FrameBeginInternal(params);
  96. }
  97. } // namespace Render
  98. } // namespace AZ