1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#include "ShaderCompiler.h"
#include <sstream>
#include <algorithm>
using namespace std;
static const char* VSH_BEGIN = "VSH_BEGIN";
static const char* VSH_END = "VSH_END";
static const char* FSH_BEGIN = "FSH_BEGIN";
static const char* FSH_END = "FSH_END";
static const char* CMD_BEGIN = "CMD_BEGIN";
static const char* CMD_END = "CMD_END";
std::string s_CompileError = "";
// GLS分为四部分
// * CMD_BEGIN 和 CMD_END 之间的命令
// * VERTEX_SHADER_BEGIN 和 VERTEX_SHADER_END之间的顶点着色器
// * FRAGMENT_SHADER_BEGIN 和 FRAGMENT_SHADER_END之间的片段着色器
// * 三者之外的公共部分
void GLSCompiler::Compile(std::string& src, std::string& vsh, std::string& fsh, RenderCommandGroup& group)/*throw GLSCompileException*/
{
#define CheckLabel(label) {\
int pos = src.find(label);\
if(pos == string::npos || !IsLabelActive(src, label)) {\
s_CompileError = std::string("Compile Shader Error: No ") + #label + " label";\
throw GLSCompileException(s_CompileError.c_str());\
}}
CheckLabel(VSH_BEGIN);
CheckLabel(VSH_END);
CheckLabel(FSH_BEGIN);
CheckLabel(FSH_END);
vsh = GetContent(src, VSH_BEGIN, VSH_END);
fsh = GetContent(src, FSH_BEGIN, FSH_END);
bool hasCmd = IsLabelActive(src, CMD_BEGIN) && IsLabelActive(src, CMD_END);
if (hasCmd)
{
string cmd = GetContent(src, CMD_BEGIN, CMD_END);
if (cmd.size() > 0)
{
ParseCmd(cmd, group);
}
else
{
hasCmd = false;
}
}
string common;
common = TrimContent(src, VSH_BEGIN, VSH_END);
common = TrimContent(common, FSH_BEGIN, FSH_END);
if (hasCmd)
common = TrimContent(common, CMD_BEGIN, CMD_END);
vsh = common + vsh;
fsh = common + fsh;
}
std::string GLSCompiler::GetContent(std::string& src, const char* from, const char* to)
{
int begin = src.find(from);
int end = src.find(to);
if (begin == string::npos || end == string::npos)
{
return "";
}
std::string content = src.substr(begin + strlen(from), end - begin - strlen(from));
return content;
}
std::string GLSCompiler::TrimContent(std::string& src, const char* from, const char* to)
{
int begin = src.find(from);
int end = src.find(to);
string result = src.erase(begin, end + strlen(to) - begin);
return result;
}
bool GLSCompiler::IsLabelActive(std::string& src, const char* label)
{
int pos = src.find(label);
if (pos == string::npos)
return false;
for (int i = pos - 1; i >= 0; --i)
{
int second = i;
int first = i - 1;
if (first < 0)
break;
if (src[second] == '\n' || src[first] == '\r')
break;
if (src[first] == '/' && src[second] == '/')
return false;
}
return true;
}
bool GLSCompiler::IsCommandActive(std::string& src, const char* label)
{
int pos = src.find(label);
if (pos == string::npos)
return false;
for (int i = pos - 1; i >= 0; --i)
{
int second = i;
int first = i - 1;
if (first < 0)
break;
if (src[second] == '\n' || src[first] == '\r')
break;
if (src[first] == '/' && src[second] == '/')
return false;
}
return true;
}
void GLSCompiler::ParseCmd(std::string& cmds, RenderCommandGroup& group)
{
istringstream ss = istringstream(cmds);
string line;
while (getline(ss, line))
{
if(line.find('\r') != string::npos)
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
if (IsLineCommentd(line))
continue;
int cb, ce; // cmd begin, end
if (!FindCmdPos(line, &cb, &ce))
continue;
string cmdName = line.substr(cb, ce - cb + 1);
string cmdValue = line.substr(ce + 1, line.size() - ce - 1);
if (cmdName == "Cull") CommandCull(cmdValue, group);
else if (cmdName == "Blend") CommandBlend(cmdValue, group);
else if (cmdName == "DepthTest") CommandDepthTest(cmdValue, group);
else if (cmdName == "DepthWrite") CommandDepthWrite(cmdValue, group);
else
{
s_CompileError = string("Unknown command " + cmdName);
throw GLSCompileException(s_CompileError.c_str());
}
}
}
#define IsSeperator(c) (c == ' ' || c == '\r' || c == '\n' || c == /*tab*/9)
// 找到行内的第一个单词,作为命令名
bool GLSCompiler::FindCmdPos(std::string& line, int* start, int* end)
{
for (int i = 0; i < line.size(); ++i)
{
if (IsSeperator(line[i]))
continue;
*start = i;
for (int j = i + 1; j < line.size(); ++j)
{
if (IsSeperator(line[j]))
{
*end = j - 1;
return true;
}
}
}
return false;
}
bool GLSCompiler::IsLineCommentd(std::string& line)
{
for (int i = 0; i < line.size(); ++i)
{
if (IsSeperator(line[i]))
continue;
int first = i;
int second = i + 1;
if (second == line.size())
return false;
if (line[first] == '/' && line[second] == '/')
return true;
return false;
}
return false;
}
#define MAX_PARAM 2
void GLSCompiler::CommandCull(std::string& p, RenderCommandGroup& group)
{
std::string params[1];
GetParams("Cull", p, params, 1);
Cmd_Cull* pCull = new Cmd_Cull();
Cmd_Cull& cull = *pCull;
if (params[0] == "Off") cull.cull = Cmd_Cull::Cull_Disable;
else if (params[0] == "Front") cull.cull = Cmd_Cull::Cull_Front;
else if (params[0] == "Back") cull.cull = Cmd_Cull::Cull_Back;
else if (params[0] == "Both") cull.cull = Cmd_Cull::Cull_Both;
else
{
delete pCull;
s_CompileError = string("Compile Shader Error: Invalid parameter of Cull: " + params[0]);
throw GLSCompileException(s_CompileError.c_str());
}
group.push_back(pCull);
}
void GLSCompiler::CommandBlend(std::string& p, RenderCommandGroup& group)
{
std::string params[2];
GetParams("Blend", p, params, 2);
Cmd_Blend* pblend = new Cmd_Blend();
Cmd_Blend& blend = *pblend;
if (params[0] == "Off")
{
blend.enable = false;
group.push_back(pblend);
return;
}
blend.enable = true;
if (params[0] == "Zero") blend.srcFac = Cmd_Blend::Blend_Zero;
else if (params[0] == "One") blend.srcFac = Cmd_Blend::Blend_One;
else if (params[0] == "SrcColor") blend.srcFac = Cmd_Blend::Blend_Src_Color;
else if (params[0] == "OneMinusSrcColor") blend.srcFac = Cmd_Blend::Blend_One_Minus_Src_Color;
else if (params[0] == "DstColor") blend.srcFac = Cmd_Blend::Blend_Dst_Color;
else if (params[0] == "OneMinusDstColor") blend.srcFac = Cmd_Blend::Blend_One_Minus_Dst_Color;
else if (params[0] == "SrcAlpha") blend.srcFac = Cmd_Blend::Blend_Src_Alpha;
else if (params[0] == "OneMinusSrcAlpha") blend.srcFac = Cmd_Blend::Blend_One_Minus_Src_Alpha;
else if (params[0] == "DstAlpha") blend.srcFac = Cmd_Blend::Blend_Dst_Alpha;
else if (params[0] == "OneMinusDstAlpha") blend.srcFac = Cmd_Blend::Blend_One_Minus_Dst_Alpha;
else if (params[0] == "ConstantColor") blend.srcFac = Cmd_Blend::Blend_Constant_Color;
else if (params[0] == "OneMinusConstantColor") blend.srcFac = Cmd_Blend::Blend_One_Minus_Constant_Color;
else if (params[0] == "ConstantAlpha") blend.srcFac = Cmd_Blend::Blend_Constant_Alpha;
else if (params[0] == "OneMinusConstantAlpha") blend.srcFac = Cmd_Blend::Blend_One_Minus_Constant_Alpha;
else
{
delete pblend;
s_CompileError = string("Compile Shader Error: Invalid parameter of Blend: " + params[0]);
throw GLSCompileException(s_CompileError.c_str());
}
if (params[1] == "Zero") blend.dstFac = Cmd_Blend::Blend_Zero;
else if (params[1] == "One") blend.dstFac = Cmd_Blend::Blend_One;
else if (params[1] == "SrcColor") blend.dstFac = Cmd_Blend::Blend_Src_Color;
else if (params[1] == "OneMinusSrcColor") blend.dstFac = Cmd_Blend::Blend_One_Minus_Src_Color;
else if (params[1] == "DstColor") blend.dstFac = Cmd_Blend::Blend_Dst_Color;
else if (params[1] == "OneMinusDstColor") blend.dstFac = Cmd_Blend::Blend_One_Minus_Dst_Color;
else if (params[1] == "SrcAlpha") blend.dstFac = Cmd_Blend::Blend_Src_Alpha;
else if (params[1] == "OneMinusSrcAlpha") blend.dstFac = Cmd_Blend::Blend_One_Minus_Src_Alpha;
else if (params[1] == "DstAlpha") blend.dstFac = Cmd_Blend::Blend_Dst_Alpha;
else if (params[1] == "OneMinusDstAlpha") blend.dstFac = Cmd_Blend::Blend_One_Minus_Dst_Alpha;
else if (params[1] == "ConstantColor") blend.dstFac = Cmd_Blend::Blend_Constant_Color;
else if (params[1] == "OneMinusConstantColor") blend.dstFac = Cmd_Blend::Blend_One_Minus_Constant_Color;
else if (params[1] == "ConstantAlpha") blend.dstFac = Cmd_Blend::Blend_Constant_Alpha;
else if (params[1] == "OneMinusConstantAlpha") blend.dstFac = Cmd_Blend::Blend_One_Minus_Constant_Alpha;
else
{
delete pblend;
s_CompileError = string("Compile Shader Error: Invalid parameter of Blend: " + params[1]);
throw GLSCompileException(s_CompileError.c_str());
}
group.push_back(pblend);
}
void GLSCompiler::CommandDepthTest(std::string& p, RenderCommandGroup& group)
{
std::string params[1];
GetParams("DepthTest", p, params, 1);
Cmd_DepthTest* pTest = new Cmd_DepthTest();
Cmd_DepthTest& test = *pTest;
if (params[0] == "Off") test.test = Cmd_DepthTest::DepthTest_Off;
else if (params[0] == "Always") test.test = Cmd_DepthTest::DepthTest_Always;
else if (params[0] == "Never") test.test = Cmd_DepthTest::DepthTest_Never;
else if (params[0] == "Less") test.test = Cmd_DepthTest::DepthTest_Less;
else if (params[0] == "Equal") test.test = Cmd_DepthTest::DepthTest_Equal;
else if (params[0] == "LEqual") test.test = Cmd_DepthTest::DepthTest_Lequal;
else if (params[0] == "Greater") test.test = Cmd_DepthTest::DepthTest_Greater;
else if (params[0] == "NotEqual") test.test = Cmd_DepthTest::DepthTest_Notequal;
else if (params[0] == "GEqual") test.test = Cmd_DepthTest::DepthTest_Gequal;
else
{
delete pTest;
s_CompileError = string("Compile Shader Error: Invalid parameter of DepthTest: " + params[0]);
throw GLSCompileException(s_CompileError.c_str());
}
group.push_back(pTest);
}
void GLSCompiler::CommandDepthWrite(std::string& p, RenderCommandGroup& group)
{
std::string params[1];
GetParams("DepthWrite", p, params, 1);
Cmd_DepthWrite* pwrite = new Cmd_DepthWrite();
Cmd_DepthWrite& write = *pwrite;
if (params[0] == "Off") write.write = false;
else if (params[0] == "On") write.write = true;
else
{
delete pwrite;
s_CompileError = string("Compile Shader Error: Invalid parameter of DepthWrite: " + params[0]);
throw GLSCompileException(s_CompileError.c_str());
}
group.push_back(pwrite);
}
void GLSCompiler::GetParams(const char* cmdName, std::string& params, std::string* out, int n)
{
int index = 0;
for (int i = 0; i < params.size(); ++i)
{
if (IsSeperator(params[i]))
continue;
int j = i + 1;
for (; j < params.size(); ++j)
{
if (j == params.size() - 1)
{
if (index >= n)
{
s_CompileError = string("Compile Shader Error: Invalid parameter count of ") + cmdName +" : " + params;
throw GLSCompileException(s_CompileError.c_str());
}
if(!IsSeperator(params[j]))
out[index++] = params.substr(i, j - i + 1);
else
out[index++] = params.substr(i, j - i);
return;
}
if (!IsSeperator(params[j]))
continue;
if (index >= n)
{
s_CompileError = string("Compile Shader Error: Invalid parameter count of ") + cmdName + " : " + params;
throw GLSCompileException(s_CompileError.c_str());
}
out[index++] = params.substr(i, j - i);
break;
}
i = j;
}
}
|