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
@@ -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();
}
}
}