AtomSampleViewerSystemComponent.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 <AtomSampleViewerSystemComponent.h>
  9. #include <MaterialFunctors/StacksShaderCollectionFunctor.h>
  10. #include <MaterialFunctors/StacksShaderInputFunctor.h>
  11. #include <Automation/ImageComparisonConfig.h>
  12. #include <EntityLatticeTestComponent.h>
  13. #include <AzCore/Asset/AssetManagerBus.h>
  14. #include <AzCore/Asset/AssetManager.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/Component/Entity.h>
  17. #include <AzCore/IO/SystemFile.h>
  18. #include <AzFramework/Input/Buses/Requests/InputSystemCursorRequestBus.h>
  19. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  20. #include <Atom/Bootstrap/DefaultWindowBus.h>
  21. #include <Atom/RHI/Factory.h>
  22. #include <Atom/RPI.Public/RPISystemInterface.h>
  23. #include <Atom/RPI.Public/Shader/Metrics/ShaderMetricsSystem.h>
  24. #include <ISystem.h>
  25. #include <IConsole.h>
  26. #include <Utils/ImGuiAssetBrowser.h>
  27. #include <Utils/ImGuiSidebar.h>
  28. #include <Utils/ImGuiSaveFilePath.h>
  29. #include <Utils/Utils.h>
  30. namespace AtomSampleViewer
  31. {
  32. void AtomSampleViewerSystemComponent::Reflect(AZ::ReflectContext* context)
  33. {
  34. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  35. {
  36. serializeContext->Class<AtomSampleViewerSystemComponent, AZ::Component>()
  37. ->Version(0)
  38. ;
  39. }
  40. PerfMetrics::Reflect(context);
  41. ImGuiAssetBrowser::Reflect(context);
  42. ImGuiSidebar::Reflect(context);
  43. ImGuiSaveFilePath::Reflect(context);
  44. StacksShaderCollectionFunctor::Reflect(context);
  45. StacksShaderInputFunctor::Reflect(context);
  46. ImageComparisonConfig::Reflect(context);
  47. }
  48. void AtomSampleViewerSystemComponent::PerfMetrics::Reflect(AZ::ReflectContext* context)
  49. {
  50. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  51. {
  52. serializeContext->Class<PerfMetrics>()
  53. ->Version(1)
  54. ->Field("TestDurationSeconds", &PerfMetrics::m_timingTargetSeconds)
  55. ->Field("AverageFramesPerSecond", &PerfMetrics::m_averageDeltaSeconds)
  56. ->Field("SecondsToRender", &PerfMetrics::m_timeToFirstRenderSeconds)
  57. ;
  58. }
  59. // Abstract base component is used by multiple components and needs to be reflected in a single location.
  60. EntityLatticeTestComponent::Reflect(context);
  61. }
  62. void AtomSampleViewerSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  63. {
  64. provided.push_back(AZ_CRC("PrototypeLmbrCentralService", 0xe35e6de0));
  65. }
  66. void AtomSampleViewerSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  67. {
  68. required.push_back(AZ::RHI::Factory::GetComponentService());
  69. required.push_back(AZ_CRC("AssetDatabaseService", 0x3abf5601));
  70. required.push_back(AZ_CRC("RPISystem", 0xf2add773));
  71. required.push_back(AZ_CRC("BootstrapSystemComponent", 0xb8f32711));
  72. }
  73. AtomSampleViewerSystemComponent::AtomSampleViewerSystemComponent()
  74. : m_timestamp(HighResTimer::now())
  75. {
  76. m_atomSampleViewerEntity = aznew AZ::Entity();
  77. m_atomSampleViewerEntity->Init();
  78. }
  79. AtomSampleViewerSystemComponent::~AtomSampleViewerSystemComponent()
  80. {
  81. }
  82. void AtomSampleViewerSystemComponent::Activate()
  83. {
  84. AZ::EntitySystemBus::Handler::BusConnect();
  85. AZ::ApplicationTypeQuery appType;
  86. AZ::ComponentApplicationBus::Broadcast(&AZ::ComponentApplicationBus::Events::QueryApplicationType, appType);
  87. if (appType.IsValid() && !appType.IsEditor())
  88. {
  89. // AtomSampleViewer SampleComponentManager creates and manages its own scene and render pipelines.
  90. // We disable the creation of default scene in BootStrapSystemComponent
  91. AZ::Render::Bootstrap::DefaultWindowBus::Broadcast(&AZ::Render::Bootstrap::DefaultWindowBus::Events::SetCreateDefaultScene, false);
  92. }
  93. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::LoadCatalog, "@products@/assetcatalog.xml");
  94. m_atomSampleViewerEntity->Activate();
  95. AZ::TickBus::Handler::BusConnect();
  96. CrySystemEventBus::Handler::BusConnect();
  97. }
  98. void AtomSampleViewerSystemComponent::Deactivate()
  99. {
  100. CrySystemEventBus::Handler::BusDisconnect();
  101. AZ::TickBus::Handler::BusDisconnect();
  102. if (m_atomSampleViewerEntity != nullptr)
  103. {
  104. m_atomSampleViewerEntity->Deactivate();
  105. }
  106. AZ::EntitySystemBus::Handler::BusDisconnect();
  107. }
  108. void AtomSampleViewerSystemComponent::OnEntityDestroyed(const AZ::EntityId& entityId)
  109. {
  110. if (m_atomSampleViewerEntity && m_atomSampleViewerEntity->GetId() == entityId)
  111. {
  112. m_atomSampleViewerEntity = nullptr;
  113. }
  114. }
  115. void AtomSampleViewerSystemComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  116. {
  117. AZ_UNUSED(time);
  118. TickTimeoutShutdown(deltaTime);
  119. #if defined(AZ_DEBUG_BUILD)
  120. TrackPerfMetrics(deltaTime);
  121. #endif
  122. }
  123. void AtomSampleViewerSystemComponent::OnCrySystemInitialized(ISystem& system, const SSystemInitParams&)
  124. {
  125. system.GetIConsole()->GetCVar("sys_asserts")->Set(2);
  126. // Currently CSystem::Init hides and constrains the mouse cursor.
  127. // For AtomSampleViewer we want it visible so that we can use the ImGui menus
  128. AzFramework::InputSystemCursorRequestBus::Event(AzFramework::InputDeviceMouse::Id,
  129. &AzFramework::InputSystemCursorRequests::SetSystemCursorState,
  130. AzFramework::SystemCursorState::UnconstrainedAndVisible);
  131. ReadTimeoutShutdown();
  132. }
  133. void AtomSampleViewerSystemComponent::ReadTimeoutShutdown()
  134. {
  135. const AzFramework::CommandLine* commandLine = nullptr;
  136. AzFramework::ApplicationRequests::Bus::BroadcastResult(commandLine, &AzFramework::ApplicationRequests::Bus::Events::GetApplicationCommandLine);
  137. if (commandLine)
  138. {
  139. if (commandLine->HasSwitch("timeout"))
  140. {
  141. const AZStd::string& timeoutValue = commandLine->GetSwitchValue("timeout", 0);
  142. const float timeoutInSeconds = static_cast<float>(atoi(timeoutValue.c_str()));
  143. AZ_Printf("AtomSampleViewer", "starting up with timeout shutdown of %f seconds", timeoutInSeconds);
  144. m_secondsBeforeShutdown = timeoutInSeconds;
  145. }
  146. }
  147. }
  148. void AtomSampleViewerSystemComponent::TickTimeoutShutdown(float deltaTimeInSeconds)
  149. {
  150. if (m_secondsBeforeShutdown > 0.f)
  151. {
  152. m_secondsBeforeShutdown -= deltaTimeInSeconds;
  153. if (m_secondsBeforeShutdown <= 0.f)
  154. {
  155. AZ_Printf("AtomSampleViewer", "Timeout reached, shutting down");
  156. AzFramework::ApplicationRequests::Bus::Broadcast(&AzFramework::ApplicationRequests::ExitMainLoop); // or ::TerminateOnError for a more forceful option
  157. }
  158. }
  159. }
  160. void AtomSampleViewerSystemComponent::TrackPerfMetrics(const float deltaTime)
  161. {
  162. m_frameCount++;
  163. if (m_frameCount != 1) // don't accumulate delta on the first frame
  164. {
  165. m_accumulatedDeltaSeconds += deltaTime;
  166. }
  167. if (!m_testsLogged)
  168. {
  169. AZStd::chrono::duration<float> elapsedTime = HighResTimer::now() - m_timestamp;
  170. if (m_frameCount == 1)
  171. {
  172. m_perfMetrics.m_timeToFirstRenderSeconds = elapsedTime.count();
  173. }
  174. if (elapsedTime.count() >= m_perfMetrics.m_timingTargetSeconds)
  175. {
  176. if (m_frameCount > 1)
  177. {
  178. m_perfMetrics.m_averageDeltaSeconds = m_accumulatedDeltaSeconds / static_cast<float>(m_frameCount - 1);
  179. }
  180. LogPerfMetrics();
  181. m_testsLogged = true;
  182. }
  183. }
  184. }
  185. void AtomSampleViewerSystemComponent::LogPerfMetrics() const
  186. {
  187. AZ::Utils::SaveObjectToFile("metrics.xml", AZ::DataStream::ST_XML, &m_perfMetrics);
  188. }
  189. } // namespace AtomSampleViewer