ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSLCompiler.h" |
| 9 | |
| 10 | #include "Test.h" |
| 11 | |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 12 | #if SK_SUPPORT_GPU |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 13 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 14 | // Note that the optimizer will aggressively kill dead code and substitute constants in place of |
| 15 | // variables, so we have to jump through a few hoops to ensure that the code in these tests has the |
| 16 | // necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being |
| 17 | // smart enough to optimize around certain constructs; as the optimizer gets smarter it will |
| 18 | // undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is |
| 19 | // equivalent! |
| 20 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 21 | static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings, |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 22 | const char* expected, SkSL::Program::Inputs* inputs, |
| 23 | SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 24 | SkSL::Compiler compiler; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 25 | SkSL::String output; |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 26 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkString(src), settings); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 27 | if (!program) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 28 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 29 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 30 | REPORTER_ASSERT(r, program); |
| 31 | *inputs = program->fInputs; |
| 32 | REPORTER_ASSERT(r, compiler.toGLSL(*program, &output)); |
| 33 | if (program) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 34 | SkSL::String skExpected(expected); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 35 | if (output != skExpected) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 36 | SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 37 | expected, output.c_str()); |
| 38 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 39 | REPORTER_ASSERT(r, output == skExpected); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 43 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 44 | const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 45 | SkSL::Program::Settings settings; |
| 46 | settings.fCaps = ∩︀ |
| 47 | SkSL::Program::Inputs inputs; |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 48 | test(r, src, settings, expected, &inputs, kind); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 49 | } |
| 50 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 51 | DEF_TEST(SkSLHelloWorld, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 52 | test(r, |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 53 | "void main() { sk_FragColor = vec4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 54 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 55 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 56 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 57 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 58 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 59 | "}\n"); |
| 60 | } |
| 61 | |
| 62 | DEF_TEST(SkSLControl, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 63 | test(r, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 64 | "void main() {" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 65 | "if (sqrt(2) > 5) { sk_FragColor = vec4(0.75); } else { discard; }" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 66 | "int i = 0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 67 | "while (i < 10) { sk_FragColor *= 0.5; i++; }" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 68 | "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 69 | "for (int i = 0; i < 10; i++) {" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 70 | "if (i % 2 == 1) break; else continue;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 71 | "}" |
| 72 | "return;" |
| 73 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 74 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 75 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 76 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 77 | "void main() {\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 78 | " if (sqrt(2.0) > 5.0) {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 79 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 80 | " } else {\n" |
| 81 | " discard;\n" |
| 82 | " }\n" |
| 83 | " int i = 0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 84 | " while (i < 10) {\n" |
| 85 | " sk_FragColor *= 0.5;\n" |
| 86 | " i++;\n" |
| 87 | " }\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 88 | " do {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 89 | " sk_FragColor += 0.01;\n" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 90 | " } while (sk_FragColor.x < 0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 91 | " for (int i = 0;i < 10; i++) {\n" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 92 | " if (i % 2 == 1) break; else continue;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 93 | " }\n" |
| 94 | " return;\n" |
| 95 | "}\n"); |
| 96 | } |
| 97 | |
| 98 | DEF_TEST(SkSLFunctions, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 99 | test(r, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 100 | "float foo(float v[2]) { return v[0] * v[1]; }" |
| 101 | "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 102 | "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 103 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 104 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 105 | "out vec4 sk_FragColor;\n" |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 106 | "float foo(float v[2]) {\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 107 | " return v[0] * v[1];\n" |
| 108 | "}\n" |
| 109 | "void bar(inout float x) {\n" |
| 110 | " float y[2], z;\n" |
| 111 | " y[0] = x;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 112 | " y[1] = x * 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 113 | " z = foo(y);\n" |
| 114 | " x = z;\n" |
| 115 | "}\n" |
| 116 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 117 | " float x = 10.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 118 | " bar(x);\n" |
| 119 | " sk_FragColor = vec4(x);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 120 | "}\n"); |
| 121 | } |
| 122 | |
| 123 | DEF_TEST(SkSLOperators, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 124 | test(r, |
| 125 | "void main() {" |
| 126 | "float x = 1, y = 2;" |
| 127 | "int z = 3;" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 128 | "x = x - x + y * z * x * (y - z);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 129 | "y = x / y / z;" |
| 130 | "z = (z / 2 % 3 << 4) >> 2 << 1;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 131 | "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 132 | "x += 12;" |
| 133 | "x -= 12;" |
| 134 | "x *= y /= z = 10;" |
| 135 | "b ||= false;" |
| 136 | "b &&= true;" |
| 137 | "b ^^= false;" |
| 138 | "z |= 0;" |
| 139 | "z &= -1;" |
| 140 | "z ^= 0;" |
| 141 | "z >>= 2;" |
| 142 | "z <<= 4;" |
| 143 | "z %= 5;" |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 144 | "x = (vec2(sqrt(1)) , 6);" |
| 145 | "z = (vec2(sqrt(1)) , 6);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 146 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 147 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 148 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 149 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 150 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 151 | " float x = 1.0, y = 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 152 | " int z = 3;\n" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 153 | " x = -6.0;\n" |
| 154 | " y = -1.0;\n" |
| 155 | " z = 8;\n" |
| 156 | " bool b = false == true || 2.0 >= sqrt(2.0) && true;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 157 | " x += 12.0;\n" |
| 158 | " x -= 12.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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" |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 169 | " x = float((vec2(sqrt(1.0)) , 6));\n" |
| 170 | " z = (vec2(sqrt(1.0)) , 6);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 171 | "}\n"); |
| 172 | } |
| 173 | |
| 174 | DEF_TEST(SkSLMatrices, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 175 | test(r, |
| 176 | "void main() {" |
| 177 | "mat2x4 x = mat2x4(1);" |
| 178 | "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" |
| 179 | "mat3x4 z = x * y;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 180 | "vec3 v1 = mat3(1) * vec3(2);" |
| 181 | "vec3 v2 = vec3(2) * mat3(1);" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 182 | "sk_FragColor = vec4(z[0].x, v1 + v2);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 183 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 184 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 185 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 186 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 187 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 188 | " mat3x4 z = mat2x4(1.0) * mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 189 | " vec3 v1 = mat3(1.0) * vec3(2.0);\n" |
| 190 | " vec3 v2 = vec3(2.0) * mat3(1.0);\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 191 | " sk_FragColor = vec4(z[0].x, v1 + v2);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 192 | "}\n"); |
| 193 | } |
| 194 | |
| 195 | DEF_TEST(SkSLInterfaceBlock, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 196 | test(r, |
| 197 | "uniform testBlock {" |
| 198 | "float x;" |
| 199 | "float y[2];" |
| 200 | "layout(binding=12) mat3x2 z;" |
| 201 | "bool w;" |
| 202 | "};" |
| 203 | "void main() {" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 204 | " sk_FragColor = vec4(x, y[0], y[1], 0);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 205 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 206 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 207 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 208 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 209 | "uniform testBlock {\n" |
| 210 | " float x;\n" |
| 211 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 212 | " layout (binding = 12) mat3x2 z;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 213 | " bool w;\n" |
| 214 | "};\n" |
| 215 | "void main() {\n" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 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 = vec4(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 = vec4(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" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 249 | "}\n"); |
| 250 | } |
| 251 | |
| 252 | DEF_TEST(SkSLStructs, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 267 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 268 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 269 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 270 | "struct A {\n" |
| 271 | " int x;\n" |
| 272 | " int y;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 273 | "} a1, a2;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 274 | "A a3;\n" |
| 275 | "struct B {\n" |
| 276 | " float x;\n" |
| 277 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 278 | " layout (binding = 1) A z;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 279 | "} b1, b2, b3;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 280 | "void main() {\n" |
| 281 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 282 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 283 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 284 | DEF_TEST(SkSLVersion, r) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 285 | test(r, |
| 286 | "in float test; void main() { sk_FragColor = vec4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 287 | *SkSL::ShaderCapsFactory::Version450Core(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 288 | "#version 450 core\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 289 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 290 | "in float test;\n" |
| 291 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 292 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 293 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 294 | test(r, |
| 295 | "in float test; void main() { sk_FragColor = vec4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 296 | *SkSL::ShaderCapsFactory::Version110(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 297 | "#version 110\n" |
| 298 | "varying float test;\n" |
| 299 | "void main() {\n" |
| 300 | " gl_FragColor = vec4(0.75);\n" |
| 301 | "}\n"); |
| 302 | } |
| 303 | |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 304 | DEF_TEST(SkSLUsesPrecisionModifiers, r) { |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 305 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 306 | "void main() { float x = 0.75; highp float y = 1; x++; y++;" |
| 307 | "sk_FragColor.rg = vec2(x, y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 308 | *SkSL::ShaderCapsFactory::Default(), |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 309 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 310 | "out vec4 sk_FragColor;\n" |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 311 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 312 | " float x = 0.75;\n" |
| 313 | " float y = 1.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 314 | " x++;\n" |
| 315 | " y++;\n" |
| 316 | " sk_FragColor.xy = vec2(x, y);\n" |
| 317 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 318 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 319 | "void main() { float x = 0.75; highp float y = 1; x++; y++;" |
| 320 | "sk_FragColor.rg = vec2(x, y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 321 | *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 322 | "#version 400\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 323 | "precision highp float;\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 324 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 325 | "void main() {\n" |
| 326 | " float x = 0.75;\n" |
| 327 | " highp float y = 1.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 328 | " x++;\n" |
| 329 | " y++;\n" |
| 330 | " sk_FragColor.xy = vec2(x, y);\n" |
| 331 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | DEF_TEST(SkSLMinAbs, r) { |
| 335 | test(r, |
| 336 | "void main() {" |
| 337 | "float x = -5;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 338 | "sk_FragColor.r = min(abs(x), 6);" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 339 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 340 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 341 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 342 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 343 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 344 | " sk_FragColor.x = min(abs(-5.0), 6.0);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 345 | "}\n"); |
| 346 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 347 | test(r, |
| 348 | "void main() {" |
| 349 | "float x = -5.0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 350 | "sk_FragColor.r = min(abs(x), 6.0);" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 351 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 352 | *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 353 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 354 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 355 | "void main() {\n" |
| 356 | " float minAbsHackVar0;\n" |
| 357 | " float minAbsHackVar1;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 358 | " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? " |
| 359 | "minAbsHackVar0 : minAbsHackVar1);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 360 | "}\n"); |
| 361 | } |
| 362 | |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 363 | DEF_TEST(SkSLNegatedAtan, r) { |
| 364 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 365 | "void main() { vec2 x = vec2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 366 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 367 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 368 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 369 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 370 | " vec2 x = vec2(sqrt(2.0));\n" |
| 371 | " sk_FragColor.x = atan(x.x, -x.y);\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 372 | "}\n"); |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 373 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 374 | "void main() { vec2 x = vec2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 375 | *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 376 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 377 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 378 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 379 | " vec2 x = vec2(sqrt(2.0));\n" |
| 380 | " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 381 | "}\n"); |
| 382 | } |
| 383 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 384 | DEF_TEST(SkSLModifiersDeclaration, r) { |
| 385 | test(r, |
| 386 | "layout(blend_support_all_equations) out;" |
| 387 | "void main() { }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 388 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 389 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 390 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 391 | "layout (blend_support_all_equations) out ;\n" |
| 392 | "void main() {\n" |
| 393 | "}\n"); |
| 394 | } |
| 395 | |
| 396 | DEF_TEST(SkSLHex, r) { |
| 397 | test(r, |
| 398 | "void main() {" |
| 399 | "int i1 = 0x0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 400 | "i1++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 401 | "int i2 = 0x1234abcd;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 402 | "i2++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 403 | "int i3 = 0x7fffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 404 | "i3++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 405 | "int i4 = 0xffffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 406 | "i4++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 407 | "int i5 = -0xbeef;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 408 | "i5++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 409 | "uint u1 = 0x0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 410 | "u1++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 411 | "uint u2 = 0x1234abcd;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 412 | "u2++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 413 | "uint u3 = 0x7fffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 414 | "u3++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 415 | "uint u4 = 0xffffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 416 | "u4++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 417 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 418 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 419 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 420 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 421 | "void main() {\n" |
| 422 | " int i1 = 0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 423 | " i1++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 424 | " int i2 = 305441741;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 425 | " i2++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 426 | " int i3 = 2147483647;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 427 | " i3++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 428 | " int i4 = -1;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 429 | " i4++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 430 | " int i5 = -48879;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 431 | " i5++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 432 | " uint u1 = 0u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 433 | " u1++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 434 | " uint u2 = 305441741u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 435 | " u2++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 436 | " uint u3 = 2147483647u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 437 | " u3++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 438 | " uint u4 = 4294967295u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 439 | " u4++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 440 | "}\n"); |
| 441 | } |
| 442 | |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 443 | DEF_TEST(SkSLVectorConstructors, r) { |
| 444 | test(r, |
| 445 | "vec2 v1 = vec2(1);" |
| 446 | "vec2 v2 = vec2(1, 2);" |
| 447 | "vec2 v3 = vec2(vec2(1));" |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 448 | "vec3 v4 = vec3(vec2(1), 1.0);" |
| 449 | "ivec2 v5 = ivec2(1);" |
| 450 | "ivec2 v6 = ivec2(vec2(1, 2));" |
| 451 | "vec2 v7 = vec2(ivec2(1, 2));", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 452 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 453 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 454 | "out vec4 sk_FragColor;\n" |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 455 | "vec2 v1 = vec2(1.0);\n" |
| 456 | "vec2 v2 = vec2(1.0, 2.0);\n" |
| 457 | "vec2 v3 = vec2(1.0);\n" |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 458 | "vec3 v4 = vec3(vec2(1.0), 1.0);\n" |
| 459 | "ivec2 v5 = ivec2(1);\n" |
| 460 | "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n" |
| 461 | "vec2 v7 = vec2(ivec2(1, 2));\n"); |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 462 | } |
| 463 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 464 | DEF_TEST(SkSLArrayConstructors, r) { |
| 465 | test(r, |
| 466 | "float test1[] = float[](1, 2, 3, 4);" |
| 467 | "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));" |
| 468 | "mat4 test3[] = mat4[]();", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 469 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 470 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 471 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 472 | "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" |
| 473 | "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" |
| 474 | "mat4 test3[] = mat4[]();\n"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 475 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 476 | |
| 477 | DEF_TEST(SkSLDerivatives, r) { |
| 478 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 479 | "void main() { sk_FragColor.r = dFdx(1); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 480 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 481 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 482 | "out vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 483 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 484 | " sk_FragColor.x = dFdx(1.0);\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 485 | "}\n"); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 486 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 487 | "void main() { sk_FragColor.r = 1; }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 488 | *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 489 | "#version 400\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 490 | "precision highp float;\n" |
| 491 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 492 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 493 | " sk_FragColor.x = 1.0;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 494 | "}\n"); |
| 495 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 496 | "void main() { sk_FragColor.r = dFdx(1); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 497 | *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 498 | "#version 400\n" |
| 499 | "#extension GL_OES_standard_derivatives : require\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 500 | "precision highp float;\n" |
| 501 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 502 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 503 | " sk_FragColor.x = dFdx(1.0);\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 504 | "}\n"); |
| 505 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 506 | |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 507 | |
| 508 | DEF_TEST(SkSLIntFolding, r) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 509 | test(r, |
| 510 | "void main() {" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 511 | "sk_FragColor.r = 32 + 2;" |
| 512 | "sk_FragColor.r = 32 - 2;" |
| 513 | "sk_FragColor.r = 32 * 2;" |
| 514 | "sk_FragColor.r = 32 / 2;" |
| 515 | "sk_FragColor.r = 12 | 6;" |
| 516 | "sk_FragColor.r = 254 & 7;" |
| 517 | "sk_FragColor.r = 2 ^ 7;" |
| 518 | "sk_FragColor.r = 1 << 4;" |
| 519 | "sk_FragColor.r = 128 >> 2;" |
| 520 | "sk_FragColor.r = -1 == -1 ? 1 : -1;" |
| 521 | "sk_FragColor.r = -1 == -2 ? 2 : -2;" |
| 522 | "sk_FragColor.r = 0 != 1 ? 3 : -3;" |
| 523 | "sk_FragColor.r = 0 != 0 ? 4 : -4;" |
| 524 | "sk_FragColor.r = 6 > 5 ? 5 : -5;" |
| 525 | "sk_FragColor.r = 6 > 6 ? 6 : -6;" |
| 526 | "sk_FragColor.r = -1 < 0 ? 7 : -7;" |
| 527 | "sk_FragColor.r = 1 < 0 ? 8 : -8;" |
| 528 | "sk_FragColor.r = 6 >= 6 ? 9 : -9;" |
| 529 | "sk_FragColor.r = 6 >= 7 ? 10 : -10;" |
| 530 | "sk_FragColor.r = 6 <= 6 ? 11 : -11;" |
| 531 | "sk_FragColor.r = 6 <= 5 ? 12 : -12;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 532 | "sk_FragColor.r = int(sqrt(1)) + 0;" |
| 533 | "sk_FragColor.r = 0 + int(sqrt(2));" |
| 534 | "sk_FragColor.r = int(sqrt(3)) - 0;" |
| 535 | "sk_FragColor.r = int(sqrt(4)) * 0;" |
| 536 | "sk_FragColor.r = int(sqrt(5)) * 1;" |
| 537 | "sk_FragColor.r = 1 * int(sqrt(6));" |
| 538 | "sk_FragColor.r = 0 * int(sqrt(7));" |
| 539 | "sk_FragColor.r = int(sqrt(8)) / 1;" |
| 540 | "sk_FragColor.r = 0 / int(sqrt(9));" |
| 541 | "int x = int(sqrt(2));" |
| 542 | "x += 1;" |
| 543 | "x += 0;" |
| 544 | "x -= 1;" |
| 545 | "x -= 0;" |
| 546 | "x *= 1;" |
| 547 | "x *= 2;" |
| 548 | "x /= 1;" |
| 549 | "x /= 2;" |
| 550 | "sk_FragColor.r = x;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 551 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 552 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 553 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 554 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 555 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 556 | " sk_FragColor.x = 34.0;\n" |
| 557 | " sk_FragColor.x = 30.0;\n" |
| 558 | " sk_FragColor.x = 64.0;\n" |
| 559 | " sk_FragColor.x = 16.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 560 | " sk_FragColor.x = 14.0;\n" |
| 561 | " sk_FragColor.x = 6.0;\n" |
| 562 | " sk_FragColor.x = 5.0;\n" |
| 563 | " sk_FragColor.x = 16.0;\n" |
| 564 | " sk_FragColor.x = 32.0;\n" |
| 565 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 566 | " sk_FragColor.x = -2.0;\n" |
| 567 | " sk_FragColor.x = 3.0;\n" |
| 568 | " sk_FragColor.x = -4.0;\n" |
| 569 | " sk_FragColor.x = 5.0;\n" |
| 570 | " sk_FragColor.x = -6.0;\n" |
| 571 | " sk_FragColor.x = 7.0;\n" |
| 572 | " sk_FragColor.x = -8.0;\n" |
| 573 | " sk_FragColor.x = 9.0;\n" |
| 574 | " sk_FragColor.x = -10.0;\n" |
| 575 | " sk_FragColor.x = 11.0;\n" |
| 576 | " sk_FragColor.x = -12.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 577 | " sk_FragColor.x = float(int(sqrt(1.0)));\n" |
| 578 | " sk_FragColor.x = float(int(sqrt(2.0)));\n" |
| 579 | " sk_FragColor.x = float(int(sqrt(3.0)));\n" |
| 580 | " sk_FragColor.x = 0.0;\n" |
| 581 | " sk_FragColor.x = float(int(sqrt(5.0)));\n" |
| 582 | " sk_FragColor.x = float(int(sqrt(6.0)));\n" |
| 583 | " sk_FragColor.x = 0.0;\n" |
| 584 | " sk_FragColor.x = float(int(sqrt(8.0)));\n" |
| 585 | " sk_FragColor.x = 0.0;\n" |
| 586 | " int x = int(sqrt(2.0));\n" |
| 587 | " x += 1;\n" |
| 588 | " x -= 1;\n" |
| 589 | " x *= 2;\n" |
| 590 | " x /= 2;\n" |
| 591 | " sk_FragColor.x = float(x);\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 592 | "}\n"); |
| 593 | } |
| 594 | |
| 595 | DEF_TEST(SkSLFloatFolding, r) { |
| 596 | test(r, |
| 597 | "void main() {" |
| 598 | "sk_FragColor.r = 32.0 + 2.0;" |
| 599 | "sk_FragColor.r = 32.0 - 2.0;" |
| 600 | "sk_FragColor.r = 32.0 * 2.0;" |
| 601 | "sk_FragColor.r = 32.0 / 2.0;" |
| 602 | "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;" |
| 603 | "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;" |
| 604 | "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;" |
| 605 | "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;" |
| 606 | "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;" |
| 607 | "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;" |
| 608 | "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;" |
| 609 | "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;" |
| 610 | "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;" |
| 611 | "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;" |
| 612 | "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;" |
| 613 | "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;" |
| 614 | "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 615 | "sk_FragColor.r = sqrt(1) + 0;" |
| 616 | "sk_FragColor.r = 0 + sqrt(2);" |
| 617 | "sk_FragColor.r = sqrt(3) - 0;" |
| 618 | "sk_FragColor.r = sqrt(4) * 0;" |
| 619 | "sk_FragColor.r = sqrt(5) * 1;" |
| 620 | "sk_FragColor.r = 1 * sqrt(6);" |
| 621 | "sk_FragColor.r = 0 * sqrt(7);" |
| 622 | "sk_FragColor.r = sqrt(8) / 1;" |
| 623 | "sk_FragColor.r = 0 / sqrt(9);" |
| 624 | "sk_FragColor.r += 1;" |
| 625 | "sk_FragColor.r += 0;" |
| 626 | "sk_FragColor.r -= 1;" |
| 627 | "sk_FragColor.r -= 0;" |
| 628 | "sk_FragColor.r *= 1;" |
| 629 | "sk_FragColor.r *= 2;" |
| 630 | "sk_FragColor.r /= 1;" |
| 631 | "sk_FragColor.r /= 2;" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 632 | "}", |
| 633 | *SkSL::ShaderCapsFactory::Default(), |
| 634 | "#version 400\n" |
| 635 | "out vec4 sk_FragColor;\n" |
| 636 | "void main() {\n" |
| 637 | " sk_FragColor.x = 34.0;\n" |
| 638 | " sk_FragColor.x = 30.0;\n" |
| 639 | " sk_FragColor.x = 64.0;\n" |
| 640 | " sk_FragColor.x = 16.0;\n" |
| 641 | " sk_FragColor.x = 19.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 642 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 643 | " sk_FragColor.x = -2.0;\n" |
| 644 | " sk_FragColor.x = 3.0;\n" |
| 645 | " sk_FragColor.x = -4.0;\n" |
| 646 | " sk_FragColor.x = 5.0;\n" |
| 647 | " sk_FragColor.x = -6.0;\n" |
| 648 | " sk_FragColor.x = 7.0;\n" |
| 649 | " sk_FragColor.x = -8.0;\n" |
| 650 | " sk_FragColor.x = 9.0;\n" |
| 651 | " sk_FragColor.x = -10.0;\n" |
| 652 | " sk_FragColor.x = 11.0;\n" |
| 653 | " sk_FragColor.x = -12.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 654 | " sk_FragColor.x = sqrt(1.0);\n" |
| 655 | " sk_FragColor.x = sqrt(2.0);\n" |
| 656 | " sk_FragColor.x = sqrt(3.0);\n" |
| 657 | " sk_FragColor.x = 0.0;\n" |
| 658 | " sk_FragColor.x = sqrt(5.0);\n" |
| 659 | " sk_FragColor.x = sqrt(6.0);\n" |
| 660 | " sk_FragColor.x = 0.0;\n" |
| 661 | " sk_FragColor.x = sqrt(8.0);\n" |
| 662 | " sk_FragColor.x = 0.0;\n" |
| 663 | " sk_FragColor.x += 1.0;\n" |
| 664 | " sk_FragColor.x -= 1.0;\n" |
| 665 | " sk_FragColor.x *= 2.0;\n" |
| 666 | " sk_FragColor.x /= 2.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 667 | "}\n"); |
| 668 | } |
| 669 | |
| 670 | DEF_TEST(SkSLBoolFolding, r) { |
| 671 | test(r, |
| 672 | "void main() {" |
| 673 | "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;" |
| 674 | "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;" |
| 675 | "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;" |
| 676 | "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;" |
| 677 | "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;" |
| 678 | "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;" |
| 679 | "}", |
| 680 | *SkSL::ShaderCapsFactory::Default(), |
| 681 | "#version 400\n" |
| 682 | "out vec4 sk_FragColor;\n" |
| 683 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 684 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 685 | " sk_FragColor.x = -2.0;\n" |
| 686 | " sk_FragColor.x = 3.0;\n" |
| 687 | " sk_FragColor.x = -4.0;\n" |
| 688 | " sk_FragColor.x = 5.0;\n" |
| 689 | " sk_FragColor.x = -6.0;\n" |
| 690 | "}\n"); |
| 691 | } |
| 692 | |
| 693 | DEF_TEST(SkSLVecFolding, r) { |
| 694 | test(r, |
| 695 | "void main() {" |
| 696 | "sk_FragColor.r = vec4(0.5, 1, 1, 1).x;" |
| 697 | "sk_FragColor = vec4(vec2(1), vec2(2, 3)) + vec4(5, 6, 7, 8);" |
| 698 | "sk_FragColor = vec4(8, vec3(10)) - vec4(1);" |
| 699 | "sk_FragColor = vec4(2) * vec4(1, 2, 3, 4);" |
| 700 | "sk_FragColor = vec4(12) / vec4(1, 2, 3, 4);" |
| 701 | "sk_FragColor.r = (vec4(12) / vec4(1, 2, 3, 4)).y;" |
| 702 | "sk_FragColor.x = vec4(1) == vec4(1) ? 1.0 : -1.0;" |
| 703 | "sk_FragColor.x = vec4(1) == vec4(2) ? 2.0 : -2.0;" |
| 704 | "sk_FragColor.x = vec2(1) == vec2(1, 1) ? 3.0 : -3.0;" |
| 705 | "sk_FragColor.x = vec2(1, 1) == vec2(1, 1) ? 4.0 : -4.0;" |
| 706 | "sk_FragColor.x = vec2(1) == vec2(1, 0) ? 5.0 : -5.0;" |
| 707 | "sk_FragColor.x = vec4(1) == vec4(vec2(1), vec2(1)) ? 6.0 : -6.0;" |
| 708 | "sk_FragColor.x = vec4(vec3(1), 1) == vec4(vec2(1), vec2(1)) ? 7.0 : -7.0;" |
| 709 | "sk_FragColor.x = vec4(vec3(1), 1) == vec4(vec2(1), 1, 0) ? 8.0 : -8.0;" |
| 710 | "sk_FragColor.x = vec2(1) != vec2(1, 0) ? 9.0 : -9.0;" |
| 711 | "sk_FragColor.x = vec4(1) != vec4(vec2(1), vec2(1)) ? 10.0 : -10.0;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 712 | "sk_FragColor = vec4(sqrt(1)) * vec4(1);" |
| 713 | "sk_FragColor = vec4(1) * vec4(sqrt(2));" |
| 714 | "sk_FragColor = vec4(0) * vec4(sqrt(3));" |
| 715 | "sk_FragColor = vec4(sqrt(4)) * vec4(0);" |
| 716 | "sk_FragColor = vec4(0) / vec4(sqrt(5));" |
| 717 | "sk_FragColor = vec4(0) + vec4(sqrt(6));" |
| 718 | "sk_FragColor = vec4(sqrt(7)) + vec4(0);" |
| 719 | "sk_FragColor = vec4(sqrt(8)) - vec4(0);" |
| 720 | "sk_FragColor = vec4(0) + sqrt(9);" |
| 721 | "sk_FragColor = vec4(0) * sqrt(10);" |
| 722 | "sk_FragColor = vec4(0) / sqrt(11);" |
| 723 | "sk_FragColor = vec4(1) * sqrt(12);" |
| 724 | "sk_FragColor = 0 + vec4(sqrt(13));" |
| 725 | "sk_FragColor = 0 * vec4(sqrt(14));" |
| 726 | "sk_FragColor = 0 / vec4(sqrt(15));" |
| 727 | "sk_FragColor = 1 * vec4(sqrt(16));" |
| 728 | "sk_FragColor = vec4(sqrt(17)) + 0;" |
| 729 | "sk_FragColor = vec4(sqrt(18)) * 0;" |
| 730 | "sk_FragColor = vec4(sqrt(19)) * 1;" |
| 731 | "sk_FragColor = vec4(sqrt(19.5)) - 0;" |
| 732 | "sk_FragColor = sqrt(20) * vec4(1);" |
| 733 | "sk_FragColor = sqrt(21) + vec4(0);" |
| 734 | "sk_FragColor = sqrt(22) - vec4(0);" |
| 735 | "sk_FragColor = sqrt(23) / vec4(1);" |
| 736 | "sk_FragColor = vec4(sqrt(24)) / 1;" |
| 737 | "sk_FragColor += vec4(1);" |
| 738 | "sk_FragColor += vec4(0);" |
| 739 | "sk_FragColor -= vec4(1);" |
| 740 | "sk_FragColor -= vec4(0);" |
| 741 | "sk_FragColor *= vec4(1);" |
| 742 | "sk_FragColor *= vec4(2);" |
| 743 | "sk_FragColor /= vec4(1);" |
| 744 | "sk_FragColor /= vec4(2);" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 745 | "}", |
| 746 | *SkSL::ShaderCapsFactory::Default(), |
| 747 | "#version 400\n" |
| 748 | "out vec4 sk_FragColor;\n" |
| 749 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 750 | " sk_FragColor.x = 0.5;\n" |
| 751 | " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n" |
| 752 | " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n" |
| 753 | " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n" |
| 754 | " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n" |
| 755 | " sk_FragColor.x = 6.0;\n" |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 756 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 757 | " sk_FragColor.x = -2.0;\n" |
| 758 | " sk_FragColor.x = 3.0;\n" |
| 759 | " sk_FragColor.x = 4.0;\n" |
| 760 | " sk_FragColor.x = -5.0;\n" |
| 761 | " sk_FragColor.x = 6.0;\n" |
| 762 | " sk_FragColor.x = 7.0;\n" |
| 763 | " sk_FragColor.x = -8.0;\n" |
| 764 | " sk_FragColor.x = 9.0;\n" |
| 765 | " sk_FragColor.x = -10.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 766 | " sk_FragColor = vec4(sqrt(1.0));\n" |
| 767 | " sk_FragColor = vec4(sqrt(2.0));\n" |
| 768 | " sk_FragColor = vec4(0.0);\n" |
| 769 | " sk_FragColor = vec4(0.0);\n" |
| 770 | " sk_FragColor = vec4(0.0);\n" |
| 771 | " sk_FragColor = vec4(sqrt(6.0));\n" |
| 772 | " sk_FragColor = vec4(sqrt(7.0));\n" |
| 773 | " sk_FragColor = vec4(sqrt(8.0));\n" |
| 774 | " sk_FragColor = vec4(sqrt(9.0));\n" |
| 775 | " sk_FragColor = vec4(0.0);\n" |
| 776 | " sk_FragColor = vec4(0.0);\n" |
| 777 | " sk_FragColor = vec4(sqrt(12.0));\n" |
| 778 | " sk_FragColor = vec4(sqrt(13.0));\n" |
| 779 | " sk_FragColor = vec4(0.0);\n" |
| 780 | " sk_FragColor = vec4(0.0);\n" |
| 781 | " sk_FragColor = vec4(sqrt(16.0));\n" |
| 782 | " sk_FragColor = vec4(sqrt(17.0));\n" |
| 783 | " sk_FragColor = vec4(0.0);\n" |
| 784 | " sk_FragColor = vec4(sqrt(19.0));\n" |
| 785 | " sk_FragColor = vec4(sqrt(19.5));\n" |
| 786 | " sk_FragColor = vec4(sqrt(20.0));\n" |
| 787 | " sk_FragColor = vec4(sqrt(21.0));\n" |
| 788 | " sk_FragColor = vec4(sqrt(22.0));\n" |
| 789 | " sk_FragColor = vec4(sqrt(23.0));\n" |
| 790 | " sk_FragColor = vec4(sqrt(24.0));\n" |
| 791 | " sk_FragColor += vec4(1.0);\n" |
| 792 | " sk_FragColor -= vec4(1.0);\n" |
| 793 | " sk_FragColor *= vec4(2.0);\n" |
| 794 | " sk_FragColor /= vec4(2.0);\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 795 | "}\n"); |
| 796 | } |
| 797 | |
| 798 | DEF_TEST(SkSLMatFolding, r) { |
| 799 | test(r, |
| 800 | "void main() {" |
| 801 | "sk_FragColor.x = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) == " |
| 802 | "mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 1 : -1;" |
| 803 | "sk_FragColor.x = mat2(vec2(1.0, 0.0), vec2(1.0, 1.0)) == " |
| 804 | "mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 2 : -2;" |
| 805 | "sk_FragColor.x = mat2(1) == mat2(1) ? 3 : -3;" |
| 806 | "sk_FragColor.x = mat2(1) == mat2(0) ? 4 : -4;" |
| 807 | "sk_FragColor.x = mat2(1) == mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 5 : -5;" |
| 808 | "sk_FragColor.x = mat2(2) == mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 6 : -6;" |
| 809 | "sk_FragColor.x = mat3x2(2) == mat3x2(vec2(2.0, 0.0), vec2(0.0, 2.0), vec2(0.0)) ? 7 : -7;" |
| 810 | "sk_FragColor.x = mat2(1) != mat2(1) ? 8 : -8;" |
| 811 | "sk_FragColor.x = mat2(1) != mat2(0) ? 9 : -9;" |
| 812 | "sk_FragColor.x = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 0.0)) == " |
| 813 | "mat3(mat2(1.0)) ? 10 : -10;" |
| 814 | "sk_FragColor.x = mat2(mat3(1.0)) == mat2(1.0) ? 11 : -11;" |
| 815 | "sk_FragColor.x = mat2(vec4(1.0, 0.0, 0.0, 1.0)) == mat2(1.0) ? 12 : -12;" |
| 816 | "sk_FragColor.x = mat2(1.0, 0.0, vec2(0.0, 1.0)) == mat2(1.0) ? 13 : -13;" |
| 817 | "sk_FragColor.x = mat2(vec2(1.0, 0.0), 0.0, 1.0) == mat2(1.0) ? 14 : -14;" |
| 818 | "}", |
| 819 | *SkSL::ShaderCapsFactory::Default(), |
| 820 | "#version 400\n" |
| 821 | "out vec4 sk_FragColor;\n" |
| 822 | "void main() {\n" |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 823 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 824 | " sk_FragColor.x = -2.0;\n" |
| 825 | " sk_FragColor.x = 3.0;\n" |
| 826 | " sk_FragColor.x = -4.0;\n" |
| 827 | " sk_FragColor.x = 5.0;\n" |
| 828 | " sk_FragColor.x = -6.0;\n" |
| 829 | " sk_FragColor.x = 7.0;\n" |
| 830 | " sk_FragColor.x = -8.0;\n" |
| 831 | " sk_FragColor.x = 9.0;\n" |
| 832 | " sk_FragColor.x = 10.0;\n" |
| 833 | " sk_FragColor.x = 11.0;\n" |
| 834 | " sk_FragColor.x = 12.0;\n" |
| 835 | " sk_FragColor.x = 13.0;\n" |
| 836 | " sk_FragColor.x = 14.0;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 837 | "}\n"); |
| 838 | } |
| 839 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 840 | DEF_TEST(SkSLConstantIf, r) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 841 | test(r, |
| 842 | "void main() {" |
| 843 | "int x;" |
| 844 | "if (true) x = 1;" |
| 845 | "if (2 > 1) x = 2; else x = 3;" |
| 846 | "if (1 > 2) x = 4; else x = 5;" |
| 847 | "if (false) x = 6;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 848 | "sk_FragColor.r = x;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 849 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 850 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 851 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 852 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 853 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 854 | " sk_FragColor.x = 5.0;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 855 | "}\n"); |
| 856 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 857 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 858 | DEF_TEST(SkSLCaps, r) { |
| 859 | test(r, |
| 860 | "void main() {" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 861 | "int x = 0;" |
| 862 | "int y = 0;" |
| 863 | "int z = 0;" |
| 864 | "int w = 0;" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 865 | "if (sk_Caps.externalTextureSupport) x = 1;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 866 | "if (sk_Caps.fbFetchSupport) y = 1;" |
| 867 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;" |
| 868 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;" |
| 869 | "sk_FragColor = vec4(x, y, z, w);" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 870 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 871 | *SkSL::ShaderCapsFactory::VariousCaps(), |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 872 | "#version 400\n" |
| 873 | "out vec4 sk_FragColor;\n" |
| 874 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 875 | " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 876 | "}\n"); |
| 877 | } |
| 878 | |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 879 | DEF_TEST(SkSLTexture, r) { |
| 880 | test(r, |
| 881 | "uniform sampler1D one;" |
| 882 | "uniform sampler2D two;" |
| 883 | "void main() {" |
| 884 | "vec4 a = texture(one, 0);" |
| 885 | "vec4 b = texture(two, vec2(0));" |
| 886 | "vec4 c = texture(one, vec2(0));" |
| 887 | "vec4 d = texture(two, vec3(0));" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 888 | "sk_FragColor = vec4(a.x, b.x, c.x, d.x);" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 889 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 890 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 891 | "#version 400\n" |
| 892 | "out vec4 sk_FragColor;\n" |
| 893 | "uniform sampler1D one;\n" |
| 894 | "uniform sampler2D two;\n" |
| 895 | "void main() {\n" |
| 896 | " vec4 a = texture(one, 0.0);\n" |
| 897 | " vec4 b = texture(two, vec2(0.0));\n" |
| 898 | " vec4 c = textureProj(one, vec2(0.0));\n" |
| 899 | " vec4 d = textureProj(two, vec3(0.0));\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 900 | " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 901 | "}\n"); |
| 902 | test(r, |
| 903 | "uniform sampler1D one;" |
| 904 | "uniform sampler2D two;" |
| 905 | "void main() {" |
| 906 | "vec4 a = texture(one, 0);" |
| 907 | "vec4 b = texture(two, vec2(0));" |
| 908 | "vec4 c = texture(one, vec2(0));" |
| 909 | "vec4 d = texture(two, vec3(0));" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 910 | "sk_FragColor = vec4(a.x, b.x, c.x, d.x);" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 911 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 912 | *SkSL::ShaderCapsFactory::Version110(), |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 913 | "#version 110\n" |
| 914 | "uniform sampler1D one;\n" |
| 915 | "uniform sampler2D two;\n" |
| 916 | "void main() {\n" |
| 917 | " vec4 a = texture1D(one, 0.0);\n" |
| 918 | " vec4 b = texture2D(two, vec2(0.0));\n" |
| 919 | " vec4 c = texture1DProj(one, vec2(0.0));\n" |
| 920 | " vec4 d = texture2DProj(two, vec3(0.0));\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 921 | " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 922 | "}\n"); |
| 923 | } |
| 924 | |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 925 | DEF_TEST(SkSLOffset, r) { |
| 926 | test(r, |
| 927 | "struct Test {" |
| 928 | "layout(offset = 0) int x;" |
| 929 | "layout(offset = 4) int y;" |
| 930 | "int z;" |
| 931 | "} test;", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 932 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 933 | "#version 400\n" |
| 934 | "out vec4 sk_FragColor;\n" |
| 935 | "struct Test {\n" |
| 936 | " layout (offset = 0) int x;\n" |
| 937 | " layout (offset = 4) int y;\n" |
| 938 | " int z;\n" |
| 939 | "} test;\n"); |
| 940 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 941 | |
| 942 | DEF_TEST(SkSLFragCoord, r) { |
| 943 | SkSL::Program::Settings settings; |
| 944 | settings.fFlipY = true; |
| 945 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld(); |
| 946 | settings.fCaps = caps.get(); |
| 947 | SkSL::Program::Inputs inputs; |
| 948 | test(r, |
| 949 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 950 | settings, |
| 951 | "#version 110\n" |
| 952 | "#extension GL_ARB_fragment_coord_conventions : require\n" |
| 953 | "layout(origin_upper_left) in vec4 gl_FragCoord;\n" |
| 954 | "void main() {\n" |
| 955 | " gl_FragColor.xy = gl_FragCoord.xy;\n" |
| 956 | "}\n", |
| 957 | &inputs); |
| 958 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 959 | |
| 960 | caps = SkSL::ShaderCapsFactory::FragCoordsNew(); |
| 961 | settings.fCaps = caps.get(); |
| 962 | test(r, |
| 963 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 964 | settings, |
| 965 | "#version 400\n" |
| 966 | "layout(origin_upper_left) in vec4 gl_FragCoord;\n" |
| 967 | "out vec4 sk_FragColor;\n" |
| 968 | "void main() {\n" |
| 969 | " sk_FragColor.xy = gl_FragCoord.xy;\n" |
| 970 | "}\n", |
| 971 | &inputs); |
| 972 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 973 | |
| 974 | caps = SkSL::ShaderCapsFactory::Default(); |
| 975 | settings.fCaps = caps.get(); |
| 976 | test(r, |
| 977 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 978 | settings, |
| 979 | "#version 400\n" |
| 980 | "uniform float u_skRTHeight;\n" |
| 981 | "out vec4 sk_FragColor;\n" |
| 982 | "void main() {\n" |
| 983 | " vec2 _sktmpCoord = gl_FragCoord.xy;\n" |
| 984 | " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n" |
| 985 | " sk_FragColor.xy = sk_FragCoord.xy;\n" |
| 986 | "}\n", |
| 987 | &inputs); |
| 988 | REPORTER_ASSERT(r, inputs.fRTHeight); |
| 989 | |
| 990 | settings.fFlipY = false; |
| 991 | test(r, |
| 992 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 993 | settings, |
| 994 | "#version 400\n" |
| 995 | "out vec4 sk_FragColor;\n" |
| 996 | "void main() {\n" |
| 997 | " sk_FragColor.xy = gl_FragCoord.xy;\n" |
| 998 | "}\n", |
| 999 | &inputs); |
| 1000 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 1001 | } |
| 1002 | |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 1003 | DEF_TEST(SkSLVertexID, r) { |
| 1004 | test(r, |
| 1005 | "out int id; void main() { id = sk_VertexID; }", |
| 1006 | *SkSL::ShaderCapsFactory::Default(), |
| 1007 | "#version 400\n" |
| 1008 | "out int id;\n" |
| 1009 | "void main() {\n" |
| 1010 | " id = gl_VertexID;\n" |
| 1011 | "}\n", |
| 1012 | SkSL::Program::kVertex_Kind); |
| 1013 | } |
| 1014 | |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 1015 | DEF_TEST(SkSLClipDistance, r) { |
| 1016 | test(r, |
| 1017 | "void main() { sk_ClipDistance[0] = 0; }", |
| 1018 | *SkSL::ShaderCapsFactory::Default(), |
| 1019 | "#version 400\n" |
| 1020 | "void main() {\n" |
| 1021 | " gl_ClipDistance[0] = 0.0;\n" |
| 1022 | "}\n", |
| 1023 | SkSL::Program::kVertex_Kind); |
| 1024 | test(r, |
| 1025 | "void main() { sk_FragColor = vec4(sk_ClipDistance[0]); }", |
| 1026 | *SkSL::ShaderCapsFactory::Default(), |
| 1027 | "#version 400\n" |
| 1028 | "out vec4 sk_FragColor;\n" |
| 1029 | "void main() {\n" |
| 1030 | " sk_FragColor = vec4(gl_ClipDistance[0]);\n" |
| 1031 | "}\n"); |
| 1032 | } |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1033 | |
| 1034 | DEF_TEST(SkSLArrayTypes, r) { |
| 1035 | test(r, |
| 1036 | "void main() { vec2 x[2] = vec2[2](vec2(1), vec2(2));" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1037 | "vec2[2] y = vec2[2](vec2(3), vec2(4));" |
| 1038 | "sk_FragColor = vec4(x[0], y[1]); }", |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1039 | *SkSL::ShaderCapsFactory::Default(), |
| 1040 | "#version 400\n" |
| 1041 | "out vec4 sk_FragColor;\n" |
| 1042 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1043 | " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], " |
| 1044 | "vec2[2](vec2(3.0), vec2(4.0))[1]);\n" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1045 | "}\n"); |
| 1046 | } |
| 1047 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1048 | DEF_TEST(SkSLGeometry, r) { |
| 1049 | test(r, |
| 1050 | "layout(points) in;" |
| 1051 | "layout(invocations = 2) in;" |
| 1052 | "layout(line_strip, max_vertices = 2) out;" |
| 1053 | "void main() {" |
| 1054 | "gl_Position = sk_in[0].gl_Position + vec4(-0.5, 0, 0, sk_InvocationID);" |
| 1055 | "EmitVertex();" |
| 1056 | "gl_Position = sk_in[0].gl_Position + vec4(0.5, 0, 0, sk_InvocationID);" |
| 1057 | "EmitVertex();" |
| 1058 | "EndPrimitive();" |
| 1059 | "}", |
| 1060 | *SkSL::ShaderCapsFactory::Default(), |
| 1061 | "#version 400\n" |
| 1062 | "layout (points) in ;\n" |
| 1063 | "layout (invocations = 2) in ;\n" |
| 1064 | "layout (line_strip, max_vertices = 2) out ;\n" |
| 1065 | "void main() {\n" |
| 1066 | " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" |
| 1067 | " EmitVertex();\n" |
| 1068 | " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n" |
| 1069 | " EmitVertex();\n" |
| 1070 | " EndPrimitive();\n" |
| 1071 | "}\n", |
| 1072 | SkSL::Program::kGeometry_Kind); |
| 1073 | } |
| 1074 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1075 | DEF_TEST(SkSLSwitch, r) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1076 | // basic "does a switch even work" test |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1077 | test(r, |
| 1078 | "void main() {" |
| 1079 | " float x;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1080 | " switch (int(sqrt(1))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1081 | " case 0:" |
| 1082 | " x = 0.0;" |
| 1083 | " break;" |
| 1084 | " case 1:" |
| 1085 | " x = 1.0;" |
| 1086 | " break;" |
| 1087 | " default:" |
| 1088 | " x = 2.0;" |
| 1089 | " }" |
| 1090 | " sk_FragColor = vec4(x);" |
| 1091 | "}", |
| 1092 | *SkSL::ShaderCapsFactory::Default(), |
| 1093 | "#version 400\n" |
| 1094 | "out vec4 sk_FragColor;\n" |
| 1095 | "void main() {\n" |
| 1096 | " float x;\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1097 | " switch (int(sqrt(1.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1098 | " case 0:\n" |
| 1099 | " x = 0.0;\n" |
| 1100 | " break;\n" |
| 1101 | " case 1:\n" |
| 1102 | " x = 1.0;\n" |
| 1103 | " break;\n" |
| 1104 | " default:\n" |
| 1105 | " x = 2.0;\n" |
| 1106 | " }\n" |
| 1107 | " sk_FragColor = vec4(x);\n" |
| 1108 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1109 | // dead code inside of switch |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1110 | test(r, |
| 1111 | "void main() {" |
| 1112 | " float x;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1113 | " switch (int(sqrt(2))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1114 | " case 0:" |
| 1115 | " x = 0.0;" |
| 1116 | " case 1:" |
| 1117 | " x = 1.0;" |
| 1118 | " default:" |
| 1119 | " x = 2.0;" |
| 1120 | " }" |
| 1121 | " sk_FragColor = vec4(x);" |
| 1122 | "}", |
| 1123 | *SkSL::ShaderCapsFactory::Default(), |
| 1124 | "#version 400\n" |
| 1125 | "out vec4 sk_FragColor;\n" |
| 1126 | "void main() {\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1127 | " switch (int(sqrt(2.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1128 | " case 0:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1129 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1130 | " case 1:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1131 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1132 | " default:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1133 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1134 | " }\n" |
| 1135 | " sk_FragColor = vec4(2.0);\n" |
| 1136 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1137 | // non-static test w/ fallthrough |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1138 | test(r, |
| 1139 | "void main() {" |
| 1140 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1141 | " switch (int(sqrt(3))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1142 | " case 0:" |
| 1143 | " x = 0.0;" |
| 1144 | " case 1:" |
| 1145 | " x = 1.0;" |
| 1146 | " }" |
| 1147 | " sk_FragColor = vec4(x);" |
| 1148 | "}", |
| 1149 | *SkSL::ShaderCapsFactory::Default(), |
| 1150 | "#version 400\n" |
| 1151 | "out vec4 sk_FragColor;\n" |
| 1152 | "void main() {\n" |
| 1153 | " float x = 0.0;\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1154 | " switch (int(sqrt(3.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1155 | " case 0:\n" |
| 1156 | " x = 0.0;\n" |
| 1157 | " case 1:\n" |
| 1158 | " x = 1.0;\n" |
| 1159 | " }\n" |
| 1160 | " sk_FragColor = vec4(x);\n" |
| 1161 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1162 | // static test w/ fallthrough |
| 1163 | test(r, |
| 1164 | "void main() {" |
| 1165 | " float x = 0.0;" |
| 1166 | " switch (0) {" |
| 1167 | " case 0:" |
| 1168 | " x = 0.0;" |
| 1169 | " case 1:" |
| 1170 | " x = 1.0;" |
| 1171 | " }" |
| 1172 | " sk_FragColor = vec4(x);" |
| 1173 | "}", |
| 1174 | *SkSL::ShaderCapsFactory::Default(), |
| 1175 | "#version 400\n" |
| 1176 | "out vec4 sk_FragColor;\n" |
| 1177 | "void main() {\n" |
| 1178 | " sk_FragColor = vec4(1.0);\n" |
| 1179 | "}\n"); |
| 1180 | // static test w/ fallthrough, different entry point |
| 1181 | test(r, |
| 1182 | "void main() {" |
| 1183 | " float x = 0.0;" |
| 1184 | " switch (1) {" |
| 1185 | " case 0:" |
| 1186 | " x = 0.0;" |
| 1187 | " case 1:" |
| 1188 | " x = 1.0;" |
| 1189 | " }" |
| 1190 | " sk_FragColor = vec4(x);" |
| 1191 | "}", |
| 1192 | *SkSL::ShaderCapsFactory::Default(), |
| 1193 | "#version 400\n" |
| 1194 | "out vec4 sk_FragColor;\n" |
| 1195 | "void main() {\n" |
| 1196 | " sk_FragColor = vec4(1.0);\n" |
| 1197 | "}\n"); |
| 1198 | // static test w/ break |
| 1199 | test(r, |
| 1200 | "void main() {" |
| 1201 | " float x = 0.0;" |
| 1202 | " switch (0) {" |
| 1203 | " case 0:" |
| 1204 | " x = 0.0;" |
| 1205 | " break;" |
| 1206 | " case 1:" |
| 1207 | " x = 1.0;" |
| 1208 | " }" |
| 1209 | " sk_FragColor = vec4(x);" |
| 1210 | "}", |
| 1211 | *SkSL::ShaderCapsFactory::Default(), |
| 1212 | "#version 400\n" |
| 1213 | "out vec4 sk_FragColor;\n" |
| 1214 | "void main() {\n" |
| 1215 | " sk_FragColor = vec4(0.0);\n" |
| 1216 | "}\n"); |
| 1217 | // static test w/ static conditional break |
| 1218 | test(r, |
| 1219 | "void main() {" |
| 1220 | " float x = 0.0;" |
| 1221 | " switch (0) {" |
| 1222 | " case 0:" |
| 1223 | " x = 0.0;" |
| 1224 | " if (x < 1) break;" |
| 1225 | " case 1:" |
| 1226 | " x = 1.0;" |
| 1227 | " }" |
| 1228 | " sk_FragColor = vec4(x);" |
| 1229 | "}", |
| 1230 | *SkSL::ShaderCapsFactory::Default(), |
| 1231 | "#version 400\n" |
| 1232 | "out vec4 sk_FragColor;\n" |
| 1233 | "void main() {\n" |
| 1234 | " sk_FragColor = vec4(0.0);\n" |
| 1235 | "}\n"); |
| 1236 | // static test w/ non-static conditional break |
| 1237 | test(r, |
| 1238 | "void main() {" |
| 1239 | " float x = 0.0;" |
| 1240 | " switch (0) {" |
| 1241 | " case 0:" |
| 1242 | " x = 0.0;" |
| 1243 | " if (x < sqrt(1)) break;" |
| 1244 | " case 1:" |
| 1245 | " x = 1.0;" |
| 1246 | " }" |
| 1247 | " sk_FragColor = vec4(x);" |
| 1248 | "}", |
| 1249 | *SkSL::ShaderCapsFactory::Default(), |
| 1250 | "#version 400\n" |
| 1251 | "out vec4 sk_FragColor;\n" |
| 1252 | "void main() {\n" |
| 1253 | " float x = 0.0;\n" |
| 1254 | " switch (0) {\n" |
| 1255 | " case 0:\n" |
| 1256 | " x = 0.0;\n" |
| 1257 | " if (0.0 < sqrt(1.0)) break;\n" |
| 1258 | " case 1:\n" |
| 1259 | " x = 1.0;\n" |
| 1260 | " }\n" |
| 1261 | " sk_FragColor = vec4(x);\n" |
| 1262 | "}\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1263 | } |
| 1264 | |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1265 | DEF_TEST(SkSLRectangleTexture, r) { |
| 1266 | test(r, |
| 1267 | "uniform sampler2D test;" |
| 1268 | "void main() {" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1269 | " sk_FragColor = texture(test, vec2(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1270 | "}", |
| 1271 | *SkSL::ShaderCapsFactory::Default(), |
| 1272 | "#version 400\n" |
| 1273 | "out vec4 sk_FragColor;\n" |
| 1274 | "uniform sampler2D test;\n" |
| 1275 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1276 | " sk_FragColor = texture(test, vec2(0.5));\n" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1277 | "}\n"); |
| 1278 | test(r, |
| 1279 | "uniform sampler2DRect test;" |
| 1280 | "void main() {" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1281 | " sk_FragColor = texture(test, vec2(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1282 | "}", |
| 1283 | *SkSL::ShaderCapsFactory::Default(), |
| 1284 | "#version 400\n" |
| 1285 | "out vec4 sk_FragColor;\n" |
| 1286 | "uniform sampler2DRect test;\n" |
| 1287 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1288 | " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1289 | "}\n"); |
| 1290 | test(r, |
| 1291 | "uniform sampler2DRect test;" |
| 1292 | "void main() {" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1293 | " sk_FragColor = texture(test, vec3(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1294 | "}", |
| 1295 | *SkSL::ShaderCapsFactory::Default(), |
| 1296 | "#version 400\n" |
| 1297 | "out vec4 sk_FragColor;\n" |
| 1298 | "uniform sampler2DRect test;\n" |
| 1299 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1300 | " sk_FragColor = texture(test, vec3(textureSize(test), 1.0) * vec3(0.5));\n" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1301 | "}\n"); |
| 1302 | } |
| 1303 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1304 | DEF_TEST(SkSLUnusedVars, r) { |
| 1305 | test(r, |
| 1306 | "void main() {" |
| 1307 | "float a = 1, b = 2, c = 3;" |
| 1308 | "float d = c;" |
| 1309 | "float e = d;" |
| 1310 | "b++;" |
| 1311 | "d++;" |
| 1312 | "sk_FragColor = vec4(b, b, d, d);" |
| 1313 | "}", |
| 1314 | *SkSL::ShaderCapsFactory::Default(), |
| 1315 | "#version 400\n" |
| 1316 | "out vec4 sk_FragColor;\n" |
| 1317 | "void main() {\n" |
| 1318 | " float b = 2.0;\n" |
| 1319 | " float d = 3.0;\n" |
| 1320 | " b++;\n" |
| 1321 | " d++;\n" |
| 1322 | " sk_FragColor = vec4(b, b, d, d);\n" |
| 1323 | "}\n"); |
| 1324 | } |
| 1325 | |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 1326 | DEF_TEST(SkSLMultipleAssignments, r) { |
| 1327 | test(r, |
| 1328 | "void main() {" |
| 1329 | "float x;" |
| 1330 | "float y;" |
| 1331 | "int z;" |
| 1332 | "x = y = z = 1;" |
| 1333 | "sk_FragColor = vec4(z);" |
| 1334 | "}", |
| 1335 | *SkSL::ShaderCapsFactory::Default(), |
| 1336 | "#version 400\n" |
| 1337 | "out vec4 sk_FragColor;\n" |
| 1338 | "void main() {\n" |
| 1339 | " sk_FragColor = vec4(1.0);\n" |
| 1340 | "}\n"); |
| 1341 | } |
| 1342 | |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1343 | DEF_TEST(SkSLComplexDelete, r) { |
| 1344 | test(r, |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1345 | "uniform mat4 colorXform;" |
| 1346 | "uniform sampler2D sampler;" |
| 1347 | "void main() {" |
| 1348 | "vec4 tmpColor;" |
| 1349 | "sk_FragColor = vec4(1.0) * (tmpColor = texture(sampler, vec2(1)) , " |
| 1350 | "colorXform != mat4(1.0) ? vec4(clamp((mat4(colorXform) * vec4(tmpColor.xyz, 1.0)).xyz, " |
| 1351 | "0.0, tmpColor.w), tmpColor.w) : tmpColor);" |
| 1352 | "}", |
| 1353 | *SkSL::ShaderCapsFactory::Default(), |
| 1354 | "#version 400\n" |
| 1355 | "out vec4 sk_FragColor;\n" |
| 1356 | "uniform mat4 colorXform;\n" |
| 1357 | "uniform sampler2D sampler;\n" |
| 1358 | "void main() {\n" |
| 1359 | " vec4 tmpColor;\n" |
| 1360 | " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? " |
| 1361 | "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : " |
| 1362 | "tmpColor);\n" |
| 1363 | "}\n"); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1364 | } |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1365 | |
| 1366 | DEF_TEST(SkSLDependentInitializers, r) { |
| 1367 | test(r, |
| 1368 | "void main() {" |
| 1369 | "float x = 0.5, y = x * 2;" |
| 1370 | "sk_FragColor = vec4(y);" |
| 1371 | "}", |
| 1372 | *SkSL::ShaderCapsFactory::Default(), |
| 1373 | "#version 400\n" |
| 1374 | "out vec4 sk_FragColor;\n" |
| 1375 | "void main() {\n" |
| 1376 | " sk_FragColor = vec4(1.0);\n" |
| 1377 | "}\n"); |
| 1378 | } |
| 1379 | |
Ethan Nicholas | b310fd5 | 2017-06-09 13:46:34 -0400 | [diff] [blame] | 1380 | DEF_TEST(SkSLDeadLoopVar, r) { |
| 1381 | test(r, |
| 1382 | "void main() {" |
| 1383 | "for (int x = 0; x < 4; ) {" |
| 1384 | "break;" |
| 1385 | "}" |
| 1386 | "}", |
| 1387 | *SkSL::ShaderCapsFactory::Default(), |
| 1388 | "#version 400\n" |
| 1389 | "out vec4 sk_FragColor;\n" |
| 1390 | "void main() {\n" |
| 1391 | " for (; true; ) {\n" |
| 1392 | " break;\n" |
| 1393 | " }\n" |
| 1394 | "}\n" |
| 1395 | ); |
| 1396 | } |
| 1397 | |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1398 | DEF_TEST(SkSLInvocations, r) { |
| 1399 | test(r, |
| 1400 | "layout(points) in;" |
| 1401 | "layout(invocations = 2) in;" |
| 1402 | "layout(line_strip, max_vertices = 2) out;" |
| 1403 | "void test() {" |
| 1404 | "gl_Position = sk_in[0].gl_Position + vec4(0.5, 0, 0, sk_InvocationID);" |
| 1405 | "EmitVertex();" |
| 1406 | "}" |
| 1407 | "void main() {" |
| 1408 | "gl_Position = sk_in[0].gl_Position + vec4(-0.5, 0, 0, sk_InvocationID);" |
| 1409 | "EmitVertex();" |
| 1410 | "}", |
| 1411 | *SkSL::ShaderCapsFactory::MustImplementGSInvocationsWithLoop(), |
| 1412 | "#version 400\n" |
| 1413 | "int sk_InvocationID;\n" |
| 1414 | "layout (points) in ;\n" |
| 1415 | "layout (line_strip, max_vertices = 4) out ;\n" |
| 1416 | "void test() {\n" |
| 1417 | " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n" |
| 1418 | " EmitVertex();\n" |
| 1419 | "}\n" |
| 1420 | "void _invoke() {\n" |
| 1421 | " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n" |
| 1422 | " EmitVertex();\n" |
| 1423 | "}\n" |
| 1424 | "void main() {\n" |
| 1425 | " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n" |
| 1426 | " _invoke();\n" |
| 1427 | " EndPrimitive();\n" |
| 1428 | " }\n" |
| 1429 | "}\n", |
| 1430 | SkSL::Program::kGeometry_Kind); |
| 1431 | } |
| 1432 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 1433 | #endif |