AtomImGuiToolsSystemComponent.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <AtomImGuiToolsSystemComponent.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <Atom/RHI/RHIMemoryStatisticsInterface.h>
  13. #include <Atom/RHI.Profiler/GraphicsProfilerBus.h>
  14. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  15. #include <AzFramework/Components/ConsoleBus.h>
  16. #include <ImGuiBus.h>
  17. namespace AtomImGuiTools
  18. {
  19. void AtomImGuiToolsSystemComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serialize->Class<AtomImGuiToolsSystemComponent, AZ::Component>()
  24. ->Version(0)
  25. ;
  26. if (AZ::EditContext* ec = serialize->GetEditContext())
  27. {
  28. ec->Class<AtomImGuiToolsSystemComponent>("AtomImGuiTools", "[Manager of various Atom ImGui tools.]")
  29. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  30. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  31. ;
  32. }
  33. }
  34. }
  35. void AtomImGuiToolsSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  36. {
  37. provided.push_back(AZ_CRC("AtomImGuiToolsService"));
  38. }
  39. void AtomImGuiToolsSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  40. {
  41. incompatible.push_back(AZ_CRC("AtomImGuiToolsService"));
  42. }
  43. void AtomImGuiToolsSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  44. {
  45. AZ_UNUSED(required);
  46. }
  47. void AtomImGuiToolsSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  48. {
  49. AZ_UNUSED(dependent);
  50. }
  51. void AtomImGuiToolsSystemComponent::Activate()
  52. {
  53. #if defined(IMGUI_ENABLED)
  54. ImGui::ImGuiUpdateListenerBus::Handler::BusConnect();
  55. AtomImGuiToolsRequestBus::Handler::BusConnect();
  56. // load switchable render pipeline paths from setting registry
  57. auto settingsRegistry = AZ::SettingsRegistry::Get();
  58. const char* settingName = "/O3DE/Viewport/SwitchableRenderPipelines";
  59. if (settingsRegistry)
  60. {
  61. settingsRegistry->GetObject<AZStd::set<AZStd::string>>(m_switchableRenderPipelines, settingName);
  62. }
  63. #endif
  64. CrySystemEventBus::Handler::BusConnect();
  65. }
  66. void AtomImGuiToolsSystemComponent::Deactivate()
  67. {
  68. #if defined(IMGUI_ENABLED)
  69. m_imguiPassTree.Reset();
  70. ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect();
  71. AtomImGuiToolsRequestBus::Handler::BusDisconnect();
  72. #endif
  73. CrySystemEventBus::Handler::BusDisconnect();
  74. }
  75. #if defined(IMGUI_ENABLED)
  76. void AtomImGuiToolsSystemComponent::OnImGuiUpdate()
  77. {
  78. if (m_showPassTree)
  79. {
  80. m_imguiPassTree.Draw(m_showPassTree, AZ::RPI::PassSystemInterface::Get()->GetRootPass().get());
  81. }
  82. if (m_showGpuProfiler)
  83. {
  84. m_imguiGpuProfiler.Draw(m_showGpuProfiler, AZ::RPI::PassSystemInterface::Get()->GetRootPass().get());
  85. }
  86. if (m_showTransientAttachmentProfiler)
  87. {
  88. auto transientStats = AZ::RHI::RHIMemoryStatisticsInterface::Get()->GetTransientAttachmentStatistics();
  89. if (!transientStats.empty())
  90. {
  91. m_showTransientAttachmentProfiler = m_imguiTransientAttachmentProfiler.Draw(transientStats);
  92. }
  93. }
  94. m_showMaterialDetails = m_imguiMaterialDetails.Tick(m_materialDetailsController.GetMeshDrawPackets(), m_materialDetailsController.GetSelectionName().c_str());
  95. }
  96. void AtomImGuiToolsSystemComponent::OnImGuiMainMenuUpdate()
  97. {
  98. if (ImGui::BeginMenu("Atom Tools"))
  99. {
  100. if (ImGui::MenuItem("Dump loaded Asset info", ""))
  101. {
  102. AZ::Data::AssetManager::Instance().DumpLoadedAssetsInfo();
  103. }
  104. ImGui::MenuItem("Pass Viewer", "", &m_showPassTree);
  105. ImGui::MenuItem("Gpu Profiler", "", &m_showGpuProfiler);
  106. if (ImGui::MenuItem("Transient Attachment Profiler", "", &m_showTransientAttachmentProfiler))
  107. {
  108. AZ::RHI::RHISystemInterface::Get()->ModifyFrameSchedulerStatisticsFlags(
  109. AZ::RHI::FrameSchedulerStatisticsFlags::GatherTransientAttachmentStatistics, m_showTransientAttachmentProfiler);
  110. }
  111. if (ImGui::MenuItem("Material Shader Details", "", &m_showMaterialDetails))
  112. {
  113. if (m_showMaterialDetails)
  114. {
  115. m_imguiMaterialDetails.OpenDialog();
  116. }
  117. else
  118. {
  119. m_imguiMaterialDetails.CloseDialog();
  120. }
  121. }
  122. if (ImGui::MenuItem("Trigger GPU Capture", "", false, AZ::RHI::GraphicsProfilerBus::HasHandlers()))
  123. {
  124. AZ::RHI::GraphicsProfilerBus::Broadcast(&AZ::RHI::GraphicsProfilerBus::Events::TriggerCapture);
  125. }
  126. ImGui::EndMenu();
  127. }
  128. if (m_switchableRenderPipelines.size() > 0)
  129. {
  130. if (ImGui::BeginMenu("Render Pipelines"))
  131. {
  132. for (const auto& renderPipelinePath : m_switchableRenderPipelines)
  133. {
  134. if (ImGui::MenuItem(renderPipelinePath.c_str()))
  135. {
  136. AZ::Interface<AZ::IConsole>::Get()->PerformCommand("r_renderPipelinePath", { renderPipelinePath });
  137. }
  138. }
  139. ImGui::EndMenu();
  140. }
  141. }
  142. }
  143. void AtomImGuiToolsSystemComponent::ShowMaterialShaderDetailsForEntity(AZ::EntityId entity, bool autoOpenDialog)
  144. {
  145. m_materialDetailsController.SetSelectedEntityId(entity);
  146. if (autoOpenDialog)
  147. {
  148. m_imguiMaterialDetails.OpenDialog();
  149. m_showMaterialDetails = true;
  150. ImGui::ImGuiManagerBus::Broadcast(&ImGui::IImGuiManager::ToggleToImGuiVisibleState, ImGui::DisplayState::Visible);
  151. }
  152. }
  153. #endif
  154. void AtomImGuiToolsSystemComponent::OnCryEditorInitialized()
  155. {
  156. AzFramework::ConsoleRequestBus::Broadcast(&AzFramework::ConsoleRequestBus::Events::ExecuteConsoleCommand, "imgui_DiscreteInputMode 1");
  157. }
  158. } // namespace AtomImGuiTools