blob: 851c0e8a34a91090bcb3865de7e73bdddf808498 [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
8#include "SkSLCompiler.h"
9
10#include "Test.h"
11
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050012#if SK_SUPPORT_GPU
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050013
Ethan Nicholascb670962017-04-20 19:31:52 -040014// Note that the optimizer will aggressively kill dead code and substitute constants in place of
15// variables, so we have to jump through a few hoops to ensure that the code in these tests has the
16// necessary side-effects to remain live. In some cases we rely on the optimizer not (yet) being
17// smart enough to optimize around certain constructs; as the optimizer gets smarter it will
18// undoubtedly end up breaking some of these tests. That is a good thing, as long as the new code is
19// equivalent!
20
Ethan Nicholas941e7e22016-12-12 15:33:30 -050021static void test(skiatest::Reporter* r, const char* src, const SkSL::Program::Settings& settings,
Ethan Nicholasa51740c2017-02-07 14:53:32 -050022 const char* expected, SkSL::Program::Inputs* inputs,
23 SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
ethannicholasf789b382016-08-03 12:43:36 -070024 SkSL::Compiler compiler;
Ethan Nicholas0df1b042017-03-31 13:56:23 -040025 SkSL::String output;
Brian Osman93ba0a42017-08-14 14:48:10 -040026 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkSL::String(src),
27 settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050028 if (!program) {
ethannicholasf789b382016-08-03 12:43:36 -070029 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
30 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050031 REPORTER_ASSERT(r, program);
32 *inputs = program->fInputs;
33 REPORTER_ASSERT(r, compiler.toGLSL(*program, &output));
34 if (program) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040035 SkSL::String skExpected(expected);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050036 if (output != skExpected) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -050037 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
ethannicholasf789b382016-08-03 12:43:36 -070038 expected, output.c_str());
39 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050040 REPORTER_ASSERT(r, output == skExpected);
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 Nicholas05d5a132017-09-15 11:42:17 -040054 "void main() { sk_FragColor = highfloat4(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 Nicholas05d5a132017-09-15 11:42:17 -040066 "if (sqrt(2) > 5) { sk_FragColor = highfloat4(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 Nicholas9e1138d2016-11-21 10:39:35 -050069 "do { sk_FragColor += 0.01; } 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 Nicholas7ef4b742016-11-11 15:16:46 -050090 " sk_FragColor += 0.01;\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 Nicholas05d5a132017-09-15 11:42:17 -0400101 "highfloat foo(highfloat v[2]) { return v[0] * v[1]; }"
102 "void bar(inout highfloat x) { highfloat y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
103 "void main() { highfloat x = 10; bar(x); sk_FragColor = highfloat4(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 Nicholas05d5a132017-09-15 11:42:17 -0400127 "highfloat 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 Nicholas05d5a132017-09-15 11:42:17 -0400145 "x = (highfloat2(sqrt(1)) , 6);"
146 "z = (highfloat2(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"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500150 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700151 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700152 " float x = 1.0, y = 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700153 " int z = 3;\n"
Ethan Nicholas86a43402017-01-19 13:32:00 -0500154 " x = -6.0;\n"
155 " y = -1.0;\n"
156 " z = 8;\n"
157 " bool b = false == true || 2.0 >= sqrt(2.0) && true;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700158 " x += 12.0;\n"
159 " x -= 12.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700160 " x *= (y /= float(z = 10));\n"
161 " b ||= false;\n"
162 " b &&= true;\n"
163 " b ^^= false;\n"
164 " z |= 0;\n"
165 " z &= -1;\n"
166 " z ^= 0;\n"
167 " z >>= 2;\n"
168 " z <<= 4;\n"
169 " z %= 5;\n"
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400170 " x = float((vec2(sqrt(1.0)) , 6));\n"
171 " z = (vec2(sqrt(1.0)) , 6);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700172 "}\n");
173}
174
175DEF_TEST(SkSLMatrices, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700176 test(r,
177 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400178 "highfloat2x4 x = highfloat2x4(1);"
179 "highfloat3x2 y = highfloat3x2(1, 0, 0, 1, highfloat2(2, 2));"
180 "highfloat3x4 z = x * y;"
181 "highfloat3 v1 = highfloat3x3(1) * highfloat3(2);"
182 "highfloat3 v2 = highfloat3(2) * highfloat3x3(1);"
183 "sk_FragColor = highfloat4(z[0].x, v1 + v2);"
ethannicholasf789b382016-08-03 12:43:36 -0700184 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500185 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700186 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500187 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700188 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400189 " 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 -0400190 " vec3 v1 = mat3(1.0) * vec3(2.0);\n"
191 " vec3 v2 = vec3(2.0) * mat3(1.0);\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400192 " sk_FragColor = vec4(z[0].x, v1 + v2);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700193 "}\n");
194}
195
196DEF_TEST(SkSLInterfaceBlock, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700197 test(r,
198 "uniform testBlock {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400199 "half x;"
200 "half y[2];"
201 "layout(binding=12) half3x2 z;"
ethannicholasf789b382016-08-03 12:43:36 -0700202 "bool w;"
203 "};"
204 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400205 " sk_FragColor = half4(x, y[0], y[1], 0);"
ethannicholasf789b382016-08-03 12:43:36 -0700206 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500207 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700208 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500209 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700210 "uniform testBlock {\n"
211 " float x;\n"
212 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700213 " layout (binding = 12) mat3x2 z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700214 " bool w;\n"
215 "};\n"
216 "void main() {\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500217 " sk_FragColor = vec4(x, y[0], y[1], 0.0);\n"
218 "}\n");
219 test(r,
220 "uniform testBlock {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400221 "highfloat x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500222 "} test;"
223 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400224 " sk_FragColor = half4(test.x);"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500225 "}",
226 *SkSL::ShaderCapsFactory::Default(),
227 "#version 400\n"
228 "out vec4 sk_FragColor;\n"
229 "uniform testBlock {\n"
230 " float x;\n"
231 "} test;\n"
232 "void main() {\n"
233 " sk_FragColor = vec4(test.x);\n"
234 "}\n");
235 test(r,
236 "uniform testBlock {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400237 "highfloat x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500238 "} test[2];"
239 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400240 " sk_FragColor = half4(test[1].x);"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500241 "}",
242 *SkSL::ShaderCapsFactory::Default(),
243 "#version 400\n"
244 "out vec4 sk_FragColor;\n"
245 "uniform testBlock {\n"
246 " float x;\n"
247 "} test[2];\n"
248 "void main() {\n"
249 " sk_FragColor = vec4(test[1].x);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700250 "}\n");
251}
252
253DEF_TEST(SkSLStructs, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700254 test(r,
255 "struct A {"
256 "int x;"
257 "int y;"
258 "} a1, a2;"
259 "A a3;"
260 "struct B {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400261 "highfloat x;"
262 "highfloat y[2];"
ethannicholasf789b382016-08-03 12:43:36 -0700263 "layout(binding=1) A z;"
264 "};"
265 "B b1, b2, b3;"
266 "void main() {"
267 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500268 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700269 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500270 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700271 "struct A {\n"
272 " int x;\n"
273 " int y;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500274 "} a1, a2;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700275 "A a3;\n"
276 "struct B {\n"
277 " float x;\n"
278 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700279 " layout (binding = 1) A z;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500280 "} b1, b2, b3;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700281 "void main() {\n"
282 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700283}
ethannicholasf789b382016-08-03 12:43:36 -0700284
ethannicholas5961bc92016-10-12 06:39:56 -0700285DEF_TEST(SkSLVersion, r) {
ethannicholas5961bc92016-10-12 06:39:56 -0700286 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400287 "in highfloat test; void main() { sk_FragColor = highfloat4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500288 *SkSL::ShaderCapsFactory::Version450Core(),
ethannicholas5961bc92016-10-12 06:39:56 -0700289 "#version 450 core\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500290 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700291 "in float test;\n"
292 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500293 " sk_FragColor = vec4(0.75);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700294 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700295 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400296 "in highfloat test; void main() { sk_FragColor = highfloat4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500297 *SkSL::ShaderCapsFactory::Version110(),
ethannicholas5961bc92016-10-12 06:39:56 -0700298 "#version 110\n"
299 "varying float test;\n"
300 "void main() {\n"
301 " gl_FragColor = vec4(0.75);\n"
302 "}\n");
303}
304
Leon Scroggins857cb972016-11-11 18:44:37 +0000305DEF_TEST(SkSLUsesPrecisionModifiers, r) {
Leon Scroggins857cb972016-11-11 18:44:37 +0000306 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400307 "void main() { half x = 0.75; highfloat y = 1; x++; y++;"
308 "sk_FragColor.rg = half2(x, y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500309 *SkSL::ShaderCapsFactory::Default(),
Leon Scroggins857cb972016-11-11 18:44:37 +0000310 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500311 "out vec4 sk_FragColor;\n"
Leon Scroggins857cb972016-11-11 18:44:37 +0000312 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700313 " float x = 0.75;\n"
314 " float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400315 " x++;\n"
316 " y++;\n"
317 " sk_FragColor.xy = vec2(x, y);\n"
318 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700319 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400320 "void main() { half x = 0.75; highfloat y = 1; x++; y++;"
321 "sk_FragColor.rg = half2(x, y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500322 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500323 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500324 "out mediump vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700325 "void main() {\n"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400326 " mediump float x = 0.75;\n"
327 " highp float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400328 " x++;\n"
329 " y++;\n"
330 " sk_FragColor.xy = vec2(x, y);\n"
331 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700332}
333
334DEF_TEST(SkSLMinAbs, r) {
335 test(r,
336 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400337 "highfloat x = -5;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400338 "sk_FragColor.r = min(abs(x), 6);"
ethannicholas5961bc92016-10-12 06:39:56 -0700339 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500340 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700341 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500342 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700343 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400344 " sk_FragColor.x = min(abs(-5.0), 6.0);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700345 "}\n");
346
ethannicholas5961bc92016-10-12 06:39:56 -0700347 test(r,
348 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400349 "highfloat x = -5.0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400350 "sk_FragColor.r = min(abs(x), 6.0);"
ethannicholas5961bc92016-10-12 06:39:56 -0700351 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500352 *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(),
ethannicholas5961bc92016-10-12 06:39:56 -0700353 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500354 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700355 "void main() {\n"
356 " float minAbsHackVar0;\n"
357 " float minAbsHackVar1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400358 " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? "
359 "minAbsHackVar0 : minAbsHackVar1);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700360 "}\n");
361}
362
Florin Malita3b30c4f2017-08-08 15:47:35 -0400363DEF_TEST(SkSLFractNegative, r) {
364 static constexpr char input[] =
365 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400366 "highfloat x = -42.0;"
Florin Malita3b30c4f2017-08-08 15:47:35 -0400367 "sk_FragColor.r = fract(x);"
368 "}";
369 static constexpr char output_default[] =
370 "#version 400\n"
371 "out vec4 sk_FragColor;\n"
372 "void main() {\n"
373 " sk_FragColor.x = fract(-42.0);\n"
374 "}\n";
375 static constexpr char output_workaround[] =
376 "#version 400\n"
377 "out vec4 sk_FragColor;\n"
378 "void main() {\n"
379 " sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n"
380 "}\n";
381
382 test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default);
383 test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround);
384}
385
ethannicholasad146f62016-10-14 06:40:02 -0700386DEF_TEST(SkSLNegatedAtan, r) {
387 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400388 "void main() { highfloat2 x = highfloat2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500389 *SkSL::ShaderCapsFactory::Default(),
ethannicholasad146f62016-10-14 06:40:02 -0700390 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500391 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700392 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400393 " vec2 x = vec2(sqrt(2.0));\n"
394 " sk_FragColor.x = atan(x.x, -x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700395 "}\n");
ethannicholasad146f62016-10-14 06:40:02 -0700396 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400397 "void main() { highfloat2 x = highfloat2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500398 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(),
ethannicholasad146f62016-10-14 06:40:02 -0700399 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500400 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700401 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400402 " vec2 x = vec2(sqrt(2.0));\n"
403 " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700404 "}\n");
405}
406
ethannicholas5961bc92016-10-12 06:39:56 -0700407DEF_TEST(SkSLModifiersDeclaration, r) {
408 test(r,
409 "layout(blend_support_all_equations) out;"
410 "void main() { }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500411 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700412 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500413 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700414 "layout (blend_support_all_equations) out ;\n"
415 "void main() {\n"
416 "}\n");
417}
418
419DEF_TEST(SkSLHex, r) {
420 test(r,
421 "void main() {"
422 "int i1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400423 "i1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700424 "int i2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400425 "i2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700426 "int i3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400427 "i3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700428 "int i4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400429 "i4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700430 "int i5 = -0xbeef;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400431 "i5++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700432 "uint u1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400433 "u1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700434 "uint u2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400435 "u2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700436 "uint u3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400437 "u3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700438 "uint u4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400439 "u4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700440 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500441 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700442 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500443 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700444 "void main() {\n"
445 " int i1 = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400446 " i1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700447 " int i2 = 305441741;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400448 " i2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700449 " int i3 = 2147483647;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400450 " i3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700451 " int i4 = -1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400452 " i4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700453 " int i5 = -48879;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400454 " i5++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700455 " uint u1 = 0u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400456 " u1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700457 " uint u2 = 305441741u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400458 " u2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700459 " uint u3 = 2147483647u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400460 " u3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700461 " uint u4 = 4294967295u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400462 " u4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700463 "}\n");
464}
465
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400466DEF_TEST(SkSLVectorConstructors, r) {
467 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400468 "highfloat2 v1 = highfloat2(1);"
469 "highfloat2 v2 = highfloat2(1, 2);"
470 "highfloat2 v3 = highfloat2(highfloat2(1));"
471 "highfloat3 v4 = highfloat3(highfloat2(1), 1.0);"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400472 "int2 v5 = int2(1);"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400473 "int2 v6 = int2(highfloat2(1, 2));"
474 "highfloat2 v7 = highfloat2(int2(1, 2));",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500475 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400476 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500477 "out vec4 sk_FragColor;\n"
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400478 "vec2 v1 = vec2(1.0);\n"
479 "vec2 v2 = vec2(1.0, 2.0);\n"
480 "vec2 v3 = vec2(1.0);\n"
Ethan Nicholas84645e32017-02-09 13:57:14 -0500481 "vec3 v4 = vec3(vec2(1.0), 1.0);\n"
482 "ivec2 v5 = ivec2(1);\n"
483 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n"
484 "vec2 v7 = vec2(ivec2(1, 2));\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400485}
486
ethannicholas5961bc92016-10-12 06:39:56 -0700487DEF_TEST(SkSLArrayConstructors, r) {
488 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400489 "highfloat test1[] = highfloat[](1, 2, 3, 4);"
490 "highfloat2 test2[] = highfloat2[](highfloat2(1, 2), highfloat2(3, 4));"
491 "highfloat4x4 test3[] = highfloat4x4[]();",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500492 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700493 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500494 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700495 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
496 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
497 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700498}
ethannicholasddb37d62016-10-20 09:54:00 -0700499
500DEF_TEST(SkSLDerivatives, r) {
501 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400502 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500503 *SkSL::ShaderCapsFactory::Default(),
ethannicholasddb37d62016-10-20 09:54:00 -0700504 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500505 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700506 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400507 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700508 "}\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700509 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400510 "void main() { sk_FragColor.r = 1; }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500511 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700512 "#version 400\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400513 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700514 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400515 " sk_FragColor.x = 1.0;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700516 "}\n");
517 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400518 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500519 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700520 "#version 400\n"
521 "#extension GL_OES_standard_derivatives : require\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400522 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700523 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400524 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700525 "}\n");
526}
ethannicholas08a92112016-11-09 13:26:45 -0800527
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400528
529DEF_TEST(SkSLIntFolding, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800530 test(r,
531 "void main() {"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400532 "sk_FragColor.r = 32 + 2;"
533 "sk_FragColor.r = 32 - 2;"
534 "sk_FragColor.r = 32 * 2;"
535 "sk_FragColor.r = 32 / 2;"
536 "sk_FragColor.r = 12 | 6;"
537 "sk_FragColor.r = 254 & 7;"
538 "sk_FragColor.r = 2 ^ 7;"
539 "sk_FragColor.r = 1 << 4;"
540 "sk_FragColor.r = 128 >> 2;"
541 "sk_FragColor.r = -1 == -1 ? 1 : -1;"
542 "sk_FragColor.r = -1 == -2 ? 2 : -2;"
543 "sk_FragColor.r = 0 != 1 ? 3 : -3;"
544 "sk_FragColor.r = 0 != 0 ? 4 : -4;"
545 "sk_FragColor.r = 6 > 5 ? 5 : -5;"
546 "sk_FragColor.r = 6 > 6 ? 6 : -6;"
547 "sk_FragColor.r = -1 < 0 ? 7 : -7;"
548 "sk_FragColor.r = 1 < 0 ? 8 : -8;"
549 "sk_FragColor.r = 6 >= 6 ? 9 : -9;"
550 "sk_FragColor.r = 6 >= 7 ? 10 : -10;"
551 "sk_FragColor.r = 6 <= 6 ? 11 : -11;"
552 "sk_FragColor.r = 6 <= 5 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400553 "sk_FragColor.r = int(sqrt(1)) + 0;"
554 "sk_FragColor.r = 0 + int(sqrt(2));"
555 "sk_FragColor.r = int(sqrt(3)) - 0;"
556 "sk_FragColor.r = int(sqrt(4)) * 0;"
557 "sk_FragColor.r = int(sqrt(5)) * 1;"
558 "sk_FragColor.r = 1 * int(sqrt(6));"
559 "sk_FragColor.r = 0 * int(sqrt(7));"
560 "sk_FragColor.r = int(sqrt(8)) / 1;"
561 "sk_FragColor.r = 0 / int(sqrt(9));"
562 "int x = int(sqrt(2));"
563 "x += 1;"
564 "x += 0;"
565 "x -= 1;"
566 "x -= 0;"
567 "x *= 1;"
568 "x *= 2;"
569 "x /= 1;"
570 "x /= 2;"
571 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800572 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500573 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800574 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500575 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800576 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400577 " sk_FragColor.x = 34.0;\n"
578 " sk_FragColor.x = 30.0;\n"
579 " sk_FragColor.x = 64.0;\n"
580 " sk_FragColor.x = 16.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400581 " sk_FragColor.x = 14.0;\n"
582 " sk_FragColor.x = 6.0;\n"
583 " sk_FragColor.x = 5.0;\n"
584 " sk_FragColor.x = 16.0;\n"
585 " sk_FragColor.x = 32.0;\n"
586 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400587 " sk_FragColor.x = -2.0;\n"
588 " sk_FragColor.x = 3.0;\n"
589 " sk_FragColor.x = -4.0;\n"
590 " sk_FragColor.x = 5.0;\n"
591 " sk_FragColor.x = -6.0;\n"
592 " sk_FragColor.x = 7.0;\n"
593 " sk_FragColor.x = -8.0;\n"
594 " sk_FragColor.x = 9.0;\n"
595 " sk_FragColor.x = -10.0;\n"
596 " sk_FragColor.x = 11.0;\n"
597 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400598 " sk_FragColor.x = float(int(sqrt(1.0)));\n"
599 " sk_FragColor.x = float(int(sqrt(2.0)));\n"
600 " sk_FragColor.x = float(int(sqrt(3.0)));\n"
601 " sk_FragColor.x = 0.0;\n"
602 " sk_FragColor.x = float(int(sqrt(5.0)));\n"
603 " sk_FragColor.x = float(int(sqrt(6.0)));\n"
604 " sk_FragColor.x = 0.0;\n"
605 " sk_FragColor.x = float(int(sqrt(8.0)));\n"
606 " sk_FragColor.x = 0.0;\n"
607 " int x = int(sqrt(2.0));\n"
608 " x += 1;\n"
609 " x -= 1;\n"
610 " x *= 2;\n"
611 " x /= 2;\n"
612 " sk_FragColor.x = float(x);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400613 "}\n");
614}
615
616DEF_TEST(SkSLFloatFolding, r) {
617 test(r,
618 "void main() {"
619 "sk_FragColor.r = 32.0 + 2.0;"
620 "sk_FragColor.r = 32.0 - 2.0;"
621 "sk_FragColor.r = 32.0 * 2.0;"
622 "sk_FragColor.r = 32.0 / 2.0;"
623 "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
624 "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;"
625 "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;"
626 "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;"
627 "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;"
628 "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;"
629 "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;"
630 "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;"
631 "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;"
632 "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;"
633 "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;"
634 "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;"
635 "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400636 "sk_FragColor.r = sqrt(1) + 0;"
637 "sk_FragColor.r = 0 + sqrt(2);"
638 "sk_FragColor.r = sqrt(3) - 0;"
639 "sk_FragColor.r = sqrt(4) * 0;"
640 "sk_FragColor.r = sqrt(5) * 1;"
641 "sk_FragColor.r = 1 * sqrt(6);"
642 "sk_FragColor.r = 0 * sqrt(7);"
643 "sk_FragColor.r = sqrt(8) / 1;"
644 "sk_FragColor.r = 0 / sqrt(9);"
645 "sk_FragColor.r += 1;"
646 "sk_FragColor.r += 0;"
647 "sk_FragColor.r -= 1;"
648 "sk_FragColor.r -= 0;"
649 "sk_FragColor.r *= 1;"
650 "sk_FragColor.r *= 2;"
651 "sk_FragColor.r /= 1;"
652 "sk_FragColor.r /= 2;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400653 "}",
654 *SkSL::ShaderCapsFactory::Default(),
655 "#version 400\n"
656 "out vec4 sk_FragColor;\n"
657 "void main() {\n"
658 " sk_FragColor.x = 34.0;\n"
659 " sk_FragColor.x = 30.0;\n"
660 " sk_FragColor.x = 64.0;\n"
661 " sk_FragColor.x = 16.0;\n"
662 " sk_FragColor.x = 19.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400663 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400664 " sk_FragColor.x = -2.0;\n"
665 " sk_FragColor.x = 3.0;\n"
666 " sk_FragColor.x = -4.0;\n"
667 " sk_FragColor.x = 5.0;\n"
668 " sk_FragColor.x = -6.0;\n"
669 " sk_FragColor.x = 7.0;\n"
670 " sk_FragColor.x = -8.0;\n"
671 " sk_FragColor.x = 9.0;\n"
672 " sk_FragColor.x = -10.0;\n"
673 " sk_FragColor.x = 11.0;\n"
674 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400675 " sk_FragColor.x = sqrt(1.0);\n"
676 " sk_FragColor.x = sqrt(2.0);\n"
677 " sk_FragColor.x = sqrt(3.0);\n"
678 " sk_FragColor.x = 0.0;\n"
679 " sk_FragColor.x = sqrt(5.0);\n"
680 " sk_FragColor.x = sqrt(6.0);\n"
681 " sk_FragColor.x = 0.0;\n"
682 " sk_FragColor.x = sqrt(8.0);\n"
683 " sk_FragColor.x = 0.0;\n"
684 " sk_FragColor.x += 1.0;\n"
685 " sk_FragColor.x -= 1.0;\n"
686 " sk_FragColor.x *= 2.0;\n"
687 " sk_FragColor.x /= 2.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400688 "}\n");
689}
690
691DEF_TEST(SkSLBoolFolding, r) {
692 test(r,
693 "void main() {"
694 "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;"
695 "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;"
696 "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;"
697 "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;"
698 "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;"
699 "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;"
700 "}",
701 *SkSL::ShaderCapsFactory::Default(),
702 "#version 400\n"
703 "out vec4 sk_FragColor;\n"
704 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400705 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400706 " sk_FragColor.x = -2.0;\n"
707 " sk_FragColor.x = 3.0;\n"
708 " sk_FragColor.x = -4.0;\n"
709 " sk_FragColor.x = 5.0;\n"
710 " sk_FragColor.x = -6.0;\n"
711 "}\n");
712}
713
714DEF_TEST(SkSLVecFolding, r) {
715 test(r,
716 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400717 "sk_FragColor.r = highfloat4(0.5, 1, 1, 1).x;"
718 "sk_FragColor = highfloat4(highfloat2(1), highfloat2(2, 3)) + highfloat4(5, 6, 7, 8);"
719 "sk_FragColor = highfloat4(8, highfloat3(10)) - highfloat4(1);"
720 "sk_FragColor = highfloat4(2) * highfloat4(1, 2, 3, 4);"
721 "sk_FragColor = highfloat4(12) / highfloat4(1, 2, 3, 4);"
722 "sk_FragColor.r = (highfloat4(12) / highfloat4(1, 2, 3, 4)).y;"
723 "sk_FragColor.x = highfloat4(1) == highfloat4(1) ? 1.0 : -1.0;"
724 "sk_FragColor.x = highfloat4(1) == highfloat4(2) ? 2.0 : -2.0;"
725 "sk_FragColor.x = highfloat2(1) == highfloat2(1, 1) ? 3.0 : -3.0;"
726 "sk_FragColor.x = highfloat2(1, 1) == highfloat2(1, 1) ? 4.0 : -4.0;"
727 "sk_FragColor.x = highfloat2(1) == highfloat2(1, 0) ? 5.0 : -5.0;"
728 "sk_FragColor.x = highfloat4(1) == highfloat4(highfloat2(1), highfloat2(1)) ? 6.0 : -6.0;"
729 "sk_FragColor.x = highfloat4(highfloat3(1), 1) == highfloat4(highfloat2(1), highfloat2(1)) ? 7.0 : -7.0;"
730 "sk_FragColor.x = highfloat4(highfloat3(1), 1) == highfloat4(highfloat2(1), 1, 0) ? 8.0 : -8.0;"
731 "sk_FragColor.x = highfloat2(1) != highfloat2(1, 0) ? 9.0 : -9.0;"
732 "sk_FragColor.x = highfloat4(1) != highfloat4(highfloat2(1), highfloat2(1)) ? 10.0 : -10.0;"
733 "sk_FragColor = highfloat4(sqrt(1)) * highfloat4(1);"
734 "sk_FragColor = highfloat4(1) * highfloat4(sqrt(2));"
735 "sk_FragColor = highfloat4(0) * highfloat4(sqrt(3));"
736 "sk_FragColor = highfloat4(sqrt(4)) * highfloat4(0);"
737 "sk_FragColor = highfloat4(0) / highfloat4(sqrt(5));"
738 "sk_FragColor = highfloat4(0) + highfloat4(sqrt(6));"
739 "sk_FragColor = highfloat4(sqrt(7)) + highfloat4(0);"
740 "sk_FragColor = highfloat4(sqrt(8)) - highfloat4(0);"
741 "sk_FragColor = highfloat4(0) + sqrt(9);"
742 "sk_FragColor = highfloat4(0) * sqrt(10);"
743 "sk_FragColor = highfloat4(0) / sqrt(11);"
744 "sk_FragColor = highfloat4(1) * sqrt(12);"
745 "sk_FragColor = 0 + highfloat4(sqrt(13));"
746 "sk_FragColor = 0 * highfloat4(sqrt(14));"
747 "sk_FragColor = 0 / highfloat4(sqrt(15));"
748 "sk_FragColor = 1 * highfloat4(sqrt(16));"
749 "sk_FragColor = highfloat4(sqrt(17)) + 0;"
750 "sk_FragColor = highfloat4(sqrt(18)) * 0;"
751 "sk_FragColor = highfloat4(sqrt(19)) * 1;"
752 "sk_FragColor = highfloat4(sqrt(19.5)) - 0;"
753 "sk_FragColor = sqrt(20) * highfloat4(1);"
754 "sk_FragColor = sqrt(21) + highfloat4(0);"
755 "sk_FragColor = sqrt(22) - highfloat4(0);"
756 "sk_FragColor = sqrt(23) / highfloat4(1);"
757 "sk_FragColor = highfloat4(sqrt(24)) / 1;"
758 "sk_FragColor += highfloat4(1);"
759 "sk_FragColor += highfloat4(0);"
760 "sk_FragColor -= highfloat4(1);"
761 "sk_FragColor -= highfloat4(0);"
762 "sk_FragColor *= highfloat4(1);"
763 "sk_FragColor *= highfloat4(2);"
764 "sk_FragColor /= highfloat4(1);"
765 "sk_FragColor /= highfloat4(2);"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400766 "}",
767 *SkSL::ShaderCapsFactory::Default(),
768 "#version 400\n"
769 "out vec4 sk_FragColor;\n"
770 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400771 " sk_FragColor.x = 0.5;\n"
772 " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
773 " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
774 " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
775 " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
776 " sk_FragColor.x = 6.0;\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400777 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400778 " sk_FragColor.x = -2.0;\n"
779 " sk_FragColor.x = 3.0;\n"
780 " sk_FragColor.x = 4.0;\n"
781 " sk_FragColor.x = -5.0;\n"
782 " sk_FragColor.x = 6.0;\n"
783 " sk_FragColor.x = 7.0;\n"
784 " sk_FragColor.x = -8.0;\n"
785 " sk_FragColor.x = 9.0;\n"
786 " sk_FragColor.x = -10.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400787 " sk_FragColor = vec4(sqrt(1.0));\n"
788 " sk_FragColor = vec4(sqrt(2.0));\n"
789 " sk_FragColor = vec4(0.0);\n"
790 " sk_FragColor = vec4(0.0);\n"
791 " sk_FragColor = vec4(0.0);\n"
792 " sk_FragColor = vec4(sqrt(6.0));\n"
793 " sk_FragColor = vec4(sqrt(7.0));\n"
794 " sk_FragColor = vec4(sqrt(8.0));\n"
795 " sk_FragColor = vec4(sqrt(9.0));\n"
796 " sk_FragColor = vec4(0.0);\n"
797 " sk_FragColor = vec4(0.0);\n"
798 " sk_FragColor = vec4(sqrt(12.0));\n"
799 " sk_FragColor = vec4(sqrt(13.0));\n"
800 " sk_FragColor = vec4(0.0);\n"
801 " sk_FragColor = vec4(0.0);\n"
802 " sk_FragColor = vec4(sqrt(16.0));\n"
803 " sk_FragColor = vec4(sqrt(17.0));\n"
804 " sk_FragColor = vec4(0.0);\n"
805 " sk_FragColor = vec4(sqrt(19.0));\n"
806 " sk_FragColor = vec4(sqrt(19.5));\n"
807 " sk_FragColor = vec4(sqrt(20.0));\n"
808 " sk_FragColor = vec4(sqrt(21.0));\n"
809 " sk_FragColor = vec4(sqrt(22.0));\n"
810 " sk_FragColor = vec4(sqrt(23.0));\n"
811 " sk_FragColor = vec4(sqrt(24.0));\n"
812 " sk_FragColor += vec4(1.0);\n"
813 " sk_FragColor -= vec4(1.0);\n"
814 " sk_FragColor *= vec4(2.0);\n"
815 " sk_FragColor /= vec4(2.0);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400816 "}\n");
817}
818
819DEF_TEST(SkSLMatFolding, r) {
820 test(r,
821 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400822 "sk_FragColor.x = highfloat2x2(highfloat2(1.0, 0.0), highfloat2(0.0, 1.0)) == "
823 "highfloat2x2(highfloat2(1.0, 0.0), highfloat2(0.0, 1.0)) ? 1 : -1;"
824 "sk_FragColor.x = highfloat2x2(highfloat2(1.0, 0.0), highfloat2(1.0, 1.0)) == "
825 "highfloat2x2(highfloat2(1.0, 0.0), highfloat2(0.0, 1.0)) ? 2 : -2;"
826 "sk_FragColor.x = highfloat2x2(1) == highfloat2x2(1) ? 3 : -3;"
827 "sk_FragColor.x = highfloat2x2(1) == highfloat2x2(0) ? 4 : -4;"
828 "sk_FragColor.x = highfloat2x2(1) == highfloat2x2(highfloat2(1.0, 0.0), highfloat2(0.0, 1.0)) ? 5 : -5;"
829 "sk_FragColor.x = highfloat2x2(2) == highfloat2x2(highfloat2(1.0, 0.0), highfloat2(0.0, 1.0)) ? 6 : -6;"
830 "sk_FragColor.x = highfloat3x2(2) == highfloat3x2(highfloat2(2.0, 0.0), highfloat2(0.0, 2.0), highfloat2(0.0))"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400831 "? 7 : -7;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400832 "sk_FragColor.x = highfloat2x2(1) != highfloat2x2(1) ? 8 : -8;"
833 "sk_FragColor.x = highfloat2x2(1) != highfloat2x2(0) ? 9 : -9;"
834 "sk_FragColor.x = highfloat3x3(highfloat3(1.0, 0.0, 0.0), highfloat3(0.0, 1.0, 0.0), "
835 "highfloat3(0.0, 0.0, 0.0)) == highfloat3x3(highfloat2x2(1.0)) ? 10 : -10;"
836 "sk_FragColor.x = highfloat2x2(highfloat3x3(1.0)) == highfloat2x2(1.0) ? 11 : -11;"
837 "sk_FragColor.x = highfloat2x2(highfloat4(1.0, 0.0, 0.0, 1.0)) == highfloat2x2(1.0) ? 12 : -12;"
838 "sk_FragColor.x = highfloat2x2(1.0, 0.0, highfloat2(0.0, 1.0)) == highfloat2x2(1.0) ? 13 : -13;"
839 "sk_FragColor.x = highfloat2x2(highfloat2(1.0, 0.0), 0.0, 1.0) == highfloat2x2(1.0) ? 14 : -14;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400840 "}",
841 *SkSL::ShaderCapsFactory::Default(),
842 "#version 400\n"
843 "out vec4 sk_FragColor;\n"
844 "void main() {\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400845 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400846 " sk_FragColor.x = -2.0;\n"
847 " sk_FragColor.x = 3.0;\n"
848 " sk_FragColor.x = -4.0;\n"
849 " sk_FragColor.x = 5.0;\n"
850 " sk_FragColor.x = -6.0;\n"
851 " sk_FragColor.x = 7.0;\n"
852 " sk_FragColor.x = -8.0;\n"
853 " sk_FragColor.x = 9.0;\n"
854 " sk_FragColor.x = 10.0;\n"
855 " sk_FragColor.x = 11.0;\n"
856 " sk_FragColor.x = 12.0;\n"
857 " sk_FragColor.x = 13.0;\n"
858 " sk_FragColor.x = 14.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800859 "}\n");
860}
861
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400862DEF_TEST(SkSLConstantIf, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800863 test(r,
864 "void main() {"
865 "int x;"
866 "if (true) x = 1;"
867 "if (2 > 1) x = 2; else x = 3;"
868 "if (1 > 2) x = 4; else x = 5;"
869 "if (false) x = 6;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400870 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800871 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500872 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800873 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500874 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800875 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400876 " sk_FragColor.x = 5.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800877 "}\n");
878}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500879
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500880DEF_TEST(SkSLCaps, r) {
881 test(r,
882 "void main() {"
Ethan Nicholascb670962017-04-20 19:31:52 -0400883 "int x = 0;"
884 "int y = 0;"
885 "int z = 0;"
886 "int w = 0;"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500887 "if (sk_Caps.externalTextureSupport) x = 1;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400888 "if (sk_Caps.fbFetchSupport) y = 1;"
889 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;"
890 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400891 "sk_FragColor = half4(x, y, z, w);"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500892 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500893 *SkSL::ShaderCapsFactory::VariousCaps(),
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500894 "#version 400\n"
895 "out vec4 sk_FragColor;\n"
896 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400897 " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500898 "}\n");
899}
900
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500901DEF_TEST(SkSLTexture, r) {
902 test(r,
903 "uniform sampler1D one;"
904 "uniform sampler2D two;"
905 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400906 "highfloat4 a = texture(one, 0);"
907 "highfloat4 b = texture(two, highfloat2(0));"
908 "highfloat4 c = texture(one, highfloat2(0));"
909 "highfloat4 d = texture(two, highfloat3(0));"
910 "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500911 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500912 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500913 "#version 400\n"
914 "out vec4 sk_FragColor;\n"
915 "uniform sampler1D one;\n"
916 "uniform sampler2D two;\n"
917 "void main() {\n"
918 " vec4 a = texture(one, 0.0);\n"
919 " vec4 b = texture(two, vec2(0.0));\n"
920 " vec4 c = textureProj(one, vec2(0.0));\n"
921 " vec4 d = textureProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400922 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500923 "}\n");
924 test(r,
925 "uniform sampler1D one;"
926 "uniform sampler2D two;"
927 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -0400928 "highfloat4 a = texture(one, 0);"
929 "highfloat4 b = texture(two, highfloat2(0));"
930 "highfloat4 c = texture(one, highfloat2(0));"
931 "highfloat4 d = texture(two, highfloat3(0));"
932 "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500933 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500934 *SkSL::ShaderCapsFactory::Version110(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500935 "#version 110\n"
936 "uniform sampler1D one;\n"
937 "uniform sampler2D two;\n"
938 "void main() {\n"
939 " vec4 a = texture1D(one, 0.0);\n"
940 " vec4 b = texture2D(two, vec2(0.0));\n"
941 " vec4 c = texture1DProj(one, vec2(0.0));\n"
942 " vec4 d = texture2DProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400943 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500944 "}\n");
945}
946
Ethan Nicholas19671772016-11-28 16:30:17 -0500947DEF_TEST(SkSLOffset, r) {
948 test(r,
949 "struct Test {"
950 "layout(offset = 0) int x;"
951 "layout(offset = 4) int y;"
952 "int z;"
953 "} test;",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500954 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas19671772016-11-28 16:30:17 -0500955 "#version 400\n"
956 "out vec4 sk_FragColor;\n"
957 "struct Test {\n"
958 " layout (offset = 0) int x;\n"
959 " layout (offset = 4) int y;\n"
960 " int z;\n"
961 "} test;\n");
962}
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500963
964DEF_TEST(SkSLFragCoord, r) {
965 SkSL::Program::Settings settings;
966 settings.fFlipY = true;
967 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
968 settings.fCaps = caps.get();
969 SkSL::Program::Inputs inputs;
970 test(r,
971 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
972 settings,
973 "#version 110\n"
974 "#extension GL_ARB_fragment_coord_conventions : require\n"
975 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
976 "void main() {\n"
977 " gl_FragColor.xy = gl_FragCoord.xy;\n"
978 "}\n",
979 &inputs);
980 REPORTER_ASSERT(r, !inputs.fRTHeight);
981
982 caps = SkSL::ShaderCapsFactory::FragCoordsNew();
983 settings.fCaps = caps.get();
984 test(r,
985 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
986 settings,
987 "#version 400\n"
988 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
989 "out vec4 sk_FragColor;\n"
990 "void main() {\n"
991 " sk_FragColor.xy = gl_FragCoord.xy;\n"
992 "}\n",
993 &inputs);
994 REPORTER_ASSERT(r, !inputs.fRTHeight);
995
996 caps = SkSL::ShaderCapsFactory::Default();
997 settings.fCaps = caps.get();
998 test(r,
999 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1000 settings,
1001 "#version 400\n"
1002 "uniform float u_skRTHeight;\n"
1003 "out vec4 sk_FragColor;\n"
1004 "void main() {\n"
1005 " vec2 _sktmpCoord = gl_FragCoord.xy;\n"
1006 " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n"
1007 " sk_FragColor.xy = sk_FragCoord.xy;\n"
1008 "}\n",
1009 &inputs);
1010 REPORTER_ASSERT(r, inputs.fRTHeight);
1011
1012 settings.fFlipY = false;
1013 test(r,
1014 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1015 settings,
1016 "#version 400\n"
1017 "out vec4 sk_FragColor;\n"
1018 "void main() {\n"
1019 " sk_FragColor.xy = gl_FragCoord.xy;\n"
1020 "}\n",
1021 &inputs);
1022 REPORTER_ASSERT(r, !inputs.fRTHeight);
1023}
1024
Ethan Nicholasa51740c2017-02-07 14:53:32 -05001025DEF_TEST(SkSLVertexID, r) {
1026 test(r,
1027 "out int id; void main() { id = sk_VertexID; }",
1028 *SkSL::ShaderCapsFactory::Default(),
1029 "#version 400\n"
1030 "out int id;\n"
1031 "void main() {\n"
1032 " id = gl_VertexID;\n"
1033 "}\n",
1034 SkSL::Program::kVertex_Kind);
1035}
1036
Ethan Nicholas67d64602017-02-09 10:15:25 -05001037DEF_TEST(SkSLClipDistance, r) {
1038 test(r,
1039 "void main() { sk_ClipDistance[0] = 0; }",
1040 *SkSL::ShaderCapsFactory::Default(),
1041 "#version 400\n"
1042 "void main() {\n"
1043 " gl_ClipDistance[0] = 0.0;\n"
1044 "}\n",
1045 SkSL::Program::kVertex_Kind);
1046 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001047 "void main() { sk_FragColor = half4(sk_ClipDistance[0]); }",
Ethan Nicholas67d64602017-02-09 10:15:25 -05001048 *SkSL::ShaderCapsFactory::Default(),
1049 "#version 400\n"
1050 "out vec4 sk_FragColor;\n"
1051 "void main() {\n"
1052 " sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1053 "}\n");
1054}
Ethan Nicholas50afc172017-02-16 14:49:57 -05001055
1056DEF_TEST(SkSLArrayTypes, r) {
1057 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001058 "void main() { highfloat2 x[2] = highfloat2[2](highfloat2(1), highfloat2(2));"
1059 "highfloat2[2] y = highfloat2[2](highfloat2(3), highfloat2(4));"
1060 "sk_FragColor = highfloat4(x[0], y[1]); }",
Ethan Nicholas50afc172017-02-16 14:49:57 -05001061 *SkSL::ShaderCapsFactory::Default(),
1062 "#version 400\n"
1063 "out vec4 sk_FragColor;\n"
1064 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001065 " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1066 "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -05001067 "}\n");
1068}
1069
Ethan Nicholas52cad152017-02-16 16:37:32 -05001070DEF_TEST(SkSLGeometry, r) {
1071 test(r,
1072 "layout(points) in;"
1073 "layout(invocations = 2) in;"
1074 "layout(line_strip, max_vertices = 2) out;"
1075 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001076 "gl_Position = sk_in[0].gl_Position + highfloat4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001077 "EmitVertex();"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001078 "gl_Position = sk_in[0].gl_Position + highfloat4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001079 "EmitVertex();"
1080 "EndPrimitive();"
1081 "}",
1082 *SkSL::ShaderCapsFactory::Default(),
1083 "#version 400\n"
1084 "layout (points) in ;\n"
1085 "layout (invocations = 2) in ;\n"
1086 "layout (line_strip, max_vertices = 2) out ;\n"
1087 "void main() {\n"
1088 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1089 " EmitVertex();\n"
1090 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1091 " EmitVertex();\n"
1092 " EndPrimitive();\n"
1093 "}\n",
1094 SkSL::Program::kGeometry_Kind);
1095}
1096
Ethan Nicholasaf197692017-02-27 13:26:45 -05001097DEF_TEST(SkSLSwitch, r) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001098 // basic "does a switch even work" test
Ethan Nicholasaf197692017-02-27 13:26:45 -05001099 test(r,
1100 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001101 " highfloat x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001102 " switch (int(sqrt(1))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001103 " case 0:"
1104 " x = 0.0;"
1105 " break;"
1106 " case 1:"
1107 " x = 1.0;"
1108 " break;"
1109 " default:"
1110 " x = 2.0;"
1111 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001112 " sk_FragColor = highfloat4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001113 "}",
1114 *SkSL::ShaderCapsFactory::Default(),
1115 "#version 400\n"
1116 "out vec4 sk_FragColor;\n"
1117 "void main() {\n"
1118 " float x;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001119 " switch (int(sqrt(1.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001120 " case 0:\n"
1121 " x = 0.0;\n"
1122 " break;\n"
1123 " case 1:\n"
1124 " x = 1.0;\n"
1125 " break;\n"
1126 " default:\n"
1127 " x = 2.0;\n"
1128 " }\n"
1129 " sk_FragColor = vec4(x);\n"
1130 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001131 // dead code inside of switch
Ethan Nicholasaf197692017-02-27 13:26:45 -05001132 test(r,
1133 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001134 " highfloat x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001135 " switch (int(sqrt(2))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001136 " case 0:"
1137 " x = 0.0;"
1138 " case 1:"
1139 " x = 1.0;"
1140 " default:"
1141 " x = 2.0;"
1142 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001143 " sk_FragColor = half4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001144 "}",
1145 *SkSL::ShaderCapsFactory::Default(),
1146 "#version 400\n"
1147 "out vec4 sk_FragColor;\n"
1148 "void main() {\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001149 " switch (int(sqrt(2.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001150 " case 0:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001151 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001152 " case 1:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001153 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001154 " default:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001155 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001156 " }\n"
1157 " sk_FragColor = vec4(2.0);\n"
1158 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001159 // non-static test w/ fallthrough
Ethan Nicholasaf197692017-02-27 13:26:45 -05001160 test(r,
1161 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001162 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001163 " switch (int(sqrt(3))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001164 " case 0:"
1165 " x = 0.0;"
1166 " case 1:"
1167 " x = 1.0;"
1168 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001169 " sk_FragColor = half4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001170 "}",
1171 *SkSL::ShaderCapsFactory::Default(),
1172 "#version 400\n"
1173 "out vec4 sk_FragColor;\n"
1174 "void main() {\n"
1175 " float x = 0.0;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001176 " switch (int(sqrt(3.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001177 " case 0:\n"
1178 " x = 0.0;\n"
1179 " case 1:\n"
1180 " x = 1.0;\n"
1181 " }\n"
1182 " sk_FragColor = vec4(x);\n"
1183 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001184 // static test w/ fallthrough
1185 test(r,
1186 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001187 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001188 " switch (0) {"
1189 " case 0:"
1190 " x = 0.0;"
1191 " case 1:"
1192 " x = 1.0;"
1193 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001194 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001195 "}",
1196 *SkSL::ShaderCapsFactory::Default(),
1197 "#version 400\n"
1198 "out vec4 sk_FragColor;\n"
1199 "void main() {\n"
1200 " sk_FragColor = vec4(1.0);\n"
1201 "}\n");
1202 // static test w/ fallthrough, different entry point
1203 test(r,
1204 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001205 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001206 " switch (1) {"
1207 " case 0:"
1208 " x = 0.0;"
1209 " case 1:"
1210 " x = 1.0;"
1211 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001212 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001213 "}",
1214 *SkSL::ShaderCapsFactory::Default(),
1215 "#version 400\n"
1216 "out vec4 sk_FragColor;\n"
1217 "void main() {\n"
1218 " sk_FragColor = vec4(1.0);\n"
1219 "}\n");
1220 // static test w/ break
1221 test(r,
1222 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001223 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001224 " switch (0) {"
1225 " case 0:"
1226 " x = 0.0;"
1227 " break;"
1228 " case 1:"
1229 " x = 1.0;"
1230 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001231 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001232 "}",
1233 *SkSL::ShaderCapsFactory::Default(),
1234 "#version 400\n"
1235 "out vec4 sk_FragColor;\n"
1236 "void main() {\n"
1237 " sk_FragColor = vec4(0.0);\n"
1238 "}\n");
1239 // static test w/ static conditional break
1240 test(r,
1241 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001242 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001243 " switch (0) {"
1244 " case 0:"
1245 " x = 0.0;"
1246 " if (x < 1) break;"
1247 " case 1:"
1248 " x = 1.0;"
1249 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001250 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001251 "}",
1252 *SkSL::ShaderCapsFactory::Default(),
1253 "#version 400\n"
1254 "out vec4 sk_FragColor;\n"
1255 "void main() {\n"
1256 " sk_FragColor = vec4(0.0);\n"
1257 "}\n");
1258 // static test w/ non-static conditional break
1259 test(r,
1260 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001261 " highfloat x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001262 " switch (0) {"
1263 " case 0:"
1264 " x = 0.0;"
1265 " if (x < sqrt(1)) break;"
1266 " case 1:"
1267 " x = 1.0;"
1268 " }"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001269 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001270 "}",
1271 *SkSL::ShaderCapsFactory::Default(),
1272 "#version 400\n"
1273 "out vec4 sk_FragColor;\n"
1274 "void main() {\n"
1275 " float x = 0.0;\n"
1276 " switch (0) {\n"
1277 " case 0:\n"
1278 " x = 0.0;\n"
1279 " if (0.0 < sqrt(1.0)) break;\n"
1280 " case 1:\n"
1281 " x = 1.0;\n"
1282 " }\n"
1283 " sk_FragColor = vec4(x);\n"
1284 "}\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -05001285}
1286
Ethan Nicholas5338f992017-04-19 15:54:07 -04001287DEF_TEST(SkSLRectangleTexture, r) {
1288 test(r,
1289 "uniform sampler2D test;"
1290 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001291 " sk_FragColor = texture(test, highfloat2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001292 "}",
1293 *SkSL::ShaderCapsFactory::Default(),
1294 "#version 400\n"
1295 "out vec4 sk_FragColor;\n"
1296 "uniform sampler2D test;\n"
1297 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001298 " sk_FragColor = texture(test, vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001299 "}\n");
1300 test(r,
1301 "uniform sampler2DRect test;"
1302 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001303 " sk_FragColor = texture(test, highfloat2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001304 "}",
1305 *SkSL::ShaderCapsFactory::Default(),
1306 "#version 400\n"
1307 "out vec4 sk_FragColor;\n"
1308 "uniform sampler2DRect test;\n"
1309 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001310 " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001311 "}\n");
1312 test(r,
1313 "uniform sampler2DRect test;"
1314 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001315 " sk_FragColor = texture(test, highfloat3(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001316 "}",
1317 *SkSL::ShaderCapsFactory::Default(),
1318 "#version 400\n"
1319 "out vec4 sk_FragColor;\n"
1320 "uniform sampler2DRect test;\n"
1321 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001322 " sk_FragColor = texture(test, vec3(textureSize(test), 1.0) * vec3(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001323 "}\n");
1324}
1325
Ethan Nicholascb670962017-04-20 19:31:52 -04001326DEF_TEST(SkSLUnusedVars, r) {
1327 test(r,
1328 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001329 "highfloat a = 1, b = 2, c = 3;"
1330 "highfloat d = c;"
1331 "highfloat e = d;"
Ethan Nicholascb670962017-04-20 19:31:52 -04001332 "b++;"
1333 "d++;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001334 "sk_FragColor = highfloat4(b, b, d, d);"
Ethan Nicholascb670962017-04-20 19:31:52 -04001335 "}",
1336 *SkSL::ShaderCapsFactory::Default(),
1337 "#version 400\n"
1338 "out vec4 sk_FragColor;\n"
1339 "void main() {\n"
1340 " float b = 2.0;\n"
1341 " float d = 3.0;\n"
1342 " b++;\n"
1343 " d++;\n"
1344 " sk_FragColor = vec4(b, b, d, d);\n"
1345 "}\n");
1346}
1347
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001348DEF_TEST(SkSLMultipleAssignments, r) {
1349 test(r,
1350 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001351 "highfloat x;"
1352 "highfloat y;"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001353 "int z;"
1354 "x = y = z = 1;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001355 "sk_FragColor = highfloat4(z);"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001356 "}",
1357 *SkSL::ShaderCapsFactory::Default(),
1358 "#version 400\n"
1359 "out vec4 sk_FragColor;\n"
1360 "void main() {\n"
1361 " sk_FragColor = vec4(1.0);\n"
1362 "}\n");
1363}
1364
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001365DEF_TEST(SkSLComplexDelete, r) {
1366 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001367 "uniform highfloat4x4 colorXform;"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001368 "uniform sampler2D sampler;"
1369 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001370 "highfloat4 tmpColor;"
1371 "sk_FragColor = highfloat4(1.0) * (tmpColor = texture(sampler, highfloat2(1)) , "
1372 "colorXform != highfloat4x4(1.0) ? highfloat4(clamp((highfloat4x4(colorXform) * "
1373 "highfloat4(tmpColor.xyz, 1.0)).xyz, "
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001374 "0.0, tmpColor.w), tmpColor.w) : tmpColor);"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001375 "}",
1376 *SkSL::ShaderCapsFactory::Default(),
1377 "#version 400\n"
1378 "out vec4 sk_FragColor;\n"
1379 "uniform mat4 colorXform;\n"
1380 "uniform sampler2D sampler;\n"
1381 "void main() {\n"
1382 " vec4 tmpColor;\n"
1383 " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1384 "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1385 "tmpColor);\n"
1386 "}\n");
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001387}
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001388
1389DEF_TEST(SkSLDependentInitializers, r) {
1390 test(r,
1391 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001392 "highfloat x = 0.5, y = x * 2;"
1393 "sk_FragColor = highfloat4(y);"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001394 "}",
1395 *SkSL::ShaderCapsFactory::Default(),
1396 "#version 400\n"
1397 "out vec4 sk_FragColor;\n"
1398 "void main() {\n"
1399 " sk_FragColor = vec4(1.0);\n"
1400 "}\n");
1401}
1402
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001403DEF_TEST(SkSLDeadLoopVar, r) {
1404 test(r,
1405 "void main() {"
1406 "for (int x = 0; x < 4; ) {"
1407 "break;"
1408 "}"
1409 "}",
1410 *SkSL::ShaderCapsFactory::Default(),
1411 "#version 400\n"
1412 "out vec4 sk_FragColor;\n"
1413 "void main() {\n"
1414 " for (; true; ) {\n"
1415 " break;\n"
1416 " }\n"
1417 "}\n"
1418 );
1419}
1420
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001421DEF_TEST(SkSLInvocations, r) {
1422 test(r,
1423 "layout(points) in;"
1424 "layout(invocations = 2) in;"
1425 "layout(line_strip, max_vertices = 2) out;"
1426 "void test() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001427 "gl_Position = sk_in[0].gl_Position + highfloat4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001428 "EmitVertex();"
1429 "}"
1430 "void main() {"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001431 "gl_Position = sk_in[0].gl_Position + highfloat4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001432 "EmitVertex();"
1433 "}",
1434 *SkSL::ShaderCapsFactory::MustImplementGSInvocationsWithLoop(),
1435 "#version 400\n"
1436 "int sk_InvocationID;\n"
1437 "layout (points) in ;\n"
1438 "layout (line_strip, max_vertices = 4) out ;\n"
1439 "void test() {\n"
1440 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1441 " EmitVertex();\n"
1442 "}\n"
1443 "void _invoke() {\n"
1444 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1445 " EmitVertex();\n"
1446 "}\n"
1447 "void main() {\n"
1448 " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1449 " _invoke();\n"
1450 " EndPrimitive();\n"
1451 " }\n"
1452 "}\n",
1453 SkSL::Program::kGeometry_Kind);
1454}
1455
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001456DEF_TEST(SkSLTypePrecision, r) {
1457 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001458 "highfloat f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001459 "half h = 2;"
1460 "double d = 3;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001461 "highfloat2 f2 = highfloat2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001462 "half3 h3 = half3(1, 2, 3);"
1463 "double4 d4 = double4(1, 2, 3, 4);"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001464 "highfloat2x2 f22 = highfloat2x2(1, 2, 3, 4);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001465 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1466 "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);",
1467 *SkSL::ShaderCapsFactory::Default(),
1468 "#version 400\n"
1469 "out vec4 sk_FragColor;\n"
1470 "float f = 1.0;\n"
1471 "float h = 2.0;\n"
1472 "double d = 3.0;\n"
1473 "vec2 f2 = vec2(1.0, 2.0);\n"
1474 "vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1475 "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n"
1476 "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1477 "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1478 "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1479 test(r,
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001480 "highfloat f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001481 "half h = 2;"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001482 "highfloat2 f2 = highfloat2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001483 "half3 h3 = half3(1, 2, 3);"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001484 "highfloat2x2 f22 = highfloat2x2(1, 2, 3, 4);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001485 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);",
1486 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1487 "#version 400\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001488 "out mediump vec4 sk_FragColor;\n"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001489 "highp float f = 1.0;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001490 "mediump float h = 2.0;\n"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001491 "highp vec2 f2 = vec2(1.0, 2.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001492 "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001493 "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001494 "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1495}
1496
1497DEF_TEST(SkSLNumberConversions, r) {
1498 test(r,
1499 "short s = short(sqrt(1));"
1500 "int i = int(sqrt(1));"
1501 "ushort us = ushort(sqrt(1));"
1502 "uint ui = uint(sqrt(1));"
1503 "half h = sqrt(1);"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001504 "highfloat f = sqrt(1);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001505 "short s2s = s;"
1506 "short i2s = i;"
1507 "short us2s = short(us);"
1508 "short ui2s = short(ui);"
1509 "short h2s = short(h);"
1510 "short f2s = short(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001511 "int s2i = s;"
1512 "int i2i = i;"
1513 "int us2i = int(us);"
1514 "int ui2i = int(ui);"
1515 "int h2i = int(h);"
1516 "int f2i = int(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001517 "ushort s2us = ushort(s);"
1518 "ushort i2us = ushort(i);"
1519 "ushort us2us = us;"
1520 "ushort ui2us = ui;"
1521 "ushort h2us = ushort(h);"
1522 "ushort f2us = ushort(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001523 "uint s2ui = uint(s);"
1524 "uint i2ui = uint(i);"
1525 "uint us2ui = us;"
1526 "uint ui2ui = ui;"
1527 "uint h2ui = uint(h);"
1528 "uint f2ui = uint(f);"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001529 "highfloat s2f = s;"
1530 "highfloat i2f = i;"
1531 "highfloat us2f = us;"
1532 "highfloat ui2f = ui;"
1533 "highfloat h2f = h;"
1534 "highfloat f2f = f;",
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001535 *SkSL::ShaderCapsFactory::Default(),
1536 "#version 400\n"
1537 "out vec4 sk_FragColor;\n"
1538 "int s = int(sqrt(1.0));\n"
1539 "int i = int(sqrt(1.0));\n"
1540 "uint us = uint(sqrt(1.0));\n"
1541 "uint ui = uint(sqrt(1.0));\n"
1542 "float h = sqrt(1.0);\n"
1543 "float f = sqrt(1.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001544 "int s2s = s;\n"
1545 "int i2s = i;\n"
1546 "int us2s = int(us);\n"
1547 "int ui2s = int(ui);\n"
1548 "int h2s = int(h);\n"
1549 "int f2s = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001550 "int s2i = s;\n"
1551 "int i2i = i;\n"
1552 "int us2i = int(us);\n"
1553 "int ui2i = int(ui);\n"
1554 "int h2i = int(h);\n"
1555 "int f2i = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001556 "uint s2us = uint(s);\n"
1557 "uint i2us = uint(i);\n"
1558 "uint us2us = us;\n"
1559 "uint ui2us = ui;\n"
1560 "uint h2us = uint(h);\n"
1561 "uint f2us = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001562 "uint s2ui = uint(s);\n"
1563 "uint i2ui = uint(i);\n"
1564 "uint us2ui = us;\n"
1565 "uint ui2ui = ui;\n"
1566 "uint h2ui = uint(h);\n"
1567 "uint f2ui = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001568 "float s2f = float(s);\n"
1569 "float i2f = float(i);\n"
1570 "float us2f = float(us);\n"
1571 "float ui2f = float(ui);\n"
1572 "float h2f = h;\n"
Ethan Nicholas05d5a132017-09-15 11:42:17 -04001573 "float f2f = f;\n");
1574}
1575
1576DEF_TEST(SkSLForceHighPrecision, r) {
1577 test(r,
1578 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1579 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1580 "#version 400\n"
1581 "out mediump vec4 sk_FragColor;\n"
1582 "void main() {\n"
1583 " mediump float x = sqrt(1.0);\n"
1584 " mediump vec4 y = vec4(x);\n"
1585 " sk_FragColor = y;\n"
1586 "}\n");
1587 SkSL::Program::Settings settings;
1588 settings.fForceHighPrecision = true;
1589 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers();
1590 settings.fCaps = caps.get();
1591 SkSL::Program::Inputs inputs;
1592 test(r,
1593 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1594 settings,
1595 "#version 400\n"
1596 "out mediump vec4 sk_FragColor;\n"
1597 "void main() {\n"
1598 " highp float x = sqrt(1.0);\n"
1599 " highp vec4 y = vec4(x);\n"
1600 " sk_FragColor = y;\n"
1601 "}\n",
1602 &inputs);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001603}
1604
Ethan Nicholas7ef4b742016-11-11 15:16:46 -05001605#endif