diff --git a/Editor/Settings/DebugXSettings.cs b/Editor/Settings/DebugXSettings.cs index e5016b3..a2fb49b 100644 --- a/Editor/Settings/DebugXSettings.cs +++ b/Editor/Settings/DebugXSettings.cs @@ -32,22 +32,23 @@ namespace DCFApixels.DebugXCore.Internal private void OnGUI() { _pos = GUILayout.BeginScrollView(_pos, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); - float tmpValue; + float tmpFloat; + int tmpInt; DebugX.GlobalTimeScale = EditorGUILayout.FloatField("TimeScale", DebugX.GlobalTimeScale); EditorGUI.BeginChangeCheck(); - tmpValue = EditorGUILayout.Slider(DebugX.GlobalTimeScale, 0, 2); + tmpFloat = EditorGUILayout.Slider(DebugX.GlobalTimeScale, 0, 2); if (EditorGUI.EndChangeCheck()) { - DebugX.GlobalTimeScale = tmpValue; + DebugX.GlobalTimeScale = tmpFloat; } DebugX.GlobalDotSize = EditorGUILayout.FloatField("DotSize", DebugX.GlobalDotSize); EditorGUI.BeginChangeCheck(); - tmpValue = EditorGUILayout.Slider(DebugX.GlobalDotSize, 0, 2); + tmpFloat = EditorGUILayout.Slider(DebugX.GlobalDotSize, 0, 2); if (EditorGUI.EndChangeCheck()) { - DebugX.GlobalDotSize = tmpValue; + DebugX.GlobalDotSize = tmpFloat; } DebugX.GlobalColor = EditorGUILayout.ColorField("Color", DebugX.GlobalColor); @@ -58,12 +59,16 @@ namespace DCFApixels.DebugXCore.Internal DebugX.GlobalGreaterPassAlpha = EditorGUILayout.FloatField("GreaterPassAlpha", DebugX.GlobalGreaterPassAlpha); EditorGUI.BeginChangeCheck(); - tmpValue = EditorGUILayout.Slider(DebugX.GlobalGreaterPassAlpha, 0, 1); + tmpFloat = EditorGUILayout.Slider(DebugX.GlobalGreaterPassAlpha, 0, 1); if (EditorGUI.EndChangeCheck()) { - DebugX.GlobalGreaterPassAlpha = tmpValue; + DebugX.GlobalGreaterPassAlpha = tmpFloat; } + DebugX.AvailablePoolMemory = EditorGUILayout.IntField("AvailablePoolMemory (kb)", DebugX.AvailablePoolMemory); + EditorGUI.BeginChangeCheck(); + + if (GUILayout.Button("Reset")) { DebugX.ResetGlobals(); diff --git a/Runtime/Consts.cs b/Runtime/Consts.cs index 85a7b1c..0b4f358 100644 --- a/Runtime/Consts.cs +++ b/Runtime/Consts.cs @@ -27,6 +27,7 @@ namespace DCFApixels private const string GLOBAL_DOT_SIZE_PREF_NAME = "DCFApixels.DebugX.DotSize"; private const string GLOBAL_COLOR_PREF_NAME = "DCFApixels.DebugX.Color"; private const string GLOBAL_GREATER_PASS_ALPHA_PREF_NAME = "DCFApixels.DebugX.GreaterPassAlpha"; + private const string GLOBAL_AVAILABLE_POOL_MEMORY_PREF_NAME = "DCFApixels.DebugX.AvailablePoolMemory"; private readonly static int GlobalDotSizePropertyID = Shader.PropertyToID("_DebugX_GlobalDotSize"); private readonly static int GlobalColorPropertyID = Shader.PropertyToID("_DebugX_GlobalColor"); diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 8040e9c..e505723 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -17,6 +17,8 @@ using UnityEngine.PlayerLoop; using Unity.Collections.LowLevel.Unsafe; using DCFApixels.DebugXCore.Internal; using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace DCFApixels { @@ -661,6 +663,25 @@ namespace DCFApixels } private class GizmosBuffer : GizmosBuffer where T : IGizmo { + private static int _lastAvailablePoolMemory; + private static int _bufferMaxSize; + private static readonly int _elementSize = RuntimeHelpers.IsReferenceOrContainsReferences() ? sizeof(IntPtr) : Marshal.SizeOf(); + private static void CheckAvailablePoolMemory() + { + if(_bufferMaxSize != 0 && _lastAvailablePoolMemory == DebugX.AvailablePoolMemory) + { + return; + } + if(DebugX.AvailablePoolMemory < _elementSize * 10) + { + _bufferMaxSize = DebugX.AvailablePoolMemory * 1024 / _elementSize; + } + else + { + _bufferMaxSize = DebugX.AvailablePoolMemory / _elementSize * 1024; + } + _lastAvailablePoolMemory = DebugX.AvailablePoolMemory; + } private class DummyRenderer : IGizmoRenderer { public int ExecuteOrder { get { return 0; } } @@ -714,17 +735,24 @@ namespace DCFApixels [IN(LINE)] public void Add(T value, float time, Color color) { + CheckAvailablePoolMemory(); + if ((_gizmos.Count + 1) >= _bufferMaxSize) { return; } + _gizmos.Add(new GizmoInternal(value, time, color)); } [IN(LINE)] public void AddRange(ReadOnlySpan values, float time, Color color) { + CheckAvailablePoolMemory(); + if ((_gizmos.Count + values.Length) >= _bufferMaxSize) { return; } + _gizmos.UpSize(_gizmos._count + values.Length); for (int i = 0; i < values.Length; i++) { _gizmos.Add(new GizmoInternal(values[i], time, color)); } } + public sealed override int UpdateTimer(float deltaTime) { diff --git a/Runtime/DebugX.globals.cs b/Runtime/DebugX.globals.cs index c2f68a0..8644964 100644 --- a/Runtime/DebugX.globals.cs +++ b/Runtime/DebugX.globals.cs @@ -67,6 +67,20 @@ namespace DCFApixels #if UNITY_EDITOR EditorPrefs.SetFloat(GLOBAL_GREATER_PASS_ALPHA_PREF_NAME, _globalGreaterPassAlphaCache); +#endif + } + } + private static int _availablePoolMemory; //KB + public static int AvailablePoolMemory + { + get { return _availablePoolMemory; } + set + { + value = Mathf.Max(0, value); + if (_availablePoolMemory == value) { return; } + _availablePoolMemory = value; +#if UNITY_EDITOR + EditorPrefs.SetInt(GLOBAL_AVAILABLE_POOL_MEMORY_PREF_NAME, value); #endif } } @@ -77,6 +91,7 @@ namespace DCFApixels EditorPrefs.DeleteKey(GLOBAL_DOT_SIZE_PREF_NAME); EditorPrefs.DeleteKey(GLOBAL_COLOR_PREF_NAME); EditorPrefs.DeleteKey(GLOBAL_GREATER_PASS_ALPHA_PREF_NAME); + EditorPrefs.DeleteKey(GLOBAL_AVAILABLE_POOL_MEMORY_PREF_NAME); #endif _timeScaleCache = default; _dotSizeCache = default; @@ -92,11 +107,13 @@ namespace DCFApixels var colorCode = EditorPrefs.GetInt(GLOBAL_COLOR_PREF_NAME, -1); GlobalColor = (Color)(*(Color32*)&colorCode); GlobalGreaterPassAlpha = EditorPrefs.GetFloat(GLOBAL_GREATER_PASS_ALPHA_PREF_NAME, 0.1f); + AvailablePoolMemory = EditorPrefs.GetInt(GLOBAL_AVAILABLE_POOL_MEMORY_PREF_NAME, 524288); #else GlobalTimeScale = 1; GlobalDotSize = 1; GlobalColor = Color.white; GlobalGreaterPassAlpha = 0.1f; + AvailablePoolMemory = 524288; #endif } } diff --git a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs index c0906f8..befb7a7 100644 --- a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs +++ b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs @@ -29,7 +29,7 @@ namespace DCFApixels _singleWarningToggle = false; } #endif - settings = settings.SetSize(DebugXTextSettings.DEFAULT_FONT_SIZE); + settings = settings.Size(DebugXTextSettings.DEFAULT_FONT_SIZE); } return h.Gizmo(new TextGizmo(position, text, settings)); }