SampleComponentManager.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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/ImGuiPassTree.h>
  21. #include <Atom/Utils/ImGuiFrameVisualizer.h>
  22. #include <Atom/Utils/ImGuiTransientAttachmentProfiler.h>
  23. #include <Atom/Utils/ImGuiShaderMetrics.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 <AzFramework/Input/Events/InputChannelEventListener.h>
  30. #include <AzFramework/Entity/EntityContextBus.h>
  31. #include <RHI/BasicRHIComponent.h>
  32. #include <Utils/ImGuiSaveFilePath.h>
  33. #include <Utils/ImGuiHistogramQueue.h>
  34. namespace AZ
  35. {
  36. class Entity;
  37. } // namespace AZ
  38. namespace AtomSampleViewer
  39. {
  40. class ScriptManager;
  41. enum class SamplePipelineType : uint32_t
  42. {
  43. RHI = 0,
  44. RPI
  45. };
  46. class SampleEntry
  47. {
  48. public:
  49. AZStd::string m_parentMenuName;
  50. AZStd::string m_sampleName;
  51. // m_parentMenuName/m_sampleName
  52. AZStd::string m_fullName;
  53. AZ::Uuid m_sampleUuid;
  54. AZStd::function<bool()> m_isSupportedFunc;
  55. SamplePipelineType m_pipelineType = SamplePipelineType::RHI;
  56. AZ::ComponentDescriptor* m_componentDescriptor;
  57. bool operator==(const SampleEntry& other)
  58. {
  59. return other.m_sampleName == m_sampleName && other.m_parentMenuName == m_parentMenuName;
  60. }
  61. };
  62. class SampleComponentManager final
  63. : public AZ::Component
  64. , public SampleComponentManagerRequestBus::Handler
  65. , public AZ::TickBus::Handler
  66. , public AzFramework::InputChannelEventListener
  67. , public AZ::Render::FrameCaptureNotificationBus::Handler
  68. , public AzFramework::AssetCatalogEventBus::Handler
  69. , public AZ::Render::ImGuiSystemNotificationBus::Handler
  70. {
  71. public:
  72. AZ_COMPONENT(SampleComponentManager, "{CECAE969-D0F2-4DB2-990F-05D6AD259C90}");
  73. static void Reflect(AZ::ReflectContext* context);
  74. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required);
  75. static void GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent);
  76. static AZStd::vector<SampleEntry> GetSamples();
  77. SampleComponentManager();
  78. ~SampleComponentManager() override;
  79. void Init() override;
  80. void Activate() override;
  81. void Deactivate() override;
  82. private:
  83. void RegisterSampleComponent(const SampleEntry& sample);
  84. // AZ::TickBus::Handler
  85. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  86. // AzFramework::InputChannelEventListener
  87. bool OnInputChannelEventFiltered(const AzFramework::InputChannel& inputChannel) override;
  88. // For samples' render context
  89. void CreateDefaultCamera();
  90. void SetupImGuiContext();
  91. // For RHI samples
  92. void CreateSceneForRHISample();
  93. void ReleaseRHIScene();
  94. void SwitchSceneForRHISample();
  95. // For RPI samples
  96. void CreateSceneForRPISample();
  97. void ReleaseRPIScene();
  98. void SwitchSceneForRPISample();
  99. void RenderImGui(float deltaTime);
  100. void ShowMenuBar();
  101. void ShowSampleHelper();
  102. void ShowResizeViewportDialog();
  103. void ShowFramerateHistogram(float deltaTime);
  104. void ShowFrameCaptureDialog();
  105. void ShowAboutWindow();
  106. void ShowPassTreeWindow();
  107. void ShowFrameGraphVisualizerWindow();
  108. void ShowCpuProfilerWindow();
  109. void ShowGpuProfilerWindow();
  110. void ShowShaderMetricsWindow();
  111. void ShowTransientAttachmentProfilerWindow();
  112. void RequestExit();
  113. void SampleChange();
  114. void CameraReset();
  115. void ShutdownActiveSample();
  116. // SampleComponentManagerRequestBus overrides...
  117. void Reset() override;
  118. bool OpenSample(const AZStd::string& sampleName) override;
  119. bool ShowTool(const AZStd::string& toolName, bool enable) override;
  120. void RequestFrameCapture(const AZStd::string& filePath, bool hideImGui) override;
  121. bool IsFrameCapturePending() override;
  122. void RunMainTestSuite(const AZStd::string& suiteFilePath, bool exitOnTestEnd, int randomSeed) override;
  123. void SetNumMSAASamples(int numMSAASamples) override;
  124. void ResetNumMSAASamples() override;
  125. void ResetRPIScene() override;
  126. void ClearRPIScene() override;
  127. // FrameCaptureNotificationBus overrides...
  128. void OnCaptureFinished(AZ::Render::FrameCaptureResult result, const AZStd::string& info) override;
  129. // AzFramework::AssetCatalogEventBus::Handler overrides ...
  130. void OnCatalogLoaded(const char* catalogFile) override;
  131. // AZ::Render::ImGuiSystemNotificationBus::Handler overrides ...
  132. void ActiveImGuiContextChanged(ImGuiContext* context) override;
  133. // Should be called after the asset catalog has been loaded.
  134. // In particular it should be called AFTER the BootstrapSystemComponent received
  135. // OnCatalogLoaded
  136. void ActivateInternal();
  137. static bool IsMultiViewportSwapchainSampleSupported();
  138. void AdjustImGuiFontScale();
  139. const char* GetRootPassTemplateName();
  140. int GetDefaultNumMSAASamples();
  141. // ---------- variables -----------------
  142. bool m_wasActivated = false;
  143. AZStd::vector<SampleEntry> m_availableSamples;
  144. // Maps from parent menu item name to a vector of indices into the available samples vector above
  145. // Note: we specifically use an ordered map to ensure menus are alphabatized.
  146. AZStd::map<AZStd::string, AZStd::vector<int32_t>> m_groupedSamples;
  147. // Entity to hold only example component. It doesn't need an entity context.
  148. AZ::Entity* m_exampleEntity = nullptr;
  149. AZ::Component* m_activeSample = nullptr;
  150. AZ::Entity* m_cameraEntity = nullptr;
  151. AZ::Data::Instance<AZ::RPI::AttachmentImage> m_brdfTexture;
  152. int32_t m_selectedSampleIndex = -1;
  153. static constexpr uint32_t FrameTimeLogSize = 30;
  154. ImGuiHistogramQueue m_imGuiFrameTimer;
  155. bool m_showImGuiMetrics = false;
  156. bool m_showSampleHelper = false;
  157. bool m_showResizeViewportDialog = false;
  158. bool m_showFrameGraphVisualizer = false;
  159. bool m_showFramerateHistogram = false;
  160. bool m_showFrameCaptureDialog = false;
  161. bool m_showAbout = false;
  162. bool m_showPassTree = false;
  163. bool m_showCullingDebugWindow = false;
  164. bool m_showCpuProfiler = false;
  165. bool m_showGpuProfiler = false;
  166. bool m_showTransientAttachmentProfiler = false;
  167. bool m_showShaderMetrics = false;
  168. bool m_ctrlModifierLDown = false;
  169. bool m_ctrlModifierRDown = false;
  170. bool m_alphanumericQDown = false;
  171. bool m_alphanumericTDown = false;
  172. bool m_alphanumericPDown = false;
  173. bool m_escapeDown = false;
  174. uint32_t m_screenshotKeyDownCount = 0;
  175. bool m_sampleChangeRequest = false;
  176. bool m_canSwitchSample = true;
  177. bool m_canCaptureRADTM = true;
  178. // 10 number keys 0-9
  179. static constexpr size_t s_alphanumericCount = 10;
  180. bool m_alphanumericNumbersDown[s_alphanumericCount];
  181. bool m_exitRequested = false;
  182. AzFramework::EntityContextId m_entityContextId;
  183. AZStd::vector<bool> m_isSampleSupported;
  184. AZ::Render::ImGuiPassTree m_imguiPassTree;
  185. AZ::Render::ImGuiFrameVisualizer m_imguiFrameGraphVisualizer;
  186. AZ::Render::ImGuiGpuProfiler m_imguiGpuProfiler;
  187. AZ::Render::ImGuiTransientAttachmentProfiler m_imguiTransientAttachmentProfiler;
  188. AZ::Render::ImGuiShaderMetrics m_imguiShaderMetrics;
  189. ImGuiSaveFilePath m_imguiFrameCaptureSaver;
  190. bool m_isFrameCapturePending = false;
  191. bool m_hideImGuiDuringFrameCapture = true;
  192. int m_countdownForFrameCapture = 0;
  193. AZStd::string m_frameCaptureFilePath;
  194. AZStd::unique_ptr<ScriptManager> m_scriptManager;
  195. AZStd::shared_ptr<AZ::RPI::WindowContext> m_windowContext;
  196. // Whether imgui is available
  197. bool m_isImGuiAvailable = false;
  198. // Scene and some variables for RHI samples
  199. AZ::RPI::ScenePtr m_rhiScene;
  200. AZ::RPI::Ptr<RHISamplePass> m_rhiSamplePass = nullptr;
  201. // Scene and some variables for RPI samples
  202. AZ::RPI::ScenePtr m_rpiScene;
  203. // number of MSAA samples, initialized in Activate() and can vary by platform
  204. int m_numMSAASamples = 0;
  205. };
  206. } // namespace AtomSampleViewer