blob: 674cf35507eba3f40ff0c6958f25cc7486e2151f [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 Nicholas6f624122019-09-24 13:07:06 -0400291// A naive implementation of / or % using skvx operations will likely crash with a divide by zero
292// in inactive vector lanesm, so we need to be sure to avoid masked-off lanes.
293#define VECTOR_BINARY_MASKED_OP(base, field, op) \
294 LABEL(base ## 4) \
295 for (int i = 0; i < VecWidth; ++i) { \
296 if (mask()[i]) { \
297 sp[-4].field[i] op ## = sp[0].field[i]; \
298 } \
299 } \
300 POP(); \
301 /* fall through */ \
302 LABEL(base ## 3) { \
303 for (int i = 0; i < VecWidth; ++i) { \
304 if (mask()[i]) { \
305 sp[-ip[0]].field[i] op ## = sp[0].field[i]; \
306 } \
307 } \
308 POP(); \
309 } /* fall through */ \
310 LABEL(base ## 2) { \
311 for (int i = 0; i < VecWidth; ++i) { \
312 if (mask()[i]) { \
313 sp[-ip[0]].field[i] op ## = sp[0].field[i]; \
314 } \
315 } \
316 POP(); \
317 } /* fall through */ \
318 LABEL(base) { \
319 for (int i = 0; i < VecWidth; ++i) { \
320 if (mask()[i]) { \
321 sp[-ip[0]].field[i] op ## = sp[0].field[i]; \
322 } \
323 } \
324 POP(); \
325 ++ip; \
326 NEXT(); \
327 }
328
329
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400330#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
331 VECTOR_BINARY_OP(base, field, op) \
332 LABEL(base ## N) { \
333 int count = READ8(); \
334 for (int i = count; i > 0; --i) { \
335 sp[-count] = sp[-count].field op sp[0].field; \
336 POP(); \
337 } \
338 NEXT(); \
Brian Osman1e855b22019-05-29 15:21:52 -0400339 }
340
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400341#define VECTOR_BINARY_FN(base, field, fn) \
342 LABEL(base ## 4) \
343 sp[-4] = fn(sp[-4].field, sp[0].field); \
344 POP(); \
345 /* fall through */ \
346 LABEL(base ## 3) { \
347 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
348 POP(); \
349 } /* fall through */ \
350 LABEL(base ## 2) { \
351 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
352 POP(); \
353 } /* fall through */ \
354 LABEL(base) { \
355 sp[-ip[0]] = fn(sp[-ip[0]].field, sp[0].field); \
356 POP(); \
357 ++ip; \
358 NEXT(); \
Mike Kleine7007382019-05-21 08:36:32 -0500359 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400360
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400361#define VECTOR_UNARY_FN(base, fn, field) \
362 LABEL(base ## 4) sp[-3] = fn(sp[-3].field); \
363 LABEL(base ## 3) sp[-2] = fn(sp[-2].field); \
364 LABEL(base ## 2) sp[-1] = fn(sp[-1].field); \
365 LABEL(base) sp[ 0] = fn(sp[ 0].field); \
366 NEXT();
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400367
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400368#define VECTOR_UNARY_FN_VEC(base, fn) \
369 LABEL(base ## 4) \
370 LABEL(base ## 3) \
371 LABEL(base ## 2) \
372 LABEL(base) { \
373 int count = READ8(); \
374 float* v = (float*)sp - count + 1; \
375 for (int i = VecWidth * count; i > 0; --i, ++v) { \
376 *v = fn(*v); \
377 } \
378 NEXT(); \
Brian Osman569f12f2019-06-13 11:23:57 -0400379 }
380
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400381#define VECTOR_LABELS(base) \
382 &&base ## 4, \
383 &&base ## 3, \
384 &&base ## 2, \
385 &&base
386
387#define VECTOR_MATRIX_LABELS(base) \
388 VECTOR_LABELS(base), \
389 &&base ## N
390
391// If you trip this assert, it means that the order of the opcodes listed in ByteCodeInstruction
392// does not match the order of the opcodes listed in the 'labels' array in innerRun().
393#define CHECK_LABEL(name) \
394 SkASSERT(labels[(int) ByteCodeInstruction::name] == &&name)
395
396#define CHECK_VECTOR_LABELS(name) \
397 CHECK_LABEL(name ## 4); \
398 CHECK_LABEL(name ## 3); \
399 CHECK_LABEL(name ## 2); \
400 CHECK_LABEL(name)
401
402#define CHECK_VECTOR_MATRIX_LABELS(name) \
403 CHECK_VECTOR_LABELS(name); \
404 CHECK_LABEL(name ## N)
405
Brian Osman569f12f2019-06-13 11:23:57 -0400406union VValue {
407 VValue() {}
408
409 VValue(F32 f)
410 : fFloat(f) {
411 }
412
413 VValue(I32 s)
414 : fSigned(s) {
415 }
416
417 VValue(U32 u)
418 : fUnsigned(u) {
419 }
420
421 F32 fFloat;
422 I32 fSigned;
423 U32 fUnsigned;
424};
425
Brian Osman226668a2019-05-14 16:47:30 -0400426struct StackFrame {
427 const uint8_t* fCode;
428 const uint8_t* fIP;
Brian Osman569f12f2019-06-13 11:23:57 -0400429 VValue* fStack;
Brian Osmand3494ed2019-06-20 15:41:34 -0400430 int fParameterCount;
Brian Osman226668a2019-05-14 16:47:30 -0400431};
432
Brian Osman569f12f2019-06-13 11:23:57 -0400433template <typename T>
434static T vec_mod(T a, T b) {
435 return a - skvx::trunc(a / b) * b;
436}
Mike Kleine7007382019-05-21 08:36:32 -0500437
Mike Reed634c9412019-07-18 13:20:04 -0400438#define spf(index) sp[index].fFloat
439
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400440static void call_external(const ByteCode* byteCode, const uint8_t*& ip, VValue*& sp,
441 int baseIndex, I32 mask) {
442 int argumentCount = READ8();
443 int returnCount = READ8();
444 int target = READ8();
445 ExternalValue* v = byteCode->fExternalValues[target];
446 sp -= argumentCount - 1;
447
448 float tmpArgs[4];
449 float tmpReturn[4];
450 SkASSERT(argumentCount <= (int)SK_ARRAY_COUNT(tmpArgs));
451 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmpReturn));
452
453 for (int i = 0; i < VecWidth; ++i) {
454 if (mask[i]) {
455 for (int j = 0; j < argumentCount; ++j) {
456 tmpArgs[j] = sp[j].fFloat[i];
457 }
458 v->call(baseIndex + i, tmpArgs, tmpReturn);
459 for (int j = 0; j < returnCount; ++j) {
460 sp[j].fFloat[i] = tmpReturn[j];
461 }
462 }
463 }
464 sp += returnCount - 1;
465}
466
467static void inverse2x2(VValue* sp) {
468 F32 a = sp[-3].fFloat,
469 b = sp[-2].fFloat,
470 c = sp[-1].fFloat,
471 d = sp[ 0].fFloat;
472 F32 idet = F32(1) / (a*d - b*c);
473 sp[-3].fFloat = d * idet;
474 sp[-2].fFloat = -b * idet;
475 sp[-1].fFloat = -c * idet;
476 sp[ 0].fFloat = a * idet;
477}
478
479static void inverse3x3(VValue* sp) {
480 F32 a11 = sp[-8].fFloat, a12 = sp[-5].fFloat, a13 = sp[-2].fFloat,
481 a21 = sp[-7].fFloat, a22 = sp[-4].fFloat, a23 = sp[-1].fFloat,
482 a31 = sp[-6].fFloat, a32 = sp[-3].fFloat, a33 = sp[ 0].fFloat;
483 F32 idet = F32(1) / (a11 * a22 * a33 + a12 * a23 * a31 + a13 * a21 * a32 -
484 a11 * a23 * a32 - a12 * a21 * a33 - a13 * a22 * a31);
485 sp[-8].fFloat = (a22 * a33 - a23 * a32) * idet;
486 sp[-7].fFloat = (a23 * a31 - a21 * a33) * idet;
487 sp[-6].fFloat = (a21 * a32 - a22 * a31) * idet;
488 sp[-5].fFloat = (a13 * a32 - a12 * a33) * idet;
489 sp[-4].fFloat = (a11 * a33 - a13 * a31) * idet;
490 sp[-3].fFloat = (a12 * a31 - a11 * a32) * idet;
491 sp[-2].fFloat = (a12 * a23 - a13 * a22) * idet;
492 sp[-1].fFloat = (a13 * a21 - a11 * a23) * idet;
493 sp[ 0].fFloat = (a11 * a22 - a12 * a21) * idet;
494}
495
496static void inverse4x4(VValue* sp) {
497 F32 a00 = spf(-15), a10 = spf(-11), a20 = spf( -7), a30 = spf( -3),
498 a01 = spf(-14), a11 = spf(-10), a21 = spf( -6), a31 = spf( -2),
499 a02 = spf(-13), a12 = spf( -9), a22 = spf( -5), a32 = spf( -1),
500 a03 = spf(-12), a13 = spf( -8), a23 = spf( -4), a33 = spf( 0);
501
502 F32 b00 = a00 * a11 - a01 * a10,
503 b01 = a00 * a12 - a02 * a10,
504 b02 = a00 * a13 - a03 * a10,
505 b03 = a01 * a12 - a02 * a11,
506 b04 = a01 * a13 - a03 * a11,
507 b05 = a02 * a13 - a03 * a12,
508 b06 = a20 * a31 - a21 * a30,
509 b07 = a20 * a32 - a22 * a30,
510 b08 = a20 * a33 - a23 * a30,
511 b09 = a21 * a32 - a22 * a31,
512 b10 = a21 * a33 - a23 * a31,
513 b11 = a22 * a33 - a23 * a32;
514
515 F32 idet = F32(1) /
516 (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06);
517
518 b00 *= idet;
519 b01 *= idet;
520 b02 *= idet;
521 b03 *= idet;
522 b04 *= idet;
523 b05 *= idet;
524 b06 *= idet;
525 b07 *= idet;
526 b08 *= idet;
527 b09 *= idet;
528 b10 *= idet;
529 b11 *= idet;
530
531 spf(-15) = a11 * b11 - a12 * b10 + a13 * b09;
532 spf(-14) = a02 * b10 - a01 * b11 - a03 * b09;
533 spf(-13) = a31 * b05 - a32 * b04 + a33 * b03;
534 spf(-12) = a22 * b04 - a21 * b05 - a23 * b03;
535 spf(-11) = a12 * b08 - a10 * b11 - a13 * b07;
536 spf(-10) = a00 * b11 - a02 * b08 + a03 * b07;
537 spf( -9) = a32 * b02 - a30 * b05 - a33 * b01;
538 spf( -8) = a20 * b05 - a22 * b02 + a23 * b01;
539 spf( -7) = a10 * b10 - a11 * b08 + a13 * b06;
540 spf( -6) = a01 * b08 - a00 * b10 - a03 * b06;
541 spf( -5) = a30 * b04 - a31 * b02 + a33 * b00;
542 spf( -4) = a21 * b02 - a20 * b04 - a23 * b00;
543 spf( -3) = a11 * b07 - a10 * b09 - a12 * b06;
544 spf( -2) = a00 * b09 - a01 * b07 + a02 * b06;
545 spf( -1) = a31 * b01 - a30 * b03 - a32 * b00;
546 spf( 0) = a20 * b03 - a21 * b01 + a22 * b00;
547}
548
Brian Osman869a3e82019-07-18 17:00:34 -0400549static bool innerRun(const ByteCode* byteCode, const ByteCodeFunction* f, VValue* stack,
Brian Osman1a79f0b2019-06-24 16:32:14 -0400550 float* outReturn[], VValue globals[], bool stripedOutput, int N,
551 int baseIndex) {
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400552#ifdef SKSLC_THREADED_CODE
553 static const void* labels[] = {
554 // If you aren't familiar with it, the &&label syntax is the GCC / Clang "labels as values"
555 // extension. If you add anything to this array, be sure to add the corresponding
556 // CHECK_LABEL() or CHECK_*_LABELS() assert below.
557 VECTOR_MATRIX_LABELS(kAddF),
558 VECTOR_LABELS(kAddI),
559 &&kAndB,
560 &&kBranch,
561 &&kCall,
562 &&kCallExternal,
563 &&kClampIndex,
564 VECTOR_LABELS(kCompareIEQ),
565 VECTOR_LABELS(kCompareINEQ),
566 VECTOR_MATRIX_LABELS(kCompareFEQ),
567 VECTOR_MATRIX_LABELS(kCompareFNEQ),
568 VECTOR_LABELS(kCompareFGT),
569 VECTOR_LABELS(kCompareFGTEQ),
570 VECTOR_LABELS(kCompareFLT),
571 VECTOR_LABELS(kCompareFLTEQ),
572 VECTOR_LABELS(kCompareSGT),
573 VECTOR_LABELS(kCompareSGTEQ),
574 VECTOR_LABELS(kCompareSLT),
575 VECTOR_LABELS(kCompareSLTEQ),
576 VECTOR_LABELS(kCompareUGT),
577 VECTOR_LABELS(kCompareUGTEQ),
578 VECTOR_LABELS(kCompareULT),
579 VECTOR_LABELS(kCompareULTEQ),
580 VECTOR_LABELS(kConvertFtoI),
581 VECTOR_LABELS(kConvertStoF),
582 VECTOR_LABELS(kConvertUtoF),
583 VECTOR_LABELS(kCos),
584 VECTOR_MATRIX_LABELS(kDivideF),
585 VECTOR_LABELS(kDivideS),
586 VECTOR_LABELS(kDivideU),
587 VECTOR_MATRIX_LABELS(kDup),
588 &&kInverse2x2,
589 &&kInverse3x3,
590 &&kInverse4x4,
591 VECTOR_LABELS(kLoad),
592 VECTOR_LABELS(kLoadGlobal),
593 &&kLoadSwizzle,
594 &&kLoadSwizzleGlobal,
595 &&kLoadExtended,
596 &&kLoadExtendedGlobal,
597 &&kMatrixToMatrix,
598 &&kMatrixMultiply,
599 VECTOR_MATRIX_LABELS(kNegateF),
600 VECTOR_LABELS(kNegateI),
601 VECTOR_MATRIX_LABELS(kMultiplyF),
602 VECTOR_LABELS(kMultiplyI),
603 &&kNotB,
604 &&kOrB,
605 VECTOR_MATRIX_LABELS(kPop),
606 &&kPushImmediate,
607 VECTOR_LABELS(kReadExternal),
608 VECTOR_LABELS(kRemainderF),
609 VECTOR_LABELS(kRemainderS),
610 VECTOR_LABELS(kRemainderU),
611 &&kReserve,
612 &&kReturn,
613 &&kScalarToMatrix,
Brian Osman4c2146f2019-09-24 09:39:38 -0400614 &&kShiftLeft,
615 &&kShiftRightS,
616 &&kShiftRightU,
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400617 VECTOR_LABELS(kSin),
618 VECTOR_LABELS(kSqrt),
619 VECTOR_LABELS(kStore),
620 VECTOR_LABELS(kStoreGlobal),
621 &&kStoreExtended,
622 &&kStoreExtendedGlobal,
623 &&kStoreSwizzle,
624 &&kStoreSwizzleGlobal,
625 &&kStoreSwizzleIndirect,
626 &&kStoreSwizzleIndirectGlobal,
627 &&kSwizzle,
628 VECTOR_MATRIX_LABELS(kSubtractF),
629 VECTOR_LABELS(kSubtractI),
630 VECTOR_LABELS(kTan),
631 VECTOR_LABELS(kWriteExternal),
632 &&kXorB,
633
634 &&kMaskPush,
635 &&kMaskPop,
636 &&kMaskNegate,
637 &&kMaskBlend,
638 &&kBranchIfAllFalse,
639
640 &&kLoopBegin,
641 &&kLoopNext,
642 &&kLoopMask,
643 &&kLoopEnd,
644 &&kLoopBreak,
645 &&kLoopContinue,
646 };
647 // Verify that the order of the labels array matches the order of the ByteCodeInstruction enum.
648 CHECK_VECTOR_MATRIX_LABELS(kAddF);
649 CHECK_VECTOR_LABELS(kAddI);
650 CHECK_LABEL(kAndB);
651 CHECK_LABEL(kBranch);
652 CHECK_LABEL(kCall);
653 CHECK_LABEL(kCallExternal);
654 CHECK_LABEL(kClampIndex);
655 CHECK_VECTOR_LABELS(kCompareIEQ);
656 CHECK_VECTOR_LABELS(kCompareINEQ);
657 CHECK_VECTOR_MATRIX_LABELS(kCompareFEQ);
658 CHECK_VECTOR_MATRIX_LABELS(kCompareFNEQ);
659 CHECK_VECTOR_LABELS(kCompareFGT);
660 CHECK_VECTOR_LABELS(kCompareFGTEQ);
661 CHECK_VECTOR_LABELS(kCompareFLT);
662 CHECK_VECTOR_LABELS(kCompareFLTEQ);
663 CHECK_VECTOR_LABELS(kCompareSGT);
664 CHECK_VECTOR_LABELS(kCompareSGTEQ);
665 CHECK_VECTOR_LABELS(kCompareSLT);
666 CHECK_VECTOR_LABELS(kCompareSLTEQ);
667 CHECK_VECTOR_LABELS(kCompareUGT);
668 CHECK_VECTOR_LABELS(kCompareUGTEQ);
669 CHECK_VECTOR_LABELS(kCompareULT);
670 CHECK_VECTOR_LABELS(kCompareULTEQ);
671 CHECK_VECTOR_LABELS(kConvertFtoI);
672 CHECK_VECTOR_LABELS(kConvertStoF);
673 CHECK_VECTOR_LABELS(kConvertUtoF);
674 CHECK_VECTOR_LABELS(kCos);
675 CHECK_VECTOR_MATRIX_LABELS(kDivideF);
676 CHECK_VECTOR_LABELS(kDivideS);
677 CHECK_VECTOR_LABELS(kDivideU);
678 CHECK_VECTOR_MATRIX_LABELS(kDup);
679 CHECK_LABEL(kInverse2x2);
680 CHECK_LABEL(kInverse3x3);
681 CHECK_LABEL(kInverse4x4);
682 CHECK_VECTOR_LABELS(kLoad);
683 CHECK_VECTOR_LABELS(kLoadGlobal);
684 CHECK_LABEL(kLoadSwizzle);
685 CHECK_LABEL(kLoadSwizzleGlobal);
686 CHECK_LABEL(kLoadExtended);
687 CHECK_LABEL(kLoadExtendedGlobal);
688 CHECK_LABEL(kMatrixToMatrix);
689 CHECK_LABEL(kMatrixMultiply);
690 CHECK_VECTOR_MATRIX_LABELS(kNegateF);
691 CHECK_VECTOR_LABELS(kNegateI);
692 CHECK_VECTOR_MATRIX_LABELS(kMultiplyF);
693 CHECK_VECTOR_LABELS(kMultiplyI);
694 CHECK_LABEL(kNotB);
695 CHECK_LABEL(kOrB);
696 CHECK_VECTOR_MATRIX_LABELS(kPop);
697 CHECK_LABEL(kPushImmediate);
698 CHECK_VECTOR_LABELS(kReadExternal);
699 CHECK_VECTOR_LABELS(kRemainderF);
700 CHECK_VECTOR_LABELS(kRemainderS);
701 CHECK_VECTOR_LABELS(kRemainderU);
702 CHECK_LABEL(kReserve);
703 CHECK_LABEL(kReturn);
704 CHECK_LABEL(kScalarToMatrix);
Brian Osman4c2146f2019-09-24 09:39:38 -0400705 CHECK_LABEL(kShiftLeft);
706 CHECK_LABEL(kShiftRightS);
707 CHECK_LABEL(kShiftRightU);
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400708 CHECK_VECTOR_LABELS(kSin);
709 CHECK_VECTOR_LABELS(kSqrt);
710 CHECK_VECTOR_LABELS(kStore);
711 CHECK_VECTOR_LABELS(kStoreGlobal);
712 CHECK_LABEL(kStoreExtended);
713 CHECK_LABEL(kStoreExtendedGlobal);
714 CHECK_LABEL(kStoreSwizzle);
715 CHECK_LABEL(kStoreSwizzleGlobal);
716 CHECK_LABEL(kStoreSwizzleIndirect);
717 CHECK_LABEL(kStoreSwizzleIndirectGlobal);
718 CHECK_LABEL(kSwizzle);
719 CHECK_VECTOR_MATRIX_LABELS(kSubtractF);
720 CHECK_VECTOR_LABELS(kSubtractI);
721 CHECK_VECTOR_LABELS(kTan);
722 CHECK_VECTOR_LABELS(kWriteExternal);
723 CHECK_LABEL(kXorB);
724 CHECK_LABEL(kMaskPush);
725 CHECK_LABEL(kMaskPop);
726 CHECK_LABEL(kMaskNegate);
727 CHECK_LABEL(kMaskBlend);
728 CHECK_LABEL(kBranchIfAllFalse);
729 CHECK_LABEL(kLoopBegin);
730 CHECK_LABEL(kLoopNext);
731 CHECK_LABEL(kLoopMask);
732 CHECK_LABEL(kLoopEnd);
733 CHECK_LABEL(kLoopBreak);
734 CHECK_LABEL(kLoopContinue);
Brian Osman11b877e2019-09-17 10:21:06 -0400735 f->fPreprocessOnce([f] { ((ByteCodeFunction*)f)->preprocess(labels); });
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400736#endif
737
Brian Osman4b202a32019-06-21 09:50:29 -0400738 // Needs to be the first N non-negative integers, at least as large as VecWidth
739 static const Interpreter::I32 gLanes = {
740 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
741 };
742
Brian Osman569f12f2019-06-13 11:23:57 -0400743 VValue* sp = stack + f->fParameterCount + f->fLocalCount - 1;
744
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400745 #define POP() (*(sp--))
746 #define PUSH(v) (sp[1] = v, ++sp)
Mike Kleine7007382019-05-21 08:36:32 -0500747
Brian Osman80164412019-06-07 13:00:23 -0400748 const uint8_t* code = f->fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400749 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400750 std::vector<StackFrame> frames;
751
Brian Osman569f12f2019-06-13 11:23:57 -0400752 I32 condStack[16]; // Independent condition masks
753 I32 maskStack[16]; // Combined masks (eg maskStack[0] & maskStack[1] & ...)
754 I32 contStack[16]; // Continue flags for loops
755 I32 loopStack[16]; // Loop execution masks
Brian Osman4b202a32019-06-21 09:50:29 -0400756 condStack[0] = maskStack[0] = (gLanes < N);
Brian Osman569f12f2019-06-13 11:23:57 -0400757 contStack[0] = I32( 0);
758 loopStack[0] = I32(~0);
759 I32* condPtr = condStack;
760 I32* maskPtr = maskStack;
761 I32* contPtr = contStack;
762 I32* loopPtr = loopStack;
763
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400764 if (f->fConditionCount + 1 > (int)SK_ARRAY_COUNT(condStack) ||
765 f->fLoopCount + 1 > (int)SK_ARRAY_COUNT(loopStack)) {
Brian Osman869a3e82019-07-18 17:00:34 -0400766 return false;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -0400767 }
768
Brian Osman569f12f2019-06-13 11:23:57 -0400769 auto mask = [&]() { return *maskPtr & *loopPtr; };
770
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400771#ifdef SKSLC_THREADED_CODE
772 // If the "labels as values" extension is available, we implement this using threaded code.
773 // Instead of opcodes, the code directly contains the addresses of the labels to jump to. Then
774 // the code for each opcode simply grabs the address of the next opcode and uses a goto to jump
775 // there.
776 NEXT();
777#else
778 // Otherwise, we have to use a switch statement and a loop to execute the right label.
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400779 for (;;) {
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400780 #ifdef TRACE
781 printf("at %3d ", (int) (ip - code));
782 disassemble_instruction(ip);
783 printf(" (stack: %d)\n", (int) (sp - stack) + 1);
784 #endif
785 switch ((ByteCodeInstruction) READ16()) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400786#endif
Brian Osman569f12f2019-06-13 11:23:57 -0400787
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400788 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
789 VECTOR_BINARY_OP(kAddI, fSigned, +)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400790
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400791 // Booleans are integer masks: 0/~0 for false/true. So bitwise ops do what we want:
792 LABEL(kAndB)
793 sp[-1] = sp[-1].fSigned & sp[0].fSigned;
794 POP();
795 NEXT();
796 LABEL(kNotB)
797 sp[0] = ~sp[0].fSigned;
798 NEXT();
799 LABEL(kOrB)
800 sp[-1] = sp[-1].fSigned | sp[0].fSigned;
801 POP();
802 NEXT();
803 LABEL(kXorB)
804 sp[-1] = sp[-1].fSigned ^ sp[0].fSigned;
805 POP();
806 NEXT();
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400807
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400808 LABEL(kBranch)
809 ip = code + READ16();
810 NEXT();
811
812 LABEL(kCall) {
813 // Precursor code reserved space for the return value, and pushed all parameters to
814 // the stack. Update our bottom of stack to point at the first parameter, and our
815 // sp to point past those parameters (plus space for locals).
816 int target = READ8();
817 const ByteCodeFunction* fun = byteCode->fFunctions[target].get();
818#ifdef SKSLC_THREADED_CODE
Brian Osman11b877e2019-09-17 10:21:06 -0400819 fun->fPreprocessOnce([fun] { ((ByteCodeFunction*)fun)->preprocess(labels); });
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400820#endif
821 if (skvx::any(mask())) {
822 frames.push_back({ code, ip, stack, fun->fParameterCount });
823 ip = code = fun->fCode.data();
824 stack = sp - fun->fParameterCount + 1;
825 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
826 }
827 NEXT();
828 }
829
830 LABEL(kCallExternal) {
831 call_external(byteCode, ip, sp, baseIndex, mask());
832 NEXT();
833 }
834
835 LABEL(kClampIndex) {
836 int length = READ8();
837 if (skvx::any(mask() & ((sp[0].fSigned < 0) | (sp[0].fSigned >= length)))) {
838 return false;
839 }
840 NEXT();
841 }
842
843 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
844 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
845 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
846 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
847 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
848 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
849 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
850 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
851 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
852 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
853 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
854 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
855 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
856 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
857 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
858 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
859
860 LABEL(kConvertFtoI4) sp[-3] = skvx::cast<int>(sp[-3].fFloat);
861 LABEL(kConvertFtoI3) sp[-2] = skvx::cast<int>(sp[-2].fFloat);
862 LABEL(kConvertFtoI2) sp[-1] = skvx::cast<int>(sp[-1].fFloat);
863 LABEL(kConvertFtoI) sp[ 0] = skvx::cast<int>(sp[ 0].fFloat);
864 NEXT();
865
866 LABEL(kConvertStoF4) sp[-3] = skvx::cast<float>(sp[-3].fSigned);
867 LABEL(kConvertStoF3) sp[-2] = skvx::cast<float>(sp[-2].fSigned);
868 LABEL(kConvertStoF2) sp[-1] = skvx::cast<float>(sp[-1].fSigned);
869 LABEL(kConvertStoF) sp[ 0] = skvx::cast<float>(sp[ 0].fSigned);
870 NEXT();
871
872 LABEL(kConvertUtoF4) sp[-3] = skvx::cast<float>(sp[-3].fUnsigned);
873 LABEL(kConvertUtoF3) sp[-2] = skvx::cast<float>(sp[-2].fUnsigned);
874 LABEL(kConvertUtoF2) sp[-1] = skvx::cast<float>(sp[-1].fUnsigned);
875 LABEL(kConvertUtoF) sp[ 0] = skvx::cast<float>(sp[ 0].fUnsigned);
876 NEXT();
877
878 VECTOR_UNARY_FN_VEC(kCos, cosf)
879
Ethan Nicholas6f624122019-09-24 13:07:06 -0400880 VECTOR_BINARY_MASKED_OP(kDivideS, fSigned, /)
881 VECTOR_BINARY_MASKED_OP(kDivideU, fUnsigned, /)
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400882 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
883
884 LABEL(kDup4) PUSH(sp[1 - ip[0]]);
885 LABEL(kDup3) PUSH(sp[1 - ip[0]]);
886 LABEL(kDup2) PUSH(sp[1 - ip[0]]);
887 LABEL(kDup) PUSH(sp[1 - ip[0]]);
888 ++ip;
889 NEXT();
890
891 LABEL(kDupN) {
892 int count = READ8();
893 memcpy(sp + 1, sp - count + 1, count * sizeof(VValue));
894 sp += count;
895 NEXT();
896 }
897
898 LABEL(kInverse2x2) {
899 inverse2x2(sp);
900 NEXT();
901 }
902 LABEL(kInverse3x3) {
903 inverse3x3(sp);
904 NEXT();
905 }
906 LABEL(kInverse4x4) {
907 inverse4x4(sp);
908 NEXT();
909 }
910
911 LABEL(kLoad4) sp[4] = stack[ip[1] + 3];
912 LABEL(kLoad3) sp[3] = stack[ip[1] + 2];
913 LABEL(kLoad2) sp[2] = stack[ip[1] + 1];
914 LABEL(kLoad) sp[1] = stack[ip[1] + 0];
915 sp += ip[0];
916 ip += 2;
917 NEXT();
918
919 LABEL(kLoadGlobal4) sp[4] = globals[ip[1] + 3];
920 LABEL(kLoadGlobal3) sp[3] = globals[ip[1] + 2];
921 LABEL(kLoadGlobal2) sp[2] = globals[ip[1] + 1];
922 LABEL(kLoadGlobal) sp[1] = globals[ip[1] + 0];
923 sp += ip[0];
924 ip += 2;
925 NEXT();
926
927 LABEL(kLoadExtended) {
928 int count = READ8();
929 I32 src = POP().fSigned;
930 I32 m = mask();
931 for (int i = 0; i < count; ++i) {
932 for (int j = 0; j < VecWidth; ++j) {
933 if (m[j]) {
934 sp[i + 1].fSigned[j] = stack[src[j] + i].fSigned[j];
Brian Osman569f12f2019-06-13 11:23:57 -0400935 }
Brian Osman226668a2019-05-14 16:47:30 -0400936 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400937 }
938 sp += count;
939 NEXT();
940 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400941
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400942 LABEL(kLoadExtendedGlobal) {
943 int count = READ8();
944 I32 src = POP().fSigned;
945 I32 m = mask();
946 for (int i = 0; i < count; ++i) {
947 for (int j = 0; j < VecWidth; ++j) {
948 if (m[j]) {
949 sp[i + 1].fSigned[j] = globals[src[j] + i].fSigned[j];
950 }
951 }
952 }
953 sp += count;
954 NEXT();
955 }
Mike Kleine7007382019-05-21 08:36:32 -0500956
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400957 LABEL(kLoadSwizzle) {
958 int src = READ8();
959 int count = READ8();
960 for (int i = 0; i < count; ++i) {
961 PUSH(stack[src + *(ip + i)]);
962 }
963 ip += count;
964 NEXT();
965 }
Brian Osman569f12f2019-06-13 11:23:57 -0400966
Ethan Nicholasc70027b2019-09-05 16:50:52 -0400967 LABEL(kLoadSwizzleGlobal) {
968 int src = READ8();
969 int count = READ8();
970 for (int i = 0; i < count; ++i) {
971 PUSH(globals[src + *(ip + i)]);
972 }
973 ip += count;
974 NEXT();
975 }
976
977 LABEL(kMatrixToMatrix) {
978 int srcCols = READ8();
979 int srcRows = READ8();
980 int dstCols = READ8();
981 int dstRows = READ8();
982 SkASSERT(srcCols >= 2 && srcCols <= 4);
983 SkASSERT(srcRows >= 2 && srcRows <= 4);
984 SkASSERT(dstCols >= 2 && dstCols <= 4);
985 SkASSERT(dstRows >= 2 && dstRows <= 4);
986 F32 tmp[16];
987 memset(tmp, 0, sizeof(tmp));
988 tmp[0] = tmp[5] = tmp[10] = tmp[15] = F32(1.0f);
989 for (int c = srcCols - 1; c >= 0; --c) {
990 for (int r = srcRows - 1; r >= 0; --r) {
991 tmp[c*4 + r] = POP().fFloat;
992 }
993 }
994 for (int c = 0; c < dstCols; ++c) {
995 for (int r = 0; r < dstRows; ++r) {
996 PUSH(tmp[c*4 + r]);
997 }
998 }
999 NEXT();
1000 }
1001
1002 LABEL(kMatrixMultiply) {
1003 int lCols = READ8();
1004 int lRows = READ8();
1005 int rCols = READ8();
1006 int rRows = lCols;
1007 F32 tmp[16] = { 0.0f };
1008 F32* B = &(sp - (rCols * rRows) + 1)->fFloat;
1009 F32* A = B - (lCols * lRows);
1010 for (int c = 0; c < rCols; ++c) {
1011 for (int r = 0; r < lRows; ++r) {
1012 for (int j = 0; j < lCols; ++j) {
1013 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
1014 }
1015 }
1016 }
1017 sp -= (lCols * lRows) + (rCols * rRows);
1018 memcpy(sp + 1, tmp, rCols * lRows * sizeof(VValue));
1019 sp += (rCols * lRows);
1020 NEXT();
1021 }
1022
1023 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
1024 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
1025
1026 LABEL(kNegateF4) sp[-3] = -sp[-3].fFloat;
1027 LABEL(kNegateF3) sp[-2] = -sp[-2].fFloat;
1028 LABEL(kNegateF2) sp[-1] = -sp[-1].fFloat;
1029 LABEL(kNegateF) sp[ 0] = -sp[ 0].fFloat;
1030 NEXT();
1031
1032 LABEL(kNegateFN) {
1033 int count = READ8();
1034 for (int i = count - 1; i >= 0; --i) {
1035 sp[-i] = -sp[-i].fFloat;
1036 }
1037 NEXT();
1038 }
1039
1040 LABEL(kNegateI4) sp[-3] = -sp[-3].fSigned;
1041 LABEL(kNegateI3) sp[-2] = -sp[-2].fSigned;
1042 LABEL(kNegateI2) sp[-1] = -sp[-1].fSigned;
1043 LABEL(kNegateI) sp[ 0] = -sp[ 0].fSigned;
1044 NEXT();
1045
1046 LABEL(kPop4) POP();
1047 LABEL(kPop3) POP();
1048 LABEL(kPop2) POP();
1049 LABEL(kPop) POP();
1050 NEXT();
1051
1052 LABEL(kPopN)
1053 sp -= READ8();
1054 NEXT();
1055
1056 LABEL(kPushImmediate)
1057 PUSH(U32(READ32()));
1058 NEXT();
1059
1060 LABEL(kReadExternal)
1061 LABEL(kReadExternal2)
1062 LABEL(kReadExternal3)
1063 LABEL(kReadExternal4) {
1064 int count = READ8();
1065 int src = READ8();
1066 float tmp[4];
1067 I32 m = mask();
1068 for (int i = 0; i < VecWidth; ++i) {
1069 if (m[i]) {
1070 byteCode->fExternalValues[src]->read(baseIndex + i, tmp);
1071 for (int j = 0; j < count; ++j) {
1072 sp[j + 1].fFloat[i] = tmp[j];
1073 }
1074 }
1075 }
1076 sp += count;
1077 NEXT();
1078 }
1079
1080 VECTOR_BINARY_FN(kRemainderF, fFloat, vec_mod<F32>)
Ethan Nicholas6f624122019-09-24 13:07:06 -04001081 VECTOR_BINARY_MASKED_OP(kRemainderS, fSigned, %)
1082 VECTOR_BINARY_MASKED_OP(kRemainderU, fUnsigned, %)
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001083
1084 LABEL(kReserve)
1085 sp += READ8();
1086 NEXT();
1087
1088 LABEL(kReturn) {
1089 int count = READ8();
1090 if (frames.empty()) {
1091 if (outReturn) {
1092 VValue* src = sp - count + 1;
1093 if (stripedOutput) {
1094 for (int i = 0; i < count; ++i) {
1095 memcpy(outReturn[i], &src->fFloat, N * sizeof(float));
1096 ++src;
Brian Osman569f12f2019-06-13 11:23:57 -04001097 }
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001098 } else {
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001099 float* outPtr = outReturn[0];
1100 for (int i = 0; i < count; ++i) {
1101 for (int j = 0; j < N; ++j) {
1102 outPtr[count * j] = src->fFloat[j];
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001103 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001104 ++outPtr;
1105 ++src;
Ethan Nicholascbdc8292019-09-05 18:06:38 +00001106 }
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001107 }
1108 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001109 return true;
1110 } else {
1111 // When we were called, the caller reserved stack space for their copy of our
1112 // return value, then 'stack' was positioned after that, where our parameters
1113 // were placed. Copy our return values to their reserved area.
1114 memcpy(stack - count, sp - count + 1, count * sizeof(VValue));
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001115
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001116 // Now move the stack pointer to the end of the passed-in parameters. This odd
1117 // calling convention requires the caller to pop the arguments after calling,
1118 // but allows them to store any out-parameters back during that unwinding.
1119 // After that sequence finishes, the return value will be the top of the stack.
1120 const StackFrame& frame(frames.back());
1121 sp = stack + frame.fParameterCount - 1;
1122 stack = frame.fStack;
1123 code = frame.fCode;
1124 ip = frame.fIP;
1125 frames.pop_back();
1126 NEXT();
Ethan Nicholasadecf4b2019-09-05 12:43:34 -04001127 }
1128 }
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001129
1130 LABEL(kScalarToMatrix) {
1131 int cols = READ8();
1132 int rows = READ8();
1133 VValue v = POP();
1134 for (int c = 0; c < cols; ++c) {
1135 for (int r = 0; r < rows; ++r) {
1136 PUSH(c == r ? v : F32(0.0f));
1137 }
1138 }
1139 NEXT();
1140 }
1141
Brian Osman4c2146f2019-09-24 09:39:38 -04001142 LABEL(kShiftLeft)
1143 sp[0] = sp[0].fSigned << READ8();
1144 NEXT();
1145 LABEL(kShiftRightS)
1146 sp[0] = sp[0].fSigned >> READ8();
1147 NEXT();
1148 LABEL(kShiftRightU)
1149 sp[0] = sp[0].fUnsigned >> READ8();
1150 NEXT();
1151
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001152 VECTOR_UNARY_FN_VEC(kSin, sinf)
1153 VECTOR_UNARY_FN(kSqrt, skvx::sqrt, fFloat)
1154
1155 LABEL(kStore4)
1156 stack[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+3].fFloat);
1157 LABEL(kStore3)
1158 stack[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+2].fFloat);
1159 LABEL(kStore2)
1160 stack[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+1].fFloat);
1161 LABEL(kStore)
1162 stack[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, stack[*ip+0].fFloat);
1163 ++ip;
1164 NEXT();
1165
1166 LABEL(kStoreGlobal4)
1167 globals[*ip+3] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+3].fFloat);
1168 LABEL(kStoreGlobal3)
1169 globals[*ip+2] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+2].fFloat);
1170 LABEL(kStoreGlobal2)
1171 globals[*ip+1] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+1].fFloat);
1172 LABEL(kStoreGlobal)
1173 globals[*ip+0] = skvx::if_then_else(mask(), POP().fFloat, globals[*ip+0].fFloat);
1174 ++ip;
1175 NEXT();
1176
1177 LABEL(kStoreExtended) {
1178 int count = READ8();
1179 I32 target = POP().fSigned;
1180 VValue* src = sp - count + 1;
1181 I32 m = mask();
1182 for (int i = 0; i < count; ++i) {
1183 for (int j = 0; j < VecWidth; ++j) {
1184 if (m[j]) {
1185 stack[target[j] + i].fSigned[j] = src[i].fSigned[j];
1186 }
1187 }
1188 }
1189 sp -= count;
1190 NEXT();
1191 }
1192 LABEL(kStoreExtendedGlobal) {
1193 int count = READ8();
1194 I32 target = POP().fSigned;
1195 VValue* src = sp - count + 1;
1196 I32 m = mask();
1197 for (int i = 0; i < count; ++i) {
1198 for (int j = 0; j < VecWidth; ++j) {
1199 if (m[j]) {
1200 globals[target[j] + i].fSigned[j] = src[i].fSigned[j];
1201 }
1202 }
1203 }
1204 sp -= count;
1205 NEXT();
1206 }
1207
1208 LABEL(kStoreSwizzle) {
1209 int target = READ8();
1210 int count = READ8();
1211 for (int i = count - 1; i >= 0; --i) {
1212 stack[target + *(ip + i)] = skvx::if_then_else(
1213 mask(), POP().fFloat, stack[target + *(ip + i)].fFloat);
1214 }
1215 ip += count;
1216 NEXT();
1217 }
1218
1219 LABEL(kStoreSwizzleGlobal) {
1220 int target = READ8();
1221 int count = READ8();
1222 for (int i = count - 1; i >= 0; --i) {
1223 globals[target + *(ip + i)] = skvx::if_then_else(
1224 mask(), POP().fFloat, globals[target + *(ip + i)].fFloat);
1225 }
1226 ip += count;
1227 NEXT();
1228 }
1229
1230 LABEL(kStoreSwizzleIndirect) {
1231 int count = READ8();
1232 I32 target = POP().fSigned;
1233 I32 m = mask();
1234 for (int i = count - 1; i >= 0; --i) {
1235 I32 v = POP().fSigned;
1236 for (int j = 0; j < VecWidth; ++j) {
1237 if (m[j]) {
1238 stack[target[j] + *(ip + i)].fSigned[j] = v[j];
1239 }
1240 }
1241 }
1242 ip += count;
1243 NEXT();
1244 }
1245
1246 LABEL(kStoreSwizzleIndirectGlobal) {
1247 int count = READ8();
1248 I32 target = POP().fSigned;
1249 I32 m = mask();
1250 for (int i = count - 1; i >= 0; --i) {
1251 I32 v = POP().fSigned;
1252 for (int j = 0; j < VecWidth; ++j) {
1253 if (m[j]) {
1254 globals[target[j] + *(ip + i)].fSigned[j] = v[j];
1255 }
1256 }
1257 }
1258 ip += count;
1259 NEXT();
1260 }
1261
1262 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
1263 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
1264
1265 LABEL(kSwizzle) {
1266 VValue tmp[4];
1267 for (int i = READ8() - 1; i >= 0; --i) {
1268 tmp[i] = POP();
1269 }
1270 for (int i = READ8() - 1; i >= 0; --i) {
1271 PUSH(tmp[READ8()]);
1272 }
1273 NEXT();
1274 }
1275
1276 VECTOR_UNARY_FN_VEC(kTan, tanf)
1277
1278 LABEL(kWriteExternal4)
1279 LABEL(kWriteExternal3)
1280 LABEL(kWriteExternal2)
1281 LABEL(kWriteExternal) {
1282 int count = READ8();
1283 int target = READ8();
1284 float tmp[4];
1285 I32 m = mask();
1286 sp -= count;
1287 for (int i = 0; i < VecWidth; ++i) {
1288 if (m[i]) {
1289 for (int j = 0; j < count; ++j) {
1290 tmp[j] = sp[j + 1].fFloat[i];
1291 }
1292 byteCode->fExternalValues[target]->write(baseIndex + i, tmp);
1293 }
1294 }
1295 NEXT();
1296 }
1297
1298 LABEL(kMaskPush)
1299 condPtr[1] = POP().fSigned;
1300 maskPtr[1] = maskPtr[0] & condPtr[1];
1301 ++condPtr; ++maskPtr;
1302 NEXT();
1303 LABEL(kMaskPop)
1304 --condPtr; --maskPtr;
1305 NEXT();
1306 LABEL(kMaskNegate)
1307 maskPtr[0] = maskPtr[-1] & ~condPtr[0];
1308 NEXT();
1309 LABEL(kMaskBlend) {
1310 int count = READ8();
1311 I32 m = condPtr[0];
1312 --condPtr; --maskPtr;
1313 for (int i = 0; i < count; ++i) {
1314 sp[-count] = skvx::if_then_else(m, sp[-count].fFloat, sp[0].fFloat);
1315 --sp;
1316 }
1317 NEXT();
1318 }
1319 LABEL(kBranchIfAllFalse) {
1320 int target = READ16();
1321 if (!skvx::any(mask())) {
1322 ip = code + target;
1323 }
1324 NEXT();
1325 }
1326
1327 LABEL(kLoopBegin)
1328 contPtr[1] = 0;
1329 loopPtr[1] = loopPtr[0];
1330 ++contPtr; ++loopPtr;
1331 NEXT();
1332 LABEL(kLoopNext)
1333 *loopPtr |= *contPtr;
1334 *contPtr = 0;
1335 NEXT();
1336 LABEL(kLoopMask)
1337 *loopPtr &= POP().fSigned;
1338 NEXT();
1339 LABEL(kLoopEnd)
1340 --contPtr; --loopPtr;
1341 NEXT();
1342 LABEL(kLoopBreak)
1343 *loopPtr &= ~mask();
1344 NEXT();
1345 LABEL(kLoopContinue) {
1346 I32 m = mask();
1347 *contPtr |= m;
1348 *loopPtr &= ~m;
1349 NEXT();
1350 }
1351#ifdef SKSLC_THREADED_CODE
1352 #ifdef TRACE
1353 next:
1354 printf("at %3d (stack: %d) (disable threaded code for disassembly)\n",
1355 (int) (ip - code), (int) (sp - stack) + 1);
1356 goto *READ_INST();
1357 #endif
1358#else
1359 }
1360 }
1361#endif
Brian Osman569f12f2019-06-13 11:23:57 -04001362}
1363
Brian Osman08a84962019-06-14 10:17:16 -04001364} // namespace Interpreter
1365
Brian Osman489cf882019-07-09 10:48:28 -04001366#endif // SK_ENABLE_SKSL_INTERPRETER
1367
Mike Reed634c9412019-07-18 13:20:04 -04001368#undef spf
1369
Brian Osman08a84962019-06-14 10:17:16 -04001370void ByteCodeFunction::disassemble() const {
Brian Osman489cf882019-07-09 10:48:28 -04001371#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman08a84962019-06-14 10:17:16 -04001372 const uint8_t* ip = fCode.data();
1373 while (ip < fCode.data() + fCode.size()) {
1374 printf("%d: ", (int)(ip - fCode.data()));
1375 ip = Interpreter::disassemble_instruction(ip);
1376 printf("\n");
1377 }
Brian Osman489cf882019-07-09 10:48:28 -04001378#endif
Brian Osman08a84962019-06-14 10:17:16 -04001379}
1380
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001381#define VECTOR_PREPROCESS(base) \
1382 case ByteCodeInstruction::base ## 4: \
1383 case ByteCodeInstruction::base ## 3: \
1384 case ByteCodeInstruction::base ## 2: \
1385 case ByteCodeInstruction::base: READ8(); break;
1386
1387#define VECTOR_PREPROCESS_NO_COUNT(base) \
1388 case ByteCodeInstruction::base ## 4: \
1389 case ByteCodeInstruction::base ## 3: \
1390 case ByteCodeInstruction::base ## 2: \
1391 case ByteCodeInstruction::base: break;
1392
1393#define VECTOR_MATRIX_PREPROCESS(base) \
1394 VECTOR_PREPROCESS(base) \
1395 case ByteCodeInstruction::base ## N: READ8(); break;
1396
1397#define VECTOR_MATRIX_PREPROCESS_NO_COUNT(base) \
1398 VECTOR_PREPROCESS_NO_COUNT(base) \
1399 case ByteCodeInstruction::base ## N: READ8(); break;
1400
1401void ByteCodeFunction::preprocess(const void* labels[]) {
1402#if defined(SK_ENABLE_SKSL_INTERPRETER)
1403#ifdef TRACE
1404 this->disassemble();
1405#endif
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001406 uint8_t* ip = fCode.data();
1407 while (ip < fCode.data() + fCode.size()) {
1408 ByteCodeInstruction inst = (ByteCodeInstruction) (intptr_t) READ_INST();
1409 const void* label = labels[(int) inst];
1410 memcpy(ip - sizeof(instruction), &label, sizeof(label));
1411 switch (inst) {
1412 VECTOR_MATRIX_PREPROCESS(kAddF)
1413 VECTOR_PREPROCESS(kAddI)
1414 case ByteCodeInstruction::kAndB: break;
1415 case ByteCodeInstruction::kBranch: READ16(); break;
1416 case ByteCodeInstruction::kCall: READ8(); break;
1417 case ByteCodeInstruction::kCallExternal: {
1418 READ8();
1419 READ8();
1420 READ8();
1421 break;
1422 }
1423 case ByteCodeInstruction::kClampIndex: READ8(); break;
1424 VECTOR_PREPROCESS(kCompareIEQ)
1425 VECTOR_PREPROCESS(kCompareINEQ)
1426 VECTOR_MATRIX_PREPROCESS(kCompareFEQ)
1427 VECTOR_MATRIX_PREPROCESS(kCompareFNEQ)
1428 VECTOR_PREPROCESS(kCompareFGT)
1429 VECTOR_PREPROCESS(kCompareFGTEQ)
1430 VECTOR_PREPROCESS(kCompareFLT)
1431 VECTOR_PREPROCESS(kCompareFLTEQ)
1432 VECTOR_PREPROCESS(kCompareSGT)
1433 VECTOR_PREPROCESS(kCompareSGTEQ)
1434 VECTOR_PREPROCESS(kCompareSLT)
1435 VECTOR_PREPROCESS(kCompareSLTEQ)
1436 VECTOR_PREPROCESS(kCompareUGT)
1437 VECTOR_PREPROCESS(kCompareUGTEQ)
1438 VECTOR_PREPROCESS(kCompareULT)
1439 VECTOR_PREPROCESS(kCompareULTEQ)
1440 VECTOR_PREPROCESS_NO_COUNT(kConvertFtoI)
1441 VECTOR_PREPROCESS_NO_COUNT(kConvertStoF)
1442 VECTOR_PREPROCESS_NO_COUNT(kConvertUtoF)
1443 VECTOR_PREPROCESS(kCos)
1444 VECTOR_MATRIX_PREPROCESS(kDivideF)
1445 VECTOR_PREPROCESS(kDivideS)
1446 VECTOR_PREPROCESS(kDivideU)
1447 VECTOR_MATRIX_PREPROCESS(kDup)
1448
1449 case ByteCodeInstruction::kInverse2x2:
1450 case ByteCodeInstruction::kInverse3x3:
1451 case ByteCodeInstruction::kInverse4x4: break;
1452
1453 case ByteCodeInstruction::kLoad:
1454 case ByteCodeInstruction::kLoad2:
1455 case ByteCodeInstruction::kLoad3:
1456 case ByteCodeInstruction::kLoad4:
1457 case ByteCodeInstruction::kLoadGlobal:
1458 case ByteCodeInstruction::kLoadGlobal2:
1459 case ByteCodeInstruction::kLoadGlobal3:
1460 case ByteCodeInstruction::kLoadGlobal4: READ16(); break;
1461
1462 case ByteCodeInstruction::kLoadSwizzle:
1463 case ByteCodeInstruction::kLoadSwizzleGlobal: {
1464 READ8();
1465 int count = READ8();
1466 ip += count;
1467 break;
1468 }
1469
1470 case ByteCodeInstruction::kLoadExtended:
1471 case ByteCodeInstruction::kLoadExtendedGlobal:
1472 READ8();
1473 break;
1474
1475 case ByteCodeInstruction::kMatrixToMatrix: {
1476 READ8();
1477 READ8();
1478 READ8();
1479 READ8();
1480 break;
1481 }
1482 case ByteCodeInstruction::kMatrixMultiply: {
1483 READ8();
1484 READ8();
1485 READ8();
1486 break;
1487 }
1488 VECTOR_MATRIX_PREPROCESS(kMultiplyF)
1489 VECTOR_PREPROCESS(kMultiplyI)
1490 VECTOR_MATRIX_PREPROCESS_NO_COUNT(kNegateF)
1491 VECTOR_PREPROCESS_NO_COUNT(kNegateI)
1492 case ByteCodeInstruction::kNotB: break;
1493 case ByteCodeInstruction::kOrB: break;
1494 VECTOR_MATRIX_PREPROCESS_NO_COUNT(kPop)
1495 case ByteCodeInstruction::kPushImmediate: READ32(); break;
1496
1497 case ByteCodeInstruction::kReadExternal:
1498 case ByteCodeInstruction::kReadExternal2:
1499 case ByteCodeInstruction::kReadExternal3:
1500 case ByteCodeInstruction::kReadExternal4: READ16(); break;
1501
1502 VECTOR_PREPROCESS(kRemainderF)
1503 VECTOR_PREPROCESS(kRemainderS)
1504 VECTOR_PREPROCESS(kRemainderU)
1505 case ByteCodeInstruction::kReserve: READ8(); break;
1506 case ByteCodeInstruction::kReturn: READ8(); break;
1507 case ByteCodeInstruction::kScalarToMatrix: READ8(); READ8(); break;
Brian Osman4c2146f2019-09-24 09:39:38 -04001508 case ByteCodeInstruction::kShiftLeft: READ8(); break;
1509 case ByteCodeInstruction::kShiftRightS: READ8(); break;
1510 case ByteCodeInstruction::kShiftRightU: READ8(); break;
Ethan Nicholasc70027b2019-09-05 16:50:52 -04001511 VECTOR_PREPROCESS(kSin)
1512 VECTOR_PREPROCESS_NO_COUNT(kSqrt)
1513
1514 case ByteCodeInstruction::kStore:
1515 case ByteCodeInstruction::kStore2:
1516 case ByteCodeInstruction::kStore3:
1517 case ByteCodeInstruction::kStore4:
1518 case ByteCodeInstruction::kStoreGlobal:
1519 case ByteCodeInstruction::kStoreGlobal2:
1520 case ByteCodeInstruction::kStoreGlobal3:
1521 case ByteCodeInstruction::kStoreGlobal4: READ8(); break;
1522
1523 case ByteCodeInstruction::kStoreSwizzle:
1524 case ByteCodeInstruction::kStoreSwizzleGlobal: {
1525 READ8();
1526 int count = READ8();
1527 ip += count;
1528 break;
1529 }
1530
1531 case ByteCodeInstruction::kStoreSwizzleIndirect:
1532 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
1533 int count = READ8();
1534 ip += count;
1535 break;
1536 }
1537
1538 case ByteCodeInstruction::kStoreExtended: READ8(); break;
1539 case ByteCodeInstruction::kStoreExtendedGlobal: READ8(); break;
1540
1541 VECTOR_MATRIX_PREPROCESS(kSubtractF)
1542 VECTOR_PREPROCESS(kSubtractI)
1543
1544 case ByteCodeInstruction::kSwizzle: {
1545 READ8();
1546 int count = READ8();
1547 ip += count;
1548 break;
1549 }
1550 VECTOR_PREPROCESS(kTan)
1551 case ByteCodeInstruction::kWriteExternal:
1552 case ByteCodeInstruction::kWriteExternal2:
1553 case ByteCodeInstruction::kWriteExternal3:
1554 case ByteCodeInstruction::kWriteExternal4: READ16(); break;
1555
1556 case ByteCodeInstruction::kXorB: break;
1557 case ByteCodeInstruction::kMaskPush: break;
1558 case ByteCodeInstruction::kMaskPop: break;
1559 case ByteCodeInstruction::kMaskNegate: break;
1560 case ByteCodeInstruction::kMaskBlend: READ8(); break;
1561 case ByteCodeInstruction::kBranchIfAllFalse: READ16(); break;
1562 case ByteCodeInstruction::kLoopBegin: break;
1563 case ByteCodeInstruction::kLoopNext: break;
1564 case ByteCodeInstruction::kLoopMask: break;
1565 case ByteCodeInstruction::kLoopEnd: break;
1566 case ByteCodeInstruction::kLoopContinue: break;
1567 case ByteCodeInstruction::kLoopBreak: break;
1568 default:
1569 ip -= 2;
1570 printf("unknown(%d)\n", READ16());
1571 SkASSERT(false);
1572 }
1573 }
1574#endif
1575}
1576
Brian Osman869a3e82019-07-18 17:00:34 -04001577bool ByteCode::run(const ByteCodeFunction* f, float* args, float* outReturn, int N,
Brian Osman08a84962019-06-14 10:17:16 -04001578 const float* uniforms, int uniformCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001579#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman4b202a32019-06-21 09:50:29 -04001580 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001581 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1582 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman869a3e82019-07-18 17:00:34 -04001583 return false;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001584 }
Brian Osmanef787f72019-06-13 13:07:12 -04001585
Brian Osman869a3e82019-07-18 17:00:34 -04001586 if (uniformCount != (int)fInputSlots.size()) {
1587 return false;
1588 }
1589
Brian Osman08a84962019-06-14 10:17:16 -04001590 Interpreter::VValue globals[32];
Brian Osman869a3e82019-07-18 17:00:34 -04001591 if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
1592 return false;
1593 }
Brian Osman08a84962019-06-14 10:17:16 -04001594 for (uint8_t slot : fInputSlots) {
1595 globals[slot].fFloat = *uniforms++;
Brian Osman569f12f2019-06-13 11:23:57 -04001596 }
1597
Brian Osman1a79f0b2019-06-24 16:32:14 -04001598 int baseIndex = 0;
1599
Brian Osman569f12f2019-06-13 11:23:57 -04001600 while (N) {
Brian Osman08a84962019-06-14 10:17:16 -04001601 int w = std::min(N, Interpreter::VecWidth);
Brian Osman569f12f2019-06-13 11:23:57 -04001602
1603 // Transpose args into stack
1604 {
Brian Osman08a84962019-06-14 10:17:16 -04001605 float* src = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001606 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001607 float* dst = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001608 for (int j = f->fParameterCount; j > 0; --j) {
1609 *dst = *src++;
Brian Osman08a84962019-06-14 10:17:16 -04001610 dst += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001611 }
1612 }
1613 }
1614
Mike Reed3fd3cc92019-06-20 12:40:30 -04001615 bool stripedOutput = false;
1616 float** outArray = outReturn ? &outReturn : nullptr;
Brian Osman869a3e82019-07-18 17:00:34 -04001617 if (!innerRun(this, f, stack, outArray, globals, stripedOutput, w, baseIndex)) {
1618 return false;
1619 }
Brian Osman569f12f2019-06-13 11:23:57 -04001620
1621 // Transpose out parameters back
1622 {
Brian Osman08a84962019-06-14 10:17:16 -04001623 float* dst = args;
Brian Osman569f12f2019-06-13 11:23:57 -04001624 for (int i = 0; i < w; ++i) {
Brian Osman08a84962019-06-14 10:17:16 -04001625 float* src = (float*)stack + i;
Brian Osman569f12f2019-06-13 11:23:57 -04001626 for (const auto& p : f->fParameters) {
1627 if (p.fIsOutParameter) {
1628 for (int j = p.fSlotCount; j > 0; --j) {
1629 *dst++ = *src;
Brian Osman08a84962019-06-14 10:17:16 -04001630 src += Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001631 }
1632 } else {
1633 dst += p.fSlotCount;
Brian Osman08a84962019-06-14 10:17:16 -04001634 src += p.fSlotCount * Interpreter::VecWidth;
Brian Osman569f12f2019-06-13 11:23:57 -04001635 }
1636 }
1637 }
1638 }
1639
1640 args += f->fParameterCount * w;
Mike Reed3fd3cc92019-06-20 12:40:30 -04001641 if (outReturn) {
1642 outReturn += f->fReturnCount * w;
1643 }
Brian Osman4b202a32019-06-21 09:50:29 -04001644 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001645 baseIndex += w;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -04001646 }
Brian Osman869a3e82019-07-18 17:00:34 -04001647 return true;
Brian Osman489cf882019-07-09 10:48:28 -04001648#else
1649 SkDEBUGFAIL("ByteCode interpreter not enabled");
Brian Osman869a3e82019-07-18 17:00:34 -04001650 return false;
Brian Osman489cf882019-07-09 10:48:28 -04001651#endif
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001652}
1653
Brian Osman869a3e82019-07-18 17:00:34 -04001654bool ByteCode::runStriped(const ByteCodeFunction* f, float* args[], int nargs, int N,
Mike Reed3fd3cc92019-06-20 12:40:30 -04001655 const float* uniforms, int uniformCount,
1656 float* outArgs[], int outCount) const {
Brian Osman489cf882019-07-09 10:48:28 -04001657#if defined(SK_ENABLE_SKSL_INTERPRETER)
Brian Osman2b1a5442019-06-19 11:40:33 -04001658 Interpreter::VValue stack[128];
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001659 int stackNeeded = f->fParameterCount + f->fLocalCount + f->fStackCount;
1660 if (stackNeeded > (int)SK_ARRAY_COUNT(stack)) {
Brian Osman869a3e82019-07-18 17:00:34 -04001661 return false;
1662 }
1663
1664 if (nargs != f->fParameterCount ||
1665 outCount != f->fReturnCount ||
1666 uniformCount != (int)fInputSlots.size()) {
1667 return false;
1668 }
1669
1670 Interpreter::VValue globals[32];
1671 if (fGlobalCount > (int)SK_ARRAY_COUNT(globals)) {
1672 return false;
1673 }
1674 for (uint8_t slot : fInputSlots) {
1675 globals[slot].fFloat = *uniforms++;
Brian Osmanaa2ca3f2019-07-15 13:24:48 -04001676 }
Brian Osman2b1a5442019-06-19 11:40:33 -04001677
Mike Reed3fd3cc92019-06-20 12:40:30 -04001678 // innerRun just takes outArgs, so clear it if the count is zero
1679 if (outCount == 0) {
1680 outArgs = nullptr;
1681 }
1682
Brian Osman1a79f0b2019-06-24 16:32:14 -04001683 int baseIndex = 0;
1684
Brian Osman2b1a5442019-06-19 11:40:33 -04001685 while (N) {
1686 int w = std::min(N, Interpreter::VecWidth);
1687
1688 // Copy args into stack
1689 for (int i = 0; i < nargs; ++i) {
1690 memcpy(stack + i, args[i], w * sizeof(float));
1691 }
1692
Mike Reed3fd3cc92019-06-20 12:40:30 -04001693 bool stripedOutput = true;
Brian Osman869a3e82019-07-18 17:00:34 -04001694 if (!innerRun(this, f, stack, outArgs, globals, stripedOutput, w, baseIndex)) {
1695 return false;
1696 }
Brian Osman2b1a5442019-06-19 11:40:33 -04001697
1698 // Copy out parameters back
1699 int slot = 0;
1700 for (const auto& p : f->fParameters) {
1701 if (p.fIsOutParameter) {
1702 for (int i = slot; i < slot + p.fSlotCount; ++i) {
1703 memcpy(args[i], stack + i, w * sizeof(float));
1704 }
1705 }
1706 slot += p.fSlotCount;
1707 }
1708
1709 // Step each argument pointer ahead
1710 for (int i = 0; i < nargs; ++i) {
1711 args[i] += w;
1712 }
1713 N -= w;
Brian Osman1a79f0b2019-06-24 16:32:14 -04001714 baseIndex += w;
Brian Osman2b1a5442019-06-19 11:40:33 -04001715 }
Brian Osman869a3e82019-07-18 17:00:34 -04001716
1717 return true;
Brian Osman489cf882019-07-09 10:48:28 -04001718#else
1719 SkDEBUGFAIL("ByteCode interpreter not enabled");
Brian Osman869a3e82019-07-18 17:00:34 -04001720 return false;
Brian Osman489cf882019-07-09 10:48:28 -04001721#endif
Brian Osman2b1a5442019-06-19 11:40:33 -04001722}
1723
Brian Osman80164412019-06-07 13:00:23 -04001724} // namespace SkSL
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001725
1726#endif