blob: e748d3d7492e505df89d9da23a550e0ed5fef6c0 [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")
149 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400150 case ByteCodeInstruction::kScalarToMatrix: {
151 int cols = READ8();
152 int rows = READ8();
153 printf("scalartomatrix %dx%d", cols, rows);
154 break;
155 }
Brian Osman3e833e12019-05-23 13:23:24 -0700156 VECTOR_DISASSEMBLE(kSin, "sin")
157 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
158 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
159 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
160 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
161 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
162 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
163 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
164 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
165 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
166 case ByteCodeInstruction::kStoreSwizzle: {
167 int target = READ8();
168 int count = READ8();
169 printf("storeswizzle %d %d", target, count);
170 for (int i = 0; i < count; ++i) {
171 printf(", %d", READ8());
172 }
173 break;
174 }
175 case ByteCodeInstruction::kStoreSwizzleGlobal: {
176 int target = READ8();
177 int count = READ8();
178 printf("storeswizzleglobal %d %d", target, count);
179 for (int i = 0; i < count; ++i) {
180 printf(", %d", READ8());
181 }
182 break;
183 }
184 case ByteCodeInstruction::kStoreSwizzleIndirect: {
185 int count = READ8();
186 printf("storeswizzleindirect %d", count);
187 for (int i = 0; i < count; ++i) {
188 printf(", %d", READ8());
189 }
190 break;
191 }
192 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
193 int count = READ8();
194 printf("storeswizzleindirectglobal %d", count);
195 for (int i = 0; i < count; ++i) {
196 printf(", %d", READ8());
197 }
198 break;
199 }
200 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
201 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
202 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400203 VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf")
Brian Osman3e833e12019-05-23 13:23:24 -0700204 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
205 case ByteCodeInstruction::kSwizzle: {
206 printf("swizzle %d, ", READ8());
207 int count = READ8();
208 printf("%d", count);
209 for (int i = 0; i < count; ++i) {
210 printf(", %d", READ8());
211 }
212 break;
213 }
214 VECTOR_DISASSEMBLE(kTan, "tan")
215 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
216 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
217 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
218 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
Brian Osman569f12f2019-06-13 11:23:57 -0400219 case ByteCodeInstruction::kXorB: printf("xorb"); break;
220 case ByteCodeInstruction::kMaskPush: printf("maskpush"); break;
221 case ByteCodeInstruction::kMaskPop: printf("maskpop"); break;
222 case ByteCodeInstruction::kMaskNegate: printf("masknegate"); break;
223 case ByteCodeInstruction::kMaskBlend: printf("maskblend %d", READ8()); break;
224 case ByteCodeInstruction::kBranchIfAllFalse:
225 printf("branchifallfalse %d", READ16());
226 break;
227 case ByteCodeInstruction::kLoopBegin: printf("loopbegin"); break;
228 case ByteCodeInstruction::kLoopNext: printf("loopnext"); break;
229 case ByteCodeInstruction::kLoopMask: printf("loopmask"); break;
230 case ByteCodeInstruction::kLoopEnd: printf("loopend"); break;
231 case ByteCodeInstruction::kLoopContinue: printf("loopcontinue"); break;
232 case ByteCodeInstruction::kLoopBreak: printf("loopbreak"); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700233 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
234 }
235 return ip;
236}
237
Mike Klein459aed12019-05-21 15:46:36 -0500238#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500239 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500240 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500241 POP(); \
242 /* fall through */ \
243 case ByteCodeInstruction::base ## 3: { \
244 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500245 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500246 POP(); \
247 } /* fall through */ \
248 case ByteCodeInstruction::base ## 2: { \
249 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500250 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500251 POP(); \
252 } /* fall through */ \
253 case ByteCodeInstruction::base: { \
254 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500255 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500256 POP(); \
257 break; \
258 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400259
Brian Osman1e855b22019-05-29 15:21:52 -0400260#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
261 VECTOR_BINARY_OP(base, field, op) \
262 case ByteCodeInstruction::base ## N: { \
263 int count = READ8(); \
264 for (int i = count; i > 0; --i) { \
265 sp[-count] = sp[-count].field op sp[0].field; \
266 POP(); \
267 } \
268 break; \
269 }
270
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400271#define VECTOR_BINARY_FN(base, field, fn) \
272 case ByteCodeInstruction::base ## 4: \
273 sp[-4] = fn(sp[-4].field, sp[0].field); \
274 POP(); \
275 /* fall through */ \
276 case ByteCodeInstruction::base ## 3: { \
277 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
278 sp[target] = fn(sp[target].field, sp[0].field); \
279 POP(); \
280 } /* fall through */ \
281 case ByteCodeInstruction::base ## 2: { \
282 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
283 sp[target] = fn(sp[target].field, sp[0].field); \
284 POP(); \
285 } /* fall through */ \
286 case ByteCodeInstruction::base: { \
287 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
288 sp[target] = fn(sp[target].field, sp[0].field); \
289 POP(); \
290 break; \
Mike Kleine7007382019-05-21 08:36:32 -0500291 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400292
Mike Klein459aed12019-05-21 15:46:36 -0500293#define VECTOR_UNARY_FN(base, fn, field) \
294 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
295 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
296 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
297 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400298 break;
299
Brian Osman569f12f2019-06-13 11:23:57 -0400300#define VECTOR_UNARY_FN_VEC(base, fn) \
301 case ByteCodeInstruction::base ## 4: \
302 case ByteCodeInstruction::base ## 3: \
303 case ByteCodeInstruction::base ## 2: \
304 case ByteCodeInstruction::base : { \
305 int count = (int)inst - (int)ByteCodeInstruction::base + 1; \
306 float* v = (float*)sp - count + 1; \
307 for (int i = VecWidth * count; i > 0; --i, ++v) { \
308 *v = fn(*v); \
309 } \
310 break; \
311 }
312
313union VValue {
314 VValue() {}
315
316 VValue(F32 f)
317 : fFloat(f) {
318 }
319
320 VValue(I32 s)
321 : fSigned(s) {
322 }
323
324 VValue(U32 u)
325 : fUnsigned(u) {
326 }
327
328 F32 fFloat;
329 I32 fSigned;
330 U32 fUnsigned;
331};
332
Brian Osman226668a2019-05-14 16:47:30 -0400333struct StackFrame {
334 const uint8_t* fCode;
335 const uint8_t* fIP;
Brian Osman569f12f2019-06-13 11:23:57 -0400336 VValue* fStack;
Brian Osman226668a2019-05-14 16:47:30 -0400337};
338
Brian Osman569f12f2019-06-13 11:23:57 -0400339static F32 mix(F32 start, F32 end, F32 t) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400340 return start * (1 - t) + end * t;
341}
342
Brian Osman569f12f2019-06-13 11:23:57 -0400343// TODO: trunc on integers?
344template <typename T>
345static T vec_mod(T a, T b) {
346 return a - skvx::trunc(a / b) * b;
347}
Mike Kleine7007382019-05-21 08:36:32 -0500348
Brian Osman08a84962019-06-14 10:17:16 -0400349void innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
Mike Reed3fd3cc92019-06-20 12:40:30 -0400350 float* outReturn[], I32 initMask, VValue globals[], bool stripedOutput) {
Brian Osman569f12f2019-06-13 11:23:57 -0400351 VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1;
352
353 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
354 auto PUSH = [&](VValue v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
Mike Kleine7007382019-05-21 08:36:32 -0500355
Brian Osman80164412019-06-07 13:00:23 -0400356 const uint8_t* code = f->fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400357 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400358 std::vector<StackFrame> frames;
359
Brian Osman569f12f2019-06-13 11:23:57 -0400360 I32 condStack[16]; // Independent condition masks
361 I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...)
362 I32 contStack[16]; // Continue flags for loops
363 I32 loopStack[16]; // Loop execution masks
364 condStack[0] = maskStack[0] = initMask;
365 contStack[0] = I32( 0);
366 loopStack[0] = I32(~0);
367 I32* condPtr = condStack;
368 I32* maskPtr = maskStack;
369 I32* contPtr = contStack;
370 I32* loopPtr = loopStack;
371
372 auto mask = [&]() { return *maskPtr & *loopPtr; };
373
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400374 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400375#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700376 printf("at %3d ", (int) (ip - code));
377 disassemble_instruction(ip);
378 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400379#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700380 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400381 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500382 VECTOR_BINARY_OP(kAddI, fSigned, +)
Brian Osman1e855b22019-05-29 15:21:52 -0400383 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
Brian Osman569f12f2019-06-13 11:23:57 -0400384
385 // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want:
Brian Osman32c526b2019-06-03 16:13:52 -0400386 case ByteCodeInstruction::kAndB:
Brian Osman569f12f2019-06-13 11:23:57 -0400387 sp[-1] = sp[-1].fSigned & sp[0].fSigned;
388 POP();
389 break;
390 case ByteCodeInstruction::kNotB:
391 sp[0] = ~sp[0].fSigned;
392 break;
393 case ByteCodeInstruction::kOrB:
394 sp[-1] = sp[-1].fSigned | sp[0].fSigned;
395 POP();
396 break;
397 case ByteCodeInstruction::kXorB:
398 sp[-1] = sp[-1].fSigned ^ sp[0].fSigned;
Brian Osman32c526b2019-06-03 16:13:52 -0400399 POP();
400 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400401
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400402 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400403 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400404 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400405
Brian Osman226668a2019-05-14 16:47:30 -0400406 case ByteCodeInstruction::kCall: {
407 // Precursor code has pushed all parameters to the stack. Update our bottom of
408 // stack to point at the first parameter, and our sp to point past those parameters
409 // (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500410 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400411 const ByteCodeFunction* fun = byteCode->fFunctions[target].get();
Brian Osman569f12f2019-06-13 11:23:57 -0400412 if (skvx::any(mask())) {
413 frames.push_back({ code, ip, stack });
414 ip = code = fun->fCode.data();
415 stack = sp - fun->fParameterCount + 1;
416 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
417 } else {
418 sp -= fun->fParameterCount;
419 sp += fun->fReturnCount;
420 }
Brian Osman226668a2019-05-14 16:47:30 -0400421 break;
422 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400423
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400424 case ByteCodeInstruction::kCallExternal: {
425 int argumentCount = READ8();
426 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500427 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400428 ExternalValue* v = byteCode->fExternalValues[target];
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400429 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500430
Brian Osman08a84962019-06-14 10:17:16 -0400431 int32_t tmpArgs[4];
432 int32_t tmpReturn[4];
Brian Osman569f12f2019-06-13 11:23:57 -0400433 SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs));
434 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn));
435
436 I32 m = mask();
437 for (int i = 0; i < VecWidth; ++i) {
438 if (m[i]) {
439 for (int j = 0; j < argumentCount; ++j) {
Brian Osman08a84962019-06-14 10:17:16 -0400440 tmpArgs[j] = sp[j].fSigned[i];
Brian Osman569f12f2019-06-13 11:23:57 -0400441 }
442 v->call(tmpArgs, tmpReturn);
443 for (int j = 0; j < returnCount; ++j) {
Brian Osman08a84962019-06-14 10:17:16 -0400444 sp[j].fSigned[i] = tmpReturn[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400445 }
446 }
447 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400448 sp += returnCount - 1;
449 break;
450 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400451
Mike Kleinc1999982019-05-21 13:03:49 -0500452 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
Brian Osman1e855b22019-05-29 15:21:52 -0400453 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
Mike Kleinc1999982019-05-21 13:03:49 -0500454 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
Brian Osman1e855b22019-05-29 15:21:52 -0400455 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
Mike Kleinc1999982019-05-21 13:03:49 -0500456 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
457 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
458 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
459 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
460 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
461 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
462 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
463 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
464 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
465 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
466 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
467 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400468
Brian Osman569f12f2019-06-13 11:23:57 -0400469 case ByteCodeInstruction::kConvertFtoI4: sp[-3] = skvx::cast<int>(sp[-3].fFloat);
470 case ByteCodeInstruction::kConvertFtoI3: sp[-2] = skvx::cast<int>(sp[-2].fFloat);
471 case ByteCodeInstruction::kConvertFtoI2: sp[-1] = skvx::cast<int>(sp[-1].fFloat);
472 case ByteCodeInstruction::kConvertFtoI: sp[ 0] = skvx::cast<int>(sp[ 0].fFloat);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400473 break;
474
Brian Osman569f12f2019-06-13 11:23:57 -0400475 case ByteCodeInstruction::kConvertStoF4: sp[-3] = skvx::cast<float>(sp[-3].fSigned);
476 case ByteCodeInstruction::kConvertStoF3: sp[-2] = skvx::cast<float>(sp[-2].fSigned);
477 case ByteCodeInstruction::kConvertStoF2: sp[-1] = skvx::cast<float>(sp[-1].fSigned);
478 case ByteCodeInstruction::kConvertStoF : sp[ 0] = skvx::cast<float>(sp[ 0].fSigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400479 break;
480
Brian Osman569f12f2019-06-13 11:23:57 -0400481 case ByteCodeInstruction::kConvertUtoF4: sp[-3] = skvx::cast<float>(sp[-3].fUnsigned);
482 case ByteCodeInstruction::kConvertUtoF3: sp[-2] = skvx::cast<float>(sp[-2].fUnsigned);
483 case ByteCodeInstruction::kConvertUtoF2: sp[-1] = skvx::cast<float>(sp[-1].fUnsigned);
484 case ByteCodeInstruction::kConvertUtoF : sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400485 break;
486
Brian Osman569f12f2019-06-13 11:23:57 -0400487 VECTOR_UNARY_FN_VEC(kCos, cosf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400488
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400489 case ByteCodeInstruction::kCross: {
Brian Osman569f12f2019-06-13 11:23:57 -0400490 F32 ax = sp[-5].fFloat, ay = sp[-4].fFloat, az = sp[-3].fFloat,
491 bx = sp[-2].fFloat, by = sp[-1].fFloat, bz = sp[ 0].fFloat;
492 F32 cx = ay*bz - az*by,
493 cy = az*bx - ax*bz,
494 cz = ax*by - ay*bx;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400495 sp -= 3;
Brian Osman569f12f2019-06-13 11:23:57 -0400496 sp[-2] = cx;
497 sp[-1] = cy;
498 sp[ 0] = cz;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400499 break;
500 }
501
Mike Kleinc1999982019-05-21 13:03:49 -0500502 VECTOR_BINARY_OP(kDivideS, fSigned, /)
503 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
Brian Osman1e855b22019-05-29 15:21:52 -0400504 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500505
506 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
507 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
508 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
509 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
510 break;
511
Brian Osman07c117b2019-05-23 12:51:06 -0700512 case ByteCodeInstruction::kDupN: {
513 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400514 memcpy(sp + 1, sp - count + 1, count * sizeof(VValue));
Brian Osman07c117b2019-05-23 12:51:06 -0700515 sp += count;
516 break;
517 }
518
Mike Kleine7007382019-05-21 08:36:32 -0500519 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
520 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
521 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
522 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
523 ++ip;
524 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
525 break;
526
Brian Osman80164412019-06-07 13:00:23 -0400527 case ByteCodeInstruction::kLoadGlobal4: sp[4] = globals[*ip + 3];
528 case ByteCodeInstruction::kLoadGlobal3: sp[3] = globals[*ip + 2];
529 case ByteCodeInstruction::kLoadGlobal2: sp[2] = globals[*ip + 1];
530 case ByteCodeInstruction::kLoadGlobal : sp[1] = globals[*ip + 0];
Mike Kleine7007382019-05-21 08:36:32 -0500531 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700532 sp += (int)inst -
533 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500534 break;
535
Brian Osman07c117b2019-05-23 12:51:06 -0700536 case ByteCodeInstruction::kLoadExtended: {
537 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400538 I32 src = POP().fSigned;
539 I32 m = mask();
540 for (int i = 0; i < count; ++i) {
541 for (int j = 0; j < VecWidth; ++j) {
542 if (m[j]) {
543 sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j];
544 }
545 }
546 }
Brian Osman07c117b2019-05-23 12:51:06 -0700547 sp += count;
548 break;
549 }
550
551 case ByteCodeInstruction::kLoadExtendedGlobal: {
552 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400553 I32 src = POP().fSigned;
554 I32 m = mask();
555 for (int i = 0; i < count; ++i) {
556 for (int j = 0; j < VecWidth; ++j) {
557 if (m[j]) {
558 sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j];
559 }
560 }
561 }
Brian Osman07c117b2019-05-23 12:51:06 -0700562 sp += count;
563 break;
564 }
565
Mike Kleine7007382019-05-21 08:36:32 -0500566 case ByteCodeInstruction::kLoadSwizzle: {
567 int src = READ8();
568 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400569 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400570 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400571 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400572 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400573 break;
Mike Kleine7007382019-05-21 08:36:32 -0500574 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400575
Mike Kleine7007382019-05-21 08:36:32 -0500576 case ByteCodeInstruction::kLoadSwizzleGlobal: {
577 int src = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500578 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400579 for (int i = 0; i < count; ++i) {
Brian Osman80164412019-06-07 13:00:23 -0400580 PUSH(globals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400581 }
582 ip += count;
583 break;
Mike Kleine7007382019-05-21 08:36:32 -0500584 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400585
Brian Osman29e013d2019-05-28 17:16:03 -0400586 case ByteCodeInstruction::kMatrixToMatrix: {
587 int srcCols = READ8();
588 int srcRows = READ8();
589 int dstCols = READ8();
590 int dstRows = READ8();
591 SkASSERT(srcCols >= 2 && srcCols <= 4);
592 SkASSERT(srcRows >= 2 && srcRows <= 4);
593 SkASSERT(dstCols >= 2 && dstCols <= 4);
594 SkASSERT(dstRows >= 2 && dstRows <= 4);
Brian Osman569f12f2019-06-13 11:23:57 -0400595 F32 tmp[16];
596 memset(tmp, 0, sizeof(tmp));
597 tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f);
Brian Osman29e013d2019-05-28 17:16:03 -0400598 for (int c = srcCols - 1; c >= 0; --c) {
599 for (int r = srcRows - 1; r >= 0; --r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400600 tmp[c*4 + r] = POP().fFloat;
Brian Osman29e013d2019-05-28 17:16:03 -0400601 }
602 }
603 for (int c = 0; c < dstCols; ++c) {
604 for (int r = 0; r < dstRows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400605 PUSH(tmp[c*4 + r]);
Brian Osman29e013d2019-05-28 17:16:03 -0400606 }
607 }
608 break;
609 }
610
Brian Osman909231c2019-05-29 15:34:36 -0400611 case ByteCodeInstruction::kMatrixMultiply: {
612 int lCols = READ8();
613 int lRows = READ8();
614 int rCols = READ8();
615 int rRows = lCols;
Brian Osman569f12f2019-06-13 11:23:57 -0400616 F32 tmp[16] = { 0.0f };
617 F32* B = &(sp - (rCols * rRows) + 1)->fFloat;
618 F32* A = B - (lCols * lRows);
Brian Osman909231c2019-05-29 15:34:36 -0400619 for (int c = 0; c < rCols; ++c) {
620 for (int r = 0; r < lRows; ++r) {
621 for (int j = 0; j < lCols; ++j) {
622 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
623 }
624 }
625 }
626 sp -= (lCols * lRows) + (rCols * rRows);
Brian Osman569f12f2019-06-13 11:23:57 -0400627 memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue));
Brian Osman909231c2019-05-29 15:34:36 -0400628 sp += (rCols * lRows);
629 break;
630 }
631
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400632 // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T
633 case ByteCodeInstruction::kMix4:
634 sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat);
635 // fall through
636 case ByteCodeInstruction::kMix3: {
637 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
638 int target = 2 - count * 2;
639 sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat);
640 // fall through
641 }
642 case ByteCodeInstruction::kMix2: {
643 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
644 int target = 1 - count * 2;
645 sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat);
646 // fall through
647 }
648 case ByteCodeInstruction::kMix: {
649 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
650 int target = -count * 2;
651 sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat);
652 sp -= 1 + count;
653 break;
654 }
655
Mike Kleinc1999982019-05-21 13:03:49 -0500656 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
Brian Osman1e855b22019-05-29 15:21:52 -0400657 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400658
Mike Kleinc1999982019-05-21 13:03:49 -0500659 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
660 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
661 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
662 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500663 break;
664
Brian Osman1e855b22019-05-29 15:21:52 -0400665 case ByteCodeInstruction::kNegateFN: {
666 int count = READ8();
667 for (int i = count - 1; i >= 0; --i) {
668 sp[-i] = -sp[-i].fFloat;
669 }
670 break;
671 }
672
Mike Kleinc1999982019-05-21 13:03:49 -0500673 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
674 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
675 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
Brian Osman569f12f2019-06-13 11:23:57 -0400676 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp[ 0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500677 break;
678
679 case ByteCodeInstruction::kPop4: POP();
680 case ByteCodeInstruction::kPop3: POP();
681 case ByteCodeInstruction::kPop2: POP();
682 case ByteCodeInstruction::kPop : POP();
683 break;
684
Brian Osman07c117b2019-05-23 12:51:06 -0700685 case ByteCodeInstruction::kPopN:
686 sp -= READ8();
687 break;
688
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400689 case ByteCodeInstruction::kPushImmediate:
Brian Osman569f12f2019-06-13 11:23:57 -0400690 PUSH(U32(READ32()));
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400691 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400692
Brian Osman569f12f2019-06-13 11:23:57 -0400693 case ByteCodeInstruction::kReadExternal:
694 case ByteCodeInstruction::kReadExternal2:
695 case ByteCodeInstruction::kReadExternal3:
Mike Kleine7007382019-05-21 08:36:32 -0500696 case ByteCodeInstruction::kReadExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -0400697 // TODO: Support striped external values, or passing lane index? This model is odd.
698 int count = (int)inst - (int)ByteCodeInstruction::kReadExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500699 int src = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400700 int32_t tmp[4];
701 I32 m = mask();
702 for (int i = 0; i < VecWidth; ++i) {
703 if (m[i]) {
704 byteCode->fExternalValues[src]->read(tmp);
705 for (int j = 0; j < count; ++j) {
706 sp[j + 1].fSigned[i] = tmp[j];
707 }
708 }
709 }
710 sp += count;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400711 break;
Mike Kleine7007382019-05-21 08:36:32 -0500712 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400713
Brian Osman569f12f2019-06-13 11:23:57 -0400714 VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
715 VECTOR_BINARY_FN(kRemainderS, fSigned, vec_mod<I32>)
716 VECTOR_BINARY_FN(kRemainderU, fUnsigned, vec_mod<U32>)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400717
Mike Kleine7007382019-05-21 08:36:32 -0500718 case ByteCodeInstruction::kReturn: {
719 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400720 if (frames.empty()) {
721 if (outReturn) {
Brian Osman569f12f2019-06-13 11:23:57 -0400722 // TODO: This can be smarter, knowing that mask is left-justified
723 I32 m = mask();
724 VValue* src = sp - count + 1;
Mike Reed3fd3cc92019-06-20 12:40:30 -0400725 if (stripedOutput) {
726 for (int i = 0; i < count; ++i) {
727 memcpy(outReturn[i], &src->fFloat, VecWidth * sizeof(float));
728 src += 1;
Brian Osman569f12f2019-06-13 11:23:57 -0400729 }
Mike Reed3fd3cc92019-06-20 12:40:30 -0400730 } else {
731 float* outPtr = outReturn[0];
732 for (int i = 0; i < count; ++i) {
733 for (int j = 0; j < VecWidth; ++j) {
734 if (m[j]) {
735 outPtr[count * j] = src->fFloat[j];
736 }
737 }
738 ++outPtr;
739 ++src;
740 }
Brian Osman569f12f2019-06-13 11:23:57 -0400741 }
Brian Osman226668a2019-05-14 16:47:30 -0400742 }
743 return;
744 } else {
745 // When we were called, 'stack' was positioned at the old top-of-stack (where
746 // our parameters were placed). So copy our return values to that same spot.
Brian Osman569f12f2019-06-13 11:23:57 -0400747 memmove(stack, sp - count + 1, count * sizeof(VValue));
Brian Osman226668a2019-05-14 16:47:30 -0400748
749 // Now move the stack pointer to the end of the just-pushed return values,
750 // and restore everything else.
751 const StackFrame& frame(frames.back());
752 sp = stack + count - 1;
753 stack = frame.fStack;
754 code = frame.fCode;
755 ip = frame.fIP;
756 frames.pop_back();
757 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400758 }
Mike Kleine7007382019-05-21 08:36:32 -0500759 }
760
Brian Osman29e013d2019-05-28 17:16:03 -0400761 case ByteCodeInstruction::kScalarToMatrix: {
762 int cols = READ8();
763 int rows = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400764 VValue v = POP();
Brian Osman29e013d2019-05-28 17:16:03 -0400765 for (int c = 0; c < cols; ++c) {
766 for (int r = 0; r < rows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400767 PUSH(c == r ? v : F32(0.0f));
Brian Osman29e013d2019-05-28 17:16:03 -0400768 }
769 }
770 break;
771 }
772
Brian Osman569f12f2019-06-13 11:23:57 -0400773 VECTOR_UNARY_FN_VEC(kSin, sinf)
774 VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400775
Brian Osman569f12f2019-06-13 11:23:57 -0400776 case ByteCodeInstruction::kStore4:
777 stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat);
778 case ByteCodeInstruction::kStore3:
779 stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat);
780 case ByteCodeInstruction::kStore2:
781 stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat);
782 case ByteCodeInstruction::kStore :
783 stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat);
784 ++ip;
785 break;
Mike Kleine7007382019-05-21 08:36:32 -0500786
Brian Osman569f12f2019-06-13 11:23:57 -0400787 case ByteCodeInstruction::kStoreGlobal4:
788 globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat);
789 case ByteCodeInstruction::kStoreGlobal3:
790 globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat);
791 case ByteCodeInstruction::kStoreGlobal2:
792 globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat);
793 case ByteCodeInstruction::kStoreGlobal :
794 globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat);
795 ++ip;
796 break;
Mike Kleine7007382019-05-21 08:36:32 -0500797
Brian Osman07c117b2019-05-23 12:51:06 -0700798 case ByteCodeInstruction::kStoreExtended: {
799 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400800 I32 target = POP().fSigned;
801 VValue* src = sp - count + 1;
802 I32 m = mask();
803 for (int i = 0; i < count; ++i) {
804 for (int j = 0; j < VecWidth; ++j) {
805 if (m[j]) {
806 stack[target[j] + i].fSigned[j] = src[i].fSigned[j];
807 }
808 }
809 }
Brian Osman07c117b2019-05-23 12:51:06 -0700810 sp -= count;
811 break;
812 }
813 case ByteCodeInstruction::kStoreExtendedGlobal: {
814 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400815 I32 target = POP().fSigned;
816 VValue* src = sp - count + 1;
817 I32 m = mask();
818 for (int i = 0; i < count; ++i) {
819 for (int j = 0; j < VecWidth; ++j) {
820 if (m[j]) {
821 globals[target[j] + i].fSigned[j] = src[i].fSigned[j];
822 }
823 }
824 }
Brian Osman07c117b2019-05-23 12:51:06 -0700825 sp -= count;
826 break;
827 }
828
Mike Kleine7007382019-05-21 08:36:32 -0500829 case ByteCodeInstruction::kStoreSwizzle: {
830 int target = READ8();
831 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400832 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400833 stack[target + *(ip + i)] = skvx::if_then_else(
834 mask(), POP().fFloat, stack[target + *(ip + i)].fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400835 }
Brian Osman1091f022019-05-16 09:42:16 -0400836 ip += count;
837 break;
Mike Kleine7007382019-05-21 08:36:32 -0500838 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400839
Mike Kleine7007382019-05-21 08:36:32 -0500840 case ByteCodeInstruction::kStoreSwizzleGlobal: {
841 int target = READ8();
842 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400843 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400844 globals[target + *(ip + i)] = skvx::if_then_else(
845 mask(), POP().fFloat, globals[target + *(ip + i)].fFloat);
Brian Osman1091f022019-05-16 09:42:16 -0400846 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400847 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400848 break;
Mike Kleine7007382019-05-21 08:36:32 -0500849 }
Brian Osman569f12f2019-06-13 11:23:57 -0400850
Brian Osman07c117b2019-05-23 12:51:06 -0700851 case ByteCodeInstruction::kStoreSwizzleIndirect: {
Brian Osman07c117b2019-05-23 12:51:06 -0700852 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400853 I32 target = POP().fSigned;
854 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700855 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400856 I32 v = POP().fSigned;
857 for (int j = 0; j < VecWidth; ++j) {
858 if (m[j]) {
859 stack[target[j] + *(ip + i)].fSigned[j] = v[j];
860 }
861 }
Brian Osman07c117b2019-05-23 12:51:06 -0700862 }
863 ip += count;
864 break;
865 }
Brian Osman569f12f2019-06-13 11:23:57 -0400866
Brian Osman07c117b2019-05-23 12:51:06 -0700867 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
Brian Osman07c117b2019-05-23 12:51:06 -0700868 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400869 I32 target = POP().fSigned;
870 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700871 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400872 I32 v = POP().fSigned;
873 for (int j = 0; j < VecWidth; ++j) {
874 if (m[j]) {
875 globals[target[j] + *(ip + i)].fSigned[j] = v[j];
876 }
877 }
Brian Osman07c117b2019-05-23 12:51:06 -0700878 }
879 ip += count;
880 break;
881 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400882
Mike Kleinc1999982019-05-21 13:03:49 -0500883 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
Brian Osman1e855b22019-05-29 15:21:52 -0400884 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400885
Mike Kleine7007382019-05-21 08:36:32 -0500886 case ByteCodeInstruction::kSwizzle: {
Brian Osman569f12f2019-06-13 11:23:57 -0400887 VValue tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400888 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400889 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400890 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400891 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400892 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400893 }
894 break;
Mike Kleine7007382019-05-21 08:36:32 -0500895 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400896
Brian Osman569f12f2019-06-13 11:23:57 -0400897 VECTOR_UNARY_FN_VEC(kTan, tanf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400898
Brian Osman569f12f2019-06-13 11:23:57 -0400899 case ByteCodeInstruction::kWriteExternal:
900 case ByteCodeInstruction::kWriteExternal2:
901 case ByteCodeInstruction::kWriteExternal3:
Mike Kleine7007382019-05-21 08:36:32 -0500902 case ByteCodeInstruction::kWriteExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -0400903 int count = (int)inst - (int)ByteCodeInstruction::kWriteExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500904 int target = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400905 int32_t tmp[4];
906 I32 m = mask();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400907 sp -= count;
Brian Osman569f12f2019-06-13 11:23:57 -0400908 for (int i = 0; i < VecWidth; ++i) {
909 if (m[i]) {
910 for (int j = 0; j < count; ++j) {
911 tmp[j] = sp[j + 1].fSigned[i];
912 }
913 byteCode->fExternalValues[target]->write(tmp);
914 }
915 }
916 break;
917 }
918
919 case ByteCodeInstruction::kMaskPush:
920 condPtr[1] = POP().fSigned;
921 maskPtr[1] = maskPtr[0] & condPtr[1];
922 ++condPtr; ++maskPtr;
923 break;
924 case ByteCodeInstruction::kMaskPop:
925 --condPtr; --maskPtr;
926 break;
927 case ByteCodeInstruction::kMaskNegate:
928 maskPtr[0] = maskPtr[-1] & ~condPtr[0];
929 break;
930 case ByteCodeInstruction::kMaskBlend: {
931 int count = READ8();
932 I32 m = condPtr[0];
933 --condPtr; --maskPtr;
934 for (int i = 0; i < count; ++i) {
935 sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat);
936 --sp;
937 }
938 break;
939 }
940 case ByteCodeInstruction::kBranchIfAllFalse: {
941 int target = READ16();
942 if (!skvx::any(mask())) {
943 ip = code + target;
944 }
945 break;
946 }
947
948 case ByteCodeInstruction::kLoopBegin:
Brian Osman8d564572019-06-19 11:00:28 -0400949 contPtr[1] = 0;
950 loopPtr[1] = loopPtr[0];
951 ++contPtr; ++loopPtr;
Brian Osman569f12f2019-06-13 11:23:57 -0400952 break;
953 case ByteCodeInstruction::kLoopNext:
954 *loopPtr |= *contPtr;
955 *contPtr = 0;
956 break;
957 case ByteCodeInstruction::kLoopMask:
958 *loopPtr &= POP().fSigned;
959 break;
960 case ByteCodeInstruction::kLoopEnd:
961 --contPtr; --loopPtr;
962 break;
963 case ByteCodeInstruction::kLoopBreak:
964 *loopPtr &= ~mask();
965 break;
966 case ByteCodeInstruction::kLoopContinue: {
967 I32 m = mask();
968 *contPtr |= m;
969 *loopPtr &= ~m;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400970 break;
Mike Kleine7007382019-05-21 08:36:32 -0500971 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400972
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400973 default:
Mike Kleine7007382019-05-21 08:36:32 -0500974 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400975 }
Brian Osman569f12f2019-06-13 11:23:57 -0400976 }
977}
978
Brian Osman08a84962019-06-14 10:17:16 -0400979} // namespace Interpreter
980
981void ByteCodeFunction::disassemble() const {
982 const uint8_t* ip = fCode.data();
983 while (ip < fCode.data() + fCode.size()) {
984 printf("%d: ", (int)(ip - fCode.data()));
985 ip = Interpreter::disassemble_instruction(ip);
986 printf("\n");
987 }
988}
989
990void ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
991 const float* uniforms, int uniformCount) const {
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400992#ifdef TRACE
Brian Osman08a84962019-06-14 10:17:16 -0400993 f->disassemble();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400994#endif
Brian Osman08a84962019-06-14 10:17:16 -0400995 Interpreter::VValue smallStack[128];
Brian Osman569f12f2019-06-13 11:23:57 -0400996
Brian Osmanef787f72019-06-13 13:07:12 -0400997 // Needs to be the first N non-negative integers, at least as large as VecWidth
Brian Osman08a84962019-06-14 10:17:16 -0400998 static const Interpreter::I32 gLanes = {
999 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1000 };
Brian Osmanef787f72019-06-13 13:07:12 -04001001
Brian Osman08a84962019-06-14 10:17:16 -04001002 SkASSERT(uniformCount == (int)fInputSlots.size());
1003 Interpreter::VValue globals[32];
1004 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
1005 for (uint8_t slot : fInputSlots) {
1006 globals[slot].fFloat = *uniforms++;
Brian Osman569f12f2019-06-13 11:23:57 -04001007 }
1008
1009 while (N) {
Brian Osman08a84962019-06-14 10:17:16 -04001010 Interpreter::VValue* stack = smallStack;
Brian Osman569f12f2019-06-13 11:23:57 -04001011
Brian Osman08a84962019-06-14 10:17:16 -04001012 int w = std::min(N, Interpreter::VecWidth);
Brian Osman569f12f2019-06-13 11:23:57 -04001013 N -= w;
1014
1015 // Transpose args into stack
1016 {
Brian Osman08a84962019-06-14 10:17:16 -04001017 float* src = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001018 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001019 float* dst = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001020 for (int j = f->fParameterCount; j > 0; --j) {
1021 *dst = *src++;
Brian Osman08a84962019-06-14 10:17:16 -04001022 dst += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001023 }
1024 }
1025 }
1026
Mike Reed3fd3cc92019-06-20 12:40:30 -04001027 bool stripedOutput = false;
1028 float** outArray = outReturn ? &outReturn : nullptr;
Brian Osman569f12f2019-06-13 11:23:57 -04001029 auto mask = w > gLanes;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001030 innerRun(this, f, stack, outArray, mask, globals, stripedOutput);
Brian Osman569f12f2019-06-13 11:23:57 -04001031
1032 // Transpose out parameters back
1033 {
Brian Osman08a84962019-06-14 10:17:16 -04001034 float* dst = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001035 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001036 float* src = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001037 for (const auto& p : f->fParameters) {
1038 if (p.fIsOutParameter) {
1039 for (int j = p.fSlotCount; j > 0; --j) {
1040 *dst++ = *src;
Brian Osman08a84962019-06-14 10:17:16 -04001041 src += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001042 }
1043 } else {
1044 dst += p.fSlotCount;
Brian Osman08a84962019-06-14 10:17:16 -04001045 src += p.fSlotCount * Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001046 }
1047 }
1048 }
1049 }
1050
1051 args += f->fParameterCount * w;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001052 if (outReturn) {
1053 outReturn += f->fReturnCount * w;
1054 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001055 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001056}
1057
Brian Osman2b1a5442019-06-19 11:40:33 -04001058void ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
Mike Reed3fd3cc92019-06-20 12:40:30 -04001059 const float* uniforms, int uniformCount,
1060 float* outArgs[], int outCount) const {
Brian Osman2b1a5442019-06-19 11:40:33 -04001061#ifdef TRACE
Brian Osmanecb3bb52019-06-20 14:59:12 -04001062 f->disassemble();
Brian Osman2b1a5442019-06-19 11:40:33 -04001063#endif
1064 Interpreter::VValue stack[128];
1065
1066 // Needs to be the first N non-negative integers, at least as large as VecWidth
1067 static const Interpreter::I32 gLanes = {
1068 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
1069 };
1070
Mike Reed3fd3cc92019-06-20 12:40:30 -04001071 // innerRun just takes outArgs, so clear it if the count is zero
1072 if (outCount == 0) {
1073 outArgs = nullptr;
1074 }
1075
Brian Osman2b1a5442019-06-19 11:40:33 -04001076 SkASSERT(nargs == f->fParameterCount);
Mike Reed3fd3cc92019-06-20 12:40:30 -04001077 SkASSERT(outCount == f->fReturnCount);
Brian Osman2b1a5442019-06-19 11:40:33 -04001078 SkASSERT(uniformCount == (int)fInputSlots.size());
1079 Interpreter::VValue globals[32];
1080 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
1081 for (uint8_t slot : fInputSlots) {
1082 globals[slot].fFloat = *uniforms++;
1083 }
1084
1085 while (N) {
1086 int w = std::min(N, Interpreter::VecWidth);
1087
1088 // Copy args into stack
1089 for (int i = 0; i < nargs; ++i) {
1090 memcpy(stack + i, args[i], w * sizeof(float));
1091 }
1092
Mike Reed3fd3cc92019-06-20 12:40:30 -04001093 bool stripedOutput = true;
Brian Osman2b1a5442019-06-19 11:40:33 -04001094 auto mask = w > gLanes;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001095 innerRun(this, f, stack, outArgs, mask, globals, stripedOutput);
Brian Osman2b1a5442019-06-19 11:40:33 -04001096
1097 // Copy out parameters back
1098 int slot = 0;
1099 for (const auto& p : f->fParameters) {
1100 if (p.fIsOutParameter) {
1101 for (int i = slot; i < slot + p.fSlotCount; ++i) {
1102 memcpy(args[i], stack + i, w * sizeof(float));
1103 }
1104 }
1105 slot += p.fSlotCount;
1106 }
1107
1108 // Step each argument pointer ahead
1109 for (int i = 0; i < nargs; ++i) {
1110 args[i] += w;
1111 }
1112 N -= w;
1113 }
1114}
1115
Brian Osman80164412019-06-07 13:00:23 -04001116} // namespace SkSL
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001117
1118#endif