SampleComponentManager.h 9.9 KB

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