From da4708825088b18321a4f073344fc7216f243fe9 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 25 Sep 2023 18:34:07 +0200 Subject: [PATCH] Add conditional profiling in Editor (run only when using Profiler window) --- Source/Editor/Windows/Profiler/ProfilerWindow.cs | 8 +++++++- Source/Engine/Engine/Engine.cpp | 3 --- Source/Engine/Profiler/ProfilerCPU.cpp | 2 +- Source/Engine/Profiler/ProfilerGPU.cpp | 2 +- Source/Engine/Profiler/ProfilingTools.cpp | 11 +++++++++++ Source/Engine/Profiler/ProfilingTools.h | 10 ++++++++++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Source/Editor/Windows/Profiler/ProfilerWindow.cs b/Source/Editor/Windows/Profiler/ProfilerWindow.cs index f5a5c6f86..f68021358 100644 --- a/Source/Editor/Windows/Profiler/ProfilerWindow.cs +++ b/Source/Editor/Windows/Profiler/ProfilerWindow.cs @@ -93,7 +93,7 @@ namespace FlaxEditor.Windows.Profiler _liveRecordingButton = toolstrip.AddButton(editor.Icons.Play64); _liveRecordingButton.LinkTooltip("Live profiling events recording"); _liveRecordingButton.AutoCheck = true; - _liveRecordingButton.Clicked += () => _liveRecordingButton.Icon = LiveRecording ? editor.Icons.Stop64 : editor.Icons.Play64; + _liveRecordingButton.Clicked += OnLiveRecordingChanged; _clearButton = toolstrip.AddButton(editor.Icons.Rotate32, Clear); _clearButton.LinkTooltip("Clear data"); toolstrip.AddSeparator(); @@ -118,6 +118,12 @@ namespace FlaxEditor.Windows.Profiler _tabs.SelectedTabChanged += OnSelectedTabChanged; } + private void OnLiveRecordingChanged() + { + _liveRecordingButton.Icon = LiveRecording ? Editor.Icons.Stop64 : Editor.Icons.Play64; + ProfilingTools.Enabled = LiveRecording; + } + /// /// Adds the mode. /// diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp index bafcd30b3..ec56861ba 100644 --- a/Source/Engine/Engine/Engine.cpp +++ b/Source/Engine/Engine/Engine.cpp @@ -102,9 +102,6 @@ int32 Engine::Main(const Char* cmdLine) Platform::SetHighDpiAwarenessEnabled(!CommandLine::Options.LowDPI.IsTrue()); Time::StartupTime = DateTime::Now(); -#if COMPILE_WITH_PROFILER - ProfilerCPU::Enabled = true; -#endif Globals::StartupFolder = Globals::BinariesFolder = Platform::GetMainDirectory(); #if USE_EDITOR Globals::StartupFolder /= TEXT("../../../.."); diff --git a/Source/Engine/Profiler/ProfilerCPU.cpp b/Source/Engine/Profiler/ProfilerCPU.cpp index 417178747..26f9e49c2 100644 --- a/Source/Engine/Profiler/ProfilerCPU.cpp +++ b/Source/Engine/Profiler/ProfilerCPU.cpp @@ -205,7 +205,7 @@ int32 ProfilerCPU::BeginEvent(const char* name) void ProfilerCPU::EndEvent(int32 index) { - if (Enabled && Thread::Current) + if (index != -1 && Thread::Current) Thread::Current->EndEvent(index); } diff --git a/Source/Engine/Profiler/ProfilerGPU.cpp b/Source/Engine/Profiler/ProfilerGPU.cpp index c100b8df0..3054abe67 100644 --- a/Source/Engine/Profiler/ProfilerGPU.cpp +++ b/Source/Engine/Profiler/ProfilerGPU.cpp @@ -14,7 +14,7 @@ RenderStatsData RenderStatsData::Counter; int32 ProfilerGPU::_depth = 0; Array ProfilerGPU::_timerQueriesPool; Array ProfilerGPU::_timerQueriesFree; -bool ProfilerGPU::Enabled = true; +bool ProfilerGPU::Enabled = false; int32 ProfilerGPU::CurrentBuffer = 0; ProfilerGPU::EventBuffer ProfilerGPU::Buffers[PROFILER_GPU_EVENTS_FRAMES]; diff --git a/Source/Engine/Profiler/ProfilingTools.cpp b/Source/Engine/Profiler/ProfilingTools.cpp index 6ce8082ba..4c6a9e19e 100644 --- a/Source/Engine/Profiler/ProfilingTools.cpp +++ b/Source/Engine/Profiler/ProfilingTools.cpp @@ -175,4 +175,15 @@ void ProfilingToolsService::Dispose() ProfilingTools::EventsGPU.SetCapacity(0); } +bool ProfilingTools::GetEnabled() +{ + return ProfilerCPU::Enabled && ProfilerGPU::Enabled; +} + +void ProfilingTools::SetEnabled(bool enabled) +{ + ProfilerCPU::Enabled = enabled; + ProfilerGPU::Enabled = enabled; +} + #endif diff --git a/Source/Engine/Profiler/ProfilingTools.h b/Source/Engine/Profiler/ProfilingTools.h index e1c010d77..af9b79d80 100644 --- a/Source/Engine/Profiler/ProfilingTools.h +++ b/Source/Engine/Profiler/ProfilingTools.h @@ -106,6 +106,16 @@ public: }; public: + /// + /// Controls the engine profiler (CPU, GPU, etc.) usage. + /// + API_PROPERTY() static bool GetEnabled(); + + /// + /// Controls the engine profiler (CPU, GPU, etc.) usage. + /// + API_PROPERTY() static void SetEnabled(bool enabled); + /// /// The current collected main stats by the profiler from the local session. Updated every frame. ///