daily update on 20240213 by nitload youmu. 逐步弃用GLFW,正在使用wgl重写glfw底层,相关实现转移到C#实现,SSC彻底变成线性代数计算器。

This commit is contained in:
Nitload
2024-02-13 23:42:22 +08:00
parent 8391569482
commit a2241a5a58
307 changed files with 16701 additions and 10296 deletions
+3 -4
View File
@@ -11,7 +11,6 @@ using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Reflection.Emit;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Principal;
@@ -38,7 +37,7 @@ namespace StgSharp.Graphics
internal int width;
internal IntPtr windowID;
private bool closed;
private IglFunc gl;
internal IglFunc gl;
#endregion
#region form thread setting
@@ -180,7 +179,7 @@ namespace StgSharp.Graphics
windowID = windowPtr;
}
}
InternalIO.InternalSetCurrentGLContext(windowID, contextID);
gl.SwitchContext();
}
/// <summary>
@@ -267,7 +266,7 @@ namespace StgSharp.Graphics
else
{
M128 m = default;
m.IntPtr0 = windowID;
m.UInt64[0] = (ulong)windowID;
GLonUpdate glu = gl as GLonUpdate;
glu.frameworkHandleEnqueue(
(IntPtr)(delegate*<void>)&InternalSwapBuffer, m);
@@ -9,7 +9,7 @@ namespace StgSharp.Graphics
/// <summary>
/// Interface of all kinds OpenGL buffer onjects
/// </summary>
public abstract class IBufferObject : IDisposable
public abstract class BufferObjectBase : IDisposable
{
protected glHandleSet _bufferHandle;
protected IglFunc GL;
@@ -30,9 +30,15 @@ namespace StgSharp.Graphics
/// <param name="index">Index of handle of the object in this instance to be bind</param>
public abstract void Bind(int index);
/// <inheritdoc/>
public override string ToString()
{
return $"This is a {GetType().FullName}, containing {_bufferHandle.length} buffer handles: {_bufferHandle.ToString()}.";
}
protected abstract void Dispose(bool disposing);
~IBufferObject()
~BufferObjectBase()
{
Dispose(disposing: false);
}
+1 -5
View File
@@ -14,7 +14,7 @@ namespace StgSharp.Graphics
/// Each <see langword="int"/> value indexed from instance of this class this
/// is the only handle to one Element Buffer Object.
/// </summary>
public unsafe class ElementBuffer : IBufferObject
public unsafe class ElementBuffer : BufferObjectBase
{
internal ElementBuffer(int n, IglFunc gl)
{
@@ -22,10 +22,6 @@ namespace StgSharp.Graphics
_bufferHandle = GL.GenBuffers(n);
}
public glHandle this[int n]
{
get { return _bufferHandle[n]; }
}
public override void Bind(int index)
{
+1 -1
View File
@@ -6,7 +6,7 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexArray : IBufferObject
public unsafe class VertexArray : BufferObjectBase
{
internal VertexArray(int n, IglFunc gl)
{
+3 -1
View File
@@ -10,7 +10,7 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexBuffer : IBufferObject
public unsafe class VertexBuffer : BufferObjectBase
{
internal VertexBuffer(int n, IglFunc glFunc)
{
@@ -58,6 +58,8 @@ namespace StgSharp.Graphics
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, glHandle.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
@@ -41,12 +41,12 @@ namespace StgSharp.Graphics
/// <param name="loader"> </param>
public unsafe void LoadTexture(int index, string name, ImageLoader loader)
{
sourceList[index] = new Image();
fixed (Image.ImageInfo* textureInfo = &sourceList[index].info)
{
InternalIO.InternalLoadImage(name, textureInfo, loader);
}
Image.ImageInfo info = default;
InternalIO.InternalLoadImage(name, &info, loader);
Console.WriteLine(info);
sourceList[index] = new Image(info);
InternalIO.InternalUnloadImage(&info);
Console.WriteLine(sourceList[index]);
if (sourceList[index].info.pixelPtr != IntPtr.Zero)
{
uint channel;
+8 -2
View File
@@ -21,6 +21,11 @@ namespace StgSharp.Graphics
api = context;
}
GLcontext* IglFunc.Context
{
get => api;
}
void IglFunc.SwitchContext()
{
@@ -104,11 +109,11 @@ namespace StgSharp.Graphics
byte[] data = Encoding.UTF8.GetBytes(shaderCodeArray[i] + '\0');
fixed (byte* codePtr = data)
{
shaderID = InternalIO.InternalLoadShaderCode(codePtr, (int)shaderType);
shaderID = InternalIO.InternalLoadShaderCode(api, codePtr, (int)shaderType);
}
if (shaderID == 0) //compile error handler
{
IntPtr logPtr = InternalIO.InternalReadLog();
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
@@ -326,6 +331,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, T[] pixels)
{
Console.WriteLine(pixels);
IntPtr source = GCHandle.Alloc(pixels, GCHandleType.Pinned).AddrOfPinnedObject();
api->glTexImage2D(
(uint)target, level, internalformat, width, height, 0, format, type, (void*)source
@@ -17,6 +17,8 @@ namespace StgSharp.Graphics
FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
unsafe GLcontext* IglFunc.Context => throw signal;
private GLonTimeout() { }
void IglFunc.ActiveTexture(uint type) => throw signal;
+21 -11
View File
@@ -20,8 +20,15 @@ namespace StgSharp.Graphics
private static GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
private GLcontext* api;
private IntPtr SwapBufferPtr = (IntPtr)(delegate*<IntPtr, void>)&InternalIO.glfwSwapBuffers;
GLcontext* IglFunc.Context
{
get => api;
}
internal GLonUpdate(GLcontext* context)
{
if (paramQueue == null)
@@ -29,6 +36,7 @@ namespace StgSharp.Graphics
paramQueue = new GLparamQueue();
}
glActionQueue = new ConcurrentQueue<IntPtr>();
api = context;
}
internal static GLparamQueue ParamQueue
@@ -78,7 +86,7 @@ namespace StgSharp.Graphics
{
M128 m = default;
m.UInt32[0] = (uint)bufferType;
m.IntPtr1 = handle.Handle;
m.UInt64[1] = (ulong)handle.Handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindBufferPtr);
}
@@ -86,7 +94,8 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, glHandle texture)
{
M128 m = new M128 { IntPtr1 = (IntPtr)texture.valuePtr };
M128 m = default;
m.UInt64[1] = (ulong)texture.valuePtr;
m.UInt32[0] = (uint)type;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindTexturePtr);
@@ -150,7 +159,7 @@ namespace StgSharp.Graphics
{
M128 m = default;
glHandleSet h = new glHandleSet(count);
m.IntPtr0 = (IntPtr)h.handle;
m.UInt64[0] = (ulong)h.handle;
m.Int32[2] = count;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(CreateProgramSetPtr);
@@ -194,7 +203,8 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffer(glHandle handle)
{
M128 m = new M128() { IntPtr0 = handle.Handle };
M128 m = default;
m.UInt64[0] = (ulong)handle.Handle;
m.UInt32[2] = 1;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
@@ -204,14 +214,14 @@ namespace StgSharp.Graphics
{
M128 m = default;
m.UInt32[2] = (uint)handle.length;
m.IntPtr0 = (IntPtr)handle.handle;
m.UInt64[0] = (ulong)handle.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
}
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
M128 m = new() { IntPtr1 = (IntPtr)arrays.handle };
M128 m = default; m.UInt64[1] = (ulong)arrays.handle;
m.Int32[0] = arrays.length;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleteVertexArrayPtr);
@@ -246,7 +256,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenBufferPtr);
return h;
@@ -255,7 +265,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenTextures(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -264,7 +274,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.IntPtr0 = (IntPtr)h.handle;
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -275,8 +285,8 @@ namespace StgSharp.Graphics
{
glSHandle h = glSHandle.Alloc(0);
M128 m;
m.IntPtr0 = program.Handle;
m.IntPtr1 = GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject();
m.UInt64[0] = (ulong)program.Handle;
m.UInt64[1] = (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject();
paramQueue.Enqueue(m);
paramQueue.Enqueue((IntPtr)h.valuePtr);
glActionQueue.Enqueue(GetUniformLocationPtr);
@@ -57,14 +57,14 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glBindBuffer(m.UInt32[0], *(uint*)m.IntPtr1);
paramQueue.context->glBindBuffer(m.UInt32[0], *(uint*)m.UInt64[1]);
}
private static void InternalBindTexture()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glBindTexture(m.UInt32[0], *(uint*)m.IntPtr1);
paramQueue.context->glBindTexture(m.UInt32[0], *(uint*)m.UInt64[1]);
}
private static void InternalBindVertexArray()
@@ -101,10 +101,10 @@ namespace StgSharp.Graphics
for (int i = 0; i < count; i++)
{
paramQueue.TryDequeue(out shaderHandle, out codeHandle);
*(uint*)shaderHandle = InternalIO.InternalLoadShaderCode((byte*)codeHandle, type);
*(uint*)shaderHandle = InternalIO.InternalLoadShaderCode(paramQueue.context, (byte*)codeHandle, type);
if (*(uint*)shaderHandle == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
@@ -146,13 +146,13 @@ namespace StgSharp.Graphics
private static void InternalCreateProgramSet()
{
IntPtr programHandle;
uint* programHandle;
M128 m = default;
paramQueue.TryDequeue(out m);
programHandle = m.IntPtr0;
programHandle = (uint*)m.UInt64[0];
for (int i = 0; i < m.Int32[2]; i++)
{
*((uint*)programHandle + i) = paramQueue.context->glCreateProgram();
*(programHandle + i) = paramQueue.context->glCreateProgram();
}
}
@@ -160,14 +160,14 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteBuffers(m.UInt32[2], (uint*)m.IntPtr0);
paramQueue.context->glDeleteBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
}
private static void InternalDeleteVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteVertexArrays((uint)m.Int32[0], (uint*)m.IntPtr1);
paramQueue.context->glDeleteVertexArrays((uint)m.Int32[0], (uint*)m.UInt64[0]);
}
private static void InternalDrawElements()
@@ -183,21 +183,21 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenBuffers(m.UInt32[2], (uint*)m.IntPtr0);
paramQueue.context->glGenBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
}
private static void InternalGenTextures()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenTextures(m.UInt32[2], (uint*)m.IntPtr0);
paramQueue.context->glGenTextures(m.UInt32[2], (uint*)m.UInt64[0]);
}
private static void InternalGenVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenVertexArrays(m.UInt32[2], (uint*)m.IntPtr0);
paramQueue.context->glGenVertexArrays(m.UInt32[2], (uint*)m.UInt64[0]);
}
private static void InternalGetUniformLocation()
@@ -41,10 +41,10 @@ namespace StgSharp.Graphics
/// <summary>
/// Compile the vertex shader to be attached to this shader.
/// </summary>
/// <param name="shaderCode">GLSL code of vertex shader.</param>
public unsafe void Compile(ShaderType usage, params string[] code)
/// <param name="code">GLSL code of vertex shader.</param>
public unsafe void Compile(params string[] code)
{
glHandleSet.Copy(GL.CompileShader(usage, code), handle);
glHandleSet.Copy(GL.CompileShader(type, code), handle);
}
//TODO:写链接着色器的文档注释
@@ -59,6 +59,10 @@ namespace StgSharp.Graphics
GL.AttachShader(target.handle, handle[index]);
}
public override string ToString()
{
return $"This is a {type} shader, its shader handles are {handle.ToString()}.";
}
}
public class ShaderProgram
@@ -78,11 +82,11 @@ namespace StgSharp.Graphics
/// <summary>
/// Link the shader to current context.
/// </summary>
public void Link()
public unsafe void Link()
{
if (InternalIO.InternalLinkShaderProgram(handle.Value) == 0)
if (InternalIO.InternalLinkShaderProgram(GL.Context, handle.Value) == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
IntPtr logPtr = InternalIO.InternalReadSSCLog();
try
{
byte[] logByte = new byte[512];
@@ -143,6 +147,5 @@ namespace StgSharp.Graphics
{
GL.UseProgram(handle);
}
}
}
@@ -139,69 +139,111 @@ namespace StgSharp.Graphics
internal unsafe partial class GLonUpdate
{
private IntPtr UniformVal1fPtr = (IntPtr)(delegate*<void>)&UniformVal1f;
private IntPtr UniformVal1fvPtr = (IntPtr)(delegate*<void>)&UniformVal1fv;
private IntPtr UniformVal1iPtr = (IntPtr)(delegate*<void>)&UniformVal1i;
private IntPtr UniformVal2fPtr = (IntPtr)(delegate*<void>)&UniformVal2f;
private IntPtr UniformVal3fPtr = (IntPtr)(delegate*<void>)&UniformVal3f;
//TODO xontinue here
private static void UniformVal3f()
{
private IntPtr UniformVal4fPtr = (IntPtr)(delegate*<void>)&UniformVal4f;
paramQueue.
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float f0, float f1, float f2)
{
paramQueue.Enqueue((IntPtr)uniform.id.valuePtr);
paramQueue.Enqueue(f0, f1, f2);
glActionQueue.Enqueue(UniformVal3fPtr);
}
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float f0, float f1)
{
glActionQueue.Enqueue(
() => { api->glUniform3f(uniform.id, v0, v1, v2); }
);
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0; m.Single[3] = f1;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal2fPtr);
}
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float f0, float f1, float f2, float f3)
{
glActionQueue.Enqueue(
() => { api->glUniform2f(uniform.id, v0, v1); }
);
}
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
glActionQueue.Enqueue(
() => { api->glUniform4f(uniform.id, v0, v1, v2, v3); }
);
paramQueue.Enqueue((IntPtr)uniform.id.valuePtr);
paramQueue.Enqueue(f0, f1, f2, f3);
glActionQueue.Enqueue(UniformVal4fPtr);
}
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
{
glActionQueue.Enqueue(
() =>
{
Vec4d cache = vec;
api->glUniform1fv(uniform.id, 4, &cache);
}
);
Vec4d m = vec;
paramQueue.Enqueue((IntPtr)uniform.id.valuePtr, (IntPtr)(Vec4d*)&m);
glActionQueue.Enqueue(UniformVal1fvPtr);
}
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
unsafe void IglFunc.SetValue(Uniform<float> uniform, float f0)
{
glActionQueue.Enqueue(
() => { api->glUniform1f(uniform.id, v0); }
);
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
glActionQueue.Enqueue(
() => { api->glUniform1d(uniform.id, i0); }
);
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Int32[2] = i0;
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
unsafe void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
glActionQueue.Enqueue(
() =>
{
Vec4d cache = vec;
api->glUniform1fv(uniform.id, 4, &cache);
}
);
Vec4d m = vec;
paramQueue.Enqueue((IntPtr)uniform.id.valuePtr, (IntPtr)(Vec4d*)&m);
glActionQueue.Enqueue(UniformVal1fvPtr);
}
private static void UniformVal1f()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1f(*(int*)m.UInt64[0], m.Single[2]);
}
private static unsafe void UniformVal1fv()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1fv(*(int*)m.UInt64[0], 4, (Vec4d*)m.UInt64[0]);
}
private static void UniformVal1i()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1i(*(int*)m.UInt64[0], m.Int32[2]);
}
private static void UniformVal2f()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform2f(*(int*)m.UInt64[0], m.Single[2], m.Single[3]);
}
private static void UniformVal3f()
{
IntPtr handle = IntPtr.Zero;
M128 m = default;
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform3f(*(int*)handle, m.Single[0], m.Single[1], m.Single[2]);
}
private static void UniformVal4f()
{
IntPtr handle = IntPtr.Zero;
M128 m = default;
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform4f(*(int*)handle, m.Single[0], m.Single[1], m.Single[2], m.Single[2]);
}
}
}
+6 -1
View File
@@ -4,7 +4,10 @@ using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if Windows
using Windows.Win32;
#endif
namespace StgSharp
{
internal partial class InternalIO
@@ -131,6 +134,8 @@ namespace StgSharp
//[DllImport(InternalIO.SSC_libname, EntryPoint = "glfwCreateWindowSurface")] internal extern unsafe static VkResult glfwCreateWindowSurface(VkInstance instance, IntPtr window, VkAllocationCallbacks* allocator, VkSurfaceKHR* surface);
#endregion
}
}
@@ -45,6 +45,14 @@ namespace StgSharp.Graphics
IntPtr proc = loader("glGetString");
context->glGetString = (delegate*<uint, IntPtr>)proc;
if ((IntPtr)context->glGetString == IntPtr.Zero)
{
#if DEBUG
Console.WriteLine("Cannot load GL.");
throw new Exception("Cannot load gl");
#endif
}
string coreVersion;
if (context->glGetString(glVersion) == IntPtr.Zero)
{
+5
View File
@@ -298,5 +298,10 @@ namespace StgSharp.Graphics
void Viewport(int x, int y, uint width, uint height);
internal void SwitchContext();
internal unsafe GLcontext* Context
{
get;
}
}
}
+18 -2
View File
@@ -30,13 +30,29 @@ namespace StgSharp.Graphics
internal Image(ImageInfo information)
{
info = information;
int length = info.width * info.height * info.channel;
pixelArray = new byte[length];
Marshal.Copy(
source: info.pixelPtr,
destination: pixelArray,
startIndex: 0,
length: info.width * info.height * info.channel
length: length
);
Marshal.FreeHGlobal(info.pixelPtr);
}
internal byte[] PixelArray
{
get
{
if (pixelArray == null)
{
pixelArray = new byte[info.width * info.height * info.channel];
Marshal.Copy(source: info.pixelPtr, destination: pixelArray,
startIndex: 0, length: info.width * info.height * info.channel);
Marshal.FreeHGlobal(info.pixelPtr);
}
return pixelArray;
}
}
public unsafe Image()