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.arbshader;
30 
31 private
32 {
33     import std.stdio;
34     import std.string;
35     import std.conv;
36 
37     import dlib.core.memory;
38 
39     import derelict.opengl.gl;
40     import derelict.opengl.glext;
41 
42     import dgl.graphics.shader;
43 }
44 
45 final class ARBShader: Shader
46 {
47     GLuint vertShader;
48     GLuint fragShader;
49     bool _supported;
50 
51     this(string vertexProgram, string fragmentProgram)
52     {
53         _supported = supported;
54 
55         if (_supported)
56         {
57             int err;
58 
59             // Vertex shader
60             glEnable(GL_VERTEX_PROGRAM_ARB);
61             glGenProgramsARB(1, &vertShader);
62             glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertShader);
63             glProgramStringARB(GL_VERTEX_PROGRAM_ARB,
64                                GL_PROGRAM_FORMAT_ASCII_ARB,
65                                cast(uint)vertexProgram.length,
66                                toStringz(vertexProgram));
67             glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &err);
68             if (err >= 0)
69                 writefln("Error in vertex shader:\n%s",
70     		        to!string(glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
71 
72             // Fragment shader
73             glEnable(GL_FRAGMENT_PROGRAM_ARB);
74             glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragShader);
75             glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB,
76                                GL_PROGRAM_FORMAT_ASCII_ARB,
77                                cast(uint)fragmentProgram.length,
78                                toStringz(fragmentProgram));
79             glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &err);
80             if (err >= 0)
81                 writefln("Error in fragment shader:\n%s",
82     		        to!string(glGetString(GL_PROGRAM_ERROR_STRING_ARB)));
83 
84             unbind();
85         }
86     }
87 
88     override @property bool supported()
89     {
90         return DerelictGL.isExtensionSupported("GL_ARB_vertex_program") &&
91                DerelictGL.isExtensionSupported("GL_ARB_fragment_program");
92     }
93 
94     override void bind(double delta)
95     {
96         if (_supported)
97         {
98             glEnable(GL_VERTEX_PROGRAM_ARB);
99             glEnable(GL_FRAGMENT_PROGRAM_ARB);
100             glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertShader);
101             glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragShader);
102         }
103     }
104 
105     override void unbind()
106     {
107         if (_supported)
108         {
109             glBindProgramARB(GL_VERTEX_PROGRAM_ARB, 0);
110             glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, 0);
111             glDisable(GL_VERTEX_PROGRAM_ARB);
112             glDisable(GL_FRAGMENT_PROGRAM_ARB);
113         }
114     }
115 
116     ~this()
117     {
118         unbind();
119     }
120 
121     void free()
122     {
123         Delete(this);
124     }
125 }