blob: 610ff2b5abf7c855ecf33d6ecd10e70dc5e43532 [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
12static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, const char* expected) {
13 SkSL::Compiler compiler;
14 std::string output;
15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &output);
16 if (!result) {
17 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().c_str());
18 }
19 REPORTER_ASSERT(r, result);
20 if (result) {
21 if (output != expected) {
22 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived:\n'%s'", src,
23 expected, output.c_str());
24 }
25 REPORTER_ASSERT(r, output == expected);
26 }
27}
28
ethannicholas5961bc92016-10-12 06:39:56 -070029static SkSL::GLCaps default_caps() {
ethannicholasddb37d62016-10-20 09:54:00 -070030 return SkSL::GLCaps();
ethannicholas5961bc92016-10-12 06:39:56 -070031}
32
ethannicholasf789b382016-08-03 12:43:36 -070033DEF_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); }",
36 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070037 "#version 400\n"
ethannicholasf789b382016-08-03 12:43:36 -070038 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -070039 " gl_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070040 "}\n");
41}
42
43DEF_TEST(SkSLControl, r) {
ethannicholasf789b382016-08-03 12:43:36 -070044 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070045 "void main() {"
ethannicholas5961bc92016-10-12 06:39:56 -070046 "if (1 + 2 + 3 > 5) { sk_FragColor = vec4(0.75); } else { discard; }"
ethannicholasf789b382016-08-03 12:43:36 -070047 "int i = 0;"
ethannicholas5961bc92016-10-12 06:39:56 -070048 "while (i < 10) sk_FragColor *= 0.5;"
49 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.7);"
ethannicholasf789b382016-08-03 12:43:36 -070050 "for (int i = 0; i < 10; i++) {"
51 "if (i % 0 == 1) break; else continue;"
52 "}"
53 "return;"
54 "}",
ethannicholas5961bc92016-10-12 06:39:56 -070055 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070056 "#version 400\n"
ethannicholasf789b382016-08-03 12:43:36 -070057 "void main() {\n"
58 " if ((1 + 2) + 3 > 5) {\n"
ethannicholas5961bc92016-10-12 06:39:56 -070059 " gl_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070060 " } else {\n"
61 " discard;\n"
62 " }\n"
63 " int i = 0;\n"
ethannicholas5961bc92016-10-12 06:39:56 -070064 " while (i < 10) gl_FragColor *= 0.5;\n"
ethannicholasf789b382016-08-03 12:43:36 -070065 " do {\n"
ethannicholas5961bc92016-10-12 06:39:56 -070066 " gl_FragColor += 0.01;\n"
67 " } while (gl_FragColor.x < 0.7);\n"
ethannicholasf789b382016-08-03 12:43:36 -070068 " for (int i = 0;i < 10; i++) {\n"
69 " if (i % 0 == 1) break; else continue;\n"
70 " }\n"
71 " return;\n"
72 "}\n");
73}
74
75DEF_TEST(SkSLFunctions, r) {
ethannicholasf789b382016-08-03 12:43:36 -070076 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070077 "float foo(float v[2]) { return v[0] * v[1]; }"
78 "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 -070079 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }",
80 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070081 "#version 400\n"
ethannicholas5961bc92016-10-12 06:39:56 -070082 "float foo(in float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -070083 " return v[0] * v[1];\n"
84 "}\n"
85 "void bar(inout float x) {\n"
86 " float y[2], z;\n"
87 " y[0] = x;\n"
ethannicholas5961bc92016-10-12 06:39:56 -070088 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -070089 " z = foo(y);\n"
90 " x = z;\n"
91 "}\n"
92 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -070093 " float x = 10.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -070094 " bar(x);\n"
ethannicholas5961bc92016-10-12 06:39:56 -070095 " gl_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -070096 "}\n");
97}
98
99DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700100 test(r,
101 "void main() {"
102 "float x = 1, y = 2;"
103 "int z = 3;"
104 "x = x + y * z * x * (y - z);"
105 "y = x / y / z;"
106 "z = (z / 2 % 3 << 4) >> 2 << 1;"
107 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;"
108 "x += 12;"
109 "x -= 12;"
110 "x *= y /= z = 10;"
111 "b ||= false;"
112 "b &&= true;"
113 "b ^^= false;"
114 "z |= 0;"
115 "z &= -1;"
116 "z ^= 0;"
117 "z >>= 2;"
118 "z <<= 4;"
119 "z %= 5;"
120 "}",
ethannicholas5961bc92016-10-12 06:39:56 -0700121 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700122 "#version 400\n"
123 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700124 " float x = 1.0, y = 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700125 " int z = 3;\n"
126 " x = x + ((y * float(z)) * x) * (y - float(z));\n"
127 " y = (x / y) / float(z);\n"
128 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700129 " bool b = x > 4.0 == x < 2.0 || (2 >= 5 && y <= float(z)) && 12 != 11;\n"
130 " x += 12.0;\n"
131 " x -= 12.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700132 " x *= (y /= float(z = 10));\n"
133 " b ||= false;\n"
134 " b &&= true;\n"
135 " b ^^= false;\n"
136 " z |= 0;\n"
137 " z &= -1;\n"
138 " z ^= 0;\n"
139 " z >>= 2;\n"
140 " z <<= 4;\n"
141 " z %= 5;\n"
142 "}\n");
143}
144
145DEF_TEST(SkSLMatrices, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700146 test(r,
147 "void main() {"
148 "mat2x4 x = mat2x4(1);"
149 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));"
150 "mat3x4 z = x * y;"
151 "vec3 v1 = mat3(1) * vec3(1);"
152 "vec3 v2 = vec3(1) * mat3(1);"
153 "}",
ethannicholas5961bc92016-10-12 06:39:56 -0700154 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700155 "#version 400\n"
156 "void main() {\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700157 " mat2x4 x = mat2x4(1.0);\n"
158 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n"
ethannicholasf789b382016-08-03 12:43:36 -0700159 " mat3x4 z = x * y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700160 " vec3 v1 = mat3(1.0) * vec3(1.0);\n"
161 " vec3 v2 = vec3(1.0) * mat3(1.0);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700162 "}\n");
163}
164
165DEF_TEST(SkSLInterfaceBlock, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700166 test(r,
167 "uniform testBlock {"
168 "float x;"
169 "float y[2];"
170 "layout(binding=12) mat3x2 z;"
171 "bool w;"
172 "};"
173 "void main() {"
174 "}",
ethannicholas5961bc92016-10-12 06:39:56 -0700175 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700176 "#version 400\n"
177 "uniform testBlock {\n"
178 " float x;\n"
179 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700180 " layout (binding = 12) mat3x2 z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700181 " bool w;\n"
182 "};\n"
183 "void main() {\n"
184 "}\n");
185}
186
187DEF_TEST(SkSLStructs, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700188 test(r,
189 "struct A {"
190 "int x;"
191 "int y;"
192 "} a1, a2;"
193 "A a3;"
194 "struct B {"
195 "float x;"
196 "float y[2];"
197 "layout(binding=1) A z;"
198 "};"
199 "B b1, b2, b3;"
200 "void main() {"
201 "}",
ethannicholas5961bc92016-10-12 06:39:56 -0700202 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700203 "#version 400\n"
204 "struct A {\n"
205 " int x;\n"
206 " int y;\n"
207 "}\n"
208 " a1, a2;\n"
209 "A a3;\n"
210 "struct B {\n"
211 " float x;\n"
212 " float[2] y;\n"
ethannicholas5961bc92016-10-12 06:39:56 -0700213 " layout (binding = 1) A z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700214 "}\n"
215 " b1, b2, b3;\n"
216 "void main() {\n"
217 "}\n");
ethannicholas5961bc92016-10-12 06:39:56 -0700218}
ethannicholasf789b382016-08-03 12:43:36 -0700219
ethannicholas5961bc92016-10-12 06:39:56 -0700220DEF_TEST(SkSLVersion, r) {
221 SkSL::GLCaps caps = default_caps();
222 caps.fVersion = 450;
223 caps.fIsCoreProfile = true;
224 test(r,
225 "in float test; void main() { sk_FragColor = vec4(0.75); }",
226 caps,
227 "#version 450 core\n"
228 "in float test;\n"
229 "void main() {\n"
230 " gl_FragColor = vec4(0.75);\n"
231 "}\n");
232 caps.fVersion = 110;
233 caps.fIsCoreProfile = false;
234 test(r,
235 "in float test; void main() { sk_FragColor = vec4(0.75); }",
236 caps,
237 "#version 110\n"
238 "varying float test;\n"
239 "void main() {\n"
240 " gl_FragColor = vec4(0.75);\n"
241 "}\n");
242}
243
244DEF_TEST(SkSLDeclareOutput, r) {
245 SkSL::GLCaps caps = default_caps();
246 caps.fMustDeclareFragmentShaderOutput = true;
247 test(r,
248 "void main() { sk_FragColor = vec4(0.75); }",
249 caps,
250 "#version 400\n"
251 "out vec4 sk_FragColor;\n"
252 "void main() {\n"
253 " sk_FragColor = vec4(0.75);\n"
254 "}\n");
255}
256
257DEF_TEST(SkSLUsesPrecisionModifiers, r) {
258 SkSL::GLCaps caps = default_caps();
259 test(r,
260 "void main() { float x = 0.75; highp float y = 1; }",
261 caps,
262 "#version 400\n"
263 "void main() {\n"
264 " float x = 0.75;\n"
265 " float y = 1.0;\n"
266 "}\n");
267 caps.fStandard = SkSL::GLCaps::kGLES_Standard;
268 caps.fUsesPrecisionModifiers = true;
269 test(r,
270 "void main() { float x = 0.75; highp float y = 1; }",
271 caps,
272 "#version 400 es\n"
273 "precision highp float;\n"
274 "void main() {\n"
275 " float x = 0.75;\n"
276 " highp float y = 1.0;\n"
277 "}\n");
278}
279
280DEF_TEST(SkSLMinAbs, r) {
281 test(r,
282 "void main() {"
283 "float x = -5;"
284 "x = min(abs(x), 6);"
285 "}",
286 default_caps(),
287 "#version 400\n"
288 "void main() {\n"
289 " float x = -5.0;\n"
290 " x = min(abs(x), 6.0);\n"
291 "}\n");
292
293 SkSL::GLCaps caps = default_caps();
294 caps.fCanUseMinAndAbsTogether = false;
295 test(r,
296 "void main() {"
297 "float x = -5.0;"
298 "x = min(abs(x), 6.0);"
299 "}",
300 caps,
301 "#version 400\n"
302 "void main() {\n"
303 " float minAbsHackVar0;\n"
304 " float minAbsHackVar1;\n"
305 " float x = -5.0;\n"
306 " x = ((minAbsHackVar0 = abs(x)) < (minAbsHackVar1 = 6.0) ? minAbsHackVar0 : "
307 "minAbsHackVar1);\n"
308 "}\n");
309}
310
ethannicholasad146f62016-10-14 06:40:02 -0700311DEF_TEST(SkSLNegatedAtan, r) {
312 test(r,
313 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }",
314 default_caps(),
315 "#version 400\n"
316 "void main() {\n"
317 " vec2 x = vec2(1.0, 2.0);\n"
318 " float y = atan(x.x, -(2.0 * x.y));\n"
319 "}\n");
320 SkSL::GLCaps caps = default_caps();
321 caps.fMustForceNegatedAtanParamToFloat = true;
322 test(r,
323 "void main() { vec2 x = vec2(1, 2); float y = atan(x.x, -(2 * x.y)); }",
324 caps,
325 "#version 400\n"
326 "void main() {\n"
327 " vec2 x = vec2(1.0, 2.0);\n"
328 " float y = atan(x.x, -1.0 * (2.0 * x.y));\n"
329 "}\n");
330}
331
ethannicholas5961bc92016-10-12 06:39:56 -0700332DEF_TEST(SkSLModifiersDeclaration, r) {
333 test(r,
334 "layout(blend_support_all_equations) out;"
335 "void main() { }",
336 default_caps(),
337 "#version 400\n"
338 "layout (blend_support_all_equations) out ;\n"
339 "void main() {\n"
340 "}\n");
341}
342
343DEF_TEST(SkSLHex, r) {
344 test(r,
345 "void main() {"
346 "int i1 = 0x0;"
347 "int i2 = 0x1234abcd;"
348 "int i3 = 0x7fffffff;"
349 "int i4 = 0xffffffff;"
350 "int i5 = -0xbeef;"
351 "uint u1 = 0x0;"
352 "uint u2 = 0x1234abcd;"
353 "uint u3 = 0x7fffffff;"
354 "uint u4 = 0xffffffff;"
355 "}",
356 default_caps(),
357 "#version 400\n"
358 "void main() {\n"
359 " int i1 = 0;\n"
360 " int i2 = 305441741;\n"
361 " int i3 = 2147483647;\n"
362 " int i4 = -1;\n"
363 " int i5 = -48879;\n"
364 " uint u1 = 0u;\n"
365 " uint u2 = 305441741u;\n"
366 " uint u3 = 2147483647u;\n"
367 " uint u4 = 4294967295u;\n"
368 "}\n");
369}
370
Ethan Nicholas4578a8e2016-11-01 11:57:42 -0400371DEF_TEST(SkSLVectorConstructors, r) {
372 test(r,
373 "vec2 v1 = vec2(1);"
374 "vec2 v2 = vec2(1, 2);"
375 "vec2 v3 = vec2(vec2(1));"
376 "vec2 v4 = vec2(vec3(1));"
377 "vec3 v5 = vec3(vec2(1), 1.0);"
378 "vec3 v6 = vec3(vec4(1, 2, 3, 4));"
379 "ivec2 v7 = ivec2(1);"
380 "ivec2 v8 = ivec2(vec2(1, 2));"
381 "vec2 v9 = vec2(ivec2(1, 2));",
382 default_caps(),
383 "#version 400\n"
384 "vec2 v1 = vec2(1.0);\n"
385 "vec2 v2 = vec2(1.0, 2.0);\n"
386 "vec2 v3 = vec2(1.0);\n"
387 "vec2 v4 = vec2(vec3(1.0));\n"
388 "vec3 v5 = vec3(vec2(1.0), 1.0);\n"
389 "vec3 v6 = vec3(vec4(1.0, 2.0, 3.0, 4.0));\n"
390 "ivec2 v7 = ivec2(1);\n"
391 "ivec2 v8 = ivec2(vec2(1.0, 2.0));\n"
392 "vec2 v9 = vec2(ivec2(1, 2));\n");
393}
394
ethannicholas5961bc92016-10-12 06:39:56 -0700395DEF_TEST(SkSLArrayConstructors, r) {
396 test(r,
397 "float test1[] = float[](1, 2, 3, 4);"
398 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));"
399 "mat4 test3[] = mat4[]();",
400 default_caps(),
401 "#version 400\n"
402 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
403 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
404 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700405}
ethannicholasddb37d62016-10-20 09:54:00 -0700406
407DEF_TEST(SkSLDerivatives, r) {
408 test(r,
409 "void main() { float x = dFdx(1); }",
410 default_caps(),
411 "#version 400\n"
412 "void main() {\n"
413 " float x = dFdx(1.0);\n"
414 "}\n");
415 SkSL::GLCaps caps = default_caps();
416 caps.fShaderDerivativeExtensionString = "GL_OES_standard_derivatives";
417 test(r,
418 "void main() { float x = 1; }",
419 caps,
420 "#version 400\n"
421 "void main() {\n"
422 " float x = 1.0;\n"
423 "}\n");
424 test(r,
425 "void main() { float x = dFdx(1); }",
426 caps,
427 "#version 400\n"
428 "#extension GL_OES_standard_derivatives : require\n"
429 "void main() {\n"
430 " float x = dFdx(1.0);\n"
431 "}\n");
432}