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