blob: 4fdd82a9cf3d8d2e32acd5c1646c10bf99615112 [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 Nicholas961d9442021-03-16 16:37:29 -040030 AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true) {
Ethan Nicholas95046142021-01-07 10:57:27 -050031 Start(gpu->shaderCompiler());
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050032 DSLWriter::Instance().fMangle = false;
Ethan Nicholas961d9442021-03-16 16:37:29 -040033 DSLWriter::Instance().fMarkVarsDeclared = markVarsDeclared;
Ethan Nicholas95046142021-01-07 10:57:27 -050034 }
35
36 ~AutoDSLContext() {
37 End();
38 }
39};
40
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050041class ExpectError : public ErrorHandler {
42public:
43 ExpectError(skiatest::Reporter* reporter, const char* msg)
44 : fMsg(msg)
45 , fReporter(reporter) {
46 SetErrorHandler(this);
47 }
48
49 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050050 REPORTER_ASSERT(fReporter, !fMsg,
51 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050052 SetErrorHandler(nullptr);
53 }
54
Ethan Nicholasb9563042021-02-25 09:45:49 -050055 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050056 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
57 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
58 fMsg = nullptr;
59 }
60
61private:
62 const char* fMsg;
63 skiatest::Reporter* fReporter;
64};
65
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050066static bool whitespace_insensitive_compare(const char* a, const char* b) {
67 for (;;) {
68 while (isspace(*a)) {
69 ++a;
70 }
71 while (isspace(*b)) {
72 ++b;
73 }
74 if (*a != *b) {
75 return false;
76 }
77 if (*a == 0) {
78 return true;
79 }
80 ++a;
81 ++b;
82 }
83}
84
Ethan Nicholas24c17722021-03-09 13:10:59 -050085// for use from SkSLDSLOnlyTest.cpp
86void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
87 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
88}
89
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050090DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050091 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
92 Expression e1 = 1;
93 REPORTER_ASSERT(r, e1.release()->description() == "1");
94 Expression e2 = 1.0;
95 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
96 Expression e3 = true;
97 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040098 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050099 Expression e4 = a;
100 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500101
102 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
103 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
104 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
105 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
106 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
107 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
108 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
109 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500110}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500111
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500112static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
113static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
114static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
115static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500116static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
117
118template <typename T>
119static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
120 SkSL::String actual = stringize(input);
121 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
122 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
123 lineNumber, expected, actual.c_str());
124 }
125}
126
127template <typename T>
128static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
129 // This overload allows temporary values to be passed to expect_equal.
130 return expect_equal(r, lineNumber, dsl, expected);
131}
132
133#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
134
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500135DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
136 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
137 Expression e1 = Float(std::numeric_limits<float>::max());
138 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
139 std::numeric_limits<float>::max());
140
141 Expression e2 = Float(std::numeric_limits<float>::min());
142 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
143 std::numeric_limits<float>::min());
144
John Stilesb4d7b582021-02-19 09:56:31 -0500145 EXPECT_EQUAL(Float2(0),
146 "float2(0.0)");
147 EXPECT_EQUAL(Float2(-0.5, 1),
148 "float2(-0.5, 1.0)");
149 EXPECT_EQUAL(Float3(0.75),
150 "float3(0.75)");
151 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500152 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500153 EXPECT_EQUAL(Float3(0, 1, 2),
154 "float3(0.0, 1.0, 2.0)");
155 EXPECT_EQUAL(Float4(0),
156 "float4(0.0)");
157 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500158 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500159 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500160 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500161 EXPECT_EQUAL(Float4(0, 1, 2, 3),
162 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500163
164 {
165 ExpectError error(r, "error: floating point value is infinite\n");
166 Float(std::numeric_limits<float>::infinity()).release();
167 }
168
169 {
170 ExpectError error(r, "error: floating point value is NaN\n");
171 Float(std::numeric_limits<float>::quiet_NaN()).release();
172 }
173
174 {
175 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
176 " but found 4)\n");
177 Float2(Float4(1)).release();
178 }
179
180 {
181 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
182 " but found 3)\n");
183 Float4(Float3(1)).release();
184 }
185}
186
187DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
188 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
189 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500190 REPORTER_ASSERT(r,
191 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500192
193 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500194 REPORTER_ASSERT(r,
195 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500196
John Stilesb9e4f642021-03-05 09:11:38 -0500197 EXPECT_EQUAL(Half2(0),
198 "half2(0.0)");
199 EXPECT_EQUAL(Half2(-0.5, 1),
200 "half2(-0.5, 1.0)");
201 EXPECT_EQUAL(Half3(0.75),
202 "half3(0.75)");
203 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
204 "half3(0.0, 1.0, -2.0)");
205 EXPECT_EQUAL(Half3(0, 1, 2),
206 "half3(0.0, 1.0, 2.0)");
207 EXPECT_EQUAL(Half4(0),
208 "half4(0.0)");
209 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
210 "half4(0.0, 1.0, 2.0, 3.0)");
211 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
212 "half4(0.0, 1.0, 2.0, 3.0)");
213 EXPECT_EQUAL(Half4(0, 1, 2, 3),
214 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500215
216 {
217 ExpectError error(r, "error: floating point value is infinite\n");
218 Half(std::numeric_limits<float>::infinity()).release();
219 }
220
221 {
222 ExpectError error(r, "error: floating point value is NaN\n");
223 Half(std::numeric_limits<float>::quiet_NaN()).release();
224 }
225
226 {
227 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
228 " but found 4)\n");
229 Half2(Half4(1)).release();
230 }
231
232 {
233 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
234 " but found 3)\n");
235 Half4(Half3(1)).release();
236 }
237}
238
239DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
240 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500241
John Stilesb9e4f642021-03-05 09:11:38 -0500242 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
243 "2147483647");
244 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
245 "int2(-2147483648)");
246 EXPECT_EQUAL(Int2(0, 1),
247 "int2(0, 1)");
248 EXPECT_EQUAL(Int3(0),
249 "int3(0)");
250 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
251 "int3(0, 1, -2)");
252 EXPECT_EQUAL(Int3(0, 1, 2),
253 "int3(0, 1, 2)");
254 EXPECT_EQUAL(Int4(0),
255 "int4(0)");
256 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
257 "int4(0, 1, 2, 3)");
258 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
259 "int4(0, 1, 2, 3)");
260 EXPECT_EQUAL(Int4(0, 1, 2, 3),
261 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500262
263 {
264 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
265 " but found 4)\n");
266 Int2(Int4(1)).release();
267 }
268
269 {
270 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
271 " but found 3)\n");
272 Int4(Int3(1)).release();
273 }
274}
275
276DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
277 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500278
John Stilesb9e4f642021-03-05 09:11:38 -0500279 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
280 "32767");
281 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
282 "short2(-32768)");
283 EXPECT_EQUAL(Short2(0, 1),
284 "short2(0, 1)");
285 EXPECT_EQUAL(Short3(0),
286 "short3(0)");
287 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
288 "short3(0, 1, -2)");
289 EXPECT_EQUAL(Short3(0, 1, 2),
290 "short3(0, 1, 2)");
291 EXPECT_EQUAL(Short4(0),
292 "short4(0)");
293 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
294 "short4(0, 1, 2, 3)");
295 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
296 "short4(0, 1, 2, 3)");
297 EXPECT_EQUAL(Short4(0, 1, 2, 3),
298 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500299
300 {
301 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
302 " but found 4)\n");
303 Short2(Short4(1)).release();
304 }
305
306 {
307 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
308 " but found 3)\n");
309 Short4(Short3(1)).release();
310 }
311}
312
313DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
314 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500315
John Stilesb9e4f642021-03-05 09:11:38 -0500316 EXPECT_EQUAL(Bool2(false),
317 "bool2(false)");
318 EXPECT_EQUAL(Bool2(false, true),
319 "bool2(false, true)");
320 EXPECT_EQUAL(Bool3(false),
321 "bool3(false)");
322 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
323 "bool3(false, true, false)");
324 EXPECT_EQUAL(Bool3(false, true, false),
325 "bool3(false, true, false)");
326 EXPECT_EQUAL(Bool4(false),
327 "bool4(false)");
328 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
329 "bool4(false, true, false, true)");
330 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
331 "bool4(false, true, false, true)");
332 EXPECT_EQUAL(Bool4(false, true, false, true),
333 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500334
335 {
336 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
337 " but found 4)\n");
338 Bool2(Bool4(true)).release();
339 }
340
341 {
342 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
343 " but found 3)\n");
344 Bool4(Bool3(true)).release();
345 }
346}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500347
348DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
349 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400350 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500351
John Stiles8f440b42021-03-05 16:48:56 -0500352 EXPECT_EQUAL(a + b,
353 "(a + b)");
354 EXPECT_EQUAL(a + 1,
355 "(a + 1.0)");
356 EXPECT_EQUAL(0.5 + a + -99,
357 "((0.5 + a) + -99.0)");
358 EXPECT_EQUAL(a += b + 1,
359 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500360 EXPECT_EQUAL(+a,
361 "a");
362 EXPECT_EQUAL(+(a + b),
363 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500364
365 {
366 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500367 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500368 }
369
370 {
371 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500372 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500373 }
374
375 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500376 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500377 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500378 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500379
380 {
381 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400382 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500383 DSLExpression(+c);
384 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500385}
386
387DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
388 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400389 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500390
John Stiles8f440b42021-03-05 16:48:56 -0500391 EXPECT_EQUAL(a - b,
392 "(a - b)");
393 EXPECT_EQUAL(a - 1,
394 "(a - 1)");
395 EXPECT_EQUAL(2 - a - b,
396 "((2 - a) - b)");
397 EXPECT_EQUAL(a -= b + 1,
398 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500399 EXPECT_EQUAL(-a,
400 "-a");
401 EXPECT_EQUAL(-(a - b),
402 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500403
404 {
405 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500406 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500407 }
408
409 {
410 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500411 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500412 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500413
414 {
415 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500416 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500417 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500418
419 {
420 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400421 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500422 DSLExpression(-c);
423 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500424}
425
426DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
427 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400428 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500429
John Stiles8f440b42021-03-05 16:48:56 -0500430 EXPECT_EQUAL(a * b,
431 "(a * b)");
432 EXPECT_EQUAL(a * 2,
433 "(a * 2.0)");
434 EXPECT_EQUAL(0.5 * a * -99,
435 "((0.5 * a) * -99.0)");
436 EXPECT_EQUAL(a *= b + 1,
437 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500438
439 {
440 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500441 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500442 }
443
444 {
445 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500446 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500447 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500448
449 {
450 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500451 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500452 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500453}
454
455DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
456 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400457 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500458
John Stiles8f440b42021-03-05 16:48:56 -0500459 EXPECT_EQUAL(a / b,
460 "(a / b)");
461 EXPECT_EQUAL(a / 2,
462 "(a / 2.0)");
463 EXPECT_EQUAL(0.5 / a / -99,
464 "((0.5 / a) / -99.0)");
465 EXPECT_EQUAL(b / (a - 1),
466 "(b / (a - 1.0))");
467 EXPECT_EQUAL(a /= b + 1,
468 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500469
470 {
471 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500472 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500473 }
474
475 {
476 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500477 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500478 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500479
480 {
481 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500482 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500483 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500484
485 {
486 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500487 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500488 }
489
490 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400491 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500492 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500493 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500494 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500495}
496
497DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
498 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400499 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500500 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500501 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500502
503 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500504 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500505
506 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500507 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500508
509 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500510 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500511
512 {
513 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500514 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500515 }
516
517 {
518 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500519 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500520 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500521
522 {
523 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500524 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500525 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500526
527 {
528 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500529 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500530 }
531
532 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400533 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500534 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500535 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500536 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500537}
538
539DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
540 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400541 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500542 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500543 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500544
545 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500546 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500547
548 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500549 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500550
551 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500552 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500553
554 {
555 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500556 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500557 }
558
559 {
560 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500561 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500562 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500563
564 {
565 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500566 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500567 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500568}
569
570DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
571 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400572 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500573 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500574 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500575
576 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500577 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500578
579 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500580 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500581
582 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500583 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500584
585 {
586 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500587 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500588 }
589
590 {
591 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500592 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500593 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500594
595 {
596 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500597 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500598 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500599}
600
601DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
602 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400603 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500604 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500605 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500606
607 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500608 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500609
610 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500611 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500612
613 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500614 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500615
616 {
617 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500618 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500619 }
620
621 {
622 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500623 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500624 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500625
626 {
627 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500628 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500629 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500630}
631
632DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
633 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400634 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500635 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500636 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500637
638 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500639 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500640
641 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500642 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500643
644 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500645 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500646
647 {
648 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500649 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500650 }
651
652 {
653 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500654 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500655 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500656
657 {
658 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500659 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500660 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500661}
662
663DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
664 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400665 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500666 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500667 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500668
669 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500670 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500671
672 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500673 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674
675 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500676 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500677
678 {
679 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500680 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500681 }
682
683 {
684 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500685 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500686 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500687
688 {
689 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500690 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500691 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500692}
693
694DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
695 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400696 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500697 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500698 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500699
700 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500701 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500702
703 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500704 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500705
706 {
707 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500708 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500709 }
710}
711
712DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
713 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400714 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500715 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500716 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500717
718 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500719 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500720
721 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500722 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500723
724 {
725 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500726 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500727 }
728}
729
730DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
731 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400732 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500733 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -0500734 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500735
736 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -0500737 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500738}
739
740DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
741 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400742 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500743 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -0500744 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500745
746 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500747 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500748
749 {
750 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500751 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500752 }
753}
754
755DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
756 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400757 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500758 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -0500759 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500760
761 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500762 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500763
764 {
765 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500766 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500767 }
768}
769
770DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
771 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400772 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500773 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -0500774 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500775
776 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500777 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500778
779 {
780 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500781 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500782 }
783}
784
785DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
786 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400787 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500788 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500789 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500790
791 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500792 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500793
794 {
795 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500796 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500797 }
798}
799
800DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
801 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400802 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500803 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -0500804 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500805
806 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500807 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500808
809 {
810 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500811 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500812 }
813}
814
815DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
816 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400817 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500818 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500819 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500820
821 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500822 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500823
824 {
825 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500826 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500827 }
828}
829
830DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
831 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400832 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -0500834 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500835
836 {
837 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500838 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500839 }
840}
841
842DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
843 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400844 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500845 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -0500846 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500847
848 {
849 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500850 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851 }
852}
853
854DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
855 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400856 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500857 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -0500858 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500859
860 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -0500861 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500862
863 {
864 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500865 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866 }
867
868 {
869 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500870 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871 }
872
873 {
874 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500875 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500876 }
877
878 {
879 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500880 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500881 }
882}
883
884DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
885 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400886 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500887 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -0500888 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500889
890 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -0500891 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500892
893 {
894 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500895 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500896 }
897
898 {
899 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500900 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500901 }
902
903 {
904 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500905 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500906 }
907
908 {
909 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500910 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500911 }
912}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500913
914DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400915 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500916 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -0500917 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400918 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500919 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -0500920 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500921}
922
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500923DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400924 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400925 Var i(kInt_Type, "i", 0);
926 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500927 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500928 If(i > 5, Break())
929 ))
930 );
931 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500932 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
933 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500934
935 {
936 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400937 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500938 Break()
939 );
940 }
941}
942
943DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400944 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400945 Var i(kInt_Type, "i", 0);
946 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500947 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500948 If(i < 5, Continue())
949 ))
950 );
951 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500952 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
953 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500954
955 {
956 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400957 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500958 Continue()
959 );
960 }
961}
962
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500963DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400964 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400965 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500966 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -0500967 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500968 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -0500969 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500970
971 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400972 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500973 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500974 Declare(c).release();
975 }
976
977 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400978 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500979 Declare(d).release();
980 ExpectError error(r, "error: variable has already been declared\n");
981 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500982 }
983}
984
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500985DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
986 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
987 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -0500988 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500989}
990
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500991DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
992 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
993 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -0500994 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500995
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400996 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500997 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -0500998 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500999
1000 {
1001 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1002 Do(Block(), 7).release();
1003 }
1004}
1005
1006DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001007 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001008 Statement x = For(Statement(), Expression(), Expression(), Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001009 EXPECT_EQUAL(x, "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001010
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001011 Var i(kInt_Type, "i", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001012 Statement y = For(Declare(i), i < 10, ++i, i += 5);
John Stilesb4d7b582021-02-19 09:56:31 -05001013 EXPECT_EQUAL(y, "for (int i = 0; (i < 10); ++i) (i += 5);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001014
1015 {
1016 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1017 For(i = 0, i + 10, ++i, i += 5).release();
1018 }
1019}
1020
Ethan Nicholase2c05042021-02-03 10:27:22 -05001021DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001022 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001023 Var coords(kHalf2_Type, "coords");
1024 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001025 sk_FragColor() = Half4(coords, 0, 1)
1026 );
1027 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001028 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1029 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001030
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001031 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001032 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001033 Var x(kFloat_Type, "x");
1034 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001035 sqr.define(
1036 Return(x * x)
1037 );
1038 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1039 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1040 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1041 }
1042
1043 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001044 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001045 Var x(kFloat2_Type, "x");
1046 Var y(kFloat2_Type, "y");
1047 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001048 dot.define(
1049 Return(x * x + y * y)
1050 );
1051 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1052 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1053 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1054 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1055 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1056 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001057
1058 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001059 DSLWriter::Reset();
1060 Var x(kFloat_Type, "x");
1061 Var y(kFloat_Type, "y");
1062 DSLFunction pair(kFloat2_Type, "pair", x, y);
1063 pair.define(
1064 Return(Float2(x, y))
1065 );
1066 Var varArg1(kFloat_Type, "varArg1");
1067 Var varArg2(kFloat_Type, "varArg2");
1068 DSLWriter::MarkDeclared(varArg1);
1069 DSLWriter::MarkDeclared(varArg2);
1070 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1071 }
1072
1073 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001074 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001075 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001076 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001077 Return(true)
1078 );
1079 }
1080
1081 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001082 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001083 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001084 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001085 Return()
1086 );
1087 }
1088
1089 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001090 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001091 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001092 Var x(kFloat_Type, "x", 0);
1093 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001094 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001095 If(x == 1, Return(x))
1096 );
1097 }
1098
1099 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001100 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001101 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001102 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001103 Return(0)
1104 );
1105 }
1106
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001107 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001108 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001109 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001110 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001111 );
1112 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001113
1114 {
1115 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1116 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001117 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001118 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001119 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001120 );
1121 }
1122
1123 {
1124 ExpectError error(r, "error: variable has already been declared\n");
1125 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001126 DSLVar p(kFloat_Type);
1127 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001128 );
1129 Declare(p).release();
1130 }
1131
1132 {
1133 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1134 "values\n");
1135 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001136 DSLVar p(kFloat_Type, 1);
1137 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001138 );
1139 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001140}
1141
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001142DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1143 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001144 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001145 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001146 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001147
1148 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001149 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001150
1151 {
1152 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1153 If(a + b, a -= b).release();
1154 }
1155}
1156
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001157DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1158 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1159
1160 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001161 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001162
1163 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001164 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001165}
1166
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001167DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1168 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001169 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001170 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001171 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001172
1173 {
1174 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001175 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001176 }
1177
1178 {
1179 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001180 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001181 }
1182}
1183
Ethan Nicholascfefec02021-02-09 15:22:57 -05001184DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1185 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1186
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001187 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001188
John Stilesf3a28db2021-03-10 23:00:47 -05001189 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001190 Case(0, a = 0, Break()),
1191 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001192 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001193 Default(Discard())
1194 );
John Stilese1d1b082021-02-23 13:44:36 -05001195 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001196 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001197 case 0: (a = 0.0); break;
1198 case 1: (a = 1.0); continue;
1199 case 2: (a = 2.0);
1200 default: discard;
1201 }
1202 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001203
John Stiles642cde22021-02-23 14:57:01 -05001204 EXPECT_EQUAL(Switch(b),
1205 "switch (b) {}");
1206
1207 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1208 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001209
1210 {
John Stilese1d1b082021-02-23 13:44:36 -05001211 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001212 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001213 }
1214
1215 {
John Stilese1d1b082021-02-23 13:44:36 -05001216 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001217 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001218 }
1219
1220 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001221 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001222 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001223 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001224 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001225}
1226
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001227DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1228 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001229 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001230
John Stilesf04e09c2021-03-05 13:13:14 -05001231 EXPECT_EQUAL(a.x(),
1232 "a.x");
1233 EXPECT_EQUAL(a.y(),
1234 "a.y");
1235 EXPECT_EQUAL(a.z(),
1236 "a.z");
1237 EXPECT_EQUAL(a.w(),
1238 "a.w");
1239 EXPECT_EQUAL(a.r(),
1240 "a.x");
1241 EXPECT_EQUAL(a.g(),
1242 "a.y");
1243 EXPECT_EQUAL(a.b(),
1244 "a.z");
1245 EXPECT_EQUAL(a.a(),
1246 "a.w");
1247 EXPECT_EQUAL(Swizzle(a, R),
1248 "a.x");
1249 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1250 "float2(0.0, a.y)");
1251 EXPECT_EQUAL(Swizzle(a, B, G, G),
1252 "a.zyy");
1253 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1254 "float4(a.xyz, 1.0)");
1255 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1256 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001257}
1258
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001259DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1260 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1261 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001262 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001263
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001264 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001265 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001266 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001267
1268 {
1269 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001270 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001271 }
1272}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001273
1274DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1275 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001276 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001277
1278 EXPECT_EQUAL(a[0], "a[0]");
1279 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001280
1281 {
1282 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001283 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001284 }
1285
1286 {
1287 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001288 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001289 }
1290
1291 {
1292 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001293 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001294 }
1295}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001296
1297DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1298 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1299 // There is a Fract type on Mac which can conflict with our Fract builtin
1300 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001301 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1302 Var h3(kHalf3_Type, "h3");
1303 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001304 EXPECT_EQUAL(Abs(a), "abs(a)");
1305 EXPECT_EQUAL(All(b4), "all(b4)");
1306 EXPECT_EQUAL(Any(b4), "any(b4)");
1307 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1308 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1309 EXPECT_EQUAL(Cos(a), "cos(a)");
1310 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1311 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1312 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1313 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1314 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1315 EXPECT_EQUAL(Exp(a), "exp(a)");
1316 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1317 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1318 EXPECT_EQUAL(Floor(a), "floor(a)");
1319 EXPECT_EQUAL(Fract(a), "fract(a)");
1320 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1321 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1322 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1323 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1324 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1325 EXPECT_EQUAL(Length(a), "length(a)");
1326 EXPECT_EQUAL(Log(a), "log(a)");
1327 EXPECT_EQUAL(Log2(a), "log2(a)");
1328 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1329 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1330 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1331 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1332 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1333 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1334 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1335 EXPECT_EQUAL(Radians(a), "radians(a)");
1336 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1337 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1338 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1339 EXPECT_EQUAL(Sign(a), "sign(a)");
1340 EXPECT_EQUAL(Sin(a), "sin(a)");
1341 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1342 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1343 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1344 EXPECT_EQUAL(Tan(a), "tan(a)");
1345 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001346
1347 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1348 // one of them reports errors correctly
1349 {
1350 ExpectError error(r, "error: no match for ceil(bool)\n");
1351 Ceil(a == b).release();
1352 }
1353}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001354
1355DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001356 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001357
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001358 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001359 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001360 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001361
1362 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1363 // context, so we can't as yet Declare() variables with these modifiers.
1364 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001365 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001366 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001367 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001368
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001369 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001370 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001371 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001372
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001373 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001374 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001375 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001376
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001377 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001378 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001379 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001380 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001381
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001382 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001383 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1384 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001385 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001386
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001387 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001388 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1389 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001390 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001391
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001392 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001393 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001394 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001395}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001396
1397DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001398 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001399
1400 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001401 Field(kFloat_Type, "x"),
1402 Field(kBool_Type, "b"),
1403 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001404 );
1405 DSLVar result(simpleStruct, "result");
1406 DSLFunction(simpleStruct, "returnStruct").define(
1407 Declare(result),
1408 result.field("x") = 123,
1409 result.field("b") = result.field("x") > 0,
1410 result.field("a")[0] = result.field("x"),
1411 Return(result)
1412 );
1413 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001414 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1415 "struct SimpleStruct { float x; bool b; float[3] a; };");
1416 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1417 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1418 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001419
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001420 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001421 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001422 Field(simpleStruct, "simple")
1423 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001424 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1425 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001426 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001427}