blob: 8f09d6b0849218b536dd33070b15737571b41efc [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;
Ethan Nicholasa51740c2017-02-07 14:53:32 -050026 std::unique_ptr<SkSL::Program> program = compiler.convertProgram(kind, SkString(src), settings);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050027 if (!program) {
ethannicholasf789b382016-08-03 12:43:36 -070028 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
29 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -050030 REPORTER_ASSERT(r, program);
31 *inputs = program->fInputs;
32 REPORTER_ASSERT(r, compiler.toGLSL(*program, &output));
33 if (program) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040034 SkSL::String skExpected(expected);
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050035 if (output != skExpected) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -050036 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
ethannicholasf789b382016-08-03 12:43:36 -070037 expected, output.c_str());
38 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050039 REPORTER_ASSERT(r, output == skExpected);
ethannicholasf789b382016-08-03 12:43:36 -070040 }
41}
42
Ethan Nicholas941e7e22016-12-12 15:33:30 -050043static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
Ethan Nicholasa51740c2017-02-07 14:53:32 -050044 const char* expected, SkSL::Program::Kind kind = SkSL::Program::kFragment_Kind) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -050045 SkSL::Program::Settings settings;
46 settings.fCaps = &caps;
47 SkSL::Program::Inputs inputs;
Ethan Nicholasa51740c2017-02-07 14:53:32 -050048 test(r, src, settings, expected, &inputs, kind);
Ethan Nicholas941e7e22016-12-12 15:33:30 -050049}
50
ethannicholasf789b382016-08-03 12:43:36 -070051DEF_TEST(SkSLHelloWorld, r) {
ethannicholasf789b382016-08-03 12:43:36 -070052 test(r,
ethannicholas5961bc92016-10-12 06:39:56 -070053 "void main() { sk_FragColor = vec4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -050054 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070055 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050056 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070057 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050058 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070059 "}\n");
60}
61
62DEF_TEST(SkSLControl, r) {
ethannicholasf789b382016-08-03 12:43:36 -070063 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070064 "void main() {"
ethannicholas08a92112016-11-09 13:26:45 -080065 "if (sqrt(2) > 5) { sk_FragColor = vec4(0.75); } else { discard; }"
ethannicholasf789b382016-08-03 12:43:36 -070066 "int i = 0;"
Ethan Nicholascb670962017-04-20 19:31:52 -040067 "while (i < 10) { sk_FragColor *= 0.5; i++; }"
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050068 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);"
ethannicholasf789b382016-08-03 12:43:36 -070069 "for (int i = 0; i < 10; i++) {"
Ethan Nicholas86a43402017-01-19 13:32:00 -050070 "if (i % 2 == 1) break; else continue;"
ethannicholasf789b382016-08-03 12:43:36 -070071 "}"
72 "return;"
73 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -050074 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070075 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050076 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070077 "void main() {\n"
ethannicholas08a92112016-11-09 13:26:45 -080078 " if (sqrt(2.0) > 5.0) {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050079 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070080 " } else {\n"
81 " discard;\n"
82 " }\n"
83 " int i = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -040084 " while (i < 10) {\n"
85 " sk_FragColor *= 0.5;\n"
86 " i++;\n"
87 " }\n"
ethannicholasf789b382016-08-03 12:43:36 -070088 " do {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050089 " sk_FragColor += 0.01;\n"
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050090 " } while (sk_FragColor.x < 0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070091 " for (int i = 0;i < 10; i++) {\n"
Ethan Nicholas86a43402017-01-19 13:32:00 -050092 " if (i % 2 == 1) break; else continue;\n"
ethannicholasf789b382016-08-03 12:43:36 -070093 " }\n"
94 " return;\n"
95 "}\n");
96}
97
98DEF_TEST(SkSLFunctions, r) {
ethannicholasf789b382016-08-03 12:43:36 -070099 test(r,
ethannicholasf789b382016-08-03 12:43:36 -0700100 "float foo(float v[2]) { return v[0] * v[1]; }"
101 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
ethannicholas5961bc92016-10-12 06:39:56 -0700102 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500103 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700104 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500105 "out vec4 sk_FragColor;\n"
Ethan Nicholasc6f5e102017-03-31 14:53:17 -0400106 "float foo(float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -0700107 " return v[0] * v[1];\n"
108 "}\n"
109 "void bar(inout float x) {\n"
110 " float y[2], z;\n"
111 " y[0] = x;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700112 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700113 " z = foo(y);\n"
114 " x = z;\n"
115 "}\n"
116 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700117 " float x = 10.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400118 " bar(x);\n"
119 " sk_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700120 "}\n");
121}
122
123DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700124 test(r,
125 "void main() {"
126 "float x = 1, y = 2;"
127 "int z = 3;"
Ethan Nicholas86a43402017-01-19 13:32:00 -0500128 "x = x - x + y * z * x * (y - z);"
ethannicholasf789b382016-08-03 12:43:36 -0700129 "y = x / y / z;"
130 "z = (z / 2 % 3 << 4) >> 2 << 1;"
ethannicholas08a92112016-11-09 13:26:45 -0800131 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;"
ethannicholasf789b382016-08-03 12:43:36 -0700132 "x += 12;"
133 "x -= 12;"
134 "x *= y /= z = 10;"
135 "b ||= false;"
136 "b &&= true;"
137 "b ^^= false;"
138 "z |= 0;"
139 "z &= -1;"
140 "z ^= 0;"
141 "z >>= 2;"
142 "z <<= 4;"
143 "z %= 5;"
Ethan Nicholas4b330df2017-05-17 10:52:55 -0400144 "x = (vec2(sqrt(1)) , 6);"
145 "z = (vec2(sqrt(1)) , 6);"
ethannicholasf789b382016-08-03 12:43:36 -0700146 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500147 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700148 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500149 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700150 "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"
156 " bool b = false == true || 2.0 >= sqrt(2.0) && true;\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() {"
177 "mat2x4 x = mat2x4(1);"
178 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));"
179 "mat3x4 z = x * y;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400180 "vec3 v1 = mat3(1) * vec3(2);"
181 "vec3 v2 = vec3(2) * mat3(1);"
Ethan Nicholascb670962017-04-20 19:31:52 -0400182 "sk_FragColor = vec4(z[0].x, 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 {"
198 "float x;"
199 "float y[2];"
200 "layout(binding=12) mat3x2 z;"
201 "bool w;"
202 "};"
203 "void main() {"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500204 " sk_FragColor = vec4(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 {"
220 "float x;"
221 "} test;"
222 "void main() {"
223 " sk_FragColor = vec4(test.x);"
224 "}",
225 *SkSL::ShaderCapsFactory::Default(),
226 "#version 400\n"
227 "out vec4 sk_FragColor;\n"
228 "uniform testBlock {\n"
229 " float x;\n"
230 "} test;\n"
231 "void main() {\n"
232 " sk_FragColor = vec4(test.x);\n"
233 "}\n");
234 test(r,
235 "uniform testBlock {"
236 "float x;"
237 "} test[2];"
238 "void main() {"
239 " sk_FragColor = vec4(test[1].x);"
240 "}",
241 *SkSL::ShaderCapsFactory::Default(),
242 "#version 400\n"
243 "out vec4 sk_FragColor;\n"
244 "uniform testBlock {\n"
245 " float x;\n"
246 "} test[2];\n"
247 "void main() {\n"
248 " sk_FragColor = vec4(test[1].x);\n"
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 {"
260 "float x;"
261 "float y[2];"
262 "layout(binding=1) A z;"
263 "};"
264 "B b1, b2, b3;"
265 "void main() {"
266 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500267 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700268 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500269 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700270 "struct A {\n"
271 " int x;\n"
272 " int y;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500273 "} a1, a2;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700274 "A a3;\n"
275 "struct B {\n"
276 " float x;\n"
277 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700278 " layout (binding = 1) A z;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500279 "} b1, b2, b3;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700280 "void main() {\n"
281 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700282}
ethannicholasf789b382016-08-03 12:43:36 -0700283
ethannicholas5961bc92016-10-12 06:39:56 -0700284DEF_TEST(SkSLVersion, r) {
ethannicholas5961bc92016-10-12 06:39:56 -0700285 test(r,
286 "in float test; void main() { sk_FragColor = vec4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500287 *SkSL::ShaderCapsFactory::Version450Core(),
ethannicholas5961bc92016-10-12 06:39:56 -0700288 "#version 450 core\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500289 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700290 "in float test;\n"
291 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500292 " sk_FragColor = vec4(0.75);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700293 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700294 test(r,
295 "in float test; void main() { sk_FragColor = vec4(0.75); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500296 *SkSL::ShaderCapsFactory::Version110(),
ethannicholas5961bc92016-10-12 06:39:56 -0700297 "#version 110\n"
298 "varying float test;\n"
299 "void main() {\n"
300 " gl_FragColor = vec4(0.75);\n"
301 "}\n");
302}
303
Leon Scroggins857cb972016-11-11 18:44:37 +0000304DEF_TEST(SkSLUsesPrecisionModifiers, r) {
Leon Scroggins857cb972016-11-11 18:44:37 +0000305 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400306 "void main() { float x = 0.75; highp float y = 1; x++; y++;"
307 "sk_FragColor.rg = vec2(x, y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500308 *SkSL::ShaderCapsFactory::Default(),
Leon Scroggins857cb972016-11-11 18:44:37 +0000309 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500310 "out vec4 sk_FragColor;\n"
Leon Scroggins857cb972016-11-11 18:44:37 +0000311 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700312 " float x = 0.75;\n"
313 " float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400314 " x++;\n"
315 " y++;\n"
316 " sk_FragColor.xy = vec2(x, y);\n"
317 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700318 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400319 "void main() { float x = 0.75; highp float y = 1; x++; y++;"
320 "sk_FragColor.rg = vec2(x, y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500321 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500322 "#version 400\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700323 "precision highp float;\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"
326 " 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() {"
337 "float 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() {"
349 "float 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
ethannicholasad146f62016-10-14 06:40:02 -0700363DEF_TEST(SkSLNegatedAtan, r) {
364 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400365 "void main() { vec2 x = vec2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500366 *SkSL::ShaderCapsFactory::Default(),
ethannicholasad146f62016-10-14 06:40:02 -0700367 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500368 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700369 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400370 " vec2 x = vec2(sqrt(2.0));\n"
371 " sk_FragColor.x = atan(x.x, -x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700372 "}\n");
ethannicholasad146f62016-10-14 06:40:02 -0700373 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400374 "void main() { vec2 x = vec2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500375 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(),
ethannicholasad146f62016-10-14 06:40:02 -0700376 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500377 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700378 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400379 " vec2 x = vec2(sqrt(2.0));\n"
380 " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700381 "}\n");
382}
383
ethannicholas5961bc92016-10-12 06:39:56 -0700384DEF_TEST(SkSLModifiersDeclaration, r) {
385 test(r,
386 "layout(blend_support_all_equations) out;"
387 "void main() { }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500388 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700389 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500390 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700391 "layout (blend_support_all_equations) out ;\n"
392 "void main() {\n"
393 "}\n");
394}
395
396DEF_TEST(SkSLHex, r) {
397 test(r,
398 "void main() {"
399 "int i1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400400 "i1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700401 "int i2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400402 "i2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700403 "int i3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400404 "i3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700405 "int i4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400406 "i4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700407 "int i5 = -0xbeef;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400408 "i5++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700409 "uint u1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400410 "u1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700411 "uint u2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400412 "u2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700413 "uint u3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400414 "u3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700415 "uint u4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400416 "u4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700417 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500418 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700419 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500420 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700421 "void main() {\n"
422 " int i1 = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400423 " i1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700424 " int i2 = 305441741;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400425 " i2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700426 " int i3 = 2147483647;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400427 " i3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700428 " int i4 = -1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400429 " i4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700430 " int i5 = -48879;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400431 " i5++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700432 " uint u1 = 0u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400433 " u1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700434 " uint u2 = 305441741u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400435 " u2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700436 " uint u3 = 2147483647u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400437 " u3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700438 " uint u4 = 4294967295u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400439 " u4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700440 "}\n");
441}
442
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400443DEF_TEST(SkSLVectorConstructors, r) {
444 test(r,
445 "vec2 v1 = vec2(1);"
446 "vec2 v2 = vec2(1, 2);"
447 "vec2 v3 = vec2(vec2(1));"
Ethan Nicholas84645e32017-02-09 13:57:14 -0500448 "vec3 v4 = vec3(vec2(1), 1.0);"
449 "ivec2 v5 = ivec2(1);"
450 "ivec2 v6 = ivec2(vec2(1, 2));"
451 "vec2 v7 = vec2(ivec2(1, 2));",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500452 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400453 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500454 "out vec4 sk_FragColor;\n"
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400455 "vec2 v1 = vec2(1.0);\n"
456 "vec2 v2 = vec2(1.0, 2.0);\n"
457 "vec2 v3 = vec2(1.0);\n"
Ethan Nicholas84645e32017-02-09 13:57:14 -0500458 "vec3 v4 = vec3(vec2(1.0), 1.0);\n"
459 "ivec2 v5 = ivec2(1);\n"
460 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n"
461 "vec2 v7 = vec2(ivec2(1, 2));\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400462}
463
ethannicholas5961bc92016-10-12 06:39:56 -0700464DEF_TEST(SkSLArrayConstructors, r) {
465 test(r,
466 "float test1[] = float[](1, 2, 3, 4);"
467 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));"
468 "mat4 test3[] = mat4[]();",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500469 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700470 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500471 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700472 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
473 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
474 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700475}
ethannicholasddb37d62016-10-20 09:54:00 -0700476
477DEF_TEST(SkSLDerivatives, r) {
478 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400479 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500480 *SkSL::ShaderCapsFactory::Default(),
ethannicholasddb37d62016-10-20 09:54:00 -0700481 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500482 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700483 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400484 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700485 "}\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700486 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400487 "void main() { sk_FragColor.r = 1; }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500488 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700489 "#version 400\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400490 "precision highp float;\n"
491 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700492 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400493 " sk_FragColor.x = 1.0;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700494 "}\n");
495 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400496 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500497 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700498 "#version 400\n"
499 "#extension GL_OES_standard_derivatives : require\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400500 "precision highp float;\n"
501 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700502 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400503 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700504 "}\n");
505}
ethannicholas08a92112016-11-09 13:26:45 -0800506
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400507
508DEF_TEST(SkSLIntFolding, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800509 test(r,
510 "void main() {"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400511 "sk_FragColor.r = 32 + 2;"
512 "sk_FragColor.r = 32 - 2;"
513 "sk_FragColor.r = 32 * 2;"
514 "sk_FragColor.r = 32 / 2;"
515 "sk_FragColor.r = 12 | 6;"
516 "sk_FragColor.r = 254 & 7;"
517 "sk_FragColor.r = 2 ^ 7;"
518 "sk_FragColor.r = 1 << 4;"
519 "sk_FragColor.r = 128 >> 2;"
520 "sk_FragColor.r = -1 == -1 ? 1 : -1;"
521 "sk_FragColor.r = -1 == -2 ? 2 : -2;"
522 "sk_FragColor.r = 0 != 1 ? 3 : -3;"
523 "sk_FragColor.r = 0 != 0 ? 4 : -4;"
524 "sk_FragColor.r = 6 > 5 ? 5 : -5;"
525 "sk_FragColor.r = 6 > 6 ? 6 : -6;"
526 "sk_FragColor.r = -1 < 0 ? 7 : -7;"
527 "sk_FragColor.r = 1 < 0 ? 8 : -8;"
528 "sk_FragColor.r = 6 >= 6 ? 9 : -9;"
529 "sk_FragColor.r = 6 >= 7 ? 10 : -10;"
530 "sk_FragColor.r = 6 <= 6 ? 11 : -11;"
531 "sk_FragColor.r = 6 <= 5 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400532 "sk_FragColor.r = int(sqrt(1)) + 0;"
533 "sk_FragColor.r = 0 + int(sqrt(2));"
534 "sk_FragColor.r = int(sqrt(3)) - 0;"
535 "sk_FragColor.r = int(sqrt(4)) * 0;"
536 "sk_FragColor.r = int(sqrt(5)) * 1;"
537 "sk_FragColor.r = 1 * int(sqrt(6));"
538 "sk_FragColor.r = 0 * int(sqrt(7));"
539 "sk_FragColor.r = int(sqrt(8)) / 1;"
540 "sk_FragColor.r = 0 / int(sqrt(9));"
541 "int x = int(sqrt(2));"
542 "x += 1;"
543 "x += 0;"
544 "x -= 1;"
545 "x -= 0;"
546 "x *= 1;"
547 "x *= 2;"
548 "x /= 1;"
549 "x /= 2;"
550 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800551 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500552 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800553 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500554 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800555 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400556 " sk_FragColor.x = 34.0;\n"
557 " sk_FragColor.x = 30.0;\n"
558 " sk_FragColor.x = 64.0;\n"
559 " sk_FragColor.x = 16.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400560 " sk_FragColor.x = 14.0;\n"
561 " sk_FragColor.x = 6.0;\n"
562 " sk_FragColor.x = 5.0;\n"
563 " sk_FragColor.x = 16.0;\n"
564 " sk_FragColor.x = 32.0;\n"
565 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400566 " sk_FragColor.x = -2.0;\n"
567 " sk_FragColor.x = 3.0;\n"
568 " sk_FragColor.x = -4.0;\n"
569 " sk_FragColor.x = 5.0;\n"
570 " sk_FragColor.x = -6.0;\n"
571 " sk_FragColor.x = 7.0;\n"
572 " sk_FragColor.x = -8.0;\n"
573 " sk_FragColor.x = 9.0;\n"
574 " sk_FragColor.x = -10.0;\n"
575 " sk_FragColor.x = 11.0;\n"
576 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400577 " sk_FragColor.x = float(int(sqrt(1.0)));\n"
578 " sk_FragColor.x = float(int(sqrt(2.0)));\n"
579 " sk_FragColor.x = float(int(sqrt(3.0)));\n"
580 " sk_FragColor.x = 0.0;\n"
581 " sk_FragColor.x = float(int(sqrt(5.0)));\n"
582 " sk_FragColor.x = float(int(sqrt(6.0)));\n"
583 " sk_FragColor.x = 0.0;\n"
584 " sk_FragColor.x = float(int(sqrt(8.0)));\n"
585 " sk_FragColor.x = 0.0;\n"
586 " int x = int(sqrt(2.0));\n"
587 " x += 1;\n"
588 " x -= 1;\n"
589 " x *= 2;\n"
590 " x /= 2;\n"
591 " sk_FragColor.x = float(x);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400592 "}\n");
593}
594
595DEF_TEST(SkSLFloatFolding, r) {
596 test(r,
597 "void main() {"
598 "sk_FragColor.r = 32.0 + 2.0;"
599 "sk_FragColor.r = 32.0 - 2.0;"
600 "sk_FragColor.r = 32.0 * 2.0;"
601 "sk_FragColor.r = 32.0 / 2.0;"
602 "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
603 "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;"
604 "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;"
605 "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;"
606 "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;"
607 "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;"
608 "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;"
609 "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;"
610 "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;"
611 "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;"
612 "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;"
613 "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;"
614 "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400615 "sk_FragColor.r = sqrt(1) + 0;"
616 "sk_FragColor.r = 0 + sqrt(2);"
617 "sk_FragColor.r = sqrt(3) - 0;"
618 "sk_FragColor.r = sqrt(4) * 0;"
619 "sk_FragColor.r = sqrt(5) * 1;"
620 "sk_FragColor.r = 1 * sqrt(6);"
621 "sk_FragColor.r = 0 * sqrt(7);"
622 "sk_FragColor.r = sqrt(8) / 1;"
623 "sk_FragColor.r = 0 / sqrt(9);"
624 "sk_FragColor.r += 1;"
625 "sk_FragColor.r += 0;"
626 "sk_FragColor.r -= 1;"
627 "sk_FragColor.r -= 0;"
628 "sk_FragColor.r *= 1;"
629 "sk_FragColor.r *= 2;"
630 "sk_FragColor.r /= 1;"
631 "sk_FragColor.r /= 2;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400632 "}",
633 *SkSL::ShaderCapsFactory::Default(),
634 "#version 400\n"
635 "out vec4 sk_FragColor;\n"
636 "void main() {\n"
637 " sk_FragColor.x = 34.0;\n"
638 " sk_FragColor.x = 30.0;\n"
639 " sk_FragColor.x = 64.0;\n"
640 " sk_FragColor.x = 16.0;\n"
641 " sk_FragColor.x = 19.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400642 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400643 " sk_FragColor.x = -2.0;\n"
644 " sk_FragColor.x = 3.0;\n"
645 " sk_FragColor.x = -4.0;\n"
646 " sk_FragColor.x = 5.0;\n"
647 " sk_FragColor.x = -6.0;\n"
648 " sk_FragColor.x = 7.0;\n"
649 " sk_FragColor.x = -8.0;\n"
650 " sk_FragColor.x = 9.0;\n"
651 " sk_FragColor.x = -10.0;\n"
652 " sk_FragColor.x = 11.0;\n"
653 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400654 " sk_FragColor.x = sqrt(1.0);\n"
655 " sk_FragColor.x = sqrt(2.0);\n"
656 " sk_FragColor.x = sqrt(3.0);\n"
657 " sk_FragColor.x = 0.0;\n"
658 " sk_FragColor.x = sqrt(5.0);\n"
659 " sk_FragColor.x = sqrt(6.0);\n"
660 " sk_FragColor.x = 0.0;\n"
661 " sk_FragColor.x = sqrt(8.0);\n"
662 " sk_FragColor.x = 0.0;\n"
663 " sk_FragColor.x += 1.0;\n"
664 " sk_FragColor.x -= 1.0;\n"
665 " sk_FragColor.x *= 2.0;\n"
666 " sk_FragColor.x /= 2.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400667 "}\n");
668}
669
670DEF_TEST(SkSLBoolFolding, r) {
671 test(r,
672 "void main() {"
673 "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;"
674 "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;"
675 "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;"
676 "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;"
677 "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;"
678 "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;"
679 "}",
680 *SkSL::ShaderCapsFactory::Default(),
681 "#version 400\n"
682 "out vec4 sk_FragColor;\n"
683 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400684 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400685 " sk_FragColor.x = -2.0;\n"
686 " sk_FragColor.x = 3.0;\n"
687 " sk_FragColor.x = -4.0;\n"
688 " sk_FragColor.x = 5.0;\n"
689 " sk_FragColor.x = -6.0;\n"
690 "}\n");
691}
692
693DEF_TEST(SkSLVecFolding, r) {
694 test(r,
695 "void main() {"
696 "sk_FragColor.r = vec4(0.5, 1, 1, 1).x;"
697 "sk_FragColor = vec4(vec2(1), vec2(2, 3)) + vec4(5, 6, 7, 8);"
698 "sk_FragColor = vec4(8, vec3(10)) - vec4(1);"
699 "sk_FragColor = vec4(2) * vec4(1, 2, 3, 4);"
700 "sk_FragColor = vec4(12) / vec4(1, 2, 3, 4);"
701 "sk_FragColor.r = (vec4(12) / vec4(1, 2, 3, 4)).y;"
702 "sk_FragColor.x = vec4(1) == vec4(1) ? 1.0 : -1.0;"
703 "sk_FragColor.x = vec4(1) == vec4(2) ? 2.0 : -2.0;"
704 "sk_FragColor.x = vec2(1) == vec2(1, 1) ? 3.0 : -3.0;"
705 "sk_FragColor.x = vec2(1, 1) == vec2(1, 1) ? 4.0 : -4.0;"
706 "sk_FragColor.x = vec2(1) == vec2(1, 0) ? 5.0 : -5.0;"
707 "sk_FragColor.x = vec4(1) == vec4(vec2(1), vec2(1)) ? 6.0 : -6.0;"
708 "sk_FragColor.x = vec4(vec3(1), 1) == vec4(vec2(1), vec2(1)) ? 7.0 : -7.0;"
709 "sk_FragColor.x = vec4(vec3(1), 1) == vec4(vec2(1), 1, 0) ? 8.0 : -8.0;"
710 "sk_FragColor.x = vec2(1) != vec2(1, 0) ? 9.0 : -9.0;"
711 "sk_FragColor.x = vec4(1) != vec4(vec2(1), vec2(1)) ? 10.0 : -10.0;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400712 "sk_FragColor = vec4(sqrt(1)) * vec4(1);"
713 "sk_FragColor = vec4(1) * vec4(sqrt(2));"
714 "sk_FragColor = vec4(0) * vec4(sqrt(3));"
715 "sk_FragColor = vec4(sqrt(4)) * vec4(0);"
716 "sk_FragColor = vec4(0) / vec4(sqrt(5));"
717 "sk_FragColor = vec4(0) + vec4(sqrt(6));"
718 "sk_FragColor = vec4(sqrt(7)) + vec4(0);"
719 "sk_FragColor = vec4(sqrt(8)) - vec4(0);"
720 "sk_FragColor = vec4(0) + sqrt(9);"
721 "sk_FragColor = vec4(0) * sqrt(10);"
722 "sk_FragColor = vec4(0) / sqrt(11);"
723 "sk_FragColor = vec4(1) * sqrt(12);"
724 "sk_FragColor = 0 + vec4(sqrt(13));"
725 "sk_FragColor = 0 * vec4(sqrt(14));"
726 "sk_FragColor = 0 / vec4(sqrt(15));"
727 "sk_FragColor = 1 * vec4(sqrt(16));"
728 "sk_FragColor = vec4(sqrt(17)) + 0;"
729 "sk_FragColor = vec4(sqrt(18)) * 0;"
730 "sk_FragColor = vec4(sqrt(19)) * 1;"
731 "sk_FragColor = vec4(sqrt(19.5)) - 0;"
732 "sk_FragColor = sqrt(20) * vec4(1);"
733 "sk_FragColor = sqrt(21) + vec4(0);"
734 "sk_FragColor = sqrt(22) - vec4(0);"
735 "sk_FragColor = sqrt(23) / vec4(1);"
736 "sk_FragColor = vec4(sqrt(24)) / 1;"
737 "sk_FragColor += vec4(1);"
738 "sk_FragColor += vec4(0);"
739 "sk_FragColor -= vec4(1);"
740 "sk_FragColor -= vec4(0);"
741 "sk_FragColor *= vec4(1);"
742 "sk_FragColor *= vec4(2);"
743 "sk_FragColor /= vec4(1);"
744 "sk_FragColor /= vec4(2);"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400745 "}",
746 *SkSL::ShaderCapsFactory::Default(),
747 "#version 400\n"
748 "out vec4 sk_FragColor;\n"
749 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400750 " sk_FragColor.x = 0.5;\n"
751 " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
752 " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
753 " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
754 " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
755 " sk_FragColor.x = 6.0;\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400756 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400757 " sk_FragColor.x = -2.0;\n"
758 " sk_FragColor.x = 3.0;\n"
759 " sk_FragColor.x = 4.0;\n"
760 " sk_FragColor.x = -5.0;\n"
761 " sk_FragColor.x = 6.0;\n"
762 " sk_FragColor.x = 7.0;\n"
763 " sk_FragColor.x = -8.0;\n"
764 " sk_FragColor.x = 9.0;\n"
765 " sk_FragColor.x = -10.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400766 " sk_FragColor = vec4(sqrt(1.0));\n"
767 " sk_FragColor = vec4(sqrt(2.0));\n"
768 " sk_FragColor = vec4(0.0);\n"
769 " sk_FragColor = vec4(0.0);\n"
770 " sk_FragColor = vec4(0.0);\n"
771 " sk_FragColor = vec4(sqrt(6.0));\n"
772 " sk_FragColor = vec4(sqrt(7.0));\n"
773 " sk_FragColor = vec4(sqrt(8.0));\n"
774 " sk_FragColor = vec4(sqrt(9.0));\n"
775 " sk_FragColor = vec4(0.0);\n"
776 " sk_FragColor = vec4(0.0);\n"
777 " sk_FragColor = vec4(sqrt(12.0));\n"
778 " sk_FragColor = vec4(sqrt(13.0));\n"
779 " sk_FragColor = vec4(0.0);\n"
780 " sk_FragColor = vec4(0.0);\n"
781 " sk_FragColor = vec4(sqrt(16.0));\n"
782 " sk_FragColor = vec4(sqrt(17.0));\n"
783 " sk_FragColor = vec4(0.0);\n"
784 " sk_FragColor = vec4(sqrt(19.0));\n"
785 " sk_FragColor = vec4(sqrt(19.5));\n"
786 " sk_FragColor = vec4(sqrt(20.0));\n"
787 " sk_FragColor = vec4(sqrt(21.0));\n"
788 " sk_FragColor = vec4(sqrt(22.0));\n"
789 " sk_FragColor = vec4(sqrt(23.0));\n"
790 " sk_FragColor = vec4(sqrt(24.0));\n"
791 " sk_FragColor += vec4(1.0);\n"
792 " sk_FragColor -= vec4(1.0);\n"
793 " sk_FragColor *= vec4(2.0);\n"
794 " sk_FragColor /= vec4(2.0);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400795 "}\n");
796}
797
798DEF_TEST(SkSLMatFolding, r) {
799 test(r,
800 "void main() {"
801 "sk_FragColor.x = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) == "
802 "mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 1 : -1;"
803 "sk_FragColor.x = mat2(vec2(1.0, 0.0), vec2(1.0, 1.0)) == "
804 "mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 2 : -2;"
805 "sk_FragColor.x = mat2(1) == mat2(1) ? 3 : -3;"
806 "sk_FragColor.x = mat2(1) == mat2(0) ? 4 : -4;"
807 "sk_FragColor.x = mat2(1) == mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 5 : -5;"
808 "sk_FragColor.x = mat2(2) == mat2(vec2(1.0, 0.0), vec2(0.0, 1.0)) ? 6 : -6;"
809 "sk_FragColor.x = mat3x2(2) == mat3x2(vec2(2.0, 0.0), vec2(0.0, 2.0), vec2(0.0)) ? 7 : -7;"
810 "sk_FragColor.x = mat2(1) != mat2(1) ? 8 : -8;"
811 "sk_FragColor.x = mat2(1) != mat2(0) ? 9 : -9;"
812 "sk_FragColor.x = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 0.0)) == "
813 "mat3(mat2(1.0)) ? 10 : -10;"
814 "sk_FragColor.x = mat2(mat3(1.0)) == mat2(1.0) ? 11 : -11;"
815 "sk_FragColor.x = mat2(vec4(1.0, 0.0, 0.0, 1.0)) == mat2(1.0) ? 12 : -12;"
816 "sk_FragColor.x = mat2(1.0, 0.0, vec2(0.0, 1.0)) == mat2(1.0) ? 13 : -13;"
817 "sk_FragColor.x = mat2(vec2(1.0, 0.0), 0.0, 1.0) == mat2(1.0) ? 14 : -14;"
818 "}",
819 *SkSL::ShaderCapsFactory::Default(),
820 "#version 400\n"
821 "out vec4 sk_FragColor;\n"
822 "void main() {\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400823 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400824 " sk_FragColor.x = -2.0;\n"
825 " sk_FragColor.x = 3.0;\n"
826 " sk_FragColor.x = -4.0;\n"
827 " sk_FragColor.x = 5.0;\n"
828 " sk_FragColor.x = -6.0;\n"
829 " sk_FragColor.x = 7.0;\n"
830 " sk_FragColor.x = -8.0;\n"
831 " sk_FragColor.x = 9.0;\n"
832 " sk_FragColor.x = 10.0;\n"
833 " sk_FragColor.x = 11.0;\n"
834 " sk_FragColor.x = 12.0;\n"
835 " sk_FragColor.x = 13.0;\n"
836 " sk_FragColor.x = 14.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800837 "}\n");
838}
839
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400840DEF_TEST(SkSLConstantIf, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800841 test(r,
842 "void main() {"
843 "int x;"
844 "if (true) x = 1;"
845 "if (2 > 1) x = 2; else x = 3;"
846 "if (1 > 2) x = 4; else x = 5;"
847 "if (false) x = 6;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400848 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800849 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500850 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800851 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500852 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800853 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400854 " sk_FragColor.x = 5.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800855 "}\n");
856}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500857
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500858DEF_TEST(SkSLCaps, r) {
859 test(r,
860 "void main() {"
Ethan Nicholascb670962017-04-20 19:31:52 -0400861 "int x = 0;"
862 "int y = 0;"
863 "int z = 0;"
864 "int w = 0;"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500865 "if (sk_Caps.externalTextureSupport) x = 1;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400866 "if (sk_Caps.fbFetchSupport) y = 1;"
867 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;"
868 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;"
869 "sk_FragColor = vec4(x, y, z, w);"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500870 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500871 *SkSL::ShaderCapsFactory::VariousCaps(),
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500872 "#version 400\n"
873 "out vec4 sk_FragColor;\n"
874 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400875 " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500876 "}\n");
877}
878
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500879DEF_TEST(SkSLTexture, r) {
880 test(r,
881 "uniform sampler1D one;"
882 "uniform sampler2D two;"
883 "void main() {"
884 "vec4 a = texture(one, 0);"
885 "vec4 b = texture(two, vec2(0));"
886 "vec4 c = texture(one, vec2(0));"
887 "vec4 d = texture(two, vec3(0));"
Ethan Nicholascb670962017-04-20 19:31:52 -0400888 "sk_FragColor = vec4(a.x, b.x, c.x, d.x);"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500889 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500890 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500891 "#version 400\n"
892 "out vec4 sk_FragColor;\n"
893 "uniform sampler1D one;\n"
894 "uniform sampler2D two;\n"
895 "void main() {\n"
896 " vec4 a = texture(one, 0.0);\n"
897 " vec4 b = texture(two, vec2(0.0));\n"
898 " vec4 c = textureProj(one, vec2(0.0));\n"
899 " vec4 d = textureProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400900 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500901 "}\n");
902 test(r,
903 "uniform sampler1D one;"
904 "uniform sampler2D two;"
905 "void main() {"
906 "vec4 a = texture(one, 0);"
907 "vec4 b = texture(two, vec2(0));"
908 "vec4 c = texture(one, vec2(0));"
909 "vec4 d = texture(two, vec3(0));"
Ethan Nicholascb670962017-04-20 19:31:52 -0400910 "sk_FragColor = vec4(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::Version110(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500913 "#version 110\n"
914 "uniform sampler1D one;\n"
915 "uniform sampler2D two;\n"
916 "void main() {\n"
917 " vec4 a = texture1D(one, 0.0);\n"
918 " vec4 b = texture2D(two, vec2(0.0));\n"
919 " vec4 c = texture1DProj(one, vec2(0.0));\n"
920 " vec4 d = texture2DProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400921 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500922 "}\n");
923}
924
Ethan Nicholas19671772016-11-28 16:30:17 -0500925DEF_TEST(SkSLOffset, r) {
926 test(r,
927 "struct Test {"
928 "layout(offset = 0) int x;"
929 "layout(offset = 4) int y;"
930 "int z;"
931 "} test;",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500932 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas19671772016-11-28 16:30:17 -0500933 "#version 400\n"
934 "out vec4 sk_FragColor;\n"
935 "struct Test {\n"
936 " layout (offset = 0) int x;\n"
937 " layout (offset = 4) int y;\n"
938 " int z;\n"
939 "} test;\n");
940}
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500941
942DEF_TEST(SkSLFragCoord, r) {
943 SkSL::Program::Settings settings;
944 settings.fFlipY = true;
945 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
946 settings.fCaps = caps.get();
947 SkSL::Program::Inputs inputs;
948 test(r,
949 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
950 settings,
951 "#version 110\n"
952 "#extension GL_ARB_fragment_coord_conventions : require\n"
953 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
954 "void main() {\n"
955 " gl_FragColor.xy = gl_FragCoord.xy;\n"
956 "}\n",
957 &inputs);
958 REPORTER_ASSERT(r, !inputs.fRTHeight);
959
960 caps = SkSL::ShaderCapsFactory::FragCoordsNew();
961 settings.fCaps = caps.get();
962 test(r,
963 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
964 settings,
965 "#version 400\n"
966 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
967 "out vec4 sk_FragColor;\n"
968 "void main() {\n"
969 " sk_FragColor.xy = gl_FragCoord.xy;\n"
970 "}\n",
971 &inputs);
972 REPORTER_ASSERT(r, !inputs.fRTHeight);
973
974 caps = SkSL::ShaderCapsFactory::Default();
975 settings.fCaps = caps.get();
976 test(r,
977 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
978 settings,
979 "#version 400\n"
980 "uniform float u_skRTHeight;\n"
981 "out vec4 sk_FragColor;\n"
982 "void main() {\n"
983 " vec2 _sktmpCoord = gl_FragCoord.xy;\n"
984 " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n"
985 " sk_FragColor.xy = sk_FragCoord.xy;\n"
986 "}\n",
987 &inputs);
988 REPORTER_ASSERT(r, inputs.fRTHeight);
989
990 settings.fFlipY = false;
991 test(r,
992 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
993 settings,
994 "#version 400\n"
995 "out vec4 sk_FragColor;\n"
996 "void main() {\n"
997 " sk_FragColor.xy = gl_FragCoord.xy;\n"
998 "}\n",
999 &inputs);
1000 REPORTER_ASSERT(r, !inputs.fRTHeight);
1001}
1002
Ethan Nicholasa51740c2017-02-07 14:53:32 -05001003DEF_TEST(SkSLVertexID, r) {
1004 test(r,
1005 "out int id; void main() { id = sk_VertexID; }",
1006 *SkSL::ShaderCapsFactory::Default(),
1007 "#version 400\n"
1008 "out int id;\n"
1009 "void main() {\n"
1010 " id = gl_VertexID;\n"
1011 "}\n",
1012 SkSL::Program::kVertex_Kind);
1013}
1014
Ethan Nicholas67d64602017-02-09 10:15:25 -05001015DEF_TEST(SkSLClipDistance, r) {
1016 test(r,
1017 "void main() { sk_ClipDistance[0] = 0; }",
1018 *SkSL::ShaderCapsFactory::Default(),
1019 "#version 400\n"
1020 "void main() {\n"
1021 " gl_ClipDistance[0] = 0.0;\n"
1022 "}\n",
1023 SkSL::Program::kVertex_Kind);
1024 test(r,
1025 "void main() { sk_FragColor = vec4(sk_ClipDistance[0]); }",
1026 *SkSL::ShaderCapsFactory::Default(),
1027 "#version 400\n"
1028 "out vec4 sk_FragColor;\n"
1029 "void main() {\n"
1030 " sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1031 "}\n");
1032}
Ethan Nicholas50afc172017-02-16 14:49:57 -05001033
1034DEF_TEST(SkSLArrayTypes, r) {
1035 test(r,
1036 "void main() { vec2 x[2] = vec2[2](vec2(1), vec2(2));"
Ethan Nicholascb670962017-04-20 19:31:52 -04001037 "vec2[2] y = vec2[2](vec2(3), vec2(4));"
1038 "sk_FragColor = vec4(x[0], y[1]); }",
Ethan Nicholas50afc172017-02-16 14:49:57 -05001039 *SkSL::ShaderCapsFactory::Default(),
1040 "#version 400\n"
1041 "out vec4 sk_FragColor;\n"
1042 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001043 " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1044 "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -05001045 "}\n");
1046}
1047
Ethan Nicholas52cad152017-02-16 16:37:32 -05001048DEF_TEST(SkSLGeometry, r) {
1049 test(r,
1050 "layout(points) in;"
1051 "layout(invocations = 2) in;"
1052 "layout(line_strip, max_vertices = 2) out;"
1053 "void main() {"
1054 "gl_Position = sk_in[0].gl_Position + vec4(-0.5, 0, 0, sk_InvocationID);"
1055 "EmitVertex();"
1056 "gl_Position = sk_in[0].gl_Position + vec4(0.5, 0, 0, sk_InvocationID);"
1057 "EmitVertex();"
1058 "EndPrimitive();"
1059 "}",
1060 *SkSL::ShaderCapsFactory::Default(),
1061 "#version 400\n"
1062 "layout (points) in ;\n"
1063 "layout (invocations = 2) in ;\n"
1064 "layout (line_strip, max_vertices = 2) out ;\n"
1065 "void main() {\n"
1066 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1067 " EmitVertex();\n"
1068 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1069 " EmitVertex();\n"
1070 " EndPrimitive();\n"
1071 "}\n",
1072 SkSL::Program::kGeometry_Kind);
1073}
1074
Ethan Nicholasaf197692017-02-27 13:26:45 -05001075DEF_TEST(SkSLSwitch, r) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001076 // basic "does a switch even work" test
Ethan Nicholasaf197692017-02-27 13:26:45 -05001077 test(r,
1078 "void main() {"
1079 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001080 " switch (int(sqrt(1))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001081 " case 0:"
1082 " x = 0.0;"
1083 " break;"
1084 " case 1:"
1085 " x = 1.0;"
1086 " break;"
1087 " default:"
1088 " x = 2.0;"
1089 " }"
1090 " sk_FragColor = vec4(x);"
1091 "}",
1092 *SkSL::ShaderCapsFactory::Default(),
1093 "#version 400\n"
1094 "out vec4 sk_FragColor;\n"
1095 "void main() {\n"
1096 " float x;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001097 " switch (int(sqrt(1.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001098 " case 0:\n"
1099 " x = 0.0;\n"
1100 " break;\n"
1101 " case 1:\n"
1102 " x = 1.0;\n"
1103 " break;\n"
1104 " default:\n"
1105 " x = 2.0;\n"
1106 " }\n"
1107 " sk_FragColor = vec4(x);\n"
1108 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001109 // dead code inside of switch
Ethan Nicholasaf197692017-02-27 13:26:45 -05001110 test(r,
1111 "void main() {"
1112 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001113 " switch (int(sqrt(2))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001114 " case 0:"
1115 " x = 0.0;"
1116 " case 1:"
1117 " x = 1.0;"
1118 " default:"
1119 " x = 2.0;"
1120 " }"
1121 " sk_FragColor = vec4(x);"
1122 "}",
1123 *SkSL::ShaderCapsFactory::Default(),
1124 "#version 400\n"
1125 "out vec4 sk_FragColor;\n"
1126 "void main() {\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001127 " switch (int(sqrt(2.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001128 " case 0:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001129 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001130 " case 1:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001131 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001132 " default:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001133 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001134 " }\n"
1135 " sk_FragColor = vec4(2.0);\n"
1136 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001137 // non-static test w/ fallthrough
Ethan Nicholasaf197692017-02-27 13:26:45 -05001138 test(r,
1139 "void main() {"
1140 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001141 " switch (int(sqrt(3))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001142 " case 0:"
1143 " x = 0.0;"
1144 " case 1:"
1145 " x = 1.0;"
1146 " }"
1147 " sk_FragColor = vec4(x);"
1148 "}",
1149 *SkSL::ShaderCapsFactory::Default(),
1150 "#version 400\n"
1151 "out vec4 sk_FragColor;\n"
1152 "void main() {\n"
1153 " float x = 0.0;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001154 " switch (int(sqrt(3.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001155 " case 0:\n"
1156 " x = 0.0;\n"
1157 " case 1:\n"
1158 " x = 1.0;\n"
1159 " }\n"
1160 " sk_FragColor = vec4(x);\n"
1161 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001162 // static test w/ fallthrough
1163 test(r,
1164 "void main() {"
1165 " float x = 0.0;"
1166 " switch (0) {"
1167 " case 0:"
1168 " x = 0.0;"
1169 " case 1:"
1170 " x = 1.0;"
1171 " }"
1172 " sk_FragColor = vec4(x);"
1173 "}",
1174 *SkSL::ShaderCapsFactory::Default(),
1175 "#version 400\n"
1176 "out vec4 sk_FragColor;\n"
1177 "void main() {\n"
1178 " sk_FragColor = vec4(1.0);\n"
1179 "}\n");
1180 // static test w/ fallthrough, different entry point
1181 test(r,
1182 "void main() {"
1183 " float x = 0.0;"
1184 " switch (1) {"
1185 " case 0:"
1186 " x = 0.0;"
1187 " case 1:"
1188 " x = 1.0;"
1189 " }"
1190 " sk_FragColor = vec4(x);"
1191 "}",
1192 *SkSL::ShaderCapsFactory::Default(),
1193 "#version 400\n"
1194 "out vec4 sk_FragColor;\n"
1195 "void main() {\n"
1196 " sk_FragColor = vec4(1.0);\n"
1197 "}\n");
1198 // static test w/ break
1199 test(r,
1200 "void main() {"
1201 " float x = 0.0;"
1202 " switch (0) {"
1203 " case 0:"
1204 " x = 0.0;"
1205 " break;"
1206 " case 1:"
1207 " x = 1.0;"
1208 " }"
1209 " sk_FragColor = vec4(x);"
1210 "}",
1211 *SkSL::ShaderCapsFactory::Default(),
1212 "#version 400\n"
1213 "out vec4 sk_FragColor;\n"
1214 "void main() {\n"
1215 " sk_FragColor = vec4(0.0);\n"
1216 "}\n");
1217 // static test w/ static conditional break
1218 test(r,
1219 "void main() {"
1220 " float x = 0.0;"
1221 " switch (0) {"
1222 " case 0:"
1223 " x = 0.0;"
1224 " if (x < 1) break;"
1225 " case 1:"
1226 " x = 1.0;"
1227 " }"
1228 " sk_FragColor = vec4(x);"
1229 "}",
1230 *SkSL::ShaderCapsFactory::Default(),
1231 "#version 400\n"
1232 "out vec4 sk_FragColor;\n"
1233 "void main() {\n"
1234 " sk_FragColor = vec4(0.0);\n"
1235 "}\n");
1236 // static test w/ non-static conditional break
1237 test(r,
1238 "void main() {"
1239 " float x = 0.0;"
1240 " switch (0) {"
1241 " case 0:"
1242 " x = 0.0;"
1243 " if (x < sqrt(1)) break;"
1244 " case 1:"
1245 " x = 1.0;"
1246 " }"
1247 " sk_FragColor = vec4(x);"
1248 "}",
1249 *SkSL::ShaderCapsFactory::Default(),
1250 "#version 400\n"
1251 "out vec4 sk_FragColor;\n"
1252 "void main() {\n"
1253 " float x = 0.0;\n"
1254 " switch (0) {\n"
1255 " case 0:\n"
1256 " x = 0.0;\n"
1257 " if (0.0 < sqrt(1.0)) break;\n"
1258 " case 1:\n"
1259 " x = 1.0;\n"
1260 " }\n"
1261 " sk_FragColor = vec4(x);\n"
1262 "}\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -05001263}
1264
Ethan Nicholas5338f992017-04-19 15:54:07 -04001265DEF_TEST(SkSLRectangleTexture, r) {
1266 test(r,
1267 "uniform sampler2D test;"
1268 "void main() {"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001269 " sk_FragColor = texture(test, vec2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001270 "}",
1271 *SkSL::ShaderCapsFactory::Default(),
1272 "#version 400\n"
1273 "out vec4 sk_FragColor;\n"
1274 "uniform sampler2D test;\n"
1275 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001276 " sk_FragColor = texture(test, vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001277 "}\n");
1278 test(r,
1279 "uniform sampler2DRect test;"
1280 "void main() {"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001281 " sk_FragColor = texture(test, vec2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001282 "}",
1283 *SkSL::ShaderCapsFactory::Default(),
1284 "#version 400\n"
1285 "out vec4 sk_FragColor;\n"
1286 "uniform sampler2DRect test;\n"
1287 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001288 " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001289 "}\n");
1290 test(r,
1291 "uniform sampler2DRect test;"
1292 "void main() {"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001293 " sk_FragColor = texture(test, vec3(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001294 "}",
1295 *SkSL::ShaderCapsFactory::Default(),
1296 "#version 400\n"
1297 "out vec4 sk_FragColor;\n"
1298 "uniform sampler2DRect test;\n"
1299 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001300 " sk_FragColor = texture(test, vec3(textureSize(test), 1.0) * vec3(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001301 "}\n");
1302}
1303
Ethan Nicholascb670962017-04-20 19:31:52 -04001304DEF_TEST(SkSLUnusedVars, r) {
1305 test(r,
1306 "void main() {"
1307 "float a = 1, b = 2, c = 3;"
1308 "float d = c;"
1309 "float e = d;"
1310 "b++;"
1311 "d++;"
1312 "sk_FragColor = vec4(b, b, d, d);"
1313 "}",
1314 *SkSL::ShaderCapsFactory::Default(),
1315 "#version 400\n"
1316 "out vec4 sk_FragColor;\n"
1317 "void main() {\n"
1318 " float b = 2.0;\n"
1319 " float d = 3.0;\n"
1320 " b++;\n"
1321 " d++;\n"
1322 " sk_FragColor = vec4(b, b, d, d);\n"
1323 "}\n");
1324}
1325
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001326DEF_TEST(SkSLMultipleAssignments, r) {
1327 test(r,
1328 "void main() {"
1329 "float x;"
1330 "float y;"
1331 "int z;"
1332 "x = y = z = 1;"
1333 "sk_FragColor = vec4(z);"
1334 "}",
1335 *SkSL::ShaderCapsFactory::Default(),
1336 "#version 400\n"
1337 "out vec4 sk_FragColor;\n"
1338 "void main() {\n"
1339 " sk_FragColor = vec4(1.0);\n"
1340 "}\n");
1341}
1342
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001343DEF_TEST(SkSLComplexDelete, r) {
1344 test(r,
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001345 "uniform mat4 colorXform;"
1346 "uniform sampler2D sampler;"
1347 "void main() {"
1348 "vec4 tmpColor;"
1349 "sk_FragColor = vec4(1.0) * (tmpColor = texture(sampler, vec2(1)) , "
1350 "colorXform != mat4(1.0) ? vec4(clamp((mat4(colorXform) * vec4(tmpColor.xyz, 1.0)).xyz, "
1351 "0.0, tmpColor.w), tmpColor.w) : tmpColor);"
1352 "}",
1353 *SkSL::ShaderCapsFactory::Default(),
1354 "#version 400\n"
1355 "out vec4 sk_FragColor;\n"
1356 "uniform mat4 colorXform;\n"
1357 "uniform sampler2D sampler;\n"
1358 "void main() {\n"
1359 " vec4 tmpColor;\n"
1360 " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1361 "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1362 "tmpColor);\n"
1363 "}\n");
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001364}
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001365
1366DEF_TEST(SkSLDependentInitializers, r) {
1367 test(r,
1368 "void main() {"
1369 "float x = 0.5, y = x * 2;"
1370 "sk_FragColor = vec4(y);"
1371 "}",
1372 *SkSL::ShaderCapsFactory::Default(),
1373 "#version 400\n"
1374 "out vec4 sk_FragColor;\n"
1375 "void main() {\n"
1376 " sk_FragColor = vec4(1.0);\n"
1377 "}\n");
1378}
1379
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001380DEF_TEST(SkSLDeadLoopVar, r) {
1381 test(r,
1382 "void main() {"
1383 "for (int x = 0; x < 4; ) {"
1384 "break;"
1385 "}"
1386 "}",
1387 *SkSL::ShaderCapsFactory::Default(),
1388 "#version 400\n"
1389 "out vec4 sk_FragColor;\n"
1390 "void main() {\n"
1391 " for (; true; ) {\n"
1392 " break;\n"
1393 " }\n"
1394 "}\n"
1395 );
1396}
1397
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001398DEF_TEST(SkSLInvocations, r) {
1399 test(r,
1400 "layout(points) in;"
1401 "layout(invocations = 2) in;"
1402 "layout(line_strip, max_vertices = 2) out;"
1403 "void test() {"
1404 "gl_Position = sk_in[0].gl_Position + vec4(0.5, 0, 0, sk_InvocationID);"
1405 "EmitVertex();"
1406 "}"
1407 "void main() {"
1408 "gl_Position = sk_in[0].gl_Position + vec4(-0.5, 0, 0, sk_InvocationID);"
1409 "EmitVertex();"
1410 "}",
1411 *SkSL::ShaderCapsFactory::MustImplementGSInvocationsWithLoop(),
1412 "#version 400\n"
1413 "int sk_InvocationID;\n"
1414 "layout (points) in ;\n"
1415 "layout (line_strip, max_vertices = 4) out ;\n"
1416 "void test() {\n"
1417 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1418 " EmitVertex();\n"
1419 "}\n"
1420 "void _invoke() {\n"
1421 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1422 " EmitVertex();\n"
1423 "}\n"
1424 "void main() {\n"
1425 " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1426 " _invoke();\n"
1427 " EndPrimitive();\n"
1428 " }\n"
1429 "}\n",
1430 SkSL::Program::kGeometry_Kind);
1431}
1432
Ethan Nicholas7ef4b742016-11-11 15:16:46 -05001433#endif