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