blob: 2850b0b07b7ac2ee6b0064425ee0c4162c67ac03 [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 Nicholas92969f22021-01-13 10:38:59 -0500484}
485
486DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
487 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
488 Var a(kInt, "a"), b(kInt, "b");
489 Expression e1 = a % b;
490 REPORTER_ASSERT(r, e1.release()->description() == "(a % b)");
491
492 Expression e2 = a % 2;
493 REPORTER_ASSERT(r, e2.release()->description() == "(a % 2)");
494
495 Expression e3 = 10 % a % -99;
496 REPORTER_ASSERT(r, e3.release()->description() == "((10 % a) % -99)");
497
498 Expression e4 = a %= b + 1;
499 REPORTER_ASSERT(r, e4.release()->description() == "(a %= (b + 1))");
500
501 {
502 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
503 (Bool2(true) % a).release();
504 }
505
506 {
507 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
508 (a %= Bool2(true)).release();
509 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500510
511 {
512 ExpectError error(r, "error: cannot assign to this expression\n");
513 (1 %= a).release();
514 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500515}
516
517DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
518 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
519 Var a(kInt, "a"), b(kInt, "b");
520 Expression e1 = a << b;
521 REPORTER_ASSERT(r, e1.release()->description() == "(a << b)");
522
523 Expression e2 = a << 1;
524 REPORTER_ASSERT(r, e2.release()->description() == "(a << 1)");
525
526 Expression e3 = 1 << a << 2;
527 REPORTER_ASSERT(r, e3.release()->description() == "((1 << a) << 2)");
528
529 Expression e4 = a <<= b + 1;
530 REPORTER_ASSERT(r, e4.release()->description() == "(a <<= (b + 1))");
531
532 {
533 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
534 (Bool2(true) << a).release();
535 }
536
537 {
538 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
539 (a <<= Bool2(true)).release();
540 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500541
542 {
543 ExpectError error(r, "error: cannot assign to this expression\n");
544 (1 <<= a).release();
545 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500546}
547
548DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
549 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
550 Var a(kInt, "a"), b(kInt, "b");
551 Expression e1 = a >> b;
552 REPORTER_ASSERT(r, e1.release()->description() == "(a >> b)");
553
554 Expression e2 = a >> 1;
555 REPORTER_ASSERT(r, e2.release()->description() == "(a >> 1)");
556
557 Expression e3 = 1 >> a >> 2;
558 REPORTER_ASSERT(r, e3.release()->description() == "((1 >> a) >> 2)");
559
560 Expression e4 = a >>= b + 1;
561 REPORTER_ASSERT(r, e4.release()->description() == "(a >>= (b + 1))");
562
563 {
564 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
565 (Bool2(true) >> a).release();
566 }
567
568 {
569 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
570 (a >>= Bool2(true)).release();
571 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500572
573 {
574 ExpectError error(r, "error: cannot assign to this expression\n");
575 (1 >>= a).release();
576 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500577}
578
579DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
580 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
581 Var a(kInt, "a"), b(kInt, "b");
582 Expression e1 = a & b;
583 REPORTER_ASSERT(r, e1.release()->description() == "(a & b)");
584
585 Expression e2 = a & 1;
586 REPORTER_ASSERT(r, e2.release()->description() == "(a & 1)");
587
588 Expression e3 = 1 & a & 2;
589 REPORTER_ASSERT(r, e3.release()->description() == "((1 & a) & 2)");
590
591 Expression e4 = a &= b + 1;
592 REPORTER_ASSERT(r, e4.release()->description() == "(a &= (b + 1))");
593
594 {
595 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
596 (Bool2(true) & a).release();
597 }
598
599 {
600 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
601 (a &= Bool2(true)).release();
602 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500603
604 {
605 ExpectError error(r, "error: cannot assign to this expression\n");
606 (1 &= a).release();
607 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500608}
609
610DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
611 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
612 Var a(kInt, "a"), b(kInt, "b");
613 Expression e1 = a | b;
614 REPORTER_ASSERT(r, e1.release()->description() == "(a | b)");
615
616 Expression e2 = a | 1;
617 REPORTER_ASSERT(r, e2.release()->description() == "(a | 1)");
618
619 Expression e3 = 1 | a | 2;
620 REPORTER_ASSERT(r, e3.release()->description() == "((1 | a) | 2)");
621
622 Expression e4 = a |= b + 1;
623 REPORTER_ASSERT(r, e4.release()->description() == "(a |= (b + 1))");
624
625 {
626 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
627 (Bool2(true) | a).release();
628 }
629
630 {
631 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
632 (a |= Bool2(true)).release();
633 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500634
635 {
636 ExpectError error(r, "error: cannot assign to this expression\n");
637 (1 |= a).release();
638 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500639}
640
641DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
642 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
643 Var a(kInt, "a"), b(kInt, "b");
644 Expression e1 = a ^ b;
645 REPORTER_ASSERT(r, e1.release()->description() == "(a ^ b)");
646
647 Expression e2 = a ^ 1;
648 REPORTER_ASSERT(r, e2.release()->description() == "(a ^ 1)");
649
650 Expression e3 = 1 ^ a ^ 2;
651 REPORTER_ASSERT(r, e3.release()->description() == "((1 ^ a) ^ 2)");
652
653 Expression e4 = a ^= b + 1;
654 REPORTER_ASSERT(r, e4.release()->description() == "(a ^= (b + 1))");
655
656 {
657 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
658 (Bool2(true) ^ a).release();
659 }
660
661 {
662 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
663 (a ^= Bool2(true)).release();
664 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500665
666 {
667 ExpectError error(r, "error: cannot assign to this expression\n");
668 (1 ^= a).release();
669 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500670}
671
672DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
673 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
674 Var a(kBool, "a"), b(kBool, "b");
675 Expression e1 = a && b;
676 REPORTER_ASSERT(r, e1.release()->description() == "(a && b)");
677
678 Expression e2 = a && true && b;
679 REPORTER_ASSERT(r, e2.release()->description() == "(a && b)");
680
681 Expression e3 = a && false && b;
682 REPORTER_ASSERT(r, e3.release()->description() == "false");
683
684 {
685 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
686 (a && 5).release();
687 }
688}
689
690DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
691 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
692 Var a(kBool, "a"), b(kBool, "b");
693 Expression e1 = a || b;
694 REPORTER_ASSERT(r, e1.release()->description() == "(a || b)");
695
696 Expression e2 = a || true || b;
697 REPORTER_ASSERT(r, e2.release()->description() == "true");
698
699 Expression e3 = a || false || b;
700 REPORTER_ASSERT(r, e3.release()->description() == "(a || b)");
701
702 {
703 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
704 (a || 5).release();
705 }
706}
707
708DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
709 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
710 Var a(kInt, "a"), b(kInt, "b");
711 Expression e1 = (a += b, b);
712 REPORTER_ASSERT(r, e1.release()->description() == "((a += b) , b)");
713
714 Expression e2 = (a += b, b += b, Int2(a));
715 REPORTER_ASSERT(r, e2.release()->description() == "(((a += b) , (b += b)) , int2(a))");
716}
717
718DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
719 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
720 Var a(kInt, "a"), b(kInt, "b");
721 Expression e1 = a == b;
722 REPORTER_ASSERT(r, e1.release()->description() == "(a == b)");
723
724 Expression e2 = a == 5;
725 REPORTER_ASSERT(r, e2.release()->description() == "(a == 5)");
726
727 {
728 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
729 (a == Bool2(true)).release();
730 }
731}
732
733DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
734 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
735 Var a(kInt, "a"), b(kInt, "b");
736 Expression e1 = a != b;
737 REPORTER_ASSERT(r, e1.release()->description() == "(a != b)");
738
739 Expression e2 = a != 5;
740 REPORTER_ASSERT(r, e2.release()->description() == "(a != 5)");
741
742 {
743 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
744 (a != Bool2(true)).release();
745 }
746}
747
748DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
749 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
750 Var a(kInt, "a"), b(kInt, "b");
751 Expression e1 = a > b;
752 REPORTER_ASSERT(r, e1.release()->description() == "(a > b)");
753
754 Expression e2 = a > 5;
755 REPORTER_ASSERT(r, e2.release()->description() == "(a > 5)");
756
757 {
758 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
759 (a > Bool2(true)).release();
760 }
761}
762
763DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
764 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
765 Var a(kInt, "a"), b(kInt, "b");
766 Expression e1 = a >= b;
767 REPORTER_ASSERT(r, e1.release()->description() == "(a >= b)");
768
769 Expression e2 = a >= 5;
770 REPORTER_ASSERT(r, e2.release()->description() == "(a >= 5)");
771
772 {
773 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
774 (a >= Bool2(true)).release();
775 }
776}
777
778DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
779 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
780 Var a(kInt, "a"), b(kInt, "b");
781 Expression e1 = a < b;
782 REPORTER_ASSERT(r, e1.release()->description() == "(a < b)");
783
784 Expression e2 = a < 5;
785 REPORTER_ASSERT(r, e2.release()->description() == "(a < 5)");
786
787 {
788 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
789 (a < Bool2(true)).release();
790 }
791}
792
793DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
794 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
795 Var a(kInt, "a"), b(kInt, "b");
796 Expression e1 = a <= b;
797 REPORTER_ASSERT(r, e1.release()->description() == "(a <= b)");
798
799 Expression e2 = a <= 5;
800 REPORTER_ASSERT(r, e2.release()->description() == "(a <= 5)");
801
802 {
803 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
804 (a <= Bool2(true)).release();
805 }
806}
807
808DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
809 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
810 Var a(kInt, "a"), b(kInt, "b");
811 Expression e1 = !(a <= b);
812 REPORTER_ASSERT(r, e1.release()->description() == "!(a <= b)");
813
814 {
815 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
816 (!a).release();
817 }
818}
819
820DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
821 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
822 Var a(kInt, "a"), b(kBool, "b");
823 Expression e1 = ~a;
824 REPORTER_ASSERT(r, e1.release()->description() == "~a");
825
826 {
827 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
828 (~b).release();
829 }
830}
831
832DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
833 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
834 Var a(kInt, "a"), b(kBool, "b");
835 Expression e1 = ++a;
836 REPORTER_ASSERT(r, e1.release()->description() == "++a");
837
838 Expression e2 = a++;
839 REPORTER_ASSERT(r, e2.release()->description() == "a++");
840
841 {
842 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
843 (++b).release();
844 }
845
846 {
847 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
848 (b++).release();
849 }
850
851 {
852 ExpectError error(r, "error: cannot assign to this expression\n");
853 (++(a + 1)).release();
854 }
855
856 {
857 ExpectError error(r, "error: cannot assign to this expression\n");
858 ((a + 1)++).release();
859 }
860}
861
862DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
863 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
864 Var a(kInt, "a"), b(kBool, "b");
865 Expression e1 = --a;
866 REPORTER_ASSERT(r, e1.release()->description() == "--a");
867
868 Expression e2 = a--;
869 REPORTER_ASSERT(r, e2.release()->description() == "a--");
870
871 {
872 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
873 (--b).release();
874 }
875
876 {
877 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
878 (b--).release();
879 }
880
881 {
882 ExpectError error(r, "error: cannot assign to this expression\n");
883 (--(a + 1)).release();
884 }
885
886 {
887 ExpectError error(r, "error: cannot assign to this expression\n");
888 ((a + 1)--).release();
889 }
890}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500891
892DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
893 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
894 Statement x = Block();
895 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "{ }"));
896 Var a(kInt, "a"), b(kInt, "b");
897 Statement y = Block(Declare(a, 1), Declare(b, 2), a = b);
898 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "{ int a = 1; int b = 2; (a = b); }"));
899}
900
901DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
902 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
903 Var a(kHalf4, "a"), b(kHalf4, "b");
904 Statement x = Declare(a);
905 REPORTER_ASSERT(r, x.release()->description() == "half4 a;");
906 Statement y = Declare(b, Half4(1));
907 REPORTER_ASSERT(r, y.release()->description() == "half4 b = half4(1.0);");
908
909 {
910 Var c(kHalf4, "c");
911 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
912 Declare(c, 1).release();
913 }
914}
915
916DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
917 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
918 Statement x = Do(Block(), true);
919 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "do {} while (true);"));
920
921 Var a(kFloat, "a"), b(kFloat, "b");
922 Statement y = Do(Block(a++, --b), a != b);
923 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "do { a++; --b; } while ((a != b));"));
924
925 {
926 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
927 Do(Block(), 7).release();
928 }
929}
930
931DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
932 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
933 Statement x = For(Statement(), Expression(), Expression(), Block());
934 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "for (;;) {}"));
935
936 Var i(kInt, "i");
937 Statement y = For(Declare(i, 0), i < 10, ++i, i += 5);
938 REPORTER_ASSERT(r, whitespace_insensitive_compare(y,
939 "for (int i = 0; (i < 10); ++i) (i += 5);"));
940
941 {
942 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
943 For(i = 0, i + 10, ++i, i += 5).release();
944 }
945}
946
Ethan Nicholas1ff76092021-01-28 10:02:43 -0500947DEF_GPUTEST_FOR_ALL_CONTEXTS(DSLFunction, r, ctxInfo) {
948 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
949 DSLWriter::ProgramElements().clear();
950 Var coords(kHalf2, "coords");
951 DSLFunction(kVoid, "main", coords).define(
952 sk_FragColor() = Half4(coords, 0, 1)
953 );
954 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
955 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
956 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }"));
957
958 DSLWriter::ProgramElements().clear();
959 Var x(kFloat, "x");
960 DSLFunction(kFloat, "sqr", x).define(
961 Return(x * x)
962 );
963 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
964 REPORTER_ASSERT(r, whitespace_insensitive_compare(*DSLWriter::ProgramElements()[0],
965 "float sqr(float x) { return (x * x); }"));
966
967 {
968 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
969 DSLWriter::ProgramElements().clear();
970 DSLFunction(kFloat, "broken").define(
971 Return(true)
972 );
973 }
974
975 {
976 ExpectError error(r, "error: expected function to return 'float'\n");
977 DSLWriter::ProgramElements().clear();
978 DSLFunction(kFloat, "broken").define(
979 Return()
980 );
981 }
982
983 {
984 ExpectError error(r, "error: may not return a value from a void function\n");
985 DSLWriter::ProgramElements().clear();
986 DSLFunction(kVoid, "broken").define(
987 Return(0)
988 );
989 }
990
991/* TODO: detect this case
992 {
993 ExpectError error(r, "error: expected function to return 'float'\n");
994 DSLWriter::ProgramElements().clear();
995 DSLFunction(kFloat, "broken").define(
996 );
997 }
998*/
999}
1000
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001001DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1002 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1003 Var a(kFloat, "a"), b(kFloat, "b");
1004 Statement x = If(a > b, a -= b);
1005 REPORTER_ASSERT(r, x.release()->description() == "if ((a > b)) (a -= b);");
1006
1007 Statement y = If(a > b, a -= b, b -= a);
1008 REPORTER_ASSERT(r, y.release()->description() == "if ((a > b)) (a -= b); else (b -= a);");
1009
1010 {
1011 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1012 If(a + b, a -= b).release();
1013 }
1014}
1015
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001016DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1017 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1018
1019 Statement x = Return();
1020 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "return;"));
1021
1022 Statement y = Return(true);
1023 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "return true;"));
1024}
1025
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001026DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1027 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1028 Var a(kFloat4, "a");
1029
1030 Expression e1 = a.x();
1031 REPORTER_ASSERT(r, e1.release()->description() == "a.x");
1032
1033 Expression e2 = a.y();
1034 REPORTER_ASSERT(r, e2.release()->description() == "a.y");
1035
1036 Expression e3 = a.z();
1037 REPORTER_ASSERT(r, e3.release()->description() == "a.z");
1038
1039 Expression e4 = a.w();
1040 REPORTER_ASSERT(r, e4.release()->description() == "a.w");
1041
1042 Expression e5 = a.r();
1043 REPORTER_ASSERT(r, e5.release()->description() == "a.x");
1044
1045 Expression e6 = a.g();
1046 REPORTER_ASSERT(r, e6.release()->description() == "a.y");
1047
1048 Expression e7 = a.b();
1049 REPORTER_ASSERT(r, e7.release()->description() == "a.z");
1050
1051 Expression e8 = a.a();
1052 REPORTER_ASSERT(r, e8.release()->description() == "a.w");
1053
1054 Expression e9 = Swizzle(a, R);
1055 REPORTER_ASSERT(r, e9.release()->description() == "a.x");
1056
1057 Expression e10 = Swizzle(a, ZERO, G);
1058 REPORTER_ASSERT(r, e10.release()->description() == "float2(a.y, float(0)).yx");
1059
1060 Expression e11 = Swizzle(a, B, G, G);
1061 REPORTER_ASSERT(r, e11.release()->description() == "a.zyy");
1062
1063 Expression e12 = Swizzle(a, R, G, B, ONE);
1064 REPORTER_ASSERT(r, e12.release()->description() == "float4(a.xyz, float(1))");
1065
1066 Expression e13 = Swizzle(a, R, G, B, ONE).r();
1067 REPORTER_ASSERT(r, e13.release()->description() == "float4(a.xyz, float(1)).x");
1068}
1069
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001070DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLTernary, r, ctxInfo) {
1071 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1072 Var a(kInt, "a");
1073 Expression x = Ternary(a > 0, 1, -1);
1074 REPORTER_ASSERT(r, x.release()->description() == "((a > 0) ? 1 : -1)");
1075
1076 {
1077 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1078 Ternary(a, 1, -1).release();
1079 }
1080
1081 {
1082 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
1083 Ternary(a > 0, Float2(1), Float3(1)).release();
1084 }
1085}
1086
1087DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1088 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1089 Statement x = While(true, Block());
1090 REPORTER_ASSERT(r, whitespace_insensitive_compare(x, "for (; true;) {}"));
1091
1092 Var a(kFloat, "a"), b(kFloat, "b");
1093 Statement y = While(a != b, Block(a++, --b));
1094 REPORTER_ASSERT(r, whitespace_insensitive_compare(y, "for (; (a != b);) { a++; --b; }"));
1095
1096 {
1097 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1098 While(7, Block()).release();
1099 }
1100}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001101
1102DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1103 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1104 Var a(Array(kInt, 5), "a"), b(kInt, "b");
1105 Expression e1 = a[0];
1106 REPORTER_ASSERT(r, e1.release()->description() == "a[0]");
1107 Expression e2 = a[b];
1108 REPORTER_ASSERT(r, e2.release()->description() == "a[b]");
1109
1110 {
1111 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
1112 a[true].release();
1113 }
1114
1115 {
1116 ExpectError error(r, "error: expected array, but found 'int'\n");
1117 b[0].release();
1118 }
1119
1120 {
1121 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
1122 a[-1].release();
1123 }
1124}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001125
1126DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1127 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1128 // There is a Fract type on Mac which can conflict with our Fract builtin
1129 using SkSL::dsl::Fract;
1130 Var a(kHalf4, "a"), b(kHalf4, "b"), c(kHalf4, "c");
1131 Var h3(kHalf3, "h3");
1132 Var b4(kBool4, "b4");
1133 REPORTER_ASSERT(r, Abs(a).release()->description() == "abs(a)");
1134 REPORTER_ASSERT(r, All(b4).release()->description() == "all(b4)");
1135 REPORTER_ASSERT(r, Any(b4).release()->description() == "any(b4)");
1136 REPORTER_ASSERT(r, Ceil(a).release()->description() == "ceil(a)");
1137 REPORTER_ASSERT(r, Clamp(a, 0, 1).release()->description() == "clamp(a, 0.0, 1.0)");
1138 REPORTER_ASSERT(r, Cos(a).release()->description() == "cos(a)");
1139 REPORTER_ASSERT(r, Cross(h3, h3).release()->description() == "cross(h3, h3)");
1140 REPORTER_ASSERT(r, Degrees(a).release()->description() == "degrees(a)");
1141 REPORTER_ASSERT(r, Distance(a, b).release()->description() == "distance(a, b)");
1142 REPORTER_ASSERT(r, Dot(a, b).release()->description() == "dot(a, b)");
1143 REPORTER_ASSERT(r, Equal(a, b).release()->description() == "equal(a, b)");
1144 REPORTER_ASSERT(r, Exp(a).release()->description() == "exp(a)");
1145 REPORTER_ASSERT(r, Exp2(a).release()->description() == "exp2(a)");
1146 REPORTER_ASSERT(r, Faceforward(a, b, c).release()->description() == "faceforward(a, b, c)");
1147 REPORTER_ASSERT(r, Floor(a).release()->description() == "floor(a)");
1148 REPORTER_ASSERT(r, Fract(a).release()->description() == "fract(a)");
1149 REPORTER_ASSERT(r, GreaterThan(a, b).release()->description() == "greaterThan(a, b)");
1150 REPORTER_ASSERT(r, GreaterThanEqual(a, b).release()->description() == "greaterThanEqual(a, b)");
1151 REPORTER_ASSERT(r, Inversesqrt(a).release()->description() == "inversesqrt(a)");
1152 REPORTER_ASSERT(r, LessThan(a, b).release()->description() == "lessThan(a, b)");
1153 REPORTER_ASSERT(r, LessThanEqual(a, b).release()->description() == "lessThanEqual(a, b)");
1154 REPORTER_ASSERT(r, Length(a).release()->description() == "length(a)");
1155 REPORTER_ASSERT(r, Log(a).release()->description() == "log(a)");
1156 REPORTER_ASSERT(r, Log2(a).release()->description() == "log2(a)");
1157 REPORTER_ASSERT(r, Max(a, b).release()->description() == "max(a, b)");
1158 REPORTER_ASSERT(r, Min(a, b).release()->description() == "min(a, b)");
1159 REPORTER_ASSERT(r, Mix(a, b, c).release()->description() == "mix(a, b, c)");
1160 REPORTER_ASSERT(r, Mod(a, b).release()->description() == "mod(a, b)");
1161 REPORTER_ASSERT(r, Normalize(a).release()->description() == "normalize(a)");
1162 REPORTER_ASSERT(r, NotEqual(a, b).release()->description() == "notEqual(a, b)");
1163 REPORTER_ASSERT(r, Pow(a, b).release()->description() == "pow(a, b)");
1164 REPORTER_ASSERT(r, Radians(a).release()->description() == "radians(a)");
1165 REPORTER_ASSERT(r, Reflect(a, b).release()->description() == "reflect(a, b)");
1166 REPORTER_ASSERT(r, Refract(a, b, 1).release()->description() == "refract(a, b, 1.0)");
1167 REPORTER_ASSERT(r, Saturate(a).release()->description() == "saturate(a)");
1168 REPORTER_ASSERT(r, Sign(a).release()->description() == "sign(a)");
1169 REPORTER_ASSERT(r, Sin(a).release()->description() == "sin(a)");
1170 REPORTER_ASSERT(r, Smoothstep(a, b, c).release()->description() == "smoothstep(a, b, c)");
1171 REPORTER_ASSERT(r, Sqrt(a).release()->description() == "sqrt(a)");
1172 REPORTER_ASSERT(r, Step(a, b).release()->description() == "step(a, b)");
1173 REPORTER_ASSERT(r, Tan(a).release()->description() == "tan(a)");
1174 REPORTER_ASSERT(r, Unpremul(a).release()->description() == "unpremul(a)");
1175
1176 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1177 // one of them reports errors correctly
1178 {
1179 ExpectError error(r, "error: no match for ceil(bool)\n");
1180 Ceil(a == b).release();
1181 }
1182}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001183
1184DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
1185 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1186
1187 Var v1(kConst_Modifier, kInt, "v1");
1188 Statement d1 = Declare(v1);
1189 REPORTER_ASSERT(r, d1.release()->description() == "const int v1;");
1190
1191 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1192 // context, so we can't as yet Declare() variables with these modifiers.
1193 // TODO: better tests when able
1194 Var v2(kIn_Modifier, kInt, "v2");
1195 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
1196
1197 Var v3(kOut_Modifier, kInt, "v3");
1198 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
1199
1200 Var v4(kUniform_Modifier, kInt, "v4");
1201 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
1202
1203 Var v5(kFlat_Modifier, kInt, "v5");
1204 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
1205
1206 Var v6(kNoPerspective_Modifier, kInt, "v6");
1207 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1208 SkSL::Modifiers::kNoPerspective_Flag);
1209
1210 Var v7(kIn_Modifier | kOut_Modifier, kInt, "v7");
1211 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1212 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
1213
1214 Var v8(kInOut_Modifier, kInt, "v8");
1215 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags ==
1216 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
1217}