daily uodate on 20231208 by nitload youmu, preparation for multi thread rendering

This commit is contained in:
Nitload
2023-12-08 05:36:04 +08:00
parent ae804553ce
commit ab85c1a9fe
297 changed files with 157896 additions and 5170 deletions
+41 -33
View File
@@ -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;
}
}
}
+45 -29
View File
@@ -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);
}
}
}
}
+58 -47
View File
@@ -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);
}
}
}
}
+133 -12
View File
@@ -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,
}
}
+6 -1
View File
@@ -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();
}
}
}
+13 -5
View File
@@ -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)]