AtomSampleViewerSystemComponent.cpp 8.6 KB

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