daily update on 20240209 by nitload youmu

This commit is contained in:
Nitload
2024-02-09 23:31:33 +08:00
parent 6fe6b3b3cf
commit 8391569482
159 changed files with 948 additions and 2431 deletions
+100 -162
View File
@@ -16,10 +16,12 @@ namespace StgSharp.Graphics
internal unsafe partial class GLonUpdate : IglFunc
{
private ConcurrentQueue<IntPtr> glActionQueue;
private static ConcurrentQueue<IntPtr> glActionQueue;
private static GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
private IntPtr SwapBufferPtr = (IntPtr)(delegate*<IntPtr, void>)&InternalIO.glfwSwapBuffers;
internal GLonUpdate(GLcontext* context)
{
if (paramQueue == null)
@@ -29,6 +31,11 @@ namespace StgSharp.Graphics
glActionQueue = new ConcurrentQueue<IntPtr>();
}
internal static GLparamQueue ParamQueue
{
get { return paramQueue; }
}
internal ConcurrentQueue<IntPtr> GLactionQueue
{
get
@@ -36,13 +43,13 @@ namespace StgSharp.Graphics
return glActionQueue;
}
}
void IglFunc.ActiveTexture(uint type)
{
paramQueue.Enqueue(type);
glActionQueue.Enqueue(ActiveTexturePtr);
}
void IglFunc.AfterAlign(Form form, Action action)
{
throw new NotImplementedException();
@@ -77,9 +84,11 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, uint texture)
void IglFunc.BindTexture(uint type, glHandle texture)
{
paramQueue.Enqueue(type, texture);
M128 m = new M128 { IntPtr1 = (IntPtr)texture.valuePtr };
m.UInt32[0] = (uint)type;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindTexturePtr);
}
@@ -109,12 +118,6 @@ namespace StgSharp.Graphics
glActionQueue.Enqueue(ClearGraphicErrorPtr);
}
//TODO:双线程,换运行环境
void IglFunc.SwitchContext()
{
}
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode)
{
int l = shaderCode.Length;
@@ -133,7 +136,6 @@ namespace StgSharp.Graphics
return shaderHandle;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreateProgram()
{
@@ -161,12 +163,12 @@ namespace StgSharp.Graphics
IntPtr windowPtr;
if (share == null)
{
windowPtr = InternalIO.glfw.CreateWindow(
windowPtr = InternalIO.glfwCreateWindow(
width, height, tittleBytes, monitor, IntPtr.Zero);
}
else
{
windowPtr = InternalIO.glfw.CreateWindow(
windowPtr = InternalIO.glfwCreateWindow(
width, height, tittleBytes, monitor, share.windowID);
}
@@ -180,7 +182,6 @@ namespace StgSharp.Graphics
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreatGraphicProgram()
{
@@ -208,25 +209,18 @@ namespace StgSharp.Graphics
glActionQueue.Enqueue(DeleBufferPtr);
}
//TODO: 从这里开始
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
glActionQueue.Enqueue(
() =>
{
uint[] target = arrays;
fixed (uint* ptr = target)
{
api->glDeleteVertexArrays(count, ptr);
}
}
);
M128 m = new() { IntPtr1 = (IntPtr)arrays.handle };
m.Int32[0] = arrays.length;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleteVertexArrayPtr);
}
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
void IglFunc.DrawElements(GepmetryType mode, uint count, TypeCode type, IntPtr ptr)
{
uint a;
switch (Type.GetTypeCode(type))
switch (type)
{
case TypeCode.UInt32:
a = GLconst.UNSIGNED_INT;
@@ -239,73 +233,60 @@ namespace StgSharp.Graphics
break;
default:
InternalIO.InternalWriteLog(
$"Parameter error in parameter {nameof(type)} method DrawElements. " +
$"Only {typeof(uint).Name},{typeof(ushort).Name},{typeof(byte).Name} types are suporrted.",
$"Parameter error in parameter {type} method DrawElements. " +
$"Only {TypeCode.Byte}, {TypeCode.UInt16}, {TypeCode.UInt32} types are suporrted.",
LogType.Error);
return;
}
glActionQueue.Enqueue(
() =>
{ api->glDrawElements((uint)mode, count, a, (void*)ptr); }
);
paramQueue.Enqueue((uint)mode, count, a);
paramQueue.Enqueue(ptr);
glActionQueue.Enqueue(DrawElementsPtr);
}
unsafe void IglFunc.GenBuffers(uint count, glHandleSet handle)
unsafe glHandleSet IglFunc.GenBuffers(int count)
{
glActionQueue.Enqueue(
() =>
{
}
);
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenBufferPtr);
return h;
}
unsafe void IglFunc.GenTextures(uint count, glHandleSet handle)
unsafe glHandleSet IglFunc.GenTextures(int count)
{
glActionQueue.Enqueue(
() =>
{
}
);
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
}
unsafe void IglFunc.GenVertexArrays(uint count, glHandleSet ptr)
unsafe glHandleSet IglFunc.GenVertexArrays(int count)
{
glActionQueue.Enqueue(
() =>
{
api->glGenVertexArrays(count, ptr);
}
);
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
}
public void GetGraphicError()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe glSHandle IglFunc.GetUniformLocation(glHandle program, string ptr)
{
throw new NotImplementedException();
}
void IglFunc.GetGraphicError()
{
throw new NotImplementedException();
}
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
int ret = -1;
glActionQueue.Enqueue(
() =>
{
int location = api->glGetUniformLocation(program, ptr);
Interlocked.Add(ref ret, location + 1);
}
);
while (ret < 0) { }
return ret;
glSHandle h = glSHandle.Alloc(0);
M128 m;
m.IntPtr0 = program.Handle;
m.IntPtr1 = GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject();
paramQueue.Enqueue(m);
paramQueue.Enqueue((IntPtr)h.valuePtr);
glActionQueue.Enqueue(GetUniformLocationPtr);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
bool IglFunc.GetWindowShouldClose(Form form)
{
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
return InternalIO.glfwWindowShouldClose(form.windowID) == 0;
}
public void GLswapBuffers(Form form)
@@ -314,7 +295,7 @@ namespace StgSharp.Graphics
{
return;
}
InternalIO.glfw.SwapBuffers(form.windowID);
InternalIO.glfwSwapBuffers(form.windowID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -324,120 +305,77 @@ namespace StgSharp.Graphics
{
return;
}
InternalIO.glfw.MakeContextCurrent(form.windowID);
InternalIO.glfwMakeContextCurrent(form.windowID);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
glActionQueue.Enqueue(
() =>
{
T data = bufferData;
int size = sizeof(T);
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
&data,
(uint)usage);
}
);
paramQueue.Enqueue((uint)bufferType, (uint)usage);
paramQueue.Enqueue((IntPtr)sizeof(T), GCHandle.Alloc(bufferData).AddrOfPinnedObject());
glActionQueue.Enqueue(BufferDataPtr);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
glActionQueue.Enqueue(
() =>
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
fixed (void* bufferPtr = bufferArray)
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
bufferPtr,
(uint)usage);
}
);
paramQueue.Enqueue((uint)bufferType, (uint)usage);
paramQueue.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length), GCHandle.Alloc(bufferArray).AddrOfPinnedObject());
glActionQueue.Enqueue(BufferDataPtr);
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
void IglFunc.SetVertexAttribute(uint index, int size, TypeCode t, bool normalized, uint stride, int pointer)
{
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(handler);
InternalIO.glfw.SetFramebufferSizeCallback(id, handlerPtr);
uint type = InternalIO.GLtype[t];
paramQueue.Enqueue(index, type, stride);
paramQueue.Enqueue(size, normalized ? 1 : 0, pointer);
glActionQueue.Enqueue(SetVertexArrayPtr);
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
//TODO:双线程,换运行环境
void IglFunc.SwitchContext()
{
glActionQueue.Enqueue(
() =>
{
uint type = InternalIO.GLtype[t];
api->glVertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
api->glEnableVertexAttribArray(index);
}
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetWindowShouldClose(Form form, bool mark)
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, T[] pixels)
{
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
}
void IglFunc.SwapBuffers(Form form)
{
glActionQueue.Enqueue(
() =>
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfw.SwapBuffers(form.windowID);
}
);
IntPtr source = GCHandle.Alloc(pixels, GCHandleType.Pinned).AddrOfPinnedObject();
paramQueue.Enqueue(source);
paramQueue.Enqueue((uint)level, internalformat, width, height);
paramQueue.Enqueue((uint)target, format, type);
glActionQueue.Enqueue(textureImage2dPtr);
}
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
void IglFunc.TextureParameter(int target, uint pname, int param)
{
glActionQueue.Enqueue(
() =>
{
api->glTexImage2D(target, level, internalformat, width, height, 0, format, type, pixels);
}
);
M128 m = default;
m.Int32[0] = target;
m.UInt32[2] = pname;
m.Int32[3] = param;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(TextureParameterPtr);
}
void IglFunc.TextureParameter(uint target, uint pname, int param)
void IglFunc.UseProgram(glHandle program)
{
glActionQueue.Enqueue(
() =>
{
api->glTextureParameteri(target, pname, param);
}
);
}
void IglFunc.UseProgram(uint rogram)
{
glActionQueue.Enqueue(
() =>
{
api->glUseProgram(rogram);
}
);
paramQueue.Enqueue((IntPtr)program.valuePtr);
glActionQueue.Enqueue(UseProgramPtr);
}
void IglFunc.Viewport(int x, int y, uint width, uint height)
{
glActionQueue.Enqueue(
() =>
{
api->glViewport(x, y, width, height);
}
);
Vec4d v = new Vec4d(x, y, (int)width, (int)height);
paramQueue.Enqueue(v);
glActionQueue.Enqueue(ViewPortPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void frameworkHandleEnqueue(IntPtr methodHandle, params M128[] param)
{
glActionQueue.Enqueue(methodHandle);
foreach (M128 item in param)
{
paramQueue.Enqueue(item);
}
}
}
}