1 module opengl.d.program;
2 
3 import opengl.gl4;
4 
5 import opengl.d.shader;
6 
7 ///
8 struct Program
9 {
10 	~this()
11 	{
12 		if (id)
13 		{
14 			foreach (shader; attached)
15 				glDetachShader(id, shader.id);
16 			glDeleteProgram(id);
17 		}
18 	}
19 
20 	/// Empty constructor that needs to be called explicitly
21 	static Program opCall()
22 	{
23 		Program ret;
24 		ret.id = glCreateProgram();
25 		return ret;
26 	}
27 
28 	///
29 	void attach(ref in Shader shader)
30 	{
31 		glAttachShader(id, shader.id);
32 		attached ~= shader;
33 	}
34 
35 	///
36 	void link()
37 	{
38 		glLinkProgram(id);
39 		int linked;
40 		glGetProgramiv(id, GL_LINK_STATUS, &linked);
41 		if (!linked)
42 		{
43 			char[4 * 1024] buffer;
44 			GLsizei len;
45 			glGetProgramInfoLog(id, buffer.length, &len, buffer.ptr);
46 			string source = "[";
47 			foreach (shader; attached)
48 				source ~= shader.source ~ ", ";
49 			if (attached.length)
50 				source.length -= 2;
51 			throw new Exception(source ~ "]: " ~ buffer[0 .. len].idup);
52 		}
53 	}
54 
55 	///
56 	void use()
57 	{
58 		glUseProgram(id);
59 	}
60 
61 	///
62 	uint id;
63 	Shader[] attached;
64 }