1 /*
2 Copyright (c) 2019 dayllenger
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.shaderloader;
29 
30 import std.stdio;
31 import std.string : stripRight;
32 import dlib.math.utils : min2;
33 import dagon.core.libs;
34 
35 enum ShaderStage : ubyte
36 {
37     vertex = 1,
38     tessControl = 2,
39     tessEval = 4,
40     geometry = 8,
41     fragment = 16,
42     compute = 32
43 }
44 
45 private GLenum shaderStageToGLenum(ShaderStage stage)
46 {
47     final switch (stage) with(ShaderStage)
48     {
49         case vertex:      return GL_VERTEX_SHADER;
50         case tessControl: return GL_TESS_CONTROL_SHADER;
51         case tessEval:    return GL_TESS_EVALUATION_SHADER;
52         case geometry:    return GL_GEOMETRY_SHADER;
53         case fragment:    return GL_FRAGMENT_SHADER;
54     static if (glSupport >= GLSupport.gl43)
55         case compute:     return GL_COMPUTE_SHADER;
56     else
57         case compute:     return 0;
58     }
59 }
60 
61 /// Compile single shader from source
62 GLuint compileShader(string source, const ShaderStage stage)
63 {
64     // create a shader
65     GLuint shaderID = glCreateShader(shaderStageToGLenum(stage));
66 
67     // compile the shader
68     const char* csource = source.ptr;
69     GLint length = cast(GLint)source.length;
70     glShaderSource(shaderID, 1, &csource, &length);
71     glCompileShader(shaderID);
72 
73     // check the shader
74     if (!checkCompilation(shaderID, stage))
75     {
76         shaderID = 0;
77         glDeleteShader(shaderID);
78     }
79 
80     return shaderID;
81 }
82 
83 /// Link compiled shaders
84 GLuint linkShaders(const GLuint[] shaderIDs...)
85 {
86     // create and link program
87     GLuint programID = glCreateProgram();
88     foreach(sh; shaderIDs)
89         glAttachShader(programID, sh);
90     glLinkProgram(programID);
91 
92     // check the program
93     if (!checkLinking(programID))
94     {
95         programID = 0;
96         glDeleteProgram(programID);
97     }
98 
99     // delete the program parts
100     foreach(sh; shaderIDs)
101     {
102         glDetachShader(programID, sh);
103         glDeleteShader(sh);
104     }
105 
106     return programID;
107 }
108 
109 private enum logMaxLen = 1023;
110 
111 private bool checkCompilation(const GLuint shaderID, const ShaderStage stage)
112 {
113     // get status
114     GLint status = GL_FALSE;
115     glGetShaderiv(shaderID, GL_COMPILE_STATUS, &status);
116     const bool ok = status != GL_FALSE;
117     // get log
118     GLint infolen;
119     glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infolen); // includes \0
120     if (infolen > 1)
121     {
122         char[logMaxLen + 1] infobuffer = 0;
123         glGetShaderInfoLog(shaderID, logMaxLen, null, infobuffer.ptr);
124         infolen = min2(infolen - 1, logMaxLen);
125         char[] s = stripRight(infobuffer[0 .. infolen]);
126         // it can be some warning
127         if (!ok)
128             writefln("Failed to compile %s shader:", stage);
129         writeln(s);
130     }
131     return ok;
132 }
133 
134 private bool checkLinking(const GLuint programID)
135 {
136     // get status
137     GLint status = GL_FALSE;
138     glGetProgramiv(programID, GL_LINK_STATUS, &status);
139     const bool ok = status != GL_FALSE;
140     // get log
141     GLint infolen;
142     glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infolen); // includes \0
143     if (infolen > 1)
144     {
145         char[logMaxLen + 1] infobuffer = 0;
146         glGetProgramInfoLog(programID, logMaxLen, null, infobuffer.ptr);
147         infolen = min2(infolen - 1, logMaxLen);
148         char[] s = stripRight(infobuffer[0 .. infolen]);
149         // it can be some warning
150         if (!ok)
151             writeln("Failed to link shaders:");
152         writeln(s);
153     }
154     return ok;
155 }