SMAABasePass.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <Atom/RPI.Public/Pass/PassUtils.h>
  9. #include <Atom/RPI.Public/Pass/PassFactory.h>
  10. #include <Atom/RPI.Public/Pass/FullscreenTrianglePass.h>
  11. #include <Atom/RPI.Public/RenderPipeline.h>
  12. #include <Atom/RPI.Public/RPIUtils.h>
  13. #include <Atom/RPI.Public/View.h>
  14. #include <Atom/RPI.Public/Shader/ShaderVariant.h>
  15. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  16. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  17. #include <Atom/RHI/Factory.h>
  18. #include <Atom/RHI/FrameScheduler.h>
  19. #include <Atom/RHI/DevicePipelineState.h>
  20. #include <AzCore/Asset/AssetCommon.h>
  21. #include <AzCore/Asset/AssetManagerBus.h>
  22. #include <PostProcessing/SMAABasePass.h>
  23. namespace AZ
  24. {
  25. namespace Render
  26. {
  27. SMAABasePass::SMAABasePass(const RPI::PassDescriptor& descriptor)
  28. : AZ::RPI::FullscreenTrianglePass(descriptor)
  29. {
  30. }
  31. SMAABasePass::~SMAABasePass()
  32. {
  33. }
  34. void SMAABasePass::InitializeInternal()
  35. {
  36. FullscreenTrianglePass::InitializeInternal();
  37. AZ_Assert(m_shaderResourceGroup != nullptr, "SMAABasePass %s has a null shader resource group when calling Init.", GetPathName().GetCStr());
  38. UpdateCurrentShaderVariant();
  39. }
  40. void SMAABasePass::CompileResources(const RHI::FrameGraphCompileContext& context)
  41. {
  42. AZ_Assert(m_shaderResourceGroup != nullptr, "SMAABasePass %s has a null shader resource group when calling Compile.", GetPathName().GetCStr());
  43. BindPassSrg(context, m_shaderResourceGroup);
  44. AZ::Vector4 renderTargetMetrics = CalculateRenderTargetMetrics(GetOutputBinding(0).GetAttachment().get());
  45. if (renderTargetMetrics.GetX() != m_renderTargetMetrics.GetX() ||
  46. renderTargetMetrics.GetY() != m_renderTargetMetrics.GetY())
  47. {
  48. m_renderTargetMetrics = renderTargetMetrics;
  49. InvalidateSRG();
  50. }
  51. if (m_needToUpdateShaderVariant)
  52. {
  53. UpdateCurrentShaderVariant();
  54. }
  55. if (m_needToUpdateSRG)
  56. {
  57. UpdateSRG();
  58. m_needToUpdateSRG = false;
  59. }
  60. m_shaderResourceGroup->Compile();
  61. }
  62. void SMAABasePass::UpdateCurrentShaderVariant()
  63. {
  64. auto shaderOption = m_shader->CreateShaderOptionGroup();
  65. GetCurrentShaderOption(shaderOption);
  66. shaderOption.SetUnspecifiedToDefaultValues();
  67. UpdateShaderOptions(shaderOption.GetShaderVariantId());
  68. m_needToUpdateShaderVariant = false;
  69. InvalidateSRG();
  70. }
  71. void SMAABasePass::InvalidateShaderVariant()
  72. {
  73. m_needToUpdateShaderVariant = true;
  74. }
  75. void SMAABasePass::InvalidateSRG()
  76. {
  77. m_needToUpdateSRG = true;
  78. }
  79. AZ::Vector4 SMAABasePass::CalculateRenderTargetMetrics(const RPI::PassAttachment* attachment)
  80. {
  81. AZ_Assert(attachment != nullptr, "Null PassAttachment pointer have been passed in SMAABasePass::CalculateRenderTargetMetrics().");
  82. const RPI::PassAttachmentBinding* sizeSource = attachment->m_sizeSource;
  83. AZ_Assert(sizeSource != nullptr, "Binding sizeSource of attachment is null.");
  84. AZ_Assert(sizeSource->GetAttachment() != nullptr, "Attachment of sizeSource is null.");
  85. AZ::RHI::Size size = sizeSource->GetAttachment()->m_descriptor.m_image.m_size;
  86. return AZ::Vector4(
  87. 1.0f / static_cast<float>(size.m_width),
  88. 1.0f / static_cast<float>(size.m_height),
  89. static_cast<float>(size.m_width),
  90. static_cast<float>(size.m_height));
  91. }
  92. } // namespace Render
  93. } // namespace AZ