blob: 7faf2cc8bb11255c80c74ba6356e1db9654d4c43 [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
Ethan Nicholasa60cc3e2021-04-23 16:15:11 -0400165 DSLVar x(kFloat_Type, "x");
166 EXPECT_EQUAL(x = 1.0, "(x = 1.0)");
167 EXPECT_EQUAL(x = 1.0f, "(x = 1.0)");
168
169 DSLVar y(kFloat2_Type, "y");
170 EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)");
171 EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)");
172
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500173 {
174 ExpectError error(r, "error: floating point value is infinite\n");
175 Float(std::numeric_limits<float>::infinity()).release();
176 }
177
178 {
179 ExpectError error(r, "error: floating point value is NaN\n");
180 Float(std::numeric_limits<float>::quiet_NaN()).release();
181 }
182
183 {
184 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
185 " but found 4)\n");
186 Float2(Float4(1)).release();
187 }
188
189 {
190 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
191 " but found 3)\n");
192 Float4(Float3(1)).release();
193 }
194}
195
196DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
197 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
198 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500199 REPORTER_ASSERT(r,
200 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500201
202 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500203 REPORTER_ASSERT(r,
204 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500205
John Stilesb9e4f642021-03-05 09:11:38 -0500206 EXPECT_EQUAL(Half2(0),
207 "half2(0.0)");
208 EXPECT_EQUAL(Half2(-0.5, 1),
209 "half2(-0.5, 1.0)");
210 EXPECT_EQUAL(Half3(0.75),
211 "half3(0.75)");
212 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
213 "half3(0.0, 1.0, -2.0)");
214 EXPECT_EQUAL(Half3(0, 1, 2),
215 "half3(0.0, 1.0, 2.0)");
216 EXPECT_EQUAL(Half4(0),
217 "half4(0.0)");
218 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
219 "half4(0.0, 1.0, 2.0, 3.0)");
220 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
221 "half4(0.0, 1.0, 2.0, 3.0)");
222 EXPECT_EQUAL(Half4(0, 1, 2, 3),
223 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500224
225 {
226 ExpectError error(r, "error: floating point value is infinite\n");
227 Half(std::numeric_limits<float>::infinity()).release();
228 }
229
230 {
231 ExpectError error(r, "error: floating point value is NaN\n");
232 Half(std::numeric_limits<float>::quiet_NaN()).release();
233 }
234
235 {
236 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
237 " but found 4)\n");
238 Half2(Half4(1)).release();
239 }
240
241 {
242 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
243 " but found 3)\n");
244 Half4(Half3(1)).release();
245 }
246}
247
248DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
249 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500250
John Stilesb9e4f642021-03-05 09:11:38 -0500251 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
252 "2147483647");
253 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
254 "int2(-2147483648)");
255 EXPECT_EQUAL(Int2(0, 1),
256 "int2(0, 1)");
257 EXPECT_EQUAL(Int3(0),
258 "int3(0)");
259 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
260 "int3(0, 1, -2)");
261 EXPECT_EQUAL(Int3(0, 1, 2),
262 "int3(0, 1, 2)");
263 EXPECT_EQUAL(Int4(0),
264 "int4(0)");
265 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
266 "int4(0, 1, 2, 3)");
267 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
268 "int4(0, 1, 2, 3)");
269 EXPECT_EQUAL(Int4(0, 1, 2, 3),
270 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500271
272 {
273 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
274 " but found 4)\n");
275 Int2(Int4(1)).release();
276 }
277
278 {
279 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
280 " but found 3)\n");
281 Int4(Int3(1)).release();
282 }
283}
284
285DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
286 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500287
John Stilesb9e4f642021-03-05 09:11:38 -0500288 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
289 "32767");
290 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
291 "short2(-32768)");
292 EXPECT_EQUAL(Short2(0, 1),
293 "short2(0, 1)");
294 EXPECT_EQUAL(Short3(0),
295 "short3(0)");
296 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
297 "short3(0, 1, -2)");
298 EXPECT_EQUAL(Short3(0, 1, 2),
299 "short3(0, 1, 2)");
300 EXPECT_EQUAL(Short4(0),
301 "short4(0)");
302 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
303 "short4(0, 1, 2, 3)");
304 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
305 "short4(0, 1, 2, 3)");
306 EXPECT_EQUAL(Short4(0, 1, 2, 3),
307 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500308
309 {
310 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
311 " but found 4)\n");
312 Short2(Short4(1)).release();
313 }
314
315 {
316 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
317 " but found 3)\n");
318 Short4(Short3(1)).release();
319 }
320}
321
322DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
323 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500324
John Stilesb9e4f642021-03-05 09:11:38 -0500325 EXPECT_EQUAL(Bool2(false),
326 "bool2(false)");
327 EXPECT_EQUAL(Bool2(false, true),
328 "bool2(false, true)");
329 EXPECT_EQUAL(Bool3(false),
330 "bool3(false)");
331 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
332 "bool3(false, true, false)");
333 EXPECT_EQUAL(Bool3(false, true, false),
334 "bool3(false, true, false)");
335 EXPECT_EQUAL(Bool4(false),
336 "bool4(false)");
337 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
338 "bool4(false, true, false, true)");
339 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
340 "bool4(false, true, false, true)");
341 EXPECT_EQUAL(Bool4(false, true, false, true),
342 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500343
344 {
345 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
346 " but found 4)\n");
347 Bool2(Bool4(true)).release();
348 }
349
350 {
351 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
352 " but found 3)\n");
353 Bool4(Bool3(true)).release();
354 }
355}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500356
Ethan Nicholas84558932021-04-12 16:56:37 -0400357DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) {
358 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
359 Var f22(kFloat2x2_Type, "f22");
360 EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))");
361 Var f32(kFloat3x2_Type, "f32");
362 EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6),
363 "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
364 Var f42(kFloat4x2_Type, "f42");
365 EXPECT_EQUAL(f42 = Float4x2(Float4(1, 2, 3, 4), 5, 6, 7, 8),
366 "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
367 Var f23(kFloat2x3_Type, "f23");
368 EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)),
369 "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
370 Var f33(kFloat3x3_Type, "f33");
371 EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9),
372 "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
373 Var f43(kFloat4x3_Type, "f43");
374 EXPECT_EQUAL(f43 = Float4x3(Float4(1, 2, 3, 4), Float4(5, 6, 7, 8), Float4(9, 10, 11, 12)),
375 "(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))");
376 Var f24(kFloat2x4_Type, "f24");
377 EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8),
378 "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
379 Var f34(kFloat3x4_Type, "f34");
380 EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Float3(10, 11, 12)),
381 "(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))");
382 Var f44(kFloat4x4_Type, "f44");
383 EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))");
384
385 Var h22(kHalf2x2_Type, "h22");
386 EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))");
387 Var h32(kHalf3x2_Type, "h32");
388 EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6),
389 "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
390 Var h42(kHalf4x2_Type, "h42");
391 EXPECT_EQUAL(h42 = Half4x2(Half4(1, 2, 3, 4), 5, 6, 7, 8),
392 "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
393 Var h23(kHalf2x3_Type, "h23");
394 EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)),
395 "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))");
396 Var h33(kHalf3x3_Type, "h33");
397 EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9),
398 "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))");
399 Var h43(kHalf4x3_Type, "h43");
400 EXPECT_EQUAL(h43 = Half4x3(Half4(1, 2, 3, 4), Half4(5, 6, 7, 8), Half4(9, 10, 11, 12)),
401 "(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))");
402 Var h24(kHalf2x4_Type, "h24");
403 EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8),
404 "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))");
405 Var h34(kHalf3x4_Type, "h34");
406 EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, 9, Half3(10, 11, 12)),
407 "(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))");
408 Var h44(kHalf4x4_Type, "h44");
409 EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))");
410
411 EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)");
412 EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))");
413 EXPECT_EQUAL(h42[0][1], "h42[0].y");
414 EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))");
415 EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)");
416 EXPECT_EQUAL(Inverse(f44), "inverse(f44)");
417
418 {
419 ExpectError error(r, "error: invalid arguments to 'float3x3' constructor (expected 9 "
420 "scalars, but found 2)\n");
421 DSLExpression(Float3x3(Float2(1))).release();
422 }
423
424 {
425 ExpectError error(r, "error: invalid arguments to 'half2x2' constructor (expected 4 "
426 "scalars, but found 5)\n");
427 DSLExpression(Half2x2(1, 2, 3, 4, 5)).release();
428 }
429
430 {
431 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'float4x3', 'float3'\n");
432 DSLExpression(f43 * Float3(1)).release();
433 }
434
435 {
436 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'float4x3', "
437 "'float3x3'\n");
438 DSLExpression(f43 = f33).release();
439 }
440
441 {
442 ExpectError error(r, "error: type mismatch: '=' cannot operate on 'half2x2', "
443 "'float2x2'\n");
444 DSLExpression(h22 = f22).release();
445 }
446
447 {
448 ExpectError error(r,
449 "error: no match for inverse(float4x3)\n");
450 DSLExpression(Inverse(f43)).release();
451 }
452}
453
Ethan Nicholas92969f22021-01-13 10:38:59 -0500454DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
455 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400456 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500457
John Stiles8f440b42021-03-05 16:48:56 -0500458 EXPECT_EQUAL(a + b,
459 "(a + b)");
460 EXPECT_EQUAL(a + 1,
461 "(a + 1.0)");
462 EXPECT_EQUAL(0.5 + a + -99,
463 "((0.5 + a) + -99.0)");
464 EXPECT_EQUAL(a += b + 1,
465 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500466 EXPECT_EQUAL(+a,
467 "a");
468 EXPECT_EQUAL(+(a + b),
469 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500470
471 {
472 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500473 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500474 }
475
476 {
477 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500478 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500479 }
480
481 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500482 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500483 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500484 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500485
486 {
487 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400488 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500489 DSLExpression(+c);
490 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500491}
492
493DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
494 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400495 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500496
John Stiles8f440b42021-03-05 16:48:56 -0500497 EXPECT_EQUAL(a - b,
498 "(a - b)");
499 EXPECT_EQUAL(a - 1,
500 "(a - 1)");
501 EXPECT_EQUAL(2 - a - b,
502 "((2 - a) - b)");
503 EXPECT_EQUAL(a -= b + 1,
504 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500505 EXPECT_EQUAL(-a,
506 "-a");
507 EXPECT_EQUAL(-(a - b),
508 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500509
510 {
511 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500512 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500513 }
514
515 {
516 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500517 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500518 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500519
520 {
521 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500522 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500523 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500524
525 {
526 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400527 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500528 DSLExpression(-c);
529 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500530}
531
532DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
533 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400534 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500535
John Stiles8f440b42021-03-05 16:48:56 -0500536 EXPECT_EQUAL(a * b,
537 "(a * b)");
538 EXPECT_EQUAL(a * 2,
539 "(a * 2.0)");
540 EXPECT_EQUAL(0.5 * a * -99,
541 "((0.5 * a) * -99.0)");
542 EXPECT_EQUAL(a *= b + 1,
543 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500544
545 {
546 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500547 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500548 }
549
550 {
551 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500552 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500553 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500554
555 {
556 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500557 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500558 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500559}
560
561DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
562 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400563 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500564
John Stiles8f440b42021-03-05 16:48:56 -0500565 EXPECT_EQUAL(a / b,
566 "(a / b)");
567 EXPECT_EQUAL(a / 2,
568 "(a / 2.0)");
569 EXPECT_EQUAL(0.5 / a / -99,
570 "((0.5 / a) / -99.0)");
571 EXPECT_EQUAL(b / (a - 1),
572 "(b / (a - 1.0))");
573 EXPECT_EQUAL(a /= b + 1,
574 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500575
576 {
577 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500578 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500579 }
580
581 {
582 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500583 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500584 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500585
586 {
587 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500588 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500589 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500590
591 {
592 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500593 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500594 }
595
596 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400597 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500598 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500599 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500600 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500601}
602
603DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
604 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400605 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500606 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500607 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500608
609 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500610 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500611
612 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500613 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500614
615 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500616 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500617
618 {
619 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500620 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500621 }
622
623 {
624 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500625 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500626 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500627
628 {
629 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500630 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500631 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500632
633 {
634 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500635 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500636 }
637
638 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400639 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500640 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500641 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500642 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500643}
644
645DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
646 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400647 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500648 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500649 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500650
651 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500652 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500653
654 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500655 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500656
657 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500658 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500659
660 {
661 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500662 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500663 }
664
665 {
666 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500667 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500668 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500669
670 {
671 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500672 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500673 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674}
675
676DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
677 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400678 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500679 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500680 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500681
682 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500683 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500684
685 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500686 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500687
688 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500689 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500690
691 {
692 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500693 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500694 }
695
696 {
697 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500698 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500699 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500700
701 {
702 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500703 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500704 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500705}
706
707DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
708 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400709 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500710 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500711 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500712
713 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500714 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500715
716 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500717 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500718
719 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500720 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500721
722 {
723 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500724 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500725 }
726
727 {
728 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500729 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500730 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500731
732 {
733 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500734 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500735 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500736}
737
738DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
739 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400740 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500741 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500742 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500743
744 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500745 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500746
747 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500748 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500749
750 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500751 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500752
753 {
754 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500755 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500756 }
757
758 {
759 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500760 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500761 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500762
763 {
764 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500765 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500766 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500767}
768
769DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
770 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400771 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500772 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500773 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500774
775 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500776 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500777
778 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500779 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500780
781 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500782 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500783
784 {
785 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500786 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500787 }
788
789 {
790 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500791 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500792 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500793
794 {
795 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500796 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500797 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500798}
799
800DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
801 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400802 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500803 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500804 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500805
806 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500807 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500808
809 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500810 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500811
812 {
813 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500814 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500815 }
816}
817
818DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
819 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400820 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500821 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500822 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500823
824 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500825 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500826
827 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500828 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500829
830 {
831 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500832 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833 }
834}
835
836DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
837 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400838 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500839 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -0500840 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500841
842 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -0500843 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500844}
845
846DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
847 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400848 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500849 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -0500850 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851
852 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500853 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500854
855 {
856 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500857 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500858 }
859}
860
861DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
862 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400863 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500864 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -0500865 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866
867 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500868 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500869
870 {
871 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500872 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500873 }
874}
875
876DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
877 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400878 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500879 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -0500880 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500881
882 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500883 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500884
885 {
886 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500887 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500888 }
889}
890
891DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
892 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400893 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500894 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500895 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500896
897 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500898 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500899
900 {
901 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500902 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500903 }
904}
905
906DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
907 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400908 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500909 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -0500910 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500911
912 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500913 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500914
915 {
916 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500917 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500918 }
919}
920
921DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
922 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400923 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500924 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500925 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500926
927 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500928 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500929
930 {
931 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500932 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500933 }
934}
935
936DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
937 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400938 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500939 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -0500940 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500941
942 {
943 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500944 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500945 }
946}
947
948DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
949 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400950 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500951 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -0500952 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500953
954 {
955 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500956 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500957 }
958}
959
960DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
961 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400962 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500963 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -0500964 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500965
966 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -0500967 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500968
969 {
970 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500971 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500972 }
973
974 {
975 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500976 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500977 }
978
979 {
980 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500981 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500982 }
983
984 {
985 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500986 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500987 }
988}
989
990DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
991 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400992 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500993 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -0500994 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500995
996 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -0500997 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500998
999 {
1000 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001001 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001002 }
1003
1004 {
1005 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001006 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001007 }
1008
1009 {
1010 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001011 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001012 }
1013
1014 {
1015 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001016 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -05001017 }
1018}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001019
1020DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001021 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001022 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -05001023 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001024 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001025 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -05001026 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasdb2326b2021-04-19 10:55:18 -04001027
1028 Statement z = (If(a > 0, --a), ++b);
1029 EXPECT_EQUAL(z, "if ((a > 0)) --a; ++b;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001030}
1031
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001032DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001033 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001034 Var i(kInt_Type, "i", 0);
1035 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001036 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001037 If(i > 5, Break())
1038 ))
1039 );
1040 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001041 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1042 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001043
1044 {
1045 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001046 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001047 Break()
1048 );
1049 }
1050}
1051
1052DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001053 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001054 Var i(kInt_Type, "i", 0);
1055 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001056 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001057 If(i < 5, Continue())
1058 ))
1059 );
1060 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001061 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1062 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001063
1064 {
1065 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001066 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001067 Continue()
1068 );
1069 }
1070}
1071
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001072DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001073 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001074 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001075 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -05001076 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001077 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -05001078 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001079
1080 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001081 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001082 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001083 Declare(c).release();
1084 }
1085
1086 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001087 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001088 Declare(d).release();
1089 ExpectError error(r, "error: variable has already been declared\n");
1090 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001091 }
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001092
1093 {
1094 Var e(kUniform_Modifier, kInt_Type, "e");
1095 ExpectError error(r, "error: this variable must be declared with DeclareGlobal\n");
1096 Declare(e).release();
1097 }
1098}
1099
1100DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) {
1101 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
1102 Var x(kInt_Type, "x", 0);
1103 DeclareGlobal(x);
1104 Var y(kUniform_Modifier, kFloat2_Type, "y");
1105 DeclareGlobal(y);
1106 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
1107 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;");
1108 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001109}
1110
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001111DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
1112 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1113 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -05001114 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -05001115}
1116
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001117DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
1118 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1119 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -05001120 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001121
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001122 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001123 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -05001124 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001125
1126 {
1127 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1128 Do(Block(), 7).release();
1129 }
1130}
1131
1132DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001133 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
John Stiles8676ebe2021-04-20 15:30:41 -04001134 EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()),
1135 "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001136
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001137 Var i(kInt_Type, "i", 0);
John Stiles8676ebe2021-04-20 15:30:41 -04001138 EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5),
1139 "for (int i = 0; (i < 10); ++i) (i += 5);");
1140
1141 Var j(kInt_Type, "j", 0);
1142 Var k(kInt_Type, "k", 10);
1143 EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"(
1144 {
1145 int j = 0;
1146 int k = 10;
1147 for (; (j < k); ++j) {}
1148 }
1149 )");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001150
1151 {
1152 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1153 For(i = 0, i + 10, ++i, i += 5).release();
1154 }
Ethan Nicholasa0f76542021-04-16 16:02:18 -04001155
1156 {
1157 ExpectError error(r, "error: invalid for loop initializer\n");
1158 For(If(i == 0, i = 1), i < 10, ++i, i += 5).release();
1159 }
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001160}
1161
Ethan Nicholase2c05042021-02-03 10:27:22 -05001162DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001163 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001164 Var coords(kHalf2_Type, "coords");
1165 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001166 sk_FragColor() = Half4(coords, 0, 1)
1167 );
1168 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001169 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1170 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001171
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001172 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001173 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001174 Var x(kFloat_Type, "x");
1175 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001176 sqr.define(
1177 Return(x * x)
1178 );
1179 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1180 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1181 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1182 }
1183
1184 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001185 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001186 Var x(kFloat2_Type, "x");
1187 Var y(kFloat2_Type, "y");
1188 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001189 dot.define(
1190 Return(x * x + y * y)
1191 );
1192 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1193 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1194 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1195 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1196 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1197 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001198
1199 {
Ethan Nicholas80f62352021-04-09 12:25:03 -04001200 DSLWriter::Reset();
1201 Var x(kFloat_Type, "x");
1202 Var y(kFloat_Type, "y");
1203 DSLFunction pair(kFloat2_Type, "pair", x, y);
1204 pair.define(
1205 Return(Float2(x, y))
1206 );
1207 Var varArg1(kFloat_Type, "varArg1");
1208 Var varArg2(kFloat_Type, "varArg2");
1209 DSLWriter::MarkDeclared(varArg1);
1210 DSLWriter::MarkDeclared(varArg2);
1211 EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)");
1212 }
1213
1214 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001215 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001216 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001217 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001218 Return(true)
1219 );
1220 }
1221
1222 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001223 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001224 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001225 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001226 Return()
1227 );
1228 }
1229
1230 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001231 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001232 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001233 Var x(kFloat_Type, "x", 0);
1234 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001235 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001236 If(x == 1, Return(x))
1237 );
1238 }
1239
1240 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001241 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001242 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001243 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001244 Return(0)
1245 );
1246 }
1247
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001248 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001249 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001250 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001251 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001252 );
1253 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001254
1255 {
1256 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1257 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001258 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001259 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001260 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001261 );
1262 }
1263
1264 {
1265 ExpectError error(r, "error: variable has already been declared\n");
1266 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001267 DSLVar p(kFloat_Type);
1268 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001269 );
1270 Declare(p).release();
1271 }
1272
1273 {
1274 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1275 "values\n");
1276 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001277 DSLVar p(kFloat_Type, 1);
1278 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001279 );
1280 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001281}
1282
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001283DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1284 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001285 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001286 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001287 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001288
1289 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001290 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001291
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001292 Statement z = StaticIf(a > b, a -= b, b -= a);
1293 EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);");
1294
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001295 {
1296 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1297 If(a + b, a -= b).release();
1298 }
1299}
1300
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001301DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1302 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1303
1304 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001305 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001306
1307 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001308 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001309}
1310
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001311DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1312 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001313 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001314 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001315 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001316
1317 {
1318 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001319 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001320 }
1321
1322 {
1323 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001324 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001325 }
1326}
1327
Ethan Nicholascfefec02021-02-09 15:22:57 -05001328DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1329 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1330
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001331 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001332
John Stilesf3a28db2021-03-10 23:00:47 -05001333 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001334 Case(0, a = 0, Break()),
1335 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001336 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001337 Default(Discard())
1338 );
John Stilese1d1b082021-02-23 13:44:36 -05001339 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001340 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001341 case 0: (a = 0.0); break;
1342 case 1: (a = 1.0); continue;
1343 case 2: (a = 2.0);
1344 default: discard;
1345 }
1346 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001347
Ethan Nicholas8a6537d2021-04-30 12:44:00 -04001348 Statement y = StaticSwitch(b,
1349 Case(0, a = 0, Break()),
1350 Case(1, a = 1, Continue()),
1351 Case(2, a = 2 /*Fallthrough*/),
1352 Default(Discard())
1353 );
1354 EXPECT_EQUAL(y, R"(
1355 @switch (b) {
1356 case 0: (a = 0.0); break;
1357 case 1: (a = 1.0); continue;
1358 case 2: (a = 2.0);
1359 default: discard;
1360 }
1361 )");
1362
John Stiles642cde22021-02-23 14:57:01 -05001363 EXPECT_EQUAL(Switch(b),
1364 "switch (b) {}");
1365
1366 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1367 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001368
1369 {
John Stilese1d1b082021-02-23 13:44:36 -05001370 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001371 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001372 }
1373
1374 {
John Stilese1d1b082021-02-23 13:44:36 -05001375 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001376 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001377 }
1378
1379 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001380 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001381 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001382 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001383 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001384}
1385
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001386DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1387 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001388 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001389
John Stilesf04e09c2021-03-05 13:13:14 -05001390 EXPECT_EQUAL(a.x(),
1391 "a.x");
1392 EXPECT_EQUAL(a.y(),
1393 "a.y");
1394 EXPECT_EQUAL(a.z(),
1395 "a.z");
1396 EXPECT_EQUAL(a.w(),
1397 "a.w");
1398 EXPECT_EQUAL(a.r(),
1399 "a.x");
1400 EXPECT_EQUAL(a.g(),
1401 "a.y");
1402 EXPECT_EQUAL(a.b(),
1403 "a.z");
1404 EXPECT_EQUAL(a.a(),
1405 "a.w");
1406 EXPECT_EQUAL(Swizzle(a, R),
1407 "a.x");
1408 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1409 "float2(0.0, a.y)");
1410 EXPECT_EQUAL(Swizzle(a, B, G, G),
1411 "a.zyy");
1412 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1413 "float4(a.xyz, 1.0)");
1414 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1415 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001416}
1417
John Stiles08771b02021-04-26 09:35:10 -04001418
1419DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) {
1420 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
1421
1422 // We should be able to convert `a` into a proper var by swapping it, even from within a scope.
1423 Var a;
1424 if (true)
1425 {
1426 Var(kInt_Type, "a").swap(a);
1427 }
1428
1429 EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)),
1430 "{ int a; (a = 123); }");
1431}
1432
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001433DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1434 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1435 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001436 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001437
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001438 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001439 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001440 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001441
1442 {
1443 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001444 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001445 }
1446}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001447
1448DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1449 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001450 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001451
1452 EXPECT_EQUAL(a[0], "a[0]");
1453 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001454
1455 {
1456 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001457 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001458 }
1459
1460 {
1461 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001462 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001463 }
1464
1465 {
1466 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001467 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001468 }
1469}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001470
1471DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1472 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1473 // There is a Fract type on Mac which can conflict with our Fract builtin
1474 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001475 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1476 Var h3(kHalf3_Type, "h3");
1477 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001478 EXPECT_EQUAL(Abs(a), "abs(a)");
1479 EXPECT_EQUAL(All(b4), "all(b4)");
1480 EXPECT_EQUAL(Any(b4), "any(b4)");
John Stilese3fa7452021-04-26 09:36:07 -04001481 EXPECT_EQUAL(Atan(a), "atan(a)");
1482 EXPECT_EQUAL(Atan(a, b), "atan(a, b)");
John Stilesb4d7b582021-02-19 09:56:31 -05001483 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1484 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1485 EXPECT_EQUAL(Cos(a), "cos(a)");
1486 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1487 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1488 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1489 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1490 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1491 EXPECT_EQUAL(Exp(a), "exp(a)");
1492 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1493 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1494 EXPECT_EQUAL(Floor(a), "floor(a)");
1495 EXPECT_EQUAL(Fract(a), "fract(a)");
1496 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1497 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1498 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1499 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1500 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1501 EXPECT_EQUAL(Length(a), "length(a)");
1502 EXPECT_EQUAL(Log(a), "log(a)");
1503 EXPECT_EQUAL(Log2(a), "log2(a)");
1504 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1505 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1506 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1507 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1508 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1509 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1510 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1511 EXPECT_EQUAL(Radians(a), "radians(a)");
1512 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1513 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1514 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1515 EXPECT_EQUAL(Sign(a), "sign(a)");
1516 EXPECT_EQUAL(Sin(a), "sin(a)");
1517 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1518 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1519 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1520 EXPECT_EQUAL(Tan(a), "tan(a)");
1521 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001522
1523 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1524 // one of them reports errors correctly
1525 {
1526 ExpectError error(r, "error: no match for ceil(bool)\n");
1527 Ceil(a == b).release();
1528 }
1529}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001530
1531DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001532 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001533
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001534 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001535 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001536 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001537
1538 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1539 // context, so we can't as yet Declare() variables with these modifiers.
1540 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001541 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001542 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001543 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001544
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001545 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001546 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001547 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001548
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001549 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001550 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001551 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001552
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001553 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001554 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001555 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001556 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001557
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001558 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001559 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1560 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001561 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001562
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001563 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001564 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1565 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001566 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001567
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001568 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001569 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholase9c2c5a2021-04-30 13:14:24 -04001570 DSLWriter::MarkDeclared(v8);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001571 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001572}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001573
Ethan Nicholas624a5292021-04-16 14:54:43 -04001574DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleFragmentProcessor, r, ctxInfo) {
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001575 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
1576 SkSL::ProgramKind::kFragmentProcessor);
1577 DSLVar child(kUniform_Modifier, kFragmentProcessor_Type, "child");
1578 EXPECT_EQUAL(Sample(child), "sample(child)");
1579 EXPECT_EQUAL(Sample(child, Float2(0, 0)), "sample(child, float2(0.0, 0.0))");
1580 EXPECT_EQUAL(Sample(child, Half4(1)), "sample(child, half4(1.0))");
Brian Osmandebcbbf2021-04-13 09:50:27 -04001581 EXPECT_EQUAL(Sample(child, Float2(0), Half4(1)), "sample(child, float2(0.0), half4(1.0))");
Ethan Nicholasee49efc2021-04-09 15:33:53 -04001582
1583 {
1584 ExpectError error(r, "error: no match for sample(fragmentProcessor, bool)\n");
1585 Sample(child, true).release();
1586 }
1587}
1588
Ethan Nicholas624a5292021-04-16 14:54:43 -04001589DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) {
1590 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/true,
Brian Osman552fcb92021-04-28 17:41:57 -04001591 SkSL::ProgramKind::kRuntimeShader);
Ethan Nicholas624a5292021-04-16 14:54:43 -04001592 DSLVar shader(kUniform_Modifier, kShader_Type, "shader");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001593 EXPECT_EQUAL(Sample(shader, Float2(0, 0)), "sample(shader, float2(0.0, 0.0))");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001594
1595 {
Brian Osmanadadb952021-04-21 09:57:19 -04001596 ExpectError error(r, "error: no match for sample(shader, half4)\n");
Ethan Nicholas624a5292021-04-16 14:54:43 -04001597 Sample(shader, Half4(1)).release();
1598 }
1599}
1600
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001601DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001602 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001603
1604 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001605 Field(kFloat_Type, "x"),
1606 Field(kBool_Type, "b"),
1607 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001608 );
1609 DSLVar result(simpleStruct, "result");
1610 DSLFunction(simpleStruct, "returnStruct").define(
1611 Declare(result),
1612 result.field("x") = 123,
1613 result.field("b") = result.field("x") > 0,
1614 result.field("a")[0] = result.field("x"),
1615 Return(result)
1616 );
1617 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001618 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1619 "struct SimpleStruct { float x; bool b; float[3] a; };");
1620 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1621 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1622 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001623
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001624 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001625 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001626 Field(simpleStruct, "simple")
1627 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001628 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1629 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001630 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001631}