• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "SkSLCompiler.h"
9 
10 #include "Test.h"
11 
12 // Note that the optimizer will aggressively kill dead code and substitute constants in place of
13 // variables, so we have to jump through a few hoops to ensure that the code in these tests has the
14 // necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being
15 // smart enough to optimize around certain constructs; as the optimizer gets smarter it will
16 // undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is
17 // equivalent!
18 
test(skiatest::Reporter * r,const char * src,const SkSL::Program::Settings & settings,const char * expected,SkSL::Program::Inputs * inputs,SkSL::Program::Kind kind=SkSL::Program::kFragment_Kind)19 static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings,
20                  const char* expected, SkSL::Program::Inputs* inputs,
21                  SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
22     SkSL::Compiler compiler;
23     SkSL::String output;
24     std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
25                                                                      settings);
26     if (!program) {
27         SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
28     }
29     REPORTER_ASSERT(r, program);
30     *inputs = program->fInputs;
31     REPORTER_ASSERT(r, compiler.toGLSL(*program, &output));
32     if (program) {
33         SkSL::String skExpected(expected);
34         if (output != skExpected) {
35             SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
36                      expected, output.c_str());
37         }
38         REPORTER_ASSERT(r, output == skExpected);
39     }
40 }
41 
test(skiatest::Reporter * r,const char * src,const GrShaderCaps & caps,const char * expected,SkSL::Program::Kind kind=SkSL::Program::kFragment_Kind)42 static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
43                  const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
44     SkSL::Program::Settings settings;
45     settings.fCaps = &caps;
46     SkSL::Program::Inputs inputs;
47     test(r, src, settings, expected, &inputs, kind);
48 }
49 
DEF_TEST(SkSLHelloWorld,r)50 DEF_TEST(SkSLHelloWorld, r) {
51     test(r,
52          "void main() { sk_FragColor = float4(0.75); }",
53          *SkSL::ShaderCapsFactory::Default(),
54          "#version 400\n"
55          "out vec4 sk_FragColor;\n"
56          "void main() {\n"
57          "    sk_FragColor = vec4(0.75);\n"
58          "}\n");
59 }
60 
DEF_TEST(SkSLControl,r)61 DEF_TEST(SkSLControl, r) {
62     test(r,
63          "void main() {"
64          "if (sqrt(2) > 5) { sk_FragColor = float4(0.75); } else { discard; }"
65          "int i = 0;"
66          "while (i < 10) { sk_FragColor *= 0.5; i++; }"
67          "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);"
68          "for (int i = 0; i < 10; i++) {"
69          "if (i % 2 == 1) break; else continue;"
70          "}"
71          "return;"
72          "}",
73          *SkSL::ShaderCapsFactory::Default(),
74          "#version 400\n"
75          "out vec4 sk_FragColor;\n"
76          "void main() {\n"
77          "    if (sqrt(2.0) > 5.0) {\n"
78          "        sk_FragColor = vec4(0.75);\n"
79          "    } else {\n"
80          "        discard;\n"
81          "    }\n"
82          "    int i = 0;\n"
83          "    while (i < 10) {\n"
84          "        sk_FragColor *= 0.5;\n"
85          "        i++;\n"
86          "    }\n"
87          "    do {\n"
88          "        sk_FragColor += 0.01;\n"
89          "    } while (sk_FragColor.x < 0.75);\n"
90          "    for (int i = 0;i < 10; i++) {\n"
91          "        if (i % 2 == 1) break; else continue;\n"
92          "    }\n"
93          "    return;\n"
94          "}\n");
95 }
96 
DEF_TEST(SkSLFunctions,r)97 DEF_TEST(SkSLFunctions, r) {
98     test(r,
99          "float foo(float v[2]) { return v[0] * v[1]; }"
100          "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
101          "void main() { float x = 10; bar(x); sk_FragColor = float4(x); }",
102          *SkSL::ShaderCapsFactory::Default(),
103          "#version 400\n"
104          "out vec4 sk_FragColor;\n"
105          "float foo(float v[2]) {\n"
106          "    return v[0] * v[1];\n"
107          "}\n"
108          "void bar(inout float x) {\n"
109          "    float y[2], z;\n"
110          "    y[0] = x;\n"
111          "    y[1] = x * 2.0;\n"
112          "    z = foo(y);\n"
113          "    x = z;\n"
114          "}\n"
115          "void main() {\n"
116          "    float x = 10.0;\n"
117          "    bar(x);\n"
118          "    sk_FragColor = vec4(x);\n"
119          "}\n");
120 }
121 
DEF_TEST(SkSLOperators,r)122 DEF_TEST(SkSLOperators, r) {
123     test(r,
124          "void main() {"
125          "float x = 1, y = 2;"
126          "int z = 3;"
127          "x = x - x + y * z * x * (y - z);"
128          "y = x / y / z;"
129          "z = (z / 2 % 3 << 4) >> 2 << 1;"
130          "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;"
131          "x += 12;"
132          "x -= 12;"
133          "x *= y /= z = 10;"
134          "b ||= false;"
135          "b &&= true;"
136          "b ^^= false;"
137          "z |= 0;"
138          "z &= -1;"
139          "z ^= 0;"
140          "z >>= 2;"
141          "z <<= 4;"
142          "z %= 5;"
143          "x = (float2(sqrt(1)) , 6);"
144          "z = (float2(sqrt(1)) , 6);"
145          "}",
146          *SkSL::ShaderCapsFactory::Default(),
147          "#version 400\n"
148          "void main() {\n"
149          "    float x = 1.0, y = 2.0;\n"
150          "    int z = 3;\n"
151          "    x = -6.0;\n"
152          "    y = -1.0;\n"
153          "    z = 8;\n"
154          "    bool b = false == true || 2.0 >= sqrt(2.0);\n"
155          "    x += 12.0;\n"
156          "    x -= 12.0;\n"
157          "    x *= (y /= float(z = 10));\n"
158          "    b ||= false;\n"
159          "    b &&= true;\n"
160          "    b ^^= false;\n"
161          "    z |= 0;\n"
162          "    z &= -1;\n"
163          "    z ^= 0;\n"
164          "    z >>= 2;\n"
165          "    z <<= 4;\n"
166          "    z %= 5;\n"
167          "    x = float((vec2(sqrt(1.0)) , 6));\n"
168          "    z = (vec2(sqrt(1.0)) , 6);\n"
169          "}\n");
170 }
171 
DEF_TEST(SkSLMatrices,r)172 DEF_TEST(SkSLMatrices, r) {
173     test(r,
174          "void main() {"
175          "float2x4 x = float2x4(1);"
176          "float3x2 y = float3x2(1, 0, 0, 1, float2(2, 2));"
177          "float3x4 z = x * y;"
178          "float3 v1 = float3x3(1) * float3(2);"
179          "float3 v2 = float3(2) * float3x3(1);"
180          "sk_FragColor = float4(z[0].x, v1 + v2);"
181          "}",
182          *SkSL::ShaderCapsFactory::Default(),
183          "#version 400\n"
184          "out vec4 sk_FragColor;\n"
185          "void main() {\n"
186          "    mat3x4 z = mat2x4(1.0) * mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n"
187          "    vec3 v1 = mat3(1.0) * vec3(2.0);\n"
188          "    vec3 v2 = vec3(2.0) * mat3(1.0);\n"
189          "    sk_FragColor = vec4(z[0].x, v1 + v2);\n"
190          "}\n");
191 }
192 
DEF_TEST(SkSLInterfaceBlock,r)193 DEF_TEST(SkSLInterfaceBlock, r) {
194     test(r,
195          "uniform testBlock {"
196          "half x;"
197          "half y[2];"
198          "layout(binding=12) half3x2 z;"
199          "bool w;"
200          "};"
201          "void main() {"
202          "    sk_FragColor = half4(x, y[0], y[1], 0);"
203          "}",
204          *SkSL::ShaderCapsFactory::Default(),
205          "#version 400\n"
206          "out vec4 sk_FragColor;\n"
207          "uniform testBlock {\n"
208          "    float x;\n"
209          "    float[2] y;\n"
210          "    layout (binding = 12) mat3x2 z;\n"
211          "    bool w;\n"
212          "};\n"
213          "void main() {\n"
214          "    sk_FragColor = vec4(x, y[0], y[1], 0.0);\n"
215          "}\n");
216     test(r,
217          "uniform testBlock {"
218          "float x;"
219          "} test;"
220          "void main() {"
221          "    sk_FragColor = half4(test.x);"
222          "}",
223          *SkSL::ShaderCapsFactory::Default(),
224          "#version 400\n"
225          "out vec4 sk_FragColor;\n"
226          "uniform testBlock {\n"
227          "    float x;\n"
228          "} test;\n"
229          "void main() {\n"
230          "    sk_FragColor = vec4(test.x);\n"
231          "}\n");
232     test(r,
233          "uniform testBlock {"
234          "float x;"
235          "} test[2];"
236          "void main() {"
237          "    sk_FragColor = half4(test[1].x);"
238          "}",
239          *SkSL::ShaderCapsFactory::Default(),
240          "#version 400\n"
241          "out vec4 sk_FragColor;\n"
242          "uniform testBlock {\n"
243          "    float x;\n"
244          "} test[2];\n"
245          "void main() {\n"
246          "    sk_FragColor = vec4(test[1].x);\n"
247          "}\n");
248 }
249 
DEF_TEST(SkSLStructs,r)250 DEF_TEST(SkSLStructs, r) {
251     test(r,
252          "struct A {"
253          "int x;"
254          "int y;"
255          "} a1, a2;"
256          "A a3;"
257          "struct B {"
258          "float x;"
259          "float y[2];"
260          "layout(binding=1) A z;"
261          "};"
262          "B b1, b2, b3;"
263          "void main() {"
264          "}",
265          *SkSL::ShaderCapsFactory::Default(),
266          "#version 400\n"
267          "struct A {\n"
268          "    int x;\n"
269          "    int y;\n"
270          "} a1, a2;\n"
271          "A a3;\n"
272          "struct B {\n"
273          "    float x;\n"
274          "    float[2] y;\n"
275          "    layout (binding = 1) A z;\n"
276          "} b1, b2, b3;\n"
277          "void main() {\n"
278          "}\n");
279 }
280 
DEF_TEST(SkSLVersion,r)281 DEF_TEST(SkSLVersion, r) {
282     test(r,
283          "in float test; void main() { sk_FragColor = float4(0.75); }",
284          *SkSL::ShaderCapsFactory::Version450Core(),
285          "#version 450 core\n"
286          "out vec4 sk_FragColor;\n"
287          "in float test;\n"
288          "void main() {\n"
289          "    sk_FragColor = vec4(0.75);\n"
290          "}\n");
291     test(r,
292          "in float test; void main() { sk_FragColor = float4(0.75); }",
293          *SkSL::ShaderCapsFactory::Version110(),
294          "#version 110\n"
295          "varying float test;\n"
296          "void main() {\n"
297          "    gl_FragColor = vec4(0.75);\n"
298          "}\n");
299 }
300 
DEF_TEST(SkSLUsesPrecisionModifiers,r)301 DEF_TEST(SkSLUsesPrecisionModifiers, r) {
302     test(r,
303          "void main() { half x = 0.75; float y = 1; x++; y++;"
304          "sk_FragColor.rg = half2(x, y); }",
305          *SkSL::ShaderCapsFactory::Default(),
306          "#version 400\n"
307          "out vec4 sk_FragColor;\n"
308          "void main() {\n"
309          "    float x = 0.75;\n"
310          "    float y = 1.0;\n"
311          "    x++;\n"
312          "    y++;\n"
313          "    sk_FragColor.xy = vec2(x, y);\n"
314          "}\n");
315     test(r,
316          "void main() { half x = 0.75; float y = 1; x++; y++;"
317          "sk_FragColor.rg = half2(x, y); }",
318          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
319          "#version 400\n"
320          "precision mediump float;\n"
321          "out mediump vec4 sk_FragColor;\n"
322          "void main() {\n"
323          "    mediump float x = 0.75;\n"
324          "    highp float y = 1.0;\n"
325          "    x++;\n"
326          "    y++;\n"
327          "    sk_FragColor.xy = vec2(x, y);\n"
328          "}\n");
329 }
330 
DEF_TEST(SkSLMinAbs,r)331 DEF_TEST(SkSLMinAbs, r) {
332     test(r,
333          "void main() {"
334          "float x = -5;"
335          "sk_FragColor.r = min(abs(x), 6);"
336          "}",
337          *SkSL::ShaderCapsFactory::Default(),
338          "#version 400\n"
339          "out vec4 sk_FragColor;\n"
340          "void main() {\n"
341          "    sk_FragColor.x = min(abs(-5.0), 6.0);\n"
342          "}\n");
343 
344     test(r,
345          "void main() {"
346          "float x = -5.0;"
347          "sk_FragColor.r = min(abs(x), 6.0);"
348          "}",
349          *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(),
350          "#version 400\n"
351          "out vec4 sk_FragColor;\n"
352          "void main() {\n"
353          "    float minAbsHackVar0;\n"
354          "    float minAbsHackVar1;\n"
355          "    sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? "
356                                                                "minAbsHackVar0 : minAbsHackVar1);\n"
357          "}\n");
358 }
359 
DEF_TEST(SkSLFractNegative,r)360 DEF_TEST(SkSLFractNegative, r) {
361     static constexpr char input[] =
362         "void main() {"
363         "float x = -42.0;"
364         "sk_FragColor.r = fract(x);"
365         "}";
366     static constexpr char output_default[] =
367         "#version 400\n"
368         "out vec4 sk_FragColor;\n"
369         "void main() {\n"
370         "    sk_FragColor.x = fract(-42.0);\n"
371         "}\n";
372     static constexpr char output_workaround[] =
373         "#version 400\n"
374         "out vec4 sk_FragColor;\n"
375         "void main() {\n"
376         "    sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n"
377         "}\n";
378 
379     test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default);
380     test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround);
381 }
382 
DEF_TEST(SkSLNegatedAtan,r)383 DEF_TEST(SkSLNegatedAtan, r) {
384     test(r,
385          "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
386          *SkSL::ShaderCapsFactory::Default(),
387          "#version 400\n"
388          "out vec4 sk_FragColor;\n"
389          "void main() {\n"
390          "    vec2 x = vec2(sqrt(2.0));\n"
391          "    sk_FragColor.x = atan(x.x, -x.y);\n"
392          "}\n");
393     test(r,
394          "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
395          *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(),
396          "#version 400\n"
397          "out vec4 sk_FragColor;\n"
398          "void main() {\n"
399          "    vec2 x = vec2(sqrt(2.0));\n"
400          "    sk_FragColor.x = atan(x.x, -1.0 * x.y);\n"
401          "}\n");
402 }
403 
DEF_TEST(SkSLModifiersDeclaration,r)404 DEF_TEST(SkSLModifiersDeclaration, r) {
405     test(r,
406          "layout(blend_support_all_equations) out;"
407          "layout(blend_support_all_equations) out;"
408          "layout(blend_support_multiply) out;"
409          "layout(blend_support_screen) out;"
410          "layout(blend_support_overlay) out;"
411          "layout(blend_support_darken) out;"
412          "layout(blend_support_lighten) out;"
413          "layout(blend_support_colordodge) out;"
414          "layout(blend_support_colorburn) out;"
415          "layout(blend_support_hardlight) out;"
416          "layout(blend_support_softlight) out;"
417          "layout(blend_support_difference) out;"
418          "layout(blend_support_exclusion) out;"
419          "layout(blend_support_hsl_hue) out;"
420          "layout(blend_support_hsl_saturation) out;"
421          "layout(blend_support_hsl_color) out;"
422          "layout(blend_support_hsl_luminosity) out;"
423          "void main() { }",
424          *SkSL::ShaderCapsFactory::Default(),
425          "#version 400\n"
426          "layout (blend_support_all_equations) out ;\n"
427          "layout (blend_support_all_equations) out ;\n"
428          "layout (blend_support_multiply) out ;\n"
429          "layout (blend_support_screen) out ;\n"
430          "layout (blend_support_overlay) out ;\n"
431          "layout (blend_support_darken) out ;\n"
432          "layout (blend_support_lighten) out ;\n"
433          "layout (blend_support_colordodge) out ;\n"
434          "layout (blend_support_colorburn) out ;\n"
435          "layout (blend_support_hardlight) out ;\n"
436          "layout (blend_support_softlight) out ;\n"
437          "layout (blend_support_difference) out ;\n"
438          "layout (blend_support_exclusion) out ;\n"
439          "layout (blend_support_hsl_hue) out ;\n"
440          "layout (blend_support_hsl_saturation) out ;\n"
441          "layout (blend_support_hsl_color) out ;\n"
442          "layout (blend_support_hsl_luminosity) out ;\n"
443          "void main() {\n"
444          "}\n");
445 }
446 
DEF_TEST(SkSLHex,r)447 DEF_TEST(SkSLHex, r) {
448     test(r,
449          "void main() {"
450          "int i1 = 0x0;"
451          "i1++;"
452          "int i2 = 0x1234abcd;"
453          "i2++;"
454          "int i3 = 0x7fffffff;"
455          "i3++;"
456          "int i4 = 0xffffffff;"
457          "i4++;"
458          "int i5 = -0xbeef;"
459          "i5++;"
460          "uint u1 = 0x0;"
461          "u1++;"
462          "uint u2 = 0x1234abcd;"
463          "u2++;"
464          "uint u3 = 0x7fffffff;"
465          "u3++;"
466          "uint u4 = 0xffffffff;"
467          "u4++;"
468          "ushort u5 = 0xffff;"
469          "u5++;"
470          "}",
471          *SkSL::ShaderCapsFactory::Default(),
472          "#version 400\n"
473          "void main() {\n"
474          "    int i1 = 0;\n"
475          "    i1++;\n"
476          "    int i2 = 305441741;\n"
477          "    i2++;\n"
478          "    int i3 = 2147483647;\n"
479          "    i3++;\n"
480          "    int i4 = -1;\n"
481          "    i4++;\n"
482          "    int i5 = -48879;\n"
483          "    i5++;\n"
484          "    uint u1 = 0u;\n"
485          "    u1++;\n"
486          "    uint u2 = 305441741u;\n"
487          "    u2++;\n"
488          "    uint u3 = 2147483647u;\n"
489          "    u3++;\n"
490          "    uint u4 = 4294967295u;\n"
491          "    u4++;\n"
492          "    uint u5 = 65535u;\n"
493          "    u5++;\n"
494          "}\n");
495 }
496 
DEF_TEST(SkSLVectorConstructors,r)497 DEF_TEST(SkSLVectorConstructors, r) {
498     test(r,
499          "float2 v1 = float2(1);"
500          "float2 v2 = float2(1, 2);"
501          "float2 v3 = float2(float2(1));"
502          "float3 v4 = float3(float2(1), 1.0);"
503          "int2 v5 = int2(1);"
504          "int2 v6 = int2(float2(1, 2));"
505          "float2 v7 = float2(int2(1, 2));",
506          *SkSL::ShaderCapsFactory::Default(),
507          "#version 400\n"
508          "vec2 v1 = vec2(1.0);\n"
509          "vec2 v2 = vec2(1.0, 2.0);\n"
510          "vec2 v3 = vec2(1.0);\n"
511          "vec3 v4 = vec3(vec2(1.0), 1.0);\n"
512          "ivec2 v5 = ivec2(1);\n"
513          "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n"
514          "vec2 v7 = vec2(ivec2(1, 2));\n");
515 }
516 
DEF_TEST(SkSLArrayConstructors,r)517 DEF_TEST(SkSLArrayConstructors, r) {
518     test(r,
519          "float test1[] = float[](1, 2, 3, 4);"
520          "float2 test2[] = float2[](float2(1, 2), float2(3, 4));"
521          "float4x4 test3[] = float4x4[]();",
522          *SkSL::ShaderCapsFactory::Default(),
523          "#version 400\n"
524          "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
525          "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
526          "mat4 test3[] = mat4[]();\n");
527 }
528 
DEF_TEST(SkSLDerivatives,r)529 DEF_TEST(SkSLDerivatives, r) {
530     test(r,
531          "void main() { sk_FragColor.r = dFdx(1); }",
532          *SkSL::ShaderCapsFactory::Default(),
533          "#version 400\n"
534          "out vec4 sk_FragColor;\n"
535          "void main() {\n"
536          "    sk_FragColor.x = dFdx(1.0);\n"
537          "}\n");
538     test(r,
539          "void main() { sk_FragColor.r = 1; }",
540          *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
541          "#version 400\n"
542          "precision mediump float;\n"
543          "out mediump vec4 sk_FragColor;\n"
544          "void main() {\n"
545          "    sk_FragColor.x = 1.0;\n"
546          "}\n");
547     test(r,
548          "void main() { sk_FragColor.r = dFdx(1); }",
549          *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
550          "#version 400\n"
551          "#extension GL_OES_standard_derivatives : require\n"
552          "precision mediump float;\n"
553          "out mediump vec4 sk_FragColor;\n"
554          "void main() {\n"
555          "    sk_FragColor.x = dFdx(1.0);\n"
556          "}\n");
557 }
558 
559 
DEF_TEST(SkSLIntFolding,r)560 DEF_TEST(SkSLIntFolding, r) {
561     test(r,
562          "void main() {"
563          "sk_FragColor.r = 32 + 2;"
564          "sk_FragColor.r = 32 - 2;"
565          "sk_FragColor.r = 32 * 2;"
566          "sk_FragColor.r = 32 / 2;"
567          "sk_FragColor.r = 12 | 6;"
568          "sk_FragColor.r = 254 & 7;"
569          "sk_FragColor.r = 2 ^ 7;"
570          "sk_FragColor.r = 1 << 4;"
571          "sk_FragColor.r = 128 >> 2;"
572          "sk_FragColor.r = -1 == -1 ? 1 : -1;"
573          "sk_FragColor.r = -1 == -2 ? 2 : -2;"
574          "sk_FragColor.r = 0 != 1 ? 3 : -3;"
575          "sk_FragColor.r = 0 != 0 ? 4 : -4;"
576          "sk_FragColor.r = 6 > 5 ? 5 : -5;"
577          "sk_FragColor.r = 6 > 6 ? 6 : -6;"
578          "sk_FragColor.r = -1 < 0 ? 7 : -7;"
579          "sk_FragColor.r = 1 < 0 ? 8 : -8;"
580          "sk_FragColor.r = 6 >= 6 ? 9 : -9;"
581          "sk_FragColor.r = 6 >= 7 ? 10 : -10;"
582          "sk_FragColor.r = 6 <= 6 ? 11 : -11;"
583          "sk_FragColor.r = 6 <= 5 ? 12 : -12;"
584          "sk_FragColor.r = int(sqrt(1)) + 0;"
585          "sk_FragColor.r = 0 + int(sqrt(2));"
586          "sk_FragColor.r = int(sqrt(3)) - 0;"
587          "sk_FragColor.r = int(sqrt(4)) * 0;"
588          "sk_FragColor.r = int(sqrt(5)) * 1;"
589          "sk_FragColor.r = 1 * int(sqrt(6));"
590          "sk_FragColor.r = 0 * int(sqrt(7));"
591          "sk_FragColor.r = int(sqrt(8)) / 1;"
592          "sk_FragColor.r = 0 / int(sqrt(9));"
593          "int x = int(sqrt(2));"
594          "x += 1;"
595          "x += 0;"
596          "x -= 1;"
597          "x -= 0;"
598          "x *= 1;"
599          "x *= 2;"
600          "x /= 1;"
601          "x /= 2;"
602          "sk_FragColor.r = x;"
603          "}",
604          *SkSL::ShaderCapsFactory::Default(),
605          "#version 400\n"
606          "out vec4 sk_FragColor;\n"
607          "void main() {\n"
608          "    sk_FragColor.x = 34.0;\n"
609          "    sk_FragColor.x = 30.0;\n"
610          "    sk_FragColor.x = 64.0;\n"
611          "    sk_FragColor.x = 16.0;\n"
612          "    sk_FragColor.x = 14.0;\n"
613          "    sk_FragColor.x = 6.0;\n"
614          "    sk_FragColor.x = 5.0;\n"
615          "    sk_FragColor.x = 16.0;\n"
616          "    sk_FragColor.x = 32.0;\n"
617          "    sk_FragColor.x = 1.0;\n"
618          "    sk_FragColor.x = -2.0;\n"
619          "    sk_FragColor.x = 3.0;\n"
620          "    sk_FragColor.x = -4.0;\n"
621          "    sk_FragColor.x = 5.0;\n"
622          "    sk_FragColor.x = -6.0;\n"
623          "    sk_FragColor.x = 7.0;\n"
624          "    sk_FragColor.x = -8.0;\n"
625          "    sk_FragColor.x = 9.0;\n"
626          "    sk_FragColor.x = -10.0;\n"
627          "    sk_FragColor.x = 11.0;\n"
628          "    sk_FragColor.x = -12.0;\n"
629          "    sk_FragColor.x = float(int(sqrt(1.0)));\n"
630          "    sk_FragColor.x = float(int(sqrt(2.0)));\n"
631          "    sk_FragColor.x = float(int(sqrt(3.0)));\n"
632          "    sk_FragColor.x = 0.0;\n"
633          "    sk_FragColor.x = float(int(sqrt(5.0)));\n"
634          "    sk_FragColor.x = float(int(sqrt(6.0)));\n"
635          "    sk_FragColor.x = 0.0;\n"
636          "    sk_FragColor.x = float(int(sqrt(8.0)));\n"
637          "    sk_FragColor.x = 0.0;\n"
638          "    int x = int(sqrt(2.0));\n"
639          "    x += 1;\n"
640          "    x -= 1;\n"
641          "    x *= 2;\n"
642          "    x /= 2;\n"
643          "    sk_FragColor.x = float(x);\n"
644          "}\n");
645 }
646 
DEF_TEST(SkSLFloatFolding,r)647 DEF_TEST(SkSLFloatFolding, r) {
648     test(r,
649          "void main() {"
650          "sk_FragColor.r = 32.0 + 2.0;"
651          "sk_FragColor.r = 32.0 - 2.0;"
652          "sk_FragColor.r = 32.0 * 2.0;"
653          "sk_FragColor.r = 32.0 / 2.0;"
654          "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
655          "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;"
656          "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;"
657          "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;"
658          "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;"
659          "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;"
660          "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;"
661          "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;"
662          "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;"
663          "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;"
664          "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;"
665          "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;"
666          "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;"
667          "sk_FragColor.r = sqrt(1) + 0;"
668          "sk_FragColor.r = 0 + sqrt(2);"
669          "sk_FragColor.r = sqrt(3) - 0;"
670          "sk_FragColor.r = sqrt(4) * 0;"
671          "sk_FragColor.r = sqrt(5) * 1;"
672          "sk_FragColor.r = 1 * sqrt(6);"
673          "sk_FragColor.r = 0 * sqrt(7);"
674          "sk_FragColor.r = sqrt(8) / 1;"
675          "sk_FragColor.r = 0 / sqrt(9);"
676          "sk_FragColor.r += 1;"
677          "sk_FragColor.r += 0;"
678          "sk_FragColor.r -= 1;"
679          "sk_FragColor.r -= 0;"
680          "sk_FragColor.r *= 1;"
681          "sk_FragColor.r *= 2;"
682          "sk_FragColor.r /= 1;"
683          "sk_FragColor.r /= 2;"
684          "}",
685          *SkSL::ShaderCapsFactory::Default(),
686          "#version 400\n"
687          "out vec4 sk_FragColor;\n"
688          "void main() {\n"
689          "    sk_FragColor.x = 34.0;\n"
690          "    sk_FragColor.x = 30.0;\n"
691          "    sk_FragColor.x = 64.0;\n"
692          "    sk_FragColor.x = 16.0;\n"
693          "    sk_FragColor.x = 19.0;\n"
694          "    sk_FragColor.x = 1.0;\n"
695          "    sk_FragColor.x = -2.0;\n"
696          "    sk_FragColor.x = 3.0;\n"
697          "    sk_FragColor.x = -4.0;\n"
698          "    sk_FragColor.x = 5.0;\n"
699          "    sk_FragColor.x = -6.0;\n"
700          "    sk_FragColor.x = 7.0;\n"
701          "    sk_FragColor.x = -8.0;\n"
702          "    sk_FragColor.x = 9.0;\n"
703          "    sk_FragColor.x = -10.0;\n"
704          "    sk_FragColor.x = 11.0;\n"
705          "    sk_FragColor.x = -12.0;\n"
706          "    sk_FragColor.x = sqrt(1.0);\n"
707          "    sk_FragColor.x = sqrt(2.0);\n"
708          "    sk_FragColor.x = sqrt(3.0);\n"
709          "    sk_FragColor.x = 0.0;\n"
710          "    sk_FragColor.x = sqrt(5.0);\n"
711          "    sk_FragColor.x = sqrt(6.0);\n"
712          "    sk_FragColor.x = 0.0;\n"
713          "    sk_FragColor.x = sqrt(8.0);\n"
714          "    sk_FragColor.x = 0.0;\n"
715          "    sk_FragColor.x += 1.0;\n"
716          "    sk_FragColor.x -= 1.0;\n"
717          "    sk_FragColor.x *= 2.0;\n"
718          "    sk_FragColor.x /= 2.0;\n"
719          "}\n");
720 }
721 
DEF_TEST(SkSLBoolFolding,r)722 DEF_TEST(SkSLBoolFolding, r) {
723     test(r,
724          "void main() {"
725          "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;"
726          "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;"
727          "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;"
728          "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;"
729          "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;"
730          "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;"
731          "}",
732          *SkSL::ShaderCapsFactory::Default(),
733          "#version 400\n"
734          "out vec4 sk_FragColor;\n"
735          "void main() {\n"
736          "    sk_FragColor.x = 1.0;\n"
737          "    sk_FragColor.x = -2.0;\n"
738          "    sk_FragColor.x = 3.0;\n"
739          "    sk_FragColor.x = -4.0;\n"
740          "    sk_FragColor.x = 5.0;\n"
741          "    sk_FragColor.x = -6.0;\n"
742          "}\n");
743 }
744 
DEF_TEST(SkSLShortCircuitBoolFolding,r)745 DEF_TEST(SkSLShortCircuitBoolFolding, r) {
746     test(r,
747          "void main() {"
748          "bool expr1 = sk_FragCoord.x > 0;"
749          "bool expr2 = sk_FragCoord.y > 0;"
750          "    if (true && expr1) {"          // -> if (expr1)
751          "        sk_FragColor.r = 1;"
752          "    } else if (false && expr1) {"  // -> if (false) -> block removed
753          "        sk_FragColor.r = -2;"
754          "    } else if (false || expr2) {"  // -> if (expr2)
755          "        sk_FragColor.r = 3;"
756          "    } else if (true || expr2) {"   // -> if (true) -> replaces unreachable else
757          "        sk_FragColor.r = 4;"
758          "    } else {"                      // removed
759          "        sk_FragColor.r = -5;"
760          "    }"
761          // Test short-circuiting of right hand side boolean literals
762          "    if (expr1 && true) {"          // -> if (expr1)
763          "        sk_FragColor.r = 1;"
764          "    } else if (expr1 && false) {"  // -> if (false) -> block removed
765          "        sk_FragColor.r = -2;"
766          "    } else if (expr2 || false) {"  // -> if (expr2)
767          "        sk_FragColor.r = 3;"
768          "    } else if (expr2 || true) {"   // -> if (true) -> replaces unreachable else
769          "        sk_FragColor.r = 4;"
770          "    } else {"                      // removed
771          "        sk_FragColor.r = -5;"
772          "    }"
773          "}",
774          *SkSL::ShaderCapsFactory::Default(),
775          "#version 400\n"
776          "out vec4 sk_FragColor;\n"
777          "void main() {\n"
778          "    bool expr1 = gl_FragCoord.x > 0.0;\n"
779          "    bool expr2 = gl_FragCoord.y > 0.0;\n"
780          "    if (expr1) {\n"
781          "        sk_FragColor.x = 1.0;\n"
782          "    } else if (expr2) {\n"
783          "        sk_FragColor.x = 3.0;\n"
784          "    } else {\n"
785          "        sk_FragColor.x = 4.0;\n"
786          "    }\n"
787          "    if (expr1) {\n"
788          "        sk_FragColor.x = 1.0;\n"
789          "    } else if (expr2) {\n"
790          "        sk_FragColor.x = 3.0;\n"
791          "    } else {\n"
792          "        sk_FragColor.x = 4.0;\n"
793          "    }\n"
794          "}\n");
795 }
796 
DEF_TEST(SkSLVecFolding,r)797 DEF_TEST(SkSLVecFolding, r) {
798     test(r,
799          "void main() {"
800          "sk_FragColor.r = float4(0.5, 1, 1, 1).x;"
801          "sk_FragColor = float4(float2(1), float2(2, 3)) + float4(5, 6, 7, 8);"
802          "sk_FragColor = float4(8, float3(10)) - float4(1);"
803          "sk_FragColor = float4(2) * float4(1, 2, 3, 4);"
804          "sk_FragColor = float4(12) / float4(1, 2, 3, 4);"
805          "sk_FragColor.r = (float4(12) / float4(1, 2, 3, 4)).y;"
806          "sk_FragColor.x = float4(1) == float4(1) ? 1.0 : -1.0;"
807          "sk_FragColor.x = float4(1) == float4(2) ? 2.0 : -2.0;"
808          "sk_FragColor.x = float2(1) == float2(1, 1) ? 3.0 : -3.0;"
809          "sk_FragColor.x = float2(1, 1) == float2(1, 1) ? 4.0 : -4.0;"
810          "sk_FragColor.x = float2(1) == float2(1, 0) ? 5.0 : -5.0;"
811          "sk_FragColor.x = float4(1) == float4(float2(1), float2(1)) ? 6.0 : -6.0;"
812          "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), float2(1)) ? 7.0 : -7.0;"
813          "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), 1, 0) ? 8.0 : -8.0;"
814          "sk_FragColor.x = float2(1) != float2(1, 0) ? 9.0 : -9.0;"
815          "sk_FragColor.x = float4(1) != float4(float2(1), float2(1)) ? 10.0 : -10.0;"
816          "sk_FragColor = float4(sqrt(1)) * float4(1);"
817          "sk_FragColor = float4(1) * float4(sqrt(2));"
818          "sk_FragColor = float4(0) * float4(sqrt(3));"
819          "sk_FragColor = float4(sqrt(4)) * float4(0);"
820          "sk_FragColor = float4(0) / float4(sqrt(5));"
821          "sk_FragColor = float4(0) + float4(sqrt(6));"
822          "sk_FragColor = float4(sqrt(7)) + float4(0);"
823          "sk_FragColor = float4(sqrt(8)) - float4(0);"
824          "sk_FragColor = float4(0) + sqrt(9);"
825          "sk_FragColor = float4(0) * sqrt(10);"
826          "sk_FragColor = float4(0) / sqrt(11);"
827          "sk_FragColor = float4(1) * sqrt(12);"
828          "sk_FragColor = 0 + float4(sqrt(13));"
829          "sk_FragColor = 0 * float4(sqrt(14));"
830          "sk_FragColor = 0 / float4(sqrt(15));"
831          "sk_FragColor = 1 * float4(sqrt(16));"
832          "sk_FragColor = float4(sqrt(17)) + 0;"
833          "sk_FragColor = float4(sqrt(18)) * 0;"
834          "sk_FragColor = float4(sqrt(19)) * 1;"
835          "sk_FragColor = float4(sqrt(19.5)) - 0;"
836          "sk_FragColor = sqrt(20) * float4(1);"
837          "sk_FragColor = sqrt(21) + float4(0);"
838          "sk_FragColor = sqrt(22) - float4(0);"
839          "sk_FragColor = sqrt(23) / float4(1);"
840          "sk_FragColor = float4(sqrt(24)) / 1;"
841          "sk_FragColor += float4(1);"
842          "sk_FragColor += float4(0);"
843          "sk_FragColor -= float4(1);"
844          "sk_FragColor -= float4(0);"
845          "sk_FragColor *= float4(1);"
846          "sk_FragColor *= float4(2);"
847          "sk_FragColor /= float4(1);"
848          "sk_FragColor /= float4(2);"
849          "}",
850          *SkSL::ShaderCapsFactory::Default(),
851          "#version 400\n"
852          "out vec4 sk_FragColor;\n"
853          "void main() {\n"
854          "    sk_FragColor.x = 0.5;\n"
855          "    sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
856          "    sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
857          "    sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
858          "    sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
859          "    sk_FragColor.x = 6.0;\n"
860          "    sk_FragColor.x = 1.0;\n"
861          "    sk_FragColor.x = -2.0;\n"
862          "    sk_FragColor.x = 3.0;\n"
863          "    sk_FragColor.x = 4.0;\n"
864          "    sk_FragColor.x = -5.0;\n"
865          "    sk_FragColor.x = 6.0;\n"
866          "    sk_FragColor.x = 7.0;\n"
867          "    sk_FragColor.x = -8.0;\n"
868          "    sk_FragColor.x = 9.0;\n"
869          "    sk_FragColor.x = -10.0;\n"
870          "    sk_FragColor = vec4(sqrt(1.0));\n"
871          "    sk_FragColor = vec4(sqrt(2.0));\n"
872          "    sk_FragColor = vec4(0.0);\n"
873          "    sk_FragColor = vec4(0.0);\n"
874          "    sk_FragColor = vec4(0.0);\n"
875          "    sk_FragColor = vec4(sqrt(6.0));\n"
876          "    sk_FragColor = vec4(sqrt(7.0));\n"
877          "    sk_FragColor = vec4(sqrt(8.0));\n"
878          "    sk_FragColor = vec4(sqrt(9.0));\n"
879          "    sk_FragColor = vec4(0.0);\n"
880          "    sk_FragColor = vec4(0.0);\n"
881          "    sk_FragColor = vec4(sqrt(12.0));\n"
882          "    sk_FragColor = vec4(sqrt(13.0));\n"
883          "    sk_FragColor = vec4(0.0);\n"
884          "    sk_FragColor = vec4(0.0);\n"
885          "    sk_FragColor = vec4(sqrt(16.0));\n"
886          "    sk_FragColor = vec4(sqrt(17.0));\n"
887          "    sk_FragColor = vec4(0.0);\n"
888          "    sk_FragColor = vec4(sqrt(19.0));\n"
889          "    sk_FragColor = vec4(sqrt(19.5));\n"
890          "    sk_FragColor = vec4(sqrt(20.0));\n"
891          "    sk_FragColor = vec4(sqrt(21.0));\n"
892          "    sk_FragColor = vec4(sqrt(22.0));\n"
893          "    sk_FragColor = vec4(sqrt(23.0));\n"
894          "    sk_FragColor = vec4(sqrt(24.0));\n"
895          "    sk_FragColor += vec4(1.0);\n"
896          "    sk_FragColor -= vec4(1.0);\n"
897          "    sk_FragColor *= vec4(2.0);\n"
898          "    sk_FragColor /= vec4(2.0);\n"
899          "}\n");
900 }
901 
DEF_TEST(SkSLMatFolding,r)902 DEF_TEST(SkSLMatFolding, r) {
903     test(r,
904          "void main() {"
905          "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == "
906                           "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;"
907          "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == "
908                           "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;"
909          "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;"
910          "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;"
911          "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;"
912          "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;"
913          "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))"
914                            "? 7 : -7;"
915          "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;"
916          "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;"
917          "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), "
918                           "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;"
919          "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;"
920          "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;"
921          "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;"
922          "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;"
923          "}",
924          *SkSL::ShaderCapsFactory::Default(),
925          "#version 400\n"
926          "out vec4 sk_FragColor;\n"
927          "void main() {\n"
928          "    sk_FragColor.x = 1.0;\n"
929          "    sk_FragColor.x = -2.0;\n"
930          "    sk_FragColor.x = 3.0;\n"
931          "    sk_FragColor.x = -4.0;\n"
932          "    sk_FragColor.x = 5.0;\n"
933          "    sk_FragColor.x = -6.0;\n"
934          "    sk_FragColor.x = 7.0;\n"
935          "    sk_FragColor.x = -8.0;\n"
936          "    sk_FragColor.x = 9.0;\n"
937          "    sk_FragColor.x = 10.0;\n"
938          "    sk_FragColor.x = 11.0;\n"
939          "    sk_FragColor.x = 12.0;\n"
940          "    sk_FragColor.x = 13.0;\n"
941          "    sk_FragColor.x = 14.0;\n"
942          "}\n");
943 }
944 
DEF_TEST(SkSLConstantIf,r)945 DEF_TEST(SkSLConstantIf, r) {
946     test(r,
947          "void main() {"
948          "int x;"
949          "if (true) x = 1;"
950          "if (2 > 1) x = 2; else x = 3;"
951          "if (1 > 2) x = 4; else x = 5;"
952          "if (false) x = 6;"
953          "sk_FragColor.r = x;"
954          "}",
955          *SkSL::ShaderCapsFactory::Default(),
956          "#version 400\n"
957          "out vec4 sk_FragColor;\n"
958          "void main() {\n"
959          "    sk_FragColor.x = 5.0;\n"
960          "}\n");
961 }
962 
DEF_TEST(SkSLCaps,r)963 DEF_TEST(SkSLCaps, r) {
964     test(r,
965          "void main() {"
966          "int x = 0;"
967          "int y = 0;"
968          "int z = 0;"
969          "int w = 0;"
970          "if (sk_Caps.externalTextureSupport) x = 1;"
971          "if (sk_Caps.fbFetchSupport) y = 1;"
972          "if (sk_Caps.dropsTileOnZeroDivide) z = 1;"
973          "if (sk_Caps.canUseAnyFunctionInShader) w = 1;"
974          "sk_FragColor = half4(x, y, z, w);"
975          "}",
976          *SkSL::ShaderCapsFactory::VariousCaps(),
977          "#version 400\n"
978          "out vec4 sk_FragColor;\n"
979          "void main() {\n"
980          "    sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n"
981          "}\n");
982 }
983 
DEF_TEST(SkSLTexture,r)984 DEF_TEST(SkSLTexture, r) {
985     test(r,
986          "uniform sampler1D one;"
987          "uniform sampler2D two;"
988          "void main() {"
989          "float4 a = texture(one, 0);"
990          "float4 b = texture(two, float2(0));"
991          "float4 c = texture(one, float2(0));"
992          "float4 d = texture(two, float3(0));"
993          "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
994          "}",
995          *SkSL::ShaderCapsFactory::Default(),
996          "#version 400\n"
997          "out vec4 sk_FragColor;\n"
998          "uniform sampler1D one;\n"
999          "uniform sampler2D two;\n"
1000          "void main() {\n"
1001          "    vec4 a = texture(one, 0.0);\n"
1002          "    vec4 b = texture(two, vec2(0.0));\n"
1003          "    vec4 c = textureProj(one, vec2(0.0));\n"
1004          "    vec4 d = textureProj(two, vec3(0.0));\n"
1005          "    sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1006          "}\n");
1007     test(r,
1008          "uniform sampler1D one;"
1009          "uniform sampler2D two;"
1010          "void main() {"
1011          "float4 a = texture(one, 0);"
1012          "float4 b = texture(two, float2(0));"
1013          "float4 c = texture(one, float2(0));"
1014          "float4 d = texture(two, float3(0));"
1015          "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
1016          "}",
1017          *SkSL::ShaderCapsFactory::Version110(),
1018          "#version 110\n"
1019          "uniform sampler1D one;\n"
1020          "uniform sampler2D two;\n"
1021          "void main() {\n"
1022          "    vec4 a = texture1D(one, 0.0);\n"
1023          "    vec4 b = texture2D(two, vec2(0.0));\n"
1024          "    vec4 c = texture1DProj(one, vec2(0.0));\n"
1025          "    vec4 d = texture2DProj(two, vec3(0.0));\n"
1026          "    gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1027          "}\n");
1028 }
1029 
DEF_TEST(SkSLSharpen,r)1030 DEF_TEST(SkSLSharpen, r) {
1031     SkSL::Program::Settings settings;
1032     settings.fSharpenTextures = true;
1033     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1034     settings.fCaps = caps.get();
1035     SkSL::Program::Inputs inputs;
1036     test(r,
1037          "uniform sampler1D one;"
1038          "uniform sampler2D two;"
1039          "void main() {"
1040          "float4 a = texture(one, 0);"
1041          "float4 b = texture(two, float2(0));"
1042          "float4 c = texture(one, float2(0));"
1043          "float4 d = texture(two, float3(0));"
1044          "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
1045          "}",
1046          settings,
1047          "#version 400\n"
1048          "out vec4 sk_FragColor;\n"
1049          "uniform sampler1D one;\n"
1050          "uniform sampler2D two;\n"
1051          "void main() {\n"
1052          "    vec4 a = texture(one, 0.0, -0.5);\n"
1053          "    vec4 b = texture(two, vec2(0.0), -0.5);\n"
1054          "    vec4 c = textureProj(one, vec2(0.0), -0.5);\n"
1055          "    vec4 d = textureProj(two, vec3(0.0), -0.5);\n"
1056          "    sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1057          "}\n",
1058          &inputs);
1059 
1060     caps = SkSL::ShaderCapsFactory::Version110();
1061     settings.fCaps = caps.get();
1062     test(r,
1063          "uniform sampler1D one;"
1064          "uniform sampler2D two;"
1065          "void main() {"
1066          "float4 a = texture(one, 0);"
1067          "float4 b = texture(two, float2(0));"
1068          "float4 c = texture(one, float2(0));"
1069          "float4 d = texture(two, float3(0));"
1070          "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
1071          "}",
1072          settings,
1073          "#version 110\n"
1074          "uniform sampler1D one;\n"
1075          "uniform sampler2D two;\n"
1076          "void main() {\n"
1077          "    vec4 a = texture1D(one, 0.0, -0.5);\n"
1078          "    vec4 b = texture2D(two, vec2(0.0), -0.5);\n"
1079          "    vec4 c = texture1DProj(one, vec2(0.0), -0.5);\n"
1080          "    vec4 d = texture2DProj(two, vec3(0.0), -0.5);\n"
1081          "    gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1082          "}\n",
1083          &inputs);
1084 }
1085 
DEF_TEST(SkSLOffset,r)1086 DEF_TEST(SkSLOffset, r) {
1087     test(r,
1088          "struct Test {"
1089          "layout(offset = 0) int x;"
1090          "layout(offset = 4) int y;"
1091          "int z;"
1092          "} test;",
1093          *SkSL::ShaderCapsFactory::Default(),
1094          "#version 400\n"
1095          "struct Test {\n"
1096          "    layout (offset = 0) int x;\n"
1097          "    layout (offset = 4) int y;\n"
1098          "    int z;\n"
1099          "} test;\n");
1100 }
1101 
DEF_TEST(SkSLFragCoord,r)1102 DEF_TEST(SkSLFragCoord, r) {
1103     SkSL::Program::Settings settings;
1104     settings.fFlipY = true;
1105     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
1106     settings.fCaps = caps.get();
1107     SkSL::Program::Inputs inputs;
1108     test(r,
1109          "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1110          settings,
1111          "#version 110\n"
1112          "#extension GL_ARB_fragment_coord_conventions : require\n"
1113          "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1114          "void main() {\n"
1115          "    gl_FragColor.xy = gl_FragCoord.xy;\n"
1116          "}\n",
1117          &inputs);
1118     REPORTER_ASSERT(r, !inputs.fRTHeight);
1119 
1120     caps = SkSL::ShaderCapsFactory::FragCoordsNew();
1121     settings.fCaps = caps.get();
1122     test(r,
1123          "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1124          settings,
1125          "#version 400\n"
1126          "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1127          "out vec4 sk_FragColor;\n"
1128          "void main() {\n"
1129          "    sk_FragColor.xy = gl_FragCoord.xy;\n"
1130          "}\n",
1131          &inputs);
1132     REPORTER_ASSERT(r, !inputs.fRTHeight);
1133 
1134     caps = SkSL::ShaderCapsFactory::Default();
1135     settings.fCaps = caps.get();
1136     test(r,
1137          "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1138          settings,
1139          "#version 400\n"
1140          "uniform float u_skRTHeight;\n"
1141          "out vec4 sk_FragColor;\n"
1142          "void main() {\n"
1143          "    vec4 sk_FragCoord = vec4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, "
1144                  "gl_FragCoord.z, gl_FragCoord.w);\n"
1145          "    sk_FragColor.xy = sk_FragCoord.xy;\n"
1146          "}\n",
1147          &inputs);
1148     REPORTER_ASSERT(r, inputs.fRTHeight);
1149 
1150     settings.fFlipY = false;
1151     test(r,
1152          "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1153          settings,
1154          "#version 400\n"
1155          "out vec4 sk_FragColor;\n"
1156          "void main() {\n"
1157          "    sk_FragColor.xy = gl_FragCoord.xy;\n"
1158          "}\n",
1159          &inputs);
1160     REPORTER_ASSERT(r, !inputs.fRTHeight);
1161 
1162     test(r,
1163          "in float4 pos; void main() { sk_Position = pos; }",
1164          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1165          "#version 400\n"
1166          "out vec4 sk_FragCoord_Workaround;\n"
1167          "in vec4 pos;\n"
1168          "void main() {\n"
1169          "    sk_FragCoord_Workaround = (gl_Position = pos);\n"
1170          "}\n",
1171          SkSL::Program::kVertex_Kind);
1172 
1173     test(r,
1174          "in uniform float4 sk_RTAdjust; in float4 pos; void main() { sk_Position = pos; }",
1175          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1176          "#version 400\n"
1177          "out vec4 sk_FragCoord_Workaround;\n"
1178          "in uniform vec4 sk_RTAdjust;\n"
1179          "in vec4 pos;\n"
1180          "void main() {\n"
1181          "    sk_FragCoord_Workaround = (gl_Position = pos);\n"
1182          "    gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * sk_RTAdjust.yw,"
1183                                 " 0.0, gl_Position.w);\n"
1184          "}\n",
1185          SkSL::Program::kVertex_Kind);
1186 
1187     test(r,
1188          "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1189          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1190          "#version 400\n"
1191          "in vec4 sk_FragCoord_Workaround;\n"
1192          "out vec4 sk_FragColor;\n"
1193          "void main() {\n"
1194          "    float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"
1195          "    vec4 sk_FragCoord_Resolved = vec4(sk_FragCoord_Workaround.xyz * "
1196               "sk_FragCoord_InvW, sk_FragCoord_InvW);\n"
1197          "    sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + vec2(.5);\n"
1198          "    sk_FragColor.xy = sk_FragCoord_Resolved.xy;\n"
1199          "}\n");
1200 }
1201 
DEF_TEST(SkSLWidthAndHeight,r)1202 DEF_TEST(SkSLWidthAndHeight, r) {
1203     SkSL::Program::Settings settings;
1204     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1205     settings.fCaps = caps.get();
1206     SkSL::Program::Inputs inputs;
1207     test(r,
1208          "void main() { sk_FragColor.r = sk_FragCoord.x / sk_Width; }",
1209          settings,
1210          "#version 400\n"
1211          "uniform float u_skRTWidth;\n"
1212          "out vec4 sk_FragColor;\n"
1213          "void main() {\n"
1214          "    sk_FragColor.x = gl_FragCoord.x / u_skRTWidth;\n"
1215          "}\n",
1216          &inputs);
1217     REPORTER_ASSERT(r, inputs.fRTWidth);
1218     REPORTER_ASSERT(r, !inputs.fRTHeight);
1219 
1220     test(r,
1221          "void main() { sk_FragColor.r = sk_FragCoord.y / sk_Height; }",
1222          settings,
1223          "#version 400\n"
1224          "uniform float u_skRTHeight;\n"
1225          "out vec4 sk_FragColor;\n"
1226          "void main() {\n"
1227          "    sk_FragColor.x = gl_FragCoord.y / u_skRTHeight;\n"
1228          "}\n",
1229          &inputs);
1230     REPORTER_ASSERT(r, !inputs.fRTWidth);
1231     REPORTER_ASSERT(r, inputs.fRTHeight);
1232 }
1233 
DEF_TEST(SkSLClockwise,r)1234 DEF_TEST(SkSLClockwise, r) {
1235     test(r,
1236          "void main() { sk_FragColor = half4(sk_Clockwise ? +1 : -1); }",
1237          *SkSL::ShaderCapsFactory::Default(),
1238          "#version 400\n"
1239          "out vec4 sk_FragColor;\n"
1240          "void main() {\n"
1241          "    sk_FragColor = vec4(float(gl_FrontFacing ? 1 : -1));\n"
1242          "}\n");
1243 }
1244 
DEF_TEST(SkSLVertexID,r)1245 DEF_TEST(SkSLVertexID, r) {
1246     test(r,
1247          "out int id; void main() { id = sk_VertexID; }",
1248          *SkSL::ShaderCapsFactory::Default(),
1249          "#version 400\n"
1250          "out int id;\n"
1251          "void main() {\n"
1252          "    id = gl_VertexID;\n"
1253          "}\n",
1254          SkSL::Program::kVertex_Kind);
1255 }
1256 
DEF_TEST(SkSLInstanceID,r)1257 DEF_TEST(SkSLInstanceID, r) {
1258     test(r,
1259          "out int id; void main() { id = sk_InstanceID; }",
1260          *SkSL::ShaderCapsFactory::Default(),
1261          "#version 400\n"
1262          "out int id;\n"
1263          "void main() {\n"
1264          "    id = gl_InstanceID;\n"
1265          "}\n",
1266          SkSL::Program::kVertex_Kind);
1267 }
1268 
DEF_TEST(SkSLClipDistance,r)1269 DEF_TEST(SkSLClipDistance, r) {
1270     test(r,
1271          "void main() { sk_ClipDistance[0] = 0; }",
1272          *SkSL::ShaderCapsFactory::Default(),
1273          "#version 400\n"
1274          "void main() {\n"
1275          "    gl_ClipDistance[0] = 0.0;\n"
1276          "}\n",
1277          SkSL::Program::kVertex_Kind);
1278     test(r,
1279          "void main() { sk_FragColor = half4(sk_ClipDistance[0]); }",
1280          *SkSL::ShaderCapsFactory::Default(),
1281          "#version 400\n"
1282          "out vec4 sk_FragColor;\n"
1283          "void main() {\n"
1284          "    sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1285          "}\n");
1286 }
1287 
DEF_TEST(SkSLArrayTypes,r)1288 DEF_TEST(SkSLArrayTypes, r) {
1289     test(r,
1290          "void main() { float2 x[2] = float2[2](float2(1), float2(2));"
1291          "float2[2] y = float2[2](float2(3), float2(4));"
1292          "sk_FragColor = float4(x[0], y[1]); }",
1293          *SkSL::ShaderCapsFactory::Default(),
1294          "#version 400\n"
1295          "out vec4 sk_FragColor;\n"
1296          "void main() {\n"
1297          "    sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1298                                  "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
1299          "}\n");
1300 }
1301 
DEF_TEST(SkSLArrayIndexTypes,r)1302 DEF_TEST(SkSLArrayIndexTypes, r) {
1303     test(r,
1304          "void main() { float array[4] = float[4](1, 2, 3, 4);"
1305          "short x = 0; ushort y = 1; int z = 2; uint w = 3;"
1306          "sk_FragColor = float4(array[x], array[y], array[z], array[w]); }",
1307          *SkSL::ShaderCapsFactory::Default(),
1308          "#version 400\n"
1309          "out vec4 sk_FragColor;\n"
1310          "void main() {\n"
1311          "    sk_FragColor = vec4(float[4](1.0, 2.0, 3.0, 4.0)[0], "
1312                                  "float[4](1.0, 2.0, 3.0, 4.0)[1], "
1313                                  "float[4](1.0, 2.0, 3.0, 4.0)[2], "
1314                                  "float[4](1.0, 2.0, 3.0, 4.0)[3]);\n"
1315          "}\n");
1316 }
1317 
DEF_TEST(SkSLGeometry,r)1318 DEF_TEST(SkSLGeometry, r) {
1319     test(r,
1320          "layout(points) in;"
1321          "layout(invocations = 2) in;"
1322          "layout(line_strip, max_vertices = 2) out;"
1323          "void main() {"
1324          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1325          "EmitVertex();"
1326          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
1327          "EmitVertex();"
1328          "EndPrimitive();"
1329          "}",
1330          *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
1331          "#version 400\n"
1332          "layout (points) in ;\n"
1333          "layout (invocations = 2) in ;\n"
1334          "layout (line_strip, max_vertices = 2) out ;\n"
1335          "void main() {\n"
1336          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1337          "    EmitVertex();\n"
1338          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1339          "    EmitVertex();\n"
1340          "    EndPrimitive();\n"
1341          "}\n",
1342          SkSL::Program::kGeometry_Kind);
1343 }
1344 
DEF_TEST(SkSLSwitch,r)1345 DEF_TEST(SkSLSwitch, r) {
1346     // basic "does a switch even work" test
1347     test(r,
1348          "void main() {"
1349          "    float x;"
1350          "    switch (int(sqrt(1))) {"
1351          "        case 0:"
1352          "            x = 0.0;"
1353          "            break;"
1354          "        case 1:"
1355          "            x = 1.0;"
1356          "            break;"
1357          "        default:"
1358          "            x = 2.0;"
1359          "    }"
1360          "    sk_FragColor = float4(x);"
1361          "}",
1362          *SkSL::ShaderCapsFactory::Default(),
1363          "#version 400\n"
1364          "out vec4 sk_FragColor;\n"
1365          "void main() {\n"
1366          "    float x;\n"
1367          "    switch (int(sqrt(1.0))) {\n"
1368          "        case 0:\n"
1369          "            x = 0.0;\n"
1370          "            break;\n"
1371          "        case 1:\n"
1372          "            x = 1.0;\n"
1373          "            break;\n"
1374          "        default:\n"
1375          "            x = 2.0;\n"
1376          "    }\n"
1377          "    sk_FragColor = vec4(x);\n"
1378          "}\n");
1379     // dead code inside of switch
1380     test(r,
1381          "void main() {"
1382          "    float x;"
1383          "    switch (int(sqrt(2))) {"
1384          "        case 0:"
1385          "            x = 0.0;"
1386          "        case 1:"
1387          "            x = 1.0;"
1388          "        default:"
1389          "            x = 2.0;"
1390          "    }"
1391          "    sk_FragColor = half4(x);"
1392          "}",
1393          *SkSL::ShaderCapsFactory::Default(),
1394          "#version 400\n"
1395          "out vec4 sk_FragColor;\n"
1396          "void main() {\n"
1397          "    switch (int(sqrt(2.0))) {\n"
1398          "        case 0:\n"
1399          "            ;\n"
1400          "        case 1:\n"
1401          "            ;\n"
1402          "        default:\n"
1403          "            ;\n"
1404          "    }\n"
1405          "    sk_FragColor = vec4(2.0);\n"
1406          "}\n");
1407     // non-static test w/ fallthrough
1408     test(r,
1409          "void main() {"
1410          "    float x = 0.0;"
1411          "    switch (int(sqrt(3))) {"
1412          "        case 0:"
1413          "            x = 0.0;"
1414          "        case 1:"
1415          "            x = 1.0;"
1416          "    }"
1417          "    sk_FragColor = half4(x);"
1418          "}",
1419          *SkSL::ShaderCapsFactory::Default(),
1420          "#version 400\n"
1421          "out vec4 sk_FragColor;\n"
1422          "void main() {\n"
1423          "    float x = 0.0;\n"
1424          "    switch (int(sqrt(3.0))) {\n"
1425          "        case 0:\n"
1426          "            x = 0.0;\n"
1427          "        case 1:\n"
1428          "            x = 1.0;\n"
1429          "    }\n"
1430          "    sk_FragColor = vec4(x);\n"
1431          "}\n");
1432     // static test w/ fallthrough
1433     test(r,
1434          "void main() {"
1435          "    float x = 0.0;"
1436          "    switch (0) {"
1437          "        case 0:"
1438          "            x = 0.0;"
1439          "        case 1:"
1440          "            x = 1.0;"
1441          "    }"
1442          "    sk_FragColor = half4(x);"
1443          "}",
1444          *SkSL::ShaderCapsFactory::Default(),
1445          "#version 400\n"
1446          "out vec4 sk_FragColor;\n"
1447          "void main() {\n"
1448          "    sk_FragColor = vec4(1.0);\n"
1449          "}\n");
1450     // static test w/ fallthrough, different entry point
1451     test(r,
1452          "void main() {"
1453          "    float x = 0.0;"
1454          "    switch (1) {"
1455          "        case 0:"
1456          "            x = 0.0;"
1457          "        case 1:"
1458          "            x = 1.0;"
1459          "    }"
1460          "    sk_FragColor = half4(x);"
1461          "}",
1462          *SkSL::ShaderCapsFactory::Default(),
1463          "#version 400\n"
1464          "out vec4 sk_FragColor;\n"
1465          "void main() {\n"
1466          "    sk_FragColor = vec4(1.0);\n"
1467          "}\n");
1468     // static test w/ break
1469     test(r,
1470          "void main() {"
1471          "    float x = 0.0;"
1472          "    switch (0) {"
1473          "        case 0:"
1474          "            x = 0.0;"
1475          "            break;"
1476          "        case 1:"
1477          "            x = 1.0;"
1478          "    }"
1479          "    sk_FragColor = half4(x);"
1480          "}",
1481          *SkSL::ShaderCapsFactory::Default(),
1482          "#version 400\n"
1483          "out vec4 sk_FragColor;\n"
1484          "void main() {\n"
1485          "    sk_FragColor = vec4(0.0);\n"
1486          "}\n");
1487     // static test w/ static conditional break
1488     test(r,
1489          "void main() {"
1490          "    float x = 0.0;"
1491          "    switch (0) {"
1492          "        case 0:"
1493          "            x = 0.0;"
1494          "            if (x < 1) break;"
1495          "        case 1:"
1496          "            x = 1.0;"
1497          "    }"
1498          "    sk_FragColor = half4(x);"
1499          "}",
1500          *SkSL::ShaderCapsFactory::Default(),
1501          "#version 400\n"
1502          "out vec4 sk_FragColor;\n"
1503          "void main() {\n"
1504          "    sk_FragColor = vec4(0.0);\n"
1505          "}\n");
1506     // static test w/ non-static conditional break
1507     test(r,
1508          "void main() {"
1509          "    float x = 0.0;"
1510          "    switch (0) {"
1511          "        case 0:"
1512          "            x = 0.0;"
1513          "            if (x < sqrt(1)) break;"
1514          "        case 1:"
1515          "            x = 1.0;"
1516          "    }"
1517          "    sk_FragColor = half4(x);"
1518          "}",
1519          *SkSL::ShaderCapsFactory::Default(),
1520          "#version 400\n"
1521          "out vec4 sk_FragColor;\n"
1522          "void main() {\n"
1523          "    float x = 0.0;\n"
1524          "    switch (0) {\n"
1525          "        case 0:\n"
1526          "            x = 0.0;\n"
1527          "            if (0.0 < sqrt(1.0)) break;\n"
1528          "        case 1:\n"
1529          "            x = 1.0;\n"
1530          "    }\n"
1531          "    sk_FragColor = vec4(x);\n"
1532          "}\n");
1533 }
1534 
DEF_TEST(SkSLRectangleTexture,r)1535 DEF_TEST(SkSLRectangleTexture, r) {
1536     test(r,
1537          "uniform sampler2D test;"
1538          "void main() {"
1539          "    sk_FragColor = texture(test, float2(0.5));"
1540          "}",
1541          *SkSL::ShaderCapsFactory::Default(),
1542          "#version 400\n"
1543          "out vec4 sk_FragColor;\n"
1544          "uniform sampler2D test;\n"
1545          "void main() {\n"
1546          "    sk_FragColor = texture(test, vec2(0.5));\n"
1547          "}\n");
1548     test(r,
1549          "uniform sampler2DRect test;"
1550          "void main() {"
1551          "    sk_FragColor = texture(test, float2(0.5));"
1552          "}",
1553          *SkSL::ShaderCapsFactory::Default(),
1554          "#version 400\n"
1555          "out vec4 sk_FragColor;\n"
1556          "uniform sampler2DRect test;\n"
1557          "void main() {\n"
1558          "    sk_FragColor = texture(test, vec2(0.5));\n"
1559          "}\n");
1560     test(r,
1561          "uniform sampler2DRect test;"
1562          "void main() {"
1563          "    sk_FragColor = texture(test, float3(0.5));"
1564          "}",
1565          *SkSL::ShaderCapsFactory::Default(),
1566          "#version 400\n"
1567          "out vec4 sk_FragColor;\n"
1568          "uniform sampler2DRect test;\n"
1569          "void main() {\n"
1570          "    sk_FragColor = texture(test, vec3(0.5));\n"
1571          "}\n");
1572 }
1573 
DEF_TEST(SkSLUnusedVars,r)1574 DEF_TEST(SkSLUnusedVars, r) {
1575     test(r,
1576          "void main() {"
1577          "float a = 1, b = 2, c = 3;"
1578          "float d = c;"
1579          "float e = d;"
1580          "b++;"
1581          "d++;"
1582          "sk_FragColor = float4(b, b, d, d);"
1583          "}",
1584         *SkSL::ShaderCapsFactory::Default(),
1585          "#version 400\n"
1586          "out vec4 sk_FragColor;\n"
1587          "void main() {\n"
1588          "    float b = 2.0;\n"
1589          "    float d = 3.0;\n"
1590          "    b++;\n"
1591          "    d++;\n"
1592          "    sk_FragColor = vec4(b, b, d, d);\n"
1593          "}\n");
1594 }
1595 
DEF_TEST(SkSLMultipleAssignments,r)1596 DEF_TEST(SkSLMultipleAssignments, r) {
1597     test(r,
1598          "void main() {"
1599          "float x;"
1600          "float y;"
1601          "int z;"
1602          "x = y = z = 1;"
1603          "sk_FragColor = float4(z);"
1604          "}",
1605          *SkSL::ShaderCapsFactory::Default(),
1606          "#version 400\n"
1607          "out vec4 sk_FragColor;\n"
1608          "void main() {\n"
1609          "    sk_FragColor = vec4(1.0);\n"
1610          "}\n");
1611 }
1612 
DEF_TEST(SkSLComplexDelete,r)1613 DEF_TEST(SkSLComplexDelete, r) {
1614     test(r,
1615          "uniform float4x4 colorXform;"
1616          "uniform sampler2D sampler;"
1617          "void main() {"
1618          "float4 tmpColor;"
1619          "sk_FragColor = float4(1.0) * (tmpColor = texture(sampler, float2(1)) , "
1620          "colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * "
1621                                                      "float4(tmpColor.xyz, 1.0)).xyz, "
1622                                                      "0.0, tmpColor.w), tmpColor.w) : tmpColor);"
1623          "}",
1624          *SkSL::ShaderCapsFactory::Default(),
1625          "#version 400\n"
1626          "out vec4 sk_FragColor;\n"
1627          "uniform mat4 colorXform;\n"
1628          "uniform sampler2D sampler;\n"
1629          "void main() {\n"
1630          "    vec4 tmpColor;\n"
1631          "    sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1632          "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1633          "tmpColor);\n"
1634          "}\n");
1635 }
1636 
DEF_TEST(SkSLDependentInitializers,r)1637 DEF_TEST(SkSLDependentInitializers, r) {
1638     test(r,
1639          "void main() {"
1640          "float x = 0.5, y = x * 2;"
1641          "sk_FragColor = float4(y);"
1642          "}",
1643          *SkSL::ShaderCapsFactory::Default(),
1644          "#version 400\n"
1645          "out vec4 sk_FragColor;\n"
1646          "void main() {\n"
1647          "    sk_FragColor = vec4(1.0);\n"
1648          "}\n");
1649 }
1650 
DEF_TEST(SkSLDeadLoopVar,r)1651 DEF_TEST(SkSLDeadLoopVar, r) {
1652     test(r,
1653          "void main() {"
1654          "for (int x = 0; x < 4; ) {"
1655          "break;"
1656          "}"
1657          "}",
1658          *SkSL::ShaderCapsFactory::Default(),
1659          "#version 400\n"
1660          "void main() {\n"
1661          "    for (; true; ) {\n"
1662          "        break;\n"
1663          "    }\n"
1664          "}\n"
1665          );
1666 }
1667 
DEF_TEST(SkSLGeometryShaders,r)1668 DEF_TEST(SkSLGeometryShaders, r) {
1669     test(r,
1670          "layout(points) in;"
1671          "layout(invocations = 2) in;"
1672          "layout(line_strip, max_vertices = 2) out;"
1673          "void test() {"
1674          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
1675          "EmitVertex();"
1676          "}"
1677          "void main() {"
1678          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1679          "EmitVertex();"
1680          "}",
1681          *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(),
1682          "#version 400\n"
1683          "int sk_InvocationID;\n"
1684          "layout (points) in ;\n"
1685          "layout (line_strip, max_vertices = 4) out ;\n"
1686          "void test() {\n"
1687          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1688          "    EmitVertex();\n"
1689          "}\n"
1690          "void _invoke() {\n"
1691          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1692          "    EmitVertex();\n"
1693          "}\n"
1694          "void main() {\n"
1695          "    for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1696          "        _invoke();\n"
1697          "        EndPrimitive();\n"
1698          "    }\n"
1699          "}\n",
1700          SkSL::Program::kGeometry_Kind);
1701     test(r,
1702          "layout(points, invocations = 2) in;"
1703          "layout(invocations = 3) in;"
1704          "layout(line_strip, max_vertices = 2) out;"
1705          "void main() {"
1706          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1707          "EmitVertex();"
1708          "EndPrimitive();"
1709          "}",
1710          *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(),
1711          "#version 400\n"
1712          "#extension GL_ARB_gpu_shader5 : require\n"
1713          "layout (points, invocations = 2) in ;\n"
1714          "layout (invocations = 3) in ;\n"
1715          "layout (line_strip, max_vertices = 2) out ;\n"
1716          "void main() {\n"
1717          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1718          "    EmitVertex();\n"
1719          "    EndPrimitive();\n"
1720          "}\n",
1721          SkSL::Program::kGeometry_Kind);
1722     test(r,
1723          "layout(points, invocations = 2) in;"
1724          "layout(invocations = 3) in;"
1725          "layout(line_strip, max_vertices = 2) out;"
1726          "void main() {"
1727          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1728          "EmitVertex();"
1729          "EndPrimitive();"
1730          "}",
1731          *SkSL::ShaderCapsFactory::GeometryShaderExtensionString(),
1732          "#version 310es\n"
1733          "#extension GL_EXT_geometry_shader : require\n"
1734          "layout (points, invocations = 2) in ;\n"
1735          "layout (invocations = 3) in ;\n"
1736          "layout (line_strip, max_vertices = 2) out ;\n"
1737          "void main() {\n"
1738          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1739          "    EmitVertex();\n"
1740          "    EndPrimitive();\n"
1741          "}\n",
1742          SkSL::Program::kGeometry_Kind);
1743 }
1744 
DEF_TEST(SkSLTypePrecision,r)1745 DEF_TEST(SkSLTypePrecision, r) {
1746     test(r,
1747          "float f = 1;"
1748          "half h = 2;"
1749          "double d = 3;"
1750          "float2 f2 = float2(1, 2);"
1751          "half3 h3 = half3(1, 2, 3);"
1752          "double4 d4 = double4(1, 2, 3, 4);"
1753          "float2x2 f22 = float2x2(1, 2, 3, 4);"
1754          "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1755          "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);",
1756          *SkSL::ShaderCapsFactory::Default(),
1757          "#version 400\n"
1758          "float f = 1.0;\n"
1759          "float h = 2.0;\n"
1760          "double d = 3.0;\n"
1761          "vec2 f2 = vec2(1.0, 2.0);\n"
1762          "vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1763          "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n"
1764          "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1765          "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1766          "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1767     test(r,
1768          "float f = 1;"
1769          "half h = 2;"
1770          "float2 f2 = float2(1, 2);"
1771          "half3 h3 = half3(1, 2, 3);"
1772          "float2x2 f22 = float2x2(1, 2, 3, 4);"
1773          "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);",
1774          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1775          "#version 400\n"
1776          "precision mediump float;\n"
1777          "highp float f = 1.0;\n"
1778          "mediump float h = 2.0;\n"
1779          "highp vec2 f2 = vec2(1.0, 2.0);\n"
1780          "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1781          "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1782          "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1783 }
1784 
DEF_TEST(SkSLNumberConversions,r)1785 DEF_TEST(SkSLNumberConversions, r) {
1786     test(r,
1787          "short s = short(sqrt(1));"
1788          "int i = int(sqrt(1));"
1789          "ushort us = ushort(sqrt(1));"
1790          "uint ui = uint(sqrt(1));"
1791          "half h = sqrt(1);"
1792          "float f = sqrt(1);"
1793          "short s2s = s;"
1794          "short i2s = i;"
1795          "short us2s = short(us);"
1796          "short ui2s = short(ui);"
1797          "short h2s = short(h);"
1798          "short f2s = short(f);"
1799          "int s2i = s;"
1800          "int i2i = i;"
1801          "int us2i = int(us);"
1802          "int ui2i = int(ui);"
1803          "int h2i = int(h);"
1804          "int f2i = int(f);"
1805          "ushort s2us = ushort(s);"
1806          "ushort i2us = ushort(i);"
1807          "ushort us2us = us;"
1808          "ushort ui2us = ui;"
1809          "ushort h2us = ushort(h);"
1810          "ushort f2us = ushort(f);"
1811          "uint s2ui = uint(s);"
1812          "uint i2ui = uint(i);"
1813          "uint us2ui = us;"
1814          "uint ui2ui = ui;"
1815          "uint h2ui = uint(h);"
1816          "uint f2ui = uint(f);"
1817          "float s2f = s;"
1818          "float i2f = i;"
1819          "float us2f = us;"
1820          "float ui2f = ui;"
1821          "float h2f = h;"
1822          "float f2f = f;",
1823          *SkSL::ShaderCapsFactory::Default(),
1824          "#version 400\n"
1825          "int s = int(sqrt(1.0));\n"
1826          "int i = int(sqrt(1.0));\n"
1827          "uint us = uint(sqrt(1.0));\n"
1828          "uint ui = uint(sqrt(1.0));\n"
1829          "float h = sqrt(1.0);\n"
1830          "float f = sqrt(1.0);\n"
1831          "int s2s = s;\n"
1832          "int i2s = i;\n"
1833          "int us2s = int(us);\n"
1834          "int ui2s = int(ui);\n"
1835          "int h2s = int(h);\n"
1836          "int f2s = int(f);\n"
1837          "int s2i = s;\n"
1838          "int i2i = i;\n"
1839          "int us2i = int(us);\n"
1840          "int ui2i = int(ui);\n"
1841          "int h2i = int(h);\n"
1842          "int f2i = int(f);\n"
1843          "uint s2us = uint(s);\n"
1844          "uint i2us = uint(i);\n"
1845          "uint us2us = us;\n"
1846          "uint ui2us = ui;\n"
1847          "uint h2us = uint(h);\n"
1848          "uint f2us = uint(f);\n"
1849          "uint s2ui = uint(s);\n"
1850          "uint i2ui = uint(i);\n"
1851          "uint us2ui = us;\n"
1852          "uint ui2ui = ui;\n"
1853          "uint h2ui = uint(h);\n"
1854          "uint f2ui = uint(f);\n"
1855          "float s2f = float(s);\n"
1856          "float i2f = float(i);\n"
1857          "float us2f = float(us);\n"
1858          "float ui2f = float(ui);\n"
1859          "float h2f = h;\n"
1860          "float f2f = f;\n");
1861 }
1862 
DEF_TEST(SkSLForceHighPrecision,r)1863 DEF_TEST(SkSLForceHighPrecision, r) {
1864     test(r,
1865          "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1866          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1867          "#version 400\n"
1868          "precision mediump float;\n"
1869          "out mediump vec4 sk_FragColor;\n"
1870          "void main() {\n"
1871          "    mediump float x = sqrt(1.0);\n"
1872          "    mediump vec4 y = vec4(x);\n"
1873          "    sk_FragColor = y;\n"
1874          "}\n");
1875     SkSL::Program::Settings settings;
1876     settings.fForceHighPrecision = true;
1877     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers();
1878     settings.fCaps = caps.get();
1879     SkSL::Program::Inputs inputs;
1880     test(r,
1881          "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1882          settings,
1883          "#version 400\n"
1884          "precision mediump float;\n"
1885          "out mediump vec4 sk_FragColor;\n"
1886          "void main() {\n"
1887          "    highp float x = sqrt(1.0);\n"
1888          "    highp vec4 y = vec4(x);\n"
1889          "    sk_FragColor = y;\n"
1890          "}\n",
1891          &inputs);
1892 }
1893 
DEF_TEST(SkSLNormalization,r)1894 DEF_TEST(SkSLNormalization, r) {
1895     test(r,
1896          "uniform float4 sk_RTAdjust; void main() { sk_Position = half4(1); }",
1897          *SkSL::ShaderCapsFactory::Default(),
1898          "#version 400\n"
1899          "uniform vec4 sk_RTAdjust;\n"
1900          "void main() {\n"
1901          "    gl_Position = vec4(1.0);\n"
1902          "    gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
1903                                 "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
1904          "}\n",
1905          SkSL::Program::kVertex_Kind);
1906     test(r,
1907          "uniform float4 sk_RTAdjust;"
1908          "layout(points) in;"
1909          "layout(invocations = 2) in;"
1910          "layout(line_strip, max_vertices = 2) out;"
1911          "void main() {"
1912          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1913          "EmitVertex();"
1914          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
1915          "EmitVertex();"
1916          "EndPrimitive();"
1917          "}",
1918          *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
1919          "#version 400\n"
1920          "uniform vec4 sk_RTAdjust;\n"
1921          "layout (points) in ;\n"
1922          "layout (invocations = 2) in ;\n"
1923          "layout (line_strip, max_vertices = 2) out ;\n"
1924          "void main() {\n"
1925          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1926          "    {\n"
1927          "        gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
1928                                     "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
1929          "        EmitVertex();\n"
1930          "    }\n"
1931          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1932          "    {\n"
1933          "        gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
1934                                     "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
1935          "        EmitVertex();\n"
1936          "    }\n"
1937          "    EndPrimitive();\n"
1938          "}\n",
1939          SkSL::Program::kGeometry_Kind);
1940 }
1941 
DEF_TEST(SkSLTernaryLValue,r)1942 DEF_TEST(SkSLTernaryLValue, r) {
1943     test(r,
1944          "void main() { half r, g; (true ? r : g) = 1; (false ? r : g) = 0; "
1945          "sk_FragColor = half4(r, g, 1, 1); }",
1946          *SkSL::ShaderCapsFactory::Default(),
1947          "#version 400\n"
1948          "out vec4 sk_FragColor;\n"
1949          "void main() {\n"
1950          "    sk_FragColor = vec4(1.0, 0.0, 1.0, 1.0);\n"
1951          "}\n");
1952     test(r,
1953          "void main() { half r, g; (true ? r : g) = sqrt(1); (false ? r : g) = sqrt(0); "
1954          "sk_FragColor = half4(r, g, 1, 1); }",
1955          *SkSL::ShaderCapsFactory::Default(),
1956          "#version 400\n"
1957          "out vec4 sk_FragColor;\n"
1958          "void main() {\n"
1959          "    float r, g;\n"
1960          "    r = sqrt(1.0);\n"
1961          "    g = sqrt(0.0);\n"
1962          "    sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
1963          "}\n");
1964     test(r,
1965          "void main() {"
1966          "half r, g;"
1967          "(sqrt(1) > 0 ? r : g) = sqrt(1);"
1968          "(sqrt(0) > 0 ? r : g) = sqrt(0);"
1969          "sk_FragColor = half4(r, g, 1, 1);"
1970          "}",
1971          *SkSL::ShaderCapsFactory::Default(),
1972          "#version 400\n"
1973          "out vec4 sk_FragColor;\n"
1974          "void main() {\n"
1975          "    float r, g;\n"
1976          "    sqrt(1.0) > 0.0 ? r : g = sqrt(1.0);\n"
1977          "    sqrt(0.0) > 0.0 ? r : g = sqrt(0.0);\n"
1978          "    sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
1979          "}\n");
1980 }
1981 
DEF_TEST(SkSLIncompleteShortIntPrecision,r)1982 DEF_TEST(SkSLIncompleteShortIntPrecision, r) {
1983     test(r,
1984          "uniform sampler2D tex;"
1985          "in float2 texcoord;"
1986          "in short2 offset;"
1987          "void main() {"
1988          "    short scalar = offset.y;"
1989          "    sk_FragColor = texture(tex, texcoord + float2(offset * scalar));"
1990          "}",
1991          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1992          "#version 400\n"
1993          "precision mediump float;\n"
1994          "out mediump vec4 sk_FragColor;\n"
1995          "uniform sampler2D tex;\n"
1996          "in highp vec2 texcoord;\n"
1997          "in mediump ivec2 offset;\n"
1998          "void main() {\n"
1999          "    mediump int scalar = offset.y;\n"
2000          "    sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2001          "}\n",
2002          SkSL::Program::kFragment_Kind);
2003     test(r,
2004          "uniform sampler2D tex;"
2005          "in float2 texcoord;"
2006          "in short2 offset;"
2007          "void main() {"
2008          "    short scalar = offset.y;"
2009          "    sk_FragColor = texture(tex, texcoord + float2(offset * scalar));"
2010          "}",
2011          *SkSL::ShaderCapsFactory::IncompleteShortIntPrecision(),
2012          "#version 310es\n"
2013          "precision mediump float;\n"
2014          "out mediump vec4 sk_FragColor;\n"
2015          "uniform sampler2D tex;\n"
2016          "in highp vec2 texcoord;\n"
2017          "in highp ivec2 offset;\n"
2018          "void main() {\n"
2019          "    highp int scalar = offset.y;\n"
2020          "    sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2021          "}\n",
2022          SkSL::Program::kFragment_Kind);
2023 }
2024 
DEF_TEST(SkSLFrExp,r)2025 DEF_TEST(SkSLFrExp, r) {
2026     test(r,
2027          "void main() {"
2028          "    int exp;"
2029          "    float foo = frexp(0.5, exp);"
2030          "    sk_FragColor = float4(exp);"
2031          "}",
2032          *SkSL::ShaderCapsFactory::Default(),
2033          "#version 400\n"
2034          "out vec4 sk_FragColor;\n"
2035          "void main() {\n"
2036          "    int exp;\n"
2037          "    float foo = frexp(0.5, exp);\n"
2038          "    sk_FragColor = vec4(float(exp));\n"
2039          "}\n");
2040 }
2041 
DEF_TEST(SkSLWorkaroundAddAndTrueToLoopCondition,r)2042 DEF_TEST(SkSLWorkaroundAddAndTrueToLoopCondition, r) {
2043     test(r,
2044          "void main() {"
2045          "    int c = 0;"
2046          "    for (int i = 0; i < 4 || c < 10; ++i) {"
2047          "        c += 1;"
2048          "    }"
2049          "}",
2050          *SkSL::ShaderCapsFactory::AddAndTrueToLoopCondition(),
2051          "#version 400\n"
2052          "void main() {\n"
2053          "    int c = 0;\n"
2054          "    for (int i = 0;(i < 4 || c < 10) && true; ++i) {\n"
2055          "        c += 1;\n"
2056          "    }\n"
2057          "}\n",
2058          SkSL::Program::kFragment_Kind
2059          );
2060 }
2061 
DEF_TEST(SkSLWorkaroundUnfoldShortCircuitAsTernary,r)2062 DEF_TEST(SkSLWorkaroundUnfoldShortCircuitAsTernary, r) {
2063     test(r,
2064          "uniform bool x;"
2065          "uniform bool y;"
2066          "uniform int i;"
2067          "uniform int j;"
2068          "void main() {"
2069          "    bool andXY = x && y;"
2070          "    bool orXY = x || y;"
2071          "    bool combo = (x && y) || (x || y);"
2072          "    bool prec = (i + j == 3) && y;"
2073          "    while (andXY && orXY && combo && prec) {"
2074          "        sk_FragColor = float4(0);"
2075          "        break;"
2076          "    }"
2077          "}",
2078          *SkSL::ShaderCapsFactory::UnfoldShortCircuitAsTernary(),
2079          "#version 400\n"
2080          "out vec4 sk_FragColor;\n"
2081          "uniform bool x;\n"
2082          "uniform bool y;\n"
2083          "uniform int i;\n"
2084          "uniform int j;\n"
2085          "void main() {\n"
2086          "    bool andXY = x ? y : false;\n"
2087          "    bool orXY = x ? true : y;\n"
2088          "    bool combo = (x ? y : false) ? true : (x ? true : y);\n"
2089          "    bool prec = i + j == 3 ? y : false;\n"
2090          "    while (((andXY ? orXY : false) ? combo : false) ? prec : false) {\n"
2091          "        sk_FragColor = vec4(0.0);\n"
2092          "        break;\n"
2093          "    }\n"
2094          "}\n",
2095          SkSL::Program::kFragment_Kind
2096          );
2097 }
2098 
DEF_TEST(SkSLWorkaroundEmulateAbsIntFunction,r)2099 DEF_TEST(SkSLWorkaroundEmulateAbsIntFunction, r) {
2100     test(r,
2101          "uniform int i;"
2102          "uniform float f;"
2103          "void main() {"
2104          "    float output = abs(f) + abs(i);"
2105          "    sk_FragColor = float4(output);"
2106          "}",
2107          *SkSL::ShaderCapsFactory::EmulateAbsIntFunction(),
2108          "#version 400\n"
2109          "int _absemulation(int x) {\n"
2110          "    return x * sign(x);\n"
2111          "}\n"
2112          "out vec4 sk_FragColor;\n"
2113          "uniform int i;\n"
2114          "uniform float f;\n"
2115          "void main() {\n"
2116          "    float output = abs(f) + float(_absemulation(i));\n"
2117          "    sk_FragColor = vec4(output);\n"
2118          "}\n",
2119          SkSL::Program::kFragment_Kind
2120          );
2121 }
2122 
DEF_TEST(SkSLWorkaroundRewriteDoWhileLoops,r)2123 DEF_TEST(SkSLWorkaroundRewriteDoWhileLoops, r) {
2124     test(r,
2125          "void main() {"
2126          "    int i = 0;"
2127          "    do {"
2128          "      ++i;"
2129          "      do {"
2130          "        i++;"
2131          "      } while (true);"
2132          "    } while (i < 10);"
2133          "    sk_FragColor = float4(i);"
2134          "}",
2135          *SkSL::ShaderCapsFactory::RewriteDoWhileLoops(),
2136          "#version 400\n"
2137          "out vec4 sk_FragColor;\n"
2138          "void main() {\n"
2139          "    int i = 0;\n"
2140          "    bool _tmpLoopSeenOnce0 = false;\n"
2141          "    while (true) {\n"
2142          "        if (_tmpLoopSeenOnce0) {\n"
2143          "            if (!(i < 10)) {\n"
2144          "                break;\n"
2145          "            }\n"
2146          "        }\n"
2147          "        _tmpLoopSeenOnce0 = true;\n"
2148          "        {\n"
2149          "            ++i;\n"
2150          "            bool _tmpLoopSeenOnce1 = false;\n"
2151          "            while (true) {\n"
2152          "                if (_tmpLoopSeenOnce1) {\n"
2153          "                    if (!true) {\n"
2154          "                        break;\n"
2155          "                    }\n"
2156          "                }\n"
2157          "                _tmpLoopSeenOnce1 = true;\n"
2158          "                {\n"
2159          "                    i++;\n"
2160          "                }\n"
2161          "            }\n"
2162          "        }\n"
2163          "    }\n"
2164          "    sk_FragColor = vec4(float(i));\n"
2165          "}\n",
2166          SkSL::Program::kFragment_Kind
2167          );
2168 }
2169 
DEF_TEST(SkSLWorkaroundRemovePowWithConstantExponent,r)2170 DEF_TEST(SkSLWorkaroundRemovePowWithConstantExponent, r) {
2171     test(r,
2172          "uniform float x;"
2173          "uniform float y;"
2174          "void main() {"
2175          "    float z = pow(x + 1.0, y + 2.0);"
2176          "    sk_FragColor = float4(z);"
2177          "}",
2178          *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2179          "#version 400\n"
2180          "out vec4 sk_FragColor;\n"
2181          "uniform float x;\n"
2182          "uniform float y;\n"
2183          "void main() {\n"
2184          "    float z = exp2((y + 2.0) * log2(x + 1.0));\n"
2185          "    sk_FragColor = vec4(z);\n"
2186          "}\n",
2187          SkSL::Program::kFragment_Kind
2188          );
2189 }
2190