blob: 6b837adff379ebd0a8fd698e93850c69f74db386 [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 Nicholasdb2326b2021-04-19 10:55:18 -04001019
1020 Statement z = (If(a > 0, --a), ++b);
1021 EXPECT_EQUAL(z, "if ((a > 0)) --a; ++b;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001022}
1023
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001024DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001025 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001026 Var i(kInt_Type, "i", 0);
1027 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001028 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001029 If(i > 5, Break())
1030 ))
1031 );
1032 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001033 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1034 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001035
1036 {
1037 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001038 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001039 Break()
1040 );
1041 }
1042}
1043
1044DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001045 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001046 Var i(kInt_Type, "i", 0);
1047 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001048 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001049 If(i < 5, Continue())
1050 ))
1051 );
1052 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001053 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1054 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001055
1056 {
1057 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001058 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001059 Continue()
1060 );
1061 }
1062}
1063
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001064DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001065 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001066 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001067 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001068 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001069 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001070 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001071
1072 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001073 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001074 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001075 Declare(c).release();
1076 }
1077
1078 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001079 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001080 Declare(d).release();
1081 ExpectError error(r, "error: variable has already been declared\n");
1082 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001083 }
1084}
1085
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001086DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1087 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1088 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001089 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001090}
1091
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001092DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1093 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1094 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001095 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001096
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001097 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001098 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001099 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001100
1101 {
1102 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1103 Do(Block(), 7).release();
1104 }
1105}
1106
1107DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001108 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001109 Statement x = For(Statement(), Expression(), Expression(), Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001110 EXPECT_EQUAL(x, "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001111
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001112 Var i(kInt_Type, "i", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001113 Statement y = For(Declare(i), i < 10, ++i, i += 5);
John Stilesb4d7b582021-02-19 09:56:31 -05001114 EXPECT_EQUAL(y, "for (int i = 0; (i < 10); ++i) (i += 5);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001115
1116 {
1117 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1118 For(i = 0, i + 10, ++i, i += 5).release();
1119 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001120
1121 {
1122 ExpectError error(r, "error: invalid for loop initializer\n");
1123 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1124 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001125}
1126
Ethan Nicholase2c05042021-02-03 10:27:22 -05001127DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001128 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001129 Var coords(kHalf2_Type, "coords");
1130 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001131 sk_FragColor() = Half4(coords, 0, 1)
1132 );
1133 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001134 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1135 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001136
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001137 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001138 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001139 Var x(kFloat_Type, "x");
1140 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001141 sqr.define(
1142 Return(x * x)
1143 );
1144 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1145 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1146 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1147 }
1148
1149 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001150 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001151 Var x(kFloat2_Type, "x");
1152 Var y(kFloat2_Type, "y");
1153 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001154 dot.define(
1155 Return(x * x + y * y)
1156 );
1157 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1158 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1159 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1160 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1161 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1162 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001163
1164 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001165 DSLWriter::Reset();
1166 Var x(kFloat_Type, "x");
1167 Var y(kFloat_Type, "y");
1168 DSLFunction pair(kFloat2_Type, "pair", x, y);
1169 pair.define(
1170 Return(Float2(x, y))
1171 );
1172 Var varArg1(kFloat_Type, "varArg1");
1173 Var varArg2(kFloat_Type, "varArg2");
1174 DSLWriter::MarkDeclared(varArg1);
1175 DSLWriter::MarkDeclared(varArg2);
1176 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1177 }
1178
1179 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001180 ExpectError error(r, "error: expected 'float', but found 'bool'\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(true)
1184 );
1185 }
1186
1187 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001188 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001189 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001190 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001191 Return()
1192 );
1193 }
1194
1195 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001196 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001197 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001198 Var x(kFloat_Type, "x", 0);
1199 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001200 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001201 If(x == 1, Return(x))
1202 );
1203 }
1204
1205 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001206 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001207 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001208 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001209 Return(0)
1210 );
1211 }
1212
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001213 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001214 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001215 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001216 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001217 );
1218 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001219
1220 {
1221 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1222 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001223 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001224 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001225 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001226 );
1227 }
1228
1229 {
1230 ExpectError error(r, "error: variable has already been declared\n");
1231 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001232 DSLVar p(kFloat_Type);
1233 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001234 );
1235 Declare(p).release();
1236 }
1237
1238 {
1239 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1240 "values\n");
1241 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001242 DSLVar p(kFloat_Type, 1);
1243 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001244 );
1245 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001246}
1247
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001248DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1249 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001250 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001251 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001252 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001253
1254 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001255 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001256
1257 {
1258 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1259 If(a + b, a -= b).release();
1260 }
1261}
1262
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001263DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1264 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1265
1266 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001267 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001268
1269 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001270 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001271}
1272
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001273DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1274 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001275 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001276 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001277 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001278
1279 {
1280 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001281 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001282 }
1283
1284 {
1285 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001286 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001287 }
1288}
1289
Ethan Nicholascfefec02021-02-09 15:22:57 -05001290DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1291 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1292
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001293 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001294
John Stilesf3a28db2021-03-10 23:00:47 -05001295 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001296 Case(0, a = 0, Break()),
1297 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001298 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001299 Default(Discard())
1300 );
John Stilese1d1b082021-02-23 13:44:36 -05001301 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001302 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001303 case 0: (a = 0.0); break;
1304 case 1: (a = 1.0); continue;
1305 case 2: (a = 2.0);
1306 default: discard;
1307 }
1308 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001309
John Stiles642cde22021-02-23 14:57:01 -05001310 EXPECT_EQUAL(Switch(b),
1311 "switch (b) {}");
1312
1313 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1314 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001315
1316 {
John Stilese1d1b082021-02-23 13:44:36 -05001317 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001318 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001319 }
1320
1321 {
John Stilese1d1b082021-02-23 13:44:36 -05001322 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001323 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001324 }
1325
1326 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001327 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001328 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001329 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001330 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001331}
1332
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001333DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1334 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001335 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001336
John Stilesf04e09c2021-03-05 13:13:14 -05001337 EXPECT_EQUAL(a.x(),
1338 "a.x");
1339 EXPECT_EQUAL(a.y(),
1340 "a.y");
1341 EXPECT_EQUAL(a.z(),
1342 "a.z");
1343 EXPECT_EQUAL(a.w(),
1344 "a.w");
1345 EXPECT_EQUAL(a.r(),
1346 "a.x");
1347 EXPECT_EQUAL(a.g(),
1348 "a.y");
1349 EXPECT_EQUAL(a.b(),
1350 "a.z");
1351 EXPECT_EQUAL(a.a(),
1352 "a.w");
1353 EXPECT_EQUAL(Swizzle(a, R),
1354 "a.x");
1355 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1356 "float2(0.0, a.y)");
1357 EXPECT_EQUAL(Swizzle(a, B, G, G),
1358 "a.zyy");
1359 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1360 "float4(a.xyz, 1.0)");
1361 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1362 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001363}
1364
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001365DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1366 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1367 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001368 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001369
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001370 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001371 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001372 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001373
1374 {
1375 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001376 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001377 }
1378}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001379
1380DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1381 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001382 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001383
1384 EXPECT_EQUAL(a[0], "a[0]");
1385 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001386
1387 {
1388 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001389 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001390 }
1391
1392 {
1393 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001394 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001395 }
1396
1397 {
1398 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001399 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001400 }
1401}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001402
1403DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1404 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1405 // There is a Fract type on Mac which can conflict with our Fract builtin
1406 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001407 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1408 Var h3(kHalf3_Type, "h3");
1409 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001410 EXPECT_EQUAL(Abs(a), "abs(a)");
1411 EXPECT_EQUAL(All(b4), "all(b4)");
1412 EXPECT_EQUAL(Any(b4), "any(b4)");
1413 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1414 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1415 EXPECT_EQUAL(Cos(a), "cos(a)");
1416 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1417 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1418 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1419 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1420 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1421 EXPECT_EQUAL(Exp(a), "exp(a)");
1422 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1423 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1424 EXPECT_EQUAL(Floor(a), "floor(a)");
1425 EXPECT_EQUAL(Fract(a), "fract(a)");
1426 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1427 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1428 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1429 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1430 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1431 EXPECT_EQUAL(Length(a), "length(a)");
1432 EXPECT_EQUAL(Log(a), "log(a)");
1433 EXPECT_EQUAL(Log2(a), "log2(a)");
1434 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1435 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1436 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1437 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1438 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1439 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1440 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1441 EXPECT_EQUAL(Radians(a), "radians(a)");
1442 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1443 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1444 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1445 EXPECT_EQUAL(Sign(a), "sign(a)");
1446 EXPECT_EQUAL(Sin(a), "sin(a)");
1447 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1448 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1449 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1450 EXPECT_EQUAL(Tan(a), "tan(a)");
1451 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001452
1453 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1454 // one of them reports errors correctly
1455 {
1456 ExpectError error(r, "error: no match for ceil(bool)\n");
1457 Ceil(a == b).release();
1458 }
1459}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001460
1461DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001462 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001463
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001464 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001465 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001466 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001467
1468 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1469 // context, so we can't as yet Declare() variables with these modifiers.
1470 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001471 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001472 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001473 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001474
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001475 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001476 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001477 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001478
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001479 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001480 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001481 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001482
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001483 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001484 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001485 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001486 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001487
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001488 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001489 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1490 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001491 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001492
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001493 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001494 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1495 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001496 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001497
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001498 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001499 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001500 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001501}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001502
Ethan Nicholas624a5292021-04-16 14:54:43 -04001503DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001504 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1505 SkSL::ProgramKind::kFragmentProcessor);
1506 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1507 EXPECT_EQUAL(Sample(child), "sample(child)");
1508 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
Ethan Nicholas84558932021-04-12 16:56:37 -04001509 EXPECT_EQUAL(Sample(child, Float3x3(1.0)), "sample(child, float3x3(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001510 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001511 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
1512 EXPECT_EQUAL(Sample(child, Float3x3(1.0), Half4(1)),
1513 "sample(child, float3x3(1.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001514
1515 {
1516 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1517 Sample(child, true).release();
1518 }
1519}
1520
Ethan Nicholas624a5292021-04-16 14:54:43 -04001521DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
1522 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1523 SkSL::ProgramKind::kRuntimeEffect);
1524 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
1525 EXPECT_EQUAL(Sample(shader), "sample(shader)");
1526 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
1527 EXPECT_EQUAL(Sample(shader, Float3x3(1)), "sample(shader, float3x3(1.0))");
1528
1529 {
1530 ExpectError error(r, "error: no match for sample(shader, half4)\n");
1531 Sample(shader, Half4(1)).release();
1532 }
1533}
1534
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001535DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001536 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001537
1538 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001539 Field(kFloat_Type, "x"),
1540 Field(kBool_Type, "b"),
1541 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001542 );
1543 DSLVar result(simpleStruct, "result");
1544 DSLFunction(simpleStruct, "returnStruct").define(
1545 Declare(result),
1546 result.field("x") = 123,
1547 result.field("b") = result.field("x") > 0,
1548 result.field("a")[0] = result.field("x"),
1549 Return(result)
1550 );
1551 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001552 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1553 "struct SimpleStruct { float x; bool b; float[3] a; };");
1554 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1555 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1556 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001557
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001558 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001559 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001560 Field(simpleStruct, "simple")
1561 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001562 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1563 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001564 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001565}