daily update on 20240312 by nitload youmu
This commit is contained in:
@@ -31,14 +31,13 @@ namespace StgSharp.Graphics
|
||||
internal Vector4 backgroundColor;
|
||||
internal unsafe GLcontext* contextID;
|
||||
internal DynamicArrayList<IntPtr> formBuffer;
|
||||
internal IglFunc gl;
|
||||
internal int height;
|
||||
internal IntPtr monitor;
|
||||
internal string name;
|
||||
internal Form sharedForm;
|
||||
internal int width;
|
||||
internal IntPtr windowID;
|
||||
private bool closed;
|
||||
internal IglFunc gl;
|
||||
|
||||
#endregion metadata
|
||||
|
||||
@@ -48,7 +47,6 @@ namespace StgSharp.Graphics
|
||||
private SemaphoreSlim _renderFinishSignal = new SemaphoreSlim(0, 1);
|
||||
private SemaphoreSlim _renderStartSignal = new SemaphoreSlim(1, 1);
|
||||
private bool isSingleThread = true;
|
||||
private Thread renderThread;
|
||||
|
||||
#endregion form thread setting
|
||||
|
||||
@@ -63,6 +61,11 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
}
|
||||
|
||||
unsafe ~Form()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)contextID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// OpenGL package used by current form context.
|
||||
/// </summary>
|
||||
@@ -89,11 +92,20 @@ namespace StgSharp.Graphics
|
||||
get { return isSingleThread; }
|
||||
}
|
||||
|
||||
private int _shouldClose = 0;
|
||||
|
||||
public bool ShouldClose
|
||||
{
|
||||
get
|
||||
{
|
||||
Interlocked.Exchange(ref _shouldClose, InternalIO.glfwWindowShouldClose(windowID));
|
||||
return 0 != _shouldClose;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
InternalIO.glfwSetWindowShouldClose(windowID, value ? 1 : 0);
|
||||
Interlocked.Exchange(ref _shouldClose, value ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,11 +148,6 @@ namespace StgSharp.Graphics
|
||||
set { _renderStartSignal = value; }
|
||||
}
|
||||
|
||||
internal Thread RenderThread
|
||||
{
|
||||
get { return renderThread; }
|
||||
}
|
||||
|
||||
protected FrameBufferSizeHandler FrameBufferSizeCallback
|
||||
{
|
||||
set
|
||||
@@ -150,11 +157,6 @@ namespace StgSharp.Graphics
|
||||
}
|
||||
}
|
||||
|
||||
unsafe ~Form()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)contextID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init an OpenGl context if current form has not been binded to any context.
|
||||
/// and set the context binded to current form as current OpenGL context.
|
||||
@@ -185,6 +187,12 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
windowID = windowPtr;
|
||||
}
|
||||
|
||||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
|
||||
contextID = (GLcontext*)ptr;
|
||||
*contextID = new GLcontext();
|
||||
contextID->glGetString = (delegate*<uint, IntPtr>)IntPtr.Zero;
|
||||
gl = new GLonRender(contextID);
|
||||
}
|
||||
InternalIO.glfwMakeContextCurrent(windowID);
|
||||
gl.SwitchContext();
|
||||
@@ -259,37 +267,12 @@ namespace StgSharp.Graphics
|
||||
(int)minSize.X, (int)minSize.Y, (int)maxSize.X, (int)maxSize.Y);
|
||||
}
|
||||
|
||||
public unsafe void SwapAndPollEvent()
|
||||
{
|
||||
if (windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isSingleThread)
|
||||
{
|
||||
InternalIO.glfwSwapBuffers(windowID);
|
||||
InternalIO.glfwPollEvents();
|
||||
}
|
||||
else
|
||||
{
|
||||
M128 m = default;
|
||||
m.Write<ulong>(0, (ulong)windowID);
|
||||
GLonUpdate glu = gl as GLonUpdate;
|
||||
glu.frameworkHandleEnqueue(
|
||||
(IntPtr)(delegate*<void>)&InternalSwapBuffer, m);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void InternalSwapBuffer()
|
||||
{
|
||||
IntPtr handle = default;
|
||||
GLonUpdate.ParamQueue.TryDequeue(out handle);
|
||||
InternalIO.glfwSwapBuffers(handle);
|
||||
InternalIO.glfwPollEvents();
|
||||
}
|
||||
|
||||
#region form main
|
||||
|
||||
private static unsafe delegate*<void> pollEventPtr = &InternalIO.glfwPollEvents;
|
||||
|
||||
private static unsafe delegate*<void> swapBufferPtr = &InternalSwapBuffers;
|
||||
|
||||
public abstract void Deinit(params object[] args);
|
||||
|
||||
/// <summary>
|
||||
@@ -305,10 +288,22 @@ namespace StgSharp.Graphics
|
||||
/// <param name="args"></param>
|
||||
public abstract void Main(params object[] args);
|
||||
|
||||
public unsafe void PollEvents()
|
||||
{
|
||||
if (isSingleThread)
|
||||
{
|
||||
InternalIO.glfwPollEvents();
|
||||
}
|
||||
else
|
||||
{
|
||||
gl.frameworkHandleEnqueue((IntPtr)pollEventPtr);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init this form and render it on screen, and respond with operation
|
||||
/// </summary>
|
||||
public void Show(params object[] args)
|
||||
public unsafe void Show(params object[] args)
|
||||
{
|
||||
if (gl == null)
|
||||
{
|
||||
@@ -316,21 +311,53 @@ namespace StgSharp.Graphics
|
||||
}
|
||||
else if (isSingleThread)
|
||||
{
|
||||
while (gl.GetWindowShouldClose(this))
|
||||
while (!ShouldClose)
|
||||
{
|
||||
Main(args);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while (gl.GetWindowShouldClose(this))
|
||||
gl = new GLonUpdate(contextID);
|
||||
Thread renderThread = new Thread(new ThreadStart(InternalRender));
|
||||
renderThread.Start();
|
||||
int a = 0;
|
||||
while (!ShouldClose)
|
||||
{
|
||||
Main(args);
|
||||
if (gl.FrameCanUpdate)
|
||||
{
|
||||
Main(args);
|
||||
}
|
||||
}
|
||||
//TODO 对逻辑线程和渲染线程进行同步,两种gl模式包需要改名字
|
||||
renderThread.Join();
|
||||
//Thread.Sleep(1000 * 20);
|
||||
//ShouldClose = true;
|
||||
//(gl as GLonUpdate).GLactionQueue.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void SwapBuffers()
|
||||
{
|
||||
if (isSingleThread)
|
||||
{
|
||||
InternalIO.glfwSwapBuffers(windowID);
|
||||
}
|
||||
else
|
||||
{
|
||||
M128 m = new M128();
|
||||
m.Write<ulong>(0, (ulong)windowID);
|
||||
gl.frameworkHandleEnqueue((IntPtr)swapBufferPtr, m);
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe void InternalSwapBuffers()
|
||||
{
|
||||
IntPtr winHandle = default;
|
||||
GLonUpdate.ParamQueue.TryDequeue(out winHandle);
|
||||
InternalIO.glfwSwapBuffers(winHandle);
|
||||
}
|
||||
|
||||
internal void InternalDeinit(params object[] args)
|
||||
{
|
||||
Deinit(args);
|
||||
@@ -338,7 +365,7 @@ namespace StgSharp.Graphics
|
||||
|
||||
internal void InternalInit(params object[] args)
|
||||
{
|
||||
Init((object[])args);
|
||||
Init(args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -354,50 +381,38 @@ namespace StgSharp.Graphics
|
||||
else
|
||||
{
|
||||
InternalIO.InternalWriteLog("Attempt to get OpenGL API package in other graphic envitronment.", LogType.Error);
|
||||
throw new Exception();
|
||||
throw new InvalidOperationException("Attempt to get OpenGL API package in other graphic envitronment.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Define how current form render in one frame.
|
||||
/// </summary>
|
||||
/// <param name="requireDoubleRendering">
|
||||
/// <param name="leastThreds">
|
||||
/// Please define this befor calling any OpenGL api and other graphic methods.
|
||||
/// </param>
|
||||
protected unsafe void SetRenderThreads(int requiredThreds)
|
||||
protected unsafe void SetRenderThreads(int leastThreds)
|
||||
{
|
||||
if (gl != default)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot define threads after loading gl context.");
|
||||
}
|
||||
if (requiredThreds == 1)
|
||||
if (leastThreds == 1)
|
||||
{
|
||||
isSingleThread = true;
|
||||
}
|
||||
else if (requiredThreds == 2)
|
||||
else if (leastThreds >= 2)
|
||||
{
|
||||
isSingleThread = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception();
|
||||
throw new ArgumentException("Amount of threads should be bigger than 0");
|
||||
}
|
||||
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(GLcontext)));
|
||||
contextID = (GLcontext*)ptr;
|
||||
*contextID = new GLcontext();
|
||||
contextID->glGetString = (delegate*<uint, IntPtr>)IntPtr.Zero;
|
||||
InternalIO.InternalWriteLog("Successfully create a context binded to current form. " +
|
||||
$"Its ID is {(ulong)ptr}", LogType.Info);
|
||||
if (isSingleThread)
|
||||
{
|
||||
gl = new GLonRender(contextID);
|
||||
}
|
||||
else
|
||||
{
|
||||
GLonUpdate pack = new GLonUpdate(contextID);
|
||||
gl = pack;
|
||||
renderThread = new Thread(new ThreadStart(InternalRender));
|
||||
}
|
||||
}
|
||||
|
||||
private unsafe void InternalRender()
|
||||
@@ -406,25 +421,19 @@ namespace StgSharp.Graphics
|
||||
ConcurrentQueue<IntPtr> glActions = (gl as GLonUpdate).GLactionQueue;
|
||||
#pragma warning restore CS8602
|
||||
IntPtr currentOperation;
|
||||
TimeSpan span = new TimeSpan(100L);
|
||||
try
|
||||
{
|
||||
while (gl.GetWindowShouldClose(this))
|
||||
while (_shouldClose == 0)
|
||||
{
|
||||
if (glActions.TryDequeue(out currentOperation))
|
||||
{
|
||||
((delegate*<void>)currentOperation)();
|
||||
}
|
||||
else
|
||||
{
|
||||
Thread.Sleep(span);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
this.ShouldClose = true;
|
||||
(gl as GLonUpdate).ShowDequeueStream();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user