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