Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | |
| 8 | #ifndef SKSL_STANDALONE |
| 9 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 10 | #include "include/core/SkPoint3.h" |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 11 | #include "include/private/SkVx.h" |
Mike Klein | 7a177b4 | 2019-06-17 17:17:47 -0500 | [diff] [blame] | 12 | #include "src/core/SkUtils.h" // sk_unaligned_load |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLByteCode.h" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 14 | #include "src/sksl/SkSLByteCodeGenerator.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 15 | #include "src/sksl/SkSLExternalValue.h" |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 16 | |
| 17 | #include <vector> |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 18 | |
| 19 | namespace SkSL { |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 20 | |
| 21 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
| 22 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 23 | namespace Interpreter { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 24 | |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 25 | constexpr int VecWidth = ByteCode::kVecWidth; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 26 | |
| 27 | using F32 = skvx::Vec<VecWidth, float>; |
| 28 | using I32 = skvx::Vec<VecWidth, int32_t>; |
| 29 | using U32 = skvx::Vec<VecWidth, uint32_t>; |
| 30 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 31 | #define READ8() (*(ip++)) |
Mike Klein | 7a177b4 | 2019-06-17 17:17:47 -0500 | [diff] [blame] | 32 | #define READ16() (ip += 2, sk_unaligned_load<uint16_t>(ip - 2)) |
| 33 | #define READ32() (ip += 4, sk_unaligned_load<uint32_t>(ip - 4)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 34 | |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 35 | #define VECTOR_DISASSEMBLE(op, text) \ |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 36 | case ByteCodeInstruction::op: printf(text); break; \ |
| 37 | case ByteCodeInstruction::op##2: printf(text "2"); break; \ |
| 38 | case ByteCodeInstruction::op##3: printf(text "3"); break; \ |
| 39 | case ByteCodeInstruction::op##4: printf(text "4"); break; |
| 40 | |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 41 | #define VECTOR_MATRIX_DISASSEMBLE(op, text) \ |
| 42 | case ByteCodeInstruction::op: printf(text); break; \ |
| 43 | case ByteCodeInstruction::op##2: printf(text "2"); break; \ |
| 44 | case ByteCodeInstruction::op##3: printf(text "3"); break; \ |
| 45 | case ByteCodeInstruction::op##4: printf(text "4"); break; \ |
| 46 | case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break; |
| 47 | |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 48 | static const uint8_t* disassemble_instruction(const uint8_t* ip) { |
| 49 | switch ((ByteCodeInstruction) READ16()) { |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 50 | VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 51 | VECTOR_DISASSEMBLE(kAddI, "addi") |
Brian Osman | 32c526b | 2019-06-03 16:13:52 -0400 | [diff] [blame] | 52 | case ByteCodeInstruction::kAndB: printf("andb"); break; |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 53 | case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break; |
| 54 | case ByteCodeInstruction::kCall: printf("call %d", READ8()); break; |
| 55 | case ByteCodeInstruction::kCallExternal: { |
| 56 | int argumentCount = READ8(); |
| 57 | int returnCount = READ8(); |
| 58 | int externalValue = READ8(); |
| 59 | printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue); |
| 60 | break; |
| 61 | } |
| 62 | VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq") |
| 63 | VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq") |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 64 | VECTOR_MATRIX_DISASSEMBLE(kCompareFEQ, "comparefeq") |
| 65 | VECTOR_MATRIX_DISASSEMBLE(kCompareFNEQ, "comparefneq") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 66 | VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt") |
| 67 | VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq") |
| 68 | VECTOR_DISASSEMBLE(kCompareFLT, "compareflt") |
| 69 | VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq") |
| 70 | VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt") |
| 71 | VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq") |
| 72 | VECTOR_DISASSEMBLE(kCompareSLT, "compareslt") |
| 73 | VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq") |
| 74 | VECTOR_DISASSEMBLE(kCompareUGT, "compareugt") |
| 75 | VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq") |
| 76 | VECTOR_DISASSEMBLE(kCompareULT, "compareult") |
| 77 | VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 78 | VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi") |
| 79 | VECTOR_DISASSEMBLE(kConvertStoF, "convertstof") |
| 80 | VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof") |
| 81 | VECTOR_DISASSEMBLE(kCos, "cos") |
Brian Osman | ecb3bb5 | 2019-06-20 14:59:12 -0400 | [diff] [blame] | 82 | case ByteCodeInstruction::kCross: printf("cross"); break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 83 | VECTOR_MATRIX_DISASSEMBLE(kDivideF, "dividef") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 84 | VECTOR_DISASSEMBLE(kDivideS, "divideS") |
| 85 | VECTOR_DISASSEMBLE(kDivideU, "divideu") |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 86 | VECTOR_MATRIX_DISASSEMBLE(kDup, "dup") |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame^] | 87 | case ByteCodeInstruction::kInverse2x2: printf("inverse2x2"); break; |
| 88 | case ByteCodeInstruction::kInverse3x3: printf("inverse3x3"); break; |
| 89 | case ByteCodeInstruction::kInverse4x4: printf("inverse4x4"); break; |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 90 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break; |
| 91 | case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break; |
| 92 | case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break; |
| 93 | case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break; |
| 94 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
| 95 | case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break; |
| 96 | case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break; |
| 97 | case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break; |
| 98 | case ByteCodeInstruction::kLoadSwizzle: { |
| 99 | int target = READ8(); |
| 100 | int count = READ8(); |
| 101 | printf("loadswizzle %d %d", target, count); |
| 102 | for (int i = 0; i < count; ++i) { |
| 103 | printf(", %d", READ8()); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 108 | int target = READ8(); |
| 109 | int count = READ8(); |
| 110 | printf("loadswizzleglobal %d %d", target, count); |
| 111 | for (int i = 0; i < count; ++i) { |
| 112 | printf(", %d", READ8()); |
| 113 | } |
| 114 | break; |
| 115 | } |
| 116 | case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break; |
| 117 | case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8()); |
| 118 | break; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 119 | case ByteCodeInstruction::kMatrixToMatrix: { |
| 120 | int srcCols = READ8(); |
| 121 | int srcRows = READ8(); |
| 122 | int dstCols = READ8(); |
| 123 | int dstRows = READ8(); |
| 124 | printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows); |
| 125 | break; |
| 126 | } |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 127 | case ByteCodeInstruction::kMatrixMultiply: { |
| 128 | int lCols = READ8(); |
| 129 | int lRows = READ8(); |
| 130 | int rCols = READ8(); |
| 131 | printf("matrixmultiply %dx%d %dx%d", lCols, lRows, rCols, lCols); |
| 132 | break; |
| 133 | } |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 134 | VECTOR_DISASSEMBLE(kMix, "mix") |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 135 | VECTOR_MATRIX_DISASSEMBLE(kMultiplyF, "multiplyf") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 136 | VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi") |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 137 | VECTOR_MATRIX_DISASSEMBLE(kNegateF, "negatef") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 138 | VECTOR_DISASSEMBLE(kNegateI, "negatei") |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 139 | case ByteCodeInstruction::kNotB: printf("notb"); break; |
Brian Osman | 32c526b | 2019-06-03 16:13:52 -0400 | [diff] [blame] | 140 | case ByteCodeInstruction::kOrB: printf("orb"); break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 141 | VECTOR_MATRIX_DISASSEMBLE(kPop, "pop") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 142 | case ByteCodeInstruction::kPushImmediate: { |
| 143 | uint32_t v = READ32(); |
| 144 | union { uint32_t u; float f; } pun = { v }; |
| 145 | printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str()); |
| 146 | break; |
| 147 | } |
| 148 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
| 149 | case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break; |
| 150 | case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break; |
| 151 | case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break; |
| 152 | VECTOR_DISASSEMBLE(kRemainderF, "remainderf") |
| 153 | VECTOR_DISASSEMBLE(kRemainderS, "remainders") |
| 154 | VECTOR_DISASSEMBLE(kRemainderU, "remainderu") |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 155 | case ByteCodeInstruction::kReserve: printf("reserve %d", READ8()); break; |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 156 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 157 | case ByteCodeInstruction::kScalarToMatrix: { |
| 158 | int cols = READ8(); |
| 159 | int rows = READ8(); |
| 160 | printf("scalartomatrix %dx%d", cols, rows); |
| 161 | break; |
| 162 | } |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 163 | VECTOR_DISASSEMBLE(kSin, "sin") |
| 164 | VECTOR_DISASSEMBLE(kSqrt, "sqrt") |
| 165 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
| 166 | case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break; |
| 167 | case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break; |
| 168 | case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break; |
| 169 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
| 170 | case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break; |
| 171 | case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break; |
| 172 | case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break; |
| 173 | case ByteCodeInstruction::kStoreSwizzle: { |
| 174 | int target = READ8(); |
| 175 | int count = READ8(); |
| 176 | printf("storeswizzle %d %d", target, count); |
| 177 | for (int i = 0; i < count; ++i) { |
| 178 | printf(", %d", READ8()); |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 183 | int target = READ8(); |
| 184 | int count = READ8(); |
| 185 | printf("storeswizzleglobal %d %d", target, count); |
| 186 | for (int i = 0; i < count; ++i) { |
| 187 | printf(", %d", READ8()); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
| 192 | int count = READ8(); |
| 193 | printf("storeswizzleindirect %d", count); |
| 194 | for (int i = 0; i < count; ++i) { |
| 195 | printf(", %d", READ8()); |
| 196 | } |
| 197 | break; |
| 198 | } |
| 199 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
| 200 | int count = READ8(); |
| 201 | printf("storeswizzleindirectglobal %d", count); |
| 202 | for (int i = 0; i < count; ++i) { |
| 203 | printf(", %d", READ8()); |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break; |
| 208 | case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8()); |
| 209 | break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 210 | VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 211 | VECTOR_DISASSEMBLE(kSubtractI, "subtracti") |
| 212 | case ByteCodeInstruction::kSwizzle: { |
| 213 | printf("swizzle %d, ", READ8()); |
| 214 | int count = READ8(); |
| 215 | printf("%d", count); |
| 216 | for (int i = 0; i < count; ++i) { |
| 217 | printf(", %d", READ8()); |
| 218 | } |
| 219 | break; |
| 220 | } |
| 221 | VECTOR_DISASSEMBLE(kTan, "tan") |
| 222 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
| 223 | case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break; |
| 224 | case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break; |
| 225 | case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 226 | case ByteCodeInstruction::kXorB: printf("xorb"); break; |
| 227 | case ByteCodeInstruction::kMaskPush: printf("maskpush"); break; |
| 228 | case ByteCodeInstruction::kMaskPop: printf("maskpop"); break; |
| 229 | case ByteCodeInstruction::kMaskNegate: printf("masknegate"); break; |
| 230 | case ByteCodeInstruction::kMaskBlend: printf("maskblend %d", READ8()); break; |
| 231 | case ByteCodeInstruction::kBranchIfAllFalse: |
| 232 | printf("branchifallfalse %d", READ16()); |
| 233 | break; |
| 234 | case ByteCodeInstruction::kLoopBegin: printf("loopbegin"); break; |
| 235 | case ByteCodeInstruction::kLoopNext: printf("loopnext"); break; |
| 236 | case ByteCodeInstruction::kLoopMask: printf("loopmask"); break; |
| 237 | case ByteCodeInstruction::kLoopEnd: printf("loopend"); break; |
| 238 | case ByteCodeInstruction::kLoopContinue: printf("loopcontinue"); break; |
| 239 | case ByteCodeInstruction::kLoopBreak: printf("loopbreak"); break; |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 240 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
| 241 | } |
| 242 | return ip; |
| 243 | } |
| 244 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 245 | #define VECTOR_BINARY_OP(base, field, op) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 246 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 247 | sp[-4] = sp[-4].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 248 | POP(); \ |
| 249 | /* fall through */ \ |
| 250 | case ByteCodeInstruction::base ## 3: { \ |
| 251 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 252 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 253 | POP(); \ |
| 254 | } /* fall through */ \ |
| 255 | case ByteCodeInstruction::base ## 2: { \ |
| 256 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 257 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 258 | POP(); \ |
| 259 | } /* fall through */ \ |
| 260 | case ByteCodeInstruction::base: { \ |
| 261 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 262 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 263 | POP(); \ |
| 264 | break; \ |
| 265 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 266 | |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 267 | #define VECTOR_MATRIX_BINARY_OP(base, field, op) \ |
| 268 | VECTOR_BINARY_OP(base, field, op) \ |
| 269 | case ByteCodeInstruction::base ## N: { \ |
| 270 | int count = READ8(); \ |
| 271 | for (int i = count; i > 0; --i) { \ |
| 272 | sp[-count] = sp[-count].field op sp[0].field; \ |
| 273 | POP(); \ |
| 274 | } \ |
| 275 | break; \ |
| 276 | } |
| 277 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 278 | #define VECTOR_BINARY_FN(base, field, fn) \ |
| 279 | case ByteCodeInstruction::base ## 4: \ |
| 280 | sp[-4] = fn(sp[-4].field, sp[0].field); \ |
| 281 | POP(); \ |
| 282 | /* fall through */ \ |
| 283 | case ByteCodeInstruction::base ## 3: { \ |
| 284 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 285 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 286 | POP(); \ |
| 287 | } /* fall through */ \ |
| 288 | case ByteCodeInstruction::base ## 2: { \ |
| 289 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 290 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 291 | POP(); \ |
| 292 | } /* fall through */ \ |
| 293 | case ByteCodeInstruction::base: { \ |
| 294 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 295 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 296 | POP(); \ |
| 297 | break; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 298 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 299 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 300 | #define VECTOR_UNARY_FN(base, fn, field) \ |
| 301 | case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \ |
| 302 | case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \ |
| 303 | case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \ |
| 304 | case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \ |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 305 | break; |
| 306 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 307 | #define VECTOR_UNARY_FN_VEC(base, fn) \ |
| 308 | case ByteCodeInstruction::base ## 4: \ |
| 309 | case ByteCodeInstruction::base ## 3: \ |
| 310 | case ByteCodeInstruction::base ## 2: \ |
| 311 | case ByteCodeInstruction::base : { \ |
| 312 | int count = (int)inst - (int)ByteCodeInstruction::base + 1; \ |
| 313 | float* v = (float*)sp - count + 1; \ |
| 314 | for (int i = VecWidth * count; i > 0; --i, ++v) { \ |
| 315 | *v = fn(*v); \ |
| 316 | } \ |
| 317 | break; \ |
| 318 | } |
| 319 | |
| 320 | union VValue { |
| 321 | VValue() {} |
| 322 | |
| 323 | VValue(F32 f) |
| 324 | : fFloat(f) { |
| 325 | } |
| 326 | |
| 327 | VValue(I32 s) |
| 328 | : fSigned(s) { |
| 329 | } |
| 330 | |
| 331 | VValue(U32 u) |
| 332 | : fUnsigned(u) { |
| 333 | } |
| 334 | |
| 335 | F32 fFloat; |
| 336 | I32 fSigned; |
| 337 | U32 fUnsigned; |
| 338 | }; |
| 339 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 340 | struct StackFrame { |
| 341 | const uint8_t* fCode; |
| 342 | const uint8_t* fIP; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 343 | VValue* fStack; |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 344 | int fParameterCount; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 345 | }; |
| 346 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 347 | static F32 mix(F32 start, F32 end, F32 t) { |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 348 | return start * (1 - t) + end * t; |
| 349 | } |
| 350 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 351 | // TODO: trunc on integers? |
| 352 | template <typename T> |
| 353 | static T vec_mod(T a, T b) { |
| 354 | return a - skvx::trunc(a / b) * b; |
| 355 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 356 | |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame^] | 357 | #define spf(index) sp[index].fFloat |
| 358 | |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 359 | static void innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack, |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 360 | float* outReturn[], VValue globals[], bool stripedOutput, int N, |
| 361 | int baseIndex) { |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 362 | // Needs to be the first N non-negative integers, at least as large as VecWidth |
| 363 | static const Interpreter::I32 gLanes = { |
| 364 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 |
| 365 | }; |
| 366 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 367 | VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1; |
| 368 | |
| 369 | auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); }; |
| 370 | auto PUSH = [&](VValue v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; }; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 371 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 372 | const uint8_t* code = f->fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 373 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 374 | std::vector<StackFrame> frames; |
| 375 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 376 | I32 condStack[16]; // Independent condition masks |
| 377 | I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...) |
| 378 | I32 contStack[16]; // Continue flags for loops |
| 379 | I32 loopStack[16]; // Loop execution masks |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 380 | condStack[0] = maskStack[0] = (gLanes < N); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 381 | contStack[0] = I32( 0); |
| 382 | loopStack[0] = I32(~0); |
| 383 | I32* condPtr = condStack; |
| 384 | I32* maskPtr = maskStack; |
| 385 | I32* contPtr = contStack; |
| 386 | I32* loopPtr = loopStack; |
| 387 | |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 388 | if (f->fConditionCount + 1 > (int)SK_ARRAY_COUNT(condStack) || |
| 389 | f->fLoopCount + 1 > (int)SK_ARRAY_COUNT(loopStack)) { |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 390 | SkDEBUGFAIL("Function with too much nested control flow to evaluate"); |
| 391 | return; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 394 | auto mask = [&]() { return *maskPtr & *loopPtr; }; |
| 395 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 396 | for (;;) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 397 | #ifdef TRACE |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 398 | printf("at %3d ", (int) (ip - code)); |
| 399 | disassemble_instruction(ip); |
| 400 | printf("\n"); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 401 | #endif |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 402 | ByteCodeInstruction inst = (ByteCodeInstruction) READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 403 | switch (inst) { |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 404 | VECTOR_BINARY_OP(kAddI, fSigned, +) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 405 | VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +) |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 406 | |
| 407 | // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want: |
Brian Osman | 32c526b | 2019-06-03 16:13:52 -0400 | [diff] [blame] | 408 | case ByteCodeInstruction::kAndB: |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 409 | sp[-1] = sp[-1].fSigned & sp[0].fSigned; |
| 410 | POP(); |
| 411 | break; |
| 412 | case ByteCodeInstruction::kNotB: |
| 413 | sp[0] = ~sp[0].fSigned; |
| 414 | break; |
| 415 | case ByteCodeInstruction::kOrB: |
| 416 | sp[-1] = sp[-1].fSigned | sp[0].fSigned; |
| 417 | POP(); |
| 418 | break; |
| 419 | case ByteCodeInstruction::kXorB: |
| 420 | sp[-1] = sp[-1].fSigned ^ sp[0].fSigned; |
Brian Osman | 32c526b | 2019-06-03 16:13:52 -0400 | [diff] [blame] | 421 | POP(); |
| 422 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 423 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 424 | case ByteCodeInstruction::kBranch: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 425 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 426 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 427 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 428 | case ByteCodeInstruction::kCall: { |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 429 | // Precursor code reserved space for the return value, and pushed all parameters to |
| 430 | // the stack. Update our bottom of stack to point at the first parameter, and our |
| 431 | // sp to point past those parameters (plus space for locals). |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 432 | int target = READ8(); |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 433 | const ByteCodeFunction* fun = byteCode->fFunctions[target].get(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 434 | if (skvx::any(mask())) { |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 435 | frames.push_back({ code, ip, stack, fun->fParameterCount }); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 436 | ip = code = fun->fCode.data(); |
| 437 | stack = sp - fun->fParameterCount + 1; |
| 438 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 439 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 440 | break; |
| 441 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 442 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 443 | case ByteCodeInstruction::kCallExternal: { |
| 444 | int argumentCount = READ8(); |
| 445 | int returnCount = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 446 | int target = READ8(); |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 447 | ExternalValue* v = byteCode->fExternalValues[target]; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 448 | sp -= argumentCount - 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 449 | |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 450 | float tmpArgs[4]; |
| 451 | float tmpReturn[4]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 452 | SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs)); |
| 453 | SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn)); |
| 454 | |
| 455 | I32 m = mask(); |
| 456 | for (int i = 0; i < VecWidth; ++i) { |
| 457 | if (m[i]) { |
| 458 | for (int j = 0; j < argumentCount; ++j) { |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 459 | tmpArgs[j] = sp[j].fFloat[i]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 460 | } |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 461 | v->call(baseIndex + i, tmpArgs, tmpReturn); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 462 | for (int j = 0; j < returnCount; ++j) { |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 463 | sp[j].fFloat[i] = tmpReturn[j]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 467 | sp += returnCount - 1; |
| 468 | break; |
| 469 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 470 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 471 | VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 472 | VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==) |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 473 | VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 474 | VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=) |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 475 | VECTOR_BINARY_OP(kCompareSGT, fSigned, >) |
| 476 | VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >) |
| 477 | VECTOR_BINARY_OP(kCompareFGT, fFloat, >) |
| 478 | VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=) |
| 479 | VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=) |
| 480 | VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=) |
| 481 | VECTOR_BINARY_OP(kCompareSLT, fSigned, <) |
| 482 | VECTOR_BINARY_OP(kCompareULT, fUnsigned, <) |
| 483 | VECTOR_BINARY_OP(kCompareFLT, fFloat, <) |
| 484 | VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=) |
| 485 | VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=) |
| 486 | VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 487 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 488 | case ByteCodeInstruction::kConvertFtoI4: sp[-3] = skvx::cast<int>(sp[-3].fFloat); |
| 489 | case ByteCodeInstruction::kConvertFtoI3: sp[-2] = skvx::cast<int>(sp[-2].fFloat); |
| 490 | case ByteCodeInstruction::kConvertFtoI2: sp[-1] = skvx::cast<int>(sp[-1].fFloat); |
| 491 | case ByteCodeInstruction::kConvertFtoI: sp[ 0] = skvx::cast<int>(sp[ 0].fFloat); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 492 | break; |
| 493 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 494 | case ByteCodeInstruction::kConvertStoF4: sp[-3] = skvx::cast<float>(sp[-3].fSigned); |
| 495 | case ByteCodeInstruction::kConvertStoF3: sp[-2] = skvx::cast<float>(sp[-2].fSigned); |
| 496 | case ByteCodeInstruction::kConvertStoF2: sp[-1] = skvx::cast<float>(sp[-1].fSigned); |
| 497 | case ByteCodeInstruction::kConvertStoF : sp[ 0] = skvx::cast<float>(sp[ 0].fSigned); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 498 | break; |
| 499 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 500 | case ByteCodeInstruction::kConvertUtoF4: sp[-3] = skvx::cast<float>(sp[-3].fUnsigned); |
| 501 | case ByteCodeInstruction::kConvertUtoF3: sp[-2] = skvx::cast<float>(sp[-2].fUnsigned); |
| 502 | case ByteCodeInstruction::kConvertUtoF2: sp[-1] = skvx::cast<float>(sp[-1].fUnsigned); |
| 503 | case ByteCodeInstruction::kConvertUtoF : sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 504 | break; |
| 505 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 506 | VECTOR_UNARY_FN_VEC(kCos, cosf) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 507 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 508 | case ByteCodeInstruction::kCross: { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 509 | F32 ax = sp[-5].fFloat, ay = sp[-4].fFloat, az = sp[-3].fFloat, |
| 510 | bx = sp[-2].fFloat, by = sp[-1].fFloat, bz = sp[ 0].fFloat; |
| 511 | F32 cx = ay*bz - az*by, |
| 512 | cy = az*bx - ax*bz, |
| 513 | cz = ax*by - ay*bx; |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 514 | sp -= 3; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 515 | sp[-2] = cx; |
| 516 | sp[-1] = cy; |
| 517 | sp[ 0] = cz; |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 521 | VECTOR_BINARY_OP(kDivideS, fSigned, /) |
| 522 | VECTOR_BINARY_OP(kDivideU, fUnsigned, /) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 523 | VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 524 | |
| 525 | case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 526 | case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 527 | case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 528 | case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 529 | break; |
| 530 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 531 | case ByteCodeInstruction::kDupN: { |
| 532 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 533 | memcpy(sp + 1, sp - count + 1, count * sizeof(VValue)); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 534 | sp += count; |
| 535 | break; |
| 536 | } |
| 537 | |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame^] | 538 | case ByteCodeInstruction::kInverse2x2: { |
| 539 | F32 a = sp[-3].fFloat, |
| 540 | b = sp[-2].fFloat, |
| 541 | c = sp[-1].fFloat, |
| 542 | d = sp[ 0].fFloat; |
| 543 | F32 idet = F32(1) / (a*d - b*c); |
| 544 | sp[-3].fFloat = d * idet; |
| 545 | sp[-2].fFloat = -b * idet; |
| 546 | sp[-1].fFloat = -c * idet; |
| 547 | sp[ 0].fFloat = a * idet; |
| 548 | break; |
| 549 | } |
| 550 | case ByteCodeInstruction::kInverse3x3: { |
| 551 | F32 a11 = sp[-8].fFloat, a12 = sp[-5].fFloat, a13 = sp[-2].fFloat, |
| 552 | a21 = sp[-7].fFloat, a22 = sp[-4].fFloat, a23 = sp[-1].fFloat, |
| 553 | a31 = sp[-6].fFloat, a32 = sp[-3].fFloat, a33 = sp[ 0].fFloat; |
| 554 | F32 idet = F32(1) / (a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 - |
| 555 | a11 * a23 * a32 - a12 * a21 * a33 - a13 * a22 * a31); |
| 556 | sp[-8].fFloat = (a22 * a33 - a23 * a32) * idet; |
| 557 | sp[-7].fFloat = (a23 * a31 - a21 * a33) * idet; |
| 558 | sp[-6].fFloat = (a21 * a32 - a22 * a31) * idet; |
| 559 | sp[-5].fFloat = (a13 * a32 - a12 * a33) * idet; |
| 560 | sp[-4].fFloat = (a11 * a33 - a13 * a31) * idet; |
| 561 | sp[-3].fFloat = (a12 * a31 - a11 * a32) * idet; |
| 562 | sp[-2].fFloat = (a12 * a23 - a13 * a22) * idet; |
| 563 | sp[-1].fFloat = (a13 * a21 - a11 * a23) * idet; |
| 564 | sp[ 0].fFloat = (a11 * a22 - a12 * a21) * idet; |
| 565 | break; |
| 566 | } |
| 567 | case ByteCodeInstruction::kInverse4x4: { |
| 568 | F32 a00 = spf(-15), a10 = spf(-11), a20 = spf( -7), a30 = spf( -3), |
| 569 | a01 = spf(-14), a11 = spf(-10), a21 = spf( -6), a31 = spf( -2), |
| 570 | a02 = spf(-13), a12 = spf( -9), a22 = spf( -5), a32 = spf( -1), |
| 571 | a03 = spf(-12), a13 = spf( -8), a23 = spf( -4), a33 = spf( 0); |
| 572 | |
| 573 | F32 b00 = a00 * a11 - a01 * a10, |
| 574 | b01 = a00 * a12 - a02 * a10, |
| 575 | b02 = a00 * a13 - a03 * a10, |
| 576 | b03 = a01 * a12 - a02 * a11, |
| 577 | b04 = a01 * a13 - a03 * a11, |
| 578 | b05 = a02 * a13 - a03 * a12, |
| 579 | b06 = a20 * a31 - a21 * a30, |
| 580 | b07 = a20 * a32 - a22 * a30, |
| 581 | b08 = a20 * a33 - a23 * a30, |
| 582 | b09 = a21 * a32 - a22 * a31, |
| 583 | b10 = a21 * a33 - a23 * a31, |
| 584 | b11 = a22 * a33 - a23 * a32; |
| 585 | |
| 586 | F32 idet = F32(1) / |
| 587 | (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06); |
| 588 | |
| 589 | b00 *= idet; |
| 590 | b01 *= idet; |
| 591 | b02 *= idet; |
| 592 | b03 *= idet; |
| 593 | b04 *= idet; |
| 594 | b05 *= idet; |
| 595 | b06 *= idet; |
| 596 | b07 *= idet; |
| 597 | b08 *= idet; |
| 598 | b09 *= idet; |
| 599 | b10 *= idet; |
| 600 | b11 *= idet; |
| 601 | |
| 602 | spf(-15) = a11 * b11 - a12 * b10 + a13 * b09; |
| 603 | spf(-14) = a02 * b10 - a01 * b11 - a03 * b09; |
| 604 | spf(-13) = a31 * b05 - a32 * b04 + a33 * b03; |
| 605 | spf(-12) = a22 * b04 - a21 * b05 - a23 * b03; |
| 606 | spf(-11) = a12 * b08 - a10 * b11 - a13 * b07; |
| 607 | spf(-10) = a00 * b11 - a02 * b08 + a03 * b07; |
| 608 | spf( -9) = a32 * b02 - a30 * b05 - a33 * b01; |
| 609 | spf( -8) = a20 * b05 - a22 * b02 + a23 * b01; |
| 610 | spf( -7) = a10 * b10 - a11 * b08 + a13 * b06; |
| 611 | spf( -6) = a01 * b08 - a00 * b10 - a03 * b06; |
| 612 | spf( -5) = a30 * b04 - a31 * b02 + a33 * b00; |
| 613 | spf( -4) = a21 * b02 - a20 * b04 - a23 * b00; |
| 614 | spf( -3) = a11 * b07 - a10 * b09 - a12 * b06; |
| 615 | spf( -2) = a00 * b09 - a01 * b07 + a02 * b06; |
| 616 | spf( -1) = a31 * b01 - a30 * b03 - a32 * b00; |
| 617 | spf( 0) = a20 * b03 - a21 * b01 + a22 * b00; |
| 618 | break; |
| 619 | } |
| 620 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 621 | case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3]; |
| 622 | case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2]; |
| 623 | case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1]; |
| 624 | case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0]; |
| 625 | ++ip; |
| 626 | sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1; |
| 627 | break; |
| 628 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 629 | case ByteCodeInstruction::kLoadGlobal4: sp[4] = globals[*ip + 3]; |
| 630 | case ByteCodeInstruction::kLoadGlobal3: sp[3] = globals[*ip + 2]; |
| 631 | case ByteCodeInstruction::kLoadGlobal2: sp[2] = globals[*ip + 1]; |
| 632 | case ByteCodeInstruction::kLoadGlobal : sp[1] = globals[*ip + 0]; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 633 | ++ip; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 634 | sp += (int)inst - |
| 635 | (int)ByteCodeInstruction::kLoadGlobal + 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 636 | break; |
| 637 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 638 | case ByteCodeInstruction::kLoadExtended: { |
| 639 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 640 | I32 src = POP().fSigned; |
| 641 | I32 m = mask(); |
| 642 | for (int i = 0; i < count; ++i) { |
| 643 | for (int j = 0; j < VecWidth; ++j) { |
| 644 | if (m[j]) { |
| 645 | sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j]; |
| 646 | } |
| 647 | } |
| 648 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 649 | sp += count; |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | case ByteCodeInstruction::kLoadExtendedGlobal: { |
| 654 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 655 | I32 src = POP().fSigned; |
| 656 | I32 m = mask(); |
| 657 | for (int i = 0; i < count; ++i) { |
| 658 | for (int j = 0; j < VecWidth; ++j) { |
| 659 | if (m[j]) { |
| 660 | sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j]; |
| 661 | } |
| 662 | } |
| 663 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 664 | sp += count; |
| 665 | break; |
| 666 | } |
| 667 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 668 | case ByteCodeInstruction::kLoadSwizzle: { |
| 669 | int src = READ8(); |
| 670 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 671 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 672 | PUSH(stack[src + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 673 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 674 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 675 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 676 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 677 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 678 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 679 | int src = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 680 | int count = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 681 | for (int i = 0; i < count; ++i) { |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 682 | PUSH(globals[src + *(ip + i)]); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 683 | } |
| 684 | ip += count; |
| 685 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 686 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 687 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 688 | case ByteCodeInstruction::kMatrixToMatrix: { |
| 689 | int srcCols = READ8(); |
| 690 | int srcRows = READ8(); |
| 691 | int dstCols = READ8(); |
| 692 | int dstRows = READ8(); |
| 693 | SkASSERT(srcCols >= 2 && srcCols <= 4); |
| 694 | SkASSERT(srcRows >= 2 && srcRows <= 4); |
| 695 | SkASSERT(dstCols >= 2 && dstCols <= 4); |
| 696 | SkASSERT(dstRows >= 2 && dstRows <= 4); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 697 | F32 tmp[16]; |
| 698 | memset(tmp, 0, sizeof(tmp)); |
| 699 | tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 700 | for (int c = srcCols - 1; c >= 0; --c) { |
| 701 | for (int r = srcRows - 1; r >= 0; --r) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 702 | tmp[c*4 + r] = POP().fFloat; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | for (int c = 0; c < dstCols; ++c) { |
| 706 | for (int r = 0; r < dstRows; ++r) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 707 | PUSH(tmp[c*4 + r]); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | break; |
| 711 | } |
| 712 | |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 713 | case ByteCodeInstruction::kMatrixMultiply: { |
| 714 | int lCols = READ8(); |
| 715 | int lRows = READ8(); |
| 716 | int rCols = READ8(); |
| 717 | int rRows = lCols; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 718 | F32 tmp[16] = { 0.0f }; |
| 719 | F32* B = &(sp - (rCols * rRows) + 1)->fFloat; |
| 720 | F32* A = B - (lCols * lRows); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 721 | for (int c = 0; c < rCols; ++c) { |
| 722 | for (int r = 0; r < lRows; ++r) { |
| 723 | for (int j = 0; j < lCols; ++j) { |
| 724 | tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j]; |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | sp -= (lCols * lRows) + (rCols * rRows); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 729 | memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue)); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 730 | sp += (rCols * lRows); |
| 731 | break; |
| 732 | } |
| 733 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 734 | // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T |
| 735 | case ByteCodeInstruction::kMix4: |
| 736 | sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat); |
| 737 | // fall through |
| 738 | case ByteCodeInstruction::kMix3: { |
| 739 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 740 | int target = 2 - count * 2; |
| 741 | sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat); |
| 742 | // fall through |
| 743 | } |
| 744 | case ByteCodeInstruction::kMix2: { |
| 745 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 746 | int target = 1 - count * 2; |
| 747 | sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat); |
| 748 | // fall through |
| 749 | } |
| 750 | case ByteCodeInstruction::kMix: { |
| 751 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 752 | int target = -count * 2; |
| 753 | sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat); |
| 754 | sp -= 1 + count; |
| 755 | break; |
| 756 | } |
| 757 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 758 | VECTOR_BINARY_OP(kMultiplyI, fSigned, *) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 759 | VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 760 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 761 | case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat; |
| 762 | case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat; |
| 763 | case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat; |
| 764 | case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 765 | break; |
| 766 | |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 767 | case ByteCodeInstruction::kNegateFN: { |
| 768 | int count = READ8(); |
| 769 | for (int i = count - 1; i >= 0; --i) { |
| 770 | sp[-i] = -sp[-i].fFloat; |
| 771 | } |
| 772 | break; |
| 773 | } |
| 774 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 775 | case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned; |
| 776 | case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned; |
| 777 | case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 778 | case ByteCodeInstruction::kNegateI : sp[ 0] = -sp[ 0].fSigned; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 779 | break; |
| 780 | |
| 781 | case ByteCodeInstruction::kPop4: POP(); |
| 782 | case ByteCodeInstruction::kPop3: POP(); |
| 783 | case ByteCodeInstruction::kPop2: POP(); |
| 784 | case ByteCodeInstruction::kPop : POP(); |
| 785 | break; |
| 786 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 787 | case ByteCodeInstruction::kPopN: |
| 788 | sp -= READ8(); |
| 789 | break; |
| 790 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 791 | case ByteCodeInstruction::kPushImmediate: |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 792 | PUSH(U32(READ32())); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 793 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 794 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 795 | case ByteCodeInstruction::kReadExternal: |
| 796 | case ByteCodeInstruction::kReadExternal2: |
| 797 | case ByteCodeInstruction::kReadExternal3: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 798 | case ByteCodeInstruction::kReadExternal4: { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 799 | int count = (int)inst - (int)ByteCodeInstruction::kReadExternal + 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 800 | int src = READ8(); |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 801 | float tmp[4]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 802 | I32 m = mask(); |
| 803 | for (int i = 0; i < VecWidth; ++i) { |
| 804 | if (m[i]) { |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 805 | byteCode->fExternalValues[src]->read(baseIndex + i, tmp); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 806 | for (int j = 0; j < count; ++j) { |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 807 | sp[j + 1].fFloat[i] = tmp[j]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | } |
| 811 | sp += count; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 812 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 813 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 814 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 815 | VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>) |
| 816 | VECTOR_BINARY_FN(kRemainderS, fSigned, vec_mod<I32>) |
| 817 | VECTOR_BINARY_FN(kRemainderU, fUnsigned, vec_mod<U32>) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 818 | |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 819 | case ByteCodeInstruction::kReserve: |
| 820 | sp += READ8(); |
| 821 | break; |
| 822 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 823 | case ByteCodeInstruction::kReturn: { |
| 824 | int count = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 825 | if (frames.empty()) { |
| 826 | if (outReturn) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 827 | VValue* src = sp - count + 1; |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 828 | if (stripedOutput) { |
| 829 | for (int i = 0; i < count; ++i) { |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 830 | memcpy(outReturn[i], &src->fFloat, N * sizeof(float)); |
| 831 | ++src; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 832 | } |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 833 | } else { |
| 834 | float* outPtr = outReturn[0]; |
| 835 | for (int i = 0; i < count; ++i) { |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 836 | for (int j = 0; j < N; ++j) { |
| 837 | outPtr[count * j] = src->fFloat[j]; |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 838 | } |
| 839 | ++outPtr; |
| 840 | ++src; |
| 841 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 842 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 843 | } |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 844 | return; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 845 | } else { |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 846 | // When we were called, the caller reserved stack space for their copy of our |
| 847 | // return value, then 'stack' was positioned after that, where our parameters |
| 848 | // were placed. Copy our return values to their reserved area. |
| 849 | memcpy(stack - count, sp - count + 1, count * sizeof(VValue)); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 850 | |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 851 | // Now move the stack pointer to the end of the passed-in parameters. This odd |
| 852 | // calling convention requires the caller to pop the arguments after calling, |
| 853 | // but allows them to store any out-parameters back during that unwinding. |
| 854 | // After that sequence finishes, the return value will be the top of the stack. |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 855 | const StackFrame& frame(frames.back()); |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 856 | sp = stack + frame.fParameterCount - 1; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 857 | stack = frame.fStack; |
| 858 | code = frame.fCode; |
| 859 | ip = frame.fIP; |
| 860 | frames.pop_back(); |
| 861 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 862 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 863 | } |
| 864 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 865 | case ByteCodeInstruction::kScalarToMatrix: { |
| 866 | int cols = READ8(); |
| 867 | int rows = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 868 | VValue v = POP(); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 869 | for (int c = 0; c < cols; ++c) { |
| 870 | for (int r = 0; r < rows; ++r) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 871 | PUSH(c == r ? v : F32(0.0f)); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | break; |
| 875 | } |
| 876 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 877 | VECTOR_UNARY_FN_VEC(kSin, sinf) |
| 878 | VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 879 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 880 | case ByteCodeInstruction::kStore4: |
| 881 | stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat); |
| 882 | case ByteCodeInstruction::kStore3: |
| 883 | stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat); |
| 884 | case ByteCodeInstruction::kStore2: |
| 885 | stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat); |
| 886 | case ByteCodeInstruction::kStore : |
| 887 | stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat); |
| 888 | ++ip; |
| 889 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 890 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 891 | case ByteCodeInstruction::kStoreGlobal4: |
| 892 | globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat); |
| 893 | case ByteCodeInstruction::kStoreGlobal3: |
| 894 | globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat); |
| 895 | case ByteCodeInstruction::kStoreGlobal2: |
| 896 | globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat); |
| 897 | case ByteCodeInstruction::kStoreGlobal : |
| 898 | globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat); |
| 899 | ++ip; |
| 900 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 901 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 902 | case ByteCodeInstruction::kStoreExtended: { |
| 903 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 904 | I32 target = POP().fSigned; |
| 905 | VValue* src = sp - count + 1; |
| 906 | I32 m = mask(); |
| 907 | for (int i = 0; i < count; ++i) { |
| 908 | for (int j = 0; j < VecWidth; ++j) { |
| 909 | if (m[j]) { |
| 910 | stack[target[j] + i].fSigned[j] = src[i].fSigned[j]; |
| 911 | } |
| 912 | } |
| 913 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 914 | sp -= count; |
| 915 | break; |
| 916 | } |
| 917 | case ByteCodeInstruction::kStoreExtendedGlobal: { |
| 918 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 919 | I32 target = POP().fSigned; |
| 920 | VValue* src = sp - count + 1; |
| 921 | I32 m = mask(); |
| 922 | for (int i = 0; i < count; ++i) { |
| 923 | for (int j = 0; j < VecWidth; ++j) { |
| 924 | if (m[j]) { |
| 925 | globals[target[j] + i].fSigned[j] = src[i].fSigned[j]; |
| 926 | } |
| 927 | } |
| 928 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 929 | sp -= count; |
| 930 | break; |
| 931 | } |
| 932 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 933 | case ByteCodeInstruction::kStoreSwizzle: { |
| 934 | int target = READ8(); |
| 935 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 936 | for (int i = count - 1; i >= 0; --i) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 937 | stack[target + *(ip + i)] = skvx::if_then_else( |
| 938 | mask(), POP().fFloat, stack[target + *(ip + i)].fFloat); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 939 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 940 | ip += count; |
| 941 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 942 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 943 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 944 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 945 | int target = READ8(); |
| 946 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 947 | for (int i = count - 1; i >= 0; --i) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 948 | globals[target + *(ip + i)] = skvx::if_then_else( |
| 949 | mask(), POP().fFloat, globals[target + *(ip + i)].fFloat); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 950 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 951 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 952 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 953 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 954 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 955 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 956 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 957 | I32 target = POP().fSigned; |
| 958 | I32 m = mask(); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 959 | for (int i = count - 1; i >= 0; --i) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 960 | I32 v = POP().fSigned; |
| 961 | for (int j = 0; j < VecWidth; ++j) { |
| 962 | if (m[j]) { |
| 963 | stack[target[j] + *(ip + i)].fSigned[j] = v[j]; |
| 964 | } |
| 965 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 966 | } |
| 967 | ip += count; |
| 968 | break; |
| 969 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 970 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 971 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 972 | int count = READ8(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 973 | I32 target = POP().fSigned; |
| 974 | I32 m = mask(); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 975 | for (int i = count - 1; i >= 0; --i) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 976 | I32 v = POP().fSigned; |
| 977 | for (int j = 0; j < VecWidth; ++j) { |
| 978 | if (m[j]) { |
| 979 | globals[target[j] + *(ip + i)].fSigned[j] = v[j]; |
| 980 | } |
| 981 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 982 | } |
| 983 | ip += count; |
| 984 | break; |
| 985 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 986 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 987 | VECTOR_BINARY_OP(kSubtractI, fSigned, -) |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 988 | VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 989 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 990 | case ByteCodeInstruction::kSwizzle: { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 991 | VValue tmp[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 992 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 993 | tmp[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 994 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 995 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 996 | PUSH(tmp[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 997 | } |
| 998 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 999 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 1000 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1001 | VECTOR_UNARY_FN_VEC(kTan, tanf) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 1002 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1003 | case ByteCodeInstruction::kWriteExternal: |
| 1004 | case ByteCodeInstruction::kWriteExternal2: |
| 1005 | case ByteCodeInstruction::kWriteExternal3: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 1006 | case ByteCodeInstruction::kWriteExternal4: { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1007 | int count = (int)inst - (int)ByteCodeInstruction::kWriteExternal + 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 1008 | int target = READ8(); |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1009 | float tmp[4]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1010 | I32 m = mask(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1011 | sp -= count; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1012 | for (int i = 0; i < VecWidth; ++i) { |
| 1013 | if (m[i]) { |
| 1014 | for (int j = 0; j < count; ++j) { |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1015 | tmp[j] = sp[j + 1].fFloat[i]; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1016 | } |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1017 | byteCode->fExternalValues[target]->write(baseIndex + i, tmp); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | break; |
| 1021 | } |
| 1022 | |
| 1023 | case ByteCodeInstruction::kMaskPush: |
| 1024 | condPtr[1] = POP().fSigned; |
| 1025 | maskPtr[1] = maskPtr[0] & condPtr[1]; |
| 1026 | ++condPtr; ++maskPtr; |
| 1027 | break; |
| 1028 | case ByteCodeInstruction::kMaskPop: |
| 1029 | --condPtr; --maskPtr; |
| 1030 | break; |
| 1031 | case ByteCodeInstruction::kMaskNegate: |
| 1032 | maskPtr[0] = maskPtr[-1] & ~condPtr[0]; |
| 1033 | break; |
| 1034 | case ByteCodeInstruction::kMaskBlend: { |
| 1035 | int count = READ8(); |
| 1036 | I32 m = condPtr[0]; |
| 1037 | --condPtr; --maskPtr; |
| 1038 | for (int i = 0; i < count; ++i) { |
| 1039 | sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat); |
| 1040 | --sp; |
| 1041 | } |
| 1042 | break; |
| 1043 | } |
| 1044 | case ByteCodeInstruction::kBranchIfAllFalse: { |
| 1045 | int target = READ16(); |
| 1046 | if (!skvx::any(mask())) { |
| 1047 | ip = code + target; |
| 1048 | } |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | case ByteCodeInstruction::kLoopBegin: |
Brian Osman | 8d56457 | 2019-06-19 11:00:28 -0400 | [diff] [blame] | 1053 | contPtr[1] = 0; |
| 1054 | loopPtr[1] = loopPtr[0]; |
| 1055 | ++contPtr; ++loopPtr; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1056 | break; |
| 1057 | case ByteCodeInstruction::kLoopNext: |
| 1058 | *loopPtr |= *contPtr; |
| 1059 | *contPtr = 0; |
| 1060 | break; |
| 1061 | case ByteCodeInstruction::kLoopMask: |
| 1062 | *loopPtr &= POP().fSigned; |
| 1063 | break; |
| 1064 | case ByteCodeInstruction::kLoopEnd: |
| 1065 | --contPtr; --loopPtr; |
| 1066 | break; |
| 1067 | case ByteCodeInstruction::kLoopBreak: |
| 1068 | *loopPtr &= ~mask(); |
| 1069 | break; |
| 1070 | case ByteCodeInstruction::kLoopContinue: { |
| 1071 | I32 m = mask(); |
| 1072 | *contPtr |= m; |
| 1073 | *loopPtr &= ~m; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 1074 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 1075 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 1076 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 1077 | default: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 1078 | SkDEBUGFAILF("unsupported instruction %d\n", (int) inst); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 1079 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1080 | } |
| 1081 | } |
| 1082 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1083 | } // namespace Interpreter |
| 1084 | |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1085 | #endif // SK_ENABLE_SKSL_INTERPRETER |
| 1086 | |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame^] | 1087 | #undef spf |
| 1088 | |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1089 | void ByteCodeFunction::disassemble() const { |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1090 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1091 | const uint8_t* ip = fCode.data(); |
| 1092 | while (ip < fCode.data() + fCode.size()) { |
| 1093 | printf("%d: ", (int)(ip - fCode.data())); |
| 1094 | ip = Interpreter::disassemble_instruction(ip); |
| 1095 | printf("\n"); |
| 1096 | } |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1097 | #endif |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1098 | } |
| 1099 | |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1100 | void ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N, |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1101 | const float* uniforms, int uniformCount) const { |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1102 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 1103 | #ifdef TRACE |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1104 | f->disassemble(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 1105 | #endif |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 1106 | Interpreter::VValue stack[128]; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1107 | int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount; |
| 1108 | if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) { |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1109 | SkDEBUGFAIL("Function requires too much stack space to evaluate"); |
| 1110 | return; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1111 | } |
Brian Osman | ef787f7 | 2019-06-13 13:07:12 -0400 | [diff] [blame] | 1112 | |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1113 | SkASSERT(uniformCount == (int)fInputSlots.size()); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1114 | Interpreter::VValue globals[32]; |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1115 | SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1116 | for (uint8_t slot : fInputSlots) { |
| 1117 | globals[slot].fFloat = *uniforms++; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1118 | } |
| 1119 | |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1120 | int baseIndex = 0; |
| 1121 | |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1122 | while (N) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1123 | int w = std::min(N, Interpreter::VecWidth); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1124 | |
| 1125 | // Transpose args into stack |
| 1126 | { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1127 | float* src = args; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1128 | for (int i = 0; i < w; ++i) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1129 | float* dst = (float*)stack + i; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1130 | for (int j = f->fParameterCount; j > 0; --j) { |
| 1131 | *dst = *src++; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1132 | dst += Interpreter::VecWidth; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 1137 | bool stripedOutput = false; |
| 1138 | float** outArray = outReturn ? &outReturn : nullptr; |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1139 | innerRun(this, f, stack, outArray, globals, stripedOutput, w, baseIndex); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1140 | |
| 1141 | // Transpose out parameters back |
| 1142 | { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1143 | float* dst = args; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1144 | for (int i = 0; i < w; ++i) { |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1145 | float* src = (float*)stack + i; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1146 | for (const auto& p : f->fParameters) { |
| 1147 | if (p.fIsOutParameter) { |
| 1148 | for (int j = p.fSlotCount; j > 0; --j) { |
| 1149 | *dst++ = *src; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1150 | src += Interpreter::VecWidth; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1151 | } |
| 1152 | } else { |
| 1153 | dst += p.fSlotCount; |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 1154 | src += p.fSlotCount * Interpreter::VecWidth; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | args += f->fParameterCount * w; |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 1161 | if (outReturn) { |
| 1162 | outReturn += f->fReturnCount * w; |
| 1163 | } |
Brian Osman | 4b202a3 | 2019-06-21 09:50:29 -0400 | [diff] [blame] | 1164 | N -= w; |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1165 | baseIndex += w; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1166 | } |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1167 | #else |
| 1168 | SkDEBUGFAIL("ByteCode interpreter not enabled"); |
| 1169 | #endif |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1172 | void ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N, |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 1173 | const float* uniforms, int uniformCount, |
| 1174 | float* outArgs[], int outCount) const { |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1175 | #if defined(SK_ENABLE_SKSL_INTERPRETER) |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1176 | #ifdef TRACE |
Brian Osman | ecb3bb5 | 2019-06-20 14:59:12 -0400 | [diff] [blame] | 1177 | f->disassemble(); |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1178 | #endif |
| 1179 | Interpreter::VValue stack[128]; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1180 | int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount; |
| 1181 | if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) { |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1182 | SkDEBUGFAIL("Function requires too much stack space to evaluate"); |
| 1183 | return; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1184 | } |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1185 | |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 1186 | // innerRun just takes outArgs, so clear it if the count is zero |
| 1187 | if (outCount == 0) { |
| 1188 | outArgs = nullptr; |
| 1189 | } |
| 1190 | |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1191 | SkASSERT(nargs == f->fParameterCount); |
| 1192 | SkASSERT(outCount == f->fReturnCount); |
| 1193 | SkASSERT(uniformCount == (int)fInputSlots.size()); |
| 1194 | Interpreter::VValue globals[32]; |
| 1195 | SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount); |
| 1196 | for (uint8_t slot : fInputSlots) { |
| 1197 | globals[slot].fFloat = *uniforms++; |
| 1198 | } |
| 1199 | |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1200 | int baseIndex = 0; |
| 1201 | |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1202 | while (N) { |
| 1203 | int w = std::min(N, Interpreter::VecWidth); |
| 1204 | |
| 1205 | // Copy args into stack |
| 1206 | for (int i = 0; i < nargs; ++i) { |
| 1207 | memcpy(stack + i, args[i], w * sizeof(float)); |
| 1208 | } |
| 1209 | |
Mike Reed | 3fd3cc9 | 2019-06-20 12:40:30 -0400 | [diff] [blame] | 1210 | bool stripedOutput = true; |
Brian Osman | 3e6aa9f | 2019-07-18 17:22:33 +0000 | [diff] [blame] | 1211 | innerRun(this, f, stack, outArgs, globals, stripedOutput, w, baseIndex); |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1212 | |
| 1213 | // Copy out parameters back |
| 1214 | int slot = 0; |
| 1215 | for (const auto& p : f->fParameters) { |
| 1216 | if (p.fIsOutParameter) { |
| 1217 | for (int i = slot; i < slot + p.fSlotCount; ++i) { |
| 1218 | memcpy(args[i], stack + i, w * sizeof(float)); |
| 1219 | } |
| 1220 | } |
| 1221 | slot += p.fSlotCount; |
| 1222 | } |
| 1223 | |
| 1224 | // Step each argument pointer ahead |
| 1225 | for (int i = 0; i < nargs; ++i) { |
| 1226 | args[i] += w; |
| 1227 | } |
| 1228 | N -= w; |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 1229 | baseIndex += w; |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1230 | } |
Brian Osman | 489cf88 | 2019-07-09 10:48:28 -0400 | [diff] [blame] | 1231 | #else |
| 1232 | SkDEBUGFAIL("ByteCode interpreter not enabled"); |
| 1233 | #endif |
Brian Osman | 2b1a544 | 2019-06-19 11:40:33 -0400 | [diff] [blame] | 1234 | } |
| 1235 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 1236 | } // namespace SkSL |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1237 | |
| 1238 | #endif |