Skip to content

Commit

Permalink
Merge pull request #29 from dkavolis/voxel_threading
Browse files Browse the repository at this point in the history
Thread safe DebugVisualVoxelMeshController
  • Loading branch information
dkavolis authored Mar 12, 2019
2 parents 43418da + e1fdd1a commit 47307e3
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<PropertyGroup>
<KSP_DIR_BUILD>C:\Zaidimai\KSP 1.6.1\</KSP_DIR_BUILD>
<KSP_DIR_INSTALL>$(KSP_DIR_BUILD)</KSP_DIR_INSTALL>
<PDB2MDB>..\KSP Assemblies\pdb2mdb.exe</PDB2MDB>
<PDB2MDB>$(SolutionDir)..\KSP Assemblies\pdb2mdb.exe</PDB2MDB>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ You should have received a copy of the GNU General Public License
using System;
using System.Collections.Generic;
using UnityEngine;
using FerramAerospaceResearch.FARThreading;
using FerramAerospaceResearch.FARUtils;

namespace FerramAerospaceResearch.FARPartGeometry
Expand All @@ -61,7 +62,6 @@ public class DebugVisualVoxelMeshController : IDisposable
public DebugVisualVoxelMeshController(Transform parent = null)
{
m_parent = parent;
m_submeshes.Add(DebugVisualVoxelSubmesh.Create(m_parent, m_active));
}

public bool active
Expand All @@ -71,11 +71,8 @@ public bool active
{
if (m_active != value)
{
foreach (DebugVisualVoxelSubmesh submesh in m_submeshes)
{
submesh.active = value;
}
m_active = value;
QueueMainThreadTask(UpdateActive);
}
}
}
Expand All @@ -90,6 +87,14 @@ public Transform Parent
set => m_parent = value;
}

private void UpdateActive()
{
foreach (DebugVisualVoxelSubmesh submesh in m_submeshes)
{
submesh.active = m_active;
}
}

public void Rebuild()
{
FARLogger.Info("Rebuilding visual voxel mesh...");
Expand All @@ -109,6 +114,11 @@ public void Rebuild()
FARLogger.Info("Finished rebuilding visual voxel mesh.");
}

public void RebuildSafe()
{
QueueMainThreadTask(Rebuild);
}

public void Clear(bool clearVoxels = false)
{
foreach (DebugVisualVoxelSubmesh submesh in m_submeshes)
Expand All @@ -121,6 +131,11 @@ public void Clear(bool clearVoxels = false)
}
}

public void ClearSafe(bool clearVoxels = false)
{
QueueMainThreadTask(() => Clear(clearVoxels));
}

private void SetupSubmeshes(int submeshes)
{
for (int i = m_submeshes.Count; i < submeshes; i++)
Expand All @@ -130,11 +145,31 @@ private void SetupSubmeshes(int submeshes)
}

public void Dispose()
{
QueueMainThreadTask(DisposeSafe);
}

private void DisposeSafe()
{
foreach (DebugVisualVoxelSubmesh submesh in m_submeshes)
{
MonoBehaviour.Destroy(submesh);
}
}

private void QueueMainThreadTask(Action action)
{
if (VoxelizationThreadpool.Instance.inMainThread)
{
FARLogger.Debug("In main thread, not queueing " + action.Method.Name);
action();
}
else
{
ThreadSafeDebugLogger.Instance.RegisterDebugMessage("Running " + action.Method.Name + " in main thread");
VoxelizationThreadpool.Instance.RunOnMainThread(action);
}
}

}
}
2 changes: 1 addition & 1 deletion FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ public void VisualizeVoxel(Matrix4x4 vesselLocalToWorldMatrix)
}
}
}
voxelMesh.Rebuild();
voxelMesh.RebuildSafe();
voxelMesh.active = true;
}
#endregion
Expand Down
39 changes: 36 additions & 3 deletions FerramAerospaceResearch/FARThreading/ThreadSafeDebugLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ You should have received a copy of the GNU General Public License

using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using FerramAerospaceResearch.FARUtils;

Expand All @@ -55,16 +56,21 @@ class ThreadSafeDebugLogger : MonoBehaviour
static ThreadSafeDebugLogger _instance;
public static ThreadSafeDebugLogger Instance
{
get { return _instance; }
get
{
return _instance;
}
}

List<Exception> _exceptionsThrown;
List<string> _infoMessages;
List<string> _debugMessages;

void Awake()
{
_instance = this;
_exceptionsThrown = new List<Exception>();
_infoMessages = new List<string>();
_debugMessages = new List<string>();
GameObject.DontDestroyOnLoad(this.gameObject);
}
Expand All @@ -79,7 +85,28 @@ void Update()
_exceptionsThrown.Clear();
}

UpdateInfo();
UpdateDebug();
}

[Conditional("DEBUG"), Conditional("INFO")]
private void UpdateInfo()
{
if (_infoMessages.Count > 0)
{
System.Text.StringBuilder sB = new System.Text.StringBuilder();
for (int i = 0; i < _infoMessages.Count; i++)
sB.AppendLine(_infoMessages[i]);

_infoMessages.Clear();

FARLogger.Info("" + sB.ToString());
}
}

[Conditional("DEBUG")]
private void UpdateDebug()
{
if (_debugMessages.Count > 0)
{
System.Text.StringBuilder sB = new System.Text.StringBuilder();
Expand All @@ -88,12 +115,18 @@ void Update()

_debugMessages.Clear();

FARLogger.Info("" + sB.ToString());
FARLogger.Debug("" + sB.ToString());
}

}

[Conditional("DEBUG"), Conditional("INFO")]
public void RegisterMessage(string s)
{
_infoMessages.Add(s);
}

[Conditional("DEBUG")]
public void RegisterDebugMessage(string s)
{
_debugMessages.Add(s);
}
Expand Down
40 changes: 31 additions & 9 deletions FerramAerospaceResearch/FARThreading/VoxelizationThreadpool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You should have received a copy of the GNU General Public License
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using FerramAerospaceResearch.FARUtils;

namespace FerramAerospaceResearch.FARThreading
{
Expand All @@ -54,8 +55,7 @@ class VoxelizationThreadpool
{
// Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
static VoxelizationThreadpool()
{
}
{ }

public static readonly VoxelizationThreadpool Instance = new VoxelizationThreadpool();

Expand All @@ -79,6 +79,16 @@ public Task(Action action)

public static bool RunInMainThread = false;

private Thread _mainThread = null;

public bool inMainThread
{
get
{
return _mainThread == Thread.CurrentThread;
}
}

private VoxelizationThreadpool()
{
_threads = new Thread[THREAD_COUNT];
Expand All @@ -90,22 +100,30 @@ private VoxelizationThreadpool()
//_threads[i].IsBackground = true;
_threads[i].Start();
}
// make sure we get main thread while in main thread, ctor is not guaranteed to be run in main thread
queuedMainThreadTasks.Enqueue(new Task(SetupMainThread));
}

~VoxelizationThreadpool()
{
for (int i = 0; i < _threads.Length; i++)
{
QueueVoxelization(null); //this will pass a null action to each thread, ending it
QueueVoxelization(null); //this will pass a null action to each thread, ending it
}
}

void SetupMainThread()
{
_mainThread = Thread.CurrentThread;
FARLogger.Debug("Main thread: " + _mainThread.Name);
}

void ExecuteQueuedVoxelization()
{
while (true)
{
Action task;
lock (this)
lock(this)
{
while (queuedVoxelizations.Count == 0)
{
Expand All @@ -123,7 +141,7 @@ void ExecuteQueuedVoxelization()

public void QueueVoxelization(Action voxelAction)
{
lock (this)
lock(this)
{
queuedVoxelizations.Enqueue(voxelAction);
Monitor.Pulse(this);
Expand All @@ -132,12 +150,16 @@ public void QueueVoxelization(Action voxelAction)

public void RunOnMainThread(Action action)
{
if (inMainThread)
{
action();
}
var task = new Task(action);
lock (queuedMainThreadTasks)
lock(queuedMainThreadTasks)
{
queuedMainThreadTasks.Enqueue(task);
}
lock (task)
lock(task)
{
while (!task.Executed)
{
Expand All @@ -151,14 +173,14 @@ public void ExecuteMainThreadTasks()
while (true)
{
Task task;
lock (queuedMainThreadTasks)
lock(queuedMainThreadTasks)
{
if (queuedMainThreadTasks.Count == 0)
break;
task = queuedMainThreadTasks.Dequeue();
}
task.Action();
lock (task)
lock(task)
{
task.Executed = true;
Monitor.Pulse(task);
Expand Down
Binary file not shown.
Binary file modified GameData/FerramAerospaceResearch/Plugins/ferramGraph.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ CHANGELOG
-------------------master branch-------------------

Fix new stock parts ([#24](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/24), thanks [@HSJasperism](https://github.com/HSJasperism))
Greatly improved debug visual voxels framerate ([#18](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/18))
Greatly improved debug visual voxels framerate ([#18](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/18), [#29](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/29))
Enlarged debug voxel texture for higher fidelity ([#18](https://github.com/dkavolis/Ferram-Aerospace-Research/pull/18))

0.15.9.6V "Lin"------------------------------------
Expand Down

0 comments on commit 47307e3

Please sign in to comment.