blob: e15b0fdb1352e07404a7ce617b98f5f4033e372d [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
Ethan Nicholas24c17722021-03-09 13:10:59 -05008#include "include/private/SkSLIRNode.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -05009#include "include/sksl/DSL.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050010#include "src/gpu/GrDirectContextPriv.h"
11#include "src/gpu/GrGpu.h"
12#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050013#include "src/sksl/dsl/priv/DSLWriter.h"
14
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
Ethan Nicholas961d9442021-03-16 16:37:29 -040021/**
22 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
23 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
24 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
25 * errors, especially given that some of the variables have options that make them an error to
26 * actually declare.
27 */
Ethan Nicholas95046142021-01-07 10:57:27 -050028class AutoDSLContext {
29public:
Ethan Nicholasee49efc2021-04-09 15:33:53 -040030 AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true,
31 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
32 Start(gpu->shaderCompiler(), kind);
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050033 DSLWriter::Instance().fMangle = false;
Ethan Nicholas961d9442021-03-16 16:37:29 -040034 DSLWriter::Instance().fMarkVarsDeclared = markVarsDeclared;
Ethan Nicholas95046142021-01-07 10:57:27 -050035 }
36
37 ~AutoDSLContext() {
38 End();
39 }
40};
41
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050042class ExpectError : public ErrorHandler {
43public:
44 ExpectError(skiatest::Reporter* reporter, const char* msg)
45 : fMsg(msg)
46 , fReporter(reporter) {
47 SetErrorHandler(this);
48 }
49
50 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050051 REPORTER_ASSERT(fReporter, !fMsg,
52 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050053 SetErrorHandler(nullptr);
54 }
55
Ethan Nicholasb9563042021-02-25 09:45:49 -050056 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050057 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
58 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
59 fMsg = nullptr;
60 }
61
62private:
63 const char* fMsg;
64 skiatest::Reporter* fReporter;
65};
66
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050067static bool whitespace_insensitive_compare(const char* a, const char* b) {
68 for (;;) {
69 while (isspace(*a)) {
70 ++a;
71 }
72 while (isspace(*b)) {
73 ++b;
74 }
75 if (*a != *b) {
76 return false;
77 }
78 if (*a == 0) {
79 return true;
80 }
81 ++a;
82 ++b;
83 }
84}
85
Ethan Nicholas24c17722021-03-09 13:10:59 -050086// for use from SkSLDSLOnlyTest.cpp
87void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
88 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
89}
90
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050091DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050092 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
93 Expression e1 = 1;
94 REPORTER_ASSERT(r, e1.release()->description() == "1");
95 Expression e2 = 1.0;
96 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
97 Expression e3 = true;
98 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040099 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500100 Expression e4 = a;
101 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500102
103 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
104 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
105 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
106 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
107 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
108 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
109 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
110 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500111}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500112
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500113static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
114static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
115static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
116static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500117static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
118
119template <typename T>
120static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
121 SkSL::String actual = stringize(input);
122 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
123 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
124 lineNumber, expected, actual.c_str());
125 }
126}
127
128template <typename T>
129static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
130 // This overload allows temporary values to be passed to expect_equal.
131 return expect_equal(r, lineNumber, dsl, expected);
132}
133
134#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
135
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500136DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
137 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
138 Expression e1 = Float(std::numeric_limits<float>::max());
139 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
140 std::numeric_limits<float>::max());
141
142 Expression e2 = Float(std::numeric_limits<float>::min());
143 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
144 std::numeric_limits<float>::min());
145
John Stilesb4d7b582021-02-19 09:56:31 -0500146 EXPECT_EQUAL(Float2(0),
147 "float2(0.0)");
148 EXPECT_EQUAL(Float2(-0.5, 1),
149 "float2(-0.5, 1.0)");
150 EXPECT_EQUAL(Float3(0.75),
151 "float3(0.75)");
152 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500153 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500154 EXPECT_EQUAL(Float3(0, 1, 2),
155 "float3(0.0, 1.0, 2.0)");
156 EXPECT_EQUAL(Float4(0),
157 "float4(0.0)");
158 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500159 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500160 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500161 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500162 EXPECT_EQUAL(Float4(0, 1, 2, 3),
163 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500164
165 {
166 ExpectError error(r, "error: floating point value is infinite\n");
167 Float(std::numeric_limits<float>::infinity()).release();
168 }
169
170 {
171 ExpectError error(r, "error: floating point value is NaN\n");
172 Float(std::numeric_limits<float>::quiet_NaN()).release();
173 }
174
175 {
176 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
177 " but found 4)\n");
178 Float2(Float4(1)).release();
179 }
180
181 {
182 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
183 " but found 3)\n");
184 Float4(Float3(1)).release();
185 }
186}
187
188DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
189 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
190 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500191 REPORTER_ASSERT(r,
192 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500193
194 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500195 REPORTER_ASSERT(r,
196 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500197
John Stilesb9e4f642021-03-05 09:11:38 -0500198 EXPECT_EQUAL(Half2(0),
199 "half2(0.0)");
200 EXPECT_EQUAL(Half2(-0.5, 1),
201 "half2(-0.5, 1.0)");
202 EXPECT_EQUAL(Half3(0.75),
203 "half3(0.75)");
204 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
205 "half3(0.0, 1.0, -2.0)");
206 EXPECT_EQUAL(Half3(0, 1, 2),
207 "half3(0.0, 1.0, 2.0)");
208 EXPECT_EQUAL(Half4(0),
209 "half4(0.0)");
210 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
211 "half4(0.0, 1.0, 2.0, 3.0)");
212 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
213 "half4(0.0, 1.0, 2.0, 3.0)");
214 EXPECT_EQUAL(Half4(0, 1, 2, 3),
215 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500216
217 {
218 ExpectError error(r, "error: floating point value is infinite\n");
219 Half(std::numeric_limits<float>::infinity()).release();
220 }
221
222 {
223 ExpectError error(r, "error: floating point value is NaN\n");
224 Half(std::numeric_limits<float>::quiet_NaN()).release();
225 }
226
227 {
228 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
229 " but found 4)\n");
230 Half2(Half4(1)).release();
231 }
232
233 {
234 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
235 " but found 3)\n");
236 Half4(Half3(1)).release();
237 }
238}
239
240DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
241 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500242
John Stilesb9e4f642021-03-05 09:11:38 -0500243 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
244 "2147483647");
245 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
246 "int2(-2147483648)");
247 EXPECT_EQUAL(Int2(0, 1),
248 "int2(0, 1)");
249 EXPECT_EQUAL(Int3(0),
250 "int3(0)");
251 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
252 "int3(0, 1, -2)");
253 EXPECT_EQUAL(Int3(0, 1, 2),
254 "int3(0, 1, 2)");
255 EXPECT_EQUAL(Int4(0),
256 "int4(0)");
257 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
258 "int4(0, 1, 2, 3)");
259 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
260 "int4(0, 1, 2, 3)");
261 EXPECT_EQUAL(Int4(0, 1, 2, 3),
262 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500263
264 {
265 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
266 " but found 4)\n");
267 Int2(Int4(1)).release();
268 }
269
270 {
271 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
272 " but found 3)\n");
273 Int4(Int3(1)).release();
274 }
275}
276
277DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
278 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500279
John Stilesb9e4f642021-03-05 09:11:38 -0500280 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
281 "32767");
282 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
283 "short2(-32768)");
284 EXPECT_EQUAL(Short2(0, 1),
285 "short2(0, 1)");
286 EXPECT_EQUAL(Short3(0),
287 "short3(0)");
288 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
289 "short3(0, 1, -2)");
290 EXPECT_EQUAL(Short3(0, 1, 2),
291 "short3(0, 1, 2)");
292 EXPECT_EQUAL(Short4(0),
293 "short4(0)");
294 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
295 "short4(0, 1, 2, 3)");
296 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
297 "short4(0, 1, 2, 3)");
298 EXPECT_EQUAL(Short4(0, 1, 2, 3),
299 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500300
301 {
302 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
303 " but found 4)\n");
304 Short2(Short4(1)).release();
305 }
306
307 {
308 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
309 " but found 3)\n");
310 Short4(Short3(1)).release();
311 }
312}
313
314DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
315 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500316
John Stilesb9e4f642021-03-05 09:11:38 -0500317 EXPECT_EQUAL(Bool2(false),
318 "bool2(false)");
319 EXPECT_EQUAL(Bool2(false, true),
320 "bool2(false, true)");
321 EXPECT_EQUAL(Bool3(false),
322 "bool3(false)");
323 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
324 "bool3(false, true, false)");
325 EXPECT_EQUAL(Bool3(false, true, false),
326 "bool3(false, true, false)");
327 EXPECT_EQUAL(Bool4(false),
328 "bool4(false)");
329 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
330 "bool4(false, true, false, true)");
331 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
332 "bool4(false, true, false, true)");
333 EXPECT_EQUAL(Bool4(false, true, false, true),
334 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500335
336 {
337 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
338 " but found 4)\n");
339 Bool2(Bool4(true)).release();
340 }
341
342 {
343 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
344 " but found 3)\n");
345 Bool4(Bool3(true)).release();
346 }
347}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500348
349DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
350 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400351 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500352
John Stiles8f440b42021-03-05 16:48:56 -0500353 EXPECT_EQUAL(a + b,
354 "(a + b)");
355 EXPECT_EQUAL(a + 1,
356 "(a + 1.0)");
357 EXPECT_EQUAL(0.5 + a + -99,
358 "((0.5 + a) + -99.0)");
359 EXPECT_EQUAL(a += b + 1,
360 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500361 EXPECT_EQUAL(+a,
362 "a");
363 EXPECT_EQUAL(+(a + b),
364 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500365
366 {
367 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500368 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500369 }
370
371 {
372 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500373 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500374 }
375
376 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500377 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500378 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500379 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500380
381 {
382 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400383 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500384 DSLExpression(+c);
385 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500386}
387
388DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
389 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400390 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500391
John Stiles8f440b42021-03-05 16:48:56 -0500392 EXPECT_EQUAL(a - b,
393 "(a - b)");
394 EXPECT_EQUAL(a - 1,
395 "(a - 1)");
396 EXPECT_EQUAL(2 - a - b,
397 "((2 - a) - b)");
398 EXPECT_EQUAL(a -= b + 1,
399 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500400 EXPECT_EQUAL(-a,
401 "-a");
402 EXPECT_EQUAL(-(a - b),
403 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500404
405 {
406 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500407 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500408 }
409
410 {
411 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500412 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500413 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500414
415 {
416 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500417 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500418 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500419
420 {
421 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400422 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500423 DSLExpression(-c);
424 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500425}
426
427DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
428 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400429 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500430
John Stiles8f440b42021-03-05 16:48:56 -0500431 EXPECT_EQUAL(a * b,
432 "(a * b)");
433 EXPECT_EQUAL(a * 2,
434 "(a * 2.0)");
435 EXPECT_EQUAL(0.5 * a * -99,
436 "((0.5 * a) * -99.0)");
437 EXPECT_EQUAL(a *= b + 1,
438 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500439
440 {
441 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500442 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500443 }
444
445 {
446 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500447 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500448 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500449
450 {
451 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500452 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500453 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500454}
455
456DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
457 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400458 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500459
John Stiles8f440b42021-03-05 16:48:56 -0500460 EXPECT_EQUAL(a / b,
461 "(a / b)");
462 EXPECT_EQUAL(a / 2,
463 "(a / 2.0)");
464 EXPECT_EQUAL(0.5 / a / -99,
465 "((0.5 / a) / -99.0)");
466 EXPECT_EQUAL(b / (a - 1),
467 "(b / (a - 1.0))");
468 EXPECT_EQUAL(a /= b + 1,
469 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500470
471 {
472 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500473 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500474 }
475
476 {
477 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500478 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500479 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500480
481 {
482 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500483 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500484 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500485
486 {
487 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500488 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500489 }
490
491 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400492 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500493 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500494 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500495 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500496}
497
498DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
499 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400500 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500501 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500502 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500503
504 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500505 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500506
507 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500508 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500509
510 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500511 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500512
513 {
514 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500515 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500516 }
517
518 {
519 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500520 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500521 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500522
523 {
524 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500525 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500526 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500527
528 {
529 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500530 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500531 }
532
533 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400534 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500535 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500536 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500537 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500538}
539
540DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
541 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400542 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500543 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500544 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500545
546 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500547 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500548
549 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500550 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500551
552 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500553 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500554
555 {
556 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500557 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500558 }
559
560 {
561 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500562 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500563 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500564
565 {
566 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500567 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500568 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500569}
570
571DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
572 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400573 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500574 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500575 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500576
577 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500578 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500579
580 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500581 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500582
583 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500584 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500585
586 {
587 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500588 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500589 }
590
591 {
592 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500593 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500594 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500595
596 {
597 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500598 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500599 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500600}
601
602DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
603 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400604 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500605 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500606 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500607
608 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500609 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500610
611 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500612 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500613
614 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500615 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500616
617 {
618 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500619 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500620 }
621
622 {
623 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500624 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500625 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500626
627 {
628 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500629 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500630 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500631}
632
633DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
634 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400635 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500636 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500637 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500638
639 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500640 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500641
642 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500643 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500644
645 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500646 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500647
648 {
649 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500650 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500651 }
652
653 {
654 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500655 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500656 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500657
658 {
659 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500660 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500661 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500662}
663
664DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
665 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400666 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500667 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500668 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500669
670 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500671 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500672
673 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500674 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500675
676 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500677 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500678
679 {
680 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500681 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500682 }
683
684 {
685 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500686 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500687 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500688
689 {
690 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500691 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500692 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500693}
694
695DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
696 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400697 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500698 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500699 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500700
701 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500702 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500703
704 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500705 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500706
707 {
708 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500709 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500710 }
711}
712
713DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
714 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400715 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500716 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500717 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500718
719 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500720 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500721
722 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500723 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500724
725 {
726 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500727 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500728 }
729}
730
731DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
732 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400733 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500734 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -0500735 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500736
737 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -0500738 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500739}
740
741DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
742 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400743 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500744 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -0500745 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500746
747 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500748 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500749
750 {
751 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500752 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500753 }
754}
755
756DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
757 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400758 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500759 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -0500760 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500761
762 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500763 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500764
765 {
766 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500767 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500768 }
769}
770
771DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
772 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400773 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500774 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -0500775 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500776
777 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500778 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500779
780 {
781 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500782 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500783 }
784}
785
786DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
787 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400788 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500789 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500790 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500791
792 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500793 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500794
795 {
796 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500797 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500798 }
799}
800
801DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
802 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400803 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500804 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -0500805 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500806
807 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500808 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500809
810 {
811 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500812 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500813 }
814}
815
816DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
817 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400818 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500819 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500820 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500821
822 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500823 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500824
825 {
826 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500827 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500828 }
829}
830
831DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
832 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400833 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500834 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -0500835 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500836
837 {
838 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500839 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500840 }
841}
842
843DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
844 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400845 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500846 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -0500847 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500848
849 {
850 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500851 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500852 }
853}
854
855DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
856 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400857 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500858 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -0500859 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500860
861 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -0500862 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500863
864 {
865 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500866 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500867 }
868
869 {
870 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500871 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500872 }
873
874 {
875 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500876 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500877 }
878
879 {
880 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500881 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500882 }
883}
884
885DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
886 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400887 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500888 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -0500889 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500890
891 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -0500892 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500893
894 {
895 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500896 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500897 }
898
899 {
900 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500901 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500902 }
903
904 {
905 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500906 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500907 }
908
909 {
910 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500911 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500912 }
913}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500914
915DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400916 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500917 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -0500918 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400919 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500920 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -0500921 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500922}
923
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500924DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400925 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400926 Var i(kInt_Type, "i", 0);
927 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500928 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500929 If(i > 5, Break())
930 ))
931 );
932 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500933 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
934 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500935
936 {
937 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400938 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500939 Break()
940 );
941 }
942}
943
944DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400945 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400946 Var i(kInt_Type, "i", 0);
947 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500948 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500949 If(i < 5, Continue())
950 ))
951 );
952 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500953 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
954 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500955
956 {
957 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400958 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500959 Continue()
960 );
961 }
962}
963
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500964DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400965 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400966 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500967 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -0500968 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500969 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -0500970 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500971
972 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400973 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500974 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500975 Declare(c).release();
976 }
977
978 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400979 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500980 Declare(d).release();
981 ExpectError error(r, "error: variable has already been declared\n");
982 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500983 }
984}
985
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500986DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
987 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
988 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -0500989 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500990}
991
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500992DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
993 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
994 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -0500995 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500996
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400997 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500998 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -0500999 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001000
1001 {
1002 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1003 Do(Block(), 7).release();
1004 }
1005}
1006
1007DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001008 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001009 Statement x = For(Statement(), Expression(), Expression(), Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001010 EXPECT_EQUAL(x, "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001011
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001012 Var i(kInt_Type, "i", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001013 Statement y = For(Declare(i), i < 10, ++i, i += 5);
John Stilesb4d7b582021-02-19 09:56:31 -05001014 EXPECT_EQUAL(y, "for (int i = 0; (i < 10); ++i) (i += 5);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001015
1016 {
1017 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1018 For(i = 0, i + 10, ++i, i += 5).release();
1019 }
1020}
1021
Ethan Nicholase2c05042021-02-03 10:27:22 -05001022DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001023 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001024 Var coords(kHalf2_Type, "coords");
1025 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001026 sk_FragColor() = Half4(coords, 0, 1)
1027 );
1028 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001029 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1030 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001031
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001032 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001033 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001034 Var x(kFloat_Type, "x");
1035 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001036 sqr.define(
1037 Return(x * x)
1038 );
1039 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1040 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1041 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1042 }
1043
1044 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001045 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001046 Var x(kFloat2_Type, "x");
1047 Var y(kFloat2_Type, "y");
1048 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001049 dot.define(
1050 Return(x * x + y * y)
1051 );
1052 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1053 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1054 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1055 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1056 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1057 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001058
1059 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001060 DSLWriter::Reset();
1061 Var x(kFloat_Type, "x");
1062 Var y(kFloat_Type, "y");
1063 DSLFunction pair(kFloat2_Type, "pair", x, y);
1064 pair.define(
1065 Return(Float2(x, y))
1066 );
1067 Var varArg1(kFloat_Type, "varArg1");
1068 Var varArg2(kFloat_Type, "varArg2");
1069 DSLWriter::MarkDeclared(varArg1);
1070 DSLWriter::MarkDeclared(varArg2);
1071 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1072 }
1073
1074 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001075 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001076 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001077 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001078 Return(true)
1079 );
1080 }
1081
1082 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001083 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001084 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001085 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001086 Return()
1087 );
1088 }
1089
1090 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001091 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001092 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001093 Var x(kFloat_Type, "x", 0);
1094 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001095 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001096 If(x == 1, Return(x))
1097 );
1098 }
1099
1100 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001101 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001102 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001103 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001104 Return(0)
1105 );
1106 }
1107
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001108 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001109 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001110 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001111 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001112 );
1113 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001114
1115 {
1116 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1117 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001118 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001119 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001120 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001121 );
1122 }
1123
1124 {
1125 ExpectError error(r, "error: variable has already been declared\n");
1126 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001127 DSLVar p(kFloat_Type);
1128 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001129 );
1130 Declare(p).release();
1131 }
1132
1133 {
1134 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1135 "values\n");
1136 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001137 DSLVar p(kFloat_Type, 1);
1138 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001139 );
1140 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001141}
1142
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001143DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1144 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001145 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001146 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001147 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001148
1149 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001150 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001151
1152 {
1153 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1154 If(a + b, a -= b).release();
1155 }
1156}
1157
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001158DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1159 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1160
1161 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001162 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001163
1164 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001165 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001166}
1167
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001168DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1169 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001170 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001171 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001172 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001173
1174 {
1175 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001176 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001177 }
1178
1179 {
1180 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001181 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001182 }
1183}
1184
Ethan Nicholascfefec02021-02-09 15:22:57 -05001185DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1186 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1187
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001188 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001189
John Stilesf3a28db2021-03-10 23:00:47 -05001190 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001191 Case(0, a = 0, Break()),
1192 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001193 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001194 Default(Discard())
1195 );
John Stilese1d1b082021-02-23 13:44:36 -05001196 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001197 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001198 case 0: (a = 0.0); break;
1199 case 1: (a = 1.0); continue;
1200 case 2: (a = 2.0);
1201 default: discard;
1202 }
1203 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001204
John Stiles642cde22021-02-23 14:57:01 -05001205 EXPECT_EQUAL(Switch(b),
1206 "switch (b) {}");
1207
1208 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1209 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001210
1211 {
John Stilese1d1b082021-02-23 13:44:36 -05001212 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001213 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001214 }
1215
1216 {
John Stilese1d1b082021-02-23 13:44:36 -05001217 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001218 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001219 }
1220
1221 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001222 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001223 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001224 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001225 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001226}
1227
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001228DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1229 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001230 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001231
John Stilesf04e09c2021-03-05 13:13:14 -05001232 EXPECT_EQUAL(a.x(),
1233 "a.x");
1234 EXPECT_EQUAL(a.y(),
1235 "a.y");
1236 EXPECT_EQUAL(a.z(),
1237 "a.z");
1238 EXPECT_EQUAL(a.w(),
1239 "a.w");
1240 EXPECT_EQUAL(a.r(),
1241 "a.x");
1242 EXPECT_EQUAL(a.g(),
1243 "a.y");
1244 EXPECT_EQUAL(a.b(),
1245 "a.z");
1246 EXPECT_EQUAL(a.a(),
1247 "a.w");
1248 EXPECT_EQUAL(Swizzle(a, R),
1249 "a.x");
1250 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1251 "float2(0.0, a.y)");
1252 EXPECT_EQUAL(Swizzle(a, B, G, G),
1253 "a.zyy");
1254 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1255 "float4(a.xyz, 1.0)");
1256 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1257 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001258}
1259
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001260DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1261 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1262 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001263 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001264
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001265 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001266 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001267 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001268
1269 {
1270 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001271 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001272 }
1273}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001274
1275DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1276 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001277 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001278
1279 EXPECT_EQUAL(a[0], "a[0]");
1280 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001281
1282 {
1283 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001284 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001285 }
1286
1287 {
1288 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001289 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001290 }
1291
1292 {
1293 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001294 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001295 }
1296}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001297
1298DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1299 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1300 // There is a Fract type on Mac which can conflict with our Fract builtin
1301 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001302 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1303 Var h3(kHalf3_Type, "h3");
1304 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001305 EXPECT_EQUAL(Abs(a), "abs(a)");
1306 EXPECT_EQUAL(All(b4), "all(b4)");
1307 EXPECT_EQUAL(Any(b4), "any(b4)");
1308 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1309 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1310 EXPECT_EQUAL(Cos(a), "cos(a)");
1311 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1312 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1313 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1314 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1315 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1316 EXPECT_EQUAL(Exp(a), "exp(a)");
1317 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1318 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1319 EXPECT_EQUAL(Floor(a), "floor(a)");
1320 EXPECT_EQUAL(Fract(a), "fract(a)");
1321 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1322 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1323 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1324 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1325 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1326 EXPECT_EQUAL(Length(a), "length(a)");
1327 EXPECT_EQUAL(Log(a), "log(a)");
1328 EXPECT_EQUAL(Log2(a), "log2(a)");
1329 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1330 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1331 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1332 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1333 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1334 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1335 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1336 EXPECT_EQUAL(Radians(a), "radians(a)");
1337 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1338 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1339 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1340 EXPECT_EQUAL(Sign(a), "sign(a)");
1341 EXPECT_EQUAL(Sin(a), "sin(a)");
1342 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1343 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1344 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1345 EXPECT_EQUAL(Tan(a), "tan(a)");
1346 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001347
1348 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1349 // one of them reports errors correctly
1350 {
1351 ExpectError error(r, "error: no match for ceil(bool)\n");
1352 Ceil(a == b).release();
1353 }
1354}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001355
1356DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001357 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001358
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001359 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001360 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001361 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001362
1363 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1364 // context, so we can't as yet Declare() variables with these modifiers.
1365 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001366 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001367 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001368 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001369
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001370 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001371 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001372 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001373
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001374 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001375 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001376 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001377
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001378 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001379 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001380 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001381 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001382
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001383 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001384 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1385 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001386 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001387
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001388 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001389 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1390 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001391 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001392
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001393 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001394 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001395 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001396}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001397
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001398DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSample, r, ctxInfo) {
1399 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1400 SkSL::ProgramKind::kFragmentProcessor);
1401 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1402 EXPECT_EQUAL(Sample(child), "sample(child)");
1403 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1404 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
1405 EXPECT_EQUAL(Sample(child, Half4(1), Float2(0)), "sample(child, half4(1.0), float2(0.0))");
1406
1407 {
1408 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1409 Sample(child, true).release();
1410 }
1411}
1412
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001413DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001414 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001415
1416 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001417 Field(kFloat_Type, "x"),
1418 Field(kBool_Type, "b"),
1419 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001420 );
1421 DSLVar result(simpleStruct, "result");
1422 DSLFunction(simpleStruct, "returnStruct").define(
1423 Declare(result),
1424 result.field("x") = 123,
1425 result.field("b") = result.field("x") > 0,
1426 result.field("a")[0] = result.field("x"),
1427 Return(result)
1428 );
1429 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001430 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1431 "struct SimpleStruct { float x; bool b; float[3] a; };");
1432 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1433 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1434 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001435
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001436 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001437 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001438 Field(simpleStruct, "simple")
1439 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001440 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1441 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001442 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001443}