daily update on 20240104 by nitload youmu, double threading not work
This commit is contained in:
@@ -18,102 +18,158 @@ namespace StgSharp.Graphics
|
||||
{
|
||||
return new Shader(GL);
|
||||
}
|
||||
}
|
||||
|
||||
public class Shader
|
||||
public class Shader
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="uint"/> value represent this shader.
|
||||
/// </summary>
|
||||
public readonly uint programID;
|
||||
internal uint vertexShaderID;
|
||||
internal uint fragmentShaderID;
|
||||
private IglFunc GL;
|
||||
|
||||
/// <summary>
|
||||
/// Inti a shader with no program attached.
|
||||
/// </summary>
|
||||
internal unsafe Shader(IglFunc gl)
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="uint"/> value represent this shader.
|
||||
/// </summary>
|
||||
public readonly uint programID;
|
||||
internal uint vertexShaderID;
|
||||
internal uint fragmentShaderID;
|
||||
private IglFunc GL;
|
||||
|
||||
/// <summary>
|
||||
/// Inti a shader with no program attached.
|
||||
/// </summary>
|
||||
internal unsafe Shader(IglFunc glFunc)
|
||||
{
|
||||
GL = glFunc;
|
||||
programID = GL.CreateProgram();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile the vertex shader to be attached to this shader.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">GLSL code of vertex shader.</param>
|
||||
public unsafe void CompileVertexShader(string shaderCode)
|
||||
{
|
||||
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
|
||||
GL.AttachShader(programID, vertexShaderID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile the fragment shader to be attached to this shader.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">GLSL code of fragment shader.</param>
|
||||
public unsafe void CompileFragmentShader(string shaderCode)
|
||||
{
|
||||
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
|
||||
GL.AttachShader(programID, fragmentShaderID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Link the shader to current context.
|
||||
/// </summary>
|
||||
public void Link()
|
||||
{
|
||||
if (InternalIO.InternalLinkShaderProgram(programID) == 0)
|
||||
{
|
||||
IntPtr logPtr = InternalIO.InternalReadLog();
|
||||
try
|
||||
{
|
||||
byte[] logByte = new byte[512];
|
||||
Marshal.Copy(logPtr, logByte, 0, 512);
|
||||
string log = Encoding.UTF8.GetString(logByte);
|
||||
log = log.Replace("\0", "");
|
||||
InternalIO.InternalWriteLog(log, LogType.Error);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(name);
|
||||
int uniform;
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
uniform = GL.GetUniformLocation(programID, ptr);
|
||||
}
|
||||
if (uniform != -1)
|
||||
{
|
||||
return new Uniform<T>(uniform);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Let the current context use this shader.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Use()
|
||||
{
|
||||
GL.UseProgram(programID);
|
||||
}
|
||||
|
||||
GL = gl;
|
||||
programID = GL.CreateProgram();
|
||||
}
|
||||
|
||||
internal void ChangeEnvironment(IglFunc gl)
|
||||
{
|
||||
GL = gl;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile the vertex shader to be attached to this shader.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">GLSL code of vertex shader.</param>
|
||||
public unsafe void CompileVertexShader(string shaderCode)
|
||||
{
|
||||
vertexShaderID = GL.CompileShader(shaderCode, ShaderType.VERTEX);
|
||||
GL.AttachShader(programID, vertexShaderID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile the fragment shader to be attached to this shader.
|
||||
/// </summary>
|
||||
/// <param name="shaderCode">GLSL code of fragment shader.</param>
|
||||
public unsafe void CompileFragmentShader(string shaderCode)
|
||||
{
|
||||
fragmentShaderID = GL.CompileShader(shaderCode, ShaderType.FRAGMENT);
|
||||
GL.AttachShader(programID, fragmentShaderID);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Link the shader to current context.
|
||||
/// </summary>
|
||||
public void Link()
|
||||
{
|
||||
if (InternalIO.InternalLinkShaderProgram(programID) == 0)
|
||||
{
|
||||
IntPtr logPtr = InternalIO.InternalReadLog();
|
||||
try
|
||||
{
|
||||
byte[] logByte = new byte[512];
|
||||
Marshal.Copy(logPtr, logByte, 0, 512);
|
||||
string log = Encoding.UTF8.GetString(logByte);
|
||||
log = log.Replace("\0", "");
|
||||
InternalIO.InternalWriteLog(log, LogType.Error);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
InternalIO.InternalWriteLog(ex.Message, LogType.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(name);
|
||||
int uniform;
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
uniform = GL.GetUniformLocation(programID, ptr);
|
||||
}
|
||||
if (uniform != -1)
|
||||
{
|
||||
return new Uniform<T>(uniform);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T, U> GetUniform<T, U>(string name)
|
||||
where T : struct
|
||||
where U : struct
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(name);
|
||||
int uniform;
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
uniform = GL.GetUniformLocation(programID, ptr);
|
||||
}
|
||||
if (uniform != -1)
|
||||
{
|
||||
return new Uniform<T, U>(uniform);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Get a uniform form current shader,
|
||||
/// the value type of the uniform should be provided.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the Uniform in shader code</param>
|
||||
/// <returns></returns>
|
||||
public unsafe Uniform<T, U, V> GetUniform<T, U, V>(string name)
|
||||
where T : struct
|
||||
where U : struct
|
||||
where V : struct
|
||||
{
|
||||
byte[] bytes = Encoding.ASCII.GetBytes(name);
|
||||
int uniform;
|
||||
fixed (byte* ptr = bytes)
|
||||
{
|
||||
uniform = GL.GetUniformLocation(programID, ptr);
|
||||
}
|
||||
if (uniform != -1)
|
||||
{
|
||||
return new Uniform<T, U, V>(uniform);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Cannot find the uniform {name} in shader {programID}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Let the current context use this shader.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void Use()
|
||||
{
|
||||
GL.UseProgram(programID);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
@@ -14,13 +15,13 @@ namespace StgSharp.Graphics
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(
|
||||
Form.Shader program, string name, IglFunc gL)
|
||||
uint shaderID, string name, IglFunc gL)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = gL;
|
||||
fixed (byte* ptr = namearray)
|
||||
{
|
||||
id = GL.GetUniformLocation(program.programID, ptr);
|
||||
id = GL.GetUniformLocation(shaderID, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +41,7 @@ namespace StgSharp.Graphics
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(
|
||||
Form.Shader program, string name, IglFunc glFunc)
|
||||
Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = glFunc;
|
||||
@@ -64,7 +65,7 @@ namespace StgSharp.Graphics
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
public unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
|
||||
public unsafe Uniform(Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
GL = glFunc;
|
||||
@@ -89,7 +90,7 @@ namespace StgSharp.Graphics
|
||||
internal int id;
|
||||
private IglFunc GL;
|
||||
|
||||
internal unsafe Uniform(Form.Shader program, string name, IglFunc glFunc)
|
||||
internal unsafe Uniform(Shader program, string name, IglFunc glFunc)
|
||||
{
|
||||
byte[] namearray = Encoding.ASCII.GetBytes(name);
|
||||
fixed (byte* ptr = namearray)
|
||||
@@ -108,125 +109,107 @@ namespace StgSharp.Graphics
|
||||
|
||||
internal partial class GLonRender : IglFunc
|
||||
{
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T}"/>,
|
||||
/// all types of parameters are float.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
|
||||
/// <param name="v0">The value to be set</param>
|
||||
void IglFunc.SetValue(Uniform<float> uniform, float v0)
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
|
||||
{
|
||||
api.Uniform1f(uniform.id, v0);
|
||||
api->glUniform1f(uniform.id, v0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a int <see cref="Uniform{T}"/>,
|
||||
/// all types of parameters are int.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
|
||||
/// <param name="i0">The value to be set</param>
|
||||
void IglFunc.SetValue(Uniform<int> uniform, int i0)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
|
||||
{
|
||||
api.Uniform1i(uniform.id, i0);
|
||||
api->glUniform1i(uniform.id, i0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T,U}"/>,
|
||||
/// all types of parameters are float.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T,U}"/> target</param>
|
||||
/// <param name="v0">The first value to be set</param>
|
||||
/// <param name="v1">The second value to be set</param>
|
||||
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
|
||||
{
|
||||
api.Uniform2f(uniform.id, v0, v1);
|
||||
api->glUniform2f(uniform.id, v0, v1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T,U,V}"/>,
|
||||
/// all types of parameters are float.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T,U,V}"/> target</param>
|
||||
/// <param name="v0">The first value to be set</param>
|
||||
/// <param name="v1">The second value to be set</param>
|
||||
/// <param name="v2">The third value to be set</param>
|
||||
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
|
||||
{
|
||||
api.Uniform3f(uniform.id, v0, v1, v2);
|
||||
api->glUniform3f(uniform.id, v0, v1, v2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T,U,V,W}"/>,
|
||||
/// all types of parameters are float.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T,U,V,W}"/> target</param>
|
||||
/// <param name="v0">The first value to be set</param>
|
||||
/// <param name="v1">The second value to be set</param>
|
||||
/// <param name="v2">The third value to be set</param>
|
||||
/// <param name="v3">The forth value to be set</param>
|
||||
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
|
||||
{
|
||||
api.Uniform4f(uniform.id, v0, v1, v2, v3);
|
||||
api->glUniform4f(uniform.id, v0, v1, v2, v3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T,U,V,W}"/>,
|
||||
/// all types of parameters are float.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T,U,V,W}"/> target</param>
|
||||
/// <param name="vec">A <see cref="Vec4d"/> packing four parameters of the uniform </param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
|
||||
{
|
||||
api.Uniform1fv(uniform.id, 4, &vec.fvec);
|
||||
api->glUniform1fv(uniform.id, 4, &vec);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set data value to a float <see cref="Uniform{T}"/>,
|
||||
/// all types of parameters are <see cref="Vec4d"/>.
|
||||
/// </summary>
|
||||
/// <param name="uniform">The <see cref="Uniform{T}"/> target</param>
|
||||
/// <param name="vec">A <see cref="Vec4d"/> value of the uniform </param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public unsafe void SetValue(Uniform<Vec4d> uniform, Vec4d vec)
|
||||
{
|
||||
api.Uniform1fv(uniform.id, 4, &vec.fvec);
|
||||
api->glUniform1fv(uniform.id, 4, &vec);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class GLonUpdate
|
||||
{
|
||||
void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float, float> uniform, float v0, float v1, float v2)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() => { api->glUniform3f(uniform.id, v0, v1, v2); }
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float> uniform, float v0, float v1)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() => { api->glUniform2f(uniform.id, v0, v1); }
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, float v0, float v1, float v2, float v3)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() => { api->glUniform4f(uniform.id, v0, v1, v2, v3); }
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
|
||||
unsafe void IglFunc.SetValue(Uniform<float, float, float, float> uniform, Vec4d vec)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() =>
|
||||
{
|
||||
Vec4d cache = vec;
|
||||
api->glUniform1fv(uniform.id, 4, &cache);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<float> uniform, float v0)
|
||||
unsafe void IglFunc.SetValue(Uniform<float> uniform, float v0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() => { api->glUniform1f(uniform.id, v0); }
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<int> uniform, int i0)
|
||||
unsafe void IglFunc.SetValue(Uniform<int> uniform, int i0)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() => { api->glUniform1d(uniform.id, i0); }
|
||||
);
|
||||
}
|
||||
|
||||
void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
|
||||
unsafe void IglFunc.SetValue(Uniform<Vec4d> uniform, Vec4d vec)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
glActionQueue.Enqueue(
|
||||
() =>
|
||||
{
|
||||
Vec4d cache = vec;
|
||||
api->glUniform1fv(uniform.id, 4, &cache);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user