DepthUpsamplePass.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <PostProcess/PostProcessFeatureProcessor.h>
  9. #include <PostProcessing/DepthUpsamplePass.h>
  10. #include <AzCore/Math/MathUtils.h>
  11. #include <Atom/RPI.Public/RenderPipeline.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <Atom/RPI.Public/View.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. RPI::Ptr<DepthUpsamplePass> DepthUpsamplePass::Create(const RPI::PassDescriptor& descriptor)
  19. {
  20. RPI::Ptr<DepthUpsamplePass> pass = aznew DepthUpsamplePass(descriptor);
  21. return AZStd::move(pass);
  22. }
  23. DepthUpsamplePass::DepthUpsamplePass(const RPI::PassDescriptor& descriptor)
  24. : RPI::ComputePass(descriptor)
  25. { }
  26. void DepthUpsamplePass::FrameBeginInternal(FramePrepareParams params)
  27. {
  28. // Must match the struct in DepthUpsample.azsl
  29. struct UpsampleConstants
  30. {
  31. // The size of a pixel in the input image relative to screenspace UV
  32. // Calculated by taking the inverse of the texture dimensions
  33. AZStd::array<float, 2> m_inputPixelSize;
  34. // The size of a pixel in the output image relative to screenspace UV
  35. // Calculated by taking the inverse of the texture dimensions
  36. AZStd::array<float, 2> m_outputPixelSize;
  37. } upsampleConstants{};
  38. AZ_Assert(GetInputCount() == 3, "DepthUpsamplePass requires three inputs!");
  39. AZ_Assert(GetOutputCount() == 1, "DepthUpsamplePass requires one output!");
  40. RPI::PassAttachment* inputAttachment = GetInputBinding(2).GetAttachment().get();
  41. RPI::PassAttachment* outputAttachment = GetOutputBinding(0).GetAttachment().get();
  42. AZ_Assert(inputAttachment != nullptr, "DepthUpsamplePass: Input binding has no attachment!");
  43. AZ_Assert(outputAttachment != nullptr, "DepthUpsamplePass: Output binding has no attachment!");
  44. RHI::Size inputSize = inputAttachment->m_descriptor.m_image.m_size;
  45. RHI::Size outputSize = outputAttachment->m_descriptor.m_image.m_size;
  46. upsampleConstants.m_inputPixelSize[0] = 1.0f / float(inputSize.m_width);
  47. upsampleConstants.m_inputPixelSize[1] = 1.0f / float(inputSize.m_height);
  48. upsampleConstants.m_outputPixelSize[0] = 1.0f / float(outputSize.m_width);
  49. upsampleConstants.m_outputPixelSize[1] = 1.0f / float(outputSize.m_height);
  50. // TargetThreadCount has same dimensions as the input image + 1
  51. // This +1 only applies if the dimension is an even number
  52. // For a more detailed explanation, see the Algorithm Overview section in DepthUpsample.azsl
  53. u32 targetThreadCountX = inputSize.m_width;
  54. u32 targetThreadCountY = inputSize.m_height;
  55. if ((outputSize.m_width & 1) == 0)
  56. {
  57. ++targetThreadCountX;
  58. }
  59. if ((outputSize.m_height & 1) == 0)
  60. {
  61. ++targetThreadCountY;
  62. }
  63. SetTargetThreadCounts(targetThreadCountX, targetThreadCountY, 1);
  64. m_shaderResourceGroup->SetConstant(m_constantsIndex, upsampleConstants);
  65. RPI::ComputePass::FrameBeginInternal(params);
  66. }
  67. } // namespace Render
  68. } // namespace AZ