blob: bd07534f89470f53b6e66003f1d46b5d270c2165 [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:
Brian Osman9eb0bb62021-05-11 22:03:54 +000030 AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true,
Ethan Nicholasee49efc2021-04-09 15:33:53 -040031 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
Brian Osman9eb0bb62021-05-11 22:03:54 +000032 Start(gpu->shaderCompiler(), kind);
33 DSLWriter::Instance().fMangle = false;
34 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(); }
Ethan Nicholas722cb672021-05-06 10:47:06 -0400117static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500118static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
119
120template <typename T>
121static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
122 SkSL::String actual = stringize(input);
123 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
124 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
125 lineNumber, expected, actual.c_str());
126 }
127}
128
129template <typename T>
130static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
131 // This overload allows temporary values to be passed to expect_equal.
132 return expect_equal(r, lineNumber, dsl, expected);
133}
134
135#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
136
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500137DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
138 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
139 Expression e1 = Float(std::numeric_limits<float>::max());
140 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
141 std::numeric_limits<float>::max());
142
143 Expression e2 = Float(std::numeric_limits<float>::min());
144 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
145 std::numeric_limits<float>::min());
146
John Stilesb4d7b582021-02-19 09:56:31 -0500147 EXPECT_EQUAL(Float2(0),
148 "float2(0.0)");
149 EXPECT_EQUAL(Float2(-0.5, 1),
150 "float2(-0.5, 1.0)");
151 EXPECT_EQUAL(Float3(0.75),
152 "float3(0.75)");
153 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500154 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500155 EXPECT_EQUAL(Float3(0, 1, 2),
156 "float3(0.0, 1.0, 2.0)");
157 EXPECT_EQUAL(Float4(0),
158 "float4(0.0)");
159 EXPECT_EQUAL(Float4(Float2(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, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500162 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500163 EXPECT_EQUAL(Float4(0, 1, 2, 3),
164 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500165
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400166 DSLVar x(kFloat_Type, "x");
167 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
168 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
169
170 DSLVar y(kFloat2_Type, "y");
171 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
172 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
173
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500174 {
175 ExpectError error(r, "error: floating point value is infinite\n");
176 Float(std::numeric_limits<float>::infinity()).release();
177 }
178
179 {
180 ExpectError error(r, "error: floating point value is NaN\n");
181 Float(std::numeric_limits<float>::quiet_NaN()).release();
182 }
183
184 {
185 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
186 " but found 4)\n");
187 Float2(Float4(1)).release();
188 }
189
190 {
191 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
192 " but found 3)\n");
193 Float4(Float3(1)).release();
194 }
195}
196
197DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
198 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
199 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500200 REPORTER_ASSERT(r,
201 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500202
203 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500204 REPORTER_ASSERT(r,
205 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500206
John Stilesb9e4f642021-03-05 09:11:38 -0500207 EXPECT_EQUAL(Half2(0),
208 "half2(0.0)");
209 EXPECT_EQUAL(Half2(-0.5, 1),
210 "half2(-0.5, 1.0)");
211 EXPECT_EQUAL(Half3(0.75),
212 "half3(0.75)");
213 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
214 "half3(0.0, 1.0, -2.0)");
215 EXPECT_EQUAL(Half3(0, 1, 2),
216 "half3(0.0, 1.0, 2.0)");
217 EXPECT_EQUAL(Half4(0),
218 "half4(0.0)");
219 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
220 "half4(0.0, 1.0, 2.0, 3.0)");
221 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
222 "half4(0.0, 1.0, 2.0, 3.0)");
223 EXPECT_EQUAL(Half4(0, 1, 2, 3),
224 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500225
226 {
227 ExpectError error(r, "error: floating point value is infinite\n");
228 Half(std::numeric_limits<float>::infinity()).release();
229 }
230
231 {
232 ExpectError error(r, "error: floating point value is NaN\n");
233 Half(std::numeric_limits<float>::quiet_NaN()).release();
234 }
235
236 {
237 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
238 " but found 4)\n");
239 Half2(Half4(1)).release();
240 }
241
242 {
243 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
244 " but found 3)\n");
245 Half4(Half3(1)).release();
246 }
247}
248
249DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
250 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500251
John Stilesb9e4f642021-03-05 09:11:38 -0500252 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
253 "2147483647");
254 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
255 "int2(-2147483648)");
256 EXPECT_EQUAL(Int2(0, 1),
257 "int2(0, 1)");
258 EXPECT_EQUAL(Int3(0),
259 "int3(0)");
260 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
261 "int3(0, 1, -2)");
262 EXPECT_EQUAL(Int3(0, 1, 2),
263 "int3(0, 1, 2)");
264 EXPECT_EQUAL(Int4(0),
265 "int4(0)");
266 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
267 "int4(0, 1, 2, 3)");
268 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
269 "int4(0, 1, 2, 3)");
270 EXPECT_EQUAL(Int4(0, 1, 2, 3),
271 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500272
273 {
274 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
275 " but found 4)\n");
276 Int2(Int4(1)).release();
277 }
278
279 {
280 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
281 " but found 3)\n");
282 Int4(Int3(1)).release();
283 }
284}
285
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400286DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
287 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
288
289 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
290 "4294967295");
291 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
292 "uint2(0)");
293 EXPECT_EQUAL(UInt2(0, 1),
294 "uint2(0, 1)");
295 EXPECT_EQUAL(UInt3(0),
296 "uint3(0)");
297 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
298 "uint3(0, 1, -2)");
299 EXPECT_EQUAL(UInt3(0, 1, 2),
300 "uint3(0, 1, 2)");
301 EXPECT_EQUAL(UInt4(0),
302 "uint4(0)");
303 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
304 "uint4(0, 1, 2, 3)");
305 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
306 "uint4(0, 1, 2, 3)");
307 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
308 "uint4(0, 1, 2, 3)");
309
310 {
311 ExpectError error(r, "error: invalid arguments to 'uint2' constructor (expected 2 scalars,"
312 " but found 4)\n");
313 UInt2(UInt4(1)).release();
314 }
315
316 {
317 ExpectError error(r, "error: invalid arguments to 'uint4' constructor (expected 4 scalars,"
318 " but found 3)\n");
319 UInt4(UInt3(1)).release();
320 }
321}
322
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500323DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
324 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500325
John Stilesb9e4f642021-03-05 09:11:38 -0500326 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
327 "32767");
328 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
329 "short2(-32768)");
330 EXPECT_EQUAL(Short2(0, 1),
331 "short2(0, 1)");
332 EXPECT_EQUAL(Short3(0),
333 "short3(0)");
334 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
335 "short3(0, 1, -2)");
336 EXPECT_EQUAL(Short3(0, 1, 2),
337 "short3(0, 1, 2)");
338 EXPECT_EQUAL(Short4(0),
339 "short4(0)");
340 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
341 "short4(0, 1, 2, 3)");
342 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
343 "short4(0, 1, 2, 3)");
344 EXPECT_EQUAL(Short4(0, 1, 2, 3),
345 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500346
347 {
348 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
349 " but found 4)\n");
350 Short2(Short4(1)).release();
351 }
352
353 {
354 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
355 " but found 3)\n");
356 Short4(Short3(1)).release();
357 }
358}
359
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400360DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
361 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
362
363 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
364 "65535");
365 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
366 "ushort2(0)");
367 EXPECT_EQUAL(UShort2(0, 1),
368 "ushort2(0, 1)");
369 EXPECT_EQUAL(UShort3(0),
370 "ushort3(0)");
371 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
372 "ushort3(0, 1, -2)");
373 EXPECT_EQUAL(UShort3(0, 1, 2),
374 "ushort3(0, 1, 2)");
375 EXPECT_EQUAL(UShort4(0),
376 "ushort4(0)");
377 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
378 "ushort4(0, 1, 2, 3)");
379 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
380 "ushort4(0, 1, 2, 3)");
381 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
382 "ushort4(0, 1, 2, 3)");
383
384 {
385 ExpectError error(r, "error: invalid arguments to 'ushort2' constructor (expected 2 "
386 "scalars, but found 4)\n");
387 UShort2(UShort4(1)).release();
388 }
389
390 {
391 ExpectError error(r, "error: invalid arguments to 'ushort4' constructor (expected 4 "
392 "scalars, but found 3)\n");
393 UShort4(UShort3(1)).release();
394 }
395}
396
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500397DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
398 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500399
John Stilesb9e4f642021-03-05 09:11:38 -0500400 EXPECT_EQUAL(Bool2(false),
401 "bool2(false)");
402 EXPECT_EQUAL(Bool2(false, true),
403 "bool2(false, true)");
404 EXPECT_EQUAL(Bool3(false),
405 "bool3(false)");
406 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
407 "bool3(false, true, false)");
408 EXPECT_EQUAL(Bool3(false, true, false),
409 "bool3(false, true, false)");
410 EXPECT_EQUAL(Bool4(false),
411 "bool4(false)");
412 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
413 "bool4(false, true, false, true)");
414 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
415 "bool4(false, true, false, true)");
416 EXPECT_EQUAL(Bool4(false, true, false, true),
417 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500418
419 {
420 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
421 " but found 4)\n");
422 Bool2(Bool4(true)).release();
423 }
424
425 {
426 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
427 " but found 3)\n");
428 Bool4(Bool3(true)).release();
429 }
430}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500431
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400432DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
433 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
434 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
435 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
436 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
437 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
438 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
439 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
440 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
441 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
442 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
443 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
444 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
445
446 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
447 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
448 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
449 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
450 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
451 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
452 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
453 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
454 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
455 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
456 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
457
458 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
459 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
460 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
461 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
462 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
463 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
464 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
465 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
466 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
467 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
468 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
469
470 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
471 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
472 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
473 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
474 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
475 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
476 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
477 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
478 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
479 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
480 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
481
482 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
483 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
484 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
485 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
486 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
487 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
488 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
489 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
490 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
491 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
492 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
493
494 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
495 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
496 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
497 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
498 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
499 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
500 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
501 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
502 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
503 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
504 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
505
506 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
507 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
508 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
509 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
510 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
511 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
512 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
513 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
514 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
515 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
516 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400517
518 Var x(kFloat_Type);
519 DSLExpression e = x + 1;
520 REPORTER_ASSERT(r, e.type().isFloat());
521 e.release();
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400522}
523
Ethan Nicholas84558932021-04-12 16:56:37 -0400524DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
525 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
526 Var f22(kFloat2x2_Type, "f22");
527 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
528 Var f32(kFloat3x2_Type, "f32");
529 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
530 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
531 Var f42(kFloat4x2_Type, "f42");
532 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
533 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
534 Var f23(kFloat2x3_Type, "f23");
535 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
536 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
537 Var f33(kFloat3x3_Type, "f33");
538 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
539 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
540 Var f43(kFloat4x3_Type, "f43");
541 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
542 "(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))");
543 Var f24(kFloat2x4_Type, "f24");
544 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
545 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
546 Var f34(kFloat3x4_Type, "f34");
547 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
548 "(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))");
549 Var f44(kFloat4x4_Type, "f44");
550 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
551
552 Var h22(kHalf2x2_Type, "h22");
553 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
554 Var h32(kHalf3x2_Type, "h32");
555 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
556 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
557 Var h42(kHalf4x2_Type, "h42");
558 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
559 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
560 Var h23(kHalf2x3_Type, "h23");
561 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
562 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
563 Var h33(kHalf3x3_Type, "h33");
564 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
565 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
566 Var h43(kHalf4x3_Type, "h43");
567 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
568 "(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))");
569 Var h24(kHalf2x4_Type, "h24");
570 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
571 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
572 Var h34(kHalf3x4_Type, "h34");
573 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
574 "(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))");
575 Var h44(kHalf4x4_Type, "h44");
576 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
577
578 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
579 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
580 EXPECT_EQUAL(h42[0][1], "h42[0].y");
581 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
582 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
583 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
584
585 {
586 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
587 "scalars, but found 2)\n");
588 DSLExpression(Float3x3(Float2(1))).release();
589 }
590
591 {
592 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
593 "scalars, but found 5)\n");
594 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
595 }
596
597 {
598 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
599 DSLExpression(f43 * Float3(1)).release();
600 }
601
602 {
603 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
604 "'float3x3'\n");
605 DSLExpression(f43 = f33).release();
606 }
607
608 {
609 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
610 "'float2x2'\n");
611 DSLExpression(h22 = f22).release();
612 }
613
614 {
615 ExpectError error(r,
616 "error: no match for inverse(float4x3)\n");
617 DSLExpression(Inverse(f43)).release();
618 }
619}
620
Ethan Nicholas92969f22021-01-13 10:38:59 -0500621DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
622 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400623 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500624
John Stiles8f440b42021-03-05 16:48:56 -0500625 EXPECT_EQUAL(a + b,
626 "(a + b)");
627 EXPECT_EQUAL(a + 1,
628 "(a + 1.0)");
629 EXPECT_EQUAL(0.5 + a + -99,
630 "((0.5 + a) + -99.0)");
631 EXPECT_EQUAL(a += b + 1,
632 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500633 EXPECT_EQUAL(+a,
634 "a");
635 EXPECT_EQUAL(+(a + b),
636 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500637
638 {
639 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500640 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500641 }
642
643 {
644 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500645 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500646 }
647
648 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500649 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500650 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500651 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500652
653 {
654 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400655 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500656 DSLExpression(+c);
657 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500658}
659
660DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
661 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400662 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500663
John Stiles8f440b42021-03-05 16:48:56 -0500664 EXPECT_EQUAL(a - b,
665 "(a - b)");
666 EXPECT_EQUAL(a - 1,
667 "(a - 1)");
668 EXPECT_EQUAL(2 - a - b,
669 "((2 - a) - b)");
670 EXPECT_EQUAL(a -= b + 1,
671 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500672 EXPECT_EQUAL(-a,
673 "-a");
674 EXPECT_EQUAL(-(a - b),
675 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500676
677 {
678 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500679 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500680 }
681
682 {
683 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500684 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500685 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500686
687 {
688 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500689 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500690 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500691
692 {
693 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400694 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500695 DSLExpression(-c);
696 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500697}
698
699DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
700 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400701 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500702
John Stiles8f440b42021-03-05 16:48:56 -0500703 EXPECT_EQUAL(a * b,
704 "(a * b)");
705 EXPECT_EQUAL(a * 2,
706 "(a * 2.0)");
707 EXPECT_EQUAL(0.5 * a * -99,
708 "((0.5 * a) * -99.0)");
709 EXPECT_EQUAL(a *= b + 1,
710 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500711
712 {
713 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500714 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500715 }
716
717 {
718 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500719 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500720 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500721
722 {
723 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500724 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500725 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500726}
727
728DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
729 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400730 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500731
John Stiles8f440b42021-03-05 16:48:56 -0500732 EXPECT_EQUAL(a / b,
733 "(a / b)");
734 EXPECT_EQUAL(a / 2,
735 "(a / 2.0)");
736 EXPECT_EQUAL(0.5 / a / -99,
737 "((0.5 / a) / -99.0)");
738 EXPECT_EQUAL(b / (a - 1),
739 "(b / (a - 1.0))");
740 EXPECT_EQUAL(a /= b + 1,
741 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500742
743 {
744 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500745 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500746 }
747
748 {
749 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500750 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500751 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500752
753 {
754 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500755 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500756 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500757
758 {
759 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500760 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500761 }
762
763 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400764 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500765 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500766 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500767 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500768}
769
770DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, 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 % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500777 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500778
779 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500780 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500781
782 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500783 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500784
785 {
786 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500787 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500788 }
789
790 {
791 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500792 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500793 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500794
795 {
796 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500797 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500798 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500799
800 {
801 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500802 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500803 }
804
805 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400806 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500807 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500808 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500809 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500810}
811
812DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
813 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400814 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500815 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500816 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500817
818 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500819 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500820
821 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500822 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500823
824 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500825 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500826
827 {
828 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500829 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500830 }
831
832 {
833 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500834 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500835 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500836
837 {
838 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500839 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500840 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500841}
842
843DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
844 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400845 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500846 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500847 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500848
849 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500850 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851
852 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500853 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500854
855 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500856 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500857
858 {
859 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500860 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500861 }
862
863 {
864 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500865 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500867
868 {
869 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500870 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500871 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500872}
873
874DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
875 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400876 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500877 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500878 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500879
880 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500881 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500882
883 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500884 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500885
886 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500887 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500888
889 {
890 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500891 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500892 }
893
894 {
895 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500896 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500897 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500898
899 {
900 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500901 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500902 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500903}
904
905DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
906 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400907 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500908 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500909 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500910
911 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500912 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500913
914 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500915 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500916
917 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500918 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500919
920 {
921 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500922 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500923 }
924
925 {
926 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500927 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500928 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500929
930 {
931 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500932 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500933 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500934}
935
936DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
937 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400938 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500939 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500940 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500941
942 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500943 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500944
945 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500946 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500947
948 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500949 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500950
951 {
952 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500953 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500954 }
955
956 {
957 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500958 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500959 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500960
961 {
962 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500963 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500964 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500965}
966
967DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
968 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400969 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500970 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500971 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500972
973 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500974 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500975
976 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500977 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500978
979 {
980 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500981 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500982 }
983}
984
985DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
986 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400987 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500988 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500989 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500990
991 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500992 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500993
994 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500995 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500996
997 {
998 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500999 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001000 }
1001}
1002
1003DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1004 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001005 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001006 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001007 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001008
1009 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001010 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001011}
1012
1013DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1014 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001015 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001016 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001017 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001018
1019 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001020 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001021
1022 {
1023 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001024 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001025 }
1026}
1027
1028DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1029 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001030 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001031 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001032 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001033
1034 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001035 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001036
1037 {
1038 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001039 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001040 }
1041}
1042
1043DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1044 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001045 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001046 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001047 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001048
1049 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001050 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001051
1052 {
1053 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001054 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001055 }
1056}
1057
1058DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1059 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001060 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001061 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001062 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001063
1064 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001065 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001066
1067 {
1068 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001069 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001070 }
1071}
1072
1073DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1074 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001075 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001076 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001077 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001078
1079 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001080 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001081
1082 {
1083 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001084 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001085 }
1086}
1087
1088DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1089 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001090 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001091 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001092 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001093
1094 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001095 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001096
1097 {
1098 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001099 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001100 }
1101}
1102
1103DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1104 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001105 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001106 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001107 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001108
1109 {
1110 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001111 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001112 }
1113}
1114
1115DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1116 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001117 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001118 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001119 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001120
1121 {
1122 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001123 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001124 }
1125}
1126
1127DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1128 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001129 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001130 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001131 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001132
1133 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001134 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001135
1136 {
1137 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001138 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001139 }
1140
1141 {
1142 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001143 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001144 }
1145
1146 {
1147 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001148 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001149 }
1150
1151 {
1152 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001153 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001154 }
1155}
1156
1157DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1158 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001159 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001160 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001161 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001162
1163 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001164 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001165
1166 {
1167 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001168 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001169 }
1170
1171 {
1172 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001173 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001174 }
1175
1176 {
1177 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001178 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001179 }
1180
1181 {
1182 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001183 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001184 }
1185}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001186
Ethan Nicholas722cb672021-05-06 10:47:06 -04001187DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1188 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1189 {
1190 DSLExpression sqrt = DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt");
1191 SkTArray<DSLWrapper<DSLExpression>> args;
1192 args.emplace_back(1);
1193 EXPECT_EQUAL(sqrt(std::move(args)), "sqrt(1.0)");
1194 }
1195
1196 {
1197 DSLExpression pow = DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow");
1198 DSLVar a(kFloat_Type, "a");
1199 DSLVar b(kFloat_Type, "b");
1200 SkTArray<DSLWrapper<DSLExpression>> args;
1201 args.emplace_back(a);
1202 args.emplace_back(b);
1203 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1204 }
1205}
1206
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001207DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001208 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001209 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001210 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001211 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001212
Ethan Nicholas722cb672021-05-06 10:47:06 -04001213 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1214
1215 SkTArray<DSLStatement> statements;
1216 statements.push_back(a = 0);
1217 statements.push_back(++a);
1218 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001219}
1220
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001221DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001222 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001223 Var i(kInt_Type, "i", 0);
1224 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001225 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001226 If(i > 5, Break())
1227 ))
1228 );
1229 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001230 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1231 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001232
1233 {
1234 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001235 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001236 Break()
1237 );
1238 }
1239}
1240
1241DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001242 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001243 Var i(kInt_Type, "i", 0);
1244 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001245 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001246 If(i < 5, Continue())
1247 ))
1248 );
1249 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001250 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1251 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001252
1253 {
1254 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001255 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001256 Continue()
1257 );
1258 }
1259}
1260
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001261DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001262 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001263 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001264 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001265 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001266 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001267 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001268
1269 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001270 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001271 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001272 Declare(c).release();
1273 }
1274
1275 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001276 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001277 Declare(d).release();
1278 ExpectError error(r, "error: variable has already been declared\n");
1279 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001280 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001281
1282 {
1283 Var e(kUniform_Modifier, kInt_Type, "e");
1284 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1285 Declare(e).release();
1286 }
1287}
1288
1289DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001290 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001291 Var x(kInt_Type, "x", 0);
1292 DeclareGlobal(x);
1293 Var y(kUniform_Modifier, kFloat2_Type, "y");
1294 DeclareGlobal(y);
1295 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1296 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1297 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001298}
1299
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001300DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1301 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1302 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001303 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001304}
1305
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001306DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1307 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1308 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001309 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001310
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001311 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001312 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001313 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001314
1315 {
1316 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1317 Do(Block(), 7).release();
1318 }
1319}
1320
1321DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001322 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
John Stiles8676ebe2021-04-20 15:30:41 -04001323 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1324 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001325
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001326 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001327 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1328 "for (int i = 0; (i < 10); ++i) (i += 5);");
1329
1330 Var j(kInt_Type, "j", 0);
1331 Var k(kInt_Type, "k", 10);
1332 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1333 {
1334 int j = 0;
1335 int k = 10;
1336 for (; (j < k); ++j) {}
1337 }
1338 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001339
1340 {
1341 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1342 For(i = 0, i + 10, ++i, i += 5).release();
1343 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001344
1345 {
1346 ExpectError error(r, "error: invalid for loop initializer\n");
1347 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1348 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001349}
1350
Ethan Nicholase2c05042021-02-03 10:27:22 -05001351DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001352 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001353 Var coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001354 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001355 sk_FragColor() = Half4(coords, 0, 1)
1356 );
1357 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001358 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001359 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001360
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001361 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001362 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001363 Var x(kFloat_Type, "x");
1364 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001365 sqr.define(
1366 Return(x * x)
1367 );
1368 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1369 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1370 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1371 }
1372
1373 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001374 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001375 Var x(kFloat2_Type, "x");
1376 Var y(kFloat2_Type, "y");
1377 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001378 dot.define(
1379 Return(x * x + y * y)
1380 );
1381 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1382 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1383 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1384 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1385 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1386 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001387
1388 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001389 DSLWriter::Reset();
1390 Var x(kFloat_Type, "x");
1391 Var y(kFloat_Type, "y");
1392 DSLFunction pair(kFloat2_Type, "pair", x, y);
1393 pair.define(
1394 Return(Float2(x, y))
1395 );
1396 Var varArg1(kFloat_Type, "varArg1");
1397 Var varArg2(kFloat_Type, "varArg2");
1398 DSLWriter::MarkDeclared(varArg1);
1399 DSLWriter::MarkDeclared(varArg2);
1400 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1401 }
1402
1403 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001404 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001405 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001406 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001407 Return(true)
1408 );
1409 }
1410
1411 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001412 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001413 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001414 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001415 Return()
1416 );
1417 }
1418
1419 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001420 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001421 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001422 Var x(kFloat_Type, "x", 0);
1423 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001424 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001425 If(x == 1, Return(x))
1426 );
1427 }
1428
1429 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001430 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001431 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001432 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001433 Return(0)
1434 );
1435 }
1436
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001437 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001438 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001439 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001440 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001441 );
1442 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001443
1444 {
1445 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1446 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001447 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001448 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001449 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001450 );
1451 }
1452
1453 {
1454 ExpectError error(r, "error: variable has already been declared\n");
1455 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001456 DSLVar p(kFloat_Type);
1457 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001458 );
1459 Declare(p).release();
1460 }
1461
1462 {
1463 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1464 "values\n");
1465 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001466 DSLVar p(kFloat_Type, 1);
1467 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001468 );
1469 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001470}
1471
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001472DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1473 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001474 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001475 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001476 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001477
1478 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001479 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001480
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001481 Statement z = StaticIf(a > b, a -= b, b -= a);
1482 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1483
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001484 {
1485 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1486 If(a + b, a -= b).release();
1487 }
1488}
1489
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001490DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1491 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1492
1493 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001494 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001495
1496 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001497 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001498}
1499
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001500DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1501 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001502 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001503 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001504 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001505
1506 {
1507 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001508 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001509 }
1510
1511 {
1512 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001513 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001514 }
1515}
1516
Ethan Nicholascfefec02021-02-09 15:22:57 -05001517DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1518 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1519
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001520 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001521
Ethan Nicholas722cb672021-05-06 10:47:06 -04001522 SkTArray<DSLStatement> caseStatements;
1523 caseStatements.push_back(a = 1);
1524 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001525 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001526 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001527 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001528 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001529 Default(Discard())
1530 );
John Stilese1d1b082021-02-23 13:44:36 -05001531 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001532 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001533 case 0: (a = 0.0); break;
1534 case 1: (a = 1.0); continue;
1535 case 2: (a = 2.0);
1536 default: discard;
1537 }
1538 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001539
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001540 Statement y = StaticSwitch(b,
1541 Case(0, a = 0, Break()),
1542 Case(1, a = 1, Continue()),
1543 Case(2, a = 2 /*Fallthrough*/),
1544 Default(Discard())
1545 );
1546 EXPECT_EQUAL(y, R"(
1547 @switch (b) {
1548 case 0: (a = 0.0); break;
1549 case 1: (a = 1.0); continue;
1550 case 2: (a = 2.0);
1551 default: discard;
1552 }
1553 )");
1554
John Stiles642cde22021-02-23 14:57:01 -05001555 EXPECT_EQUAL(Switch(b),
1556 "switch (b) {}");
1557
1558 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1559 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001560
1561 {
John Stilese1d1b082021-02-23 13:44:36 -05001562 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001563 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001564 }
1565
1566 {
John Stilese1d1b082021-02-23 13:44:36 -05001567 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001568 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001569 }
1570
1571 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001572 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001573 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001574 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001575 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001576}
1577
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001578DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001579 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001580 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001581
John Stilesf04e09c2021-03-05 13:13:14 -05001582 EXPECT_EQUAL(a.x(),
1583 "a.x");
1584 EXPECT_EQUAL(a.y(),
1585 "a.y");
1586 EXPECT_EQUAL(a.z(),
1587 "a.z");
1588 EXPECT_EQUAL(a.w(),
1589 "a.w");
1590 EXPECT_EQUAL(a.r(),
1591 "a.x");
1592 EXPECT_EQUAL(a.g(),
1593 "a.y");
1594 EXPECT_EQUAL(a.b(),
1595 "a.z");
1596 EXPECT_EQUAL(a.a(),
1597 "a.w");
1598 EXPECT_EQUAL(Swizzle(a, R),
1599 "a.x");
1600 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1601 "float2(0.0, a.y)");
1602 EXPECT_EQUAL(Swizzle(a, B, G, G),
1603 "a.zyy");
1604 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1605 "float4(a.xyz, 1.0)");
1606 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1607 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001608}
1609
John Stiles08771b02021-04-26 09:35:10 -04001610
1611DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001612 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
John Stiles08771b02021-04-26 09:35:10 -04001613
1614 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1615 Var a;
1616 if (true)
1617 {
1618 Var(kInt_Type, "a").swap(a);
1619 }
1620
1621 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1622 "{ int a; (a = 123); }");
1623}
1624
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001625DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1626 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1627 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001628 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001629
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001630 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001631 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001632 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001633
1634 {
1635 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001636 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001637 }
1638}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001639
1640DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1641 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001642 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001643
1644 EXPECT_EQUAL(a[0], "a[0]");
1645 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001646
1647 {
1648 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001649 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001650 }
1651
1652 {
1653 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001654 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001655 }
1656
1657 {
1658 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001659 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001660 }
1661}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001662
1663DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1664 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1665 // There is a Fract type on Mac which can conflict with our Fract builtin
1666 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001667 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1668 Var h3(kHalf3_Type, "h3");
1669 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001670 EXPECT_EQUAL(Abs(a), "abs(a)");
1671 EXPECT_EQUAL(All(b4), "all(b4)");
1672 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001673 EXPECT_EQUAL(Atan(a), "atan(a)");
1674 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001675 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1676 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1677 EXPECT_EQUAL(Cos(a), "cos(a)");
1678 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1679 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1680 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1681 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1682 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1683 EXPECT_EQUAL(Exp(a), "exp(a)");
1684 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1685 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1686 EXPECT_EQUAL(Floor(a), "floor(a)");
1687 EXPECT_EQUAL(Fract(a), "fract(a)");
1688 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1689 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1690 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1691 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1692 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1693 EXPECT_EQUAL(Length(a), "length(a)");
1694 EXPECT_EQUAL(Log(a), "log(a)");
1695 EXPECT_EQUAL(Log2(a), "log2(a)");
1696 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1697 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1698 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1699 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1700 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1701 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1702 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1703 EXPECT_EQUAL(Radians(a), "radians(a)");
1704 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1705 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1706 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1707 EXPECT_EQUAL(Sign(a), "sign(a)");
1708 EXPECT_EQUAL(Sin(a), "sin(a)");
1709 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1710 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1711 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1712 EXPECT_EQUAL(Tan(a), "tan(a)");
1713 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001714
1715 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1716 // one of them reports errors correctly
1717 {
1718 ExpectError error(r, "error: no match for ceil(bool)\n");
1719 Ceil(a == b).release();
1720 }
1721}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001722
1723DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001724 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001725
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001726 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001727 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001728 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001729
1730 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1731 // context, so we can't as yet Declare() variables with these modifiers.
1732 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001733 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001734 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001735 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001736
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001737 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001738 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001739 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001740
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001741 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001742 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001743 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001744
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001745 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001746 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001747 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001748 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001749
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001750 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001751 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1752 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001753 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001754
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001755 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001756 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1757 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001758 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001759
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001760 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001761 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001762 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001763 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001764}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001765
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001766DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
1767 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
1768 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1769 .inputAttachmentIndex(7),
1770 kConst_Modifier), kInt_Type, "v1", 0);
1771 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1772 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1773
1774 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1775 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1776
1777 Var v3(DSLLayout().overrideCoverage(), kHalf_Type, "v3");
1778 EXPECT_EQUAL(Declare(v3), "layout (override_coverage) half v3;");
1779
1780 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1781 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1782
1783 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1784 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1785
1786 Var v6(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kBool_Type, "v6");
1787 DeclareGlobal(v6);
1788 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "layout (srgb_unpremul) uniform bool v6;");
1789
1790 {
1791 ExpectError error(r, "error: layout qualifier 'location' appears more than once\n");
1792 DSLLayout().location(1).location(2);
1793 }
1794
1795 {
1796 ExpectError error(r, "error: layout qualifier 'set' appears more than once\n");
1797 DSLLayout().set(1).set(2);
1798 }
1799
1800 {
1801 ExpectError error(r, "error: layout qualifier 'binding' appears more than once\n");
1802 DSLLayout().binding(1).binding(2);
1803 }
1804
1805 {
1806 ExpectError error(r, "error: layout qualifier 'offset' appears more than once\n");
1807 DSLLayout().offset(1).offset(2);
1808 }
1809
1810 {
1811 ExpectError error(r, "error: layout qualifier 'index' appears more than once\n");
1812 DSLLayout().index(1).index(2);
1813 }
1814
1815 {
1816 ExpectError error(r, "error: layout qualifier 'builtin' appears more than once\n");
1817 DSLLayout().builtin(1).builtin(2);
1818 }
1819
1820 {
1821 ExpectError error(r, "error: layout qualifier 'input_attachment_index' appears more than "
1822 "once\n");
1823 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1824 }
1825
1826 {
1827 ExpectError error(r, "error: layout qualifier 'origin_upper_left' appears more than "
1828 "once\n");
1829 DSLLayout().originUpperLeft().originUpperLeft();
1830 }
1831
1832 {
1833 ExpectError error(r, "error: layout qualifier 'override_coverage' appears more than "
1834 "once\n");
1835 DSLLayout().overrideCoverage().overrideCoverage();
1836 }
1837
1838 {
1839 ExpectError error(r, "error: layout qualifier 'push_constant' appears more than once\n");
1840 DSLLayout().pushConstant().pushConstant();
1841 }
1842
1843 {
1844 ExpectError error(r, "error: layout qualifier 'blend_support_all_equations' appears more "
1845 "than once\n");
1846 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1847 }
1848
1849 {
1850 ExpectError error(r, "error: layout qualifier 'srgb_unpremul' appears more than once\n");
1851 DSLLayout().srgbUnpremul().srgbUnpremul();
1852 }
1853}
1854
Ethan Nicholas624a5292021-04-16 14:54:43 -04001855DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001856 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001857 SkSL::ProgramKind::kFragmentProcessor);
1858 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1859 EXPECT_EQUAL(Sample(child), "sample(child)");
1860 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1861 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001862 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001863
1864 {
1865 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1866 Sample(child, true).release();
1867 }
1868}
1869
Ethan Nicholas624a5292021-04-16 14:54:43 -04001870DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001871 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
Brian Osman552fcb92021-04-28 17:41:57 -04001872 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001873 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001874 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001875
1876 {
Brian Osmanc9125aa2021-04-21 09:57:19 -04001877 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001878 Sample(shader, Half4(1)).release();
1879 }
1880}
1881
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001882DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Brian Osman9eb0bb62021-05-11 22:03:54 +00001883 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001884
1885 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001886 Field(kFloat_Type, "x"),
1887 Field(kBool_Type, "b"),
1888 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001889 );
1890 DSLVar result(simpleStruct, "result");
1891 DSLFunction(simpleStruct, "returnStruct").define(
1892 Declare(result),
1893 result.field("x") = 123,
1894 result.field("b") = result.field("x") > 0,
1895 result.field("a")[0] = result.field("x"),
1896 Return(result)
1897 );
1898 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001899 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1900 "struct SimpleStruct { float x; bool b; float[3] a; };");
1901 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1902 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1903 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001904
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001905 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001906 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001907 Field(simpleStruct, "simple")
1908 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001909 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1910 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001911 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001912}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001913
1914DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1915 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1916 std::vector<Wrapper<DSLExpression>> exprs;
1917 exprs.push_back(DSLExpression(1));
1918 exprs.emplace_back(2.0);
1919 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1920 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1921
1922 std::vector<Wrapper<DSLVar>> vars;
1923 vars.emplace_back(DSLVar(kInt_Type, "x"));
1924 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0]).name() == "x");
1925}