-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.cs
77 lines (73 loc) · 1.93 KB
/
Shader.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace CG_II_OpenGL
{
public class Shader : IDisposable
{
public int Id => _program;
private readonly int _program;
private readonly List<int> _shaders = new List<int>();
public Shader()
{
_program = GL.CreateProgram();
}
public void AddShader(ShaderType type, string path)
{
var shader = GL.CreateShader(type);
var src = File.ReadAllText(path);
GL.ShaderSource(shader, src);
GL.CompileShader(shader);
checkForCompileErrors(shader);
_shaders.Add(shader);
}
public void Link()
{
foreach(var shader in _shaders)
{
GL.AttachShader(_program, shader);
}
GL.LinkProgram(_program);
checkForLinkingErrors(_program);
foreach(var shader in _shaders)
{
GL.DetachShader(_program, shader);
GL.DeleteShader(shader);
}
}
public void checkForCompileErrors(int shader)
{
int success;
GL.GetShader(shader, ShaderParameter.CompileStatus, out success);
if(success==0){
var infoLog = GL.GetShaderInfoLog(shader);
Debug.WriteLine($"GL.CompileShader [vertexShader] had info log {infoLog}");
}
}
public void checkForLinkingErrors(int program)
{
int success;
GL.GetProgram(program, GetProgramParameterName.LinkStatus, out success);
if(success==0)
{
var infoLog = GL.GetProgramInfoLog(program);
Debug.WriteLine($"GL.LinkProgram {program} log: {infoLog}");
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
GL.DeleteProgram(_program);
}
}
}
}