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