SMAAEdgeDetectionPass.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/Image/ImageSystemInterface.h>
  9. #include <Atom/RPI.Public/Pass/PassUtils.h>
  10. #include <Atom/RPI.Public/Pass/PassFactory.h>
  11. #include <Atom/RPI.Public/Pass/FullscreenTrianglePass.h>
  12. #include <Atom/RPI.Public/RenderPipeline.h>
  13. #include <Atom/RPI.Public/RPIUtils.h>
  14. #include <Atom/RPI.Public/View.h>
  15. #include <Atom/RPI.Public/Shader/ShaderVariant.h>
  16. #include <Atom/RPI.Reflect/Pass/PassTemplate.h>
  17. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  18. #include <Atom/RHI/Factory.h>
  19. #include <Atom/RHI/FrameScheduler.h>
  20. #include <Atom/RHI/DevicePipelineState.h>
  21. #include <AzCore/Asset/AssetCommon.h>
  22. #include <AzCore/Asset/AssetManagerBus.h>
  23. #include <PostProcessing/SMAACommon.h>
  24. #include <PostProcessing/SMAAEdgeDetectionPass.h>
  25. namespace AZ
  26. {
  27. namespace Render
  28. {
  29. RPI::Ptr<SMAAEdgeDetectionPass> SMAAEdgeDetectionPass::Create(const RPI::PassDescriptor& descriptor)
  30. {
  31. return aznew SMAAEdgeDetectionPass(descriptor);
  32. }
  33. SMAAEdgeDetectionPass::SMAAEdgeDetectionPass(const RPI::PassDescriptor& descriptor)
  34. : SMAABasePass(descriptor)
  35. , m_enablePredicationFeatureOptionName(EnablePredicationFeatureOptionName)
  36. , m_edgeDetectionModeOptionName(EdgeDetectionModeOptionName)
  37. {
  38. }
  39. void SMAAEdgeDetectionPass::InitializeInternal()
  40. {
  41. SMAABasePass::InitializeInternal();
  42. m_renderTargetMetricsShaderInputIndex.Reset();
  43. m_chromaThresholdShaderInputIndex.Reset();
  44. m_depthThresholdShaderInputIndex.Reset();
  45. m_localContrastAdaptationFactorShaderInputIndex.Reset();
  46. m_predicationThresholdShaderInputIndex.Reset();
  47. m_predicationScaleShaderInputIndex.Reset();
  48. m_predicationStrengthShaderInputIndex.Reset();
  49. }
  50. void SMAAEdgeDetectionPass::UpdateSRG()
  51. {
  52. m_shaderResourceGroup->SetConstant(m_renderTargetMetricsShaderInputIndex, m_renderTargetMetrics);
  53. m_shaderResourceGroup->SetConstant(m_chromaThresholdShaderInputIndex, m_chromaThreshold);
  54. m_shaderResourceGroup->SetConstant(m_depthThresholdShaderInputIndex, m_depthThreshold);
  55. m_shaderResourceGroup->SetConstant(m_localContrastAdaptationFactorShaderInputIndex, m_localContrastAdaptationFactor);
  56. m_shaderResourceGroup->SetConstant(m_predicationThresholdShaderInputIndex, m_predicationThreshold);
  57. m_shaderResourceGroup->SetConstant(m_predicationScaleShaderInputIndex, m_predicationScale);
  58. m_shaderResourceGroup->SetConstant(m_predicationStrengthShaderInputIndex, m_predicationStrength);
  59. }
  60. void SMAAEdgeDetectionPass::GetCurrentShaderOption(AZ::RPI::ShaderOptionGroup& shaderOption) const
  61. {
  62. shaderOption.SetValue(m_enablePredicationFeatureOptionName, m_predicationEnable ? AZ::Name("true") : AZ::Name("false"));
  63. switch (m_edgeDetectionMode)
  64. {
  65. case SMAAEdgeDetectionMode::Depth:
  66. shaderOption.SetValue(m_edgeDetectionModeOptionName, AZ::Name("EdgeDetectionMode::Depth"));
  67. break;
  68. case SMAAEdgeDetectionMode::Luma:
  69. shaderOption.SetValue(m_edgeDetectionModeOptionName, AZ::Name("EdgeDetectionMode::Luma"));
  70. break;
  71. case SMAAEdgeDetectionMode::Color:
  72. default:
  73. shaderOption.SetValue(m_edgeDetectionModeOptionName, AZ::Name("EdgeDetectionMode::Color"));
  74. break;
  75. }
  76. }
  77. void SMAAEdgeDetectionPass::SetEdgeDetectionMode(SMAAEdgeDetectionMode mode)
  78. {
  79. if (m_edgeDetectionMode != mode)
  80. {
  81. m_edgeDetectionMode = mode;
  82. InvalidateShaderVariant();
  83. }
  84. }
  85. void SMAAEdgeDetectionPass::SetChromaThreshold(float threshold)
  86. {
  87. if (m_chromaThreshold != threshold)
  88. {
  89. m_chromaThreshold = threshold;
  90. InvalidateSRG();
  91. }
  92. }
  93. void SMAAEdgeDetectionPass::SetDepthThreshold(float threshold)
  94. {
  95. if (m_depthThreshold != threshold)
  96. {
  97. m_depthThreshold = threshold;
  98. InvalidateSRG();
  99. }
  100. }
  101. void SMAAEdgeDetectionPass::SetLocalContrastAdaptationFactor(float factor)
  102. {
  103. if (m_localContrastAdaptationFactor != factor)
  104. {
  105. m_localContrastAdaptationFactor = factor;
  106. InvalidateSRG();
  107. }
  108. }
  109. void SMAAEdgeDetectionPass::SetPredicationEnable(bool enable)
  110. {
  111. if (m_predicationEnable != enable)
  112. {
  113. m_predicationEnable = enable;
  114. InvalidateShaderVariant();
  115. }
  116. }
  117. void SMAAEdgeDetectionPass::SetPredicationThreshold(float threshold)
  118. {
  119. if (m_predicationThreshold != threshold)
  120. {
  121. m_predicationThreshold = threshold;
  122. InvalidateSRG();
  123. }
  124. }
  125. void SMAAEdgeDetectionPass::SetPredicationScale(float scale)
  126. {
  127. if (m_predicationScale != scale)
  128. {
  129. m_predicationScale = scale;
  130. InvalidateSRG();
  131. }
  132. }
  133. void SMAAEdgeDetectionPass::SetPredicationStrength(float strength)
  134. {
  135. if (m_predicationStrength != strength)
  136. {
  137. m_predicationStrength = strength;
  138. InvalidateSRG();
  139. }
  140. }
  141. } // namespace Render
  142. } // namespace AZ