192 lines
5.8 KiB
C#
192 lines
5.8 KiB
C#
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 ShaderProgram CreateShaderProgram(ShaderType shaderType)
|
|
{
|
|
return new ShaderProgram(GL, 1);
|
|
}
|
|
}
|
|
|
|
public class Shader
|
|
{
|
|
internal readonly glHandle handle;
|
|
public readonly ShaderType type;
|
|
private readonly IglFunc GL;
|
|
|
|
|
|
|
|
internal Shader(IglFunc gl, glHandle handle)
|
|
{
|
|
this.handle = handle;
|
|
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 Compile(string shaderCode, ShaderType usage, int index)
|
|
{
|
|
handle.handle[index] = GL.CompileShader(shaderCode, type).handle;
|
|
}
|
|
|
|
//TODO:写链接着色器的文档注释
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="target"></param>
|
|
/// <param name="usage"></param>
|
|
public void AttachTo(ShaderProgram target, ShaderType usage)
|
|
{
|
|
switch (usage)
|
|
{
|
|
case ShaderType.Fragment:
|
|
|
|
break;
|
|
case ShaderType.Vertex:
|
|
GL.AttachShader(target.handle,)
|
|
break;
|
|
default:
|
|
throw new ArgumentException();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public class ShaderProgram
|
|
{
|
|
internal readonly glHandle handle;
|
|
private readonly IglFunc GL;
|
|
|
|
/// <summary>
|
|
/// Inti a shader with no program attached.
|
|
/// </summary>
|
|
internal unsafe ShaderProgram(IglFunc gl, int count)
|
|
{
|
|
GL = gl;
|
|
handle = GL.CreateProgram();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Link the shader to current context.
|
|
/// </summary>
|
|
public void Link()
|
|
{
|
|
if (InternalIO.InternalLinkShaderProgram(handle[0]) == 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(handle[0], ptr);
|
|
}
|
|
if (uniform != -1)
|
|
{
|
|
return new Uniform<T>(uniform);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Cannot find the uniform {name} in shader {handle[0]}");
|
|
}
|
|
}
|
|
/// <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(handle[0], ptr);
|
|
}
|
|
if (uniform != -1)
|
|
{
|
|
return new Uniform<T, U>(uniform);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Cannot find the uniform {name} in shader {handle[0]}");
|
|
}
|
|
}
|
|
/// <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>(int index, 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(handle[index], ptr);
|
|
}
|
|
if (uniform != -1)
|
|
{
|
|
return new Uniform<T, U, V>(uniform);
|
|
}
|
|
else
|
|
{
|
|
throw new Exception($"Cannot find the uniform {name} in shader {handle[index]}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Let the current context use this shader.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public unsafe void Use(int index)
|
|
{
|
|
GL.UseProgram(handle[index]);
|
|
}
|
|
|
|
}
|
|
}
|