FastDepthAwareBlurPasses.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #pragma once
  9. #include <Atom/RPI.Public/Pass/ComputePass.h>
  10. #include <Atom/RPI.Public/Pass/ParentPass.h>
  11. namespace AZ
  12. {
  13. namespace Render
  14. {
  15. //! Must match the struct in FastDepthAwareBlurHor.azsl and FastDepthAwareBlurVer.azsl
  16. struct FastDepthAwareBlurPassConstants
  17. {
  18. //! The texture dimensions of blur output
  19. AZStd::array<u32, 2> m_outputSize;
  20. //! The size of a pixel relative to screenspace UV
  21. //! Calculated by taking the inverse of the texture dimensions
  22. AZStd::array<float, 2> m_pixelSize;
  23. //! The size of half a pixel relative to screenspace UV
  24. AZStd::array<float, 2> m_halfPixelSize;
  25. //! How much a value is reduced from pixel to pixel on a perfectly flat surface
  26. float m_constFalloff = 2.0f / 3.0f;
  27. //! Threshold used to reduce computed depth difference during blur and thus the depth falloff
  28. //! Can be thought of as a bias that blurs curved surfaces more like flat surfaces
  29. //! but generally not needed and can be set to 0.0f
  30. float m_depthFalloffThreshold = 0.0f;
  31. //! How much the difference in depth slopes between pixels affects the blur falloff.
  32. //! The higher this value, the sharper edges will appear
  33. float m_depthFalloffStrength = 50.0f;
  34. u32 PADDING[3];
  35. //! Calculates size constants based on texture size input
  36. void InitializeFromSize(RHI::Size outputTextureSize);
  37. //! Sets the constants to be forwarded to the shader
  38. void SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength);
  39. };
  40. //! Pass for an optimized horizontal blur with edge detection
  41. class FastDepthAwareBlurHorPass final
  42. : public RPI::ComputePass
  43. {
  44. AZ_RPI_PASS(FastDepthAwareBlurHorPass);
  45. public:
  46. AZ_RTTI(AZ::Render::FastDepthAwareBlurHorPass, "{934F3772-06DA-42E3-A305-2921FFCEDCD4}", AZ::RPI::ComputePass);
  47. AZ_CLASS_ALLOCATOR(FastDepthAwareBlurHorPass, SystemAllocator);
  48. virtual ~FastDepthAwareBlurHorPass() = default;
  49. //! Creates an FastDepthAwareBlurHorPass
  50. static RPI::Ptr<FastDepthAwareBlurHorPass> Create(const RPI::PassDescriptor& descriptor);
  51. //! Sets the constants to be forwarded to the shader
  52. void SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength);
  53. protected:
  54. // Behavior functions override...
  55. void FrameBeginInternal(FramePrepareParams params) override;
  56. private:
  57. FastDepthAwareBlurHorPass(const RPI::PassDescriptor& descriptor);
  58. // SRG binding indices...
  59. AZ::RHI::ShaderInputNameIndex m_constantsIndex = "m_constants";
  60. FastDepthAwareBlurPassConstants m_passConstants;
  61. };
  62. //! Pass for an optimized vertical blur with edge detection
  63. class FastDepthAwareBlurVerPass final
  64. : public RPI::ComputePass
  65. {
  66. AZ_RPI_PASS(FastDepthAwareBlurVerPass);
  67. public:
  68. AZ_RTTI(AZ::Render::FastDepthAwareBlurVerPass, "{0DCB71EB-5417-4351-AADE-444DBCDF980E}", AZ::RPI::ComputePass);
  69. AZ_CLASS_ALLOCATOR(FastDepthAwareBlurVerPass, SystemAllocator);
  70. virtual ~FastDepthAwareBlurVerPass() = default;
  71. //! Creates an FastDepthAwareBlurVerPass
  72. static RPI::Ptr<FastDepthAwareBlurVerPass> Create(const RPI::PassDescriptor& descriptor);
  73. void SetConstants(float constFalloff, float depthFalloffThreshold, float depthFalloffStrength);
  74. protected:
  75. // Behavior functions override...
  76. void FrameBeginInternal(FramePrepareParams params) override;
  77. private:
  78. FastDepthAwareBlurVerPass(const RPI::PassDescriptor& descriptor);
  79. // SRG binding indices...
  80. AZ::RHI::ShaderInputNameIndex m_constantsIndex = "m_constants";
  81. FastDepthAwareBlurPassConstants m_passConstants;
  82. };
  83. } // namespace Render
  84. } // namespace AZ