diff options
| author | godosa <godosa@godosa.eu> | 2026-10-06 23:39:36 +0200 |
|---|---|---|
| committer | godosa <godosa@godosa.eu> | 2026-10-06 23:39:36 +0200 |
| commit | 39066900773e7857faf2d02e7ed51b71d219d97d (patch) | |
| tree | ffab8e4ddd971626776c3b1f4ce1da2c186c2187 /src/Godosa.Desktop/GlProgram.cs | |
| download | godosa-engine-39066900773e7857faf2d02e7ed51b71d219d97d.tar.gz godosa-engine-39066900773e7857faf2d02e7ed51b71d219d97d.zip | |
godosa-engine: initial public history
Diffstat (limited to 'src/Godosa.Desktop/GlProgram.cs')
| -rw-r--r-- | src/Godosa.Desktop/GlProgram.cs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/Godosa.Desktop/GlProgram.cs b/src/Godosa.Desktop/GlProgram.cs new file mode 100644 index 0000000..25ebe5c --- /dev/null +++ b/src/Godosa.Desktop/GlProgram.cs @@ -0,0 +1,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; + } +} |
