blob: 29b3129230b23cc2837c2d9470f177b9d055b3f6 [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 Nicholas8aa45692017-09-20 11:24:15 -040054 "void main() { sk_FragColor = float4(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 Nicholas8aa45692017-09-20 11:24:15 -040066 "if (sqrt(2) > 5) { sk_FragColor = float4(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 Nicholas8aa45692017-09-20 11:24:15 -0400101 "float foo(float v[2]) { return v[0] * v[1]; }"
102 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
103 "void main() { float x = 10; bar(x); sk_FragColor = float4(x); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500104 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700105 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500106 "out vec4 sk_FragColor;\n"
Ethan Nicholasc6f5e102017-03-31 14:53:17 -0400107 "float foo(float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -0700108 " return v[0] * v[1];\n"
109 "}\n"
110 "void bar(inout float x) {\n"
111 " float y[2], z;\n"
112 " y[0] = x;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700113 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700114 " z = foo(y);\n"
115 " x = z;\n"
116 "}\n"
117 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700118 " float x = 10.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400119 " bar(x);\n"
120 " sk_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700121 "}\n");
122}
123
124DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700125 test(r,
126 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400127 "float x = 1, y = 2;"
ethannicholasf789b382016-08-03 12:43:36 -0700128 "int z = 3;"
Ethan Nicholas86a43402017-01-19 13:32:00 -0500129 "x = x - x + y * z * x * (y - z);"
ethannicholasf789b382016-08-03 12:43:36 -0700130 "y = x / y / z;"
131 "z = (z / 2 % 3 << 4) >> 2 << 1;"
ethannicholas08a92112016-11-09 13:26:45 -0800132 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;"
ethannicholasf789b382016-08-03 12:43:36 -0700133 "x += 12;"
134 "x -= 12;"
135 "x *= y /= z = 10;"
136 "b ||= false;"
137 "b &&= true;"
138 "b ^^= false;"
139 "z |= 0;"
140 "z &= -1;"
141 "z ^= 0;"
142 "z >>= 2;"
143 "z <<= 4;"
144 "z %= 5;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400145 "x = (float2(sqrt(1)) , 6);"
146 "z = (float2(sqrt(1)) , 6);"
ethannicholasf789b382016-08-03 12:43:36 -0700147 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500148 *SkSL::ShaderCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700149 "#version 400\n"
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 Nicholas8aa45692017-09-20 11:24:15 -0400178 "float2x4 x = float2x4(1);"
179 "float3x2 y = float3x2(1, 0, 0, 1, float2(2, 2));"
180 "float3x4 z = x * y;"
181 "float3 v1 = float3x3(1) * float3(2);"
182 "float3 v2 = float3(2) * float3x3(1);"
183 "sk_FragColor = float4(z[0].x, v1 + v2);"
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 Nicholasf7b88202017-09-18 14:10:39 -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 Nicholasf7b88202017-09-18 14:10:39 -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 Nicholas8aa45692017-09-20 11:24:15 -0400221 "float x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500222 "} test;"
223 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -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 Nicholas8aa45692017-09-20 11:24:15 -0400237 "float x;"
Ethan Nicholas50afc172017-02-16 14:49:57 -0500238 "} test[2];"
239 "void main() {"
Ethan Nicholasf7b88202017-09-18 14:10:39 -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 Nicholas8aa45692017-09-20 11:24:15 -0400261 "float x;"
262 "float 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 Nicholas8aa45692017-09-20 11:24:15 -0400287 "in float test; void main() { sk_FragColor = float4(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 Nicholas8aa45692017-09-20 11:24:15 -0400296 "in float test; void main() { sk_FragColor = float4(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 Nicholas8aa45692017-09-20 11:24:15 -0400307 "void main() { half x = 0.75; float y = 1; x++; y++;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400308 "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 Nicholas8aa45692017-09-20 11:24:15 -0400320 "void main() { half x = 0.75; float y = 1; x++; y++;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400321 "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 Nicholasf7b88202017-09-18 14:10:39 -0400324 "precision mediump float;\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500325 "out mediump vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700326 "void main() {\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400327 " mediump float x = 0.75;\n"
328 " highp float y = 1.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400329 " x++;\n"
330 " y++;\n"
331 " sk_FragColor.xy = vec2(x, y);\n"
332 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700333}
334
335DEF_TEST(SkSLMinAbs, r) {
336 test(r,
337 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400338 "float x = -5;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400339 "sk_FragColor.r = min(abs(x), 6);"
ethannicholas5961bc92016-10-12 06:39:56 -0700340 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500341 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700342 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500343 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700344 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400345 " sk_FragColor.x = min(abs(-5.0), 6.0);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700346 "}\n");
347
ethannicholas5961bc92016-10-12 06:39:56 -0700348 test(r,
349 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400350 "float x = -5.0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400351 "sk_FragColor.r = min(abs(x), 6.0);"
ethannicholas5961bc92016-10-12 06:39:56 -0700352 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500353 *SkSL::ShaderCapsFactory::CannotUseMinAndAbsTogether(),
ethannicholas5961bc92016-10-12 06:39:56 -0700354 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500355 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700356 "void main() {\n"
357 " float minAbsHackVar0;\n"
358 " float minAbsHackVar1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400359 " sk_FragColor.x = ((minAbsHackVar0 = abs(-5.0)) < (minAbsHackVar1 = 6.0) ? "
360 "minAbsHackVar0 : minAbsHackVar1);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700361 "}\n");
362}
363
Florin Malita3b30c4f2017-08-08 15:47:35 -0400364DEF_TEST(SkSLFractNegative, r) {
365 static constexpr char input[] =
366 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400367 "float x = -42.0;"
Florin Malita3b30c4f2017-08-08 15:47:35 -0400368 "sk_FragColor.r = fract(x);"
369 "}";
370 static constexpr char output_default[] =
371 "#version 400\n"
372 "out vec4 sk_FragColor;\n"
373 "void main() {\n"
374 " sk_FragColor.x = fract(-42.0);\n"
375 "}\n";
376 static constexpr char output_workaround[] =
377 "#version 400\n"
378 "out vec4 sk_FragColor;\n"
379 "void main() {\n"
380 " sk_FragColor.x = (0.5 - sign(-42.0) * (0.5 - fract(abs(-42.0))));\n"
381 "}\n";
382
383 test(r, input, *SkSL::ShaderCapsFactory::Default(), output_default);
384 test(r, input, *SkSL::ShaderCapsFactory::CannotUseFractForNegativeValues(), output_workaround);
385}
386
ethannicholasad146f62016-10-14 06:40:02 -0700387DEF_TEST(SkSLNegatedAtan, r) {
388 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400389 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500390 *SkSL::ShaderCapsFactory::Default(),
ethannicholasad146f62016-10-14 06:40:02 -0700391 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500392 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700393 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400394 " vec2 x = vec2(sqrt(2.0));\n"
395 " sk_FragColor.x = atan(x.x, -x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700396 "}\n");
ethannicholasad146f62016-10-14 06:40:02 -0700397 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400398 "void main() { float2 x = float2(sqrt(2)); sk_FragColor.r = atan(x.x, -x.y); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500399 *SkSL::ShaderCapsFactory::MustForceNegatedAtanParamToFloat(),
ethannicholasad146f62016-10-14 06:40:02 -0700400 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500401 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700402 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400403 " vec2 x = vec2(sqrt(2.0));\n"
404 " sk_FragColor.x = atan(x.x, -1.0 * x.y);\n"
ethannicholasad146f62016-10-14 06:40:02 -0700405 "}\n");
406}
407
ethannicholas5961bc92016-10-12 06:39:56 -0700408DEF_TEST(SkSLModifiersDeclaration, r) {
409 test(r,
410 "layout(blend_support_all_equations) out;"
411 "void main() { }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500412 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700413 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500414 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700415 "layout (blend_support_all_equations) out ;\n"
416 "void main() {\n"
417 "}\n");
418}
419
420DEF_TEST(SkSLHex, r) {
421 test(r,
422 "void main() {"
423 "int i1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400424 "i1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700425 "int i2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400426 "i2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700427 "int i3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400428 "i3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700429 "int i4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400430 "i4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700431 "int i5 = -0xbeef;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400432 "i5++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700433 "uint u1 = 0x0;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400434 "u1++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700435 "uint u2 = 0x1234abcd;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400436 "u2++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700437 "uint u3 = 0x7fffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400438 "u3++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700439 "uint u4 = 0xffffffff;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400440 "u4++;"
ethannicholas5961bc92016-10-12 06:39:56 -0700441 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500442 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700443 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500444 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700445 "void main() {\n"
446 " int i1 = 0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400447 " i1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700448 " int i2 = 305441741;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400449 " i2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700450 " int i3 = 2147483647;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400451 " i3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700452 " int i4 = -1;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400453 " i4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700454 " int i5 = -48879;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400455 " i5++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700456 " uint u1 = 0u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400457 " u1++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700458 " uint u2 = 305441741u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400459 " u2++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700460 " uint u3 = 2147483647u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400461 " u3++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700462 " uint u4 = 4294967295u;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400463 " u4++;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700464 "}\n");
465}
466
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400467DEF_TEST(SkSLVectorConstructors, r) {
468 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400469 "float2 v1 = float2(1);"
470 "float2 v2 = float2(1, 2);"
471 "float2 v3 = float2(float2(1));"
472 "float3 v4 = float3(float2(1), 1.0);"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400473 "int2 v5 = int2(1);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400474 "int2 v6 = int2(float2(1, 2));"
475 "float2 v7 = float2(int2(1, 2));",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500476 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400477 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500478 "out vec4 sk_FragColor;\n"
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400479 "vec2 v1 = vec2(1.0);\n"
480 "vec2 v2 = vec2(1.0, 2.0);\n"
481 "vec2 v3 = vec2(1.0);\n"
Ethan Nicholas84645e32017-02-09 13:57:14 -0500482 "vec3 v4 = vec3(vec2(1.0), 1.0);\n"
483 "ivec2 v5 = ivec2(1);\n"
484 "ivec2 v6 = ivec2(vec2(1.0, 2.0));\n"
485 "vec2 v7 = vec2(ivec2(1, 2));\n");
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400486}
487
ethannicholas5961bc92016-10-12 06:39:56 -0700488DEF_TEST(SkSLArrayConstructors, r) {
489 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400490 "float test1[] = float[](1, 2, 3, 4);"
491 "float2 test2[] = float2[](float2(1, 2), float2(3, 4));"
492 "float4x4 test3[] = float4x4[]();",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500493 *SkSL::ShaderCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700494 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500495 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700496 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
497 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
498 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700499}
ethannicholasddb37d62016-10-20 09:54:00 -0700500
501DEF_TEST(SkSLDerivatives, r) {
502 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400503 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500504 *SkSL::ShaderCapsFactory::Default(),
ethannicholasddb37d62016-10-20 09:54:00 -0700505 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500506 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700507 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400508 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700509 "}\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700510 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400511 "void main() { sk_FragColor.r = 1; }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500512 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700513 "#version 400\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400514 "precision mediump float;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400515 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700516 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400517 " sk_FragColor.x = 1.0;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700518 "}\n");
519 test(r,
Ethan Nicholascb670962017-04-20 19:31:52 -0400520 "void main() { sk_FragColor.r = dFdx(1); }",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500521 *SkSL::ShaderCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700522 "#version 400\n"
523 "#extension GL_OES_standard_derivatives : require\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400524 "precision mediump float;\n"
Ethan Nicholas762466e2017-06-29 10:03:38 -0400525 "out mediump vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700526 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400527 " sk_FragColor.x = dFdx(1.0);\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700528 "}\n");
529}
ethannicholas08a92112016-11-09 13:26:45 -0800530
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400531
532DEF_TEST(SkSLIntFolding, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800533 test(r,
534 "void main() {"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400535 "sk_FragColor.r = 32 + 2;"
536 "sk_FragColor.r = 32 - 2;"
537 "sk_FragColor.r = 32 * 2;"
538 "sk_FragColor.r = 32 / 2;"
539 "sk_FragColor.r = 12 | 6;"
540 "sk_FragColor.r = 254 & 7;"
541 "sk_FragColor.r = 2 ^ 7;"
542 "sk_FragColor.r = 1 << 4;"
543 "sk_FragColor.r = 128 >> 2;"
544 "sk_FragColor.r = -1 == -1 ? 1 : -1;"
545 "sk_FragColor.r = -1 == -2 ? 2 : -2;"
546 "sk_FragColor.r = 0 != 1 ? 3 : -3;"
547 "sk_FragColor.r = 0 != 0 ? 4 : -4;"
548 "sk_FragColor.r = 6 > 5 ? 5 : -5;"
549 "sk_FragColor.r = 6 > 6 ? 6 : -6;"
550 "sk_FragColor.r = -1 < 0 ? 7 : -7;"
551 "sk_FragColor.r = 1 < 0 ? 8 : -8;"
552 "sk_FragColor.r = 6 >= 6 ? 9 : -9;"
553 "sk_FragColor.r = 6 >= 7 ? 10 : -10;"
554 "sk_FragColor.r = 6 <= 6 ? 11 : -11;"
555 "sk_FragColor.r = 6 <= 5 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400556 "sk_FragColor.r = int(sqrt(1)) + 0;"
557 "sk_FragColor.r = 0 + int(sqrt(2));"
558 "sk_FragColor.r = int(sqrt(3)) - 0;"
559 "sk_FragColor.r = int(sqrt(4)) * 0;"
560 "sk_FragColor.r = int(sqrt(5)) * 1;"
561 "sk_FragColor.r = 1 * int(sqrt(6));"
562 "sk_FragColor.r = 0 * int(sqrt(7));"
563 "sk_FragColor.r = int(sqrt(8)) / 1;"
564 "sk_FragColor.r = 0 / int(sqrt(9));"
565 "int x = int(sqrt(2));"
566 "x += 1;"
567 "x += 0;"
568 "x -= 1;"
569 "x -= 0;"
570 "x *= 1;"
571 "x *= 2;"
572 "x /= 1;"
573 "x /= 2;"
574 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800575 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500576 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800577 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500578 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800579 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400580 " sk_FragColor.x = 34.0;\n"
581 " sk_FragColor.x = 30.0;\n"
582 " sk_FragColor.x = 64.0;\n"
583 " sk_FragColor.x = 16.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400584 " sk_FragColor.x = 14.0;\n"
585 " sk_FragColor.x = 6.0;\n"
586 " sk_FragColor.x = 5.0;\n"
587 " sk_FragColor.x = 16.0;\n"
588 " sk_FragColor.x = 32.0;\n"
589 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400590 " sk_FragColor.x = -2.0;\n"
591 " sk_FragColor.x = 3.0;\n"
592 " sk_FragColor.x = -4.0;\n"
593 " sk_FragColor.x = 5.0;\n"
594 " sk_FragColor.x = -6.0;\n"
595 " sk_FragColor.x = 7.0;\n"
596 " sk_FragColor.x = -8.0;\n"
597 " sk_FragColor.x = 9.0;\n"
598 " sk_FragColor.x = -10.0;\n"
599 " sk_FragColor.x = 11.0;\n"
600 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400601 " sk_FragColor.x = float(int(sqrt(1.0)));\n"
602 " sk_FragColor.x = float(int(sqrt(2.0)));\n"
603 " sk_FragColor.x = float(int(sqrt(3.0)));\n"
604 " sk_FragColor.x = 0.0;\n"
605 " sk_FragColor.x = float(int(sqrt(5.0)));\n"
606 " sk_FragColor.x = float(int(sqrt(6.0)));\n"
607 " sk_FragColor.x = 0.0;\n"
608 " sk_FragColor.x = float(int(sqrt(8.0)));\n"
609 " sk_FragColor.x = 0.0;\n"
610 " int x = int(sqrt(2.0));\n"
611 " x += 1;\n"
612 " x -= 1;\n"
613 " x *= 2;\n"
614 " x /= 2;\n"
615 " sk_FragColor.x = float(x);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400616 "}\n");
617}
618
619DEF_TEST(SkSLFloatFolding, r) {
620 test(r,
621 "void main() {"
622 "sk_FragColor.r = 32.0 + 2.0;"
623 "sk_FragColor.r = 32.0 - 2.0;"
624 "sk_FragColor.r = 32.0 * 2.0;"
625 "sk_FragColor.r = 32.0 / 2.0;"
626 "sk_FragColor.r = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
627 "sk_FragColor.r = 0.0 == 0.0 ? 1 : -1;"
628 "sk_FragColor.r = 0.0 == 1.0 ? 2 : -2;"
629 "sk_FragColor.r = 0.0 != 1.0 ? 3 : -3;"
630 "sk_FragColor.r = 0.0 != 0.0 ? 4 : -4;"
631 "sk_FragColor.r = 6.0 > 5.0 ? 5 : -5;"
632 "sk_FragColor.r = 6.0 > 6.0 ? 6 : -6;"
633 "sk_FragColor.r = 6.0 >= 6.0 ? 7 : -7;"
634 "sk_FragColor.r = 6.0 >= 7.0 ? 8 : -8;"
635 "sk_FragColor.r = 5.0 < 6.0 ? 9 : -9;"
636 "sk_FragColor.r = 6.0 < 6.0 ? 10 : -10;"
637 "sk_FragColor.r = 6.0 <= 6.0 ? 11 : -11;"
638 "sk_FragColor.r = 6.0 <= 5.0 ? 12 : -12;"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400639 "sk_FragColor.r = sqrt(1) + 0;"
640 "sk_FragColor.r = 0 + sqrt(2);"
641 "sk_FragColor.r = sqrt(3) - 0;"
642 "sk_FragColor.r = sqrt(4) * 0;"
643 "sk_FragColor.r = sqrt(5) * 1;"
644 "sk_FragColor.r = 1 * sqrt(6);"
645 "sk_FragColor.r = 0 * sqrt(7);"
646 "sk_FragColor.r = sqrt(8) / 1;"
647 "sk_FragColor.r = 0 / sqrt(9);"
648 "sk_FragColor.r += 1;"
649 "sk_FragColor.r += 0;"
650 "sk_FragColor.r -= 1;"
651 "sk_FragColor.r -= 0;"
652 "sk_FragColor.r *= 1;"
653 "sk_FragColor.r *= 2;"
654 "sk_FragColor.r /= 1;"
655 "sk_FragColor.r /= 2;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400656 "}",
657 *SkSL::ShaderCapsFactory::Default(),
658 "#version 400\n"
659 "out vec4 sk_FragColor;\n"
660 "void main() {\n"
661 " sk_FragColor.x = 34.0;\n"
662 " sk_FragColor.x = 30.0;\n"
663 " sk_FragColor.x = 64.0;\n"
664 " sk_FragColor.x = 16.0;\n"
665 " sk_FragColor.x = 19.0;\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400666 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400667 " sk_FragColor.x = -2.0;\n"
668 " sk_FragColor.x = 3.0;\n"
669 " sk_FragColor.x = -4.0;\n"
670 " sk_FragColor.x = 5.0;\n"
671 " sk_FragColor.x = -6.0;\n"
672 " sk_FragColor.x = 7.0;\n"
673 " sk_FragColor.x = -8.0;\n"
674 " sk_FragColor.x = 9.0;\n"
675 " sk_FragColor.x = -10.0;\n"
676 " sk_FragColor.x = 11.0;\n"
677 " sk_FragColor.x = -12.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400678 " sk_FragColor.x = sqrt(1.0);\n"
679 " sk_FragColor.x = sqrt(2.0);\n"
680 " sk_FragColor.x = sqrt(3.0);\n"
681 " sk_FragColor.x = 0.0;\n"
682 " sk_FragColor.x = sqrt(5.0);\n"
683 " sk_FragColor.x = sqrt(6.0);\n"
684 " sk_FragColor.x = 0.0;\n"
685 " sk_FragColor.x = sqrt(8.0);\n"
686 " sk_FragColor.x = 0.0;\n"
687 " sk_FragColor.x += 1.0;\n"
688 " sk_FragColor.x -= 1.0;\n"
689 " sk_FragColor.x *= 2.0;\n"
690 " sk_FragColor.x /= 2.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400691 "}\n");
692}
693
694DEF_TEST(SkSLBoolFolding, r) {
695 test(r,
696 "void main() {"
697 "sk_FragColor.r = 1 == 1 || 2 == 8 ? 1 : -1;"
698 "sk_FragColor.r = 1 > 1 || 2 == 8 ? 2 : -2;"
699 "sk_FragColor.r = 1 == 1 && 2 <= 8 ? 3 : -3;"
700 "sk_FragColor.r = 1 == 2 && 2 == 8 ? 4 : -4;"
701 "sk_FragColor.r = 1 == 1 ^^ 1 != 1 ? 5 : -5;"
702 "sk_FragColor.r = 1 == 1 ^^ 1 == 1 ? 6 : -6;"
703 "}",
704 *SkSL::ShaderCapsFactory::Default(),
705 "#version 400\n"
706 "out vec4 sk_FragColor;\n"
707 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400708 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400709 " sk_FragColor.x = -2.0;\n"
710 " sk_FragColor.x = 3.0;\n"
711 " sk_FragColor.x = -4.0;\n"
712 " sk_FragColor.x = 5.0;\n"
713 " sk_FragColor.x = -6.0;\n"
714 "}\n");
715}
716
717DEF_TEST(SkSLVecFolding, r) {
718 test(r,
719 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400720 "sk_FragColor.r = float4(0.5, 1, 1, 1).x;"
721 "sk_FragColor = float4(float2(1), float2(2, 3)) + float4(5, 6, 7, 8);"
722 "sk_FragColor = float4(8, float3(10)) - float4(1);"
723 "sk_FragColor = float4(2) * float4(1, 2, 3, 4);"
724 "sk_FragColor = float4(12) / float4(1, 2, 3, 4);"
725 "sk_FragColor.r = (float4(12) / float4(1, 2, 3, 4)).y;"
726 "sk_FragColor.x = float4(1) == float4(1) ? 1.0 : -1.0;"
727 "sk_FragColor.x = float4(1) == float4(2) ? 2.0 : -2.0;"
728 "sk_FragColor.x = float2(1) == float2(1, 1) ? 3.0 : -3.0;"
729 "sk_FragColor.x = float2(1, 1) == float2(1, 1) ? 4.0 : -4.0;"
730 "sk_FragColor.x = float2(1) == float2(1, 0) ? 5.0 : -5.0;"
731 "sk_FragColor.x = float4(1) == float4(float2(1), float2(1)) ? 6.0 : -6.0;"
732 "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), float2(1)) ? 7.0 : -7.0;"
733 "sk_FragColor.x = float4(float3(1), 1) == float4(float2(1), 1, 0) ? 8.0 : -8.0;"
734 "sk_FragColor.x = float2(1) != float2(1, 0) ? 9.0 : -9.0;"
735 "sk_FragColor.x = float4(1) != float4(float2(1), float2(1)) ? 10.0 : -10.0;"
736 "sk_FragColor = float4(sqrt(1)) * float4(1);"
737 "sk_FragColor = float4(1) * float4(sqrt(2));"
738 "sk_FragColor = float4(0) * float4(sqrt(3));"
739 "sk_FragColor = float4(sqrt(4)) * float4(0);"
740 "sk_FragColor = float4(0) / float4(sqrt(5));"
741 "sk_FragColor = float4(0) + float4(sqrt(6));"
742 "sk_FragColor = float4(sqrt(7)) + float4(0);"
743 "sk_FragColor = float4(sqrt(8)) - float4(0);"
744 "sk_FragColor = float4(0) + sqrt(9);"
745 "sk_FragColor = float4(0) * sqrt(10);"
746 "sk_FragColor = float4(0) / sqrt(11);"
747 "sk_FragColor = float4(1) * sqrt(12);"
748 "sk_FragColor = 0 + float4(sqrt(13));"
749 "sk_FragColor = 0 * float4(sqrt(14));"
750 "sk_FragColor = 0 / float4(sqrt(15));"
751 "sk_FragColor = 1 * float4(sqrt(16));"
752 "sk_FragColor = float4(sqrt(17)) + 0;"
753 "sk_FragColor = float4(sqrt(18)) * 0;"
754 "sk_FragColor = float4(sqrt(19)) * 1;"
755 "sk_FragColor = float4(sqrt(19.5)) - 0;"
756 "sk_FragColor = sqrt(20) * float4(1);"
757 "sk_FragColor = sqrt(21) + float4(0);"
758 "sk_FragColor = sqrt(22) - float4(0);"
759 "sk_FragColor = sqrt(23) / float4(1);"
760 "sk_FragColor = float4(sqrt(24)) / 1;"
761 "sk_FragColor += float4(1);"
762 "sk_FragColor += float4(0);"
763 "sk_FragColor -= float4(1);"
764 "sk_FragColor -= float4(0);"
765 "sk_FragColor *= float4(1);"
766 "sk_FragColor *= float4(2);"
767 "sk_FragColor /= float4(1);"
768 "sk_FragColor /= float4(2);"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400769 "}",
770 *SkSL::ShaderCapsFactory::Default(),
771 "#version 400\n"
772 "out vec4 sk_FragColor;\n"
773 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400774 " sk_FragColor.x = 0.5;\n"
775 " sk_FragColor = vec4(6.0, 7.0, 9.0, 11.0);\n"
776 " sk_FragColor = vec4(7.0, 9.0, 9.0, 9.0);\n"
777 " sk_FragColor = vec4(2.0, 4.0, 6.0, 8.0);\n"
778 " sk_FragColor = vec4(12.0, 6.0, 4.0, 3.0);\n"
779 " sk_FragColor.x = 6.0;\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400780 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400781 " sk_FragColor.x = -2.0;\n"
782 " sk_FragColor.x = 3.0;\n"
783 " sk_FragColor.x = 4.0;\n"
784 " sk_FragColor.x = -5.0;\n"
785 " sk_FragColor.x = 6.0;\n"
786 " sk_FragColor.x = 7.0;\n"
787 " sk_FragColor.x = -8.0;\n"
788 " sk_FragColor.x = 9.0;\n"
789 " sk_FragColor.x = -10.0;\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -0400790 " sk_FragColor = vec4(sqrt(1.0));\n"
791 " sk_FragColor = vec4(sqrt(2.0));\n"
792 " sk_FragColor = vec4(0.0);\n"
793 " sk_FragColor = vec4(0.0);\n"
794 " sk_FragColor = vec4(0.0);\n"
795 " sk_FragColor = vec4(sqrt(6.0));\n"
796 " sk_FragColor = vec4(sqrt(7.0));\n"
797 " sk_FragColor = vec4(sqrt(8.0));\n"
798 " sk_FragColor = vec4(sqrt(9.0));\n"
799 " sk_FragColor = vec4(0.0);\n"
800 " sk_FragColor = vec4(0.0);\n"
801 " sk_FragColor = vec4(sqrt(12.0));\n"
802 " sk_FragColor = vec4(sqrt(13.0));\n"
803 " sk_FragColor = vec4(0.0);\n"
804 " sk_FragColor = vec4(0.0);\n"
805 " sk_FragColor = vec4(sqrt(16.0));\n"
806 " sk_FragColor = vec4(sqrt(17.0));\n"
807 " sk_FragColor = vec4(0.0);\n"
808 " sk_FragColor = vec4(sqrt(19.0));\n"
809 " sk_FragColor = vec4(sqrt(19.5));\n"
810 " sk_FragColor = vec4(sqrt(20.0));\n"
811 " sk_FragColor = vec4(sqrt(21.0));\n"
812 " sk_FragColor = vec4(sqrt(22.0));\n"
813 " sk_FragColor = vec4(sqrt(23.0));\n"
814 " sk_FragColor = vec4(sqrt(24.0));\n"
815 " sk_FragColor += vec4(1.0);\n"
816 " sk_FragColor -= vec4(1.0);\n"
817 " sk_FragColor *= vec4(2.0);\n"
818 " sk_FragColor /= vec4(2.0);\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400819 "}\n");
820}
821
822DEF_TEST(SkSLMatFolding, r) {
823 test(r,
824 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400825 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) == "
826 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 1 : -1;"
827 "sk_FragColor.x = float2x2(float2(1.0, 0.0), float2(1.0, 1.0)) == "
828 "float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 2 : -2;"
829 "sk_FragColor.x = float2x2(1) == float2x2(1) ? 3 : -3;"
830 "sk_FragColor.x = float2x2(1) == float2x2(0) ? 4 : -4;"
831 "sk_FragColor.x = float2x2(1) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 5 : -5;"
832 "sk_FragColor.x = float2x2(2) == float2x2(float2(1.0, 0.0), float2(0.0, 1.0)) ? 6 : -6;"
833 "sk_FragColor.x = float3x2(2) == float3x2(float2(2.0, 0.0), float2(0.0, 2.0), float2(0.0))"
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400834 "? 7 : -7;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400835 "sk_FragColor.x = float2x2(1) != float2x2(1) ? 8 : -8;"
836 "sk_FragColor.x = float2x2(1) != float2x2(0) ? 9 : -9;"
837 "sk_FragColor.x = float3x3(float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), "
838 "float3(0.0, 0.0, 0.0)) == float3x3(float2x2(1.0)) ? 10 : -10;"
839 "sk_FragColor.x = float2x2(float3x3(1.0)) == float2x2(1.0) ? 11 : -11;"
840 "sk_FragColor.x = float2x2(float4(1.0, 0.0, 0.0, 1.0)) == float2x2(1.0) ? 12 : -12;"
841 "sk_FragColor.x = float2x2(1.0, 0.0, float2(0.0, 1.0)) == float2x2(1.0) ? 13 : -13;"
842 "sk_FragColor.x = float2x2(float2(1.0, 0.0), 0.0, 1.0) == float2x2(1.0) ? 14 : -14;"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400843 "}",
844 *SkSL::ShaderCapsFactory::Default(),
845 "#version 400\n"
846 "out vec4 sk_FragColor;\n"
847 "void main() {\n"
Ethan Nicholas3deaeb22017-04-25 14:42:11 -0400848 " sk_FragColor.x = 1.0;\n"
Ethan Nicholasc81d22f2017-04-26 11:25:18 -0400849 " sk_FragColor.x = -2.0;\n"
850 " sk_FragColor.x = 3.0;\n"
851 " sk_FragColor.x = -4.0;\n"
852 " sk_FragColor.x = 5.0;\n"
853 " sk_FragColor.x = -6.0;\n"
854 " sk_FragColor.x = 7.0;\n"
855 " sk_FragColor.x = -8.0;\n"
856 " sk_FragColor.x = 9.0;\n"
857 " sk_FragColor.x = 10.0;\n"
858 " sk_FragColor.x = 11.0;\n"
859 " sk_FragColor.x = 12.0;\n"
860 " sk_FragColor.x = 13.0;\n"
861 " sk_FragColor.x = 14.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800862 "}\n");
863}
864
Ethan Nicholas5ac13c22017-05-10 15:06:17 -0400865DEF_TEST(SkSLConstantIf, r) {
ethannicholas08a92112016-11-09 13:26:45 -0800866 test(r,
867 "void main() {"
868 "int x;"
869 "if (true) x = 1;"
870 "if (2 > 1) x = 2; else x = 3;"
871 "if (1 > 2) x = 4; else x = 5;"
872 "if (false) x = 6;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400873 "sk_FragColor.r = x;"
ethannicholas08a92112016-11-09 13:26:45 -0800874 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500875 *SkSL::ShaderCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800876 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500877 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800878 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400879 " sk_FragColor.x = 5.0;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800880 "}\n");
881}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500882
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500883DEF_TEST(SkSLCaps, r) {
884 test(r,
885 "void main() {"
Ethan Nicholascb670962017-04-20 19:31:52 -0400886 "int x = 0;"
887 "int y = 0;"
888 "int z = 0;"
889 "int w = 0;"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500890 "if (sk_Caps.externalTextureSupport) x = 1;"
Ethan Nicholascb670962017-04-20 19:31:52 -0400891 "if (sk_Caps.fbFetchSupport) y = 1;"
892 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) z = 1;"
893 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) w = 1;"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400894 "sk_FragColor = half4(x, y, z, w);"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500895 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500896 *SkSL::ShaderCapsFactory::VariousCaps(),
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500897 "#version 400\n"
898 "out vec4 sk_FragColor;\n"
899 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400900 " sk_FragColor = vec4(1.0, 0.0, 1.0, 0.0);\n"
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500901 "}\n");
902}
903
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500904DEF_TEST(SkSLTexture, r) {
905 test(r,
906 "uniform sampler1D one;"
907 "uniform sampler2D two;"
908 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400909 "float4 a = texture(one, 0);"
910 "float4 b = texture(two, float2(0));"
911 "float4 c = texture(one, float2(0));"
912 "float4 d = texture(two, float3(0));"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400913 "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500914 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500915 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500916 "#version 400\n"
917 "out vec4 sk_FragColor;\n"
918 "uniform sampler1D one;\n"
919 "uniform sampler2D two;\n"
920 "void main() {\n"
921 " vec4 a = texture(one, 0.0);\n"
922 " vec4 b = texture(two, vec2(0.0));\n"
923 " vec4 c = textureProj(one, vec2(0.0));\n"
924 " vec4 d = textureProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400925 " sk_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500926 "}\n");
927 test(r,
928 "uniform sampler1D one;"
929 "uniform sampler2D two;"
930 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400931 "float4 a = texture(one, 0);"
932 "float4 b = texture(two, float2(0));"
933 "float4 c = texture(one, float2(0));"
934 "float4 d = texture(two, float3(0));"
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400935 "sk_FragColor = half4(a.x, b.x, c.x, d.x);"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500936 "}",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500937 *SkSL::ShaderCapsFactory::Version110(),
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500938 "#version 110\n"
939 "uniform sampler1D one;\n"
940 "uniform sampler2D two;\n"
941 "void main() {\n"
942 " vec4 a = texture1D(one, 0.0);\n"
943 " vec4 b = texture2D(two, vec2(0.0));\n"
944 " vec4 c = texture1DProj(one, vec2(0.0));\n"
945 " vec4 d = texture2DProj(two, vec3(0.0));\n"
Ethan Nicholascb670962017-04-20 19:31:52 -0400946 " gl_FragColor = vec4(a.x, b.x, c.x, d.x);\n"
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500947 "}\n");
948}
949
Ethan Nicholas19671772016-11-28 16:30:17 -0500950DEF_TEST(SkSLOffset, r) {
951 test(r,
952 "struct Test {"
953 "layout(offset = 0) int x;"
954 "layout(offset = 4) int y;"
955 "int z;"
956 "} test;",
Brian Salomonf1dd6772016-11-29 15:27:52 -0500957 *SkSL::ShaderCapsFactory::Default(),
Ethan Nicholas19671772016-11-28 16:30:17 -0500958 "#version 400\n"
959 "out vec4 sk_FragColor;\n"
960 "struct Test {\n"
961 " layout (offset = 0) int x;\n"
962 " layout (offset = 4) int y;\n"
963 " int z;\n"
964 "} test;\n");
965}
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500966
967DEF_TEST(SkSLFragCoord, r) {
968 SkSL::Program::Settings settings;
969 settings.fFlipY = true;
970 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::FragCoordsOld();
971 settings.fCaps = caps.get();
972 SkSL::Program::Inputs inputs;
973 test(r,
974 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
975 settings,
976 "#version 110\n"
977 "#extension GL_ARB_fragment_coord_conventions : require\n"
978 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
979 "void main() {\n"
980 " gl_FragColor.xy = gl_FragCoord.xy;\n"
981 "}\n",
982 &inputs);
983 REPORTER_ASSERT(r, !inputs.fRTHeight);
984
985 caps = SkSL::ShaderCapsFactory::FragCoordsNew();
986 settings.fCaps = caps.get();
987 test(r,
988 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
989 settings,
990 "#version 400\n"
991 "layout(origin_upper_left) in vec4 gl_FragCoord;\n"
992 "out vec4 sk_FragColor;\n"
993 "void main() {\n"
994 " sk_FragColor.xy = gl_FragCoord.xy;\n"
995 "}\n",
996 &inputs);
997 REPORTER_ASSERT(r, !inputs.fRTHeight);
998
999 caps = SkSL::ShaderCapsFactory::Default();
1000 settings.fCaps = caps.get();
1001 test(r,
1002 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1003 settings,
1004 "#version 400\n"
1005 "uniform float u_skRTHeight;\n"
1006 "out vec4 sk_FragColor;\n"
1007 "void main() {\n"
1008 " vec2 _sktmpCoord = gl_FragCoord.xy;\n"
1009 " vec4 sk_FragCoord = vec4(_sktmpCoord.x, u_skRTHeight - _sktmpCoord.y, 1.0, 1.0);\n"
1010 " sk_FragColor.xy = sk_FragCoord.xy;\n"
1011 "}\n",
1012 &inputs);
1013 REPORTER_ASSERT(r, inputs.fRTHeight);
1014
1015 settings.fFlipY = false;
1016 test(r,
1017 "void main() { sk_FragColor.xy = sk_FragCoord.xy; }",
1018 settings,
1019 "#version 400\n"
1020 "out vec4 sk_FragColor;\n"
1021 "void main() {\n"
1022 " sk_FragColor.xy = gl_FragCoord.xy;\n"
1023 "}\n",
1024 &inputs);
1025 REPORTER_ASSERT(r, !inputs.fRTHeight);
1026}
1027
Ethan Nicholasa51740c2017-02-07 14:53:32 -05001028DEF_TEST(SkSLVertexID, r) {
1029 test(r,
1030 "out int id; void main() { id = sk_VertexID; }",
1031 *SkSL::ShaderCapsFactory::Default(),
1032 "#version 400\n"
1033 "out int id;\n"
1034 "void main() {\n"
1035 " id = gl_VertexID;\n"
1036 "}\n",
1037 SkSL::Program::kVertex_Kind);
1038}
1039
Chris Dalton8580d512017-10-14 22:12:33 -06001040DEF_TEST(SkSLInstanceID, r) {
1041 test(r,
1042 "out int id; void main() { id = sk_InstanceID; }",
1043 *SkSL::ShaderCapsFactory::Default(),
1044 "#version 400\n"
1045 "out int id;\n"
1046 "void main() {\n"
1047 " id = gl_InstanceID;\n"
1048 "}\n",
1049 SkSL::Program::kVertex_Kind);
1050}
1051
Ethan Nicholas67d64602017-02-09 10:15:25 -05001052DEF_TEST(SkSLClipDistance, r) {
1053 test(r,
1054 "void main() { sk_ClipDistance[0] = 0; }",
1055 *SkSL::ShaderCapsFactory::Default(),
1056 "#version 400\n"
1057 "void main() {\n"
1058 " gl_ClipDistance[0] = 0.0;\n"
1059 "}\n",
1060 SkSL::Program::kVertex_Kind);
1061 test(r,
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001062 "void main() { sk_FragColor = half4(sk_ClipDistance[0]); }",
Ethan Nicholas67d64602017-02-09 10:15:25 -05001063 *SkSL::ShaderCapsFactory::Default(),
1064 "#version 400\n"
1065 "out vec4 sk_FragColor;\n"
1066 "void main() {\n"
1067 " sk_FragColor = vec4(gl_ClipDistance[0]);\n"
1068 "}\n");
1069}
Ethan Nicholas50afc172017-02-16 14:49:57 -05001070
1071DEF_TEST(SkSLArrayTypes, r) {
1072 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001073 "void main() { float2 x[2] = float2[2](float2(1), float2(2));"
1074 "float2[2] y = float2[2](float2(3), float2(4));"
1075 "sk_FragColor = float4(x[0], y[1]); }",
Ethan Nicholas50afc172017-02-16 14:49:57 -05001076 *SkSL::ShaderCapsFactory::Default(),
1077 "#version 400\n"
1078 "out vec4 sk_FragColor;\n"
1079 "void main() {\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001080 " sk_FragColor = vec4(vec2[2](vec2(1.0), vec2(2.0))[0], "
1081 "vec2[2](vec2(3.0), vec2(4.0))[1]);\n"
Ethan Nicholas50afc172017-02-16 14:49:57 -05001082 "}\n");
1083}
1084
Ethan Nicholas52cad152017-02-16 16:37:32 -05001085DEF_TEST(SkSLGeometry, r) {
1086 test(r,
1087 "layout(points) in;"
1088 "layout(invocations = 2) in;"
1089 "layout(line_strip, max_vertices = 2) out;"
1090 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001091 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001092 "EmitVertex();"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001093 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas52cad152017-02-16 16:37:32 -05001094 "EmitVertex();"
1095 "EndPrimitive();"
1096 "}",
1097 *SkSL::ShaderCapsFactory::Default(),
1098 "#version 400\n"
1099 "layout (points) in ;\n"
1100 "layout (invocations = 2) in ;\n"
1101 "layout (line_strip, max_vertices = 2) out ;\n"
1102 "void main() {\n"
1103 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1104 " EmitVertex();\n"
1105 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1106 " EmitVertex();\n"
1107 " EndPrimitive();\n"
1108 "}\n",
1109 SkSL::Program::kGeometry_Kind);
1110}
1111
Ethan Nicholasaf197692017-02-27 13:26:45 -05001112DEF_TEST(SkSLSwitch, r) {
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001113 // basic "does a switch even work" test
Ethan Nicholasaf197692017-02-27 13:26:45 -05001114 test(r,
1115 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001116 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001117 " switch (int(sqrt(1))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001118 " case 0:"
1119 " x = 0.0;"
1120 " break;"
1121 " case 1:"
1122 " x = 1.0;"
1123 " break;"
1124 " default:"
1125 " x = 2.0;"
1126 " }"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001127 " sk_FragColor = float4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001128 "}",
1129 *SkSL::ShaderCapsFactory::Default(),
1130 "#version 400\n"
1131 "out vec4 sk_FragColor;\n"
1132 "void main() {\n"
1133 " float x;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001134 " switch (int(sqrt(1.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001135 " case 0:\n"
1136 " x = 0.0;\n"
1137 " break;\n"
1138 " case 1:\n"
1139 " x = 1.0;\n"
1140 " break;\n"
1141 " default:\n"
1142 " x = 2.0;\n"
1143 " }\n"
1144 " sk_FragColor = vec4(x);\n"
1145 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001146 // dead code inside of switch
Ethan Nicholasaf197692017-02-27 13:26:45 -05001147 test(r,
1148 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001149 " float x;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001150 " switch (int(sqrt(2))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001151 " case 0:"
1152 " x = 0.0;"
1153 " case 1:"
1154 " x = 1.0;"
1155 " default:"
1156 " x = 2.0;"
1157 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001158 " sk_FragColor = half4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001159 "}",
1160 *SkSL::ShaderCapsFactory::Default(),
1161 "#version 400\n"
1162 "out vec4 sk_FragColor;\n"
1163 "void main() {\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001164 " switch (int(sqrt(2.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001165 " case 0:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001166 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001167 " case 1:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001168 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001169 " default:\n"
Ethan Nicholascb670962017-04-20 19:31:52 -04001170 " ;\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001171 " }\n"
1172 " sk_FragColor = vec4(2.0);\n"
1173 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001174 // non-static test w/ fallthrough
Ethan Nicholasaf197692017-02-27 13:26:45 -05001175 test(r,
1176 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001177 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001178 " switch (int(sqrt(3))) {"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001179 " case 0:"
1180 " x = 0.0;"
1181 " case 1:"
1182 " x = 1.0;"
1183 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001184 " sk_FragColor = half4(x);"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001185 "}",
1186 *SkSL::ShaderCapsFactory::Default(),
1187 "#version 400\n"
1188 "out vec4 sk_FragColor;\n"
1189 "void main() {\n"
1190 " float x = 0.0;\n"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001191 " switch (int(sqrt(3.0))) {\n"
Ethan Nicholasaf197692017-02-27 13:26:45 -05001192 " case 0:\n"
1193 " x = 0.0;\n"
1194 " case 1:\n"
1195 " x = 1.0;\n"
1196 " }\n"
1197 " sk_FragColor = vec4(x);\n"
1198 "}\n");
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001199 // static test w/ fallthrough
1200 test(r,
1201 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001202 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001203 " switch (0) {"
1204 " case 0:"
1205 " x = 0.0;"
1206 " case 1:"
1207 " x = 1.0;"
1208 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001209 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001210 "}",
1211 *SkSL::ShaderCapsFactory::Default(),
1212 "#version 400\n"
1213 "out vec4 sk_FragColor;\n"
1214 "void main() {\n"
1215 " sk_FragColor = vec4(1.0);\n"
1216 "}\n");
1217 // static test w/ fallthrough, different entry point
1218 test(r,
1219 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001220 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001221 " switch (1) {"
1222 " case 0:"
1223 " x = 0.0;"
1224 " case 1:"
1225 " x = 1.0;"
1226 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001227 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001228 "}",
1229 *SkSL::ShaderCapsFactory::Default(),
1230 "#version 400\n"
1231 "out vec4 sk_FragColor;\n"
1232 "void main() {\n"
1233 " sk_FragColor = vec4(1.0);\n"
1234 "}\n");
1235 // static test w/ break
1236 test(r,
1237 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001238 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001239 " switch (0) {"
1240 " case 0:"
1241 " x = 0.0;"
1242 " break;"
1243 " case 1:"
1244 " x = 1.0;"
1245 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001246 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001247 "}",
1248 *SkSL::ShaderCapsFactory::Default(),
1249 "#version 400\n"
1250 "out vec4 sk_FragColor;\n"
1251 "void main() {\n"
1252 " sk_FragColor = vec4(0.0);\n"
1253 "}\n");
1254 // static test w/ static conditional break
1255 test(r,
1256 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001257 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001258 " switch (0) {"
1259 " case 0:"
1260 " x = 0.0;"
1261 " if (x < 1) break;"
1262 " case 1:"
1263 " x = 1.0;"
1264 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001265 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001266 "}",
1267 *SkSL::ShaderCapsFactory::Default(),
1268 "#version 400\n"
1269 "out vec4 sk_FragColor;\n"
1270 "void main() {\n"
1271 " sk_FragColor = vec4(0.0);\n"
1272 "}\n");
1273 // static test w/ non-static conditional break
1274 test(r,
1275 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001276 " float x = 0.0;"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001277 " switch (0) {"
1278 " case 0:"
1279 " x = 0.0;"
1280 " if (x < sqrt(1)) break;"
1281 " case 1:"
1282 " x = 1.0;"
1283 " }"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001284 " sk_FragColor = half4(x);"
Ethan Nicholas5ac13c22017-05-10 15:06:17 -04001285 "}",
1286 *SkSL::ShaderCapsFactory::Default(),
1287 "#version 400\n"
1288 "out vec4 sk_FragColor;\n"
1289 "void main() {\n"
1290 " float x = 0.0;\n"
1291 " switch (0) {\n"
1292 " case 0:\n"
1293 " x = 0.0;\n"
1294 " if (0.0 < sqrt(1.0)) break;\n"
1295 " case 1:\n"
1296 " x = 1.0;\n"
1297 " }\n"
1298 " sk_FragColor = vec4(x);\n"
1299 "}\n");
Ethan Nicholasaf197692017-02-27 13:26:45 -05001300}
1301
Ethan Nicholas5338f992017-04-19 15:54:07 -04001302DEF_TEST(SkSLRectangleTexture, r) {
1303 test(r,
1304 "uniform sampler2D test;"
1305 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001306 " sk_FragColor = texture(test, float2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001307 "}",
1308 *SkSL::ShaderCapsFactory::Default(),
1309 "#version 400\n"
1310 "out vec4 sk_FragColor;\n"
1311 "uniform sampler2D test;\n"
1312 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001313 " sk_FragColor = texture(test, vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001314 "}\n");
1315 test(r,
1316 "uniform sampler2DRect test;"
1317 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001318 " sk_FragColor = texture(test, float2(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001319 "}",
1320 *SkSL::ShaderCapsFactory::Default(),
1321 "#version 400\n"
1322 "out vec4 sk_FragColor;\n"
1323 "uniform sampler2DRect test;\n"
1324 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001325 " sk_FragColor = texture(test, textureSize(test) * vec2(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001326 "}\n");
1327 test(r,
1328 "uniform sampler2DRect test;"
1329 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001330 " sk_FragColor = texture(test, float3(0.5));"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001331 "}",
1332 *SkSL::ShaderCapsFactory::Default(),
1333 "#version 400\n"
1334 "out vec4 sk_FragColor;\n"
1335 "uniform sampler2DRect test;\n"
1336 "void main() {\n"
Ethan Nicholasfe53e582017-04-27 16:24:51 -04001337 " sk_FragColor = texture(test, vec3(textureSize(test), 1.0) * vec3(0.5));\n"
Ethan Nicholas5338f992017-04-19 15:54:07 -04001338 "}\n");
1339}
1340
Ethan Nicholascb670962017-04-20 19:31:52 -04001341DEF_TEST(SkSLUnusedVars, r) {
1342 test(r,
1343 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001344 "float a = 1, b = 2, c = 3;"
1345 "float d = c;"
1346 "float e = d;"
Ethan Nicholascb670962017-04-20 19:31:52 -04001347 "b++;"
1348 "d++;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001349 "sk_FragColor = float4(b, b, d, d);"
Ethan Nicholascb670962017-04-20 19:31:52 -04001350 "}",
1351 *SkSL::ShaderCapsFactory::Default(),
1352 "#version 400\n"
1353 "out vec4 sk_FragColor;\n"
1354 "void main() {\n"
1355 " float b = 2.0;\n"
1356 " float d = 3.0;\n"
1357 " b++;\n"
1358 " d++;\n"
1359 " sk_FragColor = vec4(b, b, d, d);\n"
1360 "}\n");
1361}
1362
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001363DEF_TEST(SkSLMultipleAssignments, r) {
1364 test(r,
1365 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001366 "float x;"
1367 "float y;"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001368 "int z;"
1369 "x = y = z = 1;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001370 "sk_FragColor = float4(z);"
Ethan Nicholasc2371a42017-05-05 10:04:06 -04001371 "}",
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 Nicholas4b330df2017-05-17 10:52:55 -04001380DEF_TEST(SkSLComplexDelete, r) {
1381 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001382 "uniform float4x4 colorXform;"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001383 "uniform sampler2D sampler;"
1384 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001385 "float4 tmpColor;"
1386 "sk_FragColor = float4(1.0) * (tmpColor = texture(sampler, float2(1)) , "
1387 "colorXform != float4x4(1.0) ? float4(clamp((float4x4(colorXform) * "
1388 "float4(tmpColor.xyz, 1.0)).xyz, "
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001389 "0.0, tmpColor.w), tmpColor.w) : tmpColor);"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001390 "}",
1391 *SkSL::ShaderCapsFactory::Default(),
1392 "#version 400\n"
1393 "out vec4 sk_FragColor;\n"
1394 "uniform mat4 colorXform;\n"
1395 "uniform sampler2D sampler;\n"
1396 "void main() {\n"
1397 " vec4 tmpColor;\n"
1398 " sk_FragColor = (tmpColor = texture(sampler, vec2(1.0)) , colorXform != mat4(1.0) ? "
1399 "vec4(clamp((colorXform * vec4(tmpColor.xyz, 1.0)).xyz, 0.0, tmpColor.w), tmpColor.w) : "
1400 "tmpColor);\n"
1401 "}\n");
Ethan Nicholas4b330df2017-05-17 10:52:55 -04001402}
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001403
1404DEF_TEST(SkSLDependentInitializers, r) {
1405 test(r,
1406 "void main() {"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001407 "float x = 0.5, y = x * 2;"
1408 "sk_FragColor = float4(y);"
Ethan Nicholasb4dc4192017-06-02 10:16:28 -04001409 "}",
1410 *SkSL::ShaderCapsFactory::Default(),
1411 "#version 400\n"
1412 "out vec4 sk_FragColor;\n"
1413 "void main() {\n"
1414 " sk_FragColor = vec4(1.0);\n"
1415 "}\n");
1416}
1417
Ethan Nicholasb310fd52017-06-09 13:46:34 -04001418DEF_TEST(SkSLDeadLoopVar, r) {
1419 test(r,
1420 "void main() {"
1421 "for (int x = 0; x < 4; ) {"
1422 "break;"
1423 "}"
1424 "}",
1425 *SkSL::ShaderCapsFactory::Default(),
1426 "#version 400\n"
1427 "out vec4 sk_FragColor;\n"
1428 "void main() {\n"
1429 " for (; true; ) {\n"
1430 " break;\n"
1431 " }\n"
1432 "}\n"
1433 );
1434}
1435
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001436DEF_TEST(SkSLInvocations, r) {
1437 test(r,
1438 "layout(points) in;"
1439 "layout(invocations = 2) in;"
1440 "layout(line_strip, max_vertices = 2) out;"
1441 "void test() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001442 "sk_Position = sk_in[0].sk_Position + float4(0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001443 "EmitVertex();"
1444 "}"
1445 "void main() {"
Ethan Nicholasbed683a2017-09-26 14:23:59 -04001446 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001447 "EmitVertex();"
1448 "}",
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001449 *SkSL::ShaderCapsFactory::NoGSInvocationsSupport(),
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001450 "#version 400\n"
1451 "int sk_InvocationID;\n"
1452 "layout (points) in ;\n"
1453 "layout (line_strip, max_vertices = 4) out ;\n"
1454 "void test() {\n"
1455 " gl_Position = gl_in[0].gl_Position + vec4(0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1456 " EmitVertex();\n"
1457 "}\n"
1458 "void _invoke() {\n"
1459 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(sk_InvocationID));\n"
1460 " EmitVertex();\n"
1461 "}\n"
1462 "void main() {\n"
1463 " for (sk_InvocationID = 0;sk_InvocationID < 2; sk_InvocationID++) {\n"
1464 " _invoke();\n"
1465 " EndPrimitive();\n"
1466 " }\n"
1467 "}\n",
1468 SkSL::Program::kGeometry_Kind);
Chris Daltonf1b47bb2017-10-06 11:57:51 -06001469 test(r,
1470 "layout(points, invocations = 2) in;"
1471 "layout(invocations = 3) in;"
1472 "layout(line_strip, max_vertices = 2) out;"
1473 "void main() {"
1474 "sk_Position = sk_in[0].sk_Position + float4(-0.5, 0, 0, sk_InvocationID);"
1475 "EmitVertex();"
1476 "EndPrimitive();"
1477 "}",
1478 *SkSL::ShaderCapsFactory::GSInvocationsExtensionString(),
1479 "#version 400\n"
1480 "#extension GL_ARB_gpu_shader5 : require\n"
1481 "layout (points, invocations = 2) in ;\n"
1482 "layout (invocations = 3) in ;\n"
1483 "layout (line_strip, max_vertices = 2) out ;\n"
1484 "void main() {\n"
1485 " gl_Position = gl_in[0].gl_Position + vec4(-0.5, 0.0, 0.0, float(gl_InvocationID));\n"
1486 " EmitVertex();\n"
1487 " EndPrimitive();\n"
1488 "}\n",
1489 SkSL::Program::kGeometry_Kind);
Ethan Nicholas7da6dfa2017-06-21 11:25:18 -04001490}
1491
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001492DEF_TEST(SkSLTypePrecision, r) {
1493 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001494 "float f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001495 "half h = 2;"
1496 "double d = 3;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001497 "float2 f2 = float2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001498 "half3 h3 = half3(1, 2, 3);"
1499 "double4 d4 = double4(1, 2, 3, 4);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001500 "float2x2 f22 = float2x2(1, 2, 3, 4);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001501 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);"
1502 "double4x2 d42 = double4x2(1, 2, 3, 4, 5, 6, 7, 8);",
1503 *SkSL::ShaderCapsFactory::Default(),
1504 "#version 400\n"
1505 "out vec4 sk_FragColor;\n"
1506 "float f = 1.0;\n"
1507 "float h = 2.0;\n"
1508 "double d = 3.0;\n"
1509 "vec2 f2 = vec2(1.0, 2.0);\n"
1510 "vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
1511 "dvec4 d4 = dvec4(1.0, 2.0, 3.0, 4.0);\n"
1512 "mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
1513 "mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n"
1514 "dmat4x2 d42 = dmat4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1515 test(r,
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001516 "float f = 1;"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001517 "half h = 2;"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001518 "float2 f2 = float2(1, 2);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001519 "half3 h3 = half3(1, 2, 3);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001520 "float2x2 f22 = float2x2(1, 2, 3, 4);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001521 "half2x4 h24 = half2x4(1, 2, 3, 4, 5, 6, 7, 8);",
1522 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1523 "#version 400\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001524 "precision mediump float;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001525 "out mediump vec4 sk_FragColor;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001526 "highp float f = 1.0;\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001527 "mediump float h = 2.0;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001528 "highp vec2 f2 = vec2(1.0, 2.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001529 "mediump vec3 h3 = vec3(1.0, 2.0, 3.0);\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001530 "highp mat2 f22 = mat2(1.0, 2.0, 3.0, 4.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001531 "mediump mat2x4 h24 = mat2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0);\n");
1532}
1533
1534DEF_TEST(SkSLNumberConversions, r) {
1535 test(r,
1536 "short s = short(sqrt(1));"
1537 "int i = int(sqrt(1));"
1538 "ushort us = ushort(sqrt(1));"
1539 "uint ui = uint(sqrt(1));"
1540 "half h = sqrt(1);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001541 "float f = sqrt(1);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001542 "short s2s = s;"
1543 "short i2s = i;"
1544 "short us2s = short(us);"
1545 "short ui2s = short(ui);"
1546 "short h2s = short(h);"
1547 "short f2s = short(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001548 "int s2i = s;"
1549 "int i2i = i;"
1550 "int us2i = int(us);"
1551 "int ui2i = int(ui);"
1552 "int h2i = int(h);"
1553 "int f2i = int(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001554 "ushort s2us = ushort(s);"
1555 "ushort i2us = ushort(i);"
1556 "ushort us2us = us;"
1557 "ushort ui2us = ui;"
1558 "ushort h2us = ushort(h);"
1559 "ushort f2us = ushort(f);"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001560 "uint s2ui = uint(s);"
1561 "uint i2ui = uint(i);"
1562 "uint us2ui = us;"
1563 "uint ui2ui = ui;"
1564 "uint h2ui = uint(h);"
1565 "uint f2ui = uint(f);"
Ethan Nicholas8aa45692017-09-20 11:24:15 -04001566 "float s2f = s;"
1567 "float i2f = i;"
1568 "float us2f = us;"
1569 "float ui2f = ui;"
1570 "float h2f = h;"
1571 "float f2f = f;",
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001572 *SkSL::ShaderCapsFactory::Default(),
1573 "#version 400\n"
1574 "out vec4 sk_FragColor;\n"
1575 "int s = int(sqrt(1.0));\n"
1576 "int i = int(sqrt(1.0));\n"
1577 "uint us = uint(sqrt(1.0));\n"
1578 "uint ui = uint(sqrt(1.0));\n"
1579 "float h = sqrt(1.0);\n"
1580 "float f = sqrt(1.0);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001581 "int s2s = s;\n"
1582 "int i2s = i;\n"
1583 "int us2s = int(us);\n"
1584 "int ui2s = int(ui);\n"
1585 "int h2s = int(h);\n"
1586 "int f2s = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001587 "int s2i = s;\n"
1588 "int i2i = i;\n"
1589 "int us2i = int(us);\n"
1590 "int ui2i = int(ui);\n"
1591 "int h2i = int(h);\n"
1592 "int f2i = int(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001593 "uint s2us = uint(s);\n"
1594 "uint i2us = uint(i);\n"
1595 "uint us2us = us;\n"
1596 "uint ui2us = ui;\n"
1597 "uint h2us = uint(h);\n"
1598 "uint f2us = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001599 "uint s2ui = uint(s);\n"
1600 "uint i2ui = uint(i);\n"
1601 "uint us2ui = us;\n"
1602 "uint ui2ui = ui;\n"
1603 "uint h2ui = uint(h);\n"
1604 "uint f2ui = uint(f);\n"
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001605 "float s2f = float(s);\n"
1606 "float i2f = float(i);\n"
1607 "float us2f = float(us);\n"
1608 "float ui2f = float(ui);\n"
1609 "float h2f = h;\n"
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001610 "float f2f = f;\n");
1611}
1612
1613DEF_TEST(SkSLForceHighPrecision, r) {
1614 test(r,
1615 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1616 *SkSL::ShaderCapsFactory::UsesPrecisionModifiers(),
1617 "#version 400\n"
1618 "precision mediump float;\n"
1619 "out mediump vec4 sk_FragColor;\n"
1620 "void main() {\n"
1621 " mediump float x = sqrt(1.0);\n"
1622 " mediump vec4 y = vec4(x);\n"
1623 " sk_FragColor = y;\n"
1624 "}\n");
1625 SkSL::Program::Settings settings;
1626 settings.fForceHighPrecision = true;
1627 sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::UsesPrecisionModifiers();
1628 settings.fCaps = caps.get();
1629 SkSL::Program::Inputs inputs;
1630 test(r,
1631 "void main() { half x = sqrt(1); half4 y = half4(x); sk_FragColor = y; }",
1632 settings,
1633 "#version 400\n"
1634 "precision mediump float;\n"
1635 "out mediump vec4 sk_FragColor;\n"
1636 "void main() {\n"
1637 " highp float x = sqrt(1.0);\n"
1638 " highp vec4 y = vec4(x);\n"
1639 " sk_FragColor = y;\n"
1640 "}\n",
1641 &inputs);
Ethan Nicholasdcba08e2017-08-02 10:52:54 -04001642}
1643
Ethan Nicholas7ef4b742016-11-11 15:16:46 -05001644#endif