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
554
555
556
557
558
559
560
561
562
|
//
// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include <sstream>
#include <string>
#include <vector>
#include "angle_gl.h"
#include "gtest/gtest.h"
#include "GLSLANG/ShaderLang.h"
#define SHADER(Src) #Src
class ExpressionLimitTest : public testing::Test {
protected:
static const int kMaxExpressionComplexity = 16;
static const int kMaxCallStackDepth = 16;
static const int kMaxFunctionParameters = 16;
static const char* kExpressionTooComplex;
static const char* kCallStackTooDeep;
static const char* kHasRecursion;
static const char *kTooManyParameters;
virtual void SetUp()
{
memset(&resources, 0, sizeof(resources));
GenerateResources(&resources);
}
// Set up the per compile resources
static void GenerateResources(ShBuiltInResources *res)
{
ShInitBuiltInResources(res);
res->MaxVertexAttribs = 8;
res->MaxVertexUniformVectors = 128;
res->MaxVaryingVectors = 8;
res->MaxVertexTextureImageUnits = 0;
res->MaxCombinedTextureImageUnits = 8;
res->MaxTextureImageUnits = 8;
res->MaxFragmentUniformVectors = 16;
res->MaxDrawBuffers = 1;
res->OES_standard_derivatives = 0;
res->OES_EGL_image_external = 0;
res->MaxExpressionComplexity = kMaxExpressionComplexity;
res->MaxCallStackDepth = kMaxCallStackDepth;
res->MaxFunctionParameters = kMaxFunctionParameters;
}
void GenerateLongExpression(int length, std::stringstream* ss)
{
for (int ii = 0; ii < length; ++ii) {
*ss << "+ vec4(" << ii << ")";
}
}
std::string GenerateShaderWithLongExpression(int length)
{
static const char* shaderStart = SHADER(
precision mediump float;
uniform vec4 u_color;
void main()
{
gl_FragColor = u_color
);
std::stringstream ss;
ss << shaderStart;
GenerateLongExpression(length, &ss);
ss << "; }";
return ss.str();
}
std::string GenerateShaderWithUnusedLongExpression(int length)
{
static const char* shaderStart = SHADER(
precision mediump float;
uniform vec4 u_color;
void main()
{
gl_FragColor = u_color;
}
vec4 someFunction() {
return u_color
);
std::stringstream ss;
ss << shaderStart;
GenerateLongExpression(length, &ss);
ss << "; }";
return ss.str();
}
void GenerateDeepFunctionStack(int length, std::stringstream* ss)
{
static const char* shaderStart = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 function0() {
return u_color;
}
);
*ss << shaderStart;
for (int ii = 0; ii < length; ++ii) {
*ss << "vec4 function" << (ii + 1) << "() {\n"
<< " return function" << ii << "();\n"
<< "}\n";
}
}
std::string GenerateShaderWithDeepFunctionStack(int length)
{
std::stringstream ss;
GenerateDeepFunctionStack(length, &ss);
ss << "void main() {\n"
<< " gl_FragColor = function" << length << "();\n"
<< "}";
return ss.str();
}
std::string GenerateShaderWithUnusedDeepFunctionStack(int length)
{
std::stringstream ss;
GenerateDeepFunctionStack(length, &ss);
ss << "void main() {\n"
<< " gl_FragColor = vec4(0,0,0,0);\n"
<< "}";
return ss.str();
}
std::string GenerateShaderWithFunctionParameters(int parameters)
{
std::stringstream ss;
ss << "precision mediump float;\n"
<< "\n"
<< "float foo(";
for (int i = 0; i < parameters; ++i)
{
ss << "float f" << i;
if (i + 1 < parameters)
{
ss << ", ";
}
}
ss << ")\n"
<< "{\n"
<< " return f0;\n"
<< "}\n"
<< "\n"
<< "void main()\n"
<< "{\n"
<< " gl_FragColor = vec4(0,0,0,0);\n"
<< "}";
return ss.str();
}
// Compiles a shader and if there's an error checks for a specific
// substring in the error log. This way we know the error is specific
// to the issue we are testing.
bool CheckShaderCompilation(ShHandle compiler,
const char *source,
int compileOptions,
const char *expected_error)
{
bool success = ShCompile(compiler, &source, 1, compileOptions) != 0;
if (success)
{
success = !expected_error;
}
else
{
std::string log = ShGetInfoLog(compiler);
if (expected_error)
success = log.find(expected_error) != std::string::npos;
EXPECT_TRUE(success) << log << "\n----shader----\n" << source;
}
return success;
}
ShBuiltInResources resources;
};
const char* ExpressionLimitTest::kExpressionTooComplex =
"Expression too complex";
const char* ExpressionLimitTest::kCallStackTooDeep =
"Call stack too deep";
const char* ExpressionLimitTest::kHasRecursion =
"Function recursion detected";
const char* ExpressionLimitTest::kTooManyParameters =
"Function has too many parameters";
TEST_F(ExpressionLimitTest, ExpressionComplexity)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle vertexCompiler = ShConstructCompiler(
GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = SH_LIMIT_EXPRESSION_COMPLEXITY;
// Test expression under the limit passes.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithLongExpression(
kMaxExpressionComplexity - 10).c_str(),
compileOptions, NULL));
// Test expression over the limit fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithLongExpression(
kMaxExpressionComplexity + 10).c_str(),
compileOptions, kExpressionTooComplex));
// Test expression over the limit without a limit does not fail.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithLongExpression(
kMaxExpressionComplexity + 10).c_str(),
compileOptions & ~SH_LIMIT_EXPRESSION_COMPLEXITY, NULL));
ShDestruct(vertexCompiler);
}
TEST_F(ExpressionLimitTest, UnusedExpressionComplexity)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle vertexCompiler = ShConstructCompiler(
GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = SH_LIMIT_EXPRESSION_COMPLEXITY;
// Test expression under the limit passes.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedLongExpression(
kMaxExpressionComplexity - 10).c_str(),
compileOptions, NULL));
// Test expression over the limit fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedLongExpression(
kMaxExpressionComplexity + 10).c_str(),
compileOptions, kExpressionTooComplex));
// Test expression over the limit without a limit does not fail.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedLongExpression(
kMaxExpressionComplexity + 10).c_str(),
compileOptions & ~SH_LIMIT_EXPRESSION_COMPLEXITY, NULL));
ShDestruct(vertexCompiler);
}
TEST_F(ExpressionLimitTest, CallStackDepth)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle vertexCompiler = ShConstructCompiler(
GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = SH_LIMIT_CALL_STACK_DEPTH;
// Test call stack under the limit passes.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithDeepFunctionStack(
kMaxCallStackDepth - 10).c_str(),
compileOptions, NULL));
// Test call stack over the limit fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithDeepFunctionStack(
kMaxCallStackDepth + 10).c_str(),
compileOptions, kCallStackTooDeep));
// Test call stack over the limit without limit does not fail.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithDeepFunctionStack(
kMaxCallStackDepth + 10).c_str(),
compileOptions & ~SH_LIMIT_CALL_STACK_DEPTH, NULL));
ShDestruct(vertexCompiler);
}
TEST_F(ExpressionLimitTest, UnusedCallStackDepth)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle vertexCompiler = ShConstructCompiler(
GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = SH_LIMIT_CALL_STACK_DEPTH;
// Test call stack under the limit passes.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedDeepFunctionStack(
kMaxCallStackDepth - 10).c_str(),
compileOptions, NULL));
// Test call stack over the limit fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedDeepFunctionStack(
kMaxCallStackDepth + 10).c_str(),
compileOptions, kCallStackTooDeep));
// Test call stack over the limit without limit does not fail.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler,
GenerateShaderWithUnusedDeepFunctionStack(
kMaxCallStackDepth + 10).c_str(),
compileOptions & ~SH_LIMIT_CALL_STACK_DEPTH, NULL));
ShDestruct(vertexCompiler);
}
TEST_F(ExpressionLimitTest, Recursion)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle vertexCompiler = ShConstructCompiler(
GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = 0;
static const char* shaderWithRecursion0 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
return someFunc();
}
void main() {
gl_FragColor = u_color * someFunc();
}
);
static const char* shaderWithRecursion1 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc();
vec4 someFunc1() {
return someFunc();
}
vec4 someFunc() {
return someFunc1();
}
void main() {
gl_FragColor = u_color * someFunc();
}
);
static const char* shaderWithRecursion2 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
if (u_color.x > 0.5) {
return someFunc();
} else {
return vec4(1);
}
}
void main() {
gl_FragColor = someFunc();
}
);
static const char* shaderWithRecursion3 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
if (u_color.x > 0.5) {
return vec4(1);
} else {
return someFunc();
}
}
void main() {
gl_FragColor = someFunc();
}
);
static const char* shaderWithRecursion4 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
return (u_color.x > 0.5) ? vec4(1) : someFunc();
}
void main() {
gl_FragColor = someFunc();
}
);
static const char* shaderWithRecursion5 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
return (u_color.x > 0.5) ? someFunc() : vec4(1);
}
void main() {
gl_FragColor = someFunc();
}
);
static const char* shaderWithRecursion6 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 someFunc() {
return someFunc();
}
void main() {
gl_FragColor = u_color;
}
);
static const char* shaderWithNoRecursion = SHADER(
precision mediump float;
uniform vec4 u_color;
vec3 rgb(int r, int g, int b) {
return vec3(float(r) / 255.0, float(g) / 255.0, float(b) / 255.0);
}
void main() {
vec3 hairColor0 = rgb(151, 200, 234);
vec3 faceColor2 = rgb(183, 148, 133);
gl_FragColor = u_color + vec4(hairColor0 + faceColor2, 0);
}
);
static const char* shaderWithRecursion7 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 function2() {
return u_color;
}
vec4 function1() {
vec4 a = function2();
vec4 b = function1();
return a + b;
}
void main() {
gl_FragColor = function1();
}
);
static const char* shaderWithRecursion8 = SHADER(
precision mediump float;
uniform vec4 u_color;
vec4 function1();
vec4 function3() {
return function1();
}
vec4 function2() {
return function3();
}
vec4 function1() {
return function2();
}
void main() {
gl_FragColor = function1();
}
);
// Check simple recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion0,
compileOptions, kHasRecursion));
// Check simple recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion1,
compileOptions, kHasRecursion));
// Check if recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion2,
compileOptions, kHasRecursion));
// Check if recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion3,
compileOptions, kHasRecursion));
// Check ternary recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion4,
compileOptions, kHasRecursion));
// Check ternary recursions fails.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion5,
compileOptions, kHasRecursion));
// Check some more forms of recursion
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion6,
compileOptions, kHasRecursion));
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion7,
compileOptions, kHasRecursion));
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion8,
compileOptions, kHasRecursion));
// Check unused recursions fails if limiting call stack
// since we check all paths.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithRecursion6,
compileOptions | SH_LIMIT_CALL_STACK_DEPTH, kHasRecursion));
// Check unused recursions passes.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithNoRecursion,
compileOptions, NULL));
// Check unused recursions passes if limiting call stack.
EXPECT_TRUE(CheckShaderCompilation(
vertexCompiler, shaderWithNoRecursion,
compileOptions | SH_LIMIT_CALL_STACK_DEPTH, NULL));
ShDestruct(vertexCompiler);
}
TEST_F(ExpressionLimitTest, FunctionParameterCount)
{
ShShaderSpec spec = SH_WEBGL_SPEC;
ShShaderOutput output = SH_ESSL_OUTPUT;
ShHandle compiler = ShConstructCompiler(GL_FRAGMENT_SHADER, spec, output, &resources);
int compileOptions = SH_LIMIT_EXPRESSION_COMPLEXITY;
// Test parameters under the limit succeeds.
EXPECT_TRUE(CheckShaderCompilation(
compiler, GenerateShaderWithFunctionParameters(kMaxFunctionParameters).c_str(),
compileOptions, nullptr));
// Test parameters over the limit fails.
EXPECT_TRUE(CheckShaderCompilation(
compiler, GenerateShaderWithFunctionParameters(kMaxFunctionParameters + 1).c_str(),
compileOptions, kTooManyParameters));
// Test parameters over the limit without limit does not fail.
EXPECT_TRUE(CheckShaderCompilation(
compiler, GenerateShaderWithFunctionParameters(kMaxFunctionParameters + 1).c_str(),
compileOptions & ~SH_LIMIT_EXPRESSION_COMPLEXITY, nullptr));
ShDestruct(compiler);
}
|