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; |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 26 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src), |
| 27 | settings); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 28 | if (!program) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 29 | SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str()); |
| 30 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 31 | REPORTER_ASSERT(r, program); |
| 32 | *inputs = program->fInputs; |
| 33 | REPORTER_ASSERT(r, compiler.toGLSL(*program, &output)); |
| 34 | if (program) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 35 | SkSL::String skExpected(expected); |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 36 | if (output != skExpected) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 37 | 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] | 38 | expected, output.c_str()); |
| 39 | } |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 40 | REPORTER_ASSERT(r, output == skExpected); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 44 | static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps, |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 45 | const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 46 | SkSL::Program::Settings settings; |
| 47 | settings.fCaps = ∩︀ |
| 48 | SkSL::Program::Inputs inputs; |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 49 | test(r, src, settings, expected, &inputs, kind); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 50 | } |
| 51 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 52 | DEF_TEST(SkSLHelloWorld, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 53 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 54 | "void main() { sk_FragColor = float4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 55 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 56 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 57 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 58 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 59 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 60 | "}\n"); |
| 61 | } |
| 62 | |
| 63 | DEF_TEST(SkSLControl, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 64 | test(r, |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 65 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 66 | "if (sqrt(2) > 5) { sk_FragColor = float4(0.75); } else { discard; }" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 67 | "int i = 0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 68 | "while (i < 10) { sk_FragColor *= 0.5; i++; }" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 69 | "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 70 | "for (int i = 0; i < 10; i++) {" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 71 | "if (i % 2 == 1) break; else continue;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 72 | "}" |
| 73 | "return;" |
| 74 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 75 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 76 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 77 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 78 | "void main() {\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 79 | " if (sqrt(2.0) > 5.0) {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 80 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 81 | " } else {\n" |
| 82 | " discard;\n" |
| 83 | " }\n" |
| 84 | " int i = 0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 85 | " while (i < 10) {\n" |
| 86 | " sk_FragColor *= 0.5;\n" |
| 87 | " i++;\n" |
| 88 | " }\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 89 | " do {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 90 | " sk_FragColor += 0.01;\n" |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 91 | " } while (sk_FragColor.x < 0.75);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 92 | " for (int i = 0;i < 10; i++) {\n" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 93 | " if (i % 2 == 1) break; else continue;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 94 | " }\n" |
| 95 | " return;\n" |
| 96 | "}\n"); |
| 97 | } |
| 98 | |
| 99 | DEF_TEST(SkSLFunctions, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 100 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 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 = float4(x); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 104 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 105 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 106 | "out vec4 sk_FragColor;\n" |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 107 | "float foo(float v[2]) {\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 113 | " y[1] = x * 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 114 | " z = foo(y);\n" |
| 115 | " x = z;\n" |
| 116 | "}\n" |
| 117 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 118 | " float x = 10.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 119 | " bar(x);\n" |
| 120 | " sk_FragColor = vec4(x);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 121 | "}\n"); |
| 122 | } |
| 123 | |
| 124 | DEF_TEST(SkSLOperators, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 125 | test(r, |
| 126 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 127 | "float x = 1, y = 2;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 128 | "int z = 3;" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 129 | "x = x - x + y * z * x * (y - z);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 130 | "y = x / y / z;" |
| 131 | "z = (z / 2 % 3 << 4) >> 2 << 1;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 132 | "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 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;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 145 | "x = (float2(sqrt(1)) , 6);" |
| 146 | "z = (float2(sqrt(1)) , 6);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 147 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 148 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 149 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 150 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 151 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 152 | " float x = 1.0, y = 2.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 153 | " int z = 3;\n" |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 154 | " x = -6.0;\n" |
| 155 | " y = -1.0;\n" |
| 156 | " z = 8;\n" |
| 157 | " bool b = false == true || 2.0 >= sqrt(2.0) && true;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 158 | " x += 12.0;\n" |
| 159 | " x -= 12.0;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 160 | " x *= (y /= float(z = 10));\n" |
| 161 | " b ||= false;\n" |
| 162 | " b &&= true;\n" |
| 163 | " b ^^= false;\n" |
| 164 | " z |= 0;\n" |
| 165 | " z &= -1;\n" |
| 166 | " z ^= 0;\n" |
| 167 | " z >>= 2;\n" |
| 168 | " z <<= 4;\n" |
| 169 | " z %= 5;\n" |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 170 | " x = float((vec2(sqrt(1.0)) , 6));\n" |
| 171 | " z = (vec2(sqrt(1.0)) , 6);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 172 | "}\n"); |
| 173 | } |
| 174 | |
| 175 | DEF_TEST(SkSLMatrices, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 176 | test(r, |
| 177 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 178 | "float2x4 x = float2x4(1);" |
| 179 | "float3x2 y = float3x2(1, 0, 0, 1, float2(2, 2));" |
| 180 | "float3x4 z = x * y;" |
| 181 | "float3 v1 = float3x3(1) * float3(2);" |
| 182 | "float3 v2 = float3(2) * float3x3(1);" |
| 183 | "sk_FragColor = float4(z[0].x, v1 + v2);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 184 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 185 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 186 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 187 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 188 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 189 | " 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] | 190 | " vec3 v1 = mat3(1.0) * vec3(2.0);\n" |
| 191 | " vec3 v2 = vec3(2.0) * mat3(1.0);\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 192 | " sk_FragColor = vec4(z[0].x, v1 + v2);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 193 | "}\n"); |
| 194 | } |
| 195 | |
| 196 | DEF_TEST(SkSLInterfaceBlock, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 197 | test(r, |
| 198 | "uniform testBlock {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 199 | "half x;" |
| 200 | "half y[2];" |
| 201 | "layout(binding=12) half3x2 z;" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 202 | "bool w;" |
| 203 | "};" |
| 204 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 205 | " sk_FragColor = half4(x, y[0], y[1], 0);" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 206 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 207 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 208 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 209 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 210 | "uniform testBlock {\n" |
| 211 | " float x;\n" |
| 212 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 213 | " layout (binding = 12) mat3x2 z;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 214 | " bool w;\n" |
| 215 | "};\n" |
| 216 | "void main() {\n" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 217 | " sk_FragColor = vec4(x, y[0], y[1], 0.0);\n" |
| 218 | "}\n"); |
| 219 | test(r, |
| 220 | "uniform testBlock {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 221 | "float x;" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 222 | "} test;" |
| 223 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 224 | " sk_FragColor = half4(test.x);" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 225 | "}", |
| 226 | *SkSL::ShaderCapsFactory::Default(), |
| 227 | "#version 400\n" |
| 228 | "out vec4 sk_FragColor;\n" |
| 229 | "uniform testBlock {\n" |
| 230 | " float x;\n" |
| 231 | "} test;\n" |
| 232 | "void main() {\n" |
| 233 | " sk_FragColor = vec4(test.x);\n" |
| 234 | "}\n"); |
| 235 | test(r, |
| 236 | "uniform testBlock {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 237 | "float x;" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 238 | "} test[2];" |
| 239 | "void main() {" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 240 | " sk_FragColor = half4(test[1].x);" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 241 | "}", |
| 242 | *SkSL::ShaderCapsFactory::Default(), |
| 243 | "#version 400\n" |
| 244 | "out vec4 sk_FragColor;\n" |
| 245 | "uniform testBlock {\n" |
| 246 | " float x;\n" |
| 247 | "} test[2];\n" |
| 248 | "void main() {\n" |
| 249 | " sk_FragColor = vec4(test[1].x);\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 250 | "}\n"); |
| 251 | } |
| 252 | |
| 253 | DEF_TEST(SkSLStructs, r) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 254 | test(r, |
| 255 | "struct A {" |
| 256 | "int x;" |
| 257 | "int y;" |
| 258 | "} a1, a2;" |
| 259 | "A a3;" |
| 260 | "struct B {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 261 | "float x;" |
| 262 | "float y[2];" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 263 | "layout(binding=1) A z;" |
| 264 | "};" |
| 265 | "B b1, b2, b3;" |
| 266 | "void main() {" |
| 267 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 268 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 269 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 270 | "out vec4 sk_FragColor;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 271 | "struct A {\n" |
| 272 | " int x;\n" |
| 273 | " int y;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 274 | "} a1, a2;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 275 | "A a3;\n" |
| 276 | "struct B {\n" |
| 277 | " float x;\n" |
| 278 | " float[2] y;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 279 | " layout (binding = 1) A z;\n" |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 280 | "} b1, b2, b3;\n" |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 281 | "void main() {\n" |
| 282 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 283 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 284 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 285 | DEF_TEST(SkSLVersion, r) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 286 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 287 | "in float test; void main() { sk_FragColor = float4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 288 | *SkSL::ShaderCapsFactory::Version450Core(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 289 | "#version 450 core\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 290 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 291 | "in float test;\n" |
| 292 | "void main() {\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 293 | " sk_FragColor = vec4(0.75);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 294 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 295 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 296 | "in float test; void main() { sk_FragColor = float4(0.75); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 297 | *SkSL::ShaderCapsFactory::Version110(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 298 | "#version 110\n" |
| 299 | "varying float test;\n" |
| 300 | "void main() {\n" |
| 301 | " gl_FragColor = vec4(0.75);\n" |
| 302 | "}\n"); |
| 303 | } |
| 304 | |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 305 | DEF_TEST(SkSLUsesPrecisionModifiers, r) { |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 306 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 307 | "void main() { half x = 0.75; float y = 1; x++; y++;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 308 | "sk_FragColor.rg = half2(x, y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 309 | *SkSL::ShaderCapsFactory::Default(), |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 310 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 311 | "out vec4 sk_FragColor;\n" |
Leon Scroggins | 857cb97 | 2016-11-11 18:44:37 +0000 | [diff] [blame] | 312 | "void main() {\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 313 | " float x = 0.75;\n" |
| 314 | " float y = 1.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 315 | " x++;\n" |
| 316 | " y++;\n" |
| 317 | " sk_FragColor.xy = vec2(x, y);\n" |
| 318 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 319 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 320 | "void main() { half x = 0.75; float y = 1; x++; y++;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 321 | "sk_FragColor.rg = half2(x, y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 322 | *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 323 | "#version 400\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 324 | "precision mediump float;\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 325 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 326 | "void main() {\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 327 | " mediump float x = 0.75;\n" |
| 328 | " highp float y = 1.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 329 | " x++;\n" |
| 330 | " y++;\n" |
| 331 | " sk_FragColor.xy = vec2(x, y);\n" |
| 332 | "}\n"); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | DEF_TEST(SkSLMinAbs, r) { |
| 336 | test(r, |
| 337 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 338 | "float x = -5;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 339 | "sk_FragColor.r = min(abs(x), 6);" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 340 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 341 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 342 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 343 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 344 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 345 | " sk_FragColor.x = min(abs(-5.0), 6.0);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 346 | "}\n"); |
| 347 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 348 | test(r, |
| 349 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 350 | "float x = -5.0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 351 | "sk_FragColor.r = min(abs(x), 6.0);" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 352 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 353 | *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 354 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 355 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 356 | "void main() {\n" |
| 357 | " float minAbsHackVar0;\n" |
| 358 | " float minAbsHackVar1;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 359 | " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? " |
| 360 | "minAbsHackVar0 : minAbsHackVar1);\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 361 | "}\n"); |
| 362 | } |
| 363 | |
Florin Malita | 3b30c4f | 2017-08-08 15:47:35 -0400 | [diff] [blame] | 364 | DEF_TEST(SkSLFractNegative, r) { |
| 365 | static constexpr char input[] = |
| 366 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 367 | "float x = -42.0;" |
Florin Malita | 3b30c4f | 2017-08-08 15:47:35 -0400 | [diff] [blame] | 368 | "sk_FragColor.r = fract(x);" |
| 369 | "}"; |
| 370 | static constexpr char output_default[] = |
| 371 | "#version 400\n" |
| 372 | "out vec4 sk_FragColor;\n" |
| 373 | "void main() {\n" |
| 374 | " sk_FragColor.x = fract(-42.0);\n" |
| 375 | "}\n"; |
| 376 | static constexpr char output_workaround[] = |
| 377 | "#version 400\n" |
| 378 | "out vec4 sk_FragColor;\n" |
| 379 | "void main() {\n" |
| 380 | " sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n" |
| 381 | "}\n"; |
| 382 | |
| 383 | test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default); |
| 384 | test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround); |
| 385 | } |
| 386 | |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 387 | DEF_TEST(SkSLNegatedAtan, r) { |
| 388 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 389 | "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 390 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 391 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 392 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 393 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 394 | " vec2 x = vec2(sqrt(2.0));\n" |
| 395 | " sk_FragColor.x = atan(x.x, -x.y);\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 396 | "}\n"); |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 397 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 398 | "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 399 | *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(), |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 400 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 401 | "out vec4 sk_FragColor;\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 402 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 403 | " vec2 x = vec2(sqrt(2.0));\n" |
| 404 | " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n" |
ethannicholas | ad146f6 | 2016-10-14 06:40:02 -0700 | [diff] [blame] | 405 | "}\n"); |
| 406 | } |
| 407 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 408 | DEF_TEST(SkSLModifiersDeclaration, r) { |
| 409 | test(r, |
| 410 | "layout(blend_support_all_equations) out;" |
| 411 | "void main() { }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 412 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 413 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 414 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 415 | "layout (blend_support_all_equations) out ;\n" |
| 416 | "void main() {\n" |
| 417 | "}\n"); |
| 418 | } |
| 419 | |
| 420 | DEF_TEST(SkSLHex, r) { |
| 421 | test(r, |
| 422 | "void main() {" |
| 423 | "int i1 = 0x0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 424 | "i1++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 425 | "int i2 = 0x1234abcd;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 426 | "i2++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 427 | "int i3 = 0x7fffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 428 | "i3++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 429 | "int i4 = 0xffffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 430 | "i4++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 431 | "int i5 = -0xbeef;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 432 | "i5++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 433 | "uint u1 = 0x0;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 434 | "u1++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 435 | "uint u2 = 0x1234abcd;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 436 | "u2++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 437 | "uint u3 = 0x7fffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 438 | "u3++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 439 | "uint u4 = 0xffffffff;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 440 | "u4++;" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 441 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 442 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 443 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 444 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 445 | "void main() {\n" |
| 446 | " int i1 = 0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 447 | " i1++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 448 | " int i2 = 305441741;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 449 | " i2++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 450 | " int i3 = 2147483647;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 451 | " i3++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 452 | " int i4 = -1;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 453 | " i4++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 454 | " int i5 = -48879;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 455 | " i5++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 456 | " uint u1 = 0u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 457 | " u1++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 458 | " uint u2 = 305441741u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 459 | " u2++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 460 | " uint u3 = 2147483647u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 461 | " u3++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 462 | " uint u4 = 4294967295u;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 463 | " u4++;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 464 | "}\n"); |
| 465 | } |
| 466 | |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 467 | DEF_TEST(SkSLVectorConstructors, r) { |
| 468 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 469 | "float2 v1 = float2(1);" |
| 470 | "float2 v2 = float2(1, 2);" |
| 471 | "float2 v3 = float2(float2(1));" |
| 472 | "float3 v4 = float3(float2(1), 1.0);" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 473 | "int2 v5 = int2(1);" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 474 | "int2 v6 = int2(float2(1, 2));" |
| 475 | "float2 v7 = float2(int2(1, 2));", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 476 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 477 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 478 | "out vec4 sk_FragColor;\n" |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 479 | "vec2 v1 = vec2(1.0);\n" |
| 480 | "vec2 v2 = vec2(1.0, 2.0);\n" |
| 481 | "vec2 v3 = vec2(1.0);\n" |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 482 | "vec3 v4 = vec3(vec2(1.0), 1.0);\n" |
| 483 | "ivec2 v5 = ivec2(1);\n" |
| 484 | "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n" |
| 485 | "vec2 v7 = vec2(ivec2(1, 2));\n"); |
Ethan Nicholas | 4578a8e | 2016-11-01 11:57:42 -0400 | [diff] [blame] | 486 | } |
| 487 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 488 | DEF_TEST(SkSLArrayConstructors, r) { |
| 489 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 490 | "float test1[] = float[](1, 2, 3, 4);" |
| 491 | "float2 test2[] = float2[](float2(1, 2), float2(3, 4));" |
| 492 | "float4x4 test3[] = float4x4[]();", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 493 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 494 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 495 | "out vec4 sk_FragColor;\n" |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 496 | "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" |
| 497 | "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" |
| 498 | "mat4 test3[] = mat4[]();\n"); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 499 | } |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 500 | |
| 501 | DEF_TEST(SkSLDerivatives, r) { |
| 502 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 503 | "void main() { sk_FragColor.r = dFdx(1); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 504 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 505 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 506 | "out vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 507 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 508 | " sk_FragColor.x = dFdx(1.0);\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 509 | "}\n"); |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 510 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 511 | "void main() { sk_FragColor.r = 1; }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 512 | *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 513 | "#version 400\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 514 | "precision mediump float;\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 515 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 516 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 517 | " sk_FragColor.x = 1.0;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 518 | "}\n"); |
| 519 | test(r, |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 520 | "void main() { sk_FragColor.r = dFdx(1); }", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 521 | *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(), |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 522 | "#version 400\n" |
| 523 | "#extension GL_OES_standard_derivatives : require\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 524 | "precision mediump float;\n" |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 525 | "out mediump vec4 sk_FragColor;\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 526 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 527 | " sk_FragColor.x = dFdx(1.0);\n" |
ethannicholas | ddb37d6 | 2016-10-20 09:54:00 -0700 | [diff] [blame] | 528 | "}\n"); |
| 529 | } |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 530 | |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 531 | |
| 532 | DEF_TEST(SkSLIntFolding, r) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 533 | test(r, |
| 534 | "void main() {" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 535 | "sk_FragColor.r = 32 + 2;" |
| 536 | "sk_FragColor.r = 32 - 2;" |
| 537 | "sk_FragColor.r = 32 * 2;" |
| 538 | "sk_FragColor.r = 32 / 2;" |
| 539 | "sk_FragColor.r = 12 | 6;" |
| 540 | "sk_FragColor.r = 254 & 7;" |
| 541 | "sk_FragColor.r = 2 ^ 7;" |
| 542 | "sk_FragColor.r = 1 << 4;" |
| 543 | "sk_FragColor.r = 128 >> 2;" |
| 544 | "sk_FragColor.r = -1 == -1 ? 1 : -1;" |
| 545 | "sk_FragColor.r = -1 == -2 ? 2 : -2;" |
| 546 | "sk_FragColor.r = 0 != 1 ? 3 : -3;" |
| 547 | "sk_FragColor.r = 0 != 0 ? 4 : -4;" |
| 548 | "sk_FragColor.r = 6 > 5 ? 5 : -5;" |
| 549 | "sk_FragColor.r = 6 > 6 ? 6 : -6;" |
| 550 | "sk_FragColor.r = -1 < 0 ? 7 : -7;" |
| 551 | "sk_FragColor.r = 1 < 0 ? 8 : -8;" |
| 552 | "sk_FragColor.r = 6 >= 6 ? 9 : -9;" |
| 553 | "sk_FragColor.r = 6 >= 7 ? 10 : -10;" |
| 554 | "sk_FragColor.r = 6 <= 6 ? 11 : -11;" |
| 555 | "sk_FragColor.r = 6 <= 5 ? 12 : -12;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 556 | "sk_FragColor.r = int(sqrt(1)) + 0;" |
| 557 | "sk_FragColor.r = 0 + int(sqrt(2));" |
| 558 | "sk_FragColor.r = int(sqrt(3)) - 0;" |
| 559 | "sk_FragColor.r = int(sqrt(4)) * 0;" |
| 560 | "sk_FragColor.r = int(sqrt(5)) * 1;" |
| 561 | "sk_FragColor.r = 1 * int(sqrt(6));" |
| 562 | "sk_FragColor.r = 0 * int(sqrt(7));" |
| 563 | "sk_FragColor.r = int(sqrt(8)) / 1;" |
| 564 | "sk_FragColor.r = 0 / int(sqrt(9));" |
| 565 | "int x = int(sqrt(2));" |
| 566 | "x += 1;" |
| 567 | "x += 0;" |
| 568 | "x -= 1;" |
| 569 | "x -= 0;" |
| 570 | "x *= 1;" |
| 571 | "x *= 2;" |
| 572 | "x /= 1;" |
| 573 | "x /= 2;" |
| 574 | "sk_FragColor.r = x;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 575 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 576 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 577 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 578 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 579 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 580 | " sk_FragColor.x = 34.0;\n" |
| 581 | " sk_FragColor.x = 30.0;\n" |
| 582 | " sk_FragColor.x = 64.0;\n" |
| 583 | " sk_FragColor.x = 16.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 584 | " sk_FragColor.x = 14.0;\n" |
| 585 | " sk_FragColor.x = 6.0;\n" |
| 586 | " sk_FragColor.x = 5.0;\n" |
| 587 | " sk_FragColor.x = 16.0;\n" |
| 588 | " sk_FragColor.x = 32.0;\n" |
| 589 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 590 | " sk_FragColor.x = -2.0;\n" |
| 591 | " sk_FragColor.x = 3.0;\n" |
| 592 | " sk_FragColor.x = -4.0;\n" |
| 593 | " sk_FragColor.x = 5.0;\n" |
| 594 | " sk_FragColor.x = -6.0;\n" |
| 595 | " sk_FragColor.x = 7.0;\n" |
| 596 | " sk_FragColor.x = -8.0;\n" |
| 597 | " sk_FragColor.x = 9.0;\n" |
| 598 | " sk_FragColor.x = -10.0;\n" |
| 599 | " sk_FragColor.x = 11.0;\n" |
| 600 | " sk_FragColor.x = -12.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 601 | " sk_FragColor.x = float(int(sqrt(1.0)));\n" |
| 602 | " sk_FragColor.x = float(int(sqrt(2.0)));\n" |
| 603 | " sk_FragColor.x = float(int(sqrt(3.0)));\n" |
| 604 | " sk_FragColor.x = 0.0;\n" |
| 605 | " sk_FragColor.x = float(int(sqrt(5.0)));\n" |
| 606 | " sk_FragColor.x = float(int(sqrt(6.0)));\n" |
| 607 | " sk_FragColor.x = 0.0;\n" |
| 608 | " sk_FragColor.x = float(int(sqrt(8.0)));\n" |
| 609 | " sk_FragColor.x = 0.0;\n" |
| 610 | " int x = int(sqrt(2.0));\n" |
| 611 | " x += 1;\n" |
| 612 | " x -= 1;\n" |
| 613 | " x *= 2;\n" |
| 614 | " x /= 2;\n" |
| 615 | " sk_FragColor.x = float(x);\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 616 | "}\n"); |
| 617 | } |
| 618 | |
| 619 | DEF_TEST(SkSLFloatFolding, r) { |
| 620 | test(r, |
| 621 | "void main() {" |
| 622 | "sk_FragColor.r = 32.0 + 2.0;" |
| 623 | "sk_FragColor.r = 32.0 - 2.0;" |
| 624 | "sk_FragColor.r = 32.0 * 2.0;" |
| 625 | "sk_FragColor.r = 32.0 / 2.0;" |
| 626 | "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;" |
| 627 | "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;" |
| 628 | "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;" |
| 629 | "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;" |
| 630 | "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;" |
| 631 | "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;" |
| 632 | "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;" |
| 633 | "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;" |
| 634 | "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;" |
| 635 | "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;" |
| 636 | "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;" |
| 637 | "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;" |
| 638 | "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 639 | "sk_FragColor.r = sqrt(1) + 0;" |
| 640 | "sk_FragColor.r = 0 + sqrt(2);" |
| 641 | "sk_FragColor.r = sqrt(3) - 0;" |
| 642 | "sk_FragColor.r = sqrt(4) * 0;" |
| 643 | "sk_FragColor.r = sqrt(5) * 1;" |
| 644 | "sk_FragColor.r = 1 * sqrt(6);" |
| 645 | "sk_FragColor.r = 0 * sqrt(7);" |
| 646 | "sk_FragColor.r = sqrt(8) / 1;" |
| 647 | "sk_FragColor.r = 0 / sqrt(9);" |
| 648 | "sk_FragColor.r += 1;" |
| 649 | "sk_FragColor.r += 0;" |
| 650 | "sk_FragColor.r -= 1;" |
| 651 | "sk_FragColor.r -= 0;" |
| 652 | "sk_FragColor.r *= 1;" |
| 653 | "sk_FragColor.r *= 2;" |
| 654 | "sk_FragColor.r /= 1;" |
| 655 | "sk_FragColor.r /= 2;" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 656 | "}", |
| 657 | *SkSL::ShaderCapsFactory::Default(), |
| 658 | "#version 400\n" |
| 659 | "out vec4 sk_FragColor;\n" |
| 660 | "void main() {\n" |
| 661 | " sk_FragColor.x = 34.0;\n" |
| 662 | " sk_FragColor.x = 30.0;\n" |
| 663 | " sk_FragColor.x = 64.0;\n" |
| 664 | " sk_FragColor.x = 16.0;\n" |
| 665 | " sk_FragColor.x = 19.0;\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 666 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 667 | " sk_FragColor.x = -2.0;\n" |
| 668 | " sk_FragColor.x = 3.0;\n" |
| 669 | " sk_FragColor.x = -4.0;\n" |
| 670 | " sk_FragColor.x = 5.0;\n" |
| 671 | " sk_FragColor.x = -6.0;\n" |
| 672 | " sk_FragColor.x = 7.0;\n" |
| 673 | " sk_FragColor.x = -8.0;\n" |
| 674 | " sk_FragColor.x = 9.0;\n" |
| 675 | " sk_FragColor.x = -10.0;\n" |
| 676 | " sk_FragColor.x = 11.0;\n" |
| 677 | " sk_FragColor.x = -12.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 678 | " sk_FragColor.x = sqrt(1.0);\n" |
| 679 | " sk_FragColor.x = sqrt(2.0);\n" |
| 680 | " sk_FragColor.x = sqrt(3.0);\n" |
| 681 | " sk_FragColor.x = 0.0;\n" |
| 682 | " sk_FragColor.x = sqrt(5.0);\n" |
| 683 | " sk_FragColor.x = sqrt(6.0);\n" |
| 684 | " sk_FragColor.x = 0.0;\n" |
| 685 | " sk_FragColor.x = sqrt(8.0);\n" |
| 686 | " sk_FragColor.x = 0.0;\n" |
| 687 | " sk_FragColor.x += 1.0;\n" |
| 688 | " sk_FragColor.x -= 1.0;\n" |
| 689 | " sk_FragColor.x *= 2.0;\n" |
| 690 | " sk_FragColor.x /= 2.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 691 | "}\n"); |
| 692 | } |
| 693 | |
| 694 | DEF_TEST(SkSLBoolFolding, r) { |
| 695 | test(r, |
| 696 | "void main() {" |
| 697 | "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;" |
| 698 | "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;" |
| 699 | "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;" |
| 700 | "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;" |
| 701 | "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;" |
| 702 | "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;" |
| 703 | "}", |
| 704 | *SkSL::ShaderCapsFactory::Default(), |
| 705 | "#version 400\n" |
| 706 | "out vec4 sk_FragColor;\n" |
| 707 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 708 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 709 | " sk_FragColor.x = -2.0;\n" |
| 710 | " sk_FragColor.x = 3.0;\n" |
| 711 | " sk_FragColor.x = -4.0;\n" |
| 712 | " sk_FragColor.x = 5.0;\n" |
| 713 | " sk_FragColor.x = -6.0;\n" |
| 714 | "}\n"); |
| 715 | } |
| 716 | |
| 717 | DEF_TEST(SkSLVecFolding, r) { |
| 718 | test(r, |
| 719 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 720 | "sk_FragColor.r = float4(0.5, 1, 1, 1).x;" |
| 721 | "sk_FragColor = float4(float2(1), float2(2, 3)) + float4(5, 6, 7, 8);" |
| 722 | "sk_FragColor = float4(8, float3(10)) - float4(1);" |
| 723 | "sk_FragColor = float4(2) * float4(1, 2, 3, 4);" |
| 724 | "sk_FragColor = float4(12) / float4(1, 2, 3, 4);" |
| 725 | "sk_FragColor.r = (float4(12) / float4(1, 2, 3, 4)).y;" |
| 726 | "sk_FragColor.x = float4(1) == float4(1) ? 1.0 : -1.0;" |
| 727 | "sk_FragColor.x = float4(1) == float4(2) ? 2.0 : -2.0;" |
| 728 | "sk_FragColor.x = float2(1) == float2(1, 1) ? 3.0 : -3.0;" |
| 729 | "sk_FragColor.x = float2(1, 1) == float2(1, 1) ? 4.0 : -4.0;" |
| 730 | "sk_FragColor.x = float2(1) == float2(1, 0) ? 5.0 : -5.0;" |
| 731 | "sk_FragColor.x = float4(1) == float4(float2(1), float2(1)) ? 6.0 : -6.0;" |
| 732 | "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), float2(1)) ? 7.0 : -7.0;" |
| 733 | "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), 1, 0) ? 8.0 : -8.0;" |
| 734 | "sk_FragColor.x = float2(1) != float2(1, 0) ? 9.0 : -9.0;" |
| 735 | "sk_FragColor.x = float4(1) != float4(float2(1), float2(1)) ? 10.0 : -10.0;" |
| 736 | "sk_FragColor = float4(sqrt(1)) * float4(1);" |
| 737 | "sk_FragColor = float4(1) * float4(sqrt(2));" |
| 738 | "sk_FragColor = float4(0) * float4(sqrt(3));" |
| 739 | "sk_FragColor = float4(sqrt(4)) * float4(0);" |
| 740 | "sk_FragColor = float4(0) / float4(sqrt(5));" |
| 741 | "sk_FragColor = float4(0) + float4(sqrt(6));" |
| 742 | "sk_FragColor = float4(sqrt(7)) + float4(0);" |
| 743 | "sk_FragColor = float4(sqrt(8)) - float4(0);" |
| 744 | "sk_FragColor = float4(0) + sqrt(9);" |
| 745 | "sk_FragColor = float4(0) * sqrt(10);" |
| 746 | "sk_FragColor = float4(0) / sqrt(11);" |
| 747 | "sk_FragColor = float4(1) * sqrt(12);" |
| 748 | "sk_FragColor = 0 + float4(sqrt(13));" |
| 749 | "sk_FragColor = 0 * float4(sqrt(14));" |
| 750 | "sk_FragColor = 0 / float4(sqrt(15));" |
| 751 | "sk_FragColor = 1 * float4(sqrt(16));" |
| 752 | "sk_FragColor = float4(sqrt(17)) + 0;" |
| 753 | "sk_FragColor = float4(sqrt(18)) * 0;" |
| 754 | "sk_FragColor = float4(sqrt(19)) * 1;" |
| 755 | "sk_FragColor = float4(sqrt(19.5)) - 0;" |
| 756 | "sk_FragColor = sqrt(20) * float4(1);" |
| 757 | "sk_FragColor = sqrt(21) + float4(0);" |
| 758 | "sk_FragColor = sqrt(22) - float4(0);" |
| 759 | "sk_FragColor = sqrt(23) / float4(1);" |
| 760 | "sk_FragColor = float4(sqrt(24)) / 1;" |
| 761 | "sk_FragColor += float4(1);" |
| 762 | "sk_FragColor += float4(0);" |
| 763 | "sk_FragColor -= float4(1);" |
| 764 | "sk_FragColor -= float4(0);" |
| 765 | "sk_FragColor *= float4(1);" |
| 766 | "sk_FragColor *= float4(2);" |
| 767 | "sk_FragColor /= float4(1);" |
| 768 | "sk_FragColor /= float4(2);" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 769 | "}", |
| 770 | *SkSL::ShaderCapsFactory::Default(), |
| 771 | "#version 400\n" |
| 772 | "out vec4 sk_FragColor;\n" |
| 773 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 774 | " sk_FragColor.x = 0.5;\n" |
| 775 | " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n" |
| 776 | " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n" |
| 777 | " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n" |
| 778 | " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n" |
| 779 | " sk_FragColor.x = 6.0;\n" |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 780 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 781 | " sk_FragColor.x = -2.0;\n" |
| 782 | " sk_FragColor.x = 3.0;\n" |
| 783 | " sk_FragColor.x = 4.0;\n" |
| 784 | " sk_FragColor.x = -5.0;\n" |
| 785 | " sk_FragColor.x = 6.0;\n" |
| 786 | " sk_FragColor.x = 7.0;\n" |
| 787 | " sk_FragColor.x = -8.0;\n" |
| 788 | " sk_FragColor.x = 9.0;\n" |
| 789 | " sk_FragColor.x = -10.0;\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 790 | " sk_FragColor = vec4(sqrt(1.0));\n" |
| 791 | " sk_FragColor = vec4(sqrt(2.0));\n" |
| 792 | " sk_FragColor = vec4(0.0);\n" |
| 793 | " sk_FragColor = vec4(0.0);\n" |
| 794 | " sk_FragColor = vec4(0.0);\n" |
| 795 | " sk_FragColor = vec4(sqrt(6.0));\n" |
| 796 | " sk_FragColor = vec4(sqrt(7.0));\n" |
| 797 | " sk_FragColor = vec4(sqrt(8.0));\n" |
| 798 | " sk_FragColor = vec4(sqrt(9.0));\n" |
| 799 | " sk_FragColor = vec4(0.0);\n" |
| 800 | " sk_FragColor = vec4(0.0);\n" |
| 801 | " sk_FragColor = vec4(sqrt(12.0));\n" |
| 802 | " sk_FragColor = vec4(sqrt(13.0));\n" |
| 803 | " sk_FragColor = vec4(0.0);\n" |
| 804 | " sk_FragColor = vec4(0.0);\n" |
| 805 | " sk_FragColor = vec4(sqrt(16.0));\n" |
| 806 | " sk_FragColor = vec4(sqrt(17.0));\n" |
| 807 | " sk_FragColor = vec4(0.0);\n" |
| 808 | " sk_FragColor = vec4(sqrt(19.0));\n" |
| 809 | " sk_FragColor = vec4(sqrt(19.5));\n" |
| 810 | " sk_FragColor = vec4(sqrt(20.0));\n" |
| 811 | " sk_FragColor = vec4(sqrt(21.0));\n" |
| 812 | " sk_FragColor = vec4(sqrt(22.0));\n" |
| 813 | " sk_FragColor = vec4(sqrt(23.0));\n" |
| 814 | " sk_FragColor = vec4(sqrt(24.0));\n" |
| 815 | " sk_FragColor += vec4(1.0);\n" |
| 816 | " sk_FragColor -= vec4(1.0);\n" |
| 817 | " sk_FragColor *= vec4(2.0);\n" |
| 818 | " sk_FragColor /= vec4(2.0);\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 819 | "}\n"); |
| 820 | } |
| 821 | |
| 822 | DEF_TEST(SkSLMatFolding, r) { |
| 823 | test(r, |
| 824 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 825 | "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == " |
| 826 | "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;" |
| 827 | "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == " |
| 828 | "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;" |
| 829 | "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;" |
| 830 | "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;" |
| 831 | "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;" |
| 832 | "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;" |
| 833 | "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))" |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 834 | "? 7 : -7;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 835 | "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;" |
| 836 | "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;" |
| 837 | "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), " |
| 838 | "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;" |
| 839 | "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;" |
| 840 | "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;" |
| 841 | "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;" |
| 842 | "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 843 | "}", |
| 844 | *SkSL::ShaderCapsFactory::Default(), |
| 845 | "#version 400\n" |
| 846 | "out vec4 sk_FragColor;\n" |
| 847 | "void main() {\n" |
Ethan Nicholas | 3deaeb2 | 2017-04-25 14:42:11 -0400 | [diff] [blame] | 848 | " sk_FragColor.x = 1.0;\n" |
Ethan Nicholas | c81d22f | 2017-04-26 11:25:18 -0400 | [diff] [blame] | 849 | " sk_FragColor.x = -2.0;\n" |
| 850 | " sk_FragColor.x = 3.0;\n" |
| 851 | " sk_FragColor.x = -4.0;\n" |
| 852 | " sk_FragColor.x = 5.0;\n" |
| 853 | " sk_FragColor.x = -6.0;\n" |
| 854 | " sk_FragColor.x = 7.0;\n" |
| 855 | " sk_FragColor.x = -8.0;\n" |
| 856 | " sk_FragColor.x = 9.0;\n" |
| 857 | " sk_FragColor.x = 10.0;\n" |
| 858 | " sk_FragColor.x = 11.0;\n" |
| 859 | " sk_FragColor.x = 12.0;\n" |
| 860 | " sk_FragColor.x = 13.0;\n" |
| 861 | " sk_FragColor.x = 14.0;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 862 | "}\n"); |
| 863 | } |
| 864 | |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 865 | DEF_TEST(SkSLConstantIf, r) { |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 866 | test(r, |
| 867 | "void main() {" |
| 868 | "int x;" |
| 869 | "if (true) x = 1;" |
| 870 | "if (2 > 1) x = 2; else x = 3;" |
| 871 | "if (1 > 2) x = 4; else x = 5;" |
| 872 | "if (false) x = 6;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 873 | "sk_FragColor.r = x;" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 874 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 875 | *SkSL::ShaderCapsFactory::Default(), |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 876 | "#version 400\n" |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 877 | "out vec4 sk_FragColor;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 878 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 879 | " sk_FragColor.x = 5.0;\n" |
ethannicholas | 08a9211 | 2016-11-09 13:26:45 -0800 | [diff] [blame] | 880 | "}\n"); |
| 881 | } |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 882 | |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 883 | DEF_TEST(SkSLCaps, r) { |
| 884 | test(r, |
| 885 | "void main() {" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 886 | "int x = 0;" |
| 887 | "int y = 0;" |
| 888 | "int z = 0;" |
| 889 | "int w = 0;" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 890 | "if (sk_Caps.externalTextureSupport) x = 1;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 891 | "if (sk_Caps.fbFetchSupport) y = 1;" |
| 892 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;" |
| 893 | "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 894 | "sk_FragColor = half4(x, y, z, w);" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 895 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 896 | *SkSL::ShaderCapsFactory::VariousCaps(), |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 897 | "#version 400\n" |
| 898 | "out vec4 sk_FragColor;\n" |
| 899 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 900 | " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n" |
Ethan Nicholas | 3605ace | 2016-11-21 15:59:48 -0500 | [diff] [blame] | 901 | "}\n"); |
| 902 | } |
| 903 | |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 904 | DEF_TEST(SkSLTexture, r) { |
| 905 | test(r, |
| 906 | "uniform sampler1D one;" |
| 907 | "uniform sampler2D two;" |
| 908 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 909 | "float4 a = texture(one, 0);" |
| 910 | "float4 b = texture(two, float2(0));" |
| 911 | "float4 c = texture(one, float2(0));" |
| 912 | "float4 d = texture(two, float3(0));" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 913 | "sk_FragColor = half4(a.x, b.x, c.x, d.x);" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 914 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 915 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 916 | "#version 400\n" |
| 917 | "out vec4 sk_FragColor;\n" |
| 918 | "uniform sampler1D one;\n" |
| 919 | "uniform sampler2D two;\n" |
| 920 | "void main() {\n" |
| 921 | " vec4 a = texture(one, 0.0);\n" |
| 922 | " vec4 b = texture(two, vec2(0.0));\n" |
| 923 | " vec4 c = textureProj(one, vec2(0.0));\n" |
| 924 | " vec4 d = textureProj(two, vec3(0.0));\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 925 | " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 926 | "}\n"); |
| 927 | test(r, |
| 928 | "uniform sampler1D one;" |
| 929 | "uniform sampler2D two;" |
| 930 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 931 | "float4 a = texture(one, 0);" |
| 932 | "float4 b = texture(two, float2(0));" |
| 933 | "float4 c = texture(one, float2(0));" |
| 934 | "float4 d = texture(two, float3(0));" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 935 | "sk_FragColor = half4(a.x, b.x, c.x, d.x);" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 936 | "}", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 937 | *SkSL::ShaderCapsFactory::Version110(), |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 938 | "#version 110\n" |
| 939 | "uniform sampler1D one;\n" |
| 940 | "uniform sampler2D two;\n" |
| 941 | "void main() {\n" |
| 942 | " vec4 a = texture1D(one, 0.0);\n" |
| 943 | " vec4 b = texture2D(two, vec2(0.0));\n" |
| 944 | " vec4 c = texture1DProj(one, vec2(0.0));\n" |
| 945 | " vec4 d = texture2DProj(two, vec3(0.0));\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 946 | " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n" |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 947 | "}\n"); |
| 948 | } |
| 949 | |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 950 | DEF_TEST(SkSLOffset, r) { |
| 951 | test(r, |
| 952 | "struct Test {" |
| 953 | "layout(offset = 0) int x;" |
| 954 | "layout(offset = 4) int y;" |
| 955 | "int z;" |
| 956 | "} test;", |
Brian Salomon | f1dd677 | 2016-11-29 15:27:52 -0500 | [diff] [blame] | 957 | *SkSL::ShaderCapsFactory::Default(), |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 958 | "#version 400\n" |
| 959 | "out vec4 sk_FragColor;\n" |
| 960 | "struct Test {\n" |
| 961 | " layout (offset = 0) int x;\n" |
| 962 | " layout (offset = 4) int y;\n" |
| 963 | " int z;\n" |
| 964 | "} test;\n"); |
| 965 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 966 | |
| 967 | DEF_TEST(SkSLFragCoord, r) { |
| 968 | SkSL::Program::Settings settings; |
| 969 | settings.fFlipY = true; |
| 970 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld(); |
| 971 | settings.fCaps = caps.get(); |
| 972 | SkSL::Program::Inputs inputs; |
| 973 | test(r, |
| 974 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 975 | settings, |
| 976 | "#version 110\n" |
| 977 | "#extension GL_ARB_fragment_coord_conventions : require\n" |
| 978 | "layout(origin_upper_left) in vec4 gl_FragCoord;\n" |
| 979 | "void main() {\n" |
| 980 | " gl_FragColor.xy = gl_FragCoord.xy;\n" |
| 981 | "}\n", |
| 982 | &inputs); |
| 983 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 984 | |
| 985 | caps = SkSL::ShaderCapsFactory::FragCoordsNew(); |
| 986 | settings.fCaps = caps.get(); |
| 987 | test(r, |
| 988 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 989 | settings, |
| 990 | "#version 400\n" |
| 991 | "layout(origin_upper_left) in vec4 gl_FragCoord;\n" |
| 992 | "out vec4 sk_FragColor;\n" |
| 993 | "void main() {\n" |
| 994 | " sk_FragColor.xy = gl_FragCoord.xy;\n" |
| 995 | "}\n", |
| 996 | &inputs); |
| 997 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 998 | |
| 999 | caps = SkSL::ShaderCapsFactory::Default(); |
| 1000 | settings.fCaps = caps.get(); |
| 1001 | test(r, |
| 1002 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 1003 | settings, |
| 1004 | "#version 400\n" |
| 1005 | "uniform float u_skRTHeight;\n" |
| 1006 | "out vec4 sk_FragColor;\n" |
| 1007 | "void main() {\n" |
| 1008 | " vec2 _sktmpCoord = gl_FragCoord.xy;\n" |
| 1009 | " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n" |
| 1010 | " sk_FragColor.xy = sk_FragCoord.xy;\n" |
| 1011 | "}\n", |
| 1012 | &inputs); |
| 1013 | REPORTER_ASSERT(r, inputs.fRTHeight); |
| 1014 | |
| 1015 | settings.fFlipY = false; |
| 1016 | test(r, |
| 1017 | "void main() { sk_FragColor.xy = sk_FragCoord.xy; }", |
| 1018 | settings, |
| 1019 | "#version 400\n" |
| 1020 | "out vec4 sk_FragColor;\n" |
| 1021 | "void main() {\n" |
| 1022 | " sk_FragColor.xy = gl_FragCoord.xy;\n" |
| 1023 | "}\n", |
| 1024 | &inputs); |
| 1025 | REPORTER_ASSERT(r, !inputs.fRTHeight); |
| 1026 | } |
| 1027 | |
Ethan Nicholas | a51740c | 2017-02-07 14:53:32 -0500 | [diff] [blame] | 1028 | DEF_TEST(SkSLVertexID, r) { |
| 1029 | test(r, |
| 1030 | "out int id; void main() { id = sk_VertexID; }", |
| 1031 | *SkSL::ShaderCapsFactory::Default(), |
| 1032 | "#version 400\n" |
| 1033 | "out int id;\n" |
| 1034 | "void main() {\n" |
| 1035 | " id = gl_VertexID;\n" |
| 1036 | "}\n", |
| 1037 | SkSL::Program::kVertex_Kind); |
| 1038 | } |
| 1039 | |
Chris Dalton | 8580d51 | 2017-10-14 22:12:33 -0600 | [diff] [blame] | 1040 | DEF_TEST(SkSLInstanceID, r) { |
| 1041 | test(r, |
| 1042 | "out int id; void main() { id = sk_InstanceID; }", |
| 1043 | *SkSL::ShaderCapsFactory::Default(), |
| 1044 | "#version 400\n" |
| 1045 | "out int id;\n" |
| 1046 | "void main() {\n" |
| 1047 | " id = gl_InstanceID;\n" |
| 1048 | "}\n", |
| 1049 | SkSL::Program::kVertex_Kind); |
| 1050 | } |
| 1051 | |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 1052 | DEF_TEST(SkSLClipDistance, r) { |
| 1053 | test(r, |
| 1054 | "void main() { sk_ClipDistance[0] = 0; }", |
| 1055 | *SkSL::ShaderCapsFactory::Default(), |
| 1056 | "#version 400\n" |
| 1057 | "void main() {\n" |
| 1058 | " gl_ClipDistance[0] = 0.0;\n" |
| 1059 | "}\n", |
| 1060 | SkSL::Program::kVertex_Kind); |
| 1061 | test(r, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1062 | "void main() { sk_FragColor = half4(sk_ClipDistance[0]); }", |
Ethan Nicholas | 67d6460 | 2017-02-09 10:15:25 -0500 | [diff] [blame] | 1063 | *SkSL::ShaderCapsFactory::Default(), |
| 1064 | "#version 400\n" |
| 1065 | "out vec4 sk_FragColor;\n" |
| 1066 | "void main() {\n" |
| 1067 | " sk_FragColor = vec4(gl_ClipDistance[0]);\n" |
| 1068 | "}\n"); |
| 1069 | } |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1070 | |
| 1071 | DEF_TEST(SkSLArrayTypes, r) { |
| 1072 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1073 | "void main() { float2 x[2] = float2[2](float2(1), float2(2));" |
| 1074 | "float2[2] y = float2[2](float2(3), float2(4));" |
| 1075 | "sk_FragColor = float4(x[0], y[1]); }", |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1076 | *SkSL::ShaderCapsFactory::Default(), |
| 1077 | "#version 400\n" |
| 1078 | "out vec4 sk_FragColor;\n" |
| 1079 | "void main() {\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1080 | " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], " |
| 1081 | "vec2[2](vec2(3.0), vec2(4.0))[1]);\n" |
Ethan Nicholas | 50afc17 | 2017-02-16 14:49:57 -0500 | [diff] [blame] | 1082 | "}\n"); |
| 1083 | } |
| 1084 | |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1085 | DEF_TEST(SkSLGeometry, r) { |
| 1086 | test(r, |
| 1087 | "layout(points) in;" |
| 1088 | "layout(invocations = 2) in;" |
| 1089 | "layout(line_strip, max_vertices = 2) out;" |
| 1090 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 1091 | "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1092 | "EmitVertex();" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 1093 | "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);" |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 1094 | "EmitVertex();" |
| 1095 | "EndPrimitive();" |
| 1096 | "}", |
| 1097 | *SkSL::ShaderCapsFactory::Default(), |
| 1098 | "#version 400\n" |
| 1099 | "layout (points) in ;\n" |
| 1100 | "layout (invocations = 2) in ;\n" |
| 1101 | "layout (line_strip, max_vertices = 2) out ;\n" |
| 1102 | "void main() {\n" |
| 1103 | " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" |
| 1104 | " EmitVertex();\n" |
| 1105 | " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n" |
| 1106 | " EmitVertex();\n" |
| 1107 | " EndPrimitive();\n" |
| 1108 | "}\n", |
| 1109 | SkSL::Program::kGeometry_Kind); |
| 1110 | } |
| 1111 | |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1112 | DEF_TEST(SkSLSwitch, r) { |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1113 | // basic "does a switch even work" test |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1114 | test(r, |
| 1115 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1116 | " float x;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1117 | " switch (int(sqrt(1))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1118 | " case 0:" |
| 1119 | " x = 0.0;" |
| 1120 | " break;" |
| 1121 | " case 1:" |
| 1122 | " x = 1.0;" |
| 1123 | " break;" |
| 1124 | " default:" |
| 1125 | " x = 2.0;" |
| 1126 | " }" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1127 | " sk_FragColor = float4(x);" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1128 | "}", |
| 1129 | *SkSL::ShaderCapsFactory::Default(), |
| 1130 | "#version 400\n" |
| 1131 | "out vec4 sk_FragColor;\n" |
| 1132 | "void main() {\n" |
| 1133 | " float x;\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1134 | " switch (int(sqrt(1.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1135 | " case 0:\n" |
| 1136 | " x = 0.0;\n" |
| 1137 | " break;\n" |
| 1138 | " case 1:\n" |
| 1139 | " x = 1.0;\n" |
| 1140 | " break;\n" |
| 1141 | " default:\n" |
| 1142 | " x = 2.0;\n" |
| 1143 | " }\n" |
| 1144 | " sk_FragColor = vec4(x);\n" |
| 1145 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1146 | // dead code inside of switch |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1147 | test(r, |
| 1148 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1149 | " float x;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1150 | " switch (int(sqrt(2))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1151 | " case 0:" |
| 1152 | " x = 0.0;" |
| 1153 | " case 1:" |
| 1154 | " x = 1.0;" |
| 1155 | " default:" |
| 1156 | " x = 2.0;" |
| 1157 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1158 | " sk_FragColor = half4(x);" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1159 | "}", |
| 1160 | *SkSL::ShaderCapsFactory::Default(), |
| 1161 | "#version 400\n" |
| 1162 | "out vec4 sk_FragColor;\n" |
| 1163 | "void main() {\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1164 | " switch (int(sqrt(2.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1165 | " case 0:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1166 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1167 | " case 1:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1168 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1169 | " default:\n" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1170 | " ;\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1171 | " }\n" |
| 1172 | " sk_FragColor = vec4(2.0);\n" |
| 1173 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1174 | // non-static test w/ fallthrough |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1175 | test(r, |
| 1176 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1177 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1178 | " switch (int(sqrt(3))) {" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1179 | " case 0:" |
| 1180 | " x = 0.0;" |
| 1181 | " case 1:" |
| 1182 | " x = 1.0;" |
| 1183 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1184 | " sk_FragColor = half4(x);" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1185 | "}", |
| 1186 | *SkSL::ShaderCapsFactory::Default(), |
| 1187 | "#version 400\n" |
| 1188 | "out vec4 sk_FragColor;\n" |
| 1189 | "void main() {\n" |
| 1190 | " float x = 0.0;\n" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1191 | " switch (int(sqrt(3.0))) {\n" |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1192 | " case 0:\n" |
| 1193 | " x = 0.0;\n" |
| 1194 | " case 1:\n" |
| 1195 | " x = 1.0;\n" |
| 1196 | " }\n" |
| 1197 | " sk_FragColor = vec4(x);\n" |
| 1198 | "}\n"); |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1199 | // static test w/ fallthrough |
| 1200 | test(r, |
| 1201 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1202 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1203 | " switch (0) {" |
| 1204 | " case 0:" |
| 1205 | " x = 0.0;" |
| 1206 | " case 1:" |
| 1207 | " x = 1.0;" |
| 1208 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1209 | " sk_FragColor = half4(x);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1210 | "}", |
| 1211 | *SkSL::ShaderCapsFactory::Default(), |
| 1212 | "#version 400\n" |
| 1213 | "out vec4 sk_FragColor;\n" |
| 1214 | "void main() {\n" |
| 1215 | " sk_FragColor = vec4(1.0);\n" |
| 1216 | "}\n"); |
| 1217 | // static test w/ fallthrough, different entry point |
| 1218 | test(r, |
| 1219 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1220 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1221 | " switch (1) {" |
| 1222 | " case 0:" |
| 1223 | " x = 0.0;" |
| 1224 | " case 1:" |
| 1225 | " x = 1.0;" |
| 1226 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1227 | " sk_FragColor = half4(x);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1228 | "}", |
| 1229 | *SkSL::ShaderCapsFactory::Default(), |
| 1230 | "#version 400\n" |
| 1231 | "out vec4 sk_FragColor;\n" |
| 1232 | "void main() {\n" |
| 1233 | " sk_FragColor = vec4(1.0);\n" |
| 1234 | "}\n"); |
| 1235 | // static test w/ break |
| 1236 | test(r, |
| 1237 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1238 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1239 | " switch (0) {" |
| 1240 | " case 0:" |
| 1241 | " x = 0.0;" |
| 1242 | " break;" |
| 1243 | " case 1:" |
| 1244 | " x = 1.0;" |
| 1245 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1246 | " sk_FragColor = half4(x);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1247 | "}", |
| 1248 | *SkSL::ShaderCapsFactory::Default(), |
| 1249 | "#version 400\n" |
| 1250 | "out vec4 sk_FragColor;\n" |
| 1251 | "void main() {\n" |
| 1252 | " sk_FragColor = vec4(0.0);\n" |
| 1253 | "}\n"); |
| 1254 | // static test w/ static conditional break |
| 1255 | test(r, |
| 1256 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1257 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1258 | " switch (0) {" |
| 1259 | " case 0:" |
| 1260 | " x = 0.0;" |
| 1261 | " if (x < 1) break;" |
| 1262 | " case 1:" |
| 1263 | " x = 1.0;" |
| 1264 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1265 | " sk_FragColor = half4(x);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1266 | "}", |
| 1267 | *SkSL::ShaderCapsFactory::Default(), |
| 1268 | "#version 400\n" |
| 1269 | "out vec4 sk_FragColor;\n" |
| 1270 | "void main() {\n" |
| 1271 | " sk_FragColor = vec4(0.0);\n" |
| 1272 | "}\n"); |
| 1273 | // static test w/ non-static conditional break |
| 1274 | test(r, |
| 1275 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1276 | " float x = 0.0;" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1277 | " switch (0) {" |
| 1278 | " case 0:" |
| 1279 | " x = 0.0;" |
| 1280 | " if (x < sqrt(1)) break;" |
| 1281 | " case 1:" |
| 1282 | " x = 1.0;" |
| 1283 | " }" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1284 | " sk_FragColor = half4(x);" |
Ethan Nicholas | 5ac13c2 | 2017-05-10 15:06:17 -0400 | [diff] [blame] | 1285 | "}", |
| 1286 | *SkSL::ShaderCapsFactory::Default(), |
| 1287 | "#version 400\n" |
| 1288 | "out vec4 sk_FragColor;\n" |
| 1289 | "void main() {\n" |
| 1290 | " float x = 0.0;\n" |
| 1291 | " switch (0) {\n" |
| 1292 | " case 0:\n" |
| 1293 | " x = 0.0;\n" |
| 1294 | " if (0.0 < sqrt(1.0)) break;\n" |
| 1295 | " case 1:\n" |
| 1296 | " x = 1.0;\n" |
| 1297 | " }\n" |
| 1298 | " sk_FragColor = vec4(x);\n" |
| 1299 | "}\n"); |
Ethan Nicholas | af19769 | 2017-02-27 13:26:45 -0500 | [diff] [blame] | 1300 | } |
| 1301 | |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1302 | DEF_TEST(SkSLRectangleTexture, r) { |
| 1303 | test(r, |
| 1304 | "uniform sampler2D test;" |
| 1305 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1306 | " sk_FragColor = texture(test, float2(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1307 | "}", |
| 1308 | *SkSL::ShaderCapsFactory::Default(), |
| 1309 | "#version 400\n" |
| 1310 | "out vec4 sk_FragColor;\n" |
| 1311 | "uniform sampler2D test;\n" |
| 1312 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1313 | " sk_FragColor = texture(test, vec2(0.5));\n" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1314 | "}\n"); |
| 1315 | test(r, |
| 1316 | "uniform sampler2DRect test;" |
| 1317 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1318 | " sk_FragColor = texture(test, float2(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1319 | "}", |
| 1320 | *SkSL::ShaderCapsFactory::Default(), |
| 1321 | "#version 400\n" |
| 1322 | "out vec4 sk_FragColor;\n" |
| 1323 | "uniform sampler2DRect test;\n" |
| 1324 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1325 | " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1326 | "}\n"); |
| 1327 | test(r, |
| 1328 | "uniform sampler2DRect test;" |
| 1329 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1330 | " sk_FragColor = texture(test, float3(0.5));" |
Ethan Nicholas | 5338f99 | 2017-04-19 15:54:07 -0400 | [diff] [blame] | 1331 | "}", |
| 1332 | *SkSL::ShaderCapsFactory::Default(), |
| 1333 | "#version 400\n" |
| 1334 | "out vec4 sk_FragColor;\n" |
| 1335 | "uniform sampler2DRect test;\n" |
| 1336 | "void main() {\n" |
Ethan Nicholas | fe53e58 | 2017-04-27 16:24:51 -0400 | [diff] [blame] | 1337 | " 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] | 1338 | "}\n"); |
| 1339 | } |
| 1340 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1341 | DEF_TEST(SkSLUnusedVars, r) { |
| 1342 | test(r, |
| 1343 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1344 | "float a = 1, b = 2, c = 3;" |
| 1345 | "float d = c;" |
| 1346 | "float e = d;" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1347 | "b++;" |
| 1348 | "d++;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1349 | "sk_FragColor = float4(b, b, d, d);" |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 1350 | "}", |
| 1351 | *SkSL::ShaderCapsFactory::Default(), |
| 1352 | "#version 400\n" |
| 1353 | "out vec4 sk_FragColor;\n" |
| 1354 | "void main() {\n" |
| 1355 | " float b = 2.0;\n" |
| 1356 | " float d = 3.0;\n" |
| 1357 | " b++;\n" |
| 1358 | " d++;\n" |
| 1359 | " sk_FragColor = vec4(b, b, d, d);\n" |
| 1360 | "}\n"); |
| 1361 | } |
| 1362 | |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 1363 | DEF_TEST(SkSLMultipleAssignments, r) { |
| 1364 | test(r, |
| 1365 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1366 | "float x;" |
| 1367 | "float y;" |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 1368 | "int z;" |
| 1369 | "x = y = z = 1;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1370 | "sk_FragColor = float4(z);" |
Ethan Nicholas | c2371a4 | 2017-05-05 10:04:06 -0400 | [diff] [blame] | 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 | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1380 | DEF_TEST(SkSLComplexDelete, r) { |
| 1381 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1382 | "uniform float4x4 colorXform;" |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1383 | "uniform sampler2D sampler;" |
| 1384 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1385 | "float4 tmpColor;" |
| 1386 | "sk_FragColor = float4(1.0) * (tmpColor = texture(sampler, float2(1)) , " |
| 1387 | "colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * " |
| 1388 | "float4(tmpColor.xyz, 1.0)).xyz, " |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1389 | "0.0, tmpColor.w), tmpColor.w) : tmpColor);" |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1390 | "}", |
| 1391 | *SkSL::ShaderCapsFactory::Default(), |
| 1392 | "#version 400\n" |
| 1393 | "out vec4 sk_FragColor;\n" |
| 1394 | "uniform mat4 colorXform;\n" |
| 1395 | "uniform sampler2D sampler;\n" |
| 1396 | "void main() {\n" |
| 1397 | " vec4 tmpColor;\n" |
| 1398 | " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? " |
| 1399 | "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : " |
| 1400 | "tmpColor);\n" |
| 1401 | "}\n"); |
Ethan Nicholas | 4b330df | 2017-05-17 10:52:55 -0400 | [diff] [blame] | 1402 | } |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1403 | |
| 1404 | DEF_TEST(SkSLDependentInitializers, r) { |
| 1405 | test(r, |
| 1406 | "void main() {" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1407 | "float x = 0.5, y = x * 2;" |
| 1408 | "sk_FragColor = float4(y);" |
Ethan Nicholas | b4dc419 | 2017-06-02 10:16:28 -0400 | [diff] [blame] | 1409 | "}", |
| 1410 | *SkSL::ShaderCapsFactory::Default(), |
| 1411 | "#version 400\n" |
| 1412 | "out vec4 sk_FragColor;\n" |
| 1413 | "void main() {\n" |
| 1414 | " sk_FragColor = vec4(1.0);\n" |
| 1415 | "}\n"); |
| 1416 | } |
| 1417 | |
Ethan Nicholas | b310fd5 | 2017-06-09 13:46:34 -0400 | [diff] [blame] | 1418 | DEF_TEST(SkSLDeadLoopVar, r) { |
| 1419 | test(r, |
| 1420 | "void main() {" |
| 1421 | "for (int x = 0; x < 4; ) {" |
| 1422 | "break;" |
| 1423 | "}" |
| 1424 | "}", |
| 1425 | *SkSL::ShaderCapsFactory::Default(), |
| 1426 | "#version 400\n" |
| 1427 | "out vec4 sk_FragColor;\n" |
| 1428 | "void main() {\n" |
| 1429 | " for (; true; ) {\n" |
| 1430 | " break;\n" |
| 1431 | " }\n" |
| 1432 | "}\n" |
| 1433 | ); |
| 1434 | } |
| 1435 | |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1436 | DEF_TEST(SkSLInvocations, r) { |
| 1437 | test(r, |
| 1438 | "layout(points) in;" |
| 1439 | "layout(invocations = 2) in;" |
| 1440 | "layout(line_strip, max_vertices = 2) out;" |
| 1441 | "void test() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 1442 | "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);" |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1443 | "EmitVertex();" |
| 1444 | "}" |
| 1445 | "void main() {" |
Ethan Nicholas | bed683a | 2017-09-26 14:23:59 -0400 | [diff] [blame] | 1446 | "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1447 | "EmitVertex();" |
| 1448 | "}", |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1449 | *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(), |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1450 | "#version 400\n" |
| 1451 | "int sk_InvocationID;\n" |
| 1452 | "layout (points) in ;\n" |
| 1453 | "layout (line_strip, max_vertices = 4) out ;\n" |
| 1454 | "void test() {\n" |
| 1455 | " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n" |
| 1456 | " EmitVertex();\n" |
| 1457 | "}\n" |
| 1458 | "void _invoke() {\n" |
| 1459 | " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n" |
| 1460 | " EmitVertex();\n" |
| 1461 | "}\n" |
| 1462 | "void main() {\n" |
| 1463 | " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n" |
| 1464 | " _invoke();\n" |
| 1465 | " EndPrimitive();\n" |
| 1466 | " }\n" |
| 1467 | "}\n", |
| 1468 | SkSL::Program::kGeometry_Kind); |
Chris Dalton | f1b47bb | 2017-10-06 11:57:51 -0600 | [diff] [blame] | 1469 | test(r, |
| 1470 | "layout(points, invocations = 2) in;" |
| 1471 | "layout(invocations = 3) in;" |
| 1472 | "layout(line_strip, max_vertices = 2) out;" |
| 1473 | "void main() {" |
| 1474 | "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);" |
| 1475 | "EmitVertex();" |
| 1476 | "EndPrimitive();" |
| 1477 | "}", |
| 1478 | *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(), |
| 1479 | "#version 400\n" |
| 1480 | "#extension GL_ARB_gpu_shader5 : require\n" |
| 1481 | "layout (points, invocations = 2) in ;\n" |
| 1482 | "layout (invocations = 3) in ;\n" |
| 1483 | "layout (line_strip, max_vertices = 2) out ;\n" |
| 1484 | "void main() {\n" |
| 1485 | " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n" |
| 1486 | " EmitVertex();\n" |
| 1487 | " EndPrimitive();\n" |
| 1488 | "}\n", |
| 1489 | SkSL::Program::kGeometry_Kind); |
Ethan Nicholas | 7da6dfa | 2017-06-21 11:25:18 -0400 | [diff] [blame] | 1490 | } |
| 1491 | |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1492 | DEF_TEST(SkSLTypePrecision, r) { |
| 1493 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1494 | "float f = 1;" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1495 | "half h = 2;" |
| 1496 | "double d = 3;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1497 | "float2 f2 = float2(1, 2);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1498 | "half3 h3 = half3(1, 2, 3);" |
| 1499 | "double4 d4 = double4(1, 2, 3, 4);" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1500 | "float2x2 f22 = float2x2(1, 2, 3, 4);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1501 | "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);" |
| 1502 | "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);", |
| 1503 | *SkSL::ShaderCapsFactory::Default(), |
| 1504 | "#version 400\n" |
| 1505 | "out vec4 sk_FragColor;\n" |
| 1506 | "float f = 1.0;\n" |
| 1507 | "float h = 2.0;\n" |
| 1508 | "double d = 3.0;\n" |
| 1509 | "vec2 f2 = vec2(1.0, 2.0);\n" |
| 1510 | "vec3 h3 = vec3(1.0, 2.0, 3.0);\n" |
| 1511 | "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n" |
| 1512 | "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n" |
| 1513 | "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n" |
| 1514 | "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"); |
| 1515 | test(r, |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1516 | "float f = 1;" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1517 | "half h = 2;" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1518 | "float2 f2 = float2(1, 2);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1519 | "half3 h3 = half3(1, 2, 3);" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1520 | "float2x2 f22 = float2x2(1, 2, 3, 4);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1521 | "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);", |
| 1522 | *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), |
| 1523 | "#version 400\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1524 | "precision mediump float;\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1525 | "out mediump vec4 sk_FragColor;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1526 | "highp float f = 1.0;\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1527 | "mediump float h = 2.0;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1528 | "highp vec2 f2 = vec2(1.0, 2.0);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1529 | "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1530 | "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1531 | "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"); |
| 1532 | } |
| 1533 | |
| 1534 | DEF_TEST(SkSLNumberConversions, r) { |
| 1535 | test(r, |
| 1536 | "short s = short(sqrt(1));" |
| 1537 | "int i = int(sqrt(1));" |
| 1538 | "ushort us = ushort(sqrt(1));" |
| 1539 | "uint ui = uint(sqrt(1));" |
| 1540 | "half h = sqrt(1);" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1541 | "float f = sqrt(1);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1542 | "short s2s = s;" |
| 1543 | "short i2s = i;" |
| 1544 | "short us2s = short(us);" |
| 1545 | "short ui2s = short(ui);" |
| 1546 | "short h2s = short(h);" |
| 1547 | "short f2s = short(f);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1548 | "int s2i = s;" |
| 1549 | "int i2i = i;" |
| 1550 | "int us2i = int(us);" |
| 1551 | "int ui2i = int(ui);" |
| 1552 | "int h2i = int(h);" |
| 1553 | "int f2i = int(f);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1554 | "ushort s2us = ushort(s);" |
| 1555 | "ushort i2us = ushort(i);" |
| 1556 | "ushort us2us = us;" |
| 1557 | "ushort ui2us = ui;" |
| 1558 | "ushort h2us = ushort(h);" |
| 1559 | "ushort f2us = ushort(f);" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1560 | "uint s2ui = uint(s);" |
| 1561 | "uint i2ui = uint(i);" |
| 1562 | "uint us2ui = us;" |
| 1563 | "uint ui2ui = ui;" |
| 1564 | "uint h2ui = uint(h);" |
| 1565 | "uint f2ui = uint(f);" |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 1566 | "float s2f = s;" |
| 1567 | "float i2f = i;" |
| 1568 | "float us2f = us;" |
| 1569 | "float ui2f = ui;" |
| 1570 | "float h2f = h;" |
| 1571 | "float f2f = f;", |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1572 | *SkSL::ShaderCapsFactory::Default(), |
| 1573 | "#version 400\n" |
| 1574 | "out vec4 sk_FragColor;\n" |
| 1575 | "int s = int(sqrt(1.0));\n" |
| 1576 | "int i = int(sqrt(1.0));\n" |
| 1577 | "uint us = uint(sqrt(1.0));\n" |
| 1578 | "uint ui = uint(sqrt(1.0));\n" |
| 1579 | "float h = sqrt(1.0);\n" |
| 1580 | "float f = sqrt(1.0);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1581 | "int s2s = s;\n" |
| 1582 | "int i2s = i;\n" |
| 1583 | "int us2s = int(us);\n" |
| 1584 | "int ui2s = int(ui);\n" |
| 1585 | "int h2s = int(h);\n" |
| 1586 | "int f2s = int(f);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1587 | "int s2i = s;\n" |
| 1588 | "int i2i = i;\n" |
| 1589 | "int us2i = int(us);\n" |
| 1590 | "int ui2i = int(ui);\n" |
| 1591 | "int h2i = int(h);\n" |
| 1592 | "int f2i = int(f);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1593 | "uint s2us = uint(s);\n" |
| 1594 | "uint i2us = uint(i);\n" |
| 1595 | "uint us2us = us;\n" |
| 1596 | "uint ui2us = ui;\n" |
| 1597 | "uint h2us = uint(h);\n" |
| 1598 | "uint f2us = uint(f);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1599 | "uint s2ui = uint(s);\n" |
| 1600 | "uint i2ui = uint(i);\n" |
| 1601 | "uint us2ui = us;\n" |
| 1602 | "uint ui2ui = ui;\n" |
| 1603 | "uint h2ui = uint(h);\n" |
| 1604 | "uint f2ui = uint(f);\n" |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1605 | "float s2f = float(s);\n" |
| 1606 | "float i2f = float(i);\n" |
| 1607 | "float us2f = float(us);\n" |
| 1608 | "float ui2f = float(ui);\n" |
| 1609 | "float h2f = h;\n" |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1610 | "float f2f = f;\n"); |
| 1611 | } |
| 1612 | |
| 1613 | DEF_TEST(SkSLForceHighPrecision, r) { |
| 1614 | test(r, |
| 1615 | "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }", |
| 1616 | *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(), |
| 1617 | "#version 400\n" |
| 1618 | "precision mediump float;\n" |
| 1619 | "out mediump vec4 sk_FragColor;\n" |
| 1620 | "void main() {\n" |
| 1621 | " mediump float x = sqrt(1.0);\n" |
| 1622 | " mediump vec4 y = vec4(x);\n" |
| 1623 | " sk_FragColor = y;\n" |
| 1624 | "}\n"); |
| 1625 | SkSL::Program::Settings settings; |
| 1626 | settings.fForceHighPrecision = true; |
| 1627 | sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers(); |
| 1628 | settings.fCaps = caps.get(); |
| 1629 | SkSL::Program::Inputs inputs; |
| 1630 | test(r, |
| 1631 | "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }", |
| 1632 | settings, |
| 1633 | "#version 400\n" |
| 1634 | "precision mediump float;\n" |
| 1635 | "out mediump vec4 sk_FragColor;\n" |
| 1636 | "void main() {\n" |
| 1637 | " highp float x = sqrt(1.0);\n" |
| 1638 | " highp vec4 y = vec4(x);\n" |
| 1639 | " sk_FragColor = y;\n" |
| 1640 | "}\n", |
| 1641 | &inputs); |
Ethan Nicholas | dcba08e | 2017-08-02 10:52:54 -0400 | [diff] [blame] | 1642 | } |
| 1643 | |
Ethan Nicholas | 7ef4b74 | 2016-11-11 15:16:46 -0500 | [diff] [blame] | 1644 | #endif |