• 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 (true ^^ expr1) {"   // -> if (!expr1)
821          "        sk_FragColor.r = 3;"
822          "    } else if (false ^^ expr2) {"  // -> if (expr2)
823          "        sk_FragColor.r = 4;"
824          "    } else if (false || expr2) {"  // -> if (expr2)
825          "        sk_FragColor.r = 5;"
826          "    } else if (true || expr2) {"   // -> if (true) -> replaces unreachable else
827          "        sk_FragColor.r = 6;"
828          "    } else {"                      // removed
829          "        sk_FragColor.r = -7;"
830          "    }"
831          // Test short-circuiting of right hand side boolean literals
832          "    if (expr1 && true) {"          // -> if (expr1)
833          "        sk_FragColor.r = 1;"
834          "    } else if (expr1 && false) {"  // -> if (false) -> block removed
835          "        sk_FragColor.r = -2;"
836          "    } else if (expr1 ^^ true) {"   // -> if (!expr1)
837          "        sk_FragColor.r = 3;"
838          "    } else if (expr2 ^^ false) {"  // -> if (expr2)
839          "        sk_FragColor.r = 4;"
840          "    } else if (expr2 || false) {"  // -> if (expr2)
841          "        sk_FragColor.r = 5;"
842          "    } else if (expr2 || true) {"   // -> if (true) -> replaces unreachable else
843          "        sk_FragColor.r = 6;"
844          "    } else {"                      // removed
845          "        sk_FragColor.r = -7;"
846          "    }"
847          "}",
848          *SkSL::ShaderCapsFactory::Default(),
849          "#version 400\n"
850          "out vec4 sk_FragColor;\n"
851          "void main() {\n"
852          "    bool expr1 = gl_FragCoord.x > 0.0;\n"
853          "    bool expr2 = gl_FragCoord.y > 0.0;\n"
854          "    if (expr1) {\n"
855          "        sk_FragColor.x = 1.0;\n"
856          "    } else if (!expr1) {\n"
857          "        sk_FragColor.x = 3.0;\n"
858          "    } else if (expr2) {\n"
859          "        sk_FragColor.x = 4.0;\n"
860          "    } else if (expr2) {\n"
861          "        sk_FragColor.x = 5.0;\n"
862          "    } else {\n"
863          "        sk_FragColor.x = 6.0;\n"
864          "    }\n"
865          "    if (expr1) {\n"
866          "        sk_FragColor.x = 1.0;\n"
867          "    } else if (!expr1) {\n"
868          "        sk_FragColor.x = 3.0;\n"
869          "    } else if (expr2) {\n"
870          "        sk_FragColor.x = 4.0;\n"
871          "    } else if (expr2) {\n"
872          "        sk_FragColor.x = 5.0;\n"
873          "    } else {\n"
874          "        sk_FragColor.x = 6.0;\n"
875          "    }\n"
876          "}\n");
877 }
878 
DEF_TEST(SkSLVecFolding,r)879 DEF_TEST(SkSLVecFolding, r) {
880     test(r,
881          "void main() {\n"
882          "sk_FragColor.r = half4(0.5, 1, 1, 1).x;\n"
883          "sk_FragColor = half4(half2(1), half2(2, 3)) + half4(5, 6, 7, 8);\n"
884          "sk_FragColor = half4(8, half3(10)) - half4(1);\n"
885          "sk_FragColor = half4(2) * half4(1, 2, 3, 4);\n"
886          "sk_FragColor = half4(12) / half4(1, 2, 3, 4);\n"
887          "sk_FragColor.r = (half4(12) / half4(1, 2, 3, 4)).y;\n"
888          "sk_FragColor.x = half4(1) == half4(1) ? 1.0 : -1.0;\n"
889          "sk_FragColor.x = half4(1) == half4(2) ? 2.0 : -2.0;\n"
890          "sk_FragColor.x = half2(1) == half2(1, 1) ? 3.0 : -3.0;\n"
891          "sk_FragColor.x = half2(1, 1) == half2(1, 1) ? 4.0 : -4.0;\n"
892          "sk_FragColor.x = half2(1) == half2(1, 0) ? 5.0 : -5.0;\n"
893          "sk_FragColor.x = half4(1) == half4(half2(1), half2(1)) ? 6.0 : -6.0;\n"
894          "sk_FragColor.x = half4(half3(1), 1) == half4(half2(1), half2(1)) ? 7.0 : -7.0;\n"
895          "sk_FragColor.x = half4(half3(1), 1) == half4(half2(1), 1, 0) ? 8.0 : -8.0;\n"
896          "sk_FragColor.x = half2(1) != half2(1, 0) ? 9.0 : -9.0;\n"
897          "sk_FragColor.x = half4(1) != half4(half2(1), half2(1)) ? 10.0 : -10.0;\n"
898          "sk_FragColor = half4(half(sqrt(1))) * half4(1);\n"
899          "sk_FragColor = half4(1) * half4(half(sqrt(2)));\n"
900          "sk_FragColor = half4(0) * half4(half(sqrt(3)));\n"
901          "sk_FragColor = half4(half(sqrt(4))) * half4(0);\n"
902          "sk_FragColor = half4(0) / half4(half(sqrt(5)));\n"
903          "sk_FragColor = half4(0) + half4(half(sqrt(6)));\n"
904          "sk_FragColor = half4(half(sqrt(7))) + half4(0);\n"
905          "sk_FragColor = half4(half(sqrt(8))) - half4(0);\n"
906          "sk_FragColor = half4(0) + half(half(sqrt(9)));\n"
907          "sk_FragColor = half4(0) * half(sqrt(10));\n"
908          "sk_FragColor = half4(0) / half(sqrt(11));\n"
909          "sk_FragColor = half4(1) * half(sqrt(12));\n"
910          "sk_FragColor = 0 + half4(half(sqrt(13)));\n"
911          "sk_FragColor = 0 * half4(half(sqrt(14)));\n"
912          "sk_FragColor = 0 / half4(half(sqrt(15)));\n"
913          "sk_FragColor = 1 * half4(half(sqrt(16)));\n"
914          "sk_FragColor = half4(half(sqrt(17))) + 0;\n"
915          "sk_FragColor = half4(half(sqrt(18))) * 0;\n"
916          "sk_FragColor = half4(half(sqrt(19))) * 1;\n"
917          "sk_FragColor = half4(half(sqrt(19.5))) - 0;\n"
918          "sk_FragColor = half(half(sqrt(20))) * half4(1);\n"
919          "sk_FragColor = half(half(sqrt(21))) + half4(0);\n"
920          "sk_FragColor = half(half(sqrt(22))) - half4(0);\n"
921          "sk_FragColor = half(half(sqrt(23))) / half4(1);\n"
922          "sk_FragColor = half4(half(sqrt(24))) / 1;\n"
923          "sk_FragColor += half4(1);\n"
924          "sk_FragColor += half4(0);\n"
925          "sk_FragColor -= half4(1);\n"
926          "sk_FragColor -= half4(0);\n"
927          "sk_FragColor *= half4(1);\n"
928          "sk_FragColor *= half4(2);\n"
929          "sk_FragColor /= half4(1);\n"
930          "sk_FragColor /= half4(2);\n"
931          "}",
932          *SkSL::ShaderCapsFactory::Default(),
933          "#version 400\n"
934          "out vec4 sk_FragColor;\n"
935          "void main() {\n"
936          "    sk_FragColor.x = 0.5;\n"
937          "    sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
938          "    sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
939          "    sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
940          "    sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
941          "    sk_FragColor.x = 6.0;\n"
942          "    sk_FragColor.x = 1.0;\n"
943          "    sk_FragColor.x = -2.0;\n"
944          "    sk_FragColor.x = 3.0;\n"
945          "    sk_FragColor.x = 4.0;\n"
946          "    sk_FragColor.x = -5.0;\n"
947          "    sk_FragColor.x = 6.0;\n"
948          "    sk_FragColor.x = 7.0;\n"
949          "    sk_FragColor.x = -8.0;\n"
950          "    sk_FragColor.x = 9.0;\n"
951          "    sk_FragColor.x = -10.0;\n"
952          "    sk_FragColor = vec4(sqrt(1.0));\n"
953          "    sk_FragColor = vec4(sqrt(2.0));\n"
954          "    sk_FragColor = vec4(0.0);\n"
955          "    sk_FragColor = vec4(0.0);\n"
956          "    sk_FragColor = vec4(0.0);\n"
957          "    sk_FragColor = vec4(sqrt(6.0));\n"
958          "    sk_FragColor = vec4(sqrt(7.0));\n"
959          "    sk_FragColor = vec4(sqrt(8.0));\n"
960          "    sk_FragColor = vec4(sqrt(9.0));\n"
961          "    sk_FragColor = vec4(0.0);\n"
962          "    sk_FragColor = vec4(0.0);\n"
963          "    sk_FragColor = vec4(sqrt(12.0));\n"
964          "    sk_FragColor = vec4(sqrt(13.0));\n"
965          "    sk_FragColor = vec4(0.0);\n"
966          "    sk_FragColor = vec4(0.0);\n"
967          "    sk_FragColor = vec4(sqrt(16.0));\n"
968          "    sk_FragColor = vec4(sqrt(17.0));\n"
969          "    sk_FragColor = vec4(0.0);\n"
970          "    sk_FragColor = vec4(sqrt(19.0));\n"
971          "    sk_FragColor = vec4(sqrt(19.5));\n"
972          "    sk_FragColor = vec4(sqrt(20.0));\n"
973          "    sk_FragColor = vec4(sqrt(21.0));\n"
974          "    sk_FragColor = vec4(sqrt(22.0));\n"
975          "    sk_FragColor = vec4(sqrt(23.0));\n"
976          "    sk_FragColor = vec4(sqrt(24.0));\n"
977          "    sk_FragColor += vec4(1.0);\n"
978          "    sk_FragColor -= vec4(1.0);\n"
979          "    sk_FragColor *= vec4(2.0);\n"
980          "    sk_FragColor /= vec4(2.0);\n"
981          "}\n");
982 }
983 
DEF_TEST(SkSLMatFolding,r)984 DEF_TEST(SkSLMatFolding, r) {
985     test(r,
986          "void main() {"
987          "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == "
988                           "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;"
989          "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == "
990                           "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;"
991          "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;"
992          "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;"
993          "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;"
994          "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;"
995          "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))"
996                            "? 7 : -7;"
997          "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;"
998          "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;"
999          "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), "
1000                           "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;"
1001          "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;"
1002          "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;"
1003          "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;"
1004          "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;"
1005          "}",
1006          *SkSL::ShaderCapsFactory::Default(),
1007          "#version 400\n"
1008          "out vec4 sk_FragColor;\n"
1009          "void main() {\n"
1010          "    sk_FragColor.x = 1.0;\n"
1011          "    sk_FragColor.x = -2.0;\n"
1012          "    sk_FragColor.x = 3.0;\n"
1013          "    sk_FragColor.x = -4.0;\n"
1014          "    sk_FragColor.x = 5.0;\n"
1015          "    sk_FragColor.x = -6.0;\n"
1016          "    sk_FragColor.x = 7.0;\n"
1017          "    sk_FragColor.x = -8.0;\n"
1018          "    sk_FragColor.x = 9.0;\n"
1019          "    sk_FragColor.x = 10.0;\n"
1020          "    sk_FragColor.x = 11.0;\n"
1021          "    sk_FragColor.x = 12.0;\n"
1022          "    sk_FragColor.x = 13.0;\n"
1023          "    sk_FragColor.x = 14.0;\n"
1024          "}\n");
1025 }
1026 
DEF_TEST(SkSLConstantIf,r)1027 DEF_TEST(SkSLConstantIf, r) {
1028     test(r,
1029          "void main() {"
1030          "int x;"
1031          "if (true) x = 1;"
1032          "if (2 > 1) x = 2; else x = 3;"
1033          "if (1 > 2) x = 4; else x = 5;"
1034          "if (false) x = 6;"
1035          "sk_FragColor.r = x;"
1036          "}",
1037          *SkSL::ShaderCapsFactory::Default(),
1038          "#version 400\n"
1039          "out vec4 sk_FragColor;\n"
1040          "void main() {\n"
1041          "    sk_FragColor.x = 5.0;\n"
1042          "}\n");
1043 }
1044 
DEF_TEST(SkSLCaps,r)1045 DEF_TEST(SkSLCaps, r) {
1046     test(r,
1047          "void main() {"
1048          "int x = 0;"
1049          "int y = 0;"
1050          "int z = 0;"
1051          "if (sk_Caps.externalTextureSupport) x = 1;"
1052          "if (sk_Caps.fbFetchSupport) y = 1;"
1053          "if (sk_Caps.canUseAnyFunctionInShader) z = 1;"
1054          "sk_FragColor = half4(x, y, z, 0.0);"
1055          "}",
1056          *SkSL::ShaderCapsFactory::VariousCaps(),
1057          "#version 400\n"
1058          "out vec4 sk_FragColor;\n"
1059          "void main() {\n"
1060          "    sk_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\n"
1061          "}\n");
1062 }
1063 
DEF_TEST(SkSLTexture,r)1064 DEF_TEST(SkSLTexture, r) {
1065     test(r,
1066          "uniform sampler1D one;"
1067          "uniform sampler2D two;"
1068          "void main() {"
1069          "float4 a = sample(one, 0);"
1070          "float4 b = sample(two, float2(0));"
1071          "float4 c = sample(one, float2(0));"
1072          "float4 d = sample(two, float3(0));"
1073          "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
1074          "}",
1075          *SkSL::ShaderCapsFactory::Default(),
1076          "#version 400\n"
1077          "out vec4 sk_FragColor;\n"
1078          "uniform sampler1D one;\n"
1079          "uniform sampler2D two;\n"
1080          "void main() {\n"
1081          "    vec4 a = texture(one, 0.0);\n"
1082          "    vec4 b = texture(two, vec2(0.0));\n"
1083          "    vec4 c = textureProj(one, vec2(0.0));\n"
1084          "    vec4 d = textureProj(two, vec3(0.0));\n"
1085          "    sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1086          "}\n");
1087     test(r,
1088          "uniform sampler1D one;"
1089          "uniform sampler2D two;"
1090          "void main() {"
1091          "float4 a = sample(one, 0);"
1092          "float4 b = sample(two, float2(0));"
1093          "float4 c = sample(one, float2(0));"
1094          "float4 d = sample(two, float3(0));"
1095          "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
1096          "}",
1097          *SkSL::ShaderCapsFactory::Version110(),
1098          "#version 110\n"
1099          "uniform sampler1D one;\n"
1100          "uniform sampler2D two;\n"
1101          "void main() {\n"
1102          "    vec4 a = texture1D(one, 0.0);\n"
1103          "    vec4 b = texture2D(two, vec2(0.0));\n"
1104          "    vec4 c = texture1DProj(one, vec2(0.0));\n"
1105          "    vec4 d = texture2DProj(two, vec3(0.0));\n"
1106          "    gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1107          "}\n");
1108 }
1109 
DEF_TEST(SkSLSharpen,r)1110 DEF_TEST(SkSLSharpen, r) {
1111     SkSL::Program::Settings settings;
1112     settings.fSharpenTextures = true;
1113     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1114     settings.fCaps = caps.get();
1115     SkSL::Program::Inputs inputs;
1116     test(r,
1117          "uniform sampler1D one;"
1118          "uniform sampler2D two;"
1119          "void main() {"
1120          "float4 a = sample(one, 0);"
1121          "float4 b = sample(two, float2(0));"
1122          "float4 c = sample(one, float2(0));"
1123          "float4 d = sample(two, float3(0));"
1124          "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
1125          "}",
1126          settings,
1127          "#version 400\n"
1128          "out vec4 sk_FragColor;\n"
1129          "uniform sampler1D one;\n"
1130          "uniform sampler2D two;\n"
1131          "void main() {\n"
1132          "    vec4 a = texture(one, 0.0, -0.5);\n"
1133          "    vec4 b = texture(two, vec2(0.0), -0.5);\n"
1134          "    vec4 c = textureProj(one, vec2(0.0), -0.5);\n"
1135          "    vec4 d = textureProj(two, vec3(0.0), -0.5);\n"
1136          "    sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1137          "}\n",
1138          &inputs);
1139 
1140     caps = SkSL::ShaderCapsFactory::Version110();
1141     settings.fCaps = caps.get();
1142     test(r,
1143          "uniform sampler1D one;"
1144          "uniform sampler2D two;"
1145          "void main() {"
1146          "float4 a = sample(one, 0);"
1147          "float4 b = sample(two, float2(0));"
1148          "float4 c = sample(one, float2(0));"
1149          "float4 d = sample(two, float3(0));"
1150          "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
1151          "}",
1152          settings,
1153          "#version 110\n"
1154          "uniform sampler1D one;\n"
1155          "uniform sampler2D two;\n"
1156          "void main() {\n"
1157          "    vec4 a = texture1D(one, 0.0, -0.5);\n"
1158          "    vec4 b = texture2D(two, vec2(0.0), -0.5);\n"
1159          "    vec4 c = texture1DProj(one, vec2(0.0), -0.5);\n"
1160          "    vec4 d = texture2DProj(two, vec3(0.0), -0.5);\n"
1161          "    gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1162          "}\n",
1163          &inputs);
1164 }
1165 
DEF_TEST(SkSLOffset,r)1166 DEF_TEST(SkSLOffset, r) {
1167     test(r,
1168          "struct Test {"
1169          "layout(offset = 0) int x;"
1170          "layout(offset = 4) int y;"
1171          "int z;"
1172          "} test;"
1173          "void main() {"
1174          "Test t;"
1175          "t.x = 0;"
1176          "sk_FragColor.r = half(t.x);"
1177          "}",
1178          *SkSL::ShaderCapsFactory::Default(),
1179          "#version 400\n"
1180          "out vec4 sk_FragColor;\n"
1181          "void main() {\n"
1182          "    struct Test {\n"
1183          "        layout (offset = 0) int x;\n"
1184          "        layout (offset = 4) int y;\n"
1185          "        int z;\n"
1186          "    } t;\n"
1187          "    t.x = 0;\n"
1188          "    sk_FragColor.x = float(t.x);\n"
1189          "}\n");
1190 }
1191 
DEF_TEST(SkSLFragCoord,r)1192 DEF_TEST(SkSLFragCoord, r) {
1193     SkSL::Program::Settings settings;
1194     settings.fFlipY = true;
1195     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
1196     settings.fCaps = caps.get();
1197     SkSL::Program::Inputs inputs;
1198     test(r,
1199          "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
1200          settings,
1201          "#version 110\n"
1202          "#extension GL_ARB_fragment_coord_conventions : require\n"
1203          "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1204          "void main() {\n"
1205          "    gl_FragColor.xy = gl_FragCoord.xy;\n"
1206          "}\n",
1207          &inputs);
1208     REPORTER_ASSERT(r, !inputs.fRTHeight);
1209 
1210     caps = SkSL::ShaderCapsFactory::FragCoordsNew();
1211     settings.fCaps = caps.get();
1212     test(r,
1213          "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
1214          settings,
1215          "#version 400\n"
1216          "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1217          "out vec4 sk_FragColor;\n"
1218          "void main() {\n"
1219          "    sk_FragColor.xy = gl_FragCoord.xy;\n"
1220          "}\n",
1221          &inputs);
1222     REPORTER_ASSERT(r, !inputs.fRTHeight);
1223 
1224     caps = SkSL::ShaderCapsFactory::Default();
1225     settings.fCaps = caps.get();
1226     test(r,
1227          "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
1228          settings,
1229          "#version 400\n"
1230          "uniform float u_skRTHeight;\n"
1231          "out vec4 sk_FragColor;\n"
1232          "void main() {\n"
1233          "    vec4 sk_FragCoord = vec4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, "
1234                  "gl_FragCoord.z, gl_FragCoord.w);\n"
1235          "    sk_FragColor.xy = sk_FragCoord.xy;\n"
1236          "}\n",
1237          &inputs);
1238     REPORTER_ASSERT(r, inputs.fRTHeight);
1239 
1240     settings.fFlipY = false;
1241     test(r,
1242          "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
1243          settings,
1244          "#version 400\n"
1245          "out vec4 sk_FragColor;\n"
1246          "void main() {\n"
1247          "    sk_FragColor.xy = gl_FragCoord.xy;\n"
1248          "}\n",
1249          &inputs);
1250     REPORTER_ASSERT(r, !inputs.fRTHeight);
1251 
1252     test(r,
1253          "in float4 pos; void main() { sk_Position = pos; }",
1254          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1255          "#version 400\n"
1256          "out vec4 sk_FragCoord_Workaround;\n"
1257          "in vec4 pos;\n"
1258          "void main() {\n"
1259          "    sk_FragCoord_Workaround = (gl_Position = pos);\n"
1260          "}\n",
1261          SkSL::Program::kVertex_Kind);
1262 
1263     test(r,
1264          "uniform float4 sk_RTAdjust; in float4 pos; void main() { sk_Position = pos; }",
1265          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1266          "#version 400\n"
1267          "out vec4 sk_FragCoord_Workaround;\n"
1268          "uniform vec4 sk_RTAdjust;\n"
1269          "in vec4 pos;\n"
1270          "void main() {\n"
1271          "    sk_FragCoord_Workaround = (gl_Position = pos);\n"
1272          "    gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * sk_RTAdjust.yw,"
1273                                 " 0.0, gl_Position.w);\n"
1274          "}\n",
1275          SkSL::Program::kVertex_Kind);
1276 
1277     test(r,
1278          "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
1279          *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1280          "#version 400\n"
1281          "in vec4 sk_FragCoord_Workaround;\n"
1282          "out vec4 sk_FragColor;\n"
1283          "void main() {\n"
1284          "    float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"
1285          "    vec4 sk_FragCoord_Resolved = vec4(sk_FragCoord_Workaround.xyz * "
1286               "sk_FragCoord_InvW, sk_FragCoord_InvW);\n"
1287          "    sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + vec2(.5);\n"
1288          "    sk_FragColor.xy = sk_FragCoord_Resolved.xy;\n"
1289          "}\n");
1290 }
1291 
DEF_TEST(SkSLWidthAndHeight,r)1292 DEF_TEST(SkSLWidthAndHeight, r) {
1293     SkSL::Program::Settings settings;
1294     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1295     settings.fCaps = caps.get();
1296     SkSL::Program::Inputs inputs;
1297     test(r,
1298          "void main() { sk_FragColor.r = half(sk_FragCoord.x / sk_Width); }",
1299          settings,
1300          "#version 400\n"
1301          "uniform float u_skRTWidth;\n"
1302          "out vec4 sk_FragColor;\n"
1303          "void main() {\n"
1304          "    sk_FragColor.x = gl_FragCoord.x / u_skRTWidth;\n"
1305          "}\n",
1306          &inputs);
1307     REPORTER_ASSERT(r, inputs.fRTWidth);
1308     REPORTER_ASSERT(r, !inputs.fRTHeight);
1309 
1310     test(r,
1311          "void main() { sk_FragColor.r = half(sk_FragCoord.y / sk_Height); }",
1312          settings,
1313          "#version 400\n"
1314          "uniform float u_skRTHeight;\n"
1315          "out vec4 sk_FragColor;\n"
1316          "void main() {\n"
1317          "    sk_FragColor.x = gl_FragCoord.y / u_skRTHeight;\n"
1318          "}\n",
1319          &inputs);
1320     REPORTER_ASSERT(r, !inputs.fRTWidth);
1321     REPORTER_ASSERT(r, inputs.fRTHeight);
1322 }
1323 
DEF_TEST(SkSLClockwise,r)1324 DEF_TEST(SkSLClockwise, r) {
1325     test(r,
1326          "void main() { sk_FragColor = half4(sk_Clockwise ? +1 : -1); }",
1327          *SkSL::ShaderCapsFactory::Default(),
1328          "#version 400\n"
1329          "out vec4 sk_FragColor;\n"
1330          "void main() {\n"
1331          "    sk_FragColor = vec4(float(gl_FrontFacing ? 1 : -1));\n"
1332          "}\n");
1333 }
1334 
DEF_TEST(SkSLSampleMask,r)1335 DEF_TEST(SkSLSampleMask, r) {
1336     test(r,
1337          "void main() { sk_SampleMask[0] |= 8; }",
1338          *SkSL::ShaderCapsFactory::SampleMaskSupport(),
1339          "#version 400\n"
1340          "void main() {\n"
1341          "    gl_SampleMask[0] |= 8;\n"
1342          "}\n");
1343 }
1344 
DEF_TEST(SkSLVertexID,r)1345 DEF_TEST(SkSLVertexID, r) {
1346     test(r,
1347          "out int id; void main() { id = sk_VertexID; }",
1348          *SkSL::ShaderCapsFactory::Default(),
1349          "#version 400\n"
1350          "out int id;\n"
1351          "void main() {\n"
1352          "    id = gl_VertexID;\n"
1353          "}\n",
1354          SkSL::Program::kVertex_Kind);
1355 }
1356 
DEF_TEST(SkSLInstanceID,r)1357 DEF_TEST(SkSLInstanceID, r) {
1358     test(r,
1359          "out int id; void main() { id = sk_InstanceID; }",
1360          *SkSL::ShaderCapsFactory::Default(),
1361          "#version 400\n"
1362          "out int id;\n"
1363          "void main() {\n"
1364          "    id = gl_InstanceID;\n"
1365          "}\n",
1366          SkSL::Program::kVertex_Kind);
1367 }
1368 
DEF_TEST(SkSLClipDistance,r)1369 DEF_TEST(SkSLClipDistance, r) {
1370     test(r,
1371          "void main() { sk_ClipDistance[0] = 0; }",
1372          *SkSL::ShaderCapsFactory::Default(),
1373          "#version 400\n"
1374          "void main() {\n"
1375          "    gl_ClipDistance[0] = 0.0;\n"
1376          "}\n",
1377          SkSL::Program::kVertex_Kind);
1378     test(r,
1379          "void main() { sk_FragColor = half4(half(sk_ClipDistance[0])); }",
1380          *SkSL::ShaderCapsFactory::Default(),
1381          "#version 400\n"
1382          "out vec4 sk_FragColor;\n"
1383          "void main() {\n"
1384          "    sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1385          "}\n");
1386 }
1387 
DEF_TEST(SkSLArrayTypes,r)1388 DEF_TEST(SkSLArrayTypes, r) {
1389     test(r,
1390          "void main() { float2 x[2] = float2[2](float2(1), float2(2));"
1391          "float2[2] y = float2[2](float2(3), float2(4));"
1392          "sk_FragColor = half4(half2(x[0]), half2(y[1])); }",
1393          *SkSL::ShaderCapsFactory::Default(),
1394          "#version 400\n"
1395          "out vec4 sk_FragColor;\n"
1396          "void main() {\n"
1397          "    sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1398                                  "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
1399          "}\n");
1400 }
1401 
DEF_TEST(SkSLArrayIndexTypes,r)1402 DEF_TEST(SkSLArrayIndexTypes, r) {
1403     test(r,
1404          "void main() { float array[4] = float[4](1, 2, 3, 4);\n"
1405          "short x = 0;\n ushort y = 1;\n int z = 2;\n uint w = 3;\n"
1406          "sk_FragColor = half4(half(array[x]), half(array[y]), half(array[z]), half(array[w])); }",
1407          *SkSL::ShaderCapsFactory::Default(),
1408          "#version 400\n"
1409          "out vec4 sk_FragColor;\n"
1410          "void main() {\n"
1411          "    sk_FragColor = vec4(float[4](1.0, 2.0, 3.0, 4.0)[0], "
1412                                  "float[4](1.0, 2.0, 3.0, 4.0)[1], "
1413                                  "float[4](1.0, 2.0, 3.0, 4.0)[2], "
1414                                  "float[4](1.0, 2.0, 3.0, 4.0)[3]);\n"
1415          "}\n");
1416 }
1417 
DEF_TEST(SkSLGeometry,r)1418 DEF_TEST(SkSLGeometry, r) {
1419     test(r,
1420          "layout(points) in;"
1421          "layout(invocations = 2) in;"
1422          "layout(line_strip, max_vertices = 2) out;"
1423          "void main() {"
1424          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1425          "EmitVertex();"
1426          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
1427          "EmitVertex();"
1428          "EndPrimitive();"
1429          "}",
1430          *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
1431          "#version 400\n"
1432          "layout (points) in ;\n"
1433          "layout (invocations = 2) in ;\n"
1434          "layout (line_strip, max_vertices = 2) out ;\n"
1435          "void main() {\n"
1436          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1437          "    EmitVertex();\n"
1438          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1439          "    EmitVertex();\n"
1440          "    EndPrimitive();\n"
1441          "}\n",
1442          SkSL::Program::kGeometry_Kind);
1443 }
1444 
DEF_TEST(SkSLSwitch,r)1445 DEF_TEST(SkSLSwitch, r) {
1446     // basic "does a switch even work" test
1447     test(r,
1448          "void main() {"
1449          "    float x;"
1450          "    switch (int(sqrt(1))) {"
1451          "        case 0:"
1452          "            x = 0.0;"
1453          "            break;"
1454          "        case 1:"
1455          "            x = 1.0;"
1456          "            break;"
1457          "        default:"
1458          "            x = 2.0;"
1459          "    }"
1460          "    sk_FragColor = half4(half(x));"
1461          "}",
1462          *SkSL::ShaderCapsFactory::Default(),
1463          "#version 400\n"
1464          "out vec4 sk_FragColor;\n"
1465          "void main() {\n"
1466          "    float x;\n"
1467          "    switch (int(sqrt(1.0))) {\n"
1468          "        case 0:\n"
1469          "            x = 0.0;\n"
1470          "            break;\n"
1471          "        case 1:\n"
1472          "            x = 1.0;\n"
1473          "            break;\n"
1474          "        default:\n"
1475          "            x = 2.0;\n"
1476          "    }\n"
1477          "    sk_FragColor = vec4(x);\n"
1478          "}\n");
1479     // dead code inside of switch
1480     test(r,
1481          "void main() {"
1482          "    float x;"
1483          "    switch (int(sqrt(2))) {"
1484          "        case 0:"
1485          "            x = 0.0;"
1486          "        case 1:"
1487          "            x = 1.0;"
1488          "        default:"
1489          "            x = 2.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          "    switch (int(sqrt(2.0))) {\n"
1498          "        case 0:\n"
1499          "            ;\n"
1500          "        case 1:\n"
1501          "            ;\n"
1502          "        default:\n"
1503          "            ;\n"
1504          "    }\n"
1505          "    sk_FragColor = vec4(2.0);\n"
1506          "}\n");
1507     // non-static test w/ fallthrough
1508     test(r,
1509          "void main() {"
1510          "    float x = 0.0;"
1511          "    switch (int(sqrt(3))) {"
1512          "        case 0:"
1513          "            x = 0.0;"
1514          "        case 1:"
1515          "            x = 1.0;"
1516          "    }"
1517          "    sk_FragColor = half4(half(x));"
1518          "}",
1519          *SkSL::ShaderCapsFactory::Default(),
1520          "#version 400\n"
1521          "out vec4 sk_FragColor;\n"
1522          "void main() {\n"
1523          "    float x = 0.0;\n"
1524          "    switch (int(sqrt(3.0))) {\n"
1525          "        case 0:\n"
1526          "            x = 0.0;\n"
1527          "        case 1:\n"
1528          "            x = 1.0;\n"
1529          "    }\n"
1530          "    sk_FragColor = vec4(x);\n"
1531          "}\n");
1532     // static test w/ fallthrough
1533     test(r,
1534          "void main() {"
1535          "    float x = 0.0;"
1536          "    switch (0) {"
1537          "        case 0:"
1538          "            x = 0.0;"
1539          "        case 1:"
1540          "            x = 1.0;"
1541          "    }"
1542          "    sk_FragColor = half4(half(x));"
1543          "}",
1544          *SkSL::ShaderCapsFactory::Default(),
1545          "#version 400\n"
1546          "out vec4 sk_FragColor;\n"
1547          "void main() {\n"
1548          "    sk_FragColor = vec4(1.0);\n"
1549          "}\n");
1550     // static test w/ fallthrough, different entry point
1551     test(r,
1552          "void main() {"
1553          "    float x = 0.0;"
1554          "    switch (1) {"
1555          "        case 0:"
1556          "            x = 0.0;"
1557          "        case 1:"
1558          "            x = 1.0;"
1559          "    }"
1560          "    sk_FragColor = half4(half(x));"
1561          "}",
1562          *SkSL::ShaderCapsFactory::Default(),
1563          "#version 400\n"
1564          "out vec4 sk_FragColor;\n"
1565          "void main() {\n"
1566          "    sk_FragColor = vec4(1.0);\n"
1567          "}\n");
1568     // static test w/ break
1569     test(r,
1570          "void main() {"
1571          "    float x = 0.0;"
1572          "    switch (0) {"
1573          "        case 0:"
1574          "            x = 0.0;"
1575          "            break;"
1576          "        case 1:"
1577          "            x = 1.0;"
1578          "    }"
1579          "    sk_FragColor = half4(half(x));"
1580          "}",
1581          *SkSL::ShaderCapsFactory::Default(),
1582          "#version 400\n"
1583          "out vec4 sk_FragColor;\n"
1584          "void main() {\n"
1585          "    sk_FragColor = vec4(0.0);\n"
1586          "}\n");
1587     // static test w/ static conditional break
1588     test(r,
1589          "void main() {"
1590          "    float x = 0.0;"
1591          "    switch (0) {"
1592          "        case 0:"
1593          "            x = 0.0;"
1594          "            if (x < 1) break;"
1595          "        case 1:"
1596          "            x = 1.0;"
1597          "    }"
1598          "    sk_FragColor = half4(half(x));"
1599          "}",
1600          *SkSL::ShaderCapsFactory::Default(),
1601          "#version 400\n"
1602          "out vec4 sk_FragColor;\n"
1603          "void main() {\n"
1604          "    sk_FragColor = vec4(0.0);\n"
1605          "}\n");
1606     // static test w/ non-static conditional break
1607     test(r,
1608          "void main() {"
1609          "    float x = 0.0;"
1610          "    switch (0) {"
1611          "        case 0:"
1612          "            x = 0.0;"
1613          "            if (x < sqrt(1)) break;"
1614          "        case 1:"
1615          "            x = 1.0;"
1616          "    }"
1617          "    sk_FragColor = half4(half(x));"
1618          "}",
1619          *SkSL::ShaderCapsFactory::Default(),
1620          "#version 400\n"
1621          "out vec4 sk_FragColor;\n"
1622          "void main() {\n"
1623          "    float x = 0.0;\n"
1624          "    switch (0) {\n"
1625          "        case 0:\n"
1626          "            x = 0.0;\n"
1627          "            if (0.0 < sqrt(1.0)) break;\n"
1628          "        case 1:\n"
1629          "            x = 1.0;\n"
1630          "    }\n"
1631          "    sk_FragColor = vec4(x);\n"
1632          "}\n");
1633 }
1634 
DEF_TEST(SkSLRectangleTexture,r)1635 DEF_TEST(SkSLRectangleTexture, r) {
1636     test(r,
1637          "uniform sampler2D test;"
1638          "void main() {"
1639          "    sk_FragColor = sample(test, float2(0.5));"
1640          "}",
1641          *SkSL::ShaderCapsFactory::Default(),
1642          "#version 400\n"
1643          "out vec4 sk_FragColor;\n"
1644          "uniform sampler2D test;\n"
1645          "void main() {\n"
1646          "    sk_FragColor = texture(test, vec2(0.5));\n"
1647          "}\n");
1648     test(r,
1649          "uniform sampler2DRect test;"
1650          "void main() {"
1651          "    sk_FragColor = sample(test, float2(0.5));"
1652          "}",
1653          *SkSL::ShaderCapsFactory::Default(),
1654          "#version 400\n"
1655          "out vec4 sk_FragColor;\n"
1656          "uniform sampler2DRect test;\n"
1657          "void main() {\n"
1658          "    sk_FragColor = texture(test, vec2(0.5));\n"
1659          "}\n");
1660     test(r,
1661          "uniform sampler2DRect test;"
1662          "void main() {"
1663          "    sk_FragColor = sample(test, float3(0.5));"
1664          "}",
1665          *SkSL::ShaderCapsFactory::Default(),
1666          "#version 400\n"
1667          "out vec4 sk_FragColor;\n"
1668          "uniform sampler2DRect test;\n"
1669          "void main() {\n"
1670          "    sk_FragColor = texture(test, vec3(0.5));\n"
1671          "}\n");
1672 }
1673 
DEF_TEST(SkSLUnusedVars,r)1674 DEF_TEST(SkSLUnusedVars, r) {
1675     test(r,
1676          "void main() {"
1677          "float a = 1, b = 2, c = 3;"
1678          "float d = c;"
1679          "float e = d;"
1680          "b++;"
1681          "d++;"
1682          "sk_FragColor = half4(half(b), half(b), half(d), half(d));"
1683          "}",
1684         *SkSL::ShaderCapsFactory::Default(),
1685          "#version 400\n"
1686          "out vec4 sk_FragColor;\n"
1687          "void main() {\n"
1688          "    float b = 2.0;\n"
1689          "    float d = 3.0;\n"
1690          "    b++;\n"
1691          "    d++;\n"
1692          "    sk_FragColor = vec4(b, b, d, d);\n"
1693          "}\n");
1694 }
1695 
DEF_TEST(SkSLMultipleAssignments,r)1696 DEF_TEST(SkSLMultipleAssignments, r) {
1697     test(r,
1698          "void main() {"
1699          "float x;"
1700          "float y;"
1701          "int z;"
1702          "x = y = z = 1;"
1703          "sk_FragColor = half4(z);"
1704          "}",
1705          *SkSL::ShaderCapsFactory::Default(),
1706          "#version 400\n"
1707          "out vec4 sk_FragColor;\n"
1708          "void main() {\n"
1709          "    sk_FragColor = vec4(1.0);\n"
1710          "}\n");
1711 }
1712 
DEF_TEST(SkSLComplexDelete,r)1713 DEF_TEST(SkSLComplexDelete, r) {
1714     test(r,
1715          "uniform float4x4 colorXform;"
1716          "uniform sampler2D sampler;"
1717          "void main() {"
1718          "float4 tmpColor;"
1719          "sk_FragColor = half4(1.0) * (tmpColor = sample(sampler, float2(1)) , "
1720          "half4(colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * "
1721                                                      "float4(tmpColor.xyz, 1.0)).xyz, "
1722                                                      "0.0, tmpColor.w), tmpColor.w) : tmpColor));"
1723          "}",
1724          *SkSL::ShaderCapsFactory::Default(),
1725          "#version 400\n"
1726          "out vec4 sk_FragColor;\n"
1727          "uniform mat4 colorXform;\n"
1728          "uniform sampler2D sampler;\n"
1729          "void main() {\n"
1730          "    vec4 tmpColor;\n"
1731          "    sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1732          "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1733          "tmpColor);\n"
1734          "}\n");
1735 }
1736 
DEF_TEST(SkSLDependentInitializers,r)1737 DEF_TEST(SkSLDependentInitializers, r) {
1738     test(r,
1739          "void main() {"
1740          "float x = 0.5, y = x * 2;"
1741          "sk_FragColor = half4(half(y));"
1742          "}",
1743          *SkSL::ShaderCapsFactory::Default(),
1744          "#version 400\n"
1745          "out vec4 sk_FragColor;\n"
1746          "void main() {\n"
1747          "    sk_FragColor = vec4(1.0);\n"
1748          "}\n");
1749 }
1750 
DEF_TEST(SkSLDeadLoopVar,r)1751 DEF_TEST(SkSLDeadLoopVar, r) {
1752     test(r,
1753          "void main() {"
1754          "for (int x = 0; x < 4; ) {"
1755          "break;"
1756          "}"
1757          "}",
1758          *SkSL::ShaderCapsFactory::Default(),
1759          "#version 400\n"
1760          "void main() {\n"
1761          "    for (; true; ) {\n"
1762          "        break;\n"
1763          "    }\n"
1764          "}\n"
1765          );
1766 }
1767 
DEF_TEST(SkSLGeometryShaders,r)1768 DEF_TEST(SkSLGeometryShaders, r) {
1769     test(r,
1770          "layout(points) in;"
1771          "layout(invocations = 2) in;"
1772          "layout(line_strip, max_vertices = 2) out;"
1773          "void test() {"
1774          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
1775          "EmitVertex();"
1776          "}"
1777          "void main() {"
1778          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1779          "EmitVertex();"
1780          "}",
1781          *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(),
1782          "#version 400\n"
1783          "int sk_InvocationID;\n"
1784          "layout (points) in ;\n"
1785          "layout (line_strip, max_vertices = 4) out ;\n"
1786          "void test() {\n"
1787          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1788          "    EmitVertex();\n"
1789          "}\n"
1790          "void _invoke() {\n"
1791          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1792          "    EmitVertex();\n"
1793          "}\n"
1794          "void main() {\n"
1795          "    for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1796          "        _invoke();\n"
1797          "        EndPrimitive();\n"
1798          "    }\n"
1799          "}\n",
1800          SkSL::Program::kGeometry_Kind);
1801     test(r,
1802          "layout(points, invocations = 2) in;"
1803          "layout(invocations = 3) in;"
1804          "layout(line_strip, max_vertices = 2) out;"
1805          "void main() {"
1806          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1807          "EmitVertex();"
1808          "EndPrimitive();"
1809          "}",
1810          *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(),
1811          "#version 400\n"
1812          "#extension GL_ARB_gpu_shader5 : require\n"
1813          "layout (points, invocations = 2) in ;\n"
1814          "layout (invocations = 3) in ;\n"
1815          "layout (line_strip, max_vertices = 2) out ;\n"
1816          "void main() {\n"
1817          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1818          "    EmitVertex();\n"
1819          "    EndPrimitive();\n"
1820          "}\n",
1821          SkSL::Program::kGeometry_Kind);
1822     test(r,
1823          "layout(points, invocations = 2) in;"
1824          "layout(invocations = 3) in;"
1825          "layout(line_strip, max_vertices = 2) out;"
1826          "void main() {"
1827          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1828          "EmitVertex();"
1829          "EndPrimitive();"
1830          "}",
1831          *SkSL::ShaderCapsFactory::GeometryShaderExtensionString(),
1832          "#version 310es\n"
1833          "#extension GL_EXT_geometry_shader : require\n"
1834          "layout (points, invocations = 2) in ;\n"
1835          "layout (invocations = 3) in ;\n"
1836          "layout (line_strip, max_vertices = 2) out ;\n"
1837          "void main() {\n"
1838          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1839          "    EmitVertex();\n"
1840          "    EndPrimitive();\n"
1841          "}\n",
1842          SkSL::Program::kGeometry_Kind);
1843 }
1844 
DEF_TEST(SkSLTypePrecision,r)1845 DEF_TEST(SkSLTypePrecision, r) {
1846     test(r,
1847          "float f = 1;"
1848          "half h = 2;"
1849          "double d = 3;"
1850          "float2 f2 = float2(1, 2);"
1851          "half3 h3 = half3(1, 2, 3);"
1852          "double4 d4 = double4(1, 2, 3, 4);"
1853          "float2x2 f22 = float2x2(1, 2, 3, 4);"
1854          "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1855          "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);"
1856          "void main() {"
1857          "sk_FragColor.r = half(f + h + d + f2.x + h3.x + d4.x + f22[0][0] + h24[0][0] + "
1858                                "d42[0][0]);"
1859          "}",
1860          *SkSL::ShaderCapsFactory::Default(),
1861          "#version 400\n"
1862          "out vec4 sk_FragColor;\n"
1863          "float f = 1.0;\n"
1864          "float h = 2.0;\n"
1865          "double d = 3.0;\n"
1866          "vec2 f2 = vec2(1.0, 2.0);\n"
1867          "vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1868          "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n"
1869          "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1870          "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1871          "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1872          "void main() {\n"
1873          "    sk_FragColor.x = float(((((((double(f + h) + d) + double(f2.x)) + double(h3.x)) + "
1874          "d4.x) + double(f22[0][0])) + double(h24[0][0])) + d42[0][0]);\n"
1875          "}\n");
1876     test(r,
1877          "float f = 1;"
1878          "half h = 2;"
1879          "float2 f2 = float2(1, 2);"
1880          "half3 h3 = half3(1, 2, 3);"
1881          "float2x2 f22 = float2x2(1, 2, 3, 4);"
1882          "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1883          "void main() {"
1884          "sk_FragColor.r = half(f + h + f2.x + h3.x + f22[0][0] + h24[0][0]);"
1885          "}",
1886          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1887          "#version 400\n"
1888          "precision mediump float;\n"
1889          "precision mediump sampler2D;\n"
1890          "out mediump vec4 sk_FragColor;\n"
1891          "highp float f = 1.0;\n"
1892          "mediump float h = 2.0;\n"
1893          "highp vec2 f2 = vec2(1.0, 2.0);\n"
1894          "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1895          "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1896          "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1897          "void main() {\n"
1898          "    sk_FragColor.x = ((((f + h) + f2.x) + h3.x) + f22[0][0]) + h24[0][0];\n"
1899          "}\n");
1900 }
1901 
DEF_TEST(SkSLNumberConversions,r)1902 DEF_TEST(SkSLNumberConversions, r) {
1903     test(r,
1904          "short s = short(sqrt(1));"
1905          "int i = int(sqrt(1));"
1906          "ushort us = ushort(sqrt(1));"
1907          "uint ui = uint(sqrt(1));"
1908          "half h = half(sqrt(1));"
1909          "float f = sqrt(1);"
1910          "short s2s = s;"
1911          "short i2s = short(i);"
1912          "short us2s = short(us);"
1913          "short ui2s = short(ui);"
1914          "short h2s = short(h);"
1915          "short f2s = short(f);"
1916          "int s2i = s;"
1917          "int i2i = i;"
1918          "int us2i = int(us);"
1919          "int ui2i = int(ui);"
1920          "int h2i = int(h);"
1921          "int f2i = int(f);"
1922          "ushort s2us = ushort(s);"
1923          "ushort i2us = ushort(i);"
1924          "ushort us2us = us;"
1925          "ushort ui2us = ushort(ui);"
1926          "ushort h2us = ushort(h);"
1927          "ushort f2us = ushort(f);"
1928          "uint s2ui = uint(s);"
1929          "uint i2ui = uint(i);"
1930          "uint us2ui = us;"
1931          "uint ui2ui = ui;"
1932          "uint h2ui = uint(h);"
1933          "uint f2ui = uint(f);"
1934          "float s2f = s;"
1935          "float i2f = i;"
1936          "float us2f = us;"
1937          "float ui2f = ui;"
1938          "float h2f = h;"
1939          "float f2f = f;"
1940          "void main() {"
1941          "sk_FragColor.r = half(s + i + us + half(ui) + h + f + s2s + i2s + us2s + ui2s + h2s + "
1942                                "f2s + s2i + i2i + us2i + ui2i + h2i + f2i + s2us + i2us + us2us);"
1943          "sk_FragColor.r += half(ui2us + h2us + f2us + half(s2ui) + half(i2ui) + half(us2ui) + "
1944                                 "half(ui2ui) + half(h2ui) + half(f2ui) + s2f + i2f + us2f + ui2f + "
1945                                 "h2f + f2f);"
1946          "}",
1947          *SkSL::ShaderCapsFactory::Default(),
1948          "#version 400\n"
1949          "out vec4 sk_FragColor;\n"
1950          "int s = int(sqrt(1.0));\n"
1951          "int i = int(sqrt(1.0));\n"
1952          "uint us = uint(sqrt(1.0));\n"
1953          "uint ui = uint(sqrt(1.0));\n"
1954          "float h = sqrt(1.0);\n"
1955          "float f = sqrt(1.0);\n"
1956          "int s2s = s;\n"
1957          "int i2s = i;\n"
1958          "int us2s = int(us);\n"
1959          "int ui2s = int(ui);\n"
1960          "int h2s = int(h);\n"
1961          "int f2s = int(f);\n"
1962          "int s2i = s;\n"
1963          "int i2i = i;\n"
1964          "int us2i = int(us);\n"
1965          "int ui2i = int(ui);\n"
1966          "int h2i = int(h);\n"
1967          "int f2i = int(f);\n"
1968          "uint s2us = uint(s);\n"
1969          "uint i2us = uint(i);\n"
1970          "uint us2us = us;\n"
1971          "uint ui2us = ui;\n"
1972          "uint h2us = uint(h);\n"
1973          "uint f2us = uint(f);\n"
1974          "uint s2ui = uint(s);\n"
1975          "uint i2ui = uint(i);\n"
1976          "uint us2ui = us;\n"
1977          "uint ui2ui = ui;\n"
1978          "uint h2ui = uint(h);\n"
1979          "uint f2ui = uint(f);\n"
1980          "float s2f = float(s);\n"
1981          "float i2f = float(i);\n"
1982          "float us2f = float(us);\n"
1983          "float ui2f = float(ui);\n"
1984          "float h2f = h;\n"
1985          "float f2f = f;\n"
1986          "void main() {\n"
1987          "    sk_FragColor.x = (((((((((((((((((float((s + i) + int(us)) + float(ui)) + h) + f) + "
1988          "float(s2s)) + float(i2s)) + float(us2s)) + float(ui2s)) + float(h2s)) + float(f2s)) + "
1989          "float(s2i)) + float(i2i)) + float(us2i)) + float(ui2i)) + float(h2i)) + float(f2i)) + "
1990          "float(s2us)) + float(i2us)) + float(us2us);\n"
1991          "    sk_FragColor.x += (((((((((((float((ui2us + h2us) + f2us) + float(s2ui)) + "
1992          "float(i2ui)) + float(us2ui)) + float(ui2ui)) + float(h2ui)) + float(f2ui)) + s2f) + "
1993          "i2f) + us2f) + ui2f) + h2f) + f2f;\n"
1994          "}\n");
1995 }
1996 
DEF_TEST(SkSLForceHighPrecision,r)1997 DEF_TEST(SkSLForceHighPrecision, r) {
1998     test(r,
1999          "void main() {\n half x = half(sqrt(1));\n half4 y = half4(x);\n sk_FragColor = y;\n }",
2000          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
2001          "#version 400\n"
2002          "precision mediump float;\n"
2003          "precision mediump sampler2D;\n"
2004          "out mediump vec4 sk_FragColor;\n"
2005          "void main() {\n"
2006          "    mediump float x = sqrt(1.0);\n"
2007          "    mediump vec4 y = vec4(x);\n"
2008          "    sk_FragColor = y;\n"
2009          "}\n");
2010     SkSL::Program::Settings settings;
2011     settings.fForceHighPrecision = true;
2012     sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers();
2013     settings.fCaps = caps.get();
2014     SkSL::Program::Inputs inputs;
2015     test(r,
2016          "void main() { half x = half(sqrt(1)); half4 y = half4(x); sk_FragColor = y; }",
2017          settings,
2018          "#version 400\n"
2019          "precision mediump float;\n"
2020          "precision mediump sampler2D;\n"
2021          "out mediump vec4 sk_FragColor;\n"
2022          "void main() {\n"
2023          "    highp float x = sqrt(1.0);\n"
2024          "    highp vec4 y = vec4(x);\n"
2025          "    sk_FragColor = y;\n"
2026          "}\n",
2027          &inputs);
2028 }
2029 
DEF_TEST(SkSLNormalization,r)2030 DEF_TEST(SkSLNormalization, r) {
2031     test(r,
2032          "uniform float4 sk_RTAdjust; void main() { sk_Position = half4(1); }",
2033          *SkSL::ShaderCapsFactory::Default(),
2034          "#version 400\n"
2035          "uniform vec4 sk_RTAdjust;\n"
2036          "void main() {\n"
2037          "    gl_Position = vec4(1.0);\n"
2038          "    gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2039                                 "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
2040          "}\n",
2041          SkSL::Program::kVertex_Kind);
2042     test(r,
2043          "uniform float4 sk_RTAdjust;"
2044          "layout(points) in;"
2045          "layout(invocations = 2) in;"
2046          "layout(line_strip, max_vertices = 2) out;"
2047          "void main() {"
2048          "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
2049          "EmitVertex();"
2050          "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
2051          "EmitVertex();"
2052          "EndPrimitive();"
2053          "}",
2054          *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
2055          "#version 400\n"
2056          "uniform vec4 sk_RTAdjust;\n"
2057          "layout (points) in ;\n"
2058          "layout (invocations = 2) in ;\n"
2059          "layout (line_strip, max_vertices = 2) out ;\n"
2060          "void main() {\n"
2061          "    gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
2062          "    {\n"
2063          "        gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2064                                     "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
2065          "        EmitVertex();\n"
2066          "    }\n"
2067          "    gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
2068          "    {\n"
2069          "        gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2070                                     "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
2071          "        EmitVertex();\n"
2072          "    }\n"
2073          "    EndPrimitive();\n"
2074          "}\n",
2075          SkSL::Program::kGeometry_Kind);
2076 }
2077 
DEF_TEST(SkSLTernaryLValue,r)2078 DEF_TEST(SkSLTernaryLValue, r) {
2079     test(r,
2080          "void main() { int r, g; (true ? r : g) = 1; (false ? r : g) = 0; "
2081          "sk_FragColor = half4(r, g, 1, 1); }",
2082          *SkSL::ShaderCapsFactory::Default(),
2083          "#version 400\n"
2084          "out vec4 sk_FragColor;\n"
2085          "void main() {\n"
2086          "    sk_FragColor = vec4(1.0, 0.0, 1.0, 1.0);\n"
2087          "}\n");
2088     test(r,
2089          "void main() { half r, g; (true ? r : g) = half(sqrt(1)); (false ? r : g) = half(sqrt(0));"
2090          "sk_FragColor = half4(r, g, 1, 1); }",
2091          *SkSL::ShaderCapsFactory::Default(),
2092          "#version 400\n"
2093          "out vec4 sk_FragColor;\n"
2094          "void main() {\n"
2095          "    float r, g;\n"
2096          "    r = sqrt(1.0);\n"
2097          "    g = sqrt(0.0);\n"
2098          "    sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
2099          "}\n");
2100     test(r,
2101          "void main() {"
2102          "half r, g;"
2103          "(sqrt(1) > 0 ? r : g) = half(sqrt(1));"
2104          "(sqrt(0) > 0 ? r : g) = half(sqrt(0));"
2105          "sk_FragColor = half4(r, g, 1, 1);"
2106          "}",
2107          *SkSL::ShaderCapsFactory::Default(),
2108          "#version 400\n"
2109          "out vec4 sk_FragColor;\n"
2110          "void main() {\n"
2111          "    float r, g;\n"
2112          "    sqrt(1.0) > 0.0 ? r : g = sqrt(1.0);\n"
2113          "    sqrt(0.0) > 0.0 ? r : g = sqrt(0.0);\n"
2114          "    sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
2115          "}\n");
2116 }
2117 
DEF_TEST(SkSLIncompleteShortIntPrecision,r)2118 DEF_TEST(SkSLIncompleteShortIntPrecision, r) {
2119     test(r,
2120          "uniform sampler2D tex;"
2121          "in float2 texcoord;"
2122          "in short2 offset;"
2123          "void main() {"
2124          "    short scalar = offset.y;"
2125          "    sk_FragColor = sample(tex, texcoord + float2(offset * scalar));"
2126          "}",
2127          *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
2128          "#version 400\n"
2129          "precision mediump float;\n"
2130          "precision mediump sampler2D;\n"
2131          "out mediump vec4 sk_FragColor;\n"
2132          "uniform sampler2D tex;\n"
2133          "in highp vec2 texcoord;\n"
2134          "in mediump ivec2 offset;\n"
2135          "void main() {\n"
2136          "    mediump int scalar = offset.y;\n"
2137          "    sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2138          "}\n",
2139          SkSL::Program::kFragment_Kind);
2140     test(r,
2141          "uniform sampler2D tex;"
2142          "in float2 texcoord;"
2143          "in short2 offset;"
2144          "void main() {"
2145          "    short scalar = offset.y;"
2146          "    sk_FragColor = sample(tex, texcoord + float2(offset * scalar));"
2147          "}",
2148          *SkSL::ShaderCapsFactory::IncompleteShortIntPrecision(),
2149          "#version 310es\n"
2150          "precision mediump float;\n"
2151          "precision mediump sampler2D;\n"
2152          "out mediump vec4 sk_FragColor;\n"
2153          "uniform sampler2D tex;\n"
2154          "in highp vec2 texcoord;\n"
2155          "in highp ivec2 offset;\n"
2156          "void main() {\n"
2157          "    highp int scalar = offset.y;\n"
2158          "    sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2159          "}\n",
2160          SkSL::Program::kFragment_Kind);
2161 }
2162 
DEF_TEST(SkSLFrExp,r)2163 DEF_TEST(SkSLFrExp, r) {
2164     test(r,
2165          "void main() {"
2166          "    int exp;"
2167          "    float foo = frexp(0.5, exp);"
2168          "    sk_FragColor = half4(exp);"
2169          "}",
2170          *SkSL::ShaderCapsFactory::Default(),
2171          "#version 400\n"
2172          "out vec4 sk_FragColor;\n"
2173          "void main() {\n"
2174          "    int exp;\n"
2175          "    float foo = frexp(0.5, exp);\n"
2176          "    sk_FragColor = vec4(float(exp));\n"
2177          "}\n");
2178 }
2179 
DEF_TEST(SkSLWorkaroundAddAndTrueToLoopCondition,r)2180 DEF_TEST(SkSLWorkaroundAddAndTrueToLoopCondition, r) {
2181     test(r,
2182          "void main() {"
2183          "    int c = 0;"
2184          "    for (int i = 0; i < 4 || c < 10; ++i) {"
2185          "        c += 1;"
2186          "    }"
2187          "}",
2188          *SkSL::ShaderCapsFactory::AddAndTrueToLoopCondition(),
2189          "#version 400\n"
2190          "void main() {\n"
2191          "    int c = 0;\n"
2192          "    for (int i = 0;(i < 4 || c < 10) && true; ++i) {\n"
2193          "        c += 1;\n"
2194          "    }\n"
2195          "}\n",
2196          SkSL::Program::kFragment_Kind
2197          );
2198 }
2199 
DEF_TEST(SkSLWorkaroundUnfoldShortCircuitAsTernary,r)2200 DEF_TEST(SkSLWorkaroundUnfoldShortCircuitAsTernary, r) {
2201     test(r,
2202          "uniform bool x;"
2203          "uniform bool y;"
2204          "uniform int i;"
2205          "uniform int j;"
2206          "void main() {"
2207          "    bool andXY = x && y;"
2208          "    bool orXY = x || y;"
2209          "    bool combo = (x && y) || (x || y);"
2210          "    bool prec = (i + j == 3) && y;"
2211          "    while (andXY && orXY && combo && prec) {"
2212          "        sk_FragColor = half4(0);"
2213          "        break;"
2214          "    }"
2215          "}",
2216          *SkSL::ShaderCapsFactory::UnfoldShortCircuitAsTernary(),
2217          "#version 400\n"
2218          "out vec4 sk_FragColor;\n"
2219          "uniform bool x;\n"
2220          "uniform bool y;\n"
2221          "uniform int i;\n"
2222          "uniform int j;\n"
2223          "void main() {\n"
2224          "    bool andXY = x ? y : false;\n"
2225          "    bool orXY = x ? true : y;\n"
2226          "    bool combo = (x ? y : false) ? true : (x ? true : y);\n"
2227          "    bool prec = i + j == 3 ? y : false;\n"
2228          "    while (((andXY ? orXY : false) ? combo : false) ? prec : false) {\n"
2229          "        sk_FragColor = vec4(0.0);\n"
2230          "        break;\n"
2231          "    }\n"
2232          "}\n",
2233          SkSL::Program::kFragment_Kind
2234          );
2235 }
2236 
DEF_TEST(SkSLWorkaroundEmulateAbsIntFunction,r)2237 DEF_TEST(SkSLWorkaroundEmulateAbsIntFunction, r) {
2238     test(r,
2239          "uniform int i;"
2240          "uniform float f;"
2241          "void main() {"
2242          "    float output = abs(f) + abs(i);"
2243          "    sk_FragColor = half4(half(output));"
2244          "}",
2245          *SkSL::ShaderCapsFactory::EmulateAbsIntFunction(),
2246          "#version 400\n"
2247          "int _absemulation(int x) {\n"
2248          "    return x * sign(x);\n"
2249          "}\n"
2250          "out vec4 sk_FragColor;\n"
2251          "uniform int i;\n"
2252          "uniform float f;\n"
2253          "void main() {\n"
2254          "    float output = abs(f) + float(_absemulation(i));\n"
2255          "    sk_FragColor = vec4(output);\n"
2256          "}\n",
2257          SkSL::Program::kFragment_Kind
2258          );
2259 }
2260 
DEF_TEST(SkSLWorkaroundRewriteDoWhileLoops,r)2261 DEF_TEST(SkSLWorkaroundRewriteDoWhileLoops, r) {
2262     test(r,
2263          "void main() {"
2264          "    int i = 0;"
2265          "    do {"
2266          "      ++i;"
2267          "      do {"
2268          "        i++;"
2269          "      } while (true);"
2270          "    } while (i < 10);"
2271          "    sk_FragColor = half4(i);"
2272          "}",
2273          *SkSL::ShaderCapsFactory::RewriteDoWhileLoops(),
2274          "#version 400\n"
2275          "out vec4 sk_FragColor;\n"
2276          "void main() {\n"
2277          "    int i = 0;\n"
2278          "    bool _tmpLoopSeenOnce0 = false;\n"
2279          "    while (true) {\n"
2280          "        if (_tmpLoopSeenOnce0) {\n"
2281          "            if (!(i < 10)) {\n"
2282          "                break;\n"
2283          "            }\n"
2284          "        }\n"
2285          "        _tmpLoopSeenOnce0 = true;\n"
2286          "        {\n"
2287          "            ++i;\n"
2288          "            bool _tmpLoopSeenOnce1 = false;\n"
2289          "            while (true) {\n"
2290          "                if (_tmpLoopSeenOnce1) {\n"
2291          "                    if (!true) {\n"
2292          "                        break;\n"
2293          "                    }\n"
2294          "                }\n"
2295          "                _tmpLoopSeenOnce1 = true;\n"
2296          "                {\n"
2297          "                    i++;\n"
2298          "                }\n"
2299          "            }\n"
2300          "        }\n"
2301          "    }\n"
2302          "    sk_FragColor = vec4(float(i));\n"
2303          "}\n",
2304          SkSL::Program::kFragment_Kind
2305          );
2306 }
2307 
DEF_TEST(SkSLWorkaroundRemovePowWithConstantExponent,r)2308 DEF_TEST(SkSLWorkaroundRemovePowWithConstantExponent, r) {
2309     test(r,
2310          "uniform float x;"
2311          "uniform float y;"
2312          "void main() {"
2313          "    float z = pow(x + 1.0, y + 2.0);"
2314          "    sk_FragColor = half4(half(z));"
2315          "}",
2316          *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2317          "#version 400\n"
2318          "out vec4 sk_FragColor;\n"
2319          "uniform float x;\n"
2320          "uniform float y;\n"
2321          "void main() {\n"
2322          "    float z = exp2((y + 2.0) * log2(x + 1.0));\n"
2323          "    sk_FragColor = vec4(z);\n"
2324          "}\n",
2325          SkSL::Program::kFragment_Kind
2326          );
2327 }
2328 
DEF_TEST(SkSLSwizzleLTRB,r)2329 DEF_TEST(SkSLSwizzleLTRB, r) {
2330     test(r,
2331          "void main() {"
2332          "    sk_FragColor = sk_FragColor.BRTL;"
2333          "}",
2334          *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2335          "#version 400\n"
2336          "out vec4 sk_FragColor;\n"
2337          "void main() {\n"
2338          "    sk_FragColor = sk_FragColor.wzyx;\n"
2339          "}\n",
2340          SkSL::Program::kFragment_Kind
2341          );
2342 }
2343 
DEF_TEST(SkSLSwizzleConstants,r)2344 DEF_TEST(SkSLSwizzleConstants, r) {
2345     test(r,
2346          "void main() {"
2347          "    half4 v = half4(half(sqrt(1)));"
2348          "    sk_FragColor = half4(v.x, 1, 1, 1);"
2349          "    sk_FragColor = half4(v.1, 1, 1, 1);"
2350          "    sk_FragColor = half4(v.xy, 1, 1);"
2351          "    sk_FragColor = half4(v.x1, 1, 1);"
2352          "    sk_FragColor = half4(v.0y, 1, 1);"
2353          "    sk_FragColor = half4(v.10, 1, 1);"
2354          "    sk_FragColor = half4(v.xyz, 1);"
2355          "    sk_FragColor = half4(v.xy1, 1);"
2356          "    sk_FragColor = half4(v.x0z, 1);"
2357          "    sk_FragColor = half4(v.x10, 1);"
2358          "    sk_FragColor = half4(v.1yz, 1);"
2359          "    sk_FragColor = half4(v.0y1, 1);"
2360          "    sk_FragColor = half4(v.11z, 1);"
2361          "    sk_FragColor = half4(v.101, 1);"
2362          "    sk_FragColor = v.xyzw;"
2363          "    sk_FragColor = v.xyz1;"
2364          "    sk_FragColor = v.xy0w;"
2365          "    sk_FragColor = v.xy10;"
2366          "    sk_FragColor = v.x1zw;"
2367          "    sk_FragColor = v.x0z1;"
2368          "    sk_FragColor = v.x11w;"
2369          "    sk_FragColor = v.x101;"
2370          "    sk_FragColor = v.1yzw;"
2371          "    sk_FragColor = v.0yz1;"
2372          "    sk_FragColor = v.0y1w;"
2373          "    sk_FragColor = v.1y11;"
2374          "    sk_FragColor = v.00zw;"
2375          "    sk_FragColor = v.00z1;"
2376          "    sk_FragColor = v.011w;"
2377          "    sk_FragColor = v.1101;"
2378          "}",
2379          *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2380          "#version 400\n"
2381          "out vec4 sk_FragColor;\n"
2382          "void main() {\n"
2383          "    vec4 v = vec4(sqrt(1.0));\n"
2384          "    sk_FragColor = vec4(v.x, 1.0, 1.0, 1.0);\n"
2385          "    sk_FragColor = vec4(1, 1.0, 1.0, 1.0);\n"
2386          "    sk_FragColor = vec4(v.xy, 1.0, 1.0);\n"
2387          "    sk_FragColor = vec4(vec2(v.x, 1), 1.0, 1.0);\n"
2388          "    sk_FragColor = vec4(vec2(0, v.y), 1.0, 1.0);\n"
2389          "    sk_FragColor = vec4(vec2(1, 0), 1.0, 1.0);\n"
2390          "    sk_FragColor = vec4(v.xyz, 1.0);\n"
2391          "    sk_FragColor = vec4(vec3(v.xy, 1), 1.0);\n"
2392          "    sk_FragColor = vec4(vec3(v.xz, 0).xzy, 1.0);\n"
2393          "    sk_FragColor = vec4(vec3(v.x, 1, 0), 1.0);\n"
2394          "    sk_FragColor = vec4(vec3(1, v.yz), 1.0);\n"
2395          "    sk_FragColor = vec4(vec3(v.y, 0, 1).yxz, 1.0);\n"
2396          "    sk_FragColor = vec4(vec3(1, 1, v.z), 1.0);\n"
2397          "    sk_FragColor = vec4(vec3(1, 0, 1), 1.0);\n"
2398          "    sk_FragColor = v;\n"
2399          "    sk_FragColor = vec4(v.xyz, 1);\n"
2400          "    sk_FragColor = vec4(v.xyw, 0).xywz;\n"
2401          "    sk_FragColor = vec4(v.xy, 1, 0);\n"
2402          "    sk_FragColor = vec4(v.xzw, 1).xwyz;\n"
2403          "    sk_FragColor = vec4(v.xz, 0, 1).xzyw;\n"
2404          "    sk_FragColor = vec4(v.xw, 1, 1).xzwy;\n"
2405          "    sk_FragColor = vec4(v.x, 1, 0, 1);\n"
2406          "    sk_FragColor = vec4(1, v.yzw);\n"
2407          "    sk_FragColor = vec4(v.yz, 0, 1).zxyw;\n"
2408          "    sk_FragColor = vec4(v.yw, 0, 1).zxwy;\n"
2409          "    sk_FragColor = vec4(v.y, 1, 1, 1).yxzw;\n"
2410          "    sk_FragColor = vec4(0, 0, v.zw);\n"
2411          "    sk_FragColor = vec4(v.z, 0, 0, 1).yzxw;\n"
2412          "    sk_FragColor = vec4(0, 1, 1, v.w);\n"
2413          "    sk_FragColor = vec4(1, 1, 0, 1);\n"
2414          "}\n",
2415          SkSL::Program::kFragment_Kind
2416          );
2417 }
2418 
DEF_TEST(SkSLSwizzleOpt,r)2419 DEF_TEST(SkSLSwizzleOpt, r) {
2420     test(r,
2421          "void main() {"
2422          "    half v = half(sqrt(1));"
2423          "    sk_FragColor = half4(v).rgba;"
2424          "    sk_FragColor = half4(v).rgb0.abgr;"
2425          "    sk_FragColor = half4(v).rgba.00ra;"
2426          "    sk_FragColor = half4(v).rgba.rrra.00ra.11ab;"
2427          "    sk_FragColor = half4(v).abga.gb11;"
2428          "    sk_FragColor = half4(half3(v).rrr.000, 1);"
2429          "    sk_FragColor = half4(half3(v).000.rrr, 1);"
2430          "    sk_FragColor = half4(v).abgr.abgr;"
2431          "    sk_FragColor = half4(half4(v).rrrr.bb, 1, 1);"
2432          "    sk_FragColor = half4(half4(v).ba.grgr);"
2433          "}",
2434          *SkSL::ShaderCapsFactory::Default(),
2435          "#version 400\n"
2436          "out vec4 sk_FragColor;\n"
2437          "void main() {\n"
2438          "    float v = sqrt(1.0);\n"
2439          "    sk_FragColor = vec4(v);\n"
2440          "    sk_FragColor = vec4(0, vec4(v).zyx);\n"
2441          "    sk_FragColor = vec4(0, 0, vec4(v).xw);\n"
2442          "    sk_FragColor = vec4(1, 1, vec4(v).wx);\n"
2443          "    sk_FragColor = vec4(vec4(v).zy, 1, 1);\n"
2444          "    sk_FragColor = vec4(vec3(0, 0, 0), 1.0);\n"
2445          "    sk_FragColor = vec4(vec3(0, 0, 0), 1.0);\n"
2446          "    sk_FragColor = vec4(v);\n"
2447          "    sk_FragColor = vec4(vec4(v).xx, 1.0, 1.0);\n"
2448          "    sk_FragColor = vec4(v).wzwz;\n"
2449          "}\n",
2450          SkSL::Program::kFragment_Kind
2451          );
2452 }
2453 
DEF_TEST(SkSLNegatedVectorLiteral,r)2454 DEF_TEST(SkSLNegatedVectorLiteral, r) {
2455     test(r,
2456          "void main() {"
2457          "    if (half4(1) == half4(-half2(-1), half2(1)))"
2458          "        sk_FragColor = half4(0, 1, 0, 1);"
2459          "    else"
2460          "        sk_FragColor = half4(1, 0, 0, 1);"
2461          "}",
2462          *SkSL::ShaderCapsFactory::Default(),
2463          "#version 400\n"
2464          "out vec4 sk_FragColor;\n"
2465          "void main() {\n"
2466          "    sk_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
2467          "}\n");
2468 }
2469