Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 1 | /* |
| 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 Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 8 | #include "include/private/SkSLIRNode.h" |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 9 | #include "include/sksl/DSL.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 10 | #include "src/gpu/GrDirectContextPriv.h" |
| 11 | #include "src/gpu/GrGpu.h" |
| 12 | #include "src/sksl/SkSLIRGenerator.h" |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 13 | #include "src/sksl/dsl/priv/DSLWriter.h" |
| 14 | |
| 15 | #include "tests/Test.h" |
| 16 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 17 | #include <limits> |
| 18 | |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 19 | using namespace SkSL::dsl; |
| 20 | |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 21 | SkSL::ProgramSettings default_settings() { |
| 22 | SkSL::ProgramSettings result; |
| 23 | result.fDSLMarkVarsDeclared = true; |
| 24 | result.fDSLMangling = false; |
| 25 | return result; |
| 26 | } |
| 27 | |
| 28 | SkSL::ProgramSettings no_mark_vars_declared() { |
| 29 | SkSL::ProgramSettings result = default_settings(); |
| 30 | result.fDSLMarkVarsDeclared = false; |
| 31 | return result; |
| 32 | } |
Ethan Nicholas | 22dcb73 | 2021-05-12 11:26:58 -0400 | [diff] [blame] | 33 | |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 34 | /** |
| 35 | * In addition to issuing an automatic Start() and End(), disables mangling and optionally |
| 36 | * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we |
| 37 | * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid |
| 38 | * errors, especially given that some of the variables have options that make them an error to |
| 39 | * actually declare. |
| 40 | */ |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 41 | class AutoDSLContext { |
| 42 | public: |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 43 | AutoDSLContext(GrGpu* gpu, SkSL::ProgramSettings settings = default_settings(), |
Ethan Nicholas | ee49efc | 2021-04-09 15:33:53 -0400 | [diff] [blame] | 44 | SkSL::ProgramKind kind = SkSL::ProgramKind::kFragment) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 45 | Start(gpu->shaderCompiler(), kind, settings); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | ~AutoDSLContext() { |
| 49 | End(); |
| 50 | } |
| 51 | }; |
| 52 | |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 53 | class ExpectError : public SkSL::ErrorReporter { |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 54 | public: |
| 55 | ExpectError(skiatest::Reporter* reporter, const char* msg) |
| 56 | : fMsg(msg) |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 57 | , fReporter(reporter) |
| 58 | , fOldReporter(&GetErrorReporter()) { |
| 59 | SetErrorReporter(this); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ~ExpectError() override { |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 63 | REPORTER_ASSERT(fReporter, !fMsg, |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 64 | "Error mismatch: expected:\n%s\nbut no error occurred\n", fMsg); |
| 65 | SetErrorReporter(fOldReporter); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Ethan Nicholas | 3272412 | 2021-09-07 13:49:07 -0400 | [diff] [blame] | 68 | void handleError(skstd::string_view msg, SkSL::PositionInfo pos) override { |
| 69 | REPORTER_ASSERT(fReporter, fMsg, "Received unexpected extra error: %.*s\n", |
| 70 | (int)msg.length(), msg.data()); |
| 71 | REPORTER_ASSERT(fReporter, !fMsg || msg == fMsg, |
| 72 | "Error mismatch: expected:\n%s\nbut received:\n%.*s", fMsg, (int)msg.length(), |
| 73 | msg.data()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 74 | fMsg = nullptr; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | const char* fMsg; |
| 79 | skiatest::Reporter* fReporter; |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 80 | ErrorReporter* fOldReporter; |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 83 | static bool whitespace_insensitive_compare(const char* a, const char* b) { |
| 84 | for (;;) { |
| 85 | while (isspace(*a)) { |
| 86 | ++a; |
| 87 | } |
| 88 | while (isspace(*b)) { |
| 89 | ++b; |
| 90 | } |
| 91 | if (*a != *b) { |
| 92 | return false; |
| 93 | } |
| 94 | if (*a == 0) { |
| 95 | return true; |
| 96 | } |
| 97 | ++a; |
| 98 | ++b; |
| 99 | } |
| 100 | } |
| 101 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 102 | // for use from SkSLDSLOnlyTest.cpp |
| 103 | void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) { |
| 104 | Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler()); |
| 105 | } |
| 106 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 107 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) { |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 108 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 109 | Expression e1 = 1; |
| 110 | REPORTER_ASSERT(r, e1.release()->description() == "1"); |
| 111 | Expression e2 = 1.0; |
| 112 | REPORTER_ASSERT(r, e2.release()->description() == "1.0"); |
| 113 | Expression e3 = true; |
| 114 | REPORTER_ASSERT(r, e3.release()->description() == "true"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 115 | Var a(kInt_Type, "a"); |
Ethan Nicholas | bffe80a | 2021-01-11 15:42:44 -0500 | [diff] [blame] | 116 | Expression e4 = a; |
| 117 | REPORTER_ASSERT(r, e4.release()->description() == "a"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 118 | |
| 119 | REPORTER_ASSERT(r, whitespace_insensitive_compare("", "")); |
| 120 | REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a")); |
| 121 | REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", "")); |
| 122 | REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a")); |
| 123 | REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc")); |
| 124 | REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc ")); |
| 125 | REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc")); |
| 126 | REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc")); |
Ethan Nicholas | 9504614 | 2021-01-07 10:57:27 -0500 | [diff] [blame] | 127 | } |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 128 | |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 129 | static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); } |
| 130 | static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); } |
| 131 | static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); } |
| 132 | static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); } |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 133 | static SkSL::String stringize(DSLBlock& blck) { return blck.release()->description(); } |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 134 | static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); } |
| 135 | static SkSL::String stringize(SkSL::Program& program) { return program.description(); } |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 136 | |
| 137 | template <typename T> |
| 138 | static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) { |
| 139 | SkSL::String actual = stringize(input); |
| 140 | if (!whitespace_insensitive_compare(expected, actual.c_str())) { |
| 141 | ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n", |
| 142 | lineNumber, expected, actual.c_str()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | template <typename T> |
| 147 | static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) { |
| 148 | // This overload allows temporary values to be passed to expect_equal. |
| 149 | return expect_equal(r, lineNumber, dsl, expected); |
| 150 | } |
| 151 | |
| 152 | #define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b)) |
| 153 | |
Ethan Nicholas | 22dcb73 | 2021-05-12 11:26:58 -0400 | [diff] [blame] | 154 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFlags, r, ctxInfo) { |
| 155 | { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 156 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | 22dcb73 | 2021-05-12 11:26:58 -0400 | [diff] [blame] | 157 | EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), "true"); |
| 158 | |
| 159 | Var x(kInt_Type, "x"); |
| 160 | EXPECT_EQUAL(Declare(x), "int x;"); |
| 161 | } |
| 162 | |
| 163 | { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 164 | SkSL::ProgramSettings settings; |
| 165 | settings.fOptimize = false; |
| 166 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings, |
Brian Osman | 8c26479 | 2021-07-01 16:41:27 -0400 | [diff] [blame] | 167 | SkSL::ProgramKind::kFragment); |
Ethan Nicholas | 22dcb73 | 2021-05-12 11:26:58 -0400 | [diff] [blame] | 168 | EXPECT_EQUAL(All(GreaterThan(Float4(1), Float4(0))), |
| 169 | "all(greaterThan(float4(1.0), float4(0.0)))"); |
| 170 | } |
| 171 | |
| 172 | { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 173 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), SkSL::ProgramSettings()); |
Ethan Nicholas | 22dcb73 | 2021-05-12 11:26:58 -0400 | [diff] [blame] | 174 | Var x(kInt_Type, "x"); |
| 175 | EXPECT_EQUAL(Declare(x), "int _0_x;"); |
| 176 | } |
| 177 | } |
| 178 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 179 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) { |
| 180 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 181 | Expression e1 = Float(std::numeric_limits<float>::max()); |
| 182 | REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) == |
| 183 | std::numeric_limits<float>::max()); |
| 184 | |
| 185 | Expression e2 = Float(std::numeric_limits<float>::min()); |
| 186 | REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) == |
| 187 | std::numeric_limits<float>::min()); |
| 188 | |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 189 | EXPECT_EQUAL(Float2(0), |
| 190 | "float2(0.0)"); |
| 191 | EXPECT_EQUAL(Float2(-0.5, 1), |
| 192 | "float2(-0.5, 1.0)"); |
| 193 | EXPECT_EQUAL(Float3(0.75), |
| 194 | "float3(0.75)"); |
| 195 | EXPECT_EQUAL(Float3(Float2(0, 1), -2), |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 196 | "float3(0.0, 1.0, -2.0)"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 197 | EXPECT_EQUAL(Float3(0, 1, 2), |
| 198 | "float3(0.0, 1.0, 2.0)"); |
| 199 | EXPECT_EQUAL(Float4(0), |
| 200 | "float4(0.0)"); |
| 201 | EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)), |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 202 | "float4(0.0, 1.0, 2.0, 3.0)"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 203 | EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)), |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 204 | "float4(0.0, 1.0, 2.0, 3.0)"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 205 | EXPECT_EQUAL(Float4(0, 1, 2, 3), |
| 206 | "float4(0.0, 1.0, 2.0, 3.0)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 207 | |
Ethan Nicholas | a60cc3e | 2021-04-23 16:15:11 -0400 | [diff] [blame] | 208 | DSLVar x(kFloat_Type, "x"); |
| 209 | EXPECT_EQUAL(x = 1.0, "(x = 1.0)"); |
| 210 | EXPECT_EQUAL(x = 1.0f, "(x = 1.0)"); |
| 211 | |
| 212 | DSLVar y(kFloat2_Type, "y"); |
| 213 | EXPECT_EQUAL(y.x() = 1.0, "(y.x = 1.0)"); |
| 214 | EXPECT_EQUAL(y.x() = 1.0f, "(y.x = 1.0)"); |
| 215 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 216 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 217 | ExpectError error(r, "floating point value is infinite"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 218 | Float(std::numeric_limits<float>::infinity()).release(); |
| 219 | } |
| 220 | |
| 221 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 222 | ExpectError error(r, "floating point value is NaN"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 223 | Float(std::numeric_limits<float>::quiet_NaN()).release(); |
| 224 | } |
| 225 | |
| 226 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 227 | ExpectError error(r, "'float4' is not a valid parameter to 'float2' constructor; use '.xy' " |
| 228 | "instead"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 229 | Float2(Float4(1)).release(); |
| 230 | } |
| 231 | |
| 232 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 233 | ExpectError error(r, "invalid arguments to 'float4' constructor (expected 4 scalars, but " |
| 234 | "found 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 235 | Float4(Float3(1)).release(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) { |
| 240 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 241 | Expression e1 = Half(std::numeric_limits<float>::max()); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 242 | REPORTER_ASSERT(r, |
| 243 | atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 244 | |
| 245 | Expression e2 = Half(std::numeric_limits<float>::min()); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 246 | REPORTER_ASSERT(r, |
| 247 | atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 248 | |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 249 | EXPECT_EQUAL(Half2(0), |
| 250 | "half2(0.0)"); |
| 251 | EXPECT_EQUAL(Half2(-0.5, 1), |
| 252 | "half2(-0.5, 1.0)"); |
| 253 | EXPECT_EQUAL(Half3(0.75), |
| 254 | "half3(0.75)"); |
| 255 | EXPECT_EQUAL(Half3(Half2(0, 1), -2), |
| 256 | "half3(0.0, 1.0, -2.0)"); |
| 257 | EXPECT_EQUAL(Half3(0, 1, 2), |
| 258 | "half3(0.0, 1.0, 2.0)"); |
| 259 | EXPECT_EQUAL(Half4(0), |
| 260 | "half4(0.0)"); |
| 261 | EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)), |
| 262 | "half4(0.0, 1.0, 2.0, 3.0)"); |
| 263 | EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)), |
| 264 | "half4(0.0, 1.0, 2.0, 3.0)"); |
| 265 | EXPECT_EQUAL(Half4(0, 1, 2, 3), |
| 266 | "half4(0.0, 1.0, 2.0, 3.0)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 267 | |
| 268 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 269 | ExpectError error(r, "floating point value is infinite"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 270 | Half(std::numeric_limits<float>::infinity()).release(); |
| 271 | } |
| 272 | |
| 273 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 274 | ExpectError error(r, "floating point value is NaN"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 275 | Half(std::numeric_limits<float>::quiet_NaN()).release(); |
| 276 | } |
| 277 | |
| 278 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 279 | ExpectError error(r, "'half4' is not a valid parameter to 'half2' constructor; use '.xy' " |
| 280 | "instead"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 281 | Half2(Half4(1)).release(); |
| 282 | } |
| 283 | |
| 284 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 285 | ExpectError error(r, "invalid arguments to 'half4' constructor (expected 4 scalars, but " |
| 286 | "found 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 287 | Half4(Half3(1)).release(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) { |
| 292 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 293 | |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 294 | EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()), |
| 295 | "2147483647"); |
| 296 | EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()), |
| 297 | "int2(-2147483648)"); |
| 298 | EXPECT_EQUAL(Int2(0, 1), |
| 299 | "int2(0, 1)"); |
| 300 | EXPECT_EQUAL(Int3(0), |
| 301 | "int3(0)"); |
| 302 | EXPECT_EQUAL(Int3(Int2(0, 1), -2), |
| 303 | "int3(0, 1, -2)"); |
| 304 | EXPECT_EQUAL(Int3(0, 1, 2), |
| 305 | "int3(0, 1, 2)"); |
| 306 | EXPECT_EQUAL(Int4(0), |
| 307 | "int4(0)"); |
| 308 | EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)), |
| 309 | "int4(0, 1, 2, 3)"); |
| 310 | EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)), |
| 311 | "int4(0, 1, 2, 3)"); |
| 312 | EXPECT_EQUAL(Int4(0, 1, 2, 3), |
| 313 | "int4(0, 1, 2, 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 314 | |
| 315 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 316 | ExpectError error(r, "'int4' is not a valid parameter to 'int2' constructor; use '.xy' " |
| 317 | "instead"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 318 | Int2(Int4(1)).release(); |
| 319 | } |
| 320 | |
| 321 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 322 | ExpectError error(r, "invalid arguments to 'int4' constructor (expected 4 scalars, but " |
| 323 | "found 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 324 | Int4(Int3(1)).release(); |
| 325 | } |
| 326 | } |
| 327 | |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 328 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUInt, r, ctxInfo) { |
| 329 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 330 | |
| 331 | EXPECT_EQUAL(UInt(std::numeric_limits<uint32_t>::max()), |
| 332 | "4294967295"); |
| 333 | EXPECT_EQUAL(UInt2(std::numeric_limits<uint32_t>::min()), |
| 334 | "uint2(0)"); |
| 335 | EXPECT_EQUAL(UInt2(0, 1), |
| 336 | "uint2(0, 1)"); |
| 337 | EXPECT_EQUAL(UInt3(0), |
| 338 | "uint3(0)"); |
| 339 | EXPECT_EQUAL(UInt3(UInt2(0, 1), -2), |
| 340 | "uint3(0, 1, -2)"); |
| 341 | EXPECT_EQUAL(UInt3(0, 1, 2), |
| 342 | "uint3(0, 1, 2)"); |
| 343 | EXPECT_EQUAL(UInt4(0), |
| 344 | "uint4(0)"); |
| 345 | EXPECT_EQUAL(UInt4(UInt2(0, 1), UInt2(2, 3)), |
| 346 | "uint4(0, 1, 2, 3)"); |
| 347 | EXPECT_EQUAL(UInt4(0, 1, UInt2(2, 3)), |
| 348 | "uint4(0, 1, 2, 3)"); |
| 349 | EXPECT_EQUAL(UInt4(0, 1, 2, 3), |
| 350 | "uint4(0, 1, 2, 3)"); |
| 351 | |
| 352 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 353 | ExpectError error(r, "'uint4' is not a valid parameter to 'uint2' constructor; use '.xy' " |
| 354 | "instead"); |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 355 | UInt2(UInt4(1)).release(); |
| 356 | } |
| 357 | |
| 358 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 359 | ExpectError error(r, "invalid arguments to 'uint4' constructor (expected 4 scalars, but " |
| 360 | "found 3)"); |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 361 | UInt4(UInt3(1)).release(); |
| 362 | } |
| 363 | } |
| 364 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 365 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) { |
| 366 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 367 | |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 368 | EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()), |
| 369 | "32767"); |
| 370 | EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()), |
| 371 | "short2(-32768)"); |
| 372 | EXPECT_EQUAL(Short2(0, 1), |
| 373 | "short2(0, 1)"); |
| 374 | EXPECT_EQUAL(Short3(0), |
| 375 | "short3(0)"); |
| 376 | EXPECT_EQUAL(Short3(Short2(0, 1), -2), |
| 377 | "short3(0, 1, -2)"); |
| 378 | EXPECT_EQUAL(Short3(0, 1, 2), |
| 379 | "short3(0, 1, 2)"); |
| 380 | EXPECT_EQUAL(Short4(0), |
| 381 | "short4(0)"); |
| 382 | EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)), |
| 383 | "short4(0, 1, 2, 3)"); |
| 384 | EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)), |
| 385 | "short4(0, 1, 2, 3)"); |
| 386 | EXPECT_EQUAL(Short4(0, 1, 2, 3), |
| 387 | "short4(0, 1, 2, 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 388 | |
| 389 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 390 | ExpectError error(r, "'short4' is not a valid parameter to 'short2' constructor; use '.xy' " |
| 391 | "instead"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 392 | Short2(Short4(1)).release(); |
| 393 | } |
| 394 | |
| 395 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 396 | ExpectError error(r, "invalid arguments to 'short4' constructor (expected 4 scalars, but " |
| 397 | "found 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 398 | Short4(Short3(1)).release(); |
| 399 | } |
| 400 | } |
| 401 | |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 402 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLUShort, r, ctxInfo) { |
| 403 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 404 | |
| 405 | EXPECT_EQUAL(UShort(std::numeric_limits<uint16_t>::max()), |
| 406 | "65535"); |
| 407 | EXPECT_EQUAL(UShort2(std::numeric_limits<uint16_t>::min()), |
| 408 | "ushort2(0)"); |
| 409 | EXPECT_EQUAL(UShort2(0, 1), |
| 410 | "ushort2(0, 1)"); |
| 411 | EXPECT_EQUAL(UShort3(0), |
| 412 | "ushort3(0)"); |
| 413 | EXPECT_EQUAL(UShort3(UShort2(0, 1), -2), |
| 414 | "ushort3(0, 1, -2)"); |
| 415 | EXPECT_EQUAL(UShort3(0, 1, 2), |
| 416 | "ushort3(0, 1, 2)"); |
| 417 | EXPECT_EQUAL(UShort4(0), |
| 418 | "ushort4(0)"); |
| 419 | EXPECT_EQUAL(UShort4(UShort2(0, 1), UShort2(2, 3)), |
| 420 | "ushort4(0, 1, 2, 3)"); |
| 421 | EXPECT_EQUAL(UShort4(0, 1, UShort2(2, 3)), |
| 422 | "ushort4(0, 1, 2, 3)"); |
| 423 | EXPECT_EQUAL(UShort4(0, 1, 2, 3), |
| 424 | "ushort4(0, 1, 2, 3)"); |
| 425 | |
| 426 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 427 | ExpectError error(r, "'ushort4' is not a valid parameter to 'ushort2' constructor; use " |
| 428 | "'.xy' instead"); |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 429 | UShort2(UShort4(1)).release(); |
| 430 | } |
| 431 | |
| 432 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 433 | ExpectError error(r, "invalid arguments to 'ushort4' constructor (expected 4 scalars, but " |
| 434 | "found 3)"); |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 435 | UShort4(UShort3(1)).release(); |
| 436 | } |
| 437 | } |
| 438 | |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 439 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) { |
| 440 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 441 | |
John Stiles | b9e4f64 | 2021-03-05 09:11:38 -0500 | [diff] [blame] | 442 | EXPECT_EQUAL(Bool2(false), |
| 443 | "bool2(false)"); |
| 444 | EXPECT_EQUAL(Bool2(false, true), |
| 445 | "bool2(false, true)"); |
| 446 | EXPECT_EQUAL(Bool3(false), |
| 447 | "bool3(false)"); |
| 448 | EXPECT_EQUAL(Bool3(Bool2(false, true), false), |
| 449 | "bool3(false, true, false)"); |
| 450 | EXPECT_EQUAL(Bool3(false, true, false), |
| 451 | "bool3(false, true, false)"); |
| 452 | EXPECT_EQUAL(Bool4(false), |
| 453 | "bool4(false)"); |
| 454 | EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)), |
| 455 | "bool4(false, true, false, true)"); |
| 456 | EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)), |
| 457 | "bool4(false, true, false, true)"); |
| 458 | EXPECT_EQUAL(Bool4(false, true, false, true), |
| 459 | "bool4(false, true, false, true)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 460 | |
| 461 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 462 | ExpectError error(r, "'bool4' is not a valid parameter to 'bool2' constructor; use '.xy' " |
| 463 | "instead"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 464 | Bool2(Bool4(true)).release(); |
| 465 | } |
| 466 | |
| 467 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 468 | ExpectError error(r, "invalid arguments to 'bool4' constructor (expected 4 scalars, but " |
| 469 | "found 3)"); |
Ethan Nicholas | b3d4e74 | 2021-01-08 11:42:25 -0500 | [diff] [blame] | 470 | Bool4(Bool3(true)).release(); |
| 471 | } |
| 472 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 473 | |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 474 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLType, r, ctxInfo) { |
| 475 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 476 | REPORTER_ASSERT(r, DSLType(kBool_Type).isBoolean()); |
| 477 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isNumber()); |
| 478 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isFloat()); |
| 479 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isSigned()); |
| 480 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isUnsigned()); |
| 481 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isInteger()); |
| 482 | REPORTER_ASSERT(r, DSLType(kBool_Type).isScalar()); |
| 483 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isVector()); |
| 484 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isMatrix()); |
| 485 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isArray()); |
| 486 | REPORTER_ASSERT(r, !DSLType(kBool_Type).isStruct()); |
| 487 | |
| 488 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isBoolean()); |
| 489 | REPORTER_ASSERT(r, DSLType(kInt_Type).isNumber()); |
| 490 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isFloat()); |
| 491 | REPORTER_ASSERT(r, DSLType(kInt_Type).isSigned()); |
| 492 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isUnsigned()); |
| 493 | REPORTER_ASSERT(r, DSLType(kInt_Type).isInteger()); |
| 494 | REPORTER_ASSERT(r, DSLType(kInt_Type).isScalar()); |
| 495 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isVector()); |
| 496 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isMatrix()); |
| 497 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isArray()); |
| 498 | REPORTER_ASSERT(r, !DSLType(kInt_Type).isStruct()); |
| 499 | |
| 500 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isBoolean()); |
| 501 | REPORTER_ASSERT(r, DSLType(kUInt_Type).isNumber()); |
| 502 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isFloat()); |
| 503 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isSigned()); |
| 504 | REPORTER_ASSERT(r, DSLType(kUInt_Type).isUnsigned()); |
| 505 | REPORTER_ASSERT(r, DSLType(kUInt_Type).isInteger()); |
| 506 | REPORTER_ASSERT(r, DSLType(kUInt_Type).isScalar()); |
| 507 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isVector()); |
| 508 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isMatrix()); |
| 509 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isArray()); |
| 510 | REPORTER_ASSERT(r, !DSLType(kUInt_Type).isStruct()); |
| 511 | |
| 512 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isBoolean()); |
| 513 | REPORTER_ASSERT(r, DSLType(kFloat_Type).isNumber()); |
| 514 | REPORTER_ASSERT(r, DSLType(kFloat_Type).isFloat()); |
| 515 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isSigned()); |
| 516 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isUnsigned()); |
| 517 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isInteger()); |
| 518 | REPORTER_ASSERT(r, DSLType(kFloat_Type).isScalar()); |
| 519 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isVector()); |
| 520 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isMatrix()); |
| 521 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isArray()); |
| 522 | REPORTER_ASSERT(r, !DSLType(kFloat_Type).isStruct()); |
| 523 | |
| 524 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isBoolean()); |
| 525 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isNumber()); |
| 526 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isFloat()); |
| 527 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isSigned()); |
| 528 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isUnsigned()); |
| 529 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isInteger()); |
| 530 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isScalar()); |
| 531 | REPORTER_ASSERT(r, DSLType(kFloat2_Type).isVector()); |
| 532 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isMatrix()); |
| 533 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isArray()); |
| 534 | REPORTER_ASSERT(r, !DSLType(kFloat2_Type).isStruct()); |
| 535 | |
| 536 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isBoolean()); |
| 537 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isNumber()); |
| 538 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isFloat()); |
| 539 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isSigned()); |
| 540 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isUnsigned()); |
| 541 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isInteger()); |
| 542 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isScalar()); |
| 543 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isVector()); |
| 544 | REPORTER_ASSERT(r, DSLType(kHalf2x2_Type).isMatrix()); |
| 545 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isArray()); |
| 546 | REPORTER_ASSERT(r, !DSLType(kHalf2x2_Type).isStruct()); |
| 547 | |
| 548 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isBoolean()); |
| 549 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isNumber()); |
| 550 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isFloat()); |
| 551 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isSigned()); |
| 552 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isUnsigned()); |
| 553 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isInteger()); |
| 554 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isScalar()); |
| 555 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isVector()); |
| 556 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isMatrix()); |
| 557 | REPORTER_ASSERT(r, DSLType(Array(kFloat_Type, 2)).isArray()); |
| 558 | REPORTER_ASSERT(r, !DSLType(Array(kFloat_Type, 2)).isStruct()); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 559 | |
| 560 | Var x(kFloat_Type); |
| 561 | DSLExpression e = x + 1; |
| 562 | REPORTER_ASSERT(r, e.type().isFloat()); |
| 563 | e.release(); |
Ethan Nicholas | ed6e54f | 2021-07-19 13:33:47 -0400 | [diff] [blame] | 564 | |
| 565 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 566 | ExpectError error(r, "array size must be positive"); |
Ethan Nicholas | ed6e54f | 2021-07-19 13:33:47 -0400 | [diff] [blame] | 567 | Array(kFloat_Type, -1); |
| 568 | } |
| 569 | |
| 570 | { |
Ethan Nicholas | 6f20b8d | 2021-08-31 07:40:24 -0400 | [diff] [blame] | 571 | ExpectError error(r, "multi-dimensional arrays are not supported"); |
Ethan Nicholas | ed6e54f | 2021-07-19 13:33:47 -0400 | [diff] [blame] | 572 | Array(Array(kFloat_Type, 2), 2); |
| 573 | } |
Ethan Nicholas | b83199e | 2021-05-03 14:25:35 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 576 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMatrices, r, ctxInfo) { |
| 577 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 578 | Var f22(kFloat2x2_Type, "f22"); |
| 579 | EXPECT_EQUAL(f22 = Float2x2(1), "(f22 = float2x2(1.0))"); |
| 580 | Var f32(kFloat3x2_Type, "f32"); |
| 581 | EXPECT_EQUAL(f32 = Float3x2(1, 2, 3, 4, 5, 6), |
| 582 | "(f32 = float3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))"); |
| 583 | Var f42(kFloat4x2_Type, "f42"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 584 | EXPECT_EQUAL(f42 = Float4x2(Float2(1, 2), Float2(3, 4), 5, 6, 7, 8), |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 585 | "(f42 = float4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))"); |
| 586 | Var f23(kFloat2x3_Type, "f23"); |
| 587 | EXPECT_EQUAL(f23 = Float2x3(1, Float2(2, 3), 4, Float2(5, 6)), |
| 588 | "(f23 = float2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))"); |
| 589 | Var f33(kFloat3x3_Type, "f33"); |
| 590 | EXPECT_EQUAL(f33 = Float3x3(Float3(1, 2, 3), 4, Float2(5, 6), 7, 8, 9), |
| 591 | "(f33 = float3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))"); |
| 592 | Var f43(kFloat4x3_Type, "f43"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 593 | EXPECT_EQUAL( |
| 594 | f43 = Float4x3(Float3(1, 2, 3), Float3(4, 5, 6), Float3(7, 8, 9), Float3(10, 11, 12)), |
| 595 | "(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))"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 596 | Var f24(kFloat2x4_Type, "f24"); |
| 597 | EXPECT_EQUAL(f24 = Float2x4(1, 2, 3, 4, 5, 6, 7, 8), |
| 598 | "(f24 = float2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))"); |
| 599 | Var f34(kFloat3x4_Type, "f34"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 600 | EXPECT_EQUAL(f34 = Float3x4(1, 2, 3, 4, 5, 6, 7, 8, Float4(9, 10, 11, 12)), |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 601 | "(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))"); |
| 602 | Var f44(kFloat4x4_Type, "f44"); |
| 603 | EXPECT_EQUAL(f44 = Float4x4(1), "(f44 = float4x4(1.0))"); |
| 604 | |
| 605 | Var h22(kHalf2x2_Type, "h22"); |
| 606 | EXPECT_EQUAL(h22 = Half2x2(1), "(h22 = half2x2(1.0))"); |
| 607 | Var h32(kHalf3x2_Type, "h32"); |
| 608 | EXPECT_EQUAL(h32 = Half3x2(1, 2, 3, 4, 5, 6), |
| 609 | "(h32 = half3x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))"); |
| 610 | Var h42(kHalf4x2_Type, "h42"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 611 | EXPECT_EQUAL(h42 = Half4x2(Half2(1, 2), Half2(3, 4), 5, 6, 7, 8), |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 612 | "(h42 = half4x2(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))"); |
| 613 | Var h23(kHalf2x3_Type, "h23"); |
| 614 | EXPECT_EQUAL(h23 = Half2x3(1, Half2(2, 3), 4, Half2(5, 6)), |
| 615 | "(h23 = half2x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0))"); |
| 616 | Var h33(kHalf3x3_Type, "h33"); |
| 617 | EXPECT_EQUAL(h33 = Half3x3(Half3(1, 2, 3), 4, Half2(5, 6), 7, 8, 9), |
| 618 | "(h33 = half3x3(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0))"); |
| 619 | Var h43(kHalf4x3_Type, "h43"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 620 | EXPECT_EQUAL(h43 = Half4x3(Half3(1, 2, 3), Half3(4, 5, 6), Half3(7, 8, 9), Half3(10, 11, 12)), |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 621 | "(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))"); |
| 622 | Var h24(kHalf2x4_Type, "h24"); |
| 623 | EXPECT_EQUAL(h24 = Half2x4(1, 2, 3, 4, 5, 6, 7, 8), |
| 624 | "(h24 = half2x4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))"); |
| 625 | Var h34(kHalf3x4_Type, "h34"); |
John Stiles | eb68973 | 2021-09-16 17:21:40 -0400 | [diff] [blame] | 626 | EXPECT_EQUAL(h34 = Half3x4(1, 2, 3, 4, 5, 6, 7, 8, Half4(9, 10, 11, 12)), |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 627 | "(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))"); |
| 628 | Var h44(kHalf4x4_Type, "h44"); |
| 629 | EXPECT_EQUAL(h44 = Half4x4(1), "(h44 = half4x4(1.0))"); |
| 630 | |
| 631 | EXPECT_EQUAL(f22 * 2, "(f22 * 2.0)"); |
| 632 | EXPECT_EQUAL(f22 == Float2x2(1), "(f22 == float2x2(1.0))"); |
| 633 | EXPECT_EQUAL(h42[0][1], "h42[0].y"); |
| 634 | EXPECT_EQUAL(f43 * Float4(0), "(f43 * float4(0.0))"); |
| 635 | EXPECT_EQUAL(h23 * 2, "(h23 * 2.0)"); |
| 636 | EXPECT_EQUAL(Inverse(f44), "inverse(f44)"); |
| 637 | |
| 638 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 639 | ExpectError error(r, "invalid arguments to 'float3x3' constructor (expected 9 scalars, but " |
| 640 | "found 2)"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 641 | DSLExpression(Float3x3(Float2(1))).release(); |
| 642 | } |
| 643 | |
| 644 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 645 | ExpectError error(r, "invalid arguments to 'half2x2' constructor (expected 4 scalars, but " |
| 646 | "found 5)"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 647 | DSLExpression(Half2x2(1, 2, 3, 4, 5)).release(); |
| 648 | } |
| 649 | |
| 650 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 651 | ExpectError error(r, "type mismatch: '*' cannot operate on 'float4x3', 'float3'"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 652 | DSLExpression(f43 * Float3(1)).release(); |
| 653 | } |
| 654 | |
| 655 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 656 | ExpectError error(r, "type mismatch: '=' cannot operate on 'float4x3', 'float3x3'"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 657 | DSLExpression(f43 = f33).release(); |
| 658 | } |
| 659 | |
| 660 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 661 | ExpectError error(r, "type mismatch: '=' cannot operate on 'half2x2', 'float2x2'"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 662 | DSLExpression(h22 = f22).release(); |
| 663 | } |
| 664 | |
| 665 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 666 | ExpectError error(r, "no match for inverse(float4x3)"); |
Ethan Nicholas | 8455893 | 2021-04-12 16:56:37 -0400 | [diff] [blame] | 667 | DSLExpression(Inverse(f43)).release(); |
| 668 | } |
| 669 | } |
| 670 | |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 671 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) { |
| 672 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 673 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 674 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 675 | EXPECT_EQUAL(a + b, |
| 676 | "(a + b)"); |
| 677 | EXPECT_EQUAL(a + 1, |
| 678 | "(a + 1.0)"); |
| 679 | EXPECT_EQUAL(0.5 + a + -99, |
| 680 | "((0.5 + a) + -99.0)"); |
| 681 | EXPECT_EQUAL(a += b + 1, |
| 682 | "(a += (b + 1.0))"); |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 683 | EXPECT_EQUAL(+a, |
| 684 | "a"); |
| 685 | EXPECT_EQUAL(+(a + b), |
| 686 | "(a + b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 687 | |
| 688 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 689 | ExpectError error(r, "type mismatch: '+' cannot operate on 'bool2', 'float'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 690 | DSLExpression((Bool2(true) + a)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 694 | ExpectError error(r, "type mismatch: '+=' cannot operate on 'float', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 695 | DSLExpression((a += Bool2(true))).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 699 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 700 | DSLExpression((1.0 += a)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 701 | } |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 702 | |
| 703 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 704 | ExpectError error(r, "'+' cannot operate on 'bool'"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 705 | Var c(kBool_Type); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 706 | DSLExpression(+c).release(); |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 707 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) { |
| 711 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 712 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 713 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 714 | EXPECT_EQUAL(a - b, |
| 715 | "(a - b)"); |
| 716 | EXPECT_EQUAL(a - 1, |
| 717 | "(a - 1)"); |
| 718 | EXPECT_EQUAL(2 - a - b, |
| 719 | "((2 - a) - b)"); |
| 720 | EXPECT_EQUAL(a -= b + 1, |
| 721 | "(a -= (b + 1))"); |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 722 | EXPECT_EQUAL(-a, |
| 723 | "-a"); |
| 724 | EXPECT_EQUAL(-(a - b), |
| 725 | "-(a - b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 726 | |
| 727 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 728 | ExpectError error(r, "type mismatch: '-' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 729 | DSLExpression(Bool2(true) - a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 733 | ExpectError error(r, "type mismatch: '-=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 734 | DSLExpression(a -= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 735 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 736 | |
| 737 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 738 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 739 | DSLExpression(1.0 -= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 740 | } |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 741 | |
| 742 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 743 | ExpectError error(r, "'-' cannot operate on 'bool'"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 744 | Var c(kBool_Type); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 745 | DSLExpression(-c).release(); |
Ethan Nicholas | b14b636 | 2021-03-08 17:07:58 -0500 | [diff] [blame] | 746 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) { |
| 750 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 751 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 752 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 753 | EXPECT_EQUAL(a * b, |
| 754 | "(a * b)"); |
| 755 | EXPECT_EQUAL(a * 2, |
| 756 | "(a * 2.0)"); |
| 757 | EXPECT_EQUAL(0.5 * a * -99, |
| 758 | "((0.5 * a) * -99.0)"); |
| 759 | EXPECT_EQUAL(a *= b + 1, |
| 760 | "(a *= (b + 1.0))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 761 | |
| 762 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 763 | ExpectError error(r, "type mismatch: '*' cannot operate on 'bool2', 'float'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 764 | DSLExpression(Bool2(true) * a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 768 | ExpectError error(r, "type mismatch: '*=' cannot operate on 'float', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 769 | DSLExpression(a *= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 770 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 771 | |
| 772 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 773 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 774 | DSLExpression(1.0 *= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 775 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) { |
| 779 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 780 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 781 | |
John Stiles | 8f440b4 | 2021-03-05 16:48:56 -0500 | [diff] [blame] | 782 | EXPECT_EQUAL(a / b, |
| 783 | "(a / b)"); |
| 784 | EXPECT_EQUAL(a / 2, |
| 785 | "(a / 2.0)"); |
| 786 | EXPECT_EQUAL(0.5 / a / -99, |
| 787 | "((0.5 / a) / -99.0)"); |
| 788 | EXPECT_EQUAL(b / (a - 1), |
| 789 | "(b / (a - 1.0))"); |
| 790 | EXPECT_EQUAL(a /= b + 1, |
| 791 | "(a /= (b + 1.0))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 792 | |
| 793 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 794 | ExpectError error(r, "type mismatch: '/' cannot operate on 'bool2', 'float'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 795 | DSLExpression(Bool2(true) / a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 799 | ExpectError error(r, "type mismatch: '/=' cannot operate on 'float', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 800 | DSLExpression(a /= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 801 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 802 | |
| 803 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 804 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 805 | DSLExpression(1.0 /= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 806 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 807 | |
| 808 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 809 | ExpectError error(r, "division by zero"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 810 | DSLExpression(a /= 0).release(); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 814 | Var c(kFloat2_Type, "c"); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 815 | ExpectError error(r, "division by zero"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 816 | DSLExpression(c /= Float2(Float(0), 1)).release(); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 817 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) { |
| 821 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 822 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 823 | Expression e1 = a % b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 824 | EXPECT_EQUAL(e1, "(a % b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 825 | |
| 826 | Expression e2 = a % 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 827 | EXPECT_EQUAL(e2, "(a % 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 828 | |
| 829 | Expression e3 = 10 % a % -99; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 830 | EXPECT_EQUAL(e3, "((10 % a) % -99)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 831 | |
| 832 | Expression e4 = a %= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 833 | EXPECT_EQUAL(e4, "(a %= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 834 | |
| 835 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 836 | ExpectError error(r, "type mismatch: '%' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 837 | DSLExpression(Bool2(true) % a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 841 | ExpectError error(r, "type mismatch: '%=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 842 | DSLExpression(a %= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 843 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 844 | |
| 845 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 846 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 847 | DSLExpression(1 %= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 848 | } |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 849 | |
| 850 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 851 | ExpectError error(r, "division by zero"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 852 | DSLExpression(a %= 0).release(); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | { |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 856 | Var c(kInt2_Type, "c"); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 857 | ExpectError error(r, "division by zero"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 858 | DSLExpression(c %= Int2(Int(0), 1)).release(); |
Ethan Nicholas | c0f9815 | 2021-02-05 16:21:10 -0500 | [diff] [blame] | 859 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) { |
| 863 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 864 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 865 | Expression e1 = a << b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 866 | EXPECT_EQUAL(e1, "(a << b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 867 | |
| 868 | Expression e2 = a << 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 869 | EXPECT_EQUAL(e2, "(a << 1)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 870 | |
| 871 | Expression e3 = 1 << a << 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 872 | EXPECT_EQUAL(e3, "((1 << a) << 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 873 | |
| 874 | Expression e4 = a <<= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 875 | EXPECT_EQUAL(e4, "(a <<= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 876 | |
| 877 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 878 | ExpectError error(r, "type mismatch: '<<' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 879 | DSLExpression(Bool2(true) << a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 883 | ExpectError error(r, "type mismatch: '<<=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 884 | DSLExpression(a <<= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 885 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 886 | |
| 887 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 888 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 889 | DSLExpression(1 <<= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 890 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) { |
| 894 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 895 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 896 | Expression e1 = a >> b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 897 | EXPECT_EQUAL(e1, "(a >> b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 898 | |
| 899 | Expression e2 = a >> 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 900 | EXPECT_EQUAL(e2, "(a >> 1)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 901 | |
| 902 | Expression e3 = 1 >> a >> 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 903 | EXPECT_EQUAL(e3, "((1 >> a) >> 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 904 | |
| 905 | Expression e4 = a >>= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 906 | EXPECT_EQUAL(e4, "(a >>= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 907 | |
| 908 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 909 | ExpectError error(r, "type mismatch: '>>' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 910 | DSLExpression(Bool2(true) >> a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 914 | ExpectError error(r, "type mismatch: '>>=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 915 | DSLExpression(a >>= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 916 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 917 | |
| 918 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 919 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 920 | DSLExpression(1 >>= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 921 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) { |
| 925 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 926 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 927 | Expression e1 = a & b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 928 | EXPECT_EQUAL(e1, "(a & b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 929 | |
| 930 | Expression e2 = a & 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 931 | EXPECT_EQUAL(e2, "(a & 1)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 932 | |
| 933 | Expression e3 = 1 & a & 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 934 | EXPECT_EQUAL(e3, "((1 & a) & 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 935 | |
| 936 | Expression e4 = a &= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 937 | EXPECT_EQUAL(e4, "(a &= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 938 | |
| 939 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 940 | ExpectError error(r, "type mismatch: '&' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 941 | DSLExpression(Bool2(true) & a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 945 | ExpectError error(r, "type mismatch: '&=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 946 | DSLExpression(a &= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 947 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 948 | |
| 949 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 950 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 951 | DSLExpression(1 &= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 952 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) { |
| 956 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 957 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 958 | Expression e1 = a | b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 959 | EXPECT_EQUAL(e1, "(a | b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 960 | |
| 961 | Expression e2 = a | 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 962 | EXPECT_EQUAL(e2, "(a | 1)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 963 | |
| 964 | Expression e3 = 1 | a | 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 965 | EXPECT_EQUAL(e3, "((1 | a) | 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 966 | |
| 967 | Expression e4 = a |= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 968 | EXPECT_EQUAL(e4, "(a |= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 969 | |
| 970 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 971 | ExpectError error(r, "type mismatch: '|' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 972 | DSLExpression(Bool2(true) | a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 976 | ExpectError error(r, "type mismatch: '|=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 977 | DSLExpression(a |= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 978 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 979 | |
| 980 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 981 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 982 | DSLExpression(1 |= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 983 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) { |
| 987 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 988 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 989 | Expression e1 = a ^ b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 990 | EXPECT_EQUAL(e1, "(a ^ b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 991 | |
| 992 | Expression e2 = a ^ 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 993 | EXPECT_EQUAL(e2, "(a ^ 1)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 994 | |
| 995 | Expression e3 = 1 ^ a ^ 2; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 996 | EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 997 | |
| 998 | Expression e4 = a ^= b + 1; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 999 | EXPECT_EQUAL(e4, "(a ^= (b + 1))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1000 | |
| 1001 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1002 | ExpectError error(r, "type mismatch: '^' cannot operate on 'bool2', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1003 | DSLExpression(Bool2(true) ^ a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1007 | ExpectError error(r, "type mismatch: '^=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1008 | DSLExpression(a ^= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1009 | } |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 1010 | |
| 1011 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1012 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1013 | DSLExpression(1 ^= a).release(); |
Ethan Nicholas | 67a0a8a | 2021-01-13 12:36:02 -0500 | [diff] [blame] | 1014 | } |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) { |
| 1018 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1019 | Var a(kBool_Type, "a"), b(kBool_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1020 | Expression e1 = a && b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1021 | EXPECT_EQUAL(e1, "(a && b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1022 | |
| 1023 | Expression e2 = a && true && b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1024 | EXPECT_EQUAL(e2, "(a && b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1025 | |
| 1026 | Expression e3 = a && false && b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1027 | EXPECT_EQUAL(e3, "false"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1028 | |
| 1029 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1030 | ExpectError error(r, "type mismatch: '&&' cannot operate on 'bool', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1031 | DSLExpression(a && 5).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) { |
| 1036 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1037 | Var a(kBool_Type, "a"), b(kBool_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1038 | Expression e1 = a || b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1039 | EXPECT_EQUAL(e1, "(a || b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1040 | |
| 1041 | Expression e2 = a || true || b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1042 | EXPECT_EQUAL(e2, "true"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1043 | |
| 1044 | Expression e3 = a || false || b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1045 | EXPECT_EQUAL(e3, "(a || b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1046 | |
| 1047 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1048 | ExpectError error(r, "type mismatch: '||' cannot operate on 'bool', 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1049 | DSLExpression(a || 5).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | |
Ethan Nicholas | 2ab47c9 | 2021-07-19 12:30:37 -0400 | [diff] [blame] | 1053 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalXor, r, ctxInfo) { |
| 1054 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1055 | Var a(kBool_Type, "a"), b(kBool_Type, "b"); |
| 1056 | Expression e1 = LogicalXor(a, b); |
| 1057 | EXPECT_EQUAL(e1, "(a ^^ b)"); |
| 1058 | |
| 1059 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1060 | ExpectError error(r, "type mismatch: '^^' cannot operate on 'bool', 'int'"); |
Ethan Nicholas | 2ab47c9 | 2021-07-19 12:30:37 -0400 | [diff] [blame] | 1061 | DSLExpression(LogicalXor(a, 5)).release(); |
| 1062 | } |
| 1063 | } |
| 1064 | |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1065 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) { |
| 1066 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1067 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1068 | Expression e1 = (a += b, b); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1069 | EXPECT_EQUAL(e1, "((a += b) , b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1070 | |
| 1071 | Expression e2 = (a += b, b += b, Int2(a)); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1072 | EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) { |
| 1076 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1077 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1078 | Expression e1 = a == b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1079 | EXPECT_EQUAL(e1, "(a == b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1080 | |
| 1081 | Expression e2 = a == 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1082 | EXPECT_EQUAL(e2, "(a == 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1083 | |
| 1084 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1085 | ExpectError error(r, "type mismatch: '==' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1086 | DSLExpression(a == Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) { |
| 1091 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1092 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1093 | Expression e1 = a != b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1094 | EXPECT_EQUAL(e1, "(a != b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1095 | |
| 1096 | Expression e2 = a != 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1097 | EXPECT_EQUAL(e2, "(a != 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1098 | |
| 1099 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1100 | ExpectError error(r, "type mismatch: '!=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1101 | DSLExpression(a != Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) { |
| 1106 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1107 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1108 | Expression e1 = a > b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1109 | EXPECT_EQUAL(e1, "(a > b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1110 | |
| 1111 | Expression e2 = a > 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1112 | EXPECT_EQUAL(e2, "(a > 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1113 | |
| 1114 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1115 | ExpectError error(r, "type mismatch: '>' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1116 | DSLExpression(a > Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) { |
| 1121 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1122 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1123 | Expression e1 = a >= b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1124 | EXPECT_EQUAL(e1, "(a >= b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1125 | |
| 1126 | Expression e2 = a >= 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1127 | EXPECT_EQUAL(e2, "(a >= 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1128 | |
| 1129 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1130 | ExpectError error(r, "type mismatch: '>=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1131 | DSLExpression(a >= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) { |
| 1136 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1137 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1138 | Expression e1 = a < b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1139 | EXPECT_EQUAL(e1, "(a < b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1140 | |
| 1141 | Expression e2 = a < 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1142 | EXPECT_EQUAL(e2, "(a < 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1143 | |
| 1144 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1145 | ExpectError error(r, "type mismatch: '<' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1146 | DSLExpression(a < Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) { |
| 1151 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1152 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1153 | Expression e1 = a <= b; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1154 | EXPECT_EQUAL(e1, "(a <= b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1155 | |
| 1156 | Expression e2 = a <= 5; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1157 | EXPECT_EQUAL(e2, "(a <= 5)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1158 | |
| 1159 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1160 | ExpectError error(r, "type mismatch: '<=' cannot operate on 'int', 'bool2'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1161 | DSLExpression(a <= Bool2(true)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) { |
| 1166 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1167 | Var a(kInt_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1168 | Expression e1 = !(a <= b); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1169 | EXPECT_EQUAL(e1, "!(a <= b)"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1170 | |
| 1171 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1172 | ExpectError error(r, "'!' cannot operate on 'int'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1173 | DSLExpression(!a).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) { |
| 1178 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1179 | Var a(kInt_Type, "a"), b(kBool_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1180 | Expression e1 = ~a; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1181 | EXPECT_EQUAL(e1, "~a"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1182 | |
| 1183 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1184 | ExpectError error(r, "'~' cannot operate on 'bool'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1185 | DSLExpression(~b).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) { |
| 1190 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1191 | Var a(kInt_Type, "a"), b(kBool_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1192 | Expression e1 = ++a; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1193 | EXPECT_EQUAL(e1, "++a"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1194 | |
| 1195 | Expression e2 = a++; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1196 | EXPECT_EQUAL(e2, "a++"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1197 | |
| 1198 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1199 | ExpectError error(r, "'++' cannot operate on 'bool'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1200 | DSLExpression(++b).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1204 | ExpectError error(r, "'++' cannot operate on 'bool'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1205 | DSLExpression(b++).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1209 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1210 | DSLExpression(++(a + 1)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1214 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1215 | DSLExpression((a + 1)++).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) { |
| 1220 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1221 | Var a(kInt_Type, "a"), b(kBool_Type, "b"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1222 | Expression e1 = --a; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1223 | EXPECT_EQUAL(e1, "--a"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1224 | |
| 1225 | Expression e2 = a--; |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1226 | EXPECT_EQUAL(e2, "a--"); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1227 | |
| 1228 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1229 | ExpectError error(r, "'--' cannot operate on 'bool'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1230 | DSLExpression(--b).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1234 | ExpectError error(r, "'--' cannot operate on 'bool'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1235 | DSLExpression(b--).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1239 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1240 | DSLExpression(--(a + 1)).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1244 | ExpectError error(r, "cannot assign to this expression"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1245 | DSLExpression((a + 1)--).release(); |
Ethan Nicholas | 92969f2 | 2021-01-13 10:38:59 -0500 | [diff] [blame] | 1246 | } |
| 1247 | } |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1248 | |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1249 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLCall, r, ctxInfo) { |
| 1250 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1251 | { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 1252 | DSLExpression sqrt(DSLWriter::IRGenerator().convertIdentifier(/*line=*/-1, "sqrt")); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1253 | SkTArray<DSLWrapper<DSLExpression>> args; |
John Stiles | 443fa19 | 2021-05-24 23:46:46 -0400 | [diff] [blame] | 1254 | args.emplace_back(16); |
| 1255 | EXPECT_EQUAL(sqrt(std::move(args)), "4.0"); // sqrt(16) gets optimized to 4 |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | { |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame^] | 1259 | DSLExpression pow(DSLWriter::IRGenerator().convertIdentifier(/*line=*/-1, "pow")); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1260 | DSLVar a(kFloat_Type, "a"); |
| 1261 | DSLVar b(kFloat_Type, "b"); |
| 1262 | SkTArray<DSLWrapper<DSLExpression>> args; |
| 1263 | args.emplace_back(a); |
| 1264 | args.emplace_back(b); |
| 1265 | EXPECT_EQUAL(pow(std::move(args)), "pow(a, b)"); |
| 1266 | } |
| 1267 | } |
| 1268 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1269 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1270 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1271 | EXPECT_EQUAL(Block(), "{ }"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1272 | Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2); |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1273 | EXPECT_EQUAL(Block(Declare(a), Declare(b), a = b), "{ int a = 1; int b = 2; (a = b); }"); |
Ethan Nicholas | db2326b | 2021-04-19 10:55:18 -0400 | [diff] [blame] | 1274 | |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1275 | EXPECT_EQUAL((If(a > 0, --a), ++b), "if ((a > 0)) --a; ++b;"); |
| 1276 | |
| 1277 | SkTArray<DSLStatement> statements; |
| 1278 | statements.push_back(a = 0); |
| 1279 | statements.push_back(++a); |
| 1280 | EXPECT_EQUAL(Block(std::move(statements)), "{ (a = 0); ++a; }"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1281 | } |
| 1282 | |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1283 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1284 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1285 | Var i(kInt_Type, "i", 0); |
| 1286 | DSLFunction(kVoid_Type, "success").define( |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1287 | For(Declare(i), i < 10, ++i, Block( |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1288 | If(i > 5, Break()) |
| 1289 | )) |
| 1290 | ); |
| 1291 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1292 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], |
| 1293 | "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }"); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1294 | |
| 1295 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1296 | ExpectError error(r, "break statement must be inside a loop or switch"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1297 | DSLFunction(kVoid_Type, "fail").define( |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1298 | Break() |
| 1299 | ); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1304 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1305 | Var i(kInt_Type, "i", 0); |
| 1306 | DSLFunction(kVoid_Type, "success").define( |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1307 | For(Declare(i), i < 10, ++i, Block( |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1308 | If(i < 5, Continue()) |
| 1309 | )) |
| 1310 | ); |
| 1311 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1312 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], |
| 1313 | "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }"); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1314 | |
| 1315 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1316 | ExpectError error(r, "continue statement must be inside a loop"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1317 | DSLFunction(kVoid_Type, "fail").define( |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1318 | Continue() |
| 1319 | ); |
| 1320 | } |
| 1321 | } |
| 1322 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1323 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1324 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1325 | { |
| 1326 | Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1)); |
| 1327 | EXPECT_EQUAL(Declare(a), "half4 a;"); |
| 1328 | EXPECT_EQUAL(Declare(b), "half4 b = half4(1.0);"); |
| 1329 | } |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1330 | |
| 1331 | { |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1332 | DSLWriter::Reset(); |
| 1333 | SkTArray<Var> vars; |
| 1334 | vars.push_back(Var(kBool_Type, "a", true)); |
| 1335 | vars.push_back(Var(kFloat_Type, "b")); |
| 1336 | EXPECT_EQUAL(Declare(vars), "bool a = true; float b;"); |
| 1337 | } |
| 1338 | |
| 1339 | { |
| 1340 | DSLWriter::Reset(); |
| 1341 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().empty()); |
| 1342 | GlobalVar a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1)); |
| 1343 | Declare(a); |
| 1344 | Declare(b); |
| 1345 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 1346 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "half4 a;"); |
| 1347 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "half4 b = half4(1.0);"); |
| 1348 | } |
| 1349 | |
| 1350 | { |
| 1351 | DSLWriter::Reset(); |
| 1352 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().empty()); |
| 1353 | SkTArray<GlobalVar> vars; |
| 1354 | vars.push_back(GlobalVar(kHalf4_Type, "a")); |
| 1355 | vars.push_back(GlobalVar(kHalf4_Type, "b", Half4(1))); |
| 1356 | Declare(vars); |
| 1357 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 1358 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "half4 a;"); |
| 1359 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "half4 b = half4(1.0);"); |
| 1360 | } |
| 1361 | |
| 1362 | { |
| 1363 | DSLWriter::Reset(); |
| 1364 | Var a(kHalf4_Type, "a", 1); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1365 | ExpectError error(r, "expected 'half4', but found 'int'"); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1366 | Declare(a).release(); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | { |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1370 | DSLWriter::Reset(); |
| 1371 | Var a(kInt_Type, "a"); |
| 1372 | Declare(a).release(); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1373 | ExpectError error(r, "variable has already been declared"); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1374 | Declare(a).release(); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1375 | } |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 1376 | |
| 1377 | { |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1378 | DSLWriter::Reset(); |
| 1379 | Var a(kUniform_Modifier, kInt_Type, "a"); |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1380 | ExpectError error(r, "'uniform' is not permitted here"); |
Ethan Nicholas | da72978 | 2021-07-19 12:55:52 -0400 | [diff] [blame] | 1381 | Declare(a).release(); |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclareGlobal, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1386 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1387 | DSLGlobalVar x(kInt_Type, "x", 0); |
| 1388 | Declare(x); |
| 1389 | DSLGlobalVar y(kUniform_Modifier, kFloat2_Type, "y"); |
| 1390 | Declare(y); |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 1391 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 1392 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "int x = 0;"); |
| 1393 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "uniform float2 y;"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1396 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) { |
| 1397 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
John Stiles | 443fa19 | 2021-05-24 23:46:46 -0400 | [diff] [blame] | 1398 | Var x(kFloat_Type, "x", 1); |
| 1399 | EXPECT_EQUAL(If(Sqrt(x) > 0, Discard()), "if ((sqrt(x) > 0.0)) discard;"); |
Ethan Nicholas | daceb79 | 2021-02-05 14:22:32 -0500 | [diff] [blame] | 1400 | } |
| 1401 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1402 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) { |
| 1403 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1404 | Statement x = Do(Block(), true); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1405 | EXPECT_EQUAL(x, "do {} while (true);"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1406 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1407 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1408 | Statement y = Do(Block(a++, --b), a != b); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1409 | EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1410 | |
| 1411 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1412 | ExpectError error(r, "expected 'bool', but found 'int'"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1413 | Do(Block(), 7).release(); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1418 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
John Stiles | 8676ebe | 2021-04-20 15:30:41 -0400 | [diff] [blame] | 1419 | EXPECT_EQUAL(For(Statement(), Expression(), Expression(), Block()), |
| 1420 | "for (;;) {}"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1421 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1422 | Var i(kInt_Type, "i", 0); |
John Stiles | 8676ebe | 2021-04-20 15:30:41 -0400 | [diff] [blame] | 1423 | EXPECT_EQUAL(For(Declare(i), i < 10, ++i, i += 5), |
| 1424 | "for (int i = 0; (i < 10); ++i) (i += 5);"); |
| 1425 | |
| 1426 | Var j(kInt_Type, "j", 0); |
| 1427 | Var k(kInt_Type, "k", 10); |
| 1428 | EXPECT_EQUAL(For((Declare(j), Declare(k)), j < k, ++j, Block()), R"( |
| 1429 | { |
| 1430 | int j = 0; |
| 1431 | int k = 10; |
| 1432 | for (; (j < k); ++j) {} |
| 1433 | } |
| 1434 | )"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1435 | |
| 1436 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1437 | ExpectError error(r, "expected 'bool', but found 'int'"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1438 | For(i = 0, i + 10, ++i, i += 5).release(); |
| 1439 | } |
Ethan Nicholas | a0f7654 | 2021-04-16 16:02:18 -0400 | [diff] [blame] | 1440 | |
| 1441 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1442 | ExpectError error(r, "invalid for loop initializer"); |
Ethan Nicholas | a0f7654 | 2021-04-16 16:02:18 -0400 | [diff] [blame] | 1443 | For(If(i == 0, i = 1), i < 10, ++i, i += 5).release(); |
| 1444 | } |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
Ethan Nicholas | e2c0504 | 2021-02-03 10:27:22 -0500 | [diff] [blame] | 1447 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1448 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1449 | Parameter coords(kFloat2_Type, "coords"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1450 | DSLFunction(kVoid_Type, "main", coords).define( |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1451 | sk_FragColor() = Half4(coords, 0, 1) |
| 1452 | ); |
| 1453 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1454 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], |
Ethan Nicholas | 371f6e1 | 2021-05-04 14:30:02 -0400 | [diff] [blame] | 1455 | "void main(float2 coords) { (sk_FragColor = half4(half2(coords), 0.0, 1.0)); }"); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1456 | |
Ethan Nicholas | 63f75fc | 2021-02-23 12:05:49 -0500 | [diff] [blame] | 1457 | { |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1458 | DSLWriter::Reset(); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1459 | DSLParameter x(kFloat_Type, "x"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1460 | DSLFunction sqr(kFloat_Type, "sqr", x); |
Ethan Nicholas | 63f75fc | 2021-02-23 12:05:49 -0500 | [diff] [blame] | 1461 | sqr.define( |
| 1462 | Return(x * x) |
| 1463 | ); |
| 1464 | EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)"); |
| 1465 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 1466 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }"); |
| 1467 | } |
| 1468 | |
| 1469 | { |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1470 | DSLWriter::Reset(); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1471 | DSLParameter x(kFloat2_Type, "x"); |
| 1472 | DSLParameter y(kFloat2_Type, "y"); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1473 | DSLFunction dot(kFloat2_Type, "dot", x, y); |
Ethan Nicholas | 63f75fc | 2021-02-23 12:05:49 -0500 | [diff] [blame] | 1474 | dot.define( |
| 1475 | Return(x * x + y * y) |
| 1476 | ); |
| 1477 | EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)), |
| 1478 | "dot(float2(1.0, 2.0), float2(3.0, 4.0))"); |
| 1479 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 1480 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], |
| 1481 | "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }"); |
| 1482 | } |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1483 | |
| 1484 | { |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 1485 | DSLWriter::Reset(); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1486 | DSLParameter x(kFloat_Type, "x"); |
| 1487 | DSLParameter y(kFloat_Type, "y"); |
Ethan Nicholas | 80f6235 | 2021-04-09 12:25:03 -0400 | [diff] [blame] | 1488 | DSLFunction pair(kFloat2_Type, "pair", x, y); |
| 1489 | pair.define( |
| 1490 | Return(Float2(x, y)) |
| 1491 | ); |
| 1492 | Var varArg1(kFloat_Type, "varArg1"); |
| 1493 | Var varArg2(kFloat_Type, "varArg2"); |
| 1494 | DSLWriter::MarkDeclared(varArg1); |
| 1495 | DSLWriter::MarkDeclared(varArg2); |
| 1496 | EXPECT_EQUAL(pair(varArg1, varArg2), "pair(varArg1, varArg2)"); |
| 1497 | } |
| 1498 | |
| 1499 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1500 | ExpectError error(r, "expected 'float', but found 'bool'"); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1501 | DSLWriter::Reset(); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1502 | DSLFunction(kFloat_Type, "broken").define( |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1503 | Return(true) |
| 1504 | ); |
| 1505 | } |
| 1506 | |
| 1507 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1508 | ExpectError error(r, "expected function to return 'float'"); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1509 | DSLWriter::Reset(); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1510 | DSLFunction(kFloat_Type, "broken").define( |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1511 | Return() |
| 1512 | ); |
| 1513 | } |
| 1514 | |
| 1515 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1516 | ExpectError error(r, "function 'broken' can exit without returning a value"); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1517 | DSLWriter::Reset(); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1518 | Var x(kFloat_Type, "x", 0); |
| 1519 | DSLFunction(kFloat_Type, "broken").define( |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1520 | Declare(x), |
John Stiles | b3dcbb1 | 2021-03-04 16:00:20 -0500 | [diff] [blame] | 1521 | If(x == 1, Return(x)) |
| 1522 | ); |
| 1523 | } |
| 1524 | |
| 1525 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1526 | ExpectError error(r, "may not return a value from a void function"); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1527 | DSLWriter::Reset(); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1528 | DSLFunction(kVoid_Type, "broken").define( |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1529 | Return(0) |
| 1530 | ); |
| 1531 | } |
| 1532 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1533 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1534 | ExpectError error(r, "function 'broken' can exit without returning a value"); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1535 | DSLWriter::Reset(); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1536 | DSLFunction(kFloat_Type, "broken").define( |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1537 | ); |
| 1538 | } |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1539 | |
| 1540 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1541 | ExpectError error(r, "parameter has already been used in another function"); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1542 | DSLWriter::Reset(); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1543 | DSLParameter p(kFloat_Type); |
| 1544 | DSLFunction(kVoid_Type, "ok", p).define( |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1545 | ); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1546 | DSLFunction(kVoid_Type, "broken", p).define( |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1547 | ); |
| 1548 | } |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1549 | } |
| 1550 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1551 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) { |
| 1552 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1553 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1554 | Statement x = If(a > b, a -= b); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1555 | EXPECT_EQUAL(x, "if ((a > b)) (a -= b);"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1556 | |
| 1557 | Statement y = If(a > b, a -= b, b -= a); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1558 | EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1559 | |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 1560 | Statement z = StaticIf(a > b, a -= b, b -= a); |
| 1561 | EXPECT_EQUAL(z, "@if ((a > b)) (a -= b); else (b -= a);"); |
| 1562 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1563 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1564 | ExpectError error(r, "expected 'bool', but found 'float'"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1565 | If(a + b, a -= b).release(); |
| 1566 | } |
| 1567 | } |
| 1568 | |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1569 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInterfaceBlock, r, ctxInfo) { |
| 1570 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1571 | DSLGlobalVar intf = InterfaceBlock(kUniform_Modifier, "InterfaceBlock1", |
| 1572 | { Field(kFloat_Type, "a"), Field(kInt_Type, "b") }); |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1573 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 1574 | EXPECT_EQUAL(*DSLWriter::ProgramElements().back(), |
| 1575 | "uniform InterfaceBlock1 { float a; int b; };"); |
| 1576 | EXPECT_EQUAL(intf.field("a"), "InterfaceBlock1.a"); |
| 1577 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1578 | DSLGlobalVar intf2 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock2", |
| 1579 | { Field(kFloat2_Type, "x"), Field(kHalf2x2_Type, "y") }, |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1580 | "blockVar"); |
| 1581 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 1582 | EXPECT_EQUAL(*DSLWriter::ProgramElements().back(), |
| 1583 | "uniform InterfaceBlock2 { float2 x; half2x2 y; } blockVar;"); |
| 1584 | EXPECT_EQUAL(intf2.field("x"), "blockVar.x"); |
| 1585 | |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1586 | DSLGlobalVar intf3 = InterfaceBlock(kUniform_Modifier, "InterfaceBlock3", |
| 1587 | { Field(kFloat_Type, "z") },"arrayVar", 4); |
Ethan Nicholas | e6ed3c2 | 2021-07-08 10:38:43 -0400 | [diff] [blame] | 1588 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3); |
| 1589 | EXPECT_EQUAL(*DSLWriter::ProgramElements().back(), |
| 1590 | "uniform InterfaceBlock3 { float z; } arrayVar[4];"); |
| 1591 | EXPECT_EQUAL(intf3[1].field("z"), "arrayVar[1].z"); |
| 1592 | } |
| 1593 | |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1594 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) { |
| 1595 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1596 | |
| 1597 | Statement x = Return(); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1598 | EXPECT_EQUAL(x, "return;"); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1599 | |
| 1600 | Statement y = Return(true); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1601 | EXPECT_EQUAL(y, "return true;"); |
Ethan Nicholas | 1ff7609 | 2021-01-28 10:02:43 -0500 | [diff] [blame] | 1602 | } |
| 1603 | |
Ethan Nicholas | fa648a1 | 2021-02-17 12:13:20 -0500 | [diff] [blame] | 1604 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) { |
| 1605 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1606 | Var a(kInt_Type, "a"); |
Ethan Nicholas | fa648a1 | 2021-02-17 12:13:20 -0500 | [diff] [blame] | 1607 | Expression x = Select(a > 0, 1, -1); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1608 | EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)"); |
Ethan Nicholas | fa648a1 | 2021-02-17 12:13:20 -0500 | [diff] [blame] | 1609 | |
| 1610 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1611 | ExpectError error(r, "expected 'bool', but found 'int'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1612 | Select(a, 1, -1).release(); |
Ethan Nicholas | fa648a1 | 2021-02-17 12:13:20 -0500 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1616 | ExpectError error(r, "ternary operator result mismatch: 'float2', 'float3'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1617 | Select(a > 0, Float2(1), Float3(1)).release(); |
Ethan Nicholas | fa648a1 | 2021-02-17 12:13:20 -0500 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1621 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) { |
| 1622 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1623 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1624 | Var a(kFloat_Type, "a"), b(kInt_Type, "b"); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1625 | |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1626 | SkTArray<DSLStatement> caseStatements; |
| 1627 | caseStatements.push_back(a = 1); |
| 1628 | caseStatements.push_back(Continue()); |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 1629 | Statement x = Switch(b, |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1630 | Case(0, a = 0, Break()), |
Ethan Nicholas | 722cb67 | 2021-05-06 10:47:06 -0400 | [diff] [blame] | 1631 | Case(1, std::move(caseStatements)), |
John Stiles | e1d1b08 | 2021-02-23 13:44:36 -0500 | [diff] [blame] | 1632 | Case(2, a = 2 /*Fallthrough*/), |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1633 | Default(Discard()) |
| 1634 | ); |
John Stiles | e1d1b08 | 2021-02-23 13:44:36 -0500 | [diff] [blame] | 1635 | EXPECT_EQUAL(x, R"( |
John Stiles | f3a28db | 2021-03-10 23:00:47 -0500 | [diff] [blame] | 1636 | switch (b) { |
John Stiles | e1d1b08 | 2021-02-23 13:44:36 -0500 | [diff] [blame] | 1637 | case 0: (a = 0.0); break; |
| 1638 | case 1: (a = 1.0); continue; |
| 1639 | case 2: (a = 2.0); |
| 1640 | default: discard; |
| 1641 | } |
| 1642 | )"); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1643 | |
Ethan Nicholas | 8a6537d | 2021-04-30 12:44:00 -0400 | [diff] [blame] | 1644 | Statement y = StaticSwitch(b, |
| 1645 | Case(0, a = 0, Break()), |
| 1646 | Case(1, a = 1, Continue()), |
| 1647 | Case(2, a = 2 /*Fallthrough*/), |
| 1648 | Default(Discard()) |
| 1649 | ); |
| 1650 | EXPECT_EQUAL(y, R"( |
| 1651 | @switch (b) { |
| 1652 | case 0: (a = 0.0); break; |
| 1653 | case 1: (a = 1.0); continue; |
| 1654 | case 2: (a = 2.0); |
| 1655 | default: discard; |
| 1656 | } |
| 1657 | )"); |
| 1658 | |
John Stiles | 642cde2 | 2021-02-23 14:57:01 -0500 | [diff] [blame] | 1659 | EXPECT_EQUAL(Switch(b), |
| 1660 | "switch (b) {}"); |
| 1661 | |
| 1662 | EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)), |
| 1663 | "switch (b) { default: case 0: case 1: }"); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1664 | |
| 1665 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1666 | ExpectError error(r, "duplicate case value '0'"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1667 | DSLStatement(Switch(0, Case(0), Case(0))).release(); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1671 | ExpectError error(r, "duplicate default case"); |
Ethan Nicholas | 34c7e11 | 2021-02-25 20:50:32 -0500 | [diff] [blame] | 1672 | DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release(); |
John Stiles | e1d1b08 | 2021-02-23 13:44:36 -0500 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1676 | ExpectError error(r, "case value must be a constant integer"); |
John Stiles | 628777c | 2021-08-04 22:07:41 -0400 | [diff] [blame] | 1677 | Var c(kInt_Type); |
| 1678 | DSLStatement(Switch(0, Case(c))).release(); |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1679 | } |
Ethan Nicholas | cfefec0 | 2021-02-09 15:22:57 -0500 | [diff] [blame] | 1680 | } |
| 1681 | |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 1682 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1683 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1684 | Var a(kFloat4_Type, "a"); |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 1685 | |
John Stiles | f04e09c | 2021-03-05 13:13:14 -0500 | [diff] [blame] | 1686 | EXPECT_EQUAL(a.x(), |
| 1687 | "a.x"); |
| 1688 | EXPECT_EQUAL(a.y(), |
| 1689 | "a.y"); |
| 1690 | EXPECT_EQUAL(a.z(), |
| 1691 | "a.z"); |
| 1692 | EXPECT_EQUAL(a.w(), |
| 1693 | "a.w"); |
| 1694 | EXPECT_EQUAL(a.r(), |
| 1695 | "a.x"); |
| 1696 | EXPECT_EQUAL(a.g(), |
| 1697 | "a.y"); |
| 1698 | EXPECT_EQUAL(a.b(), |
| 1699 | "a.z"); |
| 1700 | EXPECT_EQUAL(a.a(), |
| 1701 | "a.w"); |
| 1702 | EXPECT_EQUAL(Swizzle(a, R), |
| 1703 | "a.x"); |
| 1704 | EXPECT_EQUAL(Swizzle(a, ZERO, G), |
| 1705 | "float2(0.0, a.y)"); |
| 1706 | EXPECT_EQUAL(Swizzle(a, B, G, G), |
| 1707 | "a.zyy"); |
| 1708 | EXPECT_EQUAL(Swizzle(a, R, G, B, ONE), |
| 1709 | "float4(a.xyz, 1.0)"); |
| 1710 | EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(), |
| 1711 | "a.z"); |
Ethan Nicholas | 68c77d4 | 2021-01-26 14:31:29 -0500 | [diff] [blame] | 1712 | } |
| 1713 | |
John Stiles | 08771b0 | 2021-04-26 09:35:10 -0400 | [diff] [blame] | 1714 | |
| 1715 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLVarSwap, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1716 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
John Stiles | 08771b0 | 2021-04-26 09:35:10 -0400 | [diff] [blame] | 1717 | |
| 1718 | // We should be able to convert `a` into a proper var by swapping it, even from within a scope. |
| 1719 | Var a; |
| 1720 | if (true) |
| 1721 | { |
| 1722 | Var(kInt_Type, "a").swap(a); |
| 1723 | } |
| 1724 | |
| 1725 | EXPECT_EQUAL(Statement(Block(Declare(a), a = 123)), |
| 1726 | "{ int a; (a = 123); }"); |
| 1727 | } |
| 1728 | |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1729 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) { |
| 1730 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1731 | Statement x = While(true, Block()); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1732 | EXPECT_EQUAL(x, "for (; true;) {}"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1733 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1734 | Var a(kFloat_Type, "a"), b(kFloat_Type, "b"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1735 | Statement y = While(a != b, Block(a++, --b)); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1736 | EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }"); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1737 | |
| 1738 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1739 | ExpectError error(r, "expected 'bool', but found 'int'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1740 | While(7, Block()).release(); |
Ethan Nicholas | d6b6f3e | 2021-01-22 15:18:25 -0500 | [diff] [blame] | 1741 | } |
| 1742 | } |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 1743 | |
| 1744 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) { |
| 1745 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1746 | Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1747 | |
| 1748 | EXPECT_EQUAL(a[0], "a[0]"); |
| 1749 | EXPECT_EQUAL(a[b], "a[b]"); |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 1750 | |
| 1751 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1752 | ExpectError error(r, "expected 'int', but found 'bool'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1753 | a[true].release(); |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1757 | ExpectError error(r, "expected array, but found 'int'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1758 | b[0].release(); |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1762 | ExpectError error(r, "index -1 out of range for 'int[5]'"); |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 1763 | a[-1].release(); |
Ethan Nicholas | 04be339 | 2021-01-26 10:07:01 -0500 | [diff] [blame] | 1764 | } |
| 1765 | } |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 1766 | |
| 1767 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) { |
| 1768 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 1769 | // There is a Fract type on Mac which can conflict with our Fract builtin |
| 1770 | using SkSL::dsl::Fract; |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1771 | Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c"); |
| 1772 | Var h3(kHalf3_Type, "h3"); |
| 1773 | Var b4(kBool4_Type, "b4"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1774 | EXPECT_EQUAL(Abs(a), "abs(a)"); |
| 1775 | EXPECT_EQUAL(All(b4), "all(b4)"); |
| 1776 | EXPECT_EQUAL(Any(b4), "any(b4)"); |
John Stiles | e3fa745 | 2021-04-26 09:36:07 -0400 | [diff] [blame] | 1777 | EXPECT_EQUAL(Atan(a), "atan(a)"); |
| 1778 | EXPECT_EQUAL(Atan(a, b), "atan(a, b)"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1779 | EXPECT_EQUAL(Ceil(a), "ceil(a)"); |
| 1780 | EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)"); |
| 1781 | EXPECT_EQUAL(Cos(a), "cos(a)"); |
| 1782 | EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)"); |
| 1783 | EXPECT_EQUAL(Degrees(a), "degrees(a)"); |
| 1784 | EXPECT_EQUAL(Distance(a, b), "distance(a, b)"); |
| 1785 | EXPECT_EQUAL(Dot(a, b), "dot(a, b)"); |
| 1786 | EXPECT_EQUAL(Equal(a, b), "equal(a, b)"); |
| 1787 | EXPECT_EQUAL(Exp(a), "exp(a)"); |
| 1788 | EXPECT_EQUAL(Exp2(a), "exp2(a)"); |
| 1789 | EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)"); |
| 1790 | EXPECT_EQUAL(Floor(a), "floor(a)"); |
| 1791 | EXPECT_EQUAL(Fract(a), "fract(a)"); |
| 1792 | EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)"); |
| 1793 | EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)"); |
| 1794 | EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)"); |
| 1795 | EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)"); |
| 1796 | EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)"); |
| 1797 | EXPECT_EQUAL(Length(a), "length(a)"); |
| 1798 | EXPECT_EQUAL(Log(a), "log(a)"); |
| 1799 | EXPECT_EQUAL(Log2(a), "log2(a)"); |
| 1800 | EXPECT_EQUAL(Max(a, b), "max(a, b)"); |
| 1801 | EXPECT_EQUAL(Min(a, b), "min(a, b)"); |
| 1802 | EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)"); |
| 1803 | EXPECT_EQUAL(Mod(a, b), "mod(a, b)"); |
| 1804 | EXPECT_EQUAL(Normalize(a), "normalize(a)"); |
| 1805 | EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)"); |
| 1806 | EXPECT_EQUAL(Pow(a, b), "pow(a, b)"); |
| 1807 | EXPECT_EQUAL(Radians(a), "radians(a)"); |
| 1808 | EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)"); |
| 1809 | EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)"); |
John Stiles | 2533953 | 2021-09-08 17:50:09 -0400 | [diff] [blame] | 1810 | EXPECT_EQUAL(Round(a), "round(a)"); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1811 | EXPECT_EQUAL(Saturate(a), "saturate(a)"); |
| 1812 | EXPECT_EQUAL(Sign(a), "sign(a)"); |
| 1813 | EXPECT_EQUAL(Sin(a), "sin(a)"); |
| 1814 | EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)"); |
| 1815 | EXPECT_EQUAL(Sqrt(a), "sqrt(a)"); |
| 1816 | EXPECT_EQUAL(Step(a, b), "step(a, b)"); |
| 1817 | EXPECT_EQUAL(Tan(a), "tan(a)"); |
| 1818 | EXPECT_EQUAL(Unpremul(a), "unpremul(a)"); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 1819 | |
| 1820 | // these calls all go through the normal channels, so it ought to be sufficient to prove that |
| 1821 | // one of them reports errors correctly |
| 1822 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1823 | ExpectError error(r, "no match for ceil(bool)"); |
Ethan Nicholas | 30e93d5 | 2021-01-26 12:00:25 -0500 | [diff] [blame] | 1824 | Ceil(a == b).release(); |
| 1825 | } |
| 1826 | } |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1827 | |
| 1828 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1829 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1830 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1831 | Var v1(kConst_Modifier, kInt_Type, "v1", 0); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1832 | Statement d1 = Declare(v1); |
Ethan Nicholas | bd97400 | 2021-02-22 16:20:06 -0500 | [diff] [blame] | 1833 | EXPECT_EQUAL(d1, "const int v1 = 0;"); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1834 | |
| 1835 | // Most modifiers require an appropriate context to be legal. We can't yet give them that |
| 1836 | // context, so we can't as yet Declare() variables with these modifiers. |
| 1837 | // TODO: better tests when able |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1838 | Var v2(kIn_Modifier, kInt_Type, "v2"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1839 | REPORTER_ASSERT(r, v2.modifiers().flags() == SkSL::Modifiers::kIn_Flag); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1840 | DSLWriter::MarkDeclared(v2); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1841 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1842 | Var v3(kOut_Modifier, kInt_Type, "v3"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1843 | REPORTER_ASSERT(r, v3.modifiers().flags() == SkSL::Modifiers::kOut_Flag); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1844 | DSLWriter::MarkDeclared(v3); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1845 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1846 | Var v4(kFlat_Modifier, kInt_Type, "v4"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1847 | REPORTER_ASSERT(r, v4.modifiers().flags() == SkSL::Modifiers::kFlat_Flag); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1848 | DSLWriter::MarkDeclared(v4); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1849 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1850 | Var v5(kNoPerspective_Modifier, kInt_Type, "v5"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1851 | REPORTER_ASSERT(r, v5.modifiers().flags() == SkSL::Modifiers::kNoPerspective_Flag); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1852 | DSLWriter::MarkDeclared(v5); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1853 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1854 | Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1855 | REPORTER_ASSERT(r, v6.modifiers().flags() == (SkSL::Modifiers::kIn_Flag | |
| 1856 | SkSL::Modifiers::kOut_Flag)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1857 | DSLWriter::MarkDeclared(v6); |
Ethan Nicholas | 11a15b1 | 2021-02-11 15:56:27 -0500 | [diff] [blame] | 1858 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1859 | Var v7(kInOut_Modifier, kInt_Type, "v7"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1860 | REPORTER_ASSERT(r, v7.modifiers().flags() == (SkSL::Modifiers::kIn_Flag | |
| 1861 | SkSL::Modifiers::kOut_Flag)); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1862 | DSLWriter::MarkDeclared(v7); |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1863 | |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1864 | Var v8(kUniform_Modifier, kInt_Type, "v8"); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1865 | REPORTER_ASSERT(r, v8.modifiers().flags() == SkSL::Modifiers::kUniform_Flag); |
Ethan Nicholas | e9c2c5a | 2021-04-30 13:14:24 -0400 | [diff] [blame] | 1866 | DSLWriter::MarkDeclared(v8); |
Ethan Nicholas | 961d944 | 2021-03-16 16:37:29 -0400 | [diff] [blame] | 1867 | // Uniforms do not need to be explicitly declared |
Ethan Nicholas | d6b26e5 | 2021-01-27 07:53:46 -0500 | [diff] [blame] | 1868 | } |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1869 | |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1870 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLayout, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1871 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1872 | Var v1(DSLModifiers(DSLLayout().location(1).set(2).binding(3).offset(4).index(5).builtin(6) |
| 1873 | .inputAttachmentIndex(7), |
| 1874 | kConst_Modifier), kInt_Type, "v1", 0); |
| 1875 | EXPECT_EQUAL(Declare(v1), "layout (location = 1, offset = 4, binding = 3, index = 5, set = 2, " |
| 1876 | "builtin = 6, input_attachment_index = 7) const int v1 = 0;"); |
| 1877 | |
| 1878 | Var v2(DSLLayout().originUpperLeft(), kFloat2_Type, "v2"); |
| 1879 | EXPECT_EQUAL(Declare(v2), "layout (origin_upper_left) float2 v2;"); |
| 1880 | |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1881 | Var v4(DSLLayout().pushConstant(), kBool_Type, "v4"); |
| 1882 | EXPECT_EQUAL(Declare(v4), "layout (push_constant) bool v4;"); |
| 1883 | |
| 1884 | Var v5(DSLLayout().blendSupportAllEquations(), kHalf4_Type, "v5"); |
| 1885 | EXPECT_EQUAL(Declare(v5), "layout (blend_support_all_equations) half4 v5;"); |
| 1886 | |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1887 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1888 | ExpectError error(r, "'srgb_unpremul' is only permitted in runtime effects"); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 1889 | DSLGlobalVar v(DSLModifiers(DSLLayout().srgbUnpremul(), kUniform_Modifier), kHalf4_Type, |
| 1890 | "v"); |
| 1891 | Declare(v); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 1892 | } |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1893 | |
| 1894 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1895 | ExpectError error(r, "layout qualifier 'location' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1896 | DSLLayout().location(1).location(2); |
| 1897 | } |
| 1898 | |
| 1899 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1900 | ExpectError error(r, "layout qualifier 'set' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1901 | DSLLayout().set(1).set(2); |
| 1902 | } |
| 1903 | |
| 1904 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1905 | ExpectError error(r, "layout qualifier 'binding' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1906 | DSLLayout().binding(1).binding(2); |
| 1907 | } |
| 1908 | |
| 1909 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1910 | ExpectError error(r, "layout qualifier 'offset' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1911 | DSLLayout().offset(1).offset(2); |
| 1912 | } |
| 1913 | |
| 1914 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1915 | ExpectError error(r, "layout qualifier 'index' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1916 | DSLLayout().index(1).index(2); |
| 1917 | } |
| 1918 | |
| 1919 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1920 | ExpectError error(r, "layout qualifier 'builtin' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1921 | DSLLayout().builtin(1).builtin(2); |
| 1922 | } |
| 1923 | |
| 1924 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1925 | ExpectError error(r, "layout qualifier 'input_attachment_index' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1926 | DSLLayout().inputAttachmentIndex(1).inputAttachmentIndex(2); |
| 1927 | } |
| 1928 | |
| 1929 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1930 | ExpectError error(r, "layout qualifier 'origin_upper_left' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1931 | DSLLayout().originUpperLeft().originUpperLeft(); |
| 1932 | } |
| 1933 | |
| 1934 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1935 | ExpectError error(r, "layout qualifier 'push_constant' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1936 | DSLLayout().pushConstant().pushConstant(); |
| 1937 | } |
| 1938 | |
| 1939 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1940 | ExpectError error(r, "layout qualifier 'blend_support_all_equations' appears more than " |
| 1941 | "once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1942 | DSLLayout().blendSupportAllEquations().blendSupportAllEquations(); |
| 1943 | } |
| 1944 | |
| 1945 | { |
Ethan Nicholas | 4a5e22a | 2021-08-13 17:29:51 -0400 | [diff] [blame] | 1946 | ExpectError error(r, "layout qualifier 'srgb_unpremul' appears more than once"); |
Ethan Nicholas | b22fcaf | 2021-05-10 16:17:22 -0400 | [diff] [blame] | 1947 | DSLLayout().srgbUnpremul().srgbUnpremul(); |
| 1948 | } |
| 1949 | } |
| 1950 | |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1951 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSampleShader, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1952 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), default_settings(), |
Brian Osman | 552fcb9 | 2021-04-28 17:41:57 -0400 | [diff] [blame] | 1953 | SkSL::ProgramKind::kRuntimeShader); |
Brian Osman | f8a5504 | 2021-08-23 16:24:18 -0400 | [diff] [blame] | 1954 | DSLGlobalVar shader(kUniform_Modifier, kShader_Type, "child"); |
Brian Osman | e76530d | 2021-09-09 14:31:31 -0400 | [diff] [blame] | 1955 | DSLGlobalVar notShader(kUniform_Modifier, kFloat_Type, "x"); |
| 1956 | EXPECT_EQUAL(shader.eval(Float2(0, 0)), "child.eval(float2(0.0, 0.0))"); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1957 | |
| 1958 | { |
Brian Osman | e76530d | 2021-09-09 14:31:31 -0400 | [diff] [blame] | 1959 | ExpectError error(r, "no match for shader::eval(half4)"); |
| 1960 | shader.eval(Half4(1)).release(); |
| 1961 | } |
| 1962 | |
| 1963 | { |
| 1964 | ExpectError error(r, "type does not support method calls"); |
| 1965 | notShader.eval(Half4(1)).release(); |
Ethan Nicholas | 624a529 | 2021-04-16 14:54:43 -0400 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1969 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) { |
Ethan Nicholas | 55a63af | 2021-05-18 10:12:58 -0400 | [diff] [blame] | 1970 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1971 | |
| 1972 | DSLType simpleStruct = Struct("SimpleStruct", |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1973 | Field(kFloat_Type, "x"), |
| 1974 | Field(kBool_Type, "b"), |
| 1975 | Field(Array(kFloat_Type, 3), "a") |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1976 | ); |
| 1977 | DSLVar result(simpleStruct, "result"); |
| 1978 | DSLFunction(simpleStruct, "returnStruct").define( |
| 1979 | Declare(result), |
| 1980 | result.field("x") = 123, |
| 1981 | result.field("b") = result.field("x") > 0, |
| 1982 | result.field("a")[0] = result.field("x"), |
| 1983 | Return(result) |
| 1984 | ); |
| 1985 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1986 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], |
| 1987 | "struct SimpleStruct { float x; bool b; float[3] a; };"); |
| 1988 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], |
| 1989 | "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);" |
| 1990 | "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }"); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1991 | |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1992 | Struct("NestedStruct", |
Ethan Nicholas | b14e6b9 | 2021-04-08 16:56:05 -0400 | [diff] [blame] | 1993 | Field(kInt_Type, "x"), |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1994 | Field(simpleStruct, "simple") |
| 1995 | ); |
Ethan Nicholas | fe5d692 | 2021-03-05 14:23:48 -0500 | [diff] [blame] | 1996 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3); |
| 1997 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[2], |
John Stiles | b4d7b58 | 2021-02-19 09:56:31 -0500 | [diff] [blame] | 1998 | "struct NestedStruct { int x; SimpleStruct simple; };"); |
Ethan Nicholas | bf79dff | 2021-02-11 15:18:31 -0500 | [diff] [blame] | 1999 | } |
Ethan Nicholas | a1a0b92 | 2021-05-04 12:22:02 -0400 | [diff] [blame] | 2000 | |
| 2001 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWrapper, r, ctxInfo) { |
| 2002 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 2003 | std::vector<Wrapper<DSLExpression>> exprs; |
| 2004 | exprs.push_back(DSLExpression(1)); |
| 2005 | exprs.emplace_back(2.0); |
| 2006 | EXPECT_EQUAL(std::move(*exprs[0]), "1"); |
| 2007 | EXPECT_EQUAL(std::move(*exprs[1]), "2.0"); |
| 2008 | |
| 2009 | std::vector<Wrapper<DSLVar>> vars; |
| 2010 | vars.emplace_back(DSLVar(kInt_Type, "x")); |
Ethan Nicholas | b4f8b7a | 2021-06-23 10:27:09 -0400 | [diff] [blame] | 2011 | REPORTER_ASSERT(r, DSLWriter::Var(*vars[0])->name() == "x"); |
Ethan Nicholas | a1a0b92 | 2021-05-04 12:22:02 -0400 | [diff] [blame] | 2012 | } |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 2013 | |
| 2014 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLRTAdjust, r, ctxInfo) { |
Ethan Nicholas | 494eb3e | 2021-08-27 19:17:01 -0400 | [diff] [blame] | 2015 | { |
| 2016 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(), |
| 2017 | SkSL::ProgramKind::kVertex); |
| 2018 | DSLGlobalVar rtAdjust(kUniform_Modifier, kFloat4_Type, "sk_RTAdjust"); |
| 2019 | Declare(rtAdjust); |
| 2020 | DSLFunction(kVoid_Type, "main").define( |
| 2021 | sk_Position() = Half4(0) |
| 2022 | ); |
| 2023 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 2024 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], |
| 2025 | "void main() {" |
| 2026 | "(sk_PerVertex.sk_Position = float4(0.0));" |
| 2027 | "(sk_PerVertex.sk_Position = float4(((sk_PerVertex.sk_Position.xy * sk_RTAdjust.xz) + " |
| 2028 | "(sk_PerVertex.sk_Position.ww * sk_RTAdjust.yw)), 0.0, sk_PerVertex.sk_Position.w));" |
| 2029 | "}"); |
| 2030 | } |
| 2031 | |
| 2032 | { |
| 2033 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(), |
| 2034 | SkSL::ProgramKind::kVertex); |
| 2035 | REPORTER_ASSERT(r, !DSLWriter::IRGenerator().haveRTAdjustInterfaceBlock()); |
| 2036 | |
| 2037 | DSLGlobalVar intf = InterfaceBlock(kUniform_Modifier, "uniforms", |
| 2038 | { Field(kInt_Type, "unused"), |
| 2039 | Field(kFloat4_Type, "sk_RTAdjust") }); |
| 2040 | REPORTER_ASSERT(r, DSLWriter::IRGenerator().haveRTAdjustInterfaceBlock()); |
| 2041 | REPORTER_ASSERT(r, DSLWriter::IRGenerator().getRTAdjustFieldIndex() == 1); |
| 2042 | } |
| 2043 | |
| 2044 | { |
| 2045 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(), |
| 2046 | SkSL::ProgramKind::kVertex); |
| 2047 | ExpectError error(r, "sk_RTAdjust must have type 'float4'"); |
| 2048 | InterfaceBlock(kUniform_Modifier, "uniforms", |
| 2049 | { Field(kInt_Type, "unused"), Field(kHalf4_Type, "sk_RTAdjust") }); |
| 2050 | } |
| 2051 | |
| 2052 | { |
| 2053 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared(), |
| 2054 | SkSL::ProgramKind::kVertex); |
| 2055 | ExpectError error(r, "symbol 'sk_RTAdjust' was already defined"); |
| 2056 | InterfaceBlock(kUniform_Modifier, "uniforms1", |
| 2057 | { Field(kInt_Type, "unused1"), Field(kFloat4_Type, "sk_RTAdjust") }); |
| 2058 | InterfaceBlock(kUniform_Modifier, "uniforms2", |
| 2059 | { Field(kInt_Type, "unused2"), Field(kFloat4_Type, "sk_RTAdjust") }); |
| 2060 | } |
Ethan Nicholas | ebc9fad | 2021-07-09 15:35:23 -0400 | [diff] [blame] | 2061 | } |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 2062 | |
| 2063 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInlining, r, ctxInfo) { |
| 2064 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
Ethan Nicholas | a2d22b2 | 2021-07-15 10:35:54 -0400 | [diff] [blame] | 2065 | DSLParameter x(kFloat_Type, "x"); |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 2066 | DSLFunction sqr(kFloat_Type, "sqr", x); |
| 2067 | sqr.define( |
| 2068 | Return(x * x) |
| 2069 | ); |
| 2070 | DSLFunction(kVoid_Type, "main").define( |
| 2071 | sk_FragColor() = (sqr(2), Half4(sqr(3))) |
| 2072 | ); |
Ethan Nicholas | b18c1e2 | 2021-07-16 09:53:53 -0400 | [diff] [blame] | 2073 | const char* source = "source test"; |
| 2074 | std::unique_ptr<SkSL::Program> program = ReleaseProgram(std::make_unique<SkSL::String>(source)); |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 2075 | EXPECT_EQUAL(*program, |
| 2076 | "layout(location = 0, index = 0, builtin = 10001) out half4 sk_FragColor;" |
| 2077 | "layout(builtin = 17)in bool sk_Clockwise;" |
| 2078 | "void main() {" |
| 2079 | "/* inlined: sqr */;" |
| 2080 | "/* inlined: sqr */;" |
| 2081 | "(sk_FragColor = (4.0 , half4(half(9.0))));" |
| 2082 | "}"); |
Ethan Nicholas | b18c1e2 | 2021-07-16 09:53:53 -0400 | [diff] [blame] | 2083 | REPORTER_ASSERT(r, *program->fSource == source); |
Ethan Nicholas | 292a09d | 2021-07-14 09:52:16 -0400 | [diff] [blame] | 2084 | } |
Ethan Nicholas | 459777a | 2021-07-16 11:16:27 -0400 | [diff] [blame] | 2085 | |
| 2086 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReleaseUnused, r, ctxInfo) { |
| 2087 | SkSL::ProgramSettings settings = default_settings(); |
| 2088 | settings.fAssertDSLObjectsReleased = false; |
| 2089 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), settings); |
| 2090 | If(Sqrt(1) > 0, Discard()); |
| 2091 | // Ensure that we can safely destroy statements and expressions despite being unused while |
| 2092 | // settings.fAssertDSLObjectsReleased is disabled. |
| 2093 | } |
Ethan Nicholas | 57709e1 | 2021-08-10 15:02:51 -0400 | [diff] [blame] | 2094 | |
| 2095 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPrototypes, r, ctxInfo) { |
| 2096 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), no_mark_vars_declared()); |
| 2097 | { |
| 2098 | DSLParameter x(kFloat_Type, "x"); |
| 2099 | DSLFunction sqr(kFloat_Type, "sqr", x); |
| 2100 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 2101 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);"); |
| 2102 | sqr.define( |
| 2103 | Return(x * x) |
| 2104 | ); |
| 2105 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 2106 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }"); |
| 2107 | } |
| 2108 | |
| 2109 | { |
| 2110 | DSLWriter::Reset(); |
| 2111 | DSLParameter x(kFloat_Type, "x"); |
| 2112 | DSLFunction sqr(kFloat_Type, "sqr", x); |
| 2113 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 2114 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);"); |
| 2115 | DSLFunction(kVoid_Type, "main").define(sqr(5)); |
| 2116 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2); |
| 2117 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x);"); |
| 2118 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[1], "void main() { sqr(5.0); }"); |
| 2119 | sqr.define( |
| 2120 | Return(x * x) |
| 2121 | ); |
| 2122 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3); |
| 2123 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[2], "float sqr(float x) { return (x * x); }"); |
| 2124 | |
| 2125 | const char* source = "source test"; |
| 2126 | std::unique_ptr<SkSL::Program> p = ReleaseProgram(std::make_unique<SkSL::String>(source)); |
| 2127 | EXPECT_EQUAL(*p, |
| 2128 | "layout (builtin = 17) in bool sk_Clockwise;" |
| 2129 | "float sqr(float x);" |
| 2130 | "void main() {" |
| 2131 | "/* inlined: sqr */;" |
| 2132 | "25.0;" |
| 2133 | "}"); |
| 2134 | } |
| 2135 | } |
Ethan Nicholas | 642215e | 2021-09-03 11:10:54 -0400 | [diff] [blame] | 2136 | |
| 2137 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLExtension, r, ctxInfo) { |
| 2138 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 2139 | AddExtension("test_extension"); |
| 2140 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 2141 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "#extension test_extension : enable"); |
| 2142 | } |
Ethan Nicholas | 678ec71 | 2021-09-03 06:33:47 -0400 | [diff] [blame] | 2143 | |
| 2144 | DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiersDeclaration, r, ctxInfo) { |
| 2145 | AutoDSLContext context(ctxInfo.directContext()->priv().getGpu()); |
| 2146 | Declare(Modifiers(Layout().blendSupportAllEquations(), kOut_Modifier)); |
| 2147 | REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1); |
| 2148 | EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "layout(blend_support_all_equations) out;"); |
| 2149 | } |