blob: cea69497f7229beb5f1ca7b8d03d20768168777b [file] [log] [blame]
ethannicholasf789b382016-08-03 12:43:36 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLCompiler.h"
ethannicholasf789b382016-08-03 12:43:36 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "tests/Test.h"
ethannicholasf789b382016-08-03 12:43:36 -070011
Ethan Nicholascb670962017-04-20 19:31:52 -040012// Note that the optimizer will aggressively kill dead code and substitute constants in place of
13// variables, so we have to jump through a few hoops to ensure that the code in these tests has the
14// necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being
15// smart enough to optimize around certain constructs; as the optimizer gets smarter it will
16// undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is
17// equivalent!
18
Ethan Nicholas941e7e22016-12-12 15:33:30 -050019static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings,
Ethan Nicholasa51740c2017-02-07 14:53:32 -050020 const char* expected, SkSL::Program::Inputs* inputs,
21 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
ethannicholasf789b382016-08-03 12:43:36 -070022 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040023 SkSL::String output;
Brian Osman93ba0a42017-08-14 14:48:10 -040024 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
25 settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050026 if (!program) {
ethannicholasf789b382016-08-03 12:43:36 -070027 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
28 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050029 REPORTER_ASSERT(r, program);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050030 if (program) {
Ethan Nicholasfc994162019-06-06 10:04:27 -040031 *inputs = program->fInputs;
32 REPORTER_ASSERT(r, compiler.toGLSL(*program, &output));
33 if (program) {
34 SkSL::String skExpected(expected);
35 if (output != skExpected) {
36 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
37 expected, output.c_str());
38 }
39 REPORTER_ASSERT(r, output == skExpected);
ethannicholasf789b382016-08-03 12:43:36 -070040 }
ethannicholasf789b382016-08-03 12:43:36 -070041 }
42}
43
Ethan Nicholas941e7e22016-12-12 15:33:30 -050044static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
Ethan Nicholasa51740c2017-02-07 14:53:32 -050045 const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -050046 SkSL::Program::Settings settings;
47 settings.fCaps = &caps;
48 SkSL::Program::Inputs inputs;
Ethan Nicholasa51740c2017-02-07 14:53:32 -050049 test(r, src, settings, expected, &inputs, kind);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050050}
51
ethannicholasf789b382016-08-03 12:43:36 -070052DEF_TEST(SkSLHelloWorld, r) {
ethannicholasf789b382016-08-03 12:43:36 -070053 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -050054 "void main() { sk_FragColor = half4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -050055 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070056 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050057 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070058 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050059 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070060 "}\n");
61}
62
63DEF_TEST(SkSLControl, r) {
ethannicholasf789b382016-08-03 12:43:36 -070064 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070065 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -050066 "if (sqrt(2) > 5) { sk_FragColor = half4(0.75); } else { discard; }"
ethannicholasf789b382016-08-03 12:43:36 -070067 "int i = 0;"
Ethan Nicholascb670962017-04-20 19:31:52 -040068 "while (i < 10) { sk_FragColor *= 0.5; i++; }"
Ethan Nicholasfc994162019-06-06 10:04:27 -040069 "do { sk_FragColor += 0.25; } while (sk_FragColor.x < 0.75);"
ethannicholasf789b382016-08-03 12:43:36 -070070 "for (int i = 0; i < 10; i++) {"
Ethan Nicholas86a43402017-01-19 13:32:00 -050071 "if (i % 2 == 1) break; else continue;"
ethannicholasf789b382016-08-03 12:43:36 -070072 "}"
73 "return;"
74 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -050075 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070076 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050077 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070078 "void main() {\n"
ethannicholas08a92112016-11-09 13:26:45 -080079 " if (sqrt(2.0) > 5.0) {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050080 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070081 " } else {\n"
82 " discard;\n"
83 " }\n"
84 " int i = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -040085 " while (i < 10) {\n"
86 " sk_FragColor *= 0.5;\n"
87 " i++;\n"
88 " }\n"
ethannicholasf789b382016-08-03 12:43:36 -070089 " do {\n"
Ethan Nicholasfc994162019-06-06 10:04:27 -040090 " sk_FragColor += 0.25;\n"
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050091 " } while (sk_FragColor.x < 0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070092 " for (int i = 0;i < 10; i++) {\n"
Ethan Nicholas86a43402017-01-19 13:32:00 -050093 " if (i % 2 == 1) break; else continue;\n"
ethannicholasf789b382016-08-03 12:43:36 -070094 " }\n"
95 " return;\n"
96 "}\n");
97}
98
99DEF_TEST(SkSLFunctions, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700100 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400101 "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; }"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500103 "void main() { float x = 10; bar(x); sk_FragColor = half4(half(x)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500104 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700105 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500106 "out vec4 sk_FragColor;\n"
Ethan Nicholasc6f5e102017-03-31 14:53:17 -0400107 "float foo(float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -0700108 " 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"
ethannicholas5961bc92016-10-12 06:39:56 -0700113 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700114 " z = foo(y);\n"
115 " x = z;\n"
116 "}\n"
117 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700118 " float x = 10.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400119 " bar(x);\n"
120 " sk_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700121 "}\n");
122}
123
124DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700125 test(r,
126 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400127 "float x = 1, y = 2;"
ethannicholasf789b382016-08-03 12:43:36 -0700128 "int z = 3;"
Ethan Nicholas86a43402017-01-19 13:32:00 -0500129 "x = x - x + y * z * x * (y - z);"
ethannicholasf789b382016-08-03 12:43:36 -0700130 "y = x / y / z;"
131 "z = (z / 2 % 3 << 4) >> 2 << 1;"
ethannicholas08a92112016-11-09 13:26:45 -0800132 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;"
ethannicholasf789b382016-08-03 12:43:36 -0700133 "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 Nicholas8aa45692017-09-20 11:24:15 -0400145 "x = (float2(sqrt(1)) , 6);"
146 "z = (float2(sqrt(1)) , 6);"
ethannicholasf789b382016-08-03 12:43:36 -0700147 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500148 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700149 "#version 400\n"
150 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700151 " float x = 1.0, y = 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700152 " int z = 3;\n"
Ethan Nicholas86a43402017-01-19 13:32:00 -0500153 " x = -6.0;\n"
154 " y = -1.0;\n"
155 " z = 8;\n"
Michael Ludwig7b429ae2018-09-06 17:01:38 -0400156 " bool b = false == true || 2.0 >= sqrt(2.0);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700157 " x += 12.0;\n"
158 " x -= 12.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700159 " 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 Nicholas4b330df2017-05-17 10:52:55 -0400169 " x = float((vec2(sqrt(1.0)) , 6));\n"
170 " z = (vec2(sqrt(1.0)) , 6);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700171 "}\n");
172}
173
174DEF_TEST(SkSLMatrices, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700175 test(r,
176 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400177 "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);"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500182 "sk_FragColor = half4(half(z[0].x), half3(v1 + v2));"
ethannicholasf789b382016-08-03 12:43:36 -0700183 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500184 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700185 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500186 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700187 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400188 " mat3x4 z = mat2x4(1.0) * mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400189 " vec3 v1 = mat3(1.0) * vec3(2.0);\n"
190 " vec3 v2 = vec3(2.0) * mat3(1.0);\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400191 " sk_FragColor = vec4(z[0].x, v1 + v2);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700192 "}\n");
193}
194
195DEF_TEST(SkSLInterfaceBlock, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700196 test(r,
197 "uniform testBlock {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400198 "half x;"
199 "half y[2];"
200 "layout(binding=12) half3x2 z;"
ethannicholasf789b382016-08-03 12:43:36 -0700201 "bool w;"
202 "};"
203 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400204 " sk_FragColor = half4(x, y[0], y[1], 0);"
ethannicholasf789b382016-08-03 12:43:36 -0700205 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500206 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700207 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500208 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700209 "uniform testBlock {\n"
210 " float x;\n"
211 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700212 " layout (binding = 12) mat3x2 z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700213 " bool w;\n"
214 "};\n"
215 "void main() {\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500216 " sk_FragColor = vec4(x, y[0], y[1], 0.0);\n"
217 "}\n");
218 test(r,
219 "uniform testBlock {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400220 "float x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500221 "} test;"
222 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500223 " sk_FragColor = half4(half(test.x));"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500224 "}",
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 {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400236 "float x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500237 "} test[2];"
238 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500239 " sk_FragColor = half4(half(test[1].x));"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500240 "}",
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"
ethannicholasf789b382016-08-03 12:43:36 -0700249 "}\n");
250}
251
252DEF_TEST(SkSLStructs, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700253 test(r,
254 "struct A {"
255 "int x;"
256 "int y;"
257 "} a1, a2;"
258 "A a3;"
259 "struct B {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400260 "float x;"
261 "float y[2];"
ethannicholasf789b382016-08-03 12:43:36 -0700262 "layout(binding=1) A z;"
263 "};"
264 "B b1, b2, b3;"
265 "void main() {"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500266 " a1.x = 0;"
267 " b1.x = 0;"
268 " sk_FragColor.r = half(a1.x + b1.x);"
ethannicholasf789b382016-08-03 12:43:36 -0700269 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500270 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700271 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500272 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700273 "struct A {\n"
274 " int x;\n"
275 " int y;\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500276 "} a1;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700277 "struct B {\n"
278 " float x;\n"
279 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700280 " layout (binding = 1) A z;\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500281 "} b1;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700282 "void main() {\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500283 " a1.x = 0;\n"
284 " b1.x = 0.0;\n"
285 " sk_FragColor.x = float(a1.x) + b1.x;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700286 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700287}
ethannicholasf789b382016-08-03 12:43:36 -0700288
ethannicholas5961bc92016-10-12 06:39:56 -0700289DEF_TEST(SkSLVersion, r) {
ethannicholas5961bc92016-10-12 06:39:56 -0700290 test(r,
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500291 "in float test; void main() { sk_FragColor.r = half(test); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500292 *SkSL::ShaderCapsFactory::Version450Core(),
ethannicholas5961bc92016-10-12 06:39:56 -0700293 "#version 450 core\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500294 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700295 "in float test;\n"
296 "void main() {\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500297 " sk_FragColor.x = test;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700298 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700299 test(r,
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500300 "in float test; void main() { sk_FragColor.r = half(test); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500301 *SkSL::ShaderCapsFactory::Version110(),
ethannicholas5961bc92016-10-12 06:39:56 -0700302 "#version 110\n"
303 "varying float test;\n"
304 "void main() {\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500305 " gl_FragColor.x = test;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700306 "}\n");
307}
308
Leon Scroggins857cb972016-11-11 18:44:37 +0000309DEF_TEST(SkSLUsesPrecisionModifiers, r) {
Leon Scroggins857cb972016-11-11 18:44:37 +0000310 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400311 "void main() { half x = 0.75; float y = 1; x++; y++;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500312 "sk_FragColor.rg = half2(x, half(y)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500313 *SkSL::ShaderCapsFactory::Default(),
Leon Scroggins857cb972016-11-11 18:44:37 +0000314 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500315 "out vec4 sk_FragColor;\n"
Leon Scroggins857cb972016-11-11 18:44:37 +0000316 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700317 " float x = 0.75;\n"
318 " float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400319 " x++;\n"
320 " y++;\n"
321 " sk_FragColor.xy = vec2(x, y);\n"
322 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700323 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400324 "void main() { half x = 0.75; float y = 1; x++; y++;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500325 "sk_FragColor.rg = half2(x, half(y)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500326 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500327 "#version 400\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400328 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -0400329 "precision mediump sampler2D;\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500330 "out mediump vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700331 "void main() {\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400332 " mediump float x = 0.75;\n"
333 " highp float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400334 " x++;\n"
335 " y++;\n"
336 " sk_FragColor.xy = vec2(x, y);\n"
337 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700338}
339
340DEF_TEST(SkSLMinAbs, r) {
341 test(r,
342 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500343 "half x = -5;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400344 "sk_FragColor.r = min(abs(x), 6);"
ethannicholas5961bc92016-10-12 06:39:56 -0700345 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500346 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700347 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500348 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700349 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400350 " sk_FragColor.x = min(abs(-5.0), 6.0);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700351 "}\n");
352
ethannicholas5961bc92016-10-12 06:39:56 -0700353 test(r,
354 "void main() {"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500355 "half x = -5.0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400356 "sk_FragColor.r = min(abs(x), 6.0);"
ethannicholas5961bc92016-10-12 06:39:56 -0700357 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500358 *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(),
ethannicholas5961bc92016-10-12 06:39:56 -0700359 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500360 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700361 "void main() {\n"
362 " float minAbsHackVar0;\n"
363 " float minAbsHackVar1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400364 " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? "
365 "minAbsHackVar0 : minAbsHackVar1);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700366 "}\n");
367}
368
Florin Malita3b30c4f2017-08-08 15:47:35 -0400369DEF_TEST(SkSLFractNegative, r) {
370 static constexpr char input[] =
371 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400372 "float x = -42.0;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500373 "sk_FragColor.r = half(fract(x));"
Florin Malita3b30c4f2017-08-08 15:47:35 -0400374 "}";
375 static constexpr char output_default[] =
376 "#version 400\n"
377 "out vec4 sk_FragColor;\n"
378 "void main() {\n"
379 " sk_FragColor.x = fract(-42.0);\n"
380 "}\n";
381 static constexpr char output_workaround[] =
382 "#version 400\n"
383 "out vec4 sk_FragColor;\n"
384 "void main() {\n"
385 " sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n"
386 "}\n";
387
388 test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default);
389 test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround);
390}
391
ethannicholasad146f62016-10-14 06:40:02 -0700392DEF_TEST(SkSLNegatedAtan, r) {
393 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -0500394 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = half(atan(x.x, -x.y)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500395 *SkSL::ShaderCapsFactory::Default(),
ethannicholasad146f62016-10-14 06:40:02 -0700396 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500397 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700398 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400399 " vec2 x = vec2(sqrt(2.0));\n"
400 " sk_FragColor.x = atan(x.x, -x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700401 "}\n");
ethannicholasad146f62016-10-14 06:40:02 -0700402 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -0500403 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = half(atan(x.x, -x.y)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500404 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(),
ethannicholasad146f62016-10-14 06:40:02 -0700405 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500406 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700407 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400408 " vec2 x = vec2(sqrt(2.0));\n"
409 " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700410 "}\n");
411}
412
ethannicholas5961bc92016-10-12 06:39:56 -0700413DEF_TEST(SkSLModifiersDeclaration, r) {
414 test(r,
415 "layout(blend_support_all_equations) out;"
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500416 "layout(blend_support_all_equations) out;"
417 "layout(blend_support_multiply) out;"
418 "layout(blend_support_screen) out;"
419 "layout(blend_support_overlay) out;"
420 "layout(blend_support_darken) out;"
421 "layout(blend_support_lighten) out;"
422 "layout(blend_support_colordodge) out;"
423 "layout(blend_support_colorburn) out;"
424 "layout(blend_support_hardlight) out;"
425 "layout(blend_support_softlight) out;"
426 "layout(blend_support_difference) out;"
427 "layout(blend_support_exclusion) out;"
428 "layout(blend_support_hsl_hue) out;"
429 "layout(blend_support_hsl_saturation) out;"
430 "layout(blend_support_hsl_color) out;"
431 "layout(blend_support_hsl_luminosity) out;"
ethannicholas5961bc92016-10-12 06:39:56 -0700432 "void main() { }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500433 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700434 "#version 400\n"
435 "layout (blend_support_all_equations) out ;\n"
Ethan Nicholas39204fd2017-11-27 13:12:30 -0500436 "layout (blend_support_all_equations) out ;\n"
437 "layout (blend_support_multiply) out ;\n"
438 "layout (blend_support_screen) out ;\n"
439 "layout (blend_support_overlay) out ;\n"
440 "layout (blend_support_darken) out ;\n"
441 "layout (blend_support_lighten) out ;\n"
442 "layout (blend_support_colordodge) out ;\n"
443 "layout (blend_support_colorburn) out ;\n"
444 "layout (blend_support_hardlight) out ;\n"
445 "layout (blend_support_softlight) out ;\n"
446 "layout (blend_support_difference) out ;\n"
447 "layout (blend_support_exclusion) out ;\n"
448 "layout (blend_support_hsl_hue) out ;\n"
449 "layout (blend_support_hsl_saturation) out ;\n"
450 "layout (blend_support_hsl_color) out ;\n"
451 "layout (blend_support_hsl_luminosity) out ;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700452 "void main() {\n"
453 "}\n");
454}
455
456DEF_TEST(SkSLHex, r) {
457 test(r,
458 "void main() {"
459 "int i1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400460 "i1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700461 "int i2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400462 "i2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700463 "int i3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400464 "i3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700465 "int i4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400466 "i4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700467 "int i5 = -0xbeef;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400468 "i5++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700469 "uint u1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400470 "u1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700471 "uint u2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400472 "u2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700473 "uint u3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400474 "u3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700475 "uint u4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400476 "u4++;"
Ethan Nicholas58d56482017-12-19 09:29:22 -0500477 "ushort u5 = 0xffff;"
478 "u5++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700479 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500480 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700481 "#version 400\n"
482 "void main() {\n"
483 " int i1 = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400484 " i1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700485 " int i2 = 305441741;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400486 " i2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700487 " int i3 = 2147483647;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400488 " i3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700489 " int i4 = -1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400490 " i4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700491 " int i5 = -48879;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400492 " i5++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700493 " uint u1 = 0u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400494 " u1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700495 " uint u2 = 305441741u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400496 " u2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700497 " uint u3 = 2147483647u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400498 " u3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700499 " uint u4 = 4294967295u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400500 " u4++;\n"
Ethan Nicholas58d56482017-12-19 09:29:22 -0500501 " uint u5 = 65535u;\n"
502 " u5++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700503 "}\n");
504}
505
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400506DEF_TEST(SkSLVectorConstructors, r) {
507 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400508 "float2 v1 = float2(1);"
509 "float2 v2 = float2(1, 2);"
510 "float2 v3 = float2(float2(1));"
511 "float3 v4 = float3(float2(1), 1.0);"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400512 "int2 v5 = int2(1);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400513 "int2 v6 = int2(float2(1, 2));"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500514 "float2 v7 = float2(int2(1, 2));"
515 "void main() {"
516 "sk_FragColor.r = half(v1.x + v2.x + v3.x + v4.x + v5.x + v6.x + v7.x);"
517 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500518 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400519 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500520 "out vec4 sk_FragColor;\n"
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400521 "vec2 v1 = vec2(1.0);\n"
522 "vec2 v2 = vec2(1.0, 2.0);\n"
523 "vec2 v3 = vec2(1.0);\n"
Ethan Nicholas84645e32017-02-09 13:57:14 -0500524 "vec3 v4 = vec3(vec2(1.0), 1.0);\n"
525 "ivec2 v5 = ivec2(1);\n"
526 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500527 "vec2 v7 = vec2(ivec2(1, 2));\n"
528 "void main() {\n"
529 " sk_FragColor.x = (((((v1.x + v2.x) + v3.x) + v4.x) + float(v5.x)) + float(v6.x)) + "
530 "v7.x;\n"
531 "}\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400532}
533
ethannicholas5961bc92016-10-12 06:39:56 -0700534DEF_TEST(SkSLArrayConstructors, r) {
535 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400536 "float test1[] = float[](1, 2, 3, 4);"
537 "float2 test2[] = float2[](float2(1, 2), float2(3, 4));"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500538 "float4x4 test3[] = float4x4[]();"
539 "void main() {"
540 "sk_FragColor.r = half(test1[0] + test2[0].x + test3[0][0][0]);"
541 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500542 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700543 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500544 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700545 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
546 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -0500547 "mat4 test3[] = mat4[]();\n"
548 "void main() {\n"
549 " sk_FragColor.x = (test1[0] + test2[0].x) + test3[0][0][0];\n"
550 "}\n");
ethannicholasf789b382016-08-03 12:43:36 -0700551}
ethannicholasddb37d62016-10-20 09:54:00 -0700552
Ethan Nicholas39080592019-07-22 14:54:43 -0400553DEF_TEST(SkSLConstArray, r) {
554 test(r,
555 "const half test[] = half[](1, 2, 3, 4);"
556 "void main() {"
557 "sk_FragColor = half4(test[0], test[1], test[2], test[3]);"
558 "}",
559 *SkSL::ShaderCapsFactory::Default(),
560 "#version 400\n"
561 "out vec4 sk_FragColor;\n"
562 "const float test[] = float[](1.0, 2.0, 3.0, 4.0);\n"
563 "void main() {\n"
564 " sk_FragColor = vec4(test[0], test[1], test[2], test[3]);\n"
565 "}\n");
566}
567
ethannicholasddb37d62016-10-20 09:54:00 -0700568DEF_TEST(SkSLDerivatives, r) {
569 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -0500570 "void main() { sk_FragColor.r = half(dFdx(1)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500571 *SkSL::ShaderCapsFactory::Default(),
ethannicholasddb37d62016-10-20 09:54:00 -0700572 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500573 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700574 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400575 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700576 "}\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700577 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400578 "void main() { sk_FragColor.r = 1; }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500579 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700580 "#version 400\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400581 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -0400582 "precision mediump sampler2D;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400583 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700584 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400585 " sk_FragColor.x = 1.0;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700586 "}\n");
587 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -0500588 "void main() { sk_FragColor.r = half(dFdx(1)); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500589 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700590 "#version 400\n"
591 "#extension GL_OES_standard_derivatives : require\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400592 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -0400593 "precision mediump sampler2D;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400594 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700595 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400596 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700597 "}\n");
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700598
599 SkSL::Program::Settings settings;
600 settings.fFlipY = false;
601 auto caps = SkSL::ShaderCapsFactory::Default();
602 settings.fCaps = caps.get();
603 SkSL::Program::Inputs inputs;
604 test(r,
605 "void main() { sk_FragColor.r = half(dFdx(1)), sk_FragColor.g = half(dFdy(1)); }",
606 settings,
607 "#version 400\n"
608 "out vec4 sk_FragColor;\n"
609 "void main() {\n"
610 " (sk_FragColor.x = dFdx(1.0) , sk_FragColor.y = dFdy(1.0));\n"
611 "}\n",
612 &inputs);
613 settings.fFlipY = true;
614 test(r,
615 "void main() { sk_FragColor.r = half(dFdx(1)), sk_FragColor.g = half(dFdy(1)); }",
616 settings,
617 "#version 400\n"
618 "out vec4 sk_FragColor;\n"
619 "void main() {\n"
620 " (sk_FragColor.x = dFdx(1.0) , sk_FragColor.y = -dFdy(1.0));\n"
621 "}\n",
622 &inputs);
ethannicholasddb37d62016-10-20 09:54:00 -0700623}
ethannicholas08a92112016-11-09 13:26:45 -0800624
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400625
626DEF_TEST(SkSLIntFolding, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800627 test(r,
628 "void main() {"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400629 "sk_FragColor.r = 32 + 2;"
630 "sk_FragColor.r = 32 - 2;"
631 "sk_FragColor.r = 32 * 2;"
632 "sk_FragColor.r = 32 / 2;"
633 "sk_FragColor.r = 12 | 6;"
634 "sk_FragColor.r = 254 & 7;"
635 "sk_FragColor.r = 2 ^ 7;"
636 "sk_FragColor.r = 1 << 4;"
637 "sk_FragColor.r = 128 >> 2;"
638 "sk_FragColor.r = -1 == -1 ? 1 : -1;"
639 "sk_FragColor.r = -1 == -2 ? 2 : -2;"
640 "sk_FragColor.r = 0 != 1 ? 3 : -3;"
641 "sk_FragColor.r = 0 != 0 ? 4 : -4;"
642 "sk_FragColor.r = 6 > 5 ? 5 : -5;"
643 "sk_FragColor.r = 6 > 6 ? 6 : -6;"
644 "sk_FragColor.r = -1 < 0 ? 7 : -7;"
645 "sk_FragColor.r = 1 < 0 ? 8 : -8;"
646 "sk_FragColor.r = 6 >= 6 ? 9 : -9;"
647 "sk_FragColor.r = 6 >= 7 ? 10 : -10;"
648 "sk_FragColor.r = 6 <= 6 ? 11 : -11;"
649 "sk_FragColor.r = 6 <= 5 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400650 "sk_FragColor.r = int(sqrt(1)) + 0;"
651 "sk_FragColor.r = 0 + int(sqrt(2));"
652 "sk_FragColor.r = int(sqrt(3)) - 0;"
653 "sk_FragColor.r = int(sqrt(4)) * 0;"
654 "sk_FragColor.r = int(sqrt(5)) * 1;"
655 "sk_FragColor.r = 1 * int(sqrt(6));"
656 "sk_FragColor.r = 0 * int(sqrt(7));"
657 "sk_FragColor.r = int(sqrt(8)) / 1;"
658 "sk_FragColor.r = 0 / int(sqrt(9));"
659 "int x = int(sqrt(2));"
660 "x += 1;"
661 "x += 0;"
662 "x -= 1;"
663 "x -= 0;"
664 "x *= 1;"
665 "x *= 2;"
666 "x /= 1;"
667 "x /= 2;"
668 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800669 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500670 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800671 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500672 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800673 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400674 " sk_FragColor.x = 34.0;\n"
675 " sk_FragColor.x = 30.0;\n"
676 " sk_FragColor.x = 64.0;\n"
677 " sk_FragColor.x = 16.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400678 " sk_FragColor.x = 14.0;\n"
679 " sk_FragColor.x = 6.0;\n"
680 " sk_FragColor.x = 5.0;\n"
681 " sk_FragColor.x = 16.0;\n"
682 " sk_FragColor.x = 32.0;\n"
683 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400684 " sk_FragColor.x = -2.0;\n"
685 " sk_FragColor.x = 3.0;\n"
686 " sk_FragColor.x = -4.0;\n"
687 " sk_FragColor.x = 5.0;\n"
688 " sk_FragColor.x = -6.0;\n"
689 " sk_FragColor.x = 7.0;\n"
690 " sk_FragColor.x = -8.0;\n"
691 " sk_FragColor.x = 9.0;\n"
692 " sk_FragColor.x = -10.0;\n"
693 " sk_FragColor.x = 11.0;\n"
694 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400695 " sk_FragColor.x = float(int(sqrt(1.0)));\n"
696 " sk_FragColor.x = float(int(sqrt(2.0)));\n"
697 " sk_FragColor.x = float(int(sqrt(3.0)));\n"
698 " sk_FragColor.x = 0.0;\n"
699 " sk_FragColor.x = float(int(sqrt(5.0)));\n"
700 " sk_FragColor.x = float(int(sqrt(6.0)));\n"
701 " sk_FragColor.x = 0.0;\n"
702 " sk_FragColor.x = float(int(sqrt(8.0)));\n"
703 " sk_FragColor.x = 0.0;\n"
704 " int x = int(sqrt(2.0));\n"
705 " x += 1;\n"
706 " x -= 1;\n"
707 " x *= 2;\n"
708 " x /= 2;\n"
709 " sk_FragColor.x = float(x);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400710 "}\n");
711}
712
713DEF_TEST(SkSLFloatFolding, r) {
714 test(r,
715 "void main() {"
716 "sk_FragColor.r = 32.0 + 2.0;"
717 "sk_FragColor.r = 32.0 - 2.0;"
718 "sk_FragColor.r = 32.0 * 2.0;"
719 "sk_FragColor.r = 32.0 / 2.0;"
720 "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
721 "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;"
722 "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;"
723 "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;"
724 "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;"
725 "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;"
726 "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;"
727 "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;"
728 "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;"
729 "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;"
730 "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;"
731 "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;"
732 "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;"
Ethan Nicholase1f55022019-02-05 17:17:40 -0500733 "sk_FragColor.r = half(sqrt(1) + 0);"
734 "sk_FragColor.r = half(0 + sqrt(2));"
735 "sk_FragColor.r = half(sqrt(3) - 0);"
736 "sk_FragColor.r = half(sqrt(4) * 0);"
737 "sk_FragColor.r = half(sqrt(5) * 1);"
738 "sk_FragColor.r = half(1 * sqrt(6));"
739 "sk_FragColor.r = half(0 * sqrt(7));"
740 "sk_FragColor.r = half(sqrt(8) / 1);"
741 "sk_FragColor.r = half(0 / sqrt(9));"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400742 "sk_FragColor.r += 1;"
743 "sk_FragColor.r += 0;"
744 "sk_FragColor.r -= 1;"
745 "sk_FragColor.r -= 0;"
746 "sk_FragColor.r *= 1;"
747 "sk_FragColor.r *= 2;"
748 "sk_FragColor.r /= 1;"
749 "sk_FragColor.r /= 2;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400750 "}",
751 *SkSL::ShaderCapsFactory::Default(),
752 "#version 400\n"
753 "out vec4 sk_FragColor;\n"
754 "void main() {\n"
755 " sk_FragColor.x = 34.0;\n"
756 " sk_FragColor.x = 30.0;\n"
757 " sk_FragColor.x = 64.0;\n"
758 " sk_FragColor.x = 16.0;\n"
759 " sk_FragColor.x = 19.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400760 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400761 " sk_FragColor.x = -2.0;\n"
762 " sk_FragColor.x = 3.0;\n"
763 " sk_FragColor.x = -4.0;\n"
764 " sk_FragColor.x = 5.0;\n"
765 " sk_FragColor.x = -6.0;\n"
766 " sk_FragColor.x = 7.0;\n"
767 " sk_FragColor.x = -8.0;\n"
768 " sk_FragColor.x = 9.0;\n"
769 " sk_FragColor.x = -10.0;\n"
770 " sk_FragColor.x = 11.0;\n"
771 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400772 " sk_FragColor.x = sqrt(1.0);\n"
773 " sk_FragColor.x = sqrt(2.0);\n"
774 " sk_FragColor.x = sqrt(3.0);\n"
775 " sk_FragColor.x = 0.0;\n"
776 " sk_FragColor.x = sqrt(5.0);\n"
777 " sk_FragColor.x = sqrt(6.0);\n"
778 " sk_FragColor.x = 0.0;\n"
779 " sk_FragColor.x = sqrt(8.0);\n"
780 " sk_FragColor.x = 0.0;\n"
781 " sk_FragColor.x += 1.0;\n"
782 " sk_FragColor.x -= 1.0;\n"
783 " sk_FragColor.x *= 2.0;\n"
784 " sk_FragColor.x /= 2.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400785 "}\n");
786}
787
788DEF_TEST(SkSLBoolFolding, r) {
789 test(r,
790 "void main() {"
791 "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;"
792 "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;"
793 "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;"
794 "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;"
795 "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;"
796 "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;"
797 "}",
798 *SkSL::ShaderCapsFactory::Default(),
799 "#version 400\n"
800 "out vec4 sk_FragColor;\n"
801 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400802 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400803 " sk_FragColor.x = -2.0;\n"
804 " sk_FragColor.x = 3.0;\n"
805 " sk_FragColor.x = -4.0;\n"
806 " sk_FragColor.x = 5.0;\n"
807 " sk_FragColor.x = -6.0;\n"
808 "}\n");
809}
810
Michael Ludwig7b429ae2018-09-06 17:01:38 -0400811DEF_TEST(SkSLShortCircuitBoolFolding, r) {
812 test(r,
813 "void main() {"
814 "bool expr1 = sk_FragCoord.x > 0;"
815 "bool expr2 = sk_FragCoord.y > 0;"
816 " if (true && expr1) {" // -> if (expr1)
817 " sk_FragColor.r = 1;"
818 " } else if (false && expr1) {" // -> if (false) -> block removed
819 " sk_FragColor.r = -2;"
820 " } else if (false || expr2) {" // -> if (expr2)
821 " sk_FragColor.r = 3;"
822 " } else if (true || expr2) {" // -> if (true) -> replaces unreachable else
823 " sk_FragColor.r = 4;"
824 " } else {" // removed
825 " sk_FragColor.r = -5;"
826 " }"
827 // Test short-circuiting of right hand side boolean literals
828 " if (expr1 && true) {" // -> if (expr1)
829 " sk_FragColor.r = 1;"
830 " } else if (expr1 && false) {" // -> if (false) -> block removed
831 " sk_FragColor.r = -2;"
832 " } else if (expr2 || false) {" // -> if (expr2)
833 " sk_FragColor.r = 3;"
834 " } else if (expr2 || true) {" // -> if (true) -> replaces unreachable else
835 " sk_FragColor.r = 4;"
836 " } else {" // removed
837 " sk_FragColor.r = -5;"
838 " }"
839 "}",
840 *SkSL::ShaderCapsFactory::Default(),
841 "#version 400\n"
842 "out vec4 sk_FragColor;\n"
843 "void main() {\n"
844 " bool expr1 = gl_FragCoord.x > 0.0;\n"
845 " bool expr2 = gl_FragCoord.y > 0.0;\n"
846 " if (expr1) {\n"
847 " sk_FragColor.x = 1.0;\n"
848 " } else if (expr2) {\n"
849 " sk_FragColor.x = 3.0;\n"
850 " } else {\n"
851 " sk_FragColor.x = 4.0;\n"
852 " }\n"
853 " if (expr1) {\n"
854 " sk_FragColor.x = 1.0;\n"
855 " } else if (expr2) {\n"
856 " sk_FragColor.x = 3.0;\n"
857 " } else {\n"
858 " sk_FragColor.x = 4.0;\n"
859 " }\n"
860 "}\n");
861}
862
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400863DEF_TEST(SkSLVecFolding, r) {
864 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -0500865 "void main() {\n"
866 "sk_FragColor.r = half4(0.5, 1, 1, 1).x;\n"
867 "sk_FragColor = half4(half2(1), half2(2, 3)) + half4(5, 6, 7, 8);\n"
868 "sk_FragColor = half4(8, half3(10)) - half4(1);\n"
869 "sk_FragColor = half4(2) * half4(1, 2, 3, 4);\n"
870 "sk_FragColor = half4(12) / half4(1, 2, 3, 4);\n"
871 "sk_FragColor.r = (half4(12) / half4(1, 2, 3, 4)).y;\n"
872 "sk_FragColor.x = half4(1) == half4(1) ? 1.0 : -1.0;\n"
873 "sk_FragColor.x = half4(1) == half4(2) ? 2.0 : -2.0;\n"
874 "sk_FragColor.x = half2(1) == half2(1, 1) ? 3.0 : -3.0;\n"
875 "sk_FragColor.x = half2(1, 1) == half2(1, 1) ? 4.0 : -4.0;\n"
876 "sk_FragColor.x = half2(1) == half2(1, 0) ? 5.0 : -5.0;\n"
877 "sk_FragColor.x = half4(1) == half4(half2(1), half2(1)) ? 6.0 : -6.0;\n"
878 "sk_FragColor.x = half4(half3(1), 1) == half4(half2(1), half2(1)) ? 7.0 : -7.0;\n"
879 "sk_FragColor.x = half4(half3(1), 1) == half4(half2(1), 1, 0) ? 8.0 : -8.0;\n"
880 "sk_FragColor.x = half2(1) != half2(1, 0) ? 9.0 : -9.0;\n"
881 "sk_FragColor.x = half4(1) != half4(half2(1), half2(1)) ? 10.0 : -10.0;\n"
882 "sk_FragColor = half4(half(sqrt(1))) * half4(1);\n"
883 "sk_FragColor = half4(1) * half4(half(sqrt(2)));\n"
884 "sk_FragColor = half4(0) * half4(half(sqrt(3)));\n"
885 "sk_FragColor = half4(half(sqrt(4))) * half4(0);\n"
886 "sk_FragColor = half4(0) / half4(half(sqrt(5)));\n"
887 "sk_FragColor = half4(0) + half4(half(sqrt(6)));\n"
888 "sk_FragColor = half4(half(sqrt(7))) + half4(0);\n"
889 "sk_FragColor = half4(half(sqrt(8))) - half4(0);\n"
890 "sk_FragColor = half4(0) + half(half(sqrt(9)));\n"
891 "sk_FragColor = half4(0) * half(sqrt(10));\n"
892 "sk_FragColor = half4(0) / half(sqrt(11));\n"
893 "sk_FragColor = half4(1) * half(sqrt(12));\n"
894 "sk_FragColor = 0 + half4(half(sqrt(13)));\n"
895 "sk_FragColor = 0 * half4(half(sqrt(14)));\n"
896 "sk_FragColor = 0 / half4(half(sqrt(15)));\n"
897 "sk_FragColor = 1 * half4(half(sqrt(16)));\n"
898 "sk_FragColor = half4(half(sqrt(17))) + 0;\n"
899 "sk_FragColor = half4(half(sqrt(18))) * 0;\n"
900 "sk_FragColor = half4(half(sqrt(19))) * 1;\n"
901 "sk_FragColor = half4(half(sqrt(19.5))) - 0;\n"
902 "sk_FragColor = half(half(sqrt(20))) * half4(1);\n"
903 "sk_FragColor = half(half(sqrt(21))) + half4(0);\n"
904 "sk_FragColor = half(half(sqrt(22))) - half4(0);\n"
905 "sk_FragColor = half(half(sqrt(23))) / half4(1);\n"
906 "sk_FragColor = half4(half(sqrt(24))) / 1;\n"
907 "sk_FragColor += half4(1);\n"
908 "sk_FragColor += half4(0);\n"
909 "sk_FragColor -= half4(1);\n"
910 "sk_FragColor -= half4(0);\n"
911 "sk_FragColor *= half4(1);\n"
912 "sk_FragColor *= half4(2);\n"
913 "sk_FragColor /= half4(1);\n"
914 "sk_FragColor /= half4(2);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400915 "}",
916 *SkSL::ShaderCapsFactory::Default(),
917 "#version 400\n"
918 "out vec4 sk_FragColor;\n"
919 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400920 " sk_FragColor.x = 0.5;\n"
921 " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
922 " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
923 " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
924 " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
925 " sk_FragColor.x = 6.0;\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400926 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400927 " sk_FragColor.x = -2.0;\n"
928 " sk_FragColor.x = 3.0;\n"
929 " sk_FragColor.x = 4.0;\n"
930 " sk_FragColor.x = -5.0;\n"
931 " sk_FragColor.x = 6.0;\n"
932 " sk_FragColor.x = 7.0;\n"
933 " sk_FragColor.x = -8.0;\n"
934 " sk_FragColor.x = 9.0;\n"
935 " sk_FragColor.x = -10.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400936 " sk_FragColor = vec4(sqrt(1.0));\n"
937 " sk_FragColor = vec4(sqrt(2.0));\n"
938 " sk_FragColor = vec4(0.0);\n"
939 " sk_FragColor = vec4(0.0);\n"
940 " sk_FragColor = vec4(0.0);\n"
941 " sk_FragColor = vec4(sqrt(6.0));\n"
942 " sk_FragColor = vec4(sqrt(7.0));\n"
943 " sk_FragColor = vec4(sqrt(8.0));\n"
944 " sk_FragColor = vec4(sqrt(9.0));\n"
945 " sk_FragColor = vec4(0.0);\n"
946 " sk_FragColor = vec4(0.0);\n"
947 " sk_FragColor = vec4(sqrt(12.0));\n"
948 " sk_FragColor = vec4(sqrt(13.0));\n"
949 " sk_FragColor = vec4(0.0);\n"
950 " sk_FragColor = vec4(0.0);\n"
951 " sk_FragColor = vec4(sqrt(16.0));\n"
952 " sk_FragColor = vec4(sqrt(17.0));\n"
953 " sk_FragColor = vec4(0.0);\n"
954 " sk_FragColor = vec4(sqrt(19.0));\n"
955 " sk_FragColor = vec4(sqrt(19.5));\n"
956 " sk_FragColor = vec4(sqrt(20.0));\n"
957 " sk_FragColor = vec4(sqrt(21.0));\n"
958 " sk_FragColor = vec4(sqrt(22.0));\n"
959 " sk_FragColor = vec4(sqrt(23.0));\n"
960 " sk_FragColor = vec4(sqrt(24.0));\n"
961 " sk_FragColor += vec4(1.0);\n"
962 " sk_FragColor -= vec4(1.0);\n"
963 " sk_FragColor *= vec4(2.0);\n"
964 " sk_FragColor /= vec4(2.0);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400965 "}\n");
966}
967
968DEF_TEST(SkSLMatFolding, r) {
969 test(r,
970 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400971 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == "
972 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;"
973 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == "
974 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;"
975 "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;"
976 "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;"
977 "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;"
978 "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;"
979 "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400980 "? 7 : -7;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400981 "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;"
982 "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;"
983 "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), "
984 "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;"
985 "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;"
986 "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;"
987 "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;"
988 "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400989 "}",
990 *SkSL::ShaderCapsFactory::Default(),
991 "#version 400\n"
992 "out vec4 sk_FragColor;\n"
993 "void main() {\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400994 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400995 " sk_FragColor.x = -2.0;\n"
996 " sk_FragColor.x = 3.0;\n"
997 " sk_FragColor.x = -4.0;\n"
998 " sk_FragColor.x = 5.0;\n"
999 " sk_FragColor.x = -6.0;\n"
1000 " sk_FragColor.x = 7.0;\n"
1001 " sk_FragColor.x = -8.0;\n"
1002 " sk_FragColor.x = 9.0;\n"
1003 " sk_FragColor.x = 10.0;\n"
1004 " sk_FragColor.x = 11.0;\n"
1005 " sk_FragColor.x = 12.0;\n"
1006 " sk_FragColor.x = 13.0;\n"
1007 " sk_FragColor.x = 14.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -08001008 "}\n");
1009}
1010
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001011DEF_TEST(SkSLConstantIf, r) {
ethannicholas08a92112016-11-09 13:26:45 -08001012 test(r,
1013 "void main() {"
1014 "int x;"
1015 "if (true) x = 1;"
1016 "if (2 > 1) x = 2; else x = 3;"
1017 "if (1 > 2) x = 4; else x = 5;"
1018 "if (false) x = 6;"
Ethan Nicholascb670962017-04-20 19:31:52 -04001019 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -08001020 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -05001021 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -08001022 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -05001023 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -08001024 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001025 " sk_FragColor.x = 5.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -08001026 "}\n");
1027}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -05001028
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001029DEF_TEST(SkSLCaps, r) {
1030 test(r,
1031 "void main() {"
Ethan Nicholascb670962017-04-20 19:31:52 -04001032 "int x = 0;"
1033 "int y = 0;"
1034 "int z = 0;"
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001035 "if (sk_Caps.externalTextureSupport) x = 1;"
Ethan Nicholascb670962017-04-20 19:31:52 -04001036 "if (sk_Caps.fbFetchSupport) y = 1;"
Brian Osman6386efa2019-06-05 10:59:00 -04001037 "if (sk_Caps.canUseAnyFunctionInShader) z = 1;"
1038 "sk_FragColor = half4(x, y, z, 0.0);"
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001039 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -05001040 *SkSL::ShaderCapsFactory::VariousCaps(),
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001041 "#version 400\n"
1042 "out vec4 sk_FragColor;\n"
1043 "void main() {\n"
Brian Osman6386efa2019-06-05 10:59:00 -04001044 " sk_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\n"
Ethan Nicholas3605ace2016-11-21 15:59:48 -05001045 "}\n");
1046}
1047
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001048DEF_TEST(SkSLTexture, r) {
1049 test(r,
1050 "uniform sampler1D one;"
1051 "uniform sampler2D two;"
1052 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001053 "float4 a = sample(one, 0);"
1054 "float4 b = sample(two, float2(0));"
1055 "float4 c = sample(one, float2(0));"
1056 "float4 d = sample(two, float3(0));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001057 "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001058 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -05001059 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001060 "#version 400\n"
1061 "out vec4 sk_FragColor;\n"
1062 "uniform sampler1D one;\n"
1063 "uniform sampler2D two;\n"
1064 "void main() {\n"
1065 " vec4 a = texture(one, 0.0);\n"
1066 " vec4 b = texture(two, vec2(0.0));\n"
1067 " vec4 c = textureProj(one, vec2(0.0));\n"
1068 " vec4 d = textureProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001069 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001070 "}\n");
1071 test(r,
1072 "uniform sampler1D one;"
1073 "uniform sampler2D two;"
1074 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001075 "float4 a = sample(one, 0);"
1076 "float4 b = sample(two, float2(0));"
1077 "float4 c = sample(one, float2(0));"
1078 "float4 d = sample(two, float3(0));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001079 "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001080 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -05001081 *SkSL::ShaderCapsFactory::Version110(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001082 "#version 110\n"
1083 "uniform sampler1D one;\n"
1084 "uniform sampler2D two;\n"
1085 "void main() {\n"
1086 " vec4 a = texture1D(one, 0.0);\n"
1087 " vec4 b = texture2D(two, vec2(0.0));\n"
1088 " vec4 c = texture1DProj(one, vec2(0.0));\n"
1089 " vec4 d = texture2DProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001090 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -05001091 "}\n");
1092}
1093
Brian Osman8a83ca42018-02-12 14:32:17 -05001094DEF_TEST(SkSLSharpen, r) {
1095 SkSL::Program::Settings settings;
1096 settings.fSharpenTextures = true;
1097 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1098 settings.fCaps = caps.get();
1099 SkSL::Program::Inputs inputs;
1100 test(r,
1101 "uniform sampler1D one;"
1102 "uniform sampler2D two;"
1103 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001104 "float4 a = sample(one, 0);"
1105 "float4 b = sample(two, float2(0));"
1106 "float4 c = sample(one, float2(0));"
1107 "float4 d = sample(two, float3(0));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001108 "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
Brian Osman8a83ca42018-02-12 14:32:17 -05001109 "}",
1110 settings,
1111 "#version 400\n"
1112 "out vec4 sk_FragColor;\n"
1113 "uniform sampler1D one;\n"
1114 "uniform sampler2D two;\n"
1115 "void main() {\n"
1116 " vec4 a = texture(one, 0.0, -0.5);\n"
1117 " vec4 b = texture(two, vec2(0.0), -0.5);\n"
1118 " vec4 c = textureProj(one, vec2(0.0), -0.5);\n"
1119 " vec4 d = textureProj(two, vec3(0.0), -0.5);\n"
1120 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1121 "}\n",
1122 &inputs);
1123
1124 caps = SkSL::ShaderCapsFactory::Version110();
1125 settings.fCaps = caps.get();
1126 test(r,
1127 "uniform sampler1D one;"
1128 "uniform sampler2D two;"
1129 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001130 "float4 a = sample(one, 0);"
1131 "float4 b = sample(two, float2(0));"
1132 "float4 c = sample(one, float2(0));"
1133 "float4 d = sample(two, float3(0));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001134 "sk_FragColor = half4(half(a.x), half(b.x), half(c.x), half(d.x));"
Brian Osman8a83ca42018-02-12 14:32:17 -05001135 "}",
1136 settings,
1137 "#version 110\n"
1138 "uniform sampler1D one;\n"
1139 "uniform sampler2D two;\n"
1140 "void main() {\n"
1141 " vec4 a = texture1D(one, 0.0, -0.5);\n"
1142 " vec4 b = texture2D(two, vec2(0.0), -0.5);\n"
1143 " vec4 c = texture1DProj(one, vec2(0.0), -0.5);\n"
1144 " vec4 d = texture2DProj(two, vec3(0.0), -0.5);\n"
1145 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
1146 "}\n",
1147 &inputs);
1148}
1149
Ethan Nicholas19671772016-11-28 16:30:17 -05001150DEF_TEST(SkSLOffset, r) {
1151 test(r,
1152 "struct Test {"
1153 "layout(offset = 0) int x;"
1154 "layout(offset = 4) int y;"
1155 "int z;"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001156 "} test;"
1157 "void main() {"
1158 "Test t;"
1159 "t.x = 0;"
1160 "sk_FragColor.r = half(t.x);"
1161 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -05001162 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas19671772016-11-28 16:30:17 -05001163 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001164 "out vec4 sk_FragColor;\n"
1165 "void main() {\n"
1166 " struct Test {\n"
1167 " layout (offset = 0) int x;\n"
1168 " layout (offset = 4) int y;\n"
1169 " int z;\n"
1170 " } t;\n"
1171 " t.x = 0;\n"
1172 " sk_FragColor.x = float(t.x);\n"
1173 "}\n");
Ethan Nicholas19671772016-11-28 16:30:17 -05001174}
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001175
1176DEF_TEST(SkSLFragCoord, r) {
1177 SkSL::Program::Settings settings;
1178 settings.fFlipY = true;
1179 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
1180 settings.fCaps = caps.get();
1181 SkSL::Program::Inputs inputs;
1182 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001183 "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001184 settings,
1185 "#version 110\n"
1186 "#extension GL_ARB_fragment_coord_conventions : require\n"
1187 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1188 "void main() {\n"
1189 " gl_FragColor.xy = gl_FragCoord.xy;\n"
1190 "}\n",
1191 &inputs);
Greg Daniele6ab9982018-08-22 13:56:32 +00001192 REPORTER_ASSERT(r, !inputs.fRTHeight);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001193
1194 caps = SkSL::ShaderCapsFactory::FragCoordsNew();
1195 settings.fCaps = caps.get();
1196 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001197 "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001198 settings,
1199 "#version 400\n"
1200 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
1201 "out vec4 sk_FragColor;\n"
1202 "void main() {\n"
1203 " sk_FragColor.xy = gl_FragCoord.xy;\n"
1204 "}\n",
1205 &inputs);
Greg Daniele6ab9982018-08-22 13:56:32 +00001206 REPORTER_ASSERT(r, !inputs.fRTHeight);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001207
1208 caps = SkSL::ShaderCapsFactory::Default();
1209 settings.fCaps = caps.get();
1210 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001211 "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001212 settings,
1213 "#version 400\n"
Greg Daniele6ab9982018-08-22 13:56:32 +00001214 "uniform float u_skRTHeight;\n"
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001215 "out vec4 sk_FragColor;\n"
1216 "void main() {\n"
Michael Ludwigf0b60442018-12-10 14:43:38 +00001217 " vec4 sk_FragCoord = vec4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, "
1218 "gl_FragCoord.z, gl_FragCoord.w);\n"
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001219 " sk_FragColor.xy = sk_FragCoord.xy;\n"
1220 "}\n",
1221 &inputs);
Greg Daniele6ab9982018-08-22 13:56:32 +00001222 REPORTER_ASSERT(r, inputs.fRTHeight);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001223
1224 settings.fFlipY = false;
1225 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001226 "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001227 settings,
1228 "#version 400\n"
1229 "out vec4 sk_FragColor;\n"
1230 "void main() {\n"
1231 " sk_FragColor.xy = gl_FragCoord.xy;\n"
1232 "}\n",
1233 &inputs);
Greg Daniele6ab9982018-08-22 13:56:32 +00001234 REPORTER_ASSERT(r, !inputs.fRTHeight);
Brian Osmancd3261a2018-01-16 13:52:29 +00001235
1236 test(r,
1237 "in float4 pos; void main() { sk_Position = pos; }",
1238 *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1239 "#version 400\n"
1240 "out vec4 sk_FragCoord_Workaround;\n"
1241 "in vec4 pos;\n"
1242 "void main() {\n"
1243 " sk_FragCoord_Workaround = (gl_Position = pos);\n"
1244 "}\n",
1245 SkSL::Program::kVertex_Kind);
1246
1247 test(r,
Robert Phillipsfe8da172018-01-24 14:52:02 +00001248 "in uniform float4 sk_RTAdjust; in float4 pos; void main() { sk_Position = pos; }",
1249 *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1250 "#version 400\n"
1251 "out vec4 sk_FragCoord_Workaround;\n"
1252 "in uniform vec4 sk_RTAdjust;\n"
1253 "in vec4 pos;\n"
1254 "void main() {\n"
1255 " sk_FragCoord_Workaround = (gl_Position = pos);\n"
Ethan Nicholasb809efb2018-04-12 14:39:21 -04001256 " gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * sk_RTAdjust.yw,"
1257 " 0.0, gl_Position.w);\n"
Robert Phillipsfe8da172018-01-24 14:52:02 +00001258 "}\n",
1259 SkSL::Program::kVertex_Kind);
1260
1261 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001262 "void main() { sk_FragColor.xy = half2(sk_FragCoord.xy); }",
Brian Osmancd3261a2018-01-16 13:52:29 +00001263 *SkSL::ShaderCapsFactory::CannotUseFragCoord(),
1264 "#version 400\n"
1265 "in vec4 sk_FragCoord_Workaround;\n"
1266 "out vec4 sk_FragColor;\n"
1267 "void main() {\n"
Brian Salomondba65f92018-01-22 08:43:38 -05001268 " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"
1269 " vec4 sk_FragCoord_Resolved = vec4(sk_FragCoord_Workaround.xyz * "
1270 "sk_FragCoord_InvW, sk_FragCoord_InvW);\n"
1271 " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + vec2(.5);\n"
1272 " sk_FragColor.xy = sk_FragCoord_Resolved.xy;\n"
Brian Osmancd3261a2018-01-16 13:52:29 +00001273 "}\n");
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001274}
1275
Ethan Nicholascd700e92018-08-24 16:43:57 -04001276DEF_TEST(SkSLWidthAndHeight, r) {
1277 SkSL::Program::Settings settings;
1278 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
1279 settings.fCaps = caps.get();
1280 SkSL::Program::Inputs inputs;
1281 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001282 "void main() { sk_FragColor.r = half(sk_FragCoord.x / sk_Width); }",
Ethan Nicholascd700e92018-08-24 16:43:57 -04001283 settings,
1284 "#version 400\n"
1285 "uniform float u_skRTWidth;\n"
1286 "out vec4 sk_FragColor;\n"
1287 "void main() {\n"
1288 " sk_FragColor.x = gl_FragCoord.x / u_skRTWidth;\n"
1289 "}\n",
1290 &inputs);
1291 REPORTER_ASSERT(r, inputs.fRTWidth);
1292 REPORTER_ASSERT(r, !inputs.fRTHeight);
1293
1294 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001295 "void main() { sk_FragColor.r = half(sk_FragCoord.y / sk_Height); }",
Ethan Nicholascd700e92018-08-24 16:43:57 -04001296 settings,
1297 "#version 400\n"
1298 "uniform float u_skRTHeight;\n"
1299 "out vec4 sk_FragColor;\n"
1300 "void main() {\n"
1301 " sk_FragColor.x = gl_FragCoord.y / u_skRTHeight;\n"
1302 "}\n",
1303 &inputs);
1304 REPORTER_ASSERT(r, !inputs.fRTWidth);
1305 REPORTER_ASSERT(r, inputs.fRTHeight);
1306}
1307
Chris Dalton49d14e92018-07-27 12:38:35 -06001308DEF_TEST(SkSLClockwise, r) {
1309 test(r,
1310 "void main() { sk_FragColor = half4(sk_Clockwise ? +1 : -1); }",
1311 *SkSL::ShaderCapsFactory::Default(),
1312 "#version 400\n"
1313 "out vec4 sk_FragColor;\n"
1314 "void main() {\n"
1315 " sk_FragColor = vec4(float(gl_FrontFacing ? 1 : -1));\n"
1316 "}\n");
1317}
1318
Chris Daltonb0fd4b12019-10-29 13:41:22 -06001319DEF_TEST(SkSLSampleMask, r) {
1320 test(r,
1321 "void main() { sk_SampleMask[0] |= 8; }",
1322 *SkSL::ShaderCapsFactory::SampleMaskSupport(),
1323 "#version 400\n"
1324 "void main() {\n"
1325 " gl_SampleMask[0] |= 8;\n"
1326 "}\n");
1327}
1328
Ethan Nicholasa51740c2017-02-07 14:53:32 -05001329DEF_TEST(SkSLVertexID, r) {
1330 test(r,
1331 "out int id; void main() { id = sk_VertexID; }",
1332 *SkSL::ShaderCapsFactory::Default(),
1333 "#version 400\n"
1334 "out int id;\n"
1335 "void main() {\n"
1336 " id = gl_VertexID;\n"
1337 "}\n",
1338 SkSL::Program::kVertex_Kind);
1339}
1340
Chris Dalton8580d512017-10-14 22:12:33 -06001341DEF_TEST(SkSLInstanceID, r) {
1342 test(r,
1343 "out int id; void main() { id = sk_InstanceID; }",
1344 *SkSL::ShaderCapsFactory::Default(),
1345 "#version 400\n"
1346 "out int id;\n"
1347 "void main() {\n"
1348 " id = gl_InstanceID;\n"
1349 "}\n",
1350 SkSL::Program::kVertex_Kind);
1351}
1352
Ethan Nicholas67d64602017-02-09 10:15:25 -05001353DEF_TEST(SkSLClipDistance, r) {
1354 test(r,
1355 "void main() { sk_ClipDistance[0] = 0; }",
1356 *SkSL::ShaderCapsFactory::Default(),
1357 "#version 400\n"
1358 "void main() {\n"
1359 " gl_ClipDistance[0] = 0.0;\n"
1360 "}\n",
1361 SkSL::Program::kVertex_Kind);
1362 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001363 "void main() { sk_FragColor = half4(half(sk_ClipDistance[0])); }",
Ethan Nicholas67d64602017-02-09 10:15:25 -05001364 *SkSL::ShaderCapsFactory::Default(),
1365 "#version 400\n"
1366 "out vec4 sk_FragColor;\n"
1367 "void main() {\n"
1368 " sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1369 "}\n");
1370}
Ethan Nicholas50afc172017-02-16 14:49:57 -05001371
1372DEF_TEST(SkSLArrayTypes, r) {
1373 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001374 "void main() { float2 x[2] = float2[2](float2(1), float2(2));"
1375 "float2[2] y = float2[2](float2(3), float2(4));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001376 "sk_FragColor = half4(half2(x[0]), half2(y[1])); }",
Ethan Nicholas50afc172017-02-16 14:49:57 -05001377 *SkSL::ShaderCapsFactory::Default(),
1378 "#version 400\n"
1379 "out vec4 sk_FragColor;\n"
1380 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001381 " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1382 "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -05001383 "}\n");
1384}
1385
Ethan Nicholas58d56482017-12-19 09:29:22 -05001386DEF_TEST(SkSLArrayIndexTypes, r) {
1387 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001388 "void main() { float array[4] = float[4](1, 2, 3, 4);\n"
1389 "short x = 0;\n ushort y = 1;\n int z = 2;\n uint w = 3;\n"
1390 "sk_FragColor = half4(half(array[x]), half(array[y]), half(array[z]), half(array[w])); }",
Ethan Nicholas58d56482017-12-19 09:29:22 -05001391 *SkSL::ShaderCapsFactory::Default(),
1392 "#version 400\n"
1393 "out vec4 sk_FragColor;\n"
1394 "void main() {\n"
1395 " sk_FragColor = vec4(float[4](1.0, 2.0, 3.0, 4.0)[0], "
1396 "float[4](1.0, 2.0, 3.0, 4.0)[1], "
1397 "float[4](1.0, 2.0, 3.0, 4.0)[2], "
1398 "float[4](1.0, 2.0, 3.0, 4.0)[3]);\n"
1399 "}\n");
1400}
1401
Ethan Nicholas52cad152017-02-16 16:37:32 -05001402DEF_TEST(SkSLGeometry, r) {
1403 test(r,
1404 "layout(points) in;"
1405 "layout(invocations = 2) in;"
1406 "layout(line_strip, max_vertices = 2) out;"
1407 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001408 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001409 "EmitVertex();"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001410 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001411 "EmitVertex();"
1412 "EndPrimitive();"
1413 "}",
Chris Dalton8fd79552018-01-11 00:46:14 -05001414 *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
Ethan Nicholas52cad152017-02-16 16:37:32 -05001415 "#version 400\n"
1416 "layout (points) in ;\n"
1417 "layout (invocations = 2) in ;\n"
1418 "layout (line_strip, max_vertices = 2) out ;\n"
1419 "void main() {\n"
1420 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1421 " EmitVertex();\n"
1422 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1423 " EmitVertex();\n"
1424 " EndPrimitive();\n"
1425 "}\n",
1426 SkSL::Program::kGeometry_Kind);
1427}
1428
Ethan Nicholasaf197692017-02-27 13:26:45 -05001429DEF_TEST(SkSLSwitch, r) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001430 // basic "does a switch even work" test
Ethan Nicholasaf197692017-02-27 13:26:45 -05001431 test(r,
1432 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001433 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001434 " switch (int(sqrt(1))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001435 " case 0:"
1436 " x = 0.0;"
1437 " break;"
1438 " case 1:"
1439 " x = 1.0;"
1440 " break;"
1441 " default:"
1442 " x = 2.0;"
1443 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001444 " sk_FragColor = half4(half(x));"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001445 "}",
1446 *SkSL::ShaderCapsFactory::Default(),
1447 "#version 400\n"
1448 "out vec4 sk_FragColor;\n"
1449 "void main() {\n"
1450 " float x;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001451 " switch (int(sqrt(1.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001452 " case 0:\n"
1453 " x = 0.0;\n"
1454 " break;\n"
1455 " case 1:\n"
1456 " x = 1.0;\n"
1457 " break;\n"
1458 " default:\n"
1459 " x = 2.0;\n"
1460 " }\n"
1461 " sk_FragColor = vec4(x);\n"
1462 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001463 // dead code inside of switch
Ethan Nicholasaf197692017-02-27 13:26:45 -05001464 test(r,
1465 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001466 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001467 " switch (int(sqrt(2))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001468 " case 0:"
1469 " x = 0.0;"
1470 " case 1:"
1471 " x = 1.0;"
1472 " default:"
1473 " x = 2.0;"
1474 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001475 " sk_FragColor = half4(half(x));"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001476 "}",
1477 *SkSL::ShaderCapsFactory::Default(),
1478 "#version 400\n"
1479 "out vec4 sk_FragColor;\n"
1480 "void main() {\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001481 " switch (int(sqrt(2.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001482 " case 0:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001483 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001484 " case 1:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001485 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001486 " default:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001487 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001488 " }\n"
1489 " sk_FragColor = vec4(2.0);\n"
1490 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001491 // non-static test w/ fallthrough
Ethan Nicholasaf197692017-02-27 13:26:45 -05001492 test(r,
1493 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001494 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001495 " switch (int(sqrt(3))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001496 " case 0:"
1497 " x = 0.0;"
1498 " case 1:"
1499 " x = 1.0;"
1500 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001501 " sk_FragColor = half4(half(x));"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001502 "}",
1503 *SkSL::ShaderCapsFactory::Default(),
1504 "#version 400\n"
1505 "out vec4 sk_FragColor;\n"
1506 "void main() {\n"
1507 " float x = 0.0;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001508 " switch (int(sqrt(3.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001509 " case 0:\n"
1510 " x = 0.0;\n"
1511 " case 1:\n"
1512 " x = 1.0;\n"
1513 " }\n"
1514 " sk_FragColor = vec4(x);\n"
1515 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001516 // static test w/ fallthrough
1517 test(r,
1518 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001519 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001520 " switch (0) {"
1521 " case 0:"
1522 " x = 0.0;"
1523 " case 1:"
1524 " x = 1.0;"
1525 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001526 " sk_FragColor = half4(half(x));"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001527 "}",
1528 *SkSL::ShaderCapsFactory::Default(),
1529 "#version 400\n"
1530 "out vec4 sk_FragColor;\n"
1531 "void main() {\n"
1532 " sk_FragColor = vec4(1.0);\n"
1533 "}\n");
1534 // static test w/ fallthrough, different entry point
1535 test(r,
1536 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001537 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001538 " switch (1) {"
1539 " case 0:"
1540 " x = 0.0;"
1541 " case 1:"
1542 " x = 1.0;"
1543 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001544 " sk_FragColor = half4(half(x));"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001545 "}",
1546 *SkSL::ShaderCapsFactory::Default(),
1547 "#version 400\n"
1548 "out vec4 sk_FragColor;\n"
1549 "void main() {\n"
1550 " sk_FragColor = vec4(1.0);\n"
1551 "}\n");
1552 // static test w/ break
1553 test(r,
1554 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001555 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001556 " switch (0) {"
1557 " case 0:"
1558 " x = 0.0;"
1559 " break;"
1560 " case 1:"
1561 " x = 1.0;"
1562 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001563 " sk_FragColor = half4(half(x));"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001564 "}",
1565 *SkSL::ShaderCapsFactory::Default(),
1566 "#version 400\n"
1567 "out vec4 sk_FragColor;\n"
1568 "void main() {\n"
1569 " sk_FragColor = vec4(0.0);\n"
1570 "}\n");
1571 // static test w/ static conditional break
1572 test(r,
1573 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001574 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001575 " switch (0) {"
1576 " case 0:"
1577 " x = 0.0;"
1578 " if (x < 1) break;"
1579 " case 1:"
1580 " x = 1.0;"
1581 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001582 " sk_FragColor = half4(half(x));"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001583 "}",
1584 *SkSL::ShaderCapsFactory::Default(),
1585 "#version 400\n"
1586 "out vec4 sk_FragColor;\n"
1587 "void main() {\n"
1588 " sk_FragColor = vec4(0.0);\n"
1589 "}\n");
1590 // static test w/ non-static conditional break
1591 test(r,
1592 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001593 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001594 " switch (0) {"
1595 " case 0:"
1596 " x = 0.0;"
1597 " if (x < sqrt(1)) break;"
1598 " case 1:"
1599 " x = 1.0;"
1600 " }"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001601 " sk_FragColor = half4(half(x));"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001602 "}",
1603 *SkSL::ShaderCapsFactory::Default(),
1604 "#version 400\n"
1605 "out vec4 sk_FragColor;\n"
1606 "void main() {\n"
1607 " float x = 0.0;\n"
1608 " switch (0) {\n"
1609 " case 0:\n"
1610 " x = 0.0;\n"
1611 " if (0.0 < sqrt(1.0)) break;\n"
1612 " case 1:\n"
1613 " x = 1.0;\n"
1614 " }\n"
1615 " sk_FragColor = vec4(x);\n"
1616 "}\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -05001617}
1618
Ethan Nicholas5338f992017-04-19 15:54:07 -04001619DEF_TEST(SkSLRectangleTexture, r) {
1620 test(r,
1621 "uniform sampler2D test;"
1622 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001623 " sk_FragColor = sample(test, float2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001624 "}",
1625 *SkSL::ShaderCapsFactory::Default(),
1626 "#version 400\n"
1627 "out vec4 sk_FragColor;\n"
1628 "uniform sampler2D test;\n"
1629 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001630 " sk_FragColor = texture(test, vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001631 "}\n");
1632 test(r,
1633 "uniform sampler2DRect test;"
1634 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001635 " sk_FragColor = sample(test, float2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001636 "}",
1637 *SkSL::ShaderCapsFactory::Default(),
1638 "#version 400\n"
1639 "out vec4 sk_FragColor;\n"
1640 "uniform sampler2DRect test;\n"
1641 "void main() {\n"
Brian Salomon246bc3d2018-12-06 15:33:02 -05001642 " sk_FragColor = texture(test, vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001643 "}\n");
1644 test(r,
1645 "uniform sampler2DRect test;"
1646 "void main() {"
Ethan Nicholas13863662019-07-29 13:05:15 -04001647 " sk_FragColor = sample(test, float3(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001648 "}",
1649 *SkSL::ShaderCapsFactory::Default(),
1650 "#version 400\n"
1651 "out vec4 sk_FragColor;\n"
1652 "uniform sampler2DRect test;\n"
1653 "void main() {\n"
Brian Salomon246bc3d2018-12-06 15:33:02 -05001654 " sk_FragColor = texture(test, vec3(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001655 "}\n");
1656}
1657
Ethan Nicholascb670962017-04-20 19:31:52 -04001658DEF_TEST(SkSLUnusedVars, r) {
1659 test(r,
1660 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001661 "float a = 1, b = 2, c = 3;"
1662 "float d = c;"
1663 "float e = d;"
Ethan Nicholascb670962017-04-20 19:31:52 -04001664 "b++;"
1665 "d++;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001666 "sk_FragColor = half4(half(b), half(b), half(d), half(d));"
Ethan Nicholascb670962017-04-20 19:31:52 -04001667 "}",
1668 *SkSL::ShaderCapsFactory::Default(),
1669 "#version 400\n"
1670 "out vec4 sk_FragColor;\n"
1671 "void main() {\n"
1672 " float b = 2.0;\n"
1673 " float d = 3.0;\n"
1674 " b++;\n"
1675 " d++;\n"
1676 " sk_FragColor = vec4(b, b, d, d);\n"
1677 "}\n");
1678}
1679
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001680DEF_TEST(SkSLMultipleAssignments, r) {
1681 test(r,
1682 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001683 "float x;"
1684 "float y;"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001685 "int z;"
1686 "x = y = z = 1;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001687 "sk_FragColor = half4(z);"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001688 "}",
1689 *SkSL::ShaderCapsFactory::Default(),
1690 "#version 400\n"
1691 "out vec4 sk_FragColor;\n"
1692 "void main() {\n"
1693 " sk_FragColor = vec4(1.0);\n"
1694 "}\n");
1695}
1696
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001697DEF_TEST(SkSLComplexDelete, r) {
1698 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001699 "uniform float4x4 colorXform;"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001700 "uniform sampler2D sampler;"
1701 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001702 "float4 tmpColor;"
Ethan Nicholas13863662019-07-29 13:05:15 -04001703 "sk_FragColor = half4(1.0) * (tmpColor = sample(sampler, float2(1)) , "
Ethan Nicholase1f55022019-02-05 17:17:40 -05001704 "half4(colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * "
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001705 "float4(tmpColor.xyz, 1.0)).xyz, "
Ethan Nicholase1f55022019-02-05 17:17:40 -05001706 "0.0, tmpColor.w), tmpColor.w) : tmpColor));"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001707 "}",
1708 *SkSL::ShaderCapsFactory::Default(),
1709 "#version 400\n"
1710 "out vec4 sk_FragColor;\n"
1711 "uniform mat4 colorXform;\n"
1712 "uniform sampler2D sampler;\n"
1713 "void main() {\n"
1714 " vec4 tmpColor;\n"
1715 " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1716 "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1717 "tmpColor);\n"
1718 "}\n");
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001719}
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001720
1721DEF_TEST(SkSLDependentInitializers, r) {
1722 test(r,
1723 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001724 "float x = 0.5, y = x * 2;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001725 "sk_FragColor = half4(half(y));"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001726 "}",
1727 *SkSL::ShaderCapsFactory::Default(),
1728 "#version 400\n"
1729 "out vec4 sk_FragColor;\n"
1730 "void main() {\n"
1731 " sk_FragColor = vec4(1.0);\n"
1732 "}\n");
1733}
1734
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001735DEF_TEST(SkSLDeadLoopVar, r) {
1736 test(r,
1737 "void main() {"
1738 "for (int x = 0; x < 4; ) {"
1739 "break;"
1740 "}"
1741 "}",
1742 *SkSL::ShaderCapsFactory::Default(),
1743 "#version 400\n"
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001744 "void main() {\n"
1745 " for (; true; ) {\n"
1746 " break;\n"
1747 " }\n"
1748 "}\n"
1749 );
1750}
1751
Chris Dalton8fd79552018-01-11 00:46:14 -05001752DEF_TEST(SkSLGeometryShaders, r) {
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001753 test(r,
1754 "layout(points) in;"
1755 "layout(invocations = 2) in;"
1756 "layout(line_strip, max_vertices = 2) out;"
1757 "void test() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001758 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001759 "EmitVertex();"
1760 "}"
1761 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001762 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001763 "EmitVertex();"
1764 "}",
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001765 *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001766 "#version 400\n"
1767 "int sk_InvocationID;\n"
1768 "layout (points) in ;\n"
1769 "layout (line_strip, max_vertices = 4) out ;\n"
1770 "void test() {\n"
1771 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1772 " EmitVertex();\n"
1773 "}\n"
1774 "void _invoke() {\n"
1775 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1776 " EmitVertex();\n"
1777 "}\n"
1778 "void main() {\n"
1779 " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1780 " _invoke();\n"
1781 " EndPrimitive();\n"
1782 " }\n"
1783 "}\n",
1784 SkSL::Program::kGeometry_Kind);
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001785 test(r,
1786 "layout(points, invocations = 2) in;"
1787 "layout(invocations = 3) in;"
1788 "layout(line_strip, max_vertices = 2) out;"
1789 "void main() {"
1790 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1791 "EmitVertex();"
1792 "EndPrimitive();"
1793 "}",
1794 *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(),
1795 "#version 400\n"
1796 "#extension GL_ARB_gpu_shader5 : require\n"
1797 "layout (points, invocations = 2) in ;\n"
1798 "layout (invocations = 3) in ;\n"
1799 "layout (line_strip, max_vertices = 2) out ;\n"
1800 "void main() {\n"
1801 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1802 " EmitVertex();\n"
1803 " EndPrimitive();\n"
1804 "}\n",
1805 SkSL::Program::kGeometry_Kind);
Chris Dalton8fd79552018-01-11 00:46:14 -05001806 test(r,
1807 "layout(points, invocations = 2) in;"
1808 "layout(invocations = 3) in;"
1809 "layout(line_strip, max_vertices = 2) out;"
1810 "void main() {"
1811 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1812 "EmitVertex();"
1813 "EndPrimitive();"
1814 "}",
1815 *SkSL::ShaderCapsFactory::GeometryShaderExtensionString(),
1816 "#version 310es\n"
1817 "#extension GL_EXT_geometry_shader : require\n"
1818 "layout (points, invocations = 2) in ;\n"
1819 "layout (invocations = 3) in ;\n"
1820 "layout (line_strip, max_vertices = 2) out ;\n"
1821 "void main() {\n"
1822 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1823 " EmitVertex();\n"
1824 " EndPrimitive();\n"
1825 "}\n",
1826 SkSL::Program::kGeometry_Kind);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001827}
1828
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001829DEF_TEST(SkSLTypePrecision, r) {
1830 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001831 "float f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001832 "half h = 2;"
1833 "double d = 3;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001834 "float2 f2 = float2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001835 "half3 h3 = half3(1, 2, 3);"
1836 "double4 d4 = double4(1, 2, 3, 4);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001837 "float2x2 f22 = float2x2(1, 2, 3, 4);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001838 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001839 "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);"
1840 "void main() {"
1841 "sk_FragColor.r = half(f + h + d + f2.x + h3.x + d4.x + f22[0][0] + h24[0][0] + "
1842 "d42[0][0]);"
1843 "}",
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001844 *SkSL::ShaderCapsFactory::Default(),
1845 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001846 "out vec4 sk_FragColor;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001847 "float f = 1.0;\n"
1848 "float h = 2.0;\n"
1849 "double d = 3.0;\n"
1850 "vec2 f2 = vec2(1.0, 2.0);\n"
1851 "vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1852 "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n"
1853 "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1854 "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001855 "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1856 "void main() {\n"
1857 " sk_FragColor.x = float(((((((double(f + h) + d) + double(f2.x)) + double(h3.x)) + "
1858 "d4.x) + double(f22[0][0])) + double(h24[0][0])) + d42[0][0]);\n"
1859 "}\n");
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001860 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001861 "float f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001862 "half h = 2;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001863 "float2 f2 = float2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001864 "half3 h3 = half3(1, 2, 3);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001865 "float2x2 f22 = float2x2(1, 2, 3, 4);"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001866 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1867 "void main() {"
1868 "sk_FragColor.r = half(f + h + f2.x + h3.x + f22[0][0] + h24[0][0]);"
1869 "}",
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001870 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1871 "#version 400\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001872 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -04001873 "precision mediump sampler2D;\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001874 "out mediump vec4 sk_FragColor;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001875 "highp float f = 1.0;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001876 "mediump float h = 2.0;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001877 "highp vec2 f2 = vec2(1.0, 2.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001878 "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001879 "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001880 "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1881 "void main() {\n"
1882 " sk_FragColor.x = ((((f + h) + f2.x) + h3.x) + f22[0][0]) + h24[0][0];\n"
1883 "}\n");
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001884}
1885
1886DEF_TEST(SkSLNumberConversions, r) {
1887 test(r,
1888 "short s = short(sqrt(1));"
1889 "int i = int(sqrt(1));"
1890 "ushort us = ushort(sqrt(1));"
1891 "uint ui = uint(sqrt(1));"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001892 "half h = half(sqrt(1));"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001893 "float f = sqrt(1);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001894 "short s2s = s;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001895 "short i2s = short(i);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001896 "short us2s = short(us);"
1897 "short ui2s = short(ui);"
1898 "short h2s = short(h);"
1899 "short f2s = short(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001900 "int s2i = s;"
1901 "int i2i = i;"
1902 "int us2i = int(us);"
1903 "int ui2i = int(ui);"
1904 "int h2i = int(h);"
1905 "int f2i = int(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001906 "ushort s2us = ushort(s);"
1907 "ushort i2us = ushort(i);"
1908 "ushort us2us = us;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05001909 "ushort ui2us = ushort(ui);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001910 "ushort h2us = ushort(h);"
1911 "ushort f2us = ushort(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001912 "uint s2ui = uint(s);"
1913 "uint i2ui = uint(i);"
1914 "uint us2ui = us;"
1915 "uint ui2ui = ui;"
1916 "uint h2ui = uint(h);"
1917 "uint f2ui = uint(f);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001918 "float s2f = s;"
1919 "float i2f = i;"
1920 "float us2f = us;"
1921 "float ui2f = ui;"
1922 "float h2f = h;"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001923 "float f2f = f;"
1924 "void main() {"
1925 "sk_FragColor.r = half(s + i + us + half(ui) + h + f + s2s + i2s + us2s + ui2s + h2s + "
Ethan Nicholasfc994162019-06-06 10:04:27 -04001926 "f2s + s2i + i2i + us2i + ui2i + h2i + f2i + s2us + i2us + us2us);"
1927 "sk_FragColor.r += half(ui2us + h2us + f2us + half(s2ui) + half(i2ui) + half(us2ui) + "
1928 "half(ui2ui) + half(h2ui) + half(f2ui) + s2f + i2f + us2f + ui2f + "
1929 "h2f + f2f);"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001930 "}",
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001931 *SkSL::ShaderCapsFactory::Default(),
1932 "#version 400\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001933 "out vec4 sk_FragColor;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001934 "int s = int(sqrt(1.0));\n"
1935 "int i = int(sqrt(1.0));\n"
1936 "uint us = uint(sqrt(1.0));\n"
1937 "uint ui = uint(sqrt(1.0));\n"
1938 "float h = sqrt(1.0);\n"
1939 "float f = sqrt(1.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001940 "int s2s = s;\n"
1941 "int i2s = i;\n"
1942 "int us2s = int(us);\n"
1943 "int ui2s = int(ui);\n"
1944 "int h2s = int(h);\n"
1945 "int f2s = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001946 "int s2i = s;\n"
1947 "int i2i = i;\n"
1948 "int us2i = int(us);\n"
1949 "int ui2i = int(ui);\n"
1950 "int h2i = int(h);\n"
1951 "int f2i = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001952 "uint s2us = uint(s);\n"
1953 "uint i2us = uint(i);\n"
1954 "uint us2us = us;\n"
1955 "uint ui2us = ui;\n"
1956 "uint h2us = uint(h);\n"
1957 "uint f2us = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001958 "uint s2ui = uint(s);\n"
1959 "uint i2ui = uint(i);\n"
1960 "uint us2ui = us;\n"
1961 "uint ui2ui = ui;\n"
1962 "uint h2ui = uint(h);\n"
1963 "uint f2ui = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001964 "float s2f = float(s);\n"
1965 "float i2f = float(i);\n"
1966 "float us2f = float(us);\n"
1967 "float ui2f = float(ui);\n"
1968 "float h2f = h;\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001969 "float f2f = f;\n"
1970 "void main() {\n"
Ethan Nicholasfc994162019-06-06 10:04:27 -04001971 " sk_FragColor.x = (((((((((((((((((float((s + i) + int(us)) + float(ui)) + h) + f) + "
1972 "float(s2s)) + float(i2s)) + float(us2s)) + float(ui2s)) + float(h2s)) + float(f2s)) + "
1973 "float(s2i)) + float(i2i)) + float(us2i)) + float(ui2i)) + float(h2i)) + float(f2i)) + "
1974 "float(s2us)) + float(i2us)) + float(us2us);\n"
1975 " sk_FragColor.x += (((((((((((float((ui2us + h2us) + f2us) + float(s2ui)) + "
1976 "float(i2ui)) + float(us2ui)) + float(ui2ui)) + float(h2ui)) + float(f2ui)) + s2f) + "
1977 "i2f) + us2f) + ui2f) + h2f) + f2f;\n"
Ethan Nicholas0dc80872019-02-08 15:46:24 -05001978 "}\n");
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001979}
1980
1981DEF_TEST(SkSLForceHighPrecision, r) {
1982 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05001983 "void main() {\n half x = half(sqrt(1));\n half4 y = half4(x);\n sk_FragColor = y;\n }",
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001984 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1985 "#version 400\n"
1986 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -04001987 "precision mediump sampler2D;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001988 "out mediump vec4 sk_FragColor;\n"
1989 "void main() {\n"
1990 " mediump float x = sqrt(1.0);\n"
1991 " mediump vec4 y = vec4(x);\n"
1992 " sk_FragColor = y;\n"
1993 "}\n");
1994 SkSL::Program::Settings settings;
1995 settings.fForceHighPrecision = true;
1996 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers();
1997 settings.fCaps = caps.get();
1998 SkSL::Program::Inputs inputs;
1999 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05002000 "void main() { half x = half(sqrt(1)); half4 y = half4(x); sk_FragColor = y; }",
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002001 settings,
2002 "#version 400\n"
2003 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -04002004 "precision mediump sampler2D;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002005 "out mediump vec4 sk_FragColor;\n"
2006 "void main() {\n"
2007 " highp float x = sqrt(1.0);\n"
2008 " highp vec4 y = vec4(x);\n"
2009 " sk_FragColor = y;\n"
2010 "}\n",
2011 &inputs);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04002012}
2013
Robert Phillipsfe8da172018-01-24 14:52:02 +00002014DEF_TEST(SkSLNormalization, r) {
2015 test(r,
2016 "uniform float4 sk_RTAdjust; void main() { sk_Position = half4(1); }",
2017 *SkSL::ShaderCapsFactory::Default(),
2018 "#version 400\n"
2019 "uniform vec4 sk_RTAdjust;\n"
2020 "void main() {\n"
2021 " gl_Position = vec4(1.0);\n"
Ethan Nicholasb809efb2018-04-12 14:39:21 -04002022 " gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2023 "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
Robert Phillipsfe8da172018-01-24 14:52:02 +00002024 "}\n",
2025 SkSL::Program::kVertex_Kind);
2026 test(r,
2027 "uniform float4 sk_RTAdjust;"
2028 "layout(points) in;"
2029 "layout(invocations = 2) in;"
2030 "layout(line_strip, max_vertices = 2) out;"
2031 "void main() {"
2032 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
2033 "EmitVertex();"
2034 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
2035 "EmitVertex();"
2036 "EndPrimitive();"
2037 "}",
2038 *SkSL::ShaderCapsFactory::GeometryShaderSupport(),
2039 "#version 400\n"
2040 "uniform vec4 sk_RTAdjust;\n"
2041 "layout (points) in ;\n"
2042 "layout (invocations = 2) in ;\n"
2043 "layout (line_strip, max_vertices = 2) out ;\n"
2044 "void main() {\n"
2045 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
2046 " {\n"
Ethan Nicholasb809efb2018-04-12 14:39:21 -04002047 " gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2048 "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
Robert Phillipsfe8da172018-01-24 14:52:02 +00002049 " EmitVertex();\n"
2050 " }\n"
2051 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
2052 " {\n"
Ethan Nicholasb809efb2018-04-12 14:39:21 -04002053 " gl_Position = vec4(gl_Position.xy * sk_RTAdjust.xz + gl_Position.ww * "
2054 "sk_RTAdjust.yw, 0.0, gl_Position.w);\n"
Robert Phillipsfe8da172018-01-24 14:52:02 +00002055 " EmitVertex();\n"
2056 " }\n"
2057 " EndPrimitive();\n"
2058 "}\n",
2059 SkSL::Program::kGeometry_Kind);
2060}
2061
Ethan Nicholasa583b812018-01-18 13:32:11 -05002062DEF_TEST(SkSLTernaryLValue, r) {
2063 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05002064 "void main() { int r, g; (true ? r : g) = 1; (false ? r : g) = 0; "
Ethan Nicholasa583b812018-01-18 13:32:11 -05002065 "sk_FragColor = half4(r, g, 1, 1); }",
2066 *SkSL::ShaderCapsFactory::Default(),
2067 "#version 400\n"
2068 "out vec4 sk_FragColor;\n"
2069 "void main() {\n"
2070 " sk_FragColor = vec4(1.0, 0.0, 1.0, 1.0);\n"
2071 "}\n");
2072 test(r,
Ethan Nicholase1f55022019-02-05 17:17:40 -05002073 "void main() { half r, g; (true ? r : g) = half(sqrt(1)); (false ? r : g) = half(sqrt(0));"
Ethan Nicholasa583b812018-01-18 13:32:11 -05002074 "sk_FragColor = half4(r, g, 1, 1); }",
2075 *SkSL::ShaderCapsFactory::Default(),
2076 "#version 400\n"
2077 "out vec4 sk_FragColor;\n"
2078 "void main() {\n"
2079 " float r, g;\n"
2080 " r = sqrt(1.0);\n"
2081 " g = sqrt(0.0);\n"
2082 " sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
2083 "}\n");
2084 test(r,
2085 "void main() {"
2086 "half r, g;"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002087 "(sqrt(1) > 0 ? r : g) = half(sqrt(1));"
2088 "(sqrt(0) > 0 ? r : g) = half(sqrt(0));"
Ethan Nicholasa583b812018-01-18 13:32:11 -05002089 "sk_FragColor = half4(r, g, 1, 1);"
2090 "}",
2091 *SkSL::ShaderCapsFactory::Default(),
2092 "#version 400\n"
2093 "out vec4 sk_FragColor;\n"
2094 "void main() {\n"
2095 " float r, g;\n"
2096 " sqrt(1.0) > 0.0 ? r : g = sqrt(1.0);\n"
2097 " sqrt(0.0) > 0.0 ? r : g = sqrt(0.0);\n"
2098 " sk_FragColor = vec4(r, g, 1.0, 1.0);\n"
2099 "}\n");
2100}
2101
Chris Daltonc2d0dd62018-03-07 07:46:10 -07002102DEF_TEST(SkSLIncompleteShortIntPrecision, r) {
2103 test(r,
2104 "uniform sampler2D tex;"
2105 "in float2 texcoord;"
2106 "in short2 offset;"
2107 "void main() {"
2108 " short scalar = offset.y;"
Ethan Nicholas13863662019-07-29 13:05:15 -04002109 " sk_FragColor = sample(tex, texcoord + float2(offset * scalar));"
Chris Daltonc2d0dd62018-03-07 07:46:10 -07002110 "}",
2111 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
2112 "#version 400\n"
2113 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -04002114 "precision mediump sampler2D;\n"
Chris Daltonc2d0dd62018-03-07 07:46:10 -07002115 "out mediump vec4 sk_FragColor;\n"
2116 "uniform sampler2D tex;\n"
2117 "in highp vec2 texcoord;\n"
2118 "in mediump ivec2 offset;\n"
2119 "void main() {\n"
2120 " mediump int scalar = offset.y;\n"
2121 " sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2122 "}\n",
2123 SkSL::Program::kFragment_Kind);
2124 test(r,
2125 "uniform sampler2D tex;"
2126 "in float2 texcoord;"
2127 "in short2 offset;"
2128 "void main() {"
2129 " short scalar = offset.y;"
Ethan Nicholas13863662019-07-29 13:05:15 -04002130 " sk_FragColor = sample(tex, texcoord + float2(offset * scalar));"
Chris Daltonc2d0dd62018-03-07 07:46:10 -07002131 "}",
2132 *SkSL::ShaderCapsFactory::IncompleteShortIntPrecision(),
2133 "#version 310es\n"
2134 "precision mediump float;\n"
Brian Salomon67529b22019-08-13 15:31:04 -04002135 "precision mediump sampler2D;\n"
Chris Daltonc2d0dd62018-03-07 07:46:10 -07002136 "out mediump vec4 sk_FragColor;\n"
2137 "uniform sampler2D tex;\n"
2138 "in highp vec2 texcoord;\n"
2139 "in highp ivec2 offset;\n"
2140 "void main() {\n"
2141 " highp int scalar = offset.y;\n"
2142 " sk_FragColor = texture(tex, texcoord + vec2(offset * scalar));\n"
2143 "}\n",
2144 SkSL::Program::kFragment_Kind);
2145}
2146
Ethan Nicholasc6a19f12018-03-29 16:46:56 -04002147DEF_TEST(SkSLFrExp, r) {
2148 test(r,
2149 "void main() {"
2150 " int exp;"
2151 " float foo = frexp(0.5, exp);"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002152 " sk_FragColor = half4(exp);"
Ethan Nicholasc6a19f12018-03-29 16:46:56 -04002153 "}",
2154 *SkSL::ShaderCapsFactory::Default(),
2155 "#version 400\n"
2156 "out vec4 sk_FragColor;\n"
2157 "void main() {\n"
2158 " int exp;\n"
2159 " float foo = frexp(0.5, exp);\n"
2160 " sk_FragColor = vec4(float(exp));\n"
2161 "}\n");
2162}
Adrienne Walkeree8295c2018-08-21 10:56:30 -07002163
2164DEF_TEST(SkSLWorkaroundAddAndTrueToLoopCondition, r) {
2165 test(r,
2166 "void main() {"
2167 " int c = 0;"
2168 " for (int i = 0; i < 4 || c < 10; ++i) {"
2169 " c += 1;"
2170 " }"
2171 "}",
2172 *SkSL::ShaderCapsFactory::AddAndTrueToLoopCondition(),
2173 "#version 400\n"
Adrienne Walkeree8295c2018-08-21 10:56:30 -07002174 "void main() {\n"
2175 " int c = 0;\n"
2176 " for (int i = 0;(i < 4 || c < 10) && true; ++i) {\n"
2177 " c += 1;\n"
2178 " }\n"
2179 "}\n",
2180 SkSL::Program::kFragment_Kind
2181 );
2182}
Adrienne Walkerc02165f2018-08-21 11:08:11 -07002183
2184DEF_TEST(SkSLWorkaroundUnfoldShortCircuitAsTernary, r) {
2185 test(r,
2186 "uniform bool x;"
2187 "uniform bool y;"
2188 "uniform int i;"
2189 "uniform int j;"
2190 "void main() {"
2191 " bool andXY = x && y;"
2192 " bool orXY = x || y;"
2193 " bool combo = (x && y) || (x || y);"
2194 " bool prec = (i + j == 3) && y;"
2195 " while (andXY && orXY && combo && prec) {"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002196 " sk_FragColor = half4(0);"
Adrienne Walkerc02165f2018-08-21 11:08:11 -07002197 " break;"
2198 " }"
2199 "}",
2200 *SkSL::ShaderCapsFactory::UnfoldShortCircuitAsTernary(),
2201 "#version 400\n"
2202 "out vec4 sk_FragColor;\n"
2203 "uniform bool x;\n"
2204 "uniform bool y;\n"
2205 "uniform int i;\n"
2206 "uniform int j;\n"
2207 "void main() {\n"
2208 " bool andXY = x ? y : false;\n"
2209 " bool orXY = x ? true : y;\n"
2210 " bool combo = (x ? y : false) ? true : (x ? true : y);\n"
2211 " bool prec = i + j == 3 ? y : false;\n"
2212 " while (((andXY ? orXY : false) ? combo : false) ? prec : false) {\n"
2213 " sk_FragColor = vec4(0.0);\n"
2214 " break;\n"
2215 " }\n"
2216 "}\n",
2217 SkSL::Program::kFragment_Kind
2218 );
2219}
Adrienne Walker92b161f2018-08-22 10:41:52 -07002220
2221DEF_TEST(SkSLWorkaroundEmulateAbsIntFunction, r) {
2222 test(r,
2223 "uniform int i;"
2224 "uniform float f;"
2225 "void main() {"
2226 " float output = abs(f) + abs(i);"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002227 " sk_FragColor = half4(half(output));"
Adrienne Walker92b161f2018-08-22 10:41:52 -07002228 "}",
2229 *SkSL::ShaderCapsFactory::EmulateAbsIntFunction(),
2230 "#version 400\n"
2231 "int _absemulation(int x) {\n"
2232 " return x * sign(x);\n"
2233 "}\n"
2234 "out vec4 sk_FragColor;\n"
2235 "uniform int i;\n"
2236 "uniform float f;\n"
2237 "void main() {\n"
2238 " float output = abs(f) + float(_absemulation(i));\n"
2239 " sk_FragColor = vec4(output);\n"
2240 "}\n",
2241 SkSL::Program::kFragment_Kind
2242 );
2243}
Adrienne Walker8b23ca62018-08-22 10:45:41 -07002244
2245DEF_TEST(SkSLWorkaroundRewriteDoWhileLoops, r) {
2246 test(r,
2247 "void main() {"
2248 " int i = 0;"
2249 " do {"
2250 " ++i;"
2251 " do {"
2252 " i++;"
2253 " } while (true);"
2254 " } while (i < 10);"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002255 " sk_FragColor = half4(i);"
Adrienne Walker8b23ca62018-08-22 10:45:41 -07002256 "}",
2257 *SkSL::ShaderCapsFactory::RewriteDoWhileLoops(),
2258 "#version 400\n"
2259 "out vec4 sk_FragColor;\n"
2260 "void main() {\n"
2261 " int i = 0;\n"
2262 " bool _tmpLoopSeenOnce0 = false;\n"
2263 " while (true) {\n"
2264 " if (_tmpLoopSeenOnce0) {\n"
2265 " if (!(i < 10)) {\n"
2266 " break;\n"
2267 " }\n"
2268 " }\n"
2269 " _tmpLoopSeenOnce0 = true;\n"
2270 " {\n"
2271 " ++i;\n"
2272 " bool _tmpLoopSeenOnce1 = false;\n"
2273 " while (true) {\n"
2274 " if (_tmpLoopSeenOnce1) {\n"
2275 " if (!true) {\n"
2276 " break;\n"
2277 " }\n"
2278 " }\n"
2279 " _tmpLoopSeenOnce1 = true;\n"
2280 " {\n"
2281 " i++;\n"
2282 " }\n"
2283 " }\n"
2284 " }\n"
2285 " }\n"
2286 " sk_FragColor = vec4(float(i));\n"
2287 "}\n",
2288 SkSL::Program::kFragment_Kind
2289 );
2290}
Adrienne Walker2f4c09b2018-08-22 16:04:57 -07002291
2292DEF_TEST(SkSLWorkaroundRemovePowWithConstantExponent, r) {
2293 test(r,
2294 "uniform float x;"
2295 "uniform float y;"
2296 "void main() {"
2297 " float z = pow(x + 1.0, y + 2.0);"
Ethan Nicholase1f55022019-02-05 17:17:40 -05002298 " sk_FragColor = half4(half(z));"
Adrienne Walker2f4c09b2018-08-22 16:04:57 -07002299 "}",
2300 *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2301 "#version 400\n"
2302 "out vec4 sk_FragColor;\n"
2303 "uniform float x;\n"
2304 "uniform float y;\n"
2305 "void main() {\n"
2306 " float z = exp2((y + 2.0) * log2(x + 1.0));\n"
2307 " sk_FragColor = vec4(z);\n"
2308 "}\n",
2309 SkSL::Program::kFragment_Kind
2310 );
2311}
Ethan Nicholasac285b12019-02-12 16:05:18 -05002312
Ethan Nicholase455f652019-09-13 12:52:55 -04002313DEF_TEST(SkSLSwizzleLTRB, r) {
2314 test(r,
2315 "void main() {"
2316 " sk_FragColor = sk_FragColor.BRTL;"
2317 "}",
2318 *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2319 "#version 400\n"
2320 "out vec4 sk_FragColor;\n"
2321 "void main() {\n"
2322 " sk_FragColor = sk_FragColor.wzyx;\n"
2323 "}\n",
2324 SkSL::Program::kFragment_Kind
2325 );
2326}
2327
Ethan Nicholasac285b12019-02-12 16:05:18 -05002328DEF_TEST(SkSLSwizzleConstants, r) {
2329 test(r,
2330 "void main() {"
2331 " half4 v = half4(half(sqrt(1)));"
Ethan Nicholase455f652019-09-13 12:52:55 -04002332 " sk_FragColor = half4(v.x, 1, 1, 1);"
2333 " sk_FragColor = half4(v.1, 1, 1, 1);"
2334 " sk_FragColor = half4(v.xy, 1, 1);"
2335 " sk_FragColor = half4(v.x1, 1, 1);"
2336 " sk_FragColor = half4(v.0y, 1, 1);"
2337 " sk_FragColor = half4(v.10, 1, 1);"
2338 " sk_FragColor = half4(v.xyz, 1);"
2339 " sk_FragColor = half4(v.xy1, 1);"
2340 " sk_FragColor = half4(v.x0z, 1);"
2341 " sk_FragColor = half4(v.x10, 1);"
2342 " sk_FragColor = half4(v.1yz, 1);"
2343 " sk_FragColor = half4(v.0y1, 1);"
2344 " sk_FragColor = half4(v.11z, 1);"
2345 " sk_FragColor = half4(v.101, 1);"
2346 " sk_FragColor = v.xyzw;"
2347 " sk_FragColor = v.xyz1;"
2348 " sk_FragColor = v.xy0w;"
2349 " sk_FragColor = v.xy10;"
2350 " sk_FragColor = v.x1zw;"
2351 " sk_FragColor = v.x0z1;"
2352 " sk_FragColor = v.x11w;"
2353 " sk_FragColor = v.x101;"
2354 " sk_FragColor = v.1yzw;"
2355 " sk_FragColor = v.0yz1;"
2356 " sk_FragColor = v.0y1w;"
2357 " sk_FragColor = v.1y11;"
2358 " sk_FragColor = v.00zw;"
2359 " sk_FragColor = v.00z1;"
2360 " sk_FragColor = v.011w;"
2361 " sk_FragColor = v.1101;"
Ethan Nicholasac285b12019-02-12 16:05:18 -05002362 "}",
2363 *SkSL::ShaderCapsFactory::RemovePowWithConstantExponent(),
2364 "#version 400\n"
2365 "out vec4 sk_FragColor;\n"
2366 "void main() {\n"
2367 " vec4 v = vec4(sqrt(1.0));\n"
Ethan Nicholase455f652019-09-13 12:52:55 -04002368 " sk_FragColor = vec4(v.x, 1.0, 1.0, 1.0);\n"
2369 " sk_FragColor = vec4(1, 1.0, 1.0, 1.0);\n"
2370 " sk_FragColor = vec4(v.xy, 1.0, 1.0);\n"
2371 " sk_FragColor = vec4(vec2(v.x, 1), 1.0, 1.0);\n"
2372 " sk_FragColor = vec4(vec2(0, v.y), 1.0, 1.0);\n"
2373 " sk_FragColor = vec4(vec2(1, 0), 1.0, 1.0);\n"
2374 " sk_FragColor = vec4(v.xyz, 1.0);\n"
2375 " sk_FragColor = vec4(vec3(v.xy, 1), 1.0);\n"
2376 " sk_FragColor = vec4(vec3(v.xz, 0).xzy, 1.0);\n"
2377 " sk_FragColor = vec4(vec3(v.x, 1, 0), 1.0);\n"
2378 " sk_FragColor = vec4(vec3(1, v.yz), 1.0);\n"
2379 " sk_FragColor = vec4(vec3(v.y, 0, 1).yxz, 1.0);\n"
2380 " sk_FragColor = vec4(vec3(1, 1, v.z), 1.0);\n"
2381 " sk_FragColor = vec4(vec3(1, 0, 1), 1.0);\n"
Ethan Nicholas409f6f02019-09-17 12:34:39 -04002382 " sk_FragColor = v;\n"
Ethan Nicholasac285b12019-02-12 16:05:18 -05002383 " sk_FragColor = vec4(v.xyz, 1);\n"
Ethan Nicholase455f652019-09-13 12:52:55 -04002384 " sk_FragColor = vec4(v.xyw, 0).xywz;\n"
2385 " sk_FragColor = vec4(v.xy, 1, 0);\n"
2386 " sk_FragColor = vec4(v.xzw, 1).xwyz;\n"
2387 " sk_FragColor = vec4(v.xz, 0, 1).xzyw;\n"
2388 " sk_FragColor = vec4(v.xw, 1, 1).xzwy;\n"
2389 " sk_FragColor = vec4(v.x, 1, 0, 1);\n"
2390 " sk_FragColor = vec4(1, v.yzw);\n"
2391 " sk_FragColor = vec4(v.yz, 0, 1).zxyw;\n"
2392 " sk_FragColor = vec4(v.yw, 0, 1).zxwy;\n"
2393 " sk_FragColor = vec4(v.y, 1, 1, 1).yxzw;\n"
2394 " sk_FragColor = vec4(0, 0, v.zw);\n"
2395 " sk_FragColor = vec4(v.z, 0, 0, 1).yzxw;\n"
2396 " sk_FragColor = vec4(0, 1, 1, v.w);\n"
2397 " sk_FragColor = vec4(1, 1, 0, 1);\n"
Ethan Nicholasac285b12019-02-12 16:05:18 -05002398 "}\n",
2399 SkSL::Program::kFragment_Kind
2400 );
2401}
Ethan Nicholasd188c182019-06-10 15:55:38 -04002402
Ethan Nicholas409f6f02019-09-17 12:34:39 -04002403DEF_TEST(SkSLSwizzleOpt, r) {
2404 test(r,
2405 "void main() {"
2406 " half v = half(sqrt(1));"
2407 " sk_FragColor = half4(v).rgba;"
2408 " sk_FragColor = half4(v).rgb0.abgr;"
2409 " sk_FragColor = half4(v).rgba.00ra;"
2410 " sk_FragColor = half4(v).rgba.rrra.00ra.11ab;"
2411 " sk_FragColor = half4(v).abga.gb11;"
2412 " sk_FragColor = half4(half3(v).rrr.000, 1);"
2413 " sk_FragColor = half4(half3(v).000.rrr, 1);"
2414 " sk_FragColor = half4(v).abgr.abgr;"
2415 " sk_FragColor = half4(half4(v).rrrr.bb, 1, 1);"
2416 " sk_FragColor = half4(half4(v).ba.grgr);"
2417 "}",
2418 *SkSL::ShaderCapsFactory::Default(),
2419 "#version 400\n"
2420 "out vec4 sk_FragColor;\n"
2421 "void main() {\n"
2422 " float v = sqrt(1.0);\n"
2423 " sk_FragColor = vec4(v);\n"
2424 " sk_FragColor = vec4(0, vec4(v).zyx);\n"
2425 " sk_FragColor = vec4(0, 0, vec4(v).xw);\n"
2426 " sk_FragColor = vec4(1, 1, vec4(v).wx);\n"
2427 " sk_FragColor = vec4(vec4(v).zy, 1, 1);\n"
2428 " sk_FragColor = vec4(vec3(0, 0, 0), 1.0);\n"
2429 " sk_FragColor = vec4(vec3(0, 0, 0), 1.0);\n"
2430 " sk_FragColor = vec4(v);\n"
2431 " sk_FragColor = vec4(vec4(v).xx, 1.0, 1.0);\n"
2432 " sk_FragColor = vec4(v).wzwz;\n"
2433 "}\n",
2434 SkSL::Program::kFragment_Kind
2435 );
2436}
2437
Ethan Nicholasd188c182019-06-10 15:55:38 -04002438DEF_TEST(SkSLNegatedVectorLiteral, r) {
2439 test(r,
2440 "void main() {"
2441 " if (half4(1) == half4(-half2(-1), half2(1)))"
2442 " sk_FragColor = half4(0, 1, 0, 1);"
2443 " else"
2444 " sk_FragColor = half4(1, 0, 0, 1);"
2445 "}",
2446 *SkSL::ShaderCapsFactory::Default(),
2447 "#version 400\n"
2448 "out vec4 sk_FragColor;\n"
2449 "void main() {\n"
2450 " sk_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
2451 "}\n");
2452}