daily update on 20240326 by nitload youmu, removing old MT

This commit is contained in:
YoumuLiu
2024-03-27 02:27:00 +08:00
parent f0d91b6d51
commit ea2a4a7dcb
59 changed files with 623 additions and 295 deletions
+70 -118
View File
@@ -34,6 +34,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
@@ -47,10 +48,41 @@ namespace StgSharp.Graphics
public abstract unsafe partial class Form
{
private int _frameCount;
private int _frameSpeed;
private short fpsCount;
#region form thread setting
private List<FrameAsyncThread> frameAsyncTaskList;
#endregion form thread setting
private IntPtr windowID;
internal LinkedList<Control> allControl;
internal Vector4 backgroundColor;
internal TimeSpanProvider fpsCountProvider;
internal TimeSpanProvider frameTimeProvider;
internal IglFunc gl;
internal IntPtr graphicContextID;
internal int height;
internal IntPtr monitor;
internal string name;
internal Form sharedForm;
internal int width;
protected Form()
{
frameAsyncTaskList = new List<FrameAsyncThread>();
fpsCountProvider = new TimeSpanProvider(1.0, TimeSpanProvider.DefaultProvider);
}
public int CurrentFrameCount => frameTimeProvider.CurrentSpan;
public int FrameSpeed => _frameSpeed;
/// <summary>
/// OpenGL package binded to current form context.
/// A OpenGL context is only binded to current form.
@@ -66,15 +98,9 @@ namespace StgSharp.Graphics
set => height = value;
}
/// <summary>
/// Representing
/// </summary>
public bool IsDoubleRendering => isSingleThread;
public bool ShouldClose
{
get => InternalIO.glfwWindowShouldClose(windowID) != 0;
set => InternalIO.glfwSetWindowShouldClose(windowID, value ? 1 : 0);
}
@@ -102,6 +128,8 @@ namespace StgSharp.Graphics
public unsafe IntPtr WindowID => windowID;
internal unsafe IntPtr id => windowID;
protected FrameBufferSizeHandler FrameBufferSizeCallback
{
set
@@ -111,24 +139,16 @@ namespace StgSharp.Graphics
}
}
internal unsafe IntPtr id => windowID;
internal bool IsSingleThread => isSingleThread;
public abstract void Deinit(params object[] args);
public abstract void Deinit();
/// <summary>
/// Necessary operation to init this form.
/// </summary>
/// <param name="args"></param>
public abstract void Init(params object[] args);
public abstract void Init();
/// <summary>
/// Behaviour of current form when in a frame cycle.
/// </summary>
/// <param name="args"></param>
public abstract void Main();
/// <summary>
@@ -244,33 +264,6 @@ namespace StgSharp.Graphics
public void SetSizeLimit(int minWidth, int minHeight, int maxWidth, int maxHeight)
{ InternalIO.glfwSetWindowSizeLimits(this.windowID, minWidth, minHeight, maxWidth, maxHeight); }
#region metadata
internal LinkedList<Control> allControl;
internal Vector4 backgroundColor;
internal IglFunc gl;
internal IntPtr graphicContextID;
internal int height;
internal IntPtr monitor;
internal string name;
internal Form sharedForm;
internal int width;
private IntPtr windowID;
#endregion metadata
#region form time
private short fpsCount;
internal TimeSpanProvider frameTimeProvider;
internal TimeSpanProvider fpsCountProvider = new TimeSpanProvider(1.0, TimeSpanProvider.DefaultProvider);
#endregion
#region form thread setting
private bool isSingleThread = true;
private List<FrameAsyncThread> frameAsyncTaskList = new List<FrameAsyncThread>();
#endregion form thread setting
#region form main
@@ -287,58 +280,38 @@ namespace StgSharp.Graphics
else
{
frameTimeProvider = new TimeSpanProvider(1 / 60.0, TimeSpanProvider.DefaultProvider);
if (isSingleThread)
{
while (!ShouldClose)
MethodInfo[] methods = this.GetType().GetMethods(
BindingFlags.Public |
BindingFlags.Instance |
BindingFlags.DeclaredOnly);
if (methods.Length != 0)
foreach (MethodInfo m in methods)
{
Main();
SwapBuffers();
PollEvents();
frameTimeProvider.WaitNextSpan();
Console.WriteLine(frameTimeProvider.CurrentSpan);
if (m.GetCustomAttribute<RenderActivityAttribute>(false) != null)
{
if (!m.IsStatic && m.GetParameters().Length == 0 && m.ReturnType == typeof(void) )
{
if (m.Name != "Main")
{
FrameAsyncThread t = new();
}
}
else
{
throw new NotSupportedException("A render activity cannot have parameters, returning value or be static.");
}
}
}
}
else
while (!ShouldClose)
{
GLonUpdate update = new GLonUpdate((GLcontext*)graphicContextID); //更换GL库至双线程模式
AsyncUpdateDataTask updateT = new AsyncUpdateDataTask(this); //建立两个异步线程
AsyncRenderFrameTask renderT = new AsyncRenderFrameTask(this);
frameAsyncTaskList.Add(renderT);
frameAsyncTaskList.Add(updateT);
gl = update;
update.ActivatedOperationBuffer = updateT.opBuffer;
Barrier frameBarrier = FrameAsyncThread.CombineBeginningAsync(true, renderT, updateT);
Barrier swapBufferBarrier = renderT.TodoCompleteBarrier;
Barrier exchangeBufferBarrier = updateT.TodoCompleteBarrier;
renderT.AsyncStart();
updateT.AsyncStart();
while (!ShouldClose)
{
swapBufferBarrier.SignalAndWait();
SwapBuffers(); //绘制帧必要操作
PollEvents();
exchangeBufferBarrier.SignalAndWait();
FrameAsyncThread.ExchangeBuffer(updateT, renderT); //交换两个线程的缓冲区
update.ActivatedOperationBuffer = updateT.opBuffer;
frameTimeProvider.WaitNextSpan(); //等待下一帧开始
frameBarrier.SignalAndWait();
Console.WriteLine(frameTimeProvider.CurrentSpan);
}
updateT.Terminate();
renderT.Terminate();
Main();
SwapBuffers();
PollEvents();
frameTimeProvider.WaitNextSpan();
}
}
}
public int CurrentFrameCount
=> frameTimeProvider.CurrentSpan;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void InternalDeinit(params object[] args) { Deinit(args); }
@@ -369,38 +342,17 @@ namespace StgSharp.Graphics
}
}
/// <summary>
/// Define how current form render in one frame.
/// </summary>
/// <param name="leastThreds">
/// Please define this befor calling any OpenGL api and other graphic methods.
/// </param>
protected unsafe void SetRenderThreads(int leastThreds)
{
if (gl != default)
{
throw new InvalidOperationException("Cannot define threads after loading gl context.");
}
if (leastThreds == 1)
{
isSingleThread = true;
}
else if (leastThreds >= 2)
{
isSingleThread = false;
}
else
{
throw new ArgumentException("Amount of threads should be bigger than 0");
}
}
unsafe ~Form() { Marshal.FreeHGlobal(graphicContextID); }
#endregion form main
}
/// <summary>
/// Mark that a method in a <see cref="Form"/>
/// should be called when rendering.
/// </summary>
public class RenderActivityAttribute : Attribute
{
}
}
@@ -250,7 +250,7 @@ namespace StgSharp.Graphics
{
m = new();
bool ret = paramQueue.TryDequeue(out m);
//Console.WriteLine(m.ToString(16));
Console.WriteLine(m.ToString(16));
return ret;
}
@@ -494,17 +494,16 @@ namespace StgSharp.Graphics
}
public class FrameAsyncThread
{
protected Barrier _drawCompleteBarrier;
private Thread _task;
private int _terminateToken;
protected Barrier _frameSpeedSyncBarrier;
protected Barrier _spanCompleteBarrier;
protected Action _taskTODO;
internal FrameOperationBuffer opBuffer;
@@ -513,7 +512,7 @@ namespace StgSharp.Graphics
_taskTODO = action;
opBuffer = new FrameOperationBuffer(gl);
_terminateToken = 1;
_drawCompleteBarrier = new Barrier(2);
_spanCompleteBarrier = new Barrier(2);
}
public Action TaskTODO
@@ -524,9 +523,7 @@ namespace StgSharp.Graphics
public Barrier TodoCompleteBarrier
{
get => _drawCompleteBarrier;
}
=> _spanCompleteBarrier;
public virtual void AsyncStart()
{
@@ -535,9 +532,9 @@ namespace StgSharp.Graphics
{
while (_terminateToken != 0)
{
_taskTODO();
_drawCompleteBarrier.SignalAndWait();
_frameSpeedSyncBarrier.SignalAndWait();
_taskTODO();
_spanCompleteBarrier.SignalAndWait();
}
});
_task.Start();
@@ -595,21 +592,4 @@ namespace StgSharp.Graphics
}
internal sealed class AsyncRenderFrameTask : FrameAsyncThread
{
public AsyncRenderFrameTask(Form f) : base(null, f.gl)
{
TaskTODO = FrameOperation;
}
private void FrameOperation()
{
while (opBuffer.CallNext()) { }
}
}
}
+10 -5
View File
@@ -43,7 +43,7 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal unsafe partial class GLonUpdate : IglFunc
internal sealed unsafe partial class GLonUpdate : IglFunc
{
private GLcontext* api;
@@ -122,7 +122,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
frameOpBuffer.ParamEnqueue((int)mask);
frameOpBuffer.ParamEnqueue((uint)mask);
frameOpBuffer.MethodEnqueue(ClearPtr);
}
@@ -350,9 +350,14 @@ namespace StgSharp.Graphics
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
T[] copy = new T[bufferArray.Length];
lock (bufferArray)
{
bufferArray.CopyTo(copy, 0);
}
frameOpBuffer.ParamEnqueue((uint)bufferType, (uint)usage);
frameOpBuffer.ParamEnqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length));
frameOpBuffer.ParamEnqueue<T>(bufferArray);
frameOpBuffer.ParamEnqueue<T>(copy);
frameOpBuffer.MethodEnqueue(BufferDataPtr);
}
@@ -365,10 +370,10 @@ namespace StgSharp.Graphics
}
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, glHandle pixels)
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalFormat, uint width, uint height, uint format, uint type, glHandle pixels)
{
frameOpBuffer.ParamEnqueue(pixels);
frameOpBuffer.ParamEnqueue((uint)level, internalformat, width, height);
frameOpBuffer.ParamEnqueue((uint)level, internalFormat, width, height);
frameOpBuffer.ParamEnqueue((uint)target, format, type);
frameOpBuffer.MethodEnqueue(textureImage2dPtr);
}
+4 -1
View File
@@ -69,7 +69,7 @@ namespace StgSharp.Math
public string ToString(int sample)
{
string str = string.Empty;
for (int i = 0; i < 16; i++)
for (int i = 15; i >= 0; i--)
{
str += $"{Convert.ToString(this.Read<byte>(i), sample)} ";
}
@@ -78,6 +78,9 @@ namespace StgSharp.Math
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Write<T>(int index, T value) where T : struct
#if NET8_0_OR_GREATER
, INumber
#endif
{
fixed (Vector4* p = &vec)
{
@@ -76,7 +76,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.200\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.202\\RuntimeIdentifierGraph.json"
}
}
}
@@ -7,7 +7,7 @@
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Nitlo\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.9.1</NuGetToolVersion>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.9.2</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Nitlo\.nuget\packages\" />
+1 -1
View File
@@ -478,7 +478,7 @@
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.200\\RuntimeIdentifierGraph.json"
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.202\\RuntimeIdentifierGraph.json"
}
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"version": 2,
"dgSpecHash": "xsb1MbFQpqBCYdQPA/4envmaHoUkPHoEi0xOw2a3gr5s56AndbAygoRuWnsVcCeV230ECqLFF5UDU8zLmgv6Iw==",
"dgSpecHash": "ZPDe1JI7Wx6qgAfoUxccO7bepdlHRo3d3YlIJ3OaR4SOQAO2IFjQgv3H5Y8S5d37x/fP3pn350jCofkiCwCIsw==",
"success": true,
"projectFilePath": "D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\源代码\\StgSharp\\StgSharp.csproj",
"expectedPackageFiles": [
@@ -44,7 +44,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+02be081eeec25a0e028a94751b7120f22bb80e3c")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f0d91b6d5195682edaab2a326e5daaba61f53271")]
[assembly: System.Reflection.AssemblyProductAttribute("StgSharp")]
[assembly: System.Reflection.AssemblyTitleAttribute("StgSharp")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
e975e1f3a179d311b4aad771af458c0cc2539109253a4aa2a194bbfa1c451ab7
8f8cd456f79aeac09162ed14b0ca15407c6bbf42d945e66dbac8506190809dde
@@ -1 +1 @@
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/02be081eeec25a0e028a94751b7120f22bb80e3c/*"}}
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/f0d91b6d5195682edaab2a326e5daaba61f53271/*"}}