blob: dc806f6282ebfbfa82256506b7a8873ecf0916e7 [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
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400165 DSLVar x(kFloat_Type, "x");
166 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
167 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
168
169 DSLVar y(kFloat2_Type, "y");
170 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
171 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
172
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500173 {
174 ExpectError error(r, "error: floating point value is infinite\n");
175 Float(std::numeric_limits<float>::infinity()).release();
176 }
177
178 {
179 ExpectError error(r, "error: floating point value is NaN\n");
180 Float(std::numeric_limits<float>::quiet_NaN()).release();
181 }
182
183 {
184 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
185 " but found 4)\n");
186 Float2(Float4(1)).release();
187 }
188
189 {
190 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
191 " but found 3)\n");
192 Float4(Float3(1)).release();
193 }
194}
195
196DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
197 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
198 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500199 REPORTER_ASSERT(r,
200 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500201
202 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500203 REPORTER_ASSERT(r,
204 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500205
John Stilesb9e4f642021-03-05 09:11:38 -0500206 EXPECT_EQUAL(Half2(0),
207 "half2(0.0)");
208 EXPECT_EQUAL(Half2(-0.5, 1),
209 "half2(-0.5, 1.0)");
210 EXPECT_EQUAL(Half3(0.75),
211 "half3(0.75)");
212 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
213 "half3(0.0, 1.0, -2.0)");
214 EXPECT_EQUAL(Half3(0, 1, 2),
215 "half3(0.0, 1.0, 2.0)");
216 EXPECT_EQUAL(Half4(0),
217 "half4(0.0)");
218 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
219 "half4(0.0, 1.0, 2.0, 3.0)");
220 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
221 "half4(0.0, 1.0, 2.0, 3.0)");
222 EXPECT_EQUAL(Half4(0, 1, 2, 3),
223 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500224
225 {
226 ExpectError error(r, "error: floating point value is infinite\n");
227 Half(std::numeric_limits<float>::infinity()).release();
228 }
229
230 {
231 ExpectError error(r, "error: floating point value is NaN\n");
232 Half(std::numeric_limits<float>::quiet_NaN()).release();
233 }
234
235 {
236 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
237 " but found 4)\n");
238 Half2(Half4(1)).release();
239 }
240
241 {
242 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
243 " but found 3)\n");
244 Half4(Half3(1)).release();
245 }
246}
247
248DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
249 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500250
John Stilesb9e4f642021-03-05 09:11:38 -0500251 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
252 "2147483647");
253 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
254 "int2(-2147483648)");
255 EXPECT_EQUAL(Int2(0, 1),
256 "int2(0, 1)");
257 EXPECT_EQUAL(Int3(0),
258 "int3(0)");
259 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
260 "int3(0, 1, -2)");
261 EXPECT_EQUAL(Int3(0, 1, 2),
262 "int3(0, 1, 2)");
263 EXPECT_EQUAL(Int4(0),
264 "int4(0)");
265 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
266 "int4(0, 1, 2, 3)");
267 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
268 "int4(0, 1, 2, 3)");
269 EXPECT_EQUAL(Int4(0, 1, 2, 3),
270 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500271
272 {
273 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
274 " but found 4)\n");
275 Int2(Int4(1)).release();
276 }
277
278 {
279 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
280 " but found 3)\n");
281 Int4(Int3(1)).release();
282 }
283}
284
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400285DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
286 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
287
288 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
289 "4294967295");
290 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
291 "uint2(0)");
292 EXPECT_EQUAL(UInt2(0, 1),
293 "uint2(0, 1)");
294 EXPECT_EQUAL(UInt3(0),
295 "uint3(0)");
296 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
297 "uint3(0, 1, -2)");
298 EXPECT_EQUAL(UInt3(0, 1, 2),
299 "uint3(0, 1, 2)");
300 EXPECT_EQUAL(UInt4(0),
301 "uint4(0)");
302 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
303 "uint4(0, 1, 2, 3)");
304 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
305 "uint4(0, 1, 2, 3)");
306 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
307 "uint4(0, 1, 2, 3)");
308
309 {
310 ExpectError error(r, "error: invalid arguments to 'uint2' constructor (expected 2 scalars,"
311 " but found 4)\n");
312 UInt2(UInt4(1)).release();
313 }
314
315 {
316 ExpectError error(r, "error: invalid arguments to 'uint4' constructor (expected 4 scalars,"
317 " but found 3)\n");
318 UInt4(UInt3(1)).release();
319 }
320}
321
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500322DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
323 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500324
John Stilesb9e4f642021-03-05 09:11:38 -0500325 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
326 "32767");
327 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
328 "short2(-32768)");
329 EXPECT_EQUAL(Short2(0, 1),
330 "short2(0, 1)");
331 EXPECT_EQUAL(Short3(0),
332 "short3(0)");
333 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
334 "short3(0, 1, -2)");
335 EXPECT_EQUAL(Short3(0, 1, 2),
336 "short3(0, 1, 2)");
337 EXPECT_EQUAL(Short4(0),
338 "short4(0)");
339 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
340 "short4(0, 1, 2, 3)");
341 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
342 "short4(0, 1, 2, 3)");
343 EXPECT_EQUAL(Short4(0, 1, 2, 3),
344 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500345
346 {
347 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
348 " but found 4)\n");
349 Short2(Short4(1)).release();
350 }
351
352 {
353 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
354 " but found 3)\n");
355 Short4(Short3(1)).release();
356 }
357}
358
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400359DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
360 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
361
362 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
363 "65535");
364 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
365 "ushort2(0)");
366 EXPECT_EQUAL(UShort2(0, 1),
367 "ushort2(0, 1)");
368 EXPECT_EQUAL(UShort3(0),
369 "ushort3(0)");
370 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
371 "ushort3(0, 1, -2)");
372 EXPECT_EQUAL(UShort3(0, 1, 2),
373 "ushort3(0, 1, 2)");
374 EXPECT_EQUAL(UShort4(0),
375 "ushort4(0)");
376 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
377 "ushort4(0, 1, 2, 3)");
378 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
379 "ushort4(0, 1, 2, 3)");
380 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
381 "ushort4(0, 1, 2, 3)");
382
383 {
384 ExpectError error(r, "error: invalid arguments to 'ushort2' constructor (expected 2 "
385 "scalars, but found 4)\n");
386 UShort2(UShort4(1)).release();
387 }
388
389 {
390 ExpectError error(r, "error: invalid arguments to 'ushort4' constructor (expected 4 "
391 "scalars, but found 3)\n");
392 UShort4(UShort3(1)).release();
393 }
394}
395
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500396DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
397 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500398
John Stilesb9e4f642021-03-05 09:11:38 -0500399 EXPECT_EQUAL(Bool2(false),
400 "bool2(false)");
401 EXPECT_EQUAL(Bool2(false, true),
402 "bool2(false, true)");
403 EXPECT_EQUAL(Bool3(false),
404 "bool3(false)");
405 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
406 "bool3(false, true, false)");
407 EXPECT_EQUAL(Bool3(false, true, false),
408 "bool3(false, true, false)");
409 EXPECT_EQUAL(Bool4(false),
410 "bool4(false)");
411 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
412 "bool4(false, true, false, true)");
413 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
414 "bool4(false, true, false, true)");
415 EXPECT_EQUAL(Bool4(false, true, false, true),
416 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500417
418 {
419 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
420 " but found 4)\n");
421 Bool2(Bool4(true)).release();
422 }
423
424 {
425 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
426 " but found 3)\n");
427 Bool4(Bool3(true)).release();
428 }
429}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500430
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400431DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
432 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
433 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
434 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
435 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
436 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
437 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
438 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
439 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
440 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
441 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
442 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
443 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
444
445 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
446 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
447 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
448 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
449 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
450 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
451 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
452 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
453 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
454 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
455 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
456
457 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
458 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
459 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
460 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
461 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
462 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
463 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
464 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
465 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
466 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
467 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
468
469 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
470 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
471 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
472 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
473 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
474 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
475 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
476 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
477 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
478 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
479 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
480
481 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
482 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
483 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
484 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
485 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
486 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
487 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
488 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
489 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
490 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
491 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
492
493 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
494 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
495 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
496 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
497 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
498 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
499 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
500 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
501 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
502 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
503 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
504
505 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
506 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
507 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
508 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
509 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
510 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
511 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
512 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
513 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
514 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
515 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
516}
517
Ethan Nicholas84558932021-04-12 16:56:37 -0400518DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
519 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
520 Var f22(kFloat2x2_Type, "f22");
521 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
522 Var f32(kFloat3x2_Type, "f32");
523 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
524 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
525 Var f42(kFloat4x2_Type, "f42");
526 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
527 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
528 Var f23(kFloat2x3_Type, "f23");
529 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
530 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
531 Var f33(kFloat3x3_Type, "f33");
532 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
533 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
534 Var f43(kFloat4x3_Type, "f43");
535 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
536 "(f43 = float4x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
537 Var f24(kFloat2x4_Type, "f24");
538 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
539 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
540 Var f34(kFloat3x4_Type, "f34");
541 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
542 "(f34 = float3x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
543 Var f44(kFloat4x4_Type, "f44");
544 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
545
546 Var h22(kHalf2x2_Type, "h22");
547 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
548 Var h32(kHalf3x2_Type, "h32");
549 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
550 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
551 Var h42(kHalf4x2_Type, "h42");
552 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
553 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
554 Var h23(kHalf2x3_Type, "h23");
555 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
556 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
557 Var h33(kHalf3x3_Type, "h33");
558 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
559 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
560 Var h43(kHalf4x3_Type, "h43");
561 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
562 "(h43 = half4x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
563 Var h24(kHalf2x4_Type, "h24");
564 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
565 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
566 Var h34(kHalf3x4_Type, "h34");
567 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
568 "(h34 = half3x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0))");
569 Var h44(kHalf4x4_Type, "h44");
570 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
571
572 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
573 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
574 EXPECT_EQUAL(h42[0][1], "h42[0].y");
575 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
576 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
577 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
578
579 {
580 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
581 "scalars, but found 2)\n");
582 DSLExpression(Float3x3(Float2(1))).release();
583 }
584
585 {
586 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
587 "scalars, but found 5)\n");
588 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
589 }
590
591 {
592 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
593 DSLExpression(f43 * Float3(1)).release();
594 }
595
596 {
597 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
598 "'float3x3'\n");
599 DSLExpression(f43 = f33).release();
600 }
601
602 {
603 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
604 "'float2x2'\n");
605 DSLExpression(h22 = f22).release();
606 }
607
608 {
609 ExpectError error(r,
610 "error: no match for inverse(float4x3)\n");
611 DSLExpression(Inverse(f43)).release();
612 }
613}
614
Ethan Nicholas92969f22021-01-13 10:38:59 -0500615DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
616 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400617 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500618
John Stiles8f440b42021-03-05 16:48:56 -0500619 EXPECT_EQUAL(a + b,
620 "(a + b)");
621 EXPECT_EQUAL(a + 1,
622 "(a + 1.0)");
623 EXPECT_EQUAL(0.5 + a + -99,
624 "((0.5 + a) + -99.0)");
625 EXPECT_EQUAL(a += b + 1,
626 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500627 EXPECT_EQUAL(+a,
628 "a");
629 EXPECT_EQUAL(+(a + b),
630 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500631
632 {
633 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500634 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500635 }
636
637 {
638 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500639 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500640 }
641
642 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500643 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500644 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500645 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500646
647 {
648 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400649 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500650 DSLExpression(+c);
651 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500652}
653
654DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
655 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400656 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500657
John Stiles8f440b42021-03-05 16:48:56 -0500658 EXPECT_EQUAL(a - b,
659 "(a - b)");
660 EXPECT_EQUAL(a - 1,
661 "(a - 1)");
662 EXPECT_EQUAL(2 - a - b,
663 "((2 - a) - b)");
664 EXPECT_EQUAL(a -= b + 1,
665 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500666 EXPECT_EQUAL(-a,
667 "-a");
668 EXPECT_EQUAL(-(a - b),
669 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500670
671 {
672 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500673 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674 }
675
676 {
677 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500678 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500679 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500680
681 {
682 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500683 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500684 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500685
686 {
687 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400688 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500689 DSLExpression(-c);
690 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500691}
692
693DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
694 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400695 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500696
John Stiles8f440b42021-03-05 16:48:56 -0500697 EXPECT_EQUAL(a * b,
698 "(a * b)");
699 EXPECT_EQUAL(a * 2,
700 "(a * 2.0)");
701 EXPECT_EQUAL(0.5 * a * -99,
702 "((0.5 * a) * -99.0)");
703 EXPECT_EQUAL(a *= b + 1,
704 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500705
706 {
707 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500708 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500709 }
710
711 {
712 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500713 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500714 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500715
716 {
717 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500718 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500719 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500720}
721
722DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
723 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400724 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500725
John Stiles8f440b42021-03-05 16:48:56 -0500726 EXPECT_EQUAL(a / b,
727 "(a / b)");
728 EXPECT_EQUAL(a / 2,
729 "(a / 2.0)");
730 EXPECT_EQUAL(0.5 / a / -99,
731 "((0.5 / a) / -99.0)");
732 EXPECT_EQUAL(b / (a - 1),
733 "(b / (a - 1.0))");
734 EXPECT_EQUAL(a /= b + 1,
735 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500736
737 {
738 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500739 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500740 }
741
742 {
743 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500744 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500745 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500746
747 {
748 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500749 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500750 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500751
752 {
753 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500754 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500755 }
756
757 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400758 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500759 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500760 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500761 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500762}
763
764DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
765 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400766 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500767 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500768 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500769
770 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500771 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500772
773 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500774 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500775
776 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500777 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500778
779 {
780 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500781 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500782 }
783
784 {
785 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500786 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500787 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500788
789 {
790 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500791 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500792 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500793
794 {
795 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500796 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500797 }
798
799 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400800 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500801 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500802 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500803 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500804}
805
806DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
807 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400808 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500809 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500810 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500811
812 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500813 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500814
815 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500816 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500817
818 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500819 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500820
821 {
822 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500823 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500824 }
825
826 {
827 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500828 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500829 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500830
831 {
832 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500833 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500834 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500835}
836
837DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
838 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400839 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500840 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500841 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500842
843 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500844 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500845
846 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500847 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500848
849 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500850 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851
852 {
853 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500854 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500855 }
856
857 {
858 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500859 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500860 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500861
862 {
863 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500864 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500865 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866}
867
868DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
869 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400870 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500872 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500873
874 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500875 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500876
877 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500878 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500879
880 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500881 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500882
883 {
884 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500885 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500886 }
887
888 {
889 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500890 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500891 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500892
893 {
894 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500895 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500896 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500897}
898
899DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
900 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400901 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500902 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500903 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500904
905 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500906 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500907
908 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500909 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500910
911 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500912 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500913
914 {
915 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500916 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500917 }
918
919 {
920 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500921 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500922 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500923
924 {
925 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500926 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500927 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500928}
929
930DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
931 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400932 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500933 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500934 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500935
936 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500937 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500938
939 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500940 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500941
942 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500943 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500944
945 {
946 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500947 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500948 }
949
950 {
951 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500952 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500953 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500954
955 {
956 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500957 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500958 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500959}
960
961DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
962 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400963 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500964 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500965 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500966
967 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500968 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500969
970 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500971 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500972
973 {
974 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500975 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500976 }
977}
978
979DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
980 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400981 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500982 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500983 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500984
985 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500986 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500987
988 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500989 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500990
991 {
992 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500993 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500994 }
995}
996
997DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
998 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400999 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001000 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001001 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001002
1003 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001004 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001005}
1006
1007DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1008 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001009 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001010 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001011 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001012
1013 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001014 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001015
1016 {
1017 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001018 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001019 }
1020}
1021
1022DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1023 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001024 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001025 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001026 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001027
1028 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001029 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001030
1031 {
1032 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001033 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001034 }
1035}
1036
1037DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1038 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001039 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001040 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001041 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001042
1043 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001044 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001045
1046 {
1047 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001048 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001049 }
1050}
1051
1052DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1053 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001054 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001055 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001056 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001057
1058 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001059 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001060
1061 {
1062 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001063 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001064 }
1065}
1066
1067DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1068 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001069 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001070 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001071 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001072
1073 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001074 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001075
1076 {
1077 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001078 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001079 }
1080}
1081
1082DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1083 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001084 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001085 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001086 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001087
1088 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001089 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001090
1091 {
1092 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001093 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001094 }
1095}
1096
1097DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1098 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001099 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001100 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001101 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001102
1103 {
1104 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001105 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001106 }
1107}
1108
1109DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1110 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001111 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001112 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001113 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001114
1115 {
1116 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001117 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001118 }
1119}
1120
1121DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1122 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001123 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001124 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001125 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001126
1127 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001128 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001129
1130 {
1131 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001132 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001133 }
1134
1135 {
1136 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001137 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001138 }
1139
1140 {
1141 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001142 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001143 }
1144
1145 {
1146 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001147 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001148 }
1149}
1150
1151DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1152 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001153 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001154 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001155 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001156
1157 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001158 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001159
1160 {
1161 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001162 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001163 }
1164
1165 {
1166 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001167 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001168 }
1169
1170 {
1171 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001172 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001173 }
1174
1175 {
1176 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001177 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001178 }
1179}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001180
1181DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001182 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001183 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -05001184 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001185 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001186 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -05001187 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001188
1189 Statement z = (If(a > 0, --a), ++b);
1190 EXPECT_EQUAL(z, "if ((a > 0)) --a; ++b;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001191}
1192
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001193DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001194 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001195 Var i(kInt_Type, "i", 0);
1196 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001197 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001198 If(i > 5, Break())
1199 ))
1200 );
1201 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001202 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1203 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001204
1205 {
1206 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001207 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001208 Break()
1209 );
1210 }
1211}
1212
1213DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001214 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001215 Var i(kInt_Type, "i", 0);
1216 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001217 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001218 If(i < 5, Continue())
1219 ))
1220 );
1221 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001222 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1223 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001224
1225 {
1226 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001227 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001228 Continue()
1229 );
1230 }
1231}
1232
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001233DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001234 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001235 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001236 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001237 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001238 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001239 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001240
1241 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001242 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001243 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001244 Declare(c).release();
1245 }
1246
1247 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001248 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001249 Declare(d).release();
1250 ExpectError error(r, "error: variable has already been declared\n");
1251 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001252 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001253
1254 {
1255 Var e(kUniform_Modifier, kInt_Type, "e");
1256 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1257 Declare(e).release();
1258 }
1259}
1260
1261DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
1262 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
1263 Var x(kInt_Type, "x", 0);
1264 DeclareGlobal(x);
1265 Var y(kUniform_Modifier, kFloat2_Type, "y");
1266 DeclareGlobal(y);
1267 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1268 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1269 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001270}
1271
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001272DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1273 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1274 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001275 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001276}
1277
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001278DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1279 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1280 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001281 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001282
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001283 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001284 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001285 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001286
1287 {
1288 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1289 Do(Block(), 7).release();
1290 }
1291}
1292
1293DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001294 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
John Stiles8676ebe2021-04-20 15:30:41 -04001295 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1296 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001297
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001298 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001299 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1300 "for (int i = 0; (i < 10); ++i) (i += 5);");
1301
1302 Var j(kInt_Type, "j", 0);
1303 Var k(kInt_Type, "k", 10);
1304 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1305 {
1306 int j = 0;
1307 int k = 10;
1308 for (; (j < k); ++j) {}
1309 }
1310 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001311
1312 {
1313 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1314 For(i = 0, i + 10, ++i, i += 5).release();
1315 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001316
1317 {
1318 ExpectError error(r, "error: invalid for loop initializer\n");
1319 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1320 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001321}
1322
Ethan Nicholase2c05042021-02-03 10:27:22 -05001323DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001324 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001325 Var coords(kHalf2_Type, "coords");
1326 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001327 sk_FragColor() = Half4(coords, 0, 1)
1328 );
1329 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001330 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1331 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001332
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001333 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001334 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001335 Var x(kFloat_Type, "x");
1336 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001337 sqr.define(
1338 Return(x * x)
1339 );
1340 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1341 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1342 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1343 }
1344
1345 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001346 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001347 Var x(kFloat2_Type, "x");
1348 Var y(kFloat2_Type, "y");
1349 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001350 dot.define(
1351 Return(x * x + y * y)
1352 );
1353 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1354 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1355 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1356 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1357 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1358 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001359
1360 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001361 DSLWriter::Reset();
1362 Var x(kFloat_Type, "x");
1363 Var y(kFloat_Type, "y");
1364 DSLFunction pair(kFloat2_Type, "pair", x, y);
1365 pair.define(
1366 Return(Float2(x, y))
1367 );
1368 Var varArg1(kFloat_Type, "varArg1");
1369 Var varArg2(kFloat_Type, "varArg2");
1370 DSLWriter::MarkDeclared(varArg1);
1371 DSLWriter::MarkDeclared(varArg2);
1372 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1373 }
1374
1375 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001376 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001377 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001378 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001379 Return(true)
1380 );
1381 }
1382
1383 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001384 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001385 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001386 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001387 Return()
1388 );
1389 }
1390
1391 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001392 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001393 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001394 Var x(kFloat_Type, "x", 0);
1395 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001396 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001397 If(x == 1, Return(x))
1398 );
1399 }
1400
1401 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001402 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001403 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001404 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001405 Return(0)
1406 );
1407 }
1408
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001409 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001410 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001411 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001412 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001413 );
1414 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001415
1416 {
1417 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1418 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001419 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001420 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001421 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001422 );
1423 }
1424
1425 {
1426 ExpectError error(r, "error: variable has already been declared\n");
1427 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001428 DSLVar p(kFloat_Type);
1429 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001430 );
1431 Declare(p).release();
1432 }
1433
1434 {
1435 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1436 "values\n");
1437 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001438 DSLVar p(kFloat_Type, 1);
1439 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001440 );
1441 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001442}
1443
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001444DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1445 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001446 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001447 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001448 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001449
1450 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001451 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001452
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001453 Statement z = StaticIf(a > b, a -= b, b -= a);
1454 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1455
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001456 {
1457 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1458 If(a + b, a -= b).release();
1459 }
1460}
1461
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001462DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1463 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1464
1465 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001466 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001467
1468 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001469 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001470}
1471
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001472DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1473 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001474 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001475 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001476 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001477
1478 {
1479 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001480 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001481 }
1482
1483 {
1484 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001485 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001486 }
1487}
1488
Ethan Nicholascfefec02021-02-09 15:22:57 -05001489DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1490 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1491
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001492 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001493
John Stilesf3a28db2021-03-10 23:00:47 -05001494 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001495 Case(0, a = 0, Break()),
1496 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001497 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001498 Default(Discard())
1499 );
John Stilese1d1b082021-02-23 13:44:36 -05001500 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001501 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001502 case 0: (a = 0.0); break;
1503 case 1: (a = 1.0); continue;
1504 case 2: (a = 2.0);
1505 default: discard;
1506 }
1507 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001508
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001509 Statement y = StaticSwitch(b,
1510 Case(0, a = 0, Break()),
1511 Case(1, a = 1, Continue()),
1512 Case(2, a = 2 /*Fallthrough*/),
1513 Default(Discard())
1514 );
1515 EXPECT_EQUAL(y, R"(
1516 @switch (b) {
1517 case 0: (a = 0.0); break;
1518 case 1: (a = 1.0); continue;
1519 case 2: (a = 2.0);
1520 default: discard;
1521 }
1522 )");
1523
John Stiles642cde22021-02-23 14:57:01 -05001524 EXPECT_EQUAL(Switch(b),
1525 "switch (b) {}");
1526
1527 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1528 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001529
1530 {
John Stilese1d1b082021-02-23 13:44:36 -05001531 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001532 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001533 }
1534
1535 {
John Stilese1d1b082021-02-23 13:44:36 -05001536 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001537 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001538 }
1539
1540 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001541 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001542 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001543 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001544 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001545}
1546
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001547DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1548 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001549 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001550
John Stilesf04e09c2021-03-05 13:13:14 -05001551 EXPECT_EQUAL(a.x(),
1552 "a.x");
1553 EXPECT_EQUAL(a.y(),
1554 "a.y");
1555 EXPECT_EQUAL(a.z(),
1556 "a.z");
1557 EXPECT_EQUAL(a.w(),
1558 "a.w");
1559 EXPECT_EQUAL(a.r(),
1560 "a.x");
1561 EXPECT_EQUAL(a.g(),
1562 "a.y");
1563 EXPECT_EQUAL(a.b(),
1564 "a.z");
1565 EXPECT_EQUAL(a.a(),
1566 "a.w");
1567 EXPECT_EQUAL(Swizzle(a, R),
1568 "a.x");
1569 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1570 "float2(0.0, a.y)");
1571 EXPECT_EQUAL(Swizzle(a, B, G, G),
1572 "a.zyy");
1573 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1574 "float4(a.xyz, 1.0)");
1575 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1576 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001577}
1578
John Stiles08771b02021-04-26 09:35:10 -04001579
1580DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
1581 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
1582
1583 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1584 Var a;
1585 if (true)
1586 {
1587 Var(kInt_Type, "a").swap(a);
1588 }
1589
1590 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1591 "{ int a; (a = 123); }");
1592}
1593
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001594DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1595 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1596 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001597 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001598
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001599 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001600 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001601 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001602
1603 {
1604 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001605 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001606 }
1607}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001608
1609DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1610 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001611 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001612
1613 EXPECT_EQUAL(a[0], "a[0]");
1614 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001615
1616 {
1617 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001618 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001619 }
1620
1621 {
1622 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001623 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001624 }
1625
1626 {
1627 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001628 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001629 }
1630}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001631
1632DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1633 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1634 // There is a Fract type on Mac which can conflict with our Fract builtin
1635 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001636 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1637 Var h3(kHalf3_Type, "h3");
1638 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001639 EXPECT_EQUAL(Abs(a), "abs(a)");
1640 EXPECT_EQUAL(All(b4), "all(b4)");
1641 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001642 EXPECT_EQUAL(Atan(a), "atan(a)");
1643 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001644 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1645 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1646 EXPECT_EQUAL(Cos(a), "cos(a)");
1647 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1648 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1649 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1650 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1651 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1652 EXPECT_EQUAL(Exp(a), "exp(a)");
1653 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1654 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1655 EXPECT_EQUAL(Floor(a), "floor(a)");
1656 EXPECT_EQUAL(Fract(a), "fract(a)");
1657 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1658 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1659 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1660 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1661 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1662 EXPECT_EQUAL(Length(a), "length(a)");
1663 EXPECT_EQUAL(Log(a), "log(a)");
1664 EXPECT_EQUAL(Log2(a), "log2(a)");
1665 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1666 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1667 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1668 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1669 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1670 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1671 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1672 EXPECT_EQUAL(Radians(a), "radians(a)");
1673 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1674 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1675 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1676 EXPECT_EQUAL(Sign(a), "sign(a)");
1677 EXPECT_EQUAL(Sin(a), "sin(a)");
1678 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1679 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1680 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1681 EXPECT_EQUAL(Tan(a), "tan(a)");
1682 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001683
1684 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1685 // one of them reports errors correctly
1686 {
1687 ExpectError error(r, "error: no match for ceil(bool)\n");
1688 Ceil(a == b).release();
1689 }
1690}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001691
1692DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001693 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001694
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001695 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001696 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001697 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001698
1699 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1700 // context, so we can't as yet Declare() variables with these modifiers.
1701 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001702 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001703 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001704 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001705
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001706 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001707 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001708 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001709
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001710 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001711 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001712 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001713
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001714 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001715 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001716 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001717 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001718
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001719 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001720 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1721 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001722 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001723
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001724 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001725 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1726 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001727 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001728
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001729 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001730 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001731 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001732 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001733}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001734
Ethan Nicholas624a5292021-04-16 14:54:43 -04001735DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001736 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1737 SkSL::ProgramKind::kFragmentProcessor);
1738 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1739 EXPECT_EQUAL(Sample(child), "sample(child)");
1740 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1741 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001742 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001743
1744 {
1745 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1746 Sample(child, true).release();
1747 }
1748}
1749
Ethan Nicholas624a5292021-04-16 14:54:43 -04001750DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
1751 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
Brian Osman552fcb92021-04-28 17:41:57 -04001752 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001753 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001754 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001755
1756 {
Greg Danielc2cca5a2021-05-04 13:36:16 +00001757 ExpectError error(r, "error: expected 'float2', but found 'half4'\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001758 Sample(shader, Half4(1)).release();
1759 }
1760}
1761
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001762DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001763 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001764
1765 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001766 Field(kFloat_Type, "x"),
1767 Field(kBool_Type, "b"),
1768 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001769 );
1770 DSLVar result(simpleStruct, "result");
1771 DSLFunction(simpleStruct, "returnStruct").define(
1772 Declare(result),
1773 result.field("x") = 123,
1774 result.field("b") = result.field("x") > 0,
1775 result.field("a")[0] = result.field("x"),
1776 Return(result)
1777 );
1778 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001779 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1780 "struct SimpleStruct { float x; bool b; float[3] a; };");
1781 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1782 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1783 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001784
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001785 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001786 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001787 Field(simpleStruct, "simple")
1788 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001789 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1790 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001791 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001792}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001793
1794DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1795 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1796 std::vector<Wrapper<DSLExpression>> exprs;
1797 exprs.push_back(DSLExpression(1));
1798 exprs.emplace_back(2.0);
1799 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1800 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1801
1802 std::vector<Wrapper<DSLVar>> vars;
1803 vars.emplace_back(DSLVar(kInt_Type, "x"));
1804 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0]).name() == "x");
1805}