blob: 3f46c8ea0f71c738408d482f8a415d69e7f2e8af [file] [log] [blame]
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SKSL_STANDALONE
9
Ethan Nicholasae9633b2019-05-24 12:46:34 -040010#include "include/core/SkPoint3.h"
Brian Osman569f12f2019-06-13 11:23:57 -040011#include "include/private/SkVx.h"
Mike Klein7a177b42019-06-17 17:17:47 -050012#include "src/core/SkUtils.h" // sk_unaligned_load
Brian Osman80164412019-06-07 13:00:23 -040013#include "src/sksl/SkSLByteCode.h"
Brian Osman07c117b2019-05-23 12:51:06 -070014#include "src/sksl/SkSLByteCodeGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040015#include "src/sksl/SkSLExternalValue.h"
Brian Osman80164412019-06-07 13:00:23 -040016
17#include <vector>
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040018
19namespace SkSL {
Brian Osman489cf882019-07-09 10:48:28 -040020
21#if defined(SK_ENABLE_SKSL_INTERPRETER)
22
Brian Osman80164412019-06-07 13:00:23 -040023namespace Interpreter {
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040024
Mike Reed3fd3cc92019-06-20 12:40:30 -040025constexpr int VecWidth = ByteCode::kVecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -040026
27using F32 = skvx::Vec<VecWidth, float>;
28using I32 = skvx::Vec<VecWidth, int32_t>;
29using U32 = skvx::Vec<VecWidth, uint32_t>;
30
Mike Kleine7007382019-05-21 08:36:32 -050031#define READ8() (*(ip++))
Mike Klein7a177b42019-06-17 17:17:47 -050032#define READ16() (ip += 2, sk_unaligned_load<uint16_t>(ip - 2))
33#define READ32() (ip += 4, sk_unaligned_load<uint32_t>(ip - 4))
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040034
Brian Osman3e833e12019-05-23 13:23:24 -070035#define VECTOR_DISASSEMBLE(op, text) \
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040036 case ByteCodeInstruction::op: printf(text); break; \
37 case ByteCodeInstruction::op##2: printf(text "2"); break; \
38 case ByteCodeInstruction::op##3: printf(text "3"); break; \
39 case ByteCodeInstruction::op##4: printf(text "4"); break;
40
Brian Osman1e855b22019-05-29 15:21:52 -040041#define VECTOR_MATRIX_DISASSEMBLE(op, text) \
42 case ByteCodeInstruction::op: printf(text); break; \
43 case ByteCodeInstruction::op##2: printf(text "2"); break; \
44 case ByteCodeInstruction::op##3: printf(text "3"); break; \
45 case ByteCodeInstruction::op##4: printf(text "4"); break; \
46 case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
47
Brian Osman3e833e12019-05-23 13:23:24 -070048static const uint8_t* disassemble_instruction(const uint8_t* ip) {
49 switch ((ByteCodeInstruction) READ16()) {
Brian Osman1e855b22019-05-29 15:21:52 -040050 VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf")
Brian Osman3e833e12019-05-23 13:23:24 -070051 VECTOR_DISASSEMBLE(kAddI, "addi")
Brian Osman32c526b2019-06-03 16:13:52 -040052 case ByteCodeInstruction::kAndB: printf("andb"); break;
Brian Osman3e833e12019-05-23 13:23:24 -070053 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
54 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
55 case ByteCodeInstruction::kCallExternal: {
56 int argumentCount = READ8();
57 int returnCount = READ8();
58 int externalValue = READ8();
59 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
60 break;
61 }
62 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
63 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
Brian Osman1e855b22019-05-29 15:21:52 -040064 VECTOR_MATRIX_DISASSEMBLE(kCompareFEQ, "comparefeq")
65 VECTOR_MATRIX_DISASSEMBLE(kCompareFNEQ, "comparefneq")
Brian Osman3e833e12019-05-23 13:23:24 -070066 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
67 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
68 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
69 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
70 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
71 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
72 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
73 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
74 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
75 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
76 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
77 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
Brian Osman3e833e12019-05-23 13:23:24 -070078 VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi")
79 VECTOR_DISASSEMBLE(kConvertStoF, "convertstof")
80 VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof")
81 VECTOR_DISASSEMBLE(kCos, "cos")
Brian Osmanecb3bb52019-06-20 14:59:12 -040082 case ByteCodeInstruction::kCross: printf("cross"); break;
Brian Osman1e855b22019-05-29 15:21:52 -040083 VECTOR_MATRIX_DISASSEMBLE(kDivideF, "dividef")
Brian Osman3e833e12019-05-23 13:23:24 -070084 VECTOR_DISASSEMBLE(kDivideS, "divideS")
85 VECTOR_DISASSEMBLE(kDivideU, "divideu")
Brian Osman1e855b22019-05-29 15:21:52 -040086 VECTOR_MATRIX_DISASSEMBLE(kDup, "dup")
Mike Reed634c9412019-07-18 13:20:04 -040087 case ByteCodeInstruction::kInverse2x2: printf("inverse2x2"); break;
88 case ByteCodeInstruction::kInverse3x3: printf("inverse3x3"); break;
89 case ByteCodeInstruction::kInverse4x4: printf("inverse4x4"); break;
Brian Osman3e833e12019-05-23 13:23:24 -070090 case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break;
91 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break;
92 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break;
93 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break;
94 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break;
95 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break;
96 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break;
97 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break;
98 case ByteCodeInstruction::kLoadSwizzle: {
99 int target = READ8();
100 int count = READ8();
101 printf("loadswizzle %d %d", target, count);
102 for (int i = 0; i < count; ++i) {
103 printf(", %d", READ8());
104 }
105 break;
106 }
107 case ByteCodeInstruction::kLoadSwizzleGlobal: {
108 int target = READ8();
109 int count = READ8();
110 printf("loadswizzleglobal %d %d", target, count);
111 for (int i = 0; i < count; ++i) {
112 printf(", %d", READ8());
113 }
114 break;
115 }
116 case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break;
117 case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8());
118 break;
Brian Osman29e013d2019-05-28 17:16:03 -0400119 case ByteCodeInstruction::kMatrixToMatrix: {
120 int srcCols = READ8();
121 int srcRows = READ8();
122 int dstCols = READ8();
123 int dstRows = READ8();
124 printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows);
125 break;
126 }
Brian Osman909231c2019-05-29 15:34:36 -0400127 case ByteCodeInstruction::kMatrixMultiply: {
128 int lCols = READ8();
129 int lRows = READ8();
130 int rCols = READ8();
131 printf("matrixmultiply %dx%d %dx%d", lCols, lRows, rCols, lCols);
132 break;
133 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400134 VECTOR_DISASSEMBLE(kMix, "mix")
Brian Osman1e855b22019-05-29 15:21:52 -0400135 VECTOR_MATRIX_DISASSEMBLE(kMultiplyF, "multiplyf")
Brian Osman3e833e12019-05-23 13:23:24 -0700136 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
Brian Osman1e855b22019-05-29 15:21:52 -0400137 VECTOR_MATRIX_DISASSEMBLE(kNegateF, "negatef")
Brian Osman3e833e12019-05-23 13:23:24 -0700138 VECTOR_DISASSEMBLE(kNegateI, "negatei")
Brian Osman569f12f2019-06-13 11:23:57 -0400139 case ByteCodeInstruction::kNotB: printf("notb"); break;
Brian Osman32c526b2019-06-03 16:13:52 -0400140 case ByteCodeInstruction::kOrB: printf("orb"); break;
Brian Osman1e855b22019-05-29 15:21:52 -0400141 VECTOR_MATRIX_DISASSEMBLE(kPop, "pop")
Brian Osman3e833e12019-05-23 13:23:24 -0700142 case ByteCodeInstruction::kPushImmediate: {
143 uint32_t v = READ32();
144 union { uint32_t u; float f; } pun = { v };
145 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
146 break;
147 }
148 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
149 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
150 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
151 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
152 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
153 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
154 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
Brian Osmand3494ed2019-06-20 15:41:34 -0400155 case ByteCodeInstruction::kReserve: printf("reserve %d", READ8()); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700156 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400157 case ByteCodeInstruction::kScalarToMatrix: {
158 int cols = READ8();
159 int rows = READ8();
160 printf("scalartomatrix %dx%d", cols, rows);
161 break;
162 }
Brian Osman3e833e12019-05-23 13:23:24 -0700163 VECTOR_DISASSEMBLE(kSin, "sin")
164 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
165 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
166 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
167 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
168 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
169 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
170 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
171 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
172 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
173 case ByteCodeInstruction::kStoreSwizzle: {
174 int target = READ8();
175 int count = READ8();
176 printf("storeswizzle %d %d", target, count);
177 for (int i = 0; i < count; ++i) {
178 printf(", %d", READ8());
179 }
180 break;
181 }
182 case ByteCodeInstruction::kStoreSwizzleGlobal: {
183 int target = READ8();
184 int count = READ8();
185 printf("storeswizzleglobal %d %d", target, count);
186 for (int i = 0; i < count; ++i) {
187 printf(", %d", READ8());
188 }
189 break;
190 }
191 case ByteCodeInstruction::kStoreSwizzleIndirect: {
192 int count = READ8();
193 printf("storeswizzleindirect %d", count);
194 for (int i = 0; i < count; ++i) {
195 printf(", %d", READ8());
196 }
197 break;
198 }
199 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
200 int count = READ8();
201 printf("storeswizzleindirectglobal %d", count);
202 for (int i = 0; i < count; ++i) {
203 printf(", %d", READ8());
204 }
205 break;
206 }
207 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
208 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
209 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400210 VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf")
Brian Osman3e833e12019-05-23 13:23:24 -0700211 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
212 case ByteCodeInstruction::kSwizzle: {
213 printf("swizzle %d, ", READ8());
214 int count = READ8();
215 printf("%d", count);
216 for (int i = 0; i < count; ++i) {
217 printf(", %d", READ8());
218 }
219 break;
220 }
221 VECTOR_DISASSEMBLE(kTan, "tan")
222 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
223 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
224 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
225 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
Brian Osman569f12f2019-06-13 11:23:57 -0400226 case ByteCodeInstruction::kXorB: printf("xorb"); break;
227 case ByteCodeInstruction::kMaskPush: printf("maskpush"); break;
228 case ByteCodeInstruction::kMaskPop: printf("maskpop"); break;
229 case ByteCodeInstruction::kMaskNegate: printf("masknegate"); break;
230 case ByteCodeInstruction::kMaskBlend: printf("maskblend %d", READ8()); break;
231 case ByteCodeInstruction::kBranchIfAllFalse:
232 printf("branchifallfalse %d", READ16());
233 break;
234 case ByteCodeInstruction::kLoopBegin: printf("loopbegin"); break;
235 case ByteCodeInstruction::kLoopNext: printf("loopnext"); break;
236 case ByteCodeInstruction::kLoopMask: printf("loopmask"); break;
237 case ByteCodeInstruction::kLoopEnd: printf("loopend"); break;
238 case ByteCodeInstruction::kLoopContinue: printf("loopcontinue"); break;
239 case ByteCodeInstruction::kLoopBreak: printf("loopbreak"); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700240 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
241 }
242 return ip;
243}
244
Mike Klein459aed12019-05-21 15:46:36 -0500245#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500246 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500247 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500248 POP(); \
249 /* fall through */ \
250 case ByteCodeInstruction::base ## 3: { \
251 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500252 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500253 POP(); \
254 } /* fall through */ \
255 case ByteCodeInstruction::base ## 2: { \
256 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500257 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500258 POP(); \
259 } /* fall through */ \
260 case ByteCodeInstruction::base: { \
261 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500262 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500263 POP(); \
264 break; \
265 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400266
Brian Osman1e855b22019-05-29 15:21:52 -0400267#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
268 VECTOR_BINARY_OP(base, field, op) \
269 case ByteCodeInstruction::base ## N: { \
270 int count = READ8(); \
271 for (int i = count; i > 0; --i) { \
272 sp[-count] = sp[-count].field op sp[0].field; \
273 POP(); \
274 } \
275 break; \
276 }
277
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400278#define VECTOR_BINARY_FN(base, field, fn) \
279 case ByteCodeInstruction::base ## 4: \
280 sp[-4] = fn(sp[-4].field, sp[0].field); \
281 POP(); \
282 /* fall through */ \
283 case ByteCodeInstruction::base ## 3: { \
284 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
285 sp[target] = fn(sp[target].field, sp[0].field); \
286 POP(); \
287 } /* fall through */ \
288 case ByteCodeInstruction::base ## 2: { \
289 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
290 sp[target] = fn(sp[target].field, sp[0].field); \
291 POP(); \
292 } /* fall through */ \
293 case ByteCodeInstruction::base: { \
294 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
295 sp[target] = fn(sp[target].field, sp[0].field); \
296 POP(); \
297 break; \
Mike Kleine7007382019-05-21 08:36:32 -0500298 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400299
Mike Klein459aed12019-05-21 15:46:36 -0500300#define VECTOR_UNARY_FN(base, fn, field) \
301 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
302 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
303 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
304 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400305 break;
306
Brian Osman569f12f2019-06-13 11:23:57 -0400307#define VECTOR_UNARY_FN_VEC(base, fn) \
308 case ByteCodeInstruction::base ## 4: \
309 case ByteCodeInstruction::base ## 3: \
310 case ByteCodeInstruction::base ## 2: \
311 case ByteCodeInstruction::base : { \
312 int count = (int)inst - (int)ByteCodeInstruction::base + 1; \
313 float* v = (float*)sp - count + 1; \
314 for (int i = VecWidth * count; i > 0; --i, ++v) { \
315 *v = fn(*v); \
316 } \
317 break; \
318 }
319
320union VValue {
321 VValue() {}
322
323 VValue(F32 f)
324 : fFloat(f) {
325 }
326
327 VValue(I32 s)
328 : fSigned(s) {
329 }
330
331 VValue(U32 u)
332 : fUnsigned(u) {
333 }
334
335 F32 fFloat;
336 I32 fSigned;
337 U32 fUnsigned;
338};
339
Brian Osman226668a2019-05-14 16:47:30 -0400340struct StackFrame {
341 const uint8_t* fCode;
342 const uint8_t* fIP;
Brian Osman569f12f2019-06-13 11:23:57 -0400343 VValue* fStack;
Brian Osmand3494ed2019-06-20 15:41:34 -0400344 int fParameterCount;
Brian Osman226668a2019-05-14 16:47:30 -0400345};
346
Brian Osman569f12f2019-06-13 11:23:57 -0400347static F32 mix(F32 start, F32 end, F32 t) {
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400348 return start * (1 - t) + end * t;
349}
350
Brian Osman569f12f2019-06-13 11:23:57 -0400351// TODO: trunc on integers?
352template <typename T>
353static T vec_mod(T a, T b) {
354 return a - skvx::trunc(a / b) * b;
355}
Mike Kleine7007382019-05-21 08:36:32 -0500356
Mike Reed634c9412019-07-18 13:20:04 -0400357#define spf(index) sp[index].fFloat
358
Brian Osman3e6aa9f2019-07-18 17:22:33 +0000359static void innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
Brian Osman1a79f0b2019-06-24 16:32:14 -0400360 float* outReturn[], VValue globals[], bool stripedOutput, int N,
361 int baseIndex) {
Brian Osman4b202a32019-06-21 09:50:29 -0400362 // Needs to be the first N non-negative integers, at least as large as VecWidth
363 static const Interpreter::I32 gLanes = {
364 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
365 };
366
Brian Osman569f12f2019-06-13 11:23:57 -0400367 VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1;
368
369 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
370 auto PUSH = [&](VValue v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
Mike Kleine7007382019-05-21 08:36:32 -0500371
Brian Osman80164412019-06-07 13:00:23 -0400372 const uint8_t* code = f->fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400373 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400374 std::vector<StackFrame> frames;
375
Brian Osman569f12f2019-06-13 11:23:57 -0400376 I32 condStack[16]; // Independent condition masks
377 I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...)
378 I32 contStack[16]; // Continue flags for loops
379 I32 loopStack[16]; // Loop execution masks
Brian Osman4b202a32019-06-21 09:50:29 -0400380 condStack[0] = maskStack[0] = (gLanes < N);
Brian Osman569f12f2019-06-13 11:23:57 -0400381 contStack[0] = I32( 0);
382 loopStack[0] = I32(~0);
383 I32* condPtr = condStack;
384 I32* maskPtr = maskStack;
385 I32* contPtr = contStack;
386 I32* loopPtr = loopStack;
387
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400388 if (f->fConditionCount + 1 > (int)SK_ARRAY_COUNT(condStack) ||
389 f->fLoopCount + 1 > (int)SK_ARRAY_COUNT(loopStack)) {
Brian Osman3e6aa9f2019-07-18 17:22:33 +0000390 SkDEBUGFAIL("Function with too much nested control flow to evaluate");
391 return;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400392 }
393
Brian Osman569f12f2019-06-13 11:23:57 -0400394 auto mask = [&]() { return *maskPtr & *loopPtr; };
395
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400396 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400397#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700398 printf("at %3d ", (int) (ip - code));
399 disassemble_instruction(ip);
400 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400401#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700402 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400403 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500404 VECTOR_BINARY_OP(kAddI, fSigned, +)
Brian Osman1e855b22019-05-29 15:21:52 -0400405 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
Brian Osman569f12f2019-06-13 11:23:57 -0400406
407 // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want:
Brian Osman32c526b2019-06-03 16:13:52 -0400408 case ByteCodeInstruction::kAndB:
Brian Osman569f12f2019-06-13 11:23:57 -0400409 sp[-1] = sp[-1].fSigned & sp[0].fSigned;
410 POP();
411 break;
412 case ByteCodeInstruction::kNotB:
413 sp[0] = ~sp[0].fSigned;
414 break;
415 case ByteCodeInstruction::kOrB:
416 sp[-1] = sp[-1].fSigned | sp[0].fSigned;
417 POP();
418 break;
419 case ByteCodeInstruction::kXorB:
420 sp[-1] = sp[-1].fSigned ^ sp[0].fSigned;
Brian Osman32c526b2019-06-03 16:13:52 -0400421 POP();
422 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400423
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400424 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400425 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400426 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400427
Brian Osman226668a2019-05-14 16:47:30 -0400428 case ByteCodeInstruction::kCall: {
Brian Osmand3494ed2019-06-20 15:41:34 -0400429 // Precursor code reserved space for the return value, and pushed all parameters to
430 // the stack. Update our bottom of stack to point at the first parameter, and our
431 // sp to point past those parameters (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500432 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400433 const ByteCodeFunction* fun = byteCode->fFunctions[target].get();
Brian Osman569f12f2019-06-13 11:23:57 -0400434 if (skvx::any(mask())) {
Brian Osmand3494ed2019-06-20 15:41:34 -0400435 frames.push_back({ code, ip, stack, fun->fParameterCount });
Brian Osman569f12f2019-06-13 11:23:57 -0400436 ip = code = fun->fCode.data();
437 stack = sp - fun->fParameterCount + 1;
438 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
Brian Osman569f12f2019-06-13 11:23:57 -0400439 }
Brian Osman226668a2019-05-14 16:47:30 -0400440 break;
441 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400442
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400443 case ByteCodeInstruction::kCallExternal: {
444 int argumentCount = READ8();
445 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500446 int target = READ8();
Brian Osman80164412019-06-07 13:00:23 -0400447 ExternalValue* v = byteCode->fExternalValues[target];
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400448 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500449
Brian Osman1a79f0b2019-06-24 16:32:14 -0400450 float tmpArgs[4];
451 float tmpReturn[4];
Brian Osman569f12f2019-06-13 11:23:57 -0400452 SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs));
453 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn));
454
455 I32 m = mask();
456 for (int i = 0; i < VecWidth; ++i) {
457 if (m[i]) {
458 for (int j = 0; j < argumentCount; ++j) {
Brian Osman1a79f0b2019-06-24 16:32:14 -0400459 tmpArgs[j] = sp[j].fFloat[i];
Brian Osman569f12f2019-06-13 11:23:57 -0400460 }
Brian Osman1a79f0b2019-06-24 16:32:14 -0400461 v->call(baseIndex + i, tmpArgs, tmpReturn);
Brian Osman569f12f2019-06-13 11:23:57 -0400462 for (int j = 0; j < returnCount; ++j) {
Brian Osman1a79f0b2019-06-24 16:32:14 -0400463 sp[j].fFloat[i] = tmpReturn[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400464 }
465 }
466 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400467 sp += returnCount - 1;
468 break;
469 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400470
Mike Kleinc1999982019-05-21 13:03:49 -0500471 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
Brian Osman1e855b22019-05-29 15:21:52 -0400472 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
Mike Kleinc1999982019-05-21 13:03:49 -0500473 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
Brian Osman1e855b22019-05-29 15:21:52 -0400474 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
Mike Kleinc1999982019-05-21 13:03:49 -0500475 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
476 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
477 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
478 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
479 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
480 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
481 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
482 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
483 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
484 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
485 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
486 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400487
Brian Osman569f12f2019-06-13 11:23:57 -0400488 case ByteCodeInstruction::kConvertFtoI4: sp[-3] = skvx::cast<int>(sp[-3].fFloat);
489 case ByteCodeInstruction::kConvertFtoI3: sp[-2] = skvx::cast<int>(sp[-2].fFloat);
490 case ByteCodeInstruction::kConvertFtoI2: sp[-1] = skvx::cast<int>(sp[-1].fFloat);
491 case ByteCodeInstruction::kConvertFtoI: sp[ 0] = skvx::cast<int>(sp[ 0].fFloat);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400492 break;
493
Brian Osman569f12f2019-06-13 11:23:57 -0400494 case ByteCodeInstruction::kConvertStoF4: sp[-3] = skvx::cast<float>(sp[-3].fSigned);
495 case ByteCodeInstruction::kConvertStoF3: sp[-2] = skvx::cast<float>(sp[-2].fSigned);
496 case ByteCodeInstruction::kConvertStoF2: sp[-1] = skvx::cast<float>(sp[-1].fSigned);
497 case ByteCodeInstruction::kConvertStoF : sp[ 0] = skvx::cast<float>(sp[ 0].fSigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400498 break;
499
Brian Osman569f12f2019-06-13 11:23:57 -0400500 case ByteCodeInstruction::kConvertUtoF4: sp[-3] = skvx::cast<float>(sp[-3].fUnsigned);
501 case ByteCodeInstruction::kConvertUtoF3: sp[-2] = skvx::cast<float>(sp[-2].fUnsigned);
502 case ByteCodeInstruction::kConvertUtoF2: sp[-1] = skvx::cast<float>(sp[-1].fUnsigned);
503 case ByteCodeInstruction::kConvertUtoF : sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400504 break;
505
Brian Osman569f12f2019-06-13 11:23:57 -0400506 VECTOR_UNARY_FN_VEC(kCos, cosf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400507
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400508 case ByteCodeInstruction::kCross: {
Brian Osman569f12f2019-06-13 11:23:57 -0400509 F32 ax = sp[-5].fFloat, ay = sp[-4].fFloat, az = sp[-3].fFloat,
510 bx = sp[-2].fFloat, by = sp[-1].fFloat, bz = sp[ 0].fFloat;
511 F32 cx = ay*bz - az*by,
512 cy = az*bx - ax*bz,
513 cz = ax*by - ay*bx;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400514 sp -= 3;
Brian Osman569f12f2019-06-13 11:23:57 -0400515 sp[-2] = cx;
516 sp[-1] = cy;
517 sp[ 0] = cz;
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400518 break;
519 }
520
Mike Kleinc1999982019-05-21 13:03:49 -0500521 VECTOR_BINARY_OP(kDivideS, fSigned, /)
522 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
Brian Osman1e855b22019-05-29 15:21:52 -0400523 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500524
525 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
526 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
527 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
528 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
529 break;
530
Brian Osman07c117b2019-05-23 12:51:06 -0700531 case ByteCodeInstruction::kDupN: {
532 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400533 memcpy(sp + 1, sp - count + 1, count * sizeof(VValue));
Brian Osman07c117b2019-05-23 12:51:06 -0700534 sp += count;
535 break;
536 }
537
Mike Reed634c9412019-07-18 13:20:04 -0400538 case ByteCodeInstruction::kInverse2x2: {
539 F32 a = sp[-3].fFloat,
540 b = sp[-2].fFloat,
541 c = sp[-1].fFloat,
542 d = sp[ 0].fFloat;
543 F32 idet = F32(1) / (a*d - b*c);
544 sp[-3].fFloat = d * idet;
545 sp[-2].fFloat = -b * idet;
546 sp[-1].fFloat = -c * idet;
547 sp[ 0].fFloat = a * idet;
548 break;
549 }
550 case ByteCodeInstruction::kInverse3x3: {
551 F32 a11 = sp[-8].fFloat, a12 = sp[-5].fFloat, a13 = sp[-2].fFloat,
552 a21 = sp[-7].fFloat, a22 = sp[-4].fFloat, a23 = sp[-1].fFloat,
553 a31 = sp[-6].fFloat, a32 = sp[-3].fFloat, a33 = sp[ 0].fFloat;
554 F32 idet = F32(1) / (a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 -
555 a11 * a23 * a32 - a12 * a21 * a33 - a13 * a22 * a31);
556 sp[-8].fFloat = (a22 * a33 - a23 * a32) * idet;
557 sp[-7].fFloat = (a23 * a31 - a21 * a33) * idet;
558 sp[-6].fFloat = (a21 * a32 - a22 * a31) * idet;
559 sp[-5].fFloat = (a13 * a32 - a12 * a33) * idet;
560 sp[-4].fFloat = (a11 * a33 - a13 * a31) * idet;
561 sp[-3].fFloat = (a12 * a31 - a11 * a32) * idet;
562 sp[-2].fFloat = (a12 * a23 - a13 * a22) * idet;
563 sp[-1].fFloat = (a13 * a21 - a11 * a23) * idet;
564 sp[ 0].fFloat = (a11 * a22 - a12 * a21) * idet;
565 break;
566 }
567 case ByteCodeInstruction::kInverse4x4: {
568 F32 a00 = spf(-15), a10 = spf(-11), a20 = spf( -7), a30 = spf( -3),
569 a01 = spf(-14), a11 = spf(-10), a21 = spf( -6), a31 = spf( -2),
570 a02 = spf(-13), a12 = spf( -9), a22 = spf( -5), a32 = spf( -1),
571 a03 = spf(-12), a13 = spf( -8), a23 = spf( -4), a33 = spf( 0);
572
573 F32 b00 = a00 * a11 - a01 * a10,
574 b01 = a00 * a12 - a02 * a10,
575 b02 = a00 * a13 - a03 * a10,
576 b03 = a01 * a12 - a02 * a11,
577 b04 = a01 * a13 - a03 * a11,
578 b05 = a02 * a13 - a03 * a12,
579 b06 = a20 * a31 - a21 * a30,
580 b07 = a20 * a32 - a22 * a30,
581 b08 = a20 * a33 - a23 * a30,
582 b09 = a21 * a32 - a22 * a31,
583 b10 = a21 * a33 - a23 * a31,
584 b11 = a22 * a33 - a23 * a32;
585
586 F32 idet = F32(1) /
587 (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06);
588
589 b00 *= idet;
590 b01 *= idet;
591 b02 *= idet;
592 b03 *= idet;
593 b04 *= idet;
594 b05 *= idet;
595 b06 *= idet;
596 b07 *= idet;
597 b08 *= idet;
598 b09 *= idet;
599 b10 *= idet;
600 b11 *= idet;
601
602 spf(-15) = a11 * b11 - a12 * b10 + a13 * b09;
603 spf(-14) = a02 * b10 - a01 * b11 - a03 * b09;
604 spf(-13) = a31 * b05 - a32 * b04 + a33 * b03;
605 spf(-12) = a22 * b04 - a21 * b05 - a23 * b03;
606 spf(-11) = a12 * b08 - a10 * b11 - a13 * b07;
607 spf(-10) = a00 * b11 - a02 * b08 + a03 * b07;
608 spf( -9) = a32 * b02 - a30 * b05 - a33 * b01;
609 spf( -8) = a20 * b05 - a22 * b02 + a23 * b01;
610 spf( -7) = a10 * b10 - a11 * b08 + a13 * b06;
611 spf( -6) = a01 * b08 - a00 * b10 - a03 * b06;
612 spf( -5) = a30 * b04 - a31 * b02 + a33 * b00;
613 spf( -4) = a21 * b02 - a20 * b04 - a23 * b00;
614 spf( -3) = a11 * b07 - a10 * b09 - a12 * b06;
615 spf( -2) = a00 * b09 - a01 * b07 + a02 * b06;
616 spf( -1) = a31 * b01 - a30 * b03 - a32 * b00;
617 spf( 0) = a20 * b03 - a21 * b01 + a22 * b00;
618 break;
619 }
620
Mike Kleine7007382019-05-21 08:36:32 -0500621 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
622 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
623 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
624 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
625 ++ip;
626 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
627 break;
628
Brian Osman80164412019-06-07 13:00:23 -0400629 case ByteCodeInstruction::kLoadGlobal4: sp[4] = globals[*ip + 3];
630 case ByteCodeInstruction::kLoadGlobal3: sp[3] = globals[*ip + 2];
631 case ByteCodeInstruction::kLoadGlobal2: sp[2] = globals[*ip + 1];
632 case ByteCodeInstruction::kLoadGlobal : sp[1] = globals[*ip + 0];
Mike Kleine7007382019-05-21 08:36:32 -0500633 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700634 sp += (int)inst -
635 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500636 break;
637
Brian Osman07c117b2019-05-23 12:51:06 -0700638 case ByteCodeInstruction::kLoadExtended: {
639 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400640 I32 src = POP().fSigned;
641 I32 m = mask();
642 for (int i = 0; i < count; ++i) {
643 for (int j = 0; j < VecWidth; ++j) {
644 if (m[j]) {
645 sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j];
646 }
647 }
648 }
Brian Osman07c117b2019-05-23 12:51:06 -0700649 sp += count;
650 break;
651 }
652
653 case ByteCodeInstruction::kLoadExtendedGlobal: {
654 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400655 I32 src = POP().fSigned;
656 I32 m = mask();
657 for (int i = 0; i < count; ++i) {
658 for (int j = 0; j < VecWidth; ++j) {
659 if (m[j]) {
660 sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j];
661 }
662 }
663 }
Brian Osman07c117b2019-05-23 12:51:06 -0700664 sp += count;
665 break;
666 }
667
Mike Kleine7007382019-05-21 08:36:32 -0500668 case ByteCodeInstruction::kLoadSwizzle: {
669 int src = READ8();
670 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400671 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400672 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400673 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400674 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400675 break;
Mike Kleine7007382019-05-21 08:36:32 -0500676 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400677
Mike Kleine7007382019-05-21 08:36:32 -0500678 case ByteCodeInstruction::kLoadSwizzleGlobal: {
679 int src = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500680 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400681 for (int i = 0; i < count; ++i) {
Brian Osman80164412019-06-07 13:00:23 -0400682 PUSH(globals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400683 }
684 ip += count;
685 break;
Mike Kleine7007382019-05-21 08:36:32 -0500686 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400687
Brian Osman29e013d2019-05-28 17:16:03 -0400688 case ByteCodeInstruction::kMatrixToMatrix: {
689 int srcCols = READ8();
690 int srcRows = READ8();
691 int dstCols = READ8();
692 int dstRows = READ8();
693 SkASSERT(srcCols >= 2 && srcCols <= 4);
694 SkASSERT(srcRows >= 2 && srcRows <= 4);
695 SkASSERT(dstCols >= 2 && dstCols <= 4);
696 SkASSERT(dstRows >= 2 && dstRows <= 4);
Brian Osman569f12f2019-06-13 11:23:57 -0400697 F32 tmp[16];
698 memset(tmp, 0, sizeof(tmp));
699 tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f);
Brian Osman29e013d2019-05-28 17:16:03 -0400700 for (int c = srcCols - 1; c >= 0; --c) {
701 for (int r = srcRows - 1; r >= 0; --r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400702 tmp[c*4 + r] = POP().fFloat;
Brian Osman29e013d2019-05-28 17:16:03 -0400703 }
704 }
705 for (int c = 0; c < dstCols; ++c) {
706 for (int r = 0; r < dstRows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400707 PUSH(tmp[c*4 + r]);
Brian Osman29e013d2019-05-28 17:16:03 -0400708 }
709 }
710 break;
711 }
712
Brian Osman909231c2019-05-29 15:34:36 -0400713 case ByteCodeInstruction::kMatrixMultiply: {
714 int lCols = READ8();
715 int lRows = READ8();
716 int rCols = READ8();
717 int rRows = lCols;
Brian Osman569f12f2019-06-13 11:23:57 -0400718 F32 tmp[16] = { 0.0f };
719 F32* B = &(sp - (rCols * rRows) + 1)->fFloat;
720 F32* A = B - (lCols * lRows);
Brian Osman909231c2019-05-29 15:34:36 -0400721 for (int c = 0; c < rCols; ++c) {
722 for (int r = 0; r < lRows; ++r) {
723 for (int j = 0; j < lCols; ++j) {
724 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
725 }
726 }
727 }
728 sp -= (lCols * lRows) + (rCols * rRows);
Brian Osman569f12f2019-06-13 11:23:57 -0400729 memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue));
Brian Osman909231c2019-05-29 15:34:36 -0400730 sp += (rCols * lRows);
731 break;
732 }
733
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400734 // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T
735 case ByteCodeInstruction::kMix4:
736 sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat);
737 // fall through
738 case ByteCodeInstruction::kMix3: {
739 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
740 int target = 2 - count * 2;
741 sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat);
742 // fall through
743 }
744 case ByteCodeInstruction::kMix2: {
745 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
746 int target = 1 - count * 2;
747 sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat);
748 // fall through
749 }
750 case ByteCodeInstruction::kMix: {
751 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
752 int target = -count * 2;
753 sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat);
754 sp -= 1 + count;
755 break;
756 }
757
Mike Kleinc1999982019-05-21 13:03:49 -0500758 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
Brian Osman1e855b22019-05-29 15:21:52 -0400759 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400760
Mike Kleinc1999982019-05-21 13:03:49 -0500761 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
762 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
763 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
764 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500765 break;
766
Brian Osman1e855b22019-05-29 15:21:52 -0400767 case ByteCodeInstruction::kNegateFN: {
768 int count = READ8();
769 for (int i = count - 1; i >= 0; --i) {
770 sp[-i] = -sp[-i].fFloat;
771 }
772 break;
773 }
774
Mike Kleinc1999982019-05-21 13:03:49 -0500775 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
776 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
777 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
Brian Osman569f12f2019-06-13 11:23:57 -0400778 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp[ 0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500779 break;
780
781 case ByteCodeInstruction::kPop4: POP();
782 case ByteCodeInstruction::kPop3: POP();
783 case ByteCodeInstruction::kPop2: POP();
784 case ByteCodeInstruction::kPop : POP();
785 break;
786
Brian Osman07c117b2019-05-23 12:51:06 -0700787 case ByteCodeInstruction::kPopN:
788 sp -= READ8();
789 break;
790
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400791 case ByteCodeInstruction::kPushImmediate:
Brian Osman569f12f2019-06-13 11:23:57 -0400792 PUSH(U32(READ32()));
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400793 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400794
Brian Osman569f12f2019-06-13 11:23:57 -0400795 case ByteCodeInstruction::kReadExternal:
796 case ByteCodeInstruction::kReadExternal2:
797 case ByteCodeInstruction::kReadExternal3:
Mike Kleine7007382019-05-21 08:36:32 -0500798 case ByteCodeInstruction::kReadExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -0400799 int count = (int)inst - (int)ByteCodeInstruction::kReadExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500800 int src = READ8();
Brian Osman1a79f0b2019-06-24 16:32:14 -0400801 float tmp[4];
Brian Osman569f12f2019-06-13 11:23:57 -0400802 I32 m = mask();
803 for (int i = 0; i < VecWidth; ++i) {
804 if (m[i]) {
Brian Osman1a79f0b2019-06-24 16:32:14 -0400805 byteCode->fExternalValues[src]->read(baseIndex + i, tmp);
Brian Osman569f12f2019-06-13 11:23:57 -0400806 for (int j = 0; j < count; ++j) {
Brian Osman1a79f0b2019-06-24 16:32:14 -0400807 sp[j + 1].fFloat[i] = tmp[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400808 }
809 }
810 }
811 sp += count;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400812 break;
Mike Kleine7007382019-05-21 08:36:32 -0500813 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400814
Brian Osman569f12f2019-06-13 11:23:57 -0400815 VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
816 VECTOR_BINARY_FN(kRemainderS, fSigned, vec_mod<I32>)
817 VECTOR_BINARY_FN(kRemainderU, fUnsigned, vec_mod<U32>)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400818
Brian Osmand3494ed2019-06-20 15:41:34 -0400819 case ByteCodeInstruction::kReserve:
820 sp += READ8();
821 break;
822
Mike Kleine7007382019-05-21 08:36:32 -0500823 case ByteCodeInstruction::kReturn: {
824 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400825 if (frames.empty()) {
826 if (outReturn) {
Brian Osman569f12f2019-06-13 11:23:57 -0400827 VValue* src = sp - count + 1;
Mike Reed3fd3cc92019-06-20 12:40:30 -0400828 if (stripedOutput) {
829 for (int i = 0; i < count; ++i) {
Brian Osman4b202a32019-06-21 09:50:29 -0400830 memcpy(outReturn[i], &src->fFloat, N * sizeof(float));
831 ++src;
Brian Osman569f12f2019-06-13 11:23:57 -0400832 }
Mike Reed3fd3cc92019-06-20 12:40:30 -0400833 } else {
834 float* outPtr = outReturn[0];
835 for (int i = 0; i < count; ++i) {
Brian Osman4b202a32019-06-21 09:50:29 -0400836 for (int j = 0; j < N; ++j) {
837 outPtr[count * j] = src->fFloat[j];
Mike Reed3fd3cc92019-06-20 12:40:30 -0400838 }
839 ++outPtr;
840 ++src;
841 }
Brian Osman569f12f2019-06-13 11:23:57 -0400842 }
Brian Osman226668a2019-05-14 16:47:30 -0400843 }
Brian Osman3e6aa9f2019-07-18 17:22:33 +0000844 return;
Brian Osman226668a2019-05-14 16:47:30 -0400845 } else {
Brian Osmand3494ed2019-06-20 15:41:34 -0400846 // When we were called, the caller reserved stack space for their copy of our
847 // return value, then 'stack' was positioned after that, where our parameters
848 // were placed. Copy our return values to their reserved area.
849 memcpy(stack - count, sp - count + 1, count * sizeof(VValue));
Brian Osman226668a2019-05-14 16:47:30 -0400850
Brian Osmand3494ed2019-06-20 15:41:34 -0400851 // Now move the stack pointer to the end of the passed-in parameters. This odd
852 // calling convention requires the caller to pop the arguments after calling,
853 // but allows them to store any out-parameters back during that unwinding.
854 // After that sequence finishes, the return value will be the top of the stack.
Brian Osman226668a2019-05-14 16:47:30 -0400855 const StackFrame& frame(frames.back());
Brian Osmand3494ed2019-06-20 15:41:34 -0400856 sp = stack + frame.fParameterCount - 1;
Brian Osman226668a2019-05-14 16:47:30 -0400857 stack = frame.fStack;
858 code = frame.fCode;
859 ip = frame.fIP;
860 frames.pop_back();
861 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400862 }
Mike Kleine7007382019-05-21 08:36:32 -0500863 }
864
Brian Osman29e013d2019-05-28 17:16:03 -0400865 case ByteCodeInstruction::kScalarToMatrix: {
866 int cols = READ8();
867 int rows = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400868 VValue v = POP();
Brian Osman29e013d2019-05-28 17:16:03 -0400869 for (int c = 0; c < cols; ++c) {
870 for (int r = 0; r < rows; ++r) {
Brian Osman569f12f2019-06-13 11:23:57 -0400871 PUSH(c == r ? v : F32(0.0f));
Brian Osman29e013d2019-05-28 17:16:03 -0400872 }
873 }
874 break;
875 }
876
Brian Osman569f12f2019-06-13 11:23:57 -0400877 VECTOR_UNARY_FN_VEC(kSin, sinf)
878 VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400879
Brian Osman569f12f2019-06-13 11:23:57 -0400880 case ByteCodeInstruction::kStore4:
881 stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat);
882 case ByteCodeInstruction::kStore3:
883 stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat);
884 case ByteCodeInstruction::kStore2:
885 stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat);
886 case ByteCodeInstruction::kStore :
887 stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat);
888 ++ip;
889 break;
Mike Kleine7007382019-05-21 08:36:32 -0500890
Brian Osman569f12f2019-06-13 11:23:57 -0400891 case ByteCodeInstruction::kStoreGlobal4:
892 globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat);
893 case ByteCodeInstruction::kStoreGlobal3:
894 globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat);
895 case ByteCodeInstruction::kStoreGlobal2:
896 globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat);
897 case ByteCodeInstruction::kStoreGlobal :
898 globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat);
899 ++ip;
900 break;
Mike Kleine7007382019-05-21 08:36:32 -0500901
Brian Osman07c117b2019-05-23 12:51:06 -0700902 case ByteCodeInstruction::kStoreExtended: {
903 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400904 I32 target = POP().fSigned;
905 VValue* src = sp - count + 1;
906 I32 m = mask();
907 for (int i = 0; i < count; ++i) {
908 for (int j = 0; j < VecWidth; ++j) {
909 if (m[j]) {
910 stack[target[j] + i].fSigned[j] = src[i].fSigned[j];
911 }
912 }
913 }
Brian Osman07c117b2019-05-23 12:51:06 -0700914 sp -= count;
915 break;
916 }
917 case ByteCodeInstruction::kStoreExtendedGlobal: {
918 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400919 I32 target = POP().fSigned;
920 VValue* src = sp - count + 1;
921 I32 m = mask();
922 for (int i = 0; i < count; ++i) {
923 for (int j = 0; j < VecWidth; ++j) {
924 if (m[j]) {
925 globals[target[j] + i].fSigned[j] = src[i].fSigned[j];
926 }
927 }
928 }
Brian Osman07c117b2019-05-23 12:51:06 -0700929 sp -= count;
930 break;
931 }
932
Mike Kleine7007382019-05-21 08:36:32 -0500933 case ByteCodeInstruction::kStoreSwizzle: {
934 int target = READ8();
935 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400936 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400937 stack[target + *(ip + i)] = skvx::if_then_else(
938 mask(), POP().fFloat, stack[target + *(ip + i)].fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400939 }
Brian Osman1091f022019-05-16 09:42:16 -0400940 ip += count;
941 break;
Mike Kleine7007382019-05-21 08:36:32 -0500942 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400943
Mike Kleine7007382019-05-21 08:36:32 -0500944 case ByteCodeInstruction::kStoreSwizzleGlobal: {
945 int target = READ8();
946 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400947 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400948 globals[target + *(ip + i)] = skvx::if_then_else(
949 mask(), POP().fFloat, globals[target + *(ip + i)].fFloat);
Brian Osman1091f022019-05-16 09:42:16 -0400950 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400951 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400952 break;
Mike Kleine7007382019-05-21 08:36:32 -0500953 }
Brian Osman569f12f2019-06-13 11:23:57 -0400954
Brian Osman07c117b2019-05-23 12:51:06 -0700955 case ByteCodeInstruction::kStoreSwizzleIndirect: {
Brian Osman07c117b2019-05-23 12:51:06 -0700956 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400957 I32 target = POP().fSigned;
958 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700959 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400960 I32 v = POP().fSigned;
961 for (int j = 0; j < VecWidth; ++j) {
962 if (m[j]) {
963 stack[target[j] + *(ip + i)].fSigned[j] = v[j];
964 }
965 }
Brian Osman07c117b2019-05-23 12:51:06 -0700966 }
967 ip += count;
968 break;
969 }
Brian Osman569f12f2019-06-13 11:23:57 -0400970
Brian Osman07c117b2019-05-23 12:51:06 -0700971 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
Brian Osman07c117b2019-05-23 12:51:06 -0700972 int count = READ8();
Brian Osman569f12f2019-06-13 11:23:57 -0400973 I32 target = POP().fSigned;
974 I32 m = mask();
Brian Osman07c117b2019-05-23 12:51:06 -0700975 for (int i = count - 1; i >= 0; --i) {
Brian Osman569f12f2019-06-13 11:23:57 -0400976 I32 v = POP().fSigned;
977 for (int j = 0; j < VecWidth; ++j) {
978 if (m[j]) {
979 globals[target[j] + *(ip + i)].fSigned[j] = v[j];
980 }
981 }
Brian Osman07c117b2019-05-23 12:51:06 -0700982 }
983 ip += count;
984 break;
985 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400986
Mike Kleinc1999982019-05-21 13:03:49 -0500987 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
Brian Osman1e855b22019-05-29 15:21:52 -0400988 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400989
Mike Kleine7007382019-05-21 08:36:32 -0500990 case ByteCodeInstruction::kSwizzle: {
Brian Osman569f12f2019-06-13 11:23:57 -0400991 VValue tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400992 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400993 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400994 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400995 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400996 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400997 }
998 break;
Mike Kleine7007382019-05-21 08:36:32 -0500999 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -04001000
Brian Osman569f12f2019-06-13 11:23:57 -04001001 VECTOR_UNARY_FN_VEC(kTan, tanf)
Ethan Nicholas82162ee2019-05-21 16:05:08 -04001002
Brian Osman569f12f2019-06-13 11:23:57 -04001003 case ByteCodeInstruction::kWriteExternal:
1004 case ByteCodeInstruction::kWriteExternal2:
1005 case ByteCodeInstruction::kWriteExternal3:
Mike Kleine7007382019-05-21 08:36:32 -05001006 case ByteCodeInstruction::kWriteExternal4: {
Brian Osman569f12f2019-06-13 11:23:57 -04001007 int count = (int)inst - (int)ByteCodeInstruction::kWriteExternal + 1;
Mike Kleine7007382019-05-21 08:36:32 -05001008 int target = READ8();
Brian Osman1a79f0b2019-06-24 16:32:14 -04001009 float tmp[4];
Brian Osman569f12f2019-06-13 11:23:57 -04001010 I32 m = mask();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -04001011 sp -= count;
Brian Osman569f12f2019-06-13 11:23:57 -04001012 for (int i = 0; i < VecWidth; ++i) {
1013 if (m[i]) {
1014 for (int j = 0; j < count; ++j) {
Brian Osman1a79f0b2019-06-24 16:32:14 -04001015 tmp[j] = sp[j + 1].fFloat[i];
Brian Osman569f12f2019-06-13 11:23:57 -04001016 }
Brian Osman1a79f0b2019-06-24 16:32:14 -04001017 byteCode->fExternalValues[target]->write(baseIndex + i, tmp);
Brian Osman569f12f2019-06-13 11:23:57 -04001018 }
1019 }
1020 break;
1021 }
1022
1023 case ByteCodeInstruction::kMaskPush:
1024 condPtr[1] = POP().fSigned;
1025 maskPtr[1] = maskPtr[0] & condPtr[1];
1026 ++condPtr; ++maskPtr;
1027 break;
1028 case ByteCodeInstruction::kMaskPop:
1029 --condPtr; --maskPtr;
1030 break;
1031 case ByteCodeInstruction::kMaskNegate:
1032 maskPtr[0] = maskPtr[-1] & ~condPtr[0];
1033 break;
1034 case ByteCodeInstruction::kMaskBlend: {
1035 int count = READ8();
1036 I32 m = condPtr[0];
1037 --condPtr; --maskPtr;
1038 for (int i = 0; i < count; ++i) {
1039 sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat);
1040 --sp;
1041 }
1042 break;
1043 }
1044 case ByteCodeInstruction::kBranchIfAllFalse: {
1045 int target = READ16();
1046 if (!skvx::any(mask())) {
1047 ip = code + target;
1048 }
1049 break;
1050 }
1051
1052 case ByteCodeInstruction::kLoopBegin:
Brian Osman8d564572019-06-19 11:00:28 -04001053 contPtr[1] = 0;
1054 loopPtr[1] = loopPtr[0];
1055 ++contPtr; ++loopPtr;
Brian Osman569f12f2019-06-13 11:23:57 -04001056 break;
1057 case ByteCodeInstruction::kLoopNext:
1058 *loopPtr |= *contPtr;
1059 *contPtr = 0;
1060 break;
1061 case ByteCodeInstruction::kLoopMask:
1062 *loopPtr &= POP().fSigned;
1063 break;
1064 case ByteCodeInstruction::kLoopEnd:
1065 --contPtr; --loopPtr;
1066 break;
1067 case ByteCodeInstruction::kLoopBreak:
1068 *loopPtr &= ~mask();
1069 break;
1070 case ByteCodeInstruction::kLoopContinue: {
1071 I32 m = mask();
1072 *contPtr |= m;
1073 *loopPtr &= ~m;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001074 break;
Mike Kleine7007382019-05-21 08:36:32 -05001075 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -04001076
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001077 default:
Mike Kleine7007382019-05-21 08:36:32 -05001078 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001079 }
Brian Osman569f12f2019-06-13 11:23:57 -04001080 }
1081}
1082
Brian Osman08a84962019-06-14 10:17:16 -04001083} // namespace Interpreter
1084
Brian Osman489cf882019-07-09 10:48:28 -04001085#endif // SK_ENABLE_SKSL_INTERPRETER
1086
Mike Reed634c9412019-07-18 13:20:04 -04001087#undef spf
1088
Brian Osman08a84962019-06-14 10:17:16 -04001089void ByteCodeFunction::disassemble() const {
Brian Osman489cf882019-07-09 10:48:28 -04001090#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman08a84962019-06-14 10:17:16 -04001091 const uint8_t* ip = fCode.data();
1092 while (ip < fCode.data() + fCode.size()) {
1093 printf("%d: ", (int)(ip - fCode.data()));
1094 ip = Interpreter::disassemble_instruction(ip);
1095 printf("\n");
1096 }
Brian Osman489cf882019-07-09 10:48:28 -04001097#endif
Brian Osman08a84962019-06-14 10:17:16 -04001098}
1099
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001100void ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
Brian Osman08a84962019-06-14 10:17:16 -04001101 const float* uniforms, int uniformCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001102#if defined(SK_ENABLE_SKSL_INTERPRETER)
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001103#ifdef TRACE
Brian Osman08a84962019-06-14 10:17:16 -04001104 f->disassemble();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -04001105#endif
Brian Osman4b202a32019-06-21 09:50:29 -04001106 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001107 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1108 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001109 SkDEBUGFAIL("Function requires too much stack space to evaluate");
1110 return;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001111 }
Brian Osmanef787f72019-06-13 13:07:12 -04001112
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001113 SkASSERT(uniformCount == (int)fInputSlots.size());
Brian Osman08a84962019-06-14 10:17:16 -04001114 Interpreter::VValue globals[32];
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001115 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
Brian Osman08a84962019-06-14 10:17:16 -04001116 for (uint8_t slot : fInputSlots) {
1117 globals[slot].fFloat = *uniforms++;
Brian Osman569f12f2019-06-13 11:23:57 -04001118 }
1119
Brian Osman1a79f0b2019-06-24 16:32:14 -04001120 int baseIndex = 0;
1121
Brian Osman569f12f2019-06-13 11:23:57 -04001122 while (N) {
Brian Osman08a84962019-06-14 10:17:16 -04001123 int w = std::min(N, Interpreter::VecWidth);
Brian Osman569f12f2019-06-13 11:23:57 -04001124
1125 // Transpose args into stack
1126 {
Brian Osman08a84962019-06-14 10:17:16 -04001127 float* src = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001128 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001129 float* dst = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001130 for (int j = f->fParameterCount; j > 0; --j) {
1131 *dst = *src++;
Brian Osman08a84962019-06-14 10:17:16 -04001132 dst += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001133 }
1134 }
1135 }
1136
Mike Reed3fd3cc92019-06-20 12:40:30 -04001137 bool stripedOutput = false;
1138 float** outArray = outReturn ? &outReturn : nullptr;
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001139 innerRun(this, f, stack, outArray, globals, stripedOutput, w, baseIndex);
Brian Osman569f12f2019-06-13 11:23:57 -04001140
1141 // Transpose out parameters back
1142 {
Brian Osman08a84962019-06-14 10:17:16 -04001143 float* dst = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001144 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001145 float* src = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001146 for (const auto& p : f->fParameters) {
1147 if (p.fIsOutParameter) {
1148 for (int j = p.fSlotCount; j > 0; --j) {
1149 *dst++ = *src;
Brian Osman08a84962019-06-14 10:17:16 -04001150 src += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001151 }
1152 } else {
1153 dst += p.fSlotCount;
Brian Osman08a84962019-06-14 10:17:16 -04001154 src += p.fSlotCount * Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001155 }
1156 }
1157 }
1158 }
1159
1160 args += f->fParameterCount * w;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001161 if (outReturn) {
1162 outReturn += f->fReturnCount * w;
1163 }
Brian Osman4b202a32019-06-21 09:50:29 -04001164 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001165 baseIndex += w;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001166 }
Brian Osman489cf882019-07-09 10:48:28 -04001167#else
1168 SkDEBUGFAIL("ByteCode interpreter not enabled");
1169#endif
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001170}
1171
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001172void ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
Mike Reed3fd3cc92019-06-20 12:40:30 -04001173 const float* uniforms, int uniformCount,
1174 float* outArgs[], int outCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001175#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman2b1a5442019-06-19 11:40:33 -04001176#ifdef TRACE
Brian Osmanecb3bb52019-06-20 14:59:12 -04001177 f->disassemble();
Brian Osman2b1a5442019-06-19 11:40:33 -04001178#endif
1179 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001180 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1181 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001182 SkDEBUGFAIL("Function requires too much stack space to evaluate");
1183 return;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001184 }
Brian Osman2b1a5442019-06-19 11:40:33 -04001185
Mike Reed3fd3cc92019-06-20 12:40:30 -04001186 // innerRun just takes outArgs, so clear it if the count is zero
1187 if (outCount == 0) {
1188 outArgs = nullptr;
1189 }
1190
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001191 SkASSERT(nargs == f->fParameterCount);
1192 SkASSERT(outCount == f->fReturnCount);
1193 SkASSERT(uniformCount == (int)fInputSlots.size());
1194 Interpreter::VValue globals[32];
1195 SkASSERT((int)SK_ARRAY_COUNT(globals) >= fGlobalCount);
1196 for (uint8_t slot : fInputSlots) {
1197 globals[slot].fFloat = *uniforms++;
1198 }
1199
Brian Osman1a79f0b2019-06-24 16:32:14 -04001200 int baseIndex = 0;
1201
Brian Osman2b1a5442019-06-19 11:40:33 -04001202 while (N) {
1203 int w = std::min(N, Interpreter::VecWidth);
1204
1205 // Copy args into stack
1206 for (int i = 0; i < nargs; ++i) {
1207 memcpy(stack + i, args[i], w * sizeof(float));
1208 }
1209
Mike Reed3fd3cc92019-06-20 12:40:30 -04001210 bool stripedOutput = true;
Brian Osman3e6aa9f2019-07-18 17:22:33 +00001211 innerRun(this, f, stack, outArgs, globals, stripedOutput, w, baseIndex);
Brian Osman2b1a5442019-06-19 11:40:33 -04001212
1213 // Copy out parameters back
1214 int slot = 0;
1215 for (const auto& p : f->fParameters) {
1216 if (p.fIsOutParameter) {
1217 for (int i = slot; i < slot + p.fSlotCount; ++i) {
1218 memcpy(args[i], stack + i, w * sizeof(float));
1219 }
1220 }
1221 slot += p.fSlotCount;
1222 }
1223
1224 // Step each argument pointer ahead
1225 for (int i = 0; i < nargs; ++i) {
1226 args[i] += w;
1227 }
1228 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001229 baseIndex += w;
Brian Osman2b1a5442019-06-19 11:40:33 -04001230 }
Brian Osman489cf882019-07-09 10:48:28 -04001231#else
1232 SkDEBUGFAIL("ByteCode interpreter not enabled");
1233#endif
Brian Osman2b1a5442019-06-19 11:40:33 -04001234}
1235
Brian Osman80164412019-06-07 13:00:23 -04001236} // namespace SkSL
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001237
1238#endif