blob: 03a2c57c496a458c54ea6295393594a732e77504 [file] [log] [blame]
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001/*
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 Nicholasae9633b2019-05-24 12:46:34 -040010#include "include/core/SkPoint3.h"
Brian Osman569f12f2019-06-13 11:23:57 -040011#include "include/private/SkVx.h"
Mike Klein7a177b42019-06-17 17:17:47 -050012#include "src/core/SkUtils.h" // sk_unaligned_load
Brian Osman80164412019-06-07 13:00:23 -040013#include "src/sksl/SkSLByteCode.h"
Brian Osman07c117b2019-05-23 12:51:06 -070014#include "src/sksl/SkSLByteCodeGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040015#include "src/sksl/SkSLExternalValue.h"
Brian Osman80164412019-06-07 13:00:23 -040016
17#include <vector>
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040018
19namespace SkSL {
Brian Osman489cf882019-07-09 10:48:28 -040020
21#if defined(SK_ENABLE_SKSL_INTERPRETER)
22
Brian Osman80164412019-06-07 13:00:23 -040023namespace Interpreter {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040024
Mike Reed3fd3cc92019-06-20 12:40:30 -040025constexpr int VecWidth = ByteCode::kVecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -040026
27using F32 = skvx::Vec<VecWidth, float>;
28using I32 = skvx::Vec<VecWidth, int32_t>;
29using U32 = skvx::Vec<VecWidth, uint32_t>;
30
Mike Kleine7007382019-05-21 08:36:32 -050031#define READ8() (*(ip++))
Mike Klein7a177b42019-06-17 17:17:47 -050032#define READ16() (ip += 2, sk_unaligned_load<uint16_t>(ip - 2))
33#define READ32() (ip += 4, sk_unaligned_load<uint32_t>(ip - 4))
Ethan Nicholasc70027b2019-09-05 16:50:52 -040034#define READ_INST() (ip += sizeof(instruction), \
35 sk_unaligned_load<instruction>(ip - sizeof(instruction)))
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040036
Ethan Nicholasc70027b2019-09-05 16:50:52 -040037#define VECTOR_DISASSEMBLE(op, text) \
38 case ByteCodeInstruction::op: printf(text); ++ip; break; \
39 case ByteCodeInstruction::op##2: printf(text "2"); ++ip; break; \
40 case ByteCodeInstruction::op##3: printf(text "3"); ++ip; break; \
41 case ByteCodeInstruction::op##4: printf(text "4"); ++ip; break;
42
43#define VECTOR_DISASSEMBLE_NO_COUNT(op, text) \
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040044 case ByteCodeInstruction::op: printf(text); break; \
45 case ByteCodeInstruction::op##2: printf(text "2"); break; \
46 case ByteCodeInstruction::op##3: printf(text "3"); break; \
47 case ByteCodeInstruction::op##4: printf(text "4"); break;
48
Ethan Nicholasc70027b2019-09-05 16:50:52 -040049#define VECTOR_MATRIX_DISASSEMBLE(op, text) \
50 VECTOR_DISASSEMBLE(op, text) \
51 case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
52
53#define VECTOR_MATRIX_DISASSEMBLE_NO_COUNT(op, text) \
54 VECTOR_DISASSEMBLE_NO_COUNT(op, text) \
Brian Osman1e855b22019-05-29 15:21:52 -040055 case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
56
Brian Osman3e833e12019-05-23 13:23:24 -070057static const uint8_t* disassemble_instruction(const uint8_t* ip) {
Ethan Nicholasc70027b2019-09-05 16:50:52 -040058 switch ((ByteCodeInstruction) (intptr_t) READ_INST()) {
Brian Osman1e855b22019-05-29 15:21:52 -040059 VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf")
Brian Osman3e833e12019-05-23 13:23:24 -070060 VECTOR_DISASSEMBLE(kAddI, "addi")
Brian Osman32c526b2019-06-03 16:13:52 -040061 case ByteCodeInstruction::kAndB: printf("andb"); break;
Brian Osman3e833e12019-05-23 13:23:24 -070062 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
63 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
64 case ByteCodeInstruction::kCallExternal: {
65 int argumentCount = READ8();
66 int returnCount = READ8();
67 int externalValue = READ8();
68 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
69 break;
70 }
Brian Osman869a3e82019-07-18 17:00:34 -040071 case ByteCodeInstruction::kClampIndex: printf("clampindex %d", READ8()); break;
Brian Osman3e833e12019-05-23 13:23:24 -070072 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
73 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
Brian Osman1e855b22019-05-29 15:21:52 -040074 VECTOR_MATRIX_DISASSEMBLE(kCompareFEQ, "comparefeq")
75 VECTOR_MATRIX_DISASSEMBLE(kCompareFNEQ, "comparefneq")
Brian Osman3e833e12019-05-23 13:23:24 -070076 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
77 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
78 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
79 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
80 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
81 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
82 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
83 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
84 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
85 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
86 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
87 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
Ethan Nicholasc70027b2019-09-05 16:50:52 -040088 VECTOR_DISASSEMBLE_NO_COUNT(kConvertFtoI, "convertftoi")
89 VECTOR_DISASSEMBLE_NO_COUNT(kConvertStoF, "convertstof")
90 VECTOR_DISASSEMBLE_NO_COUNT(kConvertUtoF, "convertutof")
Brian Osman3e833e12019-05-23 13:23:24 -070091 VECTOR_DISASSEMBLE(kCos, "cos")
Brian Osman1e855b22019-05-29 15:21:52 -040092 VECTOR_MATRIX_DISASSEMBLE(kDivideF, "dividef")
Brian Osman3e833e12019-05-23 13:23:24 -070093 VECTOR_DISASSEMBLE(kDivideS, "divideS")
94 VECTOR_DISASSEMBLE(kDivideU, "divideu")
Brian Osman1e855b22019-05-29 15:21:52 -040095 VECTOR_MATRIX_DISASSEMBLE(kDup, "dup")
Mike Reed634c9412019-07-18 13:20:04 -040096 case ByteCodeInstruction::kInverse2x2: printf("inverse2x2"); break;
97 case ByteCodeInstruction::kInverse3x3: printf("inverse3x3"); break;
98 case ByteCodeInstruction::kInverse4x4: printf("inverse4x4"); break;
Ethan Nicholasc70027b2019-09-05 16:50:52 -040099 case ByteCodeInstruction::kLoad: printf("load %d", READ16() >> 8); break;
100 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ16() >> 8); break;
101 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ16() >> 8); break;
102 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ16() >> 8); break;
103 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ16() >> 8); break;
104 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ16() >> 8); break;
105 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ16() >> 8); break;
106 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ16() >> 8); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700107 case ByteCodeInstruction::kLoadSwizzle: {
108 int target = READ8();
109 int count = READ8();
110 printf("loadswizzle %d %d", target, count);
111 for (int i = 0; i < count; ++i) {
112 printf(", %d", READ8());
113 }
114 break;
115 }
116 case ByteCodeInstruction::kLoadSwizzleGlobal: {
117 int target = READ8();
118 int count = READ8();
119 printf("loadswizzleglobal %d %d", target, count);
120 for (int i = 0; i < count; ++i) {
121 printf(", %d", READ8());
122 }
123 break;
124 }
125 case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break;
126 case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8());
127 break;
Brian Osman29e013d2019-05-28 17:16:03 -0400128 case ByteCodeInstruction::kMatrixToMatrix: {
129 int srcCols = READ8();
130 int srcRows = READ8();
131 int dstCols = READ8();
132 int dstRows = READ8();
133 printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows);
134 break;
135 }
Brian Osman909231c2019-05-29 15:34:36 -0400136 case ByteCodeInstruction::kMatrixMultiply: {
137 int lCols = READ8();
138 int lRows = READ8();
139 int rCols = READ8();
140 printf("matrixmultiply %dx%d %dx%d", lCols, lRows, rCols, lCols);
141 break;
142 }
Brian Osman1e855b22019-05-29 15:21:52 -0400143 VECTOR_MATRIX_DISASSEMBLE(kMultiplyF, "multiplyf")
Brian Osman3e833e12019-05-23 13:23:24 -0700144 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400145 VECTOR_MATRIX_DISASSEMBLE_NO_COUNT(kNegateF, "negatef")
146 VECTOR_DISASSEMBLE_NO_COUNT(kNegateI, "negatei")
Brian Osman569f12f2019-06-13 11:23:57 -0400147 case ByteCodeInstruction::kNotB: printf("notb"); break;
Brian Osman32c526b2019-06-03 16:13:52 -0400148 case ByteCodeInstruction::kOrB: printf("orb"); break;
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400149 VECTOR_MATRIX_DISASSEMBLE_NO_COUNT(kPop, "pop")
Brian Osman3e833e12019-05-23 13:23:24 -0700150 case ByteCodeInstruction::kPushImmediate: {
151 uint32_t v = READ32();
152 union { uint32_t u; float f; } pun = { v };
153 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
154 break;
155 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400156 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ16() >> 8); break;
157 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ16() >> 8); break;
158 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ16() >> 8); break;
159 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ16() >> 8); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700160 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
161 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
162 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
Brian Osmand3494ed2019-06-20 15:41:34 -0400163 case ByteCodeInstruction::kReserve: printf("reserve %d", READ8()); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700164 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400165 case ByteCodeInstruction::kScalarToMatrix: {
166 int cols = READ8();
167 int rows = READ8();
168 printf("scalartomatrix %dx%d", cols, rows);
169 break;
170 }
Brian Osman3e833e12019-05-23 13:23:24 -0700171 VECTOR_DISASSEMBLE(kSin, "sin")
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400172 VECTOR_DISASSEMBLE_NO_COUNT(kSqrt, "sqrt")
Brian Osman3e833e12019-05-23 13:23:24 -0700173 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
174 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
175 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
176 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
177 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
178 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
179 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
180 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
181 case ByteCodeInstruction::kStoreSwizzle: {
182 int target = READ8();
183 int count = READ8();
184 printf("storeswizzle %d %d", target, count);
185 for (int i = 0; i < count; ++i) {
186 printf(", %d", READ8());
187 }
188 break;
189 }
190 case ByteCodeInstruction::kStoreSwizzleGlobal: {
191 int target = READ8();
192 int count = READ8();
193 printf("storeswizzleglobal %d %d", target, count);
194 for (int i = 0; i < count; ++i) {
195 printf(", %d", READ8());
196 }
197 break;
198 }
199 case ByteCodeInstruction::kStoreSwizzleIndirect: {
200 int count = READ8();
201 printf("storeswizzleindirect %d", count);
202 for (int i = 0; i < count; ++i) {
203 printf(", %d", READ8());
204 }
205 break;
206 }
207 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
208 int count = READ8();
209 printf("storeswizzleindirectglobal %d", count);
210 for (int i = 0; i < count; ++i) {
211 printf(", %d", READ8());
212 }
213 break;
214 }
215 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
216 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
217 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400218 VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf")
Brian Osman3e833e12019-05-23 13:23:24 -0700219 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
220 case ByteCodeInstruction::kSwizzle: {
221 printf("swizzle %d, ", READ8());
222 int count = READ8();
223 printf("%d", count);
224 for (int i = 0; i < count; ++i) {
225 printf(", %d", READ8());
226 }
227 break;
228 }
229 VECTOR_DISASSEMBLE(kTan, "tan")
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400230 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ16() >> 8); break;
231 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ16() >> 8); break;
232 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ16() >> 8); break;
233 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ16() >> 8); break;
Brian Osman569f12f2019-06-13 11:23:57 -0400234 case ByteCodeInstruction::kXorB: printf("xorb"); break;
235 case ByteCodeInstruction::kMaskPush: printf("maskpush"); break;
236 case ByteCodeInstruction::kMaskPop: printf("maskpop"); break;
237 case ByteCodeInstruction::kMaskNegate: printf("masknegate"); break;
238 case ByteCodeInstruction::kMaskBlend: printf("maskblend %d", READ8()); break;
239 case ByteCodeInstruction::kBranchIfAllFalse:
240 printf("branchifallfalse %d", READ16());
241 break;
242 case ByteCodeInstruction::kLoopBegin: printf("loopbegin"); break;
243 case ByteCodeInstruction::kLoopNext: printf("loopnext"); break;
244 case ByteCodeInstruction::kLoopMask: printf("loopmask"); break;
245 case ByteCodeInstruction::kLoopEnd: printf("loopend"); break;
246 case ByteCodeInstruction::kLoopContinue: printf("loopcontinue"); break;
247 case ByteCodeInstruction::kLoopBreak: printf("loopbreak"); break;
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400248 default:
249 ip -= sizeof(instruction);
250 printf("unknown(%d)\n", (int) (intptr_t) READ_INST());
251 SkASSERT(false);
Brian Osman3e833e12019-05-23 13:23:24 -0700252 }
253 return ip;
254}
255
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400256#ifdef SKSLC_THREADED_CODE
257 #define LABEL(name) name:
258 #ifdef TRACE
259 #define NEXT() goto next
260 #else
261 #define NEXT() goto *READ_INST()
262 #endif
263#else
264 #define LABEL(name) case ByteCodeInstruction::name:
265 #define NEXT() continue
266#endif
267
268#define VECTOR_BINARY_OP(base, field, op) \
269 LABEL(base ## 4) \
270 sp[-4] = sp[-4].field op sp[0].field; \
271 POP(); \
272 /* fall through */ \
273 LABEL(base ## 3) { \
274 sp[-ip[0]] = sp[-ip[0]].field op sp[0].field; \
275 POP(); \
276 } /* fall through */ \
277 LABEL(base ## 2) { \
278 sp[-ip[0]] = sp[-ip[0]].field op sp[0].field; \
279 POP(); \
280 } /* fall through */ \
281 LABEL(base) { \
282 sp[-ip[0]] = sp[-ip[0]].field op sp[0].field; \
283 POP(); \
284 ++ip; \
285 NEXT(); \
Mike Kleine7007382019-05-21 08:36:32 -0500286 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400287
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400288#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
289 VECTOR_BINARY_OP(base, field, op) \
290 LABEL(base ## N) { \
291 int count = READ8(); \
292 for (int i = count; i > 0; --i) { \
293 sp[-count] = sp[-count].field op sp[0].field; \
294 POP(); \
295 } \
296 NEXT(); \
Brian Osman1e855b22019-05-29 15:21:52 -0400297 }
298
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400299#define VECTOR_BINARY_FN(base, field, fn) \
300 LABEL(base ## 4) \
301 sp[-4] = fn(sp[-4].field, sp[0].field); \
302 POP(); \
303 /* fall through */ \
304 LABEL(base ## 3) { \
305 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
306 POP(); \
307 } /* fall through */ \
308 LABEL(base ## 2) { \
309 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
310 POP(); \
311 } /* fall through */ \
312 LABEL(base) { \
313 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
314 POP(); \
315 ++ip; \
316 NEXT(); \
Mike Kleine7007382019-05-21 08:36:32 -0500317 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400318
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400319#define VECTOR_UNARY_FN(base, fn, field) \
320 LABEL(base ## 4) sp[-3] = fn(sp[-3].field); \
321 LABEL(base ## 3) sp[-2] = fn(sp[-2].field); \
322 LABEL(base ## 2) sp[-1] = fn(sp[-1].field); \
323 LABEL(base) sp[ 0] = fn(sp[ 0].field); \
324 NEXT();
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400325
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400326#define VECTOR_UNARY_FN_VEC(base, fn) \
327 LABEL(base ## 4) \
328 LABEL(base ## 3) \
329 LABEL(base ## 2) \
330 LABEL(base) { \
331 int count = READ8(); \
332 float* v = (float*)sp - count + 1; \
333 for (int i = VecWidth * count; i > 0; --i, ++v) { \
334 *v = fn(*v); \
335 } \
336 NEXT(); \
Brian Osman569f12f2019-06-13 11:23:57 -0400337 }
338
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400339#define VECTOR_LABELS(base) \
340 &&base ## 4, \
341 &&base ## 3, \
342 &&base ## 2, \
343 &&base
344
345#define VECTOR_MATRIX_LABELS(base) \
346 VECTOR_LABELS(base), \
347 &&base ## N
348
349// If you trip this assert, it means that the order of the opcodes listed in ByteCodeInstruction
350// does not match the order of the opcodes listed in the 'labels' array in innerRun().
351#define CHECK_LABEL(name) \
352 SkASSERT(labels[(int) ByteCodeInstruction::name] == &&name)
353
354#define CHECK_VECTOR_LABELS(name) \
355 CHECK_LABEL(name ## 4); \
356 CHECK_LABEL(name ## 3); \
357 CHECK_LABEL(name ## 2); \
358 CHECK_LABEL(name)
359
360#define CHECK_VECTOR_MATRIX_LABELS(name) \
361 CHECK_VECTOR_LABELS(name); \
362 CHECK_LABEL(name ## N)
363
Brian Osman569f12f2019-06-13 11:23:57 -0400364union VValue {
365 VValue() {}
366
367 VValue(F32 f)
368 : fFloat(f) {
369 }
370
371 VValue(I32 s)
372 : fSigned(s) {
373 }
374
375 VValue(U32 u)
376 : fUnsigned(u) {
377 }
378
379 F32 fFloat;
380 I32 fSigned;
381 U32 fUnsigned;
382};
383
Brian Osman226668a2019-05-14 16:47:30 -0400384struct StackFrame {
385 const uint8_t* fCode;
386 const uint8_t* fIP;
Brian Osman569f12f2019-06-13 11:23:57 -0400387 VValue* fStack;
Brian Osmand3494ed2019-06-20 15:41:34 -0400388 int fParameterCount;
Brian Osman226668a2019-05-14 16:47:30 -0400389};
390
Brian Osman569f12f2019-06-13 11:23:57 -0400391// TODO: trunc on integers?
392template <typename T>
393static T vec_mod(T a, T b) {
394 return a - skvx::trunc(a / b) * b;
395}
Mike Kleine7007382019-05-21 08:36:32 -0500396
Mike Reed634c9412019-07-18 13:20:04 -0400397#define spf(index) sp[index].fFloat
398
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400399static void call_external(const ByteCode* byteCode, const uint8_t*& ip, VValue*& sp,
400 int baseIndex, I32 mask) {
401 int argumentCount = READ8();
402 int returnCount = READ8();
403 int target = READ8();
404 ExternalValue* v = byteCode->fExternalValues[target];
405 sp -= argumentCount - 1;
406
407 float tmpArgs[4];
408 float tmpReturn[4];
409 SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs));
410 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn));
411
412 for (int i = 0; i < VecWidth; ++i) {
413 if (mask[i]) {
414 for (int j = 0; j < argumentCount; ++j) {
415 tmpArgs[j] = sp[j].fFloat[i];
416 }
417 v->call(baseIndex + i, tmpArgs, tmpReturn);
418 for (int j = 0; j < returnCount; ++j) {
419 sp[j].fFloat[i] = tmpReturn[j];
420 }
421 }
422 }
423 sp += returnCount - 1;
424}
425
426static void inverse2x2(VValue* sp) {
427 F32 a = sp[-3].fFloat,
428 b = sp[-2].fFloat,
429 c = sp[-1].fFloat,
430 d = sp[ 0].fFloat;
431 F32 idet = F32(1) / (a*d - b*c);
432 sp[-3].fFloat = d * idet;
433 sp[-2].fFloat = -b * idet;
434 sp[-1].fFloat = -c * idet;
435 sp[ 0].fFloat = a * idet;
436}
437
438static void inverse3x3(VValue* sp) {
439 F32 a11 = sp[-8].fFloat, a12 = sp[-5].fFloat, a13 = sp[-2].fFloat,
440 a21 = sp[-7].fFloat, a22 = sp[-4].fFloat, a23 = sp[-1].fFloat,
441 a31 = sp[-6].fFloat, a32 = sp[-3].fFloat, a33 = sp[ 0].fFloat;
442 F32 idet = F32(1) / (a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 -
443 a11 * a23 * a32 - a12 * a21 * a33 - a13 * a22 * a31);
444 sp[-8].fFloat = (a22 * a33 - a23 * a32) * idet;
445 sp[-7].fFloat = (a23 * a31 - a21 * a33) * idet;
446 sp[-6].fFloat = (a21 * a32 - a22 * a31) * idet;
447 sp[-5].fFloat = (a13 * a32 - a12 * a33) * idet;
448 sp[-4].fFloat = (a11 * a33 - a13 * a31) * idet;
449 sp[-3].fFloat = (a12 * a31 - a11 * a32) * idet;
450 sp[-2].fFloat = (a12 * a23 - a13 * a22) * idet;
451 sp[-1].fFloat = (a13 * a21 - a11 * a23) * idet;
452 sp[ 0].fFloat = (a11 * a22 - a12 * a21) * idet;
453}
454
455static void inverse4x4(VValue* sp) {
456 F32 a00 = spf(-15), a10 = spf(-11), a20 = spf( -7), a30 = spf( -3),
457 a01 = spf(-14), a11 = spf(-10), a21 = spf( -6), a31 = spf( -2),
458 a02 = spf(-13), a12 = spf( -9), a22 = spf( -5), a32 = spf( -1),
459 a03 = spf(-12), a13 = spf( -8), a23 = spf( -4), a33 = spf( 0);
460
461 F32 b00 = a00 * a11 - a01 * a10,
462 b01 = a00 * a12 - a02 * a10,
463 b02 = a00 * a13 - a03 * a10,
464 b03 = a01 * a12 - a02 * a11,
465 b04 = a01 * a13 - a03 * a11,
466 b05 = a02 * a13 - a03 * a12,
467 b06 = a20 * a31 - a21 * a30,
468 b07 = a20 * a32 - a22 * a30,
469 b08 = a20 * a33 - a23 * a30,
470 b09 = a21 * a32 - a22 * a31,
471 b10 = a21 * a33 - a23 * a31,
472 b11 = a22 * a33 - a23 * a32;
473
474 F32 idet = F32(1) /
475 (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06);
476
477 b00 *= idet;
478 b01 *= idet;
479 b02 *= idet;
480 b03 *= idet;
481 b04 *= idet;
482 b05 *= idet;
483 b06 *= idet;
484 b07 *= idet;
485 b08 *= idet;
486 b09 *= idet;
487 b10 *= idet;
488 b11 *= idet;
489
490 spf(-15) = a11 * b11 - a12 * b10 + a13 * b09;
491 spf(-14) = a02 * b10 - a01 * b11 - a03 * b09;
492 spf(-13) = a31 * b05 - a32 * b04 + a33 * b03;
493 spf(-12) = a22 * b04 - a21 * b05 - a23 * b03;
494 spf(-11) = a12 * b08 - a10 * b11 - a13 * b07;
495 spf(-10) = a00 * b11 - a02 * b08 + a03 * b07;
496 spf( -9) = a32 * b02 - a30 * b05 - a33 * b01;
497 spf( -8) = a20 * b05 - a22 * b02 + a23 * b01;
498 spf( -7) = a10 * b10 - a11 * b08 + a13 * b06;
499 spf( -6) = a01 * b08 - a00 * b10 - a03 * b06;
500 spf( -5) = a30 * b04 - a31 * b02 + a33 * b00;
501 spf( -4) = a21 * b02 - a20 * b04 - a23 * b00;
502 spf( -3) = a11 * b07 - a10 * b09 - a12 * b06;
503 spf( -2) = a00 * b09 - a01 * b07 + a02 * b06;
504 spf( -1) = a31 * b01 - a30 * b03 - a32 * b00;
505 spf( 0) = a20 * b03 - a21 * b01 + a22 * b00;
506}
507
Brian Osman869a3e82019-07-18 17:00:34 -0400508static bool innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
Brian Osman1a79f0b2019-06-24 16:32:14 -0400509 float* outReturn[], VValue globals[], bool stripedOutput, int N,
510 int baseIndex) {
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400511#ifdef SKSLC_THREADED_CODE
512 static const void* labels[] = {
513 // If you aren't familiar with it, the &&label syntax is the GCC / Clang "labels as values"
514 // extension. If you add anything to this array, be sure to add the corresponding
515 // CHECK_LABEL() or CHECK_*_LABELS() assert below.
516 VECTOR_MATRIX_LABELS(kAddF),
517 VECTOR_LABELS(kAddI),
518 &&kAndB,
519 &&kBranch,
520 &&kCall,
521 &&kCallExternal,
522 &&kClampIndex,
523 VECTOR_LABELS(kCompareIEQ),
524 VECTOR_LABELS(kCompareINEQ),
525 VECTOR_MATRIX_LABELS(kCompareFEQ),
526 VECTOR_MATRIX_LABELS(kCompareFNEQ),
527 VECTOR_LABELS(kCompareFGT),
528 VECTOR_LABELS(kCompareFGTEQ),
529 VECTOR_LABELS(kCompareFLT),
530 VECTOR_LABELS(kCompareFLTEQ),
531 VECTOR_LABELS(kCompareSGT),
532 VECTOR_LABELS(kCompareSGTEQ),
533 VECTOR_LABELS(kCompareSLT),
534 VECTOR_LABELS(kCompareSLTEQ),
535 VECTOR_LABELS(kCompareUGT),
536 VECTOR_LABELS(kCompareUGTEQ),
537 VECTOR_LABELS(kCompareULT),
538 VECTOR_LABELS(kCompareULTEQ),
539 VECTOR_LABELS(kConvertFtoI),
540 VECTOR_LABELS(kConvertStoF),
541 VECTOR_LABELS(kConvertUtoF),
542 VECTOR_LABELS(kCos),
543 VECTOR_MATRIX_LABELS(kDivideF),
544 VECTOR_LABELS(kDivideS),
545 VECTOR_LABELS(kDivideU),
546 VECTOR_MATRIX_LABELS(kDup),
547 &&kInverse2x2,
548 &&kInverse3x3,
549 &&kInverse4x4,
550 VECTOR_LABELS(kLoad),
551 VECTOR_LABELS(kLoadGlobal),
552 &&kLoadSwizzle,
553 &&kLoadSwizzleGlobal,
554 &&kLoadExtended,
555 &&kLoadExtendedGlobal,
556 &&kMatrixToMatrix,
557 &&kMatrixMultiply,
558 VECTOR_MATRIX_LABELS(kNegateF),
559 VECTOR_LABELS(kNegateI),
560 VECTOR_MATRIX_LABELS(kMultiplyF),
561 VECTOR_LABELS(kMultiplyI),
562 &&kNotB,
563 &&kOrB,
564 VECTOR_MATRIX_LABELS(kPop),
565 &&kPushImmediate,
566 VECTOR_LABELS(kReadExternal),
567 VECTOR_LABELS(kRemainderF),
568 VECTOR_LABELS(kRemainderS),
569 VECTOR_LABELS(kRemainderU),
570 &&kReserve,
571 &&kReturn,
572 &&kScalarToMatrix,
573 VECTOR_LABELS(kSin),
574 VECTOR_LABELS(kSqrt),
575 VECTOR_LABELS(kStore),
576 VECTOR_LABELS(kStoreGlobal),
577 &&kStoreExtended,
578 &&kStoreExtendedGlobal,
579 &&kStoreSwizzle,
580 &&kStoreSwizzleGlobal,
581 &&kStoreSwizzleIndirect,
582 &&kStoreSwizzleIndirectGlobal,
583 &&kSwizzle,
584 VECTOR_MATRIX_LABELS(kSubtractF),
585 VECTOR_LABELS(kSubtractI),
586 VECTOR_LABELS(kTan),
587 VECTOR_LABELS(kWriteExternal),
588 &&kXorB,
589
590 &&kMaskPush,
591 &&kMaskPop,
592 &&kMaskNegate,
593 &&kMaskBlend,
594 &&kBranchIfAllFalse,
595
596 &&kLoopBegin,
597 &&kLoopNext,
598 &&kLoopMask,
599 &&kLoopEnd,
600 &&kLoopBreak,
601 &&kLoopContinue,
602 };
603 // Verify that the order of the labels array matches the order of the ByteCodeInstruction enum.
604 CHECK_VECTOR_MATRIX_LABELS(kAddF);
605 CHECK_VECTOR_LABELS(kAddI);
606 CHECK_LABEL(kAndB);
607 CHECK_LABEL(kBranch);
608 CHECK_LABEL(kCall);
609 CHECK_LABEL(kCallExternal);
610 CHECK_LABEL(kClampIndex);
611 CHECK_VECTOR_LABELS(kCompareIEQ);
612 CHECK_VECTOR_LABELS(kCompareINEQ);
613 CHECK_VECTOR_MATRIX_LABELS(kCompareFEQ);
614 CHECK_VECTOR_MATRIX_LABELS(kCompareFNEQ);
615 CHECK_VECTOR_LABELS(kCompareFGT);
616 CHECK_VECTOR_LABELS(kCompareFGTEQ);
617 CHECK_VECTOR_LABELS(kCompareFLT);
618 CHECK_VECTOR_LABELS(kCompareFLTEQ);
619 CHECK_VECTOR_LABELS(kCompareSGT);
620 CHECK_VECTOR_LABELS(kCompareSGTEQ);
621 CHECK_VECTOR_LABELS(kCompareSLT);
622 CHECK_VECTOR_LABELS(kCompareSLTEQ);
623 CHECK_VECTOR_LABELS(kCompareUGT);
624 CHECK_VECTOR_LABELS(kCompareUGTEQ);
625 CHECK_VECTOR_LABELS(kCompareULT);
626 CHECK_VECTOR_LABELS(kCompareULTEQ);
627 CHECK_VECTOR_LABELS(kConvertFtoI);
628 CHECK_VECTOR_LABELS(kConvertStoF);
629 CHECK_VECTOR_LABELS(kConvertUtoF);
630 CHECK_VECTOR_LABELS(kCos);
631 CHECK_VECTOR_MATRIX_LABELS(kDivideF);
632 CHECK_VECTOR_LABELS(kDivideS);
633 CHECK_VECTOR_LABELS(kDivideU);
634 CHECK_VECTOR_MATRIX_LABELS(kDup);
635 CHECK_LABEL(kInverse2x2);
636 CHECK_LABEL(kInverse3x3);
637 CHECK_LABEL(kInverse4x4);
638 CHECK_VECTOR_LABELS(kLoad);
639 CHECK_VECTOR_LABELS(kLoadGlobal);
640 CHECK_LABEL(kLoadSwizzle);
641 CHECK_LABEL(kLoadSwizzleGlobal);
642 CHECK_LABEL(kLoadExtended);
643 CHECK_LABEL(kLoadExtendedGlobal);
644 CHECK_LABEL(kMatrixToMatrix);
645 CHECK_LABEL(kMatrixMultiply);
646 CHECK_VECTOR_MATRIX_LABELS(kNegateF);
647 CHECK_VECTOR_LABELS(kNegateI);
648 CHECK_VECTOR_MATRIX_LABELS(kMultiplyF);
649 CHECK_VECTOR_LABELS(kMultiplyI);
650 CHECK_LABEL(kNotB);
651 CHECK_LABEL(kOrB);
652 CHECK_VECTOR_MATRIX_LABELS(kPop);
653 CHECK_LABEL(kPushImmediate);
654 CHECK_VECTOR_LABELS(kReadExternal);
655 CHECK_VECTOR_LABELS(kRemainderF);
656 CHECK_VECTOR_LABELS(kRemainderS);
657 CHECK_VECTOR_LABELS(kRemainderU);
658 CHECK_LABEL(kReserve);
659 CHECK_LABEL(kReturn);
660 CHECK_LABEL(kScalarToMatrix);
661 CHECK_VECTOR_LABELS(kSin);
662 CHECK_VECTOR_LABELS(kSqrt);
663 CHECK_VECTOR_LABELS(kStore);
664 CHECK_VECTOR_LABELS(kStoreGlobal);
665 CHECK_LABEL(kStoreExtended);
666 CHECK_LABEL(kStoreExtendedGlobal);
667 CHECK_LABEL(kStoreSwizzle);
668 CHECK_LABEL(kStoreSwizzleGlobal);
669 CHECK_LABEL(kStoreSwizzleIndirect);
670 CHECK_LABEL(kStoreSwizzleIndirectGlobal);
671 CHECK_LABEL(kSwizzle);
672 CHECK_VECTOR_MATRIX_LABELS(kSubtractF);
673 CHECK_VECTOR_LABELS(kSubtractI);
674 CHECK_VECTOR_LABELS(kTan);
675 CHECK_VECTOR_LABELS(kWriteExternal);
676 CHECK_LABEL(kXorB);
677 CHECK_LABEL(kMaskPush);
678 CHECK_LABEL(kMaskPop);
679 CHECK_LABEL(kMaskNegate);
680 CHECK_LABEL(kMaskBlend);
681 CHECK_LABEL(kBranchIfAllFalse);
682 CHECK_LABEL(kLoopBegin);
683 CHECK_LABEL(kLoopNext);
684 CHECK_LABEL(kLoopMask);
685 CHECK_LABEL(kLoopEnd);
686 CHECK_LABEL(kLoopBreak);
687 CHECK_LABEL(kLoopContinue);
688 if (!f->fPreprocessed) {
689 ((ByteCodeFunction*) f)->preprocess(labels);
690 }
691#endif
692
Brian Osman4b202a32019-06-21 09:50:29 -0400693 // Needs to be the first N non-negative integers, at least as large as VecWidth
694 static const Interpreter::I32 gLanes = {
695 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
696 };
697
Brian Osman569f12f2019-06-13 11:23:57 -0400698 VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1;
699
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400700 #define POP() (*(sp--))
701 #define PUSH(v) (sp[1] = v, ++sp)
Mike Kleine7007382019-05-21 08:36:32 -0500702
Brian Osman80164412019-06-07 13:00:23 -0400703 const uint8_t* code = f->fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400704 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400705 std::vector<StackFrame> frames;
706
Brian Osman569f12f2019-06-13 11:23:57 -0400707 I32 condStack[16]; // Independent condition masks
708 I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...)
709 I32 contStack[16]; // Continue flags for loops
710 I32 loopStack[16]; // Loop execution masks
Brian Osman4b202a32019-06-21 09:50:29 -0400711 condStack[0] = maskStack[0] = (gLanes < N);
Brian Osman569f12f2019-06-13 11:23:57 -0400712 contStack[0] = I32( 0);
713 loopStack[0] = I32(~0);
714 I32* condPtr = condStack;
715 I32* maskPtr = maskStack;
716 I32* contPtr = contStack;
717 I32* loopPtr = loopStack;
718
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400719 if (f->fConditionCount + 1 > (int)SK_ARRAY_COUNT(condStack) ||
720 f->fLoopCount + 1 > (int)SK_ARRAY_COUNT(loopStack)) {
Brian Osman869a3e82019-07-18 17:00:34 -0400721 return false;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400722 }
723
Brian Osman569f12f2019-06-13 11:23:57 -0400724 auto mask = [&]() { return *maskPtr & *loopPtr; };
725
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400726#ifdef SKSLC_THREADED_CODE
727 // If the "labels as values" extension is available, we implement this using threaded code.
728 // Instead of opcodes, the code directly contains the addresses of the labels to jump to. Then
729 // the code for each opcode simply grabs the address of the next opcode and uses a goto to jump
730 // there.
731 NEXT();
732#else
733 // Otherwise, we have to use a switch statement and a loop to execute the right label.
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400734 for (;;) {
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400735 #ifdef TRACE
736 printf("at %3d ", (int) (ip - code));
737 disassemble_instruction(ip);
738 printf(" (stack: %d)\n", (int) (sp - stack) + 1);
739 #endif
740 switch ((ByteCodeInstruction) READ16()) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400741#endif
Brian Osman569f12f2019-06-13 11:23:57 -0400742
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400743 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
744 VECTOR_BINARY_OP(kAddI, fSigned, +)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400745
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400746 // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want:
747 LABEL(kAndB)
748 sp[-1] = sp[-1].fSigned & sp[0].fSigned;
749 POP();
750 NEXT();
751 LABEL(kNotB)
752 sp[0] = ~sp[0].fSigned;
753 NEXT();
754 LABEL(kOrB)
755 sp[-1] = sp[-1].fSigned | sp[0].fSigned;
756 POP();
757 NEXT();
758 LABEL(kXorB)
759 sp[-1] = sp[-1].fSigned ^ sp[0].fSigned;
760 POP();
761 NEXT();
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400762
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400763 LABEL(kBranch)
764 ip = code + READ16();
765 NEXT();
766
767 LABEL(kCall) {
768 // Precursor code reserved space for the return value, and pushed all parameters to
769 // the stack. Update our bottom of stack to point at the first parameter, and our
770 // sp to point past those parameters (plus space for locals).
771 int target = READ8();
772 const ByteCodeFunction* fun = byteCode->fFunctions[target].get();
773#ifdef SKSLC_THREADED_CODE
774 if (!fun->fPreprocessed) {
775 ((ByteCodeFunction*) fun)->preprocess(labels);
776 }
777#endif
778 if (skvx::any(mask())) {
779 frames.push_back({ code, ip, stack, fun->fParameterCount });
780 ip = code = fun->fCode.data();
781 stack = sp - fun->fParameterCount + 1;
782 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
783 }
784 NEXT();
785 }
786
787 LABEL(kCallExternal) {
788 call_external(byteCode, ip, sp, baseIndex, mask());
789 NEXT();
790 }
791
792 LABEL(kClampIndex) {
793 int length = READ8();
794 if (skvx::any(mask() & ((sp[0].fSigned < 0) | (sp[0].fSigned >= length)))) {
795 return false;
796 }
797 NEXT();
798 }
799
800 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
801 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
802 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
803 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
804 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
805 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
806 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
807 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
808 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
809 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
810 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
811 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
812 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
813 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
814 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
815 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
816
817 LABEL(kConvertFtoI4) sp[-3] = skvx::cast<int>(sp[-3].fFloat);
818 LABEL(kConvertFtoI3) sp[-2] = skvx::cast<int>(sp[-2].fFloat);
819 LABEL(kConvertFtoI2) sp[-1] = skvx::cast<int>(sp[-1].fFloat);
820 LABEL(kConvertFtoI) sp[ 0] = skvx::cast<int>(sp[ 0].fFloat);
821 NEXT();
822
823 LABEL(kConvertStoF4) sp[-3] = skvx::cast<float>(sp[-3].fSigned);
824 LABEL(kConvertStoF3) sp[-2] = skvx::cast<float>(sp[-2].fSigned);
825 LABEL(kConvertStoF2) sp[-1] = skvx::cast<float>(sp[-1].fSigned);
826 LABEL(kConvertStoF) sp[ 0] = skvx::cast<float>(sp[ 0].fSigned);
827 NEXT();
828
829 LABEL(kConvertUtoF4) sp[-3] = skvx::cast<float>(sp[-3].fUnsigned);
830 LABEL(kConvertUtoF3) sp[-2] = skvx::cast<float>(sp[-2].fUnsigned);
831 LABEL(kConvertUtoF2) sp[-1] = skvx::cast<float>(sp[-1].fUnsigned);
832 LABEL(kConvertUtoF) sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned);
833 NEXT();
834
835 VECTOR_UNARY_FN_VEC(kCos, cosf)
836
837 VECTOR_BINARY_OP(kDivideS, fSigned, /)
838 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
839 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
840
841 LABEL(kDup4) PUSH(sp[1 - ip[0]]);
842 LABEL(kDup3) PUSH(sp[1 - ip[0]]);
843 LABEL(kDup2) PUSH(sp[1 - ip[0]]);
844 LABEL(kDup) PUSH(sp[1 - ip[0]]);
845 ++ip;
846 NEXT();
847
848 LABEL(kDupN) {
849 int count = READ8();
850 memcpy(sp + 1, sp - count + 1, count * sizeof(VValue));
851 sp += count;
852 NEXT();
853 }
854
855 LABEL(kInverse2x2) {
856 inverse2x2(sp);
857 NEXT();
858 }
859 LABEL(kInverse3x3) {
860 inverse3x3(sp);
861 NEXT();
862 }
863 LABEL(kInverse4x4) {
864 inverse4x4(sp);
865 NEXT();
866 }
867
868 LABEL(kLoad4) sp[4] = stack[ip[1] + 3];
869 LABEL(kLoad3) sp[3] = stack[ip[1] + 2];
870 LABEL(kLoad2) sp[2] = stack[ip[1] + 1];
871 LABEL(kLoad) sp[1] = stack[ip[1] + 0];
872 sp += ip[0];
873 ip += 2;
874 NEXT();
875
876 LABEL(kLoadGlobal4) sp[4] = globals[ip[1] + 3];
877 LABEL(kLoadGlobal3) sp[3] = globals[ip[1] + 2];
878 LABEL(kLoadGlobal2) sp[2] = globals[ip[1] + 1];
879 LABEL(kLoadGlobal) sp[1] = globals[ip[1] + 0];
880 sp += ip[0];
881 ip += 2;
882 NEXT();
883
884 LABEL(kLoadExtended) {
885 int count = READ8();
886 I32 src = POP().fSigned;
887 I32 m = mask();
888 for (int i = 0; i < count; ++i) {
889 for (int j = 0; j < VecWidth; ++j) {
890 if (m[j]) {
891 sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400892 }
Brian Osman226668a2019-05-14 16:47:30 -0400893 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400894 }
895 sp += count;
896 NEXT();
897 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400898
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400899 LABEL(kLoadExtendedGlobal) {
900 int count = READ8();
901 I32 src = POP().fSigned;
902 I32 m = mask();
903 for (int i = 0; i < count; ++i) {
904 for (int j = 0; j < VecWidth; ++j) {
905 if (m[j]) {
906 sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j];
907 }
908 }
909 }
910 sp += count;
911 NEXT();
912 }
Mike Kleine7007382019-05-21 08:36:32 -0500913
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400914 LABEL(kLoadSwizzle) {
915 int src = READ8();
916 int count = READ8();
917 for (int i = 0; i < count; ++i) {
918 PUSH(stack[src + *(ip + i)]);
919 }
920 ip += count;
921 NEXT();
922 }
Brian Osman569f12f2019-06-13 11:23:57 -0400923
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400924 LABEL(kLoadSwizzleGlobal) {
925 int src = READ8();
926 int count = READ8();
927 for (int i = 0; i < count; ++i) {
928 PUSH(globals[src + *(ip + i)]);
929 }
930 ip += count;
931 NEXT();
932 }
933
934 LABEL(kMatrixToMatrix) {
935 int srcCols = READ8();
936 int srcRows = READ8();
937 int dstCols = READ8();
938 int dstRows = READ8();
939 SkASSERT(srcCols >= 2 && srcCols <= 4);
940 SkASSERT(srcRows >= 2 && srcRows <= 4);
941 SkASSERT(dstCols >= 2 && dstCols <= 4);
942 SkASSERT(dstRows >= 2 && dstRows <= 4);
943 F32 tmp[16];
944 memset(tmp, 0, sizeof(tmp));
945 tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f);
946 for (int c = srcCols - 1; c >= 0; --c) {
947 for (int r = srcRows - 1; r >= 0; --r) {
948 tmp[c*4 + r] = POP().fFloat;
949 }
950 }
951 for (int c = 0; c < dstCols; ++c) {
952 for (int r = 0; r < dstRows; ++r) {
953 PUSH(tmp[c*4 + r]);
954 }
955 }
956 NEXT();
957 }
958
959 LABEL(kMatrixMultiply) {
960 int lCols = READ8();
961 int lRows = READ8();
962 int rCols = READ8();
963 int rRows = lCols;
964 F32 tmp[16] = { 0.0f };
965 F32* B = &(sp - (rCols * rRows) + 1)->fFloat;
966 F32* A = B - (lCols * lRows);
967 for (int c = 0; c < rCols; ++c) {
968 for (int r = 0; r < lRows; ++r) {
969 for (int j = 0; j < lCols; ++j) {
970 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
971 }
972 }
973 }
974 sp -= (lCols * lRows) + (rCols * rRows);
975 memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue));
976 sp += (rCols * lRows);
977 NEXT();
978 }
979
980 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
981 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
982
983 LABEL(kNegateF4) sp[-3] = -sp[-3].fFloat;
984 LABEL(kNegateF3) sp[-2] = -sp[-2].fFloat;
985 LABEL(kNegateF2) sp[-1] = -sp[-1].fFloat;
986 LABEL(kNegateF) sp[ 0] = -sp[ 0].fFloat;
987 NEXT();
988
989 LABEL(kNegateFN) {
990 int count = READ8();
991 for (int i = count - 1; i >= 0; --i) {
992 sp[-i] = -sp[-i].fFloat;
993 }
994 NEXT();
995 }
996
997 LABEL(kNegateI4) sp[-3] = -sp[-3].fSigned;
998 LABEL(kNegateI3) sp[-2] = -sp[-2].fSigned;
999 LABEL(kNegateI2) sp[-1] = -sp[-1].fSigned;
1000 LABEL(kNegateI) sp[ 0] = -sp[ 0].fSigned;
1001 NEXT();
1002
1003 LABEL(kPop4) POP();
1004 LABEL(kPop3) POP();
1005 LABEL(kPop2) POP();
1006 LABEL(kPop) POP();
1007 NEXT();
1008
1009 LABEL(kPopN)
1010 sp -= READ8();
1011 NEXT();
1012
1013 LABEL(kPushImmediate)
1014 PUSH(U32(READ32()));
1015 NEXT();
1016
1017 LABEL(kReadExternal)
1018 LABEL(kReadExternal2)
1019 LABEL(kReadExternal3)
1020 LABEL(kReadExternal4) {
1021 int count = READ8();
1022 int src = READ8();
1023 float tmp[4];
1024 I32 m = mask();
1025 for (int i = 0; i < VecWidth; ++i) {
1026 if (m[i]) {
1027 byteCode->fExternalValues[src]->read(baseIndex + i, tmp);
1028 for (int j = 0; j < count; ++j) {
1029 sp[j + 1].fFloat[i] = tmp[j];
1030 }
1031 }
1032 }
1033 sp += count;
1034 NEXT();
1035 }
1036
1037 VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
1038 VECTOR_BINARY_FN(kRemainderS, fSigned, vec_mod<I32>)
1039 VECTOR_BINARY_FN(kRemainderU, fUnsigned, vec_mod<U32>)
1040
1041 LABEL(kReserve)
1042 sp += READ8();
1043 NEXT();
1044
1045 LABEL(kReturn) {
1046 int count = READ8();
1047 if (frames.empty()) {
1048 if (outReturn) {
1049 VValue* src = sp - count + 1;
1050 if (stripedOutput) {
1051 for (int i = 0; i < count; ++i) {
1052 memcpy(outReturn[i], &src->fFloat, N * sizeof(float));
1053 ++src;
Brian Osman569f12f2019-06-13 11:23:57 -04001054 }
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001055 } else {
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001056 float* outPtr = outReturn[0];
1057 for (int i = 0; i < count; ++i) {
1058 for (int j = 0; j < N; ++j) {
1059 outPtr[count * j] = src->fFloat[j];
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001060 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001061 ++outPtr;
1062 ++src;
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001063 }
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001064 }
1065 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001066 return true;
1067 } else {
1068 // When we were called, the caller reserved stack space for their copy of our
1069 // return value, then 'stack' was positioned after that, where our parameters
1070 // were placed. Copy our return values to their reserved area.
1071 memcpy(stack - count, sp - count + 1, count * sizeof(VValue));
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001072
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001073 // Now move the stack pointer to the end of the passed-in parameters. This odd
1074 // calling convention requires the caller to pop the arguments after calling,
1075 // but allows them to store any out-parameters back during that unwinding.
1076 // After that sequence finishes, the return value will be the top of the stack.
1077 const StackFrame& frame(frames.back());
1078 sp = stack + frame.fParameterCount - 1;
1079 stack = frame.fStack;
1080 code = frame.fCode;
1081 ip = frame.fIP;
1082 frames.pop_back();
1083 NEXT();
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001084 }
1085 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001086
1087 LABEL(kScalarToMatrix) {
1088 int cols = READ8();
1089 int rows = READ8();
1090 VValue v = POP();
1091 for (int c = 0; c < cols; ++c) {
1092 for (int r = 0; r < rows; ++r) {
1093 PUSH(c == r ? v : F32(0.0f));
1094 }
1095 }
1096 NEXT();
1097 }
1098
1099 VECTOR_UNARY_FN_VEC(kSin, sinf)
1100 VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat)
1101
1102 LABEL(kStore4)
1103 stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat);
1104 LABEL(kStore3)
1105 stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat);
1106 LABEL(kStore2)
1107 stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat);
1108 LABEL(kStore)
1109 stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat);
1110 ++ip;
1111 NEXT();
1112
1113 LABEL(kStoreGlobal4)
1114 globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat);
1115 LABEL(kStoreGlobal3)
1116 globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat);
1117 LABEL(kStoreGlobal2)
1118 globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat);
1119 LABEL(kStoreGlobal)
1120 globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat);
1121 ++ip;
1122 NEXT();
1123
1124 LABEL(kStoreExtended) {
1125 int count = READ8();
1126 I32 target = POP().fSigned;
1127 VValue* src = sp - count + 1;
1128 I32 m = mask();
1129 for (int i = 0; i < count; ++i) {
1130 for (int j = 0; j < VecWidth; ++j) {
1131 if (m[j]) {
1132 stack[target[j] + i].fSigned[j] = src[i].fSigned[j];
1133 }
1134 }
1135 }
1136 sp -= count;
1137 NEXT();
1138 }
1139 LABEL(kStoreExtendedGlobal) {
1140 int count = READ8();
1141 I32 target = POP().fSigned;
1142 VValue* src = sp - count + 1;
1143 I32 m = mask();
1144 for (int i = 0; i < count; ++i) {
1145 for (int j = 0; j < VecWidth; ++j) {
1146 if (m[j]) {
1147 globals[target[j] + i].fSigned[j] = src[i].fSigned[j];
1148 }
1149 }
1150 }
1151 sp -= count;
1152 NEXT();
1153 }
1154
1155 LABEL(kStoreSwizzle) {
1156 int target = READ8();
1157 int count = READ8();
1158 for (int i = count - 1; i >= 0; --i) {
1159 stack[target + *(ip + i)] = skvx::if_then_else(
1160 mask(), POP().fFloat, stack[target + *(ip + i)].fFloat);
1161 }
1162 ip += count;
1163 NEXT();
1164 }
1165
1166 LABEL(kStoreSwizzleGlobal) {
1167 int target = READ8();
1168 int count = READ8();
1169 for (int i = count - 1; i >= 0; --i) {
1170 globals[target + *(ip + i)] = skvx::if_then_else(
1171 mask(), POP().fFloat, globals[target + *(ip + i)].fFloat);
1172 }
1173 ip += count;
1174 NEXT();
1175 }
1176
1177 LABEL(kStoreSwizzleIndirect) {
1178 int count = READ8();
1179 I32 target = POP().fSigned;
1180 I32 m = mask();
1181 for (int i = count - 1; i >= 0; --i) {
1182 I32 v = POP().fSigned;
1183 for (int j = 0; j < VecWidth; ++j) {
1184 if (m[j]) {
1185 stack[target[j] + *(ip + i)].fSigned[j] = v[j];
1186 }
1187 }
1188 }
1189 ip += count;
1190 NEXT();
1191 }
1192
1193 LABEL(kStoreSwizzleIndirectGlobal) {
1194 int count = READ8();
1195 I32 target = POP().fSigned;
1196 I32 m = mask();
1197 for (int i = count - 1; i >= 0; --i) {
1198 I32 v = POP().fSigned;
1199 for (int j = 0; j < VecWidth; ++j) {
1200 if (m[j]) {
1201 globals[target[j] + *(ip + i)].fSigned[j] = v[j];
1202 }
1203 }
1204 }
1205 ip += count;
1206 NEXT();
1207 }
1208
1209 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
1210 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
1211
1212 LABEL(kSwizzle) {
1213 VValue tmp[4];
1214 for (int i = READ8() - 1; i >= 0; --i) {
1215 tmp[i] = POP();
1216 }
1217 for (int i = READ8() - 1; i >= 0; --i) {
1218 PUSH(tmp[READ8()]);
1219 }
1220 NEXT();
1221 }
1222
1223 VECTOR_UNARY_FN_VEC(kTan, tanf)
1224
1225 LABEL(kWriteExternal4)
1226 LABEL(kWriteExternal3)
1227 LABEL(kWriteExternal2)
1228 LABEL(kWriteExternal) {
1229 int count = READ8();
1230 int target = READ8();
1231 float tmp[4];
1232 I32 m = mask();
1233 sp -= count;
1234 for (int i = 0; i < VecWidth; ++i) {
1235 if (m[i]) {
1236 for (int j = 0; j < count; ++j) {
1237 tmp[j] = sp[j + 1].fFloat[i];
1238 }
1239 byteCode->fExternalValues[target]->write(baseIndex + i, tmp);
1240 }
1241 }
1242 NEXT();
1243 }
1244
1245 LABEL(kMaskPush)
1246 condPtr[1] = POP().fSigned;
1247 maskPtr[1] = maskPtr[0] & condPtr[1];
1248 ++condPtr; ++maskPtr;
1249 NEXT();
1250 LABEL(kMaskPop)
1251 --condPtr; --maskPtr;
1252 NEXT();
1253 LABEL(kMaskNegate)
1254 maskPtr[0] = maskPtr[-1] & ~condPtr[0];
1255 NEXT();
1256 LABEL(kMaskBlend) {
1257 int count = READ8();
1258 I32 m = condPtr[0];
1259 --condPtr; --maskPtr;
1260 for (int i = 0; i < count; ++i) {
1261 sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat);
1262 --sp;
1263 }
1264 NEXT();
1265 }
1266 LABEL(kBranchIfAllFalse) {
1267 int target = READ16();
1268 if (!skvx::any(mask())) {
1269 ip = code + target;
1270 }
1271 NEXT();
1272 }
1273
1274 LABEL(kLoopBegin)
1275 contPtr[1] = 0;
1276 loopPtr[1] = loopPtr[0];
1277 ++contPtr; ++loopPtr;
1278 NEXT();
1279 LABEL(kLoopNext)
1280 *loopPtr |= *contPtr;
1281 *contPtr = 0;
1282 NEXT();
1283 LABEL(kLoopMask)
1284 *loopPtr &= POP().fSigned;
1285 NEXT();
1286 LABEL(kLoopEnd)
1287 --contPtr; --loopPtr;
1288 NEXT();
1289 LABEL(kLoopBreak)
1290 *loopPtr &= ~mask();
1291 NEXT();
1292 LABEL(kLoopContinue) {
1293 I32 m = mask();
1294 *contPtr |= m;
1295 *loopPtr &= ~m;
1296 NEXT();
1297 }
1298#ifdef SKSLC_THREADED_CODE
1299 #ifdef TRACE
1300 next:
1301 printf("at %3d (stack: %d) (disable threaded code for disassembly)\n",
1302 (int) (ip - code), (int) (sp - stack) + 1);
1303 goto *READ_INST();
1304 #endif
1305#else
1306 }
1307 }
1308#endif
Brian Osman569f12f2019-06-13 11:23:57 -04001309}
1310
Brian Osman08a84962019-06-14 10:17:16 -04001311} // namespace Interpreter
1312
Brian Osman489cf882019-07-09 10:48:28 -04001313#endif // SK_ENABLE_SKSL_INTERPRETER
1314
Mike Reed634c9412019-07-18 13:20:04 -04001315#undef spf
1316
Brian Osman08a84962019-06-14 10:17:16 -04001317void ByteCodeFunction::disassemble() const {
Brian Osman489cf882019-07-09 10:48:28 -04001318#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman08a84962019-06-14 10:17:16 -04001319 const uint8_t* ip = fCode.data();
1320 while (ip < fCode.data() + fCode.size()) {
1321 printf("%d: ", (int)(ip - fCode.data()));
1322 ip = Interpreter::disassemble_instruction(ip);
1323 printf("\n");
1324 }
Brian Osman489cf882019-07-09 10:48:28 -04001325#endif
Brian Osman08a84962019-06-14 10:17:16 -04001326}
1327
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001328#define VECTOR_PREPROCESS(base) \
1329 case ByteCodeInstruction::base ## 4: \
1330 case ByteCodeInstruction::base ## 3: \
1331 case ByteCodeInstruction::base ## 2: \
1332 case ByteCodeInstruction::base: READ8(); break;
1333
1334#define VECTOR_PREPROCESS_NO_COUNT(base) \
1335 case ByteCodeInstruction::base ## 4: \
1336 case ByteCodeInstruction::base ## 3: \
1337 case ByteCodeInstruction::base ## 2: \
1338 case ByteCodeInstruction::base: break;
1339
1340#define VECTOR_MATRIX_PREPROCESS(base) \
1341 VECTOR_PREPROCESS(base) \
1342 case ByteCodeInstruction::base ## N: READ8(); break;
1343
1344#define VECTOR_MATRIX_PREPROCESS_NO_COUNT(base) \
1345 VECTOR_PREPROCESS_NO_COUNT(base) \
1346 case ByteCodeInstruction::base ## N: READ8(); break;
1347
1348void ByteCodeFunction::preprocess(const void* labels[]) {
1349#if defined(SK_ENABLE_SKSL_INTERPRETER)
1350#ifdef TRACE
1351 this->disassemble();
1352#endif
1353 fPreprocessed = true;
1354 uint8_t* ip = fCode.data();
1355 while (ip < fCode.data() + fCode.size()) {
1356 ByteCodeInstruction inst = (ByteCodeInstruction) (intptr_t) READ_INST();
1357 const void* label = labels[(int) inst];
1358 memcpy(ip - sizeof(instruction), &label, sizeof(label));
1359 switch (inst) {
1360 VECTOR_MATRIX_PREPROCESS(kAddF)
1361 VECTOR_PREPROCESS(kAddI)
1362 case ByteCodeInstruction::kAndB: break;
1363 case ByteCodeInstruction::kBranch: READ16(); break;
1364 case ByteCodeInstruction::kCall: READ8(); break;
1365 case ByteCodeInstruction::kCallExternal: {
1366 READ8();
1367 READ8();
1368 READ8();
1369 break;
1370 }
1371 case ByteCodeInstruction::kClampIndex: READ8(); break;
1372 VECTOR_PREPROCESS(kCompareIEQ)
1373 VECTOR_PREPROCESS(kCompareINEQ)
1374 VECTOR_MATRIX_PREPROCESS(kCompareFEQ)
1375 VECTOR_MATRIX_PREPROCESS(kCompareFNEQ)
1376 VECTOR_PREPROCESS(kCompareFGT)
1377 VECTOR_PREPROCESS(kCompareFGTEQ)
1378 VECTOR_PREPROCESS(kCompareFLT)
1379 VECTOR_PREPROCESS(kCompareFLTEQ)
1380 VECTOR_PREPROCESS(kCompareSGT)
1381 VECTOR_PREPROCESS(kCompareSGTEQ)
1382 VECTOR_PREPROCESS(kCompareSLT)
1383 VECTOR_PREPROCESS(kCompareSLTEQ)
1384 VECTOR_PREPROCESS(kCompareUGT)
1385 VECTOR_PREPROCESS(kCompareUGTEQ)
1386 VECTOR_PREPROCESS(kCompareULT)
1387 VECTOR_PREPROCESS(kCompareULTEQ)
1388 VECTOR_PREPROCESS_NO_COUNT(kConvertFtoI)
1389 VECTOR_PREPROCESS_NO_COUNT(kConvertStoF)
1390 VECTOR_PREPROCESS_NO_COUNT(kConvertUtoF)
1391 VECTOR_PREPROCESS(kCos)
1392 VECTOR_MATRIX_PREPROCESS(kDivideF)
1393 VECTOR_PREPROCESS(kDivideS)
1394 VECTOR_PREPROCESS(kDivideU)
1395 VECTOR_MATRIX_PREPROCESS(kDup)
1396
1397 case ByteCodeInstruction::kInverse2x2:
1398 case ByteCodeInstruction::kInverse3x3:
1399 case ByteCodeInstruction::kInverse4x4: break;
1400
1401 case ByteCodeInstruction::kLoad:
1402 case ByteCodeInstruction::kLoad2:
1403 case ByteCodeInstruction::kLoad3:
1404 case ByteCodeInstruction::kLoad4:
1405 case ByteCodeInstruction::kLoadGlobal:
1406 case ByteCodeInstruction::kLoadGlobal2:
1407 case ByteCodeInstruction::kLoadGlobal3:
1408 case ByteCodeInstruction::kLoadGlobal4: READ16(); break;
1409
1410 case ByteCodeInstruction::kLoadSwizzle:
1411 case ByteCodeInstruction::kLoadSwizzleGlobal: {
1412 READ8();
1413 int count = READ8();
1414 ip += count;
1415 break;
1416 }
1417
1418 case ByteCodeInstruction::kLoadExtended:
1419 case ByteCodeInstruction::kLoadExtendedGlobal:
1420 READ8();
1421 break;
1422
1423 case ByteCodeInstruction::kMatrixToMatrix: {
1424 READ8();
1425 READ8();
1426 READ8();
1427 READ8();
1428 break;
1429 }
1430 case ByteCodeInstruction::kMatrixMultiply: {
1431 READ8();
1432 READ8();
1433 READ8();
1434 break;
1435 }
1436 VECTOR_MATRIX_PREPROCESS(kMultiplyF)
1437 VECTOR_PREPROCESS(kMultiplyI)
1438 VECTOR_MATRIX_PREPROCESS_NO_COUNT(kNegateF)
1439 VECTOR_PREPROCESS_NO_COUNT(kNegateI)
1440 case ByteCodeInstruction::kNotB: break;
1441 case ByteCodeInstruction::kOrB: break;
1442 VECTOR_MATRIX_PREPROCESS_NO_COUNT(kPop)
1443 case ByteCodeInstruction::kPushImmediate: READ32(); break;
1444
1445 case ByteCodeInstruction::kReadExternal:
1446 case ByteCodeInstruction::kReadExternal2:
1447 case ByteCodeInstruction::kReadExternal3:
1448 case ByteCodeInstruction::kReadExternal4: READ16(); break;
1449
1450 VECTOR_PREPROCESS(kRemainderF)
1451 VECTOR_PREPROCESS(kRemainderS)
1452 VECTOR_PREPROCESS(kRemainderU)
1453 case ByteCodeInstruction::kReserve: READ8(); break;
1454 case ByteCodeInstruction::kReturn: READ8(); break;
1455 case ByteCodeInstruction::kScalarToMatrix: READ8(); READ8(); break;
1456 VECTOR_PREPROCESS(kSin)
1457 VECTOR_PREPROCESS_NO_COUNT(kSqrt)
1458
1459 case ByteCodeInstruction::kStore:
1460 case ByteCodeInstruction::kStore2:
1461 case ByteCodeInstruction::kStore3:
1462 case ByteCodeInstruction::kStore4:
1463 case ByteCodeInstruction::kStoreGlobal:
1464 case ByteCodeInstruction::kStoreGlobal2:
1465 case ByteCodeInstruction::kStoreGlobal3:
1466 case ByteCodeInstruction::kStoreGlobal4: READ8(); break;
1467
1468 case ByteCodeInstruction::kStoreSwizzle:
1469 case ByteCodeInstruction::kStoreSwizzleGlobal: {
1470 READ8();
1471 int count = READ8();
1472 ip += count;
1473 break;
1474 }
1475
1476 case ByteCodeInstruction::kStoreSwizzleIndirect:
1477 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
1478 int count = READ8();
1479 ip += count;
1480 break;
1481 }
1482
1483 case ByteCodeInstruction::kStoreExtended: READ8(); break;
1484 case ByteCodeInstruction::kStoreExtendedGlobal: READ8(); break;
1485
1486 VECTOR_MATRIX_PREPROCESS(kSubtractF)
1487 VECTOR_PREPROCESS(kSubtractI)
1488
1489 case ByteCodeInstruction::kSwizzle: {
1490 READ8();
1491 int count = READ8();
1492 ip += count;
1493 break;
1494 }
1495 VECTOR_PREPROCESS(kTan)
1496 case ByteCodeInstruction::kWriteExternal:
1497 case ByteCodeInstruction::kWriteExternal2:
1498 case ByteCodeInstruction::kWriteExternal3:
1499 case ByteCodeInstruction::kWriteExternal4: READ16(); break;
1500
1501 case ByteCodeInstruction::kXorB: break;
1502 case ByteCodeInstruction::kMaskPush: break;
1503 case ByteCodeInstruction::kMaskPop: break;
1504 case ByteCodeInstruction::kMaskNegate: break;
1505 case ByteCodeInstruction::kMaskBlend: READ8(); break;
1506 case ByteCodeInstruction::kBranchIfAllFalse: READ16(); break;
1507 case ByteCodeInstruction::kLoopBegin: break;
1508 case ByteCodeInstruction::kLoopNext: break;
1509 case ByteCodeInstruction::kLoopMask: break;
1510 case ByteCodeInstruction::kLoopEnd: break;
1511 case ByteCodeInstruction::kLoopContinue: break;
1512 case ByteCodeInstruction::kLoopBreak: break;
1513 default:
1514 ip -= 2;
1515 printf("unknown(%d)\n", READ16());
1516 SkASSERT(false);
1517 }
1518 }
1519#endif
1520}
1521
Brian Osman869a3e82019-07-18 17:00:34 -04001522bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
Brian Osman08a84962019-06-14 10:17:16 -04001523 const float* uniforms, int uniformCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001524#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman4b202a32019-06-21 09:50:29 -04001525 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001526 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1527 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman869a3e82019-07-18 17:00:34 -04001528 return false;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001529 }
Brian Osmanef787f72019-06-13 13:07:12 -04001530
Brian Osman869a3e82019-07-18 17:00:34 -04001531 if (uniformCount != (int)fInputSlots.size()) {
1532 return false;
1533 }
1534
Brian Osman08a84962019-06-14 10:17:16 -04001535 Interpreter::VValue globals[32];
Brian Osman869a3e82019-07-18 17:00:34 -04001536 if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
1537 return false;
1538 }
Brian Osman08a84962019-06-14 10:17:16 -04001539 for (uint8_t slot : fInputSlots) {
1540 globals[slot].fFloat = *uniforms++;
Brian Osman569f12f2019-06-13 11:23:57 -04001541 }
1542
Brian Osman1a79f0b2019-06-24 16:32:14 -04001543 int baseIndex = 0;
1544
Brian Osman569f12f2019-06-13 11:23:57 -04001545 while (N) {
Brian Osman08a84962019-06-14 10:17:16 -04001546 int w = std::min(N, Interpreter::VecWidth);
Brian Osman569f12f2019-06-13 11:23:57 -04001547
1548 // Transpose args into stack
1549 {
Brian Osman08a84962019-06-14 10:17:16 -04001550 float* src = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001551 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001552 float* dst = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001553 for (int j = f->fParameterCount; j > 0; --j) {
1554 *dst = *src++;
Brian Osman08a84962019-06-14 10:17:16 -04001555 dst += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001556 }
1557 }
1558 }
1559
Mike Reed3fd3cc92019-06-20 12:40:30 -04001560 bool stripedOutput = false;
1561 float** outArray = outReturn ? &outReturn : nullptr;
Brian Osman869a3e82019-07-18 17:00:34 -04001562 if (!innerRun(this, f, stack, outArray, globals, stripedOutput, w, baseIndex)) {
1563 return false;
1564 }
Brian Osman569f12f2019-06-13 11:23:57 -04001565
1566 // Transpose out parameters back
1567 {
Brian Osman08a84962019-06-14 10:17:16 -04001568 float* dst = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001569 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001570 float* src = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001571 for (const auto& p : f->fParameters) {
1572 if (p.fIsOutParameter) {
1573 for (int j = p.fSlotCount; j > 0; --j) {
1574 *dst++ = *src;
Brian Osman08a84962019-06-14 10:17:16 -04001575 src += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001576 }
1577 } else {
1578 dst += p.fSlotCount;
Brian Osman08a84962019-06-14 10:17:16 -04001579 src += p.fSlotCount * Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001580 }
1581 }
1582 }
1583 }
1584
1585 args += f->fParameterCount * w;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001586 if (outReturn) {
1587 outReturn += f->fReturnCount * w;
1588 }
Brian Osman4b202a32019-06-21 09:50:29 -04001589 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001590 baseIndex += w;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001591 }
Brian Osman869a3e82019-07-18 17:00:34 -04001592 return true;
Brian Osman489cf882019-07-09 10:48:28 -04001593#else
1594 SkDEBUGFAIL("ByteCode interpreter not enabled");
Brian Osman869a3e82019-07-18 17:00:34 -04001595 return false;
Brian Osman489cf882019-07-09 10:48:28 -04001596#endif
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001597}
1598
Brian Osman869a3e82019-07-18 17:00:34 -04001599bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
Mike Reed3fd3cc92019-06-20 12:40:30 -04001600 const float* uniforms, int uniformCount,
1601 float* outArgs[], int outCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001602#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman2b1a5442019-06-19 11:40:33 -04001603 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001604 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1605 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman869a3e82019-07-18 17:00:34 -04001606 return false;
1607 }
1608
1609 if (nargs != f->fParameterCount ||
1610 outCount != f->fReturnCount ||
1611 uniformCount != (int)fInputSlots.size()) {
1612 return false;
1613 }
1614
1615 Interpreter::VValue globals[32];
1616 if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
1617 return false;
1618 }
1619 for (uint8_t slot : fInputSlots) {
1620 globals[slot].fFloat = *uniforms++;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001621 }
Brian Osman2b1a5442019-06-19 11:40:33 -04001622
Mike Reed3fd3cc92019-06-20 12:40:30 -04001623 // innerRun just takes outArgs, so clear it if the count is zero
1624 if (outCount == 0) {
1625 outArgs = nullptr;
1626 }
1627
Brian Osman1a79f0b2019-06-24 16:32:14 -04001628 int baseIndex = 0;
1629
Brian Osman2b1a5442019-06-19 11:40:33 -04001630 while (N) {
1631 int w = std::min(N, Interpreter::VecWidth);
1632
1633 // Copy args into stack
1634 for (int i = 0; i < nargs; ++i) {
1635 memcpy(stack + i, args[i], w * sizeof(float));
1636 }
1637
Mike Reed3fd3cc92019-06-20 12:40:30 -04001638 bool stripedOutput = true;
Brian Osman869a3e82019-07-18 17:00:34 -04001639 if (!innerRun(this, f, stack, outArgs, globals, stripedOutput, w, baseIndex)) {
1640 return false;
1641 }
Brian Osman2b1a5442019-06-19 11:40:33 -04001642
1643 // Copy out parameters back
1644 int slot = 0;
1645 for (const auto& p : f->fParameters) {
1646 if (p.fIsOutParameter) {
1647 for (int i = slot; i < slot + p.fSlotCount; ++i) {
1648 memcpy(args[i], stack + i, w * sizeof(float));
1649 }
1650 }
1651 slot += p.fSlotCount;
1652 }
1653
1654 // Step each argument pointer ahead
1655 for (int i = 0; i < nargs; ++i) {
1656 args[i] += w;
1657 }
1658 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001659 baseIndex += w;
Brian Osman2b1a5442019-06-19 11:40:33 -04001660 }
Brian Osman869a3e82019-07-18 17:00:34 -04001661
1662 return true;
Brian Osman489cf882019-07-09 10:48:28 -04001663#else
1664 SkDEBUGFAIL("ByteCode interpreter not enabled");
Brian Osman869a3e82019-07-18 17:00:34 -04001665 return false;
Brian Osman489cf882019-07-09 10:48:28 -04001666#endif
Brian Osman2b1a5442019-06-19 11:40:33 -04001667}
1668
Brian Osman80164412019-06-07 13:00:23 -04001669} // namespace SkSL
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001670
1671#endif