1 /*
2 Copyright (c) 2013-2015 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 
6 Permission is hereby granted, free of charge, to any person or organization
7 obtaining a copy of the software and accompanying documentation covered by
8 this license (the "Software") to use, reproduce, display, distribute,
9 execute, and transmit the Software, and to prepare derivative works of the
10 Software, and to permit third-parties to whom the Software is furnished to
11 do so, all subject to the following:
12 
13 The copyright notices in the Software and this entire statement, including
14 the above license grant, this restriction and the following disclaimer,
15 must be included in all copies of the Software, in whole or in part, and
16 all derivative works of the Software, unless such copies or derivative
17 works are solely in the form of machine-executable object code generated by
18 a source language processor.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 DEALINGS IN THE SOFTWARE.
27 */
28 
29 module dgl.graphics.glslshader;
30 
31 import std.stdio;
32 import std.string;
33 import std.conv;
34 import dlib.core.memory;
35 import dlib.container.dict;
36 import dlib.math.vector;
37 import derelict.opengl.gl;
38 import derelict.opengl.glext;
39 import dgl.core.event;
40 import dgl.graphics.shader;
41 
42 class GLSLShader: Shader
43 {
44     EventManager eventManager;
45     GLenum shaderVert;
46     GLenum shaderFrag;
47     GLenum shaderProg;
48     bool _supported;
49 
50 	char*[9] texStrings;
51 
52     Dict!(bool, string) paramBool;
53 
54     this(EventManager emgr, string vertexProgram, string fragmentProgram)
55     {
56         eventManager = emgr;
57         _supported = supported;
58         
59         paramBool = dict!(bool, string);
60 
61         if (_supported)
62         {
63             shaderProg = glCreateProgramObjectARB();
64             shaderVert = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
65             shaderFrag = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
66 
67             int len;
68             char* srcptr;
69 
70             len = cast(int)vertexProgram.length;
71             srcptr = cast(char*)vertexProgram.ptr;
72             glShaderSourceARB(shaderVert, 1, &srcptr, &len);
73 
74             len = cast(int)fragmentProgram.length;
75             srcptr = cast(char*)fragmentProgram.ptr;
76             glShaderSourceARB(shaderFrag, 1, &srcptr, &len);
77 
78             glCompileShaderARB(shaderVert);
79             glCompileShaderARB(shaderFrag);
80             glAttachObjectARB(shaderProg, shaderVert);
81             glAttachObjectARB(shaderProg, shaderFrag);
82             glLinkProgramARB(shaderProg);
83 
84             char[1000] infobuffer = 0;
85             int infobufferlen = 0;
86 
87             glGetInfoLogARB(shaderVert, 999, &infobufferlen, infobuffer.ptr);
88             if (infobuffer[0] != 0)
89                 writefln("GLSL: error in vertex shader:\n%s\n", infobuffer.ptr.to!string);
90 
91             glGetInfoLogARB(shaderFrag, 999, &infobufferlen, infobuffer.ptr);
92             if (infobuffer[0] != 0)
93                 writefln("GLSL: error in fragment shader:\n%s\n", infobuffer.ptr.to!string);
94 
95             texStrings[0] = cast(char*)toStringz("dgl_Texture0");
96 			texStrings[1] = cast(char*)toStringz("dgl_Texture1");
97 			texStrings[2] = cast(char*)toStringz("dgl_Texture2");
98 			texStrings[3] = cast(char*)toStringz("dgl_Texture3");
99 			texStrings[4] = cast(char*)toStringz("dgl_Texture4");
100 			texStrings[5] = cast(char*)toStringz("dgl_Texture5");
101 			texStrings[6] = cast(char*)toStringz("dgl_Texture6");
102 			texStrings[7] = cast(char*)toStringz("dgl_Texture7");
103             texStrings[8] = cast(char*)toStringz("dgl_WindowSize");
104         }
105     }
106 
107     bool supported()
108     {
109         return DerelictGL.isExtensionSupported("GL_ARB_shading_language_100");
110     }
111     
112     void setParamBool(string name, bool v)
113     {
114         paramBool[name] = v;
115     }
116 
117     void bind(double delta)
118     {
119         if (_supported)
120         {
121             glUseProgramObjectARB(shaderProg);
122 
123             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[0]), 0);
124             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[1]), 1);
125             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[2]), 2);
126             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[3]), 3);
127             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[4]), 4);
128             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[5]), 5);
129             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[6]), 6);
130             glUniform1iARB(glGetUniformLocationARB(shaderProg, texStrings[7]), 7);
131 
132             glUniform2fARB(glGetUniformLocationARB(shaderProg, texStrings[8]), eventManager.windowWidth, eventManager.windowHeight);
133             
134             foreach(k, v; paramBool)
135                 glUniform1i(glGetUniformLocation(shaderProg, k.ptr), v);
136         }
137     }
138 
139     void unbind()
140     {
141         if (_supported)
142         {
143             glUseProgramObjectARB(0);
144         }
145     }
146 
147     ~this()
148     {
149         unbind();
150         Delete(paramBool);
151     }
152 
153     void free()
154     {
155         Delete(this);
156     }
157 }