MultiGPURPIExampleComponent.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/ViewProviderBus.h>
  14. #include <Atom/RPI.Public/RenderPipeline.h>
  15. #include <Atom/RPI.Public/Scene.h>
  16. #include <Atom/RPI.Public/RPISystemInterface.h>
  17. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  18. #include <Atom/RPI.Reflect/Model/ModelAsset.h>
  19. #include <Atom/Feature/ImGui/ImGuiUtils.h>
  20. #include <AzCore/Math/MatrixUtils.h>
  21. #include <Automation/ScriptableImGui.h>
  22. #include <Automation/ScriptRunnerBus.h>
  23. #include <AzCore/Component/Entity.h>
  24. #include <AzFramework/Components/TransformComponent.h>
  25. #include <AzFramework/Scene/SceneSystemInterface.h>
  26. #include <AzFramework/Entity/GameEntityContextComponent.h>
  27. #include <EntityUtilityFunctions.h>
  28. #include <SampleComponentConfig.h>
  29. #include <SampleComponentManager.h>
  30. #include <Utils/Utils.h>
  31. #include <RHI/BasicRHIComponent.h>
  32. namespace AtomSampleViewer
  33. {
  34. using namespace AZ;
  35. void MultiGPURPIExampleComponent::Reflect(AZ::ReflectContext* context)
  36. {
  37. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  38. {
  39. serializeContext->Class<MultiGPURPIExampleComponent, AZ::Component>()
  40. ->Version(0)
  41. ;
  42. }
  43. }
  44. MultiGPURPIExampleComponent::MultiGPURPIExampleComponent()
  45. {
  46. }
  47. void MultiGPURPIExampleComponent::Activate()
  48. {
  49. AZ::TickBus::Handler::BusConnect();
  50. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusConnect();
  51. // preload assets
  52. AZStd::vector<AssetCollectionAsyncLoader::AssetToLoadInfo> assetList = {
  53. {CubeModelFilePath, azrtti_typeid<RPI::ModelAsset>()}
  54. };
  55. PreloadAssets(assetList);
  56. // save original render pipeline first and remove it from the scene
  57. m_originalPipeline = m_scene->GetDefaultRenderPipeline();
  58. m_scene->RemoveRenderPipeline(m_originalPipeline->GetId());
  59. // add the checker board pipeline
  60. const AZStd::string pipelineName("MultiGPUPipeline");
  61. AZ::RPI::RenderPipelineDescriptor pipelineDesc;
  62. pipelineDesc.m_name = pipelineName;
  63. pipelineDesc.m_rootPassTemplate = "MultiGPUPipeline";
  64. m_pipeline = AZ::RPI::RenderPipeline::CreateRenderPipelineForWindow(pipelineDesc, *m_windowContext);
  65. m_scene->AddRenderPipeline(m_pipeline);
  66. m_imguiScope = AZ::Render::ImGuiActiveContextScope::FromPass({ "MultiGPUPipeline", "ImGuiPass" });
  67. }
  68. void MultiGPURPIExampleComponent::Deactivate()
  69. {
  70. // remove cb pipeline before adding original pipeline.
  71. if (!m_pipeline)
  72. {
  73. return;
  74. }
  75. m_imguiScope = {}; // restores previous ImGui context.
  76. m_scene->RemoveRenderPipeline(m_pipeline->GetId());
  77. m_scene->AddRenderPipeline(m_originalPipeline);
  78. m_pipeline = nullptr;
  79. AZ::Render::Bootstrap::DefaultWindowNotificationBus::Handler::BusDisconnect();
  80. AZ::TickBus::Handler::BusDisconnect();
  81. }
  82. void MultiGPURPIExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  83. {
  84. }
  85. void MultiGPURPIExampleComponent::OnAllAssetsReadyActivate()
  86. {
  87. auto meshFeatureProcessor = GetMeshFeatureProcessor();
  88. /*
  89. auto asset = RPI::AssetUtils::LoadAssetByProductPath<RPI::ModelAsset>(BunnyModelFilePath,
  90. RPI::AssetUtils::TraceLevel::Assert); //*/
  91. auto asset = RPI::AssetUtils::LoadAssetByProductPath<RPI::ModelAsset>(CubeModelFilePath,
  92. RPI::AssetUtils::TraceLevel::Assert);
  93. m_meshHandle = meshFeatureProcessor->AcquireMesh(Render::MeshHandleDescriptor(asset));
  94. //const Vector3 nonUniformScale{ 12.f, 12.f, 0.1f };
  95. const Vector3 translation{ 0.f, 0.f, -1.0f };
  96. Transform transform = Transform::CreateTranslation(translation);
  97. meshFeatureProcessor->SetTransform(m_meshHandle, transform);//, nonUniformScale);
  98. }
  99. void MultiGPURPIExampleComponent::DefaultWindowCreated()
  100. {
  101. AZ::Render::Bootstrap::DefaultWindowBus::BroadcastResult(m_windowContext,
  102. &AZ::Render::Bootstrap::DefaultWindowBus::Events::GetDefaultWindowContext);
  103. }
  104. } // namespace AtomSampleViewer