daily update on 20240407 by nitload youmu

This commit is contained in:
YoumuLiu
2024-04-07 15:08:52 +08:00
parent 127fcdbfd3
commit e1e416bb41
141 changed files with 2578 additions and 2576 deletions
+132 -33
View File
@@ -44,58 +44,146 @@ namespace StgSharp.Graphics
/// <summary>
/// A handle to an OpenGL object,
/// must be indexed from a <see cref="glHandleSet"/>.
/// Attempt at direct creating an instance will cause <see cref="InvalidOperationException"/>
/// Direct call will cause <see langword="InvalidOperationException"/>
/// </summary>
public unsafe struct glHandle
public unsafe class glHandle
{
private static uint* zeroHandle = (uint*)Marshal.AllocHGlobal(Marshal.SizeOf<uint>());
internal uint* source;
internal uint* valuePtr;
internal glHandle(int index, GCHandle arrayHandle)
private glHandle(IntPtr handle)
{
valuePtr = ((uint*)GCHandle.ToIntPtr(arrayHandle)) + index;
this.valuePtr = (uint*)handle;
source = (uint*)0;
}
private glHandle(uint value)
{
source = (uint*)0;
valuePtr = (uint*)Marshal.AllocHGlobal(sizeof(uint));
*valuePtr = value;
}
internal glHandle(uint* val, glHandleSet set)
{
source = set!=null ? set!.handle:(uint*)0;
valuePtr = val;
}
[Obsolete("Cannot init instance of this type", true)]
public glHandle()
{
throw new InvalidOperationException();
}
public IntPtr Handle
{
get
{
return (IntPtr)valuePtr;
}
get=> (IntPtr)valuePtr;
}
public bool IsIsolated
{
get => source == (uint*)0;
}
public uint Value
{
get
{
return *valuePtr;
}
set
{
*valuePtr = value;
}
get => *valuePtr;
set => *valuePtr = value;
}
public static glHandle Zero => Alloc(0);
internal uint DirectValue => *valuePtr;
public static glHandle Alloc(uint value)
{
return new glHandle(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static glHandle FromPtr(IntPtr p)
{
return new glHandle(p);
}
~glHandle()
{
if (valuePtr != zeroHandle && IsIsolated)
{
Marshal.FreeHGlobal((IntPtr)valuePtr);
}
}
}
public unsafe struct glSHandle
public unsafe class glSHandle
{
private static int* zeroHandle = (int*)Marshal.AllocHGlobal(Marshal.SizeOf<int>());
internal int* source;
internal int* valuePtr;
internal glSHandle(int index, glHandleSet set)
private glSHandle(IntPtr handle)
{
this.valuePtr = (int*)handle;
source = (int*)0;
}
private glSHandle(int value)
{
source = (int*)0;
valuePtr = (int*)Marshal.AllocHGlobal(sizeof(int));
*valuePtr = value;
}
internal glSHandle(int* val, glHandleSet set)
{
source = set != null ? (int*)set!.handle : (int*)0;
valuePtr = val;
}
[Obsolete("Cannot init instance of this type", true)]
public glSHandle()
{
}
public IntPtr Handle
{
get => (IntPtr)valuePtr;
}
public bool IsIsolated
{
get => source == (uint*)0;
}
public int Value
{
get => *valuePtr;
set => *valuePtr = value;
}
public static glSHandle Zero => Alloc(0);
public static glSHandle Alloc(int value)
{
return new glSHandle(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static glSHandle FromPtr(IntPtr p)
{
return new glSHandle(p);
}
~glSHandle()
{
if (valuePtr != zeroHandle && IsIsolated)
{
Marshal.FreeHGlobal((IntPtr)valuePtr);
}
}
internal int DirectValue => *valuePtr;
}
public unsafe class glArrayHandle
@@ -151,20 +239,20 @@ namespace StgSharp.Graphics
public unsafe class glHandleSet
{
private uint[] _value;
internal GCHandle handle;
private readonly uint[] _value;
internal uint* handle;
internal int length;
public glHandleSet(int count)
{
_value = new uint[count];
length = count;
handle = GCHandle.Alloc(_value);
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
public IntPtr Handle
{
get => GCHandle.ToIntPtr(handle);
get => (IntPtr)handle;
}
public glHandle this[int index]
@@ -175,7 +263,7 @@ namespace StgSharp.Graphics
{
throw new IndexOutOfRangeException();
}
return new glHandle(index, this.handle);
return new glHandle(handle + index, this);
}
}
@@ -187,7 +275,7 @@ namespace StgSharp.Graphics
{
throw new IndexOutOfRangeException();
}
return new glSHandle((int)index, this);
return new glSHandle(((int*)handle) + index, this);
}
}
@@ -207,6 +295,15 @@ namespace StgSharp.Graphics
}
}
public void Pin()
{
if (_value == null)
{
return;
}
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
/// <summary>
/// Release all resource in this handle.
/// This will release all resource in this <see cref="glHandle"/>.
@@ -214,24 +311,26 @@ namespace StgSharp.Graphics
/// </summary>
public void Release()
{
if (handle == default)
if (_value == null)
{
return;
}
else
{
handle.Free();
_value = null;
GCHandle.FromIntPtr((IntPtr)handle).Free();
handle = (uint*)IntPtr.Zero;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetValue(int index, uint value)
{
_value[index] = value;
}
~glHandleSet()
{
//unsafe, 不确定这个句柄组的单一索引是不是完全卸载
}
}
}
+16 -16
View File
@@ -53,19 +53,19 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(*program.valuePtr, shader.DirectValue);
api->glAttachShader(program.Value, shader.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindBuffer(BufferType bufferType, glHandle handle)
{
api->glBindBuffer((uint)bufferType, handle.DirectValue);
api->glBindBuffer((uint)bufferType, handle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BindTexture(uint type, glHandle texture)
{
api->glBindTexture(type, texture.DirectValue);
api->glBindTexture(type, texture.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -95,7 +95,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CompileShader(glHandle shaderhandle)
{
api->glCompileShader(shaderhandle.DirectValue);
api->glCompileShader(shaderhandle.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -175,15 +175,15 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static void DeleteBuffers(glHandleSet handle)
{
api->glDeleteBuffers((uint)handle.length, handle.handle);
handle.Free();
api->glDeleteBuffers(handle.length, (uint*)handle.Handle);
handle.Release();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static void DeleteVertexArrays(glHandleSet arrays)
{
api->glDeleteVertexArrays((uint)arrays.length, arrays.handle);
arrays.Free();
api->glDeleteVertexArrays(arrays.length, (uint*)arrays.Handle);
arrays.Release();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -206,7 +206,7 @@ namespace StgSharp.Graphics
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 {nameof(type)} method DrawElements. "}{$"Only {typeof(uint).Name},{typeof(ushort).Name},{typeof(byte).Name} types are supported."}",
LogType.Error);
return;
}
@@ -217,7 +217,7 @@ namespace StgSharp.Graphics
public static unsafe glHandleSet GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
api->glGenBuffers((uint)count, h.handle);
api->glGenBuffers(count, (uint*)h.Handle);
return h;
}
@@ -233,7 +233,7 @@ namespace StgSharp.Graphics
public static unsafe glHandleSet GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
api->glGenVertexArrays((uint)count, h.handle);
api->glGenVertexArrays(count, (uint*)h.Handle);
return h;
}
@@ -243,7 +243,7 @@ namespace StgSharp.Graphics
byte[] code = Encoding.UTF8.GetBytes(name);
fixed (byte* ptr = code)
{
return glSHandle.Alloc(api->glGetUniformLocation(program.DirectValue, ptr));
return glSHandle.Alloc(api->glGetUniformLocation(program.Value, ptr));
}
}
@@ -253,16 +253,16 @@ namespace StgSharp.Graphics
byte[] stream = Encoding.UTF8.GetBytes(codeStream);
fixed (byte* streamPtr = stream)
{
api->glShaderSource(shaderHandle.DirectValue, 1, &streamPtr, (void*)0);
api->glShaderSource(shaderHandle.Value, 1, &streamPtr, (void*)0);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void MakeContexCurrent(Form form)
public unsafe static void MakeContextCurrent(Form form)
{
if (form.WindowID == IntPtr.Zero)
{
InternalIO.InternalWriteLog($"From {nameof(form)} is not initted.", LogType.Warning);
InternalIO.InternalWriteLog($"From {nameof(form)} is not inited.", LogType.Warning);
return;
}
InternalIO.glfwMakeContextCurrent(form.id);
@@ -323,7 +323,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UseProgram(glHandle program)
{
api->glUseProgram(program.DirectValue);
api->glUseProgram(program.Value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.ShaderEdit
{
public interface IglConvertable
{
/// <summary>
///
/// </summary>
public void SetAllUniforms();
/// <summary>
///
/// </summary>
public void GainAllUniforms(ShaderProgram source, params string[] uniformName);
/// <summary>
///
/// </summary>
public void DisplayGLtypeDefinition();
/// <summary>
///
/// </summary>
/// <returns></returns>
public ShaderStruct GetConvertedGLtype();
}
}
@@ -40,7 +40,6 @@ namespace StgSharp.Graphics
{
public partial class Form
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected Shader CreateShader(ShaderType type, int count)
{
@@ -55,17 +54,16 @@ namespace StgSharp.Graphics
{
return new ShaderProgram(this);
}
}
public class Shader
{
private readonly IntPtr context;
internal readonly GLhandle handle;
internal readonly glHandleSet handle;
public readonly ShaderType type;
internal unsafe Shader(uint[] handle, ShaderType usage)
internal unsafe Shader(glHandleSet handle, ShaderType usage)
{
this.handle = handle;
context = (IntPtr)GL.api;
@@ -108,7 +106,7 @@ namespace StgSharp.Graphics
internal readonly glHandle handle;
/// <summary>
/// Inti a shader with no program attached.
/// Init a program with no shader attached.
/// </summary>
internal unsafe ShaderProgram(Form binding)
{
@@ -201,5 +199,10 @@ namespace StgSharp.Graphics
GL.UseProgram(handle);
}
~ShaderProgram()
{
//handle.Dispose();
}
}
}
@@ -144,7 +144,7 @@ namespace StgSharp.Graphics.ShaderEdit
}
switch (typeof(T).Name)
{
case "Vec4d":
case "vec4d":
return InternalShaderType.Vector4;
case "Matrix2x2":
return InternalShaderType.Matrix2x2;
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics.ShaderEdit
{
public class ShaderStructMember
{
internal InternalShaderType type;
internal object value;
}
public class ShaderStruct
{
internal Dictionary<glSHandle, ShaderStructMember> uniformPair;
public ShaderStruct(params (glSHandle, ShaderStructMember)[] UniformPais)
{
uniformPair = new Dictionary<glSHandle, ShaderStructMember>();
if (UniformPais.Length == 0)
{
return;
}
foreach (var item in UniformPais)
{
uniformPair.Add(item.Item1,item.Item2);
}
}
internal ShaderStruct() { }
public unsafe void SetAllUniforms()
{
GLcontext* gl = GL.api;
foreach (var uniform in uniformPair)
{
int id = uniform.Key.Value;
ShaderStructMember s = uniform.Value;
switch (s.type)
{
case InternalShaderType.Struct: throw new ArgumentException(
"Incorrect type \"Struct\" defined in this self define type");
case InternalShaderType.Void:throw new ArgumentException(
"Incorrect type \"Void\" defined in this self define type");
case InternalShaderType.Float:
gl->glUniform1f(id, (float)s.value);
break;
case InternalShaderType.Int:
gl->glUniform1f(id, (int)s.value);
break;
default:
throw new NotImplementedException();
throw new ArgumentException(
"Incorrect type \"Unknown\" defined in this self define type");
}
}
}
}
}
@@ -28,6 +28,7 @@
//
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
using StgSharp.Graphic;
using StgSharp.Math;
using System;
@@ -39,12 +40,22 @@ using System.Text;
namespace StgSharp.Graphics
{
public class Uniform<T> where T : struct
public class Uniform
{
private IntPtr binding;
protected IntPtr binding;
internal glSHandle id;
public glSHandle Handle
{
get => id;
}
}
public sealed class Uniform<T> : Uniform
where T : struct
{
internal unsafe Uniform(glSHandle id)
{
this.id = id;
@@ -56,6 +67,7 @@ namespace StgSharp.Graphics
this.binding = binding.graphicContextID;
id = GL.GetUniformLocation(program.handle, name);
}
internal unsafe Uniform(
ShaderProgram program, string name, IntPtr binding)
{
@@ -63,16 +75,23 @@ namespace StgSharp.Graphics
id = GL.GetUniformLocation(program.handle, name);
}
internal Uniform()
{
}
internal static Uniform<T> FromHandle(glSHandle handle)
{
return new Uniform<T>() { id = handle };
}
}
public class Uniform<T, U>
public sealed class Uniform<T, U>: Uniform
where T : struct
where U : struct
{
private IntPtr binding;
internal glSHandle id;
internal unsafe Uniform(glSHandle id)
{
this.id = id;
@@ -92,17 +111,24 @@ namespace StgSharp.Graphics
id = GL.GetUniformLocation(program.handle, name);
}
internal Uniform()
{
}
internal static Uniform<T,U> FromHandle(glSHandle handle)
{
return new Uniform<T, U>() { id = handle };
}
}
public class Uniform<T, U, V>
public sealed class Uniform<T, U, V>:Uniform
where T : struct
where U : struct
where V : struct
{
private IntPtr binding;
internal glSHandle id;
internal unsafe Uniform(glSHandle id)
{
this.id = id;
@@ -121,17 +147,25 @@ namespace StgSharp.Graphics
id = GL.GetUniformLocation(program.handle, name);
}
internal Uniform()
{
}
internal static Uniform<T,U,V> FromHandle(glSHandle handle)
{
return new Uniform<T, U, V>() { id = handle };
}
}
public class Uniform<T, U, V, W>
public sealed class Uniform<T, U, V, W>:Uniform
where T : struct
where U : struct
where V : struct
where W : struct
{
private IntPtr binding;
internal glSHandle id;
internal unsafe Uniform(glSHandle id)
{
@@ -151,51 +185,69 @@ namespace StgSharp.Graphics
id = GL.GetUniformLocation(program.handle, name);
}
internal Uniform()
{
}
internal static Uniform<T, U, V, W> FromHandle(glSHandle handle)
{
return new Uniform<T, U, V, W>() { id = handle };
}
}
internal static partial class GL
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
public unsafe static void SetValue(Uniform<Matrix44> uniform, Matrix44 mat)
{
api->glUniform1fv(uniform.id.DirectValue , 4, &vec);
Matrix44 temp = mat;
api->glUniformMatrix4fv(uniform.Handle.Value, 16, false, (float*)&mat);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void SetValue(Uniform<Vec4d> uniform, Vec4d vec)
public unsafe static void SetValue(Uniform<float, float, float, float> uniform, vec4d vec)
{
api->glUniform1fv(uniform.id.DirectValue , 4, &vec);
vec4d temp = vec;
api->glUniform1fv(uniform.id.Value , 4, &temp);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe static void SetValue(Uniform<vec4d> uniform, vec4d vec)
{
vec4d temp = vec;
api->glUniform1fv(uniform.id.Value , 4, &temp);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float> uniform, float v0)
{
api->glUniform1f(uniform.id.DirectValue , v0);
api->glUniform1f(uniform.id.Value , v0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<int> uniform, int i0)
{
api->glUniform1i(uniform.id.DirectValue , i0);
api->glUniform1i(uniform.id.Value , i0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float> uniform, float v0, float v1)
{
api->glUniform2f(uniform.id.DirectValue , v0, v1);
api->glUniform2f(uniform.id.Value , v0, v1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
api->glUniform3f(uniform.id.DirectValue , v0, v1, v2);
api->glUniform3f(uniform.id.Value , v0, v1, v2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
api->glUniform4f(uniform.id.DirectValue , v0, v1, v2, v3);
api->glUniform4f(uniform.id.Value , v0, v1, v2, v3);
}
}
@@ -1081,7 +1081,7 @@ namespace StgSharp.Graphics
contextPtr->glDeleteTextures = (delegate*<uint, uint*, void>)load("glDeleteTextures");
contextPtr->glDrawArrays = (delegate*<uint, int, uint, void>)load("glDrawArrays");
contextPtr->glDrawElements = (delegate*<uint, uint, uint, void*, void>)load("glDrawElements");
contextPtr->glGenTextures = (delegate*<uint, uint*, void>)load("glGenTextures");
contextPtr->glGenTextures = (delegate*<int, uint*, void>)load("glGenTextures");
contextPtr->glGetPointerv = (delegate*<uint, void**, void>)load("glGetPointerv");
contextPtr->glIsTexture = (delegate*<uint, bool>)load("glIsTexture");
contextPtr->glPolygonOffset = (delegate*<float, float, void>)load("glPolygonOffset");
@@ -1177,10 +1177,10 @@ namespace StgSharp.Graphics
contextPtr->glBindBuffer = (delegate*<uint, uint, void>)load("glBindBuffer");
contextPtr->glBufferData = (delegate*<uint, IntPtr, IntPtr, uint, void>)load("glBufferData");
contextPtr->glBufferSubData = (delegate*<uint, IntPtr, UIntPtr, void*, void>)load("glBufferSubData");
contextPtr->glDeleteBuffers = (delegate*<uint, uint*, void>)load("glDeleteBuffers");
contextPtr->glDeleteBuffers = (delegate*<int, uint*, void>)load("glDeleteBuffers");
contextPtr->glDeleteQueries = (delegate*<uint, uint*, void>)load("glDeleteQueries");
contextPtr->glEndQuery = (delegate*<uint, void>)load("glEndQuery");
contextPtr->glGenBuffers = (delegate*<uint, uint*, void>)load("glGenBuffers");
contextPtr->glGenBuffers = (delegate*<int, uint*, void>)load("glGenBuffers");
contextPtr->glGenQueries = (delegate*<uint, uint*, void>)load("glGenQueries");
contextPtr->glGetBufferParameteriv = (delegate*<uint, uint, int*, void>)load("glGetBufferParameteriv");
contextPtr->glGetBufferPointerv = (delegate*<uint, uint, void**, void>)load("glGetBufferPointerv");
@@ -1244,7 +1244,7 @@ namespace StgSharp.Graphics
contextPtr->glStencilMaskSeparate = (delegate*<uint, uint, void>)load("glStencilMaskSeparate");
contextPtr->glStencilOpSeparate = (delegate*<uint, uint, uint, uint, void>)load("glStencilOpSeparate");
contextPtr->glUniform1f = (delegate*<int, float, void>)load("glUniform1f");
contextPtr->glUniform1fv = (delegate*<int, uint, Vec4d*, void>)load("glUniform1fv");
contextPtr->glUniform1fv = (delegate*<int, uint, vec4d*, void>)load("glUniform1fv");
contextPtr->glUniform1i = (delegate*<int, int, void>)load("glUniform1i");
contextPtr->glUniform1iv = (delegate*<int, uint, int*, void>)load("glUniform1iv");
contextPtr->glUniform2f = (delegate*<int, float, float, void>)load("glUniform2f");
@@ -1357,7 +1357,7 @@ namespace StgSharp.Graphics
contextPtr->glColorMaski = (delegate*<uint, bool, bool, bool, bool, void>)load("glColorMaski");
contextPtr->glDeleteFramebuffers = (delegate*<uint, uint*, void>)load("glDeleteFramebuffers");
contextPtr->glDeleteRenderbuffers = (delegate*<uint, uint*, void>)load("glDeleteRenderbuffers");
contextPtr->glDeleteVertexArrays = (delegate*<uint, uint*, void>)load("glDeleteVertexArrays");
contextPtr->glDeleteVertexArrays = (delegate*<int, uint*, void>)load("glDeleteVertexArrays");
contextPtr->glDisablei = (delegate*<uint, uint, void>)load("glDisablei");
contextPtr->glEnablei = (delegate*<uint, uint, void>)load("glEnablei");
contextPtr->glEndConditionalRender = (delegate*<void>)load("glEndConditionalRender");
@@ -1370,7 +1370,7 @@ namespace StgSharp.Graphics
contextPtr->glFramebufferTextureLayer = (delegate*<uint, uint, uint, int, int, void>)load("glFramebufferTextureLayer");
contextPtr->glGenFramebuffers = (delegate*<uint, uint*, void>)load("glGenFramebuffers");
contextPtr->glGenRenderbuffers = (delegate*<uint, uint*, void>)load("glGenRenderbuffers");
contextPtr->glGenVertexArrays = (delegate*<uint, uint*, void>)load("glGenVertexArrays");
contextPtr->glGenVertexArrays = (delegate*<int, uint*, void>)load("glGenVertexArrays");
contextPtr->glGenerateMipmap = (delegate*<uint, void>)load("glGenerateMipmap");
contextPtr->glGetBooleani_v = (delegate*<uint, uint, bool*, void>)load("glGetBooleani_v");
contextPtr->glGetFragDataLocation = (delegate*<uint, byte*, int>)load("glGetFragDataLocation");