daily update on 20240620 by nitload youmu
This commit is contained in:
@@ -96,8 +96,6 @@ namespace StgSharp
|
||||
vsyncActivated = value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}//------------------------end of class------------------------//
|
||||
}//-------------------------------------- End of Class ---------------------------------------//
|
||||
}
|
||||
@@ -86,7 +86,7 @@ namespace StgSharp.Geometries
|
||||
/// <summary>
|
||||
/// Calculate movement to center
|
||||
/// </summary>
|
||||
/// <param name="tick">Current time tick</param>
|
||||
/// <param name="tick">CurrentBlueprint time tick</param>
|
||||
/// <returns></returns>
|
||||
public virtual vec3d MovCenter(uint tick)
|
||||
{
|
||||
|
||||
@@ -181,7 +181,11 @@ namespace StgSharp.Graphics
|
||||
return target;
|
||||
}
|
||||
|
||||
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution => InternalDraw;
|
||||
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output)
|
||||
{
|
||||
InternalDraw(in input, in output);
|
||||
}
|
||||
|
||||
|
||||
#region native camera operation
|
||||
protected abstract void NativeCameraViewRange(Radius fieldOfRange, vec2d offset, (float frontDepth, float backDepth) viewDepth);
|
||||
@@ -223,8 +227,6 @@ namespace StgSharp.Graphics
|
||||
protected abstract Shader CreateShaderSegment(ShaderType type, int count);
|
||||
protected abstract ShaderProgram CreateShaderProgram();
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
//
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
using StgSharp.Logic;
|
||||
using StgSharp.Math;
|
||||
|
||||
using System;
|
||||
|
||||
@@ -42,7 +42,7 @@ namespace StgSharp.Graphics
|
||||
public class ViewPort
|
||||
{
|
||||
private int _width, _height;
|
||||
|
||||
|
||||
internal ViewPort()
|
||||
{
|
||||
_height = 800;
|
||||
@@ -118,5 +118,6 @@ namespace StgSharp.Graphics
|
||||
InternalIO.glfwGetWindowSize(ViewPortID, wptr, hptr);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,33 +45,60 @@ using System.Xml.Serialization;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
/// <summary>
|
||||
/// A sets of complex logic designed for running in multi threads environment.
|
||||
/// Warning: do not run a <see cref="Blueprint"/> instance in multi threads more than once.
|
||||
///
|
||||
/// </summary>
|
||||
public partial class Blueprint : IConvertableToBlueprintNode
|
||||
{
|
||||
private int runningFlag;
|
||||
|
||||
private BeginningNode beginNode;
|
||||
private Dictionary<string, BlueprintNode> allNode;
|
||||
private EndingNode endNode;
|
||||
|
||||
private Func<bool> terminate;
|
||||
|
||||
private int currentNodeIndex;
|
||||
private List<Task> allThread;
|
||||
internal int currentGlobalNodeIndex;
|
||||
internal int currentNativeNodeIndex;
|
||||
|
||||
private List<BlueprintNode> nativeNodeList;
|
||||
private List<BlueprintNode> nodeList;
|
||||
private List<BlueprintNode> globalNodeList;
|
||||
|
||||
public Blueprint()
|
||||
{
|
||||
allNode = new Dictionary<string, BlueprintNode>();
|
||||
allThread = new List<Task>();
|
||||
nodeList = new List<BlueprintNode>();
|
||||
globalNodeList = new List<BlueprintNode>();
|
||||
nativeNodeList = new List<BlueprintNode>();
|
||||
terminate = () => true;
|
||||
beginNode = new BeginningNode();
|
||||
endNode = new EndingNode();
|
||||
//terminate = () => true;
|
||||
beginNode = new BeginningNode(this);
|
||||
endNode = new EndingNode(this);
|
||||
}
|
||||
|
||||
public BlueprintNode BeginLayer => beginNode;
|
||||
public BlueprintNode EndLayer => endNode;
|
||||
|
||||
public bool IsRunning => allThread.Any(t => t.Status == TaskStatus.Running);
|
||||
public bool IsRunning
|
||||
{
|
||||
get => runningFlag == 1;
|
||||
internal set => Interlocked.Exchange(ref runningFlag ,(value ? 1 : 0));
|
||||
}
|
||||
|
||||
/**/
|
||||
internal Func<bool> ShouldClose
|
||||
{
|
||||
get => terminate;
|
||||
set => terminate = value;
|
||||
}
|
||||
/**/
|
||||
|
||||
public void AddNode(BlueprintNode node, bool isNative)
|
||||
{
|
||||
_ = node ?? throw new ArgumentNullException(nameof(node));
|
||||
node.IsNative = isNative;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public (string name, BlueprintPipelineArgs arg)[] GetOutput()
|
||||
@@ -79,115 +106,27 @@ namespace StgSharp.Logic
|
||||
return endNode.OutputData;
|
||||
}
|
||||
|
||||
public void Init(int threadCount)
|
||||
public void Init()
|
||||
{
|
||||
if (threadCount > nodeList.Count)
|
||||
foreach (KeyValuePair<string, BlueprintNode> item in allNode)
|
||||
{
|
||||
InternalIO.InternalWriteLog(
|
||||
"Thread setting error: amount of threads of this blueprint is larger than amount of nodes.", LogType.Warning);
|
||||
threadCount = nodeList.Count;
|
||||
}
|
||||
if (threadCount!=0)
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
if (item.Value.IsNative)
|
||||
{
|
||||
if (item.Value.IsNative)
|
||||
{
|
||||
nativeNodeList.Add(item.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
}
|
||||
nativeNodeList.Add(item.Value);
|
||||
}
|
||||
nativeNodeList.Sort();
|
||||
PrepareTasks(threadCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var item in allNode)
|
||||
else
|
||||
{
|
||||
nodeList.Add(item.Value);
|
||||
globalNodeList.Add(item.Value);
|
||||
}
|
||||
}
|
||||
nodeList.Sort();
|
||||
nativeNodeList.Sort();
|
||||
globalNodeList.Sort();
|
||||
}
|
||||
|
||||
public void PrepareTasks(int count)
|
||||
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
allThread.Add(new Task(IndividualTask));
|
||||
}
|
||||
}
|
||||
|
||||
private void IndividualTask()
|
||||
{
|
||||
do
|
||||
{
|
||||
BlueprintNode node;
|
||||
while (RequestNextNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
} while (!terminate());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run all nodes repeatedly until it is instructed to terminate.
|
||||
/// </summary>
|
||||
/// <param name="terminateInstructor">Instructor to terminate all tasks. Returns <see langword="true"/> if tasks should terminate</param>
|
||||
public void RunRepeatedly(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
MultiThreadRun(terminateInstructor);
|
||||
return;
|
||||
}
|
||||
SingleThreadRun(terminateInstructor);
|
||||
}
|
||||
|
||||
private void MultiThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
if (terminate != terminateInstructor)
|
||||
{
|
||||
terminate = terminateInstructor;
|
||||
if (allThread.Count != 0)
|
||||
{
|
||||
foreach (var t in allThread)
|
||||
{
|
||||
t.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
private void SingleThreadRun(Func<bool> terminateInstructor)
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var item in nodeList)
|
||||
{
|
||||
item.Run();
|
||||
}
|
||||
EndLayer.Run();
|
||||
}
|
||||
|
||||
public void AddNativeNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = true;
|
||||
allNode.Add(node.Name, node);
|
||||
}
|
||||
|
||||
public void AddAsyncNode(BlueprintNode node)
|
||||
{
|
||||
node.IsNative = false;
|
||||
allNode.Add(node.Name, node);
|
||||
BlueprintRunner.Run(this, () => true);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -196,59 +135,47 @@ namespace StgSharp.Logic
|
||||
beginNode.InputData = parameters;
|
||||
}
|
||||
|
||||
private bool RequestNextNode(out BlueprintNode node)
|
||||
internal bool RequestNexGlobalNode(out BlueprintNode node)
|
||||
{
|
||||
if (currentNodeIndex < nodeList.Count)
|
||||
if (currentGlobalNodeIndex < globalNodeList.Count)
|
||||
{
|
||||
BlueprintNode ret = nodeList[currentNodeIndex];
|
||||
Interlocked.Increment(ref currentNodeIndex);
|
||||
node = ret;
|
||||
node = globalNodeList[currentGlobalNodeIndex];
|
||||
Interlocked.Increment(ref currentGlobalNodeIndex);
|
||||
return true;
|
||||
}
|
||||
node = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
internal bool RequestNextNativeNode(out BlueprintNode node)
|
||||
{
|
||||
|
||||
if (currentNativeNodeIndex < nativeNodeList.Count)
|
||||
{
|
||||
node = nativeNodeList[currentNativeNodeIndex];
|
||||
Interlocked.Increment(ref currentNativeNodeIndex);
|
||||
return true;
|
||||
}
|
||||
Interlocked.Exchange(ref currentNodeIndex, 0);
|
||||
node = default!;
|
||||
return false;
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.InputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
BlueprintNodeAction IConvertableToBlueprintNode.MainExecution
|
||||
{
|
||||
get
|
||||
{
|
||||
return (in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output)
|
||||
=>
|
||||
{
|
||||
beginNode.Run();
|
||||
foreach (var node in nativeNodeList)
|
||||
{
|
||||
node.Run();
|
||||
}
|
||||
endNode.Run();
|
||||
};
|
||||
}
|
||||
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
|
||||
}
|
||||
|
||||
string[] IConvertableToBlueprintNode.OutputInterfacesName
|
||||
{
|
||||
get
|
||||
{
|
||||
return beginNode.OutputInterfaces.Select(
|
||||
p => p.Key).ToArray();
|
||||
}
|
||||
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
|
||||
}
|
||||
}
|
||||
|
||||
internal class NodeClearException : Exception
|
||||
{
|
||||
public void Terminate()
|
||||
{
|
||||
Interlocked.Exchange(ref currentGlobalNodeIndex, int.MaxValue);
|
||||
Interlocked.Exchange(ref currentNativeNodeIndex, int.MaxValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,6 +35,7 @@ using StgSharp.Entities;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -42,11 +43,6 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public delegate void BlueprintNodeAction(
|
||||
in Dictionary<string, BlueprintPipeline> inpput,
|
||||
in Dictionary<string, BlueprintPipeline> output);
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
@@ -59,86 +55,69 @@ namespace StgSharp.Logic
|
||||
public class BlueprintNode : IComparable<BlueprintNode>
|
||||
#pragma warning restore CA1036
|
||||
{
|
||||
private BlueprintNodeAction mainOperation;
|
||||
|
||||
private protected bool levelAvailable, nativeRequested;
|
||||
private protected int _level;
|
||||
private string name;
|
||||
|
||||
private protected Dictionary<string, BlueprintPipeline> _inputInterfaces;
|
||||
private protected Dictionary<string, BlueprintPipeline> _outputInterfaces;
|
||||
|
||||
private protected int _level;
|
||||
private IConvertableToBlueprintNode mainOperation;
|
||||
private string name;
|
||||
|
||||
|
||||
internal BlueprintNode(string name,
|
||||
BlueprintNodeAction operation, bool isNative,
|
||||
string[] inputPorts,
|
||||
string[] outputPorts)
|
||||
IConvertableToBlueprintNode node, bool isNative)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
nativeRequested = isNative;
|
||||
mainOperation = operation;
|
||||
mainOperation = node;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length == 0 )
|
||||
foreach (string portName in node.InputInterfacesName)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(inputPorts));
|
||||
_inputInterfaces.Add(portName, null!);
|
||||
}
|
||||
if (outputPorts.Length == 0)
|
||||
|
||||
foreach (string portName in node.OutputInterfacesName)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(outputPorts));
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
{
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
_outputInterfaces.Add(portName, null);
|
||||
_outputInterfaces.Add(portName, null!);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public BlueprintNode(string name, IConvertableToBlueprintNode convertable)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
this.name = name;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
mainOperation = convertable.MainExecution;
|
||||
foreach (var soket in convertable.InputInterfacesName)
|
||||
{
|
||||
_inputInterfaces.Add(soket, null);
|
||||
}
|
||||
foreach (var soket in convertable.OutputInterfacesName)
|
||||
{
|
||||
_outputInterfaces.Add(soket, null);
|
||||
}
|
||||
}
|
||||
|
||||
public BlueprintNode(string name,
|
||||
public BlueprintNode(string name, bool isNative,
|
||||
string[] inputPorts,
|
||||
string[] outputPorts
|
||||
)
|
||||
{
|
||||
this.levelAvailable = false;
|
||||
if (inputPorts == null || inputPorts.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(inputPorts));
|
||||
}
|
||||
if (outputPorts == null || outputPorts.Length == 0)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(outputPorts));
|
||||
}
|
||||
levelAvailable = false;
|
||||
this.name = name;
|
||||
IsNative = isNative;
|
||||
_inputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
_outputInterfaces = new Dictionary<string, BlueprintPipeline>();
|
||||
if (inputPorts.Length * outputPorts.Length == 0)
|
||||
foreach (string portName in inputPorts)
|
||||
{
|
||||
throw new Exception();
|
||||
_inputInterfaces.Add(portName, null!);
|
||||
}
|
||||
foreach (var portName in inputPorts)
|
||||
foreach (string portName in outputPorts)
|
||||
{
|
||||
_inputInterfaces.Add(portName, null);
|
||||
}
|
||||
foreach (var portName in outputPorts)
|
||||
{
|
||||
_outputInterfaces.Add(portName, null);
|
||||
_outputInterfaces.Add(portName, null!);
|
||||
}
|
||||
}
|
||||
|
||||
internal IConvertableToBlueprintNode Value
|
||||
{
|
||||
get => mainOperation;
|
||||
}
|
||||
|
||||
public bool IsNative
|
||||
{
|
||||
get => nativeRequested;
|
||||
@@ -154,7 +133,7 @@ namespace StgSharp.Logic
|
||||
return _level;
|
||||
}
|
||||
_level = 0;
|
||||
foreach (var item in _inputInterfaces)
|
||||
foreach (KeyValuePair<string, BlueprintPipeline> item in _inputInterfaces)
|
||||
{
|
||||
if (item.Value == null)
|
||||
{
|
||||
@@ -213,21 +192,6 @@ namespace StgSharp.Logic
|
||||
SetCertainOutputPort(thisOutputPortName, pipeline);
|
||||
}
|
||||
|
||||
|
||||
public void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine(name);
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
mainOperation(in _inputInterfaces, in _outputInterfaces);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public int CompareTo(BlueprintNode other)
|
||||
{
|
||||
#pragma warning disable CA1062
|
||||
@@ -235,12 +199,30 @@ namespace StgSharp.Logic
|
||||
#pragma warning restore CA1062
|
||||
}
|
||||
|
||||
|
||||
public virtual void Run()
|
||||
{
|
||||
try
|
||||
{
|
||||
//Console.WriteLine(name);
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
if (mainOperation != null)
|
||||
{
|
||||
mainOperation.ExecuteMain(in _inputInterfaces, in _outputInterfaces);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
#region port operation
|
||||
|
||||
public BlueprintPipeline GetCertainInputPort(string name)
|
||||
{
|
||||
BlueprintPipeline ret;
|
||||
if (!_inputInterfaces.TryGetValue(name,out ret))
|
||||
if (!_inputInterfaces.TryGetValue(name, out ret))
|
||||
{
|
||||
throw new Exception($"Cannot find the interface named {name} in node {Name}.");
|
||||
}
|
||||
@@ -283,15 +265,29 @@ namespace StgSharp.Logic
|
||||
|
||||
public interface IConvertableToBlueprintNode
|
||||
{
|
||||
|
||||
#pragma warning disable CA1819
|
||||
public string[] InputInterfacesName { get; }
|
||||
public BlueprintNodeAction MainExecution { get; }
|
||||
public string[] OutputInterfacesName { get; }
|
||||
#pragma warning restore CA1819
|
||||
|
||||
|
||||
#pragma warning disable CA1819
|
||||
public string[] OutputInterfacesName { get; }
|
||||
#pragma warning restore CA1819
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void ExecuteMain(
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output);
|
||||
}
|
||||
|
||||
|
||||
public class BlueprintNodeRunException : Exception
|
||||
{
|
||||
|
||||
protected BlueprintNodeRunException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
public BlueprintNodeRunException()
|
||||
{
|
||||
}
|
||||
@@ -308,9 +304,6 @@ namespace StgSharp.Logic
|
||||
{
|
||||
}
|
||||
|
||||
protected BlueprintNodeRunException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public static class BlueprintRunner
|
||||
{
|
||||
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
|
||||
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
|
||||
private static ReaderWriterLockSlim bpLock = new ReaderWriterLockSlim();
|
||||
private static CancellationTokenSource cts = new CancellationTokenSource();
|
||||
|
||||
public static void Run(Blueprint blueprint, Func<bool> terminateInstructor)
|
||||
{
|
||||
//Console.WriteLine();
|
||||
if (blueprint == null) return;
|
||||
if (terminateInstructor == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(terminateInstructor));
|
||||
}
|
||||
if (blueprint.IsRunning)
|
||||
{
|
||||
throw new InvalidOperationException( $"Attempt to run a blueprint twice." );
|
||||
}
|
||||
blueprint.ShouldClose = terminateInstructor;
|
||||
if (IsInternalCalling)
|
||||
{
|
||||
if(InterruptAndRunNewBp(blueprint))
|
||||
do { } while(InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!bpLock.IsWriteLockHeld)
|
||||
{
|
||||
bpLock.EnterWriteLock();
|
||||
}
|
||||
if (InterruptAndRunNewBp(blueprint))
|
||||
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
|
||||
bpLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool IsInternalCalling
|
||||
{
|
||||
get
|
||||
{
|
||||
int? id = Task.CurrentId;
|
||||
if (id is null) return false;
|
||||
foreach (var item in allAvailableTask)
|
||||
{
|
||||
if (item.Id == id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
internal static bool InterruptAndRunNewBp(Blueprint newBp)
|
||||
{
|
||||
Debug.Assert(newBp != null);
|
||||
if (newBp != NodeRunner.CurrentBlueprint && NodeRunner.CurrentBlueprint != null)
|
||||
{
|
||||
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
(NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode)!.Run();
|
||||
}
|
||||
for (int i = 0; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
var item = allAvailableTask[i];
|
||||
if (item is null || item.IsCompleted)
|
||||
{
|
||||
allAvailableTask[i] = NodeRunner.Run();
|
||||
}
|
||||
}
|
||||
BlueprintNode node;
|
||||
while (!cts.Token.IsCancellationRequested
|
||||
&& NodeRunner.CurrentBlueprint.RequestNextNativeNode(out node))
|
||||
{
|
||||
node.Run();
|
||||
if (!NodeRunner.CurrentBlueprint.IsRunning)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
(NodeRunner.CurrentBlueprint.EndLayer as EndingNode)!.Run();
|
||||
return blueprintInWaiting.TryPeek(out NodeRunner.CurrentBlueprint);
|
||||
}
|
||||
|
||||
|
||||
public static void SetMaxTaskCount(int size)
|
||||
{
|
||||
if (size < 1)
|
||||
{
|
||||
size = 1;
|
||||
}
|
||||
if (allAvailableTask.Length >= size)
|
||||
{
|
||||
for (int i = size; i < allAvailableTask.Length; i++)
|
||||
{
|
||||
if (allAvailableTask[i] != null)
|
||||
{
|
||||
allAvailableTask[i].Cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Array.Resize(ref allAvailableTask, size);
|
||||
}
|
||||
|
||||
public static void Terminate()
|
||||
{
|
||||
cts.Cancel();
|
||||
NodeRunner.CurrentBlueprint.Terminate();
|
||||
foreach (var task in allAvailableTask)
|
||||
{
|
||||
task.Cts.Cancel();
|
||||
}
|
||||
foreach (var bp in blueprintInWaiting)
|
||||
{
|
||||
bp.Terminate();
|
||||
}
|
||||
blueprintInWaiting.Clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class NodeRunner: CancellableTask
|
||||
{
|
||||
private static Blueprint _current;
|
||||
|
||||
internal static ref Blueprint CurrentBlueprint
|
||||
{
|
||||
get => ref _current;
|
||||
}
|
||||
|
||||
internal NodeRunner() : base()
|
||||
{
|
||||
_startup = InternalRun;
|
||||
}
|
||||
|
||||
internal static NodeRunner Run()
|
||||
{
|
||||
var ret = new NodeRunner() { _startup = InternalRun};
|
||||
ret.taskInternal = Task.Run(ret.InternalAction);
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static void InternalRun(CancellationToken token)
|
||||
{
|
||||
//Console.WriteLine("Task Begin");
|
||||
BlueprintNode item;
|
||||
while (CurrentBlueprint != null && CurrentBlueprint.IsRunning && !token.IsCancellationRequested
|
||||
&& CurrentBlueprint.RequestNexGlobalNode(out item))
|
||||
{
|
||||
//Console.WriteLine($"Get node of {item.Name}, going to run.");
|
||||
item.Run();
|
||||
}
|
||||
//Console.WriteLine("Task Completed");
|
||||
}
|
||||
}//------------------------------------- End of Class ------------------------------------------
|
||||
}
|
||||
@@ -32,6 +32,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Security.Authentication.ExtendedProtection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -67,17 +68,18 @@ namespace StgSharp.Logic
|
||||
set => args = value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void CompleteAndSkip()
|
||||
{
|
||||
//Console.WriteLine($"Complete\t{former.ContextName}\t->\t{after.ContextName}");
|
||||
//Console.WriteLine($"Complete\t{former.Name}\t->\t{after.Name}");
|
||||
formerCompleteSemaphore.Release();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void CompleteAndWriteData(BlueprintPipelineArgs args)
|
||||
{
|
||||
this.args = args;
|
||||
//Console.WriteLine($"Complete\t{former.ContextName}\t->\t{after.ContextName}");
|
||||
formerCompleteSemaphore.Release();
|
||||
CompleteAndSkip();
|
||||
}
|
||||
|
||||
public static void SkipAll(Dictionary<string, BlueprintPipeline> ports)
|
||||
|
||||
@@ -41,8 +41,8 @@ namespace StgSharp.Logic
|
||||
{
|
||||
private protected Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)> outputDictionary;
|
||||
|
||||
public RouteSwitch(string name, string inputPorts) :
|
||||
base(name, [inputPorts], [string.Empty])
|
||||
public RouteSwitch(string name, bool isNative, string inputPorts) :
|
||||
base(name, isNative, [inputPorts], [string.Empty])
|
||||
{
|
||||
outputDictionary = new Dictionary<string, (BlueprintPipeline pipeline, Func<bool> openingRule)>();
|
||||
}
|
||||
|
||||
@@ -5,16 +5,20 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
internal sealed class BeginningNode : BlueprintNode
|
||||
{
|
||||
public BeginningNode() : base(name: "BeginningNode", null,
|
||||
true, ["default"], ["default"]
|
||||
private Blueprint _bp;
|
||||
|
||||
public BeginningNode( Blueprint bp ) : base(name: "BeginningNode",true, ["default"], ["default"]
|
||||
)
|
||||
{ }
|
||||
{
|
||||
_bp = bp;
|
||||
}
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] InputData
|
||||
{
|
||||
@@ -40,24 +44,22 @@ namespace StgSharp.Logic
|
||||
_outputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
public override void Run()
|
||||
{
|
||||
BlueprintPipeline.SkipAll(_outputInterfaces);
|
||||
_bp.IsRunning = true;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class EndingNode : BlueprintNode
|
||||
{
|
||||
public EndingNode() : base(name: "EndingNode",
|
||||
operation: (
|
||||
in Dictionary<string, BlueprintPipeline> input,
|
||||
in Dictionary<string, BlueprintPipeline> output
|
||||
) =>
|
||||
{
|
||||
},
|
||||
true, ["default"], ["default"]
|
||||
private Blueprint _bp;
|
||||
|
||||
public EndingNode(Blueprint bp) : base(name: "EndingNode",true, ["default"], ["default"]
|
||||
)
|
||||
{ }
|
||||
{
|
||||
_bp = bp;
|
||||
}
|
||||
|
||||
internal (string name, BlueprintPipelineArgs arg)[] OutputData
|
||||
{
|
||||
@@ -78,9 +80,12 @@ namespace StgSharp.Logic
|
||||
_inputInterfaces.Add(name, pipeline);
|
||||
}
|
||||
|
||||
public new void Run()
|
||||
public override void Run()
|
||||
{
|
||||
BlueprintPipeline.WaitAll(_inputInterfaces);
|
||||
Interlocked.Exchange(ref _bp.currentGlobalNodeIndex, 0);
|
||||
Interlocked.Exchange(ref _bp.currentNativeNodeIndex, 0);
|
||||
_bp.IsRunning = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
internal class CancellableTask
|
||||
{
|
||||
private CancellationTokenSource cts;
|
||||
private protected Task taskInternal;
|
||||
private protected Action<CancellationToken> _startup;
|
||||
|
||||
public CancellationTokenSource Cts
|
||||
{
|
||||
get => cts;
|
||||
}
|
||||
|
||||
public int Id
|
||||
{
|
||||
get =>taskInternal.Id;
|
||||
}
|
||||
|
||||
private protected CancellableTask()
|
||||
{
|
||||
cts = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
private protected CancellableTask(Action<CancellationToken> action)
|
||||
{
|
||||
taskInternal = new Task(InternalAction);
|
||||
cts = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public static CancellableTask Run(Action<CancellationToken> startup)
|
||||
{
|
||||
var ret = new CancellableTask() { _startup = startup };
|
||||
ret.taskInternal = Task.Run(ret.InternalAction);
|
||||
return ret;
|
||||
}
|
||||
|
||||
public bool IsCompleted
|
||||
{
|
||||
get => taskInternal.IsCompleted;
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
Cts.Cancel();
|
||||
}
|
||||
|
||||
~CancellableTask()
|
||||
{
|
||||
Cts.Dispose();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal void InternalAction()
|
||||
{
|
||||
//Console.WriteLine($"Task {Task.CurrentId} is running.");
|
||||
_startup(Cts.Token);
|
||||
}
|
||||
|
||||
public static void WaitAll(IEnumerable<CancellableTask> tasks)
|
||||
{
|
||||
IEnumerable<Task> convertedTasks = tasks.Select<CancellableTask,Task>
|
||||
(ct=>ct.taskInternal);
|
||||
Task.WaitAll(convertedTasks.ToArray());
|
||||
}
|
||||
|
||||
public static implicit operator Task(CancellableTask task)
|
||||
{
|
||||
return task.taskInternal;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
public class CrossFrameOperation : IConvertableToBlueprintNode
|
||||
{
|
||||
public Action MainExecution { get; private protected set; }
|
||||
|
||||
private readonly int cycleCount;
|
||||
private int currentCount;
|
||||
|
||||
public static string InputInterfacesName => "TaskIn";
|
||||
|
||||
public static string OutputInterfacesName => "TaskOut";
|
||||
|
||||
string[] IConvertableToBlueprintNode.InputInterfacesName => [InputInterfacesName];
|
||||
|
||||
string[] IConvertableToBlueprintNode.OutputInterfacesName => [OutputInterfacesName];
|
||||
|
||||
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output)
|
||||
{
|
||||
if (currentCount < cycleCount)
|
||||
{
|
||||
Interlocked.Increment(ref currentCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
Interlocked.Exchange(ref currentCount, 0);
|
||||
Task.Run(MainExecution);
|
||||
}
|
||||
//Console.WriteLine(currentCount);
|
||||
BlueprintPipeline.SkipAll(output);
|
||||
}
|
||||
|
||||
public CrossFrameOperation(Action startup, int cycleCount)
|
||||
{
|
||||
MainExecution = startup;
|
||||
this.cycleCount = cycleCount > 1 ? cycleCount : throw new ArgumentException(
|
||||
$"The {typeof(CrossFrameOperation).Name} crosses only one cycle, " +
|
||||
$"please use a {typeof(BlueprintNode).Name} instead.");
|
||||
this.cycleCount = cycleCount;
|
||||
currentCount = cycleCount;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Logic
|
||||
{
|
||||
internal class SizeFixedList<T> : IList<T>
|
||||
{
|
||||
private int capacity, _index;
|
||||
private T[] values;
|
||||
|
||||
public SizeFixedList(int size)
|
||||
{
|
||||
values = new T[size];
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => index<_index ? values[index] : throw new ArgumentOutOfRangeException(nameof(index));
|
||||
set
|
||||
{
|
||||
if (true)
|
||||
{
|
||||
values[index] = value ;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int Count => _index + 1;
|
||||
|
||||
public bool IsReadOnly => false;
|
||||
|
||||
public void Add(T item)
|
||||
{
|
||||
if (_index < capacity - 1)
|
||||
{
|
||||
_index++;
|
||||
this[_index] = item;
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
values = new T[capacity];
|
||||
_index = 0;
|
||||
}
|
||||
|
||||
public bool Contains(T item)
|
||||
{
|
||||
for (int i = 0; i <= _index; i++)
|
||||
{
|
||||
if (item.Equals(values[i]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i <= _index; i++)
|
||||
{
|
||||
yield return values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(T item)
|
||||
{
|
||||
for (int i = 0; i <= _index; i++)
|
||||
{
|
||||
if (values[i].Equals(item))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void Insert(int index, T item)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public bool Remove(T item)
|
||||
{
|
||||
|
||||
for (int i = 0; i <= _index; i++)
|
||||
{
|
||||
if (values[i].Equals(item))
|
||||
{
|
||||
values[i] = values[_index];
|
||||
_index--;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
if (index <= _index)
|
||||
{
|
||||
values[index] = values[_index];
|
||||
_index--;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
yield return GetEnumerator();
|
||||
}
|
||||
|
||||
public void Resize(int newSize)
|
||||
{
|
||||
Array.Resize(ref values, newSize);
|
||||
}
|
||||
|
||||
public void RemoveAll(Predicate<T> predicate)
|
||||
{
|
||||
for (int i = _index; i > 0; i--)
|
||||
{
|
||||
if (predicate(values[i]))
|
||||
{
|
||||
values[i] = values[_index];
|
||||
_index--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}//------------------------------------- End of Class ------------------------------------------
|
||||
}
|
||||
@@ -16,7 +16,10 @@ namespace StgSharp.Logic
|
||||
|
||||
private Blueprint renderLogic;
|
||||
|
||||
public Blueprint RenderLogic
|
||||
/// <summary>
|
||||
/// Render logic for every frame with no relation to <see cref="View"/>.
|
||||
/// </summary>
|
||||
public Blueprint GeneralRenderLogic
|
||||
{
|
||||
get => renderLogic;
|
||||
set => renderLogic = value;
|
||||
@@ -39,7 +42,7 @@ namespace StgSharp.Logic
|
||||
item.Value();
|
||||
}
|
||||
}
|
||||
renderLogic.RunRepeatedly(() => ShouldClose);
|
||||
BlueprintRunner.Run(renderLogic, ()=>ShouldClose);
|
||||
frameTimeProvider.WaitNextSpan();
|
||||
Interlocked.Add(ref _frameCount, 1);
|
||||
}
|
||||
|
||||
@@ -67,12 +67,7 @@ namespace StgSharp.Logic
|
||||
fpsCountProvider = new TimeSpanProvider(1.0, TimeSpanProvider.DefaultProvider);
|
||||
_frameSpeed = 0;
|
||||
_frameCount = 0;
|
||||
fpsCountProvider.SpanRefreshCallback =
|
||||
() =>
|
||||
{
|
||||
Interlocked.Exchange(ref _frameSpeed, _frameCount);
|
||||
Interlocked.Exchange(ref _frameCount, 0);
|
||||
};
|
||||
fpsCountProvider.SpanRefreshCallback = CountFps;
|
||||
long frameLength = (FrameSpeed > 0) ? ((1000L * 1000L) / FrameSpeed) : 0;
|
||||
frameTimeProvider = new TimeSpanProvider(
|
||||
frameLength, TimeSpanProvider.DefaultProvider
|
||||
@@ -91,16 +86,7 @@ namespace StgSharp.Logic
|
||||
fpsCountProvider = new TimeSpanProvider(1.0, TimeSpanProvider.DefaultProvider);
|
||||
_frameSpeed = 0;
|
||||
_frameCount = 0;
|
||||
fpsCountProvider.SpanRefreshCallback =
|
||||
() =>
|
||||
{
|
||||
/*
|
||||
Console.Clear();
|
||||
Console.WriteLine(FrameSpeed);
|
||||
/**/
|
||||
Interlocked.Exchange(ref _frameSpeed, _frameCount);
|
||||
Interlocked.Exchange(ref _frameCount, 0);
|
||||
};
|
||||
fpsCountProvider.SpanRefreshCallback = CountFps;
|
||||
long frameLength = (framespeed > 0) ? ((1000L * 1000L) / framespeed) : 0;
|
||||
frameTimeProvider = new TimeSpanProvider(
|
||||
frameLength, TimeSpanProvider.DefaultProvider
|
||||
@@ -112,6 +98,16 @@ namespace StgSharp.Logic
|
||||
_globalKeyAction = new Dictionary<KeyTrigger, Action>();
|
||||
}
|
||||
|
||||
private void CountFps()
|
||||
{
|
||||
/**/
|
||||
Console.Clear();
|
||||
Console.WriteLine(FrameSpeed);
|
||||
/**/
|
||||
Interlocked.Exchange(ref _frameSpeed, _frameCount);
|
||||
Interlocked.Exchange(ref _frameCount, 0);
|
||||
}
|
||||
|
||||
public int CurrentFrameCount => frameTimeProvider.CurrentSpan;
|
||||
|
||||
public int FrameSpeed => _frameSpeed;
|
||||
|
||||
@@ -14,7 +14,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+be6c2e6b92a328d0b68af669d50e92dd9bbff42b")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+7fb220c523c50504348a6b44e526999a45b4c242")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("StgSharp")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("StgSharp")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
Reference in New Issue
Block a user