Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLCompiler.h" |
| 9 | #include "src/sksl/SkSLInterpreter.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "tests/Test.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 12 | |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 13 | void test(skiatest::Reporter* r, const char* src, SkSL::Interpreter::Value* in, int expectedCount, |
| 14 | SkSL::Interpreter::Value* expected) { |
| 15 | SkSL::Compiler compiler; |
| 16 | SkSL::Program::Settings settings; |
| 17 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
| 18 | SkSL::Program::kGeneric_Kind, |
| 19 | SkSL::String(src), settings); |
| 20 | REPORTER_ASSERT(r, program); |
| 21 | if (program) { |
| 22 | std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program); |
| 23 | REPORTER_ASSERT(r, !compiler.errorCount()); |
| 24 | if (compiler.errorCount() > 0) { |
| 25 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 26 | return; |
| 27 | } |
| 28 | SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get(); |
| 29 | SkSL::Interpreter interpreter(std::move(program), std::move(byteCode)); |
| 30 | SkSL::Interpreter::Value* out = interpreter.run(*main, in, nullptr); |
| 31 | bool valid = !memcmp(out, expected, sizeof(SkSL::Interpreter::Value) * expectedCount); |
| 32 | if (!valid) { |
| 33 | printf("for program: %s\n", src); |
| 34 | printf(" expected ("); |
| 35 | const char* separator = ""; |
| 36 | for (int i = 0; i < expectedCount; ++i) { |
| 37 | printf("%s%f", separator, expected[i].fFloat); |
| 38 | separator = ", "; |
| 39 | } |
| 40 | printf("), but received ("); |
| 41 | separator = ""; |
| 42 | for (int i = 0; i < expectedCount; ++i) { |
| 43 | printf("%s%f", separator, out[i].fFloat); |
| 44 | separator = ", "; |
| 45 | } |
| 46 | printf(")\n"); |
| 47 | } |
| 48 | REPORTER_ASSERT(r, valid); |
| 49 | } else { |
| 50 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 51 | } |
| 52 | } |
| 53 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 54 | void test(skiatest::Reporter* r, const char* src, float inR, float inG, float inB, float inA, |
| 55 | float expectedR, float expectedG, float expectedB, float expectedA) { |
| 56 | SkSL::Compiler compiler; |
| 57 | SkSL::Program::Settings settings; |
| 58 | std::unique_ptr<SkSL::Program> program = compiler.convertProgram( |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 59 | SkSL::Program::kGeneric_Kind, |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 60 | SkSL::String(src), settings); |
| 61 | REPORTER_ASSERT(r, program); |
| 62 | if (program) { |
| 63 | std::unique_ptr<SkSL::ByteCode> byteCode = compiler.toByteCode(*program); |
| 64 | REPORTER_ASSERT(r, !compiler.errorCount()); |
| 65 | if (compiler.errorCount() > 0) { |
| 66 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 67 | return; |
| 68 | } |
| 69 | SkSL::ByteCodeFunction* main = byteCode->fFunctions[0].get(); |
| 70 | SkSL::Interpreter interpreter(std::move(program), std::move(byteCode)); |
| 71 | float inoutColor[4] = { inR, inG, inB, inA }; |
| 72 | interpreter.run(*main, (SkSL::Interpreter::Value*) inoutColor, nullptr); |
| 73 | if (inoutColor[0] != expectedR || inoutColor[1] != expectedG || |
| 74 | inoutColor[2] != expectedB || inoutColor[3] != expectedA) { |
| 75 | printf("for program: %s\n", src); |
| 76 | printf(" expected (%f, %f, %f, %f), but received (%f, %f, %f, %f)\n", expectedR, |
| 77 | expectedG, expectedB, expectedA, inoutColor[0], inoutColor[1], inoutColor[2], |
| 78 | inoutColor[3]); |
| 79 | } |
| 80 | REPORTER_ASSERT(r, inoutColor[0] == expectedR); |
| 81 | REPORTER_ASSERT(r, inoutColor[1] == expectedG); |
| 82 | REPORTER_ASSERT(r, inoutColor[2] == expectedB); |
| 83 | REPORTER_ASSERT(r, inoutColor[3] == expectedA); |
| 84 | } else { |
| 85 | printf("%s\n%s", src, compiler.errorText().c_str()); |
| 86 | } |
| 87 | } |
| 88 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 89 | DEF_TEST(SkSLInterpreterAdd, r) { |
| 90 | test(r, "void main(inout half4 color) { color.r = color.r + color.g; }", 0.25, 0.75, 0, 0, 1, |
| 91 | 0.75, 0, 0); |
| 92 | test(r, "void main(inout half4 color) { color += half4(1, 2, 3, 4); }", 4, 3, 2, 1, 5, 5, 5, 5); |
| 93 | test(r, "void main(inout half4 color) { half4 c = color; color += c; }", 0.25, 0.5, 0.75, 1, |
| 94 | 0.5, 1, 1.5, 2); |
| 95 | test(r, "void main(inout half4 color) { int a = 1; int b = 3; color.r = a + b; }", 1, 2, 3, 4, |
| 96 | 4, 2, 3, 4); |
| 97 | } |
| 98 | |
| 99 | DEF_TEST(SkSLInterpreterSubtract, r) { |
| 100 | test(r, "void main(inout half4 color) { color.r = color.r - color.g; }", 1, 0.75, 0, 0, 0.25, |
| 101 | 0.75, 0, 0); |
| 102 | test(r, "void main(inout half4 color) { color -= half4(1, 2, 3, 4); }", 5, 5, 5, 5, 4, 3, 2, 1); |
| 103 | test(r, "void main(inout half4 color) { half4 c = color; color -= c; }", 4, 3, 2, 1, |
| 104 | 0, 0, 0, 0); |
| 105 | test(r, "void main(inout half4 color) { int a = 3; int b = 1; color.r = a - b; }", 0, 0, 0, 0, |
| 106 | 2, 0, 0, 0); |
| 107 | } |
| 108 | |
| 109 | DEF_TEST(SkSLInterpreterMultiply, r) { |
| 110 | test(r, "void main(inout half4 color) { color.r = color.r * color.g; }", 2, 3, 0, 0, 6, 3, 0, |
| 111 | 0); |
| 112 | test(r, "void main(inout half4 color) { color *= half4(1, 2, 3, 4); }", 2, 3, 4, 5, 2, 6, 12, |
| 113 | 20); |
| 114 | test(r, "void main(inout half4 color) { half4 c = color; color *= c; }", 4, 3, 2, 1, |
| 115 | 16, 9, 4, 1); |
| 116 | test(r, "void main(inout half4 color) { int a = 3; int b = -2; color.r = a * b; }", 0, 0, 0, 0, |
| 117 | -6, 0, 0, 0); |
| 118 | } |
| 119 | |
| 120 | DEF_TEST(SkSLInterpreterDivide, r) { |
| 121 | test(r, "void main(inout half4 color) { color.r = color.r / color.g; }", 1, 2, 0, 0, 0.5, 2, 0, |
| 122 | 0); |
| 123 | test(r, "void main(inout half4 color) { color /= half4(1, 2, 3, 4); }", 12, 12, 12, 12, 12, 6, |
| 124 | 4, 3); |
| 125 | test(r, "void main(inout half4 color) { half4 c = color; color /= c; }", 4, 3, 2, 1, |
| 126 | 1, 1, 1, 1); |
| 127 | test(r, "void main(inout half4 color) { int a = 8; int b = -2; color.r = a / b; }", 0, 0, 0, 0, |
| 128 | -4, 0, 0, 0); |
| 129 | } |
| 130 | |
| 131 | DEF_TEST(SkSLInterpreterRemainder, r) { |
| 132 | test(r, "void main(inout half4 color) { int a = 8; int b = 3; a %= b; color.r = a; }", 0, 0, 0, |
| 133 | 0, 2, 0, 0, 0); |
| 134 | test(r, "void main(inout half4 color) { int a = 8; int b = 3; color.r = a % b; }", 0, 0, 0, 0, |
| 135 | 2, 0, 0, 0); |
| 136 | test(r, "void main(inout half4 color) { int2 a = int2(8, 10); a %= 6; color.rg = a; }", 0, 0, 0, |
| 137 | 0, 2, 4, 0, 0); |
| 138 | } |
| 139 | |
| 140 | DEF_TEST(SkSLInterpreterIf, r) { |
| 141 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 3, 0, 0, |
| 142 | 5, 3, 0, 1); |
| 143 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 5, 0, 0, |
| 144 | 5, 5, 0, 0); |
| 145 | test(r, "void main(inout half4 color) { if (color.r > color.g) color.a = 1; }", 5, 6, 0, 0, |
| 146 | 5, 6, 0, 0); |
| 147 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 3, 5, 0, 0, |
| 148 | 3, 5, 0, 1); |
| 149 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 5, 5, 0, 0, |
| 150 | 5, 5, 0, 0); |
| 151 | test(r, "void main(inout half4 color) { if (color.r < color.g) color.a = 1; }", 6, 5, 0, 0, |
| 152 | 6, 5, 0, 0); |
| 153 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 3, 0, 0, |
| 154 | 5, 3, 0, 1); |
| 155 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 5, 0, 0, |
| 156 | 5, 5, 0, 1); |
| 157 | test(r, "void main(inout half4 color) { if (color.r >= color.g) color.a = 1; }", 5, 6, 0, 0, |
| 158 | 5, 6, 0, 0); |
| 159 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 3, 5, 0, 0, |
| 160 | 3, 5, 0, 1); |
| 161 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 5, 5, 0, 0, |
| 162 | 5, 5, 0, 1); |
| 163 | test(r, "void main(inout half4 color) { if (color.r <= color.g) color.a = 1; }", 6, 5, 0, 0, |
| 164 | 6, 5, 0, 0); |
| 165 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, 2, 0, 0, |
| 166 | 2, 2, 0, 1); |
| 167 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; }", 2, -2, 0, 0, |
| 168 | 2, -2, 0, 0); |
| 169 | test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, 2, 0, 0, |
| 170 | 2, 2, 0, 0); |
| 171 | test(r, "void main(inout half4 color) { if (color.r != color.g) color.a = 1; }", 2, -2, 0, 0, |
| 172 | 2, -2, 0, 1); |
| 173 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else " |
| 174 | "color.a = 2; }", 1, 1, 0, 0, 1, 1, 0, 1); |
| 175 | test(r, "void main(inout half4 color) { if (color.r == color.g) color.a = 1; else " |
| 176 | "color.a = 2; }", 2, -2, 0, 0, 2, -2, 0, 2); |
| 177 | } |
| 178 | |
| 179 | DEF_TEST(SkSLInterpreterWhile, r) { |
| 180 | test(r, "void main(inout half4 color) { while (color.r < 1) color.r += 0.25; }", 0, 0, 0, 0, 1, |
| 181 | 0, 0, 0); |
| 182 | test(r, "void main(inout half4 color) { while (color.r > 1) color.r += 0.25; }", 0, 0, 0, 0, 0, |
| 183 | 0, 0, 0); |
| 184 | test(r, "void main(inout half4 color) { while (true) { color.r += 0.5; " |
| 185 | "if (color.r > 1) break; } }", 0, 0, 0, 0, 1.5, 0, 0, 0); |
| 186 | test(r, "void main(inout half4 color) { while (color.r < 10) { color.r += 0.5; " |
| 187 | "if (color.r < 5) continue; break; } }", 0, 0, 0, 0, 5, 0, 0, 0); |
| 188 | } |
| 189 | |
| 190 | DEF_TEST(SkSLInterpreterDo, r) { |
| 191 | test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r < 1); }", 0, 0, 0, 0, |
| 192 | 1, 0, 0, 0); |
| 193 | test(r, "void main(inout half4 color) { do color.r += 0.25; while (color.r > 1); }", 0, 0, 0, 0, |
| 194 | 0.25, 0, 0, 0); |
| 195 | test(r, "void main(inout half4 color) { do { color.r += 0.5; if (color.r > 1) break; } while " |
| 196 | "(true); }", 0, 0, 0, 0, 1.5, 0, 0, 0); |
| 197 | test(r, "void main(inout half4 color) {do { color.r += 0.5; if (color.r < 5) " |
| 198 | "continue; if (color.r >= 5) break; } while (true); }", 0, 0, 0, 0, 5, 0, 0, 0); |
| 199 | } |
| 200 | |
| 201 | DEF_TEST(SkSLInterpreterFor, r) { |
| 202 | test(r, "void main(inout half4 color) { for (int i = 1; i <= 10; ++i) color.r += i; }", 0, 0, 0, |
| 203 | 0, 55, 0, 0, 0); |
| 204 | test(r, |
| 205 | "void main(inout half4 color) {" |
| 206 | " for (int i = 1; i <= 10; ++i)" |
| 207 | " for (int j = i; j <= 10; ++j)" |
| 208 | " color.r += j;" |
| 209 | "}", |
| 210 | 0, 0, 0, 0, |
| 211 | 385, 0, 0, 0); |
| 212 | test(r, |
| 213 | "void main(inout half4 color) {" |
| 214 | " for (int i = 1; i <= 10; ++i)" |
| 215 | " for (int j = 1; ; ++j) {" |
| 216 | " if (i == j) continue;" |
| 217 | " if (j > 10) break;" |
| 218 | " color.r += j;" |
| 219 | " }" |
| 220 | "}", |
| 221 | 0, 0, 0, 0, |
| 222 | 495, 0, 0, 0); |
| 223 | } |
| 224 | |
| 225 | DEF_TEST(SkSLInterpreterSwizzle, r) { |
| 226 | test(r, "void main(inout half4 color) { color = color.abgr; }", 1, 2, 3, 4, 4, 3, 2, 1); |
| 227 | test(r, "void main(inout half4 color) { color.rgb = half4(5, 6, 7, 8).bbg; }", 1, 2, 3, 4, 7, 7, |
| 228 | 6, 4); |
| 229 | test(r, "void main(inout half4 color) { color.bgr = int3(5, 6, 7); }", 1, 2, 3, 4, 7, 6, |
| 230 | 5, 4); |
| 231 | } |
| 232 | |
| 233 | DEF_TEST(SkSLInterpreterGlobal, r) { |
| 234 | test(r, "int x; void main(inout half4 color) { x = 10; color.b = x; }", 1, 2, 3, 4, 1, 2, 10, |
| 235 | 4); |
| 236 | } |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 237 | |
| 238 | DEF_TEST(SkSLInterpreterGeneric, r) { |
| 239 | float value1 = 5; |
| 240 | float expected1 = 25; |
| 241 | test(r, "float main(float x) { return x * x; }", (SkSL::Interpreter::Value*) &value1, 1, |
| 242 | (SkSL::Interpreter::Value*) &expected1); |
| 243 | float value2[2] = { 5, 25 }; |
| 244 | float expected2[2] = { 25, 625 }; |
| 245 | test(r, "float2 main(float x, float y) { return float2(x * x, y * y); }", |
| 246 | (SkSL::Interpreter::Value*) &value2, 2, |
| 247 | (SkSL::Interpreter::Value*) expected2); |
| 248 | } |