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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/*
* Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact:
* DongKyun Yun <dk77.yun@samsung.com>
* SangJin Kim <sangjin3.kim@samsung.com>
* HyunGoo Kang <hyungoo1.kang@samsung.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Contributors:
* - S-Core Co., Ltd
*
*/
#include "es2front.h"
#include <stdarg.h>
void GL_APIENTRY ES2INTER(ShaderObjectInit)(struct ShaderObjectUnit* pUnit) {
pUnit->eShaderType = 0;
pUnit->bDeleteStatus = GL_FALSE;
pUnit->bCompileStatus = GL_FALSE;
pUnit->nAttached = 0;
pUnit->aStrSource = NULL;
pUnit->nSourceAlloc = 0;
pUnit->nSourceUsed = 0;
pUnit->nSourceLength = 0;
pUnit->strInfoLog = NULL;
pUnit->nInfoLogAlloc = 0;
pUnit->strPreped = NULL;
pUnit->nPrepedAlloc = 0;
pUnit->symbolLevel = -1;
}
void GL_APIENTRY ES2INTER(ShaderObjectRelease)(struct ShaderObjectUnit* pUnit) {
register int i;
assert(pUnit->bDeleteStatus == GL_TRUE && pUnit->nAttached == 0);
for (i = 0; pUnit->aStrSource != NULL && i < pUnit->nSourceAlloc; i++) {
if (pUnit->aStrSource[i] != NULL) {
free(pUnit->aStrSource[i]);
}
}
if (pUnit->aStrSource != NULL) {
free(pUnit->aStrSource);
}
if (pUnit->strInfoLog != NULL) {
free(pUnit->strInfoLog);
}
if (pUnit->strPreped != NULL) {
free(pUnit->strPreped);
}
pUnit->eShaderType = 0;
pUnit->bDeleteStatus = GL_FALSE;
pUnit->bCompileStatus = GL_FALSE;
pUnit->nAttached = 0;
pUnit->aStrSource = NULL;
pUnit->nSourceAlloc = 0;
pUnit->nSourceUsed = 0;
pUnit->nSourceLength = 0;
pUnit->strInfoLog = NULL;
pUnit->nInfoLogAlloc = 0;
pUnit->strPreped = NULL;
pUnit->nPrepedAlloc = 0;
ES2INTER(SymbolRelease)(pUnit);
}
void GL_APIENTRY ES2INTER(ShaderObjectInfoLogReset)(struct ShaderObjectUnit* pUnit) {
if (pUnit->strInfoLog == NULL) {
assert(pUnit->nInfoLogAlloc == 0);
pUnit->nInfoLogAlloc = 1024;
pUnit->strInfoLog = realloc(pUnit->strInfoLog, pUnit->nInfoLogAlloc * sizeof(GLchar));
assert(pUnit->strInfoLog != NULL);
}
pUnit->strInfoLog[0] = '\0';
}
void GL_APIENTRY ES2INTER(ShaderObjectInfoLogAppend)(struct ShaderObjectUnit* pUnit,
const char* format, ...) {
static char cBuf[1024];
int nLenBuf;
int nLenLog;
va_list ap;
va_start(ap, format);
vsnprintf(cBuf, 1024, format, ap);
if (pUnit->bDeleteStatus != GL_TRUE) {
if (pUnit->strInfoLog == NULL) {
assert(pUnit->nInfoLogAlloc == 0);
pUnit->nInfoLogAlloc = 1024;
pUnit->strInfoLog = realloc(pUnit->strInfoLog, pUnit->nInfoLogAlloc * sizeof(GLchar));
assert(pUnit->strInfoLog != NULL);
pUnit->strInfoLog[0] = '\0';
}
nLenBuf = strlen(cBuf) + 1;
nLenLog = strlen(pUnit->strInfoLog);
if (nLenBuf + nLenLog >= pUnit->nInfoLogAlloc) {
while (nLenBuf + nLenLog >= pUnit->nInfoLogAlloc) {
pUnit->nInfoLogAlloc *= 2;
}
pUnit->strInfoLog = realloc(pUnit->strInfoLog, pUnit->nInfoLogAlloc * sizeof(GLchar));
assert(pUnit->strInfoLog != NULL);
}
strcat(pUnit->strInfoLog, cBuf);
} else {
ES2INTER(SetError)(GL_NO_ERROR);
}
va_end(ap);
}
void GL_APIENTRY ES2INTER(ShaderObjectPrepedReset)(struct ShaderObjectUnit* pUnit) {
if (pUnit->strPreped == NULL) {
assert(pUnit->nPrepedAlloc == 0);
pUnit->nPrepedAlloc = 1024;
pUnit->strPreped = realloc(pUnit->strPreped, pUnit->nPrepedAlloc * sizeof(GLchar));
assert(pUnit->strPreped != NULL);
}
pUnit->strPreped[0] = '\0';
}
void GL_APIENTRY ES2INTER(ShaderObjectPrepedAppend)(struct ShaderObjectUnit* pUnit,
const char* strBuf, int nLenBuf) {
int nLenPreped;
if (pUnit->strPreped == NULL) {
assert(pUnit->nPrepedAlloc == 0);
pUnit->nPrepedAlloc = 1024;
pUnit->strPreped = realloc(pUnit->strPreped, pUnit->nPrepedAlloc * sizeof(GLchar));
assert(pUnit->strPreped != NULL);
pUnit->strPreped[0] = '\0';
}
nLenPreped = strlen(pUnit->strPreped);
if (nLenBuf + nLenPreped >= pUnit->nPrepedAlloc) {
while (nLenBuf + nLenPreped >= pUnit->nPrepedAlloc) {
pUnit->nPrepedAlloc *= 2;
}
pUnit->strPreped = realloc(pUnit->strPreped, pUnit->nPrepedAlloc * sizeof(GLchar));
assert(pUnit->strPreped != NULL);
}
strncat(pUnit->strPreped, strBuf, nLenBuf);
}
GLuint GL_APIENTRY ES2ENTRY(CreateShader)(GLenum type) {
register GLuint uResult;
register GLint nAllocated;
register int i;
switch (type) {
default:
ES2INTER(SetError)(GL_INVALID_ENUM);
return 0;
case GL_VERTEX_SHADER:
case GL_FRAGMENT_SHADER:
uResult = FNPTR(CreateShader)(type);
nAllocated = CCV(nShaderObjectAllocated);
while (nAllocated <= uResult) {
nAllocated *= 2;
}
if (CCV(nShaderObjectAllocated) < nAllocated) {
register struct ShaderObjectUnit* pUnit = realloc(CCV(pShaderObject),
nAllocated * sizeof(struct ShaderObjectUnit));
assert(pUnit != NULL);
for (i = CCV(nShaderObjectAllocated); i < nAllocated; i++) {
ES2INTER(ShaderObjectInit)(&(pUnit[i]));
}
CCV(pShaderObject) = pUnit;
CCV(nShaderObjectAllocated) = nAllocated;
}
CCV(pShaderObject)[uResult].eShaderType = type;
return uResult;
}
}
void GL_APIENTRY ES2ENTRY(DeleteShader)(GLuint shader) {
if (shader == 0) {
return;
} else if (FNPTR(IsShader) != NULL && FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
}
if (FNPTR(DeleteShader) != NULL)
FNPTR(DeleteShader)(shader);
if (!CCV(nShaderObjectAllocated))
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
if (shader < CCV(nShaderObjectAllocated)) {
struct ShaderObjectUnit* pUnit = &CCV(pShaderObject[shader]);
pUnit->bDeleteStatus = GL_TRUE;
if (pUnit->nAttached == 0) {
ES2INTER(ShaderObjectRelease)(pUnit);
}
}
}
GLboolean GL_APIENTRY ES2ENTRY(IsShader)(GLuint shader) {
register GLboolean bResult;
bResult = FNPTR(IsShader)(shader);
return bResult;
}
void GL_APIENTRY ES2ENTRY(ShaderSource)(GLuint shader,
GLsizei count, const char** string, const GLint* length) {
register struct ShaderObjectUnit* pUnit;
register int i;
if (CCV(bShaderCompiler) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
if ((GLint)(shader) <= 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
if (count < 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
}
assert(shader < CCV(nShaderObjectAllocated));
pUnit = &CCV(pShaderObject[shader]);
pUnit->bCompileStatus = GL_FALSE;
pUnit->nSourceUsed = count;
if (pUnit->nSourceAlloc < count) {
pUnit->aStrSource = (GLchar**)realloc(pUnit->aStrSource, count * sizeof(GLchar*));
assert(pUnit->aStrSource != NULL);
for (i = pUnit->nSourceAlloc; i < count; i++) {
pUnit->aStrSource[i] = NULL;
}
pUnit->nSourceAlloc = count;
}
pUnit->nSourceLength = 0;
for (i = 0; i < count; i++) {
register int len = (length == NULL) ? strlen(string[i]) : length[i];
pUnit->aStrSource[i] = (GLchar*)realloc(pUnit->aStrSource[i], (len + 2) * sizeof(GLchar));
if (pUnit->aStrSource[i] == NULL) {
ES2INTER(SetError)(GL_OUT_OF_MEMORY);
}
strncpy(pUnit->aStrSource[i], string[i], len);
pUnit->aStrSource[i][len] = (pUnit->aStrSource[i][len - 1] != '\n') ? '\n' : '\0';
pUnit->aStrSource[i][len + 1] = '\0';
pUnit->nSourceLength += (pUnit->aStrSource[i][len - 1] != '\n') ? (len + 1) : len;
}
if (pUnit->nSourceLength > 0) pUnit->nSourceLength++;
ES2INTER(ShaderObjectInfoLogReset)(pUnit);
ES2INTER(ShaderObjectPrepedReset)(pUnit);
FNPTR(ShaderSource)(shader, pUnit->nSourceUsed, (const GLchar**)pUnit->aStrSource, NULL);
}
void GL_APIENTRY ES2ENTRY(GetShaderSource)(GLuint shader,
GLsizei bufsize, GLsizei* length, char* source) {
register struct ShaderObjectUnit* pUnit;
register char* pDst;
register char* pSrc;
register int i;
register int len;
if ((GLint)(shader) <= 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
if (bufsize < 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (bufsize == 0) {
return;
} else if (bufsize == 1) {
*source = '\0';
if (length != NULL) *length = 0;
return;
}
pUnit = &CCV(pShaderObject[shader]);
pDst = source;
for (i = 0; bufsize > 1 && i < pUnit->nSourceUsed; i++) {
pSrc = pUnit->aStrSource[i];
while (bufsize-- > 1) {
*pDst++ = *pSrc++;
}
}
*pDst = '\0';
len = (int)(pDst - source);
if (length != NULL) {
*length = len;
}
}
void GL_APIENTRY ES2ENTRY(ShaderBinary)(GLsizei n, const GLuint* shaders,
GLenum binaryformat, const void* binary, GLsizei length) {
register int i;
if (n < 0 || length < 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
}
for (i = 0; i < n; i++) {
if (shaders[i] == 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shaders[i]) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
}
ES2INTER(SetError)(GL_INVALID_OPERATION);
}
/* hooking for PrepLex analyzer */
extern void hazelPrepPrepare(struct ShaderObjectUnit* pShaderObject);
extern int hazelPrepGetError(void);
extern int hazelPreplex(void);
extern void hazelPrepTerminate(void);
extern void hazelParsePrepare(struct ShaderObjectUnit* ptr, const char* src, char* dst);
extern int hazelParseparse(void);
extern int hazelParseGetError(void);
extern void hazelParseTerminate(void);
int hazelEnableTexture3D = GL_FALSE;
void GL_APIENTRY ES2ENTRY(CompileShader)(GLuint shader) {
register struct ShaderObjectUnit* pUnit;
char* pExpanded;
static char* pParsed = NULL;
GLint nResult;
GLint nSemError;
if (CCV(bShaderCompiler) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
if ((GLint)(shader) <= 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
assert(shader < CCV(nShaderObjectAllocated));
pUnit = &CCV(pShaderObject[shader]);
ES2INTER(ShaderObjectInfoLogReset)(pUnit);
ES2INTER(ShaderObjectPrepedReset)(pUnit);
if (pUnit->nSourceUsed <= 0) {
ES2INTER(ShaderObjectInfoLogAppend)(pUnit, "ERROR: no source code available\n");
pUnit->bCompileStatus = GL_FALSE;
return;
}
ES2INTER(MacroInit)();
hazelPrepPrepare(pUnit);
hazelPreplex();
nResult = hazelPrepGetError();
hazelPrepTerminate();
if (nResult != 0) {
ES2INTER(ShaderObjectInfoLogAppend)(pUnit, "ERROR: stopped during preprocessing\n");
pUnit->bCompileStatus = GL_FALSE;
ES2INTER(MacroRelease)();
return;
}
ES2INTER(MacroUndef)("__LINE__");
ES2INTER(MacroUndef)("__FILE__");
pExpanded = ES2INTER(MacroExpand)(pUnit->strPreped, 0);
if (pExpanded == NULL) {
ES2INTER(ShaderObjectInfoLogAppend)(pUnit, "ERROR: stopped during source expansion\n");
pUnit->bCompileStatus = GL_FALSE;
ES2INTER(MacroRelease)();
return;
}
ES2INTER(MacroRelease)();
if (strncmp (pExpanded, "#version", 8) != 0) {
/* here pParsed has removed all the useless characters like comments.
* If the first strig is not version, set the version number to be 120.
*/
const char *versionStr = "#version 120\n";
pParsed = realloc(pParsed, 2 * strlen(pExpanded) + strlen(versionStr));
memset(pParsed, 0, 2 * strlen(pExpanded) + strlen(versionStr));
strncpy(pParsed, versionStr, strlen(versionStr));
hazelParsePrepare(pUnit, pExpanded, &pParsed[strlen(versionStr)]);
} else {
pParsed = realloc(pParsed, 2 * strlen(pExpanded));
memset(pParsed, 0, 2 * strlen(pExpanded));
hazelParsePrepare(pUnit, pExpanded, pParsed);
}
nResult = hazelParseparse();
nSemError = hazelParseGetError();
hazelParseTerminate();
if (nResult != 0 || nSemError != 0) {
ES2INTER(ShaderObjectInfoLogAppend)(pUnit, "ERROR: stopped during parsing\n");
pUnit->bCompileStatus = GL_FALSE;
return;
}
FNPTR(ShaderSource)(shader, 1, (const GLchar**)&(pParsed), NULL);
FNPTR(CompileShader)(shader);
FNPTR(GetShaderiv)(shader, GL_COMPILE_STATUS, &nResult);
pUnit->bCompileStatus = (nResult == 0) ? GL_FALSE : GL_TRUE;
FNPTR(GetShaderiv)(shader, GL_INFO_LOG_LENGTH, &nResult);
if (nResult > 1) {
GLchar* str = malloc((nResult + 1) * sizeof(GLchar));
FNPTR(GetShaderInfoLog)(shader, nResult + 1, NULL, str);
ES2INTER(ShaderObjectInfoLogAppend)(pUnit, "%s", str);
free(str);
}
}
void GL_APIENTRY ES2ENTRY(ReleaseShaderCompiler)(void) {
if (CCV(bShaderCompiler) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
}
void GL_APIENTRY ES2ENTRY(GetShaderiv)(GLuint shader, GLenum pname, GLint* params) {
register struct ShaderObjectUnit* pUnit;
int nLenInfoLog;
if (pname == GL_COMPILE_STATUS
|| pname == GL_INFO_LOG_LENGTH
|| pname == GL_SHADER_SOURCE_LENGTH) {
if (CCV(bShaderCompiler) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
}
if ((GLint)(shader) <= 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
pUnit = &CCV(pShaderObject[shader]);
switch (pname) {
default:
ES2INTER(SetError)(GL_INVALID_ENUM);
return;
case GL_SHADER_TYPE:
if (params != NULL) {
*params = pUnit->eShaderType;
}
break;
case GL_DELETE_STATUS:
if (params != NULL) {
*params = pUnit->bDeleteStatus;
}
break;
case GL_COMPILE_STATUS:
if (params != NULL) {
*params = pUnit->bCompileStatus;
}
break;
case GL_INFO_LOG_LENGTH:
if (pUnit->strInfoLog != NULL
&& (nLenInfoLog = strlen(pUnit->strInfoLog)) > 0) {
if (params != NULL) {
*params = nLenInfoLog + 1;
}
} else {
FNPTR(GetShaderiv)(shader, pname, params);
}
break;
case GL_SHADER_SOURCE_LENGTH:
if (params != NULL) {
params[0] = pUnit->nSourceLength;
}
}
}
void GL_APIENTRY ES2ENTRY(GetShaderInfoLog)(GLuint shader,
GLsizei bufsize, GLsizei* length, char* infolog) {
register struct ShaderObjectUnit* pUnit;
int len;
GLboolean done;
if ((GLint)(shader) <= 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
} else if (FNPTR(IsShader)(shader) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
if (bufsize < 0) {
ES2INTER(SetError)(GL_INVALID_VALUE);
return;
}
done = GL_FALSE;
if (shader < CCV(nShaderObjectAllocated)) {
pUnit = &CCV(pShaderObject[shader]);
if (pUnit->strInfoLog != NULL) {
len = strlen(pUnit->strInfoLog);
if (len + 1 <= bufsize) {
strcpy(infolog, pUnit->strInfoLog);
if (length != NULL) *length = len;
done = GL_TRUE;
} else if (bufsize > 0) {
strncpy(infolog, pUnit->strInfoLog, bufsize - 1);
infolog[bufsize - 1] = '\0';
if (length != NULL) *length = bufsize - 1;
done = GL_TRUE;
}
}
}
if (done == GL_FALSE) {
FNPTR(GetShaderInfoLog)(shader, bufsize, length, infolog);
}
}
void GL_APIENTRY ES2ENTRY(GetShaderPrecisionFormat)(GLenum shadertype,
GLenum precisiontype, GLint* range, GLint* precision) {
if (CCV(bShaderCompiler) == GL_FALSE) {
ES2INTER(SetError)(GL_INVALID_OPERATION);
return;
}
switch (shadertype) {
default:
ES2INTER(SetError)(GL_INVALID_ENUM);
return;
case GL_VERTEX_SHADER:
case GL_FRAGMENT_SHADER:
switch (precisiontype) {
default:
ES2INTER(SetError)(GL_INVALID_ENUM);
return;
case GL_LOW_FLOAT:
case GL_MEDIUM_FLOAT:
case GL_HIGH_FLOAT:
range[0] = -10;
range[1] = 10;
precision[0] = 1;
break;
case GL_LOW_INT:
case GL_MEDIUM_INT:
case GL_HIGH_INT:
range[0] = -10;
range[1] = 10;
precision[0] = 0;
break;
}
break;
}
}
|