OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLCompiler.h" | 8 #include "SkSLCompiler.h" |
9 | 9 |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, cons
t char* expected) { | 12 static void test(skiatest::Reporter* r, const char* src, SkSL::GLCaps caps, cons
t char* expected) { |
13 SkSL::Compiler compiler; | 13 SkSL::Compiler compiler; |
14 std::string output; | 14 std::string output; |
15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &out
put); | 15 bool result = compiler.toGLSL(SkSL::Program::kFragment_Kind, src, caps, &out
put); |
16 if (!result) { | 16 if (!result) { |
17 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().
c_str()); | 17 SkDebugf("Unexpected error compiling %s\n%s", src, compiler.errorText().
c_str()); |
18 } | 18 } |
19 REPORTER_ASSERT(r, result); | 19 REPORTER_ASSERT(r, result); |
20 if (result) { | 20 if (result) { |
21 if (output != expected) { | 21 if (output != expected) { |
22 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived
:\n'%s'", src, | 22 SkDebugf("GLSL MISMATCH:\nsource:\n%s\n\nexpected:\n'%s'\n\nreceived
:\n'%s'", src, |
23 expected, output.c_str()); | 23 expected, output.c_str()); |
24 } | 24 } |
25 REPORTER_ASSERT(r, output == expected); | 25 REPORTER_ASSERT(r, output == expected); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 static 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 | |
40 DEF_TEST(SkSLHelloWorld, r) { | 29 DEF_TEST(SkSLHelloWorld, r) { |
| 30 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
41 test(r, | 31 test(r, |
42 "void main() { sk_FragColor = vec4(0.75); }", | 32 "out vec4 fragColor; void main() { fragColor = vec4(0.75); }", |
43 default_caps(), | 33 caps, |
44 "#version 400\n" | 34 "#version 400\n" |
| 35 "out vec4 fragColor;\n" |
45 "void main() {\n" | 36 "void main() {\n" |
46 " gl_FragColor = vec4(0.75);\n" | 37 " fragColor = vec4(0.75);\n" |
47 "}\n"); | 38 "}\n"); |
48 } | 39 } |
49 | 40 |
50 DEF_TEST(SkSLControl, r) { | 41 DEF_TEST(SkSLControl, r) { |
| 42 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
51 test(r, | 43 test(r, |
| 44 "out vec4 fragColor;" |
52 "void main() {" | 45 "void main() {" |
53 "if (1 + 2 + 3 > 5) { sk_FragColor = vec4(0.75); } else { discard; }" | 46 "if (1 + 2 + 3 > 5) { fragColor = vec4(0.75); } else { discard; }" |
54 "int i = 0;" | 47 "int i = 0;" |
55 "while (i < 10) sk_FragColor *= 0.5;" | 48 "while (i < 10) fragColor *= 0.5;" |
56 "do { sk_FragColor += 0.01; } while (sk_FragColor.x < 0.7);" | 49 "do { fragColor += 0.01; } while (fragColor.x < 0.7);" |
57 "for (int i = 0; i < 10; i++) {" | 50 "for (int i = 0; i < 10; i++) {" |
58 "if (i % 0 == 1) break; else continue;" | 51 "if (i % 0 == 1) break; else continue;" |
59 "}" | 52 "}" |
60 "return;" | 53 "return;" |
61 "}", | 54 "}", |
62 default_caps(), | 55 caps, |
63 "#version 400\n" | 56 "#version 400\n" |
| 57 "out vec4 fragColor;\n" |
64 "void main() {\n" | 58 "void main() {\n" |
65 " if ((1 + 2) + 3 > 5) {\n" | 59 " if ((1 + 2) + 3 > 5) {\n" |
66 " gl_FragColor = vec4(0.75);\n" | 60 " fragColor = vec4(0.75);\n" |
67 " } else {\n" | 61 " } else {\n" |
68 " discard;\n" | 62 " discard;\n" |
69 " }\n" | 63 " }\n" |
70 " int i = 0;\n" | 64 " int i = 0;\n" |
71 " while (i < 10) gl_FragColor *= 0.5;\n" | 65 " while (i < 10) fragColor *= 0.5;\n" |
72 " do {\n" | 66 " do {\n" |
73 " gl_FragColor += 0.01;\n" | 67 " fragColor += 0.01;\n" |
74 " } while (gl_FragColor.x < 0.7);\n" | 68 " } while (fragColor.x < 0.7);\n" |
75 " for (int i = 0;i < 10; i++) {\n" | 69 " for (int i = 0;i < 10; i++) {\n" |
76 " if (i % 0 == 1) break; else continue;\n" | 70 " if (i % 0 == 1) break; else continue;\n" |
77 " }\n" | 71 " }\n" |
78 " return;\n" | 72 " return;\n" |
79 "}\n"); | 73 "}\n"); |
80 } | 74 } |
81 | 75 |
82 DEF_TEST(SkSLFunctions, r) { | 76 DEF_TEST(SkSLFunctions, r) { |
| 77 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
83 test(r, | 78 test(r, |
| 79 "out vec4 fragColor;" |
84 "float foo(float v[2]) { return v[0] * v[1]; }" | 80 "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 = f
oo(y); x = z; }" | 81 "void bar(inout float x) { float y[2], z; y[0] = x; y[1] = x * 2; z = f
oo(y); x = z; }" |
86 "void main() { float x = 10; bar(x); sk_FragColor = vec4(x); }", | 82 "void main() { float x = 10; bar(x); fragColor = vec4(x); }", |
87 default_caps(), | 83 caps, |
88 "#version 400\n" | 84 "#version 400\n" |
89 "float foo(in float v[2]) {\n" | 85 "out vec4 fragColor;\n" |
| 86 "float foo(in float[2] v) {\n" |
90 " return v[0] * v[1];\n" | 87 " return v[0] * v[1];\n" |
91 "}\n" | 88 "}\n" |
92 "void bar(inout float x) {\n" | 89 "void bar(inout float x) {\n" |
93 " float y[2], z;\n" | 90 " float y[2], z;\n" |
94 " y[0] = x;\n" | 91 " y[0] = x;\n" |
95 " y[1] = x * 2.0;\n" | 92 " y[1] = x * 2;\n" |
96 " z = foo(y);\n" | 93 " z = foo(y);\n" |
97 " x = z;\n" | 94 " x = z;\n" |
98 "}\n" | 95 "}\n" |
99 "void main() {\n" | 96 "void main() {\n" |
100 " float x = 10.0;\n" | 97 " float x = 10;\n" |
101 " bar(x);\n" | 98 " bar(x);\n" |
102 " gl_FragColor = vec4(x);\n" | 99 " fragColor = vec4(x);\n" |
103 "}\n"); | 100 "}\n"); |
104 } | 101 } |
105 | 102 |
106 DEF_TEST(SkSLOperators, r) { | 103 DEF_TEST(SkSLOperators, r) { |
| 104 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
107 test(r, | 105 test(r, |
108 "void main() {" | 106 "void main() {" |
109 "float x = 1, y = 2;" | 107 "float x = 1, y = 2;" |
110 "int z = 3;" | 108 "int z = 3;" |
111 "x = x + y * z * x * (y - z);" | 109 "x = x + y * z * x * (y - z);" |
112 "y = x / y / z;" | 110 "y = x / y / z;" |
113 "z = (z / 2 % 3 << 4) >> 2 << 1;" | 111 "z = (z / 2 % 3 << 4) >> 2 << 1;" |
114 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;" | 112 "bool b = (x > 4) == x < 2 || 2 >= 5 && y <= z && 12 != 11;" |
115 "x += 12;" | 113 "x += 12;" |
116 "x -= 12;" | 114 "x -= 12;" |
117 "x *= y /= z = 10;" | 115 "x *= y /= z = 10;" |
118 "b ||= false;" | 116 "b ||= false;" |
119 "b &&= true;" | 117 "b &&= true;" |
120 "b ^^= false;" | 118 "b ^^= false;" |
121 "z |= 0;" | 119 "z |= 0;" |
122 "z &= -1;" | 120 "z &= -1;" |
123 "z ^= 0;" | 121 "z ^= 0;" |
124 "z >>= 2;" | 122 "z >>= 2;" |
125 "z <<= 4;" | 123 "z <<= 4;" |
126 "z %= 5;" | 124 "z %= 5;" |
127 "}", | 125 "}", |
128 default_caps(), | 126 caps, |
129 "#version 400\n" | 127 "#version 400\n" |
130 "void main() {\n" | 128 "void main() {\n" |
131 " float x = 1.0, y = 2.0;\n" | 129 " float x = 1, y = 2;\n" |
132 " int z = 3;\n" | 130 " int z = 3;\n" |
133 " x = x + ((y * float(z)) * x) * (y - float(z));\n" | 131 " x = x + ((y * float(z)) * x) * (y - float(z));\n" |
134 " y = (x / y) / float(z);\n" | 132 " y = (x / y) / float(z);\n" |
135 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" | 133 " z = (((z / 2) % 3 << 4) >> 2) << 1;\n" |
136 " bool b = x > 4.0 == x < 2.0 || (2 >= 5 && y <= float(z)) && 12 !=
11;\n" | 134 " bool b = x > 4 == x < 2 || (2 >= 5 && y <= float(z)) && 12 != 11;\
n" |
137 " x += 12.0;\n" | 135 " x += 12;\n" |
138 " x -= 12.0;\n" | 136 " x -= 12;\n" |
139 " x *= (y /= float(z = 10));\n" | 137 " x *= (y /= float(z = 10));\n" |
140 " b ||= false;\n" | 138 " b ||= false;\n" |
141 " b &&= true;\n" | 139 " b &&= true;\n" |
142 " b ^^= false;\n" | 140 " b ^^= false;\n" |
143 " z |= 0;\n" | 141 " z |= 0;\n" |
144 " z &= -1;\n" | 142 " z &= -1;\n" |
145 " z ^= 0;\n" | 143 " z ^= 0;\n" |
146 " z >>= 2;\n" | 144 " z >>= 2;\n" |
147 " z <<= 4;\n" | 145 " z <<= 4;\n" |
148 " z %= 5;\n" | 146 " z %= 5;\n" |
149 "}\n"); | 147 "}\n"); |
150 } | 148 } |
151 | 149 |
152 DEF_TEST(SkSLMatrices, r) { | 150 DEF_TEST(SkSLMatrices, r) { |
| 151 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
153 test(r, | 152 test(r, |
154 "void main() {" | 153 "void main() {" |
155 "mat2x4 x = mat2x4(1);" | 154 "mat2x4 x = mat2x4(1);" |
156 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" | 155 "mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));" |
157 "mat3x4 z = x * y;" | 156 "mat3x4 z = x * y;" |
158 "vec3 v1 = mat3(1) * vec3(1);" | 157 "vec3 v1 = mat3(1) * vec3(1);" |
159 "vec3 v2 = vec3(1) * mat3(1);" | 158 "vec3 v2 = vec3(1) * mat3(1);" |
160 "}", | 159 "}", |
161 default_caps(), | 160 caps, |
162 "#version 400\n" | 161 "#version 400\n" |
163 "void main() {\n" | 162 "void main() {\n" |
164 " mat2x4 x = mat2x4(1.0);\n" | 163 " mat2x4 x = mat2x4(1);\n" |
165 " mat3x2 y = mat3x2(1.0, 0.0, 0.0, 1.0, vec2(2.0, 2.0));\n" | 164 " mat3x2 y = mat3x2(1, 0, 0, 1, vec2(2, 2));\n" |
166 " mat3x4 z = x * y;\n" | 165 " mat3x4 z = x * y;\n" |
167 " vec3 v1 = mat3(1.0) * vec3(1.0);\n" | 166 " vec3 v1 = mat3(1) * vec3(1);\n" |
168 " vec3 v2 = vec3(1.0) * mat3(1.0);\n" | 167 " vec3 v2 = vec3(1) * mat3(1);\n" |
169 "}\n"); | 168 "}\n"); |
170 } | 169 } |
171 | 170 |
172 DEF_TEST(SkSLInterfaceBlock, r) { | 171 DEF_TEST(SkSLInterfaceBlock, r) { |
| 172 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
173 test(r, | 173 test(r, |
174 "uniform testBlock {" | 174 "uniform testBlock {" |
175 "float x;" | 175 "float x;" |
176 "float y[2];" | 176 "float y[2];" |
177 "layout(binding=12) mat3x2 z;" | 177 "layout(binding=12) mat3x2 z;" |
178 "bool w;" | 178 "bool w;" |
179 "};" | 179 "};" |
180 "void main() {" | 180 "void main() {" |
181 "}", | 181 "}", |
182 default_caps(), | 182 caps, |
183 "#version 400\n" | 183 "#version 400\n" |
184 "uniform testBlock {\n" | 184 "uniform testBlock {\n" |
185 " float x;\n" | 185 " float x;\n" |
186 " float[2] y;\n" | 186 " float[2] y;\n" |
187 " layout (binding = 12) mat3x2 z;\n" | 187 " layout (binding = 12)mat3x2 z;\n" |
188 " bool w;\n" | 188 " bool w;\n" |
189 "};\n" | 189 "};\n" |
190 "void main() {\n" | 190 "void main() {\n" |
191 "}\n"); | 191 "}\n"); |
192 } | 192 } |
193 | 193 |
194 DEF_TEST(SkSLStructs, r) { | 194 DEF_TEST(SkSLStructs, r) { |
| 195 SkSL::GLCaps caps = { 400, SkSL::GLCaps::kGL_Standard }; |
195 test(r, | 196 test(r, |
196 "struct A {" | 197 "struct A {" |
197 "int x;" | 198 "int x;" |
198 "int y;" | 199 "int y;" |
199 "} a1, a2;" | 200 "} a1, a2;" |
200 "A a3;" | 201 "A a3;" |
201 "struct B {" | 202 "struct B {" |
202 "float x;" | 203 "float x;" |
203 "float y[2];" | 204 "float y[2];" |
204 "layout(binding=1) A z;" | 205 "layout(binding=1) A z;" |
205 "};" | 206 "};" |
206 "B b1, b2, b3;" | 207 "B b1, b2, b3;" |
207 "void main() {" | 208 "void main() {" |
208 "}", | 209 "}", |
209 default_caps(), | 210 caps, |
210 "#version 400\n" | 211 "#version 400\n" |
211 "struct A {\n" | 212 "struct A {\n" |
212 " int x;\n" | 213 " int x;\n" |
213 " int y;\n" | 214 " int y;\n" |
214 "}\n" | 215 "}\n" |
215 " a1, a2;\n" | 216 " a1, a2;\n" |
216 "A a3;\n" | 217 "A a3;\n" |
217 "struct B {\n" | 218 "struct B {\n" |
218 " float x;\n" | 219 " float x;\n" |
219 " float[2] y;\n" | 220 " float[2] y;\n" |
220 " layout (binding = 1) A z;\n" | 221 " layout (binding = 1)A z;\n" |
221 "}\n" | 222 "}\n" |
222 " b1, b2, b3;\n" | 223 " b1, b2, b3;\n" |
223 "void main() {\n" | 224 "void main() {\n" |
224 "}\n"); | 225 "}\n"); |
| 226 |
225 } | 227 } |
226 | |
227 DEF_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 | |
251 DEF_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 | |
264 DEF_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 mediump float;\n" | |
281 "void main() {\n" | |
282 " float x = 0.75;\n" | |
283 " highp float y = 1.0;\n" | |
284 "}\n"); | |
285 } | |
286 | |
287 DEF_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 x = -5.0;\n" | |
312 " x = (abs(x) > (minAbsHackVar0 = 6.0) ? minAbsHackVar0 : abs(x));\n
" | |
313 "}\n"); | |
314 } | |
315 | |
316 DEF_TEST(SkSLModifiersDeclaration, r) { | |
317 test(r, | |
318 "layout(blend_support_all_equations) out;" | |
319 "void main() { }", | |
320 default_caps(), | |
321 "#version 400\n" | |
322 "layout (blend_support_all_equations) out ;\n" | |
323 "void main() {\n" | |
324 "}\n"); | |
325 } | |
326 | |
327 DEF_TEST(SkSLHex, r) { | |
328 test(r, | |
329 "void main() {" | |
330 "int i1 = 0x0;" | |
331 "int i2 = 0x1234abcd;" | |
332 "int i3 = 0x7fffffff;" | |
333 "int i4 = 0xffffffff;" | |
334 "int i5 = -0xbeef;" | |
335 "uint u1 = 0x0;" | |
336 "uint u2 = 0x1234abcd;" | |
337 "uint u3 = 0x7fffffff;" | |
338 "uint u4 = 0xffffffff;" | |
339 "}", | |
340 default_caps(), | |
341 "#version 400\n" | |
342 "void main() {\n" | |
343 " int i1 = 0;\n" | |
344 " int i2 = 305441741;\n" | |
345 " int i3 = 2147483647;\n" | |
346 " int i4 = -1;\n" | |
347 " int i5 = -48879;\n" | |
348 " uint u1 = 0u;\n" | |
349 " uint u2 = 305441741u;\n" | |
350 " uint u3 = 2147483647u;\n" | |
351 " uint u4 = 4294967295u;\n" | |
352 "}\n"); | |
353 } | |
354 | |
355 DEF_TEST(SkSLArrayConstructors, r) { | |
356 test(r, | |
357 "float test1[] = float[](1, 2, 3, 4);" | |
358 "vec2 test2[] = vec2[](vec2(1, 2), vec2(3, 4));" | |
359 "mat4 test3[] = mat4[]();", | |
360 default_caps(), | |
361 "#version 400\n" | |
362 "float test1[] = float[](1.0, 2.0, 3.0, 4.0);\n" | |
363 "vec2 test2[] = vec2[](vec2(1.0, 2.0), vec2(3.0, 4.0));\n" | |
364 "mat4 test3[] = mat4[]();\n"); | |
365 } | |
OLD | NEW |