AtomBridgeSystemComponent.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <AtomBridgeSystemComponent.h>
  9. #include <AtomDebugDisplayViewportInterface.h>
  10. #include <FlyCameraInputComponent.h>
  11. #include <PerViewportDynamicDrawManager.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. #include <AzCore/Serialization/EditContextConstants.inl>
  15. #include <AzCore/Component/TransformBus.h>
  16. #include <AzCore/Interface/Interface.h>
  17. #include <AzFramework/API/ApplicationAPI.h>
  18. #include <AzFramework/Entity/GameEntityContextBus.h>
  19. #include <AzCore/Math/MatrixUtils.h>
  20. #include <Atom/RHI/Factory.h>
  21. #include <Atom/RPI.Public/RPISystemInterface.h>
  22. #include <Atom/RPI.Public/View.h>
  23. #include <Atom/RPI.Public/Scene.h>
  24. #include <Atom/RPI.Public/RenderPipeline.h>
  25. #include <Atom/RPI.Public/ViewportContextBus.h>
  26. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  27. #include <Atom/RPI.Public/AuxGeom/AuxGeomFeatureProcessorInterface.h>
  28. #include <Atom/Bootstrap/DefaultWindowBus.h>
  29. namespace AZ
  30. {
  31. namespace AtomBridge
  32. {
  33. void AtomBridgeSystemComponent::Reflect(ReflectContext* context)
  34. {
  35. if (SerializeContext* serialize = azrtti_cast<SerializeContext*>(context))
  36. {
  37. serialize->Class<AtomBridgeSystemComponent, Component>()
  38. ->Version(0)
  39. ;
  40. if (EditContext* ec = serialize->GetEditContext())
  41. {
  42. ec->Class<AtomBridgeSystemComponent>("AtomBridge", "[Description of functionality provided by this System Component]")
  43. ->ClassElement(Edit::ClassElements::EditorData, "")
  44. ->Attribute(Edit::Attributes::AutoExpand, true)
  45. ;
  46. }
  47. }
  48. }
  49. AtomBridgeSystemComponent::AtomBridgeSystemComponent()
  50. {
  51. }
  52. AtomBridgeSystemComponent::~AtomBridgeSystemComponent()
  53. {
  54. }
  55. void AtomBridgeSystemComponent::GetProvidedServices(ComponentDescriptor::DependencyArrayType& provided)
  56. {
  57. provided.push_back(AZ_CRC_CE("AtomBridgeService"));
  58. }
  59. void AtomBridgeSystemComponent::GetIncompatibleServices(ComponentDescriptor::DependencyArrayType& incompatible)
  60. {
  61. incompatible.push_back(AZ_CRC_CE("AtomBridgeService"));
  62. }
  63. void AtomBridgeSystemComponent::GetRequiredServices(ComponentDescriptor::DependencyArrayType& required)
  64. {
  65. required.push_back(AZ::RHI::Factory::GetComponentService());
  66. required.push_back(AZ_CRC_CE("AssetDatabaseService"));
  67. required.push_back(AZ_CRC_CE("RPISystem"));
  68. required.push_back(AZ_CRC_CE("BootstrapSystemComponent"));
  69. }
  70. void AtomBridgeSystemComponent::GetDependentServices(ComponentDescriptor::DependencyArrayType& dependent)
  71. {
  72. AZ_UNUSED(dependent);
  73. }
  74. void AtomBridgeSystemComponent::Init()
  75. {
  76. AZ::RPI::ViewportContextManagerNotificationsBus::Handler::BusConnect();
  77. }
  78. void AtomBridgeSystemComponent::Activate()
  79. {
  80. AzFramework::Render::RenderSystemRequestBus::Handler::BusConnect();
  81. AtomBridgeRequestBus::Handler::BusConnect();
  82. AzFramework::Components::DeprecatedComponentsRequestBus::Handler::BusConnect();
  83. AzFramework::GameEntityContextRequestBus::BroadcastResult(m_entityContextId, &AzFramework::GameEntityContextRequestBus::Events::GetGameEntityContextId);
  84. AZ::Render::Bootstrap::NotificationBus::Handler::BusConnect();
  85. m_dynamicDrawManager = AZStd::make_unique<PerViewportDynamicDrawManager>();
  86. }
  87. void AtomBridgeSystemComponent::Deactivate()
  88. {
  89. m_dynamicDrawManager.reset();
  90. AZ::RPI::ViewportContextManagerNotificationsBus::Handler::BusDisconnect();
  91. RPI::Scene* scene = AZ::RPI::Scene::GetSceneForEntityContextId(m_entityContextId);
  92. // Check if scene is emptry since scene might be released already when running AtomSampleViewer
  93. if (scene)
  94. {
  95. auto auxGeomFP = scene->GetFeatureProcessor<RPI::AuxGeomFeatureProcessorInterface>();
  96. if (auxGeomFP)
  97. {
  98. auxGeomFP->ReleaseDrawQueueForView(m_view.get());
  99. }
  100. }
  101. // Don't want to leave this until our destructor because the AZ::Data::InstanceDatabase may not be valid at that point
  102. m_view = nullptr;
  103. AZ::Render::Bootstrap::NotificationBus::Handler::BusDisconnect();
  104. AzFramework::Components::DeprecatedComponentsRequestBus::Handler::BusDisconnect();
  105. AtomBridgeRequestBus::Handler::BusDisconnect();
  106. AzFramework::Render::RenderSystemRequestBus::Handler::BusDisconnect();
  107. }
  108. AZStd::string AtomBridgeSystemComponent::GetRendererName() const
  109. {
  110. return "Other";
  111. }
  112. void AtomBridgeSystemComponent::EnumerateDeprecatedComponents(AzFramework::Components::DeprecatedComponentsList& list) const
  113. {
  114. static const AZ::Uuid legacyRenderComponentUuids[] = {
  115. AZ::Uuid("{FC315B86-3280-4D03-B4F0-5553D7D08432}"), // EditorMeshComponent
  116. };
  117. const AZStd::string deprecatedString = " (DEPRECATED By Atom)";
  118. for (const AZ::Uuid& componentUuid : legacyRenderComponentUuids)
  119. {
  120. auto deprecatedEntry = list.find(componentUuid);
  121. if (deprecatedEntry == list.end())
  122. {
  123. list[componentUuid] = AzFramework::Components::DeprecatedInfo{ true, deprecatedString };
  124. }
  125. else
  126. {
  127. deprecatedEntry->second.m_hideComponent = true;
  128. deprecatedEntry->second.m_deprecationString += deprecatedString;
  129. }
  130. }
  131. }
  132. void AtomBridgeSystemComponent::OnBootstrapSceneReady(AZ::RPI::Scene* bootstrapScene)
  133. {
  134. // If the pointer is not reset before being assigned, the destructor of the old instance of AtomDebugDisplayViewportInterface
  135. // would disconnect from DebugDisplayRequestBus immediately after the new instance was connected, since they both use the same
  136. // id.
  137. if (auto it = m_activeViewportsList.find(AzFramework::g_defaultSceneEntityDebugDisplayId); it != m_activeViewportsList.end())
  138. {
  139. it->second.reset();
  140. }
  141. // Make default AtomDebugDisplayViewportInterface
  142. AZStd::shared_ptr<AtomDebugDisplayViewportInterface> mainEntityDebugDisplay =
  143. AZStd::make_shared<AtomDebugDisplayViewportInterface>(AzFramework::g_defaultSceneEntityDebugDisplayId, bootstrapScene);
  144. m_activeViewportsList[AzFramework::g_defaultSceneEntityDebugDisplayId] = mainEntityDebugDisplay;
  145. }
  146. void AtomBridgeSystemComponent::OnViewportContextAdded(AZ::RPI::ViewportContextPtr viewportContext)
  147. {
  148. AZStd::shared_ptr<AtomDebugDisplayViewportInterface> viewportDebugDisplay = AZStd::make_shared<AtomDebugDisplayViewportInterface>(viewportContext);
  149. m_activeViewportsList[viewportContext->GetId()] = viewportDebugDisplay;
  150. }
  151. void AtomBridgeSystemComponent::OnViewportContextRemoved(AzFramework::ViewportId viewportId)
  152. {
  153. AZ_Assert(viewportId != AzFramework::g_defaultSceneEntityDebugDisplayId, "Error trying to remove the default scene draw instance");
  154. m_activeViewportsList.erase(viewportId);
  155. }
  156. } // namespace AtomBridge
  157. } // namespace AZ