daily uodate on 20231208 by nitload youmu, preparation for multi thread rendering
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
*/
|
||||
|
||||
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
@@ -1900,7 +1902,7 @@ namespace StgSharp.Graphics
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexGenxOES(uint coord, uint pname, int param);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexGenxvOES(uint coord, uint pname, int* param);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage1D(uint target, int level, int internalformat, int width, int border, uint format, uint type, void* pixels);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage2D(uint target, int level, int internalformat, int width, int height, int border, uint format, uint type, void* pixels);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage2D(uint target, int level, uint internalformat, int width, int height, int border, uint format, uint type, void* pixels);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage2DMultisample(uint target, int samples, uint internalformat, int width, int height, bool fixedsamplelocations);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage2DMultisampleCoverageNV(uint target, int coverageSamples, int colorSamples, int internalFormat, int width, int height, bool fixedSampleLocations);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glTexImage3D(uint target, int level, int internalformat, int width, int height, int depth, int border, uint format, uint type, void* pixels);
|
||||
@@ -2013,7 +2015,7 @@ namespace StgSharp.Graphics
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1dv(int location, int count, double* value);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1f(int location, float v0);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1fARB(int location, float v0);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1fv(int location, int count, float* value);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1fv(int location, int count, M128F* value);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1fvARB(int location, int count, float* value);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1i(int location, int v0);
|
||||
[UnmanagedFunctionPointer(CallingConvention.StdCall)] internal unsafe delegate void glUniform1i64ARB(int location, long x);
|
||||
|
||||
@@ -9,7 +9,6 @@ namespace StgSharp.Graphics
|
||||
|
||||
#region glfw
|
||||
|
||||
|
||||
internal const int GLFW_VERSION_MAJOR = 3;
|
||||
internal const int GLFW_VERSION_MINOR = 3;
|
||||
internal const int GLFW_VERSION_REVISION = 8;
|
||||
@@ -308,10 +307,6 @@ namespace StgSharp.Graphics
|
||||
#region glad
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
internal const int _1PASS_EXT = 0x80A1;
|
||||
internal const int _1PASS_SGIS = 0x80A1;
|
||||
internal const int _2PASS_0_EXT = 0x80A2;
|
||||
|
||||
@@ -49,7 +49,18 @@
|
||||
{
|
||||
ARRAY_BUFFER = 0x8892,
|
||||
ELEMENT_ARRAY_BUFFER = 0x8893,
|
||||
ARRAY_BUFFER_BINDING = 0x8894
|
||||
ATOMIC_COUNTER_BUFFER = GLconst.ATOMIC_COUNTER_BUFFER,
|
||||
COPY_READ_BUFFER = GLconst.COPY_READ_BUFFER,
|
||||
COPY_WRITE_BUFFER = GLconst.COPY_WRITE_BUFFER,
|
||||
DISPATCH_INDIRECT_BUFFER = GLconst.DISPATCH_INDIRECT_BUFFER,
|
||||
DRAW_INDIRECT_BUFFER = GLconst.DRAW_INDIRECT_BUFFER,
|
||||
PIXEL_PACK_BUFFER = GLconst.PIXEL_PACK_BUFFER,
|
||||
PIXEL_UNPACK_BUFFER = GLconst.PIXEL_UNPACK_BUFFER,
|
||||
QUERY_BUFFER = GLconst.QUERY_BUFFER,
|
||||
SHADER_STORAGE_BUFFER = GLconst.SHADER_STORAGE_BUFFER,
|
||||
TEXTURE_BUFFER = GLconst.TEXTURE_BUFFER,
|
||||
TRANSFORM_FEEDBACK_BUFFER = GLconst.TRANSFORM_FEEDBACK_BUFFER,
|
||||
UNIFORM_BUFFER = GLconst.UNIFORM_BUFFER,
|
||||
}
|
||||
|
||||
public enum BufferUsage : int
|
||||
@@ -65,6 +76,13 @@
|
||||
DYNAMIC_COPY = 0x88EA
|
||||
}
|
||||
|
||||
public enum MaskBufferBit
|
||||
{
|
||||
COLOR = GLconst.COLOR_BUFFER_BIT,
|
||||
DEPTH = GLconst.DEPTH_BUFFER_BIT,
|
||||
STENCIL = GLconst.STENCIL_BUFFER_BIT,
|
||||
}
|
||||
|
||||
public enum GepmetryType : int
|
||||
{
|
||||
POINTS = 0x0000,
|
||||
@@ -76,5 +94,9 @@
|
||||
TRIANGLE_FAN = 0x0006
|
||||
}
|
||||
|
||||
|
||||
public enum TextureFiltering
|
||||
{
|
||||
Nearest = 0x2600,
|
||||
Linear = 0x2601
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
using StgSharp.Math;
|
||||
using StgSharp.Controlling;
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
@@ -22,38 +21,29 @@ namespace StgSharp.Graphics
|
||||
public string name;
|
||||
}
|
||||
|
||||
public unsafe partial class Form
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public unsafe abstract partial class Form
|
||||
{
|
||||
|
||||
internal LinkedList<Pool> allControl;
|
||||
internal LinkedList<Control> allControl;
|
||||
internal Vector4 color;
|
||||
internal Thread formThread;
|
||||
internal int height;
|
||||
internal IntPtr monitor;
|
||||
internal string name;
|
||||
internal Form sharedForm = null;
|
||||
internal Form sharedForm;
|
||||
internal int width;
|
||||
internal IntPtr windowID = default;
|
||||
internal IntPtr windowID;
|
||||
protected IglFunc GL;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor of a Form, creats an instance of a form and
|
||||
/// initilize all the customizing operations.
|
||||
/// </summary>
|
||||
public Form(int width, int height, string name, IntPtr monitor, Form sharedForm)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.name = name;
|
||||
this.sharedForm = sharedForm;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
internal Form()
|
||||
{ }
|
||||
private GLonRender glRender;
|
||||
private GLonUpdate glUpdate;
|
||||
|
||||
~Form()
|
||||
{
|
||||
GL.gl = default;
|
||||
Marshal.FreeHGlobal((IntPtr)contextID);
|
||||
glRender.api = default;
|
||||
}
|
||||
|
||||
public int Height
|
||||
@@ -78,10 +68,53 @@ namespace StgSharp.Graphics
|
||||
set => width = value;
|
||||
}
|
||||
|
||||
#region form main
|
||||
|
||||
public abstract void Deinit(object[] args);
|
||||
|
||||
public abstract void Init(object[] args);
|
||||
|
||||
public abstract void OnRenderFrame(object[] args);
|
||||
|
||||
public abstract void OnUpdateFrame(object[] args);
|
||||
|
||||
internal void internalShowForm(object arg)
|
||||
{
|
||||
object[] args = (object[])arg;
|
||||
Init((object[])args[0]);
|
||||
while (InternalIO.glfw.WindowShouldClose(windowID) == 0)
|
||||
{
|
||||
OnUpdateFrame((object[])args[1]);
|
||||
}
|
||||
Deinit((object[])args[2]);
|
||||
}
|
||||
|
||||
internal void InternalUpdateFrame(object args)
|
||||
{
|
||||
OnUpdateFrame((object[])args);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// Constructor of a Form, creats an instance of a form and
|
||||
/// initilize all the customizing operations.
|
||||
/// </summary>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public void InitForm(int width, int height, string name, IntPtr monitor, Form sharedForm)
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.name = name;
|
||||
this.sharedForm = sharedForm;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init an OpenGl context if current form has not been binded to any context.
|
||||
/// and set the context binded to current form as current OpenGL context.
|
||||
/// </summary>
|
||||
/// <exception cref="Exception">Something goes wrong and
|
||||
/// StgSharp fails in creating a new OpenGL context. </exception>
|
||||
public void MakeAsCurrentContext()
|
||||
{
|
||||
if (windowID == default)
|
||||
@@ -107,6 +140,7 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
windowID = windowPtr;
|
||||
}
|
||||
glRender = new GLonRender();
|
||||
glContext = new GLcontext();
|
||||
IntPtr ptr = Marshal.AllocHGlobal(sizeof(GLcontextIO));
|
||||
contextID = (GLcontextIO*)ptr;
|
||||
@@ -114,10 +148,10 @@ namespace StgSharp.Graphics
|
||||
InternalIO.InternalWriteLog("Successfully create a context binded to current form. " +
|
||||
$"Its ID is {(ulong)ptr}", LogType.Info);
|
||||
}
|
||||
GL.gl = glContext;
|
||||
InternalIO.InternalSetCurrentGLContext(windowID, (IntPtr)contextID);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Resize the current form
|
||||
/// </summary>
|
||||
@@ -172,5 +206,28 @@ namespace StgSharp.Graphics
|
||||
this.windowID,
|
||||
(int)minSize.X, (int)minSize.Y, (int)maxSize.X, (int)maxSize.Y);
|
||||
}
|
||||
|
||||
public void ShowForm(
|
||||
object[] initArgs,
|
||||
object[] updateArgs,
|
||||
object[] deinitArgs)
|
||||
{
|
||||
Thread t = new Thread(new ParameterizedThreadStart(internalShowForm));
|
||||
t.Start(new object[] { initArgs, updateArgs, deinitArgs });
|
||||
}
|
||||
|
||||
public void ShowForm()
|
||||
{
|
||||
Thread t = new Thread(new ParameterizedThreadStart(internalShowForm));
|
||||
t.Start(new object[3] { null, null, null });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bind this form to StgSharp platform.
|
||||
/// </summary>
|
||||
protected void BindToPlatform()
|
||||
{
|
||||
TimeLine.allForm.Add(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,46 +6,54 @@ using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal unsafe class ElementBuffer : IDisposable
|
||||
public partial class Form
|
||||
{
|
||||
uint[] _bufferID;
|
||||
|
||||
|
||||
public ElementBuffer(int n)
|
||||
protected ElementBuffer CreateElementBuffer(int n)
|
||||
{
|
||||
_bufferID = new uint[n];
|
||||
fixed (uint* id = _bufferID)
|
||||
return new ElementBuffer(n, GL);
|
||||
}
|
||||
|
||||
protected unsafe class ElementBuffer : IDisposable
|
||||
{
|
||||
uint[] _bufferID;
|
||||
private IglFunc GL;
|
||||
|
||||
internal ElementBuffer(int n, IglFunc glFunc)
|
||||
{
|
||||
GL.gl.GenBuffers(n, id);
|
||||
_bufferID = new uint[n];
|
||||
GL = glFunc;
|
||||
fixed (uint* id = _bufferID)
|
||||
{
|
||||
GL.GenBuffers(n, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint this[int n]
|
||||
{
|
||||
get { return _bufferID[n]; }
|
||||
}
|
||||
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.gl.BindBuffer(GLconst.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
|
||||
}
|
||||
|
||||
|
||||
public void SetData<T>(int index, T[] bufferArray, BufferUsage usage)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
GL.gl.BindBuffer(GLconst.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
public uint this[int n]
|
||||
{
|
||||
GL.gl.DeleteBuffers(1, id);
|
||||
get { return _bufferID[n]; }
|
||||
}
|
||||
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
|
||||
}
|
||||
|
||||
public void SetValue<T>(int index, T[] bufferArray, BufferUsage usage)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
GL.BindBuffer(BufferType.ELEMENT_ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ELEMENT_ARRAY_BUFFER, bufferArray, usage);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
{
|
||||
GL.DeleteBuffers(_bufferID.Length, id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal class Shader
|
||||
{
|
||||
public readonly uint programID;
|
||||
internal uint vertexShaderID;
|
||||
internal uint fragmentShaderID;
|
||||
|
||||
public unsafe Shader()
|
||||
{
|
||||
programID = GL.gl.CreateProgram();
|
||||
}
|
||||
|
||||
public unsafe void CompileVertexShader(string shaderCode)
|
||||
{
|
||||
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
|
||||
GL.gl.AttachShader(programID, vertexShaderID);
|
||||
}
|
||||
|
||||
public unsafe void CompileFragmentShader(string shaderCode)
|
||||
{
|
||||
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
|
||||
GL.gl.AttachShader(programID, fragmentShaderID);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public unsafe void Use()
|
||||
{
|
||||
GL.gl.UseProgram(programID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics.Buffer
|
||||
{
|
||||
public struct ShaderParameter<T>
|
||||
{
|
||||
private static readonly string[] supportedType = new string[] {
|
||||
"Single",
|
||||
"Int32",
|
||||
"Uint32",
|
||||
"Single*"
|
||||
};
|
||||
|
||||
internal int signiture;
|
||||
public readonly string name;
|
||||
|
||||
internal ShaderParameter(string name, int sign)
|
||||
{
|
||||
if (ShaderParameter<T>.supportedType.Contains(typeof(T).Name))
|
||||
{
|
||||
InternalIO.InternalWriteLog($"Invalid type of ShaderProgram named {name}.", LogType.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
signiture = sign;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal unsafe struct VertexArray : IDisposable
|
||||
public partial class Form
|
||||
{
|
||||
internal uint[] _bufferID;
|
||||
|
||||
public VertexArray(int n)
|
||||
protected VertexArray CreateVertexArray(int n)
|
||||
{
|
||||
_bufferID = new uint[n];
|
||||
fixed (uint* id = _bufferID)
|
||||
return new VertexArray(n, GL);
|
||||
}
|
||||
|
||||
protected unsafe struct VertexArray : IDisposable
|
||||
{
|
||||
internal uint[] _bufferID;
|
||||
private IglFunc GL;
|
||||
|
||||
internal VertexArray(int n, IglFunc glFunc)
|
||||
{
|
||||
GL.gl.GenVertexArrays(n, id);
|
||||
_bufferID = new uint[n];
|
||||
GL = glFunc;
|
||||
fixed (uint* id = _bufferID)
|
||||
{
|
||||
GL.GenVertexArrays(n, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint this[int n]
|
||||
{
|
||||
get
|
||||
public uint this[int n]
|
||||
{
|
||||
return _bufferID[n];
|
||||
get
|
||||
{
|
||||
return _bufferID[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.gl.BindVertexArray(_bufferID[index]);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.gl.DeleteVertexArrays(_bufferID.Length, id);
|
||||
GL.BindVertexArray(_bufferID[index]);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
{
|
||||
GL.DeleteVertexArrays(_bufferID.Length, id);
|
||||
}
|
||||
}
|
||||
|
||||
public void SetVertexAttribute(uint index, int vertexLength, Type dataType, bool isNomalized, int stride, int pointer)
|
||||
{
|
||||
GL.gl.VertexAttribPointer(
|
||||
index, vertexLength, InternalIO.GLtype[dataType],
|
||||
isNomalized,
|
||||
stride, (void*)pointer);
|
||||
public void BindNull()
|
||||
{
|
||||
GL.BindVertexArray(0);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SetVertexAttribute(uint index, int vertexLength, Type dataType, bool isNomalized, int stride, int pointer)
|
||||
{
|
||||
GL.SetVertexAttribute(
|
||||
index, vertexLength, dataType,
|
||||
isNomalized,
|
||||
stride, pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,69 +1,80 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Security;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public unsafe struct VertexBuffer : IDisposable
|
||||
public partial class Form
|
||||
{
|
||||
internal uint[] _bufferID;
|
||||
|
||||
|
||||
public VertexBuffer(int n)
|
||||
protected VertexBuffer CreateVertexBuffer(int n)
|
||||
{
|
||||
_bufferID = new uint[n];
|
||||
fixed (uint* ptr = _bufferID)
|
||||
return new VertexBuffer(n, GL);
|
||||
}
|
||||
|
||||
protected unsafe struct VertexBuffer : IDisposable
|
||||
{
|
||||
internal uint[] _bufferID;
|
||||
private IglFunc GL;
|
||||
|
||||
internal VertexBuffer(int n, IglFunc glFunc)
|
||||
{
|
||||
GL.gl.GenBuffers(n, ptr);
|
||||
_bufferID = new uint[n];
|
||||
GL = glFunc;
|
||||
fixed (uint* ptr = _bufferID)
|
||||
{
|
||||
GL.GenBuffers(n, ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public uint this[int n]
|
||||
{
|
||||
get { return _bufferID[n]; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static void Unbind()
|
||||
{
|
||||
GL.gl.BindBuffer(GLconst.ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.gl.BindBuffer(GLconst.ARRAY_BUFFER, _bufferID[index]);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
public uint this[int n]
|
||||
{
|
||||
GL.gl.DeleteBuffers(_bufferID.Length, id);
|
||||
get { return _bufferID[n]; }
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="bufferData"></param>
|
||||
/// <param name="readType"></param>
|
||||
public void SetData<T>(int index, T bufferData, BufferUsage usage) where T : IMat
|
||||
{
|
||||
GL.gl.BindBuffer((int)BufferType.ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public void Unbind()
|
||||
{
|
||||
GL.BindBuffer(BufferType.ARRAY_BUFFER, 0);
|
||||
}
|
||||
|
||||
}
|
||||
public void Bind(int index)
|
||||
{
|
||||
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
|
||||
}
|
||||
|
||||
public void SetDataArray<T>(int index, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
|
||||
{
|
||||
GL.gl.BindBuffer(GLconst.ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferArray, usage);
|
||||
public void Dispose()
|
||||
{
|
||||
fixed (uint* id = _bufferID)
|
||||
{
|
||||
GL.DeleteBuffers(_bufferID.Length, id);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="bufferData"></param>
|
||||
/// <param name="readType"></param>
|
||||
public void SetValue<T>(int index, T bufferData, BufferUsage usage) where T : IMat
|
||||
{
|
||||
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferData, usage);
|
||||
|
||||
}
|
||||
|
||||
public void SetValueArray<T>(int index, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
|
||||
{
|
||||
GL.BindBuffer(BufferType.ARRAY_BUFFER, _bufferID[index]);
|
||||
GL.SetBufferData(BufferType.ARRAY_BUFFER, bufferArray, usage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,148 @@
|
||||
using System;
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public unsafe class Texture
|
||||
public partial class Form
|
||||
{
|
||||
internal uint[] textureId;
|
||||
|
||||
public Texture(int n)
|
||||
protected Texture CreateTexture(int n)
|
||||
{
|
||||
textureId = new uint[n];
|
||||
fixed (uint* id = textureId)
|
||||
{
|
||||
GL.gl.GenTextures(n, id);
|
||||
}
|
||||
return new Texture(n, GL);
|
||||
}
|
||||
|
||||
public void Bind(int index)
|
||||
protected unsafe class Texture
|
||||
{
|
||||
GL.gl.BindTexture(GLconst.PROXY_TEXTURE_2D, textureId[index]);
|
||||
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 = glFunc;
|
||||
fixed (uint* id = textureId)
|
||||
{
|
||||
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
|
||||
{
|
||||
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,11 +9,16 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
public unsafe partial class Form
|
||||
{
|
||||
internal GLcontext glContext;
|
||||
internal GLcontextIO* contextID;
|
||||
|
||||
private int missingAPIcount = 0;
|
||||
private int problemAPIcount = 0;
|
||||
|
||||
internal GLcontext glContext
|
||||
{
|
||||
get { return glRender.api; }
|
||||
set { glRender.api = value; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,46 +7,57 @@ using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public static unsafe partial class GL
|
||||
|
||||
internal unsafe partial class GLonRender : IglFunc
|
||||
{
|
||||
internal unsafe static GLcontext gl;
|
||||
internal GLcontext api;
|
||||
|
||||
/// <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="form">The Form instance to be controlled</param>
|
||||
/// <param name="handler">Method hadler </param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetFrameBufferSizeCallback(Form form, FrameBufferSizeHandler handler)
|
||||
void IglFunc.ActiveTexture(uint type)
|
||||
{
|
||||
InternalIO.glfw.SetFramebufferSizeCallback(form.windowID, handler);
|
||||
api.ActiveTexture(type);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Call glClearColor in OpenGL
|
||||
/// specifies the red, green, blue, and alpha values used by glClear to clear the color buffers.
|
||||
/// Values specified by glClearColor are clamped to the range [0,1]
|
||||
/// </summary>
|
||||
/// <param name="R">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="G">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="B">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="A">Specify the red values used when the color buffers are cleared.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ClearColor(float R, float G, float B, float A)
|
||||
void IglFunc.AttachShader(uint program, uint shader)
|
||||
{
|
||||
gl.ClearColor(R, G, B, A);
|
||||
api.AttachShader(program, shader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Conpile the provided shader code in string form.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">A string represent the shader's code</param>
|
||||
/// <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 uint value as the only mark of conpiled shader</returns>
|
||||
public static uint CompileShader(string shaderCode, ShaderType shaderType)
|
||||
void IglFunc.BindBuffer(BufferType bufferType, uint index)
|
||||
{
|
||||
api.BindBuffer((uint)bufferType, index);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IglFunc.BindTexture(uint type, uint texture)
|
||||
{
|
||||
api.BindTexture(type, texture);
|
||||
}
|
||||
|
||||
void IglFunc.BindVertexArray(uint index)
|
||||
{
|
||||
api.BindVertexArray(index);
|
||||
}
|
||||
|
||||
void IglFunc.Clear(MaskBufferBit mask)
|
||||
{
|
||||
api.Clear((uint)mask);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.ClearColor(float R, float G, float B, float A)
|
||||
{
|
||||
api.ClearColor(R, G, B, A);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.ClearGraphicError()
|
||||
{
|
||||
while (api.GetError() != 0) { }
|
||||
}
|
||||
|
||||
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
|
||||
{
|
||||
uint shaderID;
|
||||
shaderCode += '\0';
|
||||
@@ -86,17 +97,13 @@ namespace StgSharp.Graphics
|
||||
return shaderID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a form and its associated
|
||||
/// </summary>
|
||||
/// <param name="width">Required width</param>
|
||||
/// <param name="height">Required width</param>
|
||||
/// <param name="tittle">Tittle of the window </param>
|
||||
/// <param name="monitor"></param>
|
||||
/// <param name="share"></param>
|
||||
/// <returns>The instance of created form</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public static unsafe Form CreateWindow(
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
uint IglFunc.CreateProgram()
|
||||
{
|
||||
return api.CreateProgram();
|
||||
}
|
||||
|
||||
unsafe IntPtr IglFunc.CreateWindowHandle(
|
||||
int width, int height, string tittle, IntPtr monitor, Form share)
|
||||
{
|
||||
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
|
||||
@@ -118,22 +125,29 @@ namespace StgSharp.Graphics
|
||||
}
|
||||
else
|
||||
{
|
||||
Form form = new Form();
|
||||
form.Size = new Vec2d(width, height);
|
||||
form.windowID = windowPtr;
|
||||
return form;
|
||||
return windowPtr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static unsafe uint CreatGraphicProgram()
|
||||
unsafe uint IglFunc.CreatGraphicProgram()
|
||||
{
|
||||
return gl.CreateProgram();
|
||||
return api.CreateProgram();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.DeleteBuffers(int count, uint* ptr)
|
||||
{
|
||||
api.DeleteBuffers(count, ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
|
||||
unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr)
|
||||
{
|
||||
api.DeleteVertexArrays(count, ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
|
||||
{
|
||||
uint a;
|
||||
switch (type.Name)
|
||||
@@ -154,60 +168,31 @@ namespace StgSharp.Graphics
|
||||
LogType.Error);
|
||||
return;
|
||||
}
|
||||
gl.DrawElements((uint)mode, count, a, (void*)ptr);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static unsafe bool GetWindowShouldClose(Form form)
|
||||
{
|
||||
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
|
||||
}
|
||||
|
||||
|
||||
public static unsafe void GLswapBuffers(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
InternalIO.glfw.SwapBuffers(form.windowID);
|
||||
}
|
||||
|
||||
|
||||
public static unsafe void MakeContexCurrent(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
InternalIO.glfw.MakeContextCurrent(form.windowID);
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) where T : IMat
|
||||
{
|
||||
int size = sizeof(T);
|
||||
gl.BufferData(
|
||||
(uint)bufferType,
|
||||
size,
|
||||
&bufferData,
|
||||
(uint)usage);
|
||||
api.DrawElements((uint)mode, count, a, (void*)ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void ClearGraphicError()
|
||||
unsafe void IglFunc.GenBuffers(int count, uint* ptr)
|
||||
{
|
||||
while (gl.GetError() != 0) { }
|
||||
api.GenBuffers(count, ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void GetGraphicError()
|
||||
unsafe void IglFunc.GenTextures(int count, uint* ptr)
|
||||
{
|
||||
api.GenTextures(count, ptr);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.GenVertexArrays(int count, uint* ptr)
|
||||
{
|
||||
api.GenVertexArrays(count, ptr);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.GetGraphicError()
|
||||
{
|
||||
string error = null;
|
||||
uint code = gl.GetError();
|
||||
uint code = api.GetError();
|
||||
while (code != 0)
|
||||
{
|
||||
error += $"OpenGL error {code}\n";
|
||||
@@ -232,20 +217,95 @@ namespace StgSharp.Graphics
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static unsafe void SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage) where T : struct, IConvertible
|
||||
unsafe int IglFunc.GetUniformLocation(uint program, byte* ptr)
|
||||
{
|
||||
return api.GetUniformLocation(program, ptr);
|
||||
}
|
||||
|
||||
unsafe bool IglFunc.GetWindowShouldClose(Form form)
|
||||
{
|
||||
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
|
||||
}
|
||||
|
||||
unsafe void IglFunc.MakeContexCurrent(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
InternalIO.glfw.MakeContextCurrent(form.windowID);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage)
|
||||
{
|
||||
int size = sizeof(T);
|
||||
api.BufferData(
|
||||
(uint)bufferType,
|
||||
size,
|
||||
&bufferData,
|
||||
(uint)usage);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
|
||||
{
|
||||
int size = bufferArray.Length * Marshal.SizeOf(typeof(T));
|
||||
|
||||
fixed (void* bufferPtr = &bufferArray[0])
|
||||
gl.BufferData(
|
||||
api.BufferData(
|
||||
(uint)bufferType,
|
||||
size,
|
||||
bufferPtr,
|
||||
(uint)usage);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
|
||||
{
|
||||
InternalIO.glfw.SetFramebufferSizeCallback(id, handler);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer)
|
||||
{
|
||||
uint type = InternalIO.GLtype[t];
|
||||
api.VertexAttribPointer(index, size, type, normalized, stride, (void*)pointer);
|
||||
api.EnableVertexAttribArray(index);
|
||||
}
|
||||
|
||||
unsafe void IglFunc.SwapBuffers(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
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)
|
||||
{
|
||||
api.TexImage2D(
|
||||
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);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IglFunc.UseProgram(uint program)
|
||||
{
|
||||
api.UseProgram(program);
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
void IglFunc.Viewport(int x, int y, int width, int height)
|
||||
{
|
||||
api.Viewport(x, y, width, height);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
using StgSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public partial class Form
|
||||
{
|
||||
/// <summary>
|
||||
/// Create an instance of <see cref="Shader"/> program
|
||||
/// </summary>
|
||||
/// <returns>New instance of <see cref="Shader"/> instance. </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected Shader CreateShader()
|
||||
{
|
||||
return new Shader(GL);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
|
||||
public class Uniform<T> where T : struct
|
||||
{
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(
|
||||
Form.Shader program, string name, IglFunc gL)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = gL;
|
||||
fixed (byte* ptr = namearray)
|
||||
{
|
||||
id = GL.GetUniformLocation(program.programID, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe Uniform(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public class Uniform<T, U>
|
||||
where T : struct
|
||||
where U : struct
|
||||
{
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(
|
||||
Form.Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = glFunc;
|
||||
fixed (byte* ptr = namearray)
|
||||
{
|
||||
id = GL.GetUniformLocation(program.programID, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe Uniform(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public class Uniform<T, U, V>
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
{
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
public unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = glFunc;
|
||||
fixed (byte* ptr = namearray)
|
||||
{
|
||||
id = GL.GetUniformLocation(program.programID, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe Uniform(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public class Uniform<T, U, V, W>
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
where W : struct
|
||||
{
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
fixed (byte* ptr = namearray)
|
||||
{
|
||||
id = GL.GetUniformLocation(program.programID, ptr);
|
||||
}
|
||||
GL = glFunc;
|
||||
}
|
||||
|
||||
internal unsafe Uniform(int id)
|
||||
{
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
api.Uniform1f(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)
|
||||
{
|
||||
api.Uniform1i(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)
|
||||
{
|
||||
api.Uniform2f(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)
|
||||
{
|
||||
api.Uniform3f(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)
|
||||
{
|
||||
api.Uniform4f(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>
|
||||
public unsafe void SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
|
||||
{
|
||||
api.Uniform1fv(uniform.id, 4, &vec.fvec);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public unsafe void SetValue(Uniform<Vec4d> uniform, Vec4d vec)
|
||||
{
|
||||
api.Uniform1fv(uniform.id, 4, &vec.fvec);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class GLonUpdate
|
||||
{
|
||||
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, 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> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,9 +22,17 @@ namespace StgSharp.Graphics
|
||||
|
||||
private static string glExtensionList;
|
||||
|
||||
|
||||
public static unsafe void LoadGLapi(Form form, GLfuncLoader loader)
|
||||
/// <summary>
|
||||
/// Load all aupportable OpenGL APIs and bind them to given <see cref="Form"/> context.
|
||||
/// </summary>
|
||||
/// <param name="form">The <see cref="Form"/> used as current OpenGL context</param>
|
||||
/// <param name="loader">A delegate that can get o</param>
|
||||
public static unsafe void LoadGLapi(this Form form, GLfuncLoader loader)
|
||||
{
|
||||
if (loader == null)
|
||||
{
|
||||
loader = LoadGLfunc;
|
||||
}
|
||||
if (form.contextID->GetString != IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
@@ -775,14 +783,14 @@ namespace StgSharp.Graphics
|
||||
|
||||
}
|
||||
|
||||
internal unsafe static string GetExtensions()
|
||||
internal unsafe static string GetExtensions(this Form form)
|
||||
{
|
||||
if (GL.gl == null)
|
||||
if (form.glContext == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return Marshal.PtrToStringAnsi(
|
||||
GL.gl.GetString(glExtension));
|
||||
form.glContext.GetString(glExtension));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
internal partial class GLonUpdate : IglFunc
|
||||
{
|
||||
void IglFunc.ActiveTexture(uint type)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.AttachShader(uint program, uint shader)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.BindBuffer(BufferType bufferType, uint index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
void IglFunc.BindTexture(uint type, uint texture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.BindVertexArray(uint index)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.Clear(MaskBufferBit mask)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
void IglFunc.ClearColor(float R, float G, float B, float A)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.ClearGraphicError()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
uint IglFunc.CompileShader(string shaderCode, ShaderType shaderType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
uint IglFunc.CreateProgram()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
IntPtr IglFunc.CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share)
|
||||
{
|
||||
byte[] tittleBytes = Encoding.UTF8.GetBytes(tittle);
|
||||
IntPtr windowPtr;
|
||||
if (share == null)
|
||||
{
|
||||
windowPtr = InternalIO.glfw.CreateWindow(
|
||||
width, height, tittleBytes, monitor, IntPtr.Zero);
|
||||
}
|
||||
else
|
||||
{
|
||||
windowPtr = InternalIO.glfw.CreateWindow(
|
||||
width, height, tittleBytes, monitor, share.windowID);
|
||||
}
|
||||
|
||||
if ((long)windowPtr == -1 || (long)windowPtr == -2)
|
||||
{
|
||||
throw new Exception("Cannot create window instance!");
|
||||
}
|
||||
else
|
||||
{
|
||||
return windowPtr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint IglFunc.CreatGraphicProgram()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
unsafe void IglFunc.DeleteBuffers(int count, uint* ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
unsafe void IglFunc.DeleteVertexArrays(int count, uint* ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
void IglFunc.DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
unsafe void IglFunc.GenBuffers(int count, uint* ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
unsafe void IglFunc.GenTextures(int count, uint* ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
unsafe void IglFunc.GenVertexArrays(int count, uint* ptr)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void GetGraphicError()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.GetGraphicError()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool GetWindowShouldClose(Form form)
|
||||
{
|
||||
return InternalIO.glfw.WindowShouldClose(form.windowID) == 0;
|
||||
}
|
||||
|
||||
bool IglFunc.GetWindowShouldClose(Form form)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void GLswapBuffers(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
InternalIO.glfw.SwapBuffers(form.windowID);
|
||||
}
|
||||
|
||||
public void MakeContexCurrent(Form form)
|
||||
{
|
||||
if (form.windowID == IntPtr.Zero)
|
||||
{
|
||||
return;
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
void IglFunc.SetBufferData<T>(BufferType bufferType, T[] bufferArray, BufferUsage usage)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
|
||||
{
|
||||
InternalIO.glfw.SetFramebufferSizeCallback(id, handler);
|
||||
}
|
||||
|
||||
void IglFunc.SetFrameBufferSizeCallback(IntPtr id, FrameBufferSizeHandler handler)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
void IglFunc.SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.SwapBuffers(Form form)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public unsafe void TextureImage2d(uint target, int level, uint internalformat, int width, int 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();
|
||||
}
|
||||
|
||||
void IglFunc.TextureParameter(uint target, uint pname, int param)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.UseProgram(uint rogram)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
void IglFunc.Viewport(int x, int y, int width, int height)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +1,16 @@
|
||||
using StgSharp.Controlling;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
namespace StgSharp.Controlling
|
||||
{
|
||||
public static unsafe partial class Graphic
|
||||
public static class Graphic
|
||||
{
|
||||
|
||||
|
||||
|
||||
internal static TDele ConvertAPI<TDele>(IntPtr funcPtr)
|
||||
{
|
||||
if (funcPtr == IntPtr.Zero)
|
||||
{
|
||||
throw new Exception("Failed to convert api, the function pointer is zero.");
|
||||
}
|
||||
try
|
||||
{
|
||||
return Marshal.GetDelegateForFunctionPointer<TDele>(funcPtr);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
internal static Delegate ConvertAPI(IntPtr funcPtr, Type T)
|
||||
public static void Render()
|
||||
{
|
||||
|
||||
if (funcPtr == IntPtr.Zero)
|
||||
{
|
||||
throw new Exception("Failed to convert api, the function pointer is zero.");
|
||||
}
|
||||
try
|
||||
{
|
||||
return Marshal.GetDelegateForFunctionPointer(funcPtr, T);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Init an instance of OpenGL program,
|
||||
/// This method should be called before other
|
||||
/// graphic methods, after LoadGL method.
|
||||
/// </summary>
|
||||
public static void InitGL(int majorVersion, int minorVersion)
|
||||
{
|
||||
if (InternalIO.glfw == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
InternalIO.glfw = new glFramework_PInvoke();
|
||||
InternalIO.glfw.Init();
|
||||
InternalIO.InternalWriteLog("Successfully load GLFW APIs. StgSharp is now working on p/invoke mode", LogType.Info);
|
||||
}
|
||||
catch (Exception sig)
|
||||
{
|
||||
string log = $"Failed to load GLFW APIs by P/Invoke," +
|
||||
$"StgSharp will switch to delegate mode. The detailed info is:\n" +
|
||||
$"{sig.Message}\n";
|
||||
|
||||
if (InternalIO.SSClibPtr == default)
|
||||
{
|
||||
InternalIO.SSClibPtr =
|
||||
InternalIO.InternalLoadWINlib(InternalIO.SSC_libname + ".dll");
|
||||
}
|
||||
|
||||
InternalIO.glfw = new glFramework_Delegate();
|
||||
InternalIO.SSClibPtr = InternalIO.InternalLoadWINlib(InternalIO.SSC_libname);
|
||||
FieldInfo[] glfwApis = typeof(glFramework_Delegate).GetFields();
|
||||
foreach (FieldInfo item in glfwApis)
|
||||
{
|
||||
if (item.FieldType.BaseType == typeof(Delegate))
|
||||
{
|
||||
try
|
||||
{
|
||||
IntPtr funcptr = InternalIO.InternalGetWINproc(
|
||||
InternalIO.SSClibPtr, item.Name);
|
||||
item.SetValue(null, ConvertAPI(funcptr, item.FieldType));
|
||||
log += $"Successfully load GLFW API called {item.Name}";
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
log += $"Failed to load GLFW API called {item.Name}, the detailed info is\n" +
|
||||
$"{ex.Message}";
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
InternalIO.InternalWriteLog(log, LogType.Warning);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
InternalIO.InternalWriteLog("Attempt to load GLFW APIs twice", LogType.Warning);
|
||||
}
|
||||
InternalIO.InternalInitGL(4, 6);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public interface IglFunc
|
||||
{
|
||||
/// <summary>
|
||||
/// Same fucntion as OpenGL api glAttachShader
|
||||
/// </summary>
|
||||
/// <param name="program">Shader program to be attachec</param>
|
||||
/// <param name="shader">A shader part to attach</param>
|
||||
void AttachShader(uint program, uint shader);
|
||||
|
||||
/// <summary>
|
||||
/// Bind the indexed buffer object to certain type of buffer
|
||||
/// </summary>
|
||||
/// <param name="bufferType"></param>
|
||||
/// <param name="index"></param>
|
||||
void BindBuffer(BufferType bufferType, uint index);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api, glBindVertexArray
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
void BindVertexArray(uint index);
|
||||
|
||||
/// <summary>
|
||||
/// Clear buffers to preset values
|
||||
/// </summary>
|
||||
/// <param name="mask"></param>
|
||||
void Clear(MaskBufferBit mask);
|
||||
|
||||
/// <summary>
|
||||
/// Call apiClearColor in OpenGL
|
||||
/// specifies the red, green, blue, and alpha values used by apiClear to clear the color buffers.
|
||||
/// Values specified by apiClearColor are clamped to the range [0,1]
|
||||
/// </summary>
|
||||
/// <param name="R">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="G">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="B">Specify the red values used when the color buffers are cleared.</param>
|
||||
/// <param name="A">Specify the red values used when the color buffers are cleared.</param>
|
||||
void ClearColor(float R, float G, float B, float A);
|
||||
void ClearGraphicError();
|
||||
|
||||
/// <summary>
|
||||
/// Conpile the provided shader code in string form.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">A string represent the shader's code</param>
|
||||
/// <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 uint value as the only mark of conpiled shader</returns>
|
||||
uint CompileShader(string shaderCode, ShaderType shaderType);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api, glCreateProgram
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
uint CreateProgram();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a form and its associated
|
||||
/// </summary>
|
||||
/// <param name="width">Required width</param>
|
||||
/// <param name="height">Required width</param>
|
||||
/// <param name="tittle">Tittle of the window </param>
|
||||
/// <param name="monitor"></param>
|
||||
/// <param name="share"></param>
|
||||
/// <returns>The instance of created form</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
IntPtr CreateWindowHandle(int width, int height, string tittle, IntPtr monitor, Form share);
|
||||
|
||||
uint CreatGraphicProgram();
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api, glDeleteBuffer.
|
||||
/// </summary>
|
||||
/// <param name="count">Amount of bufferd to be deleted</param>
|
||||
/// <param name="ptr">Pointer of buffer to be deleted</param>
|
||||
unsafe void DeleteBuffers(int count, uint* ptr);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api, glDeleteVertexArrays
|
||||
/// </summary>
|
||||
/// <param name="count">Amount of vao to be deleted</param>
|
||||
/// <param name="ptr">pointer of vao sign array</param>
|
||||
unsafe void DeleteVertexArrays(int count, uint* ptr);
|
||||
|
||||
void DrawElements(GepmetryType mode, int count, Type type, IntPtr ptr);
|
||||
|
||||
/// <summary>
|
||||
/// Same fucntion as the OpenGL api glGenBuffers
|
||||
/// </summary>
|
||||
/// <param name="count"></param>
|
||||
/// <param name="ptr"></param>
|
||||
unsafe void GenBuffers(int count, uint* ptr);
|
||||
|
||||
/// <summary>
|
||||
/// Same fucntion as OpenGL api, glGenVertexArray
|
||||
/// </summary>
|
||||
/// <param name="count">Amount of vao to be generated</param>
|
||||
/// <param name="ptr">Pointer of vao sign array</param>
|
||||
unsafe void GenVertexArrays(int count, uint* ptr);
|
||||
|
||||
void GetGraphicError();
|
||||
|
||||
/// <summary>
|
||||
/// Same fucntion as OpenGL api, glGetUniformLocation
|
||||
/// </summary>
|
||||
/// <param name="program">Shader program to find the uniform</param>
|
||||
/// <param name="ptr">Pointer to byte array of name <see cref="string"/> of the uniform </param>
|
||||
/// <returns></returns>
|
||||
unsafe int GetUniformLocation(uint program, byte* ptr);
|
||||
|
||||
bool GetWindowShouldClose(Form form);
|
||||
|
||||
void MakeContexCurrent(Form form);
|
||||
|
||||
void SetBufferData<T>(BufferType bufferType, T bufferData, BufferUsage usage) where T : IMat;
|
||||
|
||||
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);
|
||||
|
||||
void SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3);
|
||||
|
||||
void SetValue(Uniform<float, float, float, float> uniform, Vec4d vec);
|
||||
|
||||
void SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2);
|
||||
|
||||
void SetValue(Uniform<float, float> uniform, float v0, float v1);
|
||||
|
||||
void SetValue(Uniform<float> uniform, float v0);
|
||||
|
||||
void SetValue(Uniform<int> uniform, int i0);
|
||||
|
||||
void SetValue(Uniform<Vec4d> uniform, Vec4d vec);
|
||||
|
||||
/// <summary>
|
||||
/// Set how a shader parameter read data from a VBO.
|
||||
/// </summary>
|
||||
/// <param name="index">Serial number of the parameter</param>
|
||||
/// <param name="size">Amount of data each elements contains</param>
|
||||
/// <param name="t">Type of data read by shader</param>
|
||||
/// <param name="normalized"></param>
|
||||
/// <param name="stride">Pixels stride of each sets of data</param>
|
||||
/// <param name="pointer"></param>
|
||||
void SetVertexAttribute(uint index, int size, Type t, bool normalized, int stride, int pointer);
|
||||
|
||||
void SwapBuffers(Form form);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api glTexImage2d
|
||||
/// </summary>
|
||||
/// <param name="target">texture target to load image in</param>
|
||||
/// <param name="level">level of mipmap, 0 if default</param>
|
||||
/// <param name="internalformat">OpenGL's internal color channel format</param>
|
||||
/// <param name="width">width of image</param>
|
||||
/// <param name="height">height of image</param>
|
||||
/// <param name="format">color channels of this image</param>
|
||||
/// <param name="type">how this image express each pixel</param>
|
||||
/// <param name="pixels">pointer to data array of pixels</param>
|
||||
unsafe void TextureImage2d(uint target, int level, uint internalformat, int width, int height, uint format, uint type, void* pixels);
|
||||
|
||||
/// <summary>
|
||||
/// Same fucntion as OpenGL api glTexParameteri
|
||||
/// </summary>
|
||||
/// <param name="target">texture target to set parameters</param>
|
||||
/// <param name="pname">name of parameter</param>
|
||||
/// <param name="param">value of parameter</param>
|
||||
void TextureParameter(uint target, uint pname, int param);
|
||||
|
||||
/// <summary>
|
||||
/// Same fucntion as OpenGL api glUseShader.
|
||||
/// </summary>
|
||||
/// <param name="program">Sign of program to be used</param>
|
||||
void UseProgram(uint program);
|
||||
|
||||
/// <summary>
|
||||
/// Same fucnction as OpenGL api glGenTextures.
|
||||
/// </summary>
|
||||
/// <param name="count">amount of textures to generate</param>
|
||||
/// <param name="ptr">pointer to texture signs' array</param>
|
||||
unsafe void GenTextures(int count, uint* ptr);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api, glBindTexture.
|
||||
/// </summary>
|
||||
/// <param name="type">Type of texture to bind</param>
|
||||
/// <param name="texture">Sign of texture</param>
|
||||
void BindTexture(uint type, uint texture);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as OpenGL api glActiveTexture
|
||||
/// </summary>
|
||||
/// <param name="type">type of texture to activate</param>
|
||||
void ActiveTexture(uint type);
|
||||
|
||||
/// <summary>
|
||||
/// Same function as the OpenGL api, glViewPort
|
||||
/// </summary>
|
||||
/// <param name="x"></param>
|
||||
/// <param name="y"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
void Viewport(int x, int y, int width, int height);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using StgSharp.Math;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.Graphics
|
||||
{
|
||||
public class Image
|
||||
{
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct ImageInfo
|
||||
{
|
||||
internal int width;
|
||||
internal int height;
|
||||
internal int channel;
|
||||
internal IntPtr pixelPtr;
|
||||
}
|
||||
|
||||
internal byte[] pixelArray;
|
||||
internal ImageInfo info;
|
||||
|
||||
public int Width { get => info.width; }
|
||||
public int Height { get => info.height; }
|
||||
public int Channel { get => info.channel; }
|
||||
|
||||
internal Image(ImageInfo information)
|
||||
{
|
||||
info = information;
|
||||
Marshal.Copy(
|
||||
source: info.pixelPtr,
|
||||
destination: pixelArray,
|
||||
startIndex: 0,
|
||||
length: info.width * info.height * info.channel
|
||||
);
|
||||
Marshal.FreeHGlobal(info.pixelPtr);
|
||||
}
|
||||
|
||||
public unsafe Image()
|
||||
{
|
||||
#if NET5_0_OR_GREATER
|
||||
Unsafe.SkipInit(out pixelArray);
|
||||
#endif
|
||||
info = new ImageInfo();
|
||||
}
|
||||
|
||||
~Image()
|
||||
{
|
||||
Marshal.FreeHGlobal(info.pixelPtr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user