blob: 8234f9bd421c9969edc60c935a214f80e31f7c6a [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
1040DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1041 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001042 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001043 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001044 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001045
1046 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001047 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001048}
1049
1050DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1051 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001052 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001053 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001054 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001055
1056 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001057 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001058
1059 {
1060 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001061 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001062 }
1063}
1064
1065DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1066 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001067 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001068 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001069 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001070
1071 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001072 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001073
1074 {
1075 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001076 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001077 }
1078}
1079
1080DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1081 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001082 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001083 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001084 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001085
1086 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001087 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001088
1089 {
1090 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001091 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001092 }
1093}
1094
1095DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1096 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001097 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001098 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001099 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001100
1101 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001102 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001103
1104 {
1105 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001106 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001107 }
1108}
1109
1110DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1111 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001112 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001113 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001114 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001115
1116 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001117 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001118
1119 {
1120 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001121 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001122 }
1123}
1124
1125DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1126 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001127 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001128 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001129 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001130
1131 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001132 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001133
1134 {
1135 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001136 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001137 }
1138}
1139
1140DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1141 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001142 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001143 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001144 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001145
1146 {
1147 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001148 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001149 }
1150}
1151
1152DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1153 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001154 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001155 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001156 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001157
1158 {
1159 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001160 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001161 }
1162}
1163
1164DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, 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 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001171 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001172
1173 {
1174 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001175 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001176 }
1177
1178 {
1179 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001180 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001181 }
1182
1183 {
1184 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001185 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001186 }
1187
1188 {
1189 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001190 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001191 }
1192}
1193
1194DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1195 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001196 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001197 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001198 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001199
1200 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001201 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001202
1203 {
1204 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001205 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001206 }
1207
1208 {
1209 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001210 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001211 }
1212
1213 {
1214 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001215 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001216 }
1217
1218 {
1219 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001220 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001221 }
1222}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001223
Ethan Nicholas722cb672021-05-06 10:47:06 -04001224DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1225 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1226 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001227 DSLExpression sqrt(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001228 SkTArray<DSLWrapper<DSLExpression>> args;
John Stiles443fa192021-05-24 23:46:46 -04001229 args.emplace_back(16);
1230 EXPECT_EQUAL(sqrt(std::move(args)), "4.0"); // sqrt(16) gets optimized to 4
Ethan Nicholas722cb672021-05-06 10:47:06 -04001231 }
1232
1233 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001234 DSLExpression pow(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001235 DSLVar a(kFloat_Type, "a");
1236 DSLVar b(kFloat_Type, "b");
1237 SkTArray<DSLWrapper<DSLExpression>> args;
1238 args.emplace_back(a);
1239 args.emplace_back(b);
1240 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1241 }
1242}
1243
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001244DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001245 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas722cb672021-05-06 10:47:06 -04001246 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001247 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001248 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001249
Ethan Nicholas722cb672021-05-06 10:47:06 -04001250 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1251
1252 SkTArray<DSLStatement> statements;
1253 statements.push_back(a = 0);
1254 statements.push_back(++a);
1255 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001256}
1257
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001258DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001259 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001260 Var i(kInt_Type, "i", 0);
1261 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001262 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001263 If(i > 5, Break())
1264 ))
1265 );
1266 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001267 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1268 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001269
1270 {
1271 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001272 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001273 Break()
1274 );
1275 }
1276}
1277
1278DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001279 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001280 Var i(kInt_Type, "i", 0);
1281 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001282 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001283 If(i < 5, Continue())
1284 ))
1285 );
1286 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001287 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1288 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001289
1290 {
1291 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001292 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001293 Continue()
1294 );
1295 }
1296}
1297
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001298DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001299 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001300 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001301 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001302 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001303 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001304 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001305
1306 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001307 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001308 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001309 Declare(c).release();
1310 }
1311
1312 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001313 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001314 Declare(d).release();
1315 ExpectError error(r, "error: variable has already been declared\n");
1316 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001317 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001318
1319 {
1320 Var e(kUniform_Modifier, kInt_Type, "e");
1321 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1322 Declare(e).release();
1323 }
1324}
1325
1326DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001327 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001328 Var x(kInt_Type, "x", 0);
1329 DeclareGlobal(x);
1330 Var y(kUniform_Modifier, kFloat2_Type, "y");
1331 DeclareGlobal(y);
1332 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1333 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1334 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001335}
1336
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001337DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1338 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
John Stiles443fa192021-05-24 23:46:46 -04001339 Var x(kFloat_Type, "x", 1);
1340 EXPECT_EQUAL(If(Sqrt(x) > 0, Discard()), "if ((sqrt(x) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001341}
1342
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001343DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1344 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1345 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001346 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001347
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001348 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001349 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001350 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001351
1352 {
1353 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1354 Do(Block(), 7).release();
1355 }
1356}
1357
1358DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001359 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles8676ebe2021-04-20 15:30:41 -04001360 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1361 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001362
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001363 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001364 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1365 "for (int i = 0; (i < 10); ++i) (i += 5);");
1366
1367 Var j(kInt_Type, "j", 0);
1368 Var k(kInt_Type, "k", 10);
1369 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1370 {
1371 int j = 0;
1372 int k = 10;
1373 for (; (j < k); ++j) {}
1374 }
1375 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001376
1377 {
1378 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1379 For(i = 0, i + 10, ++i, i += 5).release();
1380 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001381
1382 {
1383 ExpectError error(r, "error: invalid for loop initializer\n");
1384 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1385 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001386}
1387
Ethan Nicholase2c05042021-02-03 10:27:22 -05001388DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001389 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001390 Var coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001391 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001392 sk_FragColor() = Half4(coords, 0, 1)
1393 );
1394 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001395 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001396 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001397
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001398 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001399 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001400 Var x(kFloat_Type, "x");
1401 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001402 sqr.define(
1403 Return(x * x)
1404 );
1405 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1406 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1407 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1408 }
1409
1410 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001411 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001412 Var x(kFloat2_Type, "x");
1413 Var y(kFloat2_Type, "y");
1414 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001415 dot.define(
1416 Return(x * x + y * y)
1417 );
1418 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1419 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1420 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1421 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1422 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1423 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001424
1425 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001426 DSLWriter::Reset();
1427 Var x(kFloat_Type, "x");
1428 Var y(kFloat_Type, "y");
1429 DSLFunction pair(kFloat2_Type, "pair", x, y);
1430 pair.define(
1431 Return(Float2(x, y))
1432 );
1433 Var varArg1(kFloat_Type, "varArg1");
1434 Var varArg2(kFloat_Type, "varArg2");
1435 DSLWriter::MarkDeclared(varArg1);
1436 DSLWriter::MarkDeclared(varArg2);
1437 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1438 }
1439
1440 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001441 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001442 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001443 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001444 Return(true)
1445 );
1446 }
1447
1448 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001449 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001450 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001451 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001452 Return()
1453 );
1454 }
1455
1456 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001457 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001458 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001459 Var x(kFloat_Type, "x", 0);
1460 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001461 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001462 If(x == 1, Return(x))
1463 );
1464 }
1465
1466 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001467 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001468 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001469 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001470 Return(0)
1471 );
1472 }
1473
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001474 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001475 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001476 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001477 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001478 );
1479 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001480
1481 {
1482 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1483 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001484 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001485 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001486 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001487 );
1488 }
1489
1490 {
1491 ExpectError error(r, "error: variable has already been declared\n");
1492 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001493 DSLVar p(kFloat_Type);
1494 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001495 );
1496 Declare(p).release();
1497 }
1498
1499 {
1500 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1501 "values\n");
1502 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001503 DSLVar p(kFloat_Type, 1);
1504 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001505 );
1506 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001507}
1508
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001509DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1510 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001511 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001512 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001513 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001514
1515 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001516 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001517
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001518 Statement z = StaticIf(a > b, a -= b, b -= a);
1519 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1520
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001521 {
1522 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1523 If(a + b, a -= b).release();
1524 }
1525}
1526
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001527DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInterfaceBlock, r, ctxInfo) {
1528 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1529 DSLVar intf = InterfaceBlock(kUniform_Modifier, "InterfaceBlock1",
1530 { Field(kFloat_Type, "a"), Field(kInt_Type, "b") });
1531 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1532 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1533 "uniform InterfaceBlock1 { float a; int b; };");
1534 EXPECT_EQUAL(intf.field("a"), "InterfaceBlock1.a");
1535
1536 DSLVar intf2 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock2",
1537 { Field(kFloat2_Type, "x"), Field(kHalf2x2_Type, "y") },
1538 "blockVar");
1539 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1540 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1541 "uniform InterfaceBlock2 { float2 x; half2x2 y; } blockVar;");
1542 EXPECT_EQUAL(intf2.field("x"), "blockVar.x");
1543
1544 DSLVar intf3 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock3", { Field(kFloat_Type, "z") },
1545 "arrayVar", 4);
1546 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1547 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1548 "uniform InterfaceBlock3 { float z; } arrayVar[4];");
1549 EXPECT_EQUAL(intf3[1].field("z"), "arrayVar[1].z");
1550}
1551
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001552DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1553 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1554
1555 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001556 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001557
1558 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001559 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001560}
1561
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001562DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1563 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001564 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001565 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001566 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001567
1568 {
1569 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001570 Select(a, 1, -1).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001571 }
1572
1573 {
1574 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001575 Select(a > 0, Float2(1), Float3(1)).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001576 }
1577}
1578
Ethan Nicholascfefec02021-02-09 15:22:57 -05001579DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1580 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1581
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001582 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001583
Ethan Nicholas722cb672021-05-06 10:47:06 -04001584 SkTArray<DSLStatement> caseStatements;
1585 caseStatements.push_back(a = 1);
1586 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001587 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001588 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001589 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001590 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001591 Default(Discard())
1592 );
John Stilese1d1b082021-02-23 13:44:36 -05001593 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001594 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001595 case 0: (a = 0.0); break;
1596 case 1: (a = 1.0); continue;
1597 case 2: (a = 2.0);
1598 default: discard;
1599 }
1600 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001601
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001602 Statement y = StaticSwitch(b,
1603 Case(0, a = 0, Break()),
1604 Case(1, a = 1, Continue()),
1605 Case(2, a = 2 /*Fallthrough*/),
1606 Default(Discard())
1607 );
1608 EXPECT_EQUAL(y, R"(
1609 @switch (b) {
1610 case 0: (a = 0.0); break;
1611 case 1: (a = 1.0); continue;
1612 case 2: (a = 2.0);
1613 default: discard;
1614 }
1615 )");
1616
John Stiles642cde22021-02-23 14:57:01 -05001617 EXPECT_EQUAL(Switch(b),
1618 "switch (b) {}");
1619
1620 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1621 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001622
1623 {
John Stilese1d1b082021-02-23 13:44:36 -05001624 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001625 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001626 }
1627
1628 {
John Stilese1d1b082021-02-23 13:44:36 -05001629 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001630 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001631 }
1632
1633 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001634 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001635 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001636 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001637 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001638}
1639
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001640DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001641 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001642 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001643
John Stilesf04e09c2021-03-05 13:13:14 -05001644 EXPECT_EQUAL(a.x(),
1645 "a.x");
1646 EXPECT_EQUAL(a.y(),
1647 "a.y");
1648 EXPECT_EQUAL(a.z(),
1649 "a.z");
1650 EXPECT_EQUAL(a.w(),
1651 "a.w");
1652 EXPECT_EQUAL(a.r(),
1653 "a.x");
1654 EXPECT_EQUAL(a.g(),
1655 "a.y");
1656 EXPECT_EQUAL(a.b(),
1657 "a.z");
1658 EXPECT_EQUAL(a.a(),
1659 "a.w");
1660 EXPECT_EQUAL(Swizzle(a, R),
1661 "a.x");
1662 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1663 "float2(0.0, a.y)");
1664 EXPECT_EQUAL(Swizzle(a, B, G, G),
1665 "a.zyy");
1666 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1667 "float4(a.xyz, 1.0)");
1668 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1669 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001670}
1671
John Stiles08771b02021-04-26 09:35:10 -04001672
1673DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001674 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles08771b02021-04-26 09:35:10 -04001675
1676 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1677 Var a;
1678 if (true)
1679 {
1680 Var(kInt_Type, "a").swap(a);
1681 }
1682
1683 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1684 "{ int a; (a = 123); }");
1685}
1686
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001687DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1688 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1689 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001690 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001691
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001692 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001693 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001694 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001695
1696 {
1697 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001698 While(7, Block()).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001699 }
1700}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001701
1702DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1703 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001704 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001705
1706 EXPECT_EQUAL(a[0], "a[0]");
1707 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001708
1709 {
1710 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001711 a[true].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001712 }
1713
1714 {
1715 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001716 b[0].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001717 }
1718
1719 {
1720 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001721 a[-1].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001722 }
1723}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001724
1725DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1726 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1727 // There is a Fract type on Mac which can conflict with our Fract builtin
1728 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001729 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1730 Var h3(kHalf3_Type, "h3");
1731 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001732 EXPECT_EQUAL(Abs(a), "abs(a)");
1733 EXPECT_EQUAL(All(b4), "all(b4)");
1734 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001735 EXPECT_EQUAL(Atan(a), "atan(a)");
1736 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001737 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1738 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1739 EXPECT_EQUAL(Cos(a), "cos(a)");
1740 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1741 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1742 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1743 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1744 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1745 EXPECT_EQUAL(Exp(a), "exp(a)");
1746 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1747 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1748 EXPECT_EQUAL(Floor(a), "floor(a)");
1749 EXPECT_EQUAL(Fract(a), "fract(a)");
1750 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1751 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1752 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1753 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1754 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1755 EXPECT_EQUAL(Length(a), "length(a)");
1756 EXPECT_EQUAL(Log(a), "log(a)");
1757 EXPECT_EQUAL(Log2(a), "log2(a)");
1758 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1759 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1760 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1761 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1762 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1763 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1764 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1765 EXPECT_EQUAL(Radians(a), "radians(a)");
1766 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1767 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1768 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1769 EXPECT_EQUAL(Sign(a), "sign(a)");
1770 EXPECT_EQUAL(Sin(a), "sin(a)");
1771 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1772 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1773 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1774 EXPECT_EQUAL(Tan(a), "tan(a)");
1775 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001776
1777 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1778 // one of them reports errors correctly
1779 {
1780 ExpectError error(r, "error: no match for ceil(bool)\n");
1781 Ceil(a == b).release();
1782 }
1783}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001784
1785DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001786 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001787
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001788 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001789 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001790 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001791
1792 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1793 // context, so we can't as yet Declare() variables with these modifiers.
1794 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001795 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001796 REPORTER_ASSERT(r, v2.modifiers().flags() == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001797 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001798
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001799 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001800 REPORTER_ASSERT(r, v3.modifiers().flags() == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001801 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001802
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001803 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001804 REPORTER_ASSERT(r, v4.modifiers().flags() == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001805 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001806
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001807 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001808 REPORTER_ASSERT(r, v5.modifiers().flags() == SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001809 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001810
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001811 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001812 REPORTER_ASSERT(r, v6.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1813 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001814 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001815
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001816 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001817 REPORTER_ASSERT(r, v7.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1818 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001819 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001820
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001821 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001822 REPORTER_ASSERT(r, v8.modifiers().flags() == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001823 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001824 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001825}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001826
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001827DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001828 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001829 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1830 .inputAttachmentIndex(7),
1831 kConst_Modifier), kInt_Type, "v1", 0);
1832 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1833 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1834
1835 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1836 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1837
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001838 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1839 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1840
1841 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1842 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1843
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001844 {
1845 ExpectError error(r, "error: 'srgb_unpremul' is only permitted in runtime effects\n");
1846 Var v(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kHalf4_Type, "v");
1847 DeclareGlobal(v);
1848 }
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001849
1850 {
1851 ExpectError error(r, "error: layout qualifier 'location' appears more than once\n");
1852 DSLLayout().location(1).location(2);
1853 }
1854
1855 {
1856 ExpectError error(r, "error: layout qualifier 'set' appears more than once\n");
1857 DSLLayout().set(1).set(2);
1858 }
1859
1860 {
1861 ExpectError error(r, "error: layout qualifier 'binding' appears more than once\n");
1862 DSLLayout().binding(1).binding(2);
1863 }
1864
1865 {
1866 ExpectError error(r, "error: layout qualifier 'offset' appears more than once\n");
1867 DSLLayout().offset(1).offset(2);
1868 }
1869
1870 {
1871 ExpectError error(r, "error: layout qualifier 'index' appears more than once\n");
1872 DSLLayout().index(1).index(2);
1873 }
1874
1875 {
1876 ExpectError error(r, "error: layout qualifier 'builtin' appears more than once\n");
1877 DSLLayout().builtin(1).builtin(2);
1878 }
1879
1880 {
1881 ExpectError error(r, "error: layout qualifier 'input_attachment_index' appears more than "
1882 "once\n");
1883 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1884 }
1885
1886 {
1887 ExpectError error(r, "error: layout qualifier 'origin_upper_left' appears more than "
1888 "once\n");
1889 DSLLayout().originUpperLeft().originUpperLeft();
1890 }
1891
1892 {
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001893 ExpectError error(r, "error: layout qualifier 'push_constant' appears more than once\n");
1894 DSLLayout().pushConstant().pushConstant();
1895 }
1896
1897 {
1898 ExpectError error(r, "error: layout qualifier 'blend_support_all_equations' appears more "
1899 "than once\n");
1900 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1901 }
1902
1903 {
1904 ExpectError error(r, "error: layout qualifier 'srgb_unpremul' appears more than once\n");
1905 DSLLayout().srgbUnpremul().srgbUnpremul();
1906 }
1907}
1908
Ethan Nicholas624a5292021-04-16 14:54:43 -04001909DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001910 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(),
Brian Osman552fcb92021-04-28 17:41:57 -04001911 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001912 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001913 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001914
1915 {
Brian Osmanc9125aa2021-04-21 09:57:19 -04001916 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001917 Sample(shader, Half4(1)).release();
1918 }
1919}
1920
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001921DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001922 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001923
1924 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001925 Field(kFloat_Type, "x"),
1926 Field(kBool_Type, "b"),
1927 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001928 );
1929 DSLVar result(simpleStruct, "result");
1930 DSLFunction(simpleStruct, "returnStruct").define(
1931 Declare(result),
1932 result.field("x") = 123,
1933 result.field("b") = result.field("x") > 0,
1934 result.field("a")[0] = result.field("x"),
1935 Return(result)
1936 );
1937 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001938 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1939 "struct SimpleStruct { float x; bool b; float[3] a; };");
1940 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1941 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1942 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001943
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001944 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001945 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001946 Field(simpleStruct, "simple")
1947 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001948 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1949 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001950 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001951}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001952
1953DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
1954 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1955 std::vector<Wrapper<DSLExpression>> exprs;
1956 exprs.push_back(DSLExpression(1));
1957 exprs.emplace_back(2.0);
1958 EXPECT_EQUAL(std::move(*exprs[0]), "1");
1959 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
1960
1961 std::vector<Wrapper<DSLVar>> vars;
1962 vars.emplace_back(DSLVar(kInt_Type, "x"));
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001963 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0])->name() == "x");
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001964}
Ethan Nicholasebc9fad2021-07-09 15:35:23 -04001965
1966DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLRTAdjust, r, ctxInfo) {
1967 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
1968 SkSL::ProgramKind::kVertex);
1969 DSLVar rtAdjust(kUniform_Modifier, kFloat4_Type, "sk_RTAdjust");
1970 DeclareGlobal(rtAdjust);
1971 DSLFunction(kVoid_Type, "main").define(
1972 sk_Position() = Half4(0)
1973 );
1974 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1975 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1976 "void main() {"
1977 "(sk_PerVertex.sk_Position = float4(0.0));"
1978 "(sk_PerVertex.sk_Position = float4(((sk_PerVertex.sk_Position.xy * sk_RTAdjust.xz) + "
1979 "(sk_PerVertex.sk_Position.ww * sk_RTAdjust.yw)), 0.0, sk_PerVertex.sk_Position.w));"
1980 "}");
1981}
Ethan Nicholas292a09d2021-07-14 09:52:16 -04001982
1983DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInlining, r, ctxInfo) {
1984 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
1985 DSLVar x(kFloat_Type, "x");
1986 DSLFunction sqr(kFloat_Type, "sqr", x);
1987 sqr.define(
1988 Return(x * x)
1989 );
1990 DSLFunction(kVoid_Type, "main").define(
1991 sk_FragColor() = (sqr(2), Half4(sqr(3)))
1992 );
1993 std::unique_ptr<SkSL::Program> program = ReleaseProgram();
1994 EXPECT_EQUAL(*program,
1995 "layout(location = 0, index = 0, builtin = 10001) out half4 sk_FragColor;"
1996 "layout(builtin = 17)in bool sk_Clockwise;"
1997 "void main() {"
1998 "/* inlined: sqr */;"
1999 "/* inlined: sqr */;"
2000 "(sk_FragColor = (4.0 , half4(half(9.0))));"
2001 "}");
2002}