mirror of
https://github.com/Xevion/Paths.git
synced 2025-12-06 15:15:51 -06:00
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?
This commit is contained in:
@@ -8,7 +8,7 @@ using UnityEngine;
|
|||||||
public class ChangeController {
|
public class ChangeController {
|
||||||
private readonly GridNodeType[,] _initial;
|
private readonly GridNodeType[,] _initial;
|
||||||
public GridNodeType[,] Current { get; private set; }
|
public GridNodeType[,] Current { get; private set; }
|
||||||
|
public bool[,] DirtyFlags { get; private set; }
|
||||||
public int Index { get; private set; }
|
public int Index { get; private set; }
|
||||||
private readonly List<Change> _changes;
|
private readonly List<Change> _changes;
|
||||||
public int Count => _changes.Count;
|
public int Count => _changes.Count;
|
||||||
@@ -20,6 +20,30 @@ public class ChangeController {
|
|||||||
Current = initial;
|
Current = initial;
|
||||||
Index = -1;
|
Index = -1;
|
||||||
_changes = new List<Change>();
|
_changes = new List<Change>();
|
||||||
|
DirtyFlags = new bool[initial.GetLength(0), initial.GetLength(1)];
|
||||||
|
SetDirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets the entire grid as dirty, essentially re-rendering all values in the shader.
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets only a specific position as dirty.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="position"></param>
|
||||||
|
public void SetDirty(Vector2Int position) {
|
||||||
|
DirtyFlags[position.x, position.y] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -27,6 +51,7 @@ public class ChangeController {
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Reset() {
|
public void Reset() {
|
||||||
Current = _initial;
|
Current = _initial;
|
||||||
|
SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -50,6 +75,7 @@ public class ChangeController {
|
|||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
Change cur = _changes[++Index];
|
Change cur = _changes[++Index];
|
||||||
Current[cur.X, cur.Y] = cur.New;
|
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++) {
|
for (int i = 0; i < n; i++) {
|
||||||
Change cur = _changes[--Index];
|
Change cur = _changes[--Index];
|
||||||
Current[cur.X, cur.Y] = cur.Old;
|
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) {
|
public void RemovePositions(Vector2Int position, int count = -1) {
|
||||||
if (count == 0)
|
if (count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int i = _changes.Count - 1; i >= 0; i--)
|
for (int i = _changes.Count - 1; i >= 0; i--)
|
||||||
if (_changes[i].X == position.x && _changes[i].Y == position.y) {
|
if (_changes[i].X == position.x && _changes[i].Y == position.y) {
|
||||||
_changes.RemoveAt(i);
|
_changes.RemoveAt(i);
|
||||||
|
|
||||||
// Return if the count is now zero.
|
// Return if the count is now zero.
|
||||||
if (--count == 0)
|
if (--count == 0)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -71,14 +71,36 @@ public class GridController : MonoBehaviour {
|
|||||||
/// <param name="state"></param>
|
/// <param name="state"></param>
|
||||||
public void LoadGridState(GridNodeType[,] state) {
|
public void LoadGridState(GridNodeType[,] state) {
|
||||||
// Loop over matrix and set values via cast Enum to int
|
// Loop over matrix and set values via cast Enum to int
|
||||||
for (int x = 0; x < state.GetLength(0); x++) {
|
int gridWidth = state.GetLength(0);
|
||||||
for (int y = 0; y < state.GetLength(1); y++)
|
int gridHeight = state.GetLength(1);
|
||||||
this.SetValue(x, y, (int) state[x, y]);
|
for (int x = 0; x < gridWidth; x++) {
|
||||||
|
for (int y = 0; y < gridHeight; y++)
|
||||||
|
SetValue(x, y, (int) state[x, y]);
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateShader(PropertyName.Values);
|
UpdateShader(PropertyName.Values);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A more performant method of loading GridState values into the shader.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="state"></param>
|
||||||
|
/// <param name="dirtyFlags"></param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets a value in the 1D array at a particular 2D coordinate
|
/// Sets a value in the 1D array at a particular 2D coordinate
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using Algorithms;
|
using Algorithms;
|
||||||
using LevelGeneration;
|
using LevelGeneration;
|
||||||
using TMPro;
|
using TMPro;
|
||||||
@@ -70,7 +69,7 @@ public class Manager : MonoBehaviour {
|
|||||||
|
|
||||||
private void LoadNextState() {
|
private void LoadNextState() {
|
||||||
_state.MoveTo(CurrentIndex);
|
_state.MoveTo(CurrentIndex);
|
||||||
gridController.LoadGridState(_state.Current);
|
gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags);
|
||||||
|
|
||||||
string pathCount = _path != null ? $"{_path.Count}" : "N/A";
|
string pathCount = _path != null ? $"{_path.Count}" : "N/A";
|
||||||
debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +
|
debugText.text = $"{_state.CurrentRuntime * 1000.0:F1}ms\n" +
|
||||||
|
|||||||
Reference in New Issue
Block a user