Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Mike Reed | 46f5c5f | 2020-02-20 15:42:29 -0500 | [diff] [blame] | 8 | #include "include/core/SkM44.h" |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 9 | #include "src/sksl/SkSLByteCode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/SkSLCompiler.h" |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 11 | #include "src/sksl/SkSLExternalFunction.h" |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLVMGenerator.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 13 | #include "src/utils/SkJSON.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "tests/Test.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 16 | |
Brian Osman | 1f71f43 | 2020-11-18 15:44:46 -0500 | [diff] [blame] | 17 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
| 18 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 19 | struct ProgramBuilder { |
| 20 | ProgramBuilder(skiatest::Reporter* r, const char* src) |
| 21 | : fCaps(GrContextOptions{}), fCompiler(&fCaps) { |
| 22 | SkSL::Program::Settings settings; |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 23 | // The SkSL inliner is well tested in other contexts. Here, we disable inlining entirely, |
| 24 | // to stress-test the VM generator's handling of function calls with varying signatures. |
| 25 | settings.fInlineThreshold = 0; |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 26 | // For convenience, so we can test functions other than (and not called by) main. |
| 27 | settings.fRemoveDeadFunctions = false; |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 28 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 29 | fProgram = fCompiler.convertProgram(SkSL::Program::kGeneric_Kind, SkSL::String(src), |
| 30 | settings); |
| 31 | if (!fProgram) { |
| 32 | ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, fCompiler.errorText().c_str()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 33 | } |
| 34 | } |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 35 | |
| 36 | operator bool() const { return fProgram != nullptr; } |
| 37 | SkSL::Program& operator*() { return *fProgram; } |
| 38 | |
| 39 | GrShaderCaps fCaps; |
| 40 | SkSL::Compiler fCompiler; |
| 41 | std::unique_ptr<SkSL::Program> fProgram; |
| 42 | }; |
| 43 | |
| 44 | struct ByteCodeBuilder { |
| 45 | ByteCodeBuilder(skiatest::Reporter* r, const char* src) : fProgram(r, src), fByteCode(nullptr) { |
| 46 | if (fProgram) { |
| 47 | fByteCode = fProgram.fCompiler.toByteCode(*fProgram); |
| 48 | if (!fByteCode) { |
| 49 | ERRORF(r, "Program failed to compile:\n%s\n%s\n", src, |
| 50 | fProgram.fCompiler.errorText().c_str()); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | operator bool() const { return fByteCode != nullptr; } |
| 56 | SkSL::ByteCode* operator->() { return fByteCode.get(); } |
| 57 | |
| 58 | ProgramBuilder fProgram; |
| 59 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
| 60 | }; |
| 61 | |
| 62 | static void verify_values(skiatest::Reporter* r, |
| 63 | const char* src, |
| 64 | const float* actual, |
| 65 | const float* expected, |
| 66 | int N, |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 67 | bool exactCompare) { |
| 68 | auto exact_equiv = [](float x, float y) { |
| 69 | return x == y |
| 70 | || (isnan(x) && isnan(y)); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 71 | }; |
| 72 | |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 73 | bool valid = true; |
| 74 | for (int i = 0; i < N; ++i) { |
| 75 | if (exactCompare && !exact_equiv(actual[i], expected[i])) { |
| 76 | valid = false; |
| 77 | } |
| 78 | if (!exactCompare && !SkScalarNearlyEqual(actual[i], expected[i])) { |
| 79 | valid = false; |
| 80 | } |
| 81 | } |
| 82 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 83 | if (!valid) { |
| 84 | printf("for program: %s\n", src); |
| 85 | printf(" expected ("); |
| 86 | const char* separator = ""; |
| 87 | for (int i = 0; i < N; ++i) { |
| 88 | printf("%s%f", separator, expected[i]); |
| 89 | separator = ", "; |
| 90 | } |
| 91 | printf("), but received ("); |
| 92 | separator = ""; |
| 93 | for (int i = 0; i < N; ++i) { |
| 94 | printf("%s%f", separator, actual[i]); |
| 95 | separator = ", "; |
| 96 | } |
| 97 | printf(")\n"); |
| 98 | } |
| 99 | REPORTER_ASSERT(r, valid); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 102 | void test_skvm(skiatest::Reporter* r, const char* src, float* in, const float* expected, |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 103 | bool exactCompare) { |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 104 | ProgramBuilder program(r, src); |
| 105 | if (!program) { return; } |
| 106 | |
| 107 | const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main"); |
| 108 | REPORTER_ASSERT(r, main); |
| 109 | |
| 110 | skvm::Builder b; |
| 111 | SkSL::SkVMSignature sig; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 112 | SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}, &sig); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 113 | skvm::Program p = b.done(); |
| 114 | |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 115 | REPORTER_ASSERT(r, p.nargs() == (int)(sig.fParameterSlots + sig.fReturnSlots)); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 116 | |
| 117 | auto out = std::make_unique<float[]>(sig.fReturnSlots); |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 118 | auto args = std::make_unique<void*[]>(sig.fParameterSlots + sig.fReturnSlots); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 119 | for (size_t i = 0; i < sig.fParameterSlots; ++i) { |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 120 | args[i] = in + i; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 121 | } |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 122 | for (size_t i = 0; i < sig.fReturnSlots; ++i) { |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 123 | args[sig.fParameterSlots + i] = out.get() + i; |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // TODO: Test with and without JIT? |
| 127 | p.eval(1, args.get()); |
| 128 | |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 129 | verify_values(r, src, out.get(), expected, sig.fReturnSlots, exactCompare); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void test(skiatest::Reporter* r, const char* src, float* in, const float* expected, |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 133 | bool exactCompare = true) { |
| 134 | test_skvm(r, src, in, expected, exactCompare); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 135 | |
| 136 | ByteCodeBuilder byteCode(r, src); |
| 137 | if (!byteCode) { return; } |
| 138 | |
| 139 | const SkSL::ByteCodeFunction* main = byteCode->getFunction("main"); |
| 140 | int returnCount = main->getReturnCount(); |
| 141 | std::unique_ptr<float[]> out = std::unique_ptr<float[]>(new float[returnCount]); |
| 142 | SkAssertResult(byteCode->run(main, in, main->getParameterCount(), out.get(), returnCount, |
| 143 | nullptr, 0)); |
| 144 | |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 145 | verify_values(r, src, out.get(), expected, returnCount, exactCompare); |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 148 | void vec_test(skiatest::Reporter* r, const char* src) { |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 149 | ByteCodeBuilder byteCode(r, src); |
| 150 | if (!byteCode) { return; } |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 151 | |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 152 | const SkSL::ByteCodeFunction* main = byteCode->getFunction("main"); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 153 | |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 154 | // Test on four different vectors (with varying orderings to get divergent control flow) |
| 155 | const float input[16] = { 1, 2, 3, 4, |
| 156 | 4, 3, 2, 1, |
| 157 | 7, 5, 8, 6, |
| 158 | 6, 8, 5, 7 }; |
| 159 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 160 | float out_s[16], out_v[16]; |
| 161 | memcpy(out_s, input, sizeof(out_s)); |
| 162 | memcpy(out_v, input, sizeof(out_v)); |
| 163 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 164 | // First run in scalar mode to determine the expected output |
| 165 | for (int i = 0; i < 4; ++i) { |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 166 | SkAssertResult(byteCode->run(main, out_s + i * 4, 4, nullptr, 0, nullptr, 0)); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 167 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 168 | |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 169 | // Need to transpose input vectors for striped execution |
| 170 | auto transpose = [](float* v) { |
| 171 | for (int r = 0; r < 4; ++r) |
| 172 | for (int c = 0; c < r; ++c) |
| 173 | std::swap(v[r*4 + c], v[c*4 + r]); |
| 174 | }; |
| 175 | |
| 176 | // Need to transpose input vectors for striped execution |
| 177 | transpose(out_v); |
| 178 | float* args[] = { out_v, out_v + 4, out_v + 8, out_v + 12 }; |
| 179 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 180 | // Now run in parallel and compare results |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 181 | SkAssertResult(byteCode->runStriped(main, 4, args, 4, nullptr, 0, nullptr, 0)); |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 182 | |
| 183 | // Transpose striped outputs back |
| 184 | transpose(out_v); |
| 185 | |
John Stiles | c1c3c6d | 2020-08-15 23:22:53 -0400 | [diff] [blame] | 186 | if (0 != memcmp(out_s, out_v, sizeof(out_s))) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 187 | printf("for program: %s\n", src); |
| 188 | for (int i = 0; i < 4; ++i) { |
| 189 | printf("(%g %g %g %g) -> (%g %g %g %g), expected (%g %g %g %g)\n", |
| 190 | input[4*i + 0], input[4*i + 1], input[4*i + 2], input[4*i + 3], |
| 191 | out_v[4*i + 0], out_v[4*i + 1], out_v[4*i + 2], out_v[4*i + 3], |
| 192 | out_s[4*i + 0], out_s[4*i + 1], out_s[4*i + 2], out_s[4*i + 3]); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 193 | } |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 194 | main->disassemble(); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 195 | REPORT_FAILURE(r, "VecInterpreter mismatch", SkString()); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 199 | void test_skvm(skiatest::Reporter* r, const char* src, |
| 200 | float inR, float inG, float inB, float inA, |
| 201 | float exR, float exG, float exB, float exA) { |
| 202 | ProgramBuilder program(r, src); |
| 203 | if (!program) { return; } |
| 204 | |
| 205 | const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main"); |
| 206 | REPORTER_ASSERT(r, main); |
| 207 | |
| 208 | skvm::Builder b; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 209 | SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 210 | skvm::Program p = b.done(); |
| 211 | |
| 212 | // TODO: Test with and without JIT? |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 213 | p.eval(1, &inR, &inG, &inB, &inA); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 214 | |
| 215 | float actual[4] = { inR, inG, inB, inA }; |
| 216 | float expected[4] = { exR, exG, exB, exA }; |
| 217 | |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 218 | verify_values(r, src, actual, expected, 4, /*exactCompare=*/true); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 219 | |
| 220 | // TODO: vec_test with skvm |
| 221 | } |
| 222 | |
| 223 | void test(skiatest::Reporter* r, const char* src, |
| 224 | float inR, float inG, float inB, float inA, |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 225 | float exR, float exG, float exB, float exA) { |
| 226 | test_skvm(r, src, inR, inG, inB, inA, exR, exG, exB, exA); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 227 | |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 228 | ByteCodeBuilder byteCode(r, src); |
| 229 | if (!byteCode) { return; } |
| 230 | |
| 231 | float inoutColor[4] = { inR, inG, inB, inA }; |
| 232 | float expected[4] = { exR, exG, exB, exA }; |
| 233 | |
| 234 | const SkSL::ByteCodeFunction* main = byteCode->getFunction("main"); |
| 235 | SkAssertResult(byteCode->run(main, inoutColor, 4, nullptr, 0, nullptr, 0)); |
| 236 | |
Mike Klein | 606488a | 2021-01-07 13:55:00 -0600 | [diff] [blame] | 237 | verify_values(r, src, inoutColor, expected, 4, /*exactCompare=*/true); |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 238 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 239 | // Do additional testing of 4x1 vs 1x4 to stress divergent control flow, etc. |
| 240 | vec_test(r, src); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 241 | } |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 242 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 243 | DEF_TEST(SkSLInterpreterAdd, r) { |
| 244 | test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1, |
| 245 | 0.75, 0, 0); |
| 246 | test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5); |
| 247 | test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1, |
| 248 | 0.5, 1, 1.5, 2); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 249 | test(r, "void main(inout half4 color) { color.r = half(int(color.r) + int(color.g)); }", 1, 3, 0, 0, |
Ethan Nicholas | 6f62412 | 2019-09-24 13:07:06 -0400 | [diff] [blame] | 250 | 4, 3, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | DEF_TEST(SkSLInterpreterSubtract, r) { |
| 254 | test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25, |
| 255 | 0.75, 0, 0); |
| 256 | test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1); |
| 257 | test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1, |
| 258 | 0, 0, 0, 0); |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 259 | test(r, "void main(inout half4 color) { color.x = -color.x; }", 4, 3, 2, 1, -4, 3, 2, 1); |
| 260 | test(r, "void main(inout half4 color) { color = -color; }", 4, 3, 2, 1, -4, -3, -2, -1); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 261 | test(r, "void main(inout half4 color) { color.r = half(int(color.r) - int(color.g)); }", 3, 1, 0, 0, |
Ethan Nicholas | 6f62412 | 2019-09-24 13:07:06 -0400 | [diff] [blame] | 262 | 2, 1, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | DEF_TEST(SkSLInterpreterMultiply, r) { |
| 266 | test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0, |
| 267 | 0); |
| 268 | test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12, |
| 269 | 20); |
| 270 | test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1, |
| 271 | 16, 9, 4, 1); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 272 | test(r, "void main(inout half4 color) { color.r = half(int(color.r) * int(color.g)); }", 3, -2, 0, 0, |
Ethan Nicholas | 6f62412 | 2019-09-24 13:07:06 -0400 | [diff] [blame] | 273 | -6, -2, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | DEF_TEST(SkSLInterpreterDivide, r) { |
| 277 | test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0, |
| 278 | 0); |
| 279 | test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6, |
| 280 | 4, 3); |
| 281 | test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1, |
| 282 | 1, 1, 1, 1); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 283 | test(r, "void main(inout half4 color) { color.r = half(int(color.r) / int(color.g)); }", 8, -2, 0, 0, |
Ethan Nicholas | 6f62412 | 2019-09-24 13:07:06 -0400 | [diff] [blame] | 284 | -4, -2, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 287 | DEF_TEST(SkSLInterpreterAnd, r) { |
| 288 | test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) " |
| 289 | "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3); |
| 290 | test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) " |
| 291 | "color = half4(color.a); }", 1, 1, 0, 3, 1, 1, 0, 3); |
| 292 | test(r, "void main(inout half4 color) { if (color.r > color.g && color.g > color.b) " |
| 293 | "color = half4(color.a); }", 2, 1, 1, 3, 2, 1, 1, 3); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 294 | test(r, "half global; bool update() { global = 123; return true; }" |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 295 | "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) " |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 296 | "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 123); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 297 | test(r, "half global; bool update() { global = 123; return true; }" |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 298 | "void main(inout half4 color) { global = 0; if (color.r > color.g && update()) " |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 299 | "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 1, 1, 1, 0); |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | DEF_TEST(SkSLInterpreterOr, r) { |
| 303 | test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) " |
| 304 | "color = half4(color.a); }", 2, 1, 0, 3, 3, 3, 3, 3); |
| 305 | test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) " |
| 306 | "color = half4(color.a); }", 1, 1, 0, 3, 3, 3, 3, 3); |
| 307 | test(r, "void main(inout half4 color) { if (color.r > color.g || color.g > color.b) " |
| 308 | "color = half4(color.a); }", 1, 1, 1, 3, 1, 1, 1, 3); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 309 | test(r, "half global; bool update() { global = 123; return true; }" |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 310 | "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) " |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 311 | "color = half4(color.a); color.a = global; }", 1, 1, 1, 3, 3, 3, 3, 123); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 312 | test(r, "half global; bool update() { global = 123; return true; }" |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 313 | "void main(inout half4 color) { global = 0; if (color.r > color.g || update()) " |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 314 | "color = half4(color.a); color.a = global; }", 2, 1, 1, 3, 3, 3, 3, 0); |
Ethan Nicholas | d166d2e | 2019-09-23 11:43:45 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 317 | DEF_TEST(SkSLInterpreterMatrix, r) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 318 | float in[16]; |
| 319 | float expected[16]; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 320 | |
| 321 | // Constructing matrix from scalar produces a diagonal matrix |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 322 | in[0] = 2.0f; |
| 323 | expected[0] = 4.0f; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 324 | test(r, "float main(float x) { float4x4 m = float4x4(x); return m[1][1] + m[1][2] + m[2][2]; }", |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 325 | in, expected); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 326 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 327 | // Constructing from a different-sized matrix fills the remaining space with the identity matrix |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 328 | expected[0] = 3.0f; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 329 | test(r, "float main(float x) {" |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 330 | "float2x2 m = float2x2(x);" |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 331 | "float4x4 m2 = float4x4(m);" |
| 332 | "return m2[0][0] + m2[3][3]; }", |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 333 | in, expected); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 334 | |
| 335 | // Constructing a matrix from vectors or scalars fills in values in column-major order |
| 336 | in[0] = 1.0f; |
| 337 | in[1] = 2.0f; |
| 338 | in[2] = 4.0f; |
| 339 | in[3] = 8.0f; |
| 340 | expected[0] = 6.0f; |
| 341 | test(r, "float main(float4 v) { float2x2 m = float2x2(v); return m[0][1] + m[1][0]; }", |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 342 | in, expected); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 343 | |
| 344 | expected[0] = 10.0f; |
| 345 | test(r, "float main(float4 v) {" |
| 346 | "float2x2 m = float2x2(v.x, v.y, v.w, v.z);" |
| 347 | "return m[0][1] + m[1][0]; }", |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 348 | in, expected); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 349 | |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 350 | // Initialize 16 values to be used as inputs to matrix tests |
| 351 | for (int i = 0; i < 16; ++i) { in[i] = (float)i; } |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 352 | |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 353 | // M+M, M-S, S-M |
| 354 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(2 * i); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 355 | test(r, "float4x4 main(float4x4 m) { return m + m; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 356 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(i + 3); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 357 | test(r, "float4x4 main(float4x4 m) { return m + 3.0; }", in, expected); |
| 358 | test(r, "float4x4 main(float4x4 m) { return 3.0 + m; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 359 | |
| 360 | // M-M, M-S, S-M |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 361 | for (int i = 0; i < 4; ++i) { expected[i] = 4.0f; } |
| 362 | test(r, "float2x2 main(float2x2 m1, float2x2 m2) { return m2 - m1; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 363 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(i - 3); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 364 | test(r, "float4x4 main(float4x4 m) { return m - 3.0; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 365 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(3 - i); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 366 | test(r, "float4x4 main(float4x4 m) { return 3.0 - m; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 367 | |
| 368 | // M*S, S*M, M/S, S/M |
| 369 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(i * 3); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 370 | test(r, "float4x4 main(float4x4 m) { return m * 3.0; }", in, expected); |
| 371 | test(r, "float4x4 main(float4x4 m) { return 3.0 * m; }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 372 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(i) / 2.0f; } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 373 | test(r, "float4x4 main(float4x4 m) { return m / 2.0; }", in, expected); |
Brian Osman | 5bdf525 | 2019-05-29 17:04:54 -0400 | [diff] [blame] | 374 | for (int i = 0; i < 16; ++i) { expected[i] = 1.0f / (float)(i + 1); } |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 375 | test(r, "float4x4 main(float4x4 m) { return 1.0 / (m + 1); }", in, expected); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 376 | |
| 377 | #if 0 |
| 378 | // Matrix negation - legal in GLSL, not in SkSL? |
| 379 | for (int i = 0; i < 16; ++i) { expected[i] = (float)(-i); } |
| 380 | test(r, "float4x4 main(float4x4 m) { return -m; }", in, 16, expected); |
| 381 | #endif |
| 382 | |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 383 | // M*V, V*M |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 384 | for (int i = 0; i < 3; ++i) { |
| 385 | expected[i] = 9.0f*i + 10.0f*(i+3) + 11.0f*(i+6); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 386 | } |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 387 | test(r, "float3 main(float3x3 m, float3 v) { return m * v; }", in, expected); |
| 388 | for (int i = 0; i < 3; ++i) { |
| 389 | expected[i] = 9.0f*(3*i) + 10.0f*(3*i+1) + 11.0f*(3*i+2); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 390 | } |
Brian Osman | c0f2b64 | 2020-12-22 13:35:55 -0500 | [diff] [blame] | 391 | test(r, "float3 main(float3x3 m, float3 v) { return v * m; }", in, expected); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 392 | |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 393 | // M*M |
| 394 | { |
Mike Reed | 3ef77dd | 2020-04-06 10:41:09 -0400 | [diff] [blame] | 395 | SkM44 m = SkM44::ColMajor(in); |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 396 | SkM44 m2; |
| 397 | float in2[16]; |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 398 | for (int i = 0; i < 16; ++i) { |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 399 | in2[i] = (i + 4) % 16; |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 400 | } |
Mike Reed | 3ef77dd | 2020-04-06 10:41:09 -0400 | [diff] [blame] | 401 | m2 = SkM44::ColMajor(in2); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 402 | m.setConcat(m, m2); |
| 403 | // Rearrange the columns on the RHS so we detect left-hand/right-hand errors |
| 404 | test(r, "float4x4 main(float4x4 m) { return m * float4x4(m[1], m[2], m[3], m[0]); }", |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 405 | in, (float*)&m); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 406 | } |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 407 | } |
| 408 | |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 409 | DEF_TEST(SkSLInterpreterTernary, r) { |
| 410 | test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }", |
| 411 | 0, 1, 2, 0, 2, 1, 2, 0); |
| 412 | test(r, "void main(inout half4 color) { color.r = color.g > color.b ? color.g : color.b; }", |
| 413 | 0, 3, 2, 0, 3, 3, 2, 0); |
| 414 | } |
| 415 | |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 416 | DEF_TEST(SkSLInterpreterCast, r) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 417 | union Val { |
| 418 | float f; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 419 | int32_t s; |
| 420 | }; |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 421 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 422 | Val input[2]; |
| 423 | Val expected[2]; |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 424 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 425 | input[0].s = 3; |
| 426 | input[1].s = -5; |
| 427 | expected[0].f = 3.0f; |
| 428 | expected[1].f = -5.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 429 | test(r, "float main(int x) { return float (x); }", (float*)input, (float*)expected); |
| 430 | test(r, "float2 main(int2 x) { return float2(x); }", (float*)input, (float*)expected); |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 431 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 432 | input[0].f = 3.0f; |
| 433 | input[1].f = -5.0f; |
| 434 | expected[0].s = 3; |
| 435 | expected[1].s = -5; |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 436 | test(r, "int main(float x) { return int (x); }", (float*)input, (float*)expected); |
| 437 | test(r, "int2 main(float2 x) { return int2(x); }", (float*)input, (float*)expected); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 438 | |
| 439 | input[0].s = 3; |
| 440 | expected[0].f = 3.0f; |
| 441 | expected[1].f = 3.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 442 | test(r, "float2 main(int x) { return float2(x); }", (float*)input, (float*)expected); |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 443 | } |
| 444 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 445 | DEF_TEST(SkSLInterpreterIf, r) { |
| 446 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0, |
| 447 | 5, 3, 0, 1); |
| 448 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0, |
| 449 | 5, 5, 0, 0); |
| 450 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0, |
| 451 | 5, 6, 0, 0); |
| 452 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0, |
| 453 | 3, 5, 0, 1); |
| 454 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0, |
| 455 | 5, 5, 0, 0); |
| 456 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0, |
| 457 | 6, 5, 0, 0); |
| 458 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0, |
| 459 | 5, 3, 0, 1); |
| 460 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0, |
| 461 | 5, 5, 0, 1); |
| 462 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0, |
| 463 | 5, 6, 0, 0); |
| 464 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0, |
| 465 | 3, 5, 0, 1); |
| 466 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0, |
| 467 | 5, 5, 0, 1); |
| 468 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0, |
| 469 | 6, 5, 0, 0); |
| 470 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0, |
| 471 | 2, 2, 0, 1); |
| 472 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0, |
| 473 | 2, -2, 0, 0); |
| 474 | test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0, |
| 475 | 2, 2, 0, 0); |
| 476 | test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0, |
| 477 | 2, -2, 0, 1); |
Brian Osman | e5bbce2 | 2019-09-23 12:38:40 -0400 | [diff] [blame] | 478 | test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, 2, 0, 0, |
| 479 | 2, 2, 0, 0); |
| 480 | test(r, "void main(inout half4 color) { if (!(color.r == color.g)) color.a = 1; }", 2, -2, 0, 0, |
| 481 | 2, -2, 0, 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 482 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else " |
| 483 | "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1); |
| 484 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else " |
| 485 | "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2); |
| 486 | } |
| 487 | |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 488 | DEF_TEST(SkSLInterpreterIfVector, r) { |
| 489 | test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }", |
| 490 | 1, 2, 1, 2, 1, 2, 1, 1); |
| 491 | test(r, "void main(inout half4 color) { if (color.rg == color.ba) color.a = 1; }", |
| 492 | 1, 2, 3, 2, 1, 2, 3, 2); |
| 493 | test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }", |
| 494 | 1, 2, 1, 2, 1, 2, 1, 2); |
| 495 | test(r, "void main(inout half4 color) { if (color.rg != color.ba) color.a = 1; }", |
| 496 | 1, 2, 3, 2, 1, 2, 3, 1); |
| 497 | } |
| 498 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 499 | DEF_TEST(SkSLInterpreterFor, r) { |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 500 | test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += half(i); }", |
Brian Osman | f4a7773 | 2020-12-28 09:03:00 -0500 | [diff] [blame] | 501 | 0, 0, 0, 0, |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 502 | 55, 0, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 503 | test(r, |
| 504 | "void main(inout half4 color) {" |
| 505 | " for (int i = 1; i <= 10; ++i)" |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 506 | " for (int j = 1; j <= 10; ++j)" |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 507 | " if (j >= i) { color.r += half(j); }" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 508 | "}", |
| 509 | 0, 0, 0, 0, |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 510 | 385, 0, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 511 | test(r, |
| 512 | "void main(inout half4 color) {" |
| 513 | " for (int i = 1; i <= 10; ++i)" |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 514 | " for (int j = 1; j < 20 ; ++j) {" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 515 | " if (i == j) continue;" |
| 516 | " if (j > 10) break;" |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 517 | " color.r += half(j);" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 518 | " }" |
| 519 | "}", |
| 520 | 0, 0, 0, 0, |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 521 | 495, 0, 0, 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 522 | } |
| 523 | |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 524 | DEF_TEST(SkSLInterpreterPrefixPostfix, r) { |
| 525 | test(r, "void main(inout half4 color) { color.r = ++color.g; }", 1, 2, 3, 4, 3, 3, 3, 4); |
| 526 | test(r, "void main(inout half4 color) { color.r = color.g++; }", 1, 2, 3, 4, 2, 3, 3, 4); |
| 527 | } |
| 528 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 529 | DEF_TEST(SkSLInterpreterSwizzle, r) { |
| 530 | test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1); |
| 531 | test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7, |
| 532 | 6, 4); |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 533 | test(r, "void main(inout half4 color) { color.bgr = half3(5, 6, 7); }", 1, 2, 3, 4, 7, 6, |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 534 | 5, 4); |
| 535 | } |
| 536 | |
| 537 | DEF_TEST(SkSLInterpreterGlobal, r) { |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 538 | test(r, "int x; void main(inout half4 color) { x = 10; color.b = half(x); }", 1, 2, 3, 4, 1, 2, |
| 539 | 10, 4); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 540 | test(r, "float4 x; void main(inout float4 color) { x = color * 2; color = x; }", |
| 541 | 1, 2, 3, 4, 2, 4, 6, 8); |
| 542 | test(r, "float4 x; void main(inout float4 color) { x = float4(5, 6, 7, 8); color = x.wzyx; }", |
| 543 | 1, 2, 3, 4, 8, 7, 6, 5); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 544 | test(r, "float4 x; void main(inout float4 color) { x.wzyx = float4(5, 6, 7, 8); color = x; }", |
| 545 | 1, 2, 3, 4, 8, 7, 6, 5); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 546 | } |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 547 | |
| 548 | DEF_TEST(SkSLInterpreterGeneric, r) { |
| 549 | float value1 = 5; |
| 550 | float expected1 = 25; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 551 | test(r, "float main(float x) { return x * x; }", &value1, &expected1); |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 552 | float value2[2] = { 5, 25 }; |
| 553 | float expected2[2] = { 25, 625 }; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 554 | test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", value2, expected2); |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 555 | } |
Brian Osman | d369a5e | 2019-05-09 13:13:25 -0400 | [diff] [blame] | 556 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 557 | DEF_TEST(SkSLInterpreterCompound, r) { |
| 558 | struct RectAndColor { SkIRect fRect; SkColor4f fColor; }; |
| 559 | struct ManyRects { int fNumRects; RectAndColor fRects[4]; }; |
| 560 | |
| 561 | const char* src = |
| 562 | // Some struct definitions |
| 563 | "struct Point { int x; int y; };\n" |
| 564 | "struct Rect { Point p0; Point p1; };\n" |
| 565 | "struct RectAndColor { Rect r; float4 color; };\n" |
| 566 | |
| 567 | // Structs as globals, parameters, return values |
| 568 | "RectAndColor temp;\n" |
| 569 | "int rect_height(Rect r) { return r.p1.y - r.p0.y; }\n" |
| 570 | "RectAndColor make_blue_rect(int w, int h) {\n" |
| 571 | " temp.r.p0.x = temp.r.p0.y = 0;\n" |
| 572 | " temp.r.p1.x = w; temp.r.p1.y = h;\n" |
| 573 | " temp.color = float4(0, 1, 0, 1);\n" |
| 574 | " return temp;\n" |
| 575 | "}\n" |
| 576 | |
| 577 | // Initialization and assignment of types larger than 4 slots |
| 578 | "RectAndColor init_big(RectAndColor r) { RectAndColor s = r; return s; }\n" |
| 579 | "RectAndColor copy_big(RectAndColor r) { RectAndColor s; s = r; return s; }\n" |
| 580 | |
| 581 | // Same for arrays, including some non-constant indexing |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 582 | "int median(int a[15]) { return a[7]; }\n" |
John Stiles | 076e9a2 | 2020-12-03 10:37:45 -0500 | [diff] [blame] | 583 | |
| 584 | "float tempFloats[8];\n" |
| 585 | "float sums(float a[8]) {\n" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 586 | " tempFloats[0] = a[0];\n" |
| 587 | " for (int i = 1; i < 8; ++i) { tempFloats[i] = tempFloats[i - 1] + a[i]; }\n" |
John Stiles | 076e9a2 | 2020-12-03 10:37:45 -0500 | [diff] [blame] | 588 | " return tempFloats[7];\n" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 589 | "}\n" |
| 590 | |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 591 | // Uniforms, array-of-structs |
Ethan Nicholas | 31cff27 | 2019-09-26 13:04:48 -0400 | [diff] [blame] | 592 | "uniform Rect gRects[4];\n" |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 593 | "Rect get_rect_2() { return gRects[2]; }\n" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 594 | |
| 595 | // Kitchen sink (swizzles, inout, SoAoS) |
| 596 | "struct ManyRects { int numRects; RectAndColor rects[4]; };\n" |
| 597 | "void fill_rects(inout ManyRects mr) {\n" |
Brian Osman | 77ba810 | 2021-01-12 17:15:30 -0500 | [diff] [blame] | 598 | " for (int i = 0; i < 4; ++i) {\n" |
| 599 | " if (i >= mr.numRects) { break; }\n" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 600 | " mr.rects[i].r = gRects[i];\n" |
John Stiles | af9b58e | 2021-01-13 17:48:36 -0500 | [diff] [blame] | 601 | " float b = float(mr.rects[i].r.p1.y);\n" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 602 | " mr.rects[i].color = float4(b, b, b, b);\n" |
| 603 | " }\n" |
| 604 | "}\n"; |
| 605 | |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 606 | ProgramBuilder program(r, src); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 607 | |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 608 | auto rect_height = SkSL::Program_GetFunction(*program, "rect_height"), |
| 609 | make_blue_rect = SkSL::Program_GetFunction(*program, "make_blue_rect"), |
| 610 | median = SkSL::Program_GetFunction(*program, "median"), |
| 611 | sums = SkSL::Program_GetFunction(*program, "sums"), |
| 612 | get_rect_2 = SkSL::Program_GetFunction(*program, "get_rect_2"), |
| 613 | fill_rects = SkSL::Program_GetFunction(*program, "fill_rects"); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 614 | |
| 615 | SkIRect gRects[4] = { { 1,2,3,4 }, { 5,6,7,8 }, { 9,10,11,12 }, { 13,14,15,16 } }; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 616 | |
| 617 | auto build = [&](const SkSL::FunctionDefinition* fn) { |
| 618 | skvm::Builder b; |
| 619 | skvm::Ptr uniformPtr = b.uniform(); |
| 620 | skvm::Val uniforms[16]; |
| 621 | for (int i = 0; i < 16; ++i) { |
| 622 | uniforms[i] = b.uniform32(uniformPtr, i * sizeof(int)).id; |
| 623 | } |
| 624 | SkSL::ProgramToSkVM(*program, *fn, &b, uniforms); |
| 625 | return b.done(); |
| 626 | }; |
| 627 | |
| 628 | struct Args { |
| 629 | Args(void* uniformData) { fArgs.push_back(uniformData); } |
| 630 | void add(void* base, int n) { |
| 631 | for (int i = 0; i < n; ++i) { |
| 632 | fArgs.push_back(SkTAddOffset<void>(base, i * sizeof(float))); |
| 633 | } |
| 634 | } |
| 635 | std::vector<void*> fArgs; |
| 636 | }; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 637 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 638 | { |
| 639 | SkIRect in = SkIRect::MakeXYWH(10, 10, 20, 30); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 640 | int out = 0; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 641 | skvm::Program p = build(rect_height); |
| 642 | Args args(gRects); |
| 643 | args.add(&in, 4); |
| 644 | args.add(&out, 1); |
| 645 | p.eval(1, args.fArgs.data()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 646 | REPORTER_ASSERT(r, out == 30); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | { |
| 650 | int in[2] = { 15, 25 }; |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 651 | RectAndColor out; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 652 | skvm::Program p = build(make_blue_rect); |
| 653 | Args args(gRects); |
| 654 | args.add(&in, 2); |
| 655 | args.add(&out, 8); |
| 656 | p.eval(1, args.fArgs.data()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 657 | REPORTER_ASSERT(r, out.fRect.width() == 15); |
| 658 | REPORTER_ASSERT(r, out.fRect.height() == 25); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 659 | SkColor4f blue = { 0.0f, 1.0f, 0.0f, 1.0f }; |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 660 | REPORTER_ASSERT(r, out.fColor == blue); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | { |
| 664 | int in[15] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 665 | int out = 0; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 666 | skvm::Program p = build(median); |
| 667 | Args args(gRects); |
| 668 | args.add(&in, 15); |
| 669 | args.add(&out, 1); |
| 670 | p.eval(1, args.fArgs.data()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 671 | REPORTER_ASSERT(r, out == 8); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 674 | // TODO: Doesn't work until SkVM generator supports indexing-by-loop variable |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 675 | if (false) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 676 | float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
John Stiles | 076e9a2 | 2020-12-03 10:37:45 -0500 | [diff] [blame] | 677 | float out = 0; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 678 | skvm::Program p = build(sums); |
| 679 | Args args(gRects); |
| 680 | args.add(&in, 8); |
| 681 | args.add(&out, 1); |
| 682 | p.eval(1, args.fArgs.data()); |
John Stiles | 076e9a2 | 2020-12-03 10:37:45 -0500 | [diff] [blame] | 683 | REPORTER_ASSERT(r, out == static_cast<float>((7 + 1) * (7 + 2) / 2)); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | { |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 687 | SkIRect out = SkIRect::MakeEmpty(); |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 688 | skvm::Program p = build(get_rect_2); |
| 689 | Args args(gRects); |
| 690 | args.add(&out, 4); |
| 691 | p.eval(1, args.fArgs.data()); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 692 | REPORTER_ASSERT(r, out == gRects[2]); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Brian Osman | 9333c87 | 2021-01-13 15:06:17 -0500 | [diff] [blame] | 695 | // TODO: Doesn't work until SkVM generator supports indexing-by-loop variable |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 696 | if (false) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 697 | ManyRects in; |
| 698 | memset(&in, 0, sizeof(in)); |
| 699 | in.fNumRects = 2; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 700 | skvm::Program p = build(fill_rects); |
| 701 | Args args(gRects); |
| 702 | args.add(&in, 33); |
| 703 | p.eval(1, args.fArgs.data()); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 704 | ManyRects expected; |
| 705 | memset(&expected, 0, sizeof(expected)); |
| 706 | expected.fNumRects = 2; |
| 707 | for (int i = 0; i < 2; ++i) { |
| 708 | expected.fRects[i].fRect = gRects[i]; |
| 709 | float c = gRects[i].fBottom; |
| 710 | expected.fRects[i].fColor = { c, c, c, c }; |
| 711 | } |
| 712 | REPORTER_ASSERT(r, memcmp(&in, &expected, sizeof(in)) == 0); |
| 713 | } |
| 714 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 715 | |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 716 | static void expect_failure(skiatest::Reporter* r, const char* src) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 717 | GrShaderCaps caps(GrContextOptions{}); |
| 718 | SkSL::Compiler compiler(&caps); |
John Stiles | 759880a | 2020-09-11 12:10:43 -0400 | [diff] [blame] | 719 | SkSL::Program::Settings settings; |
| 720 | auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, |
| 721 | SkSL::String(src), settings); |
Brian Osman | c39a143 | 2021-01-11 13:15:23 -0500 | [diff] [blame] | 722 | REPORTER_ASSERT(r, !program); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static void expect_run_failure(skiatest::Reporter* r, const char* src, float* in) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 726 | GrShaderCaps caps(GrContextOptions{}); |
| 727 | SkSL::Compiler compiler(&caps); |
John Stiles | 759880a | 2020-09-11 12:10:43 -0400 | [diff] [blame] | 728 | SkSL::Program::Settings settings; |
| 729 | auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, |
| 730 | SkSL::String(src), settings); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 731 | REPORTER_ASSERT(r, program); |
| 732 | |
| 733 | auto byteCode = compiler.toByteCode(*program); |
| 734 | REPORTER_ASSERT(r, byteCode); |
| 735 | |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 736 | auto fun = byteCode->getFunction("main"); |
| 737 | bool result = byteCode->run(fun, in, fun->getParameterCount(), nullptr, 0, nullptr, 0); |
| 738 | REPORTER_ASSERT(r, !result); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Brian Osman | 818fd6d | 2020-12-30 15:06:22 -0500 | [diff] [blame] | 741 | DEF_TEST(SkSLInterpreterRestrictLoops, r) { |
| 742 | // while and do-while loops are not allowed |
| 743 | expect_failure(r, "void main(inout float x) { while (x < 1) { x++; } }"); |
| 744 | expect_failure(r, "void main(inout float x) { do { x++; } while (x < 1); }"); |
| 745 | } |
| 746 | |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 747 | DEF_TEST(SkSLInterpreterRestrictFunctionCalls, r) { |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 748 | // Ensure that simple recursion is not allowed |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 749 | expect_failure(r, "float main() { return main() + 1; }"); |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 750 | |
| 751 | // Ensure that calls to undefined functions are not allowed (to prevent mutual recursion) |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 752 | expect_failure(r, "float foo(); float bar() { return foo(); } float foo() { return bar(); }"); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 753 | } |
| 754 | |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 755 | DEF_TEST(SkSLInterpreterReturnThenCall, r) { |
| 756 | // Test that early returns disable execution in subsequently called functions |
| 757 | const char* src = R"( |
| 758 | float y; |
| 759 | void inc () { ++y; } |
| 760 | void maybe_inc() { if (y < 0) return; inc(); } |
| 761 | void main(inout float x) { y = x; maybe_inc(); x = y; } |
| 762 | )"; |
| 763 | |
| 764 | ProgramBuilder program(r, src); |
| 765 | const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main"); |
| 766 | REPORTER_ASSERT(r, main); |
| 767 | |
| 768 | skvm::Builder b; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 769 | SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 770 | skvm::Program p = b.done(); |
| 771 | |
| 772 | float xs[] = { -2.0f, 0.0f, 3.0f, -1.0f }; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 773 | p.eval(4, xs); |
Brian Osman | 54515b7 | 2021-01-07 14:38:08 -0500 | [diff] [blame] | 774 | |
| 775 | REPORTER_ASSERT(r, xs[0] == -2.0f); |
| 776 | REPORTER_ASSERT(r, xs[1] == 1.0f); |
| 777 | REPORTER_ASSERT(r, xs[2] == 4.0f); |
| 778 | REPORTER_ASSERT(r, xs[3] == -1.0f); |
| 779 | } |
| 780 | |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 781 | DEF_TEST(SkSLInterpreterEarlyReturn, r) { |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 782 | // Test early returns with divergent control flow |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 783 | const char* src = "float main(float x, float y) { if (x < y) { return x; } return y; }"; |
| 784 | |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 785 | ProgramBuilder program(r, src); |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 786 | |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 787 | const SkSL::FunctionDefinition* main = SkSL::Program_GetFunction(*program, "main"); |
| 788 | REPORTER_ASSERT(r, main); |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 789 | |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 790 | skvm::Builder b; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 791 | SkSL::ProgramToSkVM(*program, *main, &b, /*uniforms=*/{}); |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 792 | skvm::Program p = b.done(); |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 793 | |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 794 | float xs[] = { 1.0f, 3.0f }, |
| 795 | ys[] = { 2.0f, 2.0f }; |
| 796 | float rets[2]; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 797 | p.eval(2, xs, ys, rets); |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 798 | |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 799 | REPORTER_ASSERT(r, rets[0] == 1.0f); |
Brian Osman | e2416d7 | 2021-01-06 16:36:56 -0500 | [diff] [blame] | 800 | REPORTER_ASSERT(r, rets[1] == 2.0f); |
Mike Klein | 51dc285 | 2020-10-15 19:41:20 -0500 | [diff] [blame] | 801 | } |
| 802 | |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 803 | DEF_TEST(SkSLInterpreterArrayBounds, r) { |
John Stiles | 1b27c3d | 2020-12-07 12:14:55 -0500 | [diff] [blame] | 804 | // Out of bounds array access at compile time prevents a program from being generated at all |
| 805 | // (tested in ArrayIndexOutOfRange.sksl). |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 806 | |
John Stiles | 1b27c3d | 2020-12-07 12:14:55 -0500 | [diff] [blame] | 807 | // Out of bounds array access at runtime is pinned, and we don't update any inout data. |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 808 | float in[3] = { -1.0f, 1.0f, 2.0f }; |
| 809 | expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in); |
| 810 | REPORTER_ASSERT(r, in[0] == -1.0f && in[1] == 1.0f && in[2] == 2.0f); |
| 811 | |
| 812 | in[0] = 3.0f; |
| 813 | expect_run_failure(r, "void main(inout float data[3]) { data[int(data[0])] = 0; }", in); |
| 814 | REPORTER_ASSERT(r, in[0] == 3.0f && in[1] == 1.0f && in[2] == 2.0f); |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 815 | } |
| 816 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 817 | DEF_TEST(SkSLInterpreterFunctions, r) { |
| 818 | const char* src = |
| 819 | "float sqr(float x) { return x * x; }\n" |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 820 | "float sub(float x, float y) { return x - y; }\n" |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 821 | "float main(float x) { return sub(sqr(x), x); }\n" |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 822 | |
| 823 | // Different signatures |
Brian Osman | 15c98cb | 2020-02-27 18:36:57 +0000 | [diff] [blame] | 824 | "float dot(float2 a, float2 b) { return a.x*b.x + a.y*b.y; }\n" |
| 825 | "float dot(float3 a, float3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }\n" |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 826 | "float dot3_test(float x) { return dot(float3(x, x + 1, x + 2), float3(1, -1, 2)); }\n" |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 827 | "float dot2_test(float x) { return dot(float2(x, x + 1), float2(1, -1)); }\n"; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 828 | |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 829 | ProgramBuilder program(r, src); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 830 | |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 831 | auto sub = SkSL::Program_GetFunction(*program, "sub"); |
| 832 | auto sqr = SkSL::Program_GetFunction(*program, "sqr"); |
| 833 | auto main = SkSL::Program_GetFunction(*program, "main"); |
| 834 | auto tan = SkSL::Program_GetFunction(*program, "tan"); |
| 835 | auto dot3 = SkSL::Program_GetFunction(*program, "dot3_test"); |
| 836 | auto dot2 = SkSL::Program_GetFunction(*program, "dot2_test"); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 837 | |
| 838 | REPORTER_ASSERT(r, sub); |
| 839 | REPORTER_ASSERT(r, sqr); |
| 840 | REPORTER_ASSERT(r, main); |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 841 | REPORTER_ASSERT(r, !tan); // Getting a non-existent function should return nullptr |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 842 | REPORTER_ASSERT(r, dot3); |
| 843 | REPORTER_ASSERT(r, dot2); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 844 | |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 845 | auto test_fn = [&](const SkSL::FunctionDefinition* fn, float in, float expected) { |
| 846 | skvm::Builder b; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 847 | SkSL::ProgramToSkVM(*program, *fn, &b, /*uniforms=*/{}); |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 848 | skvm::Program p = b.done(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 849 | |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 850 | float out = 0.0f; |
Brian Osman | c92df39 | 2021-01-11 13:16:28 -0500 | [diff] [blame] | 851 | p.eval(1, &in, &out); |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 852 | REPORTER_ASSERT(r, out == expected); |
| 853 | }; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 854 | |
Brian Osman | 11233d2 | 2021-01-11 10:39:58 -0500 | [diff] [blame] | 855 | test_fn(main, 3.0f, 6.0f); |
| 856 | test_fn(dot3, 3.0f, 9.0f); |
| 857 | test_fn(dot2, 3.0f, -1.0f); |
Brian Osman | 8c80b19 | 2020-02-06 10:49:38 -0500 | [diff] [blame] | 858 | } |
| 859 | |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 860 | DEF_TEST(SkSLInterpreterOutParams, r) { |
| 861 | test(r, |
| 862 | "void oneAlpha(inout half4 color) { color.a = 1; }" |
| 863 | "void main(inout half4 color) { oneAlpha(color); }", |
| 864 | 0, 0, 0, 0, 0, 0, 0, 1); |
| 865 | test(r, |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 866 | "half2 tricky(half x, half y, inout half2 color, half z) {" |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 867 | " color.xy = color.yx;" |
| 868 | " return half2(x + y, z);" |
| 869 | "}" |
| 870 | "void main(inout half4 color) {" |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 871 | " half2 t = tricky(1, 2, color.rb, 5);" |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 872 | " color.ga = t;" |
| 873 | "}", |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 874 | 1, 2, 3, 4, 3, 3, 1, 5); |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 875 | } |
| 876 | |
Brian Osman | 401a366 | 2020-09-29 13:20:04 -0400 | [diff] [blame] | 877 | DEF_TEST(SkSLInterpreterSwizzleSingleLvalue, r) { |
| 878 | // Add in your SkSL here. |
| 879 | test(r, |
| 880 | "void main(inout half4 color) { color.xywz = half4(1,2,3,4); }", |
| 881 | 0, 0, 0, 0, 1, 2, 4, 3); |
| 882 | } |
| 883 | |
| 884 | DEF_TEST(SkSLInterpreterSwizzleDoubleLvalue, r) { |
| 885 | // Add in your SkSL here. |
| 886 | test(r, |
| 887 | "void main(inout half4 color) { color.xywz.yxzw = half4(1,2,3,4); }", |
| 888 | 0, 0, 0, 0, 2, 1, 4, 3); |
| 889 | } |
| 890 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 891 | DEF_TEST(SkSLInterpreterMathFunctions, r) { |
Brian Osman | b380e71 | 2019-07-24 17:02:39 -0400 | [diff] [blame] | 892 | float value[4], expected[4]; |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 893 | |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 894 | value[0] = 0.0f; expected[0] = 0.0f; |
| 895 | test(r, "float main(float x) { return sin(x); }", value, expected); |
| 896 | test(r, "float main(float x) { return tan(x); }", value, expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 897 | |
Brian Osman | b380e71 | 2019-07-24 17:02:39 -0400 | [diff] [blame] | 898 | value[0] = 0.0f; expected[0] = 1.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 899 | test(r, "float main(float x) { return cos(x); }", value, expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 900 | |
Brian Osman | b380e71 | 2019-07-24 17:02:39 -0400 | [diff] [blame] | 901 | value[0] = 25.0f; expected[0] = 5.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 902 | test(r, "float main(float x) { return sqrt(x); }", value, expected); |
Brian Osman | b380e71 | 2019-07-24 17:02:39 -0400 | [diff] [blame] | 903 | |
| 904 | value[0] = 90.0f; expected[0] = sk_float_degrees_to_radians(value[0]); |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 905 | test(r, "float main(float x) { return radians(x); }", value, expected); |
Brian Osman | b380e71 | 2019-07-24 17:02:39 -0400 | [diff] [blame] | 906 | |
| 907 | value[0] = 1.0f; value[1] = -1.0f; |
| 908 | expected[0] = 1.0f / SK_FloatSqrt2; expected[1] = -1.0f / SK_FloatSqrt2; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 909 | test(r, "float2 main(float2 x) { return normalize(x); }", value, expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Brian Osman | fba386b | 2019-06-20 14:54:15 -0400 | [diff] [blame] | 912 | DEF_TEST(SkSLInterpreterVoidFunction, r) { |
| 913 | test(r, |
| 914 | "half x; void foo() { x = 1.0; }" |
| 915 | "void main(inout half4 color) { foo(); color.r = x; }", |
| 916 | 0, 0, 0, 0, 1, 0, 0, 0); |
| 917 | } |
| 918 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 919 | DEF_TEST(SkSLInterpreterMix, r) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 920 | float value, expected; |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 921 | |
| 922 | value = 0.5f; expected = 0.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 923 | test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 924 | value = 0.75f; expected = 5.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 925 | test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 926 | value = 2.0f; expected = 30.0f; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 927 | test(r, "float main(float x) { return mix(-10, 10, x); }", &value, &expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 928 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 929 | float valueVectors[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }, |
| 930 | expectedVector[] = { 3.0f, 4.0f, 5.0f, 6.0f }; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 931 | test(r, "float4 main(float4 x, float4 y) { return mix(x, y, 0.5); }", valueVectors, |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 932 | expectedVector); |
| 933 | } |
| 934 | |
| 935 | DEF_TEST(SkSLInterpreterCross, r) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 936 | float args[] = { 1.0f, 4.0f, -6.0f, -2.0f, 7.0f, -3.0f }; |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 937 | SkV3 cross = SkV3::Cross({args[0], args[1], args[2]}, |
| 938 | {args[3], args[4], args[5]}); |
| 939 | float expected[] = { cross.x, cross.y, cross.z }; |
Brian Osman | b23d66e | 2019-09-27 10:25:57 -0400 | [diff] [blame] | 940 | test(r, "float3 main(float3 x, float3 y) { return cross(x, y); }", args, expected); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 941 | } |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 942 | |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 943 | DEF_TEST(SkSLInterpreterInverse, r) { |
| 944 | { |
| 945 | SkMatrix m; |
| 946 | m.setRotate(30).postScale(1, 2); |
| 947 | float args[4] = { m[0], m[3], m[1], m[4] }; |
| 948 | SkAssertResult(m.invert(&m)); |
| 949 | float expt[4] = { m[0], m[3], m[1], m[4] }; |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 950 | test(r, "float2x2 main(float2x2 m) { return inverse(m); }", args, expt, false); |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 951 | } |
| 952 | { |
| 953 | SkMatrix m; |
| 954 | m.setRotate(30).postScale(1, 2).postTranslate(1, 2); |
| 955 | float args[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] }; |
| 956 | SkAssertResult(m.invert(&m)); |
| 957 | float expt[9] = { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] }; |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 958 | test(r, "float3x3 main(float3x3 m) { return inverse(m); }", args, expt, false); |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 959 | } |
| 960 | { |
| 961 | float args[16], expt[16]; |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 962 | // just some crazy thing that is invertible |
Mike Reed | b26b4e7 | 2020-01-22 14:31:21 -0500 | [diff] [blame] | 963 | SkM44 m = {1, 2, 3, 4, 1, 2, 0, 3, 1, 0, 1, 4, 1, 3, 2, 0}; |
| 964 | m.getColMajor(args); |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 965 | SkAssertResult(m.invert(&m)); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 966 | m.getColMajor(expt); |
| 967 | test(r, "float4x4 main(float4x4 m) { return inverse(m); }", args, expt, false); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | DEF_TEST(SkSLInterpreterDot, r) { |
| 972 | float args[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f }; |
| 973 | float expected = args[0] * args[2] + |
| 974 | args[1] * args[3]; |
| 975 | test(r, "float main(float2 x, float2 y) { return dot(x, y); }", args, &expected); |
| 976 | |
| 977 | expected = args[0] * args[3] + |
| 978 | args[1] * args[4] + |
| 979 | args[2] * args[5]; |
| 980 | test(r, "float main(float3 x, float3 y) { return dot(x, y); }", args, &expected); |
| 981 | |
| 982 | expected = args[0] * args[4] + |
| 983 | args[1] * args[5] + |
| 984 | args[2] * args[6] + |
| 985 | args[3] * args[7]; |
| 986 | test(r, "float main(float4 x, float4 y) { return dot(x, y); }", args, &expected); |
| 987 | } |
| 988 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 989 | class ExternalSqrt : public SkSL::ExternalFunction { |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 990 | public: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 991 | ExternalSqrt(const char* name, SkSL::Compiler& compiler) |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 992 | : INHERITED(name, *compiler.context().fTypes.fFloat) |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 993 | , fCompiler(compiler) {} |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 994 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 995 | int callParameterCount() const override { return 1; } |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 996 | |
| 997 | void getCallParameterTypes(const SkSL::Type** outTypes) const override { |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 998 | outTypes[0] = fCompiler.context().fTypes.fFloat.get(); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
John Stiles | 534d799 | 2020-08-18 00:06:01 -0400 | [diff] [blame] | 1001 | void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1002 | outReturn[0] = sqrt(arguments[0]); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | private: |
| 1006 | SkSL::Compiler& fCompiler; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1007 | using INHERITED = SkSL::ExternalFunction; |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1008 | }; |
| 1009 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1010 | DEF_TEST(SkSLInterpreterExternalFunction, r) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1011 | GrShaderCaps caps(GrContextOptions{}); |
| 1012 | SkSL::Compiler compiler(&caps); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1013 | SkSL::Program::Settings settings; |
| 1014 | const char* src = "float main() {" |
| 1015 | " return external(25);" |
| 1016 | "}"; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1017 | std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions; |
| 1018 | externalFunctions.push_back(std::make_unique<ExternalSqrt>("external", compiler)); |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1019 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1020 | SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1021 | REPORTER_ASSERT(r, program); |
| 1022 | if (program) { |
| 1023 | std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program); |
| 1024 | REPORTER_ASSERT(r, !compiler.errorCount()); |
| 1025 | if (compiler.errorCount() > 0) { |
| 1026 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 1027 | return; |
| 1028 | } |
| 1029 | const SkSL::ByteCodeFunction* main = byteCode->getFunction("main"); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1030 | float out; |
| 1031 | SkAssertResult(byteCode->run(main, nullptr, 0, &out, 1, nullptr, 0)); |
| 1032 | REPORTER_ASSERT(r, out == 5.0); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1033 | } else { |
| 1034 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 1035 | } |
| 1036 | } |
| 1037 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1038 | class ExternalSqrt4 : public SkSL::ExternalFunction { |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1039 | public: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1040 | ExternalSqrt4(const char* name, SkSL::Compiler& compiler) |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1041 | : INHERITED(name, *compiler.context().fTypes.fFloat4) |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1042 | , fCompiler(compiler) {} |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1043 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1044 | int callParameterCount() const override { return 1; } |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1045 | |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1046 | void getCallParameterTypes(const SkSL::Type** outTypes) const override { |
John Stiles | 54e7c05 | 2021-01-11 14:22:36 -0500 | [diff] [blame] | 1047 | outTypes[0] = fCompiler.context().fTypes.fFloat4.get(); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1048 | } |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1049 | |
John Stiles | 534d799 | 2020-08-18 00:06:01 -0400 | [diff] [blame] | 1050 | void call(int /*unusedIndex*/, float* arguments, float* outReturn) const override { |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1051 | outReturn[0] = sqrt(arguments[0]); |
| 1052 | outReturn[1] = sqrt(arguments[1]); |
| 1053 | outReturn[2] = sqrt(arguments[2]); |
| 1054 | outReturn[3] = sqrt(arguments[3]); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1055 | } |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1056 | |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1057 | private: |
| 1058 | SkSL::Compiler& fCompiler; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1059 | using INHERITED = SkSL::ExternalFunction; |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1060 | }; |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1061 | |
| 1062 | |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1063 | DEF_TEST(SkSLInterpreterExternalFunctionVector, r) { |
Brian Osman | 0006ad0 | 2020-11-18 15:38:39 -0500 | [diff] [blame] | 1064 | GrShaderCaps caps(GrContextOptions{}); |
| 1065 | SkSL::Compiler compiler(&caps); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1066 | SkSL::Program::Settings settings; |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1067 | const char* src = |
| 1068 | "float4 main() {" |
| 1069 | " return external(float4(1, 4, 9, 16));" |
| 1070 | "}"; |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1071 | std::vector<std::unique_ptr<SkSL::ExternalFunction>> externalFunctions; |
| 1072 | externalFunctions.push_back(std::make_unique<ExternalSqrt4>("external", compiler)); |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame] | 1073 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 1074 | SkSL::Program::kGeneric_Kind, SkSL::String(src), settings, &externalFunctions); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1075 | REPORTER_ASSERT(r, program); |
| 1076 | if (program) { |
| 1077 | std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program); |
| 1078 | REPORTER_ASSERT(r, !compiler.errorCount()); |
| 1079 | if (compiler.errorCount() > 0) { |
| 1080 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 1081 | return; |
| 1082 | } |
| 1083 | const SkSL::ByteCodeFunction* main = byteCode->getFunction("main"); |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 1084 | float out[4]; |
| 1085 | SkAssertResult(byteCode->run(main, nullptr, 0, out, 4, nullptr, 0)); |
| 1086 | REPORTER_ASSERT(r, out[0] == 1.0); |
| 1087 | REPORTER_ASSERT(r, out[1] == 2.0); |
| 1088 | REPORTER_ASSERT(r, out[2] == 3.0); |
| 1089 | REPORTER_ASSERT(r, out[3] == 4.0); |
Ben Wagner | 470e0ac | 2020-01-22 16:59:21 -0500 | [diff] [blame] | 1090 | } else { |
| 1091 | printf("%s\n%s", src, compiler.errorText().c_str()); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1092 | } |
| 1093 | } |
Brian Osman | 1f71f43 | 2020-11-18 15:44:46 -0500 | [diff] [blame] | 1094 | |
| 1095 | #endif // SK_ENABLE_SKSL_INTERPRETER |