daily update on 20240104 by nitload youmu, double threading not work

This commit is contained in:
Nitload
2024-01-04 08:58:12 +08:00
parent 14eaab455f
commit 2767c91293
138 changed files with 9692 additions and 11011 deletions
+26 -11
View File
@@ -1,20 +1,37 @@
using StgSharp.Math;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics
{
public unsafe class ElementBuffer : IDisposable, IBufferObject
public partial class Form
{
uint[] _bufferID;
private IglFunc GL;
/// <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);
}
}
internal ElementBuffer(int n, IglFunc glFunc)
/// <summary>
/// A collecion of handle to Element Buffer Object in OpenGL.
/// Each <see cref="uint"/> value indexed from instance of this class this
/// is the only handle to one Element Buffer Object.
/// </summary>
public unsafe class ElementBuffer : IBufferObject
{
internal ElementBuffer(uint n, IglFunc gl)
{
_bufferID = new uint[n];
GL = glFunc;
GL = gl;
fixed (uint* id = _bufferID)
{
GL.GenBuffers(n, id);
@@ -26,7 +43,7 @@ namespace StgSharp.Graphics
get { return _bufferID[n]; }
}
public void Bind(int index)
public override void Bind(int index)
{
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
}
@@ -38,14 +55,12 @@ namespace StgSharp.Graphics
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
}
public void Dispose()
protected sealed override void Dispose(bool disposing)
{
fixed (uint* id = _bufferID)
if (disposing)
{
GL.DeleteBuffers(_bufferID.Length, id);
GL.DeleteBuffers((uint)_bufferID.Length, _bufferID);
}
}
}
}
@@ -1,25 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal class FormRenderBuffer
{
internal GLonRender GL;
internal InternalFormBuffer data;
}
internal class FormUpdateBuffer
{
internal GLonUpdate GL;
internal InternalFormBuffer data;
}
internal class InternalFormBuffer
{
}
}
@@ -9,9 +9,38 @@ namespace StgSharp.Graphics
/// <summary>
/// Interface of all kinds OpenGL buffer onjects
/// </summary>
public interface IBufferObject
public abstract class IBufferObject : IDisposable
{
uint this[int index] { get; }
void Bind(int index);
protected uint[] _bufferID;
protected IglFunc GL;
/// <summary>
/// Get the only handle to one of the Object instance
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public uint this[int index]
{
get { return _bufferID[index]; }
}
/// <summary>
/// Bind a buffer instance to OpenGL
/// </summary>
/// <param name="index">Index of handle of the object in this instance to be bind</param>
public abstract void Bind(int index);
protected abstract void Dispose(bool disposing);
~IBufferObject()
{
Dispose(disposing: false);
}
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
+28 -24
View File
@@ -5,55 +5,59 @@ using System.Text;
namespace StgSharp.Graphics
{
public unsafe struct VertexArray : IDisposable, IBufferObject
public partial class Form
{
internal uint[] _bufferID;
private IglFunc 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);
}
}
internal VertexArray(int n)
public unsafe class VertexArray : IBufferObject
{
internal VertexArray(uint n, IglFunc gl)
{
_bufferID = new uint[n];
//GL = glFunc;
GL = gl;
fixed (uint* id = _bufferID)
{
GL.GenVertexArrays(n, id);
}
}
public uint this[int n]
{
get
{
return _bufferID[n];
}
}
public void Bind(int index)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public sealed override void Bind(int index)
{
GL.BindVertexArray(_bufferID[index]);
}
public void Dispose()
{
fixed (uint* id = _bufferID)
{
GL.DeleteVertexArrays(_bufferID.Length, id);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void BindNull()
{
GL.BindVertexArray(0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetVertexAttribute(uint index, int vertexLength, Type dataType, bool isNomalized, int stride, int pointer)
public void SetVertexAttribute(uint index, int vertexLength, Type dataType, bool isNomalized, uint stride, int pointer)
{
GL.SetVertexAttribute(
index, vertexLength, dataType,
isNomalized,
stride, pointer);
}
protected sealed override void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteVertexArrays((uint)_bufferID.Length, _bufferID);
}
}
}
}
+30 -14
View File
@@ -3,17 +3,29 @@ using System;
using System.Collections.Generic;
using System.Net.Security;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace StgSharp.Graphics
{
public unsafe struct VertexBuffer : IDisposable, IBufferObject
public partial class Form
{
internal uint[] _bufferID;
private IglFunc 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);
}
}
internal VertexBuffer(int n, IglFunc glFunc)
public unsafe class VertexBuffer : IBufferObject
{
internal VertexBuffer(uint n, IglFunc glFunc)
{
_bufferID = new uint[n];
GL = glFunc;
@@ -23,29 +35,33 @@ namespace StgSharp.Graphics
}
}
public uint this[int n]
{
get { return _bufferID[n]; }
}
/// <summary>
///
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unbind()
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, 0);
}
public void Bind(int index)
public sealed override void Bind(int index)
{
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
GL.BindBuffer(BufferType.ARRAY_BUFFER, (uint)index);
}
public void Dispose()
internal void ChangeEnvironment(IglFunc gl)
{
fixed (uint* id = _bufferID)
GL = gl;
}
protected sealed override void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteBuffers(_bufferID.Length, id);
fixed (uint* id = _bufferID)
{
GL.DeleteBuffers((uint)_bufferID.Length, _bufferID);
}
}
}
+121 -117
View File
@@ -11,139 +11,143 @@ namespace StgSharp.Graphics
{
public partial class Form
{
protected Texture CreateTexture(int n)
protected Texture CreateTexture(uint n)
{
return new Texture(n, GL);
return new Texture(n, this.GL);
}
}
public unsafe class Texture
{
internal uint[] textureId;
internal Image[] sourceList;
internal static bool[] unitActivated = new bool[16];
private IglFunc GL;
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
protected unsafe class Texture
internal Texture(uint n, IglFunc gl)
{
internal uint[] textureId;
internal Image[] sourceList;
internal static bool[] unitActivated = new bool[16];
private IglFunc GL;
internal Texture(int n, IglFunc glFunc)
textureId = new uint[n];
sourceList = new Image[n];
GL = gl;
fixed (uint* id = textureId)
{
textureId = new uint[n];
sourceList = new Image[n];
GL = glFunc;
fixed (uint* id = textureId)
{
GL.GenTextures(n, id);
}
GL.GenTextures(n, id);
}
public void Bind2D(int index)
{
GL.BindTexture(GLconst.TEXTURE_2D, textureId[index]);
}
/// <summary>
///
/// </summary>
/// <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();
fixed (Image.ImageInfo* textureInfo = &sourceList[index].info)
{
InternalIO.InternalLoadImage(name, textureInfo, loader);
}
if (sourceList[index].info.pixelPtr != IntPtr.Zero)
{
uint channel;
switch (sourceList[index].info.channel)
{
case 1:
channel = GLconst.RED; break;
case 2:
channel = GLconst.RG; break;
case 3:
channel = GLconst.RGB; break;
case 4:
channel = GLconst.RGBA; break;
default:
channel = 0;
break;
}
GL.TextureImage2d(
GLconst.TEXTURE_2D, 0, channel,
sourceList[index].Width, sourceList[index].Height,
channel, GLconst.UNSIGNED_BYTE, (byte*)sourceList[index].info.pixelPtr
);
}
else
{
InternalIO.InternalWriteLog($"Failed to load image file of \"{name}\".", LogType.Error);
}
}
#region texture set
public void Set2dFilterProperty(
Filter onMinify, Filter onMagnify)
{
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MIN_FILTER, (int)onMinify);
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MAG_FILTER, (int)onMagnify);
}
public void Set2dWrapProperty(
Wrap onHorizontial, Wrap onVertical)
{
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_S, (int)onHorizontial);
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_T, (int)onVertical);
}
#endregion
public void Activate(TextureUnit unit)
{
GL.ActiveTexture((uint)unit);
}
}
public enum TextureUnit
public void Bind2D(int index)
{
Unit0 = GLconst.TEXTURE0,
Unit1 = GLconst.TEXTURE1,
Unit2 = GLconst.TEXTURE2,
Unit3 = GLconst.TEXTURE3,
Unit4 = GLconst.TEXTURE4,
Unit5 = GLconst.TEXTURE5,
Unit6 = GLconst.TEXTURE6,
Unit7 = GLconst.TEXTURE7,
Unit8 = GLconst.TEXTURE8,
Unit9 = GLconst.TEXTURE9,
Unit10 = GLconst.TEXTURE10,
Unit11 = GLconst.TEXTURE11,
Unit12 = GLconst.TEXTURE12,
Unit13 = GLconst.TEXTURE13,
Unit14 = GLconst.TEXTURE14,
Unit15 = GLconst.TEXTURE15,
GL.BindTexture(GLconst.TEXTURE_2D, textureId[index]);
}
public enum Wrap
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="index"></param>
/// <param name="loader"></param>
public unsafe void LoadTexture(int index, string name, ImageLoader loader)
{
Repeat = GLconst.REPEAT,
MirroredRepeat = GLconst.MIRRORED_REPEAT,
ClampToEdge = GLconst.CLAMP_TO_EDGE,
ClampToBorder = GLconst.CLAMP_TO_BORDER,
sourceList[index] = new Image();
fixed (Image.ImageInfo* textureInfo = &sourceList[index].info)
{
InternalIO.InternalLoadImage(name, textureInfo, loader);
}
if (sourceList[index].info.pixelPtr != IntPtr.Zero)
{
uint channel;
switch (sourceList[index].info.channel)
{
case 1:
channel = GLconst.RED; break;
case 2:
channel = GLconst.RG; break;
case 3:
channel = GLconst.RGB; break;
case 4:
channel = GLconst.RGBA; break;
default:
channel = 0;
break;
}
GL.TextureImage2d(
GLconst.TEXTURE_2D, 0, channel,
(uint)sourceList[index].Width, (uint)sourceList[index].Height,
channel, GLconst.UNSIGNED_BYTE, (byte*)sourceList[index].info.pixelPtr
);
}
else
{
InternalIO.InternalWriteLog($"Failed to load image file of \"{name}\".", LogType.Error);
}
}
public enum Filter
#region texture set
public void Set2dFilterProperty(
Filter onMinify, Filter onMagnify)
{
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,
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MIN_FILTER, (int)onMinify);
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_MAG_FILTER, (int)onMagnify);
}
public void Set2dWrapProperty(
Wrap onHorizontial, Wrap onVertical)
{
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_S, (int)onHorizontial);
GL.TextureParameter(GLconst.TEXTURE_2D, GLconst.TEXTURE_WRAP_T, (int)onVertical);
}
#endregion
public void Activate(TextureUnit unit)
{
GL.ActiveTexture((uint)unit);
}
}
public enum TextureUnit
{
Unit0 = GLconst.TEXTURE0,
Unit1 = GLconst.TEXTURE1,
Unit2 = GLconst.TEXTURE2,
Unit3 = GLconst.TEXTURE3,
Unit4 = GLconst.TEXTURE4,
Unit5 = GLconst.TEXTURE5,
Unit6 = GLconst.TEXTURE6,
Unit7 = GLconst.TEXTURE7,
Unit8 = GLconst.TEXTURE8,
Unit9 = GLconst.TEXTURE9,
Unit10 = GLconst.TEXTURE10,
Unit11 = GLconst.TEXTURE11,
Unit12 = GLconst.TEXTURE12,
Unit13 = GLconst.TEXTURE13,
Unit14 = GLconst.TEXTURE14,
Unit15 = GLconst.TEXTURE15,
}
public enum Wrap
{
Repeat = GLconst.REPEAT,
MirroredRepeat = GLconst.MIRRORED_REPEAT,
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,
}
}
@@ -9,15 +9,7 @@ namespace StgSharp.Graphics
{
public unsafe partial class Form
{
internal GLcontextIO* contextID;
private int missingAPIcount = 0;
private int problemAPIcount = 0;
internal GLcontext glContext
{
get { return RenderBuffer.GL.api; }
}
}
}
+74 -47
View File
@@ -1,6 +1,7 @@
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@@ -11,19 +12,18 @@ namespace StgSharp.Graphics
internal unsafe partial class GLonRender : IglFunc
{
internal GLcontext* api;
FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
internal GLonRender(GLcontext context)
internal GLonRender(GLcontext* context)
{
api = context;
}
internal GLcontext api;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.ActiveTexture(uint type)
{
api.ActiveTexture(type);
api->glActiveTexture(type);
}
void IglFunc.AfterAlign(Form form, Action action)
@@ -45,41 +45,44 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.AttachShader(uint program, uint shader)
{
api.AttachShader(program, shader);
api->glAttachShader(program, shader);
}
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
api.BindBuffer((uint)bufferType, index);
api->glBindBuffer((uint)bufferType, index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, uint texture)
{
api.BindTexture(type, texture);
api->glBindTexture(type, texture);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindVertexArray(uint index)
{
api.BindVertexArray(index);
api->glBindVertexArray(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
api.Clear((uint)mask);
api->glClear((uint)mask);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.ClearColor(float R, float G, float B, float A)
{
api.ClearColor(R, G, B, A);
api->glClearColor(R, G, B, A);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.ClearGraphicError()
{
while (api.GetError() != 0) { }
while (api->glGetError() != 0) { }
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
uint shaderID;
@@ -123,7 +126,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
uint IglFunc.CreateProgram()
{
return api.CreateProgram();
return api->glCreateProgram();
}
unsafe IntPtr IglFunc.CreateWindowHandle(
@@ -152,36 +155,44 @@ namespace StgSharp.Graphics
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe uint IglFunc.CreatGraphicProgram()
{
return api.CreateProgram();
return api->glCreateProgram();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffers(int count, uint* ptr)
unsafe void IglFunc.DeleteBuffer(uint handle)
{
api.DeleteBuffers(count, ptr);
api->glDeleteBuffers(1, &handle);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr)
unsafe void IglFunc.DeleteBuffers(uint count, uint[] handle)
{
api.DeleteVertexArrays(count, ptr);
fixed (uint* ptr = handle)
api->glDeleteBuffers(count, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] arrays)
{
fixed (uint* ptr = arrays)
api->glDeleteVertexArrays(count, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
{
uint a;
switch (type.Name)
switch (Type.GetTypeCode(type))
{
case "UInt32":
case TypeCode.UInt32:
a = GLconst.UNSIGNED_INT;
break;
case "UInt16":
case TypeCode.UInt16:
a = GLconst.UNSIGNED_SHORT;
break;
case "Byte":
case TypeCode.Byte:
a = GLconst.UNSIGNED_BYTE;
break;
default:
@@ -191,31 +202,31 @@ namespace StgSharp.Graphics
LogType.Error);
return;
}
api.DrawElements((uint)mode, count, a, (void*)ptr);
api->glDrawElements((uint)mode, count, a, (void*)ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenBuffers(int count, uint* ptr)
unsafe void IglFunc.GenBuffers(uint count, uint* ptr)
{
api.GenBuffers(count, ptr);
api->glGenBuffers(count, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenTextures(int count, uint* ptr)
unsafe void IglFunc.GenTextures(uint count, uint* ptr)
{
api.GenTextures(count, ptr);
api->glGenTextures(count, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GenVertexArrays(int count, uint* ptr)
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr)
{
api.GenVertexArrays(count, ptr);
api->glGenVertexArrays(count, ptr);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.GetGraphicError()
{
string error = null;
uint code = api.GetError();
uint code = api->glGetError();
while (code != 0)
{
error += $"OpenGL error {code}\n";
@@ -242,7 +253,7 @@ namespace StgSharp.Graphics
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
return api.GetUniformLocation(program, ptr);
return api->glGetUniformLocation(program, ptr);
}
unsafe bool IglFunc.GetWindowShouldClose(Form form)
@@ -263,9 +274,9 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
int size = sizeof(T);
api.BufferData(
api->glBufferData(
(uint)bufferType,
size,
(UIntPtr)size,
&bufferData,
(uint)usage);
}
@@ -274,14 +285,20 @@ namespace StgSharp.Graphics
unsafe void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
fixed (void* bufferPtr = &bufferArray[0])
api.BufferData(
if (bufferArray == null)
{
return;
}
fixed (void* bufferPtr = bufferArray)
api->glBufferData(
(uint)bufferType,
size,
(UIntPtr)size,
bufferPtr,
(uint)usage);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
@@ -290,11 +307,17 @@ namespace StgSharp.Graphics
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer)
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
uint type = InternalIO.GLtype[t];
api.VertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
api.EnableVertexAttribArray(index);
api->glVertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
api->glEnableVertexAttribArray(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
}
unsafe void IglFunc.SwapBuffers(Form form)
@@ -306,27 +329,31 @@ namespace StgSharp.Graphics
InternalIO.glfw.SwapBuffers(form.windowID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels)
void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
{
api.TexImage2D(
api->glTexImage2D(
target, level, internalformat, width, height, 0, format, type, pixels
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.TextureParameter(uint target, uint pname, int param)
{
api.TexParameteri(target, pname, param);
api->glTexParameteri(target, pname, param);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.UseProgram(uint program)
{
api.UseProgram(program);
api->glUseProgram(program);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Viewport(int x, int y, int width, int height)
void IglFunc.Viewport(int x, int y, uint width, uint height)
{
api.Viewport(x, y, width, height);
api->glViewport(x, y, width, height);
}
}
+48 -167
View File
@@ -1,6 +1,7 @@
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -10,217 +11,97 @@ namespace StgSharp.Graphics
{
internal class GLonTimeout : IglFunc
{
FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
private static readonly GLonTimeout singleInstance
= new GLonTimeout();
FrameTimeoutException refreshSIgnal = new FrameTimeoutException();
FrameTimeoutException signal = new FrameTimeoutException("This frame is timeout");
private GLonTimeout() { }
internal static GLonTimeout Set()
{
return singleInstance;
}
void IglFunc.ActiveTexture(uint type) => throw signal;
void IglFunc.ActiveTexture(uint type)
{
throw signal;
}
void IglFunc.AfterAlign(Form form, Action action) => throw signal;
void IglFunc.AfterAlign(Form form, Action action)
{
throw signal;
}
void IglFunc.AlignFrame(Form form) => throw signal;
void IglFunc.AlignFrame(Form form)
{
throw signal;
}
void IglFunc.AttachShader(uint program, uint shader) => throw signal;
void IglFunc.AttachShader(uint program, uint shader)
{
throw signal;
}
void IglFunc.BindBuffer(BufferType bufferType, uint index) => throw signal;
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
throw signal;
}
void IglFunc.BindTexture(uint type, uint texture) => throw signal;
void IglFunc.BindTexture(uint type, uint texture)
{
throw signal;
}
void IglFunc.BindVertexArray(uint index) => throw signal;
void IglFunc.BindVertexArray(uint index)
{
throw signal;
}
void IglFunc.Clear(MaskBufferBit mask) => throw signal;
void IglFunc.Clear(MaskBufferBit mask)
{
throw signal;
}
void IglFunc.ClearColor(float R, float G, float B, float A) => throw signal;
void IglFunc.ClearColor(float R, float G, float B, float A)
{
throw signal;
}
void IglFunc.ClearGraphicError() => throw signal;
void IglFunc.ClearGraphicError()
{
throw signal;
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType) => throw signal;
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
throw signal;
}
uint IglFunc.CreateProgram() => throw signal;
uint IglFunc.CreateProgram()
{
throw signal;
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share) => throw signal;
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
{
throw signal;
}
uint IglFunc.CreatGraphicProgram() => throw signal;
uint IglFunc.CreatGraphicProgram()
{
throw signal;
}
public void DeleteBuffer(uint handle) => throw signal;
unsafe void IglFunc.DeleteBuffers(int count, uint* ptr)
{
throw signal;
}
unsafe void IglFunc.DeleteBuffers(uint count, uint[] ptr) => throw signal;
unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr)
{
throw signal;
}
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] ptr) => throw signal;
void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
{
throw signal;
}
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr) => throw signal;
unsafe void IglFunc.GenBuffers(int count, uint* ptr)
{
throw signal;
}
unsafe void IglFunc.GenBuffers(uint count, uint* ptr) => throw signal;
unsafe void IglFunc.GenTextures(int count, uint* ptr)
{
throw signal;
}
unsafe void IglFunc.GenTextures(uint count, uint* ptr) => throw signal;
unsafe void IglFunc.GenVertexArrays(int count, uint* ptr)
{
throw signal;
}
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr) => throw signal;
void IglFunc.GetGraphicError()
{
throw signal;
}
void IglFunc.GetGraphicError() => throw signal;
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
throw signal;
}
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr) => throw signal;
bool IglFunc.GetWindowShouldClose(Form form)
{
throw signal;
}
bool IglFunc.GetWindowShouldClose(Form form) => throw signal;
void IglFunc.MakeContexCurrent(Form form)
{
throw signal;
}
void IglFunc.MakeContexCurrent(Form form) => throw signal;
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
throw signal;
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) => throw signal;
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
throw signal;
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage) => throw signal;
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
{
throw signal;
}
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler) => throw signal;
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
throw signal;
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3) => throw signal;
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
{
throw signal;
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec) => throw signal;
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
throw signal;
}
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2) => throw signal;
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
{
throw signal;
}
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1) => throw signal;
void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
throw signal;
}
void IglFunc.SetValue(Uniform<float> uniform, float v0) => throw signal;
void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
throw signal;
}
void IglFunc.SetValue(Uniform<int> uniform, int i0) => throw signal;
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
throw signal;
}
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec) => throw signal;
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer)
{
throw signal;
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer) => throw signal;
void IglFunc.SwapBuffers(Form form)
{
throw signal;
}
void IglFunc.SwapBuffers(Form form) => throw signal;
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels)
{
throw signal;
}
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels) => throw signal;
void IglFunc.TextureParameter(uint target, uint pname, int param)
{
throw signal;
}
void IglFunc.TextureParameter(uint target, uint pname, int param) => throw signal;
void IglFunc.UseProgram(uint program)
{
throw signal;
}
void IglFunc.UseProgram(uint program) => throw signal;
void IglFunc.Viewport(int x, int y, int width, int height)
{
throw signal;
}
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;
}
}
+295 -82
View File
@@ -1,28 +1,55 @@
using StgSharp.Logic;
using StgSharp.Math;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StgSharp.Graphics
{
internal partial class GLonUpdate : IglFunc
{
private FrameTimeoutException refreshSignal = new();
internal readonly GLcontext api;
using glMetaData = (
int i0, int i1, int i2, int i3,
double d0, double d1, double d2, double d3,
IntPtr p0, IntPtr p1, IntPtr p2, IntPtr p3
);
internal GLonUpdate(GLcontext context)
internal unsafe partial class GLonUpdate : IglFunc
{
internal GLcontext* api;
private ConcurrentQueue<Action> glActionQueue;
private FrameTimeoutException refreshSignal = new();
private DynamicArrayList<glMetaData> threadBuffer = new DynamicArrayList<glMetaData>(16, default);
ConcurrentQueue<Action<int>> GLactionQueue
{
get
{
return glActionQueue;
}
}
internal GLonUpdate(GLcontext* context)
{
api = context;
glActionQueue = new ConcurrentQueue<Action>();
glActionQueue.Enqueue(() => { });
}
void IglFunc.ActiveTexture(uint type)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glActiveTexture(type);
}
);
}
@@ -42,52 +69,125 @@ namespace StgSharp.Graphics
throw refreshSignal;
}
void IglFunc.AttachShader(uint program, uint shader)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.AttachShader(uint program, uint shader)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glAttachShader(program, shader)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindBuffer(BufferType bufferType, uint index)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glBindBuffer((uint)bufferType, index)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindTexture(uint type, uint texture)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glBindTexture((uint)type, texture)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.BindVertexArray(uint index)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glBindVertexArray((uint)index)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.Clear(MaskBufferBit mask)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glClear((uint)mask)
);
}
void IglFunc.ClearColor(float R, float G, float B, float A)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => api->glClearColor(R, G, B, A)
);
}
void IglFunc.ClearGraphicError()
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
while (api->glGetError() != 0) { };
}
);
}
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
{
throw new NotImplementedException();
int id = -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));
}
);
while (id < 0) { }
return (uint)id;
}
uint IglFunc.CreateProgram()
{
throw new NotImplementedException();
int id = -1;
glActionQueue.Enqueue(
() =>
{
int ret = (int)api->glCreateProgram();
Interlocked.Add(ref id, ret + 1);
}
);
while (id < 0) { }
return (uint)id;
}
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
@@ -118,44 +218,118 @@ namespace StgSharp.Graphics
uint IglFunc.CreatGraphicProgram()
{
throw new NotImplementedException();
int id = 0;
glActionQueue.Enqueue(
() =>
{
Interlocked.Add(ref id, (int)(api->glCreateProgram() + 1));
}
);
while (id < 0) { }
return (uint)id;
}
unsafe void IglFunc.DeleteBuffers(int count, uint* ptr)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetWindowShouldClose(Form form, bool mark)
{
throw new NotImplementedException();
InternalIO.glfw.SetWindowShouldClose(form.windowID, mark ? 1 : 0);
}
unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.DeleteBuffer(uint handle)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
uint target = handle;
api->glDeleteBuffers(1, &target);
}
);
}
public void DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
unsafe void IglFunc.DeleteBuffers(uint count, uint[] handle)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
uint[] h = handle;
fixed (uint* p = h)
{
api->glDeleteBuffers(count, p);
}
}
);
}
void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
unsafe void IglFunc.DeleteVertexArrays(uint count, uint[] arrays)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
uint[] target = arrays;
fixed (uint* ptr = target)
{
api->glDeleteVertexArrays(count, ptr);
}
}
);
}
unsafe void IglFunc.GenBuffers(int count, uint* ptr)
void IglFunc.DrawElements(GepmetryType mode, uint count, Type type, IntPtr ptr)
{
throw new NotImplementedException();
uint a;
switch (Type.GetTypeCode(type))
{
case TypeCode.UInt32:
a = GLconst.UNSIGNED_INT;
break;
case TypeCode.UInt16:
a = GLconst.UNSIGNED_SHORT;
break;
case TypeCode.Byte:
a = GLconst.UNSIGNED_BYTE;
break;
default:
InternalIO.InternalWriteLog(
$"Parameter error in parameter {nameof(type)} method DrawElements. " +
$"Only {typeof(uint).Name},{typeof(ushort).Name},{typeof(byte).Name} types are suporrted.",
LogType.Error);
return;
}
glActionQueue.Enqueue(
() =>
{ api->glDrawElements((uint)mode, count, a, (void*)ptr); }
);
}
unsafe void IglFunc.GenTextures(int count, uint* ptr)
unsafe void IglFunc.GenBuffers(uint count, uint* ptr)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glGenBuffers(count, ptr);
}
);
}
unsafe void IglFunc.GenVertexArrays(int count, uint* ptr)
unsafe void IglFunc.GenTextures(uint count, uint* ptr)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glGenTextures(count, ptr);
}
);
}
unsafe void IglFunc.GenVertexArrays(uint count, uint* ptr)
{
glActionQueue.Enqueue(
() =>
{
api->glGenVertexArrays(count, ptr);
}
);
}
public void GetGraphicError()
@@ -168,25 +342,26 @@ namespace StgSharp.Graphics
throw new NotImplementedException();
}
public unsafe int GetUniformLocation(uint program, byte* ptr)
{
throw new NotImplementedException();
}
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
{
throw new NotImplementedException();
int ret = -1;
glActionQueue.Enqueue(
() =>
{
int location = api->glGetUniformLocation(program, ptr);
Interlocked.Add(ref ret, location + 1);
}
);
while (ret < 0) { }
return ret;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool GetWindowShouldClose(Form form)
{
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
}
bool IglFunc.GetWindowShouldClose(Form form)
{
throw new NotImplementedException();
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
}
public void GLswapBuffers(Form form)
@@ -198,7 +373,8 @@ namespace StgSharp.Graphics
InternalIO.glfw.SwapBuffers(form.windowID);
}
public void MakeContexCurrent(Form form)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void IglFunc.MakeContexCurrent(Form form)
{
if (form.windowID == IntPtr.Zero)
{
@@ -207,29 +383,40 @@ namespace StgSharp.Graphics
InternalIO.glfw.MakeContextCurrent(form.windowID);
}
void IglFunc.MakeContexCurrent(Form form)
{
throw new NotImplementedException();
}
public void SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) where T : IMat
{
throw new NotImplementedException();
}
public void SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
{
throw new NotImplementedException();
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
T data = bufferData;
int size = sizeof(T);
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
&data,
(uint)usage);
}
);
}
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
if (bufferArray == null)
{
return;
}
fixed (void* bufferPtr = bufferArray)
api->glBufferData(
(uint)bufferType,
(UIntPtr)size,
bufferPtr,
(uint)usage);
}
);
}
@@ -240,44 +427,70 @@ namespace StgSharp.Graphics
}
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer)
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, uint stride, int pointer)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
uint type = InternalIO.GLtype[t];
api->glVertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
api->glEnableVertexAttribArray(index);
}
);
}
void IglFunc.SwapBuffers(Form form)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
if (form.windowID == IntPtr.Zero)
{
return;
}
InternalIO.glfw.SwapBuffers(form.windowID);
}
);
}
public unsafe void TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels)
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, uint width, uint height, uint format, uint type, void* pixels)
{
throw new NotImplementedException();
}
unsafe void IglFunc.TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels)
{
throw new NotImplementedException();
}
public void TextureParameter(uint target, uint pname, int param)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glTexImage2D(target, level, internalformat, width, height, 0, format, type, pixels);
}
);
}
void IglFunc.TextureParameter(uint target, uint pname, int param)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glTextureParameteri(target, pname, param);
}
);
}
void IglFunc.UseProgram(uint rogram)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glUseProgram(rogram);
}
);
}
void IglFunc.Viewport(int x, int y, int width, int height)
void IglFunc.Viewport(int x, int y, uint width, uint height)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
api->glViewport(x, y, width, height);
}
);
}
}
}
@@ -18,102 +18,158 @@ namespace StgSharp.Graphics
{
return new Shader(GL);
}
}
public class Shader
public class Shader
{
/// <summary>
/// A <see cref="uint"/> value represent this shader.
/// </summary>
public readonly uint programID;
internal uint vertexShaderID;
internal uint fragmentShaderID;
private IglFunc GL;
/// <summary>
/// Inti a shader with no program attached.
/// </summary>
internal unsafe Shader(IglFunc gl)
{
/// <summary>
/// A <see cref="uint"/> value represent this shader.
/// </summary>
public readonly uint programID;
internal uint vertexShaderID;
internal uint fragmentShaderID;
private IglFunc GL;
/// <summary>
/// Inti a shader with no program attached.
/// </summary>
internal unsafe Shader(IglFunc glFunc)
{
GL = glFunc;
programID = GL.CreateProgram();
}
/// <summary>
/// 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)
{
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
GL.AttachShader(programID, vertexShaderID);
}
/// <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)
{
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
GL.AttachShader(programID, fragmentShaderID);
}
/// <summary>
/// Link the shader to current context.
/// </summary>
public void Link()
{
if (InternalIO.InternalLinkShaderProgram(programID) == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
string log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
InternalIO.InternalWriteLog(log, LogType.Error);
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
}
}
}
/// <summary>
/// Get a uniform form current shader,
/// the value type of the uniform should be provided.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
{
byte[] bytes = Encoding.ASCII.GetBytes(name);
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
}
if (uniform != -1)
{
return new Uniform<T>(uniform);
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
}
}
/// <summary>
/// Let the current context use this shader.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Use()
{
GL.UseProgram(programID);
}
GL = gl;
programID = GL.CreateProgram();
}
internal void ChangeEnvironment(IglFunc gl)
{
GL = gl;
}
/// <summary>
/// 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)
{
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
GL.AttachShader(programID, vertexShaderID);
}
/// <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)
{
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
GL.AttachShader(programID, fragmentShaderID);
}
/// <summary>
/// Link the shader to current context.
/// </summary>
public void Link()
{
if (InternalIO.InternalLinkShaderProgram(programID) == 0)
{
IntPtr logPtr = InternalIO.InternalReadLog();
try
{
byte[] logByte = new byte[512];
Marshal.Copy(logPtr, logByte, 0, 512);
string log = Encoding.UTF8.GetString(logByte);
log = log.Replace("\0", "");
InternalIO.InternalWriteLog(log, LogType.Error);
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
}
}
}
/// <summary>
/// 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>
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
{
byte[] bytes = Encoding.ASCII.GetBytes(name);
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
}
if (uniform != -1)
{
return new Uniform<T>(uniform);
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
}
}
/// <summary>
/// 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>
public unsafe Uniform<T, U> GetUniform<T, U>(string name)
where T : struct
where U : struct
{
byte[] bytes = Encoding.ASCII.GetBytes(name);
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
}
if (uniform != -1)
{
return new Uniform<T, U>(uniform);
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
}
}
/// <summary>
/// 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>
public unsafe Uniform<T, U, V> GetUniform<T, U, V>(string name)
where T : struct
where U : struct
where V : struct
{
byte[] bytes = Encoding.ASCII.GetBytes(name);
int uniform;
fixed (byte* ptr = bytes)
{
uniform = GL.GetUniformLocation(programID, ptr);
}
if (uniform != -1)
{
return new Uniform<T, U, V>(uniform);
}
else
{
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
}
}
/// <summary>
/// Let the current context use this shader.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Use()
{
GL.UseProgram(programID);
}
}
}
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
@@ -14,13 +15,13 @@ namespace StgSharp.Graphics
private IglFunc GL;
internal unsafe Uniform(
Form.Shader program, string name, IglFunc gL)
uint shaderID, string name, IglFunc gL)
{
byte[] namearray = Encoding.ASCII.GetBytes(name);
GL = gL;
fixed (byte* ptr = namearray)
{
id = GL.GetUniformLocation(program.programID, ptr);
id = GL.GetUniformLocation(shaderID, ptr);
}
}
@@ -40,7 +41,7 @@ namespace StgSharp.Graphics
private IglFunc GL;
internal unsafe Uniform(
Form.Shader program, string name, IglFunc glFunc)
Shader program, string name, IglFunc glFunc)
{
byte[] namearray = Encoding.ASCII.GetBytes(name);
GL = glFunc;
@@ -64,7 +65,7 @@ namespace StgSharp.Graphics
internal int id;
private IglFunc GL;
public unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
public unsafe Uniform(Shader program, string name, IglFunc glFunc)
{
byte[] namearray = Encoding.ASCII.GetBytes(name);
GL = glFunc;
@@ -89,7 +90,7 @@ namespace StgSharp.Graphics
internal int id;
private IglFunc GL;
internal unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
internal unsafe Uniform(Shader program, string name, IglFunc glFunc)
{
byte[] namearray = Encoding.ASCII.GetBytes(name);
fixed (byte* ptr = namearray)
@@ -108,125 +109,107 @@ namespace StgSharp.Graphics
internal partial class GLonRender : IglFunc
{
/// <summary>
/// Set data value to a float <see cref="Uniform{T}"/>,
/// all types of parameters are float.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
/// <param name="v0">The value to be set</param>
void IglFunc.SetValue(Uniform<float> uniform, float v0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
api.Uniform1f(uniform.id, v0);
api->glUniform1f(uniform.id, v0);
}
/// <summary>
/// Set data value to a int <see cref="Uniform{T}"/>,
/// all types of parameters are int.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
/// <param name="i0">The value to be set</param>
void IglFunc.SetValue(Uniform<int> uniform, int i0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
api.Uniform1i(uniform.id, i0);
api->glUniform1i(uniform.id, i0);
}
/// <summary>
/// Set data value to a float <see cref="Uniform{T,U}"/>,
/// all types of parameters are float.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T,U}"/> target</param>
/// <param name="v0">The first value to be set</param>
/// <param name="v1">The second value to be set</param>
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
{
api.Uniform2f(uniform.id, v0, v1);
api->glUniform2f(uniform.id, v0, v1);
}
/// <summary>
/// Set data value to a float <see cref="Uniform{T,U,V}"/>,
/// all types of parameters are float.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T,U,V}"/> target</param>
/// <param name="v0">The first value to be set</param>
/// <param name="v1">The second value to be set</param>
/// <param name="v2">The third value to be set</param>
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
api.Uniform3f(uniform.id, v0, v1, v2);
api->glUniform3f(uniform.id, v0, v1, v2);
}
/// <summary>
/// Set data value to a float <see cref="Uniform{T,U,V,W}"/>,
/// all types of parameters are float.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T,U,V,W}"/> target</param>
/// <param name="v0">The first value to be set</param>
/// <param name="v1">The second value to be set</param>
/// <param name="v2">The third value to be set</param>
/// <param name="v3">The forth value to be set</param>
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
api.Uniform4f(uniform.id, v0, v1, v2, v3);
api->glUniform4f(uniform.id, v0, v1, v2, v3);
}
/// <summary>
/// Set data value to a float <see cref="Uniform{T,U,V,W}"/>,
/// all types of parameters are float.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T,U,V,W}"/> target</param>
/// <param name="vec">A <see cref="Vec4d"/> packing four parameters of the uniform </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
{
api.Uniform1fv(uniform.id, 4, &vec.fvec);
api->glUniform1fv(uniform.id, 4, &vec);
}
/// <summary>
/// Set data value to a float <see cref="Uniform{T}"/>,
/// all types of parameters are <see cref="Vec4d"/>.
/// </summary>
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
/// <param name="vec">A <see cref="Vec4d"/> value of the uniform </param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void SetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
api.Uniform1fv(uniform.id, 4, &vec.fvec);
api->glUniform1fv(uniform.id, 4, &vec);
}
}
internal partial class GLonUpdate
{
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => { api->glUniform3f(uniform.id, v0, v1, v2); }
);
}
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => { api->glUniform2f(uniform.id, v0, v1); }
);
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => { api->glUniform4f(uniform.id, v0, v1, v2, v3); }
);
}
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
Vec4d cache = vec;
api->glUniform1fv(uniform.id, 4, &cache);
}
);
}
void IglFunc.SetValue(Uniform<float> uniform, float v0)
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => { api->glUniform1f(uniform.id, v0); }
);
}
void IglFunc.SetValue(Uniform<int> uniform, int i0)
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() => { api->glUniform1d(uniform.id, i0); }
);
}
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
unsafe void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
{
throw new NotImplementedException();
glActionQueue.Enqueue(
() =>
{
Vec4d cache = vec;
api->glUniform1fv(uniform.id, 4, &cache);
}
);
}
}
}
File diff suppressed because it is too large Load Diff