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