daily uodate on 20240626 by nitload youmu

This commit is contained in:
Nitload
2024-06-26 07:33:12 +08:00
parent b2cb0cbddf
commit 8f9f1c4012
44 changed files with 722 additions and 383 deletions
+23 -19
View File
@@ -52,26 +52,26 @@ namespace StgSharp.Logic
/// </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 List<BlueprintNode> globalNodeList;
private List<BlueprintNode> nativeNodeList;
private SemaphoreSlim runningStat;
internal int currentGlobalNodeIndex;
internal int currentNativeNodeIndex;
private List<BlueprintNode> nativeNodeList;
private List<BlueprintNode> globalNodeList;
public Blueprint()
{
allNode = new Dictionary<string, BlueprintNode>();
globalNodeList = new List<BlueprintNode>();
nativeNodeList = new List<BlueprintNode>();
//terminate = () => true;
runningStat = new SemaphoreSlim(1, 1);
beginNode = new BeginningNode(this);
endNode = new EndingNode(this);
}
@@ -79,10 +79,14 @@ namespace StgSharp.Logic
public BlueprintNode BeginLayer => beginNode;
public BlueprintNode EndLayer => endNode;
internal SemaphoreSlim RunningStat
{
get => runningStat;
}
public bool IsRunning
{
get => runningFlag == 1;
internal set => Interlocked.Exchange(ref runningFlag ,(value ? 1 : 0));
get => runningStat.CurrentCount == 0;
}
/**/
@@ -100,6 +104,12 @@ namespace StgSharp.Logic
allNode.Add(node.Name, node);
}
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
BlueprintRunner.Run(this, () => true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (string name, BlueprintPipelineArgs arg)[] GetOutput()
{
@@ -123,18 +133,18 @@ namespace StgSharp.Logic
globalNodeList.Sort();
}
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input,
in Dictionary<string, BlueprintPipeline> output)
{
BlueprintRunner.Run(this, () => true);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetInput(params (string name, BlueprintPipelineArgs arg)[] parameters)
{
beginNode.InputData = parameters;
}
public void Terminate()
{
Interlocked.Exchange(ref currentGlobalNodeIndex, int.MaxValue);
Interlocked.Exchange(ref currentNativeNodeIndex, int.MaxValue);
}
internal bool RequestNexGlobalNode(out BlueprintNode node)
{
if (currentGlobalNodeIndex < globalNodeList.Count)
@@ -170,12 +180,6 @@ namespace StgSharp.Logic
get { return beginNode.OutputInterfaces.Select(p => p.Key).ToArray(); }
}
public void Terminate()
{
Interlocked.Exchange(ref currentGlobalNodeIndex, int.MaxValue);
Interlocked.Exchange(ref currentNativeNodeIndex, int.MaxValue);
}
}
}
@@ -0,0 +1,31 @@
using StgSharp.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharpDebug.Logic
{
public class BlueprintActionNode : IConvertableToBlueprintNode
{
private Action<Dictionary<string, BlueprintPipeline>, Dictionary<string, BlueprintPipeline>> mainAction;
private string[] inPorts;
private string[] outPorts;
internal BlueprintActionNode()
{
}
public string[] InputInterfacesName => inPorts;
public string[] OutputInterfacesName => outPorts;
public void ExecuteMain(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output)
{
mainAction?.Invoke(input, output);
}
}
}
@@ -35,6 +35,7 @@ using StgSharp.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;
@@ -57,14 +58,12 @@ namespace StgSharp.Logic
{
private protected bool levelAvailable, nativeRequested;
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,
IConvertableToBlueprintNode node, bool isNative)
{
@@ -85,10 +84,13 @@ namespace StgSharp.Logic
}
}
public BlueprintNode(string name, bool isNative,
#pragma warning disable CS8618
internal BlueprintNode(string name, bool isNative,
string[] inputPorts,
string[] outputPorts
)
#pragma warning restore CS8618
{
if (inputPorts == null || inputPorts.Length == 0)
{
@@ -260,34 +262,35 @@ namespace StgSharp.Logic
#endregion
public static IConvertableToBlueprintNode AsBlueprintNodeMainExecution(this BlueprintNodeMainExecution execution, string name)
{
if (execution == null)
{
throw new ArgumentNullException(nameof(execution));
}
var attributeArray = execution.Method.GetCustomAttributes(typeof(BpNodeActionAttribute), false);
var attri = (BpNodeActionAttribute)attributeArray[0];
if ( attributeArray.Length < 0 || attri == null)
{
throw new InvalidCastException($"Delegate {nameof(execution)} is not marked by {typeof(BpNodeActionAttribute)}");
}
if (attri.OperationSpan <= 1)
{
return new DefaultConvertableToBlueprintNode();
}
return new CrossFrameOperation(
);
}
}//--------------------------------- end of class -----------------------------------
public interface IConvertableToBlueprintNode
{
#pragma warning disable CA1819
public string[] InputInterfacesName { 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()
{
}
@@ -12,9 +12,10 @@ namespace StgSharp.Logic
{
public static class BlueprintRunner
{
private static int _callingThread;
private static ConcurrentStack<Blueprint> blueprintInWaiting = new ConcurrentStack<Blueprint>();
private static NodeRunner[] allAvailableTask = new NodeRunner[1];
private static ReaderWriterLockSlim bpLock = new ReaderWriterLockSlim();
private static SemaphoreSlim bpLock = new SemaphoreSlim(0,1);
private static CancellationTokenSource cts = new CancellationTokenSource();
public static void Run(Blueprint blueprint, Func<bool> terminateInstructor)
@@ -27,36 +28,48 @@ namespace StgSharp.Logic
}
if (blueprint.IsRunning)
{
throw new InvalidOperationException( $"Attempt to run a blueprint twice." );
throw new InvalidOperationException("Attempt to run a blueprint instance twice.");
}
blueprint.ShouldClose = terminateInstructor;
if (IsInternalCalling)
if (IsValidCalling)
{
if(InterruptAndRunNewBp(blueprint))
//Console.WriteLine("valid and run");
if (InterruptAndRunNewBp(blueprint))
do { } while(InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
}
else
{
if (!bpLock.IsWriteLockHeld)
{
bpLock.EnterWriteLock();
}
bpLock.Wait();
if (InterruptAndRunNewBp(blueprint))
do { } while (InterruptAndRunNewBp(NodeRunner.CurrentBlueprint));
bpLock.ExitWriteLock();
bpLock.Release();
}
Interlocked.Exchange(ref _callingThread, -1);
}
internal static bool IsInternalCalling
internal static bool IsValidCalling
{
get
{
if (NodeRunner.CurrentBlueprint == null || //no bp running at all
(blueprintInWaiting.Count == 0 && NodeRunner.CurrentBlueprint.IsRunning == false)) // a bp is quiting
{
//in case no bp is running
return true;
}
int? id = Task.CurrentId;
if (id is null) return false;
if (id is null)
{
// in case calling from main thread and current running bp
//Console.WriteLine($"\t{_callingThread}");
//Console.WriteLine($"\t{Thread.CurrentThread.ManagedThreadId}");
return _callingThread == Thread.CurrentThread.ManagedThreadId;
}
foreach (var item in allAvailableTask)
{
if (item.Id == id)
{
//if calling from any bp running task
return true;
}
}
@@ -69,13 +82,18 @@ namespace StgSharp.Logic
Debug.Assert(newBp != null);
if (newBp != NodeRunner.CurrentBlueprint && NodeRunner.CurrentBlueprint != null)
{
//if a running bp exists
blueprintInWaiting.Push(NodeRunner.CurrentBlueprint);
}
//load new bp to current bp
Interlocked.Exchange(ref NodeRunner.CurrentBlueprint!, newBp);
Interlocked.Exchange(ref _callingThread, Thread.CurrentThread.ManagedThreadId);
//run beginning node to init it first.
if (!NodeRunner.CurrentBlueprint.IsRunning)
{
(NodeRunner.CurrentBlueprint.BeginLayer as BeginningNode)!.Run();
}
//refresh all tasks
for (int i = 0; i < allAvailableTask.Length; i++)
{
var item = allAvailableTask[i];
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace StgSharp.Logic
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class BpNodeActionAttribute : Attribute
{
private string[] _input;
private string[] _output;
private int _operationSpan;
internal string[] InputPort => _input;
internal string[] OutputPort => _output;
internal int OperationSpan => _operationSpan;
public BpNodeActionAttribute(string[] inputPort, string[] outputPort, int operationSpan)
{
_input = inputPort;
_output = outputPort;
this._operationSpan = operationSpan;
}
public BpNodeActionAttribute(string[] inputPort, string[] outputPort)
{
_input = inputPort;
_output = outputPort;
this._operationSpan = 1;
}
public BpNodeActionAttribute()
{
_input = Array.Empty<string>();
_output = Array.Empty<string>();
_operationSpan = 1;
}
public BpNodeActionAttribute(int operationSpan)
{
_input = Array.Empty<string>();
_output = Array.Empty<string>();
_operationSpan = operationSpan;
}
}
}
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace StgSharp.Logic
{
public interface IConvertableToBlueprintNode
{
#pragma warning disable CA1819
public string[] InputInterfacesName { 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);
}
internal class DefaultConvertableToBlueprintNode : IConvertableToBlueprintNode
{
private string[] _input;
private string[] _output;
private BlueprintNodeMainExecution _execute;
string[] IConvertableToBlueprintNode.InputInterfacesName => _input;
string[] IConvertableToBlueprintNode.OutputInterfacesName => _output;
void IConvertableToBlueprintNode.ExecuteMain(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output)
{
_execute(in input, in output);
}
}
public delegate void BlueprintNodeMainExecution(in Dictionary<string, BlueprintPipeline> input, in Dictionary<string, BlueprintPipeline> output);
}
+10 -1
View File
@@ -43,7 +43,6 @@ namespace StgSharp.Logic
public class BlueprintPipeline
{
private BlueprintNode after;
private BlueprintNode former;
private BlueprintPipelineArgs args;
@@ -68,6 +67,8 @@ namespace StgSharp.Logic
set => args = value;
}
#region pipline op
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CompleteAndSkip()
{
@@ -82,6 +83,8 @@ namespace StgSharp.Logic
CompleteAndSkip();
}
#endregion
public static void SkipAll(Dictionary<string, BlueprintPipeline> ports)
{
if (ports == null)
@@ -118,6 +121,12 @@ namespace StgSharp.Logic
formerCompleteSemaphore.Wait();
}
~BlueprintPipeline()
{
formerCompleteSemaphore.Dispose();
}
}
public abstract class BlueprintPipelineArgs
@@ -46,8 +46,8 @@ namespace StgSharp.Logic
public override void Run()
{
_bp.RunningStat.Wait();
BlueprintPipeline.SkipAll(_outputInterfaces);
_bp.IsRunning = true;
}
}
@@ -85,7 +85,7 @@ namespace StgSharp.Logic
BlueprintPipeline.WaitAll(_inputInterfaces);
Interlocked.Exchange(ref _bp.currentGlobalNodeIndex, 0);
Interlocked.Exchange(ref _bp.currentNativeNodeIndex, 0);
_bp.IsRunning = false;
_bp.RunningStat.Release();
}
}
}
@@ -43,11 +43,32 @@ namespace StgSharp.Logic
}
}
BlueprintRunner.Run(renderLogic, ()=>ShouldClose);
RenderStream.PollEvents();
frameTimeProvider.WaitNextSpan();
Interlocked.Add(ref _frameCount, 1);
}
}
private unsafe void PrivateShow()
{
if(0 == InternalIO.glfwWindowShouldClose(this.viewPortDisplay.ViewPortID))
{
frameTimeProvider.WaitNextSpan();
Interlocked.Add(ref _frameCount, 1);
viewPortDisplay.FlushSize();
foreach (var item in this._globalKeyAction)
{
if (InternalIO.glfwGetKey(this.viewPortDisplay.ViewPortID, (int)item.Key.TargetKeyOrButtonID)
== item.Key.TriggeredStatus)
{
item.Value();
}
}
BlueprintRunner.Run(renderLogic, () => ShouldClose);
RenderStream.PollEvents();
}
}
public T BuildNewRenderStream<T>(View arg, (int x, int y, int z) unitCubeSize) where T : RenderStream
{
if (arg.ContextName == this.Name)
@@ -30,6 +30,7 @@
//-----------------------------------------------------------------------
using StgSharp.Controlling.UsrActivity;
using StgSharp.Graphics;
using StgSharp.Graphics.GL;
using StgSharp.Math;
using System;
@@ -96,6 +97,18 @@ namespace StgSharp.Logic
{
};
_globalKeyAction = new Dictionary<KeyTrigger, Action>();
viewPortDisplay.FrameSizeCallback = (handle, width, height)
=>
{
InternalIO.glfwMakeContextCurrent(viewPortDisplay.ViewPortID);
glFunc.Viewport(0, 0, width, height);
PrivateShow();
};
viewPortDisplay.FramePostionCallback = (handle, xpos, ypos)
=>
{
PrivateShow();
};
}
private void CountFps()