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