1 module opengl.d.shader;
2 
3 import opengl.gl4;
4 
5 ///
6 enum ShaderType
7 {
8 	/// Vertex shader
9 	vertex = GL_VERTEX_SHADER,
10 	/// Tesselation control shader 
11 	tessControl = GL_TESS_CONTROL_SHADER,
12 	/// Tesselation evauluation shader
13 	tessEvaluation = GL_TESS_EVALUATION_SHADER,
14 	/// Geometry shader
15 	geometry = GL_GEOMETRY_SHADER,
16 	/// Fragment shader
17 	fragment = GL_FRAGMENT_SHADER,
18 	/// Compute shader
19 	compute = GL_COMPUTE_SHADER
20 }
21 
22 ///
23 struct Shader
24 {
25 	///
26 	this(ShaderType type)
27 	{
28 		id = glCreateShader(type);
29 	}
30 
31 	~this()
32 	{
33 		if (id)
34 		{
35 			glDeleteShader(id);
36 		}
37 	}
38 
39 	///
40 	nothrow @nogc void shaderSource(string str)
41 	{
42 		int len = cast(int) str.length;
43 		char* ptr = cast(char*) str.ptr;
44 		glShaderSource(id, 1, &ptr, &len);
45 	}
46 
47 	///
48 	nothrow void shaderSource(string[] strs)
49 	{
50 		int[] lens = new int[strs.length];
51 		char*[] ptrs = new char*[strs.length];
52 		foreach (i, ref str; strs)
53 		{
54 			lens[i] = cast(int) str.length;
55 			ptrs[i] = cast(char*) str.ptr;
56 		}
57 		glShaderSource(id, cast(GLsizei) strs.length, ptrs.ptr, lens.ptr);
58 	}
59 
60 	///
61 	void compile()
62 	{
63 		glCompileShader(id);
64 		int compiled;
65 		glGetShaderiv(id, GL_COMPILE_STATUS, &compiled);
66 		if (!compiled)
67 		{
68 			char[4 * 1024] buffer;
69 			GLsizei len;
70 			glGetShaderInfoLog(id, buffer.length, &len, buffer.ptr);
71 			throw new Exception(source ~ ':' ~ buffer[0 .. len].idup);
72 		}
73 	}
74 
75 	///
76 	uint id;
77 	/// Human readable name of the source
78 	string source;
79 }