MultiGPURPIExampleComponent.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <AtomSampleViewerOptions.h>
  9. #include <MultiGPURPIExampleComponent.h>
  10. #include <Atom/Component/DebugCamera/CameraComponent.h>
  11. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  12. #include <Atom/RHI/RHISystemInterface.h>
  13. #include <Atom/RPI.Public/Pass/CopyPass.h>
  14. #include <Atom/RPI.Public/Pass/PassFilter.h>
  15. #include <Atom/RPI.Public/RPISystemInterface.h>
  16. #include <Atom/RPI.Public/RenderPipeline.h>
  17. #include <Atom/RPI.Public/Scene.h>
  18. #include <Atom/RPI.Public/ViewProviderBus.h>
  19. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  20. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  21. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  22. #include <AzCore/Math/MatrixUtils.h>
  23. #include <Automation/ScriptableImGui.h>
  24. #include <Automation/ScriptRunnerBus.h>
  25. #include <AzCore/Component/Entity.h>
  26. #include <AzFramework/Components/TransformComponent.h>
  27. #include <AzFramework/Scene/SceneSystemInterface.h>
  28. #include <AzFramework/Entity/GameEntityContextComponent.h>
  29. #include <EntityUtilityFunctions.h>
  30. #include <SampleComponentConfig.h>
  31. #include <SampleComponentManager.h>
  32. #include <Utils/Utils.h>
  33. #include <RHI/BasicRHIComponent.h>
  34. namespace AtomSampleViewer
  35. {
  36. using namespace AZ;
  37. void MultiGPURPIExampleComponent::Reflect(AZ::ReflectContext* context)
  38. {
  39. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  40. {
  41. serializeContext->Class<MultiGPURPIExampleComponent, AZ::Component>()
  42. ->Version(0)
  43. ;
  44. }
  45. }
  46. MultiGPURPIExampleComponent::MultiGPURPIExampleComponent()
  47. {
  48. }
  49. void MultiGPURPIExampleComponent::Activate()
  50. {
  51. AZ::TickBus::Handler::BusConnect();
  52. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  53. // save original render pipeline first and remove it from the scene
  54. m_originalPipeline = m_scene->GetDefaultRenderPipeline();
  55. m_scene->RemoveRenderPipeline(m_originalPipeline->GetId());
  56. // add the multi-GPU pipeline
  57. const AZStd::string pipelineName("MultiGPUPipeline");
  58. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  59. pipelineDesc.m_name = pipelineName;
  60. pipelineDesc.m_rootPassTemplate = "MultiGPUPipeline";
  61. m_pipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  62. m_scene->AddRenderPipeline(m_pipeline);
  63. const AZStd::string copyPipelineName("MultiGPUCopyTestPipeline");
  64. AZ::RPI::RenderPipelineDescriptor copyPipelineDesc;
  65. copyPipelineDesc.m_name = copyPipelineName;
  66. copyPipelineDesc.m_rootPassTemplate = "MultiGPUCopyTestPipeline";
  67. m_copyPipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(copyPipelineDesc, *m_windowContext);
  68. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ "MultiGPUPipeline", "ImGuiPass" });
  69. }
  70. void MultiGPURPIExampleComponent::Deactivate()
  71. {
  72. // remove cb pipeline before adding original pipeline.
  73. if (!m_pipeline)
  74. {
  75. return;
  76. }
  77. m_imguiScope = {}; // restores previous ImGui context.
  78. if (m_currentlyUsingCopyPipline)
  79. {
  80. m_scene->RemoveRenderPipeline(m_copyPipeline->GetId());
  81. }
  82. else
  83. {
  84. m_scene->RemoveRenderPipeline(m_pipeline->GetId());
  85. }
  86. m_scene->AddRenderPipeline(m_originalPipeline);
  87. m_pipeline = nullptr;
  88. m_copyPipeline = nullptr;
  89. m_useCopyPipeline = false;
  90. m_currentlyUsingCopyPipline = false;
  91. m_migrateRight = false;
  92. m_rightMigrated = false;
  93. m_migrateLeft = false;
  94. m_leftMigrated = false;
  95. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  96. AZ::TickBus::Handler::BusDisconnect();
  97. }
  98. void MultiGPURPIExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  99. {
  100. if (m_currentlyUsingCopyPipline != m_useCopyPipeline)
  101. {
  102. AZ::RPI::RenderPipelinePtr prevPipeline = m_scene->GetDefaultRenderPipeline();
  103. if (m_useCopyPipeline)
  104. {
  105. m_copyPipeline->GetRootPass()->SetEnabled(true);
  106. m_scene->AddRenderPipeline(m_copyPipeline);
  107. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  108. m_imguiScope = {};
  109. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ m_copyPipeline->GetId().GetCStr(), "ImGuiPass" });
  110. }
  111. else
  112. {
  113. m_pipeline->GetRootPass()->SetEnabled(true);
  114. m_scene->AddRenderPipeline(m_pipeline);
  115. m_scene->RemoveRenderPipeline(prevPipeline->GetId());
  116. m_imguiScope = {};
  117. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ m_pipeline->GetId().GetCStr(), "ImGuiPass" });
  118. }
  119. m_currentlyUsingCopyPipline = m_useCopyPipeline;
  120. }
  121. if (m_rightMigrated != m_migrateRight)
  122. {
  123. AZ::RPI::PassFilter trianglePassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("TrianglePass2"), m_scene);
  124. AZ::RPI::PassFilter copyPassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("CopyPass"), m_scene);
  125. AZ::RPI::PassFilter compositePassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("CompositePass"), m_scene);
  126. RPI::RenderPass* trianglePass =
  127. azrtti_cast<RPI::RenderPass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(trianglePassFilter));
  128. RPI::Pass* copyPass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(copyPassFilter);
  129. RPI::Pass* compositePass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(compositePassFilter);
  130. AZ_Assert(trianglePass && copyPass && compositePass, "Couldn't find passes");
  131. if (m_migrateRight)
  132. {
  133. trianglePass->SetDeviceIndex(0);
  134. copyPass->SetEnabled(false);
  135. compositePass->ChangeConnection(Name("Input2"), trianglePass, Name("Output"));
  136. }
  137. else
  138. {
  139. trianglePass->SetDeviceIndex(1);
  140. copyPass->SetEnabled(true);
  141. compositePass->ChangeConnection(Name("Input2"), copyPass, Name("Output"));
  142. }
  143. m_rightMigrated = m_migrateRight;
  144. }
  145. if (m_leftMigrated != m_migrateLeft)
  146. {
  147. AZ::RPI::PassFilter trianglePassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("TrianglePass1"), m_scene);
  148. AZ::RPI::PassFilter compositePassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("CompositePass"), m_scene);
  149. RPI::RenderPass* trianglePass =
  150. azrtti_cast<RPI::RenderPass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(trianglePassFilter));
  151. RPI::Pass* compositePass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(compositePassFilter);
  152. AZ_Assert(trianglePass && compositePass, "Couldn't find passes");
  153. if (m_migrateLeft)
  154. {
  155. trianglePass->SetDeviceIndex(1);
  156. AZStd::shared_ptr<RPI::PassRequest> passRequest = AZStd::make_shared<RPI::PassRequest>();
  157. passRequest->m_templateName = Name("CopyPassTemplate");
  158. passRequest->m_passName = Name("CopyPassLeft");
  159. AZStd::shared_ptr<RPI::CopyPassData> passData = AZStd::make_shared<RPI::CopyPassData>();
  160. passData->m_sourceDeviceIndex = 1;
  161. passData->m_destinationDeviceIndex = 0;
  162. passRequest->m_passData = passData;
  163. RPI::PassConnection passConnection;
  164. passConnection.m_localSlot = Name{ "Input" };
  165. passConnection.m_attachmentRef.m_pass = Name{ "TrianglePass1" };
  166. passConnection.m_attachmentRef.m_attachment = Name{ "Output" };
  167. passRequest->m_connections.emplace_back(passConnection);
  168. RPI::PassDescriptor descriptor;
  169. descriptor.m_passData = passData;
  170. descriptor.m_passRequest = passRequest;
  171. descriptor.m_passName = passRequest->m_passName;
  172. auto copyPass = RPI::PassSystemInterface::Get()->CreatePassFromRequest(passRequest.get());
  173. m_pipeline->AddPassAfter(copyPass, passConnection.m_attachmentRef.m_pass);
  174. compositePass->ChangeConnection(Name("Input1"), copyPass.get(), Name("Output"));
  175. }
  176. else
  177. {
  178. trianglePass->SetDeviceIndex(0);
  179. compositePass->ChangeConnection(Name("Input1"), trianglePass, Name("Output"));
  180. AZ::RPI::PassFilter copyPassFilter = AZ::RPI::PassFilter::CreateWithPassName(Name("CopyPassLeft"), m_scene);
  181. RHI::Ptr<RPI::Pass> copyPass = AZ::RPI::PassSystemInterface::Get()->FindFirstPass(copyPassFilter);
  182. AZ_Assert(copyPass.get(), "Couldn't find copy pass");
  183. copyPass->QueueForRemoval();
  184. }
  185. m_leftMigrated = m_migrateLeft;
  186. }
  187. if (m_imguiSidebar.Begin())
  188. {
  189. ImGui::Spacing();
  190. ImGui::Checkbox("Use copy test pipeline", &m_useCopyPipeline);
  191. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  192. {
  193. ImGui::SetTooltip("Add additional device to device copy passes to test the modes of the copy pass:\n"
  194. "image to buffer\n"
  195. "buffer to buffer\n"
  196. "buffer to image\n"
  197. "image to image\n");
  198. }
  199. if (!m_useCopyPipeline)
  200. {
  201. ImGui::Checkbox("Migrate right half (1 -> 0)", &m_migrateRight);
  202. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  203. {
  204. ImGui::SetTooltip("Migrate right half to the first GPU (default on second).");
  205. }
  206. ImGui::Checkbox("Migrate left half (0 -> 1)", &m_migrateLeft);
  207. if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
  208. {
  209. ImGui::SetTooltip("Migrate left half to the second GPU (default on first).");
  210. }
  211. }
  212. m_imguiSidebar.End();
  213. }
  214. }
  215. void MultiGPURPIExampleComponent::DefaultWindowCreated()
  216. {
  217. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext,
  218. &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  219. }
  220. } // namespace AtomSampleViewer