AtomSampleViewerSystemComponent.cpp 8.3 KB

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