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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user