forked from arookas/Demolisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLShader.cs
107 lines (90 loc) · 2.48 KB
/
GLShader.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
using OpenTK.Graphics.OpenGL;
using System;
using System.IO;
namespace Arookas.Demolisher
{
public class GLShader : IDisposable
{
bool isDisposed;
public int ID { get; private set; }
public string Source { get; private set; }
public ShaderType Type { get; private set; }
public string InfoLog { get { return GL.GetShaderInfoLog(ID); } }
public int this[ShaderParameter parameter]
{
get
{
if (!parameter.IsDefined())
{
throw new ArgumentOutOfRangeException("parameter", parameter, "The specified shader parameter was not a defined OpenTK.Graphics.OpenGL.ShaderParameter value.");
}
int value;
GL.GetShader(ID, parameter, out value);
return value;
}
}
GLShader(int id, ShaderType type, string source)
{
ID = id;
Type = type;
Source = source;
}
public void Dispose()
{
if (!isDisposed)
{
int deleteStatus;
GL.DeleteShader(ID);
GL.GetShader(ID, ShaderParameter.DeleteStatus, out deleteStatus);
if (deleteStatus != 1)
{
throw new InvalidOperationException("The GLShader failed to be deleted.");
}
isDisposed = true;
}
}
public static GLShader FromFile(ShaderType shaderType, string path)
{
if (path == null)
{
throw new ArgumentNullException("path");
}
return FromSource(shaderType, File.ReadAllText(path));
}
public static GLShader FromSource(ShaderType shaderType, string source)
{
if (!shaderType.IsDefined())
{
throw new ArgumentOutOfRangeException("shaderType", shaderType, "The specified shader type was not a defined OpenTK.Graphics.OpenGL.ShaderType value.");
}
if (source == null)
{
throw new ArgumentNullException("source");
}
int id = GL.CreateShader(shaderType);
GL.ShaderSource(id, source);
GL.CompileShader(id);
int compileResult;
GL.GetShader(id, ShaderParameter.CompileStatus, out compileResult);
if (compileResult != 1)
{
string infoLog = GL.GetShaderInfoLog(id);
GL.DeleteShader(id);
throw new ArgumentException(String.Format("The GLSL shader of type {0} failed to compile. The info log is:\n{1}", shaderType, infoLog), "source");
}
return new GLShader(id, shaderType, source);
}
public static GLShader FromSource(ShaderType shaderType, string[] source)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
return FromSource(shaderType, String.Concat(source));
}
public static implicit operator int(GLShader shader)
{
return shader.ID;
}
}
}