daily update on 20240206 by nitload youmu

This commit is contained in:
Nitload
2024-02-06 13:54:30 +08:00
parent 5d778e0f2f
commit 8548757bbf
114 changed files with 86620 additions and 752 deletions
@@ -20,9 +20,6 @@ namespace StgSharp.Graphics
/// <returns>An intptr value representing the pointer to the function</returns>
public delegate IntPtr glLoader(string name);
public delegate void FrameBufferSizeCallback(
IntPtr window, int width, int height);
#region glfwAPI
[UnmanagedFunctionPointer(callingConvention: CallingConvention.StdCall, CharSet = CharSet.Ansi)] internal unsafe delegate GLFWcursor* glfwCreateCursor(GLFWimage* image, int xhot, int yhot);
@@ -43,7 +43,6 @@
{
Fragment = 0x8B30,
Vertex = 0x8B31,
Program,
}
public enum BufferType : int
+8 -2
View File
@@ -55,6 +55,14 @@ namespace StgSharp.Graphics
{
}
protected FrameBufferSizeHandler FrameBufferSizeCallback
{
set
{
gl.SetFrameBufferSizeCallback(this.windowID, value);
}
}
/// <summary>
/// Height of this form, represent in <see cref="int"/>
/// </summary>
@@ -292,7 +300,6 @@ namespace StgSharp.Graphics
windowPtr = InternalIO.glfw.CreateWindow(
width, height, tittleBytes, monitor, sharedForm.windowID);
}
if ((long)windowPtr == -1 || (long)windowPtr == -2)
{
throw new Exception("Cannot create window instance!");
@@ -301,7 +308,6 @@ namespace StgSharp.Graphics
{
windowID = windowPtr;
}
}
InternalIO.InternalSetCurrentGLContext(windowID, contextID);
}
+50 -26
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -12,16 +13,42 @@ using System.Threading.Tasks;
namespace System
{
/// <summary>
/// A handle to an OpenGL object,
/// must be indexed from a <see cref="glHandleSet"/>.
/// Direct call will cause <see langword="InvalidOperationException"/>
/// </summary>
public unsafe struct glHandle
{
internal uint* valuePtr;
public glHandle()
{
throw new InvalidOperationException();
}
internal glHandle(uint* val)
{
valuePtr = val;
}
public uint Value
{
get { return *valuePtr; }
}
}
/// <summary>
/// A handle to an OpenGL object.
/// </summary>
public unsafe class glHandle
public unsafe class glHandleSet
{
private readonly uint[] _value;
internal int length;
internal uint* handle;
public uint this[int index]
public glHandle this[int index]
{
get
{
@@ -29,39 +56,27 @@ namespace System
{
throw new IndexOutOfRangeException();
}
return *(handle + index);
}
set
{
if (index > length)
{
throw new IndexOutOfRangeException();
}
*(handle + index) = value;
return new glHandle(handle + index);
}
}
public glHandle(int count)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetValue(int index, uint value)
{
_value[index] = value;
}
public glHandleSet(int count)
{
_value = new uint[count];
length = count;
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
internal glHandle(uint[] val)
{
_value = val;
length = val.Length;
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
internal glHandle(uint val)
{
_value = [val];
length = 1;
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
/// <summary>
/// temporarily release the handle of GLhandle sets.
/// Please call <see cref="Pin"/> method before indexing.
/// </summary>
public void Free()
{
if (_value == null)
@@ -72,6 +87,15 @@ namespace System
h.Free();
}
public void Pin()
{
if (_value == null)
{
return;
}
handle = (uint*)GCHandle.Alloc(_value, GCHandleType.Pinned).AddrOfPinnedObject();
}
/// <summary>
/// Realse all resource in this handle.
/// This will release all resource in this <see cref="glHandle"/>.
+20 -14
View File
@@ -21,6 +21,12 @@ namespace StgSharp.Graphics
{
api = context;
}
void IglFunc.SwitchContext()
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ActiveTexture(uint type)
{
@@ -46,7 +52,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(program[0], shader[0]);
api->glAttachShader(program.value, shader.value);
}
void IglFunc.BindBuffer(BufferType bufferType, uint index)
@@ -132,10 +138,10 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe glHandle IglFunc.CreateProgram(int count)
unsafe glHandleSet IglFunc.CreateProgram(int count)
{
glHandle h = new glHandle(count);
uint* p = h.handle;
glHandleSet h = new glHandleSet(count);
glHandle* p = h.handle;
for (int i = 0; i < count; i++)
{
*(p + i) = api->glCreateProgram();
@@ -181,16 +187,16 @@ namespace StgSharp.Graphics
api->glDeleteBuffers(1, &handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffers(glHandle handle)
unsafe void IglFunc.DeleteBuffers(glHandleSet handle)
{
api->glDeleteBuffers((uint)handle.length, handle.handle);
api->glDeleteBuffers((uint)handle.length, (uint*)handle.handle);
handle.Free();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteVertexArrays(glHandle arrays)
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
api->glDeleteVertexArrays((uint)arrays.length, arrays.handle);
api->glDeleteVertexArrays((uint)arrays.length, (uint*)arrays.handle);
arrays.Free();
}
@@ -220,20 +226,20 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenBuffers(uint count, glHandle handle)
unsafe void IglFunc.GenBuffers(uint count, glHandleSet handle)
{
api->glGenBuffers(count, handle.handle);
api->glGenBuffers(count, (uint*)handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenTextures(uint count, glHandle handle)
unsafe void IglFunc.GenTextures(uint count, glHandleSet handle)
{
api->glGenTextures(count, handle.handle);
api->glGenTextures(count, (uint*)handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenVertexArrays(uint count, glHandle handle)
unsafe void IglFunc.GenVertexArrays(uint count, glHandleSet handle)
{
api->glGenVertexArrays(count, handle.handle);
api->glGenVertexArrays(count, (uint*)handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
+13 -7
View File
@@ -16,6 +16,7 @@ namespace StgSharp.Graphics
FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
private GLonTimeout() { }
void IglFunc.ActiveTexture(uint type) => throw signal;
@@ -38,12 +39,12 @@ namespace StgSharp.Graphics
void IglFunc.ClearGraphicError() => throw signal;
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType) => throw signal;
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode) => throw signal;
glHandle IglFunc.CreateProgram() => throw signal;
glHandle IglFunc.CreateProgram(int count) => throw signal;
glHandleSet IglFunc.CreateProgram(int count) => throw signal;
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share) => throw signal;
@@ -52,21 +53,21 @@ namespace StgSharp.Graphics
public void DeleteBuffer(uint handle) => throw signal;
void IglFunc.DeleteBuffers(glHandle handle) => throw signal;
void IglFunc.DeleteBuffers(glHandleSet handle) => throw signal;
void IglFunc.DeleteVertexArrays(glHandle handle) => throw signal;
void IglFunc.DeleteVertexArrays(glHandleSet handle) => throw signal;
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr) => throw signal;
void IglFunc.GenBuffers(uint count, glHandle ptr) => throw signal;
void IglFunc.GenBuffers(uint count, glHandleSet handle) => throw signal;
void IglFunc.GenTextures(uint count, glHandle handle) => throw signal;
void IglFunc.GenTextures(uint count, glHandleSet handle) => throw signal;
void IglFunc.GenVertexArrays(uint count, glHandle handle) => throw signal;
void IglFunc.GenVertexArrays(uint count, glHandleSet handle) => throw signal;
void IglFunc.GetGraphicError() => throw signal;
@@ -112,5 +113,10 @@ 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;
}
}
}
+36 -65
View File
@@ -16,15 +16,16 @@ namespace StgSharp.Graphics
internal unsafe partial class GLonUpdate : IglFunc
{
internal GLonUpdateInternal glUpdateInternal;
private ConcurrentQueue<IntPtr> glActionQueue;
private GLparamQueue paramQueue;
private static GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
internal GLonUpdate(GLcontext* context)
{
glUpdateInternal = new GLonUpdateInternal();
paramQueue = glUpdateInternal.paramQueue;
if (paramQueue == null)
{
paramQueue = new GLparamQueue();
}
glActionQueue = new ConcurrentQueue<IntPtr>();
}
@@ -38,7 +39,7 @@ namespace StgSharp.Graphics
void IglFunc.ActiveTexture(uint type)
{
paramQueue.Enqueue(type);
glActionQueue.Enqueue(glUpdateInternal.ActiveTexturePtr);
glActionQueue.Enqueue(ActiveTexturePtr);
}
@@ -61,15 +62,15 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.AttachShader(glHandle program, glHandle shader)
{
paramQueue.Enqueue(program[0], shader[0]);
glActionQueue.Enqueue(glUpdateInternal.AttachShaderPtr);
paramQueue.Enqueue(program, shader);
glActionQueue.Enqueue(AttachShaderPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
paramQueue.Enqueue((uint)bufferType, index);
glActionQueue.Enqueue(glUpdateInternal.BindBufferPtr
glActionQueue.Enqueue(BindBufferPtr
);
}
@@ -100,67 +101,37 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearColor(float R, float G, float B, float A)
{
glActionQueue.Enqueue(
() => api->glClearColor(R, G, B, A)
);
glActionQueue.Enqueue(ClearColorPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearGraphicError()
{
glActionQueue.Enqueue(
() =>
{
while (api->glGetError() != 0) { };
}
);
glActionQueue.Enqueue(ClearGraphicErrorPtr);
}
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
//TODO:双线程,换运行环境
void IglFunc.SwitchContext()
{
glHandle h = new glHandle(1);
glActionQueue.Enqueue(
() =>
{
uint shaderID;
shaderCode += '\0';
shaderID = InternalIO.InternalLoadShaderCode(shaderCode, (int)shaderType);
if (shaderID == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
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);
}
Interlocked.Add(ref id, (int)(shaderID + 1));
}
);
return h;
glHandleSet IglFunc.CompileShader(ShaderType shaderType, params string[] shaderCode)
{
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());
}
glActionQueue.Enqueue(CompileShaderPtr);
return shaderHandle;
}
@@ -179,7 +150,7 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreateProgram(int count)
glHandleSet IglFunc.CreateProgram(int count)
{
throw new NotImplementedException();
}
@@ -235,7 +206,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.DeleteBuffers(glHandle handle)
unsafe void IglFunc.DeleteBuffers(glHandleSet handle)
{
glActionQueue.Enqueue(
() =>
@@ -249,7 +220,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.DeleteVertexArrays(glHandle arrays)
unsafe void IglFunc.DeleteVertexArrays(glHandleSet arrays)
{
glActionQueue.Enqueue(
() =>
@@ -290,7 +261,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.GenBuffers(uint count, glHandle handle)
unsafe void IglFunc.GenBuffers(uint count, glHandleSet handle)
{
glActionQueue.Enqueue(
() =>
@@ -299,7 +270,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.GenTextures(uint count, glHandle handle)
unsafe void IglFunc.GenTextures(uint count, glHandleSet handle)
{
glActionQueue.Enqueue(
() =>
@@ -308,7 +279,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.GenVertexArrays(uint count, glHandle ptr)
unsafe void IglFunc.GenVertexArrays(uint count, glHandleSet ptr)
{
glActionQueue.Enqueue(
() =>
@@ -1,4 +1,5 @@
using StgSharp.Math;
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -10,414 +11,290 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal unsafe struct GLonUpdateInternal : IglFunc
internal unsafe partial class GLonUpdate : IglFunc
{
public IntPtr ActiveTexturePtr;
public IntPtr AttachShaderPtr;
public IntPtr BindBufferPtr;
internal IntPtr ClearColorPtr;
internal IntPtr ClearGraphicErrorPtr;
internal IntPtr CompileShaderPtr;
internal GLparamQueue paramQueue;
private GLcontext* gl;
public GLonUpdateInternal(GLcontext* api)
{
gl = api;
}
internal IntPtr ActiveTexturePtr = (IntPtr)(delegate*<void>)&InternalActiveTexture;
internal IntPtr AttachShaderPtr = (IntPtr)(delegate*<void>)&InternalAttachShader;
internal IntPtr BindBufferPtr = (IntPtr)(delegate*<void>)&InternalBindBuffer;
internal IntPtr ClearColorPtr = (IntPtr)(delegate*<void>)&InternalClearColor;
internal IntPtr ClearGraphicErrorPtr = (IntPtr)(delegate*<void>)&InternalClearGraphicError;
internal IntPtr CompileShaderPtr = (IntPtr)(delegate*<void>)&InternalCompileShader;
private IntPtr ClearPtr = (IntPtr)(delegate*<void>)&InternalClear;
public void ActivePointer()
{
IglFunc gl = this;
ActiveTexturePtr = Marshal.GetFunctionPointerForDelegate(gl.ActiveTexture);
AttachShaderPtr = Marshal.GetFunctionPointerForDelegate(gl.AttachShader);
BindBufferPtr = Marshal.GetFunctionPointerForDelegate(gl.BindBuffer);
CompileShaderPtr = Marshal.GetFunctionPointerForDelegate(gl.CompileShader);
}
void IglFunc.ActiveTexture(uint type)
private static void InternalActiveTexture()
{
uint t;
paramQueue.TryDequeue(out t);
gl->glActiveTexture(t);
paramQueue.context->glActiveTexture(t);
}
void IglFunc.AfterAlign(Form form, Action action)
{
throw new NotImplementedException();
}
void IglFunc.AlignFrame(Form form)
{
throw new NotImplementedException();
}
void IglFunc.AttachShader(glHandle program, glHandle shader)
private static void InternalAttachShader()
{
uint p, s;
paramQueue.TryDequeue(out p, out s);
gl->glAttachShader(p, s);
paramQueue.context->glAttachShader(p, s);
}
void IglFunc.BindBuffer(BufferType bufferType, uint index)
private static void InternalBindBuffer()
{
uint t, i;
paramQueue.TryDequeue(out t, out i);
gl->glBindBuffer(t, i);
paramQueue.context->glBindBuffer(t, i);
}
void IglFunc.BindTexture(uint type, uint texture)
{
uint t, te;
paramQueue.TryDequeue(out t, out te);
gl->glBindTexture(t, te);
}
void IglFunc.BindVertexArray(uint index)
{
uint i;
paramQueue.TryDequeue(out i);
gl->glBindVertexArray(i);
}
void IglFunc.Clear(MaskBufferBit mask)
private static void InternalClear()
{
uint mt;
paramQueue.TryDequeue(out mt);
gl->glClear(mt);
paramQueue.context->glClear(mt);
}
void IglFunc.ClearColor(float R, float G, float B, float A)
private static void InternalClearColor()
{
float r, g, b, a;
paramQueue.TryDequeue(out r, out g, out b, out a);
gl->glClearColor(r, g, b, a);
paramQueue.context->glClearColor(r, g, b, a);
}
void IglFunc.ClearGraphicError()
private static void InternalClearGraphicError()
{
while (gl->glGetError() != 0) { }
while (paramQueue.context->glGetError() != 0) { }
}
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
private static void InternalCompileShader()
{
IntPtr shaderHandle, codeHandle;
paramQueue.TryDequeue(out codeHandle, out shaderHandle);
uint shaderID;
shaderCode += '\0';
shaderID = InternalIO.InternalLoadShaderCode(shaderCode, (int)shaderType);
if (shaderID == 0)
int count, type;
paramQueue.TryDequeue(out type, out count);
for (int i = 0; i < count; i++)
{
IntPtr logPtr = InternalIO.InternalReadLog();
string log;
try
paramQueue.TryDequeue(out shaderHandle, out codeHandle);
*(uint*)shaderHandle = InternalIO.InternalLoadShaderCode((byte*)codeHandle, type);
if (*(uint*)shaderHandle == 0)
{
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)
IntPtr logPtr = InternalIO.InternalReadLog();
string log;
try
{
if (!char.IsControl(item))
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)
{
isUseful = true; break;
if (!char.IsControl(item))
{
isUseful = true; break;
}
}
if (!isUseful)
{
throw new Exception();
}
}
if (!isUseful)
catch (Exception)
{
throw new Exception();
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
}
}
catch (Exception)
{
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
InternalIO.InternalWriteLog(log, LogType.Error);
}
GCHandle.FromIntPtr(codeHandle).Free();
}
*(uint*)shaderHandle = shaderID;
}
private void InternalAfterAlign(Form form, Action action)
{
throw new NotImplementedException();
}
private void InternalAlignFrame(Form form)
{
throw new NotImplementedException();
}
private void InternalBindTexture(uint type, uint texture)
{
uint t, te;
paramQueue.TryDequeue(out t, out te);
paramQueue.context->glBindTexture(t, te);
}
private void InternalBindVertexArray(uint index)
{
uint i;
paramQueue.TryDequeue(out i);
paramQueue.context->glBindVertexArray(i);
}
private glHandle InternalCreateProgram()
{
IntPtr programHandle = default;
paramQueue.TryDequeue(out programHandle);
*(uint*)programHandle = paramQueue.context->glCreateProgram();
return default;
}
glHandle IglFunc.CreateProgram()
private glHandleSet InternalCreateProgram(int count)
{
throw new NotImplementedException();
}
glHandle IglFunc.CreateProgram(int count)
private IntPtr InternalCreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
throw new NotImplementedException();
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
private glHandle InternalCreatGraphicProgram()
{
throw new NotImplementedException();
}
glHandle IglFunc.CreatGraphicProgram()
private void InternalDeleteBuffer(uint handle)
{
throw new NotImplementedException();
}
void IglFunc.DeleteBuffer(uint handle)
private void InternalDeleteBuffers(glHandleSet handle)
{
throw new NotImplementedException();
}
void IglFunc.DeleteBuffers(glHandle handle)
private void InternalDeleteVertexArrays(glHandleSet arrays)
{
throw new NotImplementedException();
}
void IglFunc.DeleteVertexArrays(glHandle arrays)
private void InternalDrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
{
throw new NotImplementedException();
}
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
private void InternalGenBuffers(uint count, glHandleSet ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenBuffers(uint count, glHandle ptr)
private void InternalGenTextures(uint count, glHandleSet ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenTextures(uint count, glHandle ptr)
private void InternalGenVertexArrays(uint count, glHandleSet ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenVertexArrays(uint count, glHandle ptr)
private void InternalGetGraphicError()
{
throw new NotImplementedException();
}
void IglFunc.GetGraphicError()
private int InternalGetUniformLocation(uint program, byte* ptr)
{
throw new NotImplementedException();
}
int IglFunc.GetUniformLocation(uint program, byte* ptr)
private bool InternalGetWindowShouldClose(Form form)
{
throw new NotImplementedException();
}
bool IglFunc.GetWindowShouldClose(Form form)
private void InternalMakeContexCurrent(Form form)
{
throw new NotImplementedException();
}
void IglFunc.MakeContexCurrent(Form form)
private void InternalSetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
throw new NotImplementedException();
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
private void InternalSetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
throw new NotImplementedException();
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
private void InternalSetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
throw new NotImplementedException();
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
private void InternalSetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
private void InternalSetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
private void InternalSetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
private void InternalSetValue(Uniform<float, float> uniform, float v0, float v1)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
private void InternalSetValue(Uniform<float> uniform, float v0)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float> uniform, float v0)
private void InternalSetValue(Uniform<int> uniform, int i0)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<int> uniform, int i0)
private void InternalSetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
private void InternalSetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
throw new NotImplementedException();
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
private void InternalSetWindowShouldClose(Form form, bool mark)
{
throw new NotImplementedException();
}
void IglFunc.SetWindowShouldClose(Form form, bool mark)
private void InternalSwapBuffers(Form form)
{
throw new NotImplementedException();
}
void IglFunc.SwapBuffers(Form form)
private void InternalSwitchContext()
{
throw new NotImplementedException();
}
void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
private void InternalTextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
{
throw new NotImplementedException();
}
void IglFunc.TextureParameter(uint target, uint pname, int param)
private void InternalTextureParameter(uint target, uint pname, int param)
{
throw new NotImplementedException();
}
void IglFunc.UseProgram(uint program)
private void InternalUseProgram(uint program)
{
throw new NotImplementedException();
}
void IglFunc.Viewport(int x, int y, uint width, uint height)
private void InternalViewport(int x, int y, uint width, uint height)
{
throw new NotImplementedException();
}
}
internal unsafe class GLparamQueue
{
private ConcurrentQueue<M128> paramQueue;
public GLparamQueue()
{
paramQueue = new ConcurrentQueue<M128>();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(Vec4d v)
{
paramQueue.Enqueue(v);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(uint u0)
{
M128 m = default;
m.UInt32[0] = u0;
paramQueue.Enqueue(m);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(uint u0, uint u1)
{
M128 m = default;
m.UInt32[0] = u0;
m.UInt32[1] = u1;
paramQueue.Enqueue(m);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(float f0, float f1, float f2, float f3)
{
M128 m = default;
m.Single[0] = f0;
m.Single[1] = f1;
m.Single[2] = f2;
m.Single[3] = f3;
paramQueue.Enqueue(m);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Enqueue(IntPtr p0, IntPtr p1)
{
M128 m = default;
m.IntPtr0 = p0;
m.IntPtr1 = p1;
paramQueue.Enqueue(m);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDequeue(out IntPtr p0, out IntPtr p1)
{
M128 ret;
if (paramQueue.TryDequeue(out ret))
{
p0 = ret.IntPtr0;
p1 = ret.IntPtr1;
}
else
{
InternalIO.InternalWriteLog("No param in buffer when calling gl function.", LogType.Error);
throw new Exception();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDequeue(out float f0, out float f1, out float f2, out float f3)
{
M128 ret;
if (paramQueue.TryDequeue(out ret))
{
f0 = ret.UInt32[0];
f1 = ret.UInt32[1];
f2 = ret.UInt32[2];
f3 = ret.UInt32[3];
}
else
{
InternalIO.InternalWriteLog("No param in buffer when calling gl function.", LogType.Error);
throw new Exception();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDequeue(out uint u0)
{
M128 ret;
if (paramQueue.TryDequeue(out ret))
{
u0 = ret.UInt32[0];
}
else
{
InternalIO.InternalWriteLog("No param in buffer when calling gl function.", LogType.Error);
throw new Exception();
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void TryDequeue(out uint u0, out uint u1)
{
M128 ret;
if (paramQueue.TryDequeue(out ret))
{
u0 = ret.UInt32[0];
u1 = ret.UInt32[1];
}
else
{
InternalIO.InternalWriteLog("No param in buffer when calling gl function.", LogType.Error);
throw new Exception();
}
}
}
}
@@ -14,34 +14,23 @@ namespace StgSharp.Graphics
/// </summary>
/// <returns>New instance of <see cref="Shader"/> instance. </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected Shader CreateShader()
protected ShaderProgram CreateShaderProgram(ShaderType shaderType)
{
return new Shader(GL);
return new ShaderProgram(GL, 1);
}
}
public class Shader
{
internal readonly glHandle Handle;
internal readonly glHandle handle;
public readonly ShaderType type;
private IglFunc GL;
private readonly IglFunc GL;
/// <summary>
/// Inti a shader with no program attached.
/// </summary>
internal unsafe Shader(IglFunc gl, ShaderType t, int count)
{
GL = gl;
type = t;
if (type == ShaderType.Program)
{
Handle = GL.CreateProgram();
}
}
internal void ChangeEnvironment(IglFunc gl)
internal Shader(IglFunc gl, glHandle handle)
{
this.handle = handle;
GL = gl;
}
@@ -49,20 +38,46 @@ namespace StgSharp.Graphics
/// Compile the vertex shader to be attached to this shader.
/// </summary>
/// <param name="shaderCode">GLSL code of vertex shader.</param>
public unsafe void CompileShader(string shaderCode)
public unsafe void Compile(string shaderCode, ShaderType usage, int index)
{
vertexHandle = GL.CompileShader(shaderCode, type);
GL.AttachShader(programHandle, vertexHandle);
handle.handle[index] = GL.CompileShader(shaderCode, type).handle;
}
//TODO:写链接着色器的文档注释
/// <summary>
/// Compile the fragment shader to be attached to this shader.
///
/// </summary>
/// <param name="shaderCode">GLSL code of fragment shader.</param>
public unsafe void CompileFragmentShader(string shaderCode)
/// <param name="target"></param>
/// <param name="usage"></param>
public void AttachTo(ShaderProgram target, ShaderType usage)
{
fragmentHandle = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
GL.AttachShader(programHandle, fragmentHandle);
switch (usage)
{
case ShaderType.Fragment:
break;
case ShaderType.Vertex:
GL.AttachShader(target.handle,)
break;
default:
throw new ArgumentException();
}
}
}
public class ShaderProgram
{
internal readonly glHandle handle;
private readonly IglFunc GL;
/// <summary>
/// Inti a shader with no program attached.
/// </summary>
internal unsafe ShaderProgram(IglFunc gl, int count)
{
GL = gl;
handle = GL.CreateProgram();
}
/// <summary>
@@ -70,7 +85,7 @@ namespace StgSharp.Graphics
/// </summary>
public void Link()
{
if (InternalIO.InternalLinkShaderProgram(programHandle) == 0)
if (InternalIO.InternalLinkShaderProgram(handle[0]) == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
try
@@ -100,7 +115,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programHandle, ptr);
uniform = GL.GetUniformLocation(handle[0], ptr);
}
if (uniform != -1)
{
@@ -108,7 +123,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
throw new Exception($"Cannot find the uniform {name} in shader {handle[0]}");
}
}
/// <summary>
@@ -125,7 +140,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programHandle, ptr);
uniform = GL.GetUniformLocation(handle[0], ptr);
}
if (uniform != -1)
{
@@ -133,7 +148,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
throw new Exception($"Cannot find the uniform {name} in shader {handle[0]}");
}
}
/// <summary>
@@ -142,7 +157,7 @@ namespace StgSharp.Graphics
/// </summary>
/// <param name="name">Name of the Uniform in shader code</param>
/// <returns></returns>
public unsafe Uniform<T, U, V> GetUniform<T, U, V>(string name)
public unsafe Uniform<T, U, V> GetUniform<T, U, V>(int index, string name)
where T : struct
where U : struct
where V : struct
@@ -151,7 +166,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programHandle, ptr);
uniform = GL.GetUniformLocation(handle[index], ptr);
}
if (uniform != -1)
{
@@ -159,7 +174,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
throw new Exception($"Cannot find the uniform {name} in shader {handle[index]}");
}
}
@@ -167,9 +182,9 @@ namespace StgSharp.Graphics
/// Let the current context use this shader.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Use()
public unsafe void Use(int index)
{
GL.UseProgram(programHandle);
GL.UseProgram(handle[index]);
}
}
+16 -21
View File
@@ -91,7 +91,7 @@ namespace StgSharp.Graphics
/// <param name="shaderType">An <see cref="ShaderType"/> enum represent the type of the shader to
/// conpile, including vertex shader and fragment shader.</param>
/// <returns>An handle of conpiled shader</returns>
glHandle CompileShader(string shaderCode, ShaderType shaderType);
glHandleSet CompileShader(ShaderType shaderType, params string[] shaderCode);
/// <summary>
/// Same function as OpenGL api, glCreateProgram
@@ -104,7 +104,7 @@ namespace StgSharp.Graphics
/// Used for creating multiple shader program.
/// </summary>
/// <returns>A handle representing all shader programs</returns>
glHandle CreateProgram(int count);
glHandleSet CreateProgram(int count);
/// <summary>
/// Creates a form and its associated
@@ -128,14 +128,14 @@ namespace StgSharp.Graphics
/// <summary>
/// Same function as OpenGL api, glDeleteBuffer. This method can delete a sets of buffers in one call.
/// </summary>
/// <param name="handle">Array of buffers to be deleted</param>
unsafe void DeleteBuffers(glHandle handle);
/// <param name="handleSet">Array of buffers to be deleted</param>
unsafe void DeleteBuffers(glHandleSet handleSet);
/// <summary>
/// Same function as OpenGL api, glDeleteVertexArrays
/// </summary>
/// <param name="handle">Handle of vertex objects.</param>
unsafe void DeleteVertexArrays(glHandle handle);
/// <param name="handleSet">Handle of vertex objects.</param>
unsafe void DeleteVertexArrays(glHandleSet handleSet);
/// <summary>
///
@@ -150,22 +150,22 @@ namespace StgSharp.Graphics
/// Same fucntion as the OpenGL api glGenBuffers
/// </summary>
/// <param name="count"></param>
/// <param name="ptr"></param>
unsafe void GenBuffers(uint count, glHandle ptr);
/// <param name="handleSet"></param>
unsafe void GenBuffers(uint count, glHandleSet handleSet);
/// <summary>
/// Same fucnction as OpenGL api glGenTextures.
/// </summary>
/// <param name="count">amount of textures to generate</param>
/// <param name="handle">Handle of texture</param>
unsafe void GenTextures(uint count, glHandle handle);
/// <param name="handleSet">Handle of texture</param>
unsafe void GenTextures(uint count, glHandleSet handleSet);
/// <summary>
/// Same fucntion as OpenGL api, glGenVertexArray
/// </summary>
/// <param name="count">Amount of vao to be generated</param>
/// <param name="handle">Handle of vao sign array</param>
unsafe void GenVertexArrays(uint count, glHandle handle);
/// <param name="handleSet">Handle of vao sign array</param>
unsafe void GenVertexArrays(uint count, glHandleSet handleSet);
void GetGraphicError();
@@ -189,13 +189,7 @@ namespace StgSharp.Graphics
void SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible;
/// <summary>
/// sets the framebuffer resize callback of the specified <see cref="Form"/>,
///which is called when the framebuffer of the specified <see cref="Form"/> is resized.
/// </summary>
/// <param name="id">The only signiture stands for the <see cref="Form"/> to be controlled</param>
/// <param name="handler">Method hadler </param>
void SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler);
internal void SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler);
/// <summary>
/// Set data value to a float <see cref="Uniform{T,U,V,W}"/>,
@@ -270,6 +264,8 @@ namespace StgSharp.Graphics
/// <param name="pointer"></param>
void SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer);
void SetWindowShouldClose(Form form, bool mark);
void SwapBuffers(Form form);
/// <summary>
@@ -308,7 +304,6 @@ namespace StgSharp.Graphics
/// <param name="height"></param>
void Viewport(int x, int y, uint width, uint height);
void SetWindowShouldClose(Form form, bool mark);
internal void SwitchContext();
}
}