blob: 1408ad35f822a2841350c020c8c1f417970c49dd [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 Nicholas22dcb732021-05-12 11:26:58 -040021constexpr int kDefaultTestFlags = (kDefaultDSLFlags & ~kMangle_Flag) | kMarkVarsDeclared_Flag;
22constexpr int kNoDeclareTestFlags = kDefaultDSLFlags & ~kMangle_Flag;
23
Ethan Nicholas961d9442021-03-16 16:37:29 -040024/**
25 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
26 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
27 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
28 * errors, especially given that some of the variables have options that make them an error to
29 * actually declare.
30 */
Ethan Nicholas95046142021-01-07 10:57:27 -050031class AutoDSLContext {
32public:
Ethan Nicholas22dcb732021-05-12 11:26:58 -040033 AutoDSLContext(GrGpu* gpu, int flags = kDefaultTestFlags,
Ethan Nicholasee49efc2021-04-09 15:33:53 -040034 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -040035 Start(gpu->shaderCompiler(), kind, flags);
Ethan Nicholas95046142021-01-07 10:57:27 -050036 }
37
38 ~AutoDSLContext() {
39 End();
40 }
41};
42
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050043class ExpectError : public ErrorHandler {
44public:
45 ExpectError(skiatest::Reporter* reporter, const char* msg)
46 : fMsg(msg)
47 , fReporter(reporter) {
48 SetErrorHandler(this);
49 }
50
51 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050052 REPORTER_ASSERT(fReporter, !fMsg,
53 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050054 SetErrorHandler(nullptr);
55 }
56
Ethan Nicholasb9563042021-02-25 09:45:49 -050057 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050058 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
59 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
60 fMsg = nullptr;
61 }
62
63private:
64 const char* fMsg;
65 skiatest::Reporter* fReporter;
66};
67
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050068static bool whitespace_insensitive_compare(const char* a, const char* b) {
69 for (;;) {
70 while (isspace(*a)) {
71 ++a;
72 }
73 while (isspace(*b)) {
74 ++b;
75 }
76 if (*a != *b) {
77 return false;
78 }
79 if (*a == 0) {
80 return true;
81 }
82 ++a;
83 ++b;
84 }
85}
86
Ethan Nicholas24c17722021-03-09 13:10:59 -050087// for use from SkSLDSLOnlyTest.cpp
88void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
89 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
90}
91
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050092DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050093 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
94 Expression e1 = 1;
95 REPORTER_ASSERT(r, e1.release()->description() == "1");
96 Expression e2 = 1.0;
97 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
98 Expression e3 = true;
99 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400100 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500101 Expression e4 = a;
102 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500103
104 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
105 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
106 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
107 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
108 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
109 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
110 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
111 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500112}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500113
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500114static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
115static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
116static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
117static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
Ethan Nicholas722cb672021-05-06 10:47:06 -0400118static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500119static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
120
121template <typename T>
122static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
123 SkSL::String actual = stringize(input);
124 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
125 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
126 lineNumber, expected, actual.c_str());
127 }
128}
129
130template <typename T>
131static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
132 // This overload allows temporary values to be passed to expect_equal.
133 return expect_equal(r, lineNumber, dsl, expected);
134}
135
136#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
137
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400138DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
139 {
140 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kOptimize_Flag);
141 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true");
142
143 Var x(kInt_Type, "x");
144 EXPECT_EQUAL(Declare(x), "int x;");
145 }
146
147 {
148 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNo_Flag);
149 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))),
150 "all(greaterThan(float4(1.0), float4(0.0)))");
151 }
152
153 {
154 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kMangle_Flag);
155 Var x(kInt_Type, "x");
156 EXPECT_EQUAL(Declare(x), "int _0_x;");
157 }
158}
159
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500160DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
161 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
162 Expression e1 = Float(std::numeric_limits<float>::max());
163 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
164 std::numeric_limits<float>::max());
165
166 Expression e2 = Float(std::numeric_limits<float>::min());
167 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
168 std::numeric_limits<float>::min());
169
John Stilesb4d7b582021-02-19 09:56:31 -0500170 EXPECT_EQUAL(Float2(0),
171 "float2(0.0)");
172 EXPECT_EQUAL(Float2(-0.5, 1),
173 "float2(-0.5, 1.0)");
174 EXPECT_EQUAL(Float3(0.75),
175 "float3(0.75)");
176 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500177 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500178 EXPECT_EQUAL(Float3(0, 1, 2),
179 "float3(0.0, 1.0, 2.0)");
180 EXPECT_EQUAL(Float4(0),
181 "float4(0.0)");
182 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500183 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500184 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500185 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500186 EXPECT_EQUAL(Float4(0, 1, 2, 3),
187 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500188
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400189 DSLVar x(kFloat_Type, "x");
190 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
191 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
192
193 DSLVar y(kFloat2_Type, "y");
194 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
195 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
196
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500197 {
198 ExpectError error(r, "error: floating point value is infinite\n");
199 Float(std::numeric_limits<float>::infinity()).release();
200 }
201
202 {
203 ExpectError error(r, "error: floating point value is NaN\n");
204 Float(std::numeric_limits<float>::quiet_NaN()).release();
205 }
206
207 {
208 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
209 " but found 4)\n");
210 Float2(Float4(1)).release();
211 }
212
213 {
214 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
215 " but found 3)\n");
216 Float4(Float3(1)).release();
217 }
218}
219
220DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
221 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
222 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500223 REPORTER_ASSERT(r,
224 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500225
226 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500227 REPORTER_ASSERT(r,
228 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500229
John Stilesb9e4f642021-03-05 09:11:38 -0500230 EXPECT_EQUAL(Half2(0),
231 "half2(0.0)");
232 EXPECT_EQUAL(Half2(-0.5, 1),
233 "half2(-0.5, 1.0)");
234 EXPECT_EQUAL(Half3(0.75),
235 "half3(0.75)");
236 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
237 "half3(0.0, 1.0, -2.0)");
238 EXPECT_EQUAL(Half3(0, 1, 2),
239 "half3(0.0, 1.0, 2.0)");
240 EXPECT_EQUAL(Half4(0),
241 "half4(0.0)");
242 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
243 "half4(0.0, 1.0, 2.0, 3.0)");
244 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
245 "half4(0.0, 1.0, 2.0, 3.0)");
246 EXPECT_EQUAL(Half4(0, 1, 2, 3),
247 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500248
249 {
250 ExpectError error(r, "error: floating point value is infinite\n");
251 Half(std::numeric_limits<float>::infinity()).release();
252 }
253
254 {
255 ExpectError error(r, "error: floating point value is NaN\n");
256 Half(std::numeric_limits<float>::quiet_NaN()).release();
257 }
258
259 {
260 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
261 " but found 4)\n");
262 Half2(Half4(1)).release();
263 }
264
265 {
266 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
267 " but found 3)\n");
268 Half4(Half3(1)).release();
269 }
270}
271
272DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
273 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500274
John Stilesb9e4f642021-03-05 09:11:38 -0500275 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
276 "2147483647");
277 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
278 "int2(-2147483648)");
279 EXPECT_EQUAL(Int2(0, 1),
280 "int2(0, 1)");
281 EXPECT_EQUAL(Int3(0),
282 "int3(0)");
283 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
284 "int3(0, 1, -2)");
285 EXPECT_EQUAL(Int3(0, 1, 2),
286 "int3(0, 1, 2)");
287 EXPECT_EQUAL(Int4(0),
288 "int4(0)");
289 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
290 "int4(0, 1, 2, 3)");
291 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
292 "int4(0, 1, 2, 3)");
293 EXPECT_EQUAL(Int4(0, 1, 2, 3),
294 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500295
296 {
297 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
298 " but found 4)\n");
299 Int2(Int4(1)).release();
300 }
301
302 {
303 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
304 " but found 3)\n");
305 Int4(Int3(1)).release();
306 }
307}
308
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400309DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
310 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
311
312 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
313 "4294967295");
314 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
315 "uint2(0)");
316 EXPECT_EQUAL(UInt2(0, 1),
317 "uint2(0, 1)");
318 EXPECT_EQUAL(UInt3(0),
319 "uint3(0)");
320 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
321 "uint3(0, 1, -2)");
322 EXPECT_EQUAL(UInt3(0, 1, 2),
323 "uint3(0, 1, 2)");
324 EXPECT_EQUAL(UInt4(0),
325 "uint4(0)");
326 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
327 "uint4(0, 1, 2, 3)");
328 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
329 "uint4(0, 1, 2, 3)");
330 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
331 "uint4(0, 1, 2, 3)");
332
333 {
334 ExpectError error(r, "error: invalid arguments to 'uint2' constructor (expected 2 scalars,"
335 " but found 4)\n");
336 UInt2(UInt4(1)).release();
337 }
338
339 {
340 ExpectError error(r, "error: invalid arguments to 'uint4' constructor (expected 4 scalars,"
341 " but found 3)\n");
342 UInt4(UInt3(1)).release();
343 }
344}
345
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500346DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
347 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500348
John Stilesb9e4f642021-03-05 09:11:38 -0500349 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
350 "32767");
351 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
352 "short2(-32768)");
353 EXPECT_EQUAL(Short2(0, 1),
354 "short2(0, 1)");
355 EXPECT_EQUAL(Short3(0),
356 "short3(0)");
357 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
358 "short3(0, 1, -2)");
359 EXPECT_EQUAL(Short3(0, 1, 2),
360 "short3(0, 1, 2)");
361 EXPECT_EQUAL(Short4(0),
362 "short4(0)");
363 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
364 "short4(0, 1, 2, 3)");
365 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
366 "short4(0, 1, 2, 3)");
367 EXPECT_EQUAL(Short4(0, 1, 2, 3),
368 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500369
370 {
371 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
372 " but found 4)\n");
373 Short2(Short4(1)).release();
374 }
375
376 {
377 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
378 " but found 3)\n");
379 Short4(Short3(1)).release();
380 }
381}
382
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400383DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
384 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
385
386 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
387 "65535");
388 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
389 "ushort2(0)");
390 EXPECT_EQUAL(UShort2(0, 1),
391 "ushort2(0, 1)");
392 EXPECT_EQUAL(UShort3(0),
393 "ushort3(0)");
394 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
395 "ushort3(0, 1, -2)");
396 EXPECT_EQUAL(UShort3(0, 1, 2),
397 "ushort3(0, 1, 2)");
398 EXPECT_EQUAL(UShort4(0),
399 "ushort4(0)");
400 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
401 "ushort4(0, 1, 2, 3)");
402 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
403 "ushort4(0, 1, 2, 3)");
404 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
405 "ushort4(0, 1, 2, 3)");
406
407 {
408 ExpectError error(r, "error: invalid arguments to 'ushort2' constructor (expected 2 "
409 "scalars, but found 4)\n");
410 UShort2(UShort4(1)).release();
411 }
412
413 {
414 ExpectError error(r, "error: invalid arguments to 'ushort4' constructor (expected 4 "
415 "scalars, but found 3)\n");
416 UShort4(UShort3(1)).release();
417 }
418}
419
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500420DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
421 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500422
John Stilesb9e4f642021-03-05 09:11:38 -0500423 EXPECT_EQUAL(Bool2(false),
424 "bool2(false)");
425 EXPECT_EQUAL(Bool2(false, true),
426 "bool2(false, true)");
427 EXPECT_EQUAL(Bool3(false),
428 "bool3(false)");
429 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
430 "bool3(false, true, false)");
431 EXPECT_EQUAL(Bool3(false, true, false),
432 "bool3(false, true, false)");
433 EXPECT_EQUAL(Bool4(false),
434 "bool4(false)");
435 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
436 "bool4(false, true, false, true)");
437 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
438 "bool4(false, true, false, true)");
439 EXPECT_EQUAL(Bool4(false, true, false, true),
440 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500441
442 {
443 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
444 " but found 4)\n");
445 Bool2(Bool4(true)).release();
446 }
447
448 {
449 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
450 " but found 3)\n");
451 Bool4(Bool3(true)).release();
452 }
453}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500454
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400455DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
456 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
457 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
458 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
459 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
460 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
461 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
462 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
463 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
464 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
465 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
466 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
467 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
468
469 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
470 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
471 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
472 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
473 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
474 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
475 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
476 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
477 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
478 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
479 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
480
481 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
482 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
483 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
484 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
485 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
486 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
487 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
488 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
489 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
490 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
491 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
492
493 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
494 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
495 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
496 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
497 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
498 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
499 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
500 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
501 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
502 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
503 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
504
505 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
506 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
507 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
508 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
509 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
510 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
511 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
512 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
513 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
514 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
515 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
516
517 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
518 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
519 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
520 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
521 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
522 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
523 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
524 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
525 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
526 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
527 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
528
529 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
530 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
531 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
532 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
533 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
534 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
535 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
536 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
537 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
538 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
539 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400540
541 Var x(kFloat_Type);
542 DSLExpression e = x + 1;
543 REPORTER_ASSERT(r, e.type().isFloat());
544 e.release();
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400545}
546
Ethan Nicholas84558932021-04-12 16:56:37 -0400547DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
548 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
549 Var f22(kFloat2x2_Type, "f22");
550 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
551 Var f32(kFloat3x2_Type, "f32");
552 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
553 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
554 Var f42(kFloat4x2_Type, "f42");
555 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
556 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
557 Var f23(kFloat2x3_Type, "f23");
558 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
559 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
560 Var f33(kFloat3x3_Type, "f33");
561 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
562 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
563 Var f43(kFloat4x3_Type, "f43");
564 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
565 "(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))");
566 Var f24(kFloat2x4_Type, "f24");
567 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
568 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
569 Var f34(kFloat3x4_Type, "f34");
570 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
571 "(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))");
572 Var f44(kFloat4x4_Type, "f44");
573 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
574
575 Var h22(kHalf2x2_Type, "h22");
576 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
577 Var h32(kHalf3x2_Type, "h32");
578 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
579 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
580 Var h42(kHalf4x2_Type, "h42");
581 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
582 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
583 Var h23(kHalf2x3_Type, "h23");
584 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
585 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
586 Var h33(kHalf3x3_Type, "h33");
587 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
588 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
589 Var h43(kHalf4x3_Type, "h43");
590 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
591 "(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))");
592 Var h24(kHalf2x4_Type, "h24");
593 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
594 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
595 Var h34(kHalf3x4_Type, "h34");
596 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
597 "(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))");
598 Var h44(kHalf4x4_Type, "h44");
599 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
600
601 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
602 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
603 EXPECT_EQUAL(h42[0][1], "h42[0].y");
604 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
605 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
606 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
607
608 {
609 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
610 "scalars, but found 2)\n");
611 DSLExpression(Float3x3(Float2(1))).release();
612 }
613
614 {
615 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
616 "scalars, but found 5)\n");
617 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
618 }
619
620 {
621 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
622 DSLExpression(f43 * Float3(1)).release();
623 }
624
625 {
626 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
627 "'float3x3'\n");
628 DSLExpression(f43 = f33).release();
629 }
630
631 {
632 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
633 "'float2x2'\n");
634 DSLExpression(h22 = f22).release();
635 }
636
637 {
638 ExpectError error(r,
639 "error: no match for inverse(float4x3)\n");
640 DSLExpression(Inverse(f43)).release();
641 }
642}
643
Ethan Nicholas92969f22021-01-13 10:38:59 -0500644DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
645 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400646 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500647
John Stiles8f440b42021-03-05 16:48:56 -0500648 EXPECT_EQUAL(a + b,
649 "(a + b)");
650 EXPECT_EQUAL(a + 1,
651 "(a + 1.0)");
652 EXPECT_EQUAL(0.5 + a + -99,
653 "((0.5 + a) + -99.0)");
654 EXPECT_EQUAL(a += b + 1,
655 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500656 EXPECT_EQUAL(+a,
657 "a");
658 EXPECT_EQUAL(+(a + b),
659 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500660
661 {
662 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500663 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500664 }
665
666 {
667 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500668 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500669 }
670
671 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500672 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500673 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500675
676 {
677 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400678 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500679 DSLExpression(+c);
680 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500681}
682
683DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
684 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400685 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500686
John Stiles8f440b42021-03-05 16:48:56 -0500687 EXPECT_EQUAL(a - b,
688 "(a - b)");
689 EXPECT_EQUAL(a - 1,
690 "(a - 1)");
691 EXPECT_EQUAL(2 - a - b,
692 "((2 - a) - b)");
693 EXPECT_EQUAL(a -= b + 1,
694 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500695 EXPECT_EQUAL(-a,
696 "-a");
697 EXPECT_EQUAL(-(a - b),
698 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500699
700 {
701 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500702 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500703 }
704
705 {
706 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500707 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500708 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500709
710 {
711 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500712 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500713 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500714
715 {
716 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400717 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500718 DSLExpression(-c);
719 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500720}
721
722DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
723 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400724 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500725
John Stiles8f440b42021-03-05 16:48:56 -0500726 EXPECT_EQUAL(a * b,
727 "(a * b)");
728 EXPECT_EQUAL(a * 2,
729 "(a * 2.0)");
730 EXPECT_EQUAL(0.5 * a * -99,
731 "((0.5 * a) * -99.0)");
732 EXPECT_EQUAL(a *= b + 1,
733 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500734
735 {
736 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500737 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500738 }
739
740 {
741 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500742 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500743 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500744
745 {
746 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500747 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500748 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500749}
750
751DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
752 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400753 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500754
John Stiles8f440b42021-03-05 16:48:56 -0500755 EXPECT_EQUAL(a / b,
756 "(a / b)");
757 EXPECT_EQUAL(a / 2,
758 "(a / 2.0)");
759 EXPECT_EQUAL(0.5 / a / -99,
760 "((0.5 / a) / -99.0)");
761 EXPECT_EQUAL(b / (a - 1),
762 "(b / (a - 1.0))");
763 EXPECT_EQUAL(a /= b + 1,
764 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500765
766 {
767 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500768 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500769 }
770
771 {
772 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500773 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500774 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500775
776 {
777 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500778 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500779 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500780
781 {
782 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500783 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500784 }
785
786 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400787 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500788 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500789 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500790 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500791}
792
793DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
794 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400795 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500796 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500797 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500798
799 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500800 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500801
802 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500803 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500804
805 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500806 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500807
808 {
809 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500810 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500811 }
812
813 {
814 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500815 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500816 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500817
818 {
819 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500820 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500821 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500822
823 {
824 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500825 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500826 }
827
828 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400829 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500830 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500831 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500832 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833}
834
835DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
836 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400837 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500838 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500839 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500840
841 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500842 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500843
844 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500845 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500846
847 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500848 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500849
850 {
851 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500852 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500853 }
854
855 {
856 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500857 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500858 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500859
860 {
861 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500862 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500863 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500864}
865
866DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
867 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400868 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500869 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500870 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871
872 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500873 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500874
875 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500876 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500877
878 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500879 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500880
881 {
882 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500883 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500884 }
885
886 {
887 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500888 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500889 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500890
891 {
892 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500893 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500894 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500895}
896
897DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
898 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400899 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500900 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500901 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500902
903 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500904 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500905
906 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500907 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500908
909 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500910 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500911
912 {
913 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500914 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500915 }
916
917 {
918 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500919 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500920 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500921
922 {
923 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500924 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500925 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500926}
927
928DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
929 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400930 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500931 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500932 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500933
934 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500935 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500936
937 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500938 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500939
940 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500941 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500942
943 {
944 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500945 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500946 }
947
948 {
949 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500950 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500951 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500952
953 {
954 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500955 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500956 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500957}
958
959DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
960 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400961 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500962 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500963 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500964
965 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500966 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500967
968 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500969 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500970
971 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500972 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500973
974 {
975 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500976 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500977 }
978
979 {
980 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500981 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500982 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500983
984 {
985 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500986 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500987 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500988}
989
990DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
991 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400992 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500993 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500994 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500995
996 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500997 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500998
999 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001000 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001001
1002 {
1003 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001004 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001005 }
1006}
1007
1008DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
1009 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001010 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001011 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001012 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001013
1014 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001015 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001016
1017 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001018 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001019
1020 {
1021 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001022 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001023 }
1024}
1025
1026DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1027 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001028 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001029 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001030 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001031
1032 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001033 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001034}
1035
1036DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1037 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001038 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001039 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001040 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001041
1042 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001043 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001044
1045 {
1046 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001047 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001048 }
1049}
1050
1051DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1052 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001053 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001054 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001055 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001056
1057 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001058 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001059
1060 {
1061 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001062 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001063 }
1064}
1065
1066DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1067 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001068 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001069 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001070 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001071
1072 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001073 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001074
1075 {
1076 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001077 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001078 }
1079}
1080
1081DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1082 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001083 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001084 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001085 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001086
1087 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001088 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001089
1090 {
1091 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001092 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001093 }
1094}
1095
1096DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1097 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001098 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001099 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001100 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001101
1102 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001103 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001104
1105 {
1106 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001107 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001108 }
1109}
1110
1111DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1112 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001113 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001114 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001115 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001116
1117 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001118 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001119
1120 {
1121 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001122 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001123 }
1124}
1125
1126DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1127 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001128 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001129 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001130 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001131
1132 {
1133 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001134 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001135 }
1136}
1137
1138DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1139 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001140 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001141 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001142 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001143
1144 {
1145 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001146 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001147 }
1148}
1149
1150DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1151 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001152 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001153 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001154 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001155
1156 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001157 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001158
1159 {
1160 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001161 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001162 }
1163
1164 {
1165 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001166 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001167 }
1168
1169 {
1170 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001171 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001172 }
1173
1174 {
1175 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001176 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001177 }
1178}
1179
1180DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1181 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001182 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001183 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001184 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001185
1186 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001187 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001188
1189 {
1190 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001191 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001192 }
1193
1194 {
1195 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001196 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001197 }
1198
1199 {
1200 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001201 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001202 }
1203
1204 {
1205 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001206 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001207 }
1208}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001209
Ethan Nicholas722cb672021-05-06 10:47:06 -04001210DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1211 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1212 {
1213 DSLExpression sqrt = DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt");
1214 SkTArray<DSLWrapper<DSLExpression>> args;
1215 args.emplace_back(1);
1216 EXPECT_EQUAL(sqrt(std::move(args)), "sqrt(1.0)");
1217 }
1218
1219 {
1220 DSLExpression pow = DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow");
1221 DSLVar a(kFloat_Type, "a");
1222 DSLVar b(kFloat_Type, "b");
1223 SkTArray<DSLWrapper<DSLExpression>> args;
1224 args.emplace_back(a);
1225 args.emplace_back(b);
1226 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1227 }
1228}
1229
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001230DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001231 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001232 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001233 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001234 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001235
Ethan Nicholas722cb672021-05-06 10:47:06 -04001236 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1237
1238 SkTArray<DSLStatement> statements;
1239 statements.push_back(a = 0);
1240 statements.push_back(++a);
1241 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001242}
1243
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001244DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001245 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001246 Var i(kInt_Type, "i", 0);
1247 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001248 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001249 If(i > 5, Break())
1250 ))
1251 );
1252 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001253 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1254 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001255
1256 {
1257 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001258 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001259 Break()
1260 );
1261 }
1262}
1263
1264DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001265 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001266 Var i(kInt_Type, "i", 0);
1267 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001268 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001269 If(i < 5, Continue())
1270 ))
1271 );
1272 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001273 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1274 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001275
1276 {
1277 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001278 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001279 Continue()
1280 );
1281 }
1282}
1283
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001284DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001285 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001286 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001287 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001288 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001289 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001290 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001291
1292 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001293 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001294 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001295 Declare(c).release();
1296 }
1297
1298 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001299 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001300 Declare(d).release();
1301 ExpectError error(r, "error: variable has already been declared\n");
1302 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001303 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001304
1305 {
1306 Var e(kUniform_Modifier, kInt_Type, "e");
1307 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1308 Declare(e).release();
1309 }
1310}
1311
1312DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001313 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001314 Var x(kInt_Type, "x", 0);
1315 DeclareGlobal(x);
1316 Var y(kUniform_Modifier, kFloat2_Type, "y");
1317 DeclareGlobal(y);
1318 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1319 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1320 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001321}
1322
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001323DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1324 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1325 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001326 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001327}
1328
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001329DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1330 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1331 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001332 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001333
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001334 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001335 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001336 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001337
1338 {
1339 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1340 Do(Block(), 7).release();
1341 }
1342}
1343
1344DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001345 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
John Stiles8676ebe2021-04-20 15:30:41 -04001346 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1347 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001348
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001349 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001350 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1351 "for (int i = 0; (i < 10); ++i) (i += 5);");
1352
1353 Var j(kInt_Type, "j", 0);
1354 Var k(kInt_Type, "k", 10);
1355 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1356 {
1357 int j = 0;
1358 int k = 10;
1359 for (; (j < k); ++j) {}
1360 }
1361 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001362
1363 {
1364 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1365 For(i = 0, i + 10, ++i, i += 5).release();
1366 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001367
1368 {
1369 ExpectError error(r, "error: invalid for loop initializer\n");
1370 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1371 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001372}
1373
Ethan Nicholase2c05042021-02-03 10:27:22 -05001374DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001375 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001376 Var coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001377 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001378 sk_FragColor() = Half4(coords, 0, 1)
1379 );
1380 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001381 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001382 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001383
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001384 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001385 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001386 Var x(kFloat_Type, "x");
1387 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001388 sqr.define(
1389 Return(x * x)
1390 );
1391 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1392 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1393 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1394 }
1395
1396 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001397 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001398 Var x(kFloat2_Type, "x");
1399 Var y(kFloat2_Type, "y");
1400 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001401 dot.define(
1402 Return(x * x + y * y)
1403 );
1404 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1405 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1406 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1407 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1408 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1409 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001410
1411 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001412 DSLWriter::Reset();
1413 Var x(kFloat_Type, "x");
1414 Var y(kFloat_Type, "y");
1415 DSLFunction pair(kFloat2_Type, "pair", x, y);
1416 pair.define(
1417 Return(Float2(x, y))
1418 );
1419 Var varArg1(kFloat_Type, "varArg1");
1420 Var varArg2(kFloat_Type, "varArg2");
1421 DSLWriter::MarkDeclared(varArg1);
1422 DSLWriter::MarkDeclared(varArg2);
1423 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1424 }
1425
1426 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001427 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001428 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001429 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001430 Return(true)
1431 );
1432 }
1433
1434 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001435 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001436 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001437 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001438 Return()
1439 );
1440 }
1441
1442 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001443 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001444 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001445 Var x(kFloat_Type, "x", 0);
1446 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001447 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001448 If(x == 1, Return(x))
1449 );
1450 }
1451
1452 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001453 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001454 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001455 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001456 Return(0)
1457 );
1458 }
1459
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001460 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001461 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001462 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001463 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001464 );
1465 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001466
1467 {
1468 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1469 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001470 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001471 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001472 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001473 );
1474 }
1475
1476 {
1477 ExpectError error(r, "error: variable has already been declared\n");
1478 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001479 DSLVar p(kFloat_Type);
1480 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001481 );
1482 Declare(p).release();
1483 }
1484
1485 {
1486 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1487 "values\n");
1488 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001489 DSLVar p(kFloat_Type, 1);
1490 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001491 );
1492 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001493}
1494
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001495DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1496 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001497 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001498 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001499 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001500
1501 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001502 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001503
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001504 Statement z = StaticIf(a > b, a -= b, b -= a);
1505 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1506
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001507 {
1508 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1509 If(a + b, a -= b).release();
1510 }
1511}
1512
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001513DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1514 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1515
1516 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001517 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001518
1519 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001520 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001521}
1522
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001523DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1524 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001525 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001526 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001527 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001528
1529 {
1530 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001531 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001532 }
1533
1534 {
1535 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001536 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001537 }
1538}
1539
Ethan Nicholascfefec02021-02-09 15:22:57 -05001540DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1541 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1542
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001543 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001544
Ethan Nicholas722cb672021-05-06 10:47:06 -04001545 SkTArray<DSLStatement> caseStatements;
1546 caseStatements.push_back(a = 1);
1547 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001548 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001549 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001550 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001551 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001552 Default(Discard())
1553 );
John Stilese1d1b082021-02-23 13:44:36 -05001554 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001555 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001556 case 0: (a = 0.0); break;
1557 case 1: (a = 1.0); continue;
1558 case 2: (a = 2.0);
1559 default: discard;
1560 }
1561 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001562
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001563 Statement y = StaticSwitch(b,
1564 Case(0, a = 0, Break()),
1565 Case(1, a = 1, Continue()),
1566 Case(2, a = 2 /*Fallthrough*/),
1567 Default(Discard())
1568 );
1569 EXPECT_EQUAL(y, R"(
1570 @switch (b) {
1571 case 0: (a = 0.0); break;
1572 case 1: (a = 1.0); continue;
1573 case 2: (a = 2.0);
1574 default: discard;
1575 }
1576 )");
1577
John Stiles642cde22021-02-23 14:57:01 -05001578 EXPECT_EQUAL(Switch(b),
1579 "switch (b) {}");
1580
1581 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1582 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001583
1584 {
John Stilese1d1b082021-02-23 13:44:36 -05001585 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001586 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001587 }
1588
1589 {
John Stilese1d1b082021-02-23 13:44:36 -05001590 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001591 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001592 }
1593
1594 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001595 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001596 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001597 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001598 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001599}
1600
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001601DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001602 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001603 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001604
John Stilesf04e09c2021-03-05 13:13:14 -05001605 EXPECT_EQUAL(a.x(),
1606 "a.x");
1607 EXPECT_EQUAL(a.y(),
1608 "a.y");
1609 EXPECT_EQUAL(a.z(),
1610 "a.z");
1611 EXPECT_EQUAL(a.w(),
1612 "a.w");
1613 EXPECT_EQUAL(a.r(),
1614 "a.x");
1615 EXPECT_EQUAL(a.g(),
1616 "a.y");
1617 EXPECT_EQUAL(a.b(),
1618 "a.z");
1619 EXPECT_EQUAL(a.a(),
1620 "a.w");
1621 EXPECT_EQUAL(Swizzle(a, R),
1622 "a.x");
1623 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1624 "float2(0.0, a.y)");
1625 EXPECT_EQUAL(Swizzle(a, B, G, G),
1626 "a.zyy");
1627 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1628 "float4(a.xyz, 1.0)");
1629 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1630 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001631}
1632
John Stiles08771b02021-04-26 09:35:10 -04001633
1634DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001635 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
John Stiles08771b02021-04-26 09:35:10 -04001636
1637 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1638 Var a;
1639 if (true)
1640 {
1641 Var(kInt_Type, "a").swap(a);
1642 }
1643
1644 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1645 "{ int a; (a = 123); }");
1646}
1647
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001648DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1649 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1650 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001651 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001652
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001653 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001654 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001655 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001656
1657 {
1658 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001659 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001660 }
1661}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001662
1663DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1664 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001665 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001666
1667 EXPECT_EQUAL(a[0], "a[0]");
1668 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001669
1670 {
1671 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001672 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001673 }
1674
1675 {
1676 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001677 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001678 }
1679
1680 {
1681 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001682 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001683 }
1684}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001685
1686DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1687 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1688 // There is a Fract type on Mac which can conflict with our Fract builtin
1689 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001690 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1691 Var h3(kHalf3_Type, "h3");
1692 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001693 EXPECT_EQUAL(Abs(a), "abs(a)");
1694 EXPECT_EQUAL(All(b4), "all(b4)");
1695 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001696 EXPECT_EQUAL(Atan(a), "atan(a)");
1697 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001698 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1699 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1700 EXPECT_EQUAL(Cos(a), "cos(a)");
1701 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1702 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1703 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1704 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1705 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1706 EXPECT_EQUAL(Exp(a), "exp(a)");
1707 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1708 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1709 EXPECT_EQUAL(Floor(a), "floor(a)");
1710 EXPECT_EQUAL(Fract(a), "fract(a)");
1711 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1712 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1713 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1714 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1715 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1716 EXPECT_EQUAL(Length(a), "length(a)");
1717 EXPECT_EQUAL(Log(a), "log(a)");
1718 EXPECT_EQUAL(Log2(a), "log2(a)");
1719 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1720 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1721 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1722 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1723 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1724 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1725 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1726 EXPECT_EQUAL(Radians(a), "radians(a)");
1727 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1728 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1729 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1730 EXPECT_EQUAL(Sign(a), "sign(a)");
1731 EXPECT_EQUAL(Sin(a), "sin(a)");
1732 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1733 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1734 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1735 EXPECT_EQUAL(Tan(a), "tan(a)");
1736 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001737
1738 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1739 // one of them reports errors correctly
1740 {
1741 ExpectError error(r, "error: no match for ceil(bool)\n");
1742 Ceil(a == b).release();
1743 }
1744}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001745
1746DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001747 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001748
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001749 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001750 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001751 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001752
1753 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1754 // context, so we can't as yet Declare() variables with these modifiers.
1755 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001756 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001757 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001758 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001759
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001760 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001761 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001762 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001763
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001764 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001765 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001766 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001767
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001768 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001769 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001770 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001771 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001772
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001773 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001774 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1775 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001776 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001777
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001778 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001779 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1780 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001781 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001782
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001783 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001784 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001785 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001786 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001787}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001788
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001789DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001790 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001791 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1792 .inputAttachmentIndex(7),
1793 kConst_Modifier), kInt_Type, "v1", 0);
1794 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1795 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1796
1797 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1798 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1799
1800 Var v3(DSLLayout().overrideCoverage(), kHalf_Type, "v3");
1801 EXPECT_EQUAL(Declare(v3), "layout (override_coverage) half v3;");
1802
1803 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1804 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1805
1806 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1807 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1808
1809 Var v6(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kBool_Type, "v6");
1810 DeclareGlobal(v6);
1811 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "layout (srgb_unpremul) uniform bool v6;");
1812
1813 {
1814 ExpectError error(r, "error: layout qualifier 'location' appears more than once\n");
1815 DSLLayout().location(1).location(2);
1816 }
1817
1818 {
1819 ExpectError error(r, "error: layout qualifier 'set' appears more than once\n");
1820 DSLLayout().set(1).set(2);
1821 }
1822
1823 {
1824 ExpectError error(r, "error: layout qualifier 'binding' appears more than once\n");
1825 DSLLayout().binding(1).binding(2);
1826 }
1827
1828 {
1829 ExpectError error(r, "error: layout qualifier 'offset' appears more than once\n");
1830 DSLLayout().offset(1).offset(2);
1831 }
1832
1833 {
1834 ExpectError error(r, "error: layout qualifier 'index' appears more than once\n");
1835 DSLLayout().index(1).index(2);
1836 }
1837
1838 {
1839 ExpectError error(r, "error: layout qualifier 'builtin' appears more than once\n");
1840 DSLLayout().builtin(1).builtin(2);
1841 }
1842
1843 {
1844 ExpectError error(r, "error: layout qualifier 'input_attachment_index' appears more than "
1845 "once\n");
1846 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1847 }
1848
1849 {
1850 ExpectError error(r, "error: layout qualifier 'origin_upper_left' appears more than "
1851 "once\n");
1852 DSLLayout().originUpperLeft().originUpperLeft();
1853 }
1854
1855 {
1856 ExpectError error(r, "error: layout qualifier 'override_coverage' appears more than "
1857 "once\n");
1858 DSLLayout().overrideCoverage().overrideCoverage();
1859 }
1860
1861 {
1862 ExpectError error(r, "error: layout qualifier 'push_constant' appears more than once\n");
1863 DSLLayout().pushConstant().pushConstant();
1864 }
1865
1866 {
1867 ExpectError error(r, "error: layout qualifier 'blend_support_all_equations' appears more "
1868 "than once\n");
1869 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1870 }
1871
1872 {
1873 ExpectError error(r, "error: layout qualifier 'srgb_unpremul' appears more than once\n");
1874 DSLLayout().srgbUnpremul().srgbUnpremul();
1875 }
1876}
1877
Ethan Nicholas624a5292021-04-16 14:54:43 -04001878DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001879 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags,
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001880 SkSL::ProgramKind::kFragmentProcessor);
1881 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1882 EXPECT_EQUAL(Sample(child), "sample(child)");
1883 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1884 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001885 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001886
1887 {
1888 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1889 Sample(child, true).release();
1890 }
1891}
1892
Ethan Nicholas624a5292021-04-16 14:54:43 -04001893DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001894 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kDefaultTestFlags,
Brian Osman552fcb92021-04-28 17:41:57 -04001895 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001896 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001897 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001898
1899 {
Brian Osmanc9125aa2021-04-21 09:57:19 -04001900 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001901 Sample(shader, Half4(1)).release();
1902 }
1903}
1904
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001905DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas22dcb732021-05-12 11:26:58 -04001906 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), kNoDeclareTestFlags);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001907
1908 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001909 Field(kFloat_Type, "x"),
1910 Field(kBool_Type, "b"),
1911 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001912 );
1913 DSLVar result(simpleStruct, "result");
1914 DSLFunction(simpleStruct, "returnStruct").define(
1915 Declare(result),
1916 result.field("x") = 123,
1917 result.field("b") = result.field("x") > 0,
1918 result.field("a")[0] = result.field("x"),
1919 Return(result)
1920 );
1921 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001922 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1923 "struct SimpleStruct { float x; bool b; float[3] a; };");
1924 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1925 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1926 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001927
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001928 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001929 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001930 Field(simpleStruct, "simple")
1931 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001932 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1933 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001934 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001935}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001936
1937DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1938 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1939 std::vector<Wrapper<DSLExpression>> exprs;
1940 exprs.push_back(DSLExpression(1));
1941 exprs.emplace_back(2.0);
1942 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1943 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1944
1945 std::vector<Wrapper<DSLVar>> vars;
1946 vars.emplace_back(DSLVar(kInt_Type, "x"));
1947 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0]).name() == "x");
1948}