-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathShaderSource.cs
More file actions
41 lines (38 loc) · 1.46 KB
/
ShaderSource.cs
File metadata and controls
41 lines (38 loc) · 1.46 KB
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
36
37
38
39
40
41
using Silk.NET.OpenGL;
using System;
namespace OpenGL_Demo
{
public class ShaderSource : Shader
{
public ShaderSource(string vertSrc, string fragSrc)
{
uint vert = LoadShader(ShaderType.VertexShader, vertSrc);
uint frag = LoadShader(ShaderType.FragmentShader, fragSrc);
_handle = Program.GL.CreateProgram();
Program.GL.AttachShader(_handle, vert);
Program.GL.AttachShader(_handle, frag);
Program.GL.LinkProgram(_handle);
Program.GL.GetProgram(_handle, GLEnum.LinkStatus, out var status);
if (status == 0)
{
throw new Exception($"Program failed to link with error: {Program.GL.GetProgramInfoLog(_handle)}");
}
Program.GL.DetachShader(_handle, vert);
Program.GL.DetachShader(_handle, frag);
Program.GL.DeleteShader(vert);
Program.GL.DeleteShader(frag);
}
uint LoadShader(ShaderType type, string src)
{
uint handle = Program.GL.CreateShader(type);
Program.GL.ShaderSource(handle, src);
Program.GL.CompileShader(handle);
string infoLog = Program.GL.GetShaderInfoLog(handle);
if (!string.IsNullOrWhiteSpace(infoLog))
{
throw new Exception($"Error compiling shader of type {type}, failed with error {infoLog}");
}
return handle;
}
}
}