LyShinePass.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #include <AzCore/Debug/Trace.h>
  8. #include <Atom/RHI/DrawListTagRegistry.h>
  9. #include <Atom/RHI/RHISystemInterface.h>
  10. #include <Atom/RPI.Public/Pass/PassAttachment.h>
  11. #include <Atom/RPI.Public/Image/ImageSystemInterface.h>
  12. #include <Atom/RPI.Public/Image/AttachmentImagePool.h>
  13. #include <Atom/RPI.Public/Scene.h>
  14. #include <Atom/RPI.Reflect/Pass/RasterPassData.h>
  15. #include <AzCore/std/iterator.h>
  16. #include "LyShinePass.h"
  17. namespace LyShine
  18. {
  19. AZ::RPI::Ptr<LyShinePass> LyShinePass::Create(const AZ::RPI::PassDescriptor& descriptor)
  20. {
  21. return aznew LyShinePass(descriptor);
  22. }
  23. LyShinePass::LyShinePass(const AZ::RPI::PassDescriptor& descriptor)
  24. : Base(descriptor)
  25. {
  26. }
  27. LyShinePass::~LyShinePass()
  28. {
  29. LyShinePassRequestBus::Handler::BusDisconnect();
  30. }
  31. void LyShinePass::ResetInternal()
  32. {
  33. LyShinePassRequestBus::Handler::BusDisconnect();
  34. Base::ResetInternal();
  35. }
  36. void LyShinePass::SetRenderPipeline(AZ::RPI::RenderPipeline* pipeline)
  37. {
  38. if (pipeline == nullptr)
  39. {
  40. // The pipeline being set to null means this pass will soon be destroyed. Disconnect from the bus so if a
  41. // new LyShinePass is being created to replace it, it will be able to connect.
  42. LyShinePassRequestBus::Handler::BusDisconnect();
  43. }
  44. ParentPass::SetRenderPipeline(pipeline);
  45. }
  46. void LyShinePass::BuildInternal()
  47. {
  48. AZ::RPI::Scene* scene = GetScene();
  49. if (scene)
  50. {
  51. // Listen for rebuild requests
  52. LyShinePassRequestBus::Handler::BusConnect(scene->GetId());
  53. }
  54. // Always recreate children when rebuild the pass
  55. m_flags.m_createChildren = true;
  56. Base::BuildInternal();
  57. }
  58. void LyShinePass::CreateChildPassesInternal()
  59. {
  60. AZ::RPI::Scene* scene = GetScene();
  61. if (scene)
  62. {
  63. // Get the current list of render targets being used across all loaded UI Canvases
  64. LyShine::AttachmentImagesAndDependencies attachmentImagesAndDependencies;
  65. LyShinePassDataRequestBus::EventResult(
  66. attachmentImagesAndDependencies,
  67. scene->GetId(),
  68. &LyShinePassDataRequestBus::Events::GetRenderTargets
  69. );
  70. AddRttChildPasses(attachmentImagesAndDependencies);
  71. AddUiCanvasChildPass(attachmentImagesAndDependencies);
  72. }
  73. }
  74. void LyShinePass::RebuildRttChildren()
  75. {
  76. QueueForBuildAndInitialization();
  77. }
  78. AZ::RPI::RasterPass* LyShinePass::GetRttPass(const AZStd::string& name)
  79. {
  80. for (auto child:m_children)
  81. {
  82. if (child->GetName() == AZ::Name(name))
  83. {
  84. return azrtti_cast<AZ::RPI::RasterPass*>(child.get());
  85. }
  86. }
  87. return nullptr;
  88. }
  89. AZ::RPI::RasterPass* LyShinePass::GetUiCanvasPass()
  90. {
  91. return m_uiCanvasChildPass.get();
  92. }
  93. void LyShinePass::AddRttChildPasses(LyShine::AttachmentImagesAndDependencies attachmentImagesAndDependencies)
  94. {
  95. for (const auto& attachmentImageAndDependencies : attachmentImagesAndDependencies)
  96. {
  97. AddRttChildPass(attachmentImageAndDependencies.first, attachmentImageAndDependencies.second);
  98. }
  99. }
  100. void LyShinePass::AddRttChildPass(AZ::Data::Instance<AZ::RPI::AttachmentImage> attachmentImage, AttachmentImages attachmentImageDependencies)
  101. {
  102. // Add a pass that renders to the specified texture
  103. AZ::RPI::PassSystemInterface* passSystem = AZ::RPI::PassSystemInterface::Get();
  104. if (attachmentImage)
  105. {
  106. auto passName = attachmentImage->GetRHIImage()->GetName(); // Use attachment name (but not attachment id) as pass name so the pass can be found by GetRttPass() function
  107. AZ::RPI::Ptr<RttChildPass> rttChildPass = azrtti_cast<RttChildPass*>(passSystem->CreatePassFromTemplate(AZ::Name("RttChildPassTemplate"), passName).get());
  108. AZ_Assert(rttChildPass, "[LyShinePass] Unable to create a RttChildPass.");
  109. // Store the info needed to attach to slots and set up frame graph dependencies
  110. rttChildPass->m_attachmentImage = attachmentImage;
  111. rttChildPass->m_attachmentImageDependencies = attachmentImageDependencies;
  112. // Disable by default, the RenderGraph will enable it when render to render target
  113. rttChildPass->SetEnabled(false);
  114. AddChild(rttChildPass);
  115. }
  116. }
  117. void LyShinePass::AddUiCanvasChildPass(LyShine::AttachmentImagesAndDependencies AttachmentImagesAndDependencies)
  118. {
  119. if (!m_uiCanvasChildPass)
  120. {
  121. AZ::RPI::PassSystemInterface* passSystem = AZ::RPI::PassSystemInterface::Get();
  122. m_uiCanvasChildPass = azrtti_cast<LyShineChildPass*>(passSystem->CreatePassFromTemplate(AZ::Name("LyShineChildPassTemplate"), AZ::Name("LyShineChildPass")).get());
  123. AZ_Assert(m_uiCanvasChildPass, "[LyShinePass] Unable to create a LyShineChildPass.");
  124. }
  125. // Store the info needed to set up frame graph dependencies
  126. m_uiCanvasChildPass->m_attachmentImageDependencies.clear();
  127. for (const auto& attachmentImageAndDescendents : AttachmentImagesAndDependencies)
  128. {
  129. m_uiCanvasChildPass->m_attachmentImageDependencies.emplace_back(attachmentImageAndDescendents.first);
  130. }
  131. AddChild(m_uiCanvasChildPass);
  132. }
  133. AZ::RPI::Ptr<LyShineChildPass> LyShineChildPass::Create(const AZ::RPI::PassDescriptor& descriptor)
  134. {
  135. return aznew LyShineChildPass(descriptor);
  136. }
  137. LyShineChildPass::LyShineChildPass(const AZ::RPI::PassDescriptor& descriptor)
  138. : RasterPass(descriptor)
  139. {
  140. }
  141. LyShineChildPass::~LyShineChildPass()
  142. {
  143. }
  144. void LyShineChildPass::SetupFrameGraphDependencies(AZ::RHI::FrameGraphInterface frameGraph)
  145. {
  146. AZ::RPI::RasterPass::SetupFrameGraphDependencies(frameGraph);
  147. for (auto attachmentImage : m_attachmentImageDependencies)
  148. {
  149. // Ensure that the image is imported into the attachment database.
  150. // The image may not be imported if the owning pass has been disabled.
  151. auto attachmentImageId = attachmentImage->GetAttachmentId();
  152. if (!frameGraph.GetAttachmentDatabase().IsAttachmentValid(attachmentImageId))
  153. {
  154. frameGraph.GetAttachmentDatabase().ImportImage(attachmentImageId, attachmentImage->GetRHIImage());
  155. }
  156. AZ::RHI::ImageScopeAttachmentDescriptor desc;
  157. desc.m_attachmentId = attachmentImageId;
  158. desc.m_imageViewDescriptor = attachmentImage->GetImageView()->GetDescriptor();
  159. desc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  160. frameGraph.UseShaderAttachment(desc, AZ::RHI::ScopeAttachmentAccess::Read, AZ::RHI::ScopeAttachmentStage::FragmentShader);
  161. }
  162. }
  163. AZ::RPI::Ptr<RttChildPass> RttChildPass::Create(const AZ::RPI::PassDescriptor& descriptor)
  164. {
  165. return aznew RttChildPass(descriptor);
  166. }
  167. RttChildPass::RttChildPass(const AZ::RPI::PassDescriptor& descriptor)
  168. : LyShineChildPass(descriptor)
  169. {
  170. }
  171. RttChildPass::~RttChildPass()
  172. {
  173. }
  174. void RttChildPass::BuildInternal()
  175. {
  176. AttachImageToSlot(AZ::Name("RenderTargetOutput"), m_attachmentImage);
  177. auto imageSize = m_attachmentImage->GetDescriptor().m_size;
  178. // use render target's size to setup override scissor and viewport
  179. m_scissorState = AZ::RHI::Scissor(0, 0, imageSize.m_width, imageSize.m_height);
  180. m_viewportState = AZ::RHI::Viewport(0.f, aznumeric_cast<float>(imageSize.m_width), 0.f, aznumeric_cast<float>(imageSize.m_height));
  181. m_overrideScissorSate = false;
  182. m_overrideViewportState = false;
  183. }
  184. } // namespace LyShine