blob: dedddad1f8a6a9ce07daebcf15e33d4f61227256 [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
ethannicholasccb1dd82016-10-11 08:47:04 -070029static SkSL::GLCaps default_caps() {
30 return {
31 400,
32 SkSL::GLCaps::kGL_Standard,
33 false, // isCoreProfile
34 false, // usesPrecisionModifiers;
35 false, // mustDeclareFragmentShaderOutput
36 true // canUseMinAndAbsTogether
37 };
38}
39
ethannicholasf789b382016-08-03 12:43:36 -070040DEF_TEST(SkSLHelloWorld, r) {
ethannicholasf789b382016-08-03 12:43:36 -070041 test(r,
ethannicholasccb1dd82016-10-11 08:47:04 -070042 "void main() { sk_FragColor = vec4(0.75); }",
43 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070044 "#version 400\n"
ethannicholasf789b382016-08-03 12:43:36 -070045 "void main() {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070046 " gl_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070047 "}\n");
48}
49
50DEF_TEST(SkSLControl, r) {
ethannicholasf789b382016-08-03 12:43:36 -070051 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070052 "void main() {"
ethannicholasccb1dd82016-10-11 08:47:04 -070053 "if (1 + 2 + 3 > 5) { sk_FragColor = vec4(0.75); } else { discard; }"
ethannicholasf789b382016-08-03 12:43:36 -070054 "int i = 0;"
ethannicholasccb1dd82016-10-11 08:47:04 -070055 "while (i < 10) sk_FragColor *= 0.5;"
56 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.7);"
ethannicholasf789b382016-08-03 12:43:36 -070057 "for (int i = 0; i < 10; i++) {"
58 "if (i % 0 == 1) break; else continue;"
59 "}"
60 "return;"
61 "}",
ethannicholasccb1dd82016-10-11 08:47:04 -070062 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070063 "#version 400\n"
ethannicholasf789b382016-08-03 12:43:36 -070064 "void main() {\n"
65 " if ((1 + 2) + 3 > 5) {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070066 " gl_FragColor = vec4(0.75);\n"
ethannicholasf789b382016-08-03 12:43:36 -070067 " } else {\n"
68 " discard;\n"
69 " }\n"
70 " int i = 0;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070071 " while (i < 10) gl_FragColor *= 0.5;\n"
ethannicholasf789b382016-08-03 12:43:36 -070072 " do {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070073 " gl_FragColor += 0.01;\n"
74 " } while (gl_FragColor.x < 0.7);\n"
ethannicholasf789b382016-08-03 12:43:36 -070075 " for (int i = 0;i < 10; i++) {\n"
76 " if (i % 0 == 1) break; else continue;\n"
77 " }\n"
78 " return;\n"
79 "}\n");
80}
81
82DEF_TEST(SkSLFunctions, r) {
ethannicholasf789b382016-08-03 12:43:36 -070083 test(r,
ethannicholasf789b382016-08-03 12:43:36 -070084 "float foo(float v[2]) { return v[0] * v[1]; }"
85 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = foo(y); x = z; }"
ethannicholasccb1dd82016-10-11 08:47:04 -070086 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }",
87 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -070088 "#version 400\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070089 "float foo(in float v[2]) {\n"
ethannicholasf789b382016-08-03 12:43:36 -070090 " return v[0] * v[1];\n"
91 "}\n"
92 "void bar(inout float x) {\n"
93 " float y[2], z;\n"
94 " y[0] = x;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -070095 " y[1] = x * 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -070096 " z = foo(y);\n"
97 " x = z;\n"
98 "}\n"
99 "void main() {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700100 " float x = 10.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700101 " bar(x);\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700102 " gl_FragColor = vec4(x);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700103 "}\n");
104}
105
106DEF_TEST(SkSLOperators, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700107 test(r,
108 "void main() {"
109 "float x = 1, y = 2;"
110 "int z = 3;"
111 "x = x + y * z * x * (y - z);"
112 "y = x / y / z;"
113 "z = (z / 2 % 3 << 4) >> 2 << 1;"
114 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;"
115 "x += 12;"
116 "x -= 12;"
117 "x *= y /= z = 10;"
118 "b ||= false;"
119 "b &&= true;"
120 "b ^^= false;"
121 "z |= 0;"
122 "z &= -1;"
123 "z ^= 0;"
124 "z >>= 2;"
125 "z <<= 4;"
126 "z %= 5;"
127 "}",
ethannicholasccb1dd82016-10-11 08:47:04 -0700128 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700129 "#version 400\n"
130 "void main() {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700131 " float x = 1.0, y = 2.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700132 " int z = 3;\n"
133 " x = x + ((y * float(z)) * x) * (y - float(z));\n"
134 " y = (x / y) / float(z);\n"
135 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700136 " bool b = x > 4.0 == x < 2.0 || (2 >= 5 && y <= float(z)) && 12 != 11;\n"
137 " x += 12.0;\n"
138 " x -= 12.0;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700139 " x *= (y /= float(z = 10));\n"
140 " b ||= false;\n"
141 " b &&= true;\n"
142 " b ^^= false;\n"
143 " z |= 0;\n"
144 " z &= -1;\n"
145 " z ^= 0;\n"
146 " z >>= 2;\n"
147 " z <<= 4;\n"
148 " z %= 5;\n"
149 "}\n");
150}
151
152DEF_TEST(SkSLMatrices, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700153 test(r,
154 "void main() {"
155 "mat2x4 x = mat2x4(1);"
156 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));"
157 "mat3x4 z = x * y;"
158 "vec3 v1 = mat3(1) * vec3(1);"
159 "vec3 v2 = vec3(1) * mat3(1);"
160 "}",
ethannicholasccb1dd82016-10-11 08:47:04 -0700161 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700162 "#version 400\n"
163 "void main() {\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700164 " mat2x4 x = mat2x4(1.0);\n"
165 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n"
ethannicholasf789b382016-08-03 12:43:36 -0700166 " mat3x4 z = x * y;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700167 " vec3 v1 = mat3(1.0) * vec3(1.0);\n"
168 " vec3 v2 = vec3(1.0) * mat3(1.0);\n"
ethannicholasf789b382016-08-03 12:43:36 -0700169 "}\n");
170}
171
172DEF_TEST(SkSLInterfaceBlock, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700173 test(r,
174 "uniform testBlock {"
175 "float x;"
176 "float y[2];"
177 "layout(binding=12) mat3x2 z;"
178 "bool w;"
179 "};"
180 "void main() {"
181 "}",
ethannicholasccb1dd82016-10-11 08:47:04 -0700182 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700183 "#version 400\n"
184 "uniform testBlock {\n"
185 " float x;\n"
186 " float[2] y;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700187 " layout (binding = 12) mat3x2 z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700188 " bool w;\n"
189 "};\n"
190 "void main() {\n"
191 "}\n");
192}
193
194DEF_TEST(SkSLStructs, r) {
ethannicholasf789b382016-08-03 12:43:36 -0700195 test(r,
196 "struct A {"
197 "int x;"
198 "int y;"
199 "} a1, a2;"
200 "A a3;"
201 "struct B {"
202 "float x;"
203 "float y[2];"
204 "layout(binding=1) A z;"
205 "};"
206 "B b1, b2, b3;"
207 "void main() {"
208 "}",
ethannicholasccb1dd82016-10-11 08:47:04 -0700209 default_caps(),
ethannicholasf789b382016-08-03 12:43:36 -0700210 "#version 400\n"
211 "struct A {\n"
212 " int x;\n"
213 " int y;\n"
214 "}\n"
215 " a1, a2;\n"
216 "A a3;\n"
217 "struct B {\n"
218 " float x;\n"
219 " float[2] y;\n"
ethannicholasccb1dd82016-10-11 08:47:04 -0700220 " layout (binding = 1) A z;\n"
ethannicholasf789b382016-08-03 12:43:36 -0700221 "}\n"
222 " b1, b2, b3;\n"
223 "void main() {\n"
224 "}\n");
ethannicholasccb1dd82016-10-11 08:47:04 -0700225}
ethannicholasf789b382016-08-03 12:43:36 -0700226
ethannicholasccb1dd82016-10-11 08:47:04 -0700227DEF_TEST(SkSLVersion, r) {
228 SkSL::GLCaps caps = default_caps();
229 caps.fVersion = 450;
230 caps.fIsCoreProfile = true;
231 test(r,
232 "in float test; void main() { sk_FragColor = vec4(0.75); }",
233 caps,
234 "#version 450 core\n"
235 "in float test;\n"
236 "void main() {\n"
237 " gl_FragColor = vec4(0.75);\n"
238 "}\n");
239 caps.fVersion = 110;
240 caps.fIsCoreProfile = false;
241 test(r,
242 "in float test; void main() { sk_FragColor = vec4(0.75); }",
243 caps,
244 "#version 110\n"
245 "varying float test;\n"
246 "void main() {\n"
247 " gl_FragColor = vec4(0.75);\n"
248 "}\n");
249}
250
251DEF_TEST(SkSLDeclareOutput, r) {
252 SkSL::GLCaps caps = default_caps();
253 caps.fMustDeclareFragmentShaderOutput = true;
254 test(r,
255 "void main() { sk_FragColor = vec4(0.75); }",
256 caps,
257 "#version 400\n"
258 "out vec4 sk_FragColor;\n"
259 "void main() {\n"
260 " sk_FragColor = vec4(0.75);\n"
261 "}\n");
262}
263
264DEF_TEST(SkSLUsesPrecisionModifiers, r) {
265 SkSL::GLCaps caps = default_caps();
266 test(r,
267 "void main() { float x = 0.75; highp float y = 1; }",
268 caps,
269 "#version 400\n"
270 "void main() {\n"
271 " float x = 0.75;\n"
272 " float y = 1.0;\n"
273 "}\n");
274 caps.fStandard = SkSL::GLCaps::kGLES_Standard;
275 caps.fUsesPrecisionModifiers = true;
276 test(r,
277 "void main() { float x = 0.75; highp float y = 1; }",
278 caps,
279 "#version 400 es\n"
280 "precision highp float;\n"
281 "void main() {\n"
282 " float x = 0.75;\n"
283 " highp float y = 1.0;\n"
284 "}\n");
285}
286
287DEF_TEST(SkSLMinAbs, r) {
288 test(r,
289 "void main() {"
290 "float x = -5;"
291 "x = min(abs(x), 6);"
292 "}",
293 default_caps(),
294 "#version 400\n"
295 "void main() {\n"
296 " float x = -5.0;\n"
297 " x = min(abs(x), 6.0);\n"
298 "}\n");
299
300 SkSL::GLCaps caps = default_caps();
301 caps.fCanUseMinAndAbsTogether = false;
302 test(r,
303 "void main() {"
304 "float x = -5.0;"
305 "x = min(abs(x), 6.0);"
306 "}",
307 caps,
308 "#version 400\n"
309 "void main() {\n"
310 " float minAbsHackVar0;\n"
311 " float minAbsHackVar1;\n"
312 " float x = -5.0;\n"
313 " x = ((minAbsHackVar0 = abs(x)) < (minAbsHackVar1 = 6.0) ? minAbsHackVar0 : "
314 "minAbsHackVar1);\n"
315 "}\n");
316}
317
318DEF_TEST(SkSLModifiersDeclaration, r) {
319 test(r,
320 "layout(blend_support_all_equations) out;"
321 "void main() { }",
322 default_caps(),
323 "#version 400\n"
324 "layout (blend_support_all_equations) out ;\n"
325 "void main() {\n"
326 "}\n");
327}
328
329DEF_TEST(SkSLHex, r) {
330 test(r,
331 "void main() {"
332 "int i1 = 0x0;"
333 "int i2 = 0x1234abcd;"
334 "int i3 = 0x7fffffff;"
335 "int i4 = 0xffffffff;"
336 "int i5 = -0xbeef;"
337 "uint u1 = 0x0;"
338 "uint u2 = 0x1234abcd;"
339 "uint u3 = 0x7fffffff;"
340 "uint u4 = 0xffffffff;"
341 "}",
342 default_caps(),
343 "#version 400\n"
344 "void main() {\n"
345 " int i1 = 0;\n"
346 " int i2 = 305441741;\n"
347 " int i3 = 2147483647;\n"
348 " int i4 = -1;\n"
349 " int i5 = -48879;\n"
350 " uint u1 = 0u;\n"
351 " uint u2 = 305441741u;\n"
352 " uint u3 = 2147483647u;\n"
353 " uint u4 = 4294967295u;\n"
354 "}\n");
355}
356
357DEF_TEST(SkSLArrayConstructors, r) {
358 test(r,
359 "float test1[] = float[](1, 2, 3, 4);"
360 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));"
361 "mat4 test3[] = mat4[]();",
362 default_caps(),
363 "#version 400\n"
364 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n"
365 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n"
366 "mat4 test3[] = mat4[]();\n");
ethannicholasf789b382016-08-03 12:43:36 -0700367}