From e9946775662837c57d20a0f341ff5fe61519d192 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sun, 15 May 2022 18:00:22 +0300 Subject: [PATCH] performance widget changes --- Content/Common/DefaultHudPrefab.prefab | 197 +++++++++---------- Content/Scenes/MainScene.scene | 34 ++-- Source/Game/Console/ConsoleContentTextBox.cs | 9 - Source/Game/Hud/FpsScript.cs | 181 ----------------- Source/Game/Hud/PerformanceWidget.cs | 74 +++++++ 5 files changed, 189 insertions(+), 306 deletions(-) delete mode 100644 Source/Game/Hud/FpsScript.cs create mode 100644 Source/Game/Hud/PerformanceWidget.cs diff --git a/Content/Common/DefaultHudPrefab.prefab b/Content/Common/DefaultHudPrefab.prefab index 11bbfbd..4704d36 100644 --- a/Content/Common/DefaultHudPrefab.prefab +++ b/Content/Common/DefaultHudPrefab.prefab @@ -6,128 +6,127 @@ { "ID": "9e1b61dc44338f962e69f78ecb3d0536", "TypeName": "FlaxEngine.UICanvas", - "ParentID": "00000000000000000000000000000000", "Name": "DefaultHudPrefab", "Transform": { "Orientation": { "X": 1.0, - "Y": 4.3711388286737938e-8, - "Z": 4.3711388286737938e-8, + "Y": 4.371138828673793e-8, + "Z": 4.371138828673793e-8, "W": 1.910685465164705e-15 } }, "V": {} }, { - "ID": "d64fff8743e0d5893c235c94e7070976", - "TypeName": "Cabrito.FpsScript", + "ID": "e94561104b27c841e6ab0bb479bde17a", + "TypeName": "Game.ConsoleScript", "ParentID": "9e1b61dc44338f962e69f78ecb3d0536", "V": { - "control": "74464027453c88b3203fa79333f0730e" - } + "BackgroundColor": { + "R": 0.0, + "G": 0.0, + "B": 0.0, + "A": 1.0 + }, + "ConsoleFont": "43f32bec443158643f53699f07b2e09c" +} }, { - "ID": "0c7588dd41779a8ada6f57974d94e073", - "TypeName": "Cabrito.ConsoleScript", - "ParentID": "9e1b61dc44338f962e69f78ecb3d0536", - "V": { - "BackgroundColor": { - "R": 0.0, - "G": 0.0, - "B": 0.0, - "A": 1.0 - }, - "ConsoleFont": "43f32bec443158643f53699f07b2e09c" - } - }, - { - "ID": "74464027453c88b3203fa79333f0730e", + "ID": "926bc11f416c9cef57d383875a480ded", "TypeName": "FlaxEngine.UIControl", "ParentID": "9e1b61dc44338f962e69f78ecb3d0536", - "Name": "FPSUIControl", + "Name": "PerformanceWidget", "Transform": { "Translation": { - "X": 0.0, - "Y": 716.0, + "X": -1.0, + "Y": 38.0, "Z": 0.0 } }, "Control": "FlaxEngine.GUI.Label", "Data": { - "Text": "eFPS: 121 uTime: 116.42902\nuFPS: 120 uTime: 0\nrFPS: 120 rTime: 0\npFPS: 30 pTime: 0", - "TextColor": { - "R": 1.0, - "G": 1.0, - "B": 1.0, - "A": 1.0 - }, - "TextColorHighlighted": { - "R": 1.0, - "G": 1.0, - "B": 1.0, - "A": 1.0 - }, - "HorizontalAlignment": 0, - "VerticalAlignment": 0, - "Wrapping": 0, - "Font": { - "Font": "4508d98f4aa1f0bd59362b81d47e38f4", - "Size": 10 - }, - "Material": null, - "Margin": { - "Left": 0.0, - "Right": 0.0, - "Top": 0.0, - "Bottom": 0.0 - }, - "ClipText": false, - "AutoWidth": true, - "AutoHeight": true, - "AutoFitText": false, - "AutoFitTextRange": { - "X": 0.1, - "Y": 100.0 - }, - "ClipChildren": true, - "CullChildren": true, - "AnchorMin": { - "X": 0.0, - "Y": 1.0 - }, - "AnchorMax": { - "X": 0.0, - "Y": 1.0 - }, - "Offsets": { - "Left": 0.0, - "Right": 161.0, - "Top": -97.0, - "Bottom": 64.0 - }, - "Scale": { - "X": 1.0, - "Y": 1.0 - }, - "Pivot": { - "X": 0.5, - "Y": 0.5 - }, - "Shear": { - "X": 0.0, - "Y": 0.0 - }, - "Rotation": 0.0, - "BackgroundColor": { - "R": 0.0, - "G": 0.0, - "B": 0.0, - "A": 0.0 - }, - "Enabled": true, - "Visible": true, - "AutoFocus": false - } + "Text": "120fps", + "TextColor": { + "R": 1.0, + "G": 1.0, + "B": 1.0, + "A": 1.0 + }, + "TextColorHighlighted": { + "R": 1.0, + "G": 1.0, + "B": 1.0, + "A": 1.0 + }, + "HorizontalAlignment": 2, + "VerticalAlignment": 2, + "Wrapping": 0, + "Font": { + "Font": "43f32bec443158643f53699f07b2e09c", + "Size": 15 + }, + "Material": null, + "Margin": { + "Left": 0.0, + "Right": 0.0, + "Top": 0.0, + "Bottom": 0.0 + }, + "ClipText": false, + "AutoWidth": false, + "AutoHeight": false, + "AutoFitText": false, + "AutoFitTextRange": { + "X": 0.1, + "Y": 100.0 + }, + "ClipChildren": true, + "CullChildren": true, + "AnchorMin": { + "X": 1.0, + "Y": 1.0 + }, + "AnchorMax": { + "X": 1.0, + "Y": 1.0 + }, + "Offsets": { + "Left": -101.0, + "Right": 100.0, + "Top": -22.0, + "Bottom": 20.0 + }, + "Scale": { + "X": 1.0, + "Y": 1.0 + }, + "Pivot": { + "X": 0.5, + "Y": 0.5 + }, + "Shear": { + "X": 0.0, + "Y": 0.0 + }, + "Rotation": 0.0, + "BackgroundColor": { + "R": 0.0, + "G": 0.0, + "B": 0.0, + "A": 0.0 + }, + "Enabled": true, + "Visible": true, + "AutoFocus": false +} + }, + { + "ID": "85ddca4e46ed0c030d063599ac716e96", + "TypeName": "Game.PerformanceWidget", + "ParentID": "926bc11f416c9cef57d383875a480ded", + "V": { + "control": "926bc11f416c9cef57d383875a480ded" +} } ] } \ No newline at end of file diff --git a/Content/Scenes/MainScene.scene b/Content/Scenes/MainScene.scene index 938e7a0..76438ca 100644 --- a/Content/Scenes/MainScene.scene +++ b/Content/Scenes/MainScene.scene @@ -33,42 +33,42 @@ "V": {} }, { - "ID": "791756db4da9af04d7513f9f305e4652", + "ID": "02119c7243cb3a4a4ffcd395ff07e23d", "PrefabID": "cb74184545f392e5c67815808bc63e58", - "PrefabObjectID": "d64fff8743e0d5893c235c94e7070976", - "ParentID": "ff6b6db54b5aa08e7286ef86246149ef", - "V": { - "control": "d15fa816476f9d66ebdfc7ae25c3c6ea" -} - }, - { - "ID": "b20f2b0a486762a15e6a7caf72ae66c2", - "PrefabID": "cb74184545f392e5c67815808bc63e58", - "PrefabObjectID": "0c7588dd41779a8ada6f57974d94e073", + "PrefabObjectID": "e94561104b27c841e6ab0bb479bde17a", "ParentID": "ff6b6db54b5aa08e7286ef86246149ef", "V": {} }, { - "ID": "d15fa816476f9d66ebdfc7ae25c3c6ea", + "ID": "ad0c546649722a6c7a6b9aa8fdcac019", "PrefabID": "cb74184545f392e5c67815808bc63e58", - "PrefabObjectID": "74464027453c88b3203fa79333f0730e", + "PrefabObjectID": "926bc11f416c9cef57d383875a480ded", "ParentID": "ff6b6db54b5aa08e7286ef86246149ef", "Transform": { "Translation": { - "X": 0.0, - "Y": 716.0, + "X": 1355.0, + "Y": 791.0, "Z": 0.0 } }, "Data": { - "Text": "eFPS: 120 uTime: 4.1501515\nuFPS: 120 uTime: 0\nrFPS: 120 rTime: 0\npFPS: 30 pTime: 0" + "Text": "15fps" +} + }, + { + "ID": "35b34b0c4476f0ed43aa8fb4d5f2eebc", + "PrefabID": "cb74184545f392e5c67815808bc63e58", + "PrefabObjectID": "85ddca4e46ed0c030d063599ac716e96", + "ParentID": "ad0c546649722a6c7a6b9aa8fdcac019", + "V": { + "control": "ad0c546649722a6c7a6b9aa8fdcac019" } }, { "ID": "9b0c532d4247061970c809a0af58c82a", "TypeName": "FlaxEngine.PostFxVolume", "ParentID": "194e05f445ece24ec5448d886e1334df", - "Name": "PostFxVolume 0", + "Name": "GlobalPostFxVolume", "Size": { "X": 10.0, "Y": 10.0, diff --git a/Source/Game/Console/ConsoleContentTextBox.cs b/Source/Game/Console/ConsoleContentTextBox.cs index 9cbcf5f..ae8bffa 100644 --- a/Source/Game/Console/ConsoleContentTextBox.cs +++ b/Source/Game/Console/ConsoleContentTextBox.cs @@ -10,9 +10,6 @@ namespace Game { public class ConsoleContentTextBox : Control { - public static double accumDrawTime; - public static long accumDrawTimes; - private readonly FontReference Font; protected TextLayoutOptions _layout; @@ -194,8 +191,6 @@ namespace Game if (!font) return; - Stopwatch sw = Stopwatch.StartNew(); - // Background Profiler.BeginEvent("ConsoleContentTextBoxDraw_Background"); Color backColor = BackgroundColor; @@ -358,10 +353,6 @@ namespace Game Profiler.EndEvent(); } - - sw.Stop(); - accumDrawTime += sw.Elapsed.TotalSeconds; - accumDrawTimes++; } diff --git a/Source/Game/Hud/FpsScript.cs b/Source/Game/Hud/FpsScript.cs deleted file mode 100644 index 54aefcc..0000000 --- a/Source/Game/Hud/FpsScript.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System; -using System.Diagnostics; -using System.Text; -using FlaxEditor.Content.Settings; -using FlaxEngine; -using FlaxEngine.GUI; - -namespace Game -{ - [ExecuteInEditMode] - public class FpsScript : Script - { - private const double updateInterval = 0.25; - private const double drawInterval = 0.25; - private const double physicsInterval = 0.25; - - private double conTime; - public UIControl control; - - private string currentRenderer = "Unknown"; - private double drawAccumTime; - private double drawTimeAvg; - private ulong drawTimeCount; - private Label label; - private double physicsAccumTime; - private double physicsTimeAvg; - private ulong physicsTimeCount; - - private Stopwatch sw; - - private Stopwatch sw0; - - private Stopwatch sw2; - - private Stopwatch sw3; - - private RenderTask t; - - private TimeSettings timeSettings; - private double updateAccumTime; - private double updateTimeAvg; - private ulong updateTimeCount; - - public override void OnAwake() - { - label = (Label)control.Control; - - sw = Stopwatch.StartNew(); - sw2 = Stopwatch.StartNew(); - sw3 = Stopwatch.StartNew(); - sw0 = Stopwatch.StartNew(); - - currentRenderer = GPUDevice.Instance.RendererType.ToString(); - - if (t == null) - { - //Destroy(t); - t = new RenderTask(); - t.Render += OnDraw; - } - - GameSettings settings = GameSettings.Load(); - timeSettings = settings.Time.CreateInstance(); - } - - public override void OnDestroy() - { - Destroy(t); - t = null; - } - - public override void OnUpdate() - { - updateAccumTime += Time.DeltaTime; - updateTimeCount++; - double elapsed = sw.Elapsed.TotalSeconds; - if (elapsed >= updateInterval) - { - sw.Restart(); - updateTimeAvg = elapsed / updateTimeCount; - updateTimeCount = 0; - - conTime = ConsoleContentTextBox.accumDrawTime / ConsoleContentTextBox.accumDrawTimes * 1000.0; - ConsoleContentTextBox.accumDrawTime = 0.0; - ConsoleContentTextBox.accumDrawTimes = 0; - } - - StringBuilder sb = new StringBuilder(); - sb.Append("eFPS: " + Engine.FramesPerSecond); - sb.Append(" uTime: " + sw0.Elapsed.TotalSeconds); - sb.Append("\nuFPS: " + (int)Math.Round(1.0f / updateTimeAvg)); - sb.Append(" uTime: " + updateAccumTime); - sb.Append("\nrFPS: " + (int)Math.Round(1.0f / drawTimeAvg)); - sb.Append(" rTime: " + drawAccumTime); - sb.Append("\npFPS: " + (int)Math.Round(1.0f / physicsTimeAvg)); - sb.Append(" pTime: " + physicsAccumTime); - //sb.Append("\nCon: " + conTime.ToString() + "ms"); - //sb.Append("\nGC memory: " + (GC.GetTotalMemory(false) / 1000000.0f).ToString() + "MB"); - //sb.Append("\nUpdate profiler: " + updateProfTime.ToString() + "ms"); - -#if false -#if BUILD_DEVELOPMENT - var nameOffset = Marshal.OffsetOf(typeof(ProfilerCPU.Event), "Name0"); - foreach (var eventsCpu in FlaxEngine.ProfilingTools.EventsCPU) - { - sb.Append("\nName: " + eventsCpu.Name + " "); - foreach (var eventt in eventsCpu.Events) - { -#if FLAX_EDITOR - string eventName = eventt.Name; -#else - string eventName; - unsafe - { - var ptr1 = &eventt; - char* ptr2 = (char*)(new IntPtr((byte*)ptr1 + nameOffset.ToInt64())); - //fixed (char* name = &eventt) - { - eventName = new string(ptr2); - } - } -#endif - - sb.Append(eventName + " " + (eventt.End-eventt.Start) + "ms, "); - } - - } -#endif -#endif - - ((Label)control.Control).Text = sb.ToString(); - /*if (!Platform.HasFocus) - { - Time.UpdateFPS = 15; - Time.DrawFPS = 15; - Time.PhysicsFPS = 15; - } -#if FLAX_EDITOR - else if (!FlaxEditor.Editor.IsPlayMode) - { - var editorFPS = FlaxEditor.Editor.Instance.Options.Options.General.EditorFPS; - Time.UpdateFPS = editorFPS; - Time.DrawFPS = editorFPS; - Time.PhysicsFPS = timeSettings.PhysicsFPS; - } -#endif - else - { - Time.UpdateFPS = timeSettings.UpdateFPS; - Time.DrawFPS = timeSettings.DrawFPS; - Time.PhysicsFPS = timeSettings.PhysicsFPS; - }*/ - } - - public override void OnFixedUpdate() - { - physicsAccumTime += Time.DeltaTime; - physicsTimeCount++; - double elapsed = sw3.Elapsed.TotalSeconds; - if (elapsed >= physicsInterval) - { - sw3.Restart(); - physicsTimeAvg = elapsed / physicsTimeCount; - physicsTimeCount = 0; - } - } - - private void OnDraw(RenderTask tt, GPUContext context) - { - drawAccumTime += Time.DeltaTime; - drawTimeCount++; - double elapsed = sw2.Elapsed.TotalSeconds; - if (elapsed >= drawInterval) - { - sw2.Restart(); - drawTimeAvg = elapsed / drawTimeCount; - drawTimeCount = 0; - } - } - } -} \ No newline at end of file diff --git a/Source/Game/Hud/PerformanceWidget.cs b/Source/Game/Hud/PerformanceWidget.cs new file mode 100644 index 0000000..fa66210 --- /dev/null +++ b/Source/Game/Hud/PerformanceWidget.cs @@ -0,0 +1,74 @@ +using System; +using System.Diagnostics; +using FlaxEngine; +using FlaxEngine.GUI; + +namespace Game +{ + [ExecuteInEditMode] + public class PerformanceWidget : Script + { + private const double updateInterval = 0.25; + + public UIControl control; + private Label label; + + private Stopwatch stopwatch; + + private double updateTimeAvg; + private ulong updateTimeCount; + + public override void OnAwake() + { + label = (Label)control.Control; + label.Text = $"0fps"; + control.HideFlags = HideFlags.None; + + stopwatch = Stopwatch.StartNew(); + } + + public override void OnUpdate() + { + updateTimeCount++; + double elapsed = stopwatch.Elapsed.TotalSeconds; + if (elapsed >= updateInterval) + { + stopwatch.Restart(); + updateTimeAvg = elapsed / updateTimeCount; + updateTimeCount = 0; + + label.Text = $"{(int)Math.Round(1.0f / updateTimeAvg)}fps"; + } + +#if false +#if BUILD_DEVELOPMENT + var nameOffset = Marshal.OffsetOf(typeof(ProfilerCPU.Event), "Name0"); + foreach (var eventsCpu in FlaxEngine.ProfilingTools.EventsCPU) + { + sb.Append("\nName: " + eventsCpu.Name + " "); + foreach (var eventt in eventsCpu.Events) + { +#if FLAX_EDITOR + string eventName = eventt.Name; +#else + string eventName; + unsafe + { + var ptr1 = &eventt; + char* ptr2 = (char*)(new IntPtr((byte*)ptr1 + nameOffset.ToInt64())); + //fixed (char* name = &eventt) + { + eventName = new string(ptr2); + } + } +#endif + + sb.Append(eventName + " " + (eventt.End-eventt.Start) + "ms, "); + } + + } +#endif +#endif + } + } +} \ No newline at end of file