FullscreenShadowPass.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/PipelineState.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_blendBetweenCascadesEnable;
  70. int m_receiverShadowPlaneBiasEnable;
  71. } constantData;
  72. const RHI::Size resolution = GetDepthBufferDimensions();
  73. constantData.m_lightIndex = m_lightIndex;
  74. constantData.m_screenSize = { static_cast<float>(resolution.m_width), static_cast<float>(resolution.m_height) };
  75. constantData.m_filterMode = static_cast<int>(m_filterMethod);
  76. constantData.m_blendBetweenCascadesEnable = m_blendBetweenCascadesEnable ? 1 : 0;
  77. constantData.m_receiverShadowPlaneBiasEnable = m_receiverShadowPlaneBiasEnable ? 1 : 0;
  78. [[maybe_unused]] bool setOk = m_shaderResourceGroup->SetConstant(m_constantDataIndex, constantData);
  79. AZ_Assert(setOk, "FullscreenShadowPass::SetConstantData() - could not set constant data");
  80. }
  81. } // namespace Render
  82. } // namespace AZ