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