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