blob: dbd55ca1605f1d8e0e6c6018107ef9d361158e15 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ethan Nicholas24c17722021-03-09 13:10:59 -05008#include "include/private/SkSLIRNode.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -05009#include "include/sksl/DSL.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050010#include "src/gpu/GrDirectContextPriv.h"
11#include "src/gpu/GrGpu.h"
12#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050013#include "src/sksl/dsl/priv/DSLWriter.h"
14
15#include "tests/Test.h"
16
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050017#include <limits>
18
Ethan Nicholas95046142021-01-07 10:57:27 -050019using namespace SkSL::dsl;
20
Ethan Nicholas55a63af2021-05-18 10:12:58 -040021SkSL::ProgramSettings default_settings() {
22 SkSL::ProgramSettings result;
23 result.fDSLMarkVarsDeclared = true;
24 result.fDSLMangling = false;
25 return result;
26}
27
28SkSL::ProgramSettings no_mark_vars_declared() {
29 SkSL::ProgramSettings result = default_settings();
30 result.fDSLMarkVarsDeclared = false;
31 return result;
32}
Ethan Nicholas22dcb732021-05-12 11:26:58 -040033
Ethan Nicholas961d9442021-03-16 16:37:29 -040034/**
35 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
36 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
37 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
38 * errors, especially given that some of the variables have options that make them an error to
39 * actually declare.
40 */
Ethan Nicholas95046142021-01-07 10:57:27 -050041class AutoDSLContext {
42public:
Ethan Nicholas55a63af2021-05-18 10:12:58 -040043 AutoDSLContext(GrGpu* gpu, SkSL::ProgramSettings settings = default_settings(),
Ethan Nicholasee49efc2021-04-09 15:33:53 -040044 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -040045 Start(gpu->shaderCompiler(), kind, settings);
Ethan Nicholas95046142021-01-07 10:57:27 -050046 }
47
48 ~AutoDSLContext() {
49 End();
50 }
51};
52
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050053class ExpectError : public ErrorHandler {
54public:
55 ExpectError(skiatest::Reporter* reporter, const char* msg)
56 : fMsg(msg)
57 , fReporter(reporter) {
58 SetErrorHandler(this);
59 }
60
61 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050062 REPORTER_ASSERT(fReporter, !fMsg,
63 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050064 SetErrorHandler(nullptr);
65 }
66
Ethan Nicholasb9563042021-02-25 09:45:49 -050067 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050068 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
69 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
70 fMsg = nullptr;
71 }
72
73private:
74 const char* fMsg;
75 skiatest::Reporter* fReporter;
76};
77
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050078static bool whitespace_insensitive_compare(const char* a, const char* b) {
79 for (;;) {
80 while (isspace(*a)) {
81 ++a;
82 }
83 while (isspace(*b)) {
84 ++b;
85 }
86 if (*a != *b) {
87 return false;
88 }
89 if (*a == 0) {
90 return true;
91 }
92 ++a;
93 ++b;
94 }
95}
96
Ethan Nicholas24c17722021-03-09 13:10:59 -050097// for use from SkSLDSLOnlyTest.cpp
98void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
99 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
100}
101
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500102DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -0500103 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
104 Expression e1 = 1;
105 REPORTER_ASSERT(r, e1.release()->description() == "1");
106 Expression e2 = 1.0;
107 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
108 Expression e3 = true;
109 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400110 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500111 Expression e4 = a;
112 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500113
114 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
115 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
116 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
117 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
118 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
119 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
120 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
121 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500122}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500123
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500124static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
125static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
126static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
127static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
Ethan Nicholas722cb672021-05-06 10:47:06 -0400128static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); }
Ethan Nicholas292a09d2021-07-14 09:52:16 -0400129static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
130static SkSL::String stringize(SkSL::Program& program) { return program.description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500131
132template <typename T>
133static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
134 SkSL::String actual = stringize(input);
135 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
136 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
137 lineNumber, expected, actual.c_str());
138 }
139}
140
141template <typename T>
142static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
143 // This overload allows temporary values to be passed to expect_equal.
144 return expect_equal(r, lineNumber, dsl, expected);
145}
146
147#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
148
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400149DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
150 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400151 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400152 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true");
153
154 Var x(kInt_Type, "x");
155 EXPECT_EQUAL(Declare(x), "int x;");
156 }
157
158 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400159 SkSL::ProgramSettings settings;
160 settings.fOptimize = false;
161 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings,
Brian Osman8c264792021-07-01 16:41:27 -0400162 SkSL::ProgramKind::kFragment);
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400163 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))),
164 "all(greaterThan(float4(1.0), float4(0.0)))");
165 }
166
167 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400168 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), SkSL::ProgramSettings());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400169 Var x(kInt_Type, "x");
170 EXPECT_EQUAL(Declare(x), "int _0_x;");
171 }
172}
173
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500174DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
175 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
176 Expression e1 = Float(std::numeric_limits<float>::max());
177 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
178 std::numeric_limits<float>::max());
179
180 Expression e2 = Float(std::numeric_limits<float>::min());
181 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
182 std::numeric_limits<float>::min());
183
John Stilesb4d7b582021-02-19 09:56:31 -0500184 EXPECT_EQUAL(Float2(0),
185 "float2(0.0)");
186 EXPECT_EQUAL(Float2(-0.5, 1),
187 "float2(-0.5, 1.0)");
188 EXPECT_EQUAL(Float3(0.75),
189 "float3(0.75)");
190 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500191 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500192 EXPECT_EQUAL(Float3(0, 1, 2),
193 "float3(0.0, 1.0, 2.0)");
194 EXPECT_EQUAL(Float4(0),
195 "float4(0.0)");
196 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500197 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500198 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500199 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500200 EXPECT_EQUAL(Float4(0, 1, 2, 3),
201 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500202
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400203 DSLVar x(kFloat_Type, "x");
204 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
205 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
206
207 DSLVar y(kFloat2_Type, "y");
208 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
209 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
210
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500211 {
212 ExpectError error(r, "error: floating point value is infinite\n");
213 Float(std::numeric_limits<float>::infinity()).release();
214 }
215
216 {
217 ExpectError error(r, "error: floating point value is NaN\n");
218 Float(std::numeric_limits<float>::quiet_NaN()).release();
219 }
220
221 {
John Stiles7729e0a2021-07-13 09:18:57 -0400222 ExpectError error(r, "error: 'float4' is not a valid parameter to 'float2' constructor; "
223 "use '.xy' instead\n");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500224 Float2(Float4(1)).release();
225 }
226
227 {
228 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
229 " but found 3)\n");
230 Float4(Float3(1)).release();
231 }
232}
233
234DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
235 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
236 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500237 REPORTER_ASSERT(r,
238 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500239
240 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500241 REPORTER_ASSERT(r,
242 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500243
John Stilesb9e4f642021-03-05 09:11:38 -0500244 EXPECT_EQUAL(Half2(0),
245 "half2(0.0)");
246 EXPECT_EQUAL(Half2(-0.5, 1),
247 "half2(-0.5, 1.0)");
248 EXPECT_EQUAL(Half3(0.75),
249 "half3(0.75)");
250 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
251 "half3(0.0, 1.0, -2.0)");
252 EXPECT_EQUAL(Half3(0, 1, 2),
253 "half3(0.0, 1.0, 2.0)");
254 EXPECT_EQUAL(Half4(0),
255 "half4(0.0)");
256 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
257 "half4(0.0, 1.0, 2.0, 3.0)");
258 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
259 "half4(0.0, 1.0, 2.0, 3.0)");
260 EXPECT_EQUAL(Half4(0, 1, 2, 3),
261 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500262
263 {
264 ExpectError error(r, "error: floating point value is infinite\n");
265 Half(std::numeric_limits<float>::infinity()).release();
266 }
267
268 {
269 ExpectError error(r, "error: floating point value is NaN\n");
270 Half(std::numeric_limits<float>::quiet_NaN()).release();
271 }
272
273 {
John Stiles7729e0a2021-07-13 09:18:57 -0400274 ExpectError error(r, "error: 'half4' is not a valid parameter to 'half2' constructor; use "
275 "'.xy' instead\n");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500276 Half2(Half4(1)).release();
277 }
278
279 {
280 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
281 " but found 3)\n");
282 Half4(Half3(1)).release();
283 }
284}
285
286DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
287 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500288
John Stilesb9e4f642021-03-05 09:11:38 -0500289 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
290 "2147483647");
291 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
292 "int2(-2147483648)");
293 EXPECT_EQUAL(Int2(0, 1),
294 "int2(0, 1)");
295 EXPECT_EQUAL(Int3(0),
296 "int3(0)");
297 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
298 "int3(0, 1, -2)");
299 EXPECT_EQUAL(Int3(0, 1, 2),
300 "int3(0, 1, 2)");
301 EXPECT_EQUAL(Int4(0),
302 "int4(0)");
303 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
304 "int4(0, 1, 2, 3)");
305 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
306 "int4(0, 1, 2, 3)");
307 EXPECT_EQUAL(Int4(0, 1, 2, 3),
308 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500309
310 {
John Stiles7729e0a2021-07-13 09:18:57 -0400311 ExpectError error(r, "error: 'int4' is not a valid parameter to 'int2' constructor; use "
312 "'.xy' instead\n");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500313 Int2(Int4(1)).release();
314 }
315
316 {
317 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
318 " but found 3)\n");
319 Int4(Int3(1)).release();
320 }
321}
322
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400323DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
324 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
325
326 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
327 "4294967295");
328 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
329 "uint2(0)");
330 EXPECT_EQUAL(UInt2(0, 1),
331 "uint2(0, 1)");
332 EXPECT_EQUAL(UInt3(0),
333 "uint3(0)");
334 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
335 "uint3(0, 1, -2)");
336 EXPECT_EQUAL(UInt3(0, 1, 2),
337 "uint3(0, 1, 2)");
338 EXPECT_EQUAL(UInt4(0),
339 "uint4(0)");
340 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
341 "uint4(0, 1, 2, 3)");
342 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
343 "uint4(0, 1, 2, 3)");
344 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
345 "uint4(0, 1, 2, 3)");
346
347 {
John Stiles7729e0a2021-07-13 09:18:57 -0400348 ExpectError error(r, "error: 'uint4' is not a valid parameter to 'uint2' constructor; use "
349 "'.xy' instead\n");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400350 UInt2(UInt4(1)).release();
351 }
352
353 {
354 ExpectError error(r, "error: invalid arguments to 'uint4' constructor (expected 4 scalars,"
355 " but found 3)\n");
356 UInt4(UInt3(1)).release();
357 }
358}
359
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500360DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
361 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500362
John Stilesb9e4f642021-03-05 09:11:38 -0500363 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
364 "32767");
365 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
366 "short2(-32768)");
367 EXPECT_EQUAL(Short2(0, 1),
368 "short2(0, 1)");
369 EXPECT_EQUAL(Short3(0),
370 "short3(0)");
371 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
372 "short3(0, 1, -2)");
373 EXPECT_EQUAL(Short3(0, 1, 2),
374 "short3(0, 1, 2)");
375 EXPECT_EQUAL(Short4(0),
376 "short4(0)");
377 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
378 "short4(0, 1, 2, 3)");
379 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
380 "short4(0, 1, 2, 3)");
381 EXPECT_EQUAL(Short4(0, 1, 2, 3),
382 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500383
384 {
John Stiles7729e0a2021-07-13 09:18:57 -0400385 ExpectError error(r, "error: 'short4' is not a valid parameter to 'short2' constructor; "
386 "use '.xy' instead\n");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500387 Short2(Short4(1)).release();
388 }
389
390 {
391 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
392 " but found 3)\n");
393 Short4(Short3(1)).release();
394 }
395}
396
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400397DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
398 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
399
400 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
401 "65535");
402 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
403 "ushort2(0)");
404 EXPECT_EQUAL(UShort2(0, 1),
405 "ushort2(0, 1)");
406 EXPECT_EQUAL(UShort3(0),
407 "ushort3(0)");
408 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
409 "ushort3(0, 1, -2)");
410 EXPECT_EQUAL(UShort3(0, 1, 2),
411 "ushort3(0, 1, 2)");
412 EXPECT_EQUAL(UShort4(0),
413 "ushort4(0)");
414 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
415 "ushort4(0, 1, 2, 3)");
416 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
417 "ushort4(0, 1, 2, 3)");
418 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
419 "ushort4(0, 1, 2, 3)");
420
421 {
John Stiles7729e0a2021-07-13 09:18:57 -0400422 ExpectError error(r, "error: 'ushort4' is not a valid parameter to 'ushort2' constructor; "
423 "use '.xy' instead\n");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400424 UShort2(UShort4(1)).release();
425 }
426
427 {
428 ExpectError error(r, "error: invalid arguments to 'ushort4' constructor (expected 4 "
429 "scalars, but found 3)\n");
430 UShort4(UShort3(1)).release();
431 }
432}
433
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500434DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
435 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500436
John Stilesb9e4f642021-03-05 09:11:38 -0500437 EXPECT_EQUAL(Bool2(false),
438 "bool2(false)");
439 EXPECT_EQUAL(Bool2(false, true),
440 "bool2(false, true)");
441 EXPECT_EQUAL(Bool3(false),
442 "bool3(false)");
443 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
444 "bool3(false, true, false)");
445 EXPECT_EQUAL(Bool3(false, true, false),
446 "bool3(false, true, false)");
447 EXPECT_EQUAL(Bool4(false),
448 "bool4(false)");
449 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
450 "bool4(false, true, false, true)");
451 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
452 "bool4(false, true, false, true)");
453 EXPECT_EQUAL(Bool4(false, true, false, true),
454 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500455
456 {
John Stiles7729e0a2021-07-13 09:18:57 -0400457 ExpectError error(r, "error: 'bool4' is not a valid parameter to 'bool2' constructor; use "
458 "'.xy' instead\n");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500459 Bool2(Bool4(true)).release();
460 }
461
462 {
463 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
464 " but found 3)\n");
465 Bool4(Bool3(true)).release();
466 }
467}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500468
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400469DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
470 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
471 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
472 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
473 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
474 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
475 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
476 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
477 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
478 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
479 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
480 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
481 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
482
483 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
484 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
485 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
486 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
487 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
488 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
489 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
490 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
491 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
492 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
493 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
494
495 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
496 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
497 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
498 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
499 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
500 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
501 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
502 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
503 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
504 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
505 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
506
507 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
508 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
509 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
510 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
511 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
512 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
513 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
514 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
515 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
516 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
517 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
518
519 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
520 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
521 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
522 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
523 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
524 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
525 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
526 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
527 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
528 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
529 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
530
531 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
532 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
533 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
534 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
535 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
536 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
537 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
538 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
539 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
540 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
541 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
542
543 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
544 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
545 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
546 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
547 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
548 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
549 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
550 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
551 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
552 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
553 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400554
555 Var x(kFloat_Type);
556 DSLExpression e = x + 1;
557 REPORTER_ASSERT(r, e.type().isFloat());
558 e.release();
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400559}
560
Ethan Nicholas84558932021-04-12 16:56:37 -0400561DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
562 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
563 Var f22(kFloat2x2_Type, "f22");
564 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
565 Var f32(kFloat3x2_Type, "f32");
566 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
567 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
568 Var f42(kFloat4x2_Type, "f42");
569 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
570 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
571 Var f23(kFloat2x3_Type, "f23");
572 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
573 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
574 Var f33(kFloat3x3_Type, "f33");
575 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
576 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
577 Var f43(kFloat4x3_Type, "f43");
578 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
579 "(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))");
580 Var f24(kFloat2x4_Type, "f24");
581 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
582 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
583 Var f34(kFloat3x4_Type, "f34");
584 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
585 "(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))");
586 Var f44(kFloat4x4_Type, "f44");
587 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
588
589 Var h22(kHalf2x2_Type, "h22");
590 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
591 Var h32(kHalf3x2_Type, "h32");
592 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
593 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
594 Var h42(kHalf4x2_Type, "h42");
595 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
596 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
597 Var h23(kHalf2x3_Type, "h23");
598 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
599 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
600 Var h33(kHalf3x3_Type, "h33");
601 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
602 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
603 Var h43(kHalf4x3_Type, "h43");
604 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
605 "(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))");
606 Var h24(kHalf2x4_Type, "h24");
607 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
608 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
609 Var h34(kHalf3x4_Type, "h34");
610 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
611 "(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))");
612 Var h44(kHalf4x4_Type, "h44");
613 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
614
615 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
616 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
617 EXPECT_EQUAL(h42[0][1], "h42[0].y");
618 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
619 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
620 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
621
622 {
623 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
624 "scalars, but found 2)\n");
625 DSLExpression(Float3x3(Float2(1))).release();
626 }
627
628 {
629 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
630 "scalars, but found 5)\n");
631 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
632 }
633
634 {
635 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
636 DSLExpression(f43 * Float3(1)).release();
637 }
638
639 {
640 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
641 "'float3x3'\n");
642 DSLExpression(f43 = f33).release();
643 }
644
645 {
646 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
647 "'float2x2'\n");
648 DSLExpression(h22 = f22).release();
649 }
650
651 {
652 ExpectError error(r,
653 "error: no match for inverse(float4x3)\n");
654 DSLExpression(Inverse(f43)).release();
655 }
656}
657
Ethan Nicholas92969f22021-01-13 10:38:59 -0500658DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
659 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400660 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500661
John Stiles8f440b42021-03-05 16:48:56 -0500662 EXPECT_EQUAL(a + b,
663 "(a + b)");
664 EXPECT_EQUAL(a + 1,
665 "(a + 1.0)");
666 EXPECT_EQUAL(0.5 + a + -99,
667 "((0.5 + a) + -99.0)");
668 EXPECT_EQUAL(a += b + 1,
669 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500670 EXPECT_EQUAL(+a,
671 "a");
672 EXPECT_EQUAL(+(a + b),
673 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674
675 {
676 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500677 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500678 }
679
680 {
681 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500682 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500683 }
684
685 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500686 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500687 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500688 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500689
690 {
691 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400692 Var c(kBool_Type);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400693 DSLExpression(+c).release();
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500694 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500695}
696
697DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
698 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400699 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500700
John Stiles8f440b42021-03-05 16:48:56 -0500701 EXPECT_EQUAL(a - b,
702 "(a - b)");
703 EXPECT_EQUAL(a - 1,
704 "(a - 1)");
705 EXPECT_EQUAL(2 - a - b,
706 "((2 - a) - b)");
707 EXPECT_EQUAL(a -= b + 1,
708 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500709 EXPECT_EQUAL(-a,
710 "-a");
711 EXPECT_EQUAL(-(a - b),
712 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500713
714 {
715 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500716 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500717 }
718
719 {
720 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500721 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500722 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500723
724 {
725 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500726 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500727 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500728
729 {
730 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400731 Var c(kBool_Type);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400732 DSLExpression(-c).release();
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500733 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500734}
735
736DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
737 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400738 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500739
John Stiles8f440b42021-03-05 16:48:56 -0500740 EXPECT_EQUAL(a * b,
741 "(a * b)");
742 EXPECT_EQUAL(a * 2,
743 "(a * 2.0)");
744 EXPECT_EQUAL(0.5 * a * -99,
745 "((0.5 * a) * -99.0)");
746 EXPECT_EQUAL(a *= b + 1,
747 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500748
749 {
750 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500751 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500752 }
753
754 {
755 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500756 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500757 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500758
759 {
760 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500761 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500762 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500763}
764
765DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
766 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400767 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500768
John Stiles8f440b42021-03-05 16:48:56 -0500769 EXPECT_EQUAL(a / b,
770 "(a / b)");
771 EXPECT_EQUAL(a / 2,
772 "(a / 2.0)");
773 EXPECT_EQUAL(0.5 / a / -99,
774 "((0.5 / a) / -99.0)");
775 EXPECT_EQUAL(b / (a - 1),
776 "(b / (a - 1.0))");
777 EXPECT_EQUAL(a /= b + 1,
778 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500779
780 {
781 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500782 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500783 }
784
785 {
786 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500787 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500788 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500789
790 {
791 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500792 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500793 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500794
795 {
796 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500797 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500798 }
799
800 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400801 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500802 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500803 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500804 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500805}
806
807DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
808 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400809 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500810 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500811 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500812
813 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500814 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500815
816 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500817 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500818
819 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500820 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500821
822 {
823 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500824 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500825 }
826
827 {
828 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500829 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500830 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500831
832 {
833 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500834 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500835 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500836
837 {
838 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500839 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500840 }
841
842 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400843 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500844 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500845 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500846 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500847}
848
849DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
850 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400851 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500852 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500853 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500854
855 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500856 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500857
858 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500859 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500860
861 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500862 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500863
864 {
865 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500866 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500867 }
868
869 {
870 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500871 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500872 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500873
874 {
875 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500876 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500877 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500878}
879
880DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
881 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400882 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500883 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500884 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500885
886 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500887 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500888
889 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500890 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500891
892 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500893 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500894
895 {
896 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500897 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500898 }
899
900 {
901 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500902 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500903 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500904
905 {
906 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500907 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500908 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500909}
910
911DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
912 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400913 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500914 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500915 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500916
917 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500918 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500919
920 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500921 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500922
923 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500924 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500925
926 {
927 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500928 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500929 }
930
931 {
932 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500933 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500934 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500935
936 {
937 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500938 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500939 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500940}
941
942DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
943 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400944 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500945 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500946 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500947
948 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500949 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500950
951 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500952 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500953
954 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500955 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500956
957 {
958 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500959 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500960 }
961
962 {
963 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500964 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500965 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500966
967 {
968 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500969 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500970 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500971}
972
973DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
974 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400975 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500976 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500977 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500978
979 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500980 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500981
982 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500983 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500984
985 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500986 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500987
988 {
989 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500990 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500991 }
992
993 {
994 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500995 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500996 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500997
998 {
999 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001000 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -05001001 }
Ethan Nicholas92969f22021-01-13 10:38:59 -05001002}
1003
1004DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
1005 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001006 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001007 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001008 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001009
1010 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001011 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001012
1013 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001014 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001015
1016 {
1017 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001018 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001019 }
1020}
1021
1022DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
1023 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001024 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001025 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001026 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001027
1028 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001029 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001030
1031 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001032 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001033
1034 {
1035 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001036 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001037 }
1038}
1039
Ethan Nicholas2ab47c92021-07-19 12:30:37 -04001040DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalXor, r, ctxInfo) {
1041 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1042 Var a(kBool_Type, "a"), b(kBool_Type, "b");
1043 Expression e1 = LogicalXor(a, b);
1044 EXPECT_EQUAL(e1, "(a ^^ b)");
1045
1046 {
1047 ExpectError error(r, "error: type mismatch: '^^' cannot operate on 'bool', 'int'\n");
1048 DSLExpression(LogicalXor(a, 5)).release();
1049 }
1050}
1051
Ethan Nicholas92969f22021-01-13 10:38:59 -05001052DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1053 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001054 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001055 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001056 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001057
1058 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001059 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001060}
1061
1062DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1063 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001064 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001065 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001066 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001067
1068 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001069 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001070
1071 {
1072 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001073 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001074 }
1075}
1076
1077DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1078 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001079 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001080 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001081 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001082
1083 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001084 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001085
1086 {
1087 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001088 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001089 }
1090}
1091
1092DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1093 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001094 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001095 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001096 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001097
1098 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001099 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001100
1101 {
1102 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001103 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001104 }
1105}
1106
1107DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1108 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001109 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001110 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001111 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001112
1113 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001114 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001115
1116 {
1117 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001118 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001119 }
1120}
1121
1122DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1123 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001124 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001125 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001126 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001127
1128 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001129 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001130
1131 {
1132 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001133 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001134 }
1135}
1136
1137DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1138 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001139 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001140 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001141 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001142
1143 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001144 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001145
1146 {
1147 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001148 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001149 }
1150}
1151
1152DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1153 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001154 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001155 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001156 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001157
1158 {
1159 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001160 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001161 }
1162}
1163
1164DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1165 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001166 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001167 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001168 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001169
1170 {
1171 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001172 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001173 }
1174}
1175
1176DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1177 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001178 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001179 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001180 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001181
1182 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001183 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001184
1185 {
1186 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001187 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001188 }
1189
1190 {
1191 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001192 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001193 }
1194
1195 {
1196 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001197 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001198 }
1199
1200 {
1201 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001202 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001203 }
1204}
1205
1206DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1207 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001208 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001209 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001210 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001211
1212 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001213 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001214
1215 {
1216 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001217 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001218 }
1219
1220 {
1221 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001222 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001223 }
1224
1225 {
1226 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001227 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001228 }
1229
1230 {
1231 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001232 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001233 }
1234}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001235
Ethan Nicholas722cb672021-05-06 10:47:06 -04001236DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1237 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1238 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001239 DSLExpression sqrt(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001240 SkTArray<DSLWrapper<DSLExpression>> args;
John Stiles443fa192021-05-24 23:46:46 -04001241 args.emplace_back(16);
1242 EXPECT_EQUAL(sqrt(std::move(args)), "4.0"); // sqrt(16) gets optimized to 4
Ethan Nicholas722cb672021-05-06 10:47:06 -04001243 }
1244
1245 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001246 DSLExpression pow(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001247 DSLVar a(kFloat_Type, "a");
1248 DSLVar b(kFloat_Type, "b");
1249 SkTArray<DSLWrapper<DSLExpression>> args;
1250 args.emplace_back(a);
1251 args.emplace_back(b);
1252 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1253 }
1254}
1255
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001256DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001257 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas722cb672021-05-06 10:47:06 -04001258 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001259 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001260 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001261
Ethan Nicholas722cb672021-05-06 10:47:06 -04001262 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1263
1264 SkTArray<DSLStatement> statements;
1265 statements.push_back(a = 0);
1266 statements.push_back(++a);
1267 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001268}
1269
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001270DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001271 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001272 Var i(kInt_Type, "i", 0);
1273 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001274 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001275 If(i > 5, Break())
1276 ))
1277 );
1278 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001279 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1280 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001281
1282 {
1283 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001284 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001285 Break()
1286 );
1287 }
1288}
1289
1290DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001291 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001292 Var i(kInt_Type, "i", 0);
1293 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001294 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001295 If(i < 5, Continue())
1296 ))
1297 );
1298 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001299 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1300 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001301
1302 {
1303 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001304 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001305 Continue()
1306 );
1307 }
1308}
1309
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001310DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001311 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001312 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001313 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001314 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001315 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001316 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001317
1318 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001319 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001320 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001321 Declare(c).release();
1322 }
1323
1324 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001325 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001326 Declare(d).release();
1327 ExpectError error(r, "error: variable has already been declared\n");
1328 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001329 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001330
1331 {
1332 Var e(kUniform_Modifier, kInt_Type, "e");
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001333 ExpectError error(r, "error: 'uniform' is not permitted here\n");
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001334 Declare(e).release();
1335 }
1336}
1337
1338DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001339 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001340 DSLGlobalVar x(kInt_Type, "x", 0);
1341 Declare(x);
1342 DSLGlobalVar y(kUniform_Modifier, kFloat2_Type, "y");
1343 Declare(y);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001344 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1345 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1346 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001347}
1348
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001349DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1350 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
John Stiles443fa192021-05-24 23:46:46 -04001351 Var x(kFloat_Type, "x", 1);
1352 EXPECT_EQUAL(If(Sqrt(x) > 0, Discard()), "if ((sqrt(x) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001353}
1354
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001355DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1356 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1357 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001358 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001359
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001360 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001361 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001362 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001363
1364 {
1365 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1366 Do(Block(), 7).release();
1367 }
1368}
1369
1370DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001371 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles8676ebe2021-04-20 15:30:41 -04001372 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1373 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001374
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001375 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001376 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1377 "for (int i = 0; (i < 10); ++i) (i += 5);");
1378
1379 Var j(kInt_Type, "j", 0);
1380 Var k(kInt_Type, "k", 10);
1381 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1382 {
1383 int j = 0;
1384 int k = 10;
1385 for (; (j < k); ++j) {}
1386 }
1387 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001388
1389 {
1390 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1391 For(i = 0, i + 10, ++i, i += 5).release();
1392 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001393
1394 {
1395 ExpectError error(r, "error: invalid for loop initializer\n");
1396 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1397 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001398}
1399
Ethan Nicholase2c05042021-02-03 10:27:22 -05001400DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001401 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001402 Parameter coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001403 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001404 sk_FragColor() = Half4(coords, 0, 1)
1405 );
1406 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001407 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001408 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001409
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001410 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001411 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001412 DSLParameter x(kFloat_Type, "x");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001413 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001414 sqr.define(
1415 Return(x * x)
1416 );
1417 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1418 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1419 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1420 }
1421
1422 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001423 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001424 DSLParameter x(kFloat2_Type, "x");
1425 DSLParameter y(kFloat2_Type, "y");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001426 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001427 dot.define(
1428 Return(x * x + y * y)
1429 );
1430 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1431 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1432 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1433 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1434 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1435 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001436
1437 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001438 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001439 DSLParameter x(kFloat_Type, "x");
1440 DSLParameter y(kFloat_Type, "y");
Ethan Nicholas80f62352021-04-09 12:25:03 -04001441 DSLFunction pair(kFloat2_Type, "pair", x, y);
1442 pair.define(
1443 Return(Float2(x, y))
1444 );
1445 Var varArg1(kFloat_Type, "varArg1");
1446 Var varArg2(kFloat_Type, "varArg2");
1447 DSLWriter::MarkDeclared(varArg1);
1448 DSLWriter::MarkDeclared(varArg2);
1449 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1450 }
1451
1452 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001453 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001454 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001455 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001456 Return(true)
1457 );
1458 }
1459
1460 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001461 ExpectError error(r, "error: expected function to return 'float'\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 Return()
1465 );
1466 }
1467
1468 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001469 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001470 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001471 Var x(kFloat_Type, "x", 0);
1472 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001473 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001474 If(x == 1, Return(x))
1475 );
1476 }
1477
1478 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001479 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001480 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001481 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001482 Return(0)
1483 );
1484 }
1485
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001486 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001487 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001488 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001489 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001490 );
1491 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001492
1493 {
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001494 ExpectError error(r, "error: parameter has already been used in another function\n");
Ethan Nicholas961d9442021-03-16 16:37:29 -04001495 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001496 DSLParameter p(kFloat_Type);
1497 DSLFunction(kVoid_Type, "ok", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001498 );
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001499 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001500 );
1501 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001502}
1503
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001504DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1505 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001506 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001507 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001508 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001509
1510 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001511 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001512
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001513 Statement z = StaticIf(a > b, a -= b, b -= a);
1514 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1515
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001516 {
1517 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1518 If(a + b, a -= b).release();
1519 }
1520}
1521
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001522DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInterfaceBlock, r, ctxInfo) {
1523 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001524 DSLGlobalVar intf = InterfaceBlock(kUniform_Modifier, "InterfaceBlock1",
1525 { Field(kFloat_Type, "a"), Field(kInt_Type, "b") });
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001526 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1527 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1528 "uniform InterfaceBlock1 { float a; int b; };");
1529 EXPECT_EQUAL(intf.field("a"), "InterfaceBlock1.a");
1530
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001531 DSLGlobalVar intf2 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock2",
1532 { Field(kFloat2_Type, "x"), Field(kHalf2x2_Type, "y") },
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001533 "blockVar");
1534 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1535 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1536 "uniform InterfaceBlock2 { float2 x; half2x2 y; } blockVar;");
1537 EXPECT_EQUAL(intf2.field("x"), "blockVar.x");
1538
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001539 DSLGlobalVar intf3 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock3",
1540 { Field(kFloat_Type, "z") },"arrayVar", 4);
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001541 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1542 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1543 "uniform InterfaceBlock3 { float z; } arrayVar[4];");
1544 EXPECT_EQUAL(intf3[1].field("z"), "arrayVar[1].z");
1545}
1546
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001547DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1548 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1549
1550 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001551 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001552
1553 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001554 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001555}
1556
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001557DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1558 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001559 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001560 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001561 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001562
1563 {
1564 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001565 Select(a, 1, -1).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001566 }
1567
1568 {
1569 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001570 Select(a > 0, Float2(1), Float3(1)).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001571 }
1572}
1573
Ethan Nicholascfefec02021-02-09 15:22:57 -05001574DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1575 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1576
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001577 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001578
Ethan Nicholas722cb672021-05-06 10:47:06 -04001579 SkTArray<DSLStatement> caseStatements;
1580 caseStatements.push_back(a = 1);
1581 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001582 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001583 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001584 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001585 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001586 Default(Discard())
1587 );
John Stilese1d1b082021-02-23 13:44:36 -05001588 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001589 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001590 case 0: (a = 0.0); break;
1591 case 1: (a = 1.0); continue;
1592 case 2: (a = 2.0);
1593 default: discard;
1594 }
1595 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001596
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001597 Statement y = StaticSwitch(b,
1598 Case(0, a = 0, Break()),
1599 Case(1, a = 1, Continue()),
1600 Case(2, a = 2 /*Fallthrough*/),
1601 Default(Discard())
1602 );
1603 EXPECT_EQUAL(y, R"(
1604 @switch (b) {
1605 case 0: (a = 0.0); break;
1606 case 1: (a = 1.0); continue;
1607 case 2: (a = 2.0);
1608 default: discard;
1609 }
1610 )");
1611
John Stiles642cde22021-02-23 14:57:01 -05001612 EXPECT_EQUAL(Switch(b),
1613 "switch (b) {}");
1614
1615 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1616 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001617
1618 {
John Stilese1d1b082021-02-23 13:44:36 -05001619 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001620 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001621 }
1622
1623 {
John Stilese1d1b082021-02-23 13:44:36 -05001624 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001625 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001626 }
1627
1628 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001629 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001630 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001631 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001632 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001633}
1634
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001635DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001636 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001637 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001638
John Stilesf04e09c2021-03-05 13:13:14 -05001639 EXPECT_EQUAL(a.x(),
1640 "a.x");
1641 EXPECT_EQUAL(a.y(),
1642 "a.y");
1643 EXPECT_EQUAL(a.z(),
1644 "a.z");
1645 EXPECT_EQUAL(a.w(),
1646 "a.w");
1647 EXPECT_EQUAL(a.r(),
1648 "a.x");
1649 EXPECT_EQUAL(a.g(),
1650 "a.y");
1651 EXPECT_EQUAL(a.b(),
1652 "a.z");
1653 EXPECT_EQUAL(a.a(),
1654 "a.w");
1655 EXPECT_EQUAL(Swizzle(a, R),
1656 "a.x");
1657 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1658 "float2(0.0, a.y)");
1659 EXPECT_EQUAL(Swizzle(a, B, G, G),
1660 "a.zyy");
1661 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1662 "float4(a.xyz, 1.0)");
1663 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1664 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001665}
1666
John Stiles08771b02021-04-26 09:35:10 -04001667
1668DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001669 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles08771b02021-04-26 09:35:10 -04001670
1671 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1672 Var a;
1673 if (true)
1674 {
1675 Var(kInt_Type, "a").swap(a);
1676 }
1677
1678 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1679 "{ int a; (a = 123); }");
1680}
1681
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001682DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1683 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1684 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001685 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001686
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001687 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001688 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001689 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001690
1691 {
1692 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001693 While(7, Block()).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001694 }
1695}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001696
1697DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1698 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001699 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001700
1701 EXPECT_EQUAL(a[0], "a[0]");
1702 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001703
1704 {
1705 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001706 a[true].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001707 }
1708
1709 {
1710 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001711 b[0].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001712 }
1713
1714 {
1715 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001716 a[-1].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001717 }
1718}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001719
1720DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1721 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1722 // There is a Fract type on Mac which can conflict with our Fract builtin
1723 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001724 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1725 Var h3(kHalf3_Type, "h3");
1726 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001727 EXPECT_EQUAL(Abs(a), "abs(a)");
1728 EXPECT_EQUAL(All(b4), "all(b4)");
1729 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001730 EXPECT_EQUAL(Atan(a), "atan(a)");
1731 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001732 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1733 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1734 EXPECT_EQUAL(Cos(a), "cos(a)");
1735 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1736 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1737 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1738 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1739 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1740 EXPECT_EQUAL(Exp(a), "exp(a)");
1741 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1742 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1743 EXPECT_EQUAL(Floor(a), "floor(a)");
1744 EXPECT_EQUAL(Fract(a), "fract(a)");
1745 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1746 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1747 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1748 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1749 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1750 EXPECT_EQUAL(Length(a), "length(a)");
1751 EXPECT_EQUAL(Log(a), "log(a)");
1752 EXPECT_EQUAL(Log2(a), "log2(a)");
1753 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1754 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1755 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1756 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1757 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1758 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1759 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1760 EXPECT_EQUAL(Radians(a), "radians(a)");
1761 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1762 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1763 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1764 EXPECT_EQUAL(Sign(a), "sign(a)");
1765 EXPECT_EQUAL(Sin(a), "sin(a)");
1766 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1767 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1768 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1769 EXPECT_EQUAL(Tan(a), "tan(a)");
1770 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001771
1772 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1773 // one of them reports errors correctly
1774 {
1775 ExpectError error(r, "error: no match for ceil(bool)\n");
1776 Ceil(a == b).release();
1777 }
1778}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001779
1780DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001781 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001782
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001783 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001784 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001785 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001786
1787 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1788 // context, so we can't as yet Declare() variables with these modifiers.
1789 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001790 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001791 REPORTER_ASSERT(r, v2.modifiers().flags() == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001792 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001793
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001794 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001795 REPORTER_ASSERT(r, v3.modifiers().flags() == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001796 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001797
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001798 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001799 REPORTER_ASSERT(r, v4.modifiers().flags() == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001800 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001801
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001802 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001803 REPORTER_ASSERT(r, v5.modifiers().flags() == SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001804 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001805
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001806 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001807 REPORTER_ASSERT(r, v6.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1808 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001809 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001810
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001811 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001812 REPORTER_ASSERT(r, v7.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1813 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001814 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001815
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001816 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001817 REPORTER_ASSERT(r, v8.modifiers().flags() == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001818 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001819 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001820}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001821
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001822DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001823 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001824 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1825 .inputAttachmentIndex(7),
1826 kConst_Modifier), kInt_Type, "v1", 0);
1827 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1828 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1829
1830 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1831 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1832
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001833 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1834 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1835
1836 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1837 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1838
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001839 {
1840 ExpectError error(r, "error: 'srgb_unpremul' is only permitted in runtime effects\n");
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001841 DSLGlobalVar v(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kHalf4_Type,
1842 "v");
1843 Declare(v);
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001844 }
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001845
1846 {
1847 ExpectError error(r, "error: layout qualifier 'location' appears more than once\n");
1848 DSLLayout().location(1).location(2);
1849 }
1850
1851 {
1852 ExpectError error(r, "error: layout qualifier 'set' appears more than once\n");
1853 DSLLayout().set(1).set(2);
1854 }
1855
1856 {
1857 ExpectError error(r, "error: layout qualifier 'binding' appears more than once\n");
1858 DSLLayout().binding(1).binding(2);
1859 }
1860
1861 {
1862 ExpectError error(r, "error: layout qualifier 'offset' appears more than once\n");
1863 DSLLayout().offset(1).offset(2);
1864 }
1865
1866 {
1867 ExpectError error(r, "error: layout qualifier 'index' appears more than once\n");
1868 DSLLayout().index(1).index(2);
1869 }
1870
1871 {
1872 ExpectError error(r, "error: layout qualifier 'builtin' appears more than once\n");
1873 DSLLayout().builtin(1).builtin(2);
1874 }
1875
1876 {
1877 ExpectError error(r, "error: layout qualifier 'input_attachment_index' appears more than "
1878 "once\n");
1879 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1880 }
1881
1882 {
1883 ExpectError error(r, "error: layout qualifier 'origin_upper_left' appears more than "
1884 "once\n");
1885 DSLLayout().originUpperLeft().originUpperLeft();
1886 }
1887
1888 {
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001889 ExpectError error(r, "error: layout qualifier 'push_constant' appears more than once\n");
1890 DSLLayout().pushConstant().pushConstant();
1891 }
1892
1893 {
1894 ExpectError error(r, "error: layout qualifier 'blend_support_all_equations' appears more "
1895 "than once\n");
1896 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1897 }
1898
1899 {
1900 ExpectError error(r, "error: layout qualifier 'srgb_unpremul' appears more than once\n");
1901 DSLLayout().srgbUnpremul().srgbUnpremul();
1902 }
1903}
1904
Ethan Nicholas624a5292021-04-16 14:54:43 -04001905DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001906 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(),
Brian Osman552fcb92021-04-28 17:41:57 -04001907 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001908 DSLGlobalVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001909 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001910
1911 {
Brian Osmanc9125aa2021-04-21 09:57:19 -04001912 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001913 Sample(shader, Half4(1)).release();
1914 }
1915}
1916
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001917DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001918 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001919
1920 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001921 Field(kFloat_Type, "x"),
1922 Field(kBool_Type, "b"),
1923 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001924 );
1925 DSLVar result(simpleStruct, "result");
1926 DSLFunction(simpleStruct, "returnStruct").define(
1927 Declare(result),
1928 result.field("x") = 123,
1929 result.field("b") = result.field("x") > 0,
1930 result.field("a")[0] = result.field("x"),
1931 Return(result)
1932 );
1933 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001934 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1935 "struct SimpleStruct { float x; bool b; float[3] a; };");
1936 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1937 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1938 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001939
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001940 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001941 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001942 Field(simpleStruct, "simple")
1943 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001944 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1945 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001946 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001947}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001948
1949DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1950 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1951 std::vector<Wrapper<DSLExpression>> exprs;
1952 exprs.push_back(DSLExpression(1));
1953 exprs.emplace_back(2.0);
1954 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1955 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1956
1957 std::vector<Wrapper<DSLVar>> vars;
1958 vars.emplace_back(DSLVar(kInt_Type, "x"));
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001959 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0])->name() == "x");
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001960}
Ethan Nicholasebc9fad2021-07-09 15:35:23 -04001961
1962DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLRTAdjust, r, ctxInfo) {
1963 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
1964 SkSL::ProgramKind::kVertex);
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001965 DSLGlobalVar rtAdjust(kUniform_Modifier, kFloat4_Type, "sk_RTAdjust");
1966 Declare(rtAdjust);
Ethan Nicholasebc9fad2021-07-09 15:35:23 -04001967 DSLFunction(kVoid_Type, "main").define(
1968 sk_Position() = Half4(0)
1969 );
1970 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1971 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1972 "void main() {"
1973 "(sk_PerVertex.sk_Position = float4(0.0));"
1974 "(sk_PerVertex.sk_Position = float4(((sk_PerVertex.sk_Position.xy * sk_RTAdjust.xz) + "
1975 "(sk_PerVertex.sk_Position.ww * sk_RTAdjust.yw)), 0.0, sk_PerVertex.sk_Position.w));"
1976 "}");
1977}
Ethan Nicholas292a09d2021-07-14 09:52:16 -04001978
1979DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInlining, r, ctxInfo) {
1980 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001981 DSLParameter x(kFloat_Type, "x");
Ethan Nicholas292a09d2021-07-14 09:52:16 -04001982 DSLFunction sqr(kFloat_Type, "sqr", x);
1983 sqr.define(
1984 Return(x * x)
1985 );
1986 DSLFunction(kVoid_Type, "main").define(
1987 sk_FragColor() = (sqr(2), Half4(sqr(3)))
1988 );
Ethan Nicholasb18c1e22021-07-16 09:53:53 -04001989 const char* source = "source test";
1990 std::unique_ptr<SkSL::Program> program = ReleaseProgram(std::make_unique<SkSL::String>(source));
Ethan Nicholas292a09d2021-07-14 09:52:16 -04001991 EXPECT_EQUAL(*program,
1992 "layout(location = 0, index = 0, builtin = 10001) out half4 sk_FragColor;"
1993 "layout(builtin = 17)in bool sk_Clockwise;"
1994 "void main() {"
1995 "/* inlined: sqr */;"
1996 "/* inlined: sqr */;"
1997 "(sk_FragColor = (4.0 , half4(half(9.0))));"
1998 "}");
Ethan Nicholasb18c1e22021-07-16 09:53:53 -04001999 REPORTER_ASSERT(r, *program->fSource == source);
Ethan Nicholas292a09d2021-07-14 09:52:16 -04002000}
Ethan Nicholas459777a2021-07-16 11:16:27 -04002001
2002DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReleaseUnused, r, ctxInfo) {
2003 SkSL::ProgramSettings settings = default_settings();
2004 settings.fAssertDSLObjectsReleased = false;
2005 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings);
2006 If(Sqrt(1) > 0, Discard());
2007 // Ensure that we can safely destroy statements and expressions despite being unused while
2008 // settings.fAssertDSLObjectsReleased is disabled.
2009}