-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderFile.cs
More file actions
78 lines (65 loc) · 2.17 KB
/
ShaderFile.cs
File metadata and controls
78 lines (65 loc) · 2.17 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
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
using OpenTK.Graphics.OpenGL4;
using JLUtility;
namespace JLGraphics
{
internal sealed class ShaderFile : SafeDispose, IFileObject
{
internal ShaderType ShaderType { get; private set; }
public List<Action> FileChangeCallback => new List<Action>();
public string FilePath { get; }
public override string Name => FilePath;
public static implicit operator int(ShaderFile d) => d.compiledShader;
int compiledShader = 0;
bool addedCallback = false;
internal ShaderFile(string path, ShaderType shaderType)
{
FilePath = path;
ShaderType = shaderType;
if (!File.Exists(path))
{
Debug.Log("Shader not found: " + path, Debug.Flag.Error);
return;
}
}
string GetShaderString()
{
var data = File.ReadAllText(FilePath);
data = ShaderParser.ParseShaderPreDefines(data, FilePath);
return data;
}
internal bool CompileShader() {
if(compiledShader != 0)
{
Debug.Log("Shader file cache found!", Debug.Flag.Warning);
return false;
}
Debug.Log("Compiling Shader: " + FilePath);
string data = GetShaderString();
if (!addedCallback)
{
FileChangeCallback.Add(() => {
GL.DeleteShader(compiledShader);
compiledShader = 0;
compiledShader = GL.CreateShader(ShaderType);
CompileShader();
});
addedCallback = true;
}
compiledShader = GL.CreateShader(ShaderType);
GL.ShaderSource(compiledShader, data);
//compile the shaders
GL.CompileShader(compiledShader);
string d = GL.GetShaderInfoLog(compiledShader);
if (d != "")
{
Debug.Log(d);
return false;
}
return true;
}
protected override void OnDispose()
{
GL.DeleteShader(compiledShader);
}
}
}