-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cs
178 lines (141 loc) · 4.91 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace GLTerrain {
public class ShaderProgram : IDisposable {
public int ID { get; private set; }
public bool IsAttached { get; private set; }
public Shader VertexShader { get; private set; }
public Shader FragmentShader { get; private set; }
public ShaderProgram(string vertex, string fragment) {
int id = GL.CreateProgram();
if (!String.IsNullOrEmpty(vertex)) {
VertexShader = new Shader(vertex, ShaderType.VertexShader);
GL.AttachShader(id, VertexShader.ID);
}
if (!String.IsNullOrEmpty(fragment)) {
FragmentShader = new Shader(fragment, ShaderType.FragmentShader);
GL.AttachShader(id, FragmentShader.ID);
}
if (VertexShader == null && FragmentShader == null) {
throw new ArgumentException("Shader source must not be empty/null!");
}
GL.LinkProgram(id);
GL.ValidateProgram(id);
int status = 0;
GL.GetProgram(id, ProgramParameter.LinkStatus, out status);
if (status == 0) {
string info = GL.GetProgramInfoLog(id);
throw new Exception(info);
}
ErrorCode code = GL.GetError();
if (code != ErrorCode.NoError) {
throw new Exception(code.ToString());
}
ID = id;
}
public void SetUniform(string name, int value) {
bool attached = false;
if (!IsAttached) {
attached = IsAttached;
Attach();
}
int location = GL.GetUniformLocation(ID, name);
GL.Uniform1(location, value);
if (!attached) {
Detach();
}
}
public void SetUniform(string name, float value) {
bool attached = false;
if (!IsAttached) {
attached = IsAttached;
Attach();
}
int location = GL.GetUniformLocation(ID, name);
GL.Uniform1(location, value);
if (!attached) {
Detach();
}
}
public void SetUniform(string name, Vector3 value) {
bool attached = false;
if (!IsAttached) {
attached = IsAttached;
Attach();
}
int location = GL.GetUniformLocation(ID, name);
GL.Uniform3(location, ref value);
if (!attached) {
Detach();
}
}
public void SetUniform(string name, bool value) {
bool attached = false;
if (!IsAttached) {
attached = IsAttached;
Attach();
}
int location = GL.GetUniformLocation(ID, name);
GL.Uniform1(location, value ? 1 : 0);
if (!attached) {
Detach();
}
}
public unsafe void SetUniform(string name, float[] values) {
bool attached = false;
if (!IsAttached) {
attached = IsAttached;
Attach();
}
int location = GL.GetUniformLocation(ID, name);
fixed (float * valuePtr = values) {
GL.Uniform1(location, values.Length, valuePtr);
}
if (!attached) {
Detach();
}
}
public void Attach() {
GL.UseProgram(ID);
IsAttached = true;
}
public void Detach() {
GL.UseProgram(0);
IsAttached = false;
}
public void Dispose() {
GL.DeleteProgram(ID);
}
}
public class Shader : IDisposable {
public int ID { get; private set; }
public ShaderType Type { get; private set; }
public Shader(string source, ShaderType type) {
Type = type;
ID = -1;
int id = GL.CreateShader(type);
GL.ShaderSource(id, source);
ErrorCode code = GL.GetError();
if (code != ErrorCode.NoError) {
throw new Exception(code.ToString());
}
GL.CompileShader(id);
int status = 0;
GL.GetShader(id, ShaderParameter.CompileStatus, out status);
if (status == 0) {
string info = GL.GetShaderInfoLog(id);
throw new Exception("In " + type.ToString() + ": " + Environment.NewLine + info);
}
code = GL.GetError();
if (code != ErrorCode.NoError) {
throw new Exception("In " + type.ToString() + ": " + Environment.NewLine + code.ToString());
}
ID = id;
}
public void Dispose() {
GL.DeleteShader(ID);
}
}
}