blob: b944bc449f30179e2295d392c93d05ccf01b10aa [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
Brian Salomon94efbf52016-11-29 13:43:05 -050014static void test(skiatest::Reporter* r, const char* src, const GrShaderCaps& caps,
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050015 const char* expected) {
ethannicholasf789b382016-08-03 12:43:36 -070016 SkSL::Compiler compiler;
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050017 SkString output;
18 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, SkString(src), caps, &output);
ethannicholasf789b382016-08-03 12:43:36 -070019 if (!result) {
20 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
21 }
22 REPORTER_ASSERT(r, result);
23 if (result) {
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050024 SkString skExpected(expected);
25 if (output != skExpected) {
ethannicholasf789b382016-08-03 12:43:36 -070026 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
27 expected, output.c_str());
28 }
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050029 REPORTER_ASSERT(r, output == skExpected);
ethannicholasf789b382016-08-03 12:43:36 -070030 }
31}
32
33DEF_TEST(SkSLHelloWorld, r) {
ethannicholasf789b382016-08-03 12:43:36 -070034 test(r,
ethannicholas5961bc92016-10-12 06:39:56 -070035 "void main() { sk_FragColor = vec4(0.75); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050036 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070037 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050038 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070039 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050040 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070041 "}\n");
42}
43
44DEF_TEST(SkSLControl, r) {
ethannicholasf789b382016-08-03 12:43:36 -070045 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070046 "void main() {"
ethannicholas08a92112016-11-09 13:26:45 -080047 "if (sqrt(2) > 5) { sk_FragColor = vec4(0.75); } else { discard; }"
ethannicholasf789b382016-08-03 12:43:36 -070048 "int i = 0;"
ethannicholas5961bc92016-10-12 06:39:56 -070049 "while (i < 10) sk_FragColor *= 0.5;"
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050050 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.75);"
ethannicholasf789b382016-08-03 12:43:36 -070051 "for (int i = 0; i < 10; i++) {"
52 "if (i % 0 == 1) break; else continue;"
53 "}"
54 "return;"
55 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050056 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070057 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050058 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -070059 "void main() {\n"
ethannicholas08a92112016-11-09 13:26:45 -080060 " if (sqrt(2.0) > 5.0) {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050061 " sk_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070062 " } else {\n"
63 " discard;\n"
64 " }\n"
65 " int i = 0;\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050066 " while (i < 10) sk_FragColor *= 0.5;\n"
ethannicholasf789b382016-08-03 12:43:36 -070067 " do {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050068 " sk_FragColor += 0.01;\n"
Ethan Nicholas9e1138d2016-11-21 10:39:35 -050069 " } while (sk_FragColor.x < 0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070070 " for (int i = 0;i < 10; i++) {\n"
71 " if (i % 0 == 1) break; else continue;\n"
72 " }\n"
73 " return;\n"
74 "}\n");
75}
76
77DEF_TEST(SkSLFunctions, r) {
ethannicholasf789b382016-08-03 12:43:36 -070078 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070079 "float foo(float v[2]) { return v[0] * v[1]; }"
80 "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 -070081 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050082 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -070083 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050084 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -070085 "float foo(in float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -070086 " return v[0] * v[1];\n"
87 "}\n"
88 "void bar(inout float x) {\n"
89 " float y[2], z;\n"
90 " y[0] = x;\n"
ethannicholas5961bc92016-10-12 06:39:56 -070091 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -070092 " z = foo(y);\n"
93 " x = z;\n"
94 "}\n"
95 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -070096 " float x = 10.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -070097 " bar(x);\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -050098 " sk_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -070099 "}\n");
100}
101
102DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700103 test(r,
104 "void main() {"
105 "float x = 1, y = 2;"
106 "int z = 3;"
107 "x = x + y * z * x * (y - z);"
108 "y = x / y / z;"
109 "z = (z / 2 % 3 << 4) >> 2 << 1;"
ethannicholas08a92112016-11-09 13:26:45 -0800110 "bool b = (x > 4) == x < 2 || 2 >= sqrt(2) && y <= z;"
ethannicholasf789b382016-08-03 12:43:36 -0700111 "x += 12;"
112 "x -= 12;"
113 "x *= y /= z = 10;"
114 "b ||= false;"
115 "b &&= true;"
116 "b ^^= false;"
117 "z |= 0;"
118 "z &= -1;"
119 "z ^= 0;"
120 "z >>= 2;"
121 "z <<= 4;"
122 "z %= 5;"
123 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500124 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700125 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500126 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700127 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700128 " float x = 1.0, y = 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700129 " int z = 3;\n"
130 " x = x + ((y * float(z)) * x) * (y - float(z));\n"
131 " y = (x / y) / float(z);\n"
132 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800133 " bool b = x > 4.0 == x < 2.0 || 2.0 >= sqrt(2.0) && y <= float(z);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700134 " x += 12.0;\n"
135 " x -= 12.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700136 " x *= (y /= float(z = 10));\n"
137 " b ||= false;\n"
138 " b &&= true;\n"
139 " b ^^= false;\n"
140 " z |= 0;\n"
141 " z &= -1;\n"
142 " z ^= 0;\n"
143 " z >>= 2;\n"
144 " z <<= 4;\n"
145 " z %= 5;\n"
146 "}\n");
147}
148
149DEF_TEST(SkSLMatrices, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700150 test(r,
151 "void main() {"
152 "mat2x4 x = mat2x4(1);"
153 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));"
154 "mat3x4 z = x * y;"
155 "vec3 v1 = mat3(1) * vec3(1);"
156 "vec3 v2 = vec3(1) * mat3(1);"
157 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500158 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700159 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500160 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700161 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700162 " mat2x4 x = mat2x4(1.0);\n"
163 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n"
ethannicholasf789b382016-08-03 12:43:36 -0700164 " mat3x4 z = x * y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700165 " vec3 v1 = mat3(1.0) * vec3(1.0);\n"
166 " vec3 v2 = vec3(1.0) * mat3(1.0);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700167 "}\n");
168}
169
170DEF_TEST(SkSLInterfaceBlock, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700171 test(r,
172 "uniform testBlock {"
173 "float x;"
174 "float y[2];"
175 "layout(binding=12) mat3x2 z;"
176 "bool w;"
177 "};"
178 "void main() {"
179 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500180 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700181 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500182 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700183 "uniform testBlock {\n"
184 " float x;\n"
185 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700186 " layout (binding = 12) mat3x2 z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700187 " bool w;\n"
188 "};\n"
189 "void main() {\n"
190 "}\n");
191}
192
193DEF_TEST(SkSLStructs, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700194 test(r,
195 "struct A {"
196 "int x;"
197 "int y;"
198 "} a1, a2;"
199 "A a3;"
200 "struct B {"
201 "float x;"
202 "float y[2];"
203 "layout(binding=1) A z;"
204 "};"
205 "B b1, b2, b3;"
206 "void main() {"
207 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500208 *SkSL::GLSLCapsFactory::Default(),
ethannicholasf789b382016-08-03 12:43:36 -0700209 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500210 "out vec4 sk_FragColor;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700211 "struct A {\n"
212 " int x;\n"
213 " int y;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500214 "} a1, a2;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700215 "A a3;\n"
216 "struct B {\n"
217 " float x;\n"
218 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700219 " layout (binding = 1) A z;\n"
Ethan Nicholas19671772016-11-28 16:30:17 -0500220 "} b1, b2, b3;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700221 "void main() {\n"
222 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700223}
ethannicholasf789b382016-08-03 12:43:36 -0700224
ethannicholas5961bc92016-10-12 06:39:56 -0700225DEF_TEST(SkSLVersion, r) {
ethannicholas5961bc92016-10-12 06:39:56 -0700226 test(r,
227 "in float test; void main() { sk_FragColor = vec4(0.75); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500228 *SkSL::GLSLCapsFactory::Version450Core(),
ethannicholas5961bc92016-10-12 06:39:56 -0700229 "#version 450 core\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500230 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700231 "in float test;\n"
232 "void main() {\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500233 " sk_FragColor = vec4(0.75);\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700234 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700235 test(r,
236 "in float test; void main() { sk_FragColor = vec4(0.75); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500237 *SkSL::GLSLCapsFactory::Version110(),
ethannicholas5961bc92016-10-12 06:39:56 -0700238 "#version 110\n"
239 "varying float test;\n"
240 "void main() {\n"
241 " gl_FragColor = vec4(0.75);\n"
242 "}\n");
243}
244
Leon Scroggins857cb972016-11-11 18:44:37 +0000245DEF_TEST(SkSLUsesPrecisionModifiers, r) {
Leon Scroggins857cb972016-11-11 18:44:37 +0000246 test(r,
247 "void main() { float x = 0.75; highp float y = 1; }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500248 *SkSL::GLSLCapsFactory::Default(),
Leon Scroggins857cb972016-11-11 18:44:37 +0000249 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500250 "out vec4 sk_FragColor;\n"
Leon Scroggins857cb972016-11-11 18:44:37 +0000251 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700252 " float x = 0.75;\n"
253 " float y = 1.0;\n"
254 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700255 test(r,
256 "void main() { float x = 0.75; highp float y = 1; }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500257 *SkSL::GLSLCapsFactory::UsesPrecisionModifiers(),
258 "#version 400\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700259 "precision highp float;\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500260 "out mediump vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700261 "void main() {\n"
262 " float x = 0.75;\n"
263 " highp float y = 1.0;\n"
264 "}\n");
265}
266
267DEF_TEST(SkSLMinAbs, r) {
268 test(r,
269 "void main() {"
270 "float x = -5;"
271 "x = min(abs(x), 6);"
272 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500273 *SkSL::GLSLCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700274 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500275 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700276 "void main() {\n"
277 " float x = -5.0;\n"
278 " x = min(abs(x), 6.0);\n"
279 "}\n");
280
ethannicholas5961bc92016-10-12 06:39:56 -0700281 test(r,
282 "void main() {"
283 "float x = -5.0;"
284 "x = min(abs(x), 6.0);"
285 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500286 *SkSL::GLSLCapsFactory::CannotUseMinAndAbsTogether(),
ethannicholas5961bc92016-10-12 06:39:56 -0700287 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500288 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700289 "void main() {\n"
290 " float minAbsHackVar0;\n"
291 " float minAbsHackVar1;\n"
292 " float x = -5.0;\n"
293 " x = ((minAbsHackVar0 = abs(x)) < (minAbsHackVar1 = 6.0) ? minAbsHackVar0 : "
294 "minAbsHackVar1);\n"
295 "}\n");
296}
297
ethannicholasad146f62016-10-14 06:40:02 -0700298DEF_TEST(SkSLNegatedAtan, r) {
299 test(r,
300 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500301 *SkSL::GLSLCapsFactory::Default(),
ethannicholasad146f62016-10-14 06:40:02 -0700302 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500303 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700304 "void main() {\n"
305 " vec2 x = vec2(1.0, 2.0);\n"
306 " float y = atan(x.x, -(2.0 * x.y));\n"
307 "}\n");
ethannicholasad146f62016-10-14 06:40:02 -0700308 test(r,
309 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500310 *SkSL::GLSLCapsFactory::MustForceNegatedAtanParamToFloat(),
ethannicholasad146f62016-10-14 06:40:02 -0700311 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500312 "out vec4 sk_FragColor;\n"
ethannicholasad146f62016-10-14 06:40:02 -0700313 "void main() {\n"
314 " vec2 x = vec2(1.0, 2.0);\n"
315 " float y = atan(x.x, -1.0 * (2.0 * x.y));\n"
316 "}\n");
317}
318
ethannicholas5961bc92016-10-12 06:39:56 -0700319DEF_TEST(SkSLModifiersDeclaration, r) {
320 test(r,
321 "layout(blend_support_all_equations) out;"
322 "void main() { }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500323 *SkSL::GLSLCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700324 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500325 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700326 "layout (blend_support_all_equations) out ;\n"
327 "void main() {\n"
328 "}\n");
329}
330
331DEF_TEST(SkSLHex, r) {
332 test(r,
333 "void main() {"
334 "int i1 = 0x0;"
335 "int i2 = 0x1234abcd;"
336 "int i3 = 0x7fffffff;"
337 "int i4 = 0xffffffff;"
338 "int i5 = -0xbeef;"
339 "uint u1 = 0x0;"
340 "uint u2 = 0x1234abcd;"
341 "uint u3 = 0x7fffffff;"
342 "uint u4 = 0xffffffff;"
343 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500344 *SkSL::GLSLCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700345 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500346 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700347 "void main() {\n"
348 " int i1 = 0;\n"
349 " int i2 = 305441741;\n"
350 " int i3 = 2147483647;\n"
351 " int i4 = -1;\n"
352 " int i5 = -48879;\n"
353 " uint u1 = 0u;\n"
354 " uint u2 = 305441741u;\n"
355 " uint u3 = 2147483647u;\n"
356 " uint u4 = 4294967295u;\n"
357 "}\n");
358}
359
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400360DEF_TEST(SkSLVectorConstructors, r) {
361 test(r,
362 "vec2 v1 = vec2(1);"
363 "vec2 v2 = vec2(1, 2);"
364 "vec2 v3 = vec2(vec2(1));"
365 "vec2 v4 = vec2(vec3(1));"
366 "vec3 v5 = vec3(vec2(1), 1.0);"
367 "vec3 v6 = vec3(vec4(1, 2, 3, 4));"
368 "ivec2 v7 = ivec2(1);"
369 "ivec2 v8 = ivec2(vec2(1, 2));"
370 "vec2 v9 = vec2(ivec2(1, 2));",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500371 *SkSL::GLSLCapsFactory::Default(),
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400372 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500373 "out vec4 sk_FragColor;\n"
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400374 "vec2 v1 = vec2(1.0);\n"
375 "vec2 v2 = vec2(1.0, 2.0);\n"
376 "vec2 v3 = vec2(1.0);\n"
377 "vec2 v4 = vec2(vec3(1.0));\n"
378 "vec3 v5 = vec3(vec2(1.0), 1.0);\n"
379 "vec3 v6 = vec3(vec4(1.0, 2.0, 3.0, 4.0));\n"
380 "ivec2 v7 = ivec2(1);\n"
381 "ivec2 v8 = ivec2(vec2(1.0, 2.0));\n"
382 "vec2 v9 = vec2(ivec2(1, 2));\n");
383}
384
ethannicholas5961bc92016-10-12 06:39:56 -0700385DEF_TEST(SkSLArrayConstructors, r) {
386 test(r,
387 "float test1[] = float[](1, 2, 3, 4);"
388 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));"
389 "mat4 test3[] = mat4[]();",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500390 *SkSL::GLSLCapsFactory::Default(),
ethannicholas5961bc92016-10-12 06:39:56 -0700391 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500392 "out vec4 sk_FragColor;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700393 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
394 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
395 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700396}
ethannicholasddb37d62016-10-20 09:54:00 -0700397
398DEF_TEST(SkSLDerivatives, r) {
399 test(r,
400 "void main() { float x = dFdx(1); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500401 *SkSL::GLSLCapsFactory::Default(),
ethannicholasddb37d62016-10-20 09:54:00 -0700402 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500403 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700404 "void main() {\n"
405 " float x = dFdx(1.0);\n"
406 "}\n");
ethannicholasddb37d62016-10-20 09:54:00 -0700407 test(r,
408 "void main() { float x = 1; }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500409 *SkSL::GLSLCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700410 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500411 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700412 "void main() {\n"
413 " float x = 1.0;\n"
414 "}\n");
415 test(r,
416 "void main() { float x = dFdx(1); }",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500417 *SkSL::GLSLCapsFactory::ShaderDerivativeExtensionString(),
ethannicholasddb37d62016-10-20 09:54:00 -0700418 "#version 400\n"
419 "#extension GL_OES_standard_derivatives : require\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500420 "out vec4 sk_FragColor;\n"
ethannicholasddb37d62016-10-20 09:54:00 -0700421 "void main() {\n"
422 " float x = dFdx(1.0);\n"
423 "}\n");
424}
ethannicholas08a92112016-11-09 13:26:45 -0800425
426DEF_TEST(SkSLConstantFolding, r) {
427 test(r,
428 "void main() {"
429 "float f_add = 32 + 2;"
430 "float f_sub = 32 - 2;"
431 "float f_mul = 32 * 2;"
432 "float f_div = 32 / 2;"
433 "float mixed = (12 > 2.0) ? (10 * 2 / 5 + 18 - 3) : 0;"
434 "int i_add = 32 + 2;"
435 "int i_sub = 32 - 2;"
436 "int i_mul = 32 * 2;"
437 "int i_div = 32 / 2;"
438 "int i_or = 12 | 6;"
439 "int i_and = 254 & 7;"
440 "int i_xor = 2 ^ 7;"
441 "int i_shl = 1 << 4;"
442 "int i_shr = 128 >> 2;"
443 "bool gt_it = 6 > 5;"
444 "bool gt_if = 6 > 6;"
445 "bool gt_ft = 6.0 > 5.0;"
446 "bool gt_ff = 6.0 > 6.0;"
447 "bool gte_it = 6 >= 6;"
448 "bool gte_if = 6 >= 7;"
449 "bool gte_ft = 6.0 >= 6.0;"
450 "bool gte_ff = 6.0 >= 7.0;"
451 "bool lte_it = 6 <= 6;"
452 "bool lte_if = 6 <= 5;"
453 "bool lte_ft = 6.0 <= 6.0;"
454 "bool lte_ff = 6.0 <= 5.0;"
455 "bool or_t = 1 == 1 || 2 == 8;"
456 "bool or_f = 1 > 1 || 2 == 8;"
457 "bool and_t = 1 == 1 && 2 <= 8;"
458 "bool and_f = 1 == 2 && 2 == 8;"
459 "bool xor_t = 1 == 1 ^^ 1 != 1;"
460 "bool xor_f = 1 == 1 ^^ 1 == 1;"
461 "int ternary = 10 > 5 ? 10 : 5;"
462 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500463 *SkSL::GLSLCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800464 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500465 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800466 "void main() {\n"
467 " float f_add = 34.0;\n"
468 " float f_sub = 30.0;\n"
469 " float f_mul = 64.0;\n"
470 " float f_div = 16.0;\n"
471 " float mixed = 19.0;\n"
472 " int i_add = 34;\n"
473 " int i_sub = 30;\n"
474 " int i_mul = 64;\n"
475 " int i_div = 16;\n"
476 " int i_or = 14;\n"
477 " int i_and = 6;\n"
478 " int i_xor = 5;\n"
479 " int i_shl = 16;\n"
480 " int i_shr = 32;\n"
481 " bool gt_it = true;\n"
482 " bool gt_if = false;\n"
483 " bool gt_ft = true;\n"
484 " bool gt_ff = false;\n"
485 " bool gte_it = true;\n"
486 " bool gte_if = false;\n"
487 " bool gte_ft = true;\n"
488 " bool gte_ff = false;\n"
489 " bool lte_it = true;\n"
490 " bool lte_if = false;\n"
491 " bool lte_ft = true;\n"
492 " bool lte_ff = false;\n"
493 " bool or_t = true;\n"
494 " bool or_f = false;\n"
495 " bool and_t = true;\n"
496 " bool and_f = false;\n"
497 " bool xor_t = true;\n"
498 " bool xor_f = false;\n"
499 " int ternary = 10;\n"
500 "}\n");
501}
502
503DEF_TEST(SkSLStaticIf, r) {
504 test(r,
505 "void main() {"
506 "int x;"
507 "if (true) x = 1;"
508 "if (2 > 1) x = 2; else x = 3;"
509 "if (1 > 2) x = 4; else x = 5;"
510 "if (false) x = 6;"
511 "}",
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500512 *SkSL::GLSLCapsFactory::Default(),
ethannicholas08a92112016-11-09 13:26:45 -0800513 "#version 400\n"
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500514 "out vec4 sk_FragColor;\n"
ethannicholas08a92112016-11-09 13:26:45 -0800515 "void main() {\n"
516 " int x;\n"
517 " x = 1;\n"
518 " x = 2;\n"
519 " x = 5;\n"
520 " {\n"
521 " }\n"
522 "}\n");
523}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500524
Ethan Nicholas3605ace2016-11-21 15:59:48 -0500525DEF_TEST(SkSLCaps, r) {
526 test(r,
527 "void main() {"
528 "int x;"
529 "if (sk_Caps.externalTextureSupport) x = 1;"
530 "if (sk_Caps.fbFetchSupport) x = 2;"
531 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.texelFetchSupport) x = 3;"
532 "if (sk_Caps.dropsTileOnZeroDivide && sk_Caps.canUseAnyFunctionInShader) x = 4;"
533 "}",
534 *SkSL::GLSLCapsFactory::VariousCaps(),
535 "#version 400\n"
536 "out vec4 sk_FragColor;\n"
537 "void main() {\n"
538 " int x;\n"
539 " x = 1;\n"
540 " {\n"
541 " }\n"
542 " x = 3;\n"
543 " {\n"
544 " }\n"
545 "}\n");
546}
547
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500548DEF_TEST(SkSLTexture, r) {
549 test(r,
550 "uniform sampler1D one;"
551 "uniform sampler2D two;"
552 "void main() {"
553 "vec4 a = texture(one, 0);"
554 "vec4 b = texture(two, vec2(0));"
555 "vec4 c = texture(one, vec2(0));"
556 "vec4 d = texture(two, vec3(0));"
557 "}",
558 *SkSL::GLSLCapsFactory::Default(),
559 "#version 400\n"
560 "out vec4 sk_FragColor;\n"
561 "uniform sampler1D one;\n"
562 "uniform sampler2D two;\n"
563 "void main() {\n"
564 " vec4 a = texture(one, 0.0);\n"
565 " vec4 b = texture(two, vec2(0.0));\n"
566 " vec4 c = textureProj(one, vec2(0.0));\n"
567 " vec4 d = textureProj(two, vec3(0.0));\n"
568 "}\n");
569 test(r,
570 "uniform sampler1D one;"
571 "uniform sampler2D two;"
572 "void main() {"
573 "vec4 a = texture(one, 0);"
574 "vec4 b = texture(two, vec2(0));"
575 "vec4 c = texture(one, vec2(0));"
576 "vec4 d = texture(two, vec3(0));"
577 "}",
578 *SkSL::GLSLCapsFactory::Version110(),
579 "#version 110\n"
580 "uniform sampler1D one;\n"
581 "uniform sampler2D two;\n"
582 "void main() {\n"
583 " vec4 a = texture1D(one, 0.0);\n"
584 " vec4 b = texture2D(two, vec2(0.0));\n"
585 " vec4 c = texture1DProj(one, vec2(0.0));\n"
586 " vec4 d = texture2DProj(two, vec3(0.0));\n"
587 "}\n");
588}
589
Ethan Nicholas19671772016-11-28 16:30:17 -0500590DEF_TEST(SkSLOffset, r) {
591 test(r,
592 "struct Test {"
593 "layout(offset = 0) int x;"
594 "layout(offset = 4) int y;"
595 "int z;"
596 "} test;",
597 *SkSL::GLSLCapsFactory::Default(),
598 "#version 400\n"
599 "out vec4 sk_FragColor;\n"
600 "struct Test {\n"
601 " layout (offset = 0) int x;\n"
602 " layout (offset = 4) int y;\n"
603 " int z;\n"
604 "} test;\n");
605}
Ethan Nicholas7ef4b742016-11-11 15:16:46 -0500606#endif