blob: a21ea761603510a7978fe908f59446072587d552 [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 Nicholas4a5e22a2021-08-13 17:29:51 -040053class ExpectError : public SkSL::ErrorReporter {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050054public:
55 ExpectError(skiatest::Reporter* reporter, const char* msg)
56 : fMsg(msg)
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040057 , fReporter(reporter)
58 , fOldReporter(&GetErrorReporter()) {
59 SetErrorReporter(this);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050060 }
61
62 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050063 REPORTER_ASSERT(fReporter, !fMsg,
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040064 "Error mismatch: expected:\n%s\nbut no error occurred\n", fMsg);
65 SetErrorReporter(fOldReporter);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050066 }
67
Ethan Nicholas32724122021-09-07 13:49:07 -040068 void handleError(skstd::string_view msg, SkSL::PositionInfo pos) override {
69 REPORTER_ASSERT(fReporter, fMsg, "Received unexpected extra error: %.*s\n",
70 (int)msg.length(), msg.data());
71 REPORTER_ASSERT(fReporter, !fMsg || msg == fMsg,
72 "Error mismatch: expected:\n%s\nbut received:\n%.*s", fMsg, (int)msg.length(),
73 msg.data());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050074 fMsg = nullptr;
75 }
76
77private:
78 const char* fMsg;
79 skiatest::Reporter* fReporter;
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -040080 ErrorReporter* fOldReporter;
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050081};
82
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050083static bool whitespace_insensitive_compare(const char* a, const char* b) {
84 for (;;) {
85 while (isspace(*a)) {
86 ++a;
87 }
88 while (isspace(*b)) {
89 ++b;
90 }
91 if (*a != *b) {
92 return false;
93 }
94 if (*a == 0) {
95 return true;
96 }
97 ++a;
98 ++b;
99 }
100}
101
Ethan Nicholas24c17722021-03-09 13:10:59 -0500102// for use from SkSLDSLOnlyTest.cpp
103void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
104 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
105}
106
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500107DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -0500108 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
109 Expression e1 = 1;
110 REPORTER_ASSERT(r, e1.release()->description() == "1");
111 Expression e2 = 1.0;
112 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
113 Expression e3 = true;
114 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400115 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500116 Expression e4 = a;
117 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500118
119 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
120 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
121 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
122 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
123 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
124 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
125 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
126 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500127}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500128
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500129static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
130static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
131static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
132static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
Ethan Nicholas722cb672021-05-06 10:47:06 -0400133static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); }
Ethan Nicholas292a09d2021-07-14 09:52:16 -0400134static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
135static SkSL::String stringize(SkSL::Program& program) { return program.description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500136
137template <typename T>
138static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
139 SkSL::String actual = stringize(input);
140 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
141 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
142 lineNumber, expected, actual.c_str());
143 }
144}
145
146template <typename T>
147static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
148 // This overload allows temporary values to be passed to expect_equal.
149 return expect_equal(r, lineNumber, dsl, expected);
150}
151
152#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
153
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400154DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) {
155 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400156 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400157 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true");
158
159 Var x(kInt_Type, "x");
160 EXPECT_EQUAL(Declare(x), "int x;");
161 }
162
163 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400164 SkSL::ProgramSettings settings;
165 settings.fOptimize = false;
166 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings,
Brian Osman8c264792021-07-01 16:41:27 -0400167 SkSL::ProgramKind::kFragment);
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400168 EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))),
169 "all(greaterThan(float4(1.0), float4(0.0)))");
170 }
171
172 {
Ethan Nicholas55a63af2021-05-18 10:12:58 -0400173 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), SkSL::ProgramSettings());
Ethan Nicholas22dcb732021-05-12 11:26:58 -0400174 Var x(kInt_Type, "x");
175 EXPECT_EQUAL(Declare(x), "int _0_x;");
176 }
177}
178
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500179DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
180 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
181 Expression e1 = Float(std::numeric_limits<float>::max());
182 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
183 std::numeric_limits<float>::max());
184
185 Expression e2 = Float(std::numeric_limits<float>::min());
186 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
187 std::numeric_limits<float>::min());
188
John Stilesb4d7b582021-02-19 09:56:31 -0500189 EXPECT_EQUAL(Float2(0),
190 "float2(0.0)");
191 EXPECT_EQUAL(Float2(-0.5, 1),
192 "float2(-0.5, 1.0)");
193 EXPECT_EQUAL(Float3(0.75),
194 "float3(0.75)");
195 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500196 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500197 EXPECT_EQUAL(Float3(0, 1, 2),
198 "float3(0.0, 1.0, 2.0)");
199 EXPECT_EQUAL(Float4(0),
200 "float4(0.0)");
201 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500202 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500203 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500204 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500205 EXPECT_EQUAL(Float4(0, 1, 2, 3),
206 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500207
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400208 DSLVar x(kFloat_Type, "x");
209 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
210 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
211
212 DSLVar y(kFloat2_Type, "y");
213 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
214 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
215
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500216 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400217 ExpectError error(r, "floating point value is infinite");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500218 Float(std::numeric_limits<float>::infinity()).release();
219 }
220
221 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400222 ExpectError error(r, "floating point value is NaN");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500223 Float(std::numeric_limits<float>::quiet_NaN()).release();
224 }
225
226 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400227 ExpectError error(r, "'float4' is not a valid parameter to 'float2' constructor; use '.xy' "
228 "instead");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500229 Float2(Float4(1)).release();
230 }
231
232 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400233 ExpectError error(r, "invalid arguments to 'float4' constructor (expected 4 scalars, but "
234 "found 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500235 Float4(Float3(1)).release();
236 }
237}
238
239DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
240 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
241 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500242 REPORTER_ASSERT(r,
243 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500244
245 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500246 REPORTER_ASSERT(r,
247 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500248
John Stilesb9e4f642021-03-05 09:11:38 -0500249 EXPECT_EQUAL(Half2(0),
250 "half2(0.0)");
251 EXPECT_EQUAL(Half2(-0.5, 1),
252 "half2(-0.5, 1.0)");
253 EXPECT_EQUAL(Half3(0.75),
254 "half3(0.75)");
255 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
256 "half3(0.0, 1.0, -2.0)");
257 EXPECT_EQUAL(Half3(0, 1, 2),
258 "half3(0.0, 1.0, 2.0)");
259 EXPECT_EQUAL(Half4(0),
260 "half4(0.0)");
261 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
262 "half4(0.0, 1.0, 2.0, 3.0)");
263 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
264 "half4(0.0, 1.0, 2.0, 3.0)");
265 EXPECT_EQUAL(Half4(0, 1, 2, 3),
266 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500267
268 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400269 ExpectError error(r, "floating point value is infinite");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500270 Half(std::numeric_limits<float>::infinity()).release();
271 }
272
273 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400274 ExpectError error(r, "floating point value is NaN");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500275 Half(std::numeric_limits<float>::quiet_NaN()).release();
276 }
277
278 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400279 ExpectError error(r, "'half4' is not a valid parameter to 'half2' constructor; use '.xy' "
280 "instead");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500281 Half2(Half4(1)).release();
282 }
283
284 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400285 ExpectError error(r, "invalid arguments to 'half4' constructor (expected 4 scalars, but "
286 "found 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500287 Half4(Half3(1)).release();
288 }
289}
290
291DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
292 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500293
John Stilesb9e4f642021-03-05 09:11:38 -0500294 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
295 "2147483647");
296 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
297 "int2(-2147483648)");
298 EXPECT_EQUAL(Int2(0, 1),
299 "int2(0, 1)");
300 EXPECT_EQUAL(Int3(0),
301 "int3(0)");
302 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
303 "int3(0, 1, -2)");
304 EXPECT_EQUAL(Int3(0, 1, 2),
305 "int3(0, 1, 2)");
306 EXPECT_EQUAL(Int4(0),
307 "int4(0)");
308 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
309 "int4(0, 1, 2, 3)");
310 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
311 "int4(0, 1, 2, 3)");
312 EXPECT_EQUAL(Int4(0, 1, 2, 3),
313 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500314
315 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400316 ExpectError error(r, "'int4' is not a valid parameter to 'int2' constructor; use '.xy' "
317 "instead");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500318 Int2(Int4(1)).release();
319 }
320
321 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400322 ExpectError error(r, "invalid arguments to 'int4' constructor (expected 4 scalars, but "
323 "found 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500324 Int4(Int3(1)).release();
325 }
326}
327
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400328DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) {
329 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
330
331 EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()),
332 "4294967295");
333 EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()),
334 "uint2(0)");
335 EXPECT_EQUAL(UInt2(0, 1),
336 "uint2(0, 1)");
337 EXPECT_EQUAL(UInt3(0),
338 "uint3(0)");
339 EXPECT_EQUAL(UInt3(UInt2(0, 1), -2),
340 "uint3(0, 1, -2)");
341 EXPECT_EQUAL(UInt3(0, 1, 2),
342 "uint3(0, 1, 2)");
343 EXPECT_EQUAL(UInt4(0),
344 "uint4(0)");
345 EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)),
346 "uint4(0, 1, 2, 3)");
347 EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)),
348 "uint4(0, 1, 2, 3)");
349 EXPECT_EQUAL(UInt4(0, 1, 2, 3),
350 "uint4(0, 1, 2, 3)");
351
352 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400353 ExpectError error(r, "'uint4' is not a valid parameter to 'uint2' constructor; use '.xy' "
354 "instead");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400355 UInt2(UInt4(1)).release();
356 }
357
358 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400359 ExpectError error(r, "invalid arguments to 'uint4' constructor (expected 4 scalars, but "
360 "found 3)");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400361 UInt4(UInt3(1)).release();
362 }
363}
364
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500365DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
366 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500367
John Stilesb9e4f642021-03-05 09:11:38 -0500368 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
369 "32767");
370 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
371 "short2(-32768)");
372 EXPECT_EQUAL(Short2(0, 1),
373 "short2(0, 1)");
374 EXPECT_EQUAL(Short3(0),
375 "short3(0)");
376 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
377 "short3(0, 1, -2)");
378 EXPECT_EQUAL(Short3(0, 1, 2),
379 "short3(0, 1, 2)");
380 EXPECT_EQUAL(Short4(0),
381 "short4(0)");
382 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
383 "short4(0, 1, 2, 3)");
384 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
385 "short4(0, 1, 2, 3)");
386 EXPECT_EQUAL(Short4(0, 1, 2, 3),
387 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500388
389 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400390 ExpectError error(r, "'short4' is not a valid parameter to 'short2' constructor; use '.xy' "
391 "instead");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500392 Short2(Short4(1)).release();
393 }
394
395 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400396 ExpectError error(r, "invalid arguments to 'short4' constructor (expected 4 scalars, but "
397 "found 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500398 Short4(Short3(1)).release();
399 }
400}
401
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400402DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) {
403 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
404
405 EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()),
406 "65535");
407 EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()),
408 "ushort2(0)");
409 EXPECT_EQUAL(UShort2(0, 1),
410 "ushort2(0, 1)");
411 EXPECT_EQUAL(UShort3(0),
412 "ushort3(0)");
413 EXPECT_EQUAL(UShort3(UShort2(0, 1), -2),
414 "ushort3(0, 1, -2)");
415 EXPECT_EQUAL(UShort3(0, 1, 2),
416 "ushort3(0, 1, 2)");
417 EXPECT_EQUAL(UShort4(0),
418 "ushort4(0)");
419 EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)),
420 "ushort4(0, 1, 2, 3)");
421 EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)),
422 "ushort4(0, 1, 2, 3)");
423 EXPECT_EQUAL(UShort4(0, 1, 2, 3),
424 "ushort4(0, 1, 2, 3)");
425
426 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400427 ExpectError error(r, "'ushort4' is not a valid parameter to 'ushort2' constructor; use "
428 "'.xy' instead");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400429 UShort2(UShort4(1)).release();
430 }
431
432 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400433 ExpectError error(r, "invalid arguments to 'ushort4' constructor (expected 4 scalars, but "
434 "found 3)");
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400435 UShort4(UShort3(1)).release();
436 }
437}
438
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500439DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
440 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500441
John Stilesb9e4f642021-03-05 09:11:38 -0500442 EXPECT_EQUAL(Bool2(false),
443 "bool2(false)");
444 EXPECT_EQUAL(Bool2(false, true),
445 "bool2(false, true)");
446 EXPECT_EQUAL(Bool3(false),
447 "bool3(false)");
448 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
449 "bool3(false, true, false)");
450 EXPECT_EQUAL(Bool3(false, true, false),
451 "bool3(false, true, false)");
452 EXPECT_EQUAL(Bool4(false),
453 "bool4(false)");
454 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
455 "bool4(false, true, false, true)");
456 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
457 "bool4(false, true, false, true)");
458 EXPECT_EQUAL(Bool4(false, true, false, true),
459 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500460
461 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400462 ExpectError error(r, "'bool4' is not a valid parameter to 'bool2' constructor; use '.xy' "
463 "instead");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500464 Bool2(Bool4(true)).release();
465 }
466
467 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400468 ExpectError error(r, "invalid arguments to 'bool4' constructor (expected 4 scalars, but "
469 "found 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500470 Bool4(Bool3(true)).release();
471 }
472}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500473
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400474DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) {
475 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
476 REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean());
477 REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber());
478 REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat());
479 REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned());
480 REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned());
481 REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger());
482 REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar());
483 REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector());
484 REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix());
485 REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray());
486 REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct());
487
488 REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean());
489 REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber());
490 REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat());
491 REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned());
492 REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned());
493 REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger());
494 REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar());
495 REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector());
496 REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix());
497 REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray());
498 REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct());
499
500 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean());
501 REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber());
502 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat());
503 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned());
504 REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned());
505 REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger());
506 REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar());
507 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector());
508 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix());
509 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray());
510 REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct());
511
512 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean());
513 REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber());
514 REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat());
515 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned());
516 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned());
517 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger());
518 REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar());
519 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector());
520 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix());
521 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray());
522 REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct());
523
524 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean());
525 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber());
526 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat());
527 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned());
528 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned());
529 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger());
530 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar());
531 REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector());
532 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix());
533 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray());
534 REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct());
535
536 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean());
537 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber());
538 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat());
539 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned());
540 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned());
541 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger());
542 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar());
543 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector());
544 REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix());
545 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray());
546 REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct());
547
548 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean());
549 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber());
550 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat());
551 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned());
552 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned());
553 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger());
554 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar());
555 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector());
556 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix());
557 REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray());
558 REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct());
Ethan Nicholas722cb672021-05-06 10:47:06 -0400559
560 Var x(kFloat_Type);
561 DSLExpression e = x + 1;
562 REPORTER_ASSERT(r, e.type().isFloat());
563 e.release();
Ethan Nicholased6e54f2021-07-19 13:33:47 -0400564
565 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400566 ExpectError error(r, "array size must be positive");
Ethan Nicholased6e54f2021-07-19 13:33:47 -0400567 Array(kFloat_Type, -1);
568 }
569
570 {
Ethan Nicholas6f20b8d2021-08-31 07:40:24 -0400571 ExpectError error(r, "multi-dimensional arrays are not supported");
Ethan Nicholased6e54f2021-07-19 13:33:47 -0400572 Array(Array(kFloat_Type, 2), 2);
573 }
Ethan Nicholasb83199e2021-05-03 14:25:35 -0400574}
575
Ethan Nicholas84558932021-04-12 16:56:37 -0400576DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
577 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
578 Var f22(kFloat2x2_Type, "f22");
579 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
580 Var f32(kFloat3x2_Type, "f32");
581 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
582 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
583 Var f42(kFloat4x2_Type, "f42");
584 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
585 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
586 Var f23(kFloat2x3_Type, "f23");
587 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
588 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
589 Var f33(kFloat3x3_Type, "f33");
590 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
591 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
592 Var f43(kFloat4x3_Type, "f43");
593 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
594 "(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))");
595 Var f24(kFloat2x4_Type, "f24");
596 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
597 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
598 Var f34(kFloat3x4_Type, "f34");
599 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
600 "(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))");
601 Var f44(kFloat4x4_Type, "f44");
602 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
603
604 Var h22(kHalf2x2_Type, "h22");
605 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
606 Var h32(kHalf3x2_Type, "h32");
607 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
608 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
609 Var h42(kHalf4x2_Type, "h42");
610 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
611 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
612 Var h23(kHalf2x3_Type, "h23");
613 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
614 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
615 Var h33(kHalf3x3_Type, "h33");
616 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
617 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
618 Var h43(kHalf4x3_Type, "h43");
619 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
620 "(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))");
621 Var h24(kHalf2x4_Type, "h24");
622 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
623 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
624 Var h34(kHalf3x4_Type, "h34");
625 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
626 "(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))");
627 Var h44(kHalf4x4_Type, "h44");
628 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
629
630 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
631 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
632 EXPECT_EQUAL(h42[0][1], "h42[0].y");
633 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
634 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
635 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
636
637 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400638 ExpectError error(r, "invalid arguments to 'float3x3' constructor (expected 9 scalars, but "
639 "found 2)");
Ethan Nicholas84558932021-04-12 16:56:37 -0400640 DSLExpression(Float3x3(Float2(1))).release();
641 }
642
643 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400644 ExpectError error(r, "invalid arguments to 'half2x2' constructor (expected 4 scalars, but "
645 "found 5)");
Ethan Nicholas84558932021-04-12 16:56:37 -0400646 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
647 }
648
649 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400650 ExpectError error(r, "type mismatch: '*' cannot operate on 'float4x3', 'float3'");
Ethan Nicholas84558932021-04-12 16:56:37 -0400651 DSLExpression(f43 * Float3(1)).release();
652 }
653
654 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400655 ExpectError error(r, "type mismatch: '=' cannot operate on 'float4x3', 'float3x3'");
Ethan Nicholas84558932021-04-12 16:56:37 -0400656 DSLExpression(f43 = f33).release();
657 }
658
659 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400660 ExpectError error(r, "type mismatch: '=' cannot operate on 'half2x2', 'float2x2'");
Ethan Nicholas84558932021-04-12 16:56:37 -0400661 DSLExpression(h22 = f22).release();
662 }
663
664 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400665 ExpectError error(r, "no match for inverse(float4x3)");
Ethan Nicholas84558932021-04-12 16:56:37 -0400666 DSLExpression(Inverse(f43)).release();
667 }
668}
669
Ethan Nicholas92969f22021-01-13 10:38:59 -0500670DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
671 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400672 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500673
John Stiles8f440b42021-03-05 16:48:56 -0500674 EXPECT_EQUAL(a + b,
675 "(a + b)");
676 EXPECT_EQUAL(a + 1,
677 "(a + 1.0)");
678 EXPECT_EQUAL(0.5 + a + -99,
679 "((0.5 + a) + -99.0)");
680 EXPECT_EQUAL(a += b + 1,
681 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500682 EXPECT_EQUAL(+a,
683 "a");
684 EXPECT_EQUAL(+(a + b),
685 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500686
687 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400688 ExpectError error(r, "type mismatch: '+' cannot operate on 'bool2', 'float'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500689 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500690 }
691
692 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400693 ExpectError error(r, "type mismatch: '+=' cannot operate on 'float', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500694 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500695 }
696
697 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400698 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500699 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500700 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500701
702 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400703 ExpectError error(r, "'+' cannot operate on 'bool'");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400704 Var c(kBool_Type);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400705 DSLExpression(+c).release();
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500706 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500707}
708
709DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
710 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400711 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500712
John Stiles8f440b42021-03-05 16:48:56 -0500713 EXPECT_EQUAL(a - b,
714 "(a - b)");
715 EXPECT_EQUAL(a - 1,
716 "(a - 1)");
717 EXPECT_EQUAL(2 - a - b,
718 "((2 - a) - b)");
719 EXPECT_EQUAL(a -= b + 1,
720 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500721 EXPECT_EQUAL(-a,
722 "-a");
723 EXPECT_EQUAL(-(a - b),
724 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500725
726 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400727 ExpectError error(r, "type mismatch: '-' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500728 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500729 }
730
731 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400732 ExpectError error(r, "type mismatch: '-=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500733 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500734 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500735
736 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400737 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500738 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500739 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500740
741 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400742 ExpectError error(r, "'-' cannot operate on 'bool'");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400743 Var c(kBool_Type);
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400744 DSLExpression(-c).release();
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500745 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500746}
747
748DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
749 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400750 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500751
John Stiles8f440b42021-03-05 16:48:56 -0500752 EXPECT_EQUAL(a * b,
753 "(a * b)");
754 EXPECT_EQUAL(a * 2,
755 "(a * 2.0)");
756 EXPECT_EQUAL(0.5 * a * -99,
757 "((0.5 * a) * -99.0)");
758 EXPECT_EQUAL(a *= b + 1,
759 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500760
761 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400762 ExpectError error(r, "type mismatch: '*' cannot operate on 'bool2', 'float'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500763 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500764 }
765
766 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400767 ExpectError error(r, "type mismatch: '*=' cannot operate on 'float', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500768 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500769 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500770
771 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400772 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500773 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500774 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500775}
776
777DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
778 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400779 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500780
John Stiles8f440b42021-03-05 16:48:56 -0500781 EXPECT_EQUAL(a / b,
782 "(a / b)");
783 EXPECT_EQUAL(a / 2,
784 "(a / 2.0)");
785 EXPECT_EQUAL(0.5 / a / -99,
786 "((0.5 / a) / -99.0)");
787 EXPECT_EQUAL(b / (a - 1),
788 "(b / (a - 1.0))");
789 EXPECT_EQUAL(a /= b + 1,
790 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500791
792 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400793 ExpectError error(r, "type mismatch: '/' cannot operate on 'bool2', 'float'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500794 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500795 }
796
797 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400798 ExpectError error(r, "type mismatch: '/=' cannot operate on 'float', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500799 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500800 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500801
802 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400803 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500804 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500805 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500806
807 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400808 ExpectError error(r, "division by zero");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500809 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500810 }
811
812 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400813 Var c(kFloat2_Type, "c");
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400814 ExpectError error(r, "division by zero");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500815 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500816 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500817}
818
819DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
820 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400821 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500822 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500823 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500824
825 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500826 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500827
828 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500829 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500830
831 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500832 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833
834 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400835 ExpectError error(r, "type mismatch: '%' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500836 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500837 }
838
839 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400840 ExpectError error(r, "type mismatch: '%=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500841 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500842 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500843
844 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400845 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500846 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500847 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500848
849 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400850 ExpectError error(r, "division by zero");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500851 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500852 }
853
854 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400855 Var c(kInt2_Type, "c");
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400856 ExpectError error(r, "division by zero");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500857 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500858 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500859}
860
861DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
862 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400863 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500864 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500865 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866
867 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500868 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500869
870 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500871 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500872
873 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500874 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500875
876 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400877 ExpectError error(r, "type mismatch: '<<' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500878 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500879 }
880
881 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400882 ExpectError error(r, "type mismatch: '<<=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500883 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500884 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500885
886 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400887 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500888 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500889 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500890}
891
892DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
893 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400894 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500895 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500896 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500897
898 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500899 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500900
901 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500902 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500903
904 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500905 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500906
907 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400908 ExpectError error(r, "type mismatch: '>>' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500909 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500910 }
911
912 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400913 ExpectError error(r, "type mismatch: '>>=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500914 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500915 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500916
917 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400918 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500919 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500920 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500921}
922
923DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
924 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400925 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500926 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500927 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500928
929 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500930 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500931
932 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500933 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500934
935 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500936 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500937
938 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400939 ExpectError error(r, "type mismatch: '&' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500940 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500941 }
942
943 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400944 ExpectError error(r, "type mismatch: '&=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500945 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500946 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500947
948 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400949 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500950 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500951 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500952}
953
954DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
955 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400956 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500957 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500958 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500959
960 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500961 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500962
963 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500964 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500965
966 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500967 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500968
969 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400970 ExpectError error(r, "type mismatch: '|' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500971 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500972 }
973
974 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400975 ExpectError error(r, "type mismatch: '|=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500976 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500977 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500978
979 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -0400980 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500981 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500982 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500983}
984
985DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
986 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400987 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500988 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500989 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500990
991 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500992 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500993
994 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500995 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500996
997 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500998 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500999
1000 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001001 ExpectError error(r, "type mismatch: '^' cannot operate on 'bool2', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001002 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001003 }
1004
1005 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001006 ExpectError error(r, "type mismatch: '^=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001007 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001008 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -05001009
1010 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001011 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001012 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -05001013 }
Ethan Nicholas92969f22021-01-13 10:38:59 -05001014}
1015
1016DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
1017 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001018 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001019 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001020 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001021
1022 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001023 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001024
1025 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -05001026 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001027
1028 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001029 ExpectError error(r, "type mismatch: '&&' cannot operate on 'bool', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001030 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001031 }
1032}
1033
1034DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
1035 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001036 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001037 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001038 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001039
1040 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001041 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001042
1043 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -05001044 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001045
1046 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001047 ExpectError error(r, "type mismatch: '||' cannot operate on 'bool', 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001048 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001049 }
1050}
1051
Ethan Nicholas2ab47c92021-07-19 12:30:37 -04001052DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalXor, r, ctxInfo) {
1053 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1054 Var a(kBool_Type, "a"), b(kBool_Type, "b");
1055 Expression e1 = LogicalXor(a, b);
1056 EXPECT_EQUAL(e1, "(a ^^ b)");
1057
1058 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001059 ExpectError error(r, "type mismatch: '^^' cannot operate on 'bool', 'int'");
Ethan Nicholas2ab47c92021-07-19 12:30:37 -04001060 DSLExpression(LogicalXor(a, 5)).release();
1061 }
1062}
1063
Ethan Nicholas92969f22021-01-13 10:38:59 -05001064DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
1065 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001066 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001067 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -05001068 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001069
1070 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -05001071 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001072}
1073
1074DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
1075 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001076 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001077 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -05001078 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001079
1080 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001081 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001082
1083 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001084 ExpectError error(r, "type mismatch: '==' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001085 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001086 }
1087}
1088
1089DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
1090 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001091 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001092 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -05001093 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001094
1095 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001096 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001097
1098 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001099 ExpectError error(r, "type mismatch: '!=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001100 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001101 }
1102}
1103
1104DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
1105 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001106 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001107 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -05001108 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001109
1110 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001111 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001112
1113 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001114 ExpectError error(r, "type mismatch: '>' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001115 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001116 }
1117}
1118
1119DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
1120 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001121 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001122 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001123 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001124
1125 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001126 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001127
1128 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001129 ExpectError error(r, "type mismatch: '>=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001130 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001131 }
1132}
1133
1134DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
1135 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001136 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001137 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -05001138 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001139
1140 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001141 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001142
1143 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001144 ExpectError error(r, "type mismatch: '<' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001145 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001146 }
1147}
1148
1149DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
1150 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001151 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001152 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -05001153 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001154
1155 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -05001156 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001157
1158 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001159 ExpectError error(r, "type mismatch: '<=' cannot operate on 'int', 'bool2'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001160 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001161 }
1162}
1163
1164DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
1165 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001166 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001167 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001168 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001169
1170 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001171 ExpectError error(r, "'!' cannot operate on 'int'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001172 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001173 }
1174}
1175
1176DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
1177 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001178 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001179 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -05001180 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001181
1182 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001183 ExpectError error(r, "'~' cannot operate on 'bool'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001184 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001185 }
1186}
1187
1188DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
1189 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001190 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001191 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -05001192 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001193
1194 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -05001195 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001196
1197 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001198 ExpectError error(r, "'++' cannot operate on 'bool'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001199 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001200 }
1201
1202 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001203 ExpectError error(r, "'++' cannot operate on 'bool'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001204 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001205 }
1206
1207 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001208 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001209 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001210 }
1211
1212 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001213 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001214 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001215 }
1216}
1217
1218DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
1219 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001220 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001221 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -05001222 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001223
1224 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -05001225 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -05001226
1227 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001228 ExpectError error(r, "'--' cannot operate on 'bool'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001229 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001230 }
1231
1232 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001233 ExpectError error(r, "'--' cannot operate on 'bool'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001234 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001235 }
1236
1237 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001238 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001239 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001240 }
1241
1242 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001243 ExpectError error(r, "cannot assign to this expression");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001244 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001245 }
1246}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001247
Ethan Nicholas722cb672021-05-06 10:47:06 -04001248DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) {
1249 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1250 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001251 DSLExpression sqrt(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "sqrt"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001252 SkTArray<DSLWrapper<DSLExpression>> args;
John Stiles443fa192021-05-24 23:46:46 -04001253 args.emplace_back(16);
1254 EXPECT_EQUAL(sqrt(std::move(args)), "4.0"); // sqrt(16) gets optimized to 4
Ethan Nicholas722cb672021-05-06 10:47:06 -04001255 }
1256
1257 {
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001258 DSLExpression pow(DSLWriter::IRGenerator().convertIdentifier(/*offset=*/-1, "pow"));
Ethan Nicholas722cb672021-05-06 10:47:06 -04001259 DSLVar a(kFloat_Type, "a");
1260 DSLVar b(kFloat_Type, "b");
1261 SkTArray<DSLWrapper<DSLExpression>> args;
1262 args.emplace_back(a);
1263 args.emplace_back(b);
1264 EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)");
1265 }
1266}
1267
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001268DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001269 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholas722cb672021-05-06 10:47:06 -04001270 EXPECT_EQUAL(Block(), "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001271 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholas722cb672021-05-06 10:47:06 -04001272 EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001273
Ethan Nicholas722cb672021-05-06 10:47:06 -04001274 EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;");
1275
1276 SkTArray<DSLStatement> statements;
1277 statements.push_back(a = 0);
1278 statements.push_back(++a);
1279 EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001280}
1281
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001282DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001283 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001284 Var i(kInt_Type, "i", 0);
1285 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001286 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001287 If(i > 5, Break())
1288 ))
1289 );
1290 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001291 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1292 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001293
1294 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001295 ExpectError error(r, "break statement must be inside a loop or switch");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001296 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001297 Break()
1298 );
1299 }
1300}
1301
1302DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001303 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001304 Var i(kInt_Type, "i", 0);
1305 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001306 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001307 If(i < 5, Continue())
1308 ))
1309 );
1310 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001311 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1312 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001313
1314 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001315 ExpectError error(r, "continue statement must be inside a loop");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001316 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001317 Continue()
1318 );
1319 }
1320}
1321
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001322DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001323 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasda729782021-07-19 12:55:52 -04001324 {
1325 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
1326 EXPECT_EQUAL(Declare(a), "half4 a;");
1327 EXPECT_EQUAL(Declare(b), "half4 b = half4(1.0);");
1328 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001329
1330 {
Ethan Nicholasda729782021-07-19 12:55:52 -04001331 DSLWriter::Reset();
1332 SkTArray<Var> vars;
1333 vars.push_back(Var(kBool_Type, "a", true));
1334 vars.push_back(Var(kFloat_Type, "b"));
1335 EXPECT_EQUAL(Declare(vars), "bool a = true; float b;");
1336 }
1337
1338 {
1339 DSLWriter::Reset();
1340 REPORTER_ASSERT(r, DSLWriter::ProgramElements().empty());
1341 GlobalVar a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
1342 Declare(a);
1343 Declare(b);
1344 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1345 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "half4 a;");
1346 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "half4 b = half4(1.0);");
1347 }
1348
1349 {
1350 DSLWriter::Reset();
1351 REPORTER_ASSERT(r, DSLWriter::ProgramElements().empty());
1352 SkTArray<GlobalVar> vars;
1353 vars.push_back(GlobalVar(kHalf4_Type, "a"));
1354 vars.push_back(GlobalVar(kHalf4_Type, "b", Half4(1)));
1355 Declare(vars);
1356 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1357 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "half4 a;");
1358 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "half4 b = half4(1.0);");
1359 }
1360
1361 {
1362 DSLWriter::Reset();
1363 Var a(kHalf4_Type, "a", 1);
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001364 ExpectError error(r, "expected 'half4', but found 'int'");
Ethan Nicholasda729782021-07-19 12:55:52 -04001365 Declare(a).release();
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001366 }
1367
1368 {
Ethan Nicholasda729782021-07-19 12:55:52 -04001369 DSLWriter::Reset();
1370 Var a(kInt_Type, "a");
1371 Declare(a).release();
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001372 ExpectError error(r, "variable has already been declared");
Ethan Nicholasda729782021-07-19 12:55:52 -04001373 Declare(a).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001374 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001375
1376 {
Ethan Nicholasda729782021-07-19 12:55:52 -04001377 DSLWriter::Reset();
1378 Var a(kUniform_Modifier, kInt_Type, "a");
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001379 ExpectError error(r, "'uniform' is not permitted here");
Ethan Nicholasda729782021-07-19 12:55:52 -04001380 Declare(a).release();
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001381 }
1382}
1383
1384DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001385 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001386 DSLGlobalVar x(kInt_Type, "x", 0);
1387 Declare(x);
1388 DSLGlobalVar y(kUniform_Modifier, kFloat2_Type, "y");
1389 Declare(y);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001390 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1391 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1392 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001393}
1394
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001395DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1396 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
John Stiles443fa192021-05-24 23:46:46 -04001397 Var x(kFloat_Type, "x", 1);
1398 EXPECT_EQUAL(If(Sqrt(x) > 0, Discard()), "if ((sqrt(x) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001399}
1400
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001401DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1402 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1403 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001404 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001405
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001406 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001407 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001408 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001409
1410 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001411 ExpectError error(r, "expected 'bool', but found 'int'");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001412 Do(Block(), 7).release();
1413 }
1414}
1415
1416DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001417 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles8676ebe2021-04-20 15:30:41 -04001418 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1419 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001420
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001421 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001422 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1423 "for (int i = 0; (i < 10); ++i) (i += 5);");
1424
1425 Var j(kInt_Type, "j", 0);
1426 Var k(kInt_Type, "k", 10);
1427 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1428 {
1429 int j = 0;
1430 int k = 10;
1431 for (; (j < k); ++j) {}
1432 }
1433 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001434
1435 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001436 ExpectError error(r, "expected 'bool', but found 'int'");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001437 For(i = 0, i + 10, ++i, i += 5).release();
1438 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001439
1440 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001441 ExpectError error(r, "invalid for loop initializer");
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001442 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1443 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001444}
1445
Ethan Nicholase2c05042021-02-03 10:27:22 -05001446DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001447 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001448 Parameter coords(kFloat2_Type, "coords");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001449 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001450 sk_FragColor() = Half4(coords, 0, 1)
1451 );
1452 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001453 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
Ethan Nicholas371f6e12021-05-04 14:30:02 -04001454 "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001455
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001456 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001457 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001458 DSLParameter x(kFloat_Type, "x");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001459 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001460 sqr.define(
1461 Return(x * x)
1462 );
1463 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1464 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1465 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1466 }
1467
1468 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001469 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001470 DSLParameter x(kFloat2_Type, "x");
1471 DSLParameter y(kFloat2_Type, "y");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001472 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001473 dot.define(
1474 Return(x * x + y * y)
1475 );
1476 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1477 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1478 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1479 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1480 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1481 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001482
1483 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001484 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001485 DSLParameter x(kFloat_Type, "x");
1486 DSLParameter y(kFloat_Type, "y");
Ethan Nicholas80f62352021-04-09 12:25:03 -04001487 DSLFunction pair(kFloat2_Type, "pair", x, y);
1488 pair.define(
1489 Return(Float2(x, y))
1490 );
1491 Var varArg1(kFloat_Type, "varArg1");
1492 Var varArg2(kFloat_Type, "varArg2");
1493 DSLWriter::MarkDeclared(varArg1);
1494 DSLWriter::MarkDeclared(varArg2);
1495 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1496 }
1497
1498 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001499 ExpectError error(r, "expected 'float', but found 'bool'");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001500 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001501 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001502 Return(true)
1503 );
1504 }
1505
1506 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001507 ExpectError error(r, "expected function to return 'float'");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001508 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001509 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001510 Return()
1511 );
1512 }
1513
1514 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001515 ExpectError error(r, "function 'broken' can exit without returning a value");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001516 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001517 Var x(kFloat_Type, "x", 0);
1518 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001519 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001520 If(x == 1, Return(x))
1521 );
1522 }
1523
1524 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001525 ExpectError error(r, "may not return a value from a void function");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001526 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001527 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001528 Return(0)
1529 );
1530 }
1531
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001532 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001533 ExpectError error(r, "function 'broken' can exit without returning a value");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001534 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001535 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001536 );
1537 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001538
1539 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001540 ExpectError error(r, "parameter has already been used in another function");
Ethan Nicholas961d9442021-03-16 16:37:29 -04001541 DSLWriter::Reset();
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001542 DSLParameter p(kFloat_Type);
1543 DSLFunction(kVoid_Type, "ok", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001544 );
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001545 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001546 );
1547 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001548}
1549
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001550DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1551 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001552 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001553 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001554 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001555
1556 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001557 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001558
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001559 Statement z = StaticIf(a > b, a -= b, b -= a);
1560 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1561
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001562 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001563 ExpectError error(r, "expected 'bool', but found 'float'");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001564 If(a + b, a -= b).release();
1565 }
1566}
1567
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001568DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInterfaceBlock, r, ctxInfo) {
1569 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001570 DSLGlobalVar intf = InterfaceBlock(kUniform_Modifier, "InterfaceBlock1",
1571 { Field(kFloat_Type, "a"), Field(kInt_Type, "b") });
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001572 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1573 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1574 "uniform InterfaceBlock1 { float a; int b; };");
1575 EXPECT_EQUAL(intf.field("a"), "InterfaceBlock1.a");
1576
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001577 DSLGlobalVar intf2 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock2",
1578 { Field(kFloat2_Type, "x"), Field(kHalf2x2_Type, "y") },
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001579 "blockVar");
1580 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1581 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1582 "uniform InterfaceBlock2 { float2 x; half2x2 y; } blockVar;");
1583 EXPECT_EQUAL(intf2.field("x"), "blockVar.x");
1584
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001585 DSLGlobalVar intf3 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock3",
1586 { Field(kFloat_Type, "z") },"arrayVar", 4);
Ethan Nicholase6ed3c22021-07-08 10:38:43 -04001587 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1588 EXPECT_EQUAL(*DSLWriter::ProgramElements().back(),
1589 "uniform InterfaceBlock3 { float z; } arrayVar[4];");
1590 EXPECT_EQUAL(intf3[1].field("z"), "arrayVar[1].z");
1591}
1592
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001593DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1594 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1595
1596 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001597 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001598
1599 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001600 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001601}
1602
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001603DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1604 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001605 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001606 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001607 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001608
1609 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001610 ExpectError error(r, "expected 'bool', but found 'int'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001611 Select(a, 1, -1).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001612 }
1613
1614 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001615 ExpectError error(r, "ternary operator result mismatch: 'float2', 'float3'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001616 Select(a > 0, Float2(1), Float3(1)).release();
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001617 }
1618}
1619
Ethan Nicholascfefec02021-02-09 15:22:57 -05001620DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1621 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1622
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001623 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001624
Ethan Nicholas722cb672021-05-06 10:47:06 -04001625 SkTArray<DSLStatement> caseStatements;
1626 caseStatements.push_back(a = 1);
1627 caseStatements.push_back(Continue());
John Stilesf3a28db2021-03-10 23:00:47 -05001628 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001629 Case(0, a = 0, Break()),
Ethan Nicholas722cb672021-05-06 10:47:06 -04001630 Case(1, std::move(caseStatements)),
John Stilese1d1b082021-02-23 13:44:36 -05001631 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001632 Default(Discard())
1633 );
John Stilese1d1b082021-02-23 13:44:36 -05001634 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001635 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001636 case 0: (a = 0.0); break;
1637 case 1: (a = 1.0); continue;
1638 case 2: (a = 2.0);
1639 default: discard;
1640 }
1641 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001642
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001643 Statement y = StaticSwitch(b,
1644 Case(0, a = 0, Break()),
1645 Case(1, a = 1, Continue()),
1646 Case(2, a = 2 /*Fallthrough*/),
1647 Default(Discard())
1648 );
1649 EXPECT_EQUAL(y, R"(
1650 @switch (b) {
1651 case 0: (a = 0.0); break;
1652 case 1: (a = 1.0); continue;
1653 case 2: (a = 2.0);
1654 default: discard;
1655 }
1656 )");
1657
John Stiles642cde22021-02-23 14:57:01 -05001658 EXPECT_EQUAL(Switch(b),
1659 "switch (b) {}");
1660
1661 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1662 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001663
1664 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001665 ExpectError error(r, "duplicate case value '0'");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001666 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001667 }
1668
1669 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001670 ExpectError error(r, "duplicate default case");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001671 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001672 }
1673
1674 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001675 ExpectError error(r, "case value must be a constant integer");
John Stiles628777c2021-08-04 22:07:41 -04001676 Var c(kInt_Type);
1677 DSLStatement(Switch(0, Case(c))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001678 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001679}
1680
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001681DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001682 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001683 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001684
John Stilesf04e09c2021-03-05 13:13:14 -05001685 EXPECT_EQUAL(a.x(),
1686 "a.x");
1687 EXPECT_EQUAL(a.y(),
1688 "a.y");
1689 EXPECT_EQUAL(a.z(),
1690 "a.z");
1691 EXPECT_EQUAL(a.w(),
1692 "a.w");
1693 EXPECT_EQUAL(a.r(),
1694 "a.x");
1695 EXPECT_EQUAL(a.g(),
1696 "a.y");
1697 EXPECT_EQUAL(a.b(),
1698 "a.z");
1699 EXPECT_EQUAL(a.a(),
1700 "a.w");
1701 EXPECT_EQUAL(Swizzle(a, R),
1702 "a.x");
1703 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1704 "float2(0.0, a.y)");
1705 EXPECT_EQUAL(Swizzle(a, B, G, G),
1706 "a.zyy");
1707 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1708 "float4(a.xyz, 1.0)");
1709 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1710 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001711}
1712
John Stiles08771b02021-04-26 09:35:10 -04001713
1714DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001715 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
John Stiles08771b02021-04-26 09:35:10 -04001716
1717 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1718 Var a;
1719 if (true)
1720 {
1721 Var(kInt_Type, "a").swap(a);
1722 }
1723
1724 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1725 "{ int a; (a = 123); }");
1726}
1727
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001728DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1729 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1730 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001731 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001732
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001733 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001734 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001735 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001736
1737 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001738 ExpectError error(r, "expected 'bool', but found 'int'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001739 While(7, Block()).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001740 }
1741}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001742
1743DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1744 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001745 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001746
1747 EXPECT_EQUAL(a[0], "a[0]");
1748 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001749
1750 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001751 ExpectError error(r, "expected 'int', but found 'bool'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001752 a[true].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001753 }
1754
1755 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001756 ExpectError error(r, "expected array, but found 'int'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001757 b[0].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001758 }
1759
1760 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001761 ExpectError error(r, "index -1 out of range for 'int[5]'");
Ethan Nicholas549c6b82021-06-25 12:31:44 -04001762 a[-1].release();
Ethan Nicholas04be3392021-01-26 10:07:01 -05001763 }
1764}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001765
1766DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1767 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1768 // There is a Fract type on Mac which can conflict with our Fract builtin
1769 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001770 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1771 Var h3(kHalf3_Type, "h3");
1772 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001773 EXPECT_EQUAL(Abs(a), "abs(a)");
1774 EXPECT_EQUAL(All(b4), "all(b4)");
1775 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001776 EXPECT_EQUAL(Atan(a), "atan(a)");
1777 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001778 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1779 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1780 EXPECT_EQUAL(Cos(a), "cos(a)");
1781 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1782 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1783 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1784 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1785 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1786 EXPECT_EQUAL(Exp(a), "exp(a)");
1787 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1788 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1789 EXPECT_EQUAL(Floor(a), "floor(a)");
1790 EXPECT_EQUAL(Fract(a), "fract(a)");
1791 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1792 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1793 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1794 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1795 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1796 EXPECT_EQUAL(Length(a), "length(a)");
1797 EXPECT_EQUAL(Log(a), "log(a)");
1798 EXPECT_EQUAL(Log2(a), "log2(a)");
1799 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1800 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1801 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1802 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1803 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1804 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1805 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1806 EXPECT_EQUAL(Radians(a), "radians(a)");
1807 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1808 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
John Stiles25339532021-09-08 17:50:09 -04001809 EXPECT_EQUAL(Round(a), "round(a)");
John Stilesb4d7b582021-02-19 09:56:31 -05001810 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1811 EXPECT_EQUAL(Sign(a), "sign(a)");
1812 EXPECT_EQUAL(Sin(a), "sin(a)");
1813 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1814 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1815 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1816 EXPECT_EQUAL(Tan(a), "tan(a)");
1817 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001818
1819 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1820 // one of them reports errors correctly
1821 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001822 ExpectError error(r, "no match for ceil(bool)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001823 Ceil(a == b).release();
1824 }
1825}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001826
1827DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001828 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001829
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001830 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001831 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001832 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001833
1834 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1835 // context, so we can't as yet Declare() variables with these modifiers.
1836 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001837 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001838 REPORTER_ASSERT(r, v2.modifiers().flags() == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001839 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001840
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001841 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001842 REPORTER_ASSERT(r, v3.modifiers().flags() == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001843 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001844
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001845 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001846 REPORTER_ASSERT(r, v4.modifiers().flags() == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001847 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001848
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001849 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001850 REPORTER_ASSERT(r, v5.modifiers().flags() == SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001851 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001852
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001853 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001854 REPORTER_ASSERT(r, v6.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1855 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001856 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001857
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001858 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001859 REPORTER_ASSERT(r, v7.modifiers().flags() == (SkSL::Modifiers::kIn_Flag |
1860 SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001861 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001862
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001863 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001864 REPORTER_ASSERT(r, v8.modifiers().flags() == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001865 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001866 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001867}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001868
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001869DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001870 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001871 Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6)
1872 .inputAttachmentIndex(7),
1873 kConst_Modifier), kInt_Type, "v1", 0);
1874 EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, "
1875 "builtin = 6, input_attachment_index = 7) const int v1 = 0;");
1876
1877 Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2");
1878 EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;");
1879
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001880 Var v4(DSLLayout().pushConstant(), kBool_Type, "v4");
1881 EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;");
1882
1883 Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5");
1884 EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;");
1885
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001886 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001887 ExpectError error(r, "'srgb_unpremul' is only permitted in runtime effects");
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04001888 DSLGlobalVar v(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kHalf4_Type,
1889 "v");
1890 Declare(v);
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04001891 }
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001892
1893 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001894 ExpectError error(r, "layout qualifier 'location' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001895 DSLLayout().location(1).location(2);
1896 }
1897
1898 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001899 ExpectError error(r, "layout qualifier 'set' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001900 DSLLayout().set(1).set(2);
1901 }
1902
1903 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001904 ExpectError error(r, "layout qualifier 'binding' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001905 DSLLayout().binding(1).binding(2);
1906 }
1907
1908 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001909 ExpectError error(r, "layout qualifier 'offset' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001910 DSLLayout().offset(1).offset(2);
1911 }
1912
1913 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001914 ExpectError error(r, "layout qualifier 'index' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001915 DSLLayout().index(1).index(2);
1916 }
1917
1918 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001919 ExpectError error(r, "layout qualifier 'builtin' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001920 DSLLayout().builtin(1).builtin(2);
1921 }
1922
1923 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001924 ExpectError error(r, "layout qualifier 'input_attachment_index' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001925 DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2);
1926 }
1927
1928 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001929 ExpectError error(r, "layout qualifier 'origin_upper_left' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001930 DSLLayout().originUpperLeft().originUpperLeft();
1931 }
1932
1933 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001934 ExpectError error(r, "layout qualifier 'push_constant' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001935 DSLLayout().pushConstant().pushConstant();
1936 }
1937
1938 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001939 ExpectError error(r, "layout qualifier 'blend_support_all_equations' appears more than "
1940 "once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001941 DSLLayout().blendSupportAllEquations().blendSupportAllEquations();
1942 }
1943
1944 {
Ethan Nicholas4a5e22a2021-08-13 17:29:51 -04001945 ExpectError error(r, "layout qualifier 'srgb_unpremul' appears more than once");
Ethan Nicholasb22fcaf2021-05-10 16:17:22 -04001946 DSLLayout().srgbUnpremul().srgbUnpremul();
1947 }
1948}
1949
Ethan Nicholas624a5292021-04-16 14:54:43 -04001950DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001951 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(),
Brian Osman552fcb92021-04-28 17:41:57 -04001952 SkSL::ProgramKind::kRuntimeShader);
Brian Osmanf8a55042021-08-23 16:24:18 -04001953 DSLGlobalVar shader(kUniform_Modifier, kShader_Type, "child");
Brian Osmane76530d2021-09-09 14:31:31 -04001954 DSLGlobalVar notShader(kUniform_Modifier, kFloat_Type, "x");
1955 EXPECT_EQUAL(shader.eval(Float2(0, 0)), "child.eval(float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001956
1957 {
Brian Osmane76530d2021-09-09 14:31:31 -04001958 ExpectError error(r, "no match for shader::eval(half4)");
1959 shader.eval(Half4(1)).release();
1960 }
1961
1962 {
1963 ExpectError error(r, "type does not support method calls");
1964 notShader.eval(Half4(1)).release();
Ethan Nicholas624a5292021-04-16 14:54:43 -04001965 }
1966}
1967
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001968DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas55a63af2021-05-18 10:12:58 -04001969 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001970
1971 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001972 Field(kFloat_Type, "x"),
1973 Field(kBool_Type, "b"),
1974 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001975 );
1976 DSLVar result(simpleStruct, "result");
1977 DSLFunction(simpleStruct, "returnStruct").define(
1978 Declare(result),
1979 result.field("x") = 123,
1980 result.field("b") = result.field("x") > 0,
1981 result.field("a")[0] = result.field("x"),
1982 Return(result)
1983 );
1984 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001985 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1986 "struct SimpleStruct { float x; bool b; float[3] a; };");
1987 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1988 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1989 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001990
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001991 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001992 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001993 Field(simpleStruct, "simple")
1994 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001995 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1996 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001997 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001998}
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04001999
2000DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) {
2001 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
2002 std::vector<Wrapper<DSLExpression>> exprs;
2003 exprs.push_back(DSLExpression(1));
2004 exprs.emplace_back(2.0);
2005 EXPECT_EQUAL(std::move(*exprs[0]), "1");
2006 EXPECT_EQUAL(std::move(*exprs[1]), "2.0");
2007
2008 std::vector<Wrapper<DSLVar>> vars;
2009 vars.emplace_back(DSLVar(kInt_Type, "x"));
Ethan Nicholasb4f8b7a2021-06-23 10:27:09 -04002010 REPORTER_ASSERT(r, DSLWriter::Var(*vars[0])->name() == "x");
Ethan Nicholasa1a0b922021-05-04 12:22:02 -04002011}
Ethan Nicholasebc9fad2021-07-09 15:35:23 -04002012
2013DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLRTAdjust, r, ctxInfo) {
Ethan Nicholas494eb3e2021-08-27 19:17:01 -04002014 {
2015 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
2016 SkSL::ProgramKind::kVertex);
2017 DSLGlobalVar rtAdjust(kUniform_Modifier, kFloat4_Type, "sk_RTAdjust");
2018 Declare(rtAdjust);
2019 DSLFunction(kVoid_Type, "main").define(
2020 sk_Position() = Half4(0)
2021 );
2022 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
2023 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
2024 "void main() {"
2025 "(sk_PerVertex.sk_Position = float4(0.0));"
2026 "(sk_PerVertex.sk_Position = float4(((sk_PerVertex.sk_Position.xy * sk_RTAdjust.xz) + "
2027 "(sk_PerVertex.sk_Position.ww * sk_RTAdjust.yw)), 0.0, sk_PerVertex.sk_Position.w));"
2028 "}");
2029 }
2030
2031 {
2032 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
2033 SkSL::ProgramKind::kVertex);
2034 REPORTER_ASSERT(r, !DSLWriter::IRGenerator().haveRTAdjustInterfaceBlock());
2035
2036 DSLGlobalVar intf = InterfaceBlock(kUniform_Modifier, "uniforms",
2037 { Field(kInt_Type, "unused"),
2038 Field(kFloat4_Type, "sk_RTAdjust") });
2039 REPORTER_ASSERT(r, DSLWriter::IRGenerator().haveRTAdjustInterfaceBlock());
2040 REPORTER_ASSERT(r, DSLWriter::IRGenerator().getRTAdjustFieldIndex() == 1);
2041 }
2042
2043 {
2044 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
2045 SkSL::ProgramKind::kVertex);
2046 ExpectError error(r, "sk_RTAdjust must have type 'float4'");
2047 InterfaceBlock(kUniform_Modifier, "uniforms",
2048 { Field(kInt_Type, "unused"), Field(kHalf4_Type, "sk_RTAdjust") });
2049 }
2050
2051 {
2052 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(),
2053 SkSL::ProgramKind::kVertex);
2054 ExpectError error(r, "symbol 'sk_RTAdjust' was already defined");
2055 InterfaceBlock(kUniform_Modifier, "uniforms1",
2056 { Field(kInt_Type, "unused1"), Field(kFloat4_Type, "sk_RTAdjust") });
2057 InterfaceBlock(kUniform_Modifier, "uniforms2",
2058 { Field(kInt_Type, "unused2"), Field(kFloat4_Type, "sk_RTAdjust") });
2059 }
Ethan Nicholasebc9fad2021-07-09 15:35:23 -04002060}
Ethan Nicholas292a09d2021-07-14 09:52:16 -04002061
2062DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInlining, r, ctxInfo) {
2063 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
Ethan Nicholasa2d22b22021-07-15 10:35:54 -04002064 DSLParameter x(kFloat_Type, "x");
Ethan Nicholas292a09d2021-07-14 09:52:16 -04002065 DSLFunction sqr(kFloat_Type, "sqr", x);
2066 sqr.define(
2067 Return(x * x)
2068 );
2069 DSLFunction(kVoid_Type, "main").define(
2070 sk_FragColor() = (sqr(2), Half4(sqr(3)))
2071 );
Ethan Nicholasb18c1e22021-07-16 09:53:53 -04002072 const char* source = "source test";
2073 std::unique_ptr<SkSL::Program> program = ReleaseProgram(std::make_unique<SkSL::String>(source));
Ethan Nicholas292a09d2021-07-14 09:52:16 -04002074 EXPECT_EQUAL(*program,
2075 "layout(location = 0, index = 0, builtin = 10001) out half4 sk_FragColor;"
2076 "layout(builtin = 17)in bool sk_Clockwise;"
2077 "void main() {"
2078 "/* inlined: sqr */;"
2079 "/* inlined: sqr */;"
2080 "(sk_FragColor = (4.0 , half4(half(9.0))));"
2081 "}");
Ethan Nicholasb18c1e22021-07-16 09:53:53 -04002082 REPORTER_ASSERT(r, *program->fSource == source);
Ethan Nicholas292a09d2021-07-14 09:52:16 -04002083}
Ethan Nicholas459777a2021-07-16 11:16:27 -04002084
2085DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReleaseUnused, r, ctxInfo) {
2086 SkSL::ProgramSettings settings = default_settings();
2087 settings.fAssertDSLObjectsReleased = false;
2088 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings);
2089 If(Sqrt(1) > 0, Discard());
2090 // Ensure that we can safely destroy statements and expressions despite being unused while
2091 // settings.fAssertDSLObjectsReleased is disabled.
2092}
Ethan Nicholas57709e12021-08-10 15:02:51 -04002093
2094DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPrototypes, r, ctxInfo) {
2095 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared());
2096 {
2097 DSLParameter x(kFloat_Type, "x");
2098 DSLFunction sqr(kFloat_Type, "sqr", x);
2099 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
2100 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);");
2101 sqr.define(
2102 Return(x * x)
2103 );
2104 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
2105 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
2106 }
2107
2108 {
2109 DSLWriter::Reset();
2110 DSLParameter x(kFloat_Type, "x");
2111 DSLFunction sqr(kFloat_Type, "sqr", x);
2112 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
2113 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);");
2114 DSLFunction(kVoid_Type, "main").define(sqr(5));
2115 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
2116 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);");
2117 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "void main() { sqr(5.0); }");
2118 sqr.define(
2119 Return(x * x)
2120 );
2121 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
2122 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2], "float sqr(float x) { return (x * x); }");
2123
2124 const char* source = "source test";
2125 std::unique_ptr<SkSL::Program> p = ReleaseProgram(std::make_unique<SkSL::String>(source));
2126 EXPECT_EQUAL(*p,
2127 "layout (builtin = 17) in bool sk_Clockwise;"
2128 "float sqr(float x);"
2129 "void main() {"
2130 "/* inlined: sqr */;"
2131 "25.0;"
2132 "}");
2133 }
2134}
Ethan Nicholas642215e2021-09-03 11:10:54 -04002135
2136DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLExtension, r, ctxInfo) {
2137 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
2138 AddExtension("test_extension");
2139 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
2140 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "#extension test_extension : enable");
2141}
Ethan Nicholas678ec712021-09-03 06:33:47 -04002142
2143DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiersDeclaration, r, ctxInfo) {
2144 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
2145 Declare(Modifiers(Layout().blendSupportAllEquations(), kOut_Modifier));
2146 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
2147 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "layout(blend_support_all_equations) out;");
2148}