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