From 944aaad6d613d7be64d81f99952a0489ee7c93a7 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 24 Nov 2020 21:31:07 -0600 Subject: [PATCH] flag changed nodes as dirty to enhance shader reading looks like it double FPS, going from ~230 to ~500 FPS. The rest of it may have to do with text or something called UI painting. Additionally, the rener thread uses 0.4ms, which is super minor, but perhaps we could evenutally look into changing off from a shader? --- Paths/Assets/Scripts/ChangeController.cs | 33 +++++++++++++++++++++--- Paths/Assets/Scripts/GridController.cs | 28 +++++++++++++++++--- Paths/Assets/Scripts/Manager.cs | 5 ++-- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Paths/Assets/Scripts/ChangeController.cs b/Paths/Assets/Scripts/ChangeController.cs index 565d27e..1f084b9 100644 --- a/Paths/Assets/Scripts/ChangeController.cs +++ b/Paths/Assets/Scripts/ChangeController.cs @@ -8,7 +8,7 @@ using UnityEngine; public class ChangeController { private readonly GridNodeType[,] _initial; public GridNodeType[,] Current { get; private set; } - + public bool[,] DirtyFlags { get; private set; } public int Index { get; private set; } private readonly List _changes; public int Count => _changes.Count; @@ -20,6 +20,30 @@ public class ChangeController { Current = initial; Index = -1; _changes = new List(); + DirtyFlags = new bool[initial.GetLength(0), initial.GetLength(1)]; + SetDirty(); + } + + /// + /// Sets the entire grid as dirty, essentially re-rendering all values in the shader. + /// + public void SetDirty() { + int width = DirtyFlags.GetLength(0); + int height = DirtyFlags.GetLength(1); + + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + DirtyFlags[x, y] = true; + } + } + } + + /// + /// Sets only a specific position as dirty. + /// + /// + public void SetDirty(Vector2Int position) { + DirtyFlags[position.x, position.y] = true; } /// @@ -27,6 +51,7 @@ public class ChangeController { /// public void Reset() { Current = _initial; + SetDirty(); } /// @@ -50,6 +75,7 @@ public class ChangeController { for (int i = 0; i < n; i++) { Change cur = _changes[++Index]; Current[cur.X, cur.Y] = cur.New; + SetDirty(new Vector2Int(cur.X, cur.Y)); } } @@ -68,6 +94,7 @@ public class ChangeController { for (int i = 0; i < n; i++) { Change cur = _changes[--Index]; Current[cur.X, cur.Y] = cur.Old; + SetDirty(new Vector2Int(cur.X, cur.Y)); } } } @@ -116,11 +143,11 @@ public class ChangeController { public void RemovePositions(Vector2Int position, int count = -1) { if (count == 0) return; - + for (int i = _changes.Count - 1; i >= 0; i--) if (_changes[i].X == position.x && _changes[i].Y == position.y) { _changes.RemoveAt(i); - + // Return if the count is now zero. if (--count == 0) return; diff --git a/Paths/Assets/Scripts/GridController.cs b/Paths/Assets/Scripts/GridController.cs index 3a3fa82..60558e3 100644 --- a/Paths/Assets/Scripts/GridController.cs +++ b/Paths/Assets/Scripts/GridController.cs @@ -71,14 +71,36 @@ public class GridController : MonoBehaviour { /// public void LoadGridState(GridNodeType[,] state) { // Loop over matrix and set values via cast Enum to int - for (int x = 0; x < state.GetLength(0); x++) { - for (int y = 0; y < state.GetLength(1); y++) - this.SetValue(x, y, (int) state[x, y]); + int gridWidth = state.GetLength(0); + int gridHeight = state.GetLength(1); + for (int x = 0; x < gridWidth; x++) { + for (int y = 0; y < gridHeight; y++) + SetValue(x, y, (int) state[x, y]); } UpdateShader(PropertyName.Values); } + /// + /// A more performant method of loading GridState values into the shader. + /// + /// + /// + public void LoadDirtyGridState(GridNodeType[,] state, bool[,] dirtyFlags) { + int gridWidth = state.GetLength(0); + for (int x = 0; x < gridWidth; x++) { + int gridHeight = state.GetLength(1); + for (int y = 0; y < gridHeight; y++) + // only set value if the value has been marked as dirty + if (dirtyFlags[x, y]) { + SetValue(x, y, (int) state[x, y]); + dirtyFlags[x, y] = false; + } + } + + UpdateShader(PropertyName.Values); + } + /// /// Sets a value in the 1D array at a particular 2D coordinate /// diff --git a/Paths/Assets/Scripts/Manager.cs b/Paths/Assets/Scripts/Manager.cs index e484f71..1c2c9c9 100644 --- a/Paths/Assets/Scripts/Manager.cs +++ b/Paths/Assets/Scripts/Manager.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Algorithms; using LevelGeneration; using TMPro; @@ -70,7 +69,7 @@ public class Manager : MonoBehaviour { private void LoadNextState() { _state.MoveTo(CurrentIndex); - gridController.LoadGridState(_state.Current); + gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags); string pathCount = _path != null ? $"{_path.Count}" : "N/A"; debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +