1#include "glsl_shader_compiler.h"
2
3#include <base/dbg.h>
4#include <base/str.h>
5
6#include <engine/graphics.h>
7
8CGLSLCompiler::CGLSLCompiler(int OpenGLVersionMajor, int OpenGLVersionMinor, int OpenGLVersionPatch, bool IsOpenGLES, float TextureLODBias)
9{
10 m_OpenGLVersionMajor = OpenGLVersionMajor;
11 m_OpenGLVersionMinor = OpenGLVersionMinor;
12 m_OpenGLVersionPatch = OpenGLVersionPatch;
13
14 m_IsOpenGLES = IsOpenGLES;
15
16 m_TextureLODBias = TextureLODBias;
17
18 m_HasTextureArray = false;
19 m_TextureReplaceType = ETextureReplaceType::TEXTURE_2D;
20}
21
22void CGLSLCompiler::AddDefine(const char *pDefineName, const char *pDefineValue)
23{
24 m_vDefines.emplace_back(args&: pDefineName, args&: pDefineValue);
25}
26
27void CGLSLCompiler::ClearDefines()
28{
29 m_vDefines.clear();
30}
31
32void CGLSLCompiler::ParseLine(std::string &Line, const char *pReadLine, EShaderType Type) const
33{
34 const bool IsNewOpenGL = !m_IsOpenGLES ?
35 m_OpenGLVersionMajor >= 4 || (m_OpenGLVersionMajor == 3 && m_OpenGLVersionMinor == 3) :
36 m_OpenGLVersionMajor >= 3;
37 if(!IsNewOpenGL)
38 {
39 ParseLineOldOpenGL(Line, pReadLine, Type);
40 }
41 else if(m_IsOpenGLES)
42 {
43 ParseLineGLES(Line, pReadLine);
44 }
45 else
46 {
47 Line = pReadLine;
48 }
49}
50
51void CGLSLCompiler::ParseLineOldOpenGL(std::string &Line, const char *pReadLine, EShaderType Type) const
52{
53 const char *pBuff = pReadLine;
54 char aTmpStr[1024];
55 size_t TmpStrSize = 0;
56 while(*pBuff)
57 {
58 while(*pBuff && str_isspace(c: *pBuff))
59 {
60 Line.append(n: 1, c: *pBuff);
61 ++pBuff;
62 }
63
64 while(*pBuff && TmpStrSize < sizeof(aTmpStr) - 1 && !str_isspace(c: *pBuff) && *pBuff != '(' && *pBuff != '.')
65 {
66 aTmpStr[TmpStrSize++] = *pBuff;
67 ++pBuff;
68 }
69
70 if(TmpStrSize > 0)
71 {
72 aTmpStr[TmpStrSize] = '\0';
73 TmpStrSize = 0;
74
75 if(str_comp(a: aTmpStr, b: "layout") == 0)
76 {
77 // search for ' in'
78 while(*pBuff && !str_startswith(str: pBuff, prefix: " in"))
79 {
80 ++pBuff;
81 }
82
83 if(const char *pInStart = str_startswith(str: pBuff, prefix: " in"))
84 {
85 pBuff = pInStart;
86 Line.append(s: "attribute");
87 Line.append(s: pBuff);
88 return;
89 }
90 else
91 {
92 dbg_msg(sys: "shadercompiler", fmt: "Fix shader for older OpenGL versions.");
93 }
94 }
95 else if(str_comp(a: aTmpStr, b: "noperspective") == 0 || str_comp(a: aTmpStr, b: "smooth") == 0 || str_comp(a: aTmpStr, b: "flat") == 0)
96 {
97 // search for 'in' or 'out'
98 while(*pBuff && !str_startswith(str: pBuff, prefix: "in") && !str_startswith(str: pBuff, prefix: "out"))
99 {
100 // append anything that is inbetween noperspective & in/out vars
101 Line.push_back(c: *pBuff);
102 ++pBuff;
103 }
104
105 if(const char *pInStart = str_startswith(str: pBuff, prefix: "in"))
106 {
107 pBuff = pInStart;
108 }
109 else if(const char *pOutStart = str_startswith(str: pBuff, prefix: "out"))
110 {
111 pBuff = pOutStart;
112 }
113 else
114 {
115 dbg_msg(sys: "shadercompiler", fmt: "Fix shader for older OpenGL versions.");
116 }
117
118 Line.append(s: "varying");
119 Line.append(s: pBuff);
120 return;
121 }
122 else if(str_comp(a: aTmpStr, b: "out") == 0 || str_comp(a: aTmpStr, b: "in") == 0)
123 {
124 if(Type == EShaderType::FRAGMENT && str_comp(a: aTmpStr, b: "out") == 0)
125 return;
126 Line.append(s: "varying");
127 Line.append(s: pBuff);
128 return;
129 }
130 else if(str_comp(a: aTmpStr, b: "FragClr") == 0)
131 {
132 Line.append(s: "gl_FragColor");
133 Line.append(s: pBuff);
134 return;
135 }
136 else if(str_comp(a: aTmpStr, b: "texture") == 0)
137 {
138 if(m_TextureReplaceType == ETextureReplaceType::TEXTURE_2D)
139 Line.append(s: "texture2D");
140 else if(m_TextureReplaceType == ETextureReplaceType::TEXTURE_3D)
141 Line.append(s: "texture3D");
142 else if(m_TextureReplaceType == ETextureReplaceType::TEXTURE_2D_ARRAY)
143 Line.append(s: "texture2DArray");
144 std::string RestLine;
145 ParseLine(Line&: RestLine, pReadLine: pBuff, Type);
146 Line.append(str: RestLine);
147 return;
148 }
149 else
150 {
151 Line.append(s: aTmpStr);
152 }
153 }
154
155 if(*pBuff)
156 {
157 Line.append(n: 1, c: *pBuff);
158 ++pBuff;
159 }
160 }
161}
162
163void CGLSLCompiler::ParseLineGLES(std::string &Line, const char *pReadLine) const
164{
165 const char *pBuff = pReadLine;
166 char aTmpStr[1024];
167 size_t TmpStrSize = 0;
168 while(*pBuff)
169 {
170 while(*pBuff && str_isspace(c: *pBuff))
171 {
172 Line.append(n: 1, c: *pBuff);
173 ++pBuff;
174 }
175
176 while(*pBuff && TmpStrSize < sizeof(aTmpStr) - 1 && !str_isspace(c: *pBuff) && *pBuff != '(' && *pBuff != '.')
177 {
178 aTmpStr[TmpStrSize++] = *pBuff;
179 ++pBuff;
180 }
181
182 if(TmpStrSize > 0)
183 {
184 aTmpStr[TmpStrSize] = '\0';
185 TmpStrSize = 0;
186
187 if(str_comp(a: aTmpStr, b: "noperspective") == 0)
188 {
189 // GLES does not support noperspective. Drop it to use the default (smooth) inexplicitly because shaders fail to compile on iOS otherwise.
190 // The centroid qualifier has to be dropped with it, because the qualifiers of the
191 // vertex shader outputs and the fragment shader inputs must match exactly on GLES.
192 if(const char *pCentroidEnd = str_startswith(str: str_skip_whitespaces_const(str: pBuff), prefix: "centroid"))
193 {
194 pBuff = pCentroidEnd;
195 }
196 Line.append(s: pBuff);
197 return;
198 }
199 // since GLES doesn't support texture LOD bias as global state, use the shader function instead(since GLES 3.0 uses shaders only anyway)
200 else if(str_comp(a: aTmpStr, b: "texture") == 0)
201 {
202 Line.append(s: "texture");
203 // check opening and closing brackets to find the end
204 int CurBrackets = 1;
205 while(*pBuff && *pBuff != '(')
206 {
207 Line.append(n: 1, c: *pBuff);
208 ++pBuff;
209 }
210
211 if(*pBuff)
212 {
213 Line.append(n: 1, c: *pBuff);
214 ++pBuff;
215 }
216
217 while(*pBuff)
218 {
219 if(*pBuff == '(')
220 ++CurBrackets;
221 if(*pBuff == ')')
222 --CurBrackets;
223
224 if(CurBrackets == 0)
225 {
226 // found end
227 Line.append(str: std::string(", ") + std::to_string(val: m_TextureLODBias) + ")");
228 ++pBuff;
229 break;
230 }
231 else
232 {
233 Line.append(n: 1, c: *pBuff);
234 }
235 ++pBuff;
236 }
237
238 Line.append(s: pBuff);
239 return;
240 }
241 else
242 {
243 Line.append(s: aTmpStr);
244 }
245 }
246
247 if(*pBuff)
248 {
249 Line.append(n: 1, c: *pBuff);
250 ++pBuff;
251 }
252 }
253}
254