aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Desktop/GlProgram.cs
blob: 25ebe5c650f32d3f964eb4ce623437f99c04945e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Silk.NET.OpenGL;

namespace Godosa.Desktop;

/// <summary>Shader program helpers: compile + link with the driver's info log in the
/// exception, so a bad shader fails loudly at startup instead of drawing nothing.</summary>
public static class GlProgram
{
    public static uint Link(GL gl, string vertexSource, string fragmentSource)
    {
        uint vs = Compile(gl, ShaderType.VertexShader, vertexSource);
        uint fs = Compile(gl, ShaderType.FragmentShader, fragmentSource);
        uint program = gl.CreateProgram();
        gl.AttachShader(program, vs);
        gl.AttachShader(program, fs);
        gl.LinkProgram(program);
        gl.GetProgram(program, ProgramPropertyARB.LinkStatus, out int linkStatus);
        if (linkStatus == 0)
            throw new InvalidOperationException($"Shader link failed: {gl.GetProgramInfoLog(program)}");
        gl.DeleteShader(vs);
        gl.DeleteShader(fs);
        return program;
    }

    public static uint Compile(GL gl, ShaderType type, string source)
    {
        uint shader = gl.CreateShader(type);
        gl.ShaderSource(shader, source);
        gl.CompileShader(shader);
        gl.GetShader(shader, ShaderParameterName.CompileStatus, out int status);
        if (status == 0)
            throw new InvalidOperationException($"Shader compile failed: {gl.GetShaderInfoLog(shader)}");
        return shader;
    }
}