daily update on 20240305 by nitload youmu

This commit is contained in:
YoumuLiu
2024-03-05 10:17:02 +08:00
parent 1f2fca4c8f
commit 6a0cccae79
156 changed files with 23040 additions and 1385 deletions
+7 -7
View File
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -7,11 +8,9 @@ using System.Text;
namespace StgSharp.Graphics
{
/// <summary>
/// A collecion of handle to Element Buffer Object in OpenGL.
/// Each <see langword="int"/> value indexed from instance of this class this
/// Each <see langword="uint"/> value indexed from instance of this class this
/// is the only handle to one Element Buffer Object.
/// </summary>
public unsafe class ElementBuffer : BufferObjectBase
@@ -22,8 +21,7 @@ namespace StgSharp.Graphics
_bufferHandle = GL.GenBuffers(n);
}
public override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
}
@@ -31,16 +29,18 @@ namespace StgSharp.Graphics
public void SetValue<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct, IConvertible
{
Console.WriteLine("Setting ebo data.");
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+5 -5
View File
@@ -5,7 +5,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexArray : BufferObjectBase
{
internal VertexArray(int n, IglFunc gl)
@@ -15,7 +14,7 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindVertexArray(_bufferHandle[index]);
}
@@ -23,7 +22,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void BindNull()
{
GL.BindVertexArray(glHandle.Alloc(0));
GL.BindVertexArray(glHandle.Zero);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -35,12 +34,13 @@ namespace StgSharp.Graphics
stride, pointer);
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteVertexArrays(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+7 -7
View File
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Net.Security;
@@ -9,7 +10,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe class VertexBuffer : BufferObjectBase
{
internal VertexBuffer(int n, IglFunc glFunc)
@@ -18,7 +18,7 @@ namespace StgSharp.Graphics
_bufferHandle = GL.GenBuffers(n);
}
public sealed override void Bind(int index)
public override sealed void Bind(int index)
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
}
@@ -32,9 +32,9 @@ namespace StgSharp.Graphics
/// <param name="usage">How OpenGL use these data, defined by <see cref="BufferUsage"/></param>
public void SetValue<T>(int index, T bufferData, BufferUsage usage) where T : IMat
{
Console.WriteLine("Setting vbo data.");
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
}
/// <summary>
@@ -51,7 +51,7 @@ namespace StgSharp.Graphics
}
/// <summary>
///
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
@@ -65,13 +65,13 @@ namespace StgSharp.Graphics
GL = gl;
}
protected sealed override void Dispose(bool disposing)
protected override sealed void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferHandle);
_bufferHandle.Release();
}
}
}
}
}
+28 -1
View File
@@ -9,8 +9,35 @@ using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace System
namespace StgSharp.Graphics
{
//TODO 发现一个大问题,paramQueue大多数时候取出的是数组,不是数组句柄,也就是说其内存将被长期固定且不能回收,这是内存泄漏
public unsafe class glArrayHandle
{
internal readonly Array copy;
internal GCHandle handle;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IntPtr AddressOfArray()
{
return handle.AddrOfPinnedObject();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static glArrayHandle Alloc<T>(T[] array) where T : struct
{
Array copy = new T[array.Length];
array.CopyTo(copy, 0);
return new glArrayHandle(copy);
}
internal glArrayHandle(Array array)
{
copy = array;
handle = GCHandle.Alloc(array, GCHandleType.Pinned);
}
}
/// <summary>
/// A handle to an OpenGL object,
/// must be indexed from a <see cref="glHandleSet"/>.
+1 -3
View File
@@ -9,7 +9,5 @@ namespace StgSharp.Graphics
{
public unsafe partial class Form
{
}
}
}
+30 -48
View File
@@ -54,13 +54,13 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(*program.valuePtr, *shader.valuePtr);
api->glAttachShader(*program.valuePtr, shader.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, glHandle handle)
{
api->glBindBuffer((uint)bufferType, *handle.valuePtr);
api->glBindBuffer((uint)bufferType, handle.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -93,57 +93,39 @@ namespace StgSharp.Graphics
while (api->glGetError() != 0) { }
}
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCodeArray)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
int l = shaderCodeArray.Length;
if (l == 0)
byte[] stream = Encoding.UTF8.GetBytes(codeStream);
fixed (byte* streamPtr = stream)
{
throw new ArgumentException("At least one pice of code is required.");
api->glShaderSource(shaderHandle.DirectValue, 1, &streamPtr, (void*)0);
}
glHandleSet handleSet = new glHandleSet(l);
for (int i = 0; i < l; i++)
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandleSet IglFunc.CreateShaderSet(int count, ShaderType type)
{
glHandleSet ret = new glHandleSet(count);
uint t = (uint)type;
for (int i = 0; i < count; i++)
{
uint shaderID;
byte[] data = Encoding.UTF8.GetBytes(shaderCodeArray[i] + '\0');
fixed (byte* codePtr = data)
uint id = api->glCreateShader(t);
if (id == 0)
{
shaderID = InternalIO.InternalLoadShaderCode(api, codePtr, (int)shaderType);
uint error = api->glGetError();
InternalIO.InternalWriteLog($"Failed in creating shader: {error}", LogType.Error);
throw new Exception();
}
if (shaderID == 0) //compile error handler
{
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
bool isUseful = false;
foreach (char item in log)
{
if (!char.IsControl(item))
{
isUseful = true; break;
}
}
if (!isUseful)
{
throw new Exception();
}
}
catch (Exception)
{
log = "Failed to compile shader.\n" +
"Failed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
}
handleSet.SetValue(i, shaderID);
ret.SetValue(i, id);
}
return handleSet;
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.CompileShader(glHandle shaderhandle)
{
api->glCompileShader(shaderhandle.DirectValue);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -301,7 +283,7 @@ namespace StgSharp.Graphics
api->glBufferData(
(uint)bufferType,
(IntPtr)size,
&bufferData,
(IntPtr)(&bufferData),
(uint)usage);
}
@@ -317,7 +299,7 @@ namespace StgSharp.Graphics
api->glBufferData(
(uint)bufferType,
(IntPtr)size,
bufferPtr,
(IntPtr)bufferPtr,
(uint)usage);
}
+18 -8
View File
@@ -18,11 +18,11 @@ namespace StgSharp.Graphics
private FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
private FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
unsafe GLcontext* IglFunc.Context => throw signal;
private GLonTimeout()
{ }
unsafe GLcontext* IglFunc.Context => throw signal;
void IglFunc.ActiveTexture(uint type) => throw signal;
void IglFunc.AfterAlign(Form form, Action action) => throw signal;
@@ -43,12 +43,17 @@ namespace StgSharp.Graphics
void IglFunc.ClearGraphicError() => throw signal;
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode) => throw signal;
public void CompileShader(glHandle shaderhandle)
{
throw signal;
}
glHandle IglFunc.CreateProgram() => throw signal;
glHandleSet IglFunc.CreateProgram(int count) => throw signal;
glHandleSet IglFunc.CreateShaderSet(int count, StgSharp.Graphics.ShaderType type) => throw signal;
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share) => throw signal;
glHandle IglFunc.CreatGraphicProgram() => throw signal;
@@ -71,6 +76,11 @@ namespace StgSharp.Graphics
bool IglFunc.GetWindowShouldClose(Form form) => throw signal;
void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
throw signal;
}
void IglFunc.MakeContexCurrent(Form form) => throw signal;
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) => throw signal;
@@ -93,6 +103,11 @@ namespace StgSharp.Graphics
void IglFunc.SetVertexAttribute(uint index, int size, TypeCode t, bool normalized, uint stride, int pointer) => throw signal;
void IglFunc.SwitchContext()
{
throw signal;
}
unsafe void IglFunc.TextureImage2d<T>(int target, int level, uint internalformat, uint width, uint height, uint format, uint type, glHandle pixels) => throw signal;
void IglFunc.TextureParameter(int target, uint pname, int param) => throw signal;
@@ -102,10 +117,5 @@ namespace StgSharp.Graphics
void IglFunc.Viewport(int x, int y, uint width, uint height) => throw signal;
internal static GLonTimeout Set() => singleInstance;
void IglFunc.SwitchContext()
{
throw signal;
}
}
}
+54 -42
View File
@@ -17,15 +17,13 @@ namespace StgSharp.Graphics
{
private static ConcurrentQueue<IntPtr> glActionQueue;
private static GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
private GLcontext* api;
private FrameTimeoutException refreshSignal = new();
private IntPtr SwapBufferPtr = (IntPtr)(delegate*<IntPtr, void>)&InternalIO.glfwSwapBuffers;
GLcontext* IglFunc.Context
internal void ShowDequeueStream()
{
get => api;
paramQueue.ShowDequeueLog();
}
internal GLonUpdate(GLcontext* context)
@@ -38,6 +36,11 @@ namespace StgSharp.Graphics
api = context;
}
GLcontext* IglFunc.Context
{
get => api;
}
internal static GLparamQueue ParamQueue
{
get { return paramQueue; }
@@ -84,8 +87,8 @@ namespace StgSharp.Graphics
void IglFunc.BindBuffer(BufferType bufferType, glHandle handle)
{
M128 m = default;
m.UInt32[0] = (uint)bufferType;
m.UInt64[1] = (ulong)handle.Handle;
m.Write<uint>(0, (uint)bufferType);
m.Write<ulong>(1, (ulong)handle.valuePtr);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindBufferPtr);
}
@@ -94,8 +97,8 @@ namespace StgSharp.Graphics
void IglFunc.BindTexture(uint type, glHandle texture)
{
M128 m = default;
m.UInt64[1] = (ulong)texture.valuePtr;
m.UInt32[0] = (uint)type;
m.Write<uint>(0, (uint)type);
m.Write<ulong>(1, (ulong)texture.valuePtr);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(BindTexturePtr);
}
@@ -110,6 +113,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
paramQueue.Enqueue((int)mask);
glActionQueue.Enqueue(ClearPtr);
}
@@ -126,22 +130,10 @@ namespace StgSharp.Graphics
glActionQueue.Enqueue(ClearGraphicErrorPtr);
}
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode)
unsafe void IglFunc.CompileShader(glHandle shaderhandle)
{
int l = shaderCode.Length;
if (l == 0)
{
throw new ArgumentException("Should input at least one piece of shader code");
}
glHandleSet shaderHandle = new glHandleSet(shaderCode.Length);
paramQueue.Enqueue((int)shaderType, l);
for (int i = 0; i < l; i++)
{
GCHandle codePtr = GCHandle.Alloc(Encoding.UTF8.GetBytes(shaderCode[i] + '\0'), GCHandleType.Pinned);
paramQueue.Enqueue((IntPtr)(shaderHandle.handle + i), codePtr.AddrOfPinnedObject());
}
paramQueue.Enqueue((IntPtr)shaderhandle.valuePtr);
glActionQueue.Enqueue(CompileShaderPtr);
return shaderHandle;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -153,18 +145,31 @@ namespace StgSharp.Graphics
return h;
}
//TODO
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandleSet IglFunc.CreateProgram(int count)
{
M128 m = default;
glHandleSet h = new glHandleSet(count);
m.UInt64[0] = (ulong)h.handle;
m.Int32[2] = count;
m.Write<ulong>(0, (ulong)h.handle);
m.Write<int>(2, count);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(CreateProgramSetPtr);
return h;
}
glHandleSet IglFunc.CreateShaderSet(int count, ShaderType type)
{
glHandleSet ret = new glHandleSet(count);
M128 m = new M128();
m.Write<int>(0, count);
m.Write<uint>(1, (uint)type);
m.Write<ulong>(1, (ulong)ret.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(CreateShaderPtr);
return ret;
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
@@ -203,8 +208,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.DeleteBuffer(glHandle handle)
{
M128 m = default;
m.UInt64[0] = (ulong)handle.Handle;
m.UInt32[2] = 1;
m.Write<ulong>(0, (ulong)handle.valuePtr);
m.Write(2, 1);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
}
@@ -212,16 +217,16 @@ namespace StgSharp.Graphics
unsafe void IglFunc.DeleteBuffers(glHandleSet handle)
{
M128 m = default;
m.UInt32[2] = (uint)handle.length;
m.UInt64[0] = (ulong)handle.handle;
m.Write<uint>(2, (uint)handle.length);
m.Write<ulong>(0, (ulong)handle.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleBufferPtr);
}
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
M128 m = default; m.UInt64[1] = (ulong)arrays.handle;
m.Int32[0] = arrays.length;
M128 m = default; m.Write<ulong>(1, (ulong)arrays.handle);
m.Write<int>(0, arrays.length);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(DeleteVertexArrayPtr);
}
@@ -258,7 +263,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenBuffers(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenBufferPtr);
return h;
@@ -267,7 +272,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenTextures(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -276,7 +281,7 @@ namespace StgSharp.Graphics
unsafe glHandleSet IglFunc.GenVertexArrays(int count)
{
glHandleSet h = new glHandleSet(count);
M128 m = default; m.UInt32[2] = (uint)count; m.UInt64[0] = (ulong)h.handle;
M128 m = default; m.Write<uint>(2, (uint)count); m.Write<ulong>(0, (ulong)h.handle);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(GenTexturesPtr);
return h;
@@ -286,9 +291,9 @@ namespace StgSharp.Graphics
unsafe glSHandle IglFunc.GetUniformLocation(glHandle program, string ptr)
{
glSHandle h = glSHandle.Alloc(0);
M128 m;
m.UInt64[0] = (ulong)program.Handle;
m.UInt64[1] = (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject();
M128 m = default;
m.Write<ulong>(0, (ulong)program.Handle);
m.Write<ulong>(1, (ulong)GCHandle.Alloc(Encoding.UTF8.GetBytes(ptr)).AddrOfPinnedObject());
paramQueue.Enqueue(m);
paramQueue.Enqueue((IntPtr)h.valuePtr);
glActionQueue.Enqueue(GetUniformLocationPtr);
@@ -310,6 +315,12 @@ namespace StgSharp.Graphics
InternalIO.glfwSwapBuffers(form.windowID);
}
void IglFunc.LoadShaderSource(glHandle shaderHandle, string codeStream)
{
paramQueue.Enqueue(shaderHandle);
paramQueue.Enqueue(codeStream);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.MakeContexCurrent(Form form)
{
@@ -330,7 +341,8 @@ namespace StgSharp.Graphics
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
paramQueue.Enqueue((uint)bufferType, (uint)usage);
paramQueue.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length), GCHandle.Alloc(bufferArray).AddrOfPinnedObject());
paramQueue.Enqueue((IntPtr)(Marshal.SizeOf<T>() * bufferArray.Length));
paramQueue.Enqueue<T>(bufferArray);
glActionQueue.Enqueue(BufferDataPtr);
}
@@ -359,9 +371,9 @@ namespace StgSharp.Graphics
void IglFunc.TextureParameter(int target, uint pname, int param)
{
M128 m = default;
m.Int32[0] = target;
m.UInt32[2] = pname;
m.Int32[3] = param;
m.Write<int>(0, target);
m.Write<uint>(2, pname);
m.Write<int>(3, param);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(TextureParameterPtr);
}
@@ -382,11 +394,11 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void frameworkHandleEnqueue(IntPtr methodHandle, params M128[] param)
{
glActionQueue.Enqueue(methodHandle);
foreach (M128 item in param)
{
paramQueue.Enqueue(item);
}
glActionQueue.Enqueue(methodHandle);
}
}
}
@@ -18,16 +18,17 @@ namespace StgSharp.Graphics
private IntPtr ActiveTexturePtr = (IntPtr)(delegate*<void>)&InternalActiveTexture;
private IntPtr AttachShaderPtr = (IntPtr)(delegate*<void>)&InternalAttachShader;
private IntPtr BindBufferPtr = (IntPtr)(delegate*<void>)&InternalBindBuffer;
private IntPtr ClearColorPtr = (IntPtr)(delegate*<void>)&InternalClearColor;
private IntPtr ClearGraphicErrorPtr = (IntPtr)(delegate*<void>)&InternalClearGraphicError;
private IntPtr CompileShaderPtr = (IntPtr)(delegate*<void>)&InternalCompileShader;
private IntPtr DeleBufferPtr = (IntPtr)(delegate*<void>)&InternalDeleteBuffer;
private IntPtr BindTexturePtr = (IntPtr)(delegate*<void>)&InternalBindTexture;
private IntPtr BindVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalBindVertexArray;
private IntPtr BufferDataPtr = (IntPtr)(delegate*<void>)&InternalSetBufferData;
private IntPtr ClearColorPtr = (IntPtr)(delegate*<void>)&InternalClearColor;
private IntPtr ClearGraphicErrorPtr = (IntPtr)(delegate*<void>)&InternalClearGraphicError;
private IntPtr ClearPtr = (IntPtr)(delegate*<void>)&InternalClear;
private IntPtr CompileShaderPtr = (IntPtr)(delegate*<void>)&InternalCompileShader;
private IntPtr CreateProgramPtr = (IntPtr)(delegate*<void>)&InternalCreateProgram;
private IntPtr CreateProgramSetPtr = (IntPtr)(delegate*<void>)&InternalCreateProgramSet;
private IntPtr CreateShaderPtr = (IntPtr)(delegate*<void>)&InternalCreateShader;
private IntPtr DeleBufferPtr = (IntPtr)(delegate*<void>)&InternalDeleteBuffer;
private IntPtr DeleteVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalDeleteVertexArrays;
private IntPtr DrawElementsPtr = (IntPtr)(delegate*<void>)&InternalDrawElements;
private IntPtr GenBufferPtr = (IntPtr)(delegate*<void>)&InternalGenBuffers;
@@ -35,6 +36,7 @@ namespace StgSharp.Graphics
private IntPtr GenVertexArraysPtr = (IntPtr)(delegate*<void>)&InternalGenVertexArrays;
private IntPtr GetUniformLocationPtr = (IntPtr)(delegate*<void>)&InternalGetUniformLocation;
private IntPtr SetVertexArrayPtr = (IntPtr)(delegate*<void>)&InternalSetVertexAttribute;
private IntPtr ShaderSourcePtr = (IntPtr)(delegate*<void>)&InternalShaderSource;
private IntPtr textureImage2dPtr = (IntPtr)(delegate*<void>)&InternalTextureImage2d;
private IntPtr TextureParameterPtr = (IntPtr)(delegate*<void>)&InternalTextureParameter;
private IntPtr UseProgramPtr = (IntPtr)(delegate*<void>)&InternalUseProgram;
@@ -56,16 +58,21 @@ namespace StgSharp.Graphics
private static void InternalBindBuffer()
{
M128 m = default;
M128 m = new M128();
paramQueue.TryDequeue(out m);
paramQueue.context->glBindBuffer(m.UInt32[0], *(uint*)m.UInt64[1]);
uint usage = m.Read<uint>(0);
uint handle = *(uint*)m.Read<ulong>(1);
paramQueue.context->glBindBuffer(usage, handle);
}
private static void InternalBindTexture()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glBindTexture(m.UInt32[0], *(uint*)m.UInt64[1]);
paramQueue.context->glBindTexture(
m.Read<uint>(0),
*(uint*)m.Read<ulong>(1)
);
}
private static void InternalBindVertexArray()
@@ -94,53 +101,14 @@ namespace StgSharp.Graphics
while (paramQueue.context->glGetError() != 0) { }
}
private static void InternalCompileShader()
private static unsafe void InternalCompileShader()
{
IntPtr shaderHandle, codeHandle;
int count, type;
paramQueue.TryDequeue(out type, out count);
for (int i = 0; i < count; i++)
{
paramQueue.TryDequeue(out shaderHandle, out codeHandle);
GCHandle code = GCHandle.FromIntPtr(codeHandle);
byte[] c = (byte[])code.Target;
Console.WriteLine(Encoding.UTF8.GetString(c));
*(uint*)shaderHandle = InternalIO.InternalLoadShaderCode(paramQueue.context, (byte*)codeHandle, type);
if (*(uint*)shaderHandle == 0)
{
IntPtr logPtr = InternalIO.InternalReadSSCLog();
string log;
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
bool isUseful = false;
foreach (char item in log)
{
if (!char.IsControl(item))
{
isUseful = true; break;
}
}
if (!isUseful)
{
throw new Exception();
}
}
catch (Exception)
{
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
}
GCHandle.FromIntPtr(codeHandle).Free();
}
IntPtr handle = default;
paramQueue.TryDequeue(out handle);
paramQueue.context->glCompileShader(*(uint*)handle);
}
//TODO
private static void InternalCreateProgram()
{
IntPtr programHandle = default;
@@ -153,25 +121,36 @@ namespace StgSharp.Graphics
uint* programHandle;
M128 m = default;
paramQueue.TryDequeue(out m);
programHandle = (uint*)m.UInt64[0];
for (int i = 0; i < m.Int32[2]; i++)
programHandle = (uint*)m.Read<ulong>(0);
for (int i = 0; i < m.Read<int>(2); i++)
{
*(programHandle + i) = paramQueue.context->glCreateProgram();
}
}
private static unsafe void InternalCreateShader()
{
M128 m = default;
paramQueue.TryDequeue(out m);
int count = m.Read<int>(0); uint type = m.Read<uint>(1); uint* handle = (uint*)m.Read<ulong>(1);
for (int i = 0; i < count; i++)
{
*(handle + i) = paramQueue.context->glCreateShader(type);
}
}
private static void InternalDeleteBuffer()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glDeleteBuffers(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalDeleteVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glDeleteVertexArrays((uint)m.Int32[0], (uint*)m.UInt64[0]);
paramQueue.context->glDeleteVertexArrays((uint)m.Read<int>(0), (uint*)m.Read<ulong>(0));
}
private static void InternalDrawElements()
@@ -187,21 +166,21 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenBuffers(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenBuffers(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGenTextures()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenTextures(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenTextures(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGenVertexArrays()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glGenVertexArrays(m.UInt32[2], (uint*)m.UInt64[0]);
paramQueue.context->glGenVertexArrays(m.Read<uint>(2), (uint*)m.Read<ulong>(0));
}
private static void InternalGetUniformLocation()
@@ -215,11 +194,12 @@ namespace StgSharp.Graphics
private static void InternalSetBufferData()
{
uint type = 0, use = 0;
IntPtr size = default, handle = default;
IntPtr size = default;
glArrayHandle array = default;
paramQueue.TryDequeue(out type, out use);
paramQueue.TryDequeue(out size, out handle);
paramQueue.context->glBufferData(type, size, (void*)handle, use);
GCHandle.FromIntPtr(handle).Free();
paramQueue.TryDequeue(out size);
paramQueue.TryDequeue(out array);
paramQueue.context->glBufferData(type, size, array.AddressOfArray(), use);
}
private static void InternalSetVertexAttribute()
@@ -232,6 +212,18 @@ namespace StgSharp.Graphics
paramQueue.context->glEnableVertexAttribArray(index);
}
private static unsafe void InternalShaderSource()
{
IntPtr handle = default;
byte[] stream = Array.Empty<byte>();
paramQueue.TryDequeue(out handle);
paramQueue.TryDequeue(out stream);
fixed (byte* sPtr = stream)
{
paramQueue.context->glShaderSource(*(uint*)handle, 1, &sPtr, (void*)0);
}
}
private static void InternalTextureImage2d()
{
IntPtr pix = IntPtr.Zero;
@@ -240,14 +232,13 @@ namespace StgSharp.Graphics
paramQueue.TryDequeue(out level, out internalFormat, out width, out height);
paramQueue.TryDequeue(out target, out format, out type);
paramQueue.context->glTexImage2D(target, (int)level, internalFormat, width, height, 0, format, type, (void*)pix);
GCHandle.FromIntPtr(pix).Free();
}
private static void InternalTextureParameter()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glTextureParameteri(m.UInt32[0], m.UInt32[2], m.Int32[3]);
paramQueue.context->glTextureParameteri(m.Read<uint>(0), m.Read<uint>(2), m.Read<int>(3));
}
private static void InternalUseProgram()
@@ -261,7 +252,7 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glViewport(m.Int32[0], m.Int32[1], m.UInt32[2], m.UInt32[3]);
paramQueue.context->glViewport(m.Read<int>(0), m.Read<int>(1), m.Read<uint>(2), m.Read<uint>(3));
}
private void InternalMakeContexCurrent(Form form)
@@ -269,11 +260,6 @@ namespace StgSharp.Graphics
throw new NotImplementedException();
}
private void InternalSetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
throw new NotImplementedException();
}
private void InternalSwitchContext()
{
throw new NotImplementedException();
@@ -1,4 +1,5 @@
using StgSharp;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
@@ -19,9 +20,10 @@ namespace StgSharp.Graphics
return new ShaderProgram(GL);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected Shader CreateShader(ShaderType type, int count)
{
return new Shader(this.gl, new glHandleSet(count), type);
return new Shader(this.gl, gl.CreateShaderSet(count, type), type);
}
}
@@ -38,18 +40,20 @@ namespace StgSharp.Graphics
type = usage;
}
/// <summary>
/// Compile the vertex shader to be attached to this shader.
/// </summary>
/// <param name="code">GLSL code of vertex shader.</param>
public unsafe void Compile(params string[] code)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Compile(int index, string code)
{
glHandleSet.Copy(GL.CompileShader(type, code), handle);
if (code == null)
{
throw new ArgumentNullException("Must provide enough code");
}
GL.LoadShaderSource(handle[index], code + '\0');
GL.CompileShader(handle[index]);
}
//TODO:写链接着色器的文档注释
/// <summary>
///
///
/// </summary>
/// <param name="target"></param>
/// <param name="index"></param>
@@ -103,8 +107,8 @@ namespace StgSharp.Graphics
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -113,9 +117,10 @@ namespace StgSharp.Graphics
{
return new Uniform<T>(this, name, GL);
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -125,9 +130,10 @@ namespace StgSharp.Graphics
{
return new Uniform<T, U>(this, name, GL);
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
@@ -148,4 +154,4 @@ namespace StgSharp.Graphics
GL.UseProgram(handle);
}
}
}
}
@@ -0,0 +1,108 @@
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace StgSharpDebug.Graphics.GL.ShaderEdit
{
public class ShaderGenerator
{
private List<string> codeList;
private List<ShaderFunction> usedShaderFunctions;
public Uniform<T> DefineUniform<T>() where T : struct
{
throw new NotImplementedException();
}
}
public class ShaderFunction
{
private string name;
private List<string> codeList;
private List<ShaderParameter> inputList;
private ShaderParameter outPut;
public int ParameterCount
{
get { return inputList.Count; }
}
public void EmitCall(ShaderFunction function, ShaderParameter outPut, params ShaderParameter[] inputParams)
{
if (inputParams.Length != function.ParameterCount)
{
throw new ArgumentException("Provided parameters does not match function's parameter list.");
}
if (!ShaderParameter.IsSameType(outPut, function.outPut))
{
throw new Exception("Provided parameters does not match function's parameter list.");
}
for (int i = 0; i < inputParams.Length; i++)
{
if (!ShaderParameter.IsSameType(function.inputList[i], inputParams[i]))
{
throw new Exception("Provided parameters does not match function's parameter list.");
}
}
string codeLine = "";
if (outPut != ShaderParameter.Void)
{
codeLine += $"{outPut.name} = ";
}
codeLine += $"{function.name}(";
foreach (ShaderParameter inputParam in inputParams)
{
codeLine += inputParam.name;
}
codeLine += ");\n";
codeList.Add(codeLine);
}
}
public class ShaderParameter
{
internal readonly InternalShaderType type;
internal readonly string name;
internal readonly bool isReadOnly;
public ShaderParameter(string name, InternalShaderType type)
{
this.name = name;
this.type = type;
isReadOnly = false;
}
public static readonly ShaderParameter Void = new ShaderParameter("", InternalShaderType.Void);
public static bool IsSameType(ShaderParameter param1, ShaderParameter param2)
{
if (param1.type != InternalShaderType.Struct)
{
return param1.type == param2.type;
}
else
{
throw new Exception("Struct is currently not supported.");
}
}
}
public enum InternalShaderType
{
#pragma warning disable CS1591
Struct, Void,
Int, UInt,
Float, Double,
Vector2, Vector3, Vector4,
VectorD2, VectorD3, VectorD4,
Matrix2x2, Matrix2x3, Matrix2x4,
Matrix3x2, Matrix3x3, Matrix3x4,
Matrix4x2, Matrix4x3, Matrix4x4,
#pragma warning restore CS1591
}
}
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,7 +9,6 @@ using System.Text;
namespace StgSharp.Graphics
{
public class Uniform<T> where T : struct
{
internal glSHandle id;
@@ -25,10 +25,8 @@ namespace StgSharp.Graphics
{
this.id = id;
}
}
public class Uniform<T, U>
where T : struct
where U : struct
@@ -88,12 +86,10 @@ namespace StgSharp.Graphics
{
this.id = id;
}
}
internal partial class GLonRender : IglFunc
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
@@ -156,8 +152,9 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float f0, float f1)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0; m.Single[3] = f1;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<float>(2, f0);
m.Write<float>(3, f1);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal2fPtr);
}
@@ -179,8 +176,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<float> uniform, float f0)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Single[2] = f0;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<float>(2, f0);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
@@ -188,8 +185,8 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
M128 m = default;
m.UInt64[0] = (ulong)uniform.id.valuePtr;
m.Int32[2] = i0;
m.Write<ulong>(0, (ulong)uniform.id.valuePtr);
m.Write<int>(2, i0);
paramQueue.Enqueue(m);
glActionQueue.Enqueue(UniformVal1fPtr);
}
@@ -205,28 +202,28 @@ namespace StgSharp.Graphics
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1f(*(int*)m.UInt64[0], m.Single[2]);
paramQueue.context->glUniform1f(*(int*)m.Read<uint>(0), m.Read<uint>(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]);
paramQueue.context->glUniform1fv(*(int*)m.Read<ulong>(0), 4, (Vec4d*)&m.vec);
}
private static void UniformVal1i()
{
M128 m = default;
paramQueue.TryDequeue(out m);
paramQueue.context->glUniform1i(*(int*)m.UInt64[0], m.Int32[2]);
paramQueue.context->glUniform1i(*(int*)m.Read<uint>(0), m.Read<int>(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]);
paramQueue.context->glUniform2f(*(int*)m.Read<uint>(0), m.Read<float>(2), m.Read<float>(3));
}
private static void UniformVal3f()
@@ -235,16 +232,16 @@ namespace StgSharp.Graphics
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]);
paramQueue.context->glUniform3f(*(int*)handle, m.Read<float>(0), m.Read<float>(1), m.Read<float>(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]);
paramQueue.context->glUniform4f(*(int*)handle, m.Read<float>(0), m.Read<float>(1), m.Read<float>(2), m.Read<float>(3));
}
}
}
}
@@ -1,4 +1,5 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -746,7 +747,6 @@ namespace StgSharp.Graphics
{
if (!form.IsSingleThread)
{
Console.WriteLine("This context runs on two threads");
form.RenderThread.Start();
}
}
@@ -1001,7 +1001,7 @@ namespace StgSharp.Graphics
contextPtr->glBeginQuery = (delegate*<uint, uint, void>)load("glBeginQuery");
contextPtr->glBindBuffer = (delegate*<uint, uint, void>)load("glBindBuffer");
contextPtr->glBufferData = (delegate*<uint, IntPtr, void*, uint, void>)load("glBufferData");
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->glDeleteQueries = (delegate*<uint, uint*, void>)load("glDeleteQueries");
@@ -1065,7 +1065,7 @@ namespace StgSharp.Graphics
contextPtr->glIsProgram = (delegate*<uint, bool>)load("glIsProgram");
contextPtr->glIsShader = (delegate*<uint, bool>)load("glIsShader");
contextPtr->glLinkProgram = (delegate*<uint, void>)load("glLinkProgram");
contextPtr->glShaderSource = (delegate*<uint, uint, byte*, string, byte*, void>)load("glShaderSource");
contextPtr->glShaderSource = (delegate* unmanaged[Stdcall]<uint, uint, byte**, void*, void>)load("glShaderSource");
contextPtr->glStencilFuncSeparate = (delegate*<uint, uint, int, uint, void>)load("glStencilFuncSeparate");
contextPtr->glStencilMaskSeparate = (delegate*<uint, uint, void>)load("glStencilMaskSeparate");
contextPtr->glStencilOpSeparate = (delegate*<uint, uint, uint, uint, void>)load("glStencilOpSeparate");