blob: 75f194f56297ea337715fe783f8cc83dca8850ca [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 Nicholas961d9442021-03-16 16:37:29 -040021/**
22 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
23 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
24 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
25 * errors, especially given that some of the variables have options that make them an error to
26 * actually declare.
27 */
Ethan Nicholas95046142021-01-07 10:57:27 -050028class AutoDSLContext {
29public:
Ethan Nicholasee49efc2021-04-09 15:33:53 -040030 AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true,
31 SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) {
32 Start(gpu->shaderCompiler(), kind);
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050033 DSLWriter::Instance().fMangle = false;
Ethan Nicholas961d9442021-03-16 16:37:29 -040034 DSLWriter::Instance().fMarkVarsDeclared = markVarsDeclared;
Ethan Nicholas95046142021-01-07 10:57:27 -050035 }
36
37 ~AutoDSLContext() {
38 End();
39 }
40};
41
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050042class ExpectError : public ErrorHandler {
43public:
44 ExpectError(skiatest::Reporter* reporter, const char* msg)
45 : fMsg(msg)
46 , fReporter(reporter) {
47 SetErrorHandler(this);
48 }
49
50 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050051 REPORTER_ASSERT(fReporter, !fMsg,
52 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050053 SetErrorHandler(nullptr);
54 }
55
Ethan Nicholasb9563042021-02-25 09:45:49 -050056 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050057 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
58 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
59 fMsg = nullptr;
60 }
61
62private:
63 const char* fMsg;
64 skiatest::Reporter* fReporter;
65};
66
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050067static bool whitespace_insensitive_compare(const char* a, const char* b) {
68 for (;;) {
69 while (isspace(*a)) {
70 ++a;
71 }
72 while (isspace(*b)) {
73 ++b;
74 }
75 if (*a != *b) {
76 return false;
77 }
78 if (*a == 0) {
79 return true;
80 }
81 ++a;
82 ++b;
83 }
84}
85
Ethan Nicholas24c17722021-03-09 13:10:59 -050086// for use from SkSLDSLOnlyTest.cpp
87void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
88 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
89}
90
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050091DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050092 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
93 Expression e1 = 1;
94 REPORTER_ASSERT(r, e1.release()->description() == "1");
95 Expression e2 = 1.0;
96 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
97 Expression e3 = true;
98 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040099 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -0500100 Expression e4 = a;
101 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500102
103 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
104 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
105 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
106 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
107 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
108 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
109 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
110 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500111}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500112
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500113static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
114static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
115static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
116static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500117static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
118
119template <typename T>
120static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
121 SkSL::String actual = stringize(input);
122 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
123 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
124 lineNumber, expected, actual.c_str());
125 }
126}
127
128template <typename T>
129static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
130 // This overload allows temporary values to be passed to expect_equal.
131 return expect_equal(r, lineNumber, dsl, expected);
132}
133
134#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
135
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500136DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
137 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
138 Expression e1 = Float(std::numeric_limits<float>::max());
139 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
140 std::numeric_limits<float>::max());
141
142 Expression e2 = Float(std::numeric_limits<float>::min());
143 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
144 std::numeric_limits<float>::min());
145
John Stilesb4d7b582021-02-19 09:56:31 -0500146 EXPECT_EQUAL(Float2(0),
147 "float2(0.0)");
148 EXPECT_EQUAL(Float2(-0.5, 1),
149 "float2(-0.5, 1.0)");
150 EXPECT_EQUAL(Float3(0.75),
151 "float3(0.75)");
152 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500153 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500154 EXPECT_EQUAL(Float3(0, 1, 2),
155 "float3(0.0, 1.0, 2.0)");
156 EXPECT_EQUAL(Float4(0),
157 "float4(0.0)");
158 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500159 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500160 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500161 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500162 EXPECT_EQUAL(Float4(0, 1, 2, 3),
163 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500164
165 {
166 ExpectError error(r, "error: floating point value is infinite\n");
167 Float(std::numeric_limits<float>::infinity()).release();
168 }
169
170 {
171 ExpectError error(r, "error: floating point value is NaN\n");
172 Float(std::numeric_limits<float>::quiet_NaN()).release();
173 }
174
175 {
176 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
177 " but found 4)\n");
178 Float2(Float4(1)).release();
179 }
180
181 {
182 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
183 " but found 3)\n");
184 Float4(Float3(1)).release();
185 }
186}
187
188DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
189 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
190 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500191 REPORTER_ASSERT(r,
192 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500193
194 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500195 REPORTER_ASSERT(r,
196 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500197
John Stilesb9e4f642021-03-05 09:11:38 -0500198 EXPECT_EQUAL(Half2(0),
199 "half2(0.0)");
200 EXPECT_EQUAL(Half2(-0.5, 1),
201 "half2(-0.5, 1.0)");
202 EXPECT_EQUAL(Half3(0.75),
203 "half3(0.75)");
204 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
205 "half3(0.0, 1.0, -2.0)");
206 EXPECT_EQUAL(Half3(0, 1, 2),
207 "half3(0.0, 1.0, 2.0)");
208 EXPECT_EQUAL(Half4(0),
209 "half4(0.0)");
210 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
211 "half4(0.0, 1.0, 2.0, 3.0)");
212 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
213 "half4(0.0, 1.0, 2.0, 3.0)");
214 EXPECT_EQUAL(Half4(0, 1, 2, 3),
215 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500216
217 {
218 ExpectError error(r, "error: floating point value is infinite\n");
219 Half(std::numeric_limits<float>::infinity()).release();
220 }
221
222 {
223 ExpectError error(r, "error: floating point value is NaN\n");
224 Half(std::numeric_limits<float>::quiet_NaN()).release();
225 }
226
227 {
228 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
229 " but found 4)\n");
230 Half2(Half4(1)).release();
231 }
232
233 {
234 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
235 " but found 3)\n");
236 Half4(Half3(1)).release();
237 }
238}
239
240DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
241 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500242
John Stilesb9e4f642021-03-05 09:11:38 -0500243 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
244 "2147483647");
245 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
246 "int2(-2147483648)");
247 EXPECT_EQUAL(Int2(0, 1),
248 "int2(0, 1)");
249 EXPECT_EQUAL(Int3(0),
250 "int3(0)");
251 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
252 "int3(0, 1, -2)");
253 EXPECT_EQUAL(Int3(0, 1, 2),
254 "int3(0, 1, 2)");
255 EXPECT_EQUAL(Int4(0),
256 "int4(0)");
257 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
258 "int4(0, 1, 2, 3)");
259 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
260 "int4(0, 1, 2, 3)");
261 EXPECT_EQUAL(Int4(0, 1, 2, 3),
262 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500263
264 {
265 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
266 " but found 4)\n");
267 Int2(Int4(1)).release();
268 }
269
270 {
271 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
272 " but found 3)\n");
273 Int4(Int3(1)).release();
274 }
275}
276
277DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
278 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500279
John Stilesb9e4f642021-03-05 09:11:38 -0500280 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
281 "32767");
282 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
283 "short2(-32768)");
284 EXPECT_EQUAL(Short2(0, 1),
285 "short2(0, 1)");
286 EXPECT_EQUAL(Short3(0),
287 "short3(0)");
288 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
289 "short3(0, 1, -2)");
290 EXPECT_EQUAL(Short3(0, 1, 2),
291 "short3(0, 1, 2)");
292 EXPECT_EQUAL(Short4(0),
293 "short4(0)");
294 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
295 "short4(0, 1, 2, 3)");
296 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
297 "short4(0, 1, 2, 3)");
298 EXPECT_EQUAL(Short4(0, 1, 2, 3),
299 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500300
301 {
302 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
303 " but found 4)\n");
304 Short2(Short4(1)).release();
305 }
306
307 {
308 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
309 " but found 3)\n");
310 Short4(Short3(1)).release();
311 }
312}
313
314DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
315 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500316
John Stilesb9e4f642021-03-05 09:11:38 -0500317 EXPECT_EQUAL(Bool2(false),
318 "bool2(false)");
319 EXPECT_EQUAL(Bool2(false, true),
320 "bool2(false, true)");
321 EXPECT_EQUAL(Bool3(false),
322 "bool3(false)");
323 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
324 "bool3(false, true, false)");
325 EXPECT_EQUAL(Bool3(false, true, false),
326 "bool3(false, true, false)");
327 EXPECT_EQUAL(Bool4(false),
328 "bool4(false)");
329 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
330 "bool4(false, true, false, true)");
331 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
332 "bool4(false, true, false, true)");
333 EXPECT_EQUAL(Bool4(false, true, false, true),
334 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500335
336 {
337 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
338 " but found 4)\n");
339 Bool2(Bool4(true)).release();
340 }
341
342 {
343 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
344 " but found 3)\n");
345 Bool4(Bool3(true)).release();
346 }
347}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500348
Ethan Nicholas84558932021-04-12 16:56:37 -0400349DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
350 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
351 Var f22(kFloat2x2_Type, "f22");
352 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
353 Var f32(kFloat3x2_Type, "f32");
354 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
355 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
356 Var f42(kFloat4x2_Type, "f42");
357 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
358 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
359 Var f23(kFloat2x3_Type, "f23");
360 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
361 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
362 Var f33(kFloat3x3_Type, "f33");
363 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
364 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
365 Var f43(kFloat4x3_Type, "f43");
366 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
367 "(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))");
368 Var f24(kFloat2x4_Type, "f24");
369 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
370 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
371 Var f34(kFloat3x4_Type, "f34");
372 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
373 "(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))");
374 Var f44(kFloat4x4_Type, "f44");
375 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
376
377 Var h22(kHalf2x2_Type, "h22");
378 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
379 Var h32(kHalf3x2_Type, "h32");
380 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
381 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
382 Var h42(kHalf4x2_Type, "h42");
383 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
384 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
385 Var h23(kHalf2x3_Type, "h23");
386 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
387 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
388 Var h33(kHalf3x3_Type, "h33");
389 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
390 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
391 Var h43(kHalf4x3_Type, "h43");
392 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
393 "(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))");
394 Var h24(kHalf2x4_Type, "h24");
395 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
396 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
397 Var h34(kHalf3x4_Type, "h34");
398 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
399 "(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))");
400 Var h44(kHalf4x4_Type, "h44");
401 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
402
403 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
404 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
405 EXPECT_EQUAL(h42[0][1], "h42[0].y");
406 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
407 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
408 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
409
410 {
411 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
412 "scalars, but found 2)\n");
413 DSLExpression(Float3x3(Float2(1))).release();
414 }
415
416 {
417 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
418 "scalars, but found 5)\n");
419 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
420 }
421
422 {
423 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
424 DSLExpression(f43 * Float3(1)).release();
425 }
426
427 {
428 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
429 "'float3x3'\n");
430 DSLExpression(f43 = f33).release();
431 }
432
433 {
434 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
435 "'float2x2'\n");
436 DSLExpression(h22 = f22).release();
437 }
438
439 {
440 ExpectError error(r,
441 "error: no match for inverse(float4x3)\n");
442 DSLExpression(Inverse(f43)).release();
443 }
444}
445
Ethan Nicholas92969f22021-01-13 10:38:59 -0500446DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
447 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400448 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500449
John Stiles8f440b42021-03-05 16:48:56 -0500450 EXPECT_EQUAL(a + b,
451 "(a + b)");
452 EXPECT_EQUAL(a + 1,
453 "(a + 1.0)");
454 EXPECT_EQUAL(0.5 + a + -99,
455 "((0.5 + a) + -99.0)");
456 EXPECT_EQUAL(a += b + 1,
457 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500458 EXPECT_EQUAL(+a,
459 "a");
460 EXPECT_EQUAL(+(a + b),
461 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500462
463 {
464 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500465 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500466 }
467
468 {
469 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500470 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500471 }
472
473 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500474 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500475 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500476 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500477
478 {
479 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400480 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500481 DSLExpression(+c);
482 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500483}
484
485DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
486 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400487 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500488
John Stiles8f440b42021-03-05 16:48:56 -0500489 EXPECT_EQUAL(a - b,
490 "(a - b)");
491 EXPECT_EQUAL(a - 1,
492 "(a - 1)");
493 EXPECT_EQUAL(2 - a - b,
494 "((2 - a) - b)");
495 EXPECT_EQUAL(a -= b + 1,
496 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500497 EXPECT_EQUAL(-a,
498 "-a");
499 EXPECT_EQUAL(-(a - b),
500 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500501
502 {
503 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500504 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500505 }
506
507 {
508 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500509 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500510 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500511
512 {
513 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500514 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500515 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500516
517 {
518 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400519 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500520 DSLExpression(-c);
521 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500522}
523
524DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
525 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400526 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500527
John Stiles8f440b42021-03-05 16:48:56 -0500528 EXPECT_EQUAL(a * b,
529 "(a * b)");
530 EXPECT_EQUAL(a * 2,
531 "(a * 2.0)");
532 EXPECT_EQUAL(0.5 * a * -99,
533 "((0.5 * a) * -99.0)");
534 EXPECT_EQUAL(a *= b + 1,
535 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500536
537 {
538 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500539 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500540 }
541
542 {
543 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500544 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500545 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500546
547 {
548 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500549 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500550 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500551}
552
553DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
554 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400555 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500556
John Stiles8f440b42021-03-05 16:48:56 -0500557 EXPECT_EQUAL(a / b,
558 "(a / b)");
559 EXPECT_EQUAL(a / 2,
560 "(a / 2.0)");
561 EXPECT_EQUAL(0.5 / a / -99,
562 "((0.5 / a) / -99.0)");
563 EXPECT_EQUAL(b / (a - 1),
564 "(b / (a - 1.0))");
565 EXPECT_EQUAL(a /= b + 1,
566 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500567
568 {
569 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500570 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500571 }
572
573 {
574 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500575 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500576 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500577
578 {
579 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500580 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500581 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500582
583 {
584 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500585 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500586 }
587
588 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400589 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500590 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500591 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500592 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500593}
594
595DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
596 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400597 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500598 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500599 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500600
601 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500602 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500603
604 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500605 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500606
607 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500608 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500609
610 {
611 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500612 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500613 }
614
615 {
616 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500617 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500618 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500619
620 {
621 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500622 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500623 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500624
625 {
626 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500627 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500628 }
629
630 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400631 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500632 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500633 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500634 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500635}
636
637DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
638 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400639 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500640 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500641 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500642
643 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500644 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500645
646 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500647 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500648
649 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500650 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500651
652 {
653 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500654 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500655 }
656
657 {
658 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500659 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500660 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500661
662 {
663 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500664 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500665 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500666}
667
668DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
669 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400670 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500671 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500672 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500673
674 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500675 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500676
677 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500678 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500679
680 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500681 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500682
683 {
684 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500685 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500686 }
687
688 {
689 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500690 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500691 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500692
693 {
694 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500695 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500696 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500697}
698
699DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
700 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400701 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500702 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500703 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500704
705 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500706 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500707
708 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500709 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500710
711 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500712 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500713
714 {
715 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500716 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500717 }
718
719 {
720 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500721 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500722 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500723
724 {
725 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500726 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500727 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500728}
729
730DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
731 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400732 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500733 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500734 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500735
736 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500737 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500738
739 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500740 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500741
742 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500743 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500744
745 {
746 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500747 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500748 }
749
750 {
751 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500752 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500753 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500754
755 {
756 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500757 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500758 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500759}
760
761DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
762 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400763 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500764 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500765 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500766
767 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500768 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500769
770 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500771 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500772
773 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500774 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500775
776 {
777 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500778 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500779 }
780
781 {
782 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500783 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500784 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500785
786 {
787 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500788 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500789 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500790}
791
792DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
793 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400794 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500795 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500796 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500797
798 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500799 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500800
801 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500802 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500803
804 {
805 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500806 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500807 }
808}
809
810DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
811 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400812 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500813 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500814 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500815
816 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500817 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500818
819 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500820 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500821
822 {
823 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500824 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500825 }
826}
827
828DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
829 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400830 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500831 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -0500832 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833
834 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -0500835 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500836}
837
838DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
839 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400840 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500841 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -0500842 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500843
844 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500845 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500846
847 {
848 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500849 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500850 }
851}
852
853DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
854 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400855 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500856 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -0500857 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500858
859 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500860 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500861
862 {
863 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500864 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500865 }
866}
867
868DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
869 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400870 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -0500872 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500873
874 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500875 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500876
877 {
878 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500879 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500880 }
881}
882
883DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
884 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400885 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500886 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500887 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500888
889 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500890 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500891
892 {
893 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500894 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500895 }
896}
897
898DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
899 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400900 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500901 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -0500902 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500903
904 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500905 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500906
907 {
908 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500909 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500910 }
911}
912
913DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
914 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400915 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500916 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500917 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500918
919 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500920 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500921
922 {
923 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500924 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500925 }
926}
927
928DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
929 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400930 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500931 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -0500932 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500933
934 {
935 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500936 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500937 }
938}
939
940DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
941 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400942 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500943 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -0500944 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500945
946 {
947 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500948 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500949 }
950}
951
952DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
953 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400954 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500955 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -0500956 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500957
958 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -0500959 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500960
961 {
962 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500963 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500964 }
965
966 {
967 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500968 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500969 }
970
971 {
972 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500973 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500974 }
975
976 {
977 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500978 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500979 }
980}
981
982DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
983 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400984 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500985 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -0500986 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500987
988 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -0500989 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500990
991 {
992 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500993 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500994 }
995
996 {
997 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500998 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500999 }
1000
1001 {
1002 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001003 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001004 }
1005
1006 {
1007 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001008 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001009 }
1010}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001011
1012DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001013 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001014 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -05001015 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001016 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001017 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -05001018 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001019}
1020
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001021DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001022 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001023 Var i(kInt_Type, "i", 0);
1024 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001025 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001026 If(i > 5, Break())
1027 ))
1028 );
1029 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001030 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1031 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001032
1033 {
1034 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001035 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001036 Break()
1037 );
1038 }
1039}
1040
1041DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001042 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001043 Var i(kInt_Type, "i", 0);
1044 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001045 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001046 If(i < 5, Continue())
1047 ))
1048 );
1049 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001050 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1051 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001052
1053 {
1054 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001055 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001056 Continue()
1057 );
1058 }
1059}
1060
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001061DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001062 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001063 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001064 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001065 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001066 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001067 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001068
1069 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001070 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001071 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001072 Declare(c).release();
1073 }
1074
1075 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001076 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001077 Declare(d).release();
1078 ExpectError error(r, "error: variable has already been declared\n");
1079 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001080 }
1081}
1082
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001083DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1084 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1085 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001086 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001087}
1088
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001089DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1090 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1091 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001092 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001093
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001094 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001095 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001096 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001097
1098 {
1099 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1100 Do(Block(), 7).release();
1101 }
1102}
1103
1104DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001105 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001106 Statement x = For(Statement(), Expression(), Expression(), Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001107 EXPECT_EQUAL(x, "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001108
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001109 Var i(kInt_Type, "i", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001110 Statement y = For(Declare(i), i < 10, ++i, i += 5);
John Stilesb4d7b582021-02-19 09:56:31 -05001111 EXPECT_EQUAL(y, "for (int i = 0; (i < 10); ++i) (i += 5);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001112
1113 {
1114 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1115 For(i = 0, i + 10, ++i, i += 5).release();
1116 }
1117}
1118
Ethan Nicholase2c05042021-02-03 10:27:22 -05001119DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001120 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001121 Var coords(kHalf2_Type, "coords");
1122 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001123 sk_FragColor() = Half4(coords, 0, 1)
1124 );
1125 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001126 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1127 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001128
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001129 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001130 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001131 Var x(kFloat_Type, "x");
1132 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001133 sqr.define(
1134 Return(x * x)
1135 );
1136 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1137 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1138 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1139 }
1140
1141 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001142 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001143 Var x(kFloat2_Type, "x");
1144 Var y(kFloat2_Type, "y");
1145 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001146 dot.define(
1147 Return(x * x + y * y)
1148 );
1149 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1150 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1151 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1152 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1153 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1154 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001155
1156 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001157 DSLWriter::Reset();
1158 Var x(kFloat_Type, "x");
1159 Var y(kFloat_Type, "y");
1160 DSLFunction pair(kFloat2_Type, "pair", x, y);
1161 pair.define(
1162 Return(Float2(x, y))
1163 );
1164 Var varArg1(kFloat_Type, "varArg1");
1165 Var varArg2(kFloat_Type, "varArg2");
1166 DSLWriter::MarkDeclared(varArg1);
1167 DSLWriter::MarkDeclared(varArg2);
1168 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1169 }
1170
1171 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001172 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001173 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001174 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001175 Return(true)
1176 );
1177 }
1178
1179 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001180 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001181 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001182 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001183 Return()
1184 );
1185 }
1186
1187 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001188 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001189 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001190 Var x(kFloat_Type, "x", 0);
1191 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001192 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001193 If(x == 1, Return(x))
1194 );
1195 }
1196
1197 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001198 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001199 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001200 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001201 Return(0)
1202 );
1203 }
1204
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001205 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001206 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001207 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001208 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001209 );
1210 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001211
1212 {
1213 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1214 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001215 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001216 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001217 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001218 );
1219 }
1220
1221 {
1222 ExpectError error(r, "error: variable has already been declared\n");
1223 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001224 DSLVar p(kFloat_Type);
1225 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001226 );
1227 Declare(p).release();
1228 }
1229
1230 {
1231 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1232 "values\n");
1233 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001234 DSLVar p(kFloat_Type, 1);
1235 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001236 );
1237 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001238}
1239
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001240DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1241 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001242 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001243 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001244 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001245
1246 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001247 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001248
1249 {
1250 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1251 If(a + b, a -= b).release();
1252 }
1253}
1254
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001255DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1256 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1257
1258 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001259 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001260
1261 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001262 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001263}
1264
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001265DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1266 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001267 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001268 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001269 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001270
1271 {
1272 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001273 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001274 }
1275
1276 {
1277 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001278 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001279 }
1280}
1281
Ethan Nicholascfefec02021-02-09 15:22:57 -05001282DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1283 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1284
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001285 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001286
John Stilesf3a28db2021-03-10 23:00:47 -05001287 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001288 Case(0, a = 0, Break()),
1289 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001290 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001291 Default(Discard())
1292 );
John Stilese1d1b082021-02-23 13:44:36 -05001293 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001294 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001295 case 0: (a = 0.0); break;
1296 case 1: (a = 1.0); continue;
1297 case 2: (a = 2.0);
1298 default: discard;
1299 }
1300 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001301
John Stiles642cde22021-02-23 14:57:01 -05001302 EXPECT_EQUAL(Switch(b),
1303 "switch (b) {}");
1304
1305 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1306 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001307
1308 {
John Stilese1d1b082021-02-23 13:44:36 -05001309 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001310 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001311 }
1312
1313 {
John Stilese1d1b082021-02-23 13:44:36 -05001314 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001315 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001316 }
1317
1318 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001319 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001320 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001321 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001322 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001323}
1324
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001325DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1326 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001327 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001328
John Stilesf04e09c2021-03-05 13:13:14 -05001329 EXPECT_EQUAL(a.x(),
1330 "a.x");
1331 EXPECT_EQUAL(a.y(),
1332 "a.y");
1333 EXPECT_EQUAL(a.z(),
1334 "a.z");
1335 EXPECT_EQUAL(a.w(),
1336 "a.w");
1337 EXPECT_EQUAL(a.r(),
1338 "a.x");
1339 EXPECT_EQUAL(a.g(),
1340 "a.y");
1341 EXPECT_EQUAL(a.b(),
1342 "a.z");
1343 EXPECT_EQUAL(a.a(),
1344 "a.w");
1345 EXPECT_EQUAL(Swizzle(a, R),
1346 "a.x");
1347 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1348 "float2(0.0, a.y)");
1349 EXPECT_EQUAL(Swizzle(a, B, G, G),
1350 "a.zyy");
1351 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1352 "float4(a.xyz, 1.0)");
1353 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1354 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001355}
1356
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001357DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1358 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1359 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001360 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001361
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001362 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001363 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001364 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001365
1366 {
1367 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001368 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001369 }
1370}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001371
1372DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1373 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001374 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001375
1376 EXPECT_EQUAL(a[0], "a[0]");
1377 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001378
1379 {
1380 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001381 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001382 }
1383
1384 {
1385 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001386 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001387 }
1388
1389 {
1390 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001391 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001392 }
1393}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001394
1395DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1396 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1397 // There is a Fract type on Mac which can conflict with our Fract builtin
1398 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001399 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1400 Var h3(kHalf3_Type, "h3");
1401 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001402 EXPECT_EQUAL(Abs(a), "abs(a)");
1403 EXPECT_EQUAL(All(b4), "all(b4)");
1404 EXPECT_EQUAL(Any(b4), "any(b4)");
1405 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1406 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1407 EXPECT_EQUAL(Cos(a), "cos(a)");
1408 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1409 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1410 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1411 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1412 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1413 EXPECT_EQUAL(Exp(a), "exp(a)");
1414 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1415 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1416 EXPECT_EQUAL(Floor(a), "floor(a)");
1417 EXPECT_EQUAL(Fract(a), "fract(a)");
1418 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1419 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1420 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1421 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1422 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1423 EXPECT_EQUAL(Length(a), "length(a)");
1424 EXPECT_EQUAL(Log(a), "log(a)");
1425 EXPECT_EQUAL(Log2(a), "log2(a)");
1426 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1427 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1428 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1429 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1430 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1431 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1432 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1433 EXPECT_EQUAL(Radians(a), "radians(a)");
1434 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1435 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1436 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1437 EXPECT_EQUAL(Sign(a), "sign(a)");
1438 EXPECT_EQUAL(Sin(a), "sin(a)");
1439 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1440 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1441 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1442 EXPECT_EQUAL(Tan(a), "tan(a)");
1443 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001444
1445 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1446 // one of them reports errors correctly
1447 {
1448 ExpectError error(r, "error: no match for ceil(bool)\n");
1449 Ceil(a == b).release();
1450 }
1451}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001452
1453DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001454 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001455
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001456 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001457 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001458 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001459
1460 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1461 // context, so we can't as yet Declare() variables with these modifiers.
1462 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001463 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001464 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001465 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001466
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001467 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001468 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001469 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001470
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001471 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001472 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001473 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001474
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001475 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001476 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001477 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001478 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001479
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001480 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001481 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1482 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001483 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001484
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001485 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001486 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1487 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001488 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001489
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001490 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001491 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001492 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001493}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001494
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001495DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSample, r, ctxInfo) {
1496 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1497 SkSL::ProgramKind::kFragmentProcessor);
1498 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1499 EXPECT_EQUAL(Sample(child), "sample(child)");
1500 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
Ethan Nicholas84558932021-04-12 16:56:37 -04001501 EXPECT_EQUAL(Sample(child, Float3x3(1.0)), "sample(child, float3x3(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001502 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001503 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
1504 EXPECT_EQUAL(Sample(child, Float3x3(1.0), Half4(1)),
1505 "sample(child, float3x3(1.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001506
1507 {
1508 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1509 Sample(child, true).release();
1510 }
1511}
1512
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001513DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001514 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001515
1516 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001517 Field(kFloat_Type, "x"),
1518 Field(kBool_Type, "b"),
1519 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001520 );
1521 DSLVar result(simpleStruct, "result");
1522 DSLFunction(simpleStruct, "returnStruct").define(
1523 Declare(result),
1524 result.field("x") = 123,
1525 result.field("b") = result.field("x") > 0,
1526 result.field("a")[0] = result.field("x"),
1527 Return(result)
1528 );
1529 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001530 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1531 "struct SimpleStruct { float x; bool b; float[3] a; };");
1532 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1533 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1534 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001535
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001536 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001537 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001538 Field(simpleStruct, "simple")
1539 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001540 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1541 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001542 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001543}