-
Notifications
You must be signed in to change notification settings - Fork 0
/
XShaderParser.cpp
345 lines (290 loc) · 6.58 KB
/
XShaderParser.cpp
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
#include "XShaderParser.h"
XShaderParser::~XShaderParser()
{
}
char* XShaderParser::SkipWhites(char* text, bool & hasNewLines)
{
char c = text[0];
while (c <= ' ' && c != '\0')
{
if (c == '\n')
hasNewLines = true;
text++;
c = *text;
}
return text;
}
char* XShaderParser::ParseToken(char** text, bool allowLineBreaks)
{
int c = 0, len;
bool hasNewLines = false;
char *data;
data = *text;
len = 0;
m_currentToken[0] = 0;
// make sure incoming data is valid
if (!data)
{
*text = NULL;
return m_currentToken;
}
while (1)
{
// skip whitespace
data = SkipWhites(data, hasNewLines);
if (!data)
{
*text = NULL;
return m_currentToken;
}
if (hasNewLines && !allowLineBreaks)
{
*text = data;
return m_currentToken;
}
c = *data;
// skip double slash comments
if (c == '/' && data[1] == '/')
{
data += 2;
while (*data && *data != '\n')
{
data++;
}
}
// skip /* */ comments
else if (c == '/' && data[1] == '*')
{
data += 2;
while (*data && (*data != '*' || data[1] != '/'))
{
data++;
}
if (*data)
{
data += 2;
}
}
else
{
break;
}
}
// handle quoted strings
if (c == '\"')
{
data++;
while (1)
{
c = *data++;
if (c == '\"' || !c)
{
m_currentToken[len] = 0;
*text = (char *)data;
return m_currentToken;
}
if (len < MAX_TOKEN_CHARS)
{
m_currentToken[len] = c;
len++;
}
}
}
// parse a regular word
do
{
if (len < MAX_TOKEN_CHARS)
{
m_currentToken[len] = c;
len++;
}
data++;
c = *data;
} while (c > ' ');
if (len == MAX_TOKEN_CHARS)
{
len = 0;
}
m_currentToken[len] = '\0';
*text = (char *)data;
return m_currentToken;
}
void XShaderParser::ParseShaderFile()
{
int i = m_scriptData->length();
char* text = new char[i];
memcpy(text, m_scriptData->c_str(), i);
char* token = " ";
while (i > 0)
{
token = ParseToken(&text, false);
if (StrCaseCmp(token, ""))
{
memset(m_currentShaderName, 0, MAX_TOKEN_CHARS);
memcpy(m_currentShaderName, token, strlen(token));
ParseShader(&text);
}
i = strlen(text);
}
}
bool XShaderParser::ParseShader(char** text)
{
int stack[64]; // used to parse nested bracket content
int s = 0;
memset(stack, 0, 64 * sizeof(int));
char* token = ParseToken(text, false);
int numStages = 0;
unsigned int surfaceParams = 0, blendDest = 0, blendSrc = 0;
float animFreq = 0;
std::string textureName;
std::string animTextureNames[8];
int numAnimTextures = 0;
XShader sh;
while(!StrCaseCmp(token, ""))
token = ParseToken(text, false);
// make sure the syntax is valid
if (!StrCaseCmp(token, "{"))
stack[s++] = 1;
else
return false;
while(stack[0]) // while the stack is not empty
{
token = ParseToken(text, false);
if (!StrCaseCmp(token, ""))
continue;
if (!StrCaseCmp(token, "{"))
{
stack[s++] = 1;
if (s >= 64)
return false;
numStages++;
token = ParseToken(text, false);
}
else if (!StrCaseCmp(token, "}"))
{
stack[--s] = 0;
if (numStages)
{
if (stack[0])
{
TextureStage ts;
ts.animated = false;
ts.destBlend = blendDest;
ts.srcBlend = blendSrc;
ts.animFrequency = animFreq;
ts.textureName = textureName;
ts.numAnimTextures = numAnimTextures;
if (numAnimTextures)
ts.animated = true;
for (int i = 0; i < numAnimTextures; i++)
ts.animTexturesNames[i] = animTextureNames[i];
sh.SetTextureStage(ts);
animFreq = 0.0f;
blendDest = blendSrc = 0;
textureName = "";
numAnimTextures = 0;
}
}
token = ParseToken(text, false);
}
else if (!StrCaseCmp(token, "surfaceparm"))
{
token = ParseToken(text, false);
surfaceParams |= ParseSurfaceParams(token);
token = ParseToken(text, false);
}
else if (!StrCaseCmp(token, "map"))
{
token = ParseToken(text, false);
textureName = std::string(token);
token = ParseToken(text, false);
}
else if (!StrCaseCmp(token, "animMap"))
{
// parse frequency
token = ParseToken(text, false);
animFreq = std::stof(token);
// parse first texture name
token = ParseToken(text, false);
animTextureNames[numAnimTextures++] = token;
int i = 0;
while (token[0] == 't' && i < 7)
{
token = ParseToken(text, false);
animTextureNames[numAnimTextures++] = token;
i++;
}
}
else if (!StrCaseCmp(token, "blendFunc"))
{
token = ParseToken(text, false);
if (!StrCaseCmp(token, "filter"))
continue;
if (!StrCaseCmp(token, "add"))
continue;
if (!StrCaseCmp(token, "blend"))
continue;
if (!StrCaseCmp(token, "GL_add"))
continue;
blendSrc |= ParseBlend(token);
token = ParseToken(text, false);
blendDest |= ParseBlend(token);
token = ParseToken(text, false);
}
else
{
token = ParseToken(text, false);
}
}
sh.SetName(m_currentShaderName);
sh.SetSurfaceFlags(surfaceParams);
m_shaders[m_currentShaderName] = sh;
m_numShaders++;
return true;
}
bool XShaderParser::FindShader(const std::string& shaderName, XShader* sh)
{
std::unordered_map<std::string, XShader>::const_iterator it = m_shaders.find(shaderName);
if (it == m_shaders.end())
return false;
else
{
*sh = it->second;
return true;
}
}
unsigned int XShaderParser::ParseSurfaceParams(const std::string& text)
{
if (!StrCaseCmp(text.c_str(), "nodraw"))
return SURF_NODRAW;
else if (!StrCaseCmp(text.c_str(), "fog"))
return CONTENTS_FOG;
return 0;
}
unsigned int XShaderParser::ParseBlend(const std::string & text)
{
if (!StrCaseCmp(text.c_str(), "GL_ZERO"))
return D3D11_BLEND_ZERO;
else if (!StrCaseCmp(text.c_str(), "GL_ONE"))
return D3D11_BLEND_ONE;
else if (!StrCaseCmp(text.c_str(), "GL_DST_COLOR"))
return D3D11_BLEND_DEST_COLOR;
else if (!StrCaseCmp(text.c_str(), "GL_SRC_COLOR"))
return D3D11_BLEND_SRC_COLOR;
else if (!StrCaseCmp(text.c_str(), "GL_ONE_MINUS_DST_COLOR"))
return D3D11_BLEND_INV_DEST_COLOR;
else if (!StrCaseCmp(text.c_str(), "GL_SRC_ALPHA"))
return D3D11_BLEND_SRC_ALPHA;
else if (!StrCaseCmp(text.c_str(), "GL_ONE_MINUS_SRC_ALPHA"))
return D3D11_BLEND_INV_SRC_ALPHA;
return 0;
}
void XShaderParser::ParseTCMod(const std::string & text)
{
}
void XShaderParser::ParseVector(const std::string & text, float * v)
{
}
void XShaderParser::BuildShader(XShader * pShader)
{
}