-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cs
More file actions
67 lines (57 loc) · 2.19 KB
/
ShaderProgram.cs
File metadata and controls
67 lines (57 loc) · 2.19 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
using OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Licenta_ver1
{
/// <summary>
/// Program abstraction.
/// </summary>
public class ShaderProgram : IDisposable
{
public ShaderProgram(string[] vertexSource, string[] fragmentSource)
{
// Create vertex and frament shaders
// Note: they can be disposed after linking to program; resources are freed when deleting the program
using (OpenGL_Object vObject = new OpenGL_Object(ShaderType.VertexShader, vertexSource))
using (OpenGL_Object fObject = new OpenGL_Object(ShaderType.FragmentShader, fragmentSource))
{
// Create program
ProgramName = Gl.CreateProgram();
// Attach shaders
Gl.AttachShader(ProgramName, vObject.ShaderName);
Gl.AttachShader(ProgramName, fObject.ShaderName);
// Link program
Gl.LinkProgram(ProgramName);
// Check linkage status
int linked;
Gl.GetProgram(ProgramName, ProgramProperty.LinkStatus, out linked);
if (linked == 0)
{
int infologLength;
StringBuilder infolog = new StringBuilder(1024);
Gl.GetProgramInfoLog(ProgramName, 1024, out infologLength, infolog);
throw new InvalidOperationException($"unable to link program: {infolog}");
}
}
}
Dictionary<string, int> UniformLocations = new();
public int GetUniformLocation(string name)
{
if (UniformLocations.TryGetValue(name, out int location))
return location;
location = Gl.GetUniformLocation(ProgramName, name);
if (location < 0)
throw new InvalidOperationException($"No uniform |{name}|.");
UniformLocations[name] = location;
return location;
}
public readonly uint ProgramName;
public void Dispose()
{
Gl.DeleteProgram(ProgramName);
}
}
}