149 lines
4.6 KiB
C#
149 lines
4.6 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()
|
|
{
|
|
return new ShaderProgram(GL);
|
|
}
|
|
|
|
protected Shader CreateShader(ShaderType type, int count)
|
|
{
|
|
return new Shader(this.gl, new glHandleSet(count), type);
|
|
}
|
|
}
|
|
|
|
public class Shader
|
|
{
|
|
internal readonly glHandleSet handle;
|
|
public readonly ShaderType type;
|
|
private readonly IglFunc GL;
|
|
|
|
internal Shader(IglFunc gl, glHandleSet handle, ShaderType usage)
|
|
{
|
|
this.handle = handle;
|
|
GL = gl;
|
|
type = usage;
|
|
}
|
|
|
|
/// <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(ShaderType usage, params string[] code)
|
|
{
|
|
glHandleSet.Copy(GL.CompileShader(usage, code), handle);
|
|
}
|
|
|
|
//TODO:写链接着色器的文档注释
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="target"></param>
|
|
/// <param name="index"></param>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void AttachTo(int index, ShaderProgram target)
|
|
{
|
|
GL.AttachShader(target.handle, handle[index]);
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
{
|
|
GL = gl;
|
|
handle = GL.CreateProgram();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Link the shader to current context.
|
|
/// </summary>
|
|
public void Link()
|
|
{
|
|
if (InternalIO.InternalLinkShaderProgram(handle.Value) == 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>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public unsafe Uniform<T> GetUniform<T>(string name) where T : struct
|
|
{
|
|
return new Uniform<T>(this, name, GL);
|
|
}
|
|
/// <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
|
|
{
|
|
return new Uniform<T, U>(this, name, GL);
|
|
}
|
|
/// <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
|
|
{
|
|
return new Uniform<T, U, V>(this, name, GL);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Let the current context use this shader.
|
|
/// </summary>
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public unsafe void Use()
|
|
{
|
|
GL.UseProgram(handle);
|
|
}
|
|
|
|
}
|
|
}
|