blob: e00528487ed489058a9c6171667596c97a75fc96 [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 Nicholas55a63af2021-05-18 10:12:58 -040021SkSL::ProgramSettings default_settings() {
22 SkSL::ProgramSettings result;
23 result.fDSLMarkVarsDeclared = true;
24 result.fDSLMangling = false;
25 return result;
26}
27
28SkSL::ProgramSettings no_mark_vars_declared() {
29 SkSL::ProgramSettings result = default_settings();
30 result.fDSLMarkVarsDeclared = false;
31 return result;
32}
Ethan Nicholas22dcb732021-05-12 11:26:58 -040033
Ethan Nicholas961d9442021-03-16 16:37:29 -040034/**
35 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
36 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
37 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
38 * errors, especially given that some of the variables have options that make them an error to
39 * actually declare.
40 */
Ethan Nicholas95046142021-01-07 10:57:27 -050041class AutoDSLContext {
42public:
Ethan Nicholas55a63af2021-05-18 10:12:58 -040043 AutoDSLContext(GrGpu* gpu, SkSL::ProgramSettings settings = default_settings(),
Ethan Nicholasee49efc2021-04-09 15:33:53 -040044 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -040045 Start(gpu->shaderCompiler(), kind, settings);
Ethan Nicholas95046142021-01-07 10:57:27 -050046 }
47
48 ~AutoDSLContext() {
49 End();
50 }
51};
52
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050053class ExpectError : public ErrorHandler {
54public:
55 ExpectError(skiatest::Reporter* reporter, const char* msg)
56 : fMsg(msg)
57 , fReporter(reporter) {
58 SetErrorHandler(this);
59 }
60
61 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050062 REPORTER_ASSERT(fReporter, !fMsg,
63 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050064 SetErrorHandler(nullptr);
65 }
66
Ethan Nicholasb9563042021-02-25 09:45:49 -050067 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050068 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
69 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
70 fMsg = nullptr;
71 }
72
73private:
74 const char* fMsg;
75 skiatest::Reporter* fReporter;
76};
77
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050078static bool whitespace_insensitive_compare(const char* a, const char* b) {
79 for (;;) {
80 while (isspace(*a)) {
81 ++a;
82 }
83 while (isspace(*b)) {
84 ++b;
85 }
86 if (*a != *b) {
87 return false;
88 }
89 if (*a == 0) {
90 return true;
91 }
92 ++a;
93 ++b;
94 }
95}
96
Ethan Nicholas24c17722021-03-09 13:10:59 -050097// for use from SkSLDSLOnlyTest.cpp
98void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
99 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
100}
101
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500102DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -0500103 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
104 Expression e1 = 1;
105 REPORTER_ASSERT(r, e1.release()->description() == "1");
106 Expression e2 = 1.0;
107 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
108 Expression e3 = true;
109 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400110 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500111 Expression e4 = a;
112 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500113
114 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
115 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
116 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
117 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
118 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
119 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
120 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
121 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500122}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500123
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500124static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
125static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
126static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
127static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
Ethan Nicholas722cb672021-05-06 10:47:06 -0400128static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500129static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
130
131template <typename T>
132static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
133 SkSL::String actual = stringize(input);
134 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
135 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
136 lineNumber, expected, actual.c_str());
137 }
138}
139
140template <typename T>
141static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
142 // This overload allows temporary values to be passed to expect_equal.
143 return expect_equal(r, lineNumber, dsl, expected);
144}
145
146#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
147
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400148DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
149 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400150 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400151 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true");
152
153 Var x(kInt_Type, "x");
154 EXPECT_EQUAL(Declare(x), "int x;");
155 }
156
157 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400158 SkSL::ProgramSettings settings;
159 settings.fOptimize = false;
160 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings,
161 SkSL::ProgramKind::kFragmentProcessor);
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400162 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))),
163 "all(greaterThan(float4(1.0), float4(0.0)))");
164 }
165
166 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400167 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), SkSL::ProgramSettings());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400168 Var x(kInt_Type, "x");
169 EXPECT_EQUAL(Declare(x), "int _0_x;");
170 }
171}
172
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500173DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
174 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
175 Expression e1 = Float(std::numeric_limits<float>::max());
176 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
177 std::numeric_limits<float>::max());
178
179 Expression e2 = Float(std::numeric_limits<float>::min());
180 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
181 std::numeric_limits<float>::min());
182
John Stilesb4d7b582021-02-19 09:56:31 -0500183 EXPECT_EQUAL(Float2(0),
184 "float2(0.0)");
185 EXPECT_EQUAL(Float2(-0.5, 1),
186 "float2(-0.5, 1.0)");
187 EXPECT_EQUAL(Float3(0.75),
188 "float3(0.75)");
189 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500190 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500191 EXPECT_EQUAL(Float3(0, 1, 2),
192 "float3(0.0, 1.0, 2.0)");
193 EXPECT_EQUAL(Float4(0),
194 "float4(0.0)");
195 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500196 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500197 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500198 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500199 EXPECT_EQUAL(Float4(0, 1, 2, 3),
200 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500201
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400202 DSLVar x(kFloat_Type, "x");
203 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
204 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
205
206 DSLVar y(kFloat2_Type, "y");
207 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
208 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
209
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500210 {
211 ExpectError error(r, "error: floating point value is infinite\n");
212 Float(std::numeric_limits<float>::infinity()).release();
213 }
214
215 {
216 ExpectError error(r, "error: floating point value is NaN\n");
217 Float(std::numeric_limits<float>::quiet_NaN()).release();
218 }
219
220 {
221 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
222 " but found 4)\n");
223 Float2(Float4(1)).release();
224 }
225
226 {
227 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
228 " but found 3)\n");
229 Float4(Float3(1)).release();
230 }
231}
232
233DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
234 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
235 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500236 REPORTER_ASSERT(r,
237 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500238
239 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500240 REPORTER_ASSERT(r,
241 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500242
John Stilesb9e4f642021-03-05 09:11:38 -0500243 EXPECT_EQUAL(Half2(0),
244 "half2(0.0)");
245 EXPECT_EQUAL(Half2(-0.5, 1),
246 "half2(-0.5, 1.0)");
247 EXPECT_EQUAL(Half3(0.75),
248 "half3(0.75)");
249 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
250 "half3(0.0, 1.0, -2.0)");
251 EXPECT_EQUAL(Half3(0, 1, 2),
252 "half3(0.0, 1.0, 2.0)");
253 EXPECT_EQUAL(Half4(0),
254 "half4(0.0)");
255 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
256 "half4(0.0, 1.0, 2.0, 3.0)");
257 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
258 "half4(0.0, 1.0, 2.0, 3.0)");
259 EXPECT_EQUAL(Half4(0, 1, 2, 3),
260 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500261
262 {
263 ExpectError error(r, "error: floating point value is infinite\n");
264 Half(std::numeric_limits<float>::infinity()).release();
265 }
266
267 {
268 ExpectError error(r, "error: floating point value is NaN\n");
269 Half(std::numeric_limits<float>::quiet_NaN()).release();
270 }
271
272 {
273 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
274 " but found 4)\n");
275 Half2(Half4(1)).release();
276 }
277
278 {
279 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
280 " but found 3)\n");
281 Half4(Half3(1)).release();
282 }
283}
284
285DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
286 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500287
John Stilesb9e4f642021-03-05 09:11:38 -0500288 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
289 "2147483647");
290 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
291 "int2(-2147483648)");
292 EXPECT_EQUAL(Int2(0, 1),
293 "int2(0, 1)");
294 EXPECT_EQUAL(Int3(0),
295 "int3(0)");
296 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
297 "int3(0, 1, -2)");
298 EXPECT_EQUAL(Int3(0, 1, 2),
299 "int3(0, 1, 2)");
300 EXPECT_EQUAL(Int4(0),
301 "int4(0)");
302 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
303 "int4(0, 1, 2, 3)");
304 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
305 "int4(0, 1, 2, 3)");
306 EXPECT_EQUAL(Int4(0, 1, 2, 3),
307 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500308
309 {
310 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
311 " but found 4)\n");
312 Int2(Int4(1)).release();
313 }
314
315 {
316 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
317 " but found 3)\n");
318 Int4(Int3(1)).release();
319 }
320}
321
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400322DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
323 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
324
325 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
326 "4294967295");
327 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
328 "uint2(0)");
329 EXPECT_EQUAL(UInt2(0, 1),
330 "uint2(0, 1)");
331 EXPECT_EQUAL(UInt3(0),
332 "uint3(0)");
333 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
334 "uint3(0, 1, -2)");
335 EXPECT_EQUAL(UInt3(0, 1, 2),
336 "uint3(0, 1, 2)");
337 EXPECT_EQUAL(UInt4(0),
338 "uint4(0)");
339 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
340 "uint4(0, 1, 2, 3)");
341 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
342 "uint4(0, 1, 2, 3)");
343 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
344 "uint4(0, 1, 2, 3)");
345
346 {
347 ExpectError error(r, "error: invalid arguments to 'uint2' constructor (expected 2 scalars,"
348 " but found 4)\n");
349 UInt2(UInt4(1)).release();
350 }
351
352 {
353 ExpectError error(r, "error: invalid arguments to 'uint4' constructor (expected 4 scalars,"
354 " but found 3)\n");
355 UInt4(UInt3(1)).release();
356 }
357}
358
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500359DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
360 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500361
John Stilesb9e4f642021-03-05 09:11:38 -0500362 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
363 "32767");
364 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
365 "short2(-32768)");
366 EXPECT_EQUAL(Short2(0, 1),
367 "short2(0, 1)");
368 EXPECT_EQUAL(Short3(0),
369 "short3(0)");
370 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
371 "short3(0, 1, -2)");
372 EXPECT_EQUAL(Short3(0, 1, 2),
373 "short3(0, 1, 2)");
374 EXPECT_EQUAL(Short4(0),
375 "short4(0)");
376 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
377 "short4(0, 1, 2, 3)");
378 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
379 "short4(0, 1, 2, 3)");
380 EXPECT_EQUAL(Short4(0, 1, 2, 3),
381 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500382
383 {
384 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
385 " but found 4)\n");
386 Short2(Short4(1)).release();
387 }
388
389 {
390 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
391 " but found 3)\n");
392 Short4(Short3(1)).release();
393 }
394}
395
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400396DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
397 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
398
399 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
400 "65535");
401 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
402 "ushort2(0)");
403 EXPECT_EQUAL(UShort2(0, 1),
404 "ushort2(0, 1)");
405 EXPECT_EQUAL(UShort3(0),
406 "ushort3(0)");
407 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
408 "ushort3(0, 1, -2)");
409 EXPECT_EQUAL(UShort3(0, 1, 2),
410 "ushort3(0, 1, 2)");
411 EXPECT_EQUAL(UShort4(0),
412 "ushort4(0)");
413 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
414 "ushort4(0, 1, 2, 3)");
415 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
416 "ushort4(0, 1, 2, 3)");
417 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
418 "ushort4(0, 1, 2, 3)");
419
420 {
421 ExpectError error(r, "error: invalid arguments to 'ushort2' constructor (expected 2 "
422 "scalars, but found 4)\n");
423 UShort2(UShort4(1)).release();
424 }
425
426 {
427 ExpectError error(r, "error: invalid arguments to 'ushort4' constructor (expected 4 "
428 "scalars, but found 3)\n");
429 UShort4(UShort3(1)).release();
430 }
431}
432
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500433DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
434 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500435
John Stilesb9e4f642021-03-05 09:11:38 -0500436 EXPECT_EQUAL(Bool2(false),
437 "bool2(false)");
438 EXPECT_EQUAL(Bool2(false, true),
439 "bool2(false, true)");
440 EXPECT_EQUAL(Bool3(false),
441 "bool3(false)");
442 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
443 "bool3(false, true, false)");
444 EXPECT_EQUAL(Bool3(false, true, false),
445 "bool3(false, true, false)");
446 EXPECT_EQUAL(Bool4(false),
447 "bool4(false)");
448 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
449 "bool4(false, true, false, true)");
450 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
451 "bool4(false, true, false, true)");
452 EXPECT_EQUAL(Bool4(false, true, false, true),
453 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500454
455 {
456 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
457 " but found 4)\n");
458 Bool2(Bool4(true)).release();
459 }
460
461 {
462 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
463 " but found 3)\n");
464 Bool4(Bool3(true)).release();
465 }
466}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500467
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400468DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
469 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
470 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
471 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
472 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
473 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
474 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
475 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
476 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
477 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
478 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
479 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
480 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
481
482 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
483 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
484 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
485 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
486 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
487 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
488 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
489 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
490 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
491 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
492 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
493
494 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
495 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
496 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
497 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
498 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
499 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
500 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
501 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
502 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
503 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
504 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
505
506 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
507 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
508 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
509 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
510 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
511 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
512 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
513 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
514 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
515 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
516 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
517
518 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
519 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
520 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
521 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
522 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
523 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
524 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
525 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
526 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
527 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
528 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
529
530 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
531 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
532 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
533 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
534 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
535 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
536 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
537 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
538 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
539 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
540 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
541
542 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
543 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
544 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
545 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
546 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
547 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
548 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
549 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
550 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
551 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
552 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400553
554 Var x(kFloat_Type);
555 DSLExpression e = x + 1;
556 REPORTER_ASSERT(r, e.type().isFloat());
557 e.release();
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400558}
559
Ethan Nicholas84558932021-04-12 16:56:37 -0400560DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
561 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
562 Var f22(kFloat2x2_Type, "f22");
563 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
564 Var f32(kFloat3x2_Type, "f32");
565 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
566 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
567 Var f42(kFloat4x2_Type, "f42");
568 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
569 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
570 Var f23(kFloat2x3_Type, "f23");
571 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
572 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
573 Var f33(kFloat3x3_Type, "f33");
574 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
575 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
576 Var f43(kFloat4x3_Type, "f43");
577 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
578 "(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))");
579 Var f24(kFloat2x4_Type, "f24");
580 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
581 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
582 Var f34(kFloat3x4_Type, "f34");
583 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
584 "(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))");
585 Var f44(kFloat4x4_Type, "f44");
586 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
587
588 Var h22(kHalf2x2_Type, "h22");
589 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
590 Var h32(kHalf3x2_Type, "h32");
591 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
592 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
593 Var h42(kHalf4x2_Type, "h42");
594 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
595 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
596 Var h23(kHalf2x3_Type, "h23");
597 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
598 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
599 Var h33(kHalf3x3_Type, "h33");
600 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
601 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
602 Var h43(kHalf4x3_Type, "h43");
603 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
604 "(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))");
605 Var h24(kHalf2x4_Type, "h24");
606 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
607 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
608 Var h34(kHalf3x4_Type, "h34");
609 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
610 "(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))");
611 Var h44(kHalf4x4_Type, "h44");
612 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
613
614 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
615 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
616 EXPECT_EQUAL(h42[0][1], "h42[0].y");
617 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
618 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
619 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
620
621 {
622 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
623 "scalars, but found 2)\n");
624 DSLExpression(Float3x3(Float2(1))).release();
625 }
626
627 {
628 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
629 "scalars, but found 5)\n");
630 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
631 }
632
633 {
634 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
635 DSLExpression(f43 * Float3(1)).release();
636 }
637
638 {
639 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
640 "'float3x3'\n");
641 DSLExpression(f43 = f33).release();
642 }
643
644 {
645 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
646 "'float2x2'\n");
647 DSLExpression(h22 = f22).release();
648 }
649
650 {
651 ExpectError error(r,
652 "error: no match for inverse(float4x3)\n");
653 DSLExpression(Inverse(f43)).release();
654 }
655}
656
Ethan Nicholas92969f22021-01-13 10:38:59 -0500657DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
658 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400659 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500660
John Stiles8f440b42021-03-05 16:48:56 -0500661 EXPECT_EQUAL(a + b,
662 "(a + b)");
663 EXPECT_EQUAL(a + 1,
664 "(a + 1.0)");
665 EXPECT_EQUAL(0.5 + a + -99,
666 "((0.5 + a) + -99.0)");
667 EXPECT_EQUAL(a += b + 1,
668 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500669 EXPECT_EQUAL(+a,
670 "a");
671 EXPECT_EQUAL(+(a + b),
672 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500673
674 {
675 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500676 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500677 }
678
679 {
680 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500681 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500682 }
683
684 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500685 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500686 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500687 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500688
689 {
690 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400691 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500692 DSLExpression(+c);
693 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500694}
695
696DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
697 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400698 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500699
John Stiles8f440b42021-03-05 16:48:56 -0500700 EXPECT_EQUAL(a - b,
701 "(a - b)");
702 EXPECT_EQUAL(a - 1,
703 "(a - 1)");
704 EXPECT_EQUAL(2 - a - b,
705 "((2 - a) - b)");
706 EXPECT_EQUAL(a -= b + 1,
707 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500708 EXPECT_EQUAL(-a,
709 "-a");
710 EXPECT_EQUAL(-(a - b),
711 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500712
713 {
714 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500715 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500716 }
717
718 {
719 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500720 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500721 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500722
723 {
724 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500725 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500726 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500727
728 {
729 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400730 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500731 DSLExpression(-c);
732 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500733}
734
735DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
736 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400737 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500738
John Stiles8f440b42021-03-05 16:48:56 -0500739 EXPECT_EQUAL(a * b,
740 "(a * b)");
741 EXPECT_EQUAL(a * 2,
742 "(a * 2.0)");
743 EXPECT_EQUAL(0.5 * a * -99,
744 "((0.5 * a) * -99.0)");
745 EXPECT_EQUAL(a *= b + 1,
746 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500747
748 {
749 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500750 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500751 }
752
753 {
754 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500755 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500756 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500757
758 {
759 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500760 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500761 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500762}
763
764DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
765 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400766 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500767
John Stiles8f440b42021-03-05 16:48:56 -0500768 EXPECT_EQUAL(a / b,
769 "(a / b)");
770 EXPECT_EQUAL(a / 2,
771 "(a / 2.0)");
772 EXPECT_EQUAL(0.5 / a / -99,
773 "((0.5 / a) / -99.0)");
774 EXPECT_EQUAL(b / (a - 1),
775 "(b / (a - 1.0))");
776 EXPECT_EQUAL(a /= b + 1,
777 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500778
779 {
780 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500781 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500782 }
783
784 {
785 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500786 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500787 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500788
789 {
790 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500791 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500792 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500793
794 {
795 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500796 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500797 }
798
799 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400800 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500801 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500802 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500803 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500804}
805
806DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
807 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400808 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500809 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500810 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500811
812 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500813 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500814
815 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500816 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500817
818 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500819 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500820
821 {
822 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500823 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500824 }
825
826 {
827 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500828 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500829 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500830
831 {
832 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500833 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500834 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500835
836 {
837 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500838 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500839 }
840
841 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400842 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500843 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500844 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500845 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500846}
847
848DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
849 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400850 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500852 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500853
854 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500855 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500856
857 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500858 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500859
860 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500861 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500862
863 {
864 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500865 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866 }
867
868 {
869 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500870 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500872
873 {
874 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500875 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500876 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500877}
878
879DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
880 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400881 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500882 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500883 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500884
885 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500886 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500887
888 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500889 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500890
891 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500892 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500893
894 {
895 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500896 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500897 }
898
899 {
900 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500901 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500902 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500903
904 {
905 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500906 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500907 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500908}
909
910DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
911 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400912 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500913 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500914 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500915
916 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500917 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500918
919 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500920 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500921
922 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500923 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500924
925 {
926 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500927 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500928 }
929
930 {
931 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500932 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500933 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500934
935 {
936 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500937 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500938 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500939}
940
941DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
942 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400943 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500944 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500945 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500946
947 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500948 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500949
950 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500951 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500952
953 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500954 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500955
956 {
957 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500958 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500959 }
960
961 {
962 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500963 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500964 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500965
966 {
967 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500968 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500969 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500970}
971
972DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
973 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400974 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500975 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500976 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500977
978 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500979 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500980
981 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500982 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500983
984 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500985 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500986
987 {
988 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500989 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500990 }
991
992 {
993 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500994 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500995 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500996
997 {
998 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500999 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -05001000 }
Ethan Nicholas92969f22021-01-13 10:38:59 -05001001}
1002
1003DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
1004 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001005 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001006 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001007 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001008
1009 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001010 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001011
1012 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001013 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001014
1015 {
1016 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001017 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001018 }
1019}
1020
1021DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
1022 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001023 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001024 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001025 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001026
1027 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001028 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001029
1030 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001031 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001032
1033 {
1034 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001035 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001036 }
1037}
1038
1039DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1040 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001041 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001042 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001043 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001044
1045 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001046 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001047}
1048
1049DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1050 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001051 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001052 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001053 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001054
1055 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001056 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001057
1058 {
1059 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001060 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001061 }
1062}
1063
1064DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1065 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001066 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001067 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001068 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001069
1070 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001071 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001072
1073 {
1074 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001075 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001076 }
1077}
1078
1079DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1080 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001081 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001082 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001083 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001084
1085 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001086 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001087
1088 {
1089 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001090 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001091 }
1092}
1093
1094DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1095 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001096 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001097 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001098 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001099
1100 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001101 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001102
1103 {
1104 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001105 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001106 }
1107}
1108
1109DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1110 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001111 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001112 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001113 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001114
1115 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001116 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001117
1118 {
1119 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001120 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001121 }
1122}
1123
1124DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1125 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001126 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001127 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001128 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001129
1130 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001131 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001132
1133 {
1134 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001135 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001136 }
1137}
1138
1139DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1140 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001141 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001142 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001143 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001144
1145 {
1146 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001147 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001148 }
1149}
1150
1151DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1152 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001153 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001154 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001155 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001156
1157 {
1158 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001159 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001160 }
1161}
1162
1163DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1164 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001165 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001166 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001167 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001168
1169 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001170 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001171
1172 {
1173 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001174 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001175 }
1176
1177 {
1178 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001179 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001180 }
1181
1182 {
1183 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001184 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001185 }
1186
1187 {
1188 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001189 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001190 }
1191}
1192
1193DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1194 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001195 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001196 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001197 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001198
1199 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001200 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001201
1202 {
1203 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001204 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001205 }
1206
1207 {
1208 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001209 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001210 }
1211
1212 {
1213 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001214 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001215 }
1216
1217 {
1218 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001219 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001220 }
1221}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001222
Ethan Nicholas722cb672021-05-06 10:47:06 -04001223DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1224 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1225 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001226 DSLExpression sqrt(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001227 SkTArray<DSLWrapper<DSLExpression>> args;
John Stiles443fa192021-05-24 23:46:46 -04001228 args.emplace_back(16);
1229 EXPECT_EQUAL(sqrt(std::move(args)), "4.0"); // sqrt(16) gets optimized to 4
Ethan Nicholas722cb672021-05-06 10:47:06 -04001230 }
1231
1232 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001233 DSLExpression pow(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001234 DSLVar a(kFloat_Type, "a");
1235 DSLVar b(kFloat_Type, "b");
1236 SkTArray<DSLWrapper<DSLExpression>> args;
1237 args.emplace_back(a);
1238 args.emplace_back(b);
1239 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1240 }
1241}
1242
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001243DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001244 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas722cb672021-05-06 10:47:06 -04001245 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001246 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001247 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001248
Ethan Nicholas722cb672021-05-06 10:47:06 -04001249 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1250
1251 SkTArray<DSLStatement> statements;
1252 statements.push_back(a = 0);
1253 statements.push_back(++a);
1254 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001255}
1256
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001257DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001258 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001259 Var i(kInt_Type, "i", 0);
1260 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001261 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001262 If(i > 5, Break())
1263 ))
1264 );
1265 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001266 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1267 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001268
1269 {
1270 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001271 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001272 Break()
1273 );
1274 }
1275}
1276
1277DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001278 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001279 Var i(kInt_Type, "i", 0);
1280 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001281 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001282 If(i < 5, Continue())
1283 ))
1284 );
1285 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001286 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1287 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001288
1289 {
1290 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001291 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001292 Continue()
1293 );
1294 }
1295}
1296
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001297DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001298 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001299 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001300 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001301 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001302 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001303 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001304
1305 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001306 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001307 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001308 Declare(c).release();
1309 }
1310
1311 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001312 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001313 Declare(d).release();
1314 ExpectError error(r, "error: variable has already been declared\n");
1315 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001316 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001317
1318 {
1319 Var e(kUniform_Modifier, kInt_Type, "e");
1320 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1321 Declare(e).release();
1322 }
1323}
1324
1325DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001326 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001327 Var x(kInt_Type, "x", 0);
1328 DeclareGlobal(x);
1329 Var y(kUniform_Modifier, kFloat2_Type, "y");
1330 DeclareGlobal(y);
1331 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1332 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1333 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001334}
1335
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001336DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1337 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
John Stiles443fa192021-05-24 23:46:46 -04001338 Var x(kFloat_Type, "x", 1);
1339 EXPECT_EQUAL(If(Sqrt(x) > 0, Discard()), "if ((sqrt(x) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001340}
1341
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001342DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1343 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1344 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001345 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001346
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001347 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001348 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001349 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001350
1351 {
1352 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1353 Do(Block(), 7).release();
1354 }
1355}
1356
1357DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001358 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles8676ebe2021-04-20 15:30:41 -04001359 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1360 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001361
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001362 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001363 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1364 "for (int i = 0; (i < 10); ++i) (i += 5);");
1365
1366 Var j(kInt_Type, "j", 0);
1367 Var k(kInt_Type, "k", 10);
1368 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1369 {
1370 int j = 0;
1371 int k = 10;
1372 for (; (j < k); ++j) {}
1373 }
1374 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001375
1376 {
1377 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1378 For(i = 0, i + 10, ++i, i += 5).release();
1379 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001380
1381 {
1382 ExpectError error(r, "error: invalid for loop initializer\n");
1383 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1384 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001385}
1386
Ethan Nicholase2c05042021-02-03 10:27:22 -05001387DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001388 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001389 Var coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001390 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001391 sk_FragColor() = Half4(coords, 0, 1)
1392 );
1393 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001394 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001395 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001396
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001397 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001398 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001399 Var x(kFloat_Type, "x");
1400 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001401 sqr.define(
1402 Return(x * x)
1403 );
1404 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1405 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1406 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1407 }
1408
1409 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001410 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001411 Var x(kFloat2_Type, "x");
1412 Var y(kFloat2_Type, "y");
1413 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001414 dot.define(
1415 Return(x * x + y * y)
1416 );
1417 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1418 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1419 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1420 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1421 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1422 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001423
1424 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001425 DSLWriter::Reset();
1426 Var x(kFloat_Type, "x");
1427 Var y(kFloat_Type, "y");
1428 DSLFunction pair(kFloat2_Type, "pair", x, y);
1429 pair.define(
1430 Return(Float2(x, y))
1431 );
1432 Var varArg1(kFloat_Type, "varArg1");
1433 Var varArg2(kFloat_Type, "varArg2");
1434 DSLWriter::MarkDeclared(varArg1);
1435 DSLWriter::MarkDeclared(varArg2);
1436 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1437 }
1438
1439 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001440 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001441 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001442 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001443 Return(true)
1444 );
1445 }
1446
1447 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001448 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001449 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001450 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001451 Return()
1452 );
1453 }
1454
1455 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001456 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001457 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001458 Var x(kFloat_Type, "x", 0);
1459 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001460 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001461 If(x == 1, Return(x))
1462 );
1463 }
1464
1465 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001466 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001467 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001468 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001469 Return(0)
1470 );
1471 }
1472
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001473 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001474 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001475 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001476 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001477 );
1478 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001479
1480 {
1481 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1482 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001483 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001484 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001485 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001486 );
1487 }
1488
1489 {
1490 ExpectError error(r, "error: variable has already been declared\n");
1491 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001492 DSLVar p(kFloat_Type);
1493 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001494 );
1495 Declare(p).release();
1496 }
1497
1498 {
1499 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1500 "values\n");
1501 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001502 DSLVar p(kFloat_Type, 1);
1503 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001504 );
1505 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001506}
1507
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001508DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1509 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001510 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001511 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001512 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001513
1514 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001515 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001516
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001517 Statement z = StaticIf(a > b, a -= b, b -= a);
1518 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1519
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001520 {
1521 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1522 If(a + b, a -= b).release();
1523 }
1524}
1525
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001526DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1527 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1528
1529 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001530 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001531
1532 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001533 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001534}
1535
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001536DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1537 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001538 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001539 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001540 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001541
1542 {
1543 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001544 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001545 }
1546
1547 {
1548 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001549 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001550 }
1551}
1552
Ethan Nicholascfefec02021-02-09 15:22:57 -05001553DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1554 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1555
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001556 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001557
Ethan Nicholas722cb672021-05-06 10:47:06 -04001558 SkTArray<DSLStatement> caseStatements;
1559 caseStatements.push_back(a = 1);
1560 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001561 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001562 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001563 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001564 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001565 Default(Discard())
1566 );
John Stilese1d1b082021-02-23 13:44:36 -05001567 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001568 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001569 case 0: (a = 0.0); break;
1570 case 1: (a = 1.0); continue;
1571 case 2: (a = 2.0);
1572 default: discard;
1573 }
1574 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001575
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001576 Statement y = StaticSwitch(b,
1577 Case(0, a = 0, Break()),
1578 Case(1, a = 1, Continue()),
1579 Case(2, a = 2 /*Fallthrough*/),
1580 Default(Discard())
1581 );
1582 EXPECT_EQUAL(y, R"(
1583 @switch (b) {
1584 case 0: (a = 0.0); break;
1585 case 1: (a = 1.0); continue;
1586 case 2: (a = 2.0);
1587 default: discard;
1588 }
1589 )");
1590
John Stiles642cde22021-02-23 14:57:01 -05001591 EXPECT_EQUAL(Switch(b),
1592 "switch (b) {}");
1593
1594 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1595 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001596
1597 {
John Stilese1d1b082021-02-23 13:44:36 -05001598 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001599 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001600 }
1601
1602 {
John Stilese1d1b082021-02-23 13:44:36 -05001603 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001604 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001605 }
1606
1607 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001608 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001609 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001610 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001611 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001612}
1613
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001614DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001615 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001616 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001617
John Stilesf04e09c2021-03-05 13:13:14 -05001618 EXPECT_EQUAL(a.x(),
1619 "a.x");
1620 EXPECT_EQUAL(a.y(),
1621 "a.y");
1622 EXPECT_EQUAL(a.z(),
1623 "a.z");
1624 EXPECT_EQUAL(a.w(),
1625 "a.w");
1626 EXPECT_EQUAL(a.r(),
1627 "a.x");
1628 EXPECT_EQUAL(a.g(),
1629 "a.y");
1630 EXPECT_EQUAL(a.b(),
1631 "a.z");
1632 EXPECT_EQUAL(a.a(),
1633 "a.w");
1634 EXPECT_EQUAL(Swizzle(a, R),
1635 "a.x");
1636 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1637 "float2(0.0, a.y)");
1638 EXPECT_EQUAL(Swizzle(a, B, G, G),
1639 "a.zyy");
1640 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1641 "float4(a.xyz, 1.0)");
1642 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1643 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001644}
1645
John Stiles08771b02021-04-26 09:35:10 -04001646
1647DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001648 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles08771b02021-04-26 09:35:10 -04001649
1650 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1651 Var a;
1652 if (true)
1653 {
1654 Var(kInt_Type, "a").swap(a);
1655 }
1656
1657 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1658 "{ int a; (a = 123); }");
1659}
1660
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001661DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1662 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1663 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001664 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001665
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001666 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001667 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001668 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001669
1670 {
1671 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001672 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001673 }
1674}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001675
1676DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1677 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001678 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001679
1680 EXPECT_EQUAL(a[0], "a[0]");
1681 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001682
1683 {
1684 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001685 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001686 }
1687
1688 {
1689 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001690 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001691 }
1692
1693 {
1694 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001695 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001696 }
1697}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001698
1699DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1700 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1701 // There is a Fract type on Mac which can conflict with our Fract builtin
1702 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001703 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1704 Var h3(kHalf3_Type, "h3");
1705 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001706 EXPECT_EQUAL(Abs(a), "abs(a)");
1707 EXPECT_EQUAL(All(b4), "all(b4)");
1708 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001709 EXPECT_EQUAL(Atan(a), "atan(a)");
1710 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001711 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1712 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1713 EXPECT_EQUAL(Cos(a), "cos(a)");
1714 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1715 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1716 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1717 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1718 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1719 EXPECT_EQUAL(Exp(a), "exp(a)");
1720 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1721 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1722 EXPECT_EQUAL(Floor(a), "floor(a)");
1723 EXPECT_EQUAL(Fract(a), "fract(a)");
1724 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1725 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1726 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1727 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1728 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1729 EXPECT_EQUAL(Length(a), "length(a)");
1730 EXPECT_EQUAL(Log(a), "log(a)");
1731 EXPECT_EQUAL(Log2(a), "log2(a)");
1732 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1733 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1734 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1735 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1736 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1737 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1738 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1739 EXPECT_EQUAL(Radians(a), "radians(a)");
1740 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1741 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1742 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1743 EXPECT_EQUAL(Sign(a), "sign(a)");
1744 EXPECT_EQUAL(Sin(a), "sin(a)");
1745 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1746 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1747 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1748 EXPECT_EQUAL(Tan(a), "tan(a)");
1749 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001750
1751 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1752 // one of them reports errors correctly
1753 {
1754 ExpectError error(r, "error: no match for ceil(bool)\n");
1755 Ceil(a == b).release();
1756 }
1757}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001758
1759DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001760 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001761
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001762 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001763 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001764 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001765
1766 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1767 // context, so we can't as yet Declare() variables with these modifiers.
1768 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001769 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001770 REPORTER_ASSERT(r, v2.modifiers().flags() == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001771 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001772
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001773 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001774 REPORTER_ASSERT(r, v3.modifiers().flags() == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001775 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001776
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001777 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001778 REPORTER_ASSERT(r, v4.modifiers().flags() == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001779 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001780
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001781 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001782 REPORTER_ASSERT(r, v5.modifiers().flags() == SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001783 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001784
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001785 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001786 REPORTER_ASSERT(r, v6.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1787 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001788 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001789
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001790 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001791 REPORTER_ASSERT(r, v7.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1792 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001793 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001794
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001795 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001796 REPORTER_ASSERT(r, v8.modifiers().flags() == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001797 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001798 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001799}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001800
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001801DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001802 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001803 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1804 .inputAttachmentIndex(7),
1805 kConst_Modifier), kInt_Type, "v1", 0);
1806 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1807 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1808
1809 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1810 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1811
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001812 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1813 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1814
1815 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1816 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1817
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001818 {
1819 ExpectError error(r, "error: 'srgb_unpremul' is only permitted in runtime effects\n");
1820 Var v(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kHalf4_Type, "v");
1821 DeclareGlobal(v);
1822 }
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001823
1824 {
1825 ExpectError error(r, "error: layout qualifier 'location' appears more than once\n");
1826 DSLLayout().location(1).location(2);
1827 }
1828
1829 {
1830 ExpectError error(r, "error: layout qualifier 'set' appears more than once\n");
1831 DSLLayout().set(1).set(2);
1832 }
1833
1834 {
1835 ExpectError error(r, "error: layout qualifier 'binding' appears more than once\n");
1836 DSLLayout().binding(1).binding(2);
1837 }
1838
1839 {
1840 ExpectError error(r, "error: layout qualifier 'offset' appears more than once\n");
1841 DSLLayout().offset(1).offset(2);
1842 }
1843
1844 {
1845 ExpectError error(r, "error: layout qualifier 'index' appears more than once\n");
1846 DSLLayout().index(1).index(2);
1847 }
1848
1849 {
1850 ExpectError error(r, "error: layout qualifier 'builtin' appears more than once\n");
1851 DSLLayout().builtin(1).builtin(2);
1852 }
1853
1854 {
1855 ExpectError error(r, "error: layout qualifier 'input_attachment_index' appears more than "
1856 "once\n");
1857 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1858 }
1859
1860 {
1861 ExpectError error(r, "error: layout qualifier 'origin_upper_left' appears more than "
1862 "once\n");
1863 DSLLayout().originUpperLeft().originUpperLeft();
1864 }
1865
1866 {
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001867 ExpectError error(r, "error: layout qualifier 'push_constant' appears more than once\n");
1868 DSLLayout().pushConstant().pushConstant();
1869 }
1870
1871 {
1872 ExpectError error(r, "error: layout qualifier 'blend_support_all_equations' appears more "
1873 "than once\n");
1874 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1875 }
1876
1877 {
1878 ExpectError error(r, "error: layout qualifier 'srgb_unpremul' appears more than once\n");
1879 DSLLayout().srgbUnpremul().srgbUnpremul();
1880 }
1881}
1882
Ethan Nicholas624a5292021-04-16 14:54:43 -04001883DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001884 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(),
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001885 SkSL::ProgramKind::kFragmentProcessor);
1886 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1887 EXPECT_EQUAL(Sample(child), "sample(child)");
1888 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1889 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001890 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001891
1892 {
1893 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1894 Sample(child, true).release();
1895 }
1896}
1897
Ethan Nicholas624a5292021-04-16 14:54:43 -04001898DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001899 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(),
Brian Osman552fcb92021-04-28 17:41:57 -04001900 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001901 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001902 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001903
1904 {
Brian Osmanc9125aa2021-04-21 09:57:19 -04001905 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001906 Sample(shader, Half4(1)).release();
1907 }
1908}
1909
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001910DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001911 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001912
1913 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001914 Field(kFloat_Type, "x"),
1915 Field(kBool_Type, "b"),
1916 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001917 );
1918 DSLVar result(simpleStruct, "result");
1919 DSLFunction(simpleStruct, "returnStruct").define(
1920 Declare(result),
1921 result.field("x") = 123,
1922 result.field("b") = result.field("x") > 0,
1923 result.field("a")[0] = result.field("x"),
1924 Return(result)
1925 );
1926 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001927 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1928 "struct SimpleStruct { float x; bool b; float[3] a; };");
1929 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1930 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1931 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001932
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001933 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001934 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001935 Field(simpleStruct, "simple")
1936 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001937 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1938 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001939 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001940}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001941
1942DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1943 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1944 std::vector<Wrapper<DSLExpression>> exprs;
1945 exprs.push_back(DSLExpression(1));
1946 exprs.emplace_back(2.0);
1947 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1948 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1949
1950 std::vector<Wrapper<DSLVar>> vars;
1951 vars.emplace_back(DSLVar(kInt_Type, "x"));
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001952 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0])->name() == "x");
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001953}