FullscreenShadowPass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 <Shadows/FullscreenShadowPass.h>
  9. #include <Atom/RHI/Factory.h>
  10. #include <Atom/RHI/DevicePipelineState.h>
  11. #include <Atom/RPI.Public/Pass/PassUtils.h>
  12. #include <Atom/RPI.Public/RenderPipeline.h>
  13. #include <Atom/RPI.Public/RPIUtils.h>
  14. #include <Atom/RHI/RHISystemInterface.h>
  15. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  16. #include <Atom/RPI.Public/View.h>
  17. namespace AZ
  18. {
  19. namespace Render
  20. {
  21. RPI::Ptr<FullscreenShadowPass> FullscreenShadowPass::Create(const RPI::PassDescriptor& descriptor)
  22. {
  23. RPI::Ptr<FullscreenShadowPass> pass = aznew FullscreenShadowPass(descriptor);
  24. return pass;
  25. }
  26. FullscreenShadowPass::FullscreenShadowPass(const RPI::PassDescriptor& descriptor)
  27. : Base(descriptor)
  28. , m_outputName("Output")
  29. , m_depthInputName("Depth")
  30. {
  31. }
  32. void FullscreenShadowPass::InitializeInternal()
  33. {
  34. Base::InitializeInternal();
  35. AZ::Name msaaOptionName = AZ::Name("MsaaMode::MsaaNone");
  36. uint16_t numSamples = GetDepthBufferMSAACount();
  37. if (numSamples > 1)
  38. {
  39. msaaOptionName = AZStd::string::format("MsaaMode::Msaa%ux", numSamples);
  40. }
  41. UpdateShaderOptions(
  42. { { AZ::Name("o_msaaMode"), msaaOptionName} }
  43. );
  44. }
  45. void FullscreenShadowPass::CompileResources(const RHI::FrameGraphCompileContext& context)
  46. {
  47. SetConstantData();
  48. Base::CompileResources(context);
  49. }
  50. RHI::Size FullscreenShadowPass::GetDepthBufferDimensions()
  51. {
  52. RPI::PassAttachmentBinding* outputBinding = RPI::Pass::FindAttachmentBinding(m_outputName);
  53. auto outputDim = outputBinding->GetAttachment()->m_descriptor.m_image.m_size;
  54. AZ_Assert(outputDim.m_width > 0 && outputDim.m_height > 0, "Height and width are not valid\n");
  55. return outputDim;
  56. }
  57. uint16_t FullscreenShadowPass::GetDepthBufferMSAACount()
  58. {
  59. RPI::PassAttachmentBinding* inputBinding = RPI::Pass::FindAttachmentBinding(m_depthInputName);
  60. return inputBinding->GetAttachment()->m_descriptor.m_image.m_multisampleState.m_samples;
  61. }
  62. void FullscreenShadowPass::SetConstantData()
  63. {
  64. struct alignas(16) ConstantData
  65. {
  66. AZStd::array<float, 2> m_screenSize;
  67. int m_lightIndex;
  68. int m_filterMode;
  69. int m_filteringSampleCountMode;
  70. int m_blendBetweenCascadesEnable;
  71. int m_receiverShadowPlaneBiasEnable;
  72. } constantData;
  73. const RHI::Size resolution = GetDepthBufferDimensions();
  74. constantData.m_lightIndex = m_lightIndex;
  75. constantData.m_screenSize = { static_cast<float>(resolution.m_width), static_cast<float>(resolution.m_height) };
  76. constantData.m_filterMode = static_cast<int>(m_filterMethod);
  77. constantData.m_filteringSampleCountMode = static_cast<int>(m_filteringSampleCountMode);
  78. constantData.m_blendBetweenCascadesEnable = m_blendBetweenCascadesEnable ? 1 : 0;
  79. constantData.m_receiverShadowPlaneBiasEnable = m_receiverShadowPlaneBiasEnable ? 1 : 0;
  80. [[maybe_unused]] bool setOk = m_shaderResourceGroup->SetConstant(m_constantDataIndex, constantData);
  81. AZ_Assert(setOk, "FullscreenShadowPass::SetConstantData() - could not set constant data");
  82. }
  83. } // namespace Render
  84. } // namespace AZ