blob: 0efe0b0db61efa7e807535b7ae2f8b8d40a03e5f [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
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 "src/gpu/GrDirectContextPriv.h"
9#include "src/gpu/GrGpu.h"
10#include "src/sksl/SkSLIRGenerator.h"
11#include "src/sksl/dsl/DSL.h"
12#include "src/sksl/dsl/priv/DSLWriter.h"
Ethan Nicholas1ff76092021-01-28 10:02:43 -050013#include "src/sksl/ir/SkSLIRNode.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050014
15#include "tests/Test.h"
16
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050017#include <limits>
18
Ethan Nicholas95046142021-01-07 10:57:27 -050019using namespace SkSL::dsl;
20
21class AutoDSLContext {
22public:
23 AutoDSLContext(GrGpu* gpu) {
24 Start(gpu->shaderCompiler());
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050025 DSLWriter::Instance().fMangle = false;
Ethan Nicholas95046142021-01-07 10:57:27 -050026 }
27
28 ~AutoDSLContext() {
29 End();
30 }
31};
32
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050033class ExpectError : public ErrorHandler {
34public:
35 ExpectError(skiatest::Reporter* reporter, const char* msg)
36 : fMsg(msg)
37 , fReporter(reporter) {
38 SetErrorHandler(this);
39 }
40
41 ~ExpectError() override {
42 REPORTER_ASSERT(fReporter, !fMsg);
43 SetErrorHandler(nullptr);
44 }
45
46 void handleError(const char* msg) override {
47 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
48 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
49 fMsg = nullptr;
50 }
51
52private:
53 const char* fMsg;
54 skiatest::Reporter* fReporter;
55};
56
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050057static bool whitespace_insensitive_compare(const char* a, const char* b) {
58 for (;;) {
59 while (isspace(*a)) {
60 ++a;
61 }
62 while (isspace(*b)) {
63 ++b;
64 }
65 if (*a != *b) {
66 return false;
67 }
68 if (*a == 0) {
69 return true;
70 }
71 ++a;
72 ++b;
73 }
74}
75
76static bool whitespace_insensitive_compare(DSLStatement& stmt, const char* description) {
77 return whitespace_insensitive_compare(stmt.release()->description().c_str(), description);
78}
79
Ethan Nicholas1ff76092021-01-28 10:02:43 -050080static bool whitespace_insensitive_compare(SkSL::IRNode& node, const char* description) {
81 return whitespace_insensitive_compare(node.description().c_str(), description);
82}
83
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050084DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050085 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
86 Expression e1 = 1;
87 REPORTER_ASSERT(r, e1.release()->description() == "1");
88 Expression e2 = 1.0;
89 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
90 Expression e3 = true;
91 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050092 Var a(kInt, "a");
93 Expression e4 = a;
94 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050095
96 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
97 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
98 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
99 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
100 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
101 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
102 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
103 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500104}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500105
106DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
107 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
108 Expression e1 = Float(std::numeric_limits<float>::max());
109 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
110 std::numeric_limits<float>::max());
111
112 Expression e2 = Float(std::numeric_limits<float>::min());
113 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
114 std::numeric_limits<float>::min());
115
116 Expression e3 = Float2(0);
117 REPORTER_ASSERT(r, e3.release()->description() == "float2(0.0)");
118
119 Expression e4 = Float2(-0.5, 1);
120 REPORTER_ASSERT(r, e4.release()->description() == "float2(-0.5, 1.0)");
121
122 Expression e5 = Float3(0.75);
123 REPORTER_ASSERT(r, e5.release()->description() == "float3(0.75)");
124
125 Expression e6 = Float3(Float2(0, 1), -2);
126 REPORTER_ASSERT(r, e6.release()->description() == "float3(float2(0.0, 1.0), -2.0)");
127
128 Expression e7 = Float3(0, 1, 2);
129 REPORTER_ASSERT(r, e7.release()->description() == "float3(0.0, 1.0, 2.0)");
130
131 Expression e8 = Float4(0);
132 REPORTER_ASSERT(r, e8.release()->description() == "float4(0.0)");
133
134 Expression e9 = Float4(Float2(0, 1), Float2(2, 3));
135 REPORTER_ASSERT(r, e9.release()->description() == "float4(float2(0.0, 1.0), float2(2.0, 3.0))");
136
137 Expression e10 = Float4(0, 1, Float2(2, 3));
138 REPORTER_ASSERT(r, e10.release()->description() == "float4(0.0, 1.0, float2(2.0, 3.0))");
139
140 Expression e11 = Float4(0, 1, 2, 3);
141 REPORTER_ASSERT(r, e11.release()->description() == "float4(0.0, 1.0, 2.0, 3.0)");
142
143 {
144 ExpectError error(r, "error: floating point value is infinite\n");
145 Float(std::numeric_limits<float>::infinity()).release();
146 }
147
148 {
149 ExpectError error(r, "error: floating point value is NaN\n");
150 Float(std::numeric_limits<float>::quiet_NaN()).release();
151 }
152
153 {
154 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
155 " but found 4)\n");
156 Float2(Float4(1)).release();
157 }
158
159 {
160 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
161 " but found 3)\n");
162 Float4(Float3(1)).release();
163 }
164}
165
166DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
167 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
168 Expression e1 = Half(std::numeric_limits<float>::max());
169 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
170 std::numeric_limits<float>::max());
171
172 Expression e2 = Half(std::numeric_limits<float>::min());
173 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
174 std::numeric_limits<float>::min());
175
176 Expression e3 = Half2(0);
177 REPORTER_ASSERT(r, e3.release()->description() == "half2(0.0)");
178
179 Expression e4 = Half2(-0.5, 1);
180 REPORTER_ASSERT(r, e4.release()->description() == "half2(-0.5, 1.0)");
181
182 Expression e5 = Half3(0.75);
183 REPORTER_ASSERT(r, e5.release()->description() == "half3(0.75)");
184
185 Expression e6 = Half3(Half2(0, 1), -2);
186 REPORTER_ASSERT(r, e6.release()->description() == "half3(half2(0.0, 1.0), -2.0)");
187
188 Expression e7 = Half3(0, 1, 2);
189 REPORTER_ASSERT(r, e7.release()->description() == "half3(0.0, 1.0, 2.0)");
190
191 Expression e8 = Half4(0);
192 REPORTER_ASSERT(r, e8.release()->description() == "half4(0.0)");
193
194 Expression e9 = Half4(Half2(0, 1), Half2(2, 3));
195 REPORTER_ASSERT(r, e9.release()->description() == "half4(half2(0.0, 1.0), half2(2.0, 3.0))");
196
197 Expression e10 = Half4(0, 1, Half2(2, 3));
198 REPORTER_ASSERT(r, e10.release()->description() == "half4(0.0, 1.0, half2(2.0, 3.0))");
199
200 Expression e11 = Half4(0, 1, 2, 3);
201 REPORTER_ASSERT(r, e11.release()->description() == "half4(0.0, 1.0, 2.0, 3.0)");
202
203 {
204 ExpectError error(r, "error: floating point value is infinite\n");
205 Half(std::numeric_limits<float>::infinity()).release();
206 }
207
208 {
209 ExpectError error(r, "error: floating point value is NaN\n");
210 Half(std::numeric_limits<float>::quiet_NaN()).release();
211 }
212
213 {
214 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
215 " but found 4)\n");
216 Half2(Half4(1)).release();
217 }
218
219 {
220 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
221 " but found 3)\n");
222 Half4(Half3(1)).release();
223 }
224}
225
226DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
227 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
228 Expression e1 = Int(std::numeric_limits<int32_t>::max());
229 REPORTER_ASSERT(r, e1.release()->description() == "2147483647");
230
231 Expression e2 = Int2(std::numeric_limits<int32_t>::min());
232 REPORTER_ASSERT(r, e2.release()->description() == "int2(-2147483648)");
233
234 Expression e3 = Int2(0, 1);
235 REPORTER_ASSERT(r, e3.release()->description() == "int2(0, 1)");
236
237 Expression e4 = Int3(0);
238 REPORTER_ASSERT(r, e4.release()->description() == "int3(0)");
239
240 Expression e5 = Int3(Int2(0, 1), -2);
241 REPORTER_ASSERT(r, e5.release()->description() == "int3(int2(0, 1), -2)");
242
243 Expression e6 = Int3(0, 1, 2);
244 REPORTER_ASSERT(r, e6.release()->description() == "int3(0, 1, 2)");
245
246 Expression e7 = Int4(0);
247 REPORTER_ASSERT(r, e7.release()->description() == "int4(0)");
248
249 Expression e8 = Int4(Int2(0, 1), Int2(2, 3));
250 REPORTER_ASSERT(r, e8.release()->description() == "int4(int2(0, 1), int2(2, 3))");
251
252 Expression e9 = Int4(0, 1, Int2(2, 3));
253 REPORTER_ASSERT(r, e9.release()->description() == "int4(0, 1, int2(2, 3))");
254
255 Expression e10 = Int4(0, 1, 2, 3);
256 REPORTER_ASSERT(r, e10.release()->description() == "int4(0, 1, 2, 3)");
257
258 {
259 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
260 " but found 4)\n");
261 Int2(Int4(1)).release();
262 }
263
264 {
265 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
266 " but found 3)\n");
267 Int4(Int3(1)).release();
268 }
269}
270
271DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
272 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
273 Expression e1 = Short(std::numeric_limits<int16_t>::max());
274 REPORTER_ASSERT(r, e1.release()->description() == "32767");
275
276 Expression e2 = Short2(std::numeric_limits<int16_t>::min());
277 REPORTER_ASSERT(r, e2.release()->description() == "short2(-32768)");
278
279 Expression e3 = Short2(0, 1);
280 REPORTER_ASSERT(r, e3.release()->description() == "short2(0, 1)");
281
282 Expression e4 = Short3(0);
283 REPORTER_ASSERT(r, e4.release()->description() == "short3(0)");
284
285 Expression e5 = Short3(Short2(0, 1), -2);
286 REPORTER_ASSERT(r, e5.release()->description() == "short3(short2(0, 1), -2)");
287
288 Expression e6 = Short3(0, 1, 2);
289 REPORTER_ASSERT(r, e6.release()->description() == "short3(0, 1, 2)");
290
291 Expression e7 = Short4(0);
292 REPORTER_ASSERT(r, e7.release()->description() == "short4(0)");
293
294 Expression e8 = Short4(Short2(0, 1), Short2(2, 3));
295 REPORTER_ASSERT(r, e8.release()->description() == "short4(short2(0, 1), short2(2, 3))");
296
297 Expression e9 = Short4(0, 1, Short2(2, 3));
298 REPORTER_ASSERT(r, e9.release()->description() == "short4(0, 1, short2(2, 3))");
299
300 Expression e10 = Short4(0, 1, 2, 3);
301 REPORTER_ASSERT(r, e10.release()->description() == "short4(0, 1, 2, 3)");
302
303 {
304 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
305 " but found 4)\n");
306 Short2(Short4(1)).release();
307 }
308
309 {
310 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
311 " but found 3)\n");
312 Short4(Short3(1)).release();
313 }
314}
315
316DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
317 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
318 Expression e1 = Bool2(false);
319 REPORTER_ASSERT(r, e1.release()->description() == "bool2(false)");
320
321 Expression e2 = Bool2(false, true);
322 REPORTER_ASSERT(r, e2.release()->description() == "bool2(false, true)");
323
324 Expression e3 = Bool3(false);
325 REPORTER_ASSERT(r, e3.release()->description() == "bool3(false)");
326
327 Expression e4 = Bool3(Bool2(false, true), false);
328 REPORTER_ASSERT(r, e4.release()->description() == "bool3(bool2(false, true), false)");
329
330 Expression e5 = Bool3(false, true, false);
331 REPORTER_ASSERT(r, e5.release()->description() == "bool3(false, true, false)");
332
333 Expression e6 = Bool4(false);
334 REPORTER_ASSERT(r, e6.release()->description() == "bool4(false)");
335
336 Expression e7 = Bool4(Bool2(false, true), Bool2(false, true));
337 REPORTER_ASSERT(r, e7.release()->description() == "bool4(bool2(false, true), "
338 "bool2(false, true))");
339
340 Expression e8 = Bool4(false, true, Bool2(false, true));
341 REPORTER_ASSERT(r, e8.release()->description() == "bool4(false, true, bool2(false, true))");
342
343 Expression e9 = Bool4(false, true, false, true);
344 REPORTER_ASSERT(r, e9.release()->description() == "bool4(false, true, false, true)");
345
346 {
347 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
348 " but found 4)\n");
349 Bool2(Bool4(true)).release();
350 }
351
352 {
353 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
354 " but found 3)\n");
355 Bool4(Bool3(true)).release();
356 }
357}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500358
359DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
360 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
361 Var a(kFloat, "a"), b(kFloat, "b");
362 Expression e1 = a + b;
363 REPORTER_ASSERT(r, e1.release()->description() == "(a + b)");
364
365 Expression e2 = a + 1;
366 REPORTER_ASSERT(r, e2.release()->description() == "(a + 1.0)");
367
368 Expression e3 = 0.5 + a + -99;
369 REPORTER_ASSERT(r, e3.release()->description() == "((0.5 + a) + -99.0)");
370
371 Expression e4 = a += b + 1;
372 REPORTER_ASSERT(r, e4.release()->description() == "(a += (b + 1.0))");
373
374 {
375 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
376 (Bool2(true) + a).release();
377 }
378
379 {
380 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
381 (a += Bool2(true)).release();
382 }
383
384 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500385 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500386 (1.0 += a).release();
387 }
388}
389
390DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
391 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
392 Var a(kInt, "a"), b(kInt, "b");
393 Expression e1 = a - b;
394 REPORTER_ASSERT(r, e1.release()->description() == "(a - b)");
395
396 Expression e2 = a - 1;
397 REPORTER_ASSERT(r, e2.release()->description() == "(a - 1)");
398
399 Expression e3 = 2 - a - b;
400 REPORTER_ASSERT(r, e3.release()->description() == "((2 - a) - b)");
401
402 Expression e4 = a -= b + 1;
403 REPORTER_ASSERT(r, e4.release()->description() == "(a -= (b + 1))");
404
405 {
406 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
407 (Bool2(true) - a).release();
408 }
409
410 {
411 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
412 (a -= Bool2(true)).release();
413 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500414
415 {
416 ExpectError error(r, "error: cannot assign to this expression\n");
417 (1.0 -= a).release();
418 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500419}
420
421DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
422 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
423 Var a(kFloat, "a"), b(kFloat, "b");
424 Expression e1 = a * b;
425 REPORTER_ASSERT(r, e1.release()->description() == "(a * b)");
426
427 Expression e2 = a * 1;
428 REPORTER_ASSERT(r, e2.release()->description() == "(a * 1.0)");
429
430 Expression e3 = 0.5 * a * -99;
431 REPORTER_ASSERT(r, e3.release()->description() == "((0.5 * a) * -99.0)");
432
433 Expression e4 = a *= b + 1;
434 REPORTER_ASSERT(r, e4.release()->description() == "(a *= (b + 1.0))");
435
436 {
437 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
438 (Bool2(true) * a).release();
439 }
440
441 {
442 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
443 (a *= Bool2(true)).release();
444 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500445
446 {
447 ExpectError error(r, "error: cannot assign to this expression\n");
448 (1.0 *= a).release();
449 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500450}
451
452DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
453 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
454 Var a(kFloat, "a"), b(kFloat, "b");
455 Expression e1 = a / b;
456 REPORTER_ASSERT(r, e1.release()->description() == "(a / b)");
457
458 Expression e2 = a / 1;
459 REPORTER_ASSERT(r, e2.release()->description() == "(a / 1.0)");
460
461 Expression e3 = 0.5 / a / -99;
462 REPORTER_ASSERT(r, e3.release()->description() == "((0.5 / a) / -99.0)");
463
464 Expression e4 = b / (a - 1);
465 REPORTER_ASSERT(r, e4.release()->description() == "(b / (a - 1.0))");
466
467 Expression e5 = a /= b + 1;
468 REPORTER_ASSERT(r, e5.release()->description() == "(a /= (b + 1.0))");
469
470 {
471 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
472 (Bool2(true) / a).release();
473 }
474
475 {
476 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
477 (a /= Bool2(true)).release();
478 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500479
480 {
481 ExpectError error(r, "error: cannot assign to this expression\n");
482 (1.0 /= a).release();
483 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500484
485 {
486 ExpectError error(r, "error: division by zero\n");
487 (a /= 0).release();
488 }
489
490 {
491 Var c(kFloat2, "c");
492 ExpectError error(r, "error: division by zero\n");
493 (c /= Float2(Float(0), 1)).release();
494 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500495}
496
497DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
498 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
499 Var a(kInt, "a"), b(kInt, "b");
500 Expression e1 = a % b;
501 REPORTER_ASSERT(r, e1.release()->description() == "(a % b)");
502
503 Expression e2 = a % 2;
504 REPORTER_ASSERT(r, e2.release()->description() == "(a % 2)");
505
506 Expression e3 = 10 % a % -99;
507 REPORTER_ASSERT(r, e3.release()->description() == "((10 % a) % -99)");
508
509 Expression e4 = a %= b + 1;
510 REPORTER_ASSERT(r, e4.release()->description() == "(a %= (b + 1))");
511
512 {
513 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
514 (Bool2(true) % a).release();
515 }
516
517 {
518 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
519 (a %= Bool2(true)).release();
520 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500521
522 {
523 ExpectError error(r, "error: cannot assign to this expression\n");
524 (1 %= a).release();
525 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500526
527 {
528 ExpectError error(r, "error: division by zero\n");
529 (a %= 0).release();
530 }
531
532 {
533 Var c(kInt2, "c");
534 ExpectError error(r, "error: division by zero\n");
535 (c %= Int2(Int(0), 1)).release();
536 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500537}
538
539DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
540 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
541 Var a(kInt, "a"), b(kInt, "b");
542 Expression e1 = a << b;
543 REPORTER_ASSERT(r, e1.release()->description() == "(a << b)");
544
545 Expression e2 = a << 1;
546 REPORTER_ASSERT(r, e2.release()->description() == "(a << 1)");
547
548 Expression e3 = 1 << a << 2;
549 REPORTER_ASSERT(r, e3.release()->description() == "((1 << a) << 2)");
550
551 Expression e4 = a <<= b + 1;
552 REPORTER_ASSERT(r, e4.release()->description() == "(a <<= (b + 1))");
553
554 {
555 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
556 (Bool2(true) << a).release();
557 }
558
559 {
560 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
561 (a <<= Bool2(true)).release();
562 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500563
564 {
565 ExpectError error(r, "error: cannot assign to this expression\n");
566 (1 <<= a).release();
567 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500568}
569
570DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
571 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
572 Var a(kInt, "a"), b(kInt, "b");
573 Expression e1 = a >> b;
574 REPORTER_ASSERT(r, e1.release()->description() == "(a >> b)");
575
576 Expression e2 = a >> 1;
577 REPORTER_ASSERT(r, e2.release()->description() == "(a >> 1)");
578
579 Expression e3 = 1 >> a >> 2;
580 REPORTER_ASSERT(r, e3.release()->description() == "((1 >> a) >> 2)");
581
582 Expression e4 = a >>= b + 1;
583 REPORTER_ASSERT(r, e4.release()->description() == "(a >>= (b + 1))");
584
585 {
586 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
587 (Bool2(true) >> a).release();
588 }
589
590 {
591 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
592 (a >>= Bool2(true)).release();
593 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500594
595 {
596 ExpectError error(r, "error: cannot assign to this expression\n");
597 (1 >>= a).release();
598 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500599}
600
601DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
602 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
603 Var a(kInt, "a"), b(kInt, "b");
604 Expression e1 = a & b;
605 REPORTER_ASSERT(r, e1.release()->description() == "(a & b)");
606
607 Expression e2 = a & 1;
608 REPORTER_ASSERT(r, e2.release()->description() == "(a & 1)");
609
610 Expression e3 = 1 & a & 2;
611 REPORTER_ASSERT(r, e3.release()->description() == "((1 & a) & 2)");
612
613 Expression e4 = a &= b + 1;
614 REPORTER_ASSERT(r, e4.release()->description() == "(a &= (b + 1))");
615
616 {
617 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
618 (Bool2(true) & a).release();
619 }
620
621 {
622 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
623 (a &= Bool2(true)).release();
624 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500625
626 {
627 ExpectError error(r, "error: cannot assign to this expression\n");
628 (1 &= a).release();
629 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500630}
631
632DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
633 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
634 Var a(kInt, "a"), b(kInt, "b");
635 Expression e1 = a | b;
636 REPORTER_ASSERT(r, e1.release()->description() == "(a | b)");
637
638 Expression e2 = a | 1;
639 REPORTER_ASSERT(r, e2.release()->description() == "(a | 1)");
640
641 Expression e3 = 1 | a | 2;
642 REPORTER_ASSERT(r, e3.release()->description() == "((1 | a) | 2)");
643
644 Expression e4 = a |= b + 1;
645 REPORTER_ASSERT(r, e4.release()->description() == "(a |= (b + 1))");
646
647 {
648 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
649 (Bool2(true) | a).release();
650 }
651
652 {
653 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
654 (a |= Bool2(true)).release();
655 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500656
657 {
658 ExpectError error(r, "error: cannot assign to this expression\n");
659 (1 |= a).release();
660 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500661}
662
663DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
664 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
665 Var a(kInt, "a"), b(kInt, "b");
666 Expression e1 = a ^ b;
667 REPORTER_ASSERT(r, e1.release()->description() == "(a ^ b)");
668
669 Expression e2 = a ^ 1;
670 REPORTER_ASSERT(r, e2.release()->description() == "(a ^ 1)");
671
672 Expression e3 = 1 ^ a ^ 2;
673 REPORTER_ASSERT(r, e3.release()->description() == "((1 ^ a) ^ 2)");
674
675 Expression e4 = a ^= b + 1;
676 REPORTER_ASSERT(r, e4.release()->description() == "(a ^= (b + 1))");
677
678 {
679 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
680 (Bool2(true) ^ a).release();
681 }
682
683 {
684 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
685 (a ^= Bool2(true)).release();
686 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500687
688 {
689 ExpectError error(r, "error: cannot assign to this expression\n");
690 (1 ^= a).release();
691 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500692}
693
694DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
695 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
696 Var a(kBool, "a"), b(kBool, "b");
697 Expression e1 = a && b;
698 REPORTER_ASSERT(r, e1.release()->description() == "(a && b)");
699
700 Expression e2 = a && true && b;
701 REPORTER_ASSERT(r, e2.release()->description() == "(a && b)");
702
703 Expression e3 = a && false && b;
704 REPORTER_ASSERT(r, e3.release()->description() == "false");
705
706 {
707 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
708 (a && 5).release();
709 }
710}
711
712DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
713 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
714 Var a(kBool, "a"), b(kBool, "b");
715 Expression e1 = a || b;
716 REPORTER_ASSERT(r, e1.release()->description() == "(a || b)");
717
718 Expression e2 = a || true || b;
719 REPORTER_ASSERT(r, e2.release()->description() == "true");
720
721 Expression e3 = a || false || b;
722 REPORTER_ASSERT(r, e3.release()->description() == "(a || b)");
723
724 {
725 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
726 (a || 5).release();
727 }
728}
729
730DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
731 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
732 Var a(kInt, "a"), b(kInt, "b");
733 Expression e1 = (a += b, b);
734 REPORTER_ASSERT(r, e1.release()->description() == "((a += b) , b)");
735
736 Expression e2 = (a += b, b += b, Int2(a));
737 REPORTER_ASSERT(r, e2.release()->description() == "(((a += b) , (b += b)) , int2(a))");
738}
739
740DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
741 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
742 Var a(kInt, "a"), b(kInt, "b");
743 Expression e1 = a == b;
744 REPORTER_ASSERT(r, e1.release()->description() == "(a == b)");
745
746 Expression e2 = a == 5;
747 REPORTER_ASSERT(r, e2.release()->description() == "(a == 5)");
748
749 {
750 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
751 (a == Bool2(true)).release();
752 }
753}
754
755DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
756 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
757 Var a(kInt, "a"), b(kInt, "b");
758 Expression e1 = a != b;
759 REPORTER_ASSERT(r, e1.release()->description() == "(a != b)");
760
761 Expression e2 = a != 5;
762 REPORTER_ASSERT(r, e2.release()->description() == "(a != 5)");
763
764 {
765 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
766 (a != Bool2(true)).release();
767 }
768}
769
770DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
771 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
772 Var a(kInt, "a"), b(kInt, "b");
773 Expression e1 = a > b;
774 REPORTER_ASSERT(r, e1.release()->description() == "(a > b)");
775
776 Expression e2 = a > 5;
777 REPORTER_ASSERT(r, e2.release()->description() == "(a > 5)");
778
779 {
780 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
781 (a > Bool2(true)).release();
782 }
783}
784
785DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
786 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
787 Var a(kInt, "a"), b(kInt, "b");
788 Expression e1 = a >= b;
789 REPORTER_ASSERT(r, e1.release()->description() == "(a >= b)");
790
791 Expression e2 = a >= 5;
792 REPORTER_ASSERT(r, e2.release()->description() == "(a >= 5)");
793
794 {
795 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
796 (a >= Bool2(true)).release();
797 }
798}
799
800DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
801 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
802 Var a(kInt, "a"), b(kInt, "b");
803 Expression e1 = a < b;
804 REPORTER_ASSERT(r, e1.release()->description() == "(a < b)");
805
806 Expression e2 = a < 5;
807 REPORTER_ASSERT(r, e2.release()->description() == "(a < 5)");
808
809 {
810 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
811 (a < Bool2(true)).release();
812 }
813}
814
815DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
816 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
817 Var a(kInt, "a"), b(kInt, "b");
818 Expression e1 = a <= b;
819 REPORTER_ASSERT(r, e1.release()->description() == "(a <= b)");
820
821 Expression e2 = a <= 5;
822 REPORTER_ASSERT(r, e2.release()->description() == "(a <= 5)");
823
824 {
825 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
826 (a <= Bool2(true)).release();
827 }
828}
829
830DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
831 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
832 Var a(kInt, "a"), b(kInt, "b");
833 Expression e1 = !(a <= b);
834 REPORTER_ASSERT(r, e1.release()->description() == "!(a <= b)");
835
836 {
837 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
838 (!a).release();
839 }
840}
841
842DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
843 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
844 Var a(kInt, "a"), b(kBool, "b");
845 Expression e1 = ~a;
846 REPORTER_ASSERT(r, e1.release()->description() == "~a");
847
848 {
849 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
850 (~b).release();
851 }
852}
853
854DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
855 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
856 Var a(kInt, "a"), b(kBool, "b");
857 Expression e1 = ++a;
858 REPORTER_ASSERT(r, e1.release()->description() == "++a");
859
860 Expression e2 = a++;
861 REPORTER_ASSERT(r, e2.release()->description() == "a++");
862
863 {
864 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
865 (++b).release();
866 }
867
868 {
869 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
870 (b++).release();
871 }
872
873 {
874 ExpectError error(r, "error: cannot assign to this expression\n");
875 (++(a + 1)).release();
876 }
877
878 {
879 ExpectError error(r, "error: cannot assign to this expression\n");
880 ((a + 1)++).release();
881 }
882}
883
884DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
885 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
886 Var a(kInt, "a"), b(kBool, "b");
887 Expression e1 = --a;
888 REPORTER_ASSERT(r, e1.release()->description() == "--a");
889
890 Expression e2 = a--;
891 REPORTER_ASSERT(r, e2.release()->description() == "a--");
892
893 {
894 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
895 (--b).release();
896 }
897
898 {
899 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
900 (b--).release();
901 }
902
903 {
904 ExpectError error(r, "error: cannot assign to this expression\n");
905 (--(a + 1)).release();
906 }
907
908 {
909 ExpectError error(r, "error: cannot assign to this expression\n");
910 ((a + 1)--).release();
911 }
912}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500913
914DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
915 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
916 Statement x = Block();
917 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "{ }"));
918 Var a(kInt, "a"), b(kInt, "b");
919 Statement y = Block(Declare(a, 1), Declare(b, 2), a = b);
920 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "{ int a = 1; int b = 2; (a = b); }"));
921}
922
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500923DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
924 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
925 Var i(kInt, "i");
926 DSLFunction(kVoid, "success").define(
927 For(Declare(i, 0), i < 10, ++i, Block(
928 If(i > 5, Break())
929 ))
930 );
931 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
932 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
933 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }"));
934
935 {
936 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
937 DSLFunction(kVoid, "fail").define(
938 Break()
939 );
940 }
941}
942
943DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
944 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
945 Var i(kInt, "i");
946 DSLFunction(kVoid, "success").define(
947 For(Declare(i, 0), i < 10, ++i, Block(
948 If(i < 5, Continue())
949 ))
950 );
951 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
952 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
953 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }"));
954
955 {
956 ExpectError error(r, "error: continue statement must be inside a loop\n");
957 DSLFunction(kVoid, "fail").define(
958 Continue()
959 );
960 }
961}
962
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500963DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
964 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
965 Var a(kHalf4, "a"), b(kHalf4, "b");
966 Statement x = Declare(a);
967 REPORTER_ASSERT(r, x.release()->description() == "half4 a;");
968 Statement y = Declare(b, Half4(1));
969 REPORTER_ASSERT(r, y.release()->description() == "half4 b = half4(1.0);");
970
971 {
972 Var c(kHalf4, "c");
973 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
974 Declare(c, 1).release();
975 }
976}
977
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500978DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
979 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
980 Statement x = If(Sqrt(1) > 0, Discard());
981 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "if ((sqrt(1.0) > 0.0)) discard;"));
982}
983
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500984DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
985 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
986 Statement x = Do(Block(), true);
987 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "do {} while (true);"));
988
989 Var a(kFloat, "a"), b(kFloat, "b");
990 Statement y = Do(Block(a++, --b), a != b);
991 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "do { a++; --b; } while ((a != b));"));
992
993 {
994 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
995 Do(Block(), 7).release();
996 }
997}
998
999DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
1000 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1001 Statement x = For(Statement(), Expression(), Expression(), Block());
1002 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "for (;;) {}"));
1003
1004 Var i(kInt, "i");
1005 Statement y = For(Declare(i, 0), i < 10, ++i, i += 5);
1006 REPORTER_ASSERT(r, whitespace_insensitive_compare(y,
1007 "for (int i = 0; (i < 10); ++i) (i += 5);"));
1008
1009 {
1010 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1011 For(i = 0, i + 10, ++i, i += 5).release();
1012 }
1013}
1014
Ethan Nicholase2c05042021-02-03 10:27:22 -05001015DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001016 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1017 DSLWriter::ProgramElements().clear();
1018 Var coords(kHalf2, "coords");
1019 DSLFunction(kVoid, "main", coords).define(
1020 sk_FragColor() = Half4(coords, 0, 1)
1021 );
1022 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1023 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
1024 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }"));
1025
1026 DSLWriter::ProgramElements().clear();
1027 Var x(kFloat, "x");
1028 DSLFunction(kFloat, "sqr", x).define(
1029 Return(x * x)
1030 );
1031 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1032 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
1033 "float sqr(float x) { return (x * x); }"));
1034
1035 {
1036 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
1037 DSLWriter::ProgramElements().clear();
1038 DSLFunction(kFloat, "broken").define(
1039 Return(true)
1040 );
1041 }
1042
1043 {
1044 ExpectError error(r, "error: expected function to return 'float'\n");
1045 DSLWriter::ProgramElements().clear();
1046 DSLFunction(kFloat, "broken").define(
1047 Return()
1048 );
1049 }
1050
1051 {
1052 ExpectError error(r, "error: may not return a value from a void function\n");
1053 DSLWriter::ProgramElements().clear();
1054 DSLFunction(kVoid, "broken").define(
1055 Return(0)
1056 );
1057 }
1058
1059/* TODO: detect this case
1060 {
1061 ExpectError error(r, "error: expected function to return 'float'\n");
1062 DSLWriter::ProgramElements().clear();
1063 DSLFunction(kFloat, "broken").define(
1064 );
1065 }
1066*/
1067}
1068
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001069DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1070 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1071 Var a(kFloat, "a"), b(kFloat, "b");
1072 Statement x = If(a > b, a -= b);
1073 REPORTER_ASSERT(r, x.release()->description() == "if ((a > b)) (a -= b);");
1074
1075 Statement y = If(a > b, a -= b, b -= a);
1076 REPORTER_ASSERT(r, y.release()->description() == "if ((a > b)) (a -= b); else (b -= a);");
1077
1078 {
1079 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1080 If(a + b, a -= b).release();
1081 }
1082}
1083
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001084DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1085 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1086
1087 Statement x = Return();
1088 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "return;"));
1089
1090 Statement y = Return(true);
1091 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "return true;"));
1092}
1093
Ethan Nicholascfefec02021-02-09 15:22:57 -05001094DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1095 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1096
1097 Var a(kFloat, "a"), b(kInt, "b");
1098
1099 Statement x = Switch(5,
1100 Case(0, a = 0, Break()),
1101 Case(1, a = 1, Continue()),
1102 Default(Discard())
1103 );
1104 REPORTER_ASSERT(r, whitespace_insensitive_compare(x,
1105 "switch (5) { case 0: (a = 0.0); break; case 1: (a = 1.0); continue; default: discard; }"));
1106
1107 Statement y = Switch(b);
1108 REPORTER_ASSERT(r, whitespace_insensitive_compare(y,
1109 "switch (b) {}"));
1110
1111 Statement z = Switch(b, Default(), Case(0), Case(1));
1112 REPORTER_ASSERT(r, whitespace_insensitive_compare(z,
1113 "switch (b) { default: case 0: case 1: }"));
1114
1115 {
1116 ExpectError error(r, "error: duplicate case value\n");
1117 Switch(0, Case(0), Case(0)).release();
1118 }
1119
1120 {
1121 ExpectError error(r, "error: case value must be a constant integer\n");
1122 Var b(kInt);
1123 Switch(0, Case(b)).release();
1124 }
1125
1126 {
1127 ExpectError error(r, "error: continue statement must be inside a loop\n");
1128 DSLFunction(kVoid, "fail").define(
1129 Switch(5,
1130 Case(0, a = 0, Break()),
1131 Case(1, a = 1, Continue()),
1132 Default(Discard())
1133 )
1134 );
1135 }
1136}
1137
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001138DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1139 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1140 Var a(kFloat4, "a");
1141
1142 Expression e1 = a.x();
1143 REPORTER_ASSERT(r, e1.release()->description() == "a.x");
1144
1145 Expression e2 = a.y();
1146 REPORTER_ASSERT(r, e2.release()->description() == "a.y");
1147
1148 Expression e3 = a.z();
1149 REPORTER_ASSERT(r, e3.release()->description() == "a.z");
1150
1151 Expression e4 = a.w();
1152 REPORTER_ASSERT(r, e4.release()->description() == "a.w");
1153
1154 Expression e5 = a.r();
1155 REPORTER_ASSERT(r, e5.release()->description() == "a.x");
1156
1157 Expression e6 = a.g();
1158 REPORTER_ASSERT(r, e6.release()->description() == "a.y");
1159
1160 Expression e7 = a.b();
1161 REPORTER_ASSERT(r, e7.release()->description() == "a.z");
1162
1163 Expression e8 = a.a();
1164 REPORTER_ASSERT(r, e8.release()->description() == "a.w");
1165
1166 Expression e9 = Swizzle(a, R);
1167 REPORTER_ASSERT(r, e9.release()->description() == "a.x");
1168
1169 Expression e10 = Swizzle(a, ZERO, G);
1170 REPORTER_ASSERT(r, e10.release()->description() == "float2(a.y, float(0)).yx");
1171
1172 Expression e11 = Swizzle(a, B, G, G);
1173 REPORTER_ASSERT(r, e11.release()->description() == "a.zyy");
1174
1175 Expression e12 = Swizzle(a, R, G, B, ONE);
1176 REPORTER_ASSERT(r, e12.release()->description() == "float4(a.xyz, float(1))");
1177
1178 Expression e13 = Swizzle(a, R, G, B, ONE).r();
1179 REPORTER_ASSERT(r, e13.release()->description() == "float4(a.xyz, float(1)).x");
1180}
1181
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001182DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLTernary, r, ctxInfo) {
1183 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1184 Var a(kInt, "a");
1185 Expression x = Ternary(a > 0, 1, -1);
1186 REPORTER_ASSERT(r, x.release()->description() == "((a > 0) ? 1 : -1)");
1187
1188 {
1189 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1190 Ternary(a, 1, -1).release();
1191 }
1192
1193 {
1194 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
1195 Ternary(a > 0, Float2(1), Float3(1)).release();
1196 }
1197}
1198
1199DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1200 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1201 Statement x = While(true, Block());
1202 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "for (; true;) {}"));
1203
1204 Var a(kFloat, "a"), b(kFloat, "b");
1205 Statement y = While(a != b, Block(a++, --b));
1206 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "for (; (a != b);) { a++; --b; }"));
1207
1208 {
1209 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1210 While(7, Block()).release();
1211 }
1212}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001213
1214DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1215 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1216 Var a(Array(kInt, 5), "a"), b(kInt, "b");
1217 Expression e1 = a[0];
1218 REPORTER_ASSERT(r, e1.release()->description() == "a[0]");
1219 Expression e2 = a[b];
1220 REPORTER_ASSERT(r, e2.release()->description() == "a[b]");
1221
1222 {
1223 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
1224 a[true].release();
1225 }
1226
1227 {
1228 ExpectError error(r, "error: expected array, but found 'int'\n");
1229 b[0].release();
1230 }
1231
1232 {
1233 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
1234 a[-1].release();
1235 }
1236}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001237
1238DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1239 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1240 // There is a Fract type on Mac which can conflict with our Fract builtin
1241 using SkSL::dsl::Fract;
1242 Var a(kHalf4, "a"), b(kHalf4, "b"), c(kHalf4, "c");
1243 Var h3(kHalf3, "h3");
1244 Var b4(kBool4, "b4");
1245 REPORTER_ASSERT(r, Abs(a).release()->description() == "abs(a)");
1246 REPORTER_ASSERT(r, All(b4).release()->description() == "all(b4)");
1247 REPORTER_ASSERT(r, Any(b4).release()->description() == "any(b4)");
1248 REPORTER_ASSERT(r, Ceil(a).release()->description() == "ceil(a)");
1249 REPORTER_ASSERT(r, Clamp(a, 0, 1).release()->description() == "clamp(a, 0.0, 1.0)");
1250 REPORTER_ASSERT(r, Cos(a).release()->description() == "cos(a)");
1251 REPORTER_ASSERT(r, Cross(h3, h3).release()->description() == "cross(h3, h3)");
1252 REPORTER_ASSERT(r, Degrees(a).release()->description() == "degrees(a)");
1253 REPORTER_ASSERT(r, Distance(a, b).release()->description() == "distance(a, b)");
1254 REPORTER_ASSERT(r, Dot(a, b).release()->description() == "dot(a, b)");
1255 REPORTER_ASSERT(r, Equal(a, b).release()->description() == "equal(a, b)");
1256 REPORTER_ASSERT(r, Exp(a).release()->description() == "exp(a)");
1257 REPORTER_ASSERT(r, Exp2(a).release()->description() == "exp2(a)");
1258 REPORTER_ASSERT(r, Faceforward(a, b, c).release()->description() == "faceforward(a, b, c)");
1259 REPORTER_ASSERT(r, Floor(a).release()->description() == "floor(a)");
1260 REPORTER_ASSERT(r, Fract(a).release()->description() == "fract(a)");
1261 REPORTER_ASSERT(r, GreaterThan(a, b).release()->description() == "greaterThan(a, b)");
1262 REPORTER_ASSERT(r, GreaterThanEqual(a, b).release()->description() == "greaterThanEqual(a, b)");
1263 REPORTER_ASSERT(r, Inversesqrt(a).release()->description() == "inversesqrt(a)");
1264 REPORTER_ASSERT(r, LessThan(a, b).release()->description() == "lessThan(a, b)");
1265 REPORTER_ASSERT(r, LessThanEqual(a, b).release()->description() == "lessThanEqual(a, b)");
1266 REPORTER_ASSERT(r, Length(a).release()->description() == "length(a)");
1267 REPORTER_ASSERT(r, Log(a).release()->description() == "log(a)");
1268 REPORTER_ASSERT(r, Log2(a).release()->description() == "log2(a)");
1269 REPORTER_ASSERT(r, Max(a, b).release()->description() == "max(a, b)");
1270 REPORTER_ASSERT(r, Min(a, b).release()->description() == "min(a, b)");
1271 REPORTER_ASSERT(r, Mix(a, b, c).release()->description() == "mix(a, b, c)");
1272 REPORTER_ASSERT(r, Mod(a, b).release()->description() == "mod(a, b)");
1273 REPORTER_ASSERT(r, Normalize(a).release()->description() == "normalize(a)");
1274 REPORTER_ASSERT(r, NotEqual(a, b).release()->description() == "notEqual(a, b)");
1275 REPORTER_ASSERT(r, Pow(a, b).release()->description() == "pow(a, b)");
1276 REPORTER_ASSERT(r, Radians(a).release()->description() == "radians(a)");
1277 REPORTER_ASSERT(r, Reflect(a, b).release()->description() == "reflect(a, b)");
1278 REPORTER_ASSERT(r, Refract(a, b, 1).release()->description() == "refract(a, b, 1.0)");
1279 REPORTER_ASSERT(r, Saturate(a).release()->description() == "saturate(a)");
1280 REPORTER_ASSERT(r, Sign(a).release()->description() == "sign(a)");
1281 REPORTER_ASSERT(r, Sin(a).release()->description() == "sin(a)");
1282 REPORTER_ASSERT(r, Smoothstep(a, b, c).release()->description() == "smoothstep(a, b, c)");
1283 REPORTER_ASSERT(r, Sqrt(a).release()->description() == "sqrt(a)");
1284 REPORTER_ASSERT(r, Step(a, b).release()->description() == "step(a, b)");
1285 REPORTER_ASSERT(r, Tan(a).release()->description() == "tan(a)");
1286 REPORTER_ASSERT(r, Unpremul(a).release()->description() == "unpremul(a)");
1287
1288 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1289 // one of them reports errors correctly
1290 {
1291 ExpectError error(r, "error: no match for ceil(bool)\n");
1292 Ceil(a == b).release();
1293 }
1294}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001295
1296DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
1297 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1298
1299 Var v1(kConst_Modifier, kInt, "v1");
1300 Statement d1 = Declare(v1);
1301 REPORTER_ASSERT(r, d1.release()->description() == "const int v1;");
1302
1303 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1304 // context, so we can't as yet Declare() variables with these modifiers.
1305 // TODO: better tests when able
1306 Var v2(kIn_Modifier, kInt, "v2");
1307 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
1308
1309 Var v3(kOut_Modifier, kInt, "v3");
1310 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
1311
1312 Var v4(kUniform_Modifier, kInt, "v4");
1313 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
1314
1315 Var v5(kFlat_Modifier, kInt, "v5");
1316 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
1317
1318 Var v6(kNoPerspective_Modifier, kInt, "v6");
1319 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1320 SkSL::Modifiers::kNoPerspective_Flag);
1321
1322 Var v7(kIn_Modifier | kOut_Modifier, kInt, "v7");
1323 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1324 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
1325
1326 Var v8(kInOut_Modifier, kInt, "v8");
1327 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags ==
1328 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
1329}