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