From 6480bc8b863efbd2f624f64de26d5efe397fdc1b Mon Sep 17 00:00:00 2001 From: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com> Date: Fri, 15 Oct 2021 18:44:10 -0700 Subject: [PATCH] [atom_cpu_profiler_gem_promotion] additional APIs for external control of the CPU profiler and visualization Signed-off-by: AMZN-ScottR <24445312+AMZN-ScottR@users.noreply.github.com> --- .../Code/Include/Profiler/ProfilerBus.h | 3 ++ .../Code/Include/Profiler/ProfilerImGuiBus.h | 35 +++++++++++++++++++ .../Source/ProfilerImGuiSystemComponent.cpp | 13 ++++--- .../Source/ProfilerImGuiSystemComponent.h | 7 ++-- .../Code/Source/ProfilerSystemComponent.cpp | 5 +++ .../Code/Source/ProfilerSystemComponent.h | 1 + Gems/Profiler/Code/profiler_files.cmake | 1 + 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 Gems/Profiler/Code/Include/Profiler/ProfilerImGuiBus.h diff --git a/Gems/Profiler/Code/Include/Profiler/ProfilerBus.h b/Gems/Profiler/Code/Include/Profiler/ProfilerBus.h index 22d02dcc87..22352185d3 100644 --- a/Gems/Profiler/Code/Include/Profiler/ProfilerBus.h +++ b/Gems/Profiler/Code/Include/Profiler/ProfilerBus.h @@ -19,6 +19,9 @@ namespace Profiler AZ_RTTI(ProfilerRequests, "{3757c4e5-1941-457c-85ae-16305e17a4c6}"); virtual ~ProfilerRequests() = default; + //! Enable/Disable the CpuProfiler + virtual void SetProfilerEnabled(bool enabled) = 0; + //! Dump a single frame of Cpu profiling data virtual bool CaptureCpuProfilingStatistics(const AZStd::string& outputFilePath) = 0; diff --git a/Gems/Profiler/Code/Include/Profiler/ProfilerImGuiBus.h b/Gems/Profiler/Code/Include/Profiler/ProfilerImGuiBus.h new file mode 100644 index 0000000000..5bc41e2b3c --- /dev/null +++ b/Gems/Profiler/Code/Include/Profiler/ProfilerImGuiBus.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ +#pragma once + +#include + +namespace Profiler +{ + class ProfilerImGuiRequests + { + public: + AZ_RTTI(ProfilerImGuiRequests, "{E0443400-D108-4D3F-8FF5-4F076FCF6D13}"); + virtual ~ProfilerImGuiRequests() = default; + + // special request to render the CPU profiler window in a non-standard way + // e.g not through ImGuiUpdateListenerBus::OnImGuiUpdate + virtual void ShowCpuProfilerWindow(bool& keepDrawing) = 0; + }; + + class ProfilerImGuiBusTraits + : public AZ::EBusTraits + { + public: + // EBusTraits overrides + static constexpr AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single; + static constexpr AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single; + }; + + using ProfilerImGuiRequestBus = AZ::EBus; +} // namespace Profiler diff --git a/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.cpp b/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.cpp index a6dad90d9f..73fdc83cdf 100644 --- a/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.cpp +++ b/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.cpp @@ -61,13 +61,10 @@ namespace Profiler { } - void ProfilerImGuiSystemComponent::Init() - { - } - void ProfilerImGuiSystemComponent::Activate() { #if defined(IMGUI_ENABLED) + ProfilerImGuiRequestBus::Handler::BusConnect(); ImGui::ImGuiUpdateListenerBus::Handler::BusConnect(); #endif // defined(IMGUI_ENABLED) } @@ -75,16 +72,22 @@ namespace Profiler void ProfilerImGuiSystemComponent::Deactivate() { #if defined(IMGUI_ENABLED) + ProfilerImGuiRequestBus::Handler::BusDisconnect(); ImGui::ImGuiUpdateListenerBus::Handler::BusDisconnect(); #endif // defined(IMGUI_ENABLED) } #if defined(IMGUI_ENABLED) + void ProfilerImGuiSystemComponent::ShowCpuProfilerWindow(bool& keepDrawing) + { + m_imguiCpuProfiler.Draw(keepDrawing); + } + void ProfilerImGuiSystemComponent::OnImGuiUpdate() { if (m_showCpuProfiler) { - m_imguiCpuProfiler.Draw(m_showCpuProfiler); + ShowCpuProfilerWindow(m_showCpuProfiler); } } diff --git a/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.h b/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.h index 8f25e4652a..04ac8db4ca 100644 --- a/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.h +++ b/Gems/Profiler/Code/Source/ProfilerImGuiSystemComponent.h @@ -8,7 +8,7 @@ #pragma once -#include +#include #include @@ -24,6 +24,7 @@ namespace Profiler class ProfilerImGuiSystemComponent : public AZ::Component #if defined(IMGUI_ENABLED) + , public ProfilerImGuiRequestBus::Handler , public ImGui::ImGuiUpdateListenerBus::Handler #endif // defined(IMGUI_ENABLED) { @@ -42,11 +43,13 @@ namespace Profiler protected: // AZ::Component interface implementation - void Init() override; void Activate() override; void Deactivate() override; #if defined(IMGUI_ENABLED) + // ProfilerImGuiRequestBus interface implementation + void ShowCpuProfilerWindow(bool& keepDrawing) override; + // ImGuiUpdateListenerBus overrides void OnImGuiUpdate() override; void OnImGuiMainMenuUpdate() override; diff --git a/Gems/Profiler/Code/Source/ProfilerSystemComponent.cpp b/Gems/Profiler/Code/Source/ProfilerSystemComponent.cpp index c51ed91ce1..f876a8d4c4 100644 --- a/Gems/Profiler/Code/Source/ProfilerSystemComponent.cpp +++ b/Gems/Profiler/Code/Source/ProfilerSystemComponent.cpp @@ -200,6 +200,11 @@ namespace Profiler } } + void ProfilerSystemComponent::SetProfilerEnabled(bool enabled) + { + m_cpuProfiler.SetProfilerEnabled(enabled); + } + bool ProfilerSystemComponent::CaptureCpuProfilingStatistics(const AZStd::string& outputFilePath) { bool expected = false; diff --git a/Gems/Profiler/Code/Source/ProfilerSystemComponent.h b/Gems/Profiler/Code/Source/ProfilerSystemComponent.h index 67519c098b..76121be04f 100644 --- a/Gems/Profiler/Code/Source/ProfilerSystemComponent.h +++ b/Gems/Profiler/Code/Source/ProfilerSystemComponent.h @@ -39,6 +39,7 @@ namespace Profiler void Deactivate() override; // ProfilerRequestBus interface implementation + void SetProfilerEnabled(bool enabled) override; bool CaptureCpuProfilingStatistics(const AZStd::string& outputFilePath) override; bool BeginContinuousCpuProfilingCapture() override; bool EndContinuousCpuProfilingCapture(const AZStd::string& outputFilePath) override; diff --git a/Gems/Profiler/Code/profiler_files.cmake b/Gems/Profiler/Code/profiler_files.cmake index bd31155b9e..51ceb00139 100644 --- a/Gems/Profiler/Code/profiler_files.cmake +++ b/Gems/Profiler/Code/profiler_files.cmake @@ -8,6 +8,7 @@ set(FILES Include/Profiler/ProfilerBus.h + Include/Profiler/ProfilerImGuiBus.h Source/CpuProfiler.h Source/CpuProfilerImpl.cpp Source/CpuProfilerImpl.h