61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using OpenTK.Graphics.OpenGL;
|
|
|
|
namespace Dashboard.Drawing.OpenGL
|
|
{
|
|
public static class ShaderUtil
|
|
{
|
|
public static int CompileShader(ShaderType type, string source)
|
|
{
|
|
int shader = GL.CreateShader(type);
|
|
|
|
GL.ShaderSource(shader, source);
|
|
GL.CompileShader(shader);
|
|
|
|
int compileStatus = 0;
|
|
GL.GetShaderi(shader, ShaderParameterName.CompileStatus, out compileStatus);
|
|
|
|
if (compileStatus == 0)
|
|
{
|
|
GL.GetShaderInfoLog(shader, out string log);
|
|
GL.DeleteShader(shader);
|
|
throw new Exception($"{type} Shader compilation failed: " + log);
|
|
}
|
|
|
|
return shader;
|
|
}
|
|
|
|
public static int CompileShader(ShaderType type, Stream stream)
|
|
{
|
|
using StreamReader reader = new StreamReader(stream, leaveOpen: true);
|
|
|
|
return CompileShader(type, reader.ReadToEnd());
|
|
}
|
|
|
|
public static int LinkProgram(int s1, int s2, IReadOnlyList<string>? attribLocations = null)
|
|
{
|
|
int program = GL.CreateProgram();
|
|
|
|
GL.AttachShader(program, s1);
|
|
GL.AttachShader(program, s2);
|
|
|
|
for (int i = 0; i < attribLocations?.Count; i++)
|
|
{
|
|
GL.BindAttribLocation(program, (uint)i, attribLocations[i]);
|
|
}
|
|
|
|
GL.LinkProgram(program);
|
|
|
|
int linkStatus = 0;
|
|
GL.GetProgrami(program, ProgramProperty.LinkStatus, out linkStatus);
|
|
if (linkStatus == 0)
|
|
{
|
|
GL.GetProgramInfoLog(program, out string log);
|
|
GL.DeleteProgram(program);
|
|
throw new Exception("Shader program linking failed: " + log);
|
|
}
|
|
|
|
return program;
|
|
}
|
|
}
|
|
}
|