AtomSampleViewerSystemComponent.cpp 8.4 KB

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