blob: f95acaace2881b433c163dda90f15ac8963f552c [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 Osman80164412019-06-07 13:00:23 -040020namespace Interpreter {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040021
Mike Reed3fd3cc92019-06-20 12:40:30 -040022constexpr int VecWidth = ByteCode::kVecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -040023
24using F32 = skvx::Vec<VecWidth, float>;
25using I32 = skvx::Vec<VecWidth, int32_t>;
26using U32 = skvx::Vec<VecWidth, uint32_t>;
27
Mike Kleine7007382019-05-21 08:36:32 -050028#define READ8() (*(ip++))
Mike Klein7a177b42019-06-17 17:17:47 -050029#define READ16() (ip += 2, sk_unaligned_load<uint16_t>(ip - 2))
30#define READ32() (ip += 4, sk_unaligned_load<uint32_t>(ip - 4))
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040031
Brian Osman3e833e12019-05-23 13:23:24 -070032#define VECTOR_DISASSEMBLE(op, text) \
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040033 case ByteCodeInstruction::op: printf(text); break; \
34 case ByteCodeInstruction::op##2: printf(text "2"); break; \
35 case ByteCodeInstruction::op##3: printf(text "3"); break; \
36 case ByteCodeInstruction::op##4: printf(text "4"); break;
37
Brian Osman1e855b22019-05-29 15:21:52 -040038#define VECTOR_MATRIX_DISASSEMBLE(op, text) \
39 case ByteCodeInstruction::op: printf(text); break; \
40 case ByteCodeInstruction::op##2: printf(text "2"); break; \
41 case ByteCodeInstruction::op##3: printf(text "3"); break; \
42 case ByteCodeInstruction::op##4: printf(text "4"); break; \
43 case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
44
Brian Osman3e833e12019-05-23 13:23:24 -070045static const uint8_t* disassemble_instruction(const uint8_t* ip) {
46 switch ((ByteCodeInstruction) READ16()) {
Brian Osman1e855b22019-05-29 15:21:52 -040047 VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf")
Brian Osman3e833e12019-05-23 13:23:24 -070048 VECTOR_DISASSEMBLE(kAddI, "addi")
Brian Osman32c526b2019-06-03 16:13:52 -040049 case ByteCodeInstruction::kAndB: printf("andb"); break;
Brian Osman3e833e12019-05-23 13:23:24 -070050 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
51 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
52 case ByteCodeInstruction::kCallExternal: {
53 int argumentCount = READ8();
54 int returnCount = READ8();
55 int externalValue = READ8();
56 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
57 break;
58 }
59 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
60 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
Brian Osman1e855b22019-05-29 15:21:52 -040061 VECTOR_MATRIX_DISASSEMBLE(kCompareFEQ, "comparefeq")
62 VECTOR_MATRIX_DISASSEMBLE(kCompareFNEQ, "comparefneq")
Brian Osman3e833e12019-05-23 13:23:24 -070063 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
64 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
65 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
66 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
67 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
68 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
69 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
70 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
71 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
72 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
73 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
74 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
Brian Osman3e833e12019-05-23 13:23:24 -070075 VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi")
76 VECTOR_DISASSEMBLE(kConvertStoF, "convertstof")
77 VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof")
78 VECTOR_DISASSEMBLE(kCos, "cos")
Brian Osmanecb3bb52019-06-20 14:59:12 -040079 case ByteCodeInstruction::kCross: printf("cross"); break;
Brian Osman1e855b22019-05-29 15:21:52 -040080 VECTOR_MATRIX_DISASSEMBLE(kDivideF, "dividef")
Brian Osman3e833e12019-05-23 13:23:24 -070081 VECTOR_DISASSEMBLE(kDivideS, "divideS")
82 VECTOR_DISASSEMBLE(kDivideU, "divideu")
Brian Osman1e855b22019-05-29 15:21:52 -040083 VECTOR_MATRIX_DISASSEMBLE(kDup, "dup")
Brian Osman3e833e12019-05-23 13:23:24 -070084 case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break;
85 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break;
86 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break;
87 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break;
88 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break;
89 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break;
90 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break;
91 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break;
92 case ByteCodeInstruction::kLoadSwizzle: {
93 int target = READ8();
94 int count = READ8();
95 printf("loadswizzle %d %d", target, count);
96 for (int i = 0; i < count; ++i) {
97 printf(", %d", READ8());
98 }
99 break;
100 }
101 case ByteCodeInstruction::kLoadSwizzleGlobal: {
102 int target = READ8();
103 int count = READ8();
104 printf("loadswizzleglobal %d %d", target, count);
105 for (int i = 0; i < count; ++i) {
106 printf(", %d", READ8());
107 }
108 break;
109 }
110 case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break;
111 case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8());
112 break;
Brian Osman29e013d2019-05-28 17:16:03 -0400113 case ByteCodeInstruction::kMatrixToMatrix: {
114 int srcCols = READ8();
115 int srcRows = READ8();
116 int dstCols = READ8();
117 int dstRows = READ8();
118 printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows);
119 break;
120 }
Brian Osman909231c2019-05-29 15:34:36 -0400121 case ByteCodeInstruction::kMatrixMultiply: {
122 int lCols = READ8();
123 int lRows = READ8();
124 int rCols = READ8();
125 printf("matrixmultiply %dx%d %dx%d", lCols, lRows, rCols, lCols);
126 break;
127 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400128 VECTOR_DISASSEMBLE(kMix, "mix")
Brian Osman1e855b22019-05-29 15:21:52 -0400129 VECTOR_MATRIX_DISASSEMBLE(kMultiplyF, "multiplyf")
Brian Osman3e833e12019-05-23 13:23:24 -0700130 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
Brian Osman1e855b22019-05-29 15:21:52 -0400131 VECTOR_MATRIX_DISASSEMBLE(kNegateF, "negatef")
Brian Osman3e833e12019-05-23 13:23:24 -0700132 VECTOR_DISASSEMBLE(kNegateI, "negatei")
Brian Osman569f12f2019-06-13 11:23:57 -0400133 case ByteCodeInstruction::kNotB: printf("notb"); break;
Brian Osman32c526b2019-06-03 16:13:52 -0400134 case ByteCodeInstruction::kOrB: printf("orb"); break;
Brian Osman1e855b22019-05-29 15:21:52 -0400135 VECTOR_MATRIX_DISASSEMBLE(kPop, "pop")
Brian Osman3e833e12019-05-23 13:23:24 -0700136 case ByteCodeInstruction::kPushImmediate: {
137 uint32_t v = READ32();
138 union { uint32_t u; float f; } pun = { v };
139 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
140 break;
141 }
142 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
143 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
144 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
145 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
146 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
147 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
148 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
Brian Osmand3494ed2019-06-20 15:41:34 -0400149 case ByteCodeInstruction::kReserve: printf("reserve %d", READ8()); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700150 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400151 case ByteCodeInstruction::kScalarToMatrix: {
152 int cols = READ8();
153 int rows = READ8();
154 printf("scalartomatrix %dx%d", cols, rows);
155 break;
156 }
Brian Osman3e833e12019-05-23 13:23:24 -0700157 VECTOR_DISASSEMBLE(kSin, "sin")
158 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
159 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
160 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
161 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
162 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
163 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
164 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
165 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
166 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
167 case ByteCodeInstruction::kStoreSwizzle: {
168 int target = READ8();
169 int count = READ8();
170 printf("storeswizzle %d %d", target, count);
171 for (int i = 0; i < count; ++i) {
172 printf(", %d", READ8());
173 }
174 break;
175 }
176 case ByteCodeInstruction::kStoreSwizzleGlobal: {
177 int target = READ8();
178 int count = READ8();
179 printf("storeswizzleglobal %d %d", target, count);
180 for (int i = 0; i < count; ++i) {
181 printf(", %d", READ8());
182 }
183 break;
184 }
185 case ByteCodeInstruction::kStoreSwizzleIndirect: {
186 int count = READ8();
187 printf("storeswizzleindirect %d", count);
188 for (int i = 0; i < count; ++i) {
189 printf(", %d", READ8());
190 }
191 break;
192 }
193 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
194 int count = READ8();
195 printf("storeswizzleindirectglobal %d", count);
196 for (int i = 0; i < count; ++i) {
197 printf(", %d", READ8());
198 }
199 break;
200 }
201 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
202 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
203 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400204 VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf")
Brian Osman3e833e12019-05-23 13:23:24 -0700205 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
206 case ByteCodeInstruction::kSwizzle: {
207 printf("swizzle %d, ", READ8());
208 int count = READ8();
209 printf("%d", count);
210 for (int i = 0; i < count; ++i) {
211 printf(", %d", READ8());
212 }
213 break;
214 }
215 VECTOR_DISASSEMBLE(kTan, "tan")
216 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
217 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
218 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
219 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
Brian Osman569f12f2019-06-13 11:23:57 -0400220 case ByteCodeInstruction::kXorB: printf("xorb"); break;
221 case ByteCodeInstruction::kMaskPush: printf("maskpush"); break;
222 case ByteCodeInstruction::kMaskPop: printf("maskpop"); break;
223 case ByteCodeInstruction::kMaskNegate: printf("masknegate"); break;
224 case ByteCodeInstruction::kMaskBlend: printf("maskblend %d", READ8()); break;
225 case ByteCodeInstruction::kBranchIfAllFalse:
226 printf("branchifallfalse %d", READ16());
227 break;
228 case ByteCodeInstruction::kLoopBegin: printf("loopbegin"); break;
229 case ByteCodeInstruction::kLoopNext: printf("loopnext"); break;
230 case ByteCodeInstruction::kLoopMask: printf("loopmask"); break;
231 case ByteCodeInstruction::kLoopEnd: printf("loopend"); break;
232 case ByteCodeInstruction::kLoopContinue: printf("loopcontinue"); break;
233 case ByteCodeInstruction::kLoopBreak: printf("loopbreak"); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700234 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
235 }
236 return ip;
237}
238
Mike Klein459aed12019-05-21 15:46:36 -0500239#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500240 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500241 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500242 POP(); \
243 /* fall through */ \
244 case ByteCodeInstruction::base ## 3: { \
245 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500246 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500247 POP(); \
248 } /* fall through */ \
249 case ByteCodeInstruction::base ## 2: { \
250 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500251 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500252 POP(); \
253 } /* fall through */ \
254 case ByteCodeInstruction::base: { \
255 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500256 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500257 POP(); \
258 break; \
259 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400260
Brian Osman1e855b22019-05-29 15:21:52 -0400261#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
262 VECTOR_BINARY_OP(base, field, op) \
263 case ByteCodeInstruction::base ## N: { \
264 int count = READ8(); \
265 for (int i = count; i > 0; --i) { \
266 sp[-count] = sp[-count].field op sp[0].field; \
267 POP(); \
268 } \
269 break; \
270 }
271
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400272#define VECTOR_BINARY_FN(base, field, fn) \
273 case ByteCodeInstruction::base ## 4: \
274 sp[-4] = fn(sp[-4].field, sp[0].field); \
275 POP(); \
276 /* fall through */ \
277 case ByteCodeInstruction::base ## 3: { \
278 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
279 sp[target] = fn(sp[target].field, sp[0].field); \
280 POP(); \
281 } /* fall through */ \
282 case ByteCodeInstruction::base ## 2: { \
283 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
284 sp[target] = fn(sp[target].field, sp[0].field); \
285 POP(); \
286 } /* fall through */ \
287 case ByteCodeInstruction::base: { \
288 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
289 sp[target] = fn(sp[target].field, sp[0].field); \
290 POP(); \
291 break; \
Mike Kleine7007382019-05-21 08:36:32 -0500292 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400293
Mike Klein459aed12019-05-21 15:46:36 -0500294#define VECTOR_UNARY_FN(base, fn, field) \
295 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
296 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
297 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
298 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400299 break;
300
Brian Osman569f12f2019-06-13 11:23:57 -0400301#define VECTOR_UNARY_FN_VEC(base, fn) \
302 case ByteCodeInstruction::base ## 4: \
303 case ByteCodeInstruction::base ## 3: \
304 case ByteCodeInstruction::base ## 2: \
305 case ByteCodeInstruction::base : { \
306 int count = (int)inst - (int)ByteCodeInstruction::base + 1; \
307 float* v = (float*)sp - count + 1; \
308 for (int i = VecWidth * count; i > 0; --i, ++v) { \
309 *v = fn(*v); \
310 } \
311 break; \
312 }
313
314union VValue {
315 VValue() {}
316
317 VValue(F32 f)
318 : fFloat(f) {
319 }
320
321 VValue(I32 s)
322 : fSigned(s) {
323 }
324
325 VValue(U32 u)
326 : fUnsigned(u) {
327 }
328
329 F32 fFloat;
330 I32 fSigned;
331 U32 fUnsigned;
332};
333
Brian Osman226668a2019-05-14 16:47:30 -0400334struct StackFrame {
335 const uint8_t* fCode;
336 const uint8_t* fIP;
Brian Osman569f12f2019-06-13 11:23:57 -0400337 VValue* fStack;
Brian Osmand3494ed2019-06-20 15:41:34 -0400338 int fParameterCount;
Brian Osman226668a2019-05-14 16:47:30 -0400339};
340
Brian Osman569f12f2019-06-13 11:23:57 -0400341static F32 mix(F32 start, F32 end, F32 t) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400342 return start * (1 - t) + end * t;
343}
344
Brian Osman569f12f2019-06-13 11:23:57 -0400345// TODO: trunc on integers?
346template <typename T>
347static T vec_mod(T a, T b) {
348 return a - skvx::trunc(a / b) * b;
349}
Mike Kleine7007382019-05-21 08:36:32 -0500350
Brian Osman08a84962019-06-14 10:17:16 -0400351void innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
Mike Reed3fd3cc92019-06-20 12:40:30 -0400352 float* outReturn[], I32 initMask, VValue globals[], bool stripedOutput) {
Brian Osman569f12f2019-06-13 11:23:57 -0400353 VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1;
354
355 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
356 auto PUSH = [&](VValue v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
Mike Kleine7007382019-05-21 08:36:32 -0500357
Brian Osman80164412019-06-07 13:00:23 -0400358 const uint8_t* code = f->fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400359 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400360 std::vector<StackFrame> frames;
361
Brian Osman569f12f2019-06-13 11:23:57 -0400362 I32 condStack[16]; // Independent condition masks
363 I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...)
364 I32 contStack[16]; // Continue flags for loops
365 I32 loopStack[16]; // Loop execution masks
366 condStack[0] = maskStack[0] = initMask;
367 contStack[0] = I32( 0);
368 loopStack[0] = I32(~0);
369 I32* condPtr = condStack;
370 I32* maskPtr = maskStack;
371 I32* contPtr = contStack;
372 I32* loopPtr = loopStack;
373
374 auto mask = [&]() { return *maskPtr & *loopPtr; };
375
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400376 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400377#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700378 printf("at %3d ", (int) (ip - code));
379 disassemble_instruction(ip);
380 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400381#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700382 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400383 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500384 VECTOR_BINARY_OP(kAddI, fSigned, +)
Brian Osman1e855b22019-05-29 15:21:52 -0400385 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
Brian Osman569f12f2019-06-13 11:23:57 -0400386
387 // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want:
Brian Osman32c526b2019-06-03 16:13:52 -0400388 case ByteCodeInstruction::kAndB:
Brian Osman569f12f2019-06-13 11:23:57 -0400389 sp[-1] = sp[-1].fSigned & sp[0].fSigned;
390 POP();
391 break;
392 case ByteCodeInstruction::kNotB:
393 sp[0] = ~sp[0].fSigned;
394 break;
395 case ByteCodeInstruction::kOrB:
396 sp[-1] = sp[-1].fSigned | sp[0].fSigned;
397 POP();
398 break;
399 case ByteCodeInstruction::kXorB:
400 sp[-1] = sp[-1].fSigned ^ sp[0].fSigned;
Brian Osman32c526b2019-06-03 16:13:52 -0400401 POP();
402 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400403
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400404 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400405 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400406 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400407
Brian Osman226668a2019-05-14 16:47:30 -0400408 case ByteCodeInstruction::kCall: {
Brian Osmand3494ed2019-06-20 15:41:34 -0400409 // Precursor code reserved space for the return value, and pushed all parameters to
410 // the stack. Update our bottom of stack to point at the first parameter, and our
411 // sp to point past those parameters (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500412 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400413 const ByteCodeFunction* fun = byteCode->fFunctions[target].get();
Brian Osman569f12f2019-06-13 11:23:57 -0400414 if (skvx::any(mask())) {
Brian Osmand3494ed2019-06-20 15:41:34 -0400415 frames.push_back({ code, ip, stack, fun->fParameterCount });
Brian Osman569f12f2019-06-13 11:23:57 -0400416 ip = code = fun->fCode.data();
417 stack = sp - fun->fParameterCount + 1;
418 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
Brian Osman569f12f2019-06-13 11:23:57 -0400419 }
Brian Osman226668a2019-05-14 16:47:30 -0400420 break;
421 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400422
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400423 case ByteCodeInstruction::kCallExternal: {
424 int argumentCount = READ8();
425 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500426 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400427 ExternalValue* v = byteCode->fExternalValues[target];
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400428 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500429
Brian Osman08a84962019-06-14 10:17:16 -0400430 int32_t tmpArgs[4];
431 int32_t tmpReturn[4];
Brian Osman569f12f2019-06-13 11:23:57 -0400432 SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs));
433 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn));
434
435 I32 m = mask();
436 for (int i = 0; i < VecWidth; ++i) {
437 if (m[i]) {
438 for (int j = 0; j < argumentCount; ++j) {
Brian Osman08a84962019-06-14 10:17:16 -0400439 tmpArgs[j] = sp[j].fSigned[i];
Brian Osman569f12f2019-06-13 11:23:57 -0400440 }
441 v->call(tmpArgs, tmpReturn);
442 for (int j = 0; j < returnCount; ++j) {
Brian Osman08a84962019-06-14 10:17:16 -0400443 sp[j].fSigned[i] = tmpReturn[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400444 }
445 }
446 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400447 sp += returnCount - 1;
448 break;
449 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400450
Mike Kleinc1999982019-05-21 13:03:49 -0500451 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
Brian Osman1e855b22019-05-29 15:21:52 -0400452 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
Mike Kleinc1999982019-05-21 13:03:49 -0500453 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
Brian Osman1e855b22019-05-29 15:21:52 -0400454 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
Mike Kleinc1999982019-05-21 13:03:49 -0500455 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
456 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
457 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
458 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
459 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
460 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
461 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
462 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
463 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
464 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
465 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
466 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400467
Brian Osman569f12f2019-06-13 11:23:57 -0400468 case ByteCodeInstruction::kConvertFtoI4: sp[-3] = skvx::cast<int>(sp[-3].fFloat);
469 case ByteCodeInstruction::kConvertFtoI3: sp[-2] = skvx::cast<int>(sp[-2].fFloat);
470 case ByteCodeInstruction::kConvertFtoI2: sp[-1] = skvx::cast<int>(sp[-1].fFloat);
471 case ByteCodeInstruction::kConvertFtoI: sp[ 0] = skvx::cast<int>(sp[ 0].fFloat);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400472 break;
473
Brian Osman569f12f2019-06-13 11:23:57 -0400474 case ByteCodeInstruction::kConvertStoF4: sp[-3] = skvx::cast<float>(sp[-3].fSigned);
475 case ByteCodeInstruction::kConvertStoF3: sp[-2] = skvx::cast<float>(sp[-2].fSigned);
476 case ByteCodeInstruction::kConvertStoF2: sp[-1] = skvx::cast<float>(sp[-1].fSigned);
477 case ByteCodeInstruction::kConvertStoF : sp[ 0] = skvx::cast<float>(sp[ 0].fSigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400478 break;
479
Brian Osman569f12f2019-06-13 11:23:57 -0400480 case ByteCodeInstruction::kConvertUtoF4: sp[-3] = skvx::cast<float>(sp[-3].fUnsigned);
481 case ByteCodeInstruction::kConvertUtoF3: sp[-2] = skvx::cast<float>(sp[-2].fUnsigned);
482 case ByteCodeInstruction::kConvertUtoF2: sp[-1] = skvx::cast<float>(sp[-1].fUnsigned);
483 case ByteCodeInstruction::kConvertUtoF : sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400484 break;
485
Brian Osman569f12f2019-06-13 11:23:57 -0400486 VECTOR_UNARY_FN_VEC(kCos, cosf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400487
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400488 case ByteCodeInstruction::kCross: {
Brian Osman569f12f2019-06-13 11:23:57 -0400489 F32 ax = sp[-5].fFloat, ay = sp[-4].fFloat, az = sp[-3].fFloat,
490 bx = sp[-2].fFloat, by = sp[-1].fFloat, bz = sp[ 0].fFloat;
491 F32 cx = ay*bz - az*by,
492 cy = az*bx - ax*bz,
493 cz = ax*by - ay*bx;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400494 sp -= 3;
Brian Osman569f12f2019-06-13 11:23:57 -0400495 sp[-2] = cx;
496 sp[-1] = cy;
497 sp[ 0] = cz;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400498 break;
499 }
500
Mike Kleinc1999982019-05-21 13:03:49 -0500501 VECTOR_BINARY_OP(kDivideS, fSigned, /)
502 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
Brian Osman1e855b22019-05-29 15:21:52 -0400503 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500504
505 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
506 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
507 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
508 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
509 break;
510
Brian Osman07c117b2019-05-23 12:51:06 -0700511 case ByteCodeInstruction::kDupN: {
512 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400513 memcpy(sp + 1, sp - count + 1, count * sizeof(VValue));
Brian Osman07c117b2019-05-23 12:51:06 -0700514 sp += count;
515 break;
516 }
517
Mike Kleine7007382019-05-21 08:36:32 -0500518 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
519 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
520 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
521 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
522 ++ip;
523 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
524 break;
525
Brian Osman80164412019-06-07 13:00:23 -0400526 case ByteCodeInstruction::kLoadGlobal4: sp[4] = globals[*ip + 3];
527 case ByteCodeInstruction::kLoadGlobal3: sp[3] = globals[*ip + 2];
528 case ByteCodeInstruction::kLoadGlobal2: sp[2] = globals[*ip + 1];
529 case ByteCodeInstruction::kLoadGlobal : sp[1] = globals[*ip + 0];
Mike Kleine7007382019-05-21 08:36:32 -0500530 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700531 sp += (int)inst -
532 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500533 break;
534
Brian Osman07c117b2019-05-23 12:51:06 -0700535 case ByteCodeInstruction::kLoadExtended: {
536 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400537 I32 src = POP().fSigned;
538 I32 m = mask();
539 for (int i = 0; i < count; ++i) {
540 for (int j = 0; j < VecWidth; ++j) {
541 if (m[j]) {
542 sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j];
543 }
544 }
545 }
Brian Osman07c117b2019-05-23 12:51:06 -0700546 sp += count;
547 break;
548 }
549
550 case ByteCodeInstruction::kLoadExtendedGlobal: {
551 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400552 I32 src = POP().fSigned;
553 I32 m = mask();
554 for (int i = 0; i < count; ++i) {
555 for (int j = 0; j < VecWidth; ++j) {
556 if (m[j]) {
557 sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j];
558 }
559 }
560 }
Brian Osman07c117b2019-05-23 12:51:06 -0700561 sp += count;
562 break;
563 }
564
Mike Kleine7007382019-05-21 08:36:32 -0500565 case ByteCodeInstruction::kLoadSwizzle: {
566 int src = READ8();
567 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400568 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400569 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400570 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400571 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400572 break;
Mike Kleine7007382019-05-21 08:36:32 -0500573 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400574
Mike Kleine7007382019-05-21 08:36:32 -0500575 case ByteCodeInstruction::kLoadSwizzleGlobal: {
576 int src = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500577 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400578 for (int i = 0; i < count; ++i) {
Brian Osman80164412019-06-07 13:00:23 -0400579 PUSH(globals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400580 }
581 ip += count;
582 break;
Mike Kleine7007382019-05-21 08:36:32 -0500583 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400584
Brian Osman29e013d2019-05-28 17:16:03 -0400585 case ByteCodeInstruction::kMatrixToMatrix: {
586 int srcCols = READ8();
587 int srcRows = READ8();
588 int dstCols = READ8();
589 int dstRows = READ8();
590 SkASSERT(srcCols >= 2 && srcCols <= 4);
591 SkASSERT(srcRows >= 2 && srcRows <= 4);
592 SkASSERT(dstCols >= 2 && dstCols <= 4);
593 SkASSERT(dstRows >= 2 && dstRows <= 4);
Brian Osman569f12f2019-06-13 11:23:57 -0400594 F32 tmp[16];
595 memset(tmp, 0, sizeof(tmp));
596 tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f);
Brian Osman29e013d2019-05-28 17:16:03 -0400597 for (int c = srcCols - 1; c >= 0; --c) {
598 for (int r = srcRows - 1; r >= 0; --r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400599 tmp[c*4 + r] = POP().fFloat;
Brian Osman29e013d2019-05-28 17:16:03 -0400600 }
601 }
602 for (int c = 0; c < dstCols; ++c) {
603 for (int r = 0; r < dstRows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400604 PUSH(tmp[c*4 + r]);
Brian Osman29e013d2019-05-28 17:16:03 -0400605 }
606 }
607 break;
608 }
609
Brian Osman909231c2019-05-29 15:34:36 -0400610 case ByteCodeInstruction::kMatrixMultiply: {
611 int lCols = READ8();
612 int lRows = READ8();
613 int rCols = READ8();
614 int rRows = lCols;
Brian Osman569f12f2019-06-13 11:23:57 -0400615 F32 tmp[16] = { 0.0f };
616 F32* B = &(sp - (rCols * rRows) + 1)->fFloat;
617 F32* A = B - (lCols * lRows);
Brian Osman909231c2019-05-29 15:34:36 -0400618 for (int c = 0; c < rCols; ++c) {
619 for (int r = 0; r < lRows; ++r) {
620 for (int j = 0; j < lCols; ++j) {
621 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
622 }
623 }
624 }
625 sp -= (lCols * lRows) + (rCols * rRows);
Brian Osman569f12f2019-06-13 11:23:57 -0400626 memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue));
Brian Osman909231c2019-05-29 15:34:36 -0400627 sp += (rCols * lRows);
628 break;
629 }
630
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400631 // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T
632 case ByteCodeInstruction::kMix4:
633 sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat);
634 // fall through
635 case ByteCodeInstruction::kMix3: {
636 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
637 int target = 2 - count * 2;
638 sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat);
639 // fall through
640 }
641 case ByteCodeInstruction::kMix2: {
642 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
643 int target = 1 - count * 2;
644 sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat);
645 // fall through
646 }
647 case ByteCodeInstruction::kMix: {
648 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
649 int target = -count * 2;
650 sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat);
651 sp -= 1 + count;
652 break;
653 }
654
Mike Kleinc1999982019-05-21 13:03:49 -0500655 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
Brian Osman1e855b22019-05-29 15:21:52 -0400656 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400657
Mike Kleinc1999982019-05-21 13:03:49 -0500658 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
659 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
660 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
661 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500662 break;
663
Brian Osman1e855b22019-05-29 15:21:52 -0400664 case ByteCodeInstruction::kNegateFN: {
665 int count = READ8();
666 for (int i = count - 1; i >= 0; --i) {
667 sp[-i] = -sp[-i].fFloat;
668 }
669 break;
670 }
671
Mike Kleinc1999982019-05-21 13:03:49 -0500672 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
673 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
674 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
Brian Osman569f12f2019-06-13 11:23:57 -0400675 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp[ 0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500676 break;
677
678 case ByteCodeInstruction::kPop4: POP();
679 case ByteCodeInstruction::kPop3: POP();
680 case ByteCodeInstruction::kPop2: POP();
681 case ByteCodeInstruction::kPop : POP();
682 break;
683
Brian Osman07c117b2019-05-23 12:51:06 -0700684 case ByteCodeInstruction::kPopN:
685 sp -= READ8();
686 break;
687
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400688 case ByteCodeInstruction::kPushImmediate:
Brian Osman569f12f2019-06-13 11:23:57 -0400689 PUSH(U32(READ32()));
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400690 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400691
Brian Osman569f12f2019-06-13 11:23:57 -0400692 case ByteCodeInstruction::kReadExternal:
693 case ByteCodeInstruction::kReadExternal2:
694 case ByteCodeInstruction::kReadExternal3:
Mike Kleine7007382019-05-21 08:36:32 -0500695 case ByteCodeInstruction::kReadExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -0400696 // TODO: Support striped external values, or passing lane index? This model is odd.
697 int count = (int)inst - (int)ByteCodeInstruction::kReadExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500698 int src = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400699 int32_t tmp[4];
700 I32 m = mask();
701 for (int i = 0; i < VecWidth; ++i) {
702 if (m[i]) {
703 byteCode->fExternalValues[src]->read(tmp);
704 for (int j = 0; j < count; ++j) {
705 sp[j + 1].fSigned[i] = tmp[j];
706 }
707 }
708 }
709 sp += count;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400710 break;
Mike Kleine7007382019-05-21 08:36:32 -0500711 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400712
Brian Osman569f12f2019-06-13 11:23:57 -0400713 VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
714 VECTOR_BINARY_FN(kRemainderS, fSigned, vec_mod<I32>)
715 VECTOR_BINARY_FN(kRemainderU, fUnsigned, vec_mod<U32>)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400716
Brian Osmand3494ed2019-06-20 15:41:34 -0400717 case ByteCodeInstruction::kReserve:
718 sp += READ8();
719 break;
720
Mike Kleine7007382019-05-21 08:36:32 -0500721 case ByteCodeInstruction::kReturn: {
722 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400723 if (frames.empty()) {
724 if (outReturn) {
Brian Osman569f12f2019-06-13 11:23:57 -0400725 // TODO: This can be smarter, knowing that mask is left-justified
726 I32 m = mask();
727 VValue* src = sp - count + 1;
Mike Reed3fd3cc92019-06-20 12:40:30 -0400728 if (stripedOutput) {
729 for (int i = 0; i < count; ++i) {
730 memcpy(outReturn[i], &src->fFloat, VecWidth * sizeof(float));
731 src += 1;
Brian Osman569f12f2019-06-13 11:23:57 -0400732 }
Mike Reed3fd3cc92019-06-20 12:40:30 -0400733 } else {
734 float* outPtr = outReturn[0];
735 for (int i = 0; i < count; ++i) {
736 for (int j = 0; j < VecWidth; ++j) {
737 if (m[j]) {
738 outPtr[count * j] = src->fFloat[j];
739 }
740 }
741 ++outPtr;
742 ++src;
743 }
Brian Osman569f12f2019-06-13 11:23:57 -0400744 }
Brian Osman226668a2019-05-14 16:47:30 -0400745 }
746 return;
747 } else {
Brian Osmand3494ed2019-06-20 15:41:34 -0400748 // When we were called, the caller reserved stack space for their copy of our
749 // return value, then 'stack' was positioned after that, where our parameters
750 // were placed. Copy our return values to their reserved area.
751 memcpy(stack - count, sp - count + 1, count * sizeof(VValue));
Brian Osman226668a2019-05-14 16:47:30 -0400752
Brian Osmand3494ed2019-06-20 15:41:34 -0400753 // Now move the stack pointer to the end of the passed-in parameters. This odd
754 // calling convention requires the caller to pop the arguments after calling,
755 // but allows them to store any out-parameters back during that unwinding.
756 // After that sequence finishes, the return value will be the top of the stack.
Brian Osman226668a2019-05-14 16:47:30 -0400757 const StackFrame& frame(frames.back());
Brian Osmand3494ed2019-06-20 15:41:34 -0400758 sp = stack + frame.fParameterCount - 1;
Brian Osman226668a2019-05-14 16:47:30 -0400759 stack = frame.fStack;
760 code = frame.fCode;
761 ip = frame.fIP;
762 frames.pop_back();
763 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400764 }
Mike Kleine7007382019-05-21 08:36:32 -0500765 }
766
Brian Osman29e013d2019-05-28 17:16:03 -0400767 case ByteCodeInstruction::kScalarToMatrix: {
768 int cols = READ8();
769 int rows = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400770 VValue v = POP();
Brian Osman29e013d2019-05-28 17:16:03 -0400771 for (int c = 0; c < cols; ++c) {
772 for (int r = 0; r < rows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400773 PUSH(c == r ? v : F32(0.0f));
Brian Osman29e013d2019-05-28 17:16:03 -0400774 }
775 }
776 break;
777 }
778
Brian Osman569f12f2019-06-13 11:23:57 -0400779 VECTOR_UNARY_FN_VEC(kSin, sinf)
780 VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400781
Brian Osman569f12f2019-06-13 11:23:57 -0400782 case ByteCodeInstruction::kStore4:
783 stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat);
784 case ByteCodeInstruction::kStore3:
785 stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat);
786 case ByteCodeInstruction::kStore2:
787 stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat);
788 case ByteCodeInstruction::kStore :
789 stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat);
790 ++ip;
791 break;
Mike Kleine7007382019-05-21 08:36:32 -0500792
Brian Osman569f12f2019-06-13 11:23:57 -0400793 case ByteCodeInstruction::kStoreGlobal4:
794 globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat);
795 case ByteCodeInstruction::kStoreGlobal3:
796 globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat);
797 case ByteCodeInstruction::kStoreGlobal2:
798 globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat);
799 case ByteCodeInstruction::kStoreGlobal :
800 globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat);
801 ++ip;
802 break;
Mike Kleine7007382019-05-21 08:36:32 -0500803
Brian Osman07c117b2019-05-23 12:51:06 -0700804 case ByteCodeInstruction::kStoreExtended: {
805 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400806 I32 target = POP().fSigned;
807 VValue* src = sp - count + 1;
808 I32 m = mask();
809 for (int i = 0; i < count; ++i) {
810 for (int j = 0; j < VecWidth; ++j) {
811 if (m[j]) {
812 stack[target[j] + i].fSigned[j] = src[i].fSigned[j];
813 }
814 }
815 }
Brian Osman07c117b2019-05-23 12:51:06 -0700816 sp -= count;
817 break;
818 }
819 case ByteCodeInstruction::kStoreExtendedGlobal: {
820 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400821 I32 target = POP().fSigned;
822 VValue* src = sp - count + 1;
823 I32 m = mask();
824 for (int i = 0; i < count; ++i) {
825 for (int j = 0; j < VecWidth; ++j) {
826 if (m[j]) {
827 globals[target[j] + i].fSigned[j] = src[i].fSigned[j];
828 }
829 }
830 }
Brian Osman07c117b2019-05-23 12:51:06 -0700831 sp -= count;
832 break;
833 }
834
Mike Kleine7007382019-05-21 08:36:32 -0500835 case ByteCodeInstruction::kStoreSwizzle: {
836 int target = READ8();
837 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400838 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400839 stack[target + *(ip + i)] = skvx::if_then_else(
840 mask(), POP().fFloat, stack[target + *(ip + i)].fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400841 }
Brian Osman1091f022019-05-16 09:42:16 -0400842 ip += count;
843 break;
Mike Kleine7007382019-05-21 08:36:32 -0500844 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400845
Mike Kleine7007382019-05-21 08:36:32 -0500846 case ByteCodeInstruction::kStoreSwizzleGlobal: {
847 int target = READ8();
848 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400849 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400850 globals[target + *(ip + i)] = skvx::if_then_else(
851 mask(), POP().fFloat, globals[target + *(ip + i)].fFloat);
Brian Osman1091f022019-05-16 09:42:16 -0400852 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400853 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400854 break;
Mike Kleine7007382019-05-21 08:36:32 -0500855 }
Brian Osman569f12f2019-06-13 11:23:57 -0400856
Brian Osman07c117b2019-05-23 12:51:06 -0700857 case ByteCodeInstruction::kStoreSwizzleIndirect: {
Brian Osman07c117b2019-05-23 12:51:06 -0700858 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400859 I32 target = POP().fSigned;
860 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700861 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400862 I32 v = POP().fSigned;
863 for (int j = 0; j < VecWidth; ++j) {
864 if (m[j]) {
865 stack[target[j] + *(ip + i)].fSigned[j] = v[j];
866 }
867 }
Brian Osman07c117b2019-05-23 12:51:06 -0700868 }
869 ip += count;
870 break;
871 }
Brian Osman569f12f2019-06-13 11:23:57 -0400872
Brian Osman07c117b2019-05-23 12:51:06 -0700873 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
Brian Osman07c117b2019-05-23 12:51:06 -0700874 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400875 I32 target = POP().fSigned;
876 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700877 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400878 I32 v = POP().fSigned;
879 for (int j = 0; j < VecWidth; ++j) {
880 if (m[j]) {
881 globals[target[j] + *(ip + i)].fSigned[j] = v[j];
882 }
883 }
Brian Osman07c117b2019-05-23 12:51:06 -0700884 }
885 ip += count;
886 break;
887 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400888
Mike Kleinc1999982019-05-21 13:03:49 -0500889 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
Brian Osman1e855b22019-05-29 15:21:52 -0400890 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400891
Mike Kleine7007382019-05-21 08:36:32 -0500892 case ByteCodeInstruction::kSwizzle: {
Brian Osman569f12f2019-06-13 11:23:57 -0400893 VValue tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400894 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400895 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400896 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400897 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400898 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400899 }
900 break;
Mike Kleine7007382019-05-21 08:36:32 -0500901 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400902
Brian Osman569f12f2019-06-13 11:23:57 -0400903 VECTOR_UNARY_FN_VEC(kTan, tanf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400904
Brian Osman569f12f2019-06-13 11:23:57 -0400905 case ByteCodeInstruction::kWriteExternal:
906 case ByteCodeInstruction::kWriteExternal2:
907 case ByteCodeInstruction::kWriteExternal3:
Mike Kleine7007382019-05-21 08:36:32 -0500908 case ByteCodeInstruction::kWriteExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -0400909 int count = (int)inst - (int)ByteCodeInstruction::kWriteExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500910 int target = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400911 int32_t tmp[4];
912 I32 m = mask();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400913 sp -= count;
Brian Osman569f12f2019-06-13 11:23:57 -0400914 for (int i = 0; i < VecWidth; ++i) {
915 if (m[i]) {
916 for (int j = 0; j < count; ++j) {
917 tmp[j] = sp[j + 1].fSigned[i];
918 }
919 byteCode->fExternalValues[target]->write(tmp);
920 }
921 }
922 break;
923 }
924
925 case ByteCodeInstruction::kMaskPush:
926 condPtr[1] = POP().fSigned;
927 maskPtr[1] = maskPtr[0] & condPtr[1];
928 ++condPtr; ++maskPtr;
929 break;
930 case ByteCodeInstruction::kMaskPop:
931 --condPtr; --maskPtr;
932 break;
933 case ByteCodeInstruction::kMaskNegate:
934 maskPtr[0] = maskPtr[-1] & ~condPtr[0];
935 break;
936 case ByteCodeInstruction::kMaskBlend: {
937 int count = READ8();
938 I32 m = condPtr[0];
939 --condPtr; --maskPtr;
940 for (int i = 0; i < count; ++i) {
941 sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat);
942 --sp;
943 }
944 break;
945 }
946 case ByteCodeInstruction::kBranchIfAllFalse: {
947 int target = READ16();
948 if (!skvx::any(mask())) {
949 ip = code + target;
950 }
951 break;
952 }
953
954 case ByteCodeInstruction::kLoopBegin:
Brian Osman8d564572019-06-19 11:00:28 -0400955 contPtr[1] = 0;
956 loopPtr[1] = loopPtr[0];
957 ++contPtr; ++loopPtr;
Brian Osman569f12f2019-06-13 11:23:57 -0400958 break;
959 case ByteCodeInstruction::kLoopNext:
960 *loopPtr |= *contPtr;
961 *contPtr = 0;
962 break;
963 case ByteCodeInstruction::kLoopMask:
964 *loopPtr &= POP().fSigned;
965 break;
966 case ByteCodeInstruction::kLoopEnd:
967 --contPtr; --loopPtr;
968 break;
969 case ByteCodeInstruction::kLoopBreak:
970 *loopPtr &= ~mask();
971 break;
972 case ByteCodeInstruction::kLoopContinue: {
973 I32 m = mask();
974 *contPtr |= m;
975 *loopPtr &= ~m;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400976 break;
Mike Kleine7007382019-05-21 08:36:32 -0500977 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400978
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400979 default:
Mike Kleine7007382019-05-21 08:36:32 -0500980 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400981 }
Brian Osman569f12f2019-06-13 11:23:57 -0400982 }
983}
984
Brian Osman08a84962019-06-14 10:17:16 -0400985} // namespace Interpreter
986
987void ByteCodeFunction::disassemble() const {
988 const uint8_t* ip = fCode.data();
989 while (ip < fCode.data() + fCode.size()) {
990 printf("%d: ", (int)(ip - fCode.data()));
991 ip = Interpreter::disassemble_instruction(ip);
992 printf("\n");
993 }
994}
995
996void ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
997 const float* uniforms, int uniformCount) const {
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400998#ifdef TRACE
Brian Osman08a84962019-06-14 10:17:16 -0400999 f->disassemble();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001000#endif
Brian Osman08a84962019-06-14 10:17:16 -04001001 Interpreter::VValue smallStack[128];
Brian Osman569f12f2019-06-13 11:23:57 -04001002
Brian Osmanef787f72019-06-13 13:07:12 -04001003 // Needs to be the first N non-negative integers, at least as large as VecWidth
Brian Osman08a84962019-06-14 10:17:16 -04001004 static const Interpreter::I32 gLanes = {
1005 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1006 };
Brian Osmanef787f72019-06-13 13:07:12 -04001007
Brian Osman08a84962019-06-14 10:17:16 -04001008 SkASSERT(uniformCount == (int)fInputSlots.size());
1009 Interpreter::VValue globals[32];
1010 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
1011 for (uint8_t slot : fInputSlots) {
1012 globals[slot].fFloat = *uniforms++;
Brian Osman569f12f2019-06-13 11:23:57 -04001013 }
1014
1015 while (N) {
Brian Osman08a84962019-06-14 10:17:16 -04001016 Interpreter::VValue* stack = smallStack;
Brian Osman569f12f2019-06-13 11:23:57 -04001017
Brian Osman08a84962019-06-14 10:17:16 -04001018 int w = std::min(N, Interpreter::VecWidth);
Brian Osman569f12f2019-06-13 11:23:57 -04001019 N -= w;
1020
1021 // Transpose args into stack
1022 {
Brian Osman08a84962019-06-14 10:17:16 -04001023 float* src = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001024 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001025 float* dst = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001026 for (int j = f->fParameterCount; j > 0; --j) {
1027 *dst = *src++;
Brian Osman08a84962019-06-14 10:17:16 -04001028 dst += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001029 }
1030 }
1031 }
1032
Mike Reed3fd3cc92019-06-20 12:40:30 -04001033 bool stripedOutput = false;
1034 float** outArray = outReturn ? &outReturn : nullptr;
Brian Osman569f12f2019-06-13 11:23:57 -04001035 auto mask = w > gLanes;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001036 innerRun(this, f, stack, outArray, mask, globals, stripedOutput);
Brian Osman569f12f2019-06-13 11:23:57 -04001037
1038 // Transpose out parameters back
1039 {
Brian Osman08a84962019-06-14 10:17:16 -04001040 float* dst = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001041 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001042 float* src = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001043 for (const auto& p : f->fParameters) {
1044 if (p.fIsOutParameter) {
1045 for (int j = p.fSlotCount; j > 0; --j) {
1046 *dst++ = *src;
Brian Osman08a84962019-06-14 10:17:16 -04001047 src += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001048 }
1049 } else {
1050 dst += p.fSlotCount;
Brian Osman08a84962019-06-14 10:17:16 -04001051 src += p.fSlotCount * Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001052 }
1053 }
1054 }
1055 }
1056
1057 args += f->fParameterCount * w;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001058 if (outReturn) {
1059 outReturn += f->fReturnCount * w;
1060 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001061 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001062}
1063
Brian Osman2b1a5442019-06-19 11:40:33 -04001064void ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
Mike Reed3fd3cc92019-06-20 12:40:30 -04001065 const float* uniforms, int uniformCount,
1066 float* outArgs[], int outCount) const {
Brian Osman2b1a5442019-06-19 11:40:33 -04001067#ifdef TRACE
Brian Osmanecb3bb52019-06-20 14:59:12 -04001068 f->disassemble();
Brian Osman2b1a5442019-06-19 11:40:33 -04001069#endif
1070 Interpreter::VValue stack[128];
1071
1072 // Needs to be the first N non-negative integers, at least as large as VecWidth
1073 static const Interpreter::I32 gLanes = {
1074 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1075 };
1076
Mike Reed3fd3cc92019-06-20 12:40:30 -04001077 // innerRun just takes outArgs, so clear it if the count is zero
1078 if (outCount == 0) {
1079 outArgs = nullptr;
1080 }
1081
Brian Osman2b1a5442019-06-19 11:40:33 -04001082 SkASSERT(nargs == f->fParameterCount);
Mike Reed3fd3cc92019-06-20 12:40:30 -04001083 SkASSERT(outCount == f->fReturnCount);
Brian Osman2b1a5442019-06-19 11:40:33 -04001084 SkASSERT(uniformCount == (int)fInputSlots.size());
1085 Interpreter::VValue globals[32];
1086 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
1087 for (uint8_t slot : fInputSlots) {
1088 globals[slot].fFloat = *uniforms++;
1089 }
1090
1091 while (N) {
1092 int w = std::min(N, Interpreter::VecWidth);
1093
1094 // Copy args into stack
1095 for (int i = 0; i < nargs; ++i) {
1096 memcpy(stack + i, args[i], w * sizeof(float));
1097 }
1098
Mike Reed3fd3cc92019-06-20 12:40:30 -04001099 bool stripedOutput = true;
Brian Osman2b1a5442019-06-19 11:40:33 -04001100 auto mask = w > gLanes;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001101 innerRun(this, f, stack, outArgs, mask, globals, stripedOutput);
Brian Osman2b1a5442019-06-19 11:40:33 -04001102
1103 // Copy out parameters back
1104 int slot = 0;
1105 for (const auto& p : f->fParameters) {
1106 if (p.fIsOutParameter) {
1107 for (int i = slot; i < slot + p.fSlotCount; ++i) {
1108 memcpy(args[i], stack + i, w * sizeof(float));
1109 }
1110 }
1111 slot += p.fSlotCount;
1112 }
1113
1114 // Step each argument pointer ahead
1115 for (int i = 0; i < nargs; ++i) {
1116 args[i] += w;
1117 }
1118 N -= w;
1119 }
1120}
1121
Brian Osman80164412019-06-07 13:00:23 -04001122} // namespace SkSL
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001123
1124#endif