daily update on 20240203 by nitloadd youmu. New glhandle setup.

This commit is contained in:
Nitload
2024-02-03 23:20:50 +08:00
parent 8148c10acd
commit 5d778e0f2f
258 changed files with 10025 additions and 1220 deletions
+7 -22
View File
@@ -7,19 +7,7 @@ using System.Text;
namespace StgSharp.Graphics
{
public partial class Form
{
/// <summary>
/// Create a set of <see cref="ElementBuffer"/>.
/// </summary>
/// <param name="count">Amount of EBO to be created.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ElementBuffer CreateElementBuffer(uint count)
{
return new ElementBuffer(count, this.gl);
}
}
/// <summary>
/// A collecion of handle to Element Buffer Object in OpenGL.
@@ -30,28 +18,25 @@ namespace StgSharp.Graphics
{
internal ElementBuffer(uint n, IglFunc gl)
{
_bufferID = new uint[n];
_bufferHandle = new glHandle((int)n);
GL = gl;
fixed (uint* id = _bufferID)
{
GL.GenBuffers(n, id);
}
GL.GenBuffers(n, _bufferHandle);
}
public uint this[int n]
{
get { return _bufferID[n]; }
get { return _bufferHandle[n]; }
}
public override void Bind(int index)
{
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
}
public void SetValue<T>(int index, T[] bufferArray, BufferUsage usage)
where T : struct, IConvertible
{
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
}
@@ -59,7 +44,7 @@ namespace StgSharp.Graphics
{
if (disposing)
{
GL.DeleteBuffers((uint)_bufferID.Length, _bufferID);
GL.DeleteBuffers(_bufferHandle);
}
}
}
@@ -11,7 +11,7 @@ namespace StgSharp.Graphics
/// </summary>
public abstract class IBufferObject : IDisposable
{
protected uint[] _bufferID;
protected glHandle _bufferHandle;
protected IglFunc GL;
/// <summary>
@@ -21,7 +21,7 @@ namespace StgSharp.Graphics
/// <returns></returns>
public uint this[int index]
{
get { return _bufferID[index]; }
get { return _bufferHandle[index]; }
}
/// <summary>
+4 -20
View File
@@ -5,36 +5,20 @@ using System.Text;
namespace StgSharp.Graphics
{
public partial class Form
{
/// <summary>
/// Create a set of <see cref="VertexArray"/>.
/// </summary>
/// <param name="count">Amount of VAO to be crated.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected VertexArray CreateVertexArray(uint count)
{
return new VertexArray(count, gl);
}
}
public unsafe class VertexArray : IBufferObject
{
internal VertexArray(uint n, IglFunc gl)
{
_bufferID = new uint[n];
_bufferHandle = new glHandle((int)n);
GL = gl;
fixed (uint* id = _bufferID)
{
GL.GenVertexArrays(n, id);
}
GL.GenVertexArrays(n, _bufferHandle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Bind(int index)
{
GL.BindVertexArray(_bufferID[index]);
GL.BindVertexArray(_bufferHandle[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -56,7 +40,7 @@ namespace StgSharp.Graphics
{
if (disposing)
{
GL.DeleteVertexArrays((uint)_bufferID.Length, _bufferID);
GL.DeleteVertexArrays(_bufferHandle);
}
}
}
+25 -45
View File
@@ -9,39 +9,14 @@ using System.Text;
namespace StgSharp.Graphics
{
public partial class Form
{
/// <summary>
/// Create aset of <see cref="VertexBuffer"/>.
/// </summary>
/// <param name="count">Amount of Object to be created.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected VertexBuffer CreateVertexBuffer(uint count)
{
return new VertexBuffer(count, gl);
}
}
public unsafe class VertexBuffer : IBufferObject
{
internal VertexBuffer(uint n, IglFunc glFunc)
{
_bufferID = new uint[n];
_bufferHandle = new glHandle((int)n);
GL = glFunc;
fixed (uint* ptr = _bufferID)
{
GL.GenBuffers(n, ptr);
}
}
/// <summary>
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, 0);
GL.GenBuffers(n, _bufferHandle);
}
public sealed override void Bind(int index)
@@ -49,22 +24,6 @@ namespace StgSharp.Graphics
GL.BindBuffer(BufferType.ARRAY_BUFFER, (uint)index);
}
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
protected sealed override void Dispose(bool disposing)
{
if (disposing)
{
fixed (uint* id = _bufferID)
{
GL.DeleteBuffers((uint)_bufferID.Length, _bufferID);
}
}
}
/// <summary>
/// Set data to current vertex buffer object
/// </summary>
@@ -74,7 +33,7 @@ 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
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
}
@@ -88,9 +47,30 @@ namespace StgSharp.Graphics
/// <param name="usage">How OpenGL use these data, defined by <see cref="BufferUsage"/></param>
public void SetValueArray<T>(int index, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferHandle[index]);
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferArray, usage);
}
/// <summary>
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, 0);
}
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
protected sealed override void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferHandle);
}
}
}
}
@@ -9,47 +9,37 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
public partial class Form
{
protected Texture CreateTexture(uint n)
{
return new Texture(n, this.GL);
}
}
public unsafe class Texture
{
internal uint[] textureId;
internal Image[] sourceList;
internal static bool[] unitActivated = new bool[16];
internal glHandle _textureHandle;
internal Image[] sourceList;
private IglFunc GL;
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
internal Texture(uint n, IglFunc gl)
{
textureId = new uint[n];
_textureHandle = new glHandle((int)n);
sourceList = new Image[n];
GL = gl;
fixed (uint* id = textureId)
{
GL.GenTextures(n, id);
}
GL.GenTextures(n, _textureHandle);
}
public void Activate(TextureUnit unit)
{
GL.ActiveTexture((uint)unit);
}
public void Bind2D(int index)
{
GL.BindTexture(GLconst.TEXTURE_2D, textureId[index]);
GL.BindTexture(GLconst.TEXTURE_2D, _textureHandle[index]);
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="index"></param>
/// <param name="loader"></param>
/// <param name="name"> </param>
/// <param name="index"> </param>
/// <param name="loader"> </param>
public unsafe void LoadTexture(int index, string name, ImageLoader loader)
{
sourceList[index] = new Image();
@@ -87,6 +77,11 @@ namespace StgSharp.Graphics
}
}
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
#region texture set
public void Set2dFilterProperty(
@@ -103,13 +98,17 @@ namespace StgSharp.Graphics
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_T, (int)onVertical);
}
#endregion
public void Activate(TextureUnit unit)
{
GL.ActiveTexture((uint)unit);
}
#endregion texture set
}
public enum Filter
{
Nearest = GLconst.NEAREST,
Linear = GLconst.LINEAR,
Nearest_LinearMipmap = GLconst.NEAREST_MIPMAP_LINEAR,
Linear_LinearMipmap = GLconst.LINEAR_MIPMAP_LINEAR,
Nearest_NearestMipmap = GLconst.NEAREST_MIPMAP_NEAREST,
Linear_NearestMipmap = GLconst.LINEAR_MIPMAP_NEAREST,
}
public enum TextureUnit
@@ -139,15 +138,4 @@ namespace StgSharp.Graphics
ClampToEdge = GLconst.CLAMP_TO_EDGE,
ClampToBorder = GLconst.CLAMP_TO_BORDER,
}
public enum Filter
{
Nearest = GLconst.NEAREST,
Linear = GLconst.LINEAR,
Nearest_LinearMipmap = GLconst.NEAREST_MIPMAP_LINEAR,
Linear_LinearMipmap = GLconst.LINEAR_MIPMAP_LINEAR,
Nearest_NearestMipmap = GLconst.NEAREST_MIPMAP_NEAREST,
Linear_NearestMipmap = GLconst.LINEAR_MIPMAP_NEAREST,
}
}
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
namespace System
{
/// <summary>
/// A handle to an OpenGL object.
/// </summary>
public unsafe class glHandle
{
private readonly uint[] _value;
internal int length;
internal uint* handle;
public uint this[int index]
{
get
{
if (index > length)
{
throw new IndexOutOfRangeException();
}
return *(handle + index);
}
set
{
if (index > length)
{
throw new IndexOutOfRangeException();
}
*(handle + index) = value;
}
}
public glHandle(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();
}
public void Free()
{
if (_value == null)
{
return;
}
GCHandle h = GCHandle.FromIntPtr((IntPtr)handle);
h.Free();
}
/// <summary>
/// Realse all resource in this handle.
/// This will release all resource in this <see cref="glHandle"/>.
/// This <see langword="glHandle"/> will be not usable.
/// </summary>
public void Release()
{
if (_value == null)
{
return;
}
else
{
GCHandle.FromIntPtr((IntPtr)handle).Free();
handle = (uint*)IntPtr.Zero;
}
}
}
}
+36 -22
View File
@@ -3,6 +3,7 @@ using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
@@ -43,9 +44,9 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.AttachShader(uint program, uint shader)
void IglFunc.AttachShader(glHandle program, glHandle shader)
{
api->glAttachShader(program, shader);
api->glAttachShader(program[0], shader[0]);
}
void IglFunc.BindBuffer(BufferType bufferType, uint index)
@@ -83,12 +84,12 @@ namespace StgSharp.Graphics
while (api->glGetError() != 0) { }
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
uint shaderID;
shaderCode += '\0';
shaderID = InternalIO.InternalLoadShaderCode(shaderCode, (int)shaderType);
if (shaderID == 0)
if (shaderID == 0) //compile error handler
{
IntPtr logPtr = InternalIO.InternalReadLog();
string log;
@@ -114,19 +115,32 @@ namespace StgSharp.Graphics
}
catch (Exception)
{
log = "Failed to compile shader.\nFailed to get GLSL compiler log";
log = "Failed to compile shader.\n" +
"Failed to get GLSL compiler log";
}
InternalIO.InternalWriteLog(log, LogType.Error);
}
return shaderID;
return new glHandle(shaderID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
uint IglFunc.CreateProgram()
glHandle IglFunc.CreateProgram()
{
return api->glCreateProgram();
return new glHandle(api->glCreateProgram());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe glHandle IglFunc.CreateProgram(int count)
{
glHandle h = new glHandle(count);
uint* p = h.handle;
for (int i = 0; i < count; i++)
{
*(p + i) = api->glCreateProgram();
}
return h;
}
unsafe IntPtr IglFunc.CreateWindowHandle(
@@ -156,9 +170,9 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe uint IglFunc.CreatGraphicProgram()
unsafe glHandle IglFunc.CreatGraphicProgram()
{
return api->glCreateProgram();
return new glHandle(api->glCreateProgram());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -167,17 +181,17 @@ namespace StgSharp.Graphics
api->glDeleteBuffers(1, &handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffers(uint count, uint[] handle)
unsafe void IglFunc.DeleteBuffers(glHandle handle)
{
fixed (uint* ptr = handle)
api->glDeleteBuffers(count, ptr);
api->glDeleteBuffers((uint)handle.length, handle.handle);
handle.Free();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] arrays)
unsafe void IglFunc.DeleteVertexArrays(glHandle arrays)
{
fixed (uint* ptr = arrays)
api->glDeleteVertexArrays(count, ptr);
api->glDeleteVertexArrays((uint)arrays.length, arrays.handle);
arrays.Free();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -206,20 +220,20 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenBuffers(uint count, uint* ptr)
unsafe void IglFunc.GenBuffers(uint count, glHandle handle)
{
api->glGenBuffers(count, ptr);
api->glGenBuffers(count, handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenTextures(uint count, uint* ptr)
unsafe void IglFunc.GenTextures(uint count, glHandle handle)
{
api->glGenTextures(count, ptr);
api->glGenTextures(count, handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr)
unsafe void IglFunc.GenVertexArrays(uint count, glHandle handle)
{
api->glGenVertexArrays(count, ptr);
api->glGenVertexArrays(count, handle.handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
+20 -11
View File
@@ -24,7 +24,7 @@ namespace StgSharp.Graphics
void IglFunc.AlignFrame(Form form) => throw signal;
void IglFunc.AttachShader(uint program, uint shader) => throw signal;
void IglFunc.AttachShader(glHandle program, glHandle shader) => throw signal;
void IglFunc.BindBuffer(BufferType bufferType, uint index) => throw signal;
@@ -38,27 +38,36 @@ namespace StgSharp.Graphics
void IglFunc.ClearGraphicError() => throw signal;
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType) => throw signal;
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType) => throw signal;
glHandle IglFunc.CreateProgram() => throw signal;
glHandle IglFunc.CreateProgram(int count) => throw signal;
uint IglFunc.CreateProgram() => throw signal;
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share) => throw signal;
uint IglFunc.CreatGraphicProgram() => throw signal;
glHandle IglFunc.CreatGraphicProgram() => throw signal;
public void DeleteBuffer(uint handle) => throw signal;
unsafe void IglFunc.DeleteBuffers(uint count, uint[] ptr) => throw signal;
void IglFunc.DeleteBuffers(glHandle handle) => throw signal;
void IglFunc.DeleteVertexArrays(glHandle handle) => throw signal;
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] ptr) => throw signal;
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr) => throw signal;
unsafe void IglFunc.GenBuffers(uint count, uint* ptr) => throw signal;
void IglFunc.GenBuffers(uint count, glHandle ptr) => throw signal;
unsafe void IglFunc.GenTextures(uint count, uint* ptr) => throw signal;
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr) => throw signal;
void IglFunc.GenTextures(uint count, glHandle handle) => throw signal;
void IglFunc.GenVertexArrays(uint count, glHandle handle) => throw signal;
void IglFunc.GetGraphicError() => throw signal;
@@ -90,6 +99,8 @@ namespace StgSharp.Graphics
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer) => throw signal;
void IglFunc.SetWindowShouldClose(Form form, bool mark) => throw signal;
void IglFunc.SwapBuffers(Form form) => throw signal;
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels) => throw signal;
@@ -101,7 +112,5 @@ namespace StgSharp.Graphics
void IglFunc.Viewport(int x, int y, uint width, uint height) => throw signal;
internal static GLonTimeout Set() => singleInstance;
void IglFunc.SetWindowShouldClose(Form form, bool mark) => throw signal;
}
}
+43 -47
View File
@@ -18,9 +18,16 @@ namespace StgSharp.Graphics
{
internal GLonUpdateInternal glUpdateInternal;
private ConcurrentQueue<IntPtr> glActionQueue;
private ConcurrentQueue<Vec4d> glParamQueue;
private GLparamQueue paramQueue;
private FrameTimeoutException refreshSignal = new();
internal GLonUpdate(GLcontext* context)
{
glUpdateInternal = new GLonUpdateInternal();
paramQueue = glUpdateInternal.paramQueue;
glActionQueue = new ConcurrentQueue<IntPtr>();
}
internal ConcurrentQueue<IntPtr> GLactionQueue
{
get
@@ -28,18 +35,9 @@ namespace StgSharp.Graphics
return glActionQueue;
}
}
internal GLonUpdate(GLcontext* context)
{
glUpdateInternal = new GLonUpdateInternal();
glParamQueue = glUpdateInternal.paramQueue;
glActionQueue = new ConcurrentQueue<IntPtr>();
}
void IglFunc.ActiveTexture(uint type)
{
glParamQueue.Enqueue(new Vec4d(type));
paramQueue.Enqueue(type);
glActionQueue.Enqueue(glUpdateInternal.ActiveTexturePtr);
}
@@ -61,18 +59,17 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.AttachShader(uint program, uint shader)
unsafe void IglFunc.AttachShader(glHandle program, glHandle shader)
{
glActionQueue.Enqueue(
() => api->glAttachShader(program, shader)
);
paramQueue.Enqueue(program[0], shader[0]);
glActionQueue.Enqueue(glUpdateInternal.AttachShaderPtr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
glActionQueue.Enqueue(
() => api->glBindBuffer((uint)bufferType, index)
paramQueue.Enqueue((uint)bufferType, index);
glActionQueue.Enqueue(glUpdateInternal.BindBufferPtr
);
}
@@ -100,7 +97,7 @@ namespace StgSharp.Graphics
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearColor(float R, float G, float B, float A)
{
glActionQueue.Enqueue(
@@ -108,6 +105,7 @@ namespace StgSharp.Graphics
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ClearGraphicError()
{
glActionQueue.Enqueue(
@@ -119,9 +117,9 @@ namespace StgSharp.Graphics
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
int id = -1;
glHandle h = new glHandle(1);
glActionQueue.Enqueue(
() =>
{
@@ -162,14 +160,14 @@ namespace StgSharp.Graphics
Interlocked.Add(ref id, (int)(shaderID + 1));
}
);
while (id < 0) { }
return (uint)id;
return h;
}
uint IglFunc.CreateProgram()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreateProgram()
{
int id = -1;
glHandle h = new glHandle(1);
glActionQueue.Enqueue(
() =>
{
@@ -177,8 +175,13 @@ namespace StgSharp.Graphics
Interlocked.Add(ref id, ret + 1);
}
);
while (id < 0) { }
return (uint)id;
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreateProgram(int count)
{
throw new NotImplementedException();
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
@@ -207,23 +210,17 @@ namespace StgSharp.Graphics
}
uint IglFunc.CreatGraphicProgram()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
glHandle IglFunc.CreatGraphicProgram()
{
int id = 0;
glHandle h = new glHandle(1);
glActionQueue.Enqueue(
() =>
{
Interlocked.Add(ref id, (int)(api->glCreateProgram() + 1));
}
);
while (id < 0) { }
return (uint)id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
return h;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -238,7 +235,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.DeleteBuffers(uint count, uint[] handle)
unsafe void IglFunc.DeleteBuffers(glHandle handle)
{
glActionQueue.Enqueue(
() =>
@@ -252,7 +249,7 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] arrays)
unsafe void IglFunc.DeleteVertexArrays(glHandle arrays)
{
glActionQueue.Enqueue(
() =>
@@ -293,27 +290,25 @@ namespace StgSharp.Graphics
);
}
unsafe void IglFunc.GenBuffers(uint count, uint* ptr)
unsafe void IglFunc.GenBuffers(uint count, glHandle handle)
{
glActionQueue.Enqueue(
() =>
{
api->glGenBuffers(count, ptr);
}
);
}
unsafe void IglFunc.GenTextures(uint count, uint* ptr)
unsafe void IglFunc.GenTextures(uint count, glHandle handle)
{
glActionQueue.Enqueue(
() =>
{
api->glGenTextures(count, ptr);
}
);
}
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr)
unsafe void IglFunc.GenVertexArrays(uint count, glHandle ptr)
{
glActionQueue.Enqueue(
() =>
@@ -347,8 +342,6 @@ namespace StgSharp.Graphics
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
bool IglFunc.GetWindowShouldClose(Form form)
{
@@ -410,14 +403,12 @@ namespace StgSharp.Graphics
);
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
IntPtr handlerPtr = Marshal.GetFunctionPointerForDelegate(handler);
InternalIO.glfw.SetFramebufferSizeCallback(id, handlerPtr);
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
glActionQueue.Enqueue(
@@ -430,6 +421,11 @@ namespace StgSharp.Graphics
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
}
void IglFunc.SwapBuffers(Form form)
{
glActionQueue.Enqueue(
@@ -10,25 +10,414 @@ using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal unsafe struct GLonUpdateInternal
internal unsafe struct GLonUpdateInternal : IglFunc
{
private GLcontext* gl;
internal ConcurrentQueue<Vec4d> paramQueue;
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;
ActiveTexturePtr = Marshal.GetFunctionPointerForDelegate(ActiveTexture);
}
private void ActiveTexture()
public void ActivePointer()
{
uint type;
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)
{
uint t;
paramQueue.TryDequeue(out t);
gl->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)
{
uint p, s;
paramQueue.TryDequeue(out p, out s);
gl->glAttachShader(p, s);
}
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
uint t, i;
paramQueue.TryDequeue(out t, out i);
gl->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)
{
uint mt;
paramQueue.TryDequeue(out mt);
gl->glClear(mt);
}
void IglFunc.ClearColor(float R, float G, float B, float A)
{
float r, g, b, a;
paramQueue.TryDequeue(out r, out g, out b, out a);
gl->glClearColor(r, g, b, a);
}
void IglFunc.ClearGraphicError()
{
while (gl->glGetError() != 0) { }
}
glHandle IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
IntPtr shaderHandle, codeHandle;
paramQueue.TryDequeue(out codeHandle, out shaderHandle);
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);
}
*(uint*)shaderHandle = shaderID;
return default;
}
glHandle IglFunc.CreateProgram()
{
throw new NotImplementedException();
}
glHandle IglFunc.CreateProgram(int count)
{
throw new NotImplementedException();
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
throw new NotImplementedException();
}
glHandle IglFunc.CreatGraphicProgram()
{
throw new NotImplementedException();
}
void IglFunc.DeleteBuffer(uint handle)
{
throw new NotImplementedException();
}
void IglFunc.DeleteBuffers(glHandle handle)
{
throw new NotImplementedException();
}
void IglFunc.DeleteVertexArrays(glHandle arrays)
{
throw new NotImplementedException();
}
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenBuffers(uint count, glHandle ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenTextures(uint count, glHandle ptr)
{
throw new NotImplementedException();
}
void IglFunc.GenVertexArrays(uint count, glHandle ptr)
{
throw new NotImplementedException();
}
void IglFunc.GetGraphicError()
{
throw new NotImplementedException();
}
int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
throw new NotImplementedException();
}
bool IglFunc.GetWindowShouldClose(Form form)
{
throw new NotImplementedException();
}
void IglFunc.MakeContexCurrent(Form form)
{
throw new NotImplementedException();
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
throw new NotImplementedException();
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
throw new NotImplementedException();
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(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, Vec4d vec)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
throw new NotImplementedException();
}
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
throw new NotImplementedException();
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
throw new NotImplementedException();
}
void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
throw new NotImplementedException();
}
void IglFunc.SwapBuffers(Form form)
{
throw new NotImplementedException();
}
void IglFunc.TextureImage2d(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)
{
throw new NotImplementedException();
}
void IglFunc.UseProgram(uint program)
{
throw new NotImplementedException();
}
void IglFunc.Viewport(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();
}
}
}
}
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
public partial class Form
{
/// <summary>
/// Create a set of <see cref="ElementBuffer"/>.
/// </summary>
/// <param name="count">Amount of EBO to be created.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected ElementBuffer CreateElementBuffer(uint count)
{
return new ElementBuffer(count, this.gl);
}
/// <summary>
/// Create a set of <see cref="VertexArray"/>.
/// </summary>
/// <param name="count">Amount of VAO to be crated.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected VertexArray CreateVertexArray(uint count)
{
return new VertexArray(count, gl);
}
/// <summary>
/// Create a sets of <see cref="Texture"/>
/// </summary>
/// <param name="n">Amount of textures to be crated.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected Texture CreateTexture(uint n)
{
return new Texture(n, this.GL);
}
/// <summary>
/// Create aset of <see cref="VertexBuffer"/>.
/// </summary>
/// <param name="count">Amount of Object to be created.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected VertexBuffer CreateVertexBuffer(uint count)
{
return new VertexBuffer(count, gl);
}
}
}
@@ -20,23 +20,24 @@ namespace StgSharp.Graphics
}
}
public class Shader
{
/// <summary>
/// A <see cref="uint"/> value represent this shader.
/// </summary>
public readonly uint programID;
internal uint vertexShaderID;
internal uint fragmentShaderID;
internal readonly glHandle Handle;
public readonly ShaderType type;
private IglFunc GL;
/// <summary>
/// Inti a shader with no program attached.
/// </summary>
internal unsafe Shader(IglFunc gl)
internal unsafe Shader(IglFunc gl, ShaderType t, int count)
{
GL = gl;
programID = GL.CreateProgram();
type = t;
if (type == ShaderType.Program)
{
Handle = GL.CreateProgram();
}
}
internal void ChangeEnvironment(IglFunc gl)
@@ -48,10 +49,10 @@ 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 CompileVertexShader(string shaderCode)
public unsafe void CompileShader(string shaderCode)
{
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
GL.AttachShader(programID, vertexShaderID);
vertexHandle = GL.CompileShader(shaderCode, type);
GL.AttachShader(programHandle, vertexHandle);
}
/// <summary>
@@ -60,8 +61,8 @@ namespace StgSharp.Graphics
/// <param name="shaderCode">GLSL code of fragment shader.</param>
public unsafe void CompileFragmentShader(string shaderCode)
{
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
GL.AttachShader(programID, fragmentShaderID);
fragmentHandle = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
GL.AttachShader(programHandle, fragmentHandle);
}
/// <summary>
@@ -69,7 +70,7 @@ namespace StgSharp.Graphics
/// </summary>
public void Link()
{
if (InternalIO.InternalLinkShaderProgram(programID) == 0)
if (InternalIO.InternalLinkShaderProgram(programHandle) == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
try
@@ -99,7 +100,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
uniform = GL.GetUniformLocation(programHandle, ptr);
}
if (uniform != -1)
{
@@ -107,7 +108,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
}
}
/// <summary>
@@ -124,7 +125,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
uniform = GL.GetUniformLocation(programHandle, ptr);
}
if (uniform != -1)
{
@@ -132,7 +133,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
}
}
/// <summary>
@@ -150,7 +151,7 @@ namespace StgSharp.Graphics
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
uniform = GL.GetUniformLocation(programHandle, ptr);
}
if (uniform != -1)
{
@@ -158,7 +159,7 @@ namespace StgSharp.Graphics
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
throw new Exception($"Cannot find the uniform {name} in shader {programHandle}");
}
}
@@ -168,7 +169,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Use()
{
GL.UseProgram(programID);
GL.UseProgram(programHandle);
}
}