SilhouetteFeatureProcessor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/Pass.h>
  9. #include <Atom/RPI.Public/Pass/PassFilter.h>
  10. #include <Atom/RPI.Public/Pass/RasterPass.h>
  11. #include <Atom/RPI.Public/View.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <Atom/RPI.Public/RenderPipeline.h>
  14. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawInterface.h>
  15. #include <AzCore/Console/IConsole.h>
  16. #include <AzFramework/Entity/GameEntityContextBus.h>
  17. #include <AzFramework/Scene/Scene.h>
  18. #include <AzFramework/Scene/SceneSystemInterface.h>
  19. #include <Silhouette/SilhouetteFeatureProcessor.h>
  20. AZ_CVAR(
  21. bool,
  22. r_silhouette,
  23. false,
  24. nullptr,
  25. AZ::ConsoleFunctorFlags::Null,
  26. "Controls if the silhouette rendering feature is active. 0 : Inactive, 1 : Active (default)");
  27. namespace AZ::Render
  28. {
  29. SilhouetteFeatureProcessor::SilhouetteFeatureProcessor() = default;
  30. SilhouetteFeatureProcessor::~SilhouetteFeatureProcessor() = default;
  31. void SilhouetteFeatureProcessor::Reflect(ReflectContext* context)
  32. {
  33. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  34. {
  35. serializeContext->Class<SilhouetteFeatureProcessor, FeatureProcessor>()->Version(0);
  36. }
  37. }
  38. void SilhouetteFeatureProcessor::Activate()
  39. {
  40. EnableSceneNotification();
  41. }
  42. void SilhouetteFeatureProcessor::Deactivate()
  43. {
  44. DisableSceneNotification();
  45. m_rasterPass = nullptr;
  46. m_compositePass = nullptr;
  47. }
  48. void SilhouetteFeatureProcessor::SetPassesEnabled(bool enabled)
  49. {
  50. if (m_compositePass && m_rasterPass)
  51. {
  52. m_compositePass->SetEnabled(enabled);
  53. m_rasterPass->SetEnabled(enabled);
  54. }
  55. }
  56. void SilhouetteFeatureProcessor::OnRenderEnd()
  57. {
  58. SetPassesEnabled(r_silhouette);
  59. }
  60. void SilhouetteFeatureProcessor::AddRenderPasses(RPI::RenderPipeline* renderPipeline)
  61. {
  62. // Early return if pass is already found in render pipeline or if the pipeline is not the default one.
  63. if (renderPipeline->GetViewType() != RPI::ViewType::Default)
  64. {
  65. return;
  66. }
  67. // the silhouette passes are already added in another render pipeline
  68. if (m_renderPipeline && m_renderPipeline != renderPipeline)
  69. {
  70. return;
  71. }
  72. UpdatePasses(renderPipeline);
  73. // if we already have valid render pass members then we don't need to dynamically
  74. // create them and add them to the pipeline
  75. if (m_compositePass && m_rasterPass)
  76. {
  77. return;
  78. }
  79. // unset the render pipeline until we add the passes successfully
  80. m_renderPipeline = nullptr;
  81. const auto mergeTemplateName = Name("SilhouettePassTemplate");
  82. const auto gatherTemplateName = Name("SilhouetteGatherPassTemplate");
  83. Name postProcessPassName = Name("PostProcessPass");
  84. if (renderPipeline->FindFirstPass(postProcessPassName) == nullptr)
  85. {
  86. AZ_Warning("SilhouetteFeatureProcessor", false, "Can't find %s in the render pipeline.", postProcessPassName.GetCStr());
  87. return;
  88. }
  89. Name forwardProcessPassName = Name("Forward");
  90. if (renderPipeline->FindFirstPass(forwardProcessPassName) == nullptr)
  91. {
  92. AZ_Warning("SilhouetteFeatureProcessor", false, "Can't find %s in the render pipeline.", forwardProcessPassName.GetCStr());
  93. return;
  94. }
  95. // Add the gather pass which draws all the silhouette objects into a render target
  96. // using depth and stencil to determine where to draw
  97. RPI::PassRequest gatherPassRequest;
  98. gatherPassRequest.m_passName = Name("SilhouetteGatherPass");
  99. gatherPassRequest.m_templateName = gatherTemplateName;
  100. gatherPassRequest.m_passEnabled = r_silhouette;
  101. gatherPassRequest.AddInputConnection(RPI::PassConnection{
  102. Name("DepthStencilInputOutput"), RPI::PassAttachmentRef{ forwardProcessPassName, Name("DepthStencilInputOutput") } });
  103. if (auto pass = RPI::PassSystemInterface::Get()->CreatePassFromRequest(&gatherPassRequest); pass != nullptr)
  104. {
  105. m_rasterPass = static_cast<AZ::RPI::RasterPass*>(pass.get());
  106. renderPipeline->AddPassAfter(pass, forwardProcessPassName);
  107. }
  108. // Add the full screen silhouette pass which merges the silhouettes render target with
  109. // the framebuffer diffuse, and adds outlines to the silhouette shapes
  110. RPI::PassRequest compositePassRequest;
  111. compositePassRequest.m_passName = Name("SilhouettePass");
  112. compositePassRequest.m_templateName = mergeTemplateName;
  113. compositePassRequest.m_passEnabled = r_silhouette;
  114. compositePassRequest.AddInputConnection(
  115. RPI::PassConnection{ Name("InputOutput"), RPI::PassAttachmentRef{ postProcessPassName, Name("Output") } });
  116. if (auto pass = RPI::PassSystemInterface::Get()->CreatePassFromRequest(&compositePassRequest); pass != nullptr)
  117. {
  118. m_compositePass = pass.get();
  119. renderPipeline->AddPassAfter(pass, postProcessPassName);
  120. }
  121. // remember which render pipeline we added our passes to
  122. m_renderPipeline = renderPipeline;
  123. }
  124. void SilhouetteFeatureProcessor::OnRenderPipelineChanged(AZ::RPI::RenderPipeline* pipeline, AZ::RPI::SceneNotification::RenderPipelineChangeType changeType)
  125. {
  126. // Only need to recache silhouette passes if the pipeline had them
  127. if (pipeline == m_renderPipeline)
  128. {
  129. if (changeType == AZ::RPI::SceneNotification::RenderPipelineChangeType::Removed)
  130. {
  131. UpdatePasses(nullptr);
  132. }
  133. else
  134. {
  135. UpdatePasses(pipeline);
  136. }
  137. }
  138. }
  139. bool SilhouetteFeatureProcessor::NeedsCompositePass() const
  140. {
  141. if (auto scene = GetParentScene(); scene != nullptr)
  142. {
  143. if (m_rasterPass && m_rasterPass->GetRenderPipeline())
  144. {
  145. // Get DrawList from the dynamic draw interface and view
  146. AZStd::vector<RHI::DrawListView> drawLists = AZ::RPI::DynamicDrawInterface::Get()->GetDrawListsForPass(m_rasterPass);
  147. const AZStd::vector<AZ::RPI::ViewPtr>& views =
  148. m_rasterPass->GetRenderPipeline()->GetViews(m_rasterPass->GetPipelineViewTag());
  149. RHI::DrawListView viewDrawList;
  150. if (!views.empty())
  151. {
  152. const AZ::RPI::ViewPtr& view = views.front();
  153. viewDrawList = view->GetDrawList(m_rasterPass->GetDrawListTag());
  154. }
  155. return !(drawLists.empty() && viewDrawList.empty());
  156. }
  157. }
  158. return false;
  159. }
  160. void SilhouetteFeatureProcessor::UpdatePasses(AZ::RPI::RenderPipeline* renderPipeline)
  161. {
  162. m_compositePass = nullptr;
  163. m_rasterPass = nullptr;
  164. if (renderPipeline == nullptr)
  165. {
  166. m_renderPipeline = nullptr;
  167. return;
  168. }
  169. const auto mergeTemplateName = Name("SilhouettePassTemplate");
  170. auto compositePassFilter = AZ::RPI::PassFilter::CreateWithTemplateName(mergeTemplateName, renderPipeline);
  171. if (auto foundPass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(compositePassFilter); foundPass)
  172. {
  173. m_compositePass = foundPass;
  174. }
  175. const auto gatherTemplateName = Name("SilhouetteGatherPassTemplate");
  176. auto gatherPassFilter = AZ::RPI::PassFilter::CreateWithTemplateName(gatherTemplateName, renderPipeline);
  177. if (auto foundPass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(gatherPassFilter); foundPass)
  178. {
  179. m_rasterPass = static_cast<AZ::RPI::RasterPass*>(foundPass);
  180. }
  181. // remember which render pipeline we found our passes on
  182. m_renderPipeline = (m_compositePass && m_rasterPass) ? renderPipeline : nullptr;
  183. }
  184. } // namespace AZ::Render