SMAABasePass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/PipelineState.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. if (m_shaderResourceGroup->HasShaderVariantKeyFallbackEntry())
  59. {
  60. m_shaderResourceGroup->SetShaderVariantKeyFallbackValue(m_currentShaderVariantKeyFallbackValue);
  61. }
  62. m_needToUpdateSRG = false;
  63. }
  64. m_shaderResourceGroup->Compile();
  65. }
  66. void SMAABasePass::UpdateCurrentShaderVariant()
  67. {
  68. auto shaderOption = m_shader->CreateShaderOptionGroup();
  69. GetCurrentShaderOption(shaderOption);
  70. m_currentShaderVariantKeyFallbackValue = shaderOption.GetShaderVariantKeyFallbackValue();
  71. m_needToUpdateShaderVariant = false;
  72. InvalidateSRG();
  73. }
  74. void SMAABasePass::InvalidateShaderVariant()
  75. {
  76. m_needToUpdateShaderVariant = true;
  77. }
  78. void SMAABasePass::InvalidateSRG()
  79. {
  80. m_needToUpdateSRG = true;
  81. }
  82. AZ::Vector4 SMAABasePass::CalculateRenderTargetMetrics(const RPI::PassAttachment* attachment)
  83. {
  84. AZ_Assert(attachment != nullptr, "Null PassAttachment pointer have been passed in SMAABasePass::CalculateRenderTargetMetrics().");
  85. const RPI::PassAttachmentBinding* sizeSource = attachment->m_sizeSource;
  86. AZ_Assert(sizeSource != nullptr, "Binding sizeSource of attachment is null.");
  87. AZ_Assert(sizeSource->GetAttachment() != nullptr, "Attachment of sizeSource is null.");
  88. AZ::RHI::Size size = sizeSource->GetAttachment()->m_descriptor.m_image.m_size;
  89. return AZ::Vector4(
  90. 1.0f / static_cast<float>(size.m_width),
  91. 1.0f / static_cast<float>(size.m_height),
  92. static_cast<float>(size.m_width),
  93. static_cast<float>(size.m_height));
  94. }
  95. } // namespace Render
  96. } // namespace AZ