blob: 5a85acd1f864a3a35544d9b7df22c901fc6fe57d [file] [log] [blame]
Mike Kleine1caee12017-02-15 13:31:12 -05001/*
Mike Klein1b9b7d52018-02-27 10:37:40 -05002 * Copyright 2018 Google Inc.
Mike Kleine1caee12017-02-15 13:31:12 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Klein1b9b7d52018-02-27 10:37:40 -05008#ifndef SkRasterPipeline_opts_DEFINED
9#define SkRasterPipeline_opts_DEFINED
Mike Kleinb9c4a6f2017-04-03 13:54:55 -040010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
Mike Klein7a177b42019-06-17 17:17:47 -050012#include "src/core/SkUtils.h" // unaligned_{load,store}
Brian Osman2b1a5442019-06-19 11:40:33 -040013#include "src/sksl/SkSLByteCode.h"
Mike Kleinb11ab572018-10-24 06:42:14 -040014
15// Every function in this file should be marked static and inline using SI.
16#if defined(__clang__)
17 #define SI __attribute__((always_inline)) static inline
18#else
19 #define SI static inline
20#endif
21
Mike Kleinb11ab572018-10-24 06:42:14 -040022template <typename Dst, typename Src>
23SI Dst bit_cast(const Src& src) {
24 static_assert(sizeof(Dst) == sizeof(Src), "");
Mike Klein7a177b42019-06-17 17:17:47 -050025 return sk_unaligned_load<Dst>(&src);
Mike Kleinb11ab572018-10-24 06:42:14 -040026}
27
28template <typename Dst, typename Src>
29SI Dst widen_cast(const Src& src) {
30 static_assert(sizeof(Dst) > sizeof(Src), "");
31 Dst dst;
32 memcpy(&dst, &src, sizeof(Src));
33 return dst;
34}
35
36// Our program is an array of void*, either
37// - 1 void* per stage with no context pointer, the next stage;
38// - 2 void* per stage with a context pointer, first the context pointer, then the next stage.
39
40// load_and_inc() steps the program forward by 1 void*, returning that pointer.
41SI void* load_and_inc(void**& program) {
42#if defined(__GNUC__) && defined(__x86_64__)
43 // If program is in %rsi (we try to make this likely) then this is a single instruction.
44 void* rax;
45 asm("lodsq" : "=a"(rax), "+S"(program)); // Write-only %rax, read-write %rsi.
46 return rax;
47#else
48 // On ARM *program++ compiles into pretty ideal code without any handholding.
49 return *program++;
50#endif
51}
52
53// Lazily resolved on first cast. Does nothing if cast to Ctx::None.
54struct Ctx {
55 struct None {};
56
57 void* ptr;
58 void**& program;
59
60 explicit Ctx(void**& p) : ptr(nullptr), program(p) {}
61
62 template <typename T>
63 operator T*() {
64 if (!ptr) { ptr = load_and_inc(program); }
65 return (T*)ptr;
66 }
67 operator None() { return None{}; }
68};
69
Mike Kleinadc78d52018-01-01 09:06:37 -050070
71#if !defined(__clang__)
72 #define JUMPER_IS_SCALAR
Mike Klein15eb1e92018-08-31 11:21:27 -040073#elif defined(SK_ARM_HAS_NEON)
Mike Kleinadc78d52018-01-01 09:06:37 -050074 #define JUMPER_IS_NEON
Mike Klein15eb1e92018-08-31 11:21:27 -040075#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX512
Mike Kleinadc78d52018-01-01 09:06:37 -050076 #define JUMPER_IS_AVX512
Mike Klein15eb1e92018-08-31 11:21:27 -040077#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
Mike Kleinadc78d52018-01-01 09:06:37 -050078 #define JUMPER_IS_HSW
Mike Klein15eb1e92018-08-31 11:21:27 -040079#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX
Mike Kleinadc78d52018-01-01 09:06:37 -050080 #define JUMPER_IS_AVX
Mike Klein15eb1e92018-08-31 11:21:27 -040081#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
Mike Kleinadc78d52018-01-01 09:06:37 -050082 #define JUMPER_IS_SSE41
Mike Klein15eb1e92018-08-31 11:21:27 -040083#elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
Mike Kleinadc78d52018-01-01 09:06:37 -050084 #define JUMPER_IS_SSE2
85#else
86 #define JUMPER_IS_SCALAR
87#endif
88
89// Older Clangs seem to crash when generating non-optimized NEON code for ARMv7.
Mike Klein15eb1e92018-08-31 11:21:27 -040090#if defined(__clang__) && !defined(__OPTIMIZE__) && defined(SK_CPU_ARM32)
Mike Kleinadc78d52018-01-01 09:06:37 -050091 // Apple Clang 9 and vanilla Clang 5 are fine, and may even be conservative.
92 #if defined(__apple_build_version__) && __clang_major__ < 9
93 #define JUMPER_IS_SCALAR
94 #elif __clang_major__ < 5
95 #define JUMPER_IS_SCALAR
96 #endif
Mike Kleinb54d2232018-06-01 15:53:21 -040097
98 #if defined(JUMPER_IS_NEON) && defined(JUMPER_IS_SCALAR)
99 #undef JUMPER_IS_NEON
100 #endif
Mike Kleinadc78d52018-01-01 09:06:37 -0500101#endif
102
103#if defined(JUMPER_IS_SCALAR)
Mike Klein5cc94cc2018-03-07 17:04:18 +0000104 #include <math.h>
Mike Klein1b9b7d52018-02-27 10:37:40 -0500105#elif defined(JUMPER_IS_NEON)
106 #include <arm_neon.h>
107#else
108 #include <immintrin.h>
109#endif
Mike Klein5cc94cc2018-03-07 17:04:18 +0000110
Mike Klein1b9b7d52018-02-27 10:37:40 -0500111namespace SK_OPTS_NS {
112
113#if defined(JUMPER_IS_SCALAR)
114 // This path should lead to portable scalar code.
Mike Kleinadc78d52018-01-01 09:06:37 -0500115 using F = float ;
116 using I32 = int32_t;
117 using U64 = uint64_t;
118 using U32 = uint32_t;
119 using U16 = uint16_t;
120 using U8 = uint8_t ;
121
122 SI F mad(F f, F m, F a) { return f*m+a; }
123 SI F min(F a, F b) { return fminf(a,b); }
124 SI F max(F a, F b) { return fmaxf(a,b); }
125 SI F abs_ (F v) { return fabsf(v); }
126 SI F floor_(F v) { return floorf(v); }
127 SI F rcp (F v) { return 1.0f / v; }
128 SI F rsqrt (F v) { return 1.0f / sqrtf(v); }
129 SI F sqrt_(F v) { return sqrtf(v); }
130 SI U32 round (F v, F scale) { return (uint32_t)(v*scale + 0.5f); }
131 SI U16 pack(U32 v) { return (U16)v; }
132 SI U8 pack(U16 v) { return (U8)v; }
133
134 SI F if_then_else(I32 c, F t, F e) { return c ? t : e; }
135
136 template <typename T>
137 SI T gather(const T* p, U32 ix) { return p[ix]; }
138
Brian Salomon217522c2019-06-11 15:55:30 -0400139 SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) {
140 *r = ptr[0];
141 *g = ptr[1];
142 }
143 SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) {
144 ptr[0] = r;
145 ptr[1] = g;
146 }
Mike Kleinadc78d52018-01-01 09:06:37 -0500147 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
148 *r = ptr[0];
149 *g = ptr[1];
150 *b = ptr[2];
151 }
152 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
153 *r = ptr[0];
154 *g = ptr[1];
155 *b = ptr[2];
156 *a = ptr[3];
157 }
158 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
159 ptr[0] = r;
160 ptr[1] = g;
161 ptr[2] = b;
162 ptr[3] = a;
163 }
164
Brian Salomon217522c2019-06-11 15:55:30 -0400165 SI void load2(const float* ptr, size_t tail, F* r, F* g) {
166 *r = ptr[0];
167 *g = ptr[1];
168 }
169 SI void store2(float* ptr, size_t tail, F r, F g) {
170 ptr[0] = r;
171 ptr[1] = g;
172 }
Mike Kleinadc78d52018-01-01 09:06:37 -0500173 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
174 *r = ptr[0];
175 *g = ptr[1];
176 *b = ptr[2];
177 *a = ptr[3];
178 }
179 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
180 ptr[0] = r;
181 ptr[1] = g;
182 ptr[2] = b;
183 ptr[3] = a;
184 }
185
186#elif defined(JUMPER_IS_NEON)
Mike Kleinadc78d52018-01-01 09:06:37 -0500187 // Since we know we're using Clang, we can use its vector extensions.
188 template <typename T> using V = T __attribute__((ext_vector_type(4)));
189 using F = V<float >;
190 using I32 = V< int32_t>;
191 using U64 = V<uint64_t>;
192 using U32 = V<uint32_t>;
193 using U16 = V<uint16_t>;
194 using U8 = V<uint8_t >;
195
196 // We polyfill a few routines that Clang doesn't build into ext_vector_types.
197 SI F min(F a, F b) { return vminq_f32(a,b); }
198 SI F max(F a, F b) { return vmaxq_f32(a,b); }
199 SI F abs_ (F v) { return vabsq_f32(v); }
200 SI F rcp (F v) { auto e = vrecpeq_f32 (v); return vrecpsq_f32 (v,e ) * e; }
201 SI F rsqrt (F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; }
202 SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); }
203 SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); }
204
205 SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); }
206
Mike Klein15eb1e92018-08-31 11:21:27 -0400207 #if defined(SK_CPU_ARM64)
Mike Kleinadc78d52018-01-01 09:06:37 -0500208 SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); }
209 SI F floor_(F v) { return vrndmq_f32(v); }
210 SI F sqrt_(F v) { return vsqrtq_f32(v); }
211 SI U32 round(F v, F scale) { return vcvtnq_u32_f32(v*scale); }
212 #else
213 SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); }
214 SI F floor_(F v) {
215 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
216 return roundtrip - if_then_else(roundtrip > v, 1, 0);
217 }
218
219 SI F sqrt_(F v) {
220 auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v).
221 e *= vrsqrtsq_f32(v,e*e);
222 e *= vrsqrtsq_f32(v,e*e);
223 return v*e; // sqrt(v) == v*rsqrt(v).
224 }
225
226 SI U32 round(F v, F scale) {
227 return vcvtq_u32_f32(mad(v,scale,0.5f));
228 }
229 #endif
230
231
232 template <typename T>
233 SI V<T> gather(const T* p, U32 ix) {
234 return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
235 }
Brian Salomon217522c2019-06-11 15:55:30 -0400236 SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) {
237 uint16x4x2_t rg;
238 if (__builtin_expect(tail,0)) {
239 if ( true ) { rg = vld2_lane_u16(ptr + 0, rg, 0); }
240 if (tail > 1) { rg = vld2_lane_u16(ptr + 2, rg, 1); }
241 if (tail > 2) { rg = vld2_lane_u16(ptr + 4, rg, 2); }
242 } else {
243 rg = vld2_u16(ptr);
244 }
245 *r = rg.val[0];
246 *g = rg.val[1];
247 }
248 SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) {
249 if (__builtin_expect(tail,0)) {
250 if ( true ) { vst2_lane_u16(ptr + 0, (uint16x4x2_t{{r,g}}), 0); }
251 if (tail > 1) { vst2_lane_u16(ptr + 2, (uint16x4x2_t{{r,g}}), 1); }
252 if (tail > 2) { vst2_lane_u16(ptr + 4, (uint16x4x2_t{{r,g}}), 2); }
253 } else {
254 vst2_u16(ptr, (uint16x4x2_t{{r,g}}));
255 }
256 }
Mike Kleinadc78d52018-01-01 09:06:37 -0500257 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
258 uint16x4x3_t rgb;
259 if (__builtin_expect(tail,0)) {
260 if ( true ) { rgb = vld3_lane_u16(ptr + 0, rgb, 0); }
261 if (tail > 1) { rgb = vld3_lane_u16(ptr + 3, rgb, 1); }
262 if (tail > 2) { rgb = vld3_lane_u16(ptr + 6, rgb, 2); }
263 } else {
264 rgb = vld3_u16(ptr);
265 }
266 *r = rgb.val[0];
267 *g = rgb.val[1];
268 *b = rgb.val[2];
269 }
270 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
271 uint16x4x4_t rgba;
272 if (__builtin_expect(tail,0)) {
273 if ( true ) { rgba = vld4_lane_u16(ptr + 0, rgba, 0); }
274 if (tail > 1) { rgba = vld4_lane_u16(ptr + 4, rgba, 1); }
275 if (tail > 2) { rgba = vld4_lane_u16(ptr + 8, rgba, 2); }
276 } else {
277 rgba = vld4_u16(ptr);
278 }
279 *r = rgba.val[0];
280 *g = rgba.val[1];
281 *b = rgba.val[2];
282 *a = rgba.val[3];
283 }
Brian Salomon217522c2019-06-11 15:55:30 -0400284
Mike Kleinadc78d52018-01-01 09:06:37 -0500285 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
286 if (__builtin_expect(tail,0)) {
287 if ( true ) { vst4_lane_u16(ptr + 0, (uint16x4x4_t{{r,g,b,a}}), 0); }
288 if (tail > 1) { vst4_lane_u16(ptr + 4, (uint16x4x4_t{{r,g,b,a}}), 1); }
289 if (tail > 2) { vst4_lane_u16(ptr + 8, (uint16x4x4_t{{r,g,b,a}}), 2); }
290 } else {
291 vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}}));
292 }
293 }
Brian Salomon217522c2019-06-11 15:55:30 -0400294 SI void load2(const float* ptr, size_t tail, F* r, F* g) {
295 float32x4x2_t rg;
296 if (__builtin_expect(tail,0)) {
297 if ( true ) { rg = vld2q_lane_f32(ptr + 0, rg, 0); }
298 if (tail > 1) { rg = vld2q_lane_f32(ptr + 2, rg, 1); }
299 if (tail > 2) { rg = vld2q_lane_f32(ptr + 4, rg, 2); }
300 } else {
301 rg = vld2q_f32(ptr);
302 }
303 *r = rg.val[0];
304 *g = rg.val[1];
305 }
306 SI void store2(float* ptr, size_t tail, F r, F g) {
307 if (__builtin_expect(tail,0)) {
308 if ( true ) { vst2q_lane_f32(ptr + 0, (float32x4x2_t{{r,g}}), 0); }
309 if (tail > 1) { vst2q_lane_f32(ptr + 2, (float32x4x2_t{{r,g}}), 1); }
310 if (tail > 2) { vst2q_lane_f32(ptr + 4, (float32x4x2_t{{r,g}}), 2); }
311 } else {
312 vst2q_f32(ptr, (float32x4x2_t{{r,g}}));
313 }
314 }
Mike Kleinadc78d52018-01-01 09:06:37 -0500315 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
316 float32x4x4_t rgba;
317 if (__builtin_expect(tail,0)) {
318 if ( true ) { rgba = vld4q_lane_f32(ptr + 0, rgba, 0); }
319 if (tail > 1) { rgba = vld4q_lane_f32(ptr + 4, rgba, 1); }
320 if (tail > 2) { rgba = vld4q_lane_f32(ptr + 8, rgba, 2); }
321 } else {
322 rgba = vld4q_f32(ptr);
323 }
324 *r = rgba.val[0];
325 *g = rgba.val[1];
326 *b = rgba.val[2];
327 *a = rgba.val[3];
328 }
329 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
330 if (__builtin_expect(tail,0)) {
331 if ( true ) { vst4q_lane_f32(ptr + 0, (float32x4x4_t{{r,g,b,a}}), 0); }
332 if (tail > 1) { vst4q_lane_f32(ptr + 4, (float32x4x4_t{{r,g,b,a}}), 1); }
333 if (tail > 2) { vst4q_lane_f32(ptr + 8, (float32x4x4_t{{r,g,b,a}}), 2); }
334 } else {
335 vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}}));
336 }
337 }
338
339#elif defined(JUMPER_IS_AVX) || defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Kleinadc78d52018-01-01 09:06:37 -0500340 // These are __m256 and __m256i, but friendlier and strongly-typed.
341 template <typename T> using V = T __attribute__((ext_vector_type(8)));
342 using F = V<float >;
343 using I32 = V< int32_t>;
344 using U64 = V<uint64_t>;
345 using U32 = V<uint32_t>;
346 using U16 = V<uint16_t>;
347 using U8 = V<uint8_t >;
348
349 SI F mad(F f, F m, F a) {
350 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
351 return _mm256_fmadd_ps(f,m,a);
352 #else
353 return f*m+a;
354 #endif
355 }
356
357 SI F min(F a, F b) { return _mm256_min_ps(a,b); }
358 SI F max(F a, F b) { return _mm256_max_ps(a,b); }
359 SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); }
360 SI F floor_(F v) { return _mm256_floor_ps(v); }
361 SI F rcp (F v) { return _mm256_rcp_ps (v); }
362 SI F rsqrt (F v) { return _mm256_rsqrt_ps(v); }
363 SI F sqrt_(F v) { return _mm256_sqrt_ps (v); }
364 SI U32 round (F v, F scale) { return _mm256_cvtps_epi32(v*scale); }
365
366 SI U16 pack(U32 v) {
367 return _mm_packus_epi32(_mm256_extractf128_si256(v, 0),
368 _mm256_extractf128_si256(v, 1));
369 }
370 SI U8 pack(U16 v) {
371 auto r = _mm_packus_epi16(v,v);
Mike Klein7a177b42019-06-17 17:17:47 -0500372 return sk_unaligned_load<U8>(&r);
Mike Kleinadc78d52018-01-01 09:06:37 -0500373 }
374
375 SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e,t,c); }
376
377 template <typename T>
378 SI V<T> gather(const T* p, U32 ix) {
379 return { p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
380 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
381 }
382 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
383 SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps (p, ix, 4); }
384 SI U32 gather(const uint32_t* p, U32 ix) { return _mm256_i32gather_epi32(p, ix, 4); }
385 SI U64 gather(const uint64_t* p, U32 ix) {
386 __m256i parts[] = {
387 _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,0), 8),
388 _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,1), 8),
389 };
390 return bit_cast<U64>(parts);
391 }
392 #endif
393
Brian Salomon217522c2019-06-11 15:55:30 -0400394 SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) {
395 U16 _0123, _4567;
396 if (__builtin_expect(tail,0)) {
397 _0123 = _4567 = _mm_setzero_si128();
398 auto* d = &_0123;
399 if (tail > 3) {
400 *d = _mm_loadu_si128(((__m128i*)ptr) + 0);
401 tail -= 4;
402 ptr += 8;
403 d = &_4567;
404 }
405 bool high = false;
406 if (tail > 1) {
407 *d = _mm_loadu_si64(ptr);
408 tail -= 2;
409 ptr += 4;
410 high = true;
411 }
412 if (tail > 0) {
413 (*d)[high ? 4 : 0] = *(ptr + 0);
414 (*d)[high ? 5 : 1] = *(ptr + 1);
415 }
416 } else {
417 _0123 = _mm_loadu_si128(((__m128i*)ptr) + 0);
418 _4567 = _mm_loadu_si128(((__m128i*)ptr) + 1);
419 }
420 *r = _mm_packs_epi32(_mm_srai_epi32(_mm_slli_epi32(_0123, 16), 16),
421 _mm_srai_epi32(_mm_slli_epi32(_4567, 16), 16));
422 *g = _mm_packs_epi32(_mm_srai_epi32(_0123, 16),
423 _mm_srai_epi32(_4567, 16));
424 }
425 SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) {
426 auto _0123 = _mm_unpacklo_epi16(r, g),
427 _4567 = _mm_unpackhi_epi16(r, g);
428 if (__builtin_expect(tail,0)) {
429 const auto* s = &_0123;
430 if (tail > 3) {
431 _mm_storeu_si128((__m128i*)ptr, *s);
432 s = &_4567;
433 tail -= 4;
434 ptr += 8;
435 }
436 bool high = false;
437 if (tail > 1) {
438 _mm_storel_epi64((__m128i*)ptr, *s);
439 ptr += 4;
440 tail -= 2;
441 high = true;
442 }
443 if (tail > 0) {
444 if (high) {
445 *(int32_t*)ptr = _mm_extract_epi32(*s, 2);
446 } else {
447 *(int32_t*)ptr = _mm_cvtsi128_si32(*s);
448 }
449 }
450 } else {
451 _mm_storeu_si128((__m128i*)ptr + 0, _0123);
452 _mm_storeu_si128((__m128i*)ptr + 1, _4567);
453 }
454 }
455
Mike Kleinadc78d52018-01-01 09:06:37 -0500456 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
457 __m128i _0,_1,_2,_3,_4,_5,_6,_7;
458 if (__builtin_expect(tail,0)) {
459 auto load_rgb = [](const uint16_t* src) {
460 auto v = _mm_cvtsi32_si128(*(const uint32_t*)src);
461 return _mm_insert_epi16(v, src[2], 2);
462 };
463 _1 = _2 = _3 = _4 = _5 = _6 = _7 = _mm_setzero_si128();
464 if ( true ) { _0 = load_rgb(ptr + 0); }
465 if (tail > 1) { _1 = load_rgb(ptr + 3); }
466 if (tail > 2) { _2 = load_rgb(ptr + 6); }
467 if (tail > 3) { _3 = load_rgb(ptr + 9); }
468 if (tail > 4) { _4 = load_rgb(ptr + 12); }
469 if (tail > 5) { _5 = load_rgb(ptr + 15); }
470 if (tail > 6) { _6 = load_rgb(ptr + 18); }
471 } else {
472 // Load 0+1, 2+3, 4+5 normally, and 6+7 backed up 4 bytes so we don't run over.
473 auto _01 = _mm_loadu_si128((const __m128i*)(ptr + 0)) ;
474 auto _23 = _mm_loadu_si128((const __m128i*)(ptr + 6)) ;
475 auto _45 = _mm_loadu_si128((const __m128i*)(ptr + 12)) ;
476 auto _67 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 16)), 4);
477 _0 = _01; _1 = _mm_srli_si128(_01, 6);
478 _2 = _23; _3 = _mm_srli_si128(_23, 6);
479 _4 = _45; _5 = _mm_srli_si128(_45, 6);
480 _6 = _67; _7 = _mm_srli_si128(_67, 6);
481 }
482
483 auto _02 = _mm_unpacklo_epi16(_0, _2), // r0 r2 g0 g2 b0 b2 xx xx
484 _13 = _mm_unpacklo_epi16(_1, _3),
485 _46 = _mm_unpacklo_epi16(_4, _6),
486 _57 = _mm_unpacklo_epi16(_5, _7);
487
488 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
489 bx0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 xx xx xx xx
490 rg4567 = _mm_unpacklo_epi16(_46, _57),
491 bx4567 = _mm_unpackhi_epi16(_46, _57);
492
493 *r = _mm_unpacklo_epi64(rg0123, rg4567);
494 *g = _mm_unpackhi_epi64(rg0123, rg4567);
495 *b = _mm_unpacklo_epi64(bx0123, bx4567);
496 }
497 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
498 __m128i _01, _23, _45, _67;
499 if (__builtin_expect(tail,0)) {
500 auto src = (const double*)ptr;
501 _01 = _23 = _45 = _67 = _mm_setzero_si128();
502 if (tail > 0) { _01 = _mm_loadl_pd(_01, src+0); }
503 if (tail > 1) { _01 = _mm_loadh_pd(_01, src+1); }
504 if (tail > 2) { _23 = _mm_loadl_pd(_23, src+2); }
505 if (tail > 3) { _23 = _mm_loadh_pd(_23, src+3); }
506 if (tail > 4) { _45 = _mm_loadl_pd(_45, src+4); }
507 if (tail > 5) { _45 = _mm_loadh_pd(_45, src+5); }
508 if (tail > 6) { _67 = _mm_loadl_pd(_67, src+6); }
509 } else {
510 _01 = _mm_loadu_si128(((__m128i*)ptr) + 0);
511 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1);
512 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2);
513 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
514 }
515
516 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
517 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
518 _46 = _mm_unpacklo_epi16(_45, _67),
519 _57 = _mm_unpackhi_epi16(_45, _67);
520
521 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
522 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
523 rg4567 = _mm_unpacklo_epi16(_46, _57),
524 ba4567 = _mm_unpackhi_epi16(_46, _57);
525
526 *r = _mm_unpacklo_epi64(rg0123, rg4567);
527 *g = _mm_unpackhi_epi64(rg0123, rg4567);
528 *b = _mm_unpacklo_epi64(ba0123, ba4567);
529 *a = _mm_unpackhi_epi64(ba0123, ba4567);
530 }
531 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
532 auto rg0123 = _mm_unpacklo_epi16(r, g), // r0 g0 r1 g1 r2 g2 r3 g3
533 rg4567 = _mm_unpackhi_epi16(r, g), // r4 g4 r5 g5 r6 g6 r7 g7
534 ba0123 = _mm_unpacklo_epi16(b, a),
535 ba4567 = _mm_unpackhi_epi16(b, a);
536
537 auto _01 = _mm_unpacklo_epi32(rg0123, ba0123),
538 _23 = _mm_unpackhi_epi32(rg0123, ba0123),
539 _45 = _mm_unpacklo_epi32(rg4567, ba4567),
540 _67 = _mm_unpackhi_epi32(rg4567, ba4567);
541
542 if (__builtin_expect(tail,0)) {
543 auto dst = (double*)ptr;
544 if (tail > 0) { _mm_storel_pd(dst+0, _01); }
545 if (tail > 1) { _mm_storeh_pd(dst+1, _01); }
546 if (tail > 2) { _mm_storel_pd(dst+2, _23); }
547 if (tail > 3) { _mm_storeh_pd(dst+3, _23); }
548 if (tail > 4) { _mm_storel_pd(dst+4, _45); }
549 if (tail > 5) { _mm_storeh_pd(dst+5, _45); }
550 if (tail > 6) { _mm_storel_pd(dst+6, _67); }
551 } else {
552 _mm_storeu_si128((__m128i*)ptr + 0, _01);
553 _mm_storeu_si128((__m128i*)ptr + 1, _23);
554 _mm_storeu_si128((__m128i*)ptr + 2, _45);
555 _mm_storeu_si128((__m128i*)ptr + 3, _67);
556 }
557 }
558
Brian Salomon217522c2019-06-11 15:55:30 -0400559 SI void load2(const float* ptr, size_t tail, F* r, F* g) {
560 F _0123, _4567;
561 if (__builtin_expect(tail, 0)) {
562 _0123 = _4567 = _mm256_setzero_ps();
563 F* d = &_0123;
564 if (tail > 3) {
565 *d = _mm256_loadu_ps(ptr);
566 ptr += 8;
567 tail -= 4;
568 d = &_4567;
569 }
570 bool high = false;
571 if (tail > 1) {
572 *d = _mm256_castps128_ps256(_mm_loadu_ps(ptr));
573 ptr += 4;
574 tail -= 2;
575 high = true;
576 }
577 if (tail > 0) {
578 *d = high ? _mm256_insertf128_ps(*d, _mm_loadu_si64(ptr), 1)
579 : _mm256_insertf128_ps(*d, _mm_loadu_si64(ptr), 0);
580 }
581 } else {
582 _0123 = _mm256_loadu_ps(ptr + 0);
583 _4567 = _mm256_loadu_ps(ptr + 8);
584 }
585
586 F _0145 = _mm256_permute2f128_pd(_0123, _4567, 0x20),
587 _2367 = _mm256_permute2f128_pd(_0123, _4567, 0x31);
588
589 *r = _mm256_shuffle_ps(_0145, _2367, 0x88);
590 *g = _mm256_shuffle_ps(_0145, _2367, 0xDD);
591 }
592 SI void store2(float* ptr, size_t tail, F r, F g) {
593 F _0145 = _mm256_unpacklo_ps(r, g),
594 _2367 = _mm256_unpackhi_ps(r, g);
595 F _0123 = _mm256_permute2f128_pd(_0145, _2367, 0x20),
596 _4567 = _mm256_permute2f128_pd(_0145, _2367, 0x31);
597
598 if (__builtin_expect(tail, 0)) {
599 const __m256* s = &_0123;
600 if (tail > 3) {
601 _mm256_storeu_ps(ptr, *s);
602 s = &_4567;
603 tail -= 4;
604 ptr += 8;
605 }
606 bool high = false;
607 if (tail > 1) {
608 _mm_storeu_ps(ptr, _mm256_extractf128_ps(*s, 0));
609 ptr += 4;
610 tail -= 2;
611 high = true;
612 }
613 if (tail > 0) {
614 *(ptr + 0) = (*s)[ high ? 4 : 0];
615 *(ptr + 1) = (*s)[ high ? 5 : 1];
616 }
617 } else {
618 _mm256_storeu_ps(ptr + 0, _0123);
619 _mm256_storeu_ps(ptr + 8, _4567);
620 }
621 }
622
Mike Kleinadc78d52018-01-01 09:06:37 -0500623 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
624 F _04, _15, _26, _37;
625 _04 = _15 = _26 = _37 = 0;
626 switch (tail) {
627 case 0: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+28), 1);
628 case 7: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+24), 1);
629 case 6: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+20), 1);
630 case 5: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+16), 1);
631 case 4: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+12), 0);
632 case 3: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+ 8), 0);
633 case 2: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+ 4), 0);
634 case 1: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+ 0), 0);
635 }
636
637 F rg0145 = _mm256_unpacklo_ps(_04,_15), // r0 r1 g0 g1 | r4 r5 g4 g5
638 ba0145 = _mm256_unpackhi_ps(_04,_15),
639 rg2367 = _mm256_unpacklo_ps(_26,_37),
640 ba2367 = _mm256_unpackhi_ps(_26,_37);
641
642 *r = _mm256_unpacklo_pd(rg0145, rg2367);
643 *g = _mm256_unpackhi_pd(rg0145, rg2367);
644 *b = _mm256_unpacklo_pd(ba0145, ba2367);
645 *a = _mm256_unpackhi_pd(ba0145, ba2367);
646 }
647 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
648 F rg0145 = _mm256_unpacklo_ps(r, g), // r0 g0 r1 g1 | r4 g4 r5 g5
649 rg2367 = _mm256_unpackhi_ps(r, g), // r2 ... | r6 ...
650 ba0145 = _mm256_unpacklo_ps(b, a), // b0 a0 b1 a1 | b4 a4 b5 a5
651 ba2367 = _mm256_unpackhi_ps(b, a); // b2 ... | b6 ...
652
653 F _04 = _mm256_unpacklo_pd(rg0145, ba0145), // r0 g0 b0 a0 | r4 g4 b4 a4
654 _15 = _mm256_unpackhi_pd(rg0145, ba0145), // r1 ... | r5 ...
655 _26 = _mm256_unpacklo_pd(rg2367, ba2367), // r2 ... | r6 ...
656 _37 = _mm256_unpackhi_pd(rg2367, ba2367); // r3 ... | r7 ...
657
658 if (__builtin_expect(tail, 0)) {
659 if (tail > 0) { _mm_storeu_ps(ptr+ 0, _mm256_extractf128_ps(_04, 0)); }
660 if (tail > 1) { _mm_storeu_ps(ptr+ 4, _mm256_extractf128_ps(_15, 0)); }
661 if (tail > 2) { _mm_storeu_ps(ptr+ 8, _mm256_extractf128_ps(_26, 0)); }
662 if (tail > 3) { _mm_storeu_ps(ptr+12, _mm256_extractf128_ps(_37, 0)); }
663 if (tail > 4) { _mm_storeu_ps(ptr+16, _mm256_extractf128_ps(_04, 1)); }
664 if (tail > 5) { _mm_storeu_ps(ptr+20, _mm256_extractf128_ps(_15, 1)); }
665 if (tail > 6) { _mm_storeu_ps(ptr+24, _mm256_extractf128_ps(_26, 1)); }
666 } else {
667 F _01 = _mm256_permute2f128_ps(_04, _15, 32), // 32 == 0010 0000 == lo, lo
668 _23 = _mm256_permute2f128_ps(_26, _37, 32),
669 _45 = _mm256_permute2f128_ps(_04, _15, 49), // 49 == 0011 0001 == hi, hi
670 _67 = _mm256_permute2f128_ps(_26, _37, 49);
671 _mm256_storeu_ps(ptr+ 0, _01);
672 _mm256_storeu_ps(ptr+ 8, _23);
673 _mm256_storeu_ps(ptr+16, _45);
674 _mm256_storeu_ps(ptr+24, _67);
675 }
676 }
677
678#elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41)
Mike Kleinadc78d52018-01-01 09:06:37 -0500679 template <typename T> using V = T __attribute__((ext_vector_type(4)));
680 using F = V<float >;
681 using I32 = V< int32_t>;
682 using U64 = V<uint64_t>;
683 using U32 = V<uint32_t>;
684 using U16 = V<uint16_t>;
685 using U8 = V<uint8_t >;
686
687 SI F mad(F f, F m, F a) { return f*m+a; }
688 SI F min(F a, F b) { return _mm_min_ps(a,b); }
689 SI F max(F a, F b) { return _mm_max_ps(a,b); }
690 SI F abs_(F v) { return _mm_and_ps(v, 0-v); }
691 SI F rcp (F v) { return _mm_rcp_ps (v); }
692 SI F rsqrt (F v) { return _mm_rsqrt_ps(v); }
693 SI F sqrt_(F v) { return _mm_sqrt_ps (v); }
694 SI U32 round(F v, F scale) { return _mm_cvtps_epi32(v*scale); }
695
696 SI U16 pack(U32 v) {
697 #if defined(JUMPER_IS_SSE41)
698 auto p = _mm_packus_epi32(v,v);
699 #else
700 // Sign extend so that _mm_packs_epi32() does the pack we want.
701 auto p = _mm_srai_epi32(_mm_slli_epi32(v, 16), 16);
702 p = _mm_packs_epi32(p,p);
703 #endif
Mike Klein7a177b42019-06-17 17:17:47 -0500704 return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
Mike Kleinadc78d52018-01-01 09:06:37 -0500705 }
706 SI U8 pack(U16 v) {
707 auto r = widen_cast<__m128i>(v);
708 r = _mm_packus_epi16(r,r);
Mike Klein7a177b42019-06-17 17:17:47 -0500709 return sk_unaligned_load<U8>(&r);
Mike Kleinadc78d52018-01-01 09:06:37 -0500710 }
711
712 SI F if_then_else(I32 c, F t, F e) {
713 return _mm_or_ps(_mm_and_ps(c, t), _mm_andnot_ps(c, e));
714 }
715
716 SI F floor_(F v) {
717 #if defined(JUMPER_IS_SSE41)
718 return _mm_floor_ps(v);
719 #else
720 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
721 return roundtrip - if_then_else(roundtrip > v, 1, 0);
722 #endif
723 }
724
725 template <typename T>
726 SI V<T> gather(const T* p, U32 ix) {
727 return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
728 }
729
Mike Klein0f55db52019-09-30 10:01:08 -0500730 // TODO: these loads and stores are incredibly difficult to follow.
731
Brian Salomon217522c2019-06-11 15:55:30 -0400732 SI void load2(const uint16_t* ptr, size_t tail, U16* r, U16* g) {
733 __m128i _01;
734 if (__builtin_expect(tail,0)) {
735 _01 = _mm_setzero_si128();
736 if (tail > 1) {
737 _01 = _mm_loadl_pd(_01, (const double*)ptr); // r0 g0 r1 g1 00 00 00 00
738 if (tail > 2) {
Robert Phillipsf73ef0b2019-09-24 13:00:42 -0400739 _01 = _mm_insert_epi16(_01, *(ptr+4), 4); // r0 g0 r1 g1 r2 00 00 00
740 _01 = _mm_insert_epi16(_01, *(ptr+5), 5); // r0 g0 r1 g1 r2 g2 00 00
Brian Salomon217522c2019-06-11 15:55:30 -0400741 }
742 } else {
Mike Klein0f55db52019-09-30 10:01:08 -0500743 _01 = _mm_cvtsi32_si128(*(const uint32_t*)ptr); // r0 g0 00 00 00 00 00 00
Brian Salomon217522c2019-06-11 15:55:30 -0400744 }
745 } else {
746 _01 = _mm_loadu_si128(((__m128i*)ptr) + 0); // r0 g0 r1 g1 r2 g2 r3 g3
747 }
748 auto rg01_23 = _mm_shufflelo_epi16(_01, 0xD8); // r0 r1 g0 g1 r2 g2 r3 g3
749 auto rg = _mm_shufflehi_epi16(rg01_23, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3
750
751 auto R = _mm_shuffle_epi32(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3
752 auto G = _mm_shuffle_epi32(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3
Mike Klein7a177b42019-06-17 17:17:47 -0500753 *r = sk_unaligned_load<U16>(&R);
754 *g = sk_unaligned_load<U16>(&G);
Brian Salomon217522c2019-06-11 15:55:30 -0400755 }
756 SI void store2(uint16_t* ptr, size_t tail, U16 r, U16 g) {
757 U32 rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g));
758 if (__builtin_expect(tail, 0)) {
759 if (tail > 1) {
760 _mm_storel_epi64((__m128i*)ptr, rg);
761 if (tail > 2) {
762 int32_t rgpair = rg[2];
763 memcpy(ptr + 4, &rgpair, sizeof(rgpair));
764 }
765 } else {
766 int32_t rgpair = rg[0];
767 memcpy(ptr, &rgpair, sizeof(rgpair));
768 }
769 } else {
770 _mm_storeu_si128((__m128i*)ptr + 0, rg);
771 }
772 }
773
Mike Kleinadc78d52018-01-01 09:06:37 -0500774 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
775 __m128i _0, _1, _2, _3;
776 if (__builtin_expect(tail,0)) {
777 _1 = _2 = _3 = _mm_setzero_si128();
778 auto load_rgb = [](const uint16_t* src) {
779 auto v = _mm_cvtsi32_si128(*(const uint32_t*)src);
780 return _mm_insert_epi16(v, src[2], 2);
781 };
782 if ( true ) { _0 = load_rgb(ptr + 0); }
783 if (tail > 1) { _1 = load_rgb(ptr + 3); }
784 if (tail > 2) { _2 = load_rgb(ptr + 6); }
785 } else {
786 // Load slightly weirdly to make sure we don't load past the end of 4x48 bits.
787 auto _01 = _mm_loadu_si128((const __m128i*)(ptr + 0)) ,
788 _23 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 4)), 4);
789
790 // Each _N holds R,G,B for pixel N in its lower 3 lanes (upper 5 are ignored).
791 _0 = _01;
792 _1 = _mm_srli_si128(_01, 6);
793 _2 = _23;
794 _3 = _mm_srli_si128(_23, 6);
795 }
796
797 // De-interlace to R,G,B.
798 auto _02 = _mm_unpacklo_epi16(_0, _2), // r0 r2 g0 g2 b0 b2 xx xx
799 _13 = _mm_unpacklo_epi16(_1, _3); // r1 r3 g1 g3 b1 b3 xx xx
800
801 auto R = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
802 G = _mm_srli_si128(R, 8),
803 B = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 xx xx xx xx
804
Mike Klein7a177b42019-06-17 17:17:47 -0500805 *r = sk_unaligned_load<U16>(&R);
806 *g = sk_unaligned_load<U16>(&G);
807 *b = sk_unaligned_load<U16>(&B);
Mike Kleinadc78d52018-01-01 09:06:37 -0500808 }
809
810 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
811 __m128i _01, _23;
812 if (__builtin_expect(tail,0)) {
813 _01 = _23 = _mm_setzero_si128();
814 auto src = (const double*)ptr;
815 if ( true ) { _01 = _mm_loadl_pd(_01, src + 0); } // r0 g0 b0 a0 00 00 00 00
816 if (tail > 1) { _01 = _mm_loadh_pd(_01, src + 1); } // r0 g0 b0 a0 r1 g1 b1 a1
817 if (tail > 2) { _23 = _mm_loadl_pd(_23, src + 2); } // r2 g2 b2 a2 00 00 00 00
818 } else {
819 _01 = _mm_loadu_si128(((__m128i*)ptr) + 0); // r0 g0 b0 a0 r1 g1 b1 a1
820 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1); // r2 g2 b2 a2 r3 g3 b3 a3
821 }
822
823 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
824 _13 = _mm_unpackhi_epi16(_01, _23); // r1 r3 g1 g3 b1 b3 a1 a3
825
826 auto rg = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
827 ba = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 a0 a1 a2 a3
828
Mike Klein7a177b42019-06-17 17:17:47 -0500829 *r = sk_unaligned_load<U16>((uint16_t*)&rg + 0);
830 *g = sk_unaligned_load<U16>((uint16_t*)&rg + 4);
831 *b = sk_unaligned_load<U16>((uint16_t*)&ba + 0);
832 *a = sk_unaligned_load<U16>((uint16_t*)&ba + 4);
Mike Kleinadc78d52018-01-01 09:06:37 -0500833 }
834
835 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
836 auto rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g)),
837 ba = _mm_unpacklo_epi16(widen_cast<__m128i>(b), widen_cast<__m128i>(a));
838
839 if (__builtin_expect(tail, 0)) {
840 auto dst = (double*)ptr;
841 if ( true ) { _mm_storel_pd(dst + 0, _mm_unpacklo_epi32(rg, ba)); }
842 if (tail > 1) { _mm_storeh_pd(dst + 1, _mm_unpacklo_epi32(rg, ba)); }
843 if (tail > 2) { _mm_storel_pd(dst + 2, _mm_unpackhi_epi32(rg, ba)); }
844 } else {
845 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg, ba));
846 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg, ba));
847 }
848 }
849
Brian Salomon217522c2019-06-11 15:55:30 -0400850 SI void load2(const float* ptr, size_t tail, F* r, F* g) {
851 F _01, _23;
852 if (__builtin_expect(tail, 0)) {
853 _01 = _23 = _mm_setzero_si128();
854 if ( true ) { _01 = _mm_loadl_pi(_01, (__m64 const*)(ptr + 0)); }
855 if (tail > 1) { _01 = _mm_loadh_pi(_01, (__m64 const*)(ptr + 2)); }
856 if (tail > 2) { _23 = _mm_loadl_pi(_23, (__m64 const*)(ptr + 4)); }
857 } else {
858 _01 = _mm_loadu_ps(ptr + 0);
859 _23 = _mm_loadu_ps(ptr + 4);
860 }
861 *r = _mm_shuffle_ps(_01, _23, 0x88);
862 *g = _mm_shuffle_ps(_01, _23, 0xDD);
863 }
864 SI void store2(float* ptr, size_t tail, F r, F g) {
865 F _01 = _mm_unpacklo_ps(r, g),
866 _23 = _mm_unpackhi_ps(r, g);
867 if (__builtin_expect(tail, 0)) {
868 if ( true ) { _mm_storel_pi((__m64*)(ptr + 0), _01); }
869 if (tail > 1) { _mm_storeh_pi((__m64*)(ptr + 2), _01); }
870 if (tail > 2) { _mm_storel_pi((__m64*)(ptr + 4), _23); }
871 } else {
872 _mm_storeu_ps(ptr + 0, _01);
873 _mm_storeu_ps(ptr + 4, _23);
874 }
875 }
876
Mike Kleinadc78d52018-01-01 09:06:37 -0500877 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
878 F _0, _1, _2, _3;
879 if (__builtin_expect(tail, 0)) {
880 _1 = _2 = _3 = _mm_setzero_si128();
881 if ( true ) { _0 = _mm_loadu_ps(ptr + 0); }
882 if (tail > 1) { _1 = _mm_loadu_ps(ptr + 4); }
883 if (tail > 2) { _2 = _mm_loadu_ps(ptr + 8); }
884 } else {
885 _0 = _mm_loadu_ps(ptr + 0);
886 _1 = _mm_loadu_ps(ptr + 4);
887 _2 = _mm_loadu_ps(ptr + 8);
888 _3 = _mm_loadu_ps(ptr +12);
889 }
890 _MM_TRANSPOSE4_PS(_0,_1,_2,_3);
891 *r = _0;
892 *g = _1;
893 *b = _2;
894 *a = _3;
895 }
896
897 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
898 _MM_TRANSPOSE4_PS(r,g,b,a);
899 if (__builtin_expect(tail, 0)) {
900 if ( true ) { _mm_storeu_ps(ptr + 0, r); }
901 if (tail > 1) { _mm_storeu_ps(ptr + 4, g); }
902 if (tail > 2) { _mm_storeu_ps(ptr + 8, b); }
903 } else {
904 _mm_storeu_ps(ptr + 0, r);
905 _mm_storeu_ps(ptr + 4, g);
906 _mm_storeu_ps(ptr + 8, b);
907 _mm_storeu_ps(ptr +12, a);
908 }
909 }
910#endif
911
912// We need to be a careful with casts.
913// (F)x means cast x to float in the portable path, but bit_cast x to float in the others.
914// These named casts and bit_cast() are always what they seem to be.
915#if defined(JUMPER_IS_SCALAR)
916 SI F cast (U32 v) { return (F)v; }
Brian Salomond608e222019-06-12 17:42:58 -0400917 SI F cast64(U64 v) { return (F)v; }
Mike Kleinadc78d52018-01-01 09:06:37 -0500918 SI U32 trunc_(F v) { return (U32)v; }
919 SI U32 expand(U16 v) { return (U32)v; }
920 SI U32 expand(U8 v) { return (U32)v; }
921#else
922 SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); }
Brian Salomond608e222019-06-12 17:42:58 -0400923 SI F cast64(U64 v) { return __builtin_convertvector( v, F); }
Mike Kleinadc78d52018-01-01 09:06:37 -0500924 SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); }
925 SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); }
926 SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); }
927#endif
928
929template <typename V>
930SI V if_then_else(I32 c, V t, V e) {
931 return bit_cast<V>(if_then_else(c, bit_cast<F>(t), bit_cast<F>(e)));
932}
933
934SI U16 bswap(U16 x) {
935#if defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41)
936 // Somewhat inexplicably Clang decides to do (x<<8) | (x>>8) in 32-bit lanes
937 // when generating code for SSE2 and SSE4.1. We'll do it manually...
938 auto v = widen_cast<__m128i>(x);
939 v = _mm_slli_epi16(v,8) | _mm_srli_epi16(v,8);
Mike Klein7a177b42019-06-17 17:17:47 -0500940 return sk_unaligned_load<U16>(&v);
Mike Kleinadc78d52018-01-01 09:06:37 -0500941#else
942 return (x<<8) | (x>>8);
943#endif
944}
945
946SI F fract(F v) { return v - floor_(v); }
947
948// See http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html.
949SI F approx_log2(F x) {
950 // e - 127 is a fair approximation of log2(x) in its own right...
951 F e = cast(bit_cast<U32>(x)) * (1.0f / (1<<23));
952
953 // ... but using the mantissa to refine its error is _much_ better.
954 F m = bit_cast<F>((bit_cast<U32>(x) & 0x007fffff) | 0x3f000000);
955 return e
956 - 124.225514990f
957 - 1.498030302f * m
958 - 1.725879990f / (0.3520887068f + m);
959}
Brian Osman11e6aa82019-10-16 13:58:42 -0400960
961SI F approx_log(F x) {
962 const float ln2 = 0.69314718f;
963 return ln2 * approx_log2(x);
964}
965
Mike Kleinadc78d52018-01-01 09:06:37 -0500966SI F approx_pow2(F x) {
967 F f = fract(x);
968 return bit_cast<F>(round(1.0f * (1<<23),
969 x + 121.274057500f
970 - 1.490129070f * f
971 + 27.728023300f / (4.84252568f - f)));
972}
973
Brian Osman11e6aa82019-10-16 13:58:42 -0400974SI F approx_exp(F x) {
975 const float log2_e = 1.4426950408889634074f;
976 return approx_pow2(log2_e * x);
977}
978
Mike Kleinadc78d52018-01-01 09:06:37 -0500979SI F approx_powf(F x, F y) {
Mike Klein229befe2018-10-26 12:07:57 -0400980#if defined(SK_LEGACY_APPROX_POWF_SPECIALCASE)
981 return if_then_else((x == 0) , 0
982#else
983 return if_then_else((x == 0)|(x == 1), x
984#endif
985 , approx_pow2(approx_log2(x) * y));
Mike Kleinadc78d52018-01-01 09:06:37 -0500986}
987
988SI F from_half(U16 h) {
Mike Klein7aacb0b2019-07-02 13:23:06 -0500989#if defined(JUMPER_IS_NEON) && defined(SK_CPU_ARM64) \
990 && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds.
Mike Kleinadc78d52018-01-01 09:06:37 -0500991 return vcvt_f32_f16(h);
992
993#elif defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
994 return _mm256_cvtph_ps(h);
995
996#else
997 // Remember, a half is 1-5-10 (sign-exponent-mantissa) with 15 exponent bias.
998 U32 sem = expand(h),
999 s = sem & 0x8000,
1000 em = sem ^ s;
1001
1002 // Convert to 1-8-23 float with 127 bias, flushing denorm halfs (including zero) to zero.
1003 auto denorm = (I32)em < 0x0400; // I32 comparison is often quicker, and always safe here.
1004 return if_then_else(denorm, F(0)
1005 , bit_cast<F>( (s<<16) + (em<<13) + ((127-15)<<23) ));
1006#endif
1007}
1008
1009SI U16 to_half(F f) {
Mike Klein7aacb0b2019-07-02 13:23:06 -05001010#if defined(JUMPER_IS_NEON) && defined(SK_CPU_ARM64) \
1011 && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds.
Mike Kleinadc78d52018-01-01 09:06:37 -05001012 return vcvt_f16_f32(f);
1013
1014#elif defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
1015 return _mm256_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
1016
1017#else
1018 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
1019 U32 sem = bit_cast<U32>(f),
1020 s = sem & 0x80000000,
1021 em = sem ^ s;
1022
1023 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
1024 auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here.
1025 return pack(if_then_else(denorm, U32(0)
1026 , (s>>16) + (em>>13) - ((127-15)<<10)));
1027#endif
1028}
1029
1030// Our fundamental vector depth is our pixel stride.
1031static const size_t N = sizeof(F) / sizeof(float);
1032
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001033// We're finally going to get to what a Stage function looks like!
Mike Klein0e4d0962017-09-27 11:04:34 -04001034// tail == 0 ~~> work on a full N pixels
Mike Kleinb5e48422017-05-30 18:09:29 -04001035// tail != 0 ~~> work on only the first tail pixels
Mike Klein0e4d0962017-09-27 11:04:34 -04001036// tail is always < N.
Mike Kleinf1b24e02017-07-27 12:31:34 -04001037
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001038// Any custom ABI to use for all (non-externally-facing) stage functions?
1039// Also decide here whether to use narrow (compromise) or wide (ideal) stages.
Mike Klein15eb1e92018-08-31 11:21:27 -04001040#if defined(SK_CPU_ARM32) && defined(JUMPER_IS_NEON)
Mike Klein1b9b7d52018-02-27 10:37:40 -05001041 // This lets us pass vectors more efficiently on 32-bit ARM.
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001042 // We can still only pass 16 floats, so best as 4x {r,g,b,a}.
Mike Klein1b9b7d52018-02-27 10:37:40 -05001043 #define ABI __attribute__((pcs("aapcs-vfp")))
Mike Klein1b9b7d52018-02-27 10:37:40 -05001044 #define JUMPER_NARROW_STAGES 1
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001045#elif 0 && defined(_MSC_VER) && defined(__clang__) && defined(__x86_64__)
1046 // SysV ABI makes it very sensible to use wide stages with clang-cl.
1047 // TODO: crashes during compilation :(
1048 #define ABI __attribute__((sysv_abi))
Mike Klein1b9b7d52018-02-27 10:37:40 -05001049 #define JUMPER_NARROW_STAGES 0
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001050#elif defined(_MSC_VER)
1051 // Even if not vectorized, this lets us pass {r,g,b,a} as registers,
1052 // instead of {b,a} on the stack. Narrow stages work best for __vectorcall.
1053 #define ABI __vectorcall
1054 #define JUMPER_NARROW_STAGES 1
Mike Klein15eb1e92018-08-31 11:21:27 -04001055#elif defined(__x86_64__) || defined(SK_CPU_ARM64)
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001056 // These platforms are ideal for wider stages, and their default ABI is ideal.
1057 #define ABI
1058 #define JUMPER_NARROW_STAGES 0
1059#else
1060 // 32-bit or unknown... shunt them down the narrow path.
1061 // Odds are these have few registers and are better off there.
1062 #define ABI
1063 #define JUMPER_NARROW_STAGES 1
Mike Klein1b9b7d52018-02-27 10:37:40 -05001064#endif
1065
1066#if JUMPER_NARROW_STAGES
Mike Kleinf1b24e02017-07-27 12:31:34 -04001067 struct Params {
Mike Kleinb4379132017-10-17 16:06:49 -04001068 size_t dx, dy, tail;
Mike Kleinf1b24e02017-07-27 12:31:34 -04001069 F dr,dg,db,da;
1070 };
Mike Klein376fd312017-12-11 16:53:26 -05001071 using Stage = void(ABI*)(Params*, void** program, F r, F g, F b, F a);
Mike Kleinf1b24e02017-07-27 12:31:34 -04001072#else
1073 // We keep program the second argument, so that it's passed in rsi for load_and_inc().
Mike Klein376fd312017-12-11 16:53:26 -05001074 using Stage = void(ABI*)(size_t tail, void** program, size_t dx, size_t dy, F,F,F,F, F,F,F,F);
Mike Kleinf1b24e02017-07-27 12:31:34 -04001075#endif
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001076
Mike Klein376fd312017-12-11 16:53:26 -05001077
Mike Klein1b9b7d52018-02-27 10:37:40 -05001078static void start_pipeline(size_t dx, size_t dy, size_t xlimit, size_t ylimit, void** program) {
Mike Klein376fd312017-12-11 16:53:26 -05001079 auto start = (Stage)load_and_inc(program);
Mike Kleinb4379132017-10-17 16:06:49 -04001080 const size_t x0 = dx;
1081 for (; dy < ylimit; dy++) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05001082 #if JUMPER_NARROW_STAGES
Mike Kleinb4379132017-10-17 16:06:49 -04001083 Params params = { x0,dy,0, 0,0,0,0 };
1084 while (params.dx + N <= xlimit) {
Mike Kleinabb8bb32017-09-27 11:12:01 -04001085 start(&params,program, 0,0,0,0);
Mike Kleinb4379132017-10-17 16:06:49 -04001086 params.dx += N;
Mike Kleinf1b24e02017-07-27 12:31:34 -04001087 }
Mike Kleinb4379132017-10-17 16:06:49 -04001088 if (size_t tail = xlimit - params.dx) {
Mike Kleinf1b24e02017-07-27 12:31:34 -04001089 params.tail = tail;
Mike Kleinabb8bb32017-09-27 11:12:01 -04001090 start(&params,program, 0,0,0,0);
Mike Kleinf1b24e02017-07-27 12:31:34 -04001091 }
1092 #else
Mike Kleinb4379132017-10-17 16:06:49 -04001093 dx = x0;
1094 while (dx + N <= xlimit) {
1095 start(0,program,dx,dy, 0,0,0,0, 0,0,0,0);
1096 dx += N;
Mike Klein45c16fa2017-07-18 18:15:13 -04001097 }
Mike Kleinb4379132017-10-17 16:06:49 -04001098 if (size_t tail = xlimit - dx) {
1099 start(tail,program,dx,dy, 0,0,0,0, 0,0,0,0);
Mike Klein45c16fa2017-07-18 18:15:13 -04001100 }
Mike Kleinf1b24e02017-07-27 12:31:34 -04001101 #endif
Mike Klein3b92b692017-07-18 11:30:25 -04001102 }
1103}
1104
Mike Klein1b9b7d52018-02-27 10:37:40 -05001105#if JUMPER_NARROW_STAGES
1106 #define STAGE(name, ...) \
1107 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
1108 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001109 static void ABI name(Params* params, void** program, \
Mike Klein1b9b7d52018-02-27 10:37:40 -05001110 F r, F g, F b, F a) { \
1111 name##_k(Ctx{program},params->dx,params->dy,params->tail, r,g,b,a, \
1112 params->dr, params->dg, params->db, params->da); \
1113 auto next = (Stage)load_and_inc(program); \
1114 next(params,program, r,g,b,a); \
1115 } \
1116 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleinf1b24e02017-07-27 12:31:34 -04001117 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1118#else
Mike Klein1b9b7d52018-02-27 10:37:40 -05001119 #define STAGE(name, ...) \
1120 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
1121 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001122 static void ABI name(size_t tail, void** program, size_t dx, size_t dy, \
Mike Klein1b9b7d52018-02-27 10:37:40 -05001123 F r, F g, F b, F a, F dr, F dg, F db, F da) { \
1124 name##_k(Ctx{program},dx,dy,tail, r,g,b,a, dr,dg,db,da); \
1125 auto next = (Stage)load_and_inc(program); \
1126 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
1127 } \
1128 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleinf1b24e02017-07-27 12:31:34 -04001129 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1130#endif
Mike Kleinb5e48422017-05-30 18:09:29 -04001131
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001132
1133// just_return() is a simple no-op stage that only exists to end the chain,
1134// returning back up to start_pipeline(), and from there to the caller.
Mike Klein1b9b7d52018-02-27 10:37:40 -05001135#if JUMPER_NARROW_STAGES
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001136 static void ABI just_return(Params*, void**, F,F,F,F) {}
Mike Kleinf1b24e02017-07-27 12:31:34 -04001137#else
Mike Klein4d4b3aa2018-03-21 13:07:35 -04001138 static void ABI just_return(size_t, void**, size_t,size_t, F,F,F,F, F,F,F,F) {}
Mike Kleinf1b24e02017-07-27 12:31:34 -04001139#endif
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001140
1141
Mike Klein8a823fa2017-04-05 17:29:26 -04001142// We could start defining normal Stages now. But first, some helper functions.
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001143
1144// These load() and store() methods are tail-aware,
1145// but focus mainly on keeping the at-stride tail==0 case fast.
Mike Kleine1caee12017-02-15 13:31:12 -05001146
Mike Kleinc31858b2017-03-01 13:07:40 -05001147template <typename V, typename T>
Mike Klein64b97482017-03-14 17:35:04 -07001148SI V load(const T* src, size_t tail) {
Mike Kleind6e12862017-08-28 12:18:26 -04001149#if !defined(JUMPER_IS_SCALAR)
Mike Klein0e4d0962017-09-27 11:04:34 -04001150 __builtin_assume(tail < N);
Mike Kleinc31858b2017-03-01 13:07:40 -05001151 if (__builtin_expect(tail, 0)) {
1152 V v{}; // Any inactive lanes are zeroed.
Mike Kleinc4fcbed2017-06-26 16:12:48 -04001153 switch (tail) {
1154 case 7: v[6] = src[6];
1155 case 6: v[5] = src[5];
1156 case 5: v[4] = src[4];
1157 case 4: memcpy(&v, src, 4*sizeof(T)); break;
1158 case 3: v[2] = src[2];
1159 case 2: memcpy(&v, src, 2*sizeof(T)); break;
1160 case 1: memcpy(&v, src, 1*sizeof(T)); break;
Mike Kleinc31858b2017-03-01 13:07:40 -05001161 }
1162 return v;
1163 }
1164#endif
Mike Klein7a177b42019-06-17 17:17:47 -05001165 return sk_unaligned_load<V>(src);
Mike Kleinc31858b2017-03-01 13:07:40 -05001166}
1167
Mike Kleinc31858b2017-03-01 13:07:40 -05001168template <typename V, typename T>
Mike Klein64b97482017-03-14 17:35:04 -07001169SI void store(T* dst, V v, size_t tail) {
Mike Kleind6e12862017-08-28 12:18:26 -04001170#if !defined(JUMPER_IS_SCALAR)
Mike Klein0e4d0962017-09-27 11:04:34 -04001171 __builtin_assume(tail < N);
Mike Kleinc31858b2017-03-01 13:07:40 -05001172 if (__builtin_expect(tail, 0)) {
Mike Kleinc4fcbed2017-06-26 16:12:48 -04001173 switch (tail) {
1174 case 7: dst[6] = v[6];
1175 case 6: dst[5] = v[5];
1176 case 5: dst[4] = v[4];
1177 case 4: memcpy(dst, &v, 4*sizeof(T)); break;
1178 case 3: dst[2] = v[2];
1179 case 2: memcpy(dst, &v, 2*sizeof(T)); break;
1180 case 1: memcpy(dst, &v, 1*sizeof(T)); break;
Mike Kleinc31858b2017-03-01 13:07:40 -05001181 }
1182 return;
1183 }
1184#endif
Mike Klein7a177b42019-06-17 17:17:47 -05001185 sk_unaligned_store(dst, v);
Mike Kleinc31858b2017-03-01 13:07:40 -05001186}
1187
Mike Klein40de6da2017-04-07 13:09:29 -04001188SI F from_byte(U8 b) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001189 return cast(expand(b)) * (1/255.0f);
Mike Klein40de6da2017-04-07 13:09:29 -04001190}
Brian Salomon217522c2019-06-11 15:55:30 -04001191SI F from_short(U16 s) {
1192 return cast(expand(s)) * (1/65535.0f);
1193}
Mike Klein64b97482017-03-14 17:35:04 -07001194SI void from_565(U16 _565, F* r, F* g, F* b) {
Mike Klein3f81f372017-02-23 13:03:57 -05001195 U32 wide = expand(_565);
Mike Kleinfe560a82017-05-01 12:56:35 -04001196 *r = cast(wide & (31<<11)) * (1.0f / (31<<11));
1197 *g = cast(wide & (63<< 5)) * (1.0f / (63<< 5));
1198 *b = cast(wide & (31<< 0)) * (1.0f / (31<< 0));
Mike Kleine1caee12017-02-15 13:31:12 -05001199}
Mike Kleinf809fef2017-03-31 13:52:45 -04001200SI void from_4444(U16 _4444, F* r, F* g, F* b, F* a) {
1201 U32 wide = expand(_4444);
Mike Kleinfe560a82017-05-01 12:56:35 -04001202 *r = cast(wide & (15<<12)) * (1.0f / (15<<12));
1203 *g = cast(wide & (15<< 8)) * (1.0f / (15<< 8));
1204 *b = cast(wide & (15<< 4)) * (1.0f / (15<< 4));
1205 *a = cast(wide & (15<< 0)) * (1.0f / (15<< 0));
Mike Kleinf809fef2017-03-31 13:52:45 -04001206}
Mike Kleindec4ea82017-04-06 15:04:05 -04001207SI void from_8888(U32 _8888, F* r, F* g, F* b, F* a) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001208 *r = cast((_8888 ) & 0xff) * (1/255.0f);
1209 *g = cast((_8888 >> 8) & 0xff) * (1/255.0f);
1210 *b = cast((_8888 >> 16) & 0xff) * (1/255.0f);
1211 *a = cast((_8888 >> 24) ) * (1/255.0f);
Mike Kleindec4ea82017-04-06 15:04:05 -04001212}
Brian Salomon217522c2019-06-11 15:55:30 -04001213SI void from_88(U16 _88, F* r, F* g) {
1214 U32 wide = expand(_88);
1215 *r = cast((wide ) & 0xff) * (1/255.0f);
1216 *g = cast((wide >> 8) & 0xff) * (1/255.0f);
1217}
Mike Kleinac568a92018-01-25 09:09:32 -05001218SI void from_1010102(U32 rgba, F* r, F* g, F* b, F* a) {
1219 *r = cast((rgba ) & 0x3ff) * (1/1023.0f);
1220 *g = cast((rgba >> 10) & 0x3ff) * (1/1023.0f);
1221 *b = cast((rgba >> 20) & 0x3ff) * (1/1023.0f);
1222 *a = cast((rgba >> 30) ) * (1/ 3.0f);
1223}
Brian Salomon217522c2019-06-11 15:55:30 -04001224SI void from_1616(U32 _1616, F* r, F* g) {
1225 *r = cast((_1616 ) & 0xffff) * (1/65535.0f);
1226 *g = cast((_1616 >> 16) & 0xffff) * (1/65535.0f);
1227}
Brian Salomond608e222019-06-12 17:42:58 -04001228SI void from_16161616(U64 _16161616, F* r, F* g, F* b, F* a) {
1229 *r = cast64((_16161616 ) & 0xffff) * (1/65535.0f);
1230 *g = cast64((_16161616 >> 16) & 0xffff) * (1/65535.0f);
1231 *b = cast64((_16161616 >> 32) & 0xffff) * (1/65535.0f);
1232 *a = cast64((_16161616 >> 48) & 0xffff) * (1/65535.0f);
1233}
Mike Kleindec4ea82017-04-06 15:04:05 -04001234
Mike Kleinb4379132017-10-17 16:06:49 -04001235// Used by load_ and store_ stages to get to the right (dx,dy) starting point of contiguous memory.
Mike Klein45c16fa2017-07-18 18:15:13 -04001236template <typename T>
Mike Kleinb11ab572018-10-24 06:42:14 -04001237SI T* ptr_at_xy(const SkRasterPipeline_MemoryCtx* ctx, size_t dx, size_t dy) {
Mike Kleinb4379132017-10-17 16:06:49 -04001238 return (T*)ctx->pixels + dy*ctx->stride + dx;
Mike Klein45c16fa2017-07-18 18:15:13 -04001239}
1240
Mike Klein1fa9c432017-12-11 09:59:47 -05001241// clamp v to [0,limit).
1242SI F clamp(F v, F limit) {
1243 F inclusive = bit_cast<F>( bit_cast<U32>(limit) - 1 ); // Exclusive -> inclusive.
1244 return min(max(0, v), inclusive);
1245}
1246
Mike Klein45c16fa2017-07-18 18:15:13 -04001247// Used by gather_ stages to calculate the base pointer and a vector of indices to load.
Mike Kleindec4ea82017-04-06 15:04:05 -04001248template <typename T>
Mike Kleinb11ab572018-10-24 06:42:14 -04001249SI U32 ix_and_ptr(T** ptr, const SkRasterPipeline_GatherCtx* ctx, F x, F y) {
Mike Kleinf3b4e162017-09-22 15:32:59 -04001250 x = clamp(x, ctx->width);
1251 y = clamp(y, ctx->height);
1252
Mike Kleindec4ea82017-04-06 15:04:05 -04001253 *ptr = (const T*)ctx->pixels;
1254 return trunc_(y)*ctx->stride + trunc_(x);
1255}
Mike Kleine1caee12017-02-15 13:31:12 -05001256
Mike Klein37155d42017-12-15 09:55:03 -05001257// We often have a nominally [0,1] float value we need to scale and convert to an integer,
1258// whether for a table lookup or to pack back down into bytes for storage.
1259//
1260// In practice, especially when dealing with interesting color spaces, that notionally
1261// [0,1] float may be out of [0,1] range. Unorms cannot represent that, so we must clamp.
1262//
1263// You can adjust the expected input to [0,bias] by tweaking that parameter.
1264SI U32 to_unorm(F v, F scale, F bias = 1.0f) {
1265 // TODO: platform-specific implementations to to_unorm(), removing round() entirely?
1266 // Any time we use round() we probably want to use to_unorm().
1267 return round(min(max(0, v), bias), scale);
1268}
1269
Mike Reeddfc0e912018-02-16 12:40:18 -05001270SI I32 cond_to_mask(I32 cond) { return if_then_else(cond, I32(~0), I32(0)); }
1271
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001272// Now finally, normal Stages!
Mike Kleine1caee12017-02-15 13:31:12 -05001273
Mike Kleine8de0242018-03-10 12:37:11 -05001274STAGE(seed_shader, Ctx::None) {
1275 static const float iota[] = {
1276 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
1277 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
1278 };
Mike Kleinb4379132017-10-17 16:06:49 -04001279 // It's important for speed to explicitly cast(dx) and cast(dy),
Mike Kleine1caee12017-02-15 13:31:12 -05001280 // which has the effect of splatting them to vectors before converting to floats.
1281 // On Intel this breaks a data dependency on previous loop iterations' registers.
Mike Klein7a177b42019-06-17 17:17:47 -05001282 r = cast(dx) + sk_unaligned_load<F>(iota);
Mike Kleinb4379132017-10-17 16:06:49 -04001283 g = cast(dy) + 0.5f;
Mike Klein2229b572017-04-21 10:30:29 -04001284 b = 1.0f;
Mike Kleine1caee12017-02-15 13:31:12 -05001285 a = 0;
1286 dr = dg = db = da = 0;
1287}
1288
Mike Kleinf7729c22017-09-27 11:42:30 -04001289STAGE(dither, const float* rate) {
Mike Kleinb4379132017-10-17 16:06:49 -04001290 // Get [(dx,dy), (dx+1,dy), (dx+2,dy), ...] loaded up in integer vectors.
Mike Klein856b3c32017-08-29 13:38:09 -04001291 uint32_t iota[] = {0,1,2,3,4,5,6,7};
Mike Klein7a177b42019-06-17 17:17:47 -05001292 U32 X = dx + sk_unaligned_load<U32>(iota),
Mike Kleinb4379132017-10-17 16:06:49 -04001293 Y = dy;
Mike Klein581e6982017-05-03 13:05:13 -04001294
1295 // We're doing 8x8 ordered dithering, see https://en.wikipedia.org/wiki/Ordered_dithering.
1296 // In this case n=8 and we're using the matrix that looks like 1/64 x [ 0 48 12 60 ... ].
1297
1298 // We only need X and X^Y from here on, so it's easier to just think of that as "Y".
1299 Y ^= X;
1300
1301 // We'll mix the bottom 3 bits of each of X and Y to make 6 bits,
1302 // for 2^6 == 64 == 8x8 matrix values. If X=abc and Y=def, we make fcebda.
1303 U32 M = (Y & 1) << 5 | (X & 1) << 4
1304 | (Y & 2) << 2 | (X & 2) << 1
1305 | (Y & 4) >> 1 | (X & 4) >> 2;
1306
Mike Kleindb711c92017-05-03 17:57:48 -04001307 // Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as 0.5-epsilon.
1308 // We want to make sure our dither is less than 0.5 in either direction to keep exact values
1309 // like 0 and 1 unchanged after rounding.
1310 F dither = cast(M) * (2/128.0f) - (63/128.0f);
Mike Klein581e6982017-05-03 13:05:13 -04001311
Mike Kleinf7729c22017-09-27 11:42:30 -04001312 r += *rate*dither;
1313 g += *rate*dither;
1314 b += *rate*dither;
Mike Klein7e68bc92017-05-16 12:03:15 -04001315
1316 r = max(0, min(r, a));
1317 g = max(0, min(g, a));
1318 b = max(0, min(b, a));
Mike Klein581e6982017-05-03 13:05:13 -04001319}
1320
Mike Reed9959f722017-05-15 09:34:22 -04001321// load 4 floats from memory, and splat them into r,g,b,a
Mike Kleinb11ab572018-10-24 06:42:14 -04001322STAGE(uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
Mike Klein1a2e3e12017-08-03 11:24:13 -04001323 r = c->r;
1324 g = c->g;
1325 b = c->b;
1326 a = c->a;
Mike Kleine1caee12017-02-15 13:31:12 -05001327}
Mike Kleinb11ab572018-10-24 06:42:14 -04001328STAGE(unbounded_uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
Mike Kleincd3e13a2018-07-10 15:52:06 +00001329 r = c->r;
1330 g = c->g;
1331 b = c->b;
1332 a = c->a;
1333}
Mike Reed9318a6c2019-08-16 16:16:25 -04001334// load 4 floats from memory, and splat them into dr,dg,db,da
1335STAGE(uniform_color_dst, const SkRasterPipeline_UniformColorCtx* c) {
1336 dr = c->r;
1337 dg = c->g;
1338 db = c->b;
1339 da = c->a;
1340}
Mike Kleine1caee12017-02-15 13:31:12 -05001341
Mike Reedc91e3872017-07-05 14:12:37 -04001342// splats opaque-black into r,g,b,a
Mike Kleinf7729c22017-09-27 11:42:30 -04001343STAGE(black_color, Ctx::None) {
Mike Reedc91e3872017-07-05 14:12:37 -04001344 r = g = b = 0.0f;
1345 a = 1.0f;
1346}
1347
Mike Kleinf7729c22017-09-27 11:42:30 -04001348STAGE(white_color, Ctx::None) {
Mike Reedc91e3872017-07-05 14:12:37 -04001349 r = g = b = a = 1.0f;
1350}
1351
Mike Reed9959f722017-05-15 09:34:22 -04001352// load registers r,g,b,a from context (mirrors store_rgba)
Mike Reed5e398c22019-03-08 11:50:35 -05001353STAGE(load_src, const float* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05001354 r = sk_unaligned_load<F>(ptr + 0*N);
1355 g = sk_unaligned_load<F>(ptr + 1*N);
1356 b = sk_unaligned_load<F>(ptr + 2*N);
1357 a = sk_unaligned_load<F>(ptr + 3*N);
Mike Reed9959f722017-05-15 09:34:22 -04001358}
1359
1360// store registers r,g,b,a into context (mirrors load_rgba)
Mike Reed5e398c22019-03-08 11:50:35 -05001361STAGE(store_src, float* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05001362 sk_unaligned_store(ptr + 0*N, r);
1363 sk_unaligned_store(ptr + 1*N, g);
1364 sk_unaligned_store(ptr + 2*N, b);
1365 sk_unaligned_store(ptr + 3*N, a);
Mike Reed9959f722017-05-15 09:34:22 -04001366}
1367
Mike Reed5e398c22019-03-08 11:50:35 -05001368// load registers dr,dg,db,da from context (mirrors store_dst)
1369STAGE(load_dst, const float* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05001370 dr = sk_unaligned_load<F>(ptr + 0*N);
1371 dg = sk_unaligned_load<F>(ptr + 1*N);
1372 db = sk_unaligned_load<F>(ptr + 2*N);
1373 da = sk_unaligned_load<F>(ptr + 3*N);
Mike Reed5e398c22019-03-08 11:50:35 -05001374}
1375
1376// store registers dr,dg,db,da into context (mirrors load_dst)
1377STAGE(store_dst, float* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05001378 sk_unaligned_store(ptr + 0*N, dr);
1379 sk_unaligned_store(ptr + 1*N, dg);
1380 sk_unaligned_store(ptr + 2*N, db);
1381 sk_unaligned_store(ptr + 3*N, da);
Mike Reed5e398c22019-03-08 11:50:35 -05001382}
1383
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001384// Most blend modes apply the same logic to each channel.
Mike Kleinaaca1e42017-03-31 09:29:01 -04001385#define BLEND_MODE(name) \
1386 SI F name##_channel(F s, F d, F sa, F da); \
Mike Kleinf7729c22017-09-27 11:42:30 -04001387 STAGE(name, Ctx::None) { \
Mike Kleinaaca1e42017-03-31 09:29:01 -04001388 r = name##_channel(r,dr,a,da); \
1389 g = name##_channel(g,dg,a,da); \
1390 b = name##_channel(b,db,a,da); \
1391 a = name##_channel(a,da,a,da); \
1392 } \
1393 SI F name##_channel(F s, F d, F sa, F da)
Mike Kleine1caee12017-02-15 13:31:12 -05001394
Mike Kleinfe560a82017-05-01 12:56:35 -04001395SI F inv(F x) { return 1.0f - x; }
Mike Klein66b09ab2017-03-31 10:29:40 -04001396SI F two(F x) { return x + x; }
Yuqian Li7741c752017-12-11 14:17:47 -05001397
Mike Kleine1caee12017-02-15 13:31:12 -05001398
Mike Kleinaaca1e42017-03-31 09:29:01 -04001399BLEND_MODE(clear) { return 0; }
1400BLEND_MODE(srcatop) { return s*da + d*inv(sa); }
1401BLEND_MODE(dstatop) { return d*sa + s*inv(da); }
1402BLEND_MODE(srcin) { return s * da; }
1403BLEND_MODE(dstin) { return d * sa; }
1404BLEND_MODE(srcout) { return s * inv(da); }
1405BLEND_MODE(dstout) { return d * inv(sa); }
1406BLEND_MODE(srcover) { return mad(d, inv(sa), s); }
1407BLEND_MODE(dstover) { return mad(s, inv(da), d); }
1408
1409BLEND_MODE(modulate) { return s*d; }
1410BLEND_MODE(multiply) { return s*inv(da) + d*inv(sa) + s*d; }
Mike Kleinb90c0802019-03-15 14:03:41 +00001411BLEND_MODE(plus_) { return min(s + d, 1.0f); } // We can clamp to either 1 or sa.
Mike Kleinaaca1e42017-03-31 09:29:01 -04001412BLEND_MODE(screen) { return s + d - s*d; }
1413BLEND_MODE(xor_) { return s*inv(da) + d*inv(sa); }
Mike Klein66b09ab2017-03-31 10:29:40 -04001414#undef BLEND_MODE
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001415
1416// Most other blend modes apply the same logic to colors, and srcover to alpha.
Mike Klein66b09ab2017-03-31 10:29:40 -04001417#define BLEND_MODE(name) \
1418 SI F name##_channel(F s, F d, F sa, F da); \
Mike Kleinf7729c22017-09-27 11:42:30 -04001419 STAGE(name, Ctx::None) { \
Mike Klein66b09ab2017-03-31 10:29:40 -04001420 r = name##_channel(r,dr,a,da); \
1421 g = name##_channel(g,dg,a,da); \
1422 b = name##_channel(b,db,a,da); \
1423 a = mad(da, inv(a), a); \
1424 } \
1425 SI F name##_channel(F s, F d, F sa, F da)
1426
1427BLEND_MODE(darken) { return s + d - max(s*da, d*sa) ; }
1428BLEND_MODE(lighten) { return s + d - min(s*da, d*sa) ; }
1429BLEND_MODE(difference) { return s + d - two(min(s*da, d*sa)); }
1430BLEND_MODE(exclusion) { return s + d - two(s*d); }
1431
Mike Klein61b84162017-03-31 11:48:14 -04001432BLEND_MODE(colorburn) {
Florin Malita59a62ed2017-08-23 12:08:37 -04001433 return if_then_else(d == da, d + s*inv(da),
1434 if_then_else(s == 0, /* s + */ d*inv(sa),
1435 sa*(da - min(da, (da-d)*sa*rcp(s))) + s*inv(da) + d*inv(sa)));
Mike Klein61b84162017-03-31 11:48:14 -04001436}
1437BLEND_MODE(colordodge) {
Florin Malita59a62ed2017-08-23 12:08:37 -04001438 return if_then_else(d == 0, /* d + */ s*inv(da),
1439 if_then_else(s == sa, s + d*inv(sa),
1440 sa*min(da, (d*sa)*rcp(sa - s)) + s*inv(da) + d*inv(sa)));
Mike Klein61b84162017-03-31 11:48:14 -04001441}
1442BLEND_MODE(hardlight) {
1443 return s*inv(da) + d*inv(sa)
1444 + if_then_else(two(s) <= sa, two(s*d), sa*da - two((da-d)*(sa-s)));
1445}
1446BLEND_MODE(overlay) {
1447 return s*inv(da) + d*inv(sa)
1448 + if_then_else(two(d) <= da, two(s*d), sa*da - two((da-d)*(sa-s)));
1449}
1450
1451BLEND_MODE(softlight) {
1452 F m = if_then_else(da > 0, d / da, 0),
1453 s2 = two(s),
1454 m4 = two(two(m));
1455
1456 // The logic forks three ways:
1457 // 1. dark src?
1458 // 2. light src, dark dst?
1459 // 3. light src, light dst?
Mike Kleinfe560a82017-05-01 12:56:35 -04001460 F darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
1461 darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
1462 liteDst = rcp(rsqrt(m)) - m, // Used in case 3.
Mike Klein61b84162017-03-31 11:48:14 -04001463 liteSrc = d*sa + da*(s2 - sa) * if_then_else(two(two(d)) <= da, darkDst, liteDst); // 2 or 3?
1464 return s*inv(da) + d*inv(sa) + if_then_else(s2 <= sa, darkSrc, liteSrc); // 1 or (2 or 3)?
1465}
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001466#undef BLEND_MODE
Mike Klein61b84162017-03-31 11:48:14 -04001467
Mike Kleinbb338332017-05-04 12:42:52 -04001468// We're basing our implemenation of non-separable blend modes on
1469// https://www.w3.org/TR/compositing-1/#blendingnonseparable.
1470// and
1471// https://www.khronos.org/registry/OpenGL/specs/es/3.2/es_spec_3.2.pdf
1472// They're equivalent, but ES' math has been better simplified.
Mike Klein08aa88d2017-05-12 12:59:24 -04001473//
1474// Anything extra we add beyond that is to make the math work with premul inputs.
Mike Kleinbb338332017-05-04 12:42:52 -04001475
Mike Klein5d835d02019-10-16 13:28:55 -05001476SI F sat(F r, F g, F b) { return max(r, max(g,b)) - min(r, min(g,b)); }
Mike Kleinbb338332017-05-04 12:42:52 -04001477SI F lum(F r, F g, F b) { return r*0.30f + g*0.59f + b*0.11f; }
1478
1479SI void set_sat(F* r, F* g, F* b, F s) {
Mike Klein5d835d02019-10-16 13:28:55 -05001480 F mn = min(*r, min(*g,*b)),
1481 mx = max(*r, max(*g,*b)),
Mike Kleinbb338332017-05-04 12:42:52 -04001482 sat = mx - mn;
1483
1484 // Map min channel to 0, max channel to s, and scale the middle proportionally.
1485 auto scale = [=](F c) {
1486 return if_then_else(sat == 0, 0, (c - mn) * s / sat);
1487 };
1488 *r = scale(*r);
1489 *g = scale(*g);
1490 *b = scale(*b);
1491}
Mike Klein08aa88d2017-05-12 12:59:24 -04001492SI void set_lum(F* r, F* g, F* b, F l) {
1493 F diff = l - lum(*r, *g, *b);
1494 *r += diff;
1495 *g += diff;
1496 *b += diff;
1497}
1498SI void clip_color(F* r, F* g, F* b, F a) {
Mike Klein5d835d02019-10-16 13:28:55 -05001499 F mn = min(*r, min(*g, *b)),
1500 mx = max(*r, max(*g, *b)),
Mike Kleinbb338332017-05-04 12:42:52 -04001501 l = lum(*r, *g, *b);
1502
1503 auto clip = [=](F c) {
1504 c = if_then_else(mn >= 0, c, l + (c - l) * ( l) / (l - mn) );
Mike Klein08aa88d2017-05-12 12:59:24 -04001505 c = if_then_else(mx > a, l + (c - l) * (a - l) / (mx - l), c);
Mike Kleinbb338332017-05-04 12:42:52 -04001506 c = max(c, 0); // Sometimes without this we may dip just a little negative.
1507 return c;
1508 };
1509 *r = clip(*r);
1510 *g = clip(*g);
1511 *b = clip(*b);
1512}
Mike Kleinbb338332017-05-04 12:42:52 -04001513
Mike Kleinf7729c22017-09-27 11:42:30 -04001514STAGE(hue, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001515 F R = r*a,
1516 G = g*a,
1517 B = b*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001518
Mike Klein08aa88d2017-05-12 12:59:24 -04001519 set_sat(&R, &G, &B, sat(dr,dg,db)*a);
1520 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
1521 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001522
Mike Klein08aa88d2017-05-12 12:59:24 -04001523 r = r*inv(da) + dr*inv(a) + R;
1524 g = g*inv(da) + dg*inv(a) + G;
1525 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001526 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001527}
Mike Kleinf7729c22017-09-27 11:42:30 -04001528STAGE(saturation, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001529 F R = dr*a,
1530 G = dg*a,
1531 B = db*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001532
Mike Klein08aa88d2017-05-12 12:59:24 -04001533 set_sat(&R, &G, &B, sat( r, g, b)*da);
1534 set_lum(&R, &G, &B, lum(dr,dg,db)* a); // (This is not redundant.)
1535 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001536
Mike Klein08aa88d2017-05-12 12:59:24 -04001537 r = r*inv(da) + dr*inv(a) + R;
1538 g = g*inv(da) + dg*inv(a) + G;
1539 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001540 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001541}
Mike Kleinf7729c22017-09-27 11:42:30 -04001542STAGE(color, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001543 F R = r*da,
1544 G = g*da,
1545 B = b*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001546
Mike Klein08aa88d2017-05-12 12:59:24 -04001547 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
1548 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001549
Mike Klein08aa88d2017-05-12 12:59:24 -04001550 r = r*inv(da) + dr*inv(a) + R;
1551 g = g*inv(da) + dg*inv(a) + G;
1552 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001553 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001554}
Mike Kleinf7729c22017-09-27 11:42:30 -04001555STAGE(luminosity, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001556 F R = dr*a,
1557 G = dg*a,
1558 B = db*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001559
Mike Klein08aa88d2017-05-12 12:59:24 -04001560 set_lum(&R, &G, &B, lum(r,g,b)*da);
1561 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001562
Mike Klein08aa88d2017-05-12 12:59:24 -04001563 r = r*inv(da) + dr*inv(a) + R;
1564 g = g*inv(da) + dg*inv(a) + G;
1565 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001566 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001567}
1568
Mike Kleinb11ab572018-10-24 06:42:14 -04001569STAGE(srcover_rgba_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001570 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
Mike Klein50626262017-05-25 13:06:57 -04001571
1572 U32 dst = load<U32>(ptr, tail);
1573 dr = cast((dst ) & 0xff);
1574 dg = cast((dst >> 8) & 0xff);
1575 db = cast((dst >> 16) & 0xff);
1576 da = cast((dst >> 24) );
1577 // {dr,dg,db,da} are in [0,255]
Mike Klein37155d42017-12-15 09:55:03 -05001578 // { r, g, b, a} are in [0, 1] (but may be out of gamut)
Mike Klein50626262017-05-25 13:06:57 -04001579
1580 r = mad(dr, inv(a), r*255.0f);
1581 g = mad(dg, inv(a), g*255.0f);
1582 b = mad(db, inv(a), b*255.0f);
1583 a = mad(da, inv(a), a*255.0f);
Mike Klein37155d42017-12-15 09:55:03 -05001584 // { r, g, b, a} are now in [0,255] (but may be out of gamut)
Mike Klein50626262017-05-25 13:06:57 -04001585
Mike Klein37155d42017-12-15 09:55:03 -05001586 // to_unorm() clamps back to gamut. Scaling by 1 since we're already 255-biased.
1587 dst = to_unorm(r, 1, 255)
1588 | to_unorm(g, 1, 255) << 8
1589 | to_unorm(b, 1, 255) << 16
1590 | to_unorm(a, 1, 255) << 24;
Mike Klein50626262017-05-25 13:06:57 -04001591 store(ptr, dst, tail);
1592}
1593
Mike Kleinf7729c22017-09-27 11:42:30 -04001594STAGE(clamp_0, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001595 r = max(r, 0);
1596 g = max(g, 0);
1597 b = max(b, 0);
1598 a = max(a, 0);
1599}
1600
Mike Kleinf7729c22017-09-27 11:42:30 -04001601STAGE(clamp_1, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001602 r = min(r, 1.0f);
1603 g = min(g, 1.0f);
1604 b = min(b, 1.0f);
1605 a = min(a, 1.0f);
Mike Kleine1caee12017-02-15 13:31:12 -05001606}
1607
Mike Kleinf7729c22017-09-27 11:42:30 -04001608STAGE(clamp_a, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001609 a = min(a, 1.0f);
Mike Kleine1caee12017-02-15 13:31:12 -05001610 r = min(r, a);
1611 g = min(g, a);
1612 b = min(b, a);
1613}
1614
Mike Kleineb50f432018-09-07 11:08:53 -04001615STAGE(clamp_gamut, Ctx::None) {
1616 // If you're using this stage, a should already be in [0,1].
1617 r = min(max(r, 0), a);
1618 g = min(max(g, 0), a);
1619 b = min(max(b, 0), a);
1620}
1621
Mike Kleinf7729c22017-09-27 11:42:30 -04001622STAGE(set_rgb, const float* rgb) {
Mike Kleind9e82252017-02-22 14:17:32 -05001623 r = rgb[0];
1624 g = rgb[1];
1625 b = rgb[2];
1626}
Mike Kleinbe569492018-09-14 09:34:21 -04001627STAGE(unbounded_set_rgb, const float* rgb) {
1628 r = rgb[0];
1629 g = rgb[1];
1630 b = rgb[2];
1631}
Mike Klein1a3eb522018-10-18 10:11:00 -04001632
Mike Kleinf7729c22017-09-27 11:42:30 -04001633STAGE(swap_rb, Ctx::None) {
Mike Kleind9e82252017-02-22 14:17:32 -05001634 auto tmp = r;
1635 r = b;
1636 b = tmp;
1637}
Mike Klein1a3eb522018-10-18 10:11:00 -04001638STAGE(swap_rb_dst, Ctx::None) {
1639 auto tmp = dr;
1640 dr = db;
1641 db = tmp;
1642}
Mike Kleind9e82252017-02-22 14:17:32 -05001643
Mike Kleinf7729c22017-09-27 11:42:30 -04001644STAGE(move_src_dst, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001645 dr = r;
1646 dg = g;
1647 db = b;
1648 da = a;
1649}
Mike Kleinf7729c22017-09-27 11:42:30 -04001650STAGE(move_dst_src, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001651 r = dr;
1652 g = dg;
1653 b = db;
1654 a = da;
1655}
1656
Mike Kleinf7729c22017-09-27 11:42:30 -04001657STAGE(premul, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001658 r = r * a;
1659 g = g * a;
1660 b = b * a;
1661}
Mike Kleinf7729c22017-09-27 11:42:30 -04001662STAGE(premul_dst, Ctx::None) {
Mike Reed883c9bc2017-07-19 10:57:53 -04001663 dr = dr * da;
1664 dg = dg * da;
1665 db = db * da;
1666}
Mike Kleinf7729c22017-09-27 11:42:30 -04001667STAGE(unpremul, Ctx::None) {
Mike Kleina65f2f02017-10-11 13:05:24 -04001668 float inf = bit_cast<float>(0x7f800000);
1669 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0);
Mike Klein08aa88d2017-05-12 12:59:24 -04001670 r *= scale;
1671 g *= scale;
1672 b *= scale;
Mike Kleine1caee12017-02-15 13:31:12 -05001673}
1674
Mike Kleinac568a92018-01-25 09:09:32 -05001675STAGE(force_opaque , Ctx::None) { a = 1; }
1676STAGE(force_opaque_dst, Ctx::None) { da = 1; }
1677
Mike Kleinf7729c22017-09-27 11:42:30 -04001678STAGE(rgb_to_hsl, Ctx::None) {
Mike Klein5d835d02019-10-16 13:28:55 -05001679 F mx = max(r, max(g,b)),
1680 mn = min(r, min(g,b)),
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001681 d = mx - mn,
Mike Kleinfe560a82017-05-01 12:56:35 -04001682 d_rcp = 1.0f / d;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001683
Mike Kleinfe560a82017-05-01 12:56:35 -04001684 F h = (1/6.0f) *
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001685 if_then_else(mx == mn, 0,
Mike Kleinfe560a82017-05-01 12:56:35 -04001686 if_then_else(mx == r, (g-b)*d_rcp + if_then_else(g < b, 6.0f, 0),
1687 if_then_else(mx == g, (b-r)*d_rcp + 2.0f,
1688 (r-g)*d_rcp + 4.0f)));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001689
Mike Kleinfe560a82017-05-01 12:56:35 -04001690 F l = (mx + mn) * 0.5f;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001691 F s = if_then_else(mx == mn, 0,
Mike Kleinfe560a82017-05-01 12:56:35 -04001692 d / if_then_else(l > 0.5f, 2.0f-mx-mn, mx+mn));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001693
1694 r = h;
1695 g = s;
1696 b = l;
1697}
Mike Kleinf7729c22017-09-27 11:42:30 -04001698STAGE(hsl_to_rgb, Ctx::None) {
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001699 F h = r,
1700 s = g,
1701 l = b;
1702
Mike Klein5664e652017-05-01 16:01:38 -04001703 F q = l + if_then_else(l >= 0.5f, s - l*s, l*s),
1704 p = 2.0f*l - q;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001705
1706 auto hue_to_rgb = [&](F t) {
Mike Klein879a08a2017-05-01 15:34:01 -04001707 t = fract(t);
Mike Klein5664e652017-05-01 16:01:38 -04001708
1709 F r = p;
1710 r = if_then_else(t >= 4/6.0f, r, p + (q-p)*(4.0f - 6.0f*t));
1711 r = if_then_else(t >= 3/6.0f, r, q);
1712 r = if_then_else(t >= 1/6.0f, r, p + (q-p)*( 6.0f*t));
1713 return r;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001714 };
1715
Mike Kleinfb11acd2017-05-01 14:22:10 -04001716 r = if_then_else(s == 0, l, hue_to_rgb(h + (1/3.0f)));
1717 g = if_then_else(s == 0, l, hue_to_rgb(h ));
1718 b = if_then_else(s == 0, l, hue_to_rgb(h - (1/3.0f)));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001719}
1720
Mike Kleinfb126fa2017-08-24 13:06:23 -04001721// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
1722SI F alpha_coverage_from_rgb_coverage(F a, F da, F cr, F cg, F cb) {
Mike Klein5d835d02019-10-16 13:28:55 -05001723 return if_then_else(a < da, min(cr, min(cg,cb))
1724 , max(cr, max(cg,cb)));
Mike Kleinfb126fa2017-08-24 13:06:23 -04001725}
1726
Mike Kleinf7729c22017-09-27 11:42:30 -04001727STAGE(scale_1_float, const float* c) {
1728 r = r * *c;
1729 g = g * *c;
1730 b = b * *c;
1731 a = a * *c;
Mike Kleine3d44212017-02-24 08:21:18 -05001732}
Mike Kleinb11ab572018-10-24 06:42:14 -04001733STAGE(scale_u8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001734 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Kleine1caee12017-02-15 13:31:12 -05001735
Mike Kleinc31858b2017-03-01 13:07:40 -05001736 auto scales = load<U8>(ptr, tail);
Mike Klein40de6da2017-04-07 13:09:29 -04001737 auto c = from_byte(scales);
Mike Kleine1caee12017-02-15 13:31:12 -05001738
1739 r = r * c;
1740 g = g * c;
1741 b = b * c;
1742 a = a * c;
1743}
Mike Kleinb11ab572018-10-24 06:42:14 -04001744STAGE(scale_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001745 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleinfb126fa2017-08-24 13:06:23 -04001746
1747 F cr,cg,cb;
1748 from_565(load<U16>(ptr, tail), &cr, &cg, &cb);
1749
1750 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
1751
1752 r = r * cr;
1753 g = g * cg;
1754 b = b * cb;
1755 a = a * ca;
1756}
Mike Kleine3d44212017-02-24 08:21:18 -05001757
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001758SI F lerp(F from, F to, F t) {
1759 return mad(to-from, t, from);
1760}
1761
Mike Kleinf7729c22017-09-27 11:42:30 -04001762STAGE(lerp_1_float, const float* c) {
1763 r = lerp(dr, r, *c);
1764 g = lerp(dg, g, *c);
1765 b = lerp(db, b, *c);
1766 a = lerp(da, a, *c);
Mike Kleine3d44212017-02-24 08:21:18 -05001767}
Mike Reed79a75422019-03-15 15:45:09 -04001768STAGE(lerp_native, const float scales[]) {
Mike Klein7a177b42019-06-17 17:17:47 -05001769 auto c = sk_unaligned_load<F>(scales);
Mike Reed79a75422019-03-15 15:45:09 -04001770 r = lerp(dr, r, c);
1771 g = lerp(dg, g, c);
1772 b = lerp(db, b, c);
1773 a = lerp(da, a, c);
1774}
Mike Kleinb11ab572018-10-24 06:42:14 -04001775STAGE(lerp_u8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001776 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Klein2b767362017-02-22 13:52:40 -05001777
Mike Kleinc31858b2017-03-01 13:07:40 -05001778 auto scales = load<U8>(ptr, tail);
Mike Klein40de6da2017-04-07 13:09:29 -04001779 auto c = from_byte(scales);
Mike Klein2b767362017-02-22 13:52:40 -05001780
1781 r = lerp(dr, r, c);
1782 g = lerp(dg, g, c);
1783 b = lerp(db, b, c);
1784 a = lerp(da, a, c);
1785}
Mike Kleinb11ab572018-10-24 06:42:14 -04001786STAGE(lerp_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001787 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleine3d44212017-02-24 08:21:18 -05001788
1789 F cr,cg,cb;
Mike Klein5224f462017-03-07 17:29:54 -05001790 from_565(load<U16>(ptr, tail), &cr, &cg, &cb);
Mike Kleine3d44212017-02-24 08:21:18 -05001791
Mike Kleinfb126fa2017-08-24 13:06:23 -04001792 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
1793
Mike Kleine3d44212017-02-24 08:21:18 -05001794 r = lerp(dr, r, cr);
1795 g = lerp(dg, g, cg);
1796 b = lerp(db, b, cb);
Mike Kleinfb126fa2017-08-24 13:06:23 -04001797 a = lerp(da, a, ca);
Mike Kleine3d44212017-02-24 08:21:18 -05001798}
Mike Kleine1caee12017-02-15 13:31:12 -05001799
Mike Kleineda2ac22018-11-06 11:53:59 -05001800STAGE(emboss, const SkRasterPipeline_EmbossCtx* ctx) {
1801 auto mptr = ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy),
1802 aptr = ptr_at_xy<const uint8_t>(&ctx->add, dx,dy);
1803
1804 F mul = from_byte(load<U8>(mptr, tail)),
1805 add = from_byte(load<U8>(aptr, tail));
1806
1807 r = mad(r, mul, add);
1808 g = mad(g, mul, add);
1809 b = mad(b, mul, add);
1810}
1811
Mike Kleinb11ab572018-10-24 06:42:14 -04001812STAGE(byte_tables, const void* ctx) { // TODO: rename Tables SkRasterPipeline_ByteTablesCtx
Mike Klein40de6da2017-04-07 13:09:29 -04001813 struct Tables { const uint8_t *r, *g, *b, *a; };
1814 auto tables = (const Tables*)ctx;
1815
Mike Klein37155d42017-12-15 09:55:03 -05001816 r = from_byte(gather(tables->r, to_unorm(r, 255)));
1817 g = from_byte(gather(tables->g, to_unorm(g, 255)));
1818 b = from_byte(gather(tables->b, to_unorm(b, 255)));
1819 a = from_byte(gather(tables->a, to_unorm(a, 255)));
Mike Klein40de6da2017-04-07 13:09:29 -04001820}
1821
Mike Kleinb1c77e42018-09-06 15:23:29 -04001822SI F strip_sign(F x, U32* sign) {
1823 U32 bits = bit_cast<U32>(x);
1824 *sign = bits & 0x80000000;
1825 return bit_cast<F>(bits ^ *sign);
1826}
Mike Kleinc4e40632018-09-05 15:16:52 -04001827
Mike Kleinb1c77e42018-09-06 15:23:29 -04001828SI F apply_sign(F x, U32 sign) {
1829 return bit_cast<F>(sign | bit_cast<U32>(x));
1830}
Mike Kleinc4e40632018-09-05 15:16:52 -04001831
Brian Osman5deadca2019-01-24 12:18:17 -05001832STAGE(parametric, const skcms_TransferFunction* ctx) {
Mike Klein4eebd9e2018-07-11 14:49:51 -04001833 auto fn = [&](F v) {
Mike Kleinc4e40632018-09-05 15:16:52 -04001834 U32 sign;
1835 v = strip_sign(v, &sign);
1836
Brian Osman5deadca2019-01-24 12:18:17 -05001837 F r = if_then_else(v <= ctx->d, mad(ctx->c, v, ctx->f)
1838 , approx_powf(mad(ctx->a, v, ctx->b), ctx->g) + ctx->e);
Mike Klein33d3d312018-09-05 17:52:25 -04001839 return apply_sign(r, sign);
Mike Klein4eebd9e2018-07-11 14:49:51 -04001840 };
1841 r = fn(r);
1842 g = fn(g);
1843 b = fn(b);
Mike Klein44375172017-04-17 19:32:05 -04001844}
Mike Klein44375172017-04-17 19:32:05 -04001845
Mike Klein1ce03a62019-04-23 08:00:35 -05001846STAGE(gamma_, const float* G) {
Mike Kleinc4e40632018-09-05 15:16:52 -04001847 auto fn = [&](F v) {
1848 U32 sign;
1849 v = strip_sign(v, &sign);
1850 return apply_sign(approx_powf(v, *G), sign);
1851 };
1852 r = fn(r);
1853 g = fn(g);
1854 b = fn(b);
1855}
1856
Brian Osman11e6aa82019-10-16 13:58:42 -04001857STAGE(PQish, const skcms_TransferFunction* ctx) {
1858 auto fn = [&](F v) {
1859 U32 sign;
1860 v = strip_sign(v, &sign);
1861
1862 F r = approx_powf(max(mad(ctx->b, approx_powf(v, ctx->c), ctx->a), 0)
1863 / (mad(ctx->e, approx_powf(v, ctx->c), ctx->d)),
1864 ctx->f);
1865
1866 return apply_sign(r, sign);
1867 };
1868 r = fn(r);
1869 g = fn(g);
1870 b = fn(b);
1871}
1872
1873STAGE(HLGish, const skcms_TransferFunction* ctx) {
1874 auto fn = [&](F v) {
1875 U32 sign;
1876 v = strip_sign(v, &sign);
1877
1878 const float R = ctx->a, G = ctx->b,
1879 a = ctx->c, b = ctx->d, c = ctx->e;
1880
1881 F r = if_then_else(v*R <= 1, approx_powf(v*R, G)
1882 , approx_exp((v-c)*a) + b);
1883
1884 return apply_sign(r, sign);
1885 };
1886 r = fn(r);
1887 g = fn(g);
1888 b = fn(b);
1889}
1890
1891STAGE(HLGinvish, const skcms_TransferFunction* ctx) {
1892 auto fn = [&](F v) {
1893 U32 sign;
1894 v = strip_sign(v, &sign);
1895
1896 const float R = ctx->a, G = ctx->b,
1897 a = ctx->c, b = ctx->d, c = ctx->e;
1898
1899 F r = if_then_else(v <= 1, R * approx_powf(v, G)
1900 , a * approx_log(v - b) + c);
1901
1902 return apply_sign(r, sign);
1903 };
1904 r = fn(r);
1905 g = fn(g);
1906 b = fn(b);
1907}
1908
Mike Kleinc4e40632018-09-05 15:16:52 -04001909STAGE(from_srgb, Ctx::None) {
1910 auto fn = [](F s) {
1911 U32 sign;
1912 s = strip_sign(s, &sign);
1913 auto lo = s * (1/12.92f);
1914 auto hi = mad(s*s, mad(s, 0.3000f, 0.6975f), 0.0025f);
1915 return apply_sign(if_then_else(s < 0.055f, lo, hi), sign);
1916 };
1917 r = fn(r);
1918 g = fn(g);
1919 b = fn(b);
1920}
1921STAGE(to_srgb, Ctx::None) {
1922 auto fn = [](F l) {
1923 U32 sign;
1924 l = strip_sign(l, &sign);
1925 // We tweak c and d for each instruction set to make sure fn(1) is exactly 1.
1926 #if defined(JUMPER_IS_AVX512)
1927 const float c = 1.130026340485f,
1928 d = 0.141387879848f;
1929 #elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) || \
1930 defined(JUMPER_IS_AVX ) || defined(JUMPER_IS_HSW )
1931 const float c = 1.130048394203f,
1932 d = 0.141357362270f;
1933 #elif defined(JUMPER_IS_NEON)
1934 const float c = 1.129999995232f,
1935 d = 0.141381442547f;
1936 #else
1937 const float c = 1.129999995232f,
1938 d = 0.141377761960f;
1939 #endif
1940 F t = rsqrt(l);
1941 auto lo = l * 12.92f;
1942 auto hi = mad(t, mad(t, -0.0024542345f, 0.013832027f), c)
1943 * rcp(d + t);
1944 return apply_sign(if_then_else(l < 0.00465985f, lo, hi), sign);
1945 };
1946 r = fn(r);
1947 g = fn(g);
1948 b = fn(b);
Mike Kleina07e4302017-08-09 13:51:35 -04001949}
1950
Mike Kleinb11ab572018-10-24 06:42:14 -04001951STAGE(load_a8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001952 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Klein420e38f2017-02-24 09:05:14 -05001953
1954 r = g = b = 0.0f;
Mike Klein40de6da2017-04-07 13:09:29 -04001955 a = from_byte(load<U8>(ptr, tail));
Mike Klein420e38f2017-02-24 09:05:14 -05001956}
Mike Kleinb11ab572018-10-24 06:42:14 -04001957STAGE(load_a8_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001958 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001959
1960 dr = dg = db = 0.0f;
1961 da = from_byte(load<U8>(ptr, tail));
1962}
Mike Kleinb11ab572018-10-24 06:42:14 -04001963STAGE(gather_a8, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001964 const uint8_t* ptr;
1965 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1966 r = g = b = 0.0f;
Mike Klein40de6da2017-04-07 13:09:29 -04001967 a = from_byte(gather(ptr, ix));
Mike Klein21bd3e42017-04-06 16:32:29 -04001968}
Mike Kleinb11ab572018-10-24 06:42:14 -04001969STAGE(store_a8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001970 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
Mike Klein420e38f2017-02-24 09:05:14 -05001971
Mike Klein37155d42017-12-15 09:55:03 -05001972 U8 packed = pack(pack(to_unorm(a, 255)));
Mike Kleinc31858b2017-03-01 13:07:40 -05001973 store(ptr, packed, tail);
Mike Klein420e38f2017-02-24 09:05:14 -05001974}
1975
Mike Kleinb11ab572018-10-24 06:42:14 -04001976STAGE(load_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001977 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Klein3f81f372017-02-23 13:03:57 -05001978
Mike Klein5224f462017-03-07 17:29:54 -05001979 from_565(load<U16>(ptr, tail), &r,&g,&b);
Mike Kleinfe560a82017-05-01 12:56:35 -04001980 a = 1.0f;
Mike Klein3f81f372017-02-23 13:03:57 -05001981}
Mike Kleinb11ab572018-10-24 06:42:14 -04001982STAGE(load_565_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001983 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001984
1985 from_565(load<U16>(ptr, tail), &dr,&dg,&db);
1986 da = 1.0f;
1987}
Mike Kleinb11ab572018-10-24 06:42:14 -04001988STAGE(gather_565, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001989 const uint16_t* ptr;
1990 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1991 from_565(gather(ptr, ix), &r,&g,&b);
Mike Kleinfe560a82017-05-01 12:56:35 -04001992 a = 1.0f;
Mike Klein21bd3e42017-04-06 16:32:29 -04001993}
Mike Kleinb11ab572018-10-24 06:42:14 -04001994STAGE(store_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001995 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
Mike Klein3f81f372017-02-23 13:03:57 -05001996
Mike Klein37155d42017-12-15 09:55:03 -05001997 U16 px = pack( to_unorm(r, 31) << 11
1998 | to_unorm(g, 63) << 5
1999 | to_unorm(b, 31) );
Mike Kleinc31858b2017-03-01 13:07:40 -05002000 store(ptr, px, tail);
Mike Klein3f81f372017-02-23 13:03:57 -05002001}
2002
Mike Kleinb11ab572018-10-24 06:42:14 -04002003STAGE(load_4444, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002004 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleinf809fef2017-03-31 13:52:45 -04002005 from_4444(load<U16>(ptr, tail), &r,&g,&b,&a);
2006}
Mike Kleinb11ab572018-10-24 06:42:14 -04002007STAGE(load_4444_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002008 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04002009 from_4444(load<U16>(ptr, tail), &dr,&dg,&db,&da);
2010}
Mike Kleinb11ab572018-10-24 06:42:14 -04002011STAGE(gather_4444, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04002012 const uint16_t* ptr;
2013 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2014 from_4444(gather(ptr, ix), &r,&g,&b,&a);
2015}
Mike Kleinb11ab572018-10-24 06:42:14 -04002016STAGE(store_4444, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002017 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
Mike Klein37155d42017-12-15 09:55:03 -05002018 U16 px = pack( to_unorm(r, 15) << 12
2019 | to_unorm(g, 15) << 8
2020 | to_unorm(b, 15) << 4
2021 | to_unorm(a, 15) );
Mike Kleinf809fef2017-03-31 13:52:45 -04002022 store(ptr, px, tail);
2023}
2024
Mike Kleinb11ab572018-10-24 06:42:14 -04002025STAGE(load_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002026 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Kleindec4ea82017-04-06 15:04:05 -04002027 from_8888(load<U32>(ptr, tail), &r,&g,&b,&a);
2028}
Mike Kleinb11ab572018-10-24 06:42:14 -04002029STAGE(load_8888_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002030 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04002031 from_8888(load<U32>(ptr, tail), &dr,&dg,&db,&da);
2032}
Mike Kleinb11ab572018-10-24 06:42:14 -04002033STAGE(gather_8888, const SkRasterPipeline_GatherCtx* ctx) {
Mike Kleindec4ea82017-04-06 15:04:05 -04002034 const uint32_t* ptr;
2035 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2036 from_8888(gather(ptr, ix), &r,&g,&b,&a);
Mike Kleine1caee12017-02-15 13:31:12 -05002037}
Mike Kleinb11ab572018-10-24 06:42:14 -04002038STAGE(store_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002039 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
Mike Klein3b92b692017-07-18 11:30:25 -04002040
Mike Klein37155d42017-12-15 09:55:03 -05002041 U32 px = to_unorm(r, 255)
2042 | to_unorm(g, 255) << 8
2043 | to_unorm(b, 255) << 16
2044 | to_unorm(a, 255) << 24;
Mike Klein3b92b692017-07-18 11:30:25 -04002045 store(ptr, px, tail);
2046}
2047
Brian Salomon217522c2019-06-11 15:55:30 -04002048STAGE(load_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -04002049 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2050 from_88(load<U16>(ptr, tail), &r, &g);
Brian Salomon217522c2019-06-11 15:55:30 -04002051 b = 0;
2052 a = 1;
Robert Phillipsd470e1b2019-09-04 15:05:35 -04002053}
2054STAGE(load_rg88_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2055 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2056 from_88(load<U16>(ptr, tail), &dr, &dg);
2057 db = 0;
2058 da = 1;
2059}
2060STAGE(gather_rg88, const SkRasterPipeline_GatherCtx* ctx) {
2061 const uint16_t* ptr;
2062 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2063 from_88(gather(ptr, ix), &r, &g);
2064 b = 0;
2065 a = 1;
Brian Salomon217522c2019-06-11 15:55:30 -04002066}
2067STAGE(store_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -04002068 auto ptr = ptr_at_xy<uint16_t>(ctx, dx, dy);
2069 U16 px = pack( to_unorm(r, 255) | to_unorm(g, 255) << 8 );
Brian Salomon217522c2019-06-11 15:55:30 -04002070 store(ptr, px, tail);
2071}
2072
2073STAGE(load_a16, const SkRasterPipeline_MemoryCtx* ctx) {
2074 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2075 r = g = b = 0;
2076 a = from_short(load<U16>(ptr, tail));
2077}
Robert Phillips429f0d32019-09-11 17:03:28 -04002078STAGE(load_a16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2079 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2080 dr = dg = db = 0.0f;
2081 da = from_short(load<U16>(ptr, tail));
2082}
2083STAGE(gather_a16, const SkRasterPipeline_GatherCtx* ctx) {
2084 const uint16_t* ptr;
2085 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2086 r = g = b = 0.0f;
2087 a = from_short(gather(ptr, ix));
2088}
Brian Salomon217522c2019-06-11 15:55:30 -04002089STAGE(store_a16, const SkRasterPipeline_MemoryCtx* ctx) {
2090 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2091
2092 U16 px = pack(to_unorm(a, 65535));
2093 store(ptr, px, tail);
2094}
Robert Phillips429f0d32019-09-11 17:03:28 -04002095
Brian Salomon217522c2019-06-11 15:55:30 -04002096STAGE(load_rg1616, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002097 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
Brian Salomon217522c2019-06-11 15:55:30 -04002098 b = 0; a = 1;
2099 from_1616(load<U32>(ptr, tail), &r,&g);
2100}
Robert Phillips429f0d32019-09-11 17:03:28 -04002101STAGE(load_rg1616_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2102 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
2103 from_1616(load<U32>(ptr, tail), &dr, &dg);
2104 db = 0;
2105 da = 1;
2106}
2107STAGE(gather_rg1616, const SkRasterPipeline_GatherCtx* ctx) {
2108 const uint32_t* ptr;
2109 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2110 from_1616(gather(ptr, ix), &r, &g);
2111 b = 0;
2112 a = 1;
2113}
Brian Salomon217522c2019-06-11 15:55:30 -04002114STAGE(store_rg1616, const SkRasterPipeline_MemoryCtx* ctx) {
2115 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2116
2117 U32 px = to_unorm(r, 65535)
2118 | to_unorm(g, 65535) << 16;
2119 store(ptr, px, tail);
2120}
Robert Phillips429f0d32019-09-11 17:03:28 -04002121
Brian Salomond608e222019-06-12 17:42:58 -04002122STAGE(load_16161616, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002123 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
Brian Salomond608e222019-06-12 17:42:58 -04002124 from_16161616(load<U64>(ptr, tail), &r,&g, &b, &a);
2125}
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002126STAGE(load_16161616_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2127 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
2128 from_16161616(load<U64>(ptr, tail), &dr, &dg, &db, &da);
2129}
2130STAGE(gather_16161616, const SkRasterPipeline_GatherCtx* ctx) {
2131 const uint64_t* ptr;
2132 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2133 from_16161616(gather(ptr, ix), &r, &g, &b, &a);
2134}
Brian Salomond608e222019-06-12 17:42:58 -04002135STAGE(store_16161616, const SkRasterPipeline_MemoryCtx* ctx) {
2136 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
2137
2138 U16 R = pack(to_unorm(r, 65535)),
2139 G = pack(to_unorm(g, 65535)),
2140 B = pack(to_unorm(b, 65535)),
2141 A = pack(to_unorm(a, 65535));
2142
2143 store4(ptr,tail, R,G,B,A);
2144}
2145
Brian Salomon217522c2019-06-11 15:55:30 -04002146
Mike Kleinb11ab572018-10-24 06:42:14 -04002147STAGE(load_1010102, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinac568a92018-01-25 09:09:32 -05002148 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2149 from_1010102(load<U32>(ptr, tail), &r,&g,&b,&a);
2150}
Mike Kleinb11ab572018-10-24 06:42:14 -04002151STAGE(load_1010102_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinac568a92018-01-25 09:09:32 -05002152 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2153 from_1010102(load<U32>(ptr, tail), &dr,&dg,&db,&da);
2154}
Mike Kleinb11ab572018-10-24 06:42:14 -04002155STAGE(gather_1010102, const SkRasterPipeline_GatherCtx* ctx) {
Mike Kleinac568a92018-01-25 09:09:32 -05002156 const uint32_t* ptr;
2157 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2158 from_1010102(gather(ptr, ix), &r,&g,&b,&a);
2159}
Mike Kleinb11ab572018-10-24 06:42:14 -04002160STAGE(store_1010102, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinac568a92018-01-25 09:09:32 -05002161 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2162
2163 U32 px = to_unorm(r, 1023)
2164 | to_unorm(g, 1023) << 10
2165 | to_unorm(b, 1023) << 20
2166 | to_unorm(a, 3) << 30;
2167 store(ptr, px, tail);
2168}
2169
Mike Kleinb11ab572018-10-24 06:42:14 -04002170STAGE(load_f16, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002171 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
Mike Kleine1caee12017-02-15 13:31:12 -05002172
Mike Klein114e6b32017-04-03 22:21:15 -04002173 U16 R,G,B,A;
Mike Kleinfa6eb912017-04-05 10:18:27 -04002174 load4((const uint16_t*)ptr,tail, &R,&G,&B,&A);
Mike Klein114e6b32017-04-03 22:21:15 -04002175 r = from_half(R);
2176 g = from_half(G);
2177 b = from_half(B);
2178 a = from_half(A);
Mike Kleine1caee12017-02-15 13:31:12 -05002179}
Mike Kleinb11ab572018-10-24 06:42:14 -04002180STAGE(load_f16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002181 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04002182
2183 U16 R,G,B,A;
2184 load4((const uint16_t*)ptr,tail, &R,&G,&B,&A);
2185 dr = from_half(R);
2186 dg = from_half(G);
2187 db = from_half(B);
2188 da = from_half(A);
2189}
Mike Kleinb11ab572018-10-24 06:42:14 -04002190STAGE(gather_f16, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein5f055f02017-04-06 20:02:11 -04002191 const uint64_t* ptr;
2192 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2193 auto px = gather(ptr, ix);
2194
2195 U16 R,G,B,A;
2196 load4((const uint16_t*)&px,0, &R,&G,&B,&A);
2197 r = from_half(R);
2198 g = from_half(G);
2199 b = from_half(B);
2200 a = from_half(A);
2201}
Mike Kleinb11ab572018-10-24 06:42:14 -04002202STAGE(store_f16, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002203 auto ptr = ptr_at_xy<uint64_t>(ctx, dx,dy);
Mike Kleinfa6eb912017-04-05 10:18:27 -04002204 store4((uint16_t*)ptr,tail, to_half(r)
2205 , to_half(g)
2206 , to_half(b)
2207 , to_half(a));
Mike Kleine1caee12017-02-15 13:31:12 -05002208}
2209
Mike Kleinb11ab572018-10-24 06:42:14 -04002210STAGE(store_u16_be, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04002211 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,dy);
Mike Klein3146bb92017-04-05 14:45:02 -04002212
Mike Klein37155d42017-12-15 09:55:03 -05002213 U16 R = bswap(pack(to_unorm(r, 65535))),
2214 G = bswap(pack(to_unorm(g, 65535))),
2215 B = bswap(pack(to_unorm(b, 65535))),
2216 A = bswap(pack(to_unorm(a, 65535)));
Mike Klein3146bb92017-04-05 14:45:02 -04002217
Mike Kleinb3821732017-04-17 10:58:05 -04002218 store4(ptr,tail, R,G,B,A);
Mike Klein3146bb92017-04-05 14:45:02 -04002219}
2220
Brian Salomon217522c2019-06-11 15:55:30 -04002221STAGE(load_af16, const SkRasterPipeline_MemoryCtx* ctx) {
2222 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2223
2224 U16 A = load<U16>((const uint16_t*)ptr, tail);
2225 r = 0;
2226 g = 0;
2227 b = 0;
2228 a = from_half(A);
2229}
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002230STAGE(load_af16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2231 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2232
2233 U16 A = load<U16>((const uint16_t*)ptr, tail);
2234 dr = dg = db = 0.0f;
2235 da = from_half(A);
2236}
2237STAGE(gather_af16, const SkRasterPipeline_GatherCtx* ctx) {
2238 const uint16_t* ptr;
2239 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2240 r = g = b = 0.0f;
2241 a = from_half(gather(ptr, ix));
2242}
Brian Salomon217522c2019-06-11 15:55:30 -04002243STAGE(store_af16, const SkRasterPipeline_MemoryCtx* ctx) {
2244 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2245 store(ptr, to_half(a), tail);
2246}
2247
2248STAGE(load_rgf16, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002249 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
Brian Salomon217522c2019-06-11 15:55:30 -04002250
2251 U16 R,G;
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002252 load2((const uint16_t*)ptr, tail, &R, &G);
Brian Salomon217522c2019-06-11 15:55:30 -04002253 r = from_half(R);
2254 g = from_half(G);
2255 b = 0;
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002256 a = 1;
2257}
2258STAGE(load_rgf16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2259 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
2260
2261 U16 R,G;
2262 load2((const uint16_t*)ptr, tail, &R, &G);
2263 dr = from_half(R);
2264 dg = from_half(G);
2265 db = 0;
2266 da = 1;
2267}
2268STAGE(gather_rgf16, const SkRasterPipeline_GatherCtx* ctx) {
2269 const uint32_t* ptr;
2270 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2271 auto px = gather(ptr, ix);
2272
2273 U16 R,G;
2274 load2((const uint16_t*)&px, 0, &R, &G);
2275 r = from_half(R);
2276 g = from_half(G);
2277 b = 0;
2278 a = 1;
Brian Salomon217522c2019-06-11 15:55:30 -04002279}
2280STAGE(store_rgf16, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillips17a3a0b2019-09-18 13:56:54 -04002281 auto ptr = ptr_at_xy<uint32_t>(ctx, dx, dy);
Brian Salomon217522c2019-06-11 15:55:30 -04002282 store2((uint16_t*)ptr, tail, to_half(r)
2283 , to_half(g));
2284}
2285
Mike Kleinb11ab572018-10-24 06:42:14 -04002286STAGE(load_f32, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein37854712018-06-26 11:43:06 -04002287 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
Mike Klein14987eb2017-04-06 10:22:26 -04002288 load4(ptr,tail, &r,&g,&b,&a);
2289}
Mike Kleinb11ab572018-10-24 06:42:14 -04002290STAGE(load_f32_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein37854712018-06-26 11:43:06 -04002291 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
Mike Reed279091e2017-06-27 16:58:00 -04002292 load4(ptr,tail, &dr,&dg,&db,&da);
2293}
Mike Kleinb11ab572018-10-24 06:42:14 -04002294STAGE(gather_f32, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein37854712018-06-26 11:43:06 -04002295 const float* ptr;
2296 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2297 r = gather(ptr, 4*ix + 0);
2298 g = gather(ptr, 4*ix + 1);
2299 b = gather(ptr, 4*ix + 2);
2300 a = gather(ptr, 4*ix + 3);
2301}
Mike Kleinb11ab572018-10-24 06:42:14 -04002302STAGE(store_f32, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein37854712018-06-26 11:43:06 -04002303 auto ptr = ptr_at_xy<float>(ctx, 4*dx,4*dy);
Mike Kleinfa6eb912017-04-05 10:18:27 -04002304 store4(ptr,tail, r,g,b,a);
Mike Klein94fc0fe2017-03-03 14:05:32 -05002305}
2306
Brian Salomon217522c2019-06-11 15:55:30 -04002307STAGE(load_rgf32, const SkRasterPipeline_MemoryCtx* ctx) {
2308 auto ptr = ptr_at_xy<const float>(ctx, 2*dx,2*dy);
2309 load2(ptr, tail, &r, &g);
2310 b = 0;
2311 a = 1;
2312}
2313STAGE(store_rgf32, const SkRasterPipeline_MemoryCtx* ctx) {
2314 auto ptr = ptr_at_xy<float>(ctx, 2*dx,2*dy);
2315 store2(ptr, tail, r, g);
2316}
2317
Mike Kleinb11ab572018-10-24 06:42:14 -04002318SI F exclusive_repeat(F v, const SkRasterPipeline_TileCtx* ctx) {
Mike Kleinf3b4e162017-09-22 15:32:59 -04002319 return v - floor_(v*ctx->invScale)*ctx->scale;
Mike Klein0cc60b82017-06-22 11:00:17 -07002320}
Mike Kleinb11ab572018-10-24 06:42:14 -04002321SI F exclusive_mirror(F v, const SkRasterPipeline_TileCtx* ctx) {
Mike Reed51e46d52017-06-23 14:21:25 -04002322 auto limit = ctx->scale;
2323 auto invLimit = ctx->invScale;
Mike Kleinf3b4e162017-09-22 15:32:59 -04002324 return abs_( (v-limit) - (limit+limit)*floor_((v-limit)*(invLimit*0.5f)) - limit );
Mike Klein0cc60b82017-06-22 11:00:17 -07002325}
Mike Kleinf3b4e162017-09-22 15:32:59 -04002326// Tile x or y to [0,limit) == [0,limit - 1 ulp] (think, sampling from images).
2327// The gather stages will hard clamp the output of these stages to [0,limit)...
2328// we just need to do the basic repeat or mirroring.
Mike Kleinb11ab572018-10-24 06:42:14 -04002329STAGE(repeat_x, const SkRasterPipeline_TileCtx* ctx) { r = exclusive_repeat(r, ctx); }
2330STAGE(repeat_y, const SkRasterPipeline_TileCtx* ctx) { g = exclusive_repeat(g, ctx); }
2331STAGE(mirror_x, const SkRasterPipeline_TileCtx* ctx) { r = exclusive_mirror(r, ctx); }
2332STAGE(mirror_y, const SkRasterPipeline_TileCtx* ctx) { g = exclusive_mirror(g, ctx); }
Mike Klein0cc60b82017-06-22 11:00:17 -07002333
Mike Kleina3b88952017-10-05 13:21:31 -04002334// Clamp x to [0,1], both sides inclusive (think, gradients).
2335// Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
2336SI F clamp_01(F v) { return min(max(0, v), 1); }
2337
2338STAGE( clamp_x_1, Ctx::None) { r = clamp_01(r); }
2339STAGE(repeat_x_1, Ctx::None) { r = clamp_01(r - floor_(r)); }
2340STAGE(mirror_x_1, Ctx::None) { r = clamp_01(abs_( (r-1.0f) - two(floor_((r-1.0f)*0.5f)) - 1.0f )); }
Mike Klein9f85d682017-05-23 07:52:01 -04002341
Mike Reeddfc0e912018-02-16 12:40:18 -05002342// Decal stores a 32bit mask after checking the coordinate (x and/or y) against its domain:
2343// mask == 0x00000000 if the coordinate(s) are out of bounds
2344// mask == 0xFFFFFFFF if the coordinate(s) are in bounds
2345// After the gather stage, the r,g,b,a values are AND'd with this mask, setting them to 0
2346// if either of the coordinates were out of bounds.
2347
Mike Kleinb11ab572018-10-24 06:42:14 -04002348STAGE(decal_x, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Reeddfc0e912018-02-16 12:40:18 -05002349 auto w = ctx->limit_x;
Mike Klein7a177b42019-06-17 17:17:47 -05002350 sk_unaligned_store(ctx->mask, cond_to_mask((0 <= r) & (r < w)));
Mike Reeddfc0e912018-02-16 12:40:18 -05002351}
Mike Kleinb11ab572018-10-24 06:42:14 -04002352STAGE(decal_y, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Reeddfc0e912018-02-16 12:40:18 -05002353 auto h = ctx->limit_y;
Mike Klein7a177b42019-06-17 17:17:47 -05002354 sk_unaligned_store(ctx->mask, cond_to_mask((0 <= g) & (g < h)));
Mike Reeddfc0e912018-02-16 12:40:18 -05002355}
Mike Kleinb11ab572018-10-24 06:42:14 -04002356STAGE(decal_x_and_y, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Reeddfc0e912018-02-16 12:40:18 -05002357 auto w = ctx->limit_x;
2358 auto h = ctx->limit_y;
Mike Klein7a177b42019-06-17 17:17:47 -05002359 sk_unaligned_store(ctx->mask,
Mike Reeddfc0e912018-02-16 12:40:18 -05002360 cond_to_mask((0 <= r) & (r < w) & (0 <= g) & (g < h)));
2361}
Mike Kleinb11ab572018-10-24 06:42:14 -04002362STAGE(check_decal_mask, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Klein7a177b42019-06-17 17:17:47 -05002363 auto mask = sk_unaligned_load<U32>(ctx->mask);
Mike Reeddfc0e912018-02-16 12:40:18 -05002364 r = bit_cast<F>( bit_cast<U32>(r) & mask );
2365 g = bit_cast<F>( bit_cast<U32>(g) & mask );
2366 b = bit_cast<F>( bit_cast<U32>(b) & mask );
2367 a = bit_cast<F>( bit_cast<U32>(a) & mask );
2368}
2369
Mike Kleinb1df5e52018-10-17 17:06:03 -04002370STAGE(alpha_to_gray, Ctx::None) {
2371 r = g = b = a;
2372 a = 1;
2373}
2374STAGE(alpha_to_gray_dst, Ctx::None) {
2375 dr = dg = db = da;
2376 da = 1;
2377}
Mike Kleinda69d592019-07-11 07:38:31 -05002378STAGE(bt709_luminance_or_luma_to_alpha, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04002379 a = r*0.2126f + g*0.7152f + b*0.0722f;
Mike Kleine9ed07d2017-03-07 12:28:11 -05002380 r = g = b = 0;
2381}
2382
Mike Kleinf7729c22017-09-27 11:42:30 -04002383STAGE(matrix_translate, const float* m) {
Mike Reed7aad8cc2017-07-05 12:33:06 -04002384 r += m[0];
2385 g += m[1];
2386}
Mike Kleinf7729c22017-09-27 11:42:30 -04002387STAGE(matrix_scale_translate, const float* m) {
Mike Kleinf04ff762017-10-20 15:50:12 -04002388 r = mad(r,m[0], m[2]);
2389 g = mad(g,m[1], m[3]);
Mike Reed7aad8cc2017-07-05 12:33:06 -04002390}
Mike Kleinf7729c22017-09-27 11:42:30 -04002391STAGE(matrix_2x3, const float* m) {
Mike Kleinb8d52752017-02-16 10:21:29 -05002392 auto R = mad(r,m[0], mad(g,m[2], m[4])),
2393 G = mad(r,m[1], mad(g,m[3], m[5]));
Mike Kleine1caee12017-02-15 13:31:12 -05002394 r = R;
2395 g = G;
2396}
Mike Kleinb82edcc2018-07-10 18:25:03 +00002397STAGE(matrix_3x3, const float* m) {
2398 auto R = mad(r,m[0], mad(g,m[3], b*m[6])),
2399 G = mad(r,m[1], mad(g,m[4], b*m[7])),
2400 B = mad(r,m[2], mad(g,m[5], b*m[8]));
2401 r = R;
2402 g = G;
2403 b = B;
2404}
Mike Kleinf7729c22017-09-27 11:42:30 -04002405STAGE(matrix_3x4, const float* m) {
Mike Kleinb8d52752017-02-16 10:21:29 -05002406 auto R = mad(r,m[0], mad(g,m[3], mad(b,m[6], m[ 9]))),
2407 G = mad(r,m[1], mad(g,m[4], mad(b,m[7], m[10]))),
2408 B = mad(r,m[2], mad(g,m[5], mad(b,m[8], m[11])));
Mike Kleine1caee12017-02-15 13:31:12 -05002409 r = R;
2410 g = G;
2411 b = B;
2412}
Mike Kleinf7729c22017-09-27 11:42:30 -04002413STAGE(matrix_4x5, const float* m) {
Mike Reed361a6402019-04-23 12:19:00 -04002414 auto R = mad(r,m[ 0], mad(g,m[ 1], mad(b,m[ 2], mad(a,m[ 3], m[ 4])))),
2415 G = mad(r,m[ 5], mad(g,m[ 6], mad(b,m[ 7], mad(a,m[ 8], m[ 9])))),
2416 B = mad(r,m[10], mad(g,m[11], mad(b,m[12], mad(a,m[13], m[14])))),
2417 A = mad(r,m[15], mad(g,m[16], mad(b,m[17], mad(a,m[18], m[19]))));
Mike Kleine9ed07d2017-03-07 12:28:11 -05002418 r = R;
2419 g = G;
2420 b = B;
2421 a = A;
2422}
Mike Kleinf7729c22017-09-27 11:42:30 -04002423STAGE(matrix_4x3, const float* m) {
Mike Reed02640952017-05-19 15:32:13 -04002424 auto X = r,
2425 Y = g;
2426
2427 r = mad(X, m[0], mad(Y, m[4], m[ 8]));
2428 g = mad(X, m[1], mad(Y, m[5], m[ 9]));
2429 b = mad(X, m[2], mad(Y, m[6], m[10]));
2430 a = mad(X, m[3], mad(Y, m[7], m[11]));
2431}
Mike Kleinf7729c22017-09-27 11:42:30 -04002432STAGE(matrix_perspective, const float* m) {
Mike Klein11d2df02017-02-24 11:51:36 -05002433 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
Mike Klein11d2df02017-02-24 11:51:36 -05002434 auto R = mad(r,m[0], mad(g,m[1], m[2])),
2435 G = mad(r,m[3], mad(g,m[4], m[5])),
2436 Z = mad(r,m[6], mad(g,m[7], m[8]));
2437 r = R * rcp(Z);
2438 g = G * rcp(Z);
2439}
Mike Kleine1caee12017-02-15 13:31:12 -05002440
Mike Kleinb11ab572018-10-24 06:42:14 -04002441SI void gradient_lookup(const SkRasterPipeline_GradientCtx* c, U32 idx, F t,
Herb Derby4de13042017-05-15 10:49:39 -04002442 F* r, F* g, F* b, F* a) {
2443 F fr, br, fg, bg, fb, bb, fa, ba;
Mike Klein106e17a2017-12-12 17:07:49 -05002444#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Herb Derby4de13042017-05-15 10:49:39 -04002445 if (c->stopCount <=8) {
2446 fr = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), idx);
2447 br = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), idx);
2448 fg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), idx);
2449 bg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), idx);
2450 fb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), idx);
2451 bb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), idx);
2452 fa = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), idx);
2453 ba = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), idx);
2454 } else
2455#endif
2456 {
2457 fr = gather(c->fs[0], idx);
2458 br = gather(c->bs[0], idx);
2459 fg = gather(c->fs[1], idx);
2460 bg = gather(c->bs[1], idx);
2461 fb = gather(c->fs[2], idx);
2462 bb = gather(c->bs[2], idx);
2463 fa = gather(c->fs[3], idx);
2464 ba = gather(c->bs[3], idx);
Herb Derby7b4202d2017-04-10 10:52:34 -04002465 }
2466
Herb Derby4de13042017-05-15 10:49:39 -04002467 *r = mad(t, fr, br);
2468 *g = mad(t, fg, bg);
2469 *b = mad(t, fb, bb);
2470 *a = mad(t, fa, ba);
2471}
2472
Mike Kleinb11ab572018-10-24 06:42:14 -04002473STAGE(evenly_spaced_gradient, const SkRasterPipeline_GradientCtx* c) {
Herb Derby4de13042017-05-15 10:49:39 -04002474 auto t = r;
2475 auto idx = trunc_(t * (c->stopCount-1));
2476 gradient_lookup(c, idx, t, &r, &g, &b, &a);
2477}
2478
Mike Kleinb11ab572018-10-24 06:42:14 -04002479STAGE(gradient, const SkRasterPipeline_GradientCtx* c) {
Herb Derby4de13042017-05-15 10:49:39 -04002480 auto t = r;
2481 U32 idx = 0;
2482
2483 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
2484 for (size_t i = 1; i < c->stopCount; i++) {
2485 idx += if_then_else(t >= c->ts[i], U32(1), U32(0));
2486 }
2487
2488 gradient_lookup(c, idx, t, &r, &g, &b, &a);
Herb Derby7b4202d2017-04-10 10:52:34 -04002489}
2490
Mike Kleinf7729c22017-09-27 11:42:30 -04002491STAGE(evenly_spaced_2_stop_gradient, const void* ctx) {
Mike Kleinb11ab572018-10-24 06:42:14 -04002492 // TODO: Rename Ctx SkRasterPipeline_EvenlySpaced2StopGradientCtx.
Herb Derby7b4202d2017-04-10 10:52:34 -04002493 struct Ctx { float f[4], b[4]; };
Mike Klein8a823fa2017-04-05 17:29:26 -04002494 auto c = (const Ctx*)ctx;
Mike Kleine1caee12017-02-15 13:31:12 -05002495
2496 auto t = r;
Herb Derby7b4202d2017-04-10 10:52:34 -04002497 r = mad(t, c->f[0], c->b[0]);
2498 g = mad(t, c->f[1], c->b[1]);
2499 b = mad(t, c->f[2], c->b[2]);
2500 a = mad(t, c->f[3], c->b[3]);
Mike Kleine1caee12017-02-15 13:31:12 -05002501}
Mike Klein0a904492017-04-12 12:52:48 -04002502
Mike Kleinf7729c22017-09-27 11:42:30 -04002503STAGE(xy_to_unit_angle, Ctx::None) {
Herb Derby7eb86982017-05-02 19:04:39 -04002504 F X = r,
2505 Y = g;
2506 F xabs = abs_(X),
2507 yabs = abs_(Y);
2508
2509 F slope = min(xabs, yabs)/max(xabs, yabs);
2510 F s = slope * slope;
2511
2512 // Use a 7th degree polynomial to approximate atan.
2513 // This was generated using sollya.gforge.inria.fr.
2514 // A float optimized polynomial was generated using the following command.
2515 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
2516 F phi = slope
2517 * (0.15912117063999176025390625f + s
2518 * (-5.185396969318389892578125e-2f + s
2519 * (2.476101927459239959716796875e-2f + s
2520 * (-7.0547382347285747528076171875e-3f))));
2521
2522 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
2523 phi = if_then_else(X < 0.0f , 1.0f/2.0f - phi, phi);
2524 phi = if_then_else(Y < 0.0f , 1.0f - phi , phi);
2525 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
2526 r = phi;
2527}
2528
Mike Kleinf7729c22017-09-27 11:42:30 -04002529STAGE(xy_to_radius, Ctx::None) {
Herb Derby090fbf82017-05-08 15:10:36 -04002530 F X2 = r * r,
2531 Y2 = g * g;
Mike Kleinfd35c742017-05-15 15:55:54 -04002532 r = sqrt_(X2 + Y2);
Herb Derby090fbf82017-05-08 15:10:36 -04002533}
2534
Yuqian Lid208a882018-01-04 10:08:42 -05002535// Please see https://skia.org/dev/design/conical for how our 2pt conical shader works.
2536
2537STAGE(negate_x, Ctx::None) { r = -r; }
2538
Mike Kleinb11ab572018-10-24 06:42:14 -04002539STAGE(xy_to_2pt_conical_strip, const SkRasterPipeline_2PtConicalCtx* ctx) {
Yuqian Lid208a882018-01-04 10:08:42 -05002540 F x = r, y = g, &t = r;
2541 t = x + sqrt_(ctx->fP0 - y*y); // ctx->fP0 = r0 * r0
2542}
2543
2544STAGE(xy_to_2pt_conical_focal_on_circle, Ctx::None) {
2545 F x = r, y = g, &t = r;
2546 t = x + y*y / x; // (x^2 + y^2) / x
2547}
2548
Mike Kleinb11ab572018-10-24 06:42:14 -04002549STAGE(xy_to_2pt_conical_well_behaved, const SkRasterPipeline_2PtConicalCtx* ctx) {
Yuqian Lid208a882018-01-04 10:08:42 -05002550 F x = r, y = g, &t = r;
2551 t = sqrt_(x*x + y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2552}
2553
Mike Kleinb11ab572018-10-24 06:42:14 -04002554STAGE(xy_to_2pt_conical_greater, const SkRasterPipeline_2PtConicalCtx* ctx) {
Yuqian Lid208a882018-01-04 10:08:42 -05002555 F x = r, y = g, &t = r;
2556 t = sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2557}
2558
Mike Kleinb11ab572018-10-24 06:42:14 -04002559STAGE(xy_to_2pt_conical_smaller, const SkRasterPipeline_2PtConicalCtx* ctx) {
Yuqian Lid208a882018-01-04 10:08:42 -05002560 F x = r, y = g, &t = r;
2561 t = -sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2562}
2563
Mike Kleinb11ab572018-10-24 06:42:14 -04002564STAGE(alter_2pt_conical_compensate_focal, const SkRasterPipeline_2PtConicalCtx* ctx) {
Yuqian Lid208a882018-01-04 10:08:42 -05002565 F& t = r;
2566 t = t + ctx->fP1; // ctx->fP1 = f
2567}
2568
2569STAGE(alter_2pt_conical_unswap, Ctx::None) {
2570 F& t = r;
2571 t = 1 - t;
2572}
2573
Mike Kleinb11ab572018-10-24 06:42:14 -04002574STAGE(mask_2pt_conical_nan, SkRasterPipeline_2PtConicalCtx* c) {
Yuqian Lid208a882018-01-04 10:08:42 -05002575 F& t = r;
2576 auto is_degenerate = (t != t); // NaN
2577 t = if_then_else(is_degenerate, F(0), t);
Mike Klein7a177b42019-06-17 17:17:47 -05002578 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
Yuqian Lid208a882018-01-04 10:08:42 -05002579}
2580
Mike Kleinb11ab572018-10-24 06:42:14 -04002581STAGE(mask_2pt_conical_degenerates, SkRasterPipeline_2PtConicalCtx* c) {
Yuqian Lid208a882018-01-04 10:08:42 -05002582 F& t = r;
2583 auto is_degenerate = (t <= 0) | (t != t);
2584 t = if_then_else(is_degenerate, F(0), t);
Mike Klein7a177b42019-06-17 17:17:47 -05002585 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
Yuqian Lid208a882018-01-04 10:08:42 -05002586}
2587
Mike Kleinf7729c22017-09-27 11:42:30 -04002588STAGE(apply_vector_mask, const uint32_t* ctx) {
Mike Klein7a177b42019-06-17 17:17:47 -05002589 const U32 mask = sk_unaligned_load<U32>(ctx);
Florin Malita9026fe12017-06-29 11:03:45 -04002590 r = bit_cast<F>(bit_cast<U32>(r) & mask);
2591 g = bit_cast<F>(bit_cast<U32>(g) & mask);
2592 b = bit_cast<F>(bit_cast<U32>(b) & mask);
2593 a = bit_cast<F>(bit_cast<U32>(a) & mask);
Florin Malita2e409002017-06-28 14:46:54 -04002594}
2595
Mike Kleinb11ab572018-10-24 06:42:14 -04002596STAGE(save_xy, SkRasterPipeline_SamplerCtx* c) {
Mike Klein0a904492017-04-12 12:52:48 -04002597 // Whether bilinear or bicubic, all sample points are at the same fractional offset (fx,fy).
2598 // They're either the 4 corners of a logical 1x1 pixel or the 16 corners of a 3x3 grid
2599 // surrounding (x,y) at (0.5,0.5) off-center.
Mike Kleinfe560a82017-05-01 12:56:35 -04002600 F fx = fract(r + 0.5f),
2601 fy = fract(g + 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002602
2603 // Samplers will need to load x and fx, or y and fy.
Mike Klein7a177b42019-06-17 17:17:47 -05002604 sk_unaligned_store(c->x, r);
2605 sk_unaligned_store(c->y, g);
2606 sk_unaligned_store(c->fx, fx);
2607 sk_unaligned_store(c->fy, fy);
Mike Klein0a904492017-04-12 12:52:48 -04002608}
2609
Mike Kleinb11ab572018-10-24 06:42:14 -04002610STAGE(accumulate, const SkRasterPipeline_SamplerCtx* c) {
Mike Klein0a904492017-04-12 12:52:48 -04002611 // Bilinear and bicubic filters are both separable, so we produce independent contributions
2612 // from x and y, multiplying them together here to get each pixel's total scale factor.
Mike Klein7a177b42019-06-17 17:17:47 -05002613 auto scale = sk_unaligned_load<F>(c->scalex)
2614 * sk_unaligned_load<F>(c->scaley);
Mike Klein0a904492017-04-12 12:52:48 -04002615 dr = mad(scale, r, dr);
2616 dg = mad(scale, g, dg);
2617 db = mad(scale, b, db);
2618 da = mad(scale, a, da);
2619}
2620
2621// In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
2622// are combined in direct proportion to their area overlapping that logical query pixel.
2623// At positive offsets, the x-axis contribution to that rectangle is fx, or (1-fx) at negative x.
2624// The y-axis is symmetric.
2625
2626template <int kScale>
Mike Kleinb11ab572018-10-24 06:42:14 -04002627SI void bilinear_x(SkRasterPipeline_SamplerCtx* ctx, F* x) {
Mike Klein7a177b42019-06-17 17:17:47 -05002628 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
2629 F fx = sk_unaligned_load<F>(ctx->fx);
Mike Klein0a904492017-04-12 12:52:48 -04002630
2631 F scalex;
Mike Kleinfe560a82017-05-01 12:56:35 -04002632 if (kScale == -1) { scalex = 1.0f - fx; }
2633 if (kScale == +1) { scalex = fx; }
Mike Klein7a177b42019-06-17 17:17:47 -05002634 sk_unaligned_store(ctx->scalex, scalex);
Mike Klein0a904492017-04-12 12:52:48 -04002635}
2636template <int kScale>
Mike Kleinb11ab572018-10-24 06:42:14 -04002637SI void bilinear_y(SkRasterPipeline_SamplerCtx* ctx, F* y) {
Mike Klein7a177b42019-06-17 17:17:47 -05002638 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
2639 F fy = sk_unaligned_load<F>(ctx->fy);
Mike Klein0a904492017-04-12 12:52:48 -04002640
2641 F scaley;
Mike Kleinfe560a82017-05-01 12:56:35 -04002642 if (kScale == -1) { scaley = 1.0f - fy; }
2643 if (kScale == +1) { scaley = fy; }
Mike Klein7a177b42019-06-17 17:17:47 -05002644 sk_unaligned_store(ctx->scaley, scaley);
Mike Klein0a904492017-04-12 12:52:48 -04002645}
2646
Mike Kleinb11ab572018-10-24 06:42:14 -04002647STAGE(bilinear_nx, SkRasterPipeline_SamplerCtx* ctx) { bilinear_x<-1>(ctx, &r); }
2648STAGE(bilinear_px, SkRasterPipeline_SamplerCtx* ctx) { bilinear_x<+1>(ctx, &r); }
2649STAGE(bilinear_ny, SkRasterPipeline_SamplerCtx* ctx) { bilinear_y<-1>(ctx, &g); }
2650STAGE(bilinear_py, SkRasterPipeline_SamplerCtx* ctx) { bilinear_y<+1>(ctx, &g); }
Mike Klein0a904492017-04-12 12:52:48 -04002651
2652
2653// In bicubic interpolation, the 16 pixels and +/- 0.5 and +/- 1.5 offsets from the sample
2654// pixel center are combined with a non-uniform cubic filter, with higher values near the center.
2655//
2656// We break this function into two parts, one for near 0.5 offsets and one for far 1.5 offsets.
2657// See GrCubicEffect for details of this particular filter.
2658
2659SI F bicubic_near(F t) {
2660 // 1/18 + 9/18t + 27/18t^2 - 21/18t^3 == t ( t ( -21/18t + 27/18) + 9/18) + 1/18
Mike Kleinfe560a82017-05-01 12:56:35 -04002661 return mad(t, mad(t, mad((-21/18.0f), t, (27/18.0f)), (9/18.0f)), (1/18.0f));
Mike Klein0a904492017-04-12 12:52:48 -04002662}
2663SI F bicubic_far(F t) {
2664 // 0/18 + 0/18*t - 6/18t^2 + 7/18t^3 == t^2 (7/18t - 6/18)
Mike Kleinfe560a82017-05-01 12:56:35 -04002665 return (t*t)*mad((7/18.0f), t, (-6/18.0f));
Mike Klein0a904492017-04-12 12:52:48 -04002666}
2667
2668template <int kScale>
Mike Kleinb11ab572018-10-24 06:42:14 -04002669SI void bicubic_x(SkRasterPipeline_SamplerCtx* ctx, F* x) {
Mike Klein7a177b42019-06-17 17:17:47 -05002670 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
2671 F fx = sk_unaligned_load<F>(ctx->fx);
Mike Klein0a904492017-04-12 12:52:48 -04002672
2673 F scalex;
Mike Kleinfe560a82017-05-01 12:56:35 -04002674 if (kScale == -3) { scalex = bicubic_far (1.0f - fx); }
2675 if (kScale == -1) { scalex = bicubic_near(1.0f - fx); }
2676 if (kScale == +1) { scalex = bicubic_near( fx); }
2677 if (kScale == +3) { scalex = bicubic_far ( fx); }
Mike Klein7a177b42019-06-17 17:17:47 -05002678 sk_unaligned_store(ctx->scalex, scalex);
Mike Klein0a904492017-04-12 12:52:48 -04002679}
2680template <int kScale>
Mike Kleinb11ab572018-10-24 06:42:14 -04002681SI void bicubic_y(SkRasterPipeline_SamplerCtx* ctx, F* y) {
Mike Klein7a177b42019-06-17 17:17:47 -05002682 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
2683 F fy = sk_unaligned_load<F>(ctx->fy);
Mike Klein0a904492017-04-12 12:52:48 -04002684
2685 F scaley;
Mike Kleinfe560a82017-05-01 12:56:35 -04002686 if (kScale == -3) { scaley = bicubic_far (1.0f - fy); }
2687 if (kScale == -1) { scaley = bicubic_near(1.0f - fy); }
2688 if (kScale == +1) { scaley = bicubic_near( fy); }
2689 if (kScale == +3) { scaley = bicubic_far ( fy); }
Mike Klein7a177b42019-06-17 17:17:47 -05002690 sk_unaligned_store(ctx->scaley, scaley);
Mike Klein0a904492017-04-12 12:52:48 -04002691}
2692
Mike Kleinb11ab572018-10-24 06:42:14 -04002693STAGE(bicubic_n3x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<-3>(ctx, &r); }
2694STAGE(bicubic_n1x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<-1>(ctx, &r); }
2695STAGE(bicubic_p1x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<+1>(ctx, &r); }
2696STAGE(bicubic_p3x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<+3>(ctx, &r); }
Mike Klein0a904492017-04-12 12:52:48 -04002697
Mike Kleinb11ab572018-10-24 06:42:14 -04002698STAGE(bicubic_n3y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<-3>(ctx, &g); }
2699STAGE(bicubic_n1y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<-1>(ctx, &g); }
2700STAGE(bicubic_p1y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<+1>(ctx, &g); }
2701STAGE(bicubic_p3y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<+3>(ctx, &g); }
Mike Klein7fee90c2017-04-07 16:55:09 -04002702
Mike Kleinb11ab572018-10-24 06:42:14 -04002703STAGE(callback, SkRasterPipeline_CallbackCtx* c) {
Mike Kleinc17dc242017-04-20 16:21:57 -04002704 store4(c->rgba,0, r,g,b,a);
Mike Klein0e4d0962017-09-27 11:04:34 -04002705 c->fn(c, tail ? tail : N);
Mike Kleinc17dc242017-04-20 16:21:57 -04002706 load4(c->read_from,0, &r,&g,&b,&a);
Mike Klein7fee90c2017-04-07 16:55:09 -04002707}
Mike Kleinc2f876b2017-08-09 18:23:25 -04002708
Mike Reed019458d2019-07-17 12:23:24 -04002709// shader: void main(float x, float y, inout half4 color)
2710// colorfilter: void main(inout half4 color)
Brian Osman2b1a5442019-06-19 11:40:33 -04002711STAGE(interpreter, SkRasterPipeline_InterpreterCtx* c) {
Brian Osman4b202a32019-06-21 09:50:29 -04002712 // If N is less than the interpreter's VecWidth, then we are doing more work than necessary in
2713 // the interpreter. This is a known issue, and will be addressed at some point.
Mike Reed8c31f2b2019-07-16 16:50:14 -04002714 float xx[N], yy[N],
2715 rr[N], gg[N], bb[N], aa[N];
Brian Osman2b1a5442019-06-19 11:40:33 -04002716
Mike Reed019458d2019-07-17 12:23:24 -04002717 float* args[] = { xx, yy, rr, gg, bb, aa };
2718 float** in_args = args;
2719 int in_count = 6;
Mike Reed3fd3cc92019-06-20 12:40:30 -04002720
Mike Reed8c31f2b2019-07-16 16:50:14 -04002721 if (c->shaderConvention) {
2722 // our caller must have called seed_shader to set these
2723 sk_unaligned_store(xx, r);
2724 sk_unaligned_store(yy, g);
2725 sk_unaligned_store(rr, F(c->paintColor.fR));
2726 sk_unaligned_store(gg, F(c->paintColor.fG));
2727 sk_unaligned_store(bb, F(c->paintColor.fB));
2728 sk_unaligned_store(aa, F(c->paintColor.fA));
Mike Reed3fd3cc92019-06-20 12:40:30 -04002729 } else {
Mike Reed019458d2019-07-17 12:23:24 -04002730 in_args += 2; // skip x,y
2731 in_count = 4;
Mike Reed8c31f2b2019-07-16 16:50:14 -04002732 sk_unaligned_store(rr, r);
2733 sk_unaligned_store(gg, g);
Mike Reed3fd3cc92019-06-20 12:40:30 -04002734 sk_unaligned_store(bb, b);
2735 sk_unaligned_store(aa, a);
Mike Reed3fd3cc92019-06-20 12:40:30 -04002736 }
2737
Brian Osmanb23d66e2019-09-27 10:25:57 -04002738 SkAssertResult(c->byteCode->runStriped(c->fn, tail ? tail : N, in_args, in_count,
2739 nullptr, 0, (const float*)c->inputs, c->ninputs));
Brian Osman2b1a5442019-06-19 11:40:33 -04002740
2741 r = sk_unaligned_load<F>(rr);
2742 g = sk_unaligned_load<F>(gg);
2743 b = sk_unaligned_load<F>(bb);
2744 a = sk_unaligned_load<F>(aa);
2745}
2746
Mike Klein3cbcb732017-10-25 12:38:25 -04002747STAGE(gauss_a_to_rgba, Ctx::None) {
2748 // x = 1 - x;
2749 // exp(-x * x * 4) - 0.018f;
2750 // ... now approximate with quartic
2751 //
2752 const float c4 = -2.26661229133605957031f;
2753 const float c3 = 2.89795351028442382812f;
2754 const float c2 = 0.21345567703247070312f;
2755 const float c1 = 0.15489584207534790039f;
2756 const float c0 = 0.00030726194381713867f;
2757 a = mad(a, mad(a, mad(a, mad(a, c4, c3), c2), c1), c0);
2758 r = a;
2759 g = a;
2760 b = a;
2761}
Mike Klein1fa9c432017-12-11 09:59:47 -05002762
Mike Klein01005622019-08-13 12:22:17 -04002763SI F tile(F v, SkTileMode mode, float limit, float invLimit) {
2764 // The ix_and_ptr() calls in sample() will clamp tile()'s output, so no need to clamp here.
2765 switch (mode) {
2766 case SkTileMode::kDecal: // TODO, for now fallthrough to clamp
2767 case SkTileMode::kClamp: return v;
2768 case SkTileMode::kRepeat: return v - floor_(v*invLimit)*limit;
2769 case SkTileMode::kMirror:
2770 return abs_( (v-limit) - (limit+limit)*floor_((v-limit)*(invLimit*0.5f)) - limit );
2771 }
2772 SkUNREACHABLE;
2773}
2774
2775SI void sample(const SkRasterPipeline_SamplerCtx2* ctx, F x, F y,
2776 F* r, F* g, F* b, F* a) {
2777 x = tile(x, ctx->tileX, ctx->width , ctx->invWidth );
2778 y = tile(y, ctx->tileY, ctx->height, ctx->invHeight);
2779
2780 switch (ctx->ct) {
2781 default: *r = *g = *b = *a = 0; // TODO
2782 break;
2783
2784 case kRGBA_8888_SkColorType:
2785 case kBGRA_8888_SkColorType: {
2786 const uint32_t* ptr;
2787 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2788 from_8888(gather(ptr, ix), r,g,b,a);
2789 if (ctx->ct == kBGRA_8888_SkColorType) {
2790 std::swap(*r,*b);
2791 }
2792 } break;
2793 }
2794}
2795
2796template <int D>
2797SI void sampler(const SkRasterPipeline_SamplerCtx2* ctx,
2798 F cx, F cy, const F (&wx)[D], const F (&wy)[D],
2799 F* r, F* g, F* b, F* a) {
2800
2801 float start = -0.5f*(D-1);
2802
2803 *r = *g = *b = *a = 0;
2804 F y = cy + start;
2805 for (int j = 0; j < D; j++, y += 1.0f) {
2806 F x = cx + start;
2807 for (int i = 0; i < D; i++, x += 1.0f) {
2808 F R,G,B,A;
2809 sample(ctx, x,y, &R,&G,&B,&A);
2810
2811 F w = wx[i] * wy[j];
2812 *r = mad(w,R,*r);
2813 *g = mad(w,G,*g);
2814 *b = mad(w,B,*b);
2815 *a = mad(w,A,*a);
2816 }
2817 }
2818}
2819
2820STAGE(bilinear, const SkRasterPipeline_SamplerCtx2* ctx) {
2821 F x = r, fx = fract(x + 0.5f),
2822 y = g, fy = fract(y + 0.5f);
2823 const F wx[] = {1.0f - fx, fx};
2824 const F wy[] = {1.0f - fy, fy};
2825
2826 sampler(ctx, x,y, wx,wy, &r,&g,&b,&a);
2827}
2828STAGE(bicubic, SkRasterPipeline_SamplerCtx2* ctx) {
2829 F x = r, fx = fract(x + 0.5f),
2830 y = g, fy = fract(y + 0.5f);
2831 const F wx[] = { bicubic_far(1-fx), bicubic_near(1-fx), bicubic_near(fx), bicubic_far(fx) };
2832 const F wy[] = { bicubic_far(1-fy), bicubic_near(1-fy), bicubic_near(fy), bicubic_far(fy) };
2833
2834 sampler(ctx, x,y, wx,wy, &r,&g,&b,&a);
2835}
2836
Mike Klein1fa9c432017-12-11 09:59:47 -05002837// A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
Mike Kleinb11ab572018-10-24 06:42:14 -04002838STAGE(bilerp_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein1fa9c432017-12-11 09:59:47 -05002839 // (cx,cy) are the center of our sample.
2840 F cx = r,
2841 cy = g;
2842
2843 // All sample points are at the same fractional offset (fx,fy).
2844 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
2845 F fx = fract(cx + 0.5f),
2846 fy = fract(cy + 0.5f);
2847
2848 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
2849 r = g = b = a = 0;
2850
2851 for (float dy = -0.5f; dy <= +0.5f; dy += 1.0f)
2852 for (float dx = -0.5f; dx <= +0.5f; dx += 1.0f) {
2853 // (x,y) are the coordinates of this sample point.
2854 F x = cx + dx,
2855 y = cy + dy;
2856
2857 // ix_and_ptr() will clamp to the image's bounds for us.
2858 const uint32_t* ptr;
2859 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2860
2861 F sr,sg,sb,sa;
2862 from_8888(gather(ptr, ix), &sr,&sg,&sb,&sa);
2863
2864 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
2865 // are combined in direct proportion to their area overlapping that logical query pixel.
2866 // At positive offsets, the x-axis contribution to that rectangle is fx,
2867 // or (1-fx) at negative x. Same deal for y.
2868 F sx = (dx > 0) ? fx : 1.0f - fx,
2869 sy = (dy > 0) ? fy : 1.0f - fy,
2870 area = sx * sy;
2871
2872 r += sr * area;
2873 g += sg * area;
2874 b += sb * area;
2875 a += sa * area;
2876 }
2877}
Mike Klein1b9b7d52018-02-27 10:37:40 -05002878
Mike Reed78eedba2019-07-31 16:39:15 -04002879// A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
2880STAGE(bicubic_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
2881 // (cx,cy) are the center of our sample.
2882 F cx = r,
2883 cy = g;
2884
2885 // All sample points are at the same fractional offset (fx,fy).
2886 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
2887 F fx = fract(cx + 0.5f),
2888 fy = fract(cy + 0.5f);
2889
2890 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
2891 r = g = b = a = 0;
2892
2893 const F scaley[4] = {
2894 bicubic_far (1.0f - fy), bicubic_near(1.0f - fy),
2895 bicubic_near( fy), bicubic_far ( fy),
2896 };
2897 const F scalex[4] = {
2898 bicubic_far (1.0f - fx), bicubic_near(1.0f - fx),
2899 bicubic_near( fx), bicubic_far ( fx),
2900 };
2901
2902 F sample_y = cy - 1.5f;
2903 for (int yy = 0; yy <= 3; ++yy) {
2904 F sample_x = cx - 1.5f;
2905 for (int xx = 0; xx <= 3; ++xx) {
2906 F scale = scalex[xx] * scaley[yy];
2907
2908 // ix_and_ptr() will clamp to the image's bounds for us.
2909 const uint32_t* ptr;
2910 U32 ix = ix_and_ptr(&ptr, ctx, sample_x, sample_y);
2911
2912 F sr,sg,sb,sa;
2913 from_8888(gather(ptr, ix), &sr,&sg,&sb,&sa);
2914
2915 r = mad(scale, sr, r);
2916 g = mad(scale, sg, g);
2917 b = mad(scale, sb, b);
2918 a = mad(scale, sa, a);
2919
2920 sample_x += 1;
2921 }
2922 sample_y += 1;
2923 }
2924}
2925
Brian Salomon217522c2019-06-11 15:55:30 -04002926// ~~~~~~ GrSwizzle stage ~~~~~~ //
2927
2928STAGE(swizzle, void* ctx) {
2929 auto ir = r, ig = g, ib = b, ia = a;
2930 F* o[] = {&r, &g, &b, &a};
2931 char swiz[4];
2932 memcpy(swiz, &ctx, sizeof(swiz));
2933
2934 for (int i = 0; i < 4; ++i) {
2935 switch (swiz[i]) {
2936 case 'r': *o[i] = ir; break;
2937 case 'g': *o[i] = ig; break;
2938 case 'b': *o[i] = ib; break;
2939 case 'a': *o[i] = ia; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04002940 case '0': *o[i] = F(0); break;
Brian Salomon217522c2019-06-11 15:55:30 -04002941 case '1': *o[i] = F(1); break;
2942 default: break;
2943 }
2944 }
2945}
2946
Mike Klein1b9b7d52018-02-27 10:37:40 -05002947namespace lowp {
Mike Klein419709d2018-10-11 22:05:14 -04002948#if defined(JUMPER_IS_SCALAR) || defined(SK_DISABLE_LOWP_RASTER_PIPELINE)
Mike Klein1b9b7d52018-02-27 10:37:40 -05002949 // If we're not compiled by Clang, or otherwise switched into scalar mode (old Clang, manually),
2950 // we don't generate lowp stages. All these nullptrs will tell SkJumper.cpp to always use the
2951 // highp float pipeline.
2952 #define M(st) static void (*st)(void) = nullptr;
2953 SK_RASTER_PIPELINE_STAGES(M)
2954 #undef M
2955 static void (*just_return)(void) = nullptr;
2956
2957 static void start_pipeline(size_t,size_t,size_t,size_t, void**) {}
2958
2959#else // We are compiling vector code with Clang... let's make some lowp stages!
2960
Mike Klein83e86eb2018-08-31 10:19:21 -04002961#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05002962 using U8 = uint8_t __attribute__((ext_vector_type(16)));
2963 using U16 = uint16_t __attribute__((ext_vector_type(16)));
2964 using I16 = int16_t __attribute__((ext_vector_type(16)));
2965 using I32 = int32_t __attribute__((ext_vector_type(16)));
2966 using U32 = uint32_t __attribute__((ext_vector_type(16)));
2967 using F = float __attribute__((ext_vector_type(16)));
2968#else
2969 using U8 = uint8_t __attribute__((ext_vector_type(8)));
2970 using U16 = uint16_t __attribute__((ext_vector_type(8)));
2971 using I16 = int16_t __attribute__((ext_vector_type(8)));
2972 using I32 = int32_t __attribute__((ext_vector_type(8)));
2973 using U32 = uint32_t __attribute__((ext_vector_type(8)));
2974 using F = float __attribute__((ext_vector_type(8)));
2975#endif
2976
2977static const size_t N = sizeof(U16) / sizeof(uint16_t);
2978
Mike Kleina46623b2018-03-10 10:27:24 -05002979// Once again, some platforms benefit from a restricted Stage calling convention,
2980// but others can pass tons and tons of registers and we're happy to exploit that.
2981// It's exactly the same decision and implementation strategy as the F stages above.
2982#if JUMPER_NARROW_STAGES
2983 struct Params {
2984 size_t dx, dy, tail;
2985 U16 dr,dg,db,da;
2986 };
2987 using Stage = void(ABI*)(Params*, void** program, U16 r, U16 g, U16 b, U16 a);
2988#else
2989 // We pass program as the second argument so that load_and_inc() will find it in %rsi on x86-64.
2990 using Stage = void (ABI*)(size_t tail, void** program, size_t dx, size_t dy,
2991 U16 r, U16 g, U16 b, U16 a,
2992 U16 dr, U16 dg, U16 db, U16 da);
2993#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05002994
2995static void start_pipeline(const size_t x0, const size_t y0,
2996 const size_t xlimit, const size_t ylimit, void** program) {
2997 auto start = (Stage)load_and_inc(program);
2998 for (size_t dy = y0; dy < ylimit; dy++) {
Mike Kleina46623b2018-03-10 10:27:24 -05002999 #if JUMPER_NARROW_STAGES
3000 Params params = { x0,dy,0, 0,0,0,0 };
3001 for (; params.dx + N <= xlimit; params.dx += N) {
3002 start(&params,program, 0,0,0,0);
3003 }
3004 if (size_t tail = xlimit - params.dx) {
3005 params.tail = tail;
3006 start(&params,program, 0,0,0,0);
3007 }
3008 #else
Mike Klein1b9b7d52018-02-27 10:37:40 -05003009 size_t dx = x0;
3010 for (; dx + N <= xlimit; dx += N) {
3011 start( 0,program,dx,dy, 0,0,0,0, 0,0,0,0);
3012 }
3013 if (size_t tail = xlimit - dx) {
3014 start(tail,program,dx,dy, 0,0,0,0, 0,0,0,0);
3015 }
Mike Kleina46623b2018-03-10 10:27:24 -05003016 #endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05003017 }
3018}
3019
Mike Kleina46623b2018-03-10 10:27:24 -05003020#if JUMPER_NARROW_STAGES
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003021 static void ABI just_return(Params*, void**, U16,U16,U16,U16) {}
Mike Kleina46623b2018-03-10 10:27:24 -05003022#else
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003023 static void ABI just_return(size_t,void**,size_t,size_t, U16,U16,U16,U16, U16,U16,U16,U16) {}
Mike Kleina46623b2018-03-10 10:27:24 -05003024#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05003025
3026// All stages use the same function call ABI to chain into each other, but there are three types:
3027// GG: geometry in, geometry out -- think, a matrix
3028// GP: geometry in, pixels out. -- think, a memory gather
3029// PP: pixels in, pixels out. -- think, a blend mode
3030//
3031// (Some stages ignore their inputs or produce no logical output. That's perfectly fine.)
3032//
3033// These three STAGE_ macros let you define each type of stage,
3034// and will have (x,y) geometry and/or (r,g,b,a, dr,dg,db,da) pixel arguments as appropriate.
3035
Mike Kleina46623b2018-03-10 10:27:24 -05003036#if JUMPER_NARROW_STAGES
Mike Klein8354c522018-12-19 10:45:14 -05003037 #define STAGE_GG(name, ...) \
Mike Klein4c249ff2019-03-18 11:57:58 -05003038 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y); \
Mike Klein8354c522018-12-19 10:45:14 -05003039 static void ABI name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
3040 auto x = join<F>(r,g), \
3041 y = join<F>(b,a); \
Mike Klein4c249ff2019-03-18 11:57:58 -05003042 name##_k(Ctx{program}, params->dx,params->dy,params->tail, x,y); \
Mike Klein8354c522018-12-19 10:45:14 -05003043 split(x, &r,&g); \
3044 split(y, &b,&a); \
3045 auto next = (Stage)load_and_inc(program); \
3046 next(params,program, r,g,b,a); \
3047 } \
Mike Klein4c249ff2019-03-18 11:57:58 -05003048 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003049
Mike Kleina46623b2018-03-10 10:27:24 -05003050 #define STAGE_GP(name, ...) \
3051 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
3052 U16& r, U16& g, U16& b, U16& a, \
3053 U16& dr, U16& dg, U16& db, U16& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003054 static void ABI name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
Mike Kleina46623b2018-03-10 10:27:24 -05003055 auto x = join<F>(r,g), \
3056 y = join<F>(b,a); \
3057 name##_k(Ctx{program}, params->dx,params->dy,params->tail, x,y, r,g,b,a, \
3058 params->dr,params->dg,params->db,params->da); \
3059 auto next = (Stage)load_and_inc(program); \
3060 next(params,program, r,g,b,a); \
3061 } \
3062 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
3063 U16& r, U16& g, U16& b, U16& a, \
3064 U16& dr, U16& dg, U16& db, U16& da)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003065
Mike Kleina46623b2018-03-10 10:27:24 -05003066 #define STAGE_PP(name, ...) \
Mike Klein4c249ff2019-03-18 11:57:58 -05003067 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleina46623b2018-03-10 10:27:24 -05003068 U16& r, U16& g, U16& b, U16& a, \
3069 U16& dr, U16& dg, U16& db, U16& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003070 static void ABI name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
Mike Klein4c249ff2019-03-18 11:57:58 -05003071 name##_k(Ctx{program}, params->dx,params->dy,params->tail, r,g,b,a, \
Mike Kleina46623b2018-03-10 10:27:24 -05003072 params->dr,params->dg,params->db,params->da); \
3073 auto next = (Stage)load_and_inc(program); \
3074 next(params,program, r,g,b,a); \
3075 } \
Mike Klein4c249ff2019-03-18 11:57:58 -05003076 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleina46623b2018-03-10 10:27:24 -05003077 U16& r, U16& g, U16& b, U16& a, \
3078 U16& dr, U16& dg, U16& db, U16& da)
3079#else
3080 #define STAGE_GG(name, ...) \
Mike Klein4c249ff2019-03-18 11:57:58 -05003081 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003082 static void ABI name(size_t tail, void** program, size_t dx, size_t dy, \
Mike Kleina46623b2018-03-10 10:27:24 -05003083 U16 r, U16 g, U16 b, U16 a, \
3084 U16 dr, U16 dg, U16 db, U16 da) { \
3085 auto x = join<F>(r,g), \
3086 y = join<F>(b,a); \
Mike Klein4c249ff2019-03-18 11:57:58 -05003087 name##_k(Ctx{program}, dx,dy,tail, x,y); \
Mike Kleina46623b2018-03-10 10:27:24 -05003088 split(x, &r,&g); \
3089 split(y, &b,&a); \
3090 auto next = (Stage)load_and_inc(program); \
3091 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
3092 } \
Mike Klein4c249ff2019-03-18 11:57:58 -05003093 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y)
Mike Kleina46623b2018-03-10 10:27:24 -05003094
3095 #define STAGE_GP(name, ...) \
3096 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
3097 U16& r, U16& g, U16& b, U16& a, \
3098 U16& dr, U16& dg, U16& db, U16& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003099 static void ABI name(size_t tail, void** program, size_t dx, size_t dy, \
Mike Kleina46623b2018-03-10 10:27:24 -05003100 U16 r, U16 g, U16 b, U16 a, \
3101 U16 dr, U16 dg, U16 db, U16 da) { \
3102 auto x = join<F>(r,g), \
3103 y = join<F>(b,a); \
3104 name##_k(Ctx{program}, dx,dy,tail, x,y, r,g,b,a, dr,dg,db,da); \
3105 auto next = (Stage)load_and_inc(program); \
3106 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
3107 } \
3108 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
3109 U16& r, U16& g, U16& b, U16& a, \
3110 U16& dr, U16& dg, U16& db, U16& da)
3111
3112 #define STAGE_PP(name, ...) \
Mike Klein4c249ff2019-03-18 11:57:58 -05003113 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleina46623b2018-03-10 10:27:24 -05003114 U16& r, U16& g, U16& b, U16& a, \
3115 U16& dr, U16& dg, U16& db, U16& da); \
Mike Klein4d4b3aa2018-03-21 13:07:35 -04003116 static void ABI name(size_t tail, void** program, size_t dx, size_t dy, \
Mike Kleina46623b2018-03-10 10:27:24 -05003117 U16 r, U16 g, U16 b, U16 a, \
3118 U16 dr, U16 dg, U16 db, U16 da) { \
Mike Klein4c249ff2019-03-18 11:57:58 -05003119 name##_k(Ctx{program}, dx,dy,tail, r,g,b,a, dr,dg,db,da); \
Mike Kleina46623b2018-03-10 10:27:24 -05003120 auto next = (Stage)load_and_inc(program); \
3121 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
3122 } \
Mike Klein4c249ff2019-03-18 11:57:58 -05003123 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleina46623b2018-03-10 10:27:24 -05003124 U16& r, U16& g, U16& b, U16& a, \
3125 U16& dr, U16& dg, U16& db, U16& da)
3126#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05003127
3128// ~~~~~~ Commonly used helper functions ~~~~~~ //
3129
3130SI U16 div255(U16 v) {
3131#if 0
3132 return (v+127)/255; // The ideal rounding divide by 255.
Mike Klein73d7ffc2018-07-25 09:19:23 -04003133#elif 1 && defined(JUMPER_IS_NEON)
Mike Kleind8853ec2018-03-10 11:34:53 -05003134 // With NEON we can compute (v+127)/255 as (v + ((v+128)>>8) + 128)>>8
3135 // just as fast as we can do the approximation below, so might as well be correct!
3136 // First we compute v + ((v+128)>>8), then one more round of (...+128)>>8 to finish up.
3137 return vrshrq_n_u16(vrsraq_n_u16(v, v, 8), 8);
Mike Klein1b9b7d52018-02-27 10:37:40 -05003138#else
3139 return (v+255)/256; // A good approximation of (v+127)/255.
3140#endif
3141}
3142
3143SI U16 inv(U16 v) { return 255-v; }
3144
3145SI U16 if_then_else(I16 c, U16 t, U16 e) { return (t & c) | (e & ~c); }
3146SI U32 if_then_else(I32 c, U32 t, U32 e) { return (t & c) | (e & ~c); }
3147
3148SI U16 max(U16 x, U16 y) { return if_then_else(x < y, y, x); }
3149SI U16 min(U16 x, U16 y) { return if_then_else(x < y, x, y); }
Mike Klein1b9b7d52018-02-27 10:37:40 -05003150
3151SI U16 from_float(float f) { return f * 255.0f + 0.5f; }
3152
3153SI U16 lerp(U16 from, U16 to, U16 t) { return div255( from*inv(t) + to*t ); }
3154
3155template <typename D, typename S>
3156SI D cast(S src) {
3157 return __builtin_convertvector(src, D);
3158}
3159
3160template <typename D, typename S>
3161SI void split(S v, D* lo, D* hi) {
3162 static_assert(2*sizeof(D) == sizeof(S), "");
3163 memcpy(lo, (const char*)&v + 0*sizeof(D), sizeof(D));
3164 memcpy(hi, (const char*)&v + 1*sizeof(D), sizeof(D));
3165}
3166template <typename D, typename S>
3167SI D join(S lo, S hi) {
3168 static_assert(sizeof(D) == 2*sizeof(S), "");
3169 D v;
3170 memcpy((char*)&v + 0*sizeof(S), &lo, sizeof(S));
3171 memcpy((char*)&v + 1*sizeof(S), &hi, sizeof(S));
3172 return v;
3173}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003174
3175SI F if_then_else(I32 c, F t, F e) {
3176 return bit_cast<F>( (bit_cast<I32>(t) & c) | (bit_cast<I32>(e) & ~c) );
3177}
3178SI F max(F x, F y) { return if_then_else(x < y, y, x); }
3179SI F min(F x, F y) { return if_then_else(x < y, x, y); }
3180
3181SI F mad(F f, F m, F a) { return f*m+a; }
3182SI U32 trunc_(F x) { return (U32)cast<I32>(x); }
3183
3184SI F rcp(F x) {
Mike Klein83e86eb2018-08-31 10:19:21 -04003185#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Kleine304a8a2018-05-31 10:49:51 -04003186 __m256 lo,hi;
3187 split(x, &lo,&hi);
3188 return join<F>(_mm256_rcp_ps(lo), _mm256_rcp_ps(hi));
Mike Klein83e86eb2018-08-31 10:19:21 -04003189#elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX)
Mike Kleine304a8a2018-05-31 10:49:51 -04003190 __m128 lo,hi;
3191 split(x, &lo,&hi);
3192 return join<F>(_mm_rcp_ps(lo), _mm_rcp_ps(hi));
Mike Klein73d7ffc2018-07-25 09:19:23 -04003193#elif defined(JUMPER_IS_NEON)
Mike Kleine304a8a2018-05-31 10:49:51 -04003194 auto rcp = [](float32x4_t v) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003195 auto est = vrecpeq_f32(v);
3196 return vrecpsq_f32(v,est)*est;
Mike Kleine304a8a2018-05-31 10:49:51 -04003197 };
3198 float32x4_t lo,hi;
3199 split(x, &lo,&hi);
3200 return join<F>(rcp(lo), rcp(hi));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003201#else
3202 return 1.0f / x;
3203#endif
3204}
3205SI F sqrt_(F x) {
Mike Klein83e86eb2018-08-31 10:19:21 -04003206#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Kleine304a8a2018-05-31 10:49:51 -04003207 __m256 lo,hi;
3208 split(x, &lo,&hi);
3209 return join<F>(_mm256_sqrt_ps(lo), _mm256_sqrt_ps(hi));
Mike Klein83e86eb2018-08-31 10:19:21 -04003210#elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX)
Mike Kleine304a8a2018-05-31 10:49:51 -04003211 __m128 lo,hi;
3212 split(x, &lo,&hi);
3213 return join<F>(_mm_sqrt_ps(lo), _mm_sqrt_ps(hi));
Mike Klein15eb1e92018-08-31 11:21:27 -04003214#elif defined(SK_CPU_ARM64)
Mike Kleine304a8a2018-05-31 10:49:51 -04003215 float32x4_t lo,hi;
3216 split(x, &lo,&hi);
3217 return join<F>(vsqrtq_f32(lo), vsqrtq_f32(hi));
Mike Klein73d7ffc2018-07-25 09:19:23 -04003218#elif defined(JUMPER_IS_NEON)
Mike Kleine304a8a2018-05-31 10:49:51 -04003219 auto sqrt = [](float32x4_t v) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003220 auto est = vrsqrteq_f32(v); // Estimate and two refinement steps for est = rsqrt(v).
3221 est *= vrsqrtsq_f32(v,est*est);
3222 est *= vrsqrtsq_f32(v,est*est);
3223 return v*est; // sqrt(v) == v*rsqrt(v).
Mike Kleine304a8a2018-05-31 10:49:51 -04003224 };
3225 float32x4_t lo,hi;
3226 split(x, &lo,&hi);
3227 return join<F>(sqrt(lo), sqrt(hi));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003228#else
3229 return F{
3230 sqrtf(x[0]), sqrtf(x[1]), sqrtf(x[2]), sqrtf(x[3]),
3231 sqrtf(x[4]), sqrtf(x[5]), sqrtf(x[6]), sqrtf(x[7]),
3232 };
3233#endif
3234}
3235
3236SI F floor_(F x) {
Mike Klein15eb1e92018-08-31 11:21:27 -04003237#if defined(SK_CPU_ARM64)
Mike Kleine304a8a2018-05-31 10:49:51 -04003238 float32x4_t lo,hi;
3239 split(x, &lo,&hi);
3240 return join<F>(vrndmq_f32(lo), vrndmq_f32(hi));
Mike Klein83e86eb2018-08-31 10:19:21 -04003241#elif defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Kleine304a8a2018-05-31 10:49:51 -04003242 __m256 lo,hi;
3243 split(x, &lo,&hi);
3244 return join<F>(_mm256_floor_ps(lo), _mm256_floor_ps(hi));
Mike Klein83e86eb2018-08-31 10:19:21 -04003245#elif defined(JUMPER_IS_SSE41) || defined(JUMPER_IS_AVX)
Mike Kleine304a8a2018-05-31 10:49:51 -04003246 __m128 lo,hi;
3247 split(x, &lo,&hi);
3248 return join<F>(_mm_floor_ps(lo), _mm_floor_ps(hi));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003249#else
3250 F roundtrip = cast<F>(cast<I32>(x));
3251 return roundtrip - if_then_else(roundtrip > x, F(1), F(0));
3252#endif
3253}
Mike Klein8e3426f2018-04-16 12:56:24 -04003254SI F fract(F x) { return x - floor_(x); }
Mike Klein1b9b7d52018-02-27 10:37:40 -05003255SI F abs_(F x) { return bit_cast<F>( bit_cast<I32>(x) & 0x7fffffff ); }
3256
3257// ~~~~~~ Basic / misc. stages ~~~~~~ //
3258
Mike Kleine8de0242018-03-10 12:37:11 -05003259STAGE_GG(seed_shader, Ctx::None) {
3260 static const float iota[] = {
3261 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
3262 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
3263 };
Mike Klein7a177b42019-06-17 17:17:47 -05003264 x = cast<F>(I32(dx)) + sk_unaligned_load<F>(iota);
Mike Klein1b9b7d52018-02-27 10:37:40 -05003265 y = cast<F>(I32(dy)) + 0.5f;
3266}
3267
3268STAGE_GG(matrix_translate, const float* m) {
3269 x += m[0];
3270 y += m[1];
3271}
3272STAGE_GG(matrix_scale_translate, const float* m) {
3273 x = mad(x,m[0], m[2]);
3274 y = mad(y,m[1], m[3]);
3275}
3276STAGE_GG(matrix_2x3, const float* m) {
3277 auto X = mad(x,m[0], mad(y,m[2], m[4])),
3278 Y = mad(x,m[1], mad(y,m[3], m[5]));
3279 x = X;
3280 y = Y;
3281}
3282STAGE_GG(matrix_perspective, const float* m) {
3283 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
3284 auto X = mad(x,m[0], mad(y,m[1], m[2])),
3285 Y = mad(x,m[3], mad(y,m[4], m[5])),
3286 Z = mad(x,m[6], mad(y,m[7], m[8]));
3287 x = X * rcp(Z);
3288 y = Y * rcp(Z);
3289}
3290
Mike Kleinb11ab572018-10-24 06:42:14 -04003291STAGE_PP(uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003292 r = c->rgba[0];
3293 g = c->rgba[1];
3294 b = c->rgba[2];
3295 a = c->rgba[3];
3296}
Mike Reed9318a6c2019-08-16 16:16:25 -04003297STAGE_PP(uniform_color_dst, const SkRasterPipeline_UniformColorCtx* c) {
3298 dr = c->rgba[0];
3299 dg = c->rgba[1];
3300 db = c->rgba[2];
3301 da = c->rgba[3];
3302}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003303STAGE_PP(black_color, Ctx::None) { r = g = b = 0; a = 255; }
3304STAGE_PP(white_color, Ctx::None) { r = g = b = 255; a = 255; }
3305
3306STAGE_PP(set_rgb, const float rgb[3]) {
3307 r = from_float(rgb[0]);
3308 g = from_float(rgb[1]);
3309 b = from_float(rgb[2]);
3310}
3311
Mike Kleinea045b52018-08-23 12:13:58 -04003312STAGE_PP(clamp_0, Ctx::None) { /*definitely a noop*/ }
3313STAGE_PP(clamp_1, Ctx::None) { /*_should_ be a noop*/ }
3314
Mike Klein1b9b7d52018-02-27 10:37:40 -05003315STAGE_PP(clamp_a, Ctx::None) {
3316 r = min(r, a);
3317 g = min(g, a);
3318 b = min(b, a);
3319}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003320
Mike Kleineb50f432018-09-07 11:08:53 -04003321STAGE_PP(clamp_gamut, Ctx::None) {
3322 // It shouldn't be possible to get out-of-gamut
3323 // colors when working in lowp.
3324}
3325
Mike Klein1b9b7d52018-02-27 10:37:40 -05003326STAGE_PP(premul, Ctx::None) {
3327 r = div255(r * a);
3328 g = div255(g * a);
3329 b = div255(b * a);
3330}
3331STAGE_PP(premul_dst, Ctx::None) {
3332 dr = div255(dr * da);
3333 dg = div255(dg * da);
3334 db = div255(db * da);
3335}
3336
3337STAGE_PP(force_opaque , Ctx::None) { a = 255; }
3338STAGE_PP(force_opaque_dst, Ctx::None) { da = 255; }
3339
3340STAGE_PP(swap_rb, Ctx::None) {
3341 auto tmp = r;
3342 r = b;
3343 b = tmp;
3344}
Mike Klein1a3eb522018-10-18 10:11:00 -04003345STAGE_PP(swap_rb_dst, Ctx::None) {
3346 auto tmp = dr;
3347 dr = db;
3348 db = tmp;
3349}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003350
3351STAGE_PP(move_src_dst, Ctx::None) {
3352 dr = r;
3353 dg = g;
3354 db = b;
3355 da = a;
3356}
3357
3358STAGE_PP(move_dst_src, Ctx::None) {
3359 r = dr;
3360 g = dg;
3361 b = db;
3362 a = da;
3363}
3364
Mike Klein1b9b7d52018-02-27 10:37:40 -05003365// ~~~~~~ Blend modes ~~~~~~ //
3366
3367// The same logic applied to all 4 channels.
3368#define BLEND_MODE(name) \
3369 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
3370 STAGE_PP(name, Ctx::None) { \
3371 r = name##_channel(r,dr,a,da); \
3372 g = name##_channel(g,dg,a,da); \
3373 b = name##_channel(b,db,a,da); \
3374 a = name##_channel(a,da,a,da); \
3375 } \
3376 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
3377
3378 BLEND_MODE(clear) { return 0; }
3379 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
3380 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
3381 BLEND_MODE(srcin) { return div255( s*da ); }
3382 BLEND_MODE(dstin) { return div255( d*sa ); }
3383 BLEND_MODE(srcout) { return div255( s*inv(da) ); }
3384 BLEND_MODE(dstout) { return div255( d*inv(sa) ); }
3385 BLEND_MODE(srcover) { return s + div255( d*inv(sa) ); }
3386 BLEND_MODE(dstover) { return d + div255( s*inv(da) ); }
3387 BLEND_MODE(modulate) { return div255( s*d ); }
3388 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
Mike Kleinb90c0802019-03-15 14:03:41 +00003389 BLEND_MODE(plus_) { return min(s+d, 255); }
Mike Klein1b9b7d52018-02-27 10:37:40 -05003390 BLEND_MODE(screen) { return s + d - div255( s*d ); }
3391 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
3392#undef BLEND_MODE
3393
3394// The same logic applied to color, and srcover for alpha.
3395#define BLEND_MODE(name) \
3396 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
3397 STAGE_PP(name, Ctx::None) { \
3398 r = name##_channel(r,dr,a,da); \
3399 g = name##_channel(g,dg,a,da); \
3400 b = name##_channel(b,db,a,da); \
3401 a = a + div255( da*inv(a) ); \
3402 } \
3403 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
3404
3405 BLEND_MODE(darken) { return s + d - div255( max(s*da, d*sa) ); }
3406 BLEND_MODE(lighten) { return s + d - div255( min(s*da, d*sa) ); }
3407 BLEND_MODE(difference) { return s + d - 2*div255( min(s*da, d*sa) ); }
3408 BLEND_MODE(exclusion) { return s + d - 2*div255( s*d ); }
3409
3410 BLEND_MODE(hardlight) {
3411 return div255( s*inv(da) + d*inv(sa) +
3412 if_then_else(2*s <= sa, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
3413 }
3414 BLEND_MODE(overlay) {
3415 return div255( s*inv(da) + d*inv(sa) +
3416 if_then_else(2*d <= da, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
3417 }
3418#undef BLEND_MODE
3419
3420// ~~~~~~ Helpers for interacting with memory ~~~~~~ //
3421
3422template <typename T>
Mike Kleinb11ab572018-10-24 06:42:14 -04003423SI T* ptr_at_xy(const SkRasterPipeline_MemoryCtx* ctx, size_t dx, size_t dy) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003424 return (T*)ctx->pixels + dy*ctx->stride + dx;
3425}
3426
3427template <typename T>
Mike Kleinb11ab572018-10-24 06:42:14 -04003428SI U32 ix_and_ptr(T** ptr, const SkRasterPipeline_GatherCtx* ctx, F x, F y) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003429 auto clamp = [](F v, F limit) {
3430 limit = bit_cast<F>( bit_cast<U32>(limit) - 1 ); // Exclusive -> inclusive.
3431 return min(max(0, v), limit);
3432 };
3433 x = clamp(x, ctx->width);
3434 y = clamp(y, ctx->height);
3435
3436 *ptr = (const T*)ctx->pixels;
3437 return trunc_(y)*ctx->stride + trunc_(x);
3438}
3439
3440template <typename V, typename T>
3441SI V load(const T* ptr, size_t tail) {
3442 V v = 0;
3443 switch (tail & (N-1)) {
3444 case 0: memcpy(&v, ptr, sizeof(v)); break;
Mike Klein83e86eb2018-08-31 10:19:21 -04003445 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003446 case 15: v[14] = ptr[14];
3447 case 14: v[13] = ptr[13];
3448 case 13: v[12] = ptr[12];
3449 case 12: memcpy(&v, ptr, 12*sizeof(T)); break;
3450 case 11: v[10] = ptr[10];
3451 case 10: v[ 9] = ptr[ 9];
3452 case 9: v[ 8] = ptr[ 8];
3453 case 8: memcpy(&v, ptr, 8*sizeof(T)); break;
3454 #endif
3455 case 7: v[ 6] = ptr[ 6];
3456 case 6: v[ 5] = ptr[ 5];
3457 case 5: v[ 4] = ptr[ 4];
3458 case 4: memcpy(&v, ptr, 4*sizeof(T)); break;
3459 case 3: v[ 2] = ptr[ 2];
3460 case 2: memcpy(&v, ptr, 2*sizeof(T)); break;
3461 case 1: v[ 0] = ptr[ 0];
3462 }
3463 return v;
3464}
3465template <typename V, typename T>
3466SI void store(T* ptr, size_t tail, V v) {
3467 switch (tail & (N-1)) {
3468 case 0: memcpy(ptr, &v, sizeof(v)); break;
Mike Klein83e86eb2018-08-31 10:19:21 -04003469 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003470 case 15: ptr[14] = v[14];
3471 case 14: ptr[13] = v[13];
3472 case 13: ptr[12] = v[12];
3473 case 12: memcpy(ptr, &v, 12*sizeof(T)); break;
3474 case 11: ptr[10] = v[10];
3475 case 10: ptr[ 9] = v[ 9];
3476 case 9: ptr[ 8] = v[ 8];
3477 case 8: memcpy(ptr, &v, 8*sizeof(T)); break;
3478 #endif
3479 case 7: ptr[ 6] = v[ 6];
3480 case 6: ptr[ 5] = v[ 5];
3481 case 5: ptr[ 4] = v[ 4];
3482 case 4: memcpy(ptr, &v, 4*sizeof(T)); break;
3483 case 3: ptr[ 2] = v[ 2];
3484 case 2: memcpy(ptr, &v, 2*sizeof(T)); break;
3485 case 1: ptr[ 0] = v[ 0];
3486 }
3487}
3488
Mike Klein83e86eb2018-08-31 10:19:21 -04003489#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003490 template <typename V, typename T>
3491 SI V gather(const T* ptr, U32 ix) {
3492 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
3493 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
3494 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
3495 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
3496 }
3497
3498 template<>
Kevin Lubickb5502b22018-03-12 10:17:06 -04003499 F gather(const float* ptr, U32 ix) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003500 __m256i lo, hi;
3501 split(ix, &lo, &hi);
3502
Kevin Lubickb5502b22018-03-12 10:17:06 -04003503 return join<F>(_mm256_i32gather_ps(ptr, lo, 4),
3504 _mm256_i32gather_ps(ptr, hi, 4));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003505 }
3506
3507 template<>
Kevin Lubickb5502b22018-03-12 10:17:06 -04003508 U32 gather(const uint32_t* ptr, U32 ix) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003509 __m256i lo, hi;
3510 split(ix, &lo, &hi);
3511
Kevin Lubickb5502b22018-03-12 10:17:06 -04003512 return join<U32>(_mm256_i32gather_epi32(ptr, lo, 4),
3513 _mm256_i32gather_epi32(ptr, hi, 4));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003514 }
3515#else
3516 template <typename V, typename T>
3517 SI V gather(const T* ptr, U32 ix) {
3518 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
3519 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]], };
3520 }
3521#endif
3522
3523
3524// ~~~~~~ 32-bit memory loads and stores ~~~~~~ //
3525
3526SI void from_8888(U32 rgba, U16* r, U16* g, U16* b, U16* a) {
Mike Klein83e86eb2018-08-31 10:19:21 -04003527#if 1 && defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003528 // Swap the middle 128-bit lanes to make _mm256_packus_epi32() in cast_U16() work out nicely.
3529 __m256i _01,_23;
3530 split(rgba, &_01, &_23);
3531 __m256i _02 = _mm256_permute2x128_si256(_01,_23, 0x20),
3532 _13 = _mm256_permute2x128_si256(_01,_23, 0x31);
3533 rgba = join<U32>(_02, _13);
3534
3535 auto cast_U16 = [](U32 v) -> U16 {
3536 __m256i _02,_13;
3537 split(v, &_02,&_13);
3538 return _mm256_packus_epi32(_02,_13);
3539 };
3540#else
3541 auto cast_U16 = [](U32 v) -> U16 {
3542 return cast<U16>(v);
3543 };
3544#endif
3545 *r = cast_U16(rgba & 65535) & 255;
3546 *g = cast_U16(rgba & 65535) >> 8;
3547 *b = cast_U16(rgba >> 16) & 255;
3548 *a = cast_U16(rgba >> 16) >> 8;
3549}
3550
3551SI void load_8888_(const uint32_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
Mike Klein73d7ffc2018-07-25 09:19:23 -04003552#if 1 && defined(JUMPER_IS_NEON)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003553 uint8x8x4_t rgba;
3554 switch (tail & (N-1)) {
3555 case 0: rgba = vld4_u8 ((const uint8_t*)(ptr+0) ); break;
3556 case 7: rgba = vld4_lane_u8((const uint8_t*)(ptr+6), rgba, 6);
3557 case 6: rgba = vld4_lane_u8((const uint8_t*)(ptr+5), rgba, 5);
3558 case 5: rgba = vld4_lane_u8((const uint8_t*)(ptr+4), rgba, 4);
3559 case 4: rgba = vld4_lane_u8((const uint8_t*)(ptr+3), rgba, 3);
3560 case 3: rgba = vld4_lane_u8((const uint8_t*)(ptr+2), rgba, 2);
3561 case 2: rgba = vld4_lane_u8((const uint8_t*)(ptr+1), rgba, 1);
3562 case 1: rgba = vld4_lane_u8((const uint8_t*)(ptr+0), rgba, 0);
3563 }
3564 *r = cast<U16>(rgba.val[0]);
3565 *g = cast<U16>(rgba.val[1]);
3566 *b = cast<U16>(rgba.val[2]);
3567 *a = cast<U16>(rgba.val[3]);
3568#else
3569 from_8888(load<U32>(ptr, tail), r,g,b,a);
3570#endif
3571}
3572SI void store_8888_(uint32_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
Mike Klein73d7ffc2018-07-25 09:19:23 -04003573#if 1 && defined(JUMPER_IS_NEON)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003574 uint8x8x4_t rgba = {{
3575 cast<U8>(r),
3576 cast<U8>(g),
3577 cast<U8>(b),
3578 cast<U8>(a),
3579 }};
3580 switch (tail & (N-1)) {
3581 case 0: vst4_u8 ((uint8_t*)(ptr+0), rgba ); break;
3582 case 7: vst4_lane_u8((uint8_t*)(ptr+6), rgba, 6);
3583 case 6: vst4_lane_u8((uint8_t*)(ptr+5), rgba, 5);
3584 case 5: vst4_lane_u8((uint8_t*)(ptr+4), rgba, 4);
3585 case 4: vst4_lane_u8((uint8_t*)(ptr+3), rgba, 3);
3586 case 3: vst4_lane_u8((uint8_t*)(ptr+2), rgba, 2);
3587 case 2: vst4_lane_u8((uint8_t*)(ptr+1), rgba, 1);
3588 case 1: vst4_lane_u8((uint8_t*)(ptr+0), rgba, 0);
3589 }
3590#else
3591 store(ptr, tail, cast<U32>(r | (g<<8)) << 0
3592 | cast<U32>(b | (a<<8)) << 16);
3593#endif
3594}
3595
Mike Kleinb11ab572018-10-24 06:42:14 -04003596STAGE_PP(load_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003597 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &r,&g,&b,&a);
3598}
Mike Kleinb11ab572018-10-24 06:42:14 -04003599STAGE_PP(load_8888_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003600 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &dr,&dg,&db,&da);
3601}
Mike Kleinb11ab572018-10-24 06:42:14 -04003602STAGE_PP(store_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003603 store_8888_(ptr_at_xy<uint32_t>(ctx, dx,dy), tail, r,g,b,a);
3604}
Mike Kleinb11ab572018-10-24 06:42:14 -04003605STAGE_GP(gather_8888, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003606 const uint32_t* ptr;
3607 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3608 from_8888(gather<U32>(ptr, ix), &r, &g, &b, &a);
3609}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003610
3611// ~~~~~~ 16-bit memory loads and stores ~~~~~~ //
3612
3613SI void from_565(U16 rgb, U16* r, U16* g, U16* b) {
3614 // Format for 565 buffers: 15|rrrrr gggggg bbbbb|0
3615 U16 R = (rgb >> 11) & 31,
3616 G = (rgb >> 5) & 63,
3617 B = (rgb >> 0) & 31;
3618
3619 // These bit replications are the same as multiplying by 255/31 or 255/63 to scale to 8-bit.
3620 *r = (R << 3) | (R >> 2);
3621 *g = (G << 2) | (G >> 4);
3622 *b = (B << 3) | (B >> 2);
3623}
3624SI void load_565_(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
3625 from_565(load<U16>(ptr, tail), r,g,b);
3626}
3627SI void store_565_(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b) {
Mike Klein1c941432019-02-27 14:22:55 -06003628 // Round from [0,255] to [0,31] or [0,63], as if x * (31/255.0f) + 0.5f.
3629 // (Don't feel like you need to find some fundamental truth in these...
3630 // they were brute-force searched.)
3631 U16 R = (r * 9 + 36) / 74, // 9/74 ≈ 31/255, plus 36/74, about half.
3632 G = (g * 21 + 42) / 85, // 21/85 = 63/255 exactly.
3633 B = (b * 9 + 36) / 74;
Mike Klein1b9b7d52018-02-27 10:37:40 -05003634 // Pack them back into 15|rrrrr gggggg bbbbb|0.
3635 store(ptr, tail, R << 11
3636 | G << 5
3637 | B << 0);
3638}
3639
Mike Kleinb11ab572018-10-24 06:42:14 -04003640STAGE_PP(load_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003641 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &r,&g,&b);
3642 a = 255;
3643}
Mike Kleinb11ab572018-10-24 06:42:14 -04003644STAGE_PP(load_565_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003645 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &dr,&dg,&db);
3646 da = 255;
3647}
Mike Kleinb11ab572018-10-24 06:42:14 -04003648STAGE_PP(store_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003649 store_565_(ptr_at_xy<uint16_t>(ctx, dx,dy), tail, r,g,b);
3650}
Mike Kleinb11ab572018-10-24 06:42:14 -04003651STAGE_GP(gather_565, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003652 const uint16_t* ptr;
3653 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3654 from_565(gather<U16>(ptr, ix), &r, &g, &b);
3655 a = 255;
3656}
3657
3658SI void from_4444(U16 rgba, U16* r, U16* g, U16* b, U16* a) {
3659 // Format for 4444 buffers: 15|rrrr gggg bbbb aaaa|0.
3660 U16 R = (rgba >> 12) & 15,
3661 G = (rgba >> 8) & 15,
3662 B = (rgba >> 4) & 15,
3663 A = (rgba >> 0) & 15;
3664
3665 // Scale [0,15] to [0,255].
3666 *r = (R << 4) | R;
3667 *g = (G << 4) | G;
3668 *b = (B << 4) | B;
3669 *a = (A << 4) | A;
3670}
3671SI void load_4444_(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
3672 from_4444(load<U16>(ptr, tail), r,g,b,a);
3673}
3674SI void store_4444_(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
Mike Klein1c941432019-02-27 14:22:55 -06003675 // Round from [0,255] to [0,15], producing the same value as (x*(15/255.0f) + 0.5f).
3676 U16 R = (r + 8) / 17,
3677 G = (g + 8) / 17,
3678 B = (b + 8) / 17,
3679 A = (a + 8) / 17;
Mike Klein1b9b7d52018-02-27 10:37:40 -05003680 // Pack them back into 15|rrrr gggg bbbb aaaa|0.
3681 store(ptr, tail, R << 12
3682 | G << 8
3683 | B << 4
3684 | A << 0);
3685}
3686
Mike Kleinb11ab572018-10-24 06:42:14 -04003687STAGE_PP(load_4444, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003688 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &r,&g,&b,&a);
3689}
Mike Kleinb11ab572018-10-24 06:42:14 -04003690STAGE_PP(load_4444_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003691 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &dr,&dg,&db,&da);
3692}
Mike Kleinb11ab572018-10-24 06:42:14 -04003693STAGE_PP(store_4444, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003694 store_4444_(ptr_at_xy<uint16_t>(ctx, dx,dy), tail, r,g,b,a);
3695}
Mike Kleinb11ab572018-10-24 06:42:14 -04003696STAGE_GP(gather_4444, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003697 const uint16_t* ptr;
3698 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3699 from_4444(gather<U16>(ptr, ix), &r,&g,&b,&a);
3700}
3701
Brian Salomon217522c2019-06-11 15:55:30 -04003702SI void from_88(U16 rg, U16* r, U16* g) {
3703 *r = (rg & 0xFF);
3704 *g = (rg >> 8);
3705}
3706
3707SI void load_88_(const uint16_t* ptr, size_t tail, U16* r, U16* g) {
3708#if 1 && defined(JUMPER_IS_NEON)
3709 uint8x8x2_t rg;
3710 switch (tail & (N-1)) {
3711 case 0: rg = vld2_u8 ((const uint8_t*)(ptr+0) ); break;
3712 case 7: rg = vld2_lane_u8((const uint8_t*)(ptr+6), rg, 6);
3713 case 6: rg = vld2_lane_u8((const uint8_t*)(ptr+5), rg, 5);
3714 case 5: rg = vld2_lane_u8((const uint8_t*)(ptr+4), rg, 4);
3715 case 4: rg = vld2_lane_u8((const uint8_t*)(ptr+3), rg, 3);
3716 case 3: rg = vld2_lane_u8((const uint8_t*)(ptr+2), rg, 2);
3717 case 2: rg = vld2_lane_u8((const uint8_t*)(ptr+1), rg, 1);
3718 case 1: rg = vld2_lane_u8((const uint8_t*)(ptr+0), rg, 0);
3719 }
3720 *r = cast<U16>(rg.val[0]);
3721 *g = cast<U16>(rg.val[1]);
3722#else
3723 from_88(load<U16>(ptr, tail), r,g);
3724#endif
3725}
3726
3727SI void store_88_(uint16_t* ptr, size_t tail, U16 r, U16 g) {
3728#if 1 && defined(JUMPER_IS_NEON)
3729 uint8x8x2_t rg = {{
3730 cast<U8>(r),
3731 cast<U8>(g),
3732 }};
3733 switch (tail & (N-1)) {
3734 case 0: vst2_u8 ((uint8_t*)(ptr+0), rg ); break;
3735 case 7: vst2_lane_u8((uint8_t*)(ptr+6), rg, 6);
3736 case 6: vst2_lane_u8((uint8_t*)(ptr+5), rg, 5);
3737 case 5: vst2_lane_u8((uint8_t*)(ptr+4), rg, 4);
3738 case 4: vst2_lane_u8((uint8_t*)(ptr+3), rg, 3);
3739 case 3: vst2_lane_u8((uint8_t*)(ptr+2), rg, 2);
3740 case 2: vst2_lane_u8((uint8_t*)(ptr+1), rg, 1);
3741 case 1: vst2_lane_u8((uint8_t*)(ptr+0), rg, 0);
3742 }
3743#else
3744 store(ptr, tail, cast<U16>(r | (g<<8)) << 0);
3745#endif
3746}
3747
3748STAGE_PP(load_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
Robert Phillipsd470e1b2019-09-04 15:05:35 -04003749 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), tail, &r, &g);
Brian Salomon217522c2019-06-11 15:55:30 -04003750 b = 0;
Brian Salomonf30b1c12019-06-20 12:25:02 -04003751 a = 255;
Robert Phillipsd470e1b2019-09-04 15:05:35 -04003752}
3753STAGE_PP(load_rg88_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3754 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), tail, &dr, &dg);
3755 db = 0;
3756 da = 255;
Brian Salomon217522c2019-06-11 15:55:30 -04003757}
3758STAGE_PP(store_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
3759 store_88_(ptr_at_xy<uint16_t>(ctx, dx, dy), tail, r, g);
3760}
Robert Phillipsd470e1b2019-09-04 15:05:35 -04003761STAGE_GP(gather_rg88, const SkRasterPipeline_GatherCtx* ctx) {
3762 const uint16_t* ptr;
3763 U32 ix = ix_and_ptr(&ptr, ctx, x, y);
3764 from_88(gather<U16>(ptr, ix), &r, &g);
3765 b = 0;
3766 a = 255;
3767}
Brian Salomon217522c2019-06-11 15:55:30 -04003768
Mike Klein1b9b7d52018-02-27 10:37:40 -05003769// ~~~~~~ 8-bit memory loads and stores ~~~~~~ //
3770
3771SI U16 load_8(const uint8_t* ptr, size_t tail) {
3772 return cast<U16>(load<U8>(ptr, tail));
3773}
3774SI void store_8(uint8_t* ptr, size_t tail, U16 v) {
3775 store(ptr, tail, cast<U8>(v));
3776}
3777
Mike Kleinb11ab572018-10-24 06:42:14 -04003778STAGE_PP(load_a8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003779 r = g = b = 0;
3780 a = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3781}
Mike Kleinb11ab572018-10-24 06:42:14 -04003782STAGE_PP(load_a8_dst, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003783 dr = dg = db = 0;
3784 da = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3785}
Mike Kleinb11ab572018-10-24 06:42:14 -04003786STAGE_PP(store_a8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003787 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), tail, a);
3788}
Mike Kleinb11ab572018-10-24 06:42:14 -04003789STAGE_GP(gather_a8, const SkRasterPipeline_GatherCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003790 const uint8_t* ptr;
3791 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3792 r = g = b = 0;
3793 a = cast<U16>(gather<U8>(ptr, ix));
3794}
3795
Mike Kleinb1df5e52018-10-17 17:06:03 -04003796STAGE_PP(alpha_to_gray, Ctx::None) {
3797 r = g = b = a;
Mike Klein1b9b7d52018-02-27 10:37:40 -05003798 a = 255;
3799}
Mike Kleinb1df5e52018-10-17 17:06:03 -04003800STAGE_PP(alpha_to_gray_dst, Ctx::None) {
3801 dr = dg = db = da;
Mike Klein1b9b7d52018-02-27 10:37:40 -05003802 da = 255;
3803}
Mike Kleinda69d592019-07-11 07:38:31 -05003804STAGE_PP(bt709_luminance_or_luma_to_alpha, Ctx::None) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003805 a = (r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
3806 r = g = b = 0;
3807}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003808
3809// ~~~~~~ Coverage scales / lerps ~~~~~~ //
3810
Mike Reed895e1ee2019-03-16 13:16:54 -04003811STAGE_PP(load_src, const uint16_t* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05003812 r = sk_unaligned_load<U16>(ptr + 0*N);
3813 g = sk_unaligned_load<U16>(ptr + 1*N);
3814 b = sk_unaligned_load<U16>(ptr + 2*N);
3815 a = sk_unaligned_load<U16>(ptr + 3*N);
Mike Reed895e1ee2019-03-16 13:16:54 -04003816}
3817STAGE_PP(store_src, uint16_t* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05003818 sk_unaligned_store(ptr + 0*N, r);
3819 sk_unaligned_store(ptr + 1*N, g);
3820 sk_unaligned_store(ptr + 2*N, b);
3821 sk_unaligned_store(ptr + 3*N, a);
Mike Reed895e1ee2019-03-16 13:16:54 -04003822}
3823STAGE_PP(load_dst, const uint16_t* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05003824 dr = sk_unaligned_load<U16>(ptr + 0*N);
3825 dg = sk_unaligned_load<U16>(ptr + 1*N);
3826 db = sk_unaligned_load<U16>(ptr + 2*N);
3827 da = sk_unaligned_load<U16>(ptr + 3*N);
Mike Reed895e1ee2019-03-16 13:16:54 -04003828}
3829STAGE_PP(store_dst, uint16_t* ptr) {
Mike Klein7a177b42019-06-17 17:17:47 -05003830 sk_unaligned_store(ptr + 0*N, dr);
3831 sk_unaligned_store(ptr + 1*N, dg);
3832 sk_unaligned_store(ptr + 2*N, db);
3833 sk_unaligned_store(ptr + 3*N, da);
Mike Reed895e1ee2019-03-16 13:16:54 -04003834}
3835
3836// ~~~~~~ Coverage scales / lerps ~~~~~~ //
3837
Mike Klein1b9b7d52018-02-27 10:37:40 -05003838STAGE_PP(scale_1_float, const float* f) {
3839 U16 c = from_float(*f);
3840 r = div255( r * c );
3841 g = div255( g * c );
3842 b = div255( b * c );
3843 a = div255( a * c );
3844}
3845STAGE_PP(lerp_1_float, const float* f) {
3846 U16 c = from_float(*f);
3847 r = lerp(dr, r, c);
3848 g = lerp(dg, g, c);
3849 b = lerp(db, b, c);
3850 a = lerp(da, a, c);
3851}
Mike Reed895e1ee2019-03-16 13:16:54 -04003852STAGE_PP(lerp_native, const uint16_t scales[]) {
Mike Klein7a177b42019-06-17 17:17:47 -05003853 auto c = sk_unaligned_load<U16>(scales);
Mike Reed895e1ee2019-03-16 13:16:54 -04003854 r = lerp(dr, r, c);
3855 g = lerp(dg, g, c);
3856 b = lerp(db, b, c);
3857 a = lerp(da, a, c);
3858}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003859
Mike Kleinb11ab572018-10-24 06:42:14 -04003860STAGE_PP(scale_u8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003861 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3862 r = div255( r * c );
3863 g = div255( g * c );
3864 b = div255( b * c );
3865 a = div255( a * c );
3866}
Mike Kleinb11ab572018-10-24 06:42:14 -04003867STAGE_PP(lerp_u8, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003868 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3869 r = lerp(dr, r, c);
3870 g = lerp(dg, g, c);
3871 b = lerp(db, b, c);
3872 a = lerp(da, a, c);
3873}
3874
3875// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
3876SI U16 alpha_coverage_from_rgb_coverage(U16 a, U16 da, U16 cr, U16 cg, U16 cb) {
Mike Klein5d835d02019-10-16 13:28:55 -05003877 return if_then_else(a < da, min(cr, min(cg,cb))
3878 , max(cr, max(cg,cb)));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003879}
Mike Kleinb11ab572018-10-24 06:42:14 -04003880STAGE_PP(scale_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003881 U16 cr,cg,cb;
3882 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &cr,&cg,&cb);
3883 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
3884
3885 r = div255( r * cr );
3886 g = div255( g * cg );
3887 b = div255( b * cb );
3888 a = div255( a * ca );
3889}
Mike Kleinb11ab572018-10-24 06:42:14 -04003890STAGE_PP(lerp_565, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003891 U16 cr,cg,cb;
3892 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &cr,&cg,&cb);
3893 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
3894
3895 r = lerp(dr, r, cr);
3896 g = lerp(dg, g, cg);
3897 b = lerp(db, b, cb);
3898 a = lerp(da, a, ca);
3899}
3900
Mike Kleineda2ac22018-11-06 11:53:59 -05003901STAGE_PP(emboss, const SkRasterPipeline_EmbossCtx* ctx) {
3902 U16 mul = load_8(ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy), tail),
3903 add = load_8(ptr_at_xy<const uint8_t>(&ctx->add, dx,dy), tail);
3904
3905 r = min(div255(r*mul) + add, a);
3906 g = min(div255(g*mul) + add, a);
3907 b = min(div255(b*mul) + add, a);
3908}
3909
3910
Mike Klein1b9b7d52018-02-27 10:37:40 -05003911// ~~~~~~ Gradient stages ~~~~~~ //
3912
3913// Clamp x to [0,1], both sides inclusive (think, gradients).
3914// Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
3915SI F clamp_01(F v) { return min(max(0, v), 1); }
3916
3917STAGE_GG(clamp_x_1 , Ctx::None) { x = clamp_01(x); }
3918STAGE_GG(repeat_x_1, Ctx::None) { x = clamp_01(x - floor_(x)); }
3919STAGE_GG(mirror_x_1, Ctx::None) {
3920 auto two = [](F x){ return x+x; };
3921 x = clamp_01(abs_( (x-1.0f) - two(floor_((x-1.0f)*0.5f)) - 1.0f ));
3922}
3923
3924SI I16 cond_to_mask_16(I32 cond) { return cast<I16>(cond); }
3925
Mike Kleinb11ab572018-10-24 06:42:14 -04003926STAGE_GG(decal_x, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003927 auto w = ctx->limit_x;
Mike Klein7a177b42019-06-17 17:17:47 -05003928 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w)));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003929}
Mike Kleinb11ab572018-10-24 06:42:14 -04003930STAGE_GG(decal_y, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003931 auto h = ctx->limit_y;
Mike Klein7a177b42019-06-17 17:17:47 -05003932 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= y) & (y < h)));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003933}
Mike Kleinb11ab572018-10-24 06:42:14 -04003934STAGE_GG(decal_x_and_y, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05003935 auto w = ctx->limit_x;
3936 auto h = ctx->limit_y;
Mike Klein7a177b42019-06-17 17:17:47 -05003937 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w) & (0 <= y) & (y < h)));
Mike Klein1b9b7d52018-02-27 10:37:40 -05003938}
Mike Kleinb11ab572018-10-24 06:42:14 -04003939STAGE_PP(check_decal_mask, SkRasterPipeline_DecalTileCtx* ctx) {
Mike Klein7a177b42019-06-17 17:17:47 -05003940 auto mask = sk_unaligned_load<U16>(ctx->mask);
Mike Klein1b9b7d52018-02-27 10:37:40 -05003941 r = r & mask;
3942 g = g & mask;
3943 b = b & mask;
3944 a = a & mask;
3945}
3946
Mike Klein24de6482018-09-07 12:05:29 -04003947SI void round_F_to_U16(F R, F G, F B, F A, bool interpolatedInPremul,
3948 U16* r, U16* g, U16* b, U16* a) {
3949 auto round = [](F x) { return cast<U16>(x * 255.0f + 0.5f); };
Mike Klein1b9b7d52018-02-27 10:37:40 -05003950
Mike Klein24de6482018-09-07 12:05:29 -04003951 F limit = interpolatedInPremul ? A
3952 : 1;
3953 *r = round(min(max(0,R), limit));
3954 *g = round(min(max(0,G), limit));
3955 *b = round(min(max(0,B), limit));
3956 *a = round(A); // we assume alpha is already in [0,1].
3957}
Mike Klein1b9b7d52018-02-27 10:37:40 -05003958
Mike Kleinb11ab572018-10-24 06:42:14 -04003959SI void gradient_lookup(const SkRasterPipeline_GradientCtx* c, U32 idx, F t,
Mike Klein1b9b7d52018-02-27 10:37:40 -05003960 U16* r, U16* g, U16* b, U16* a) {
3961
3962 F fr, fg, fb, fa, br, bg, bb, ba;
Mike Klein83e86eb2018-08-31 10:19:21 -04003963#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Klein1b9b7d52018-02-27 10:37:40 -05003964 if (c->stopCount <=8) {
3965 __m256i lo, hi;
3966 split(idx, &lo, &hi);
3967
3968 fr = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), lo),
3969 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), hi));
3970 br = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), lo),
3971 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), hi));
3972 fg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), lo),
3973 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), hi));
3974 bg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), lo),
3975 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), hi));
3976 fb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), lo),
3977 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), hi));
3978 bb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), lo),
3979 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), hi));
3980 fa = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), lo),
3981 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), hi));
3982 ba = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), lo),
3983 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), hi));
3984 } else
3985#endif
3986 {
3987 fr = gather<F>(c->fs[0], idx);
3988 fg = gather<F>(c->fs[1], idx);
3989 fb = gather<F>(c->fs[2], idx);
3990 fa = gather<F>(c->fs[3], idx);
3991 br = gather<F>(c->bs[0], idx);
3992 bg = gather<F>(c->bs[1], idx);
3993 bb = gather<F>(c->bs[2], idx);
3994 ba = gather<F>(c->bs[3], idx);
3995 }
Mike Klein24de6482018-09-07 12:05:29 -04003996 round_F_to_U16(mad(t, fr, br),
3997 mad(t, fg, bg),
3998 mad(t, fb, bb),
3999 mad(t, fa, ba),
4000 c->interpolatedInPremul,
4001 r,g,b,a);
Mike Klein1b9b7d52018-02-27 10:37:40 -05004002}
4003
Mike Kleinb11ab572018-10-24 06:42:14 -04004004STAGE_GP(gradient, const SkRasterPipeline_GradientCtx* c) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05004005 auto t = x;
4006 U32 idx = 0;
4007
4008 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
4009 for (size_t i = 1; i < c->stopCount; i++) {
4010 idx += if_then_else(t >= c->ts[i], U32(1), U32(0));
4011 }
4012
4013 gradient_lookup(c, idx, t, &r, &g, &b, &a);
4014}
4015
Mike Kleinb11ab572018-10-24 06:42:14 -04004016STAGE_GP(evenly_spaced_gradient, const SkRasterPipeline_GradientCtx* c) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05004017 auto t = x;
4018 auto idx = trunc_(t * (c->stopCount-1));
4019 gradient_lookup(c, idx, t, &r, &g, &b, &a);
4020}
4021
Mike Kleinb11ab572018-10-24 06:42:14 -04004022STAGE_GP(evenly_spaced_2_stop_gradient, const SkRasterPipeline_EvenlySpaced2StopGradientCtx* c) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05004023 auto t = x;
Mike Klein24de6482018-09-07 12:05:29 -04004024 round_F_to_U16(mad(t, c->f[0], c->b[0]),
4025 mad(t, c->f[1], c->b[1]),
4026 mad(t, c->f[2], c->b[2]),
4027 mad(t, c->f[3], c->b[3]),
4028 c->interpolatedInPremul,
4029 &r,&g,&b,&a);
Mike Klein1b9b7d52018-02-27 10:37:40 -05004030}
4031
4032STAGE_GG(xy_to_unit_angle, Ctx::None) {
4033 F xabs = abs_(x),
4034 yabs = abs_(y);
4035
4036 F slope = min(xabs, yabs)/max(xabs, yabs);
4037 F s = slope * slope;
4038
4039 // Use a 7th degree polynomial to approximate atan.
4040 // This was generated using sollya.gforge.inria.fr.
4041 // A float optimized polynomial was generated using the following command.
4042 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
4043 F phi = slope
4044 * (0.15912117063999176025390625f + s
4045 * (-5.185396969318389892578125e-2f + s
4046 * (2.476101927459239959716796875e-2f + s
4047 * (-7.0547382347285747528076171875e-3f))));
4048
4049 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
4050 phi = if_then_else(x < 0.0f , 1.0f/2.0f - phi, phi);
4051 phi = if_then_else(y < 0.0f , 1.0f - phi , phi);
4052 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
4053 x = phi;
4054}
4055STAGE_GG(xy_to_radius, Ctx::None) {
4056 x = sqrt_(x*x + y*y);
4057}
4058
4059// ~~~~~~ Compound stages ~~~~~~ //
4060
Mike Kleinb11ab572018-10-24 06:42:14 -04004061STAGE_PP(srcover_rgba_8888, const SkRasterPipeline_MemoryCtx* ctx) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05004062 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
4063
4064 load_8888_(ptr, tail, &dr,&dg,&db,&da);
4065 r = r + div255( dr*inv(a) );
4066 g = g + div255( dg*inv(a) );
4067 b = b + div255( db*inv(a) );
4068 a = a + div255( da*inv(a) );
4069 store_8888_(ptr, tail, r,g,b,a);
4070}
Mike Klein05bf9312018-12-19 10:05:03 -05004071
Mike Reedcf27e742019-03-03 22:12:16 +00004072#if defined(SK_DISABLE_LOWP_BILERP_CLAMP_CLAMP_STAGE)
4073 static void(*bilerp_clamp_8888)(void) = nullptr;
Mike Klein01005622019-08-13 12:22:17 -04004074 static void(*bilinear)(void) = nullptr;
Mike Reedcf27e742019-03-03 22:12:16 +00004075#else
4076STAGE_GP(bilerp_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
4077 // (cx,cy) are the center of our sample.
4078 F cx = x,
4079 cy = y;
4080
4081 // All sample points are at the same fractional offset (fx,fy).
4082 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
4083 F fx = fract(cx + 0.5f),
4084 fy = fract(cy + 0.5f);
4085
4086 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
4087 r = g = b = a = 0;
4088
4089 // The first three sample points will calculate their area using math
4090 // just like in the float code above, but the fourth will take up all the rest.
4091 //
4092 // Logically this is the same as doing the math for the fourth pixel too,
4093 // but rounding error makes this a better strategy, keeping opaque opaque, etc.
4094 //
4095 // We can keep up to 8 bits of fractional precision without overflowing 16-bit,
4096 // so our "1.0" area is 256.
4097 const uint16_t bias = 256;
4098 U16 remaining = bias;
4099
4100 for (float dy = -0.5f; dy <= +0.5f; dy += 1.0f)
4101 for (float dx = -0.5f; dx <= +0.5f; dx += 1.0f) {
4102 // (x,y) are the coordinates of this sample point.
4103 F x = cx + dx,
4104 y = cy + dy;
4105
4106 // ix_and_ptr() will clamp to the image's bounds for us.
4107 const uint32_t* ptr;
4108 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
4109
4110 U16 sr,sg,sb,sa;
4111 from_8888(gather<U32>(ptr, ix), &sr,&sg,&sb,&sa);
4112
4113 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
4114 // are combined in direct proportion to their area overlapping that logical query pixel.
4115 // At positive offsets, the x-axis contribution to that rectangle is fx,
4116 // or (1-fx) at negative x. Same deal for y.
4117 F sx = (dx > 0) ? fx : 1.0f - fx,
4118 sy = (dy > 0) ? fy : 1.0f - fy;
4119
4120 U16 area = (dy == 0.5f && dx == 0.5f) ? remaining
4121 : cast<U16>(sx * sy * bias);
4122 for (size_t i = 0; i < N; i++) {
4123 SkASSERT(remaining[i] >= area[i]);
4124 }
4125 remaining -= area;
4126
4127 r += sr * area;
4128 g += sg * area;
4129 b += sb * area;
4130 a += sa * area;
4131 }
4132
4133 r = (r + bias/2) / bias;
4134 g = (g + bias/2) / bias;
4135 b = (b + bias/2) / bias;
4136 a = (a + bias/2) / bias;
4137}
Mike Klein01005622019-08-13 12:22:17 -04004138
4139// TODO: lowp::tile() is identical to the highp tile()... share?
4140SI F tile(F v, SkTileMode mode, float limit, float invLimit) {
4141 // After ix_and_ptr() will clamp the output of tile(), so we need not clamp here.
4142 switch (mode) {
4143 case SkTileMode::kDecal: // TODO, for now fallthrough to clamp
4144 case SkTileMode::kClamp: return v;
4145 case SkTileMode::kRepeat: return v - floor_(v*invLimit)*limit;
4146 case SkTileMode::kMirror:
4147 return abs_( (v-limit) - (limit+limit)*floor_((v-limit)*(invLimit*0.5f)) - limit );
4148 }
4149 SkUNREACHABLE;
4150}
4151
4152SI void sample(const SkRasterPipeline_SamplerCtx2* ctx, F x, F y,
4153 U16* r, U16* g, U16* b, U16* a) {
4154 x = tile(x, ctx->tileX, ctx->width , ctx->invWidth );
4155 y = tile(y, ctx->tileY, ctx->height, ctx->invHeight);
4156
4157 switch (ctx->ct) {
4158 default: *r = *g = *b = *a = 0; // TODO
4159 break;
4160
4161 case kRGBA_8888_SkColorType:
4162 case kBGRA_8888_SkColorType: {
4163 const uint32_t* ptr;
4164 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
4165 from_8888(gather<U32>(ptr, ix), r,g,b,a);
4166 if (ctx->ct == kBGRA_8888_SkColorType) {
4167 std::swap(*r,*b);
4168 }
4169 } break;
4170 }
4171}
4172
4173template <int D>
4174SI void sampler(const SkRasterPipeline_SamplerCtx2* ctx,
4175 F cx, F cy, const F (&wx)[D], const F (&wy)[D],
4176 U16* r, U16* g, U16* b, U16* a) {
4177
4178 float start = -0.5f*(D-1);
4179
4180 const uint16_t bias = 256;
4181 U16 remaining = bias;
4182
4183 *r = *g = *b = *a = 0;
4184 F y = cy + start;
4185 for (int j = 0; j < D; j++, y += 1.0f) {
4186 F x = cx + start;
4187 for (int i = 0; i < D; i++, x += 1.0f) {
4188 U16 R,G,B,A;
4189 sample(ctx, x,y, &R,&G,&B,&A);
4190
4191 U16 w = (i == D-1 && j == D-1) ? remaining
4192 : cast<U16>(wx[i]*wy[j]*bias);
4193 remaining -= w;
4194 *r += w*R;
4195 *g += w*G;
4196 *b += w*B;
4197 *a += w*A;
4198 }
4199 }
4200 *r = (*r + bias/2) / bias;
4201 *g = (*g + bias/2) / bias;
4202 *b = (*b + bias/2) / bias;
4203 *a = (*a + bias/2) / bias;
4204}
4205
4206STAGE_GP(bilinear, const SkRasterPipeline_SamplerCtx2* ctx) {
4207 F fx = fract(x + 0.5f),
4208 fy = fract(y + 0.5f);
4209 const F wx[] = {1.0f - fx, fx};
4210 const F wy[] = {1.0f - fy, fy};
4211
4212 sampler(ctx, x,y, wx,wy, &r,&g,&b,&a);
4213}
Mike Reedcf27e742019-03-03 22:12:16 +00004214#endif
4215
Brian Salomon217522c2019-06-11 15:55:30 -04004216// ~~~~~~ GrSwizzle stage ~~~~~~ //
4217
4218STAGE_PP(swizzle, void* ctx) {
4219 auto ir = r, ig = g, ib = b, ia = a;
4220 U16* o[] = {&r, &g, &b, &a};
4221 char swiz[4];
4222 memcpy(swiz, &ctx, sizeof(swiz));
4223
4224 for (int i = 0; i < 4; ++i) {
4225 switch (swiz[i]) {
4226 case 'r': *o[i] = ir; break;
4227 case 'g': *o[i] = ig; break;
4228 case 'b': *o[i] = ib; break;
4229 case 'a': *o[i] = ia; break;
Brian Salomonf30b1c12019-06-20 12:25:02 -04004230 case '0': *o[i] = U16(0); break;
Brian Salomon217522c2019-06-11 15:55:30 -04004231 case '1': *o[i] = U16(255); break;
4232 default: break;
4233 }
4234 }
4235}
4236
Mike Klein1b9b7d52018-02-27 10:37:40 -05004237// Now we'll add null stand-ins for stages we haven't implemented in lowp.
4238// If a pipeline uses these stages, it'll boot it out of lowp into highp.
Mike Klein8b0f9d12019-01-03 11:26:57 -05004239#define NOT_IMPLEMENTED(st) static void (*st)(void) = nullptr;
Mike Klein05bf9312018-12-19 10:05:03 -05004240 NOT_IMPLEMENTED(callback)
Brian Osman2b1a5442019-06-19 11:40:33 -04004241 NOT_IMPLEMENTED(interpreter)
Mike Klein05bf9312018-12-19 10:05:03 -05004242 NOT_IMPLEMENTED(unbounded_set_rgb)
4243 NOT_IMPLEMENTED(unbounded_uniform_color)
4244 NOT_IMPLEMENTED(unpremul)
Mike Klein5ece3632019-03-01 11:31:28 -06004245 NOT_IMPLEMENTED(dither) // TODO
Mike Klein05bf9312018-12-19 10:05:03 -05004246 NOT_IMPLEMENTED(from_srgb)
4247 NOT_IMPLEMENTED(to_srgb)
Brian Salomond608e222019-06-12 17:42:58 -04004248 NOT_IMPLEMENTED(load_16161616)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004249 NOT_IMPLEMENTED(load_16161616_dst)
Brian Salomond608e222019-06-12 17:42:58 -04004250 NOT_IMPLEMENTED(store_16161616)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004251 NOT_IMPLEMENTED(gather_16161616)
Brian Salomon217522c2019-06-11 15:55:30 -04004252 NOT_IMPLEMENTED(load_a16)
Robert Phillips429f0d32019-09-11 17:03:28 -04004253 NOT_IMPLEMENTED(load_a16_dst)
Brian Salomon217522c2019-06-11 15:55:30 -04004254 NOT_IMPLEMENTED(store_a16)
Robert Phillips429f0d32019-09-11 17:03:28 -04004255 NOT_IMPLEMENTED(gather_a16)
Brian Salomon217522c2019-06-11 15:55:30 -04004256 NOT_IMPLEMENTED(load_rg1616)
Robert Phillips429f0d32019-09-11 17:03:28 -04004257 NOT_IMPLEMENTED(load_rg1616_dst)
Brian Salomon217522c2019-06-11 15:55:30 -04004258 NOT_IMPLEMENTED(store_rg1616)
Robert Phillips429f0d32019-09-11 17:03:28 -04004259 NOT_IMPLEMENTED(gather_rg1616)
Mike Klein05bf9312018-12-19 10:05:03 -05004260 NOT_IMPLEMENTED(load_f16)
4261 NOT_IMPLEMENTED(load_f16_dst)
4262 NOT_IMPLEMENTED(store_f16)
4263 NOT_IMPLEMENTED(gather_f16)
Brian Salomon217522c2019-06-11 15:55:30 -04004264 NOT_IMPLEMENTED(load_af16)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004265 NOT_IMPLEMENTED(load_af16_dst)
Brian Salomon217522c2019-06-11 15:55:30 -04004266 NOT_IMPLEMENTED(store_af16)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004267 NOT_IMPLEMENTED(gather_af16)
Brian Salomon217522c2019-06-11 15:55:30 -04004268 NOT_IMPLEMENTED(load_rgf16)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004269 NOT_IMPLEMENTED(load_rgf16_dst)
Brian Salomon217522c2019-06-11 15:55:30 -04004270 NOT_IMPLEMENTED(store_rgf16)
Robert Phillips17a3a0b2019-09-18 13:56:54 -04004271 NOT_IMPLEMENTED(gather_rgf16)
Mike Klein05bf9312018-12-19 10:05:03 -05004272 NOT_IMPLEMENTED(load_f32)
4273 NOT_IMPLEMENTED(load_f32_dst)
4274 NOT_IMPLEMENTED(store_f32)
4275 NOT_IMPLEMENTED(gather_f32)
Brian Salomon217522c2019-06-11 15:55:30 -04004276 NOT_IMPLEMENTED(load_rgf32)
4277 NOT_IMPLEMENTED(store_rgf32)
Mike Klein05bf9312018-12-19 10:05:03 -05004278 NOT_IMPLEMENTED(load_1010102)
4279 NOT_IMPLEMENTED(load_1010102_dst)
4280 NOT_IMPLEMENTED(store_1010102)
4281 NOT_IMPLEMENTED(gather_1010102)
4282 NOT_IMPLEMENTED(store_u16_be)
Mike Klein5ece3632019-03-01 11:31:28 -06004283 NOT_IMPLEMENTED(byte_tables) // TODO
Mike Klein05bf9312018-12-19 10:05:03 -05004284 NOT_IMPLEMENTED(colorburn)
4285 NOT_IMPLEMENTED(colordodge)
4286 NOT_IMPLEMENTED(softlight)
4287 NOT_IMPLEMENTED(hue)
4288 NOT_IMPLEMENTED(saturation)
4289 NOT_IMPLEMENTED(color)
4290 NOT_IMPLEMENTED(luminosity)
4291 NOT_IMPLEMENTED(matrix_3x3)
4292 NOT_IMPLEMENTED(matrix_3x4)
Mike Klein5ece3632019-03-01 11:31:28 -06004293 NOT_IMPLEMENTED(matrix_4x5) // TODO
4294 NOT_IMPLEMENTED(matrix_4x3) // TODO
Mike Klein05bf9312018-12-19 10:05:03 -05004295 NOT_IMPLEMENTED(parametric)
Mike Klein1ce03a62019-04-23 08:00:35 -05004296 NOT_IMPLEMENTED(gamma_)
Brian Osman11e6aa82019-10-16 13:58:42 -04004297 NOT_IMPLEMENTED(PQish)
4298 NOT_IMPLEMENTED(HLGish)
4299 NOT_IMPLEMENTED(HLGinvish)
Mike Klein05bf9312018-12-19 10:05:03 -05004300 NOT_IMPLEMENTED(rgb_to_hsl)
4301 NOT_IMPLEMENTED(hsl_to_rgb)
Mike Klein5ece3632019-03-01 11:31:28 -06004302 NOT_IMPLEMENTED(gauss_a_to_rgba) // TODO
4303 NOT_IMPLEMENTED(mirror_x) // TODO
4304 NOT_IMPLEMENTED(repeat_x) // TODO
4305 NOT_IMPLEMENTED(mirror_y) // TODO
4306 NOT_IMPLEMENTED(repeat_y) // TODO
Mike Klein05bf9312018-12-19 10:05:03 -05004307 NOT_IMPLEMENTED(negate_x)
Mike Klein01005622019-08-13 12:22:17 -04004308 NOT_IMPLEMENTED(bicubic) // TODO if I can figure out negative weights
Mike Reed78eedba2019-07-31 16:39:15 -04004309 NOT_IMPLEMENTED(bicubic_clamp_8888)
Mike Klein5ece3632019-03-01 11:31:28 -06004310 NOT_IMPLEMENTED(bilinear_nx) // TODO
4311 NOT_IMPLEMENTED(bilinear_ny) // TODO
4312 NOT_IMPLEMENTED(bilinear_px) // TODO
4313 NOT_IMPLEMENTED(bilinear_py) // TODO
4314 NOT_IMPLEMENTED(bicubic_n3x) // TODO
4315 NOT_IMPLEMENTED(bicubic_n1x) // TODO
4316 NOT_IMPLEMENTED(bicubic_p1x) // TODO
4317 NOT_IMPLEMENTED(bicubic_p3x) // TODO
4318 NOT_IMPLEMENTED(bicubic_n3y) // TODO
4319 NOT_IMPLEMENTED(bicubic_n1y) // TODO
4320 NOT_IMPLEMENTED(bicubic_p1y) // TODO
4321 NOT_IMPLEMENTED(bicubic_p3y) // TODO
4322 NOT_IMPLEMENTED(save_xy) // TODO
4323 NOT_IMPLEMENTED(accumulate) // TODO
Mike Klein05bf9312018-12-19 10:05:03 -05004324 NOT_IMPLEMENTED(xy_to_2pt_conical_well_behaved)
4325 NOT_IMPLEMENTED(xy_to_2pt_conical_strip)
4326 NOT_IMPLEMENTED(xy_to_2pt_conical_focal_on_circle)
4327 NOT_IMPLEMENTED(xy_to_2pt_conical_smaller)
4328 NOT_IMPLEMENTED(xy_to_2pt_conical_greater)
4329 NOT_IMPLEMENTED(alter_2pt_conical_compensate_focal)
4330 NOT_IMPLEMENTED(alter_2pt_conical_unswap)
4331 NOT_IMPLEMENTED(mask_2pt_conical_nan)
4332 NOT_IMPLEMENTED(mask_2pt_conical_degenerates)
4333 NOT_IMPLEMENTED(apply_vector_mask)
4334#undef NOT_IMPLEMENTED
Mike Klein1b9b7d52018-02-27 10:37:40 -05004335
Mike Klein1b9b7d52018-02-27 10:37:40 -05004336#endif//defined(JUMPER_IS_SCALAR) controlling whether we build lowp stages
4337} // namespace lowp
4338
4339} // namespace SK_OPTS_NS
4340
4341#endif//SkRasterPipeline_opts_DEFINED