SampleComponentManager.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #pragma once
  9. #include <SampleComponentManagerBus.h>
  10. #include <Automation/ScriptableImGui.h> // This file needs to be included before "<Atom/Utils/ImGuiPassTree.h>" to enable scriptable imgui for ImguiPassTree
  11. #include <AzCore/std/containers/span.h>
  12. #include <Atom/Feature/ImGui/SystemBus.h>
  13. #include <Atom/Feature/Utils/FrameCaptureBus.h>
  14. #include <Atom/RPI.Public/WindowContext.h>
  15. #include <Atom/RPI.Public/Image/AttachmentImage.h>
  16. #include <Atom/RPI.Public/Pass/Specific/SwapChainPass.h>
  17. #include <Atom/RPI.Public/GpuQuery/GpuQuerySystemInterface.h>
  18. #include <Atom/Utils/ImGuiCullingDebug.h>
  19. #include <Atom/Utils/ImGuiGpuProfiler.h>
  20. #include <Atom/Utils/ImGuiMaterialDetails.h>
  21. #include <Atom/Utils/ImGuiPassTree.h>
  22. #include <Atom/Utils/ImGuiFrameVisualizer.h>
  23. #include <Atom/Utils/ImGuiTransientAttachmentProfiler.h>
  24. #include <AzCore/Component/Component.h>
  25. #include <AzCore/Component/TickBus.h>
  26. #include <AzCore/std/containers/vector.h>
  27. #include <AzCore/std/containers/map.h>
  28. #include <AzCore/std/smart_ptr/shared_ptr.h>
  29. #include <AzCore/std/smart_ptr/unique_ptr.h>
  30. #include <AzFramework/Input/Events/InputChannelEventListener.h>
  31. #include <AzFramework/Entity/EntityContextBus.h>
  32. #include <RHI/BasicRHIComponent.h>
  33. #include <Utils/ImGuiSaveFilePath.h>
  34. #include <Utils/ImGuiHistogramQueue.h>
  35. #include <Utils/ImGuiMessageBox.h>
  36. namespace AZ
  37. {
  38. class Entity;
  39. } // namespace AZ
  40. namespace AtomSampleViewer
  41. {
  42. class ScriptManager;
  43. class SampleComponentManager final
  44. : public AZ::Component
  45. , public SampleComponentManagerRequestBus::Handler
  46. , public SampleComponentSingletonRequestBus::Handler
  47. , public AZ::TickBus::Handler
  48. , public AzFramework::InputChannelEventListener
  49. , public AZ::Render::FrameCaptureNotificationBus::Handler
  50. , public AzFramework::AssetCatalogEventBus::Handler
  51. , public AZ::Render::ImGuiSystemNotificationBus::Handler
  52. {
  53. public:
  54. AZ_COMPONENT(SampleComponentManager, "{CECAE969-D0F2-4DB2-990F-05D6AD259C90}");
  55. static void Reflect(AZ::ReflectContext* context);
  56. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  57. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided);
  58. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  59. static AZStd::vector<SampleEntry> GetSamples();
  60. SampleComponentManager();
  61. ~SampleComponentManager() override;
  62. void Init() override;
  63. void Activate() override;
  64. void Deactivate() override;
  65. private:
  66. // AZ::TickBus::Handler
  67. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  68. // AzFramework::InputChannelEventListener
  69. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  70. // For samples' render context
  71. void CreateDefaultCamera();
  72. void SetupImGuiContext();
  73. // For RHI samples
  74. void CreateSceneForRHISample();
  75. void ReleaseRHIScene();
  76. void SwitchSceneForRHISample();
  77. // For RPI samples
  78. void CreateSceneForRPISample();
  79. void ReleaseRPIScene();
  80. void SwitchSceneForRPISample();
  81. void RenderImGui(float deltaTime);
  82. void ShowMenuBar();
  83. void ShowSampleHelper();
  84. void ShowResizeViewportDialog();
  85. void ShowFramerateHistogram(float deltaTime);
  86. void ShowFrameCaptureDialog();
  87. void ShowAboutWindow();
  88. void ShowPassTreeWindow();
  89. void ShowFrameGraphVisualizerWindow();
  90. void ShowCpuProfilerWindow();
  91. void ShowGpuProfilerWindow();
  92. void ShowFileIoProfilerWindow();
  93. void ShowTransientAttachmentProfilerWindow();
  94. void RequestExit();
  95. void SampleChange();
  96. void CameraReset();
  97. void ShutdownActiveSample();
  98. void SetRHISamplePass(BasicRHIComponent* sampleComponent);
  99. // SampleComponentManagerRequestBus overrides...
  100. void Reset() override;
  101. bool OpenSample(const AZStd::string& sampleName) override;
  102. bool ShowTool(const AZStd::string& toolName, bool enable) override;
  103. void RequestFrameCapture(const AZStd::string& filePath, bool hideImGui) override;
  104. bool IsFrameCapturePending() override;
  105. void RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed) override;
  106. void SetNumMSAASamples(int16_t numMsaaSamples) override;
  107. int16_t GetNumMSAASamples() override;
  108. void SetDefaultNumMSAASamples(int16_t defaultNumMsaaSamples) override;
  109. int16_t GetDefaultNumMSAASamples() override;
  110. // SampleComponentSingletonRequestBus overrides
  111. void RegisterSampleComponent(const SampleEntry& sample) override;
  112. ScriptManager* GetScriptManagerInstance() override;
  113. ScriptableImGui* GetScriptableImGuiInstance() override;
  114. void ResetNumMSAASamples() override;
  115. void ResetRPIScene() override;
  116. void ClearRPIScene() override;
  117. void EnableRenderPipeline(bool value) override;
  118. void EnableXrPipelines(bool value) override;
  119. // FrameCaptureNotificationBus overrides...
  120. void OnFrameCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string& info) override;
  121. // AzFramework::AssetCatalogEventBus::Handler overrides ...
  122. void OnCatalogLoaded(const char* catalogFile) override;
  123. // AZ::Render::ImGuiSystemNotificationBus::Handler overrides ...
  124. void ActiveImGuiContextChanged(ImGuiContext* context) override;
  125. // Should be called after the asset catalog has been loaded.
  126. // In particular it should be called AFTER the BootstrapSystemComponent received
  127. // OnCatalogLoaded
  128. void ActivateInternal();
  129. static bool IsMultiViewportSwapchainSampleSupported();
  130. void AdjustImGuiFontScale();
  131. const char* GetRootPassTemplateName();
  132. const char* GetMaterialPipelineName();
  133. // ---------- variables -----------------
  134. bool m_wasActivated = false;
  135. AZStd::vector<SampleEntry> m_availableSamples;
  136. // Maps from parent menu item name to a vector of indices into the available samples vector above
  137. // Note: we specifically use an ordered map to ensure menus are alphabetized.
  138. AZStd::map<AZStd::string, AZStd::vector<int32_t>> m_groupedSamples;
  139. // Entity to hold only example component. It doesn't need an entity context.
  140. AZ::Entity* m_exampleEntity = nullptr;
  141. AZStd::vector<AZ::Component*> m_activeSamples;
  142. AZ::Entity* m_cameraEntity = nullptr;
  143. AZ::Data::Instance<AZ::RPI::AttachmentImage> m_brdfTexture;
  144. int32_t m_selectedSampleIndex = -1;
  145. static constexpr uint32_t FrameTimeDefaultLogSize = 100;
  146. static constexpr uint32_t FrameTimeMinLogSize = FrameTimeDefaultLogSize;
  147. static constexpr uint32_t FrameTimeMaxLogSize = 1000000; // 1M
  148. AZStd::unique_ptr<ImGuiHistogramQueue> m_imGuiFrameTimer;
  149. ImGuiMessageBox m_contentWarningDialog;
  150. bool m_showImGuiMetrics = false;
  151. bool m_showSampleHelper = false;
  152. bool m_showResizeViewportDialog = false;
  153. bool m_showFrameGraphVisualizer = false;
  154. bool m_showFramerateHistogram = false;
  155. bool m_showFrameCaptureDialog = false;
  156. bool m_showAbout = false;
  157. bool m_showPassTree = false;
  158. bool m_showCullingDebugWindow = false;
  159. bool m_showCpuProfiler = false;
  160. bool m_showGpuProfiler = false;
  161. bool m_showFileIoProfiler = false;
  162. bool m_showTransientAttachmentProfiler = false;
  163. bool m_ctrlModifierLDown = false;
  164. bool m_ctrlModifierRDown = false;
  165. bool m_alphanumericQDown = false;
  166. bool m_alphanumericTDown = false;
  167. bool m_alphanumericPDown = false;
  168. bool m_escapeDown = false;
  169. uint32_t m_screenshotKeyDownCount = 0;
  170. bool m_sampleChangeRequest = false;
  171. bool m_canSwitchSample = true;
  172. bool m_canCaptureRADTM = true;
  173. bool m_exitRequested = false;
  174. AzFramework::EntityContextId m_entityContextId;
  175. AZStd::vector<bool> m_isSampleSupported;
  176. AZ::Render::ImGuiPassTree m_imguiPassTree;
  177. AZ::Render::ImGuiFrameVisualizer m_imguiFrameGraphVisualizer;
  178. AZ::Render::ImGuiGpuProfiler m_imguiGpuProfiler;
  179. AZ::Render::ImGuiTransientAttachmentProfiler m_imguiTransientAttachmentProfiler;
  180. ImGuiSaveFilePath m_imguiFrameCaptureSaver;
  181. bool m_isFrameCapturePending = false;
  182. bool m_hideImGuiDuringFrameCapture = true;
  183. int m_countdownForFrameCapture = 0;
  184. AZ::Render::FrameCaptureId m_frameCaptureId = AZ::Render::InvalidFrameCaptureId;
  185. AZStd::string m_frameCaptureFilePath;
  186. AZStd::unique_ptr<ScriptManager> m_scriptManager;
  187. AZStd::unique_ptr<ScriptableImGui> m_scriptableImGui;
  188. AZStd::shared_ptr<AZ::RPI::WindowContext> m_windowContext;
  189. // Whether imgui is available
  190. bool m_isImGuiAvailable = false;
  191. // Scene and some variables for RHI samples
  192. AZ::RPI::ScenePtr m_rhiScene;
  193. AZStd::vector<AZ::RPI::Ptr<RHISamplePass>> m_rhiSamplePasses;
  194. // Scene and some variables for RPI samples
  195. AZ::RPI::ScenePtr m_rpiScene;
  196. // number of MSAA samples
  197. int16_t m_numMsaaSamples = 1;
  198. int16_t m_defaultNumMsaaSamples = 1;
  199. // Cache PC and XR pipelines
  200. AZ::RPI::RenderPipelinePtr m_renderPipeline = nullptr;
  201. AZStd::vector<AZ::RPI::RenderPipelinePtr> m_xrPipelines;
  202. };
  203. } // namespace AtomSampleViewer