blob: edc50732f3b0e506db371a11597a8f40f3765bce [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 Klein1b9b7d52018-02-27 10:37:40 -050011#include "../jumper/SkJumper.h"
12#include "../jumper/SkJumper_misc.h"
Mike Kleinadc78d52018-01-01 09:06:37 -050013
14#if !defined(__clang__)
15 #define JUMPER_IS_SCALAR
16#elif defined(__ARM_NEON)
17 #define JUMPER_IS_NEON
18#elif defined(__AVX512F__)
19 #define JUMPER_IS_AVX512
20#elif defined(__AVX2__) && defined(__F16C__) && defined(__FMA__)
21 #define JUMPER_IS_HSW
22#elif defined(__AVX__)
23 #define JUMPER_IS_AVX
24#elif defined(__SSE4_1__)
25 #define JUMPER_IS_SSE41
26#elif defined(__SSE2__)
27 #define JUMPER_IS_SSE2
28#else
29 #define JUMPER_IS_SCALAR
30#endif
31
32// Older Clangs seem to crash when generating non-optimized NEON code for ARMv7.
33#if defined(__clang__) && !defined(__OPTIMIZE__) && defined(__arm__)
34 // Apple Clang 9 and vanilla Clang 5 are fine, and may even be conservative.
35 #if defined(__apple_build_version__) && __clang_major__ < 9
36 #define JUMPER_IS_SCALAR
37 #elif __clang_major__ < 5
38 #define JUMPER_IS_SCALAR
39 #endif
40#endif
41
42#if defined(JUMPER_IS_SCALAR)
Mike Klein5cc94cc2018-03-07 17:04:18 +000043 #include <math.h>
Mike Klein1b9b7d52018-02-27 10:37:40 -050044#elif defined(JUMPER_IS_NEON)
45 #include <arm_neon.h>
46#else
47 #include <immintrin.h>
48#endif
Mike Klein5cc94cc2018-03-07 17:04:18 +000049
Mike Klein1b9b7d52018-02-27 10:37:40 -050050namespace SK_OPTS_NS {
51
52#if defined(JUMPER_IS_SCALAR)
53 // This path should lead to portable scalar code.
Mike Kleinadc78d52018-01-01 09:06:37 -050054 using F = float ;
55 using I32 = int32_t;
56 using U64 = uint64_t;
57 using U32 = uint32_t;
58 using U16 = uint16_t;
59 using U8 = uint8_t ;
60
61 SI F mad(F f, F m, F a) { return f*m+a; }
62 SI F min(F a, F b) { return fminf(a,b); }
63 SI F max(F a, F b) { return fmaxf(a,b); }
64 SI F abs_ (F v) { return fabsf(v); }
65 SI F floor_(F v) { return floorf(v); }
66 SI F rcp (F v) { return 1.0f / v; }
67 SI F rsqrt (F v) { return 1.0f / sqrtf(v); }
68 SI F sqrt_(F v) { return sqrtf(v); }
69 SI U32 round (F v, F scale) { return (uint32_t)(v*scale + 0.5f); }
70 SI U16 pack(U32 v) { return (U16)v; }
71 SI U8 pack(U16 v) { return (U8)v; }
72
73 SI F if_then_else(I32 c, F t, F e) { return c ? t : e; }
74
75 template <typename T>
76 SI T gather(const T* p, U32 ix) { return p[ix]; }
77
78 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
79 *r = ptr[0];
80 *g = ptr[1];
81 *b = ptr[2];
82 }
83 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
84 *r = ptr[0];
85 *g = ptr[1];
86 *b = ptr[2];
87 *a = ptr[3];
88 }
89 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
90 ptr[0] = r;
91 ptr[1] = g;
92 ptr[2] = b;
93 ptr[3] = a;
94 }
95
96 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
97 *r = ptr[0];
98 *g = ptr[1];
99 *b = ptr[2];
100 *a = ptr[3];
101 }
102 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
103 ptr[0] = r;
104 ptr[1] = g;
105 ptr[2] = b;
106 ptr[3] = a;
107 }
108
109#elif defined(JUMPER_IS_NEON)
Mike Kleinadc78d52018-01-01 09:06:37 -0500110 // Since we know we're using Clang, we can use its vector extensions.
111 template <typename T> using V = T __attribute__((ext_vector_type(4)));
112 using F = V<float >;
113 using I32 = V< int32_t>;
114 using U64 = V<uint64_t>;
115 using U32 = V<uint32_t>;
116 using U16 = V<uint16_t>;
117 using U8 = V<uint8_t >;
118
119 // We polyfill a few routines that Clang doesn't build into ext_vector_types.
120 SI F min(F a, F b) { return vminq_f32(a,b); }
121 SI F max(F a, F b) { return vmaxq_f32(a,b); }
122 SI F abs_ (F v) { return vabsq_f32(v); }
123 SI F rcp (F v) { auto e = vrecpeq_f32 (v); return vrecpsq_f32 (v,e ) * e; }
124 SI F rsqrt (F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; }
125 SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); }
126 SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); }
127
128 SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); }
129
130 #if defined(__aarch64__)
131 SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); }
132 SI F floor_(F v) { return vrndmq_f32(v); }
133 SI F sqrt_(F v) { return vsqrtq_f32(v); }
134 SI U32 round(F v, F scale) { return vcvtnq_u32_f32(v*scale); }
135 #else
136 SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); }
137 SI F floor_(F v) {
138 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
139 return roundtrip - if_then_else(roundtrip > v, 1, 0);
140 }
141
142 SI F sqrt_(F v) {
143 auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v).
144 e *= vrsqrtsq_f32(v,e*e);
145 e *= vrsqrtsq_f32(v,e*e);
146 return v*e; // sqrt(v) == v*rsqrt(v).
147 }
148
149 SI U32 round(F v, F scale) {
150 return vcvtq_u32_f32(mad(v,scale,0.5f));
151 }
152 #endif
153
154
155 template <typename T>
156 SI V<T> gather(const T* p, U32 ix) {
157 return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
158 }
159
160 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
161 uint16x4x3_t rgb;
162 if (__builtin_expect(tail,0)) {
163 if ( true ) { rgb = vld3_lane_u16(ptr + 0, rgb, 0); }
164 if (tail > 1) { rgb = vld3_lane_u16(ptr + 3, rgb, 1); }
165 if (tail > 2) { rgb = vld3_lane_u16(ptr + 6, rgb, 2); }
166 } else {
167 rgb = vld3_u16(ptr);
168 }
169 *r = rgb.val[0];
170 *g = rgb.val[1];
171 *b = rgb.val[2];
172 }
173 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
174 uint16x4x4_t rgba;
175 if (__builtin_expect(tail,0)) {
176 if ( true ) { rgba = vld4_lane_u16(ptr + 0, rgba, 0); }
177 if (tail > 1) { rgba = vld4_lane_u16(ptr + 4, rgba, 1); }
178 if (tail > 2) { rgba = vld4_lane_u16(ptr + 8, rgba, 2); }
179 } else {
180 rgba = vld4_u16(ptr);
181 }
182 *r = rgba.val[0];
183 *g = rgba.val[1];
184 *b = rgba.val[2];
185 *a = rgba.val[3];
186 }
187 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
188 if (__builtin_expect(tail,0)) {
189 if ( true ) { vst4_lane_u16(ptr + 0, (uint16x4x4_t{{r,g,b,a}}), 0); }
190 if (tail > 1) { vst4_lane_u16(ptr + 4, (uint16x4x4_t{{r,g,b,a}}), 1); }
191 if (tail > 2) { vst4_lane_u16(ptr + 8, (uint16x4x4_t{{r,g,b,a}}), 2); }
192 } else {
193 vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}}));
194 }
195 }
196 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
197 float32x4x4_t rgba;
198 if (__builtin_expect(tail,0)) {
199 if ( true ) { rgba = vld4q_lane_f32(ptr + 0, rgba, 0); }
200 if (tail > 1) { rgba = vld4q_lane_f32(ptr + 4, rgba, 1); }
201 if (tail > 2) { rgba = vld4q_lane_f32(ptr + 8, rgba, 2); }
202 } else {
203 rgba = vld4q_f32(ptr);
204 }
205 *r = rgba.val[0];
206 *g = rgba.val[1];
207 *b = rgba.val[2];
208 *a = rgba.val[3];
209 }
210 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
211 if (__builtin_expect(tail,0)) {
212 if ( true ) { vst4q_lane_f32(ptr + 0, (float32x4x4_t{{r,g,b,a}}), 0); }
213 if (tail > 1) { vst4q_lane_f32(ptr + 4, (float32x4x4_t{{r,g,b,a}}), 1); }
214 if (tail > 2) { vst4q_lane_f32(ptr + 8, (float32x4x4_t{{r,g,b,a}}), 2); }
215 } else {
216 vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}}));
217 }
218 }
219
220#elif defined(JUMPER_IS_AVX) || defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Mike Kleinadc78d52018-01-01 09:06:37 -0500221 // These are __m256 and __m256i, but friendlier and strongly-typed.
222 template <typename T> using V = T __attribute__((ext_vector_type(8)));
223 using F = V<float >;
224 using I32 = V< int32_t>;
225 using U64 = V<uint64_t>;
226 using U32 = V<uint32_t>;
227 using U16 = V<uint16_t>;
228 using U8 = V<uint8_t >;
229
230 SI F mad(F f, F m, F a) {
231 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
232 return _mm256_fmadd_ps(f,m,a);
233 #else
234 return f*m+a;
235 #endif
236 }
237
238 SI F min(F a, F b) { return _mm256_min_ps(a,b); }
239 SI F max(F a, F b) { return _mm256_max_ps(a,b); }
240 SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); }
241 SI F floor_(F v) { return _mm256_floor_ps(v); }
242 SI F rcp (F v) { return _mm256_rcp_ps (v); }
243 SI F rsqrt (F v) { return _mm256_rsqrt_ps(v); }
244 SI F sqrt_(F v) { return _mm256_sqrt_ps (v); }
245 SI U32 round (F v, F scale) { return _mm256_cvtps_epi32(v*scale); }
246
247 SI U16 pack(U32 v) {
248 return _mm_packus_epi32(_mm256_extractf128_si256(v, 0),
249 _mm256_extractf128_si256(v, 1));
250 }
251 SI U8 pack(U16 v) {
252 auto r = _mm_packus_epi16(v,v);
253 return unaligned_load<U8>(&r);
254 }
255
256 SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e,t,c); }
257
258 template <typename T>
259 SI V<T> gather(const T* p, U32 ix) {
260 return { p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
261 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
262 }
263 #if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
264 SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps (p, ix, 4); }
265 SI U32 gather(const uint32_t* p, U32 ix) { return _mm256_i32gather_epi32(p, ix, 4); }
266 SI U64 gather(const uint64_t* p, U32 ix) {
267 __m256i parts[] = {
268 _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,0), 8),
269 _mm256_i32gather_epi64(p, _mm256_extracti128_si256(ix,1), 8),
270 };
271 return bit_cast<U64>(parts);
272 }
273 #endif
274
275 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
276 __m128i _0,_1,_2,_3,_4,_5,_6,_7;
277 if (__builtin_expect(tail,0)) {
278 auto load_rgb = [](const uint16_t* src) {
279 auto v = _mm_cvtsi32_si128(*(const uint32_t*)src);
280 return _mm_insert_epi16(v, src[2], 2);
281 };
282 _1 = _2 = _3 = _4 = _5 = _6 = _7 = _mm_setzero_si128();
283 if ( true ) { _0 = load_rgb(ptr + 0); }
284 if (tail > 1) { _1 = load_rgb(ptr + 3); }
285 if (tail > 2) { _2 = load_rgb(ptr + 6); }
286 if (tail > 3) { _3 = load_rgb(ptr + 9); }
287 if (tail > 4) { _4 = load_rgb(ptr + 12); }
288 if (tail > 5) { _5 = load_rgb(ptr + 15); }
289 if (tail > 6) { _6 = load_rgb(ptr + 18); }
290 } else {
291 // Load 0+1, 2+3, 4+5 normally, and 6+7 backed up 4 bytes so we don't run over.
292 auto _01 = _mm_loadu_si128((const __m128i*)(ptr + 0)) ;
293 auto _23 = _mm_loadu_si128((const __m128i*)(ptr + 6)) ;
294 auto _45 = _mm_loadu_si128((const __m128i*)(ptr + 12)) ;
295 auto _67 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 16)), 4);
296 _0 = _01; _1 = _mm_srli_si128(_01, 6);
297 _2 = _23; _3 = _mm_srli_si128(_23, 6);
298 _4 = _45; _5 = _mm_srli_si128(_45, 6);
299 _6 = _67; _7 = _mm_srli_si128(_67, 6);
300 }
301
302 auto _02 = _mm_unpacklo_epi16(_0, _2), // r0 r2 g0 g2 b0 b2 xx xx
303 _13 = _mm_unpacklo_epi16(_1, _3),
304 _46 = _mm_unpacklo_epi16(_4, _6),
305 _57 = _mm_unpacklo_epi16(_5, _7);
306
307 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
308 bx0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 xx xx xx xx
309 rg4567 = _mm_unpacklo_epi16(_46, _57),
310 bx4567 = _mm_unpackhi_epi16(_46, _57);
311
312 *r = _mm_unpacklo_epi64(rg0123, rg4567);
313 *g = _mm_unpackhi_epi64(rg0123, rg4567);
314 *b = _mm_unpacklo_epi64(bx0123, bx4567);
315 }
316 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
317 __m128i _01, _23, _45, _67;
318 if (__builtin_expect(tail,0)) {
319 auto src = (const double*)ptr;
320 _01 = _23 = _45 = _67 = _mm_setzero_si128();
321 if (tail > 0) { _01 = _mm_loadl_pd(_01, src+0); }
322 if (tail > 1) { _01 = _mm_loadh_pd(_01, src+1); }
323 if (tail > 2) { _23 = _mm_loadl_pd(_23, src+2); }
324 if (tail > 3) { _23 = _mm_loadh_pd(_23, src+3); }
325 if (tail > 4) { _45 = _mm_loadl_pd(_45, src+4); }
326 if (tail > 5) { _45 = _mm_loadh_pd(_45, src+5); }
327 if (tail > 6) { _67 = _mm_loadl_pd(_67, src+6); }
328 } else {
329 _01 = _mm_loadu_si128(((__m128i*)ptr) + 0);
330 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1);
331 _45 = _mm_loadu_si128(((__m128i*)ptr) + 2);
332 _67 = _mm_loadu_si128(((__m128i*)ptr) + 3);
333 }
334
335 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
336 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
337 _46 = _mm_unpacklo_epi16(_45, _67),
338 _57 = _mm_unpackhi_epi16(_45, _67);
339
340 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
341 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
342 rg4567 = _mm_unpacklo_epi16(_46, _57),
343 ba4567 = _mm_unpackhi_epi16(_46, _57);
344
345 *r = _mm_unpacklo_epi64(rg0123, rg4567);
346 *g = _mm_unpackhi_epi64(rg0123, rg4567);
347 *b = _mm_unpacklo_epi64(ba0123, ba4567);
348 *a = _mm_unpackhi_epi64(ba0123, ba4567);
349 }
350 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
351 auto rg0123 = _mm_unpacklo_epi16(r, g), // r0 g0 r1 g1 r2 g2 r3 g3
352 rg4567 = _mm_unpackhi_epi16(r, g), // r4 g4 r5 g5 r6 g6 r7 g7
353 ba0123 = _mm_unpacklo_epi16(b, a),
354 ba4567 = _mm_unpackhi_epi16(b, a);
355
356 auto _01 = _mm_unpacklo_epi32(rg0123, ba0123),
357 _23 = _mm_unpackhi_epi32(rg0123, ba0123),
358 _45 = _mm_unpacklo_epi32(rg4567, ba4567),
359 _67 = _mm_unpackhi_epi32(rg4567, ba4567);
360
361 if (__builtin_expect(tail,0)) {
362 auto dst = (double*)ptr;
363 if (tail > 0) { _mm_storel_pd(dst+0, _01); }
364 if (tail > 1) { _mm_storeh_pd(dst+1, _01); }
365 if (tail > 2) { _mm_storel_pd(dst+2, _23); }
366 if (tail > 3) { _mm_storeh_pd(dst+3, _23); }
367 if (tail > 4) { _mm_storel_pd(dst+4, _45); }
368 if (tail > 5) { _mm_storeh_pd(dst+5, _45); }
369 if (tail > 6) { _mm_storel_pd(dst+6, _67); }
370 } else {
371 _mm_storeu_si128((__m128i*)ptr + 0, _01);
372 _mm_storeu_si128((__m128i*)ptr + 1, _23);
373 _mm_storeu_si128((__m128i*)ptr + 2, _45);
374 _mm_storeu_si128((__m128i*)ptr + 3, _67);
375 }
376 }
377
378 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
379 F _04, _15, _26, _37;
380 _04 = _15 = _26 = _37 = 0;
381 switch (tail) {
382 case 0: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+28), 1);
383 case 7: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+24), 1);
384 case 6: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+20), 1);
385 case 5: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+16), 1);
386 case 4: _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+12), 0);
387 case 3: _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+ 8), 0);
388 case 2: _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+ 4), 0);
389 case 1: _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+ 0), 0);
390 }
391
392 F rg0145 = _mm256_unpacklo_ps(_04,_15), // r0 r1 g0 g1 | r4 r5 g4 g5
393 ba0145 = _mm256_unpackhi_ps(_04,_15),
394 rg2367 = _mm256_unpacklo_ps(_26,_37),
395 ba2367 = _mm256_unpackhi_ps(_26,_37);
396
397 *r = _mm256_unpacklo_pd(rg0145, rg2367);
398 *g = _mm256_unpackhi_pd(rg0145, rg2367);
399 *b = _mm256_unpacklo_pd(ba0145, ba2367);
400 *a = _mm256_unpackhi_pd(ba0145, ba2367);
401 }
402 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
403 F rg0145 = _mm256_unpacklo_ps(r, g), // r0 g0 r1 g1 | r4 g4 r5 g5
404 rg2367 = _mm256_unpackhi_ps(r, g), // r2 ... | r6 ...
405 ba0145 = _mm256_unpacklo_ps(b, a), // b0 a0 b1 a1 | b4 a4 b5 a5
406 ba2367 = _mm256_unpackhi_ps(b, a); // b2 ... | b6 ...
407
408 F _04 = _mm256_unpacklo_pd(rg0145, ba0145), // r0 g0 b0 a0 | r4 g4 b4 a4
409 _15 = _mm256_unpackhi_pd(rg0145, ba0145), // r1 ... | r5 ...
410 _26 = _mm256_unpacklo_pd(rg2367, ba2367), // r2 ... | r6 ...
411 _37 = _mm256_unpackhi_pd(rg2367, ba2367); // r3 ... | r7 ...
412
413 if (__builtin_expect(tail, 0)) {
414 if (tail > 0) { _mm_storeu_ps(ptr+ 0, _mm256_extractf128_ps(_04, 0)); }
415 if (tail > 1) { _mm_storeu_ps(ptr+ 4, _mm256_extractf128_ps(_15, 0)); }
416 if (tail > 2) { _mm_storeu_ps(ptr+ 8, _mm256_extractf128_ps(_26, 0)); }
417 if (tail > 3) { _mm_storeu_ps(ptr+12, _mm256_extractf128_ps(_37, 0)); }
418 if (tail > 4) { _mm_storeu_ps(ptr+16, _mm256_extractf128_ps(_04, 1)); }
419 if (tail > 5) { _mm_storeu_ps(ptr+20, _mm256_extractf128_ps(_15, 1)); }
420 if (tail > 6) { _mm_storeu_ps(ptr+24, _mm256_extractf128_ps(_26, 1)); }
421 } else {
422 F _01 = _mm256_permute2f128_ps(_04, _15, 32), // 32 == 0010 0000 == lo, lo
423 _23 = _mm256_permute2f128_ps(_26, _37, 32),
424 _45 = _mm256_permute2f128_ps(_04, _15, 49), // 49 == 0011 0001 == hi, hi
425 _67 = _mm256_permute2f128_ps(_26, _37, 49);
426 _mm256_storeu_ps(ptr+ 0, _01);
427 _mm256_storeu_ps(ptr+ 8, _23);
428 _mm256_storeu_ps(ptr+16, _45);
429 _mm256_storeu_ps(ptr+24, _67);
430 }
431 }
432
433#elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41)
Mike Kleinadc78d52018-01-01 09:06:37 -0500434 template <typename T> using V = T __attribute__((ext_vector_type(4)));
435 using F = V<float >;
436 using I32 = V< int32_t>;
437 using U64 = V<uint64_t>;
438 using U32 = V<uint32_t>;
439 using U16 = V<uint16_t>;
440 using U8 = V<uint8_t >;
441
442 SI F mad(F f, F m, F a) { return f*m+a; }
443 SI F min(F a, F b) { return _mm_min_ps(a,b); }
444 SI F max(F a, F b) { return _mm_max_ps(a,b); }
445 SI F abs_(F v) { return _mm_and_ps(v, 0-v); }
446 SI F rcp (F v) { return _mm_rcp_ps (v); }
447 SI F rsqrt (F v) { return _mm_rsqrt_ps(v); }
448 SI F sqrt_(F v) { return _mm_sqrt_ps (v); }
449 SI U32 round(F v, F scale) { return _mm_cvtps_epi32(v*scale); }
450
451 SI U16 pack(U32 v) {
452 #if defined(JUMPER_IS_SSE41)
453 auto p = _mm_packus_epi32(v,v);
454 #else
455 // Sign extend so that _mm_packs_epi32() does the pack we want.
456 auto p = _mm_srai_epi32(_mm_slli_epi32(v, 16), 16);
457 p = _mm_packs_epi32(p,p);
458 #endif
459 return unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
460 }
461 SI U8 pack(U16 v) {
462 auto r = widen_cast<__m128i>(v);
463 r = _mm_packus_epi16(r,r);
464 return unaligned_load<U8>(&r);
465 }
466
467 SI F if_then_else(I32 c, F t, F e) {
468 return _mm_or_ps(_mm_and_ps(c, t), _mm_andnot_ps(c, e));
469 }
470
471 SI F floor_(F v) {
472 #if defined(JUMPER_IS_SSE41)
473 return _mm_floor_ps(v);
474 #else
475 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
476 return roundtrip - if_then_else(roundtrip > v, 1, 0);
477 #endif
478 }
479
480 template <typename T>
481 SI V<T> gather(const T* p, U32 ix) {
482 return {p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
483 }
484
485 SI void load3(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
486 __m128i _0, _1, _2, _3;
487 if (__builtin_expect(tail,0)) {
488 _1 = _2 = _3 = _mm_setzero_si128();
489 auto load_rgb = [](const uint16_t* src) {
490 auto v = _mm_cvtsi32_si128(*(const uint32_t*)src);
491 return _mm_insert_epi16(v, src[2], 2);
492 };
493 if ( true ) { _0 = load_rgb(ptr + 0); }
494 if (tail > 1) { _1 = load_rgb(ptr + 3); }
495 if (tail > 2) { _2 = load_rgb(ptr + 6); }
496 } else {
497 // Load slightly weirdly to make sure we don't load past the end of 4x48 bits.
498 auto _01 = _mm_loadu_si128((const __m128i*)(ptr + 0)) ,
499 _23 = _mm_srli_si128(_mm_loadu_si128((const __m128i*)(ptr + 4)), 4);
500
501 // Each _N holds R,G,B for pixel N in its lower 3 lanes (upper 5 are ignored).
502 _0 = _01;
503 _1 = _mm_srli_si128(_01, 6);
504 _2 = _23;
505 _3 = _mm_srli_si128(_23, 6);
506 }
507
508 // De-interlace to R,G,B.
509 auto _02 = _mm_unpacklo_epi16(_0, _2), // r0 r2 g0 g2 b0 b2 xx xx
510 _13 = _mm_unpacklo_epi16(_1, _3); // r1 r3 g1 g3 b1 b3 xx xx
511
512 auto R = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
513 G = _mm_srli_si128(R, 8),
514 B = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 xx xx xx xx
515
516 *r = unaligned_load<U16>(&R);
517 *g = unaligned_load<U16>(&G);
518 *b = unaligned_load<U16>(&B);
519 }
520
521 SI void load4(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
522 __m128i _01, _23;
523 if (__builtin_expect(tail,0)) {
524 _01 = _23 = _mm_setzero_si128();
525 auto src = (const double*)ptr;
526 if ( true ) { _01 = _mm_loadl_pd(_01, src + 0); } // r0 g0 b0 a0 00 00 00 00
527 if (tail > 1) { _01 = _mm_loadh_pd(_01, src + 1); } // r0 g0 b0 a0 r1 g1 b1 a1
528 if (tail > 2) { _23 = _mm_loadl_pd(_23, src + 2); } // r2 g2 b2 a2 00 00 00 00
529 } else {
530 _01 = _mm_loadu_si128(((__m128i*)ptr) + 0); // r0 g0 b0 a0 r1 g1 b1 a1
531 _23 = _mm_loadu_si128(((__m128i*)ptr) + 1); // r2 g2 b2 a2 r3 g3 b3 a3
532 }
533
534 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
535 _13 = _mm_unpackhi_epi16(_01, _23); // r1 r3 g1 g3 b1 b3 a1 a3
536
537 auto rg = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
538 ba = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 a0 a1 a2 a3
539
540 *r = unaligned_load<U16>((uint16_t*)&rg + 0);
541 *g = unaligned_load<U16>((uint16_t*)&rg + 4);
542 *b = unaligned_load<U16>((uint16_t*)&ba + 0);
543 *a = unaligned_load<U16>((uint16_t*)&ba + 4);
544 }
545
546 SI void store4(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
547 auto rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g)),
548 ba = _mm_unpacklo_epi16(widen_cast<__m128i>(b), widen_cast<__m128i>(a));
549
550 if (__builtin_expect(tail, 0)) {
551 auto dst = (double*)ptr;
552 if ( true ) { _mm_storel_pd(dst + 0, _mm_unpacklo_epi32(rg, ba)); }
553 if (tail > 1) { _mm_storeh_pd(dst + 1, _mm_unpacklo_epi32(rg, ba)); }
554 if (tail > 2) { _mm_storel_pd(dst + 2, _mm_unpackhi_epi32(rg, ba)); }
555 } else {
556 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg, ba));
557 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg, ba));
558 }
559 }
560
561 SI void load4(const float* ptr, size_t tail, F* r, F* g, F* b, F* a) {
562 F _0, _1, _2, _3;
563 if (__builtin_expect(tail, 0)) {
564 _1 = _2 = _3 = _mm_setzero_si128();
565 if ( true ) { _0 = _mm_loadu_ps(ptr + 0); }
566 if (tail > 1) { _1 = _mm_loadu_ps(ptr + 4); }
567 if (tail > 2) { _2 = _mm_loadu_ps(ptr + 8); }
568 } else {
569 _0 = _mm_loadu_ps(ptr + 0);
570 _1 = _mm_loadu_ps(ptr + 4);
571 _2 = _mm_loadu_ps(ptr + 8);
572 _3 = _mm_loadu_ps(ptr +12);
573 }
574 _MM_TRANSPOSE4_PS(_0,_1,_2,_3);
575 *r = _0;
576 *g = _1;
577 *b = _2;
578 *a = _3;
579 }
580
581 SI void store4(float* ptr, size_t tail, F r, F g, F b, F a) {
582 _MM_TRANSPOSE4_PS(r,g,b,a);
583 if (__builtin_expect(tail, 0)) {
584 if ( true ) { _mm_storeu_ps(ptr + 0, r); }
585 if (tail > 1) { _mm_storeu_ps(ptr + 4, g); }
586 if (tail > 2) { _mm_storeu_ps(ptr + 8, b); }
587 } else {
588 _mm_storeu_ps(ptr + 0, r);
589 _mm_storeu_ps(ptr + 4, g);
590 _mm_storeu_ps(ptr + 8, b);
591 _mm_storeu_ps(ptr +12, a);
592 }
593 }
594#endif
595
596// We need to be a careful with casts.
597// (F)x means cast x to float in the portable path, but bit_cast x to float in the others.
598// These named casts and bit_cast() are always what they seem to be.
599#if defined(JUMPER_IS_SCALAR)
600 SI F cast (U32 v) { return (F)v; }
601 SI U32 trunc_(F v) { return (U32)v; }
602 SI U32 expand(U16 v) { return (U32)v; }
603 SI U32 expand(U8 v) { return (U32)v; }
604#else
605 SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); }
606 SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); }
607 SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); }
608 SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); }
609#endif
610
611template <typename V>
612SI V if_then_else(I32 c, V t, V e) {
613 return bit_cast<V>(if_then_else(c, bit_cast<F>(t), bit_cast<F>(e)));
614}
615
616SI U16 bswap(U16 x) {
617#if defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41)
618 // Somewhat inexplicably Clang decides to do (x<<8) | (x>>8) in 32-bit lanes
619 // when generating code for SSE2 and SSE4.1. We'll do it manually...
620 auto v = widen_cast<__m128i>(x);
621 v = _mm_slli_epi16(v,8) | _mm_srli_epi16(v,8);
622 return unaligned_load<U16>(&v);
623#else
624 return (x<<8) | (x>>8);
625#endif
626}
627
628SI F fract(F v) { return v - floor_(v); }
629
630// See http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html.
631SI F approx_log2(F x) {
632 // e - 127 is a fair approximation of log2(x) in its own right...
633 F e = cast(bit_cast<U32>(x)) * (1.0f / (1<<23));
634
635 // ... but using the mantissa to refine its error is _much_ better.
636 F m = bit_cast<F>((bit_cast<U32>(x) & 0x007fffff) | 0x3f000000);
637 return e
638 - 124.225514990f
639 - 1.498030302f * m
640 - 1.725879990f / (0.3520887068f + m);
641}
642SI F approx_pow2(F x) {
643 F f = fract(x);
644 return bit_cast<F>(round(1.0f * (1<<23),
645 x + 121.274057500f
646 - 1.490129070f * f
647 + 27.728023300f / (4.84252568f - f)));
648}
649
650SI F approx_powf(F x, F y) {
651 return if_then_else(x == 0, 0
652 , approx_pow2(approx_log2(x) * y));
653}
654
655SI F from_half(U16 h) {
656#if defined(__aarch64__) && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds.
657 return vcvt_f32_f16(h);
658
659#elif defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
660 return _mm256_cvtph_ps(h);
661
662#else
663 // Remember, a half is 1-5-10 (sign-exponent-mantissa) with 15 exponent bias.
664 U32 sem = expand(h),
665 s = sem & 0x8000,
666 em = sem ^ s;
667
668 // Convert to 1-8-23 float with 127 bias, flushing denorm halfs (including zero) to zero.
669 auto denorm = (I32)em < 0x0400; // I32 comparison is often quicker, and always safe here.
670 return if_then_else(denorm, F(0)
671 , bit_cast<F>( (s<<16) + (em<<13) + ((127-15)<<23) ));
672#endif
673}
674
675SI U16 to_half(F f) {
676#if defined(__aarch64__) && !defined(SK_BUILD_FOR_GOOGLE3) // Temporary workaround for some Google3 builds.
677 return vcvt_f16_f32(f);
678
679#elif defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
680 return _mm256_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
681
682#else
683 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
684 U32 sem = bit_cast<U32>(f),
685 s = sem & 0x80000000,
686 em = sem ^ s;
687
688 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
689 auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here.
690 return pack(if_then_else(denorm, U32(0)
691 , (s>>16) + (em>>13) - ((127-15)<<10)));
692#endif
693}
694
695// Our fundamental vector depth is our pixel stride.
696static const size_t N = sizeof(F) / sizeof(float);
697
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400698// We're finally going to get to what a Stage function looks like!
Mike Klein0e4d0962017-09-27 11:04:34 -0400699// tail == 0 ~~> work on a full N pixels
Mike Kleinb5e48422017-05-30 18:09:29 -0400700// tail != 0 ~~> work on only the first tail pixels
Mike Klein0e4d0962017-09-27 11:04:34 -0400701// tail is always < N.
Mike Kleinf1b24e02017-07-27 12:31:34 -0400702
Mike Klein1b9b7d52018-02-27 10:37:40 -0500703// Any custom ABI to use for all non-externally-facing stage functions.
704#if defined(__ARM_NEON) && defined(__arm__)
705 // This lets us pass vectors more efficiently on 32-bit ARM.
706 #define ABI __attribute__((pcs("aapcs-vfp")))
Mike Kleina46623b2018-03-10 10:27:24 -0500707#elif 0 || defined(__clang__) && defined(_MSC_VER)
Mike Klein1b9b7d52018-02-27 10:37:40 -0500708 // TODO: can we use sysv_abi here instead? It'd allow passing far more registers.
709 #define ABI __attribute__((vectorcall))
710#else
711 #define ABI
712#endif
713
714// On 32-bit x86 we've only got 8 xmm registers, so we keep the 4 hottest (r,g,b,a)
715// in registers and the d-registers on the stack (giving us 4 temporary registers).
716// General-purpose registers are also tight, so we put most of those on the stack too.
717//
718// On ARMv7, we do the same so that we can make the r,g,b,a vectors wider.
719//
720// Finally, this narrower stage calling convention also fits Windows' __vectorcall very well.
Mike Kleina46623b2018-03-10 10:27:24 -0500721#if 0 || defined(__i386__) || defined(_M_IX86) || defined(__arm__) || defined(_MSC_VER)
Mike Klein1b9b7d52018-02-27 10:37:40 -0500722 #define JUMPER_NARROW_STAGES 1
723#else
724 #define JUMPER_NARROW_STAGES 0
725#endif
726
727#if JUMPER_NARROW_STAGES
Mike Kleinf1b24e02017-07-27 12:31:34 -0400728 struct Params {
Mike Kleinb4379132017-10-17 16:06:49 -0400729 size_t dx, dy, tail;
Mike Kleinf1b24e02017-07-27 12:31:34 -0400730 F dr,dg,db,da;
731 };
Mike Klein376fd312017-12-11 16:53:26 -0500732 using Stage = void(ABI*)(Params*, void** program, F r, F g, F b, F a);
Mike Kleinf1b24e02017-07-27 12:31:34 -0400733#else
734 // We keep program the second argument, so that it's passed in rsi for load_and_inc().
Mike Klein376fd312017-12-11 16:53:26 -0500735 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 -0400736#endif
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400737
Mike Klein376fd312017-12-11 16:53:26 -0500738
Mike Klein1b9b7d52018-02-27 10:37:40 -0500739static void start_pipeline(size_t dx, size_t dy, size_t xlimit, size_t ylimit, void** program) {
Mike Klein376fd312017-12-11 16:53:26 -0500740 auto start = (Stage)load_and_inc(program);
Mike Kleinb4379132017-10-17 16:06:49 -0400741 const size_t x0 = dx;
742 for (; dy < ylimit; dy++) {
Mike Klein1b9b7d52018-02-27 10:37:40 -0500743 #if JUMPER_NARROW_STAGES
Mike Kleinb4379132017-10-17 16:06:49 -0400744 Params params = { x0,dy,0, 0,0,0,0 };
745 while (params.dx + N <= xlimit) {
Mike Kleinabb8bb32017-09-27 11:12:01 -0400746 start(&params,program, 0,0,0,0);
Mike Kleinb4379132017-10-17 16:06:49 -0400747 params.dx += N;
Mike Kleinf1b24e02017-07-27 12:31:34 -0400748 }
Mike Kleinb4379132017-10-17 16:06:49 -0400749 if (size_t tail = xlimit - params.dx) {
Mike Kleinf1b24e02017-07-27 12:31:34 -0400750 params.tail = tail;
Mike Kleinabb8bb32017-09-27 11:12:01 -0400751 start(&params,program, 0,0,0,0);
Mike Kleinf1b24e02017-07-27 12:31:34 -0400752 }
753 #else
Mike Kleinb4379132017-10-17 16:06:49 -0400754 dx = x0;
755 while (dx + N <= xlimit) {
756 start(0,program,dx,dy, 0,0,0,0, 0,0,0,0);
757 dx += N;
Mike Klein45c16fa2017-07-18 18:15:13 -0400758 }
Mike Kleinb4379132017-10-17 16:06:49 -0400759 if (size_t tail = xlimit - dx) {
760 start(tail,program,dx,dy, 0,0,0,0, 0,0,0,0);
Mike Klein45c16fa2017-07-18 18:15:13 -0400761 }
Mike Kleinf1b24e02017-07-27 12:31:34 -0400762 #endif
Mike Klein3b92b692017-07-18 11:30:25 -0400763 }
764}
765
Mike Klein1b9b7d52018-02-27 10:37:40 -0500766#if JUMPER_NARROW_STAGES
767 #define STAGE(name, ...) \
768 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
769 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
770 static ABI void name(Params* params, void** program, \
771 F r, F g, F b, F a) { \
772 name##_k(Ctx{program},params->dx,params->dy,params->tail, r,g,b,a, \
773 params->dr, params->dg, params->db, params->da); \
774 auto next = (Stage)load_and_inc(program); \
775 next(params,program, r,g,b,a); \
776 } \
777 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleinf1b24e02017-07-27 12:31:34 -0400778 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
779#else
Mike Klein1b9b7d52018-02-27 10:37:40 -0500780 #define STAGE(name, ...) \
781 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
782 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
783 static ABI void name(size_t tail, void** program, size_t dx, size_t dy, \
784 F r, F g, F b, F a, F dr, F dg, F db, F da) { \
785 name##_k(Ctx{program},dx,dy,tail, r,g,b,a, dr,dg,db,da); \
786 auto next = (Stage)load_and_inc(program); \
787 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
788 } \
789 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
Mike Kleinf1b24e02017-07-27 12:31:34 -0400790 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
791#endif
Mike Kleinb5e48422017-05-30 18:09:29 -0400792
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400793
794// just_return() is a simple no-op stage that only exists to end the chain,
795// returning back up to start_pipeline(), and from there to the caller.
Mike Klein1b9b7d52018-02-27 10:37:40 -0500796#if JUMPER_NARROW_STAGES
797 static ABI void just_return(Params*, void**, F,F,F,F) {}
Mike Kleinf1b24e02017-07-27 12:31:34 -0400798#else
Mike Klein1b9b7d52018-02-27 10:37:40 -0500799 static ABI void just_return(size_t, void**, size_t,size_t, F,F,F,F, F,F,F,F) {}
Mike Kleinf1b24e02017-07-27 12:31:34 -0400800#endif
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400801
802
Mike Klein8a823fa2017-04-05 17:29:26 -0400803// We could start defining normal Stages now. But first, some helper functions.
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400804
805// These load() and store() methods are tail-aware,
806// but focus mainly on keeping the at-stride tail==0 case fast.
Mike Kleine1caee12017-02-15 13:31:12 -0500807
Mike Kleinc31858b2017-03-01 13:07:40 -0500808template <typename V, typename T>
Mike Klein64b97482017-03-14 17:35:04 -0700809SI V load(const T* src, size_t tail) {
Mike Kleind6e12862017-08-28 12:18:26 -0400810#if !defined(JUMPER_IS_SCALAR)
Mike Klein0e4d0962017-09-27 11:04:34 -0400811 __builtin_assume(tail < N);
Mike Kleinc31858b2017-03-01 13:07:40 -0500812 if (__builtin_expect(tail, 0)) {
813 V v{}; // Any inactive lanes are zeroed.
Mike Kleinc4fcbed2017-06-26 16:12:48 -0400814 switch (tail) {
815 case 7: v[6] = src[6];
816 case 6: v[5] = src[5];
817 case 5: v[4] = src[4];
818 case 4: memcpy(&v, src, 4*sizeof(T)); break;
819 case 3: v[2] = src[2];
820 case 2: memcpy(&v, src, 2*sizeof(T)); break;
821 case 1: memcpy(&v, src, 1*sizeof(T)); break;
Mike Kleinc31858b2017-03-01 13:07:40 -0500822 }
823 return v;
824 }
825#endif
826 return unaligned_load<V>(src);
827}
828
Mike Kleinc31858b2017-03-01 13:07:40 -0500829template <typename V, typename T>
Mike Klein64b97482017-03-14 17:35:04 -0700830SI void store(T* dst, V v, size_t tail) {
Mike Kleind6e12862017-08-28 12:18:26 -0400831#if !defined(JUMPER_IS_SCALAR)
Mike Klein0e4d0962017-09-27 11:04:34 -0400832 __builtin_assume(tail < N);
Mike Kleinc31858b2017-03-01 13:07:40 -0500833 if (__builtin_expect(tail, 0)) {
Mike Kleinc4fcbed2017-06-26 16:12:48 -0400834 switch (tail) {
835 case 7: dst[6] = v[6];
836 case 6: dst[5] = v[5];
837 case 5: dst[4] = v[4];
838 case 4: memcpy(dst, &v, 4*sizeof(T)); break;
839 case 3: dst[2] = v[2];
840 case 2: memcpy(dst, &v, 2*sizeof(T)); break;
841 case 1: memcpy(dst, &v, 1*sizeof(T)); break;
Mike Kleinc31858b2017-03-01 13:07:40 -0500842 }
843 return;
844 }
845#endif
Mike Kleinc33aa902017-05-15 10:20:48 -0400846 unaligned_store(dst, v);
Mike Kleinc31858b2017-03-01 13:07:40 -0500847}
848
Mike Klein40de6da2017-04-07 13:09:29 -0400849SI F from_byte(U8 b) {
Mike Kleinfe560a82017-05-01 12:56:35 -0400850 return cast(expand(b)) * (1/255.0f);
Mike Klein40de6da2017-04-07 13:09:29 -0400851}
Mike Klein64b97482017-03-14 17:35:04 -0700852SI void from_565(U16 _565, F* r, F* g, F* b) {
Mike Klein3f81f372017-02-23 13:03:57 -0500853 U32 wide = expand(_565);
Mike Kleinfe560a82017-05-01 12:56:35 -0400854 *r = cast(wide & (31<<11)) * (1.0f / (31<<11));
855 *g = cast(wide & (63<< 5)) * (1.0f / (63<< 5));
856 *b = cast(wide & (31<< 0)) * (1.0f / (31<< 0));
Mike Kleine1caee12017-02-15 13:31:12 -0500857}
Mike Kleinf809fef2017-03-31 13:52:45 -0400858SI void from_4444(U16 _4444, F* r, F* g, F* b, F* a) {
859 U32 wide = expand(_4444);
Mike Kleinfe560a82017-05-01 12:56:35 -0400860 *r = cast(wide & (15<<12)) * (1.0f / (15<<12));
861 *g = cast(wide & (15<< 8)) * (1.0f / (15<< 8));
862 *b = cast(wide & (15<< 4)) * (1.0f / (15<< 4));
863 *a = cast(wide & (15<< 0)) * (1.0f / (15<< 0));
Mike Kleinf809fef2017-03-31 13:52:45 -0400864}
Mike Kleindec4ea82017-04-06 15:04:05 -0400865SI void from_8888(U32 _8888, F* r, F* g, F* b, F* a) {
Mike Kleinfe560a82017-05-01 12:56:35 -0400866 *r = cast((_8888 ) & 0xff) * (1/255.0f);
867 *g = cast((_8888 >> 8) & 0xff) * (1/255.0f);
868 *b = cast((_8888 >> 16) & 0xff) * (1/255.0f);
869 *a = cast((_8888 >> 24) ) * (1/255.0f);
Mike Kleindec4ea82017-04-06 15:04:05 -0400870}
Mike Kleinac568a92018-01-25 09:09:32 -0500871SI void from_1010102(U32 rgba, F* r, F* g, F* b, F* a) {
872 *r = cast((rgba ) & 0x3ff) * (1/1023.0f);
873 *g = cast((rgba >> 10) & 0x3ff) * (1/1023.0f);
874 *b = cast((rgba >> 20) & 0x3ff) * (1/1023.0f);
875 *a = cast((rgba >> 30) ) * (1/ 3.0f);
876}
Mike Kleindec4ea82017-04-06 15:04:05 -0400877
Mike Kleinb4379132017-10-17 16:06:49 -0400878// 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 -0400879template <typename T>
Mike Klein678f6b12018-01-24 15:22:04 -0500880SI T* ptr_at_xy(const SkJumper_MemoryCtx* ctx, size_t dx, size_t dy) {
Mike Kleinb4379132017-10-17 16:06:49 -0400881 return (T*)ctx->pixels + dy*ctx->stride + dx;
Mike Klein45c16fa2017-07-18 18:15:13 -0400882}
883
Mike Klein1fa9c432017-12-11 09:59:47 -0500884// clamp v to [0,limit).
885SI F clamp(F v, F limit) {
886 F inclusive = bit_cast<F>( bit_cast<U32>(limit) - 1 ); // Exclusive -> inclusive.
887 return min(max(0, v), inclusive);
888}
889
Mike Klein45c16fa2017-07-18 18:15:13 -0400890// Used by gather_ stages to calculate the base pointer and a vector of indices to load.
Mike Kleindec4ea82017-04-06 15:04:05 -0400891template <typename T>
Mike Kleinf3b4e162017-09-22 15:32:59 -0400892SI U32 ix_and_ptr(T** ptr, const SkJumper_GatherCtx* ctx, F x, F y) {
Mike Kleinf3b4e162017-09-22 15:32:59 -0400893 x = clamp(x, ctx->width);
894 y = clamp(y, ctx->height);
895
Mike Kleindec4ea82017-04-06 15:04:05 -0400896 *ptr = (const T*)ctx->pixels;
897 return trunc_(y)*ctx->stride + trunc_(x);
898}
Mike Kleine1caee12017-02-15 13:31:12 -0500899
Mike Klein37155d42017-12-15 09:55:03 -0500900// We often have a nominally [0,1] float value we need to scale and convert to an integer,
901// whether for a table lookup or to pack back down into bytes for storage.
902//
903// In practice, especially when dealing with interesting color spaces, that notionally
904// [0,1] float may be out of [0,1] range. Unorms cannot represent that, so we must clamp.
905//
906// You can adjust the expected input to [0,bias] by tweaking that parameter.
907SI U32 to_unorm(F v, F scale, F bias = 1.0f) {
908 // TODO: platform-specific implementations to to_unorm(), removing round() entirely?
909 // Any time we use round() we probably want to use to_unorm().
910 return round(min(max(0, v), bias), scale);
911}
912
Mike Reeddfc0e912018-02-16 12:40:18 -0500913SI I32 cond_to_mask(I32 cond) { return if_then_else(cond, I32(~0), I32(0)); }
914
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400915// Now finally, normal Stages!
Mike Kleine1caee12017-02-15 13:31:12 -0500916
Mike Klein85f85362017-10-17 14:22:58 -0400917STAGE(seed_shader, const float* iota) {
Mike Kleinb4379132017-10-17 16:06:49 -0400918 // It's important for speed to explicitly cast(dx) and cast(dy),
Mike Kleine1caee12017-02-15 13:31:12 -0500919 // which has the effect of splatting them to vectors before converting to floats.
920 // On Intel this breaks a data dependency on previous loop iterations' registers.
Mike Kleinb4379132017-10-17 16:06:49 -0400921 r = cast(dx) + unaligned_load<F>(iota);
922 g = cast(dy) + 0.5f;
Mike Klein2229b572017-04-21 10:30:29 -0400923 b = 1.0f;
Mike Kleine1caee12017-02-15 13:31:12 -0500924 a = 0;
925 dr = dg = db = da = 0;
926}
927
Mike Kleinf7729c22017-09-27 11:42:30 -0400928STAGE(dither, const float* rate) {
Mike Kleinb4379132017-10-17 16:06:49 -0400929 // Get [(dx,dy), (dx+1,dy), (dx+2,dy), ...] loaded up in integer vectors.
Mike Klein856b3c32017-08-29 13:38:09 -0400930 uint32_t iota[] = {0,1,2,3,4,5,6,7};
Mike Kleinb4379132017-10-17 16:06:49 -0400931 U32 X = dx + unaligned_load<U32>(iota),
932 Y = dy;
Mike Klein581e6982017-05-03 13:05:13 -0400933
934 // We're doing 8x8 ordered dithering, see https://en.wikipedia.org/wiki/Ordered_dithering.
935 // In this case n=8 and we're using the matrix that looks like 1/64 x [ 0 48 12 60 ... ].
936
937 // We only need X and X^Y from here on, so it's easier to just think of that as "Y".
938 Y ^= X;
939
940 // We'll mix the bottom 3 bits of each of X and Y to make 6 bits,
941 // for 2^6 == 64 == 8x8 matrix values. If X=abc and Y=def, we make fcebda.
942 U32 M = (Y & 1) << 5 | (X & 1) << 4
943 | (Y & 2) << 2 | (X & 2) << 1
944 | (Y & 4) >> 1 | (X & 4) >> 2;
945
Mike Kleindb711c92017-05-03 17:57:48 -0400946 // Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as 0.5-epsilon.
947 // We want to make sure our dither is less than 0.5 in either direction to keep exact values
948 // like 0 and 1 unchanged after rounding.
949 F dither = cast(M) * (2/128.0f) - (63/128.0f);
Mike Klein581e6982017-05-03 13:05:13 -0400950
Mike Kleinf7729c22017-09-27 11:42:30 -0400951 r += *rate*dither;
952 g += *rate*dither;
953 b += *rate*dither;
Mike Klein7e68bc92017-05-16 12:03:15 -0400954
955 r = max(0, min(r, a));
956 g = max(0, min(g, a));
957 b = max(0, min(b, a));
Mike Klein581e6982017-05-03 13:05:13 -0400958}
959
Mike Reed9959f722017-05-15 09:34:22 -0400960// load 4 floats from memory, and splat them into r,g,b,a
Mike Kleinf7729c22017-09-27 11:42:30 -0400961STAGE(uniform_color, const SkJumper_UniformColorCtx* c) {
Mike Klein1a2e3e12017-08-03 11:24:13 -0400962 r = c->r;
963 g = c->g;
964 b = c->b;
965 a = c->a;
Mike Kleine1caee12017-02-15 13:31:12 -0500966}
967
Mike Reedc91e3872017-07-05 14:12:37 -0400968// splats opaque-black into r,g,b,a
Mike Kleinf7729c22017-09-27 11:42:30 -0400969STAGE(black_color, Ctx::None) {
Mike Reedc91e3872017-07-05 14:12:37 -0400970 r = g = b = 0.0f;
971 a = 1.0f;
972}
973
Mike Kleinf7729c22017-09-27 11:42:30 -0400974STAGE(white_color, Ctx::None) {
Mike Reedc91e3872017-07-05 14:12:37 -0400975 r = g = b = a = 1.0f;
976}
977
Mike Reed9959f722017-05-15 09:34:22 -0400978// load registers r,g,b,a from context (mirrors store_rgba)
Mike Kleinf7729c22017-09-27 11:42:30 -0400979STAGE(load_rgba, const float* ptr) {
Mike Klein0e4d0962017-09-27 11:04:34 -0400980 r = unaligned_load<F>(ptr + 0*N);
981 g = unaligned_load<F>(ptr + 1*N);
982 b = unaligned_load<F>(ptr + 2*N);
983 a = unaligned_load<F>(ptr + 3*N);
Mike Reed9959f722017-05-15 09:34:22 -0400984}
985
986// store registers r,g,b,a into context (mirrors load_rgba)
Mike Kleinf7729c22017-09-27 11:42:30 -0400987STAGE(store_rgba, float* ptr) {
Mike Klein0e4d0962017-09-27 11:04:34 -0400988 unaligned_store(ptr + 0*N, r);
989 unaligned_store(ptr + 1*N, g);
990 unaligned_store(ptr + 2*N, b);
991 unaligned_store(ptr + 3*N, a);
Mike Reed9959f722017-05-15 09:34:22 -0400992}
993
Mike Kleinb9c4a6f2017-04-03 13:54:55 -0400994// Most blend modes apply the same logic to each channel.
Mike Kleinaaca1e42017-03-31 09:29:01 -0400995#define BLEND_MODE(name) \
996 SI F name##_channel(F s, F d, F sa, F da); \
Mike Kleinf7729c22017-09-27 11:42:30 -0400997 STAGE(name, Ctx::None) { \
Mike Kleinaaca1e42017-03-31 09:29:01 -0400998 r = name##_channel(r,dr,a,da); \
999 g = name##_channel(g,dg,a,da); \
1000 b = name##_channel(b,db,a,da); \
1001 a = name##_channel(a,da,a,da); \
1002 } \
1003 SI F name##_channel(F s, F d, F sa, F da)
Mike Kleine1caee12017-02-15 13:31:12 -05001004
Mike Kleinfe560a82017-05-01 12:56:35 -04001005SI F inv(F x) { return 1.0f - x; }
Mike Klein66b09ab2017-03-31 10:29:40 -04001006SI F two(F x) { return x + x; }
Yuqian Li7741c752017-12-11 14:17:47 -05001007
Mike Kleine1caee12017-02-15 13:31:12 -05001008
Mike Kleinaaca1e42017-03-31 09:29:01 -04001009BLEND_MODE(clear) { return 0; }
1010BLEND_MODE(srcatop) { return s*da + d*inv(sa); }
1011BLEND_MODE(dstatop) { return d*sa + s*inv(da); }
1012BLEND_MODE(srcin) { return s * da; }
1013BLEND_MODE(dstin) { return d * sa; }
1014BLEND_MODE(srcout) { return s * inv(da); }
1015BLEND_MODE(dstout) { return d * inv(sa); }
1016BLEND_MODE(srcover) { return mad(d, inv(sa), s); }
1017BLEND_MODE(dstover) { return mad(s, inv(da), d); }
1018
1019BLEND_MODE(modulate) { return s*d; }
1020BLEND_MODE(multiply) { return s*inv(da) + d*inv(sa) + s*d; }
Mike Kleinfb126fa2017-08-24 13:06:23 -04001021BLEND_MODE(plus_) { return min(s + d, 1.0f); } // We can clamp to either 1 or sa.
Mike Kleinaaca1e42017-03-31 09:29:01 -04001022BLEND_MODE(screen) { return s + d - s*d; }
1023BLEND_MODE(xor_) { return s*inv(da) + d*inv(sa); }
Mike Klein66b09ab2017-03-31 10:29:40 -04001024#undef BLEND_MODE
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001025
1026// Most other blend modes apply the same logic to colors, and srcover to alpha.
Mike Klein66b09ab2017-03-31 10:29:40 -04001027#define BLEND_MODE(name) \
1028 SI F name##_channel(F s, F d, F sa, F da); \
Mike Kleinf7729c22017-09-27 11:42:30 -04001029 STAGE(name, Ctx::None) { \
Mike Klein66b09ab2017-03-31 10:29:40 -04001030 r = name##_channel(r,dr,a,da); \
1031 g = name##_channel(g,dg,a,da); \
1032 b = name##_channel(b,db,a,da); \
1033 a = mad(da, inv(a), a); \
1034 } \
1035 SI F name##_channel(F s, F d, F sa, F da)
1036
1037BLEND_MODE(darken) { return s + d - max(s*da, d*sa) ; }
1038BLEND_MODE(lighten) { return s + d - min(s*da, d*sa) ; }
1039BLEND_MODE(difference) { return s + d - two(min(s*da, d*sa)); }
1040BLEND_MODE(exclusion) { return s + d - two(s*d); }
1041
Mike Klein61b84162017-03-31 11:48:14 -04001042BLEND_MODE(colorburn) {
Florin Malita59a62ed2017-08-23 12:08:37 -04001043 return if_then_else(d == da, d + s*inv(da),
1044 if_then_else(s == 0, /* s + */ d*inv(sa),
1045 sa*(da - min(da, (da-d)*sa*rcp(s))) + s*inv(da) + d*inv(sa)));
Mike Klein61b84162017-03-31 11:48:14 -04001046}
1047BLEND_MODE(colordodge) {
Florin Malita59a62ed2017-08-23 12:08:37 -04001048 return if_then_else(d == 0, /* d + */ s*inv(da),
1049 if_then_else(s == sa, s + d*inv(sa),
1050 sa*min(da, (d*sa)*rcp(sa - s)) + s*inv(da) + d*inv(sa)));
Mike Klein61b84162017-03-31 11:48:14 -04001051}
1052BLEND_MODE(hardlight) {
1053 return s*inv(da) + d*inv(sa)
1054 + if_then_else(two(s) <= sa, two(s*d), sa*da - two((da-d)*(sa-s)));
1055}
1056BLEND_MODE(overlay) {
1057 return s*inv(da) + d*inv(sa)
1058 + if_then_else(two(d) <= da, two(s*d), sa*da - two((da-d)*(sa-s)));
1059}
1060
1061BLEND_MODE(softlight) {
1062 F m = if_then_else(da > 0, d / da, 0),
1063 s2 = two(s),
1064 m4 = two(two(m));
1065
1066 // The logic forks three ways:
1067 // 1. dark src?
1068 // 2. light src, dark dst?
1069 // 3. light src, light dst?
Mike Kleinfe560a82017-05-01 12:56:35 -04001070 F darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
1071 darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
1072 liteDst = rcp(rsqrt(m)) - m, // Used in case 3.
Mike Klein61b84162017-03-31 11:48:14 -04001073 liteSrc = d*sa + da*(s2 - sa) * if_then_else(two(two(d)) <= da, darkDst, liteDst); // 2 or 3?
1074 return s*inv(da) + d*inv(sa) + if_then_else(s2 <= sa, darkSrc, liteSrc); // 1 or (2 or 3)?
1075}
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001076#undef BLEND_MODE
Mike Klein61b84162017-03-31 11:48:14 -04001077
Mike Kleinbb338332017-05-04 12:42:52 -04001078// We're basing our implemenation of non-separable blend modes on
1079// https://www.w3.org/TR/compositing-1/#blendingnonseparable.
1080// and
1081// https://www.khronos.org/registry/OpenGL/specs/es/3.2/es_spec_3.2.pdf
1082// They're equivalent, but ES' math has been better simplified.
Mike Klein08aa88d2017-05-12 12:59:24 -04001083//
1084// Anything extra we add beyond that is to make the math work with premul inputs.
Mike Kleinbb338332017-05-04 12:42:52 -04001085
1086SI F max(F r, F g, F b) { return max(r, max(g, b)); }
1087SI F min(F r, F g, F b) { return min(r, min(g, b)); }
1088
1089SI F sat(F r, F g, F b) { return max(r,g,b) - min(r,g,b); }
1090SI F lum(F r, F g, F b) { return r*0.30f + g*0.59f + b*0.11f; }
1091
1092SI void set_sat(F* r, F* g, F* b, F s) {
1093 F mn = min(*r,*g,*b),
1094 mx = max(*r,*g,*b),
1095 sat = mx - mn;
1096
1097 // Map min channel to 0, max channel to s, and scale the middle proportionally.
1098 auto scale = [=](F c) {
1099 return if_then_else(sat == 0, 0, (c - mn) * s / sat);
1100 };
1101 *r = scale(*r);
1102 *g = scale(*g);
1103 *b = scale(*b);
1104}
Mike Klein08aa88d2017-05-12 12:59:24 -04001105SI void set_lum(F* r, F* g, F* b, F l) {
1106 F diff = l - lum(*r, *g, *b);
1107 *r += diff;
1108 *g += diff;
1109 *b += diff;
1110}
1111SI void clip_color(F* r, F* g, F* b, F a) {
Mike Kleinbb338332017-05-04 12:42:52 -04001112 F mn = min(*r, *g, *b),
1113 mx = max(*r, *g, *b),
1114 l = lum(*r, *g, *b);
1115
1116 auto clip = [=](F c) {
1117 c = if_then_else(mn >= 0, c, l + (c - l) * ( l) / (l - mn) );
Mike Klein08aa88d2017-05-12 12:59:24 -04001118 c = if_then_else(mx > a, l + (c - l) * (a - l) / (mx - l), c);
Mike Kleinbb338332017-05-04 12:42:52 -04001119 c = max(c, 0); // Sometimes without this we may dip just a little negative.
1120 return c;
1121 };
1122 *r = clip(*r);
1123 *g = clip(*g);
1124 *b = clip(*b);
1125}
Mike Kleinbb338332017-05-04 12:42:52 -04001126
Mike Kleinf7729c22017-09-27 11:42:30 -04001127STAGE(hue, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001128 F R = r*a,
1129 G = g*a,
1130 B = b*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001131
Mike Klein08aa88d2017-05-12 12:59:24 -04001132 set_sat(&R, &G, &B, sat(dr,dg,db)*a);
1133 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
1134 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001135
Mike Klein08aa88d2017-05-12 12:59:24 -04001136 r = r*inv(da) + dr*inv(a) + R;
1137 g = g*inv(da) + dg*inv(a) + G;
1138 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001139 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001140}
Mike Kleinf7729c22017-09-27 11:42:30 -04001141STAGE(saturation, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001142 F R = dr*a,
1143 G = dg*a,
1144 B = db*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001145
Mike Klein08aa88d2017-05-12 12:59:24 -04001146 set_sat(&R, &G, &B, sat( r, g, b)*da);
1147 set_lum(&R, &G, &B, lum(dr,dg,db)* a); // (This is not redundant.)
1148 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001149
Mike Klein08aa88d2017-05-12 12:59:24 -04001150 r = r*inv(da) + dr*inv(a) + R;
1151 g = g*inv(da) + dg*inv(a) + G;
1152 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001153 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001154}
Mike Kleinf7729c22017-09-27 11:42:30 -04001155STAGE(color, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001156 F R = r*da,
1157 G = g*da,
1158 B = b*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001159
Mike Klein08aa88d2017-05-12 12:59:24 -04001160 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
1161 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001162
Mike Klein08aa88d2017-05-12 12:59:24 -04001163 r = r*inv(da) + dr*inv(a) + R;
1164 g = g*inv(da) + dg*inv(a) + G;
1165 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001166 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001167}
Mike Kleinf7729c22017-09-27 11:42:30 -04001168STAGE(luminosity, Ctx::None) {
Mike Klein08aa88d2017-05-12 12:59:24 -04001169 F R = dr*a,
1170 G = dg*a,
1171 B = db*a;
Mike Kleinbb338332017-05-04 12:42:52 -04001172
Mike Klein08aa88d2017-05-12 12:59:24 -04001173 set_lum(&R, &G, &B, lum(r,g,b)*da);
1174 clip_color(&R,&G,&B, a*da);
Mike Kleinbb338332017-05-04 12:42:52 -04001175
Mike Klein08aa88d2017-05-12 12:59:24 -04001176 r = r*inv(da) + dr*inv(a) + R;
1177 g = g*inv(da) + dg*inv(a) + G;
1178 b = b*inv(da) + db*inv(a) + B;
Mike Kleinbb338332017-05-04 12:42:52 -04001179 a = a + da - a*da;
Mike Kleinbb338332017-05-04 12:42:52 -04001180}
1181
Mike Kleinf7729c22017-09-27 11:42:30 -04001182STAGE(srcover_rgba_8888, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001183 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
Mike Klein50626262017-05-25 13:06:57 -04001184
1185 U32 dst = load<U32>(ptr, tail);
1186 dr = cast((dst ) & 0xff);
1187 dg = cast((dst >> 8) & 0xff);
1188 db = cast((dst >> 16) & 0xff);
1189 da = cast((dst >> 24) );
1190 // {dr,dg,db,da} are in [0,255]
Mike Klein37155d42017-12-15 09:55:03 -05001191 // { r, g, b, a} are in [0, 1] (but may be out of gamut)
Mike Klein50626262017-05-25 13:06:57 -04001192
1193 r = mad(dr, inv(a), r*255.0f);
1194 g = mad(dg, inv(a), g*255.0f);
1195 b = mad(db, inv(a), b*255.0f);
1196 a = mad(da, inv(a), a*255.0f);
Mike Klein37155d42017-12-15 09:55:03 -05001197 // { r, g, b, a} are now in [0,255] (but may be out of gamut)
Mike Klein50626262017-05-25 13:06:57 -04001198
Mike Klein37155d42017-12-15 09:55:03 -05001199 // to_unorm() clamps back to gamut. Scaling by 1 since we're already 255-biased.
1200 dst = to_unorm(r, 1, 255)
1201 | to_unorm(g, 1, 255) << 8
1202 | to_unorm(b, 1, 255) << 16
1203 | to_unorm(a, 1, 255) << 24;
Mike Klein50626262017-05-25 13:06:57 -04001204 store(ptr, dst, tail);
1205}
1206
Mike Klein0cb15892017-10-24 11:07:09 -04001207STAGE(srcover_bgra_8888, const SkJumper_MemoryCtx* ctx) {
1208 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
1209
1210 U32 dst = load<U32>(ptr, tail);
1211 db = cast((dst ) & 0xff);
1212 dg = cast((dst >> 8) & 0xff);
1213 dr = cast((dst >> 16) & 0xff);
1214 da = cast((dst >> 24) );
1215 // {dr,dg,db,da} are in [0,255]
Mike Klein37155d42017-12-15 09:55:03 -05001216 // { r, g, b, a} are in [0, 1] (but may be out of gamut)
Mike Klein0cb15892017-10-24 11:07:09 -04001217
1218 r = mad(dr, inv(a), r*255.0f);
1219 g = mad(dg, inv(a), g*255.0f);
1220 b = mad(db, inv(a), b*255.0f);
1221 a = mad(da, inv(a), a*255.0f);
Mike Klein37155d42017-12-15 09:55:03 -05001222 // { r, g, b, a} are now in [0,255] (but may be out of gamut)
Mike Klein0cb15892017-10-24 11:07:09 -04001223
Mike Klein37155d42017-12-15 09:55:03 -05001224 // to_unorm() clamps back to gamut. Scaling by 1 since we're already 255-biased.
1225 dst = to_unorm(b, 1, 255)
1226 | to_unorm(g, 1, 255) << 8
1227 | to_unorm(r, 1, 255) << 16
1228 | to_unorm(a, 1, 255) << 24;
Mike Klein0cb15892017-10-24 11:07:09 -04001229 store(ptr, dst, tail);
1230}
1231
Mike Kleinf7729c22017-09-27 11:42:30 -04001232STAGE(clamp_0, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001233 r = max(r, 0);
1234 g = max(g, 0);
1235 b = max(b, 0);
1236 a = max(a, 0);
1237}
1238
Mike Kleinf7729c22017-09-27 11:42:30 -04001239STAGE(clamp_1, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001240 r = min(r, 1.0f);
1241 g = min(g, 1.0f);
1242 b = min(b, 1.0f);
1243 a = min(a, 1.0f);
Mike Kleine1caee12017-02-15 13:31:12 -05001244}
1245
Mike Kleinf7729c22017-09-27 11:42:30 -04001246STAGE(clamp_a, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001247 a = min(a, 1.0f);
Mike Kleine1caee12017-02-15 13:31:12 -05001248 r = min(r, a);
1249 g = min(g, a);
1250 b = min(b, a);
1251}
1252
Mike Kleinf7729c22017-09-27 11:42:30 -04001253STAGE(clamp_a_dst, Ctx::None) {
Mike Reed279091e2017-06-27 16:58:00 -04001254 da = min(da, 1.0f);
1255 dr = min(dr, da);
1256 dg = min(dg, da);
1257 db = min(db, da);
1258}
1259
Mike Kleinf7729c22017-09-27 11:42:30 -04001260STAGE(set_rgb, const float* rgb) {
Mike Kleind9e82252017-02-22 14:17:32 -05001261 r = rgb[0];
1262 g = rgb[1];
1263 b = rgb[2];
1264}
Mike Kleinf7729c22017-09-27 11:42:30 -04001265STAGE(swap_rb, Ctx::None) {
Mike Kleind9e82252017-02-22 14:17:32 -05001266 auto tmp = r;
1267 r = b;
1268 b = tmp;
1269}
Mike Kleinf7729c22017-09-27 11:42:30 -04001270STAGE(invert, Ctx::None) {
Mike Klein50d0d052017-08-08 11:29:01 -04001271 r = inv(r);
1272 g = inv(g);
1273 b = inv(b);
1274 a = inv(a);
1275}
Mike Kleind9e82252017-02-22 14:17:32 -05001276
Mike Kleinf7729c22017-09-27 11:42:30 -04001277STAGE(move_src_dst, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001278 dr = r;
1279 dg = g;
1280 db = b;
1281 da = a;
1282}
Mike Kleinf7729c22017-09-27 11:42:30 -04001283STAGE(move_dst_src, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001284 r = dr;
1285 g = dg;
1286 b = db;
1287 a = da;
1288}
1289
Mike Kleinf7729c22017-09-27 11:42:30 -04001290STAGE(premul, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001291 r = r * a;
1292 g = g * a;
1293 b = b * a;
1294}
Mike Kleinf7729c22017-09-27 11:42:30 -04001295STAGE(premul_dst, Ctx::None) {
Mike Reed883c9bc2017-07-19 10:57:53 -04001296 dr = dr * da;
1297 dg = dg * da;
1298 db = db * da;
1299}
Mike Kleinf7729c22017-09-27 11:42:30 -04001300STAGE(unpremul, Ctx::None) {
Mike Kleina65f2f02017-10-11 13:05:24 -04001301 float inf = bit_cast<float>(0x7f800000);
1302 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0);
Mike Klein08aa88d2017-05-12 12:59:24 -04001303 r *= scale;
1304 g *= scale;
1305 b *= scale;
Mike Kleine1caee12017-02-15 13:31:12 -05001306}
1307
Mike Kleinac568a92018-01-25 09:09:32 -05001308STAGE(force_opaque , Ctx::None) { a = 1; }
1309STAGE(force_opaque_dst, Ctx::None) { da = 1; }
1310
Mike Klein1b9b7d52018-02-27 10:37:40 -05001311SI F from_srgb_(F s) {
Mike Reed279091e2017-06-27 16:58:00 -04001312 auto lo = s * (1/12.92f);
1313 auto hi = mad(s*s, mad(s, 0.3000f, 0.6975f), 0.0025f);
1314 return if_then_else(s < 0.055f, lo, hi);
1315}
1316
Mike Kleinf7729c22017-09-27 11:42:30 -04001317STAGE(from_srgb, Ctx::None) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05001318 r = from_srgb_(r);
1319 g = from_srgb_(g);
1320 b = from_srgb_(b);
Mike Reed279091e2017-06-27 16:58:00 -04001321}
Mike Kleinf7729c22017-09-27 11:42:30 -04001322STAGE(from_srgb_dst, Ctx::None) {
Mike Klein1b9b7d52018-02-27 10:37:40 -05001323 dr = from_srgb_(dr);
1324 dg = from_srgb_(dg);
1325 db = from_srgb_(db);
Mike Kleine1caee12017-02-15 13:31:12 -05001326}
Mike Kleinf7729c22017-09-27 11:42:30 -04001327STAGE(to_srgb, Ctx::None) {
Mike Kleine1caee12017-02-15 13:31:12 -05001328 auto fn = [&](F l) {
Mike Kleinc998f732017-05-24 19:00:47 -04001329 // We tweak c and d for each instruction set to make sure fn(1) is exactly 1.
Mike Kleinb4373512017-10-02 11:43:20 -07001330 #if defined(JUMPER_IS_AVX512)
1331 const float c = 1.130026340485f,
1332 d = 0.141387879848f;
1333 #elif defined(JUMPER_IS_SSE2) || defined(JUMPER_IS_SSE41) || \
Mike Klein106e17a2017-12-12 17:07:49 -05001334 defined(JUMPER_IS_AVX ) || defined(JUMPER_IS_HSW )
Mike Kleinc998f732017-05-24 19:00:47 -04001335 const float c = 1.130048394203f,
1336 d = 0.141357362270f;
Mike Kleind6e12862017-08-28 12:18:26 -04001337 #elif defined(JUMPER_IS_NEON)
Mike Kleinc998f732017-05-24 19:00:47 -04001338 const float c = 1.129999995232f,
1339 d = 0.141381442547f;
1340 #else
1341 const float c = 1.129999995232f,
1342 d = 0.141377761960f;
1343 #endif
Mike Kleinf45e3d72017-05-15 17:36:59 -04001344 F t = rsqrt(l);
1345 auto lo = l * 12.92f;
Mike Kleinc998f732017-05-24 19:00:47 -04001346 auto hi = mad(t, mad(t, -0.0024542345f, 0.013832027f), c)
1347 * rcp(d + t);
Mike Kleinf45e3d72017-05-15 17:36:59 -04001348 return if_then_else(l < 0.00465985f, lo, hi);
Mike Kleine1caee12017-02-15 13:31:12 -05001349 };
1350 r = fn(r);
1351 g = fn(g);
1352 b = fn(b);
1353}
1354
Mike Kleinf7729c22017-09-27 11:42:30 -04001355STAGE(rgb_to_hsl, Ctx::None) {
Mike Kleinfb126fa2017-08-24 13:06:23 -04001356 F mx = max(r,g,b),
1357 mn = min(r,g,b),
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001358 d = mx - mn,
Mike Kleinfe560a82017-05-01 12:56:35 -04001359 d_rcp = 1.0f / d;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001360
Mike Kleinfe560a82017-05-01 12:56:35 -04001361 F h = (1/6.0f) *
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001362 if_then_else(mx == mn, 0,
Mike Kleinfe560a82017-05-01 12:56:35 -04001363 if_then_else(mx == r, (g-b)*d_rcp + if_then_else(g < b, 6.0f, 0),
1364 if_then_else(mx == g, (b-r)*d_rcp + 2.0f,
1365 (r-g)*d_rcp + 4.0f)));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001366
Mike Kleinfe560a82017-05-01 12:56:35 -04001367 F l = (mx + mn) * 0.5f;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001368 F s = if_then_else(mx == mn, 0,
Mike Kleinfe560a82017-05-01 12:56:35 -04001369 d / if_then_else(l > 0.5f, 2.0f-mx-mn, mx+mn));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001370
1371 r = h;
1372 g = s;
1373 b = l;
1374}
Mike Kleinf7729c22017-09-27 11:42:30 -04001375STAGE(hsl_to_rgb, Ctx::None) {
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001376 F h = r,
1377 s = g,
1378 l = b;
1379
Mike Klein5664e652017-05-01 16:01:38 -04001380 F q = l + if_then_else(l >= 0.5f, s - l*s, l*s),
1381 p = 2.0f*l - q;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001382
1383 auto hue_to_rgb = [&](F t) {
Mike Klein879a08a2017-05-01 15:34:01 -04001384 t = fract(t);
Mike Klein5664e652017-05-01 16:01:38 -04001385
1386 F r = p;
1387 r = if_then_else(t >= 4/6.0f, r, p + (q-p)*(4.0f - 6.0f*t));
1388 r = if_then_else(t >= 3/6.0f, r, q);
1389 r = if_then_else(t >= 1/6.0f, r, p + (q-p)*( 6.0f*t));
1390 return r;
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001391 };
1392
Mike Kleinfb11acd2017-05-01 14:22:10 -04001393 r = if_then_else(s == 0, l, hue_to_rgb(h + (1/3.0f)));
1394 g = if_then_else(s == 0, l, hue_to_rgb(h ));
1395 b = if_then_else(s == 0, l, hue_to_rgb(h - (1/3.0f)));
Mike Kleindb1cbcb2017-04-12 08:35:41 -04001396}
1397
Mike Kleinfb126fa2017-08-24 13:06:23 -04001398// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
1399SI F alpha_coverage_from_rgb_coverage(F a, F da, F cr, F cg, F cb) {
1400 return if_then_else(a < da, min(cr,cg,cb)
1401 , max(cr,cg,cb));
1402}
1403
Mike Kleinf7729c22017-09-27 11:42:30 -04001404STAGE(scale_1_float, const float* c) {
1405 r = r * *c;
1406 g = g * *c;
1407 b = b * *c;
1408 a = a * *c;
Mike Kleine3d44212017-02-24 08:21:18 -05001409}
Mike Kleinf7729c22017-09-27 11:42:30 -04001410STAGE(scale_u8, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001411 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Kleine1caee12017-02-15 13:31:12 -05001412
Mike Kleinc31858b2017-03-01 13:07:40 -05001413 auto scales = load<U8>(ptr, tail);
Mike Klein40de6da2017-04-07 13:09:29 -04001414 auto c = from_byte(scales);
Mike Kleine1caee12017-02-15 13:31:12 -05001415
1416 r = r * c;
1417 g = g * c;
1418 b = b * c;
1419 a = a * c;
1420}
Mike Kleinf7729c22017-09-27 11:42:30 -04001421STAGE(scale_565, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001422 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleinfb126fa2017-08-24 13:06:23 -04001423
1424 F cr,cg,cb;
1425 from_565(load<U16>(ptr, tail), &cr, &cg, &cb);
1426
1427 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
1428
1429 r = r * cr;
1430 g = g * cg;
1431 b = b * cb;
1432 a = a * ca;
1433}
Mike Kleine3d44212017-02-24 08:21:18 -05001434
Mike Kleinb9c4a6f2017-04-03 13:54:55 -04001435SI F lerp(F from, F to, F t) {
1436 return mad(to-from, t, from);
1437}
1438
Mike Kleinf7729c22017-09-27 11:42:30 -04001439STAGE(lerp_1_float, const float* c) {
1440 r = lerp(dr, r, *c);
1441 g = lerp(dg, g, *c);
1442 b = lerp(db, b, *c);
1443 a = lerp(da, a, *c);
Mike Kleine3d44212017-02-24 08:21:18 -05001444}
Mike Kleinf7729c22017-09-27 11:42:30 -04001445STAGE(lerp_u8, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001446 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Klein2b767362017-02-22 13:52:40 -05001447
Mike Kleinc31858b2017-03-01 13:07:40 -05001448 auto scales = load<U8>(ptr, tail);
Mike Klein40de6da2017-04-07 13:09:29 -04001449 auto c = from_byte(scales);
Mike Klein2b767362017-02-22 13:52:40 -05001450
1451 r = lerp(dr, r, c);
1452 g = lerp(dg, g, c);
1453 b = lerp(db, b, c);
1454 a = lerp(da, a, c);
1455}
Mike Kleinf7729c22017-09-27 11:42:30 -04001456STAGE(lerp_565, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001457 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleine3d44212017-02-24 08:21:18 -05001458
1459 F cr,cg,cb;
Mike Klein5224f462017-03-07 17:29:54 -05001460 from_565(load<U16>(ptr, tail), &cr, &cg, &cb);
Mike Kleine3d44212017-02-24 08:21:18 -05001461
Mike Kleinfb126fa2017-08-24 13:06:23 -04001462 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
1463
Mike Kleine3d44212017-02-24 08:21:18 -05001464 r = lerp(dr, r, cr);
1465 g = lerp(dg, g, cg);
1466 b = lerp(db, b, cb);
Mike Kleinfb126fa2017-08-24 13:06:23 -04001467 a = lerp(da, a, ca);
Mike Kleine3d44212017-02-24 08:21:18 -05001468}
Mike Kleine1caee12017-02-15 13:31:12 -05001469
Mike Kleinf7729c22017-09-27 11:42:30 -04001470STAGE(load_tables, const SkJumper_LoadTablesCtx* c) {
Mike Kleinb4379132017-10-17 16:06:49 -04001471 auto px = load<U32>((const uint32_t*)c->src + dx, tail);
Mike Klein0aa742f2017-04-27 13:36:57 -04001472 r = gather(c->r, (px ) & 0xff);
1473 g = gather(c->g, (px >> 8) & 0xff);
1474 b = gather(c->b, (px >> 16) & 0xff);
Mike Kleinfe560a82017-05-01 12:56:35 -04001475 a = cast( (px >> 24)) * (1/255.0f);
Mike Kleine1caee12017-02-15 13:31:12 -05001476}
Mike Kleinf7729c22017-09-27 11:42:30 -04001477STAGE(load_tables_u16_be, const SkJumper_LoadTablesCtx* c) {
Mike Kleinb4379132017-10-17 16:06:49 -04001478 auto ptr = (const uint16_t*)c->src + 4*dx;
Mike Kleina3735cd2017-04-17 13:19:05 -04001479
1480 U16 R,G,B,A;
1481 load4(ptr, tail, &R,&G,&B,&A);
1482
Mike Klein0aa742f2017-04-27 13:36:57 -04001483 // c->src is big-endian, so & 0xff grabs the 8 most signficant bits.
1484 r = gather(c->r, expand(R) & 0xff);
1485 g = gather(c->g, expand(G) & 0xff);
1486 b = gather(c->b, expand(B) & 0xff);
Mike Kleinfe560a82017-05-01 12:56:35 -04001487 a = (1/65535.0f) * cast(expand(bswap(A)));
Mike Kleina3735cd2017-04-17 13:19:05 -04001488}
Mike Kleinf7729c22017-09-27 11:42:30 -04001489STAGE(load_tables_rgb_u16_be, const SkJumper_LoadTablesCtx* c) {
Mike Kleinb4379132017-10-17 16:06:49 -04001490 auto ptr = (const uint16_t*)c->src + 3*dx;
Mike Kleina3735cd2017-04-17 13:19:05 -04001491
1492 U16 R,G,B;
1493 load3(ptr, tail, &R,&G,&B);
1494
Mike Klein0aa742f2017-04-27 13:36:57 -04001495 // c->src is big-endian, so & 0xff grabs the 8 most signficant bits.
1496 r = gather(c->r, expand(R) & 0xff);
1497 g = gather(c->g, expand(G) & 0xff);
1498 b = gather(c->b, expand(B) & 0xff);
Mike Kleinfe560a82017-05-01 12:56:35 -04001499 a = 1.0f;
Mike Kleina3735cd2017-04-17 13:19:05 -04001500}
Mike Kleine1caee12017-02-15 13:31:12 -05001501
Mike Kleinf7729c22017-09-27 11:42:30 -04001502STAGE(byte_tables, const void* ctx) { // TODO: rename Tables SkJumper_ByteTablesCtx
Mike Klein40de6da2017-04-07 13:09:29 -04001503 struct Tables { const uint8_t *r, *g, *b, *a; };
1504 auto tables = (const Tables*)ctx;
1505
Mike Klein37155d42017-12-15 09:55:03 -05001506 r = from_byte(gather(tables->r, to_unorm(r, 255)));
1507 g = from_byte(gather(tables->g, to_unorm(g, 255)));
1508 b = from_byte(gather(tables->b, to_unorm(b, 255)));
1509 a = from_byte(gather(tables->a, to_unorm(a, 255)));
Mike Klein40de6da2017-04-07 13:09:29 -04001510}
1511
Brian Osman2c711332018-03-06 14:28:41 -05001512STAGE(byte_tables_rgb, const SkJumper_ByteTablesRGBCtx* ctx) {
1513 int scale = ctx->n - 1;
1514 r = from_byte(gather(ctx->r, to_unorm(r, scale)));
1515 g = from_byte(gather(ctx->g, to_unorm(g, scale)));
1516 b = from_byte(gather(ctx->b, to_unorm(b, scale)));
Mike Klein40de6da2017-04-07 13:09:29 -04001517}
1518
Mike Kleinc7d9c0b2017-04-17 14:43:59 -04001519SI F table(F v, const SkJumper_TableCtx* ctx) {
Mike Klein37155d42017-12-15 09:55:03 -05001520 return gather(ctx->table, to_unorm(v, ctx->size - 1));
Mike Kleinc7d9c0b2017-04-17 14:43:59 -04001521}
Mike Kleinf7729c22017-09-27 11:42:30 -04001522STAGE(table_r, const SkJumper_TableCtx* ctx) { r = table(r, ctx); }
1523STAGE(table_g, const SkJumper_TableCtx* ctx) { g = table(g, ctx); }
1524STAGE(table_b, const SkJumper_TableCtx* ctx) { b = table(b, ctx); }
1525STAGE(table_a, const SkJumper_TableCtx* ctx) { a = table(a, ctx); }
Mike Kleinc7d9c0b2017-04-17 14:43:59 -04001526
Mike Klein44375172017-04-17 19:32:05 -04001527SI F parametric(F v, const SkJumper_ParametricTransferFunction* ctx) {
1528 F r = if_then_else(v <= ctx->D, mad(ctx->C, v, ctx->F)
1529 , approx_powf(mad(ctx->A, v, ctx->B), ctx->G) + ctx->E);
Mike Kleinfe560a82017-05-01 12:56:35 -04001530 return min(max(r, 0), 1.0f); // Clamp to [0,1], with argument order mattering to handle NaN.
Mike Klein44375172017-04-17 19:32:05 -04001531}
Mike Kleinf7729c22017-09-27 11:42:30 -04001532STAGE(parametric_r, const SkJumper_ParametricTransferFunction* ctx) { r = parametric(r, ctx); }
1533STAGE(parametric_g, const SkJumper_ParametricTransferFunction* ctx) { g = parametric(g, ctx); }
1534STAGE(parametric_b, const SkJumper_ParametricTransferFunction* ctx) { b = parametric(b, ctx); }
1535STAGE(parametric_a, const SkJumper_ParametricTransferFunction* ctx) { a = parametric(a, ctx); }
Mike Klein44375172017-04-17 19:32:05 -04001536
Mike Kleinf7729c22017-09-27 11:42:30 -04001537STAGE(gamma, const float* G) {
1538 r = approx_powf(r, *G);
1539 g = approx_powf(g, *G);
1540 b = approx_powf(b, *G);
Mike Kleina07e4302017-08-09 13:51:35 -04001541}
Mike Kleinc38cce62017-12-13 11:01:31 -05001542STAGE(gamma_dst, const float* G) {
1543 dr = approx_powf(dr, *G);
1544 dg = approx_powf(dg, *G);
1545 db = approx_powf(db, *G);
1546}
Mike Kleina07e4302017-08-09 13:51:35 -04001547
Mike Kleinf7729c22017-09-27 11:42:30 -04001548STAGE(lab_to_xyz, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001549 F L = r * 100.0f,
1550 A = g * 255.0f - 128.0f,
1551 B = b * 255.0f - 128.0f;
Mike Klein4e3e9f82017-04-20 11:04:29 -04001552
Mike Kleinfe560a82017-05-01 12:56:35 -04001553 F Y = (L + 16.0f) * (1/116.0f),
1554 X = Y + A*(1/500.0f),
1555 Z = Y - B*(1/200.0f);
Mike Klein4e3e9f82017-04-20 11:04:29 -04001556
Mike Kleinfe560a82017-05-01 12:56:35 -04001557 X = if_then_else(X*X*X > 0.008856f, X*X*X, (X - (16/116.0f)) * (1/7.787f));
1558 Y = if_then_else(Y*Y*Y > 0.008856f, Y*Y*Y, (Y - (16/116.0f)) * (1/7.787f));
1559 Z = if_then_else(Z*Z*Z > 0.008856f, Z*Z*Z, (Z - (16/116.0f)) * (1/7.787f));
Mike Klein4e3e9f82017-04-20 11:04:29 -04001560
1561 // Adjust to D50 illuminant.
Mike Kleinfe560a82017-05-01 12:56:35 -04001562 r = X * 0.96422f;
1563 g = Y ;
1564 b = Z * 0.82521f;
Mike Klein4e3e9f82017-04-20 11:04:29 -04001565}
1566
Mike Kleinf7729c22017-09-27 11:42:30 -04001567STAGE(load_a8, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001568 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Klein420e38f2017-02-24 09:05:14 -05001569
1570 r = g = b = 0.0f;
Mike Klein40de6da2017-04-07 13:09:29 -04001571 a = from_byte(load<U8>(ptr, tail));
Mike Klein420e38f2017-02-24 09:05:14 -05001572}
Mike Kleinf7729c22017-09-27 11:42:30 -04001573STAGE(load_a8_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001574 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001575
1576 dr = dg = db = 0.0f;
1577 da = from_byte(load<U8>(ptr, tail));
1578}
Mike Kleinf7729c22017-09-27 11:42:30 -04001579STAGE(gather_a8, const SkJumper_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001580 const uint8_t* ptr;
1581 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1582 r = g = b = 0.0f;
Mike Klein40de6da2017-04-07 13:09:29 -04001583 a = from_byte(gather(ptr, ix));
Mike Klein21bd3e42017-04-06 16:32:29 -04001584}
Mike Kleinf7729c22017-09-27 11:42:30 -04001585STAGE(store_a8, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001586 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
Mike Klein420e38f2017-02-24 09:05:14 -05001587
Mike Klein37155d42017-12-15 09:55:03 -05001588 U8 packed = pack(pack(to_unorm(a, 255)));
Mike Kleinc31858b2017-03-01 13:07:40 -05001589 store(ptr, packed, tail);
Mike Klein420e38f2017-02-24 09:05:14 -05001590}
1591
Mike Kleinf7729c22017-09-27 11:42:30 -04001592STAGE(load_g8, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001593 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Kleinf809fef2017-03-31 13:52:45 -04001594
Mike Klein40de6da2017-04-07 13:09:29 -04001595 r = g = b = from_byte(load<U8>(ptr, tail));
Mike Kleinfe560a82017-05-01 12:56:35 -04001596 a = 1.0f;
Mike Kleinf809fef2017-03-31 13:52:45 -04001597}
Mike Kleinf7729c22017-09-27 11:42:30 -04001598STAGE(load_g8_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001599 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001600
1601 dr = dg = db = from_byte(load<U8>(ptr, tail));
1602 da = 1.0f;
1603}
Mike Kleinf7729c22017-09-27 11:42:30 -04001604STAGE(gather_g8, const SkJumper_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001605 const uint8_t* ptr;
1606 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
Mike Klein40de6da2017-04-07 13:09:29 -04001607 r = g = b = from_byte(gather(ptr, ix));
Mike Kleinfe560a82017-05-01 12:56:35 -04001608 a = 1.0f;
Mike Klein21bd3e42017-04-06 16:32:29 -04001609}
Mike Kleinf809fef2017-03-31 13:52:45 -04001610
Mike Kleinf7729c22017-09-27 11:42:30 -04001611STAGE(load_565, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001612 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Klein3f81f372017-02-23 13:03:57 -05001613
Mike Klein5224f462017-03-07 17:29:54 -05001614 from_565(load<U16>(ptr, tail), &r,&g,&b);
Mike Kleinfe560a82017-05-01 12:56:35 -04001615 a = 1.0f;
Mike Klein3f81f372017-02-23 13:03:57 -05001616}
Mike Kleinf7729c22017-09-27 11:42:30 -04001617STAGE(load_565_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001618 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001619
1620 from_565(load<U16>(ptr, tail), &dr,&dg,&db);
1621 da = 1.0f;
1622}
Mike Kleinf7729c22017-09-27 11:42:30 -04001623STAGE(gather_565, const SkJumper_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001624 const uint16_t* ptr;
1625 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1626 from_565(gather(ptr, ix), &r,&g,&b);
Mike Kleinfe560a82017-05-01 12:56:35 -04001627 a = 1.0f;
Mike Klein21bd3e42017-04-06 16:32:29 -04001628}
Mike Kleinf7729c22017-09-27 11:42:30 -04001629STAGE(store_565, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001630 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
Mike Klein3f81f372017-02-23 13:03:57 -05001631
Mike Klein37155d42017-12-15 09:55:03 -05001632 U16 px = pack( to_unorm(r, 31) << 11
1633 | to_unorm(g, 63) << 5
1634 | to_unorm(b, 31) );
Mike Kleinc31858b2017-03-01 13:07:40 -05001635 store(ptr, px, tail);
Mike Klein3f81f372017-02-23 13:03:57 -05001636}
1637
Mike Kleinf7729c22017-09-27 11:42:30 -04001638STAGE(load_4444, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001639 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Kleinf809fef2017-03-31 13:52:45 -04001640 from_4444(load<U16>(ptr, tail), &r,&g,&b,&a);
1641}
Mike Kleinf7729c22017-09-27 11:42:30 -04001642STAGE(load_4444_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001643 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001644 from_4444(load<U16>(ptr, tail), &dr,&dg,&db,&da);
1645}
Mike Kleinf7729c22017-09-27 11:42:30 -04001646STAGE(gather_4444, const SkJumper_GatherCtx* ctx) {
Mike Klein21bd3e42017-04-06 16:32:29 -04001647 const uint16_t* ptr;
1648 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1649 from_4444(gather(ptr, ix), &r,&g,&b,&a);
1650}
Mike Kleinf7729c22017-09-27 11:42:30 -04001651STAGE(store_4444, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001652 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
Mike Klein37155d42017-12-15 09:55:03 -05001653 U16 px = pack( to_unorm(r, 15) << 12
1654 | to_unorm(g, 15) << 8
1655 | to_unorm(b, 15) << 4
1656 | to_unorm(a, 15) );
Mike Kleinf809fef2017-03-31 13:52:45 -04001657 store(ptr, px, tail);
1658}
1659
Mike Kleinf7729c22017-09-27 11:42:30 -04001660STAGE(load_8888, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001661 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Kleindec4ea82017-04-06 15:04:05 -04001662 from_8888(load<U32>(ptr, tail), &r,&g,&b,&a);
1663}
Mike Kleinf7729c22017-09-27 11:42:30 -04001664STAGE(load_8888_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001665 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001666 from_8888(load<U32>(ptr, tail), &dr,&dg,&db,&da);
1667}
Mike Kleinf7729c22017-09-27 11:42:30 -04001668STAGE(gather_8888, const SkJumper_GatherCtx* ctx) {
Mike Kleindec4ea82017-04-06 15:04:05 -04001669 const uint32_t* ptr;
1670 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1671 from_8888(gather(ptr, ix), &r,&g,&b,&a);
Mike Kleine1caee12017-02-15 13:31:12 -05001672}
Mike Kleinf7729c22017-09-27 11:42:30 -04001673STAGE(store_8888, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001674 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
Mike Klein3b92b692017-07-18 11:30:25 -04001675
Mike Klein37155d42017-12-15 09:55:03 -05001676 U32 px = to_unorm(r, 255)
1677 | to_unorm(g, 255) << 8
1678 | to_unorm(b, 255) << 16
1679 | to_unorm(a, 255) << 24;
Mike Klein3b92b692017-07-18 11:30:25 -04001680 store(ptr, px, tail);
1681}
1682
Mike Kleinf7729c22017-09-27 11:42:30 -04001683STAGE(load_bgra, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001684 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Klein111f8a92017-06-27 15:56:32 -04001685 from_8888(load<U32>(ptr, tail), &b,&g,&r,&a);
1686}
Mike Kleinf7729c22017-09-27 11:42:30 -04001687STAGE(load_bgra_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001688 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
Mike Klein111f8a92017-06-27 15:56:32 -04001689 from_8888(load<U32>(ptr, tail), &db,&dg,&dr,&da);
1690}
Mike Kleinf7729c22017-09-27 11:42:30 -04001691STAGE(gather_bgra, const SkJumper_GatherCtx* ctx) {
Mike Klein111f8a92017-06-27 15:56:32 -04001692 const uint32_t* ptr;
1693 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1694 from_8888(gather(ptr, ix), &b,&g,&r,&a);
1695}
Mike Kleinf7729c22017-09-27 11:42:30 -04001696STAGE(store_bgra, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001697 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
Mike Klein111f8a92017-06-27 15:56:32 -04001698
Mike Klein37155d42017-12-15 09:55:03 -05001699 U32 px = to_unorm(b, 255)
1700 | to_unorm(g, 255) << 8
1701 | to_unorm(r, 255) << 16
1702 | to_unorm(a, 255) << 24;
Mike Klein111f8a92017-06-27 15:56:32 -04001703 store(ptr, px, tail);
1704}
1705
Mike Kleinac568a92018-01-25 09:09:32 -05001706STAGE(load_1010102, const SkJumper_MemoryCtx* ctx) {
1707 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
1708 from_1010102(load<U32>(ptr, tail), &r,&g,&b,&a);
1709}
1710STAGE(load_1010102_dst, const SkJumper_MemoryCtx* ctx) {
1711 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
1712 from_1010102(load<U32>(ptr, tail), &dr,&dg,&db,&da);
1713}
1714STAGE(gather_1010102, const SkJumper_GatherCtx* ctx) {
1715 const uint32_t* ptr;
1716 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1717 from_1010102(gather(ptr, ix), &r,&g,&b,&a);
1718}
1719STAGE(store_1010102, const SkJumper_MemoryCtx* ctx) {
1720 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
1721
1722 U32 px = to_unorm(r, 1023)
1723 | to_unorm(g, 1023) << 10
1724 | to_unorm(b, 1023) << 20
1725 | to_unorm(a, 3) << 30;
1726 store(ptr, px, tail);
1727}
1728
Mike Kleinf7729c22017-09-27 11:42:30 -04001729STAGE(load_f16, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001730 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
Mike Kleine1caee12017-02-15 13:31:12 -05001731
Mike Klein114e6b32017-04-03 22:21:15 -04001732 U16 R,G,B,A;
Mike Kleinfa6eb912017-04-05 10:18:27 -04001733 load4((const uint16_t*)ptr,tail, &R,&G,&B,&A);
Mike Klein114e6b32017-04-03 22:21:15 -04001734 r = from_half(R);
1735 g = from_half(G);
1736 b = from_half(B);
1737 a = from_half(A);
Mike Kleine1caee12017-02-15 13:31:12 -05001738}
Mike Kleinf7729c22017-09-27 11:42:30 -04001739STAGE(load_f16_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001740 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001741
1742 U16 R,G,B,A;
1743 load4((const uint16_t*)ptr,tail, &R,&G,&B,&A);
1744 dr = from_half(R);
1745 dg = from_half(G);
1746 db = from_half(B);
1747 da = from_half(A);
1748}
Mike Kleinf7729c22017-09-27 11:42:30 -04001749STAGE(gather_f16, const SkJumper_GatherCtx* ctx) {
Mike Klein5f055f02017-04-06 20:02:11 -04001750 const uint64_t* ptr;
1751 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
1752 auto px = gather(ptr, ix);
1753
1754 U16 R,G,B,A;
1755 load4((const uint16_t*)&px,0, &R,&G,&B,&A);
1756 r = from_half(R);
1757 g = from_half(G);
1758 b = from_half(B);
1759 a = from_half(A);
1760}
Mike Kleinf7729c22017-09-27 11:42:30 -04001761STAGE(store_f16, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001762 auto ptr = ptr_at_xy<uint64_t>(ctx, dx,dy);
Mike Kleinfa6eb912017-04-05 10:18:27 -04001763 store4((uint16_t*)ptr,tail, to_half(r)
1764 , to_half(g)
1765 , to_half(b)
1766 , to_half(a));
Mike Kleine1caee12017-02-15 13:31:12 -05001767}
1768
Mike Kleinf7729c22017-09-27 11:42:30 -04001769STAGE(load_u16_be, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001770 auto ptr = ptr_at_xy<const uint16_t>(ctx, 4*dx,dy);
Mike Klein3146bb92017-04-05 14:45:02 -04001771
1772 U16 R,G,B,A;
Mike Kleinb3821732017-04-17 10:58:05 -04001773 load4(ptr,tail, &R,&G,&B,&A);
Mike Klein3146bb92017-04-05 14:45:02 -04001774
Mike Kleinfe560a82017-05-01 12:56:35 -04001775 r = (1/65535.0f) * cast(expand(bswap(R)));
1776 g = (1/65535.0f) * cast(expand(bswap(G)));
1777 b = (1/65535.0f) * cast(expand(bswap(B)));
1778 a = (1/65535.0f) * cast(expand(bswap(A)));
Mike Klein3146bb92017-04-05 14:45:02 -04001779}
Mike Kleinf7729c22017-09-27 11:42:30 -04001780STAGE(load_rgb_u16_be, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001781 auto ptr = ptr_at_xy<const uint16_t>(ctx, 3*dx,dy);
Mike Kleinb3821732017-04-17 10:58:05 -04001782
1783 U16 R,G,B;
1784 load3(ptr,tail, &R,&G,&B);
1785
Mike Kleinfe560a82017-05-01 12:56:35 -04001786 r = (1/65535.0f) * cast(expand(bswap(R)));
1787 g = (1/65535.0f) * cast(expand(bswap(G)));
1788 b = (1/65535.0f) * cast(expand(bswap(B)));
1789 a = 1.0f;
Mike Kleinb3821732017-04-17 10:58:05 -04001790}
Mike Kleinf7729c22017-09-27 11:42:30 -04001791STAGE(store_u16_be, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001792 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,dy);
Mike Klein3146bb92017-04-05 14:45:02 -04001793
Mike Klein37155d42017-12-15 09:55:03 -05001794 U16 R = bswap(pack(to_unorm(r, 65535))),
1795 G = bswap(pack(to_unorm(g, 65535))),
1796 B = bswap(pack(to_unorm(b, 65535))),
1797 A = bswap(pack(to_unorm(a, 65535)));
Mike Klein3146bb92017-04-05 14:45:02 -04001798
Mike Kleinb3821732017-04-17 10:58:05 -04001799 store4(ptr,tail, R,G,B,A);
Mike Klein3146bb92017-04-05 14:45:02 -04001800}
1801
Mike Kleinf7729c22017-09-27 11:42:30 -04001802STAGE(load_f32, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001803 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,dy);
Mike Klein14987eb2017-04-06 10:22:26 -04001804 load4(ptr,tail, &r,&g,&b,&a);
1805}
Mike Kleinf7729c22017-09-27 11:42:30 -04001806STAGE(load_f32_dst, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001807 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,dy);
Mike Reed279091e2017-06-27 16:58:00 -04001808 load4(ptr,tail, &dr,&dg,&db,&da);
1809}
Mike Kleinf7729c22017-09-27 11:42:30 -04001810STAGE(store_f32, const SkJumper_MemoryCtx* ctx) {
Mike Kleinb4379132017-10-17 16:06:49 -04001811 auto ptr = ptr_at_xy<float>(ctx, 4*dx,dy);
Mike Kleinfa6eb912017-04-05 10:18:27 -04001812 store4(ptr,tail, r,g,b,a);
Mike Klein94fc0fe2017-03-03 14:05:32 -05001813}
1814
Mike Reed51e46d52017-06-23 14:21:25 -04001815SI F exclusive_repeat(F v, const SkJumper_TileCtx* ctx) {
Mike Kleinf3b4e162017-09-22 15:32:59 -04001816 return v - floor_(v*ctx->invScale)*ctx->scale;
Mike Klein0cc60b82017-06-22 11:00:17 -07001817}
Mike Reed51e46d52017-06-23 14:21:25 -04001818SI F exclusive_mirror(F v, const SkJumper_TileCtx* ctx) {
1819 auto limit = ctx->scale;
1820 auto invLimit = ctx->invScale;
Mike Kleinf3b4e162017-09-22 15:32:59 -04001821 return abs_( (v-limit) - (limit+limit)*floor_((v-limit)*(invLimit*0.5f)) - limit );
Mike Klein0cc60b82017-06-22 11:00:17 -07001822}
Mike Kleinf3b4e162017-09-22 15:32:59 -04001823// Tile x or y to [0,limit) == [0,limit - 1 ulp] (think, sampling from images).
1824// The gather stages will hard clamp the output of these stages to [0,limit)...
1825// we just need to do the basic repeat or mirroring.
Mike Kleinf7729c22017-09-27 11:42:30 -04001826STAGE(repeat_x, const SkJumper_TileCtx* ctx) { r = exclusive_repeat(r, ctx); }
1827STAGE(repeat_y, const SkJumper_TileCtx* ctx) { g = exclusive_repeat(g, ctx); }
1828STAGE(mirror_x, const SkJumper_TileCtx* ctx) { r = exclusive_mirror(r, ctx); }
1829STAGE(mirror_y, const SkJumper_TileCtx* ctx) { g = exclusive_mirror(g, ctx); }
Mike Klein0cc60b82017-06-22 11:00:17 -07001830
Mike Kleina3b88952017-10-05 13:21:31 -04001831// Clamp x to [0,1], both sides inclusive (think, gradients).
1832// Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
1833SI F clamp_01(F v) { return min(max(0, v), 1); }
1834
1835STAGE( clamp_x_1, Ctx::None) { r = clamp_01(r); }
1836STAGE(repeat_x_1, Ctx::None) { r = clamp_01(r - floor_(r)); }
1837STAGE(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 -04001838
Mike Reeddfc0e912018-02-16 12:40:18 -05001839// Decal stores a 32bit mask after checking the coordinate (x and/or y) against its domain:
1840// mask == 0x00000000 if the coordinate(s) are out of bounds
1841// mask == 0xFFFFFFFF if the coordinate(s) are in bounds
1842// After the gather stage, the r,g,b,a values are AND'd with this mask, setting them to 0
1843// if either of the coordinates were out of bounds.
1844
1845STAGE(decal_x, SkJumper_DecalTileCtx* ctx) {
1846 auto w = ctx->limit_x;
1847 unaligned_store(ctx->mask, cond_to_mask((0 <= r) & (r < w)));
1848}
1849STAGE(decal_y, SkJumper_DecalTileCtx* ctx) {
1850 auto h = ctx->limit_y;
1851 unaligned_store(ctx->mask, cond_to_mask((0 <= g) & (g < h)));
1852}
1853STAGE(decal_x_and_y, SkJumper_DecalTileCtx* ctx) {
1854 auto w = ctx->limit_x;
1855 auto h = ctx->limit_y;
1856 unaligned_store(ctx->mask,
1857 cond_to_mask((0 <= r) & (r < w) & (0 <= g) & (g < h)));
1858}
1859STAGE(check_decal_mask, SkJumper_DecalTileCtx* ctx) {
1860 auto mask = unaligned_load<U32>(ctx->mask);
1861 r = bit_cast<F>( bit_cast<U32>(r) & mask );
1862 g = bit_cast<F>( bit_cast<U32>(g) & mask );
1863 b = bit_cast<F>( bit_cast<U32>(b) & mask );
1864 a = bit_cast<F>( bit_cast<U32>(a) & mask );
1865}
1866
Mike Kleinf7729c22017-09-27 11:42:30 -04001867STAGE(luminance_to_alpha, Ctx::None) {
Mike Kleinfe560a82017-05-01 12:56:35 -04001868 a = r*0.2126f + g*0.7152f + b*0.0722f;
Mike Kleine9ed07d2017-03-07 12:28:11 -05001869 r = g = b = 0;
1870}
1871
Mike Kleinf7729c22017-09-27 11:42:30 -04001872STAGE(matrix_translate, const float* m) {
Mike Reed7aad8cc2017-07-05 12:33:06 -04001873 r += m[0];
1874 g += m[1];
1875}
Mike Kleinf7729c22017-09-27 11:42:30 -04001876STAGE(matrix_scale_translate, const float* m) {
Mike Kleinf04ff762017-10-20 15:50:12 -04001877 r = mad(r,m[0], m[2]);
1878 g = mad(g,m[1], m[3]);
Mike Reed7aad8cc2017-07-05 12:33:06 -04001879}
Mike Kleinf7729c22017-09-27 11:42:30 -04001880STAGE(matrix_2x3, const float* m) {
Mike Kleinb8d52752017-02-16 10:21:29 -05001881 auto R = mad(r,m[0], mad(g,m[2], m[4])),
1882 G = mad(r,m[1], mad(g,m[3], m[5]));
Mike Kleine1caee12017-02-15 13:31:12 -05001883 r = R;
1884 g = G;
1885}
Mike Kleinf7729c22017-09-27 11:42:30 -04001886STAGE(matrix_3x4, const float* m) {
Mike Kleinb8d52752017-02-16 10:21:29 -05001887 auto R = mad(r,m[0], mad(g,m[3], mad(b,m[6], m[ 9]))),
1888 G = mad(r,m[1], mad(g,m[4], mad(b,m[7], m[10]))),
1889 B = mad(r,m[2], mad(g,m[5], mad(b,m[8], m[11])));
Mike Kleine1caee12017-02-15 13:31:12 -05001890 r = R;
1891 g = G;
1892 b = B;
1893}
Mike Kleinf7729c22017-09-27 11:42:30 -04001894STAGE(matrix_4x5, const float* m) {
Mike Kleine9ed07d2017-03-07 12:28:11 -05001895 auto R = mad(r,m[0], mad(g,m[4], mad(b,m[ 8], mad(a,m[12], m[16])))),
1896 G = mad(r,m[1], mad(g,m[5], mad(b,m[ 9], mad(a,m[13], m[17])))),
1897 B = mad(r,m[2], mad(g,m[6], mad(b,m[10], mad(a,m[14], m[18])))),
1898 A = mad(r,m[3], mad(g,m[7], mad(b,m[11], mad(a,m[15], m[19]))));
1899 r = R;
1900 g = G;
1901 b = B;
1902 a = A;
1903}
Mike Kleinf7729c22017-09-27 11:42:30 -04001904STAGE(matrix_4x3, const float* m) {
Mike Reed02640952017-05-19 15:32:13 -04001905 auto X = r,
1906 Y = g;
1907
1908 r = mad(X, m[0], mad(Y, m[4], m[ 8]));
1909 g = mad(X, m[1], mad(Y, m[5], m[ 9]));
1910 b = mad(X, m[2], mad(Y, m[6], m[10]));
1911 a = mad(X, m[3], mad(Y, m[7], m[11]));
1912}
Mike Kleinf7729c22017-09-27 11:42:30 -04001913STAGE(matrix_perspective, const float* m) {
Mike Klein11d2df02017-02-24 11:51:36 -05001914 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
Mike Klein11d2df02017-02-24 11:51:36 -05001915 auto R = mad(r,m[0], mad(g,m[1], m[2])),
1916 G = mad(r,m[3], mad(g,m[4], m[5])),
1917 Z = mad(r,m[6], mad(g,m[7], m[8]));
1918 r = R * rcp(Z);
1919 g = G * rcp(Z);
1920}
Mike Kleine1caee12017-02-15 13:31:12 -05001921
Herb Derby4de13042017-05-15 10:49:39 -04001922SI void gradient_lookup(const SkJumper_GradientCtx* c, U32 idx, F t,
1923 F* r, F* g, F* b, F* a) {
1924 F fr, br, fg, bg, fb, bb, fa, ba;
Mike Klein106e17a2017-12-12 17:07:49 -05001925#if defined(JUMPER_IS_HSW) || defined(JUMPER_IS_AVX512)
Herb Derby4de13042017-05-15 10:49:39 -04001926 if (c->stopCount <=8) {
1927 fr = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), idx);
1928 br = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), idx);
1929 fg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), idx);
1930 bg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), idx);
1931 fb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), idx);
1932 bb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), idx);
1933 fa = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), idx);
1934 ba = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), idx);
1935 } else
1936#endif
1937 {
1938 fr = gather(c->fs[0], idx);
1939 br = gather(c->bs[0], idx);
1940 fg = gather(c->fs[1], idx);
1941 bg = gather(c->bs[1], idx);
1942 fb = gather(c->fs[2], idx);
1943 bb = gather(c->bs[2], idx);
1944 fa = gather(c->fs[3], idx);
1945 ba = gather(c->bs[3], idx);
Herb Derby7b4202d2017-04-10 10:52:34 -04001946 }
1947
Herb Derby4de13042017-05-15 10:49:39 -04001948 *r = mad(t, fr, br);
1949 *g = mad(t, fg, bg);
1950 *b = mad(t, fb, bb);
1951 *a = mad(t, fa, ba);
1952}
1953
Mike Kleinf7729c22017-09-27 11:42:30 -04001954STAGE(evenly_spaced_gradient, const SkJumper_GradientCtx* c) {
Herb Derby4de13042017-05-15 10:49:39 -04001955 auto t = r;
1956 auto idx = trunc_(t * (c->stopCount-1));
1957 gradient_lookup(c, idx, t, &r, &g, &b, &a);
1958}
1959
Mike Kleinf7729c22017-09-27 11:42:30 -04001960STAGE(gradient, const SkJumper_GradientCtx* c) {
Herb Derby4de13042017-05-15 10:49:39 -04001961 auto t = r;
1962 U32 idx = 0;
1963
1964 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
1965 for (size_t i = 1; i < c->stopCount; i++) {
1966 idx += if_then_else(t >= c->ts[i], U32(1), U32(0));
1967 }
1968
1969 gradient_lookup(c, idx, t, &r, &g, &b, &a);
Herb Derby7b4202d2017-04-10 10:52:34 -04001970}
1971
Mike Kleinf7729c22017-09-27 11:42:30 -04001972STAGE(evenly_spaced_2_stop_gradient, const void* ctx) {
Mike Kleine95a62f2017-11-01 13:31:28 -04001973 // TODO: Rename Ctx SkJumper_EvenlySpaced2StopGradientCtx.
Herb Derby7b4202d2017-04-10 10:52:34 -04001974 struct Ctx { float f[4], b[4]; };
Mike Klein8a823fa2017-04-05 17:29:26 -04001975 auto c = (const Ctx*)ctx;
Mike Kleine1caee12017-02-15 13:31:12 -05001976
1977 auto t = r;
Herb Derby7b4202d2017-04-10 10:52:34 -04001978 r = mad(t, c->f[0], c->b[0]);
1979 g = mad(t, c->f[1], c->b[1]);
1980 b = mad(t, c->f[2], c->b[2]);
1981 a = mad(t, c->f[3], c->b[3]);
Mike Kleine1caee12017-02-15 13:31:12 -05001982}
Mike Klein0a904492017-04-12 12:52:48 -04001983
Mike Kleinf7729c22017-09-27 11:42:30 -04001984STAGE(xy_to_unit_angle, Ctx::None) {
Herb Derby7eb86982017-05-02 19:04:39 -04001985 F X = r,
1986 Y = g;
1987 F xabs = abs_(X),
1988 yabs = abs_(Y);
1989
1990 F slope = min(xabs, yabs)/max(xabs, yabs);
1991 F s = slope * slope;
1992
1993 // Use a 7th degree polynomial to approximate atan.
1994 // This was generated using sollya.gforge.inria.fr.
1995 // A float optimized polynomial was generated using the following command.
1996 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
1997 F phi = slope
1998 * (0.15912117063999176025390625f + s
1999 * (-5.185396969318389892578125e-2f + s
2000 * (2.476101927459239959716796875e-2f + s
2001 * (-7.0547382347285747528076171875e-3f))));
2002
2003 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
2004 phi = if_then_else(X < 0.0f , 1.0f/2.0f - phi, phi);
2005 phi = if_then_else(Y < 0.0f , 1.0f - phi , phi);
2006 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
2007 r = phi;
2008}
2009
Mike Kleinf7729c22017-09-27 11:42:30 -04002010STAGE(xy_to_radius, Ctx::None) {
Herb Derby090fbf82017-05-08 15:10:36 -04002011 F X2 = r * r,
2012 Y2 = g * g;
Mike Kleinfd35c742017-05-15 15:55:54 -04002013 r = sqrt_(X2 + Y2);
Herb Derby090fbf82017-05-08 15:10:36 -04002014}
2015
Yuqian Lid208a882018-01-04 10:08:42 -05002016// Please see https://skia.org/dev/design/conical for how our 2pt conical shader works.
2017
2018STAGE(negate_x, Ctx::None) { r = -r; }
2019
2020STAGE(xy_to_2pt_conical_strip, const SkJumper_2PtConicalCtx* ctx) {
2021 F x = r, y = g, &t = r;
2022 t = x + sqrt_(ctx->fP0 - y*y); // ctx->fP0 = r0 * r0
2023}
2024
2025STAGE(xy_to_2pt_conical_focal_on_circle, Ctx::None) {
2026 F x = r, y = g, &t = r;
2027 t = x + y*y / x; // (x^2 + y^2) / x
2028}
2029
2030STAGE(xy_to_2pt_conical_well_behaved, const SkJumper_2PtConicalCtx* ctx) {
2031 F x = r, y = g, &t = r;
2032 t = sqrt_(x*x + y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2033}
2034
2035STAGE(xy_to_2pt_conical_greater, const SkJumper_2PtConicalCtx* ctx) {
2036 F x = r, y = g, &t = r;
2037 t = sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2038}
2039
2040STAGE(xy_to_2pt_conical_smaller, const SkJumper_2PtConicalCtx* ctx) {
2041 F x = r, y = g, &t = r;
2042 t = -sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
2043}
2044
2045STAGE(alter_2pt_conical_compensate_focal, const SkJumper_2PtConicalCtx* ctx) {
2046 F& t = r;
2047 t = t + ctx->fP1; // ctx->fP1 = f
2048}
2049
2050STAGE(alter_2pt_conical_unswap, Ctx::None) {
2051 F& t = r;
2052 t = 1 - t;
2053}
2054
2055STAGE(mask_2pt_conical_nan, SkJumper_2PtConicalCtx* c) {
2056 F& t = r;
2057 auto is_degenerate = (t != t); // NaN
2058 t = if_then_else(is_degenerate, F(0), t);
Mike Reeddfc0e912018-02-16 12:40:18 -05002059 unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
Yuqian Lid208a882018-01-04 10:08:42 -05002060}
2061
2062STAGE(mask_2pt_conical_degenerates, SkJumper_2PtConicalCtx* c) {
2063 F& t = r;
2064 auto is_degenerate = (t <= 0) | (t != t);
2065 t = if_then_else(is_degenerate, F(0), t);
Mike Reeddfc0e912018-02-16 12:40:18 -05002066 unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
Yuqian Lid208a882018-01-04 10:08:42 -05002067}
2068
Mike Kleinf7729c22017-09-27 11:42:30 -04002069STAGE(apply_vector_mask, const uint32_t* ctx) {
2070 const U32 mask = unaligned_load<U32>(ctx);
Florin Malita9026fe12017-06-29 11:03:45 -04002071 r = bit_cast<F>(bit_cast<U32>(r) & mask);
2072 g = bit_cast<F>(bit_cast<U32>(g) & mask);
2073 b = bit_cast<F>(bit_cast<U32>(b) & mask);
2074 a = bit_cast<F>(bit_cast<U32>(a) & mask);
Florin Malita2e409002017-06-28 14:46:54 -04002075}
2076
Mike Kleinf7729c22017-09-27 11:42:30 -04002077STAGE(save_xy, SkJumper_SamplerCtx* c) {
Mike Klein0a904492017-04-12 12:52:48 -04002078 // Whether bilinear or bicubic, all sample points are at the same fractional offset (fx,fy).
2079 // They're either the 4 corners of a logical 1x1 pixel or the 16 corners of a 3x3 grid
2080 // surrounding (x,y) at (0.5,0.5) off-center.
Mike Kleinfe560a82017-05-01 12:56:35 -04002081 F fx = fract(r + 0.5f),
2082 fy = fract(g + 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002083
2084 // Samplers will need to load x and fx, or y and fy.
Mike Kleinc33aa902017-05-15 10:20:48 -04002085 unaligned_store(c->x, r);
2086 unaligned_store(c->y, g);
2087 unaligned_store(c->fx, fx);
2088 unaligned_store(c->fy, fy);
Mike Klein0a904492017-04-12 12:52:48 -04002089}
2090
Mike Kleinf7729c22017-09-27 11:42:30 -04002091STAGE(accumulate, const SkJumper_SamplerCtx* c) {
Mike Klein0a904492017-04-12 12:52:48 -04002092 // Bilinear and bicubic filters are both separable, so we produce independent contributions
2093 // from x and y, multiplying them together here to get each pixel's total scale factor.
2094 auto scale = unaligned_load<F>(c->scalex)
2095 * unaligned_load<F>(c->scaley);
2096 dr = mad(scale, r, dr);
2097 dg = mad(scale, g, dg);
2098 db = mad(scale, b, db);
2099 da = mad(scale, a, da);
2100}
2101
2102// In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
2103// are combined in direct proportion to their area overlapping that logical query pixel.
2104// At positive offsets, the x-axis contribution to that rectangle is fx, or (1-fx) at negative x.
2105// The y-axis is symmetric.
2106
2107template <int kScale>
2108SI void bilinear_x(SkJumper_SamplerCtx* ctx, F* x) {
Mike Kleinfe560a82017-05-01 12:56:35 -04002109 *x = unaligned_load<F>(ctx->x) + (kScale * 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002110 F fx = unaligned_load<F>(ctx->fx);
2111
2112 F scalex;
Mike Kleinfe560a82017-05-01 12:56:35 -04002113 if (kScale == -1) { scalex = 1.0f - fx; }
2114 if (kScale == +1) { scalex = fx; }
Mike Kleinc33aa902017-05-15 10:20:48 -04002115 unaligned_store(ctx->scalex, scalex);
Mike Klein0a904492017-04-12 12:52:48 -04002116}
2117template <int kScale>
2118SI void bilinear_y(SkJumper_SamplerCtx* ctx, F* y) {
Mike Kleinfe560a82017-05-01 12:56:35 -04002119 *y = unaligned_load<F>(ctx->y) + (kScale * 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002120 F fy = unaligned_load<F>(ctx->fy);
2121
2122 F scaley;
Mike Kleinfe560a82017-05-01 12:56:35 -04002123 if (kScale == -1) { scaley = 1.0f - fy; }
2124 if (kScale == +1) { scaley = fy; }
Mike Kleinc33aa902017-05-15 10:20:48 -04002125 unaligned_store(ctx->scaley, scaley);
Mike Klein0a904492017-04-12 12:52:48 -04002126}
2127
Mike Kleinf7729c22017-09-27 11:42:30 -04002128STAGE(bilinear_nx, SkJumper_SamplerCtx* ctx) { bilinear_x<-1>(ctx, &r); }
2129STAGE(bilinear_px, SkJumper_SamplerCtx* ctx) { bilinear_x<+1>(ctx, &r); }
2130STAGE(bilinear_ny, SkJumper_SamplerCtx* ctx) { bilinear_y<-1>(ctx, &g); }
2131STAGE(bilinear_py, SkJumper_SamplerCtx* ctx) { bilinear_y<+1>(ctx, &g); }
Mike Klein0a904492017-04-12 12:52:48 -04002132
2133
2134// In bicubic interpolation, the 16 pixels and +/- 0.5 and +/- 1.5 offsets from the sample
2135// pixel center are combined with a non-uniform cubic filter, with higher values near the center.
2136//
2137// We break this function into two parts, one for near 0.5 offsets and one for far 1.5 offsets.
2138// See GrCubicEffect for details of this particular filter.
2139
2140SI F bicubic_near(F t) {
2141 // 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 -04002142 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 -04002143}
2144SI F bicubic_far(F t) {
2145 // 0/18 + 0/18*t - 6/18t^2 + 7/18t^3 == t^2 (7/18t - 6/18)
Mike Kleinfe560a82017-05-01 12:56:35 -04002146 return (t*t)*mad((7/18.0f), t, (-6/18.0f));
Mike Klein0a904492017-04-12 12:52:48 -04002147}
2148
2149template <int kScale>
2150SI void bicubic_x(SkJumper_SamplerCtx* ctx, F* x) {
Mike Kleinfe560a82017-05-01 12:56:35 -04002151 *x = unaligned_load<F>(ctx->x) + (kScale * 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002152 F fx = unaligned_load<F>(ctx->fx);
2153
2154 F scalex;
Mike Kleinfe560a82017-05-01 12:56:35 -04002155 if (kScale == -3) { scalex = bicubic_far (1.0f - fx); }
2156 if (kScale == -1) { scalex = bicubic_near(1.0f - fx); }
2157 if (kScale == +1) { scalex = bicubic_near( fx); }
2158 if (kScale == +3) { scalex = bicubic_far ( fx); }
Mike Kleinc33aa902017-05-15 10:20:48 -04002159 unaligned_store(ctx->scalex, scalex);
Mike Klein0a904492017-04-12 12:52:48 -04002160}
2161template <int kScale>
2162SI void bicubic_y(SkJumper_SamplerCtx* ctx, F* y) {
Mike Kleinfe560a82017-05-01 12:56:35 -04002163 *y = unaligned_load<F>(ctx->y) + (kScale * 0.5f);
Mike Klein0a904492017-04-12 12:52:48 -04002164 F fy = unaligned_load<F>(ctx->fy);
2165
2166 F scaley;
Mike Kleinfe560a82017-05-01 12:56:35 -04002167 if (kScale == -3) { scaley = bicubic_far (1.0f - fy); }
2168 if (kScale == -1) { scaley = bicubic_near(1.0f - fy); }
2169 if (kScale == +1) { scaley = bicubic_near( fy); }
2170 if (kScale == +3) { scaley = bicubic_far ( fy); }
Mike Kleinc33aa902017-05-15 10:20:48 -04002171 unaligned_store(ctx->scaley, scaley);
Mike Klein0a904492017-04-12 12:52:48 -04002172}
2173
Mike Kleinf7729c22017-09-27 11:42:30 -04002174STAGE(bicubic_n3x, SkJumper_SamplerCtx* ctx) { bicubic_x<-3>(ctx, &r); }
2175STAGE(bicubic_n1x, SkJumper_SamplerCtx* ctx) { bicubic_x<-1>(ctx, &r); }
2176STAGE(bicubic_p1x, SkJumper_SamplerCtx* ctx) { bicubic_x<+1>(ctx, &r); }
2177STAGE(bicubic_p3x, SkJumper_SamplerCtx* ctx) { bicubic_x<+3>(ctx, &r); }
Mike Klein0a904492017-04-12 12:52:48 -04002178
Mike Kleinf7729c22017-09-27 11:42:30 -04002179STAGE(bicubic_n3y, SkJumper_SamplerCtx* ctx) { bicubic_y<-3>(ctx, &g); }
2180STAGE(bicubic_n1y, SkJumper_SamplerCtx* ctx) { bicubic_y<-1>(ctx, &g); }
2181STAGE(bicubic_p1y, SkJumper_SamplerCtx* ctx) { bicubic_y<+1>(ctx, &g); }
2182STAGE(bicubic_p3y, SkJumper_SamplerCtx* ctx) { bicubic_y<+3>(ctx, &g); }
Mike Klein7fee90c2017-04-07 16:55:09 -04002183
Mike Kleinf7729c22017-09-27 11:42:30 -04002184STAGE(callback, SkJumper_CallbackCtx* c) {
Mike Kleinc17dc242017-04-20 16:21:57 -04002185 store4(c->rgba,0, r,g,b,a);
Mike Klein0e4d0962017-09-27 11:04:34 -04002186 c->fn(c, tail ? tail : N);
Mike Kleinc17dc242017-04-20 16:21:57 -04002187 load4(c->read_from,0, &r,&g,&b,&a);
Mike Klein7fee90c2017-04-07 16:55:09 -04002188}
Mike Kleinc2f876b2017-08-09 18:23:25 -04002189
2190// Our general strategy is to recursively interpolate each dimension,
2191// accumulating the index to sample at, and our current pixel stride to help accumulate the index.
2192template <int dim>
2193SI void color_lookup_table(const SkJumper_ColorLookupTableCtx* ctx,
2194 F& r, F& g, F& b, F a, U32 index, U32 stride) {
2195 // We'd logically like to sample this dimension at x.
2196 int limit = ctx->limits[dim-1];
2197 F src;
2198 switch(dim) {
2199 case 1: src = r; break;
2200 case 2: src = g; break;
2201 case 3: src = b; break;
2202 case 4: src = a; break;
2203 }
2204 F x = src * (limit - 1);
2205
2206 // We can't index an array by a float (darn) so we have to snap to nearby integers lo and hi.
2207 U32 lo = trunc_(x ),
2208 hi = trunc_(x + 0.9999f);
2209
2210 // Recursively sample at lo and hi.
2211 F lr = r, lg = g, lb = b,
2212 hr = r, hg = g, hb = b;
2213 color_lookup_table<dim-1>(ctx, lr,lg,lb,a, stride*lo + index, stride*limit);
2214 color_lookup_table<dim-1>(ctx, hr,hg,hb,a, stride*hi + index, stride*limit);
2215
2216 // Linearly interpolate those colors based on their distance to x.
2217 F t = x - cast(lo);
2218 r = lerp(lr, hr, t);
2219 g = lerp(lg, hg, t);
2220 b = lerp(lb, hb, t);
2221}
2222
2223// Bottom out our recursion at 0 dimensions, i.e. just return the colors at index.
2224template<>
2225inline void color_lookup_table<0>(const SkJumper_ColorLookupTableCtx* ctx,
2226 F& r, F& g, F& b, F a, U32 index, U32 stride) {
2227 r = gather(ctx->table, 3*index+0);
2228 g = gather(ctx->table, 3*index+1);
2229 b = gather(ctx->table, 3*index+2);
2230}
2231
Mike Kleinf7729c22017-09-27 11:42:30 -04002232STAGE(clut_3D, const SkJumper_ColorLookupTableCtx* ctx) {
Mike Kleinc2f876b2017-08-09 18:23:25 -04002233 color_lookup_table<3>(ctx, r,g,b,a, 0,1);
2234 // This 3D color lookup table leaves alpha alone.
2235}
Mike Kleinf7729c22017-09-27 11:42:30 -04002236STAGE(clut_4D, const SkJumper_ColorLookupTableCtx* ctx) {
Mike Kleinc2f876b2017-08-09 18:23:25 -04002237 color_lookup_table<4>(ctx, r,g,b,a, 0,1);
2238 // "a" was really CMYK's K, so we just set alpha opaque.
2239 a = 1.0f;
2240}
Mike Klein3cbcb732017-10-25 12:38:25 -04002241
2242STAGE(gauss_a_to_rgba, Ctx::None) {
2243 // x = 1 - x;
2244 // exp(-x * x * 4) - 0.018f;
2245 // ... now approximate with quartic
2246 //
2247 const float c4 = -2.26661229133605957031f;
2248 const float c3 = 2.89795351028442382812f;
2249 const float c2 = 0.21345567703247070312f;
2250 const float c1 = 0.15489584207534790039f;
2251 const float c0 = 0.00030726194381713867f;
2252 a = mad(a, mad(a, mad(a, mad(a, c4, c3), c2), c1), c0);
2253 r = a;
2254 g = a;
2255 b = a;
2256}
Mike Klein1fa9c432017-12-11 09:59:47 -05002257
2258// A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
2259STAGE(bilerp_clamp_8888, SkJumper_GatherCtx* ctx) {
2260 // (cx,cy) are the center of our sample.
2261 F cx = r,
2262 cy = g;
2263
2264 // All sample points are at the same fractional offset (fx,fy).
2265 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
2266 F fx = fract(cx + 0.5f),
2267 fy = fract(cy + 0.5f);
2268
2269 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
2270 r = g = b = a = 0;
2271
2272 for (float dy = -0.5f; dy <= +0.5f; dy += 1.0f)
2273 for (float dx = -0.5f; dx <= +0.5f; dx += 1.0f) {
2274 // (x,y) are the coordinates of this sample point.
2275 F x = cx + dx,
2276 y = cy + dy;
2277
2278 // ix_and_ptr() will clamp to the image's bounds for us.
2279 const uint32_t* ptr;
2280 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2281
2282 F sr,sg,sb,sa;
2283 from_8888(gather(ptr, ix), &sr,&sg,&sb,&sa);
2284
2285 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
2286 // are combined in direct proportion to their area overlapping that logical query pixel.
2287 // At positive offsets, the x-axis contribution to that rectangle is fx,
2288 // or (1-fx) at negative x. Same deal for y.
2289 F sx = (dx > 0) ? fx : 1.0f - fx,
2290 sy = (dy > 0) ? fy : 1.0f - fy,
2291 area = sx * sy;
2292
2293 r += sr * area;
2294 g += sg * area;
2295 b += sb * area;
2296 a += sa * area;
2297 }
2298}
Mike Klein1b9b7d52018-02-27 10:37:40 -05002299
2300namespace lowp {
2301#if defined(JUMPER_IS_SCALAR)
2302 // If we're not compiled by Clang, or otherwise switched into scalar mode (old Clang, manually),
2303 // we don't generate lowp stages. All these nullptrs will tell SkJumper.cpp to always use the
2304 // highp float pipeline.
2305 #define M(st) static void (*st)(void) = nullptr;
2306 SK_RASTER_PIPELINE_STAGES(M)
2307 #undef M
2308 static void (*just_return)(void) = nullptr;
2309
2310 static void start_pipeline(size_t,size_t,size_t,size_t, void**) {}
2311
2312#else // We are compiling vector code with Clang... let's make some lowp stages!
2313
2314#if defined(__AVX2__)
2315 using U8 = uint8_t __attribute__((ext_vector_type(16)));
2316 using U16 = uint16_t __attribute__((ext_vector_type(16)));
2317 using I16 = int16_t __attribute__((ext_vector_type(16)));
2318 using I32 = int32_t __attribute__((ext_vector_type(16)));
2319 using U32 = uint32_t __attribute__((ext_vector_type(16)));
2320 using F = float __attribute__((ext_vector_type(16)));
2321#else
2322 using U8 = uint8_t __attribute__((ext_vector_type(8)));
2323 using U16 = uint16_t __attribute__((ext_vector_type(8)));
2324 using I16 = int16_t __attribute__((ext_vector_type(8)));
2325 using I32 = int32_t __attribute__((ext_vector_type(8)));
2326 using U32 = uint32_t __attribute__((ext_vector_type(8)));
2327 using F = float __attribute__((ext_vector_type(8)));
2328#endif
2329
2330static const size_t N = sizeof(U16) / sizeof(uint16_t);
2331
Mike Kleina46623b2018-03-10 10:27:24 -05002332// Once again, some platforms benefit from a restricted Stage calling convention,
2333// but others can pass tons and tons of registers and we're happy to exploit that.
2334// It's exactly the same decision and implementation strategy as the F stages above.
2335#if JUMPER_NARROW_STAGES
2336 struct Params {
2337 size_t dx, dy, tail;
2338 U16 dr,dg,db,da;
2339 };
2340 using Stage = void(ABI*)(Params*, void** program, U16 r, U16 g, U16 b, U16 a);
2341#else
2342 // We pass program as the second argument so that load_and_inc() will find it in %rsi on x86-64.
2343 using Stage = void (ABI*)(size_t tail, void** program, size_t dx, size_t dy,
2344 U16 r, U16 g, U16 b, U16 a,
2345 U16 dr, U16 dg, U16 db, U16 da);
2346#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05002347
2348static void start_pipeline(const size_t x0, const size_t y0,
2349 const size_t xlimit, const size_t ylimit, void** program) {
2350 auto start = (Stage)load_and_inc(program);
2351 for (size_t dy = y0; dy < ylimit; dy++) {
Mike Kleina46623b2018-03-10 10:27:24 -05002352 #if JUMPER_NARROW_STAGES
2353 Params params = { x0,dy,0, 0,0,0,0 };
2354 for (; params.dx + N <= xlimit; params.dx += N) {
2355 start(&params,program, 0,0,0,0);
2356 }
2357 if (size_t tail = xlimit - params.dx) {
2358 params.tail = tail;
2359 start(&params,program, 0,0,0,0);
2360 }
2361 #else
Mike Klein1b9b7d52018-02-27 10:37:40 -05002362 size_t dx = x0;
2363 for (; dx + N <= xlimit; dx += N) {
2364 start( 0,program,dx,dy, 0,0,0,0, 0,0,0,0);
2365 }
2366 if (size_t tail = xlimit - dx) {
2367 start(tail,program,dx,dy, 0,0,0,0, 0,0,0,0);
2368 }
Mike Kleina46623b2018-03-10 10:27:24 -05002369 #endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05002370 }
2371}
2372
Mike Kleina46623b2018-03-10 10:27:24 -05002373#if JUMPER_NARROW_STAGES
2374 static ABI void just_return(Params*, void**, U16,U16,U16,U16) {}
2375#else
2376 static ABI void just_return(size_t,void**,size_t,size_t, U16,U16,U16,U16, U16,U16,U16,U16) {}
2377#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05002378
2379// All stages use the same function call ABI to chain into each other, but there are three types:
2380// GG: geometry in, geometry out -- think, a matrix
2381// GP: geometry in, pixels out. -- think, a memory gather
2382// PP: pixels in, pixels out. -- think, a blend mode
2383//
2384// (Some stages ignore their inputs or produce no logical output. That's perfectly fine.)
2385//
2386// These three STAGE_ macros let you define each type of stage,
2387// and will have (x,y) geometry and/or (r,g,b,a, dr,dg,db,da) pixel arguments as appropriate.
2388
Mike Kleina46623b2018-03-10 10:27:24 -05002389#if JUMPER_NARROW_STAGES
2390 #define STAGE_GG(name, ...) \
2391 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y); \
2392 static ABI void name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
2393 auto x = join<F>(r,g), \
2394 y = join<F>(b,a); \
2395 name##_k(Ctx{program}, params->dx,params->dy,params->tail, x,y); \
2396 split(x, &r,&g); \
2397 split(y, &b,&a); \
2398 auto next = (Stage)load_and_inc(program); \
2399 next(params,program, r,g,b,a); \
2400 } \
2401 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 -05002402
Mike Kleina46623b2018-03-10 10:27:24 -05002403 #define STAGE_GP(name, ...) \
2404 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
2405 U16& r, U16& g, U16& b, U16& a, \
2406 U16& dr, U16& dg, U16& db, U16& da); \
2407 static ABI void name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
2408 auto x = join<F>(r,g), \
2409 y = join<F>(b,a); \
2410 name##_k(Ctx{program}, params->dx,params->dy,params->tail, x,y, r,g,b,a, \
2411 params->dr,params->dg,params->db,params->da); \
2412 auto next = (Stage)load_and_inc(program); \
2413 next(params,program, r,g,b,a); \
2414 } \
2415 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
2416 U16& r, U16& g, U16& b, U16& a, \
2417 U16& dr, U16& dg, U16& db, U16& da)
Mike Klein1b9b7d52018-02-27 10:37:40 -05002418
Mike Kleina46623b2018-03-10 10:27:24 -05002419 #define STAGE_PP(name, ...) \
2420 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
2421 U16& r, U16& g, U16& b, U16& a, \
2422 U16& dr, U16& dg, U16& db, U16& da); \
2423 static ABI void name(Params* params, void** program, U16 r, U16 g, U16 b, U16 a) { \
2424 name##_k(Ctx{program}, params->dx,params->dy,params->tail, r,g,b,a, \
2425 params->dr,params->dg,params->db,params->da); \
2426 auto next = (Stage)load_and_inc(program); \
2427 next(params,program, r,g,b,a); \
2428 } \
2429 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
2430 U16& r, U16& g, U16& b, U16& a, \
2431 U16& dr, U16& dg, U16& db, U16& da)
2432#else
2433 #define STAGE_GG(name, ...) \
2434 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y); \
2435 static ABI void name(size_t tail, void** program, size_t dx, size_t dy, \
2436 U16 r, U16 g, U16 b, U16 a, \
2437 U16 dr, U16 dg, U16 db, U16 da) { \
2438 auto x = join<F>(r,g), \
2439 y = join<F>(b,a); \
2440 name##_k(Ctx{program}, dx,dy,tail, x,y); \
2441 split(x, &r,&g); \
2442 split(y, &b,&a); \
2443 auto next = (Stage)load_and_inc(program); \
2444 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
2445 } \
2446 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F& x, F& y)
2447
2448 #define STAGE_GP(name, ...) \
2449 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
2450 U16& r, U16& g, U16& b, U16& a, \
2451 U16& dr, U16& dg, U16& db, U16& da); \
2452 static ABI void name(size_t tail, void** program, size_t dx, size_t dy, \
2453 U16 r, U16 g, U16 b, U16 a, \
2454 U16 dr, U16 dg, U16 db, U16 da) { \
2455 auto x = join<F>(r,g), \
2456 y = join<F>(b,a); \
2457 name##_k(Ctx{program}, dx,dy,tail, x,y, r,g,b,a, dr,dg,db,da); \
2458 auto next = (Stage)load_and_inc(program); \
2459 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
2460 } \
2461 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, F x, F y, \
2462 U16& r, U16& g, U16& b, U16& a, \
2463 U16& dr, U16& dg, U16& db, U16& da)
2464
2465 #define STAGE_PP(name, ...) \
2466 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
2467 U16& r, U16& g, U16& b, U16& a, \
2468 U16& dr, U16& dg, U16& db, U16& da); \
2469 static ABI void name(size_t tail, void** program, size_t dx, size_t dy, \
2470 U16 r, U16 g, U16 b, U16 a, \
2471 U16 dr, U16 dg, U16 db, U16 da) { \
2472 name##_k(Ctx{program}, dx,dy,tail, r,g,b,a, dr,dg,db,da); \
2473 auto next = (Stage)load_and_inc(program); \
2474 next(tail,program,dx,dy, r,g,b,a, dr,dg,db,da); \
2475 } \
2476 SI void name##_k(__VA_ARGS__, size_t dx, size_t dy, size_t tail, \
2477 U16& r, U16& g, U16& b, U16& a, \
2478 U16& dr, U16& dg, U16& db, U16& da)
2479#endif
Mike Klein1b9b7d52018-02-27 10:37:40 -05002480
2481// ~~~~~~ Commonly used helper functions ~~~~~~ //
2482
2483SI U16 div255(U16 v) {
2484#if 0
2485 return (v+127)/255; // The ideal rounding divide by 255.
2486#else
2487 return (v+255)/256; // A good approximation of (v+127)/255.
2488#endif
2489}
2490
2491SI U16 inv(U16 v) { return 255-v; }
2492
2493SI U16 if_then_else(I16 c, U16 t, U16 e) { return (t & c) | (e & ~c); }
2494SI U32 if_then_else(I32 c, U32 t, U32 e) { return (t & c) | (e & ~c); }
2495
2496SI U16 max(U16 x, U16 y) { return if_then_else(x < y, y, x); }
2497SI U16 min(U16 x, U16 y) { return if_then_else(x < y, x, y); }
2498SI U16 max(U16 x, U16 y, U16 z) { return max(x, max(y, z)); }
2499SI U16 min(U16 x, U16 y, U16 z) { return min(x, min(y, z)); }
2500
2501SI U16 from_float(float f) { return f * 255.0f + 0.5f; }
2502
2503SI U16 lerp(U16 from, U16 to, U16 t) { return div255( from*inv(t) + to*t ); }
2504
2505template <typename D, typename S>
2506SI D cast(S src) {
2507 return __builtin_convertvector(src, D);
2508}
2509
2510template <typename D, typename S>
2511SI void split(S v, D* lo, D* hi) {
2512 static_assert(2*sizeof(D) == sizeof(S), "");
2513 memcpy(lo, (const char*)&v + 0*sizeof(D), sizeof(D));
2514 memcpy(hi, (const char*)&v + 1*sizeof(D), sizeof(D));
2515}
2516template <typename D, typename S>
2517SI D join(S lo, S hi) {
2518 static_assert(sizeof(D) == 2*sizeof(S), "");
2519 D v;
2520 memcpy((char*)&v + 0*sizeof(S), &lo, sizeof(S));
2521 memcpy((char*)&v + 1*sizeof(S), &hi, sizeof(S));
2522 return v;
2523}
2524template <typename V, typename H>
2525SI V map(V v, H (*fn)(H)) {
2526 H lo,hi;
2527 split(v, &lo,&hi);
2528 lo = fn(lo);
2529 hi = fn(hi);
2530 return join<V>(lo,hi);
2531}
2532
2533SI F if_then_else(I32 c, F t, F e) {
2534 return bit_cast<F>( (bit_cast<I32>(t) & c) | (bit_cast<I32>(e) & ~c) );
2535}
2536SI F max(F x, F y) { return if_then_else(x < y, y, x); }
2537SI F min(F x, F y) { return if_then_else(x < y, x, y); }
2538
2539SI F mad(F f, F m, F a) { return f*m+a; }
2540SI U32 trunc_(F x) { return (U32)cast<I32>(x); }
2541
2542SI F rcp(F x) {
2543#if defined(__AVX2__)
2544 return map(x, _mm256_rcp_ps);
2545#elif defined(__SSE__)
2546 return map(x, _mm_rcp_ps);
2547#elif defined(__ARM_NEON)
2548 return map(x, +[](float32x4_t v) {
2549 auto est = vrecpeq_f32(v);
2550 return vrecpsq_f32(v,est)*est;
2551 });
2552#else
2553 return 1.0f / x;
2554#endif
2555}
2556SI F sqrt_(F x) {
2557#if defined(__AVX2__)
2558 return map(x, _mm256_sqrt_ps);
2559#elif defined(__SSE__)
2560 return map(x, _mm_sqrt_ps);
2561#elif defined(__aarch64__)
2562 return map(x, vsqrtq_f32);
2563#elif defined(__ARM_NEON)
2564 return map(x, +[](float32x4_t v) {
2565 auto est = vrsqrteq_f32(v); // Estimate and two refinement steps for est = rsqrt(v).
2566 est *= vrsqrtsq_f32(v,est*est);
2567 est *= vrsqrtsq_f32(v,est*est);
2568 return v*est; // sqrt(v) == v*rsqrt(v).
2569 });
2570#else
2571 return F{
2572 sqrtf(x[0]), sqrtf(x[1]), sqrtf(x[2]), sqrtf(x[3]),
2573 sqrtf(x[4]), sqrtf(x[5]), sqrtf(x[6]), sqrtf(x[7]),
2574 };
2575#endif
2576}
2577
2578SI F floor_(F x) {
2579#if defined(__aarch64__)
2580 return map(x, vrndmq_f32);
2581#elif defined(__AVX2__)
2582 return map(x, +[](__m256 v){ return _mm256_floor_ps(v); }); // _mm256_floor_ps is a macro...
2583#elif defined(__SSE4_1__)
2584 return map(x, +[](__m128 v){ return _mm_floor_ps(v); }); // _mm_floor_ps() is a macro too.
2585#else
2586 F roundtrip = cast<F>(cast<I32>(x));
2587 return roundtrip - if_then_else(roundtrip > x, F(1), F(0));
2588#endif
2589}
2590SI F abs_(F x) { return bit_cast<F>( bit_cast<I32>(x) & 0x7fffffff ); }
2591
2592// ~~~~~~ Basic / misc. stages ~~~~~~ //
2593
2594STAGE_GG(seed_shader, const float* iota) {
2595 x = cast<F>(I32(dx)) + unaligned_load<F>(iota);
2596 y = cast<F>(I32(dy)) + 0.5f;
2597}
2598
2599STAGE_GG(matrix_translate, const float* m) {
2600 x += m[0];
2601 y += m[1];
2602}
2603STAGE_GG(matrix_scale_translate, const float* m) {
2604 x = mad(x,m[0], m[2]);
2605 y = mad(y,m[1], m[3]);
2606}
2607STAGE_GG(matrix_2x3, const float* m) {
2608 auto X = mad(x,m[0], mad(y,m[2], m[4])),
2609 Y = mad(x,m[1], mad(y,m[3], m[5]));
2610 x = X;
2611 y = Y;
2612}
2613STAGE_GG(matrix_perspective, const float* m) {
2614 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
2615 auto X = mad(x,m[0], mad(y,m[1], m[2])),
2616 Y = mad(x,m[3], mad(y,m[4], m[5])),
2617 Z = mad(x,m[6], mad(y,m[7], m[8]));
2618 x = X * rcp(Z);
2619 y = Y * rcp(Z);
2620}
2621
2622STAGE_PP(uniform_color, const SkJumper_UniformColorCtx* c) {
2623 r = c->rgba[0];
2624 g = c->rgba[1];
2625 b = c->rgba[2];
2626 a = c->rgba[3];
2627}
2628STAGE_PP(black_color, Ctx::None) { r = g = b = 0; a = 255; }
2629STAGE_PP(white_color, Ctx::None) { r = g = b = 255; a = 255; }
2630
2631STAGE_PP(set_rgb, const float rgb[3]) {
2632 r = from_float(rgb[0]);
2633 g = from_float(rgb[1]);
2634 b = from_float(rgb[2]);
2635}
2636
2637STAGE_PP(clamp_a, Ctx::None) {
2638 r = min(r, a);
2639 g = min(g, a);
2640 b = min(b, a);
2641}
2642STAGE_PP(clamp_a_dst, Ctx::None) {
2643 dr = min(dr, da);
2644 dg = min(dg, da);
2645 db = min(db, da);
2646}
2647
2648STAGE_PP(premul, Ctx::None) {
2649 r = div255(r * a);
2650 g = div255(g * a);
2651 b = div255(b * a);
2652}
2653STAGE_PP(premul_dst, Ctx::None) {
2654 dr = div255(dr * da);
2655 dg = div255(dg * da);
2656 db = div255(db * da);
2657}
2658
2659STAGE_PP(force_opaque , Ctx::None) { a = 255; }
2660STAGE_PP(force_opaque_dst, Ctx::None) { da = 255; }
2661
2662STAGE_PP(swap_rb, Ctx::None) {
2663 auto tmp = r;
2664 r = b;
2665 b = tmp;
2666}
2667
2668STAGE_PP(move_src_dst, Ctx::None) {
2669 dr = r;
2670 dg = g;
2671 db = b;
2672 da = a;
2673}
2674
2675STAGE_PP(move_dst_src, Ctx::None) {
2676 r = dr;
2677 g = dg;
2678 b = db;
2679 a = da;
2680}
2681
2682STAGE_PP(invert, Ctx::None) {
2683 r = inv(r);
2684 g = inv(g);
2685 b = inv(b);
2686 a = inv(a);
2687}
2688
2689// ~~~~~~ Blend modes ~~~~~~ //
2690
2691// The same logic applied to all 4 channels.
2692#define BLEND_MODE(name) \
2693 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
2694 STAGE_PP(name, Ctx::None) { \
2695 r = name##_channel(r,dr,a,da); \
2696 g = name##_channel(g,dg,a,da); \
2697 b = name##_channel(b,db,a,da); \
2698 a = name##_channel(a,da,a,da); \
2699 } \
2700 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
2701
2702 BLEND_MODE(clear) { return 0; }
2703 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
2704 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
2705 BLEND_MODE(srcin) { return div255( s*da ); }
2706 BLEND_MODE(dstin) { return div255( d*sa ); }
2707 BLEND_MODE(srcout) { return div255( s*inv(da) ); }
2708 BLEND_MODE(dstout) { return div255( d*inv(sa) ); }
2709 BLEND_MODE(srcover) { return s + div255( d*inv(sa) ); }
2710 BLEND_MODE(dstover) { return d + div255( s*inv(da) ); }
2711 BLEND_MODE(modulate) { return div255( s*d ); }
2712 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
2713 BLEND_MODE(plus_) { return min(s+d, 255); }
2714 BLEND_MODE(screen) { return s + d - div255( s*d ); }
2715 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
2716#undef BLEND_MODE
2717
2718// The same logic applied to color, and srcover for alpha.
2719#define BLEND_MODE(name) \
2720 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
2721 STAGE_PP(name, Ctx::None) { \
2722 r = name##_channel(r,dr,a,da); \
2723 g = name##_channel(g,dg,a,da); \
2724 b = name##_channel(b,db,a,da); \
2725 a = a + div255( da*inv(a) ); \
2726 } \
2727 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
2728
2729 BLEND_MODE(darken) { return s + d - div255( max(s*da, d*sa) ); }
2730 BLEND_MODE(lighten) { return s + d - div255( min(s*da, d*sa) ); }
2731 BLEND_MODE(difference) { return s + d - 2*div255( min(s*da, d*sa) ); }
2732 BLEND_MODE(exclusion) { return s + d - 2*div255( s*d ); }
2733
2734 BLEND_MODE(hardlight) {
2735 return div255( s*inv(da) + d*inv(sa) +
2736 if_then_else(2*s <= sa, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
2737 }
2738 BLEND_MODE(overlay) {
2739 return div255( s*inv(da) + d*inv(sa) +
2740 if_then_else(2*d <= da, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
2741 }
2742#undef BLEND_MODE
2743
2744// ~~~~~~ Helpers for interacting with memory ~~~~~~ //
2745
2746template <typename T>
2747SI T* ptr_at_xy(const SkJumper_MemoryCtx* ctx, size_t dx, size_t dy) {
2748 return (T*)ctx->pixels + dy*ctx->stride + dx;
2749}
2750
2751template <typename T>
2752SI U32 ix_and_ptr(T** ptr, const SkJumper_GatherCtx* ctx, F x, F y) {
2753 auto clamp = [](F v, F limit) {
2754 limit = bit_cast<F>( bit_cast<U32>(limit) - 1 ); // Exclusive -> inclusive.
2755 return min(max(0, v), limit);
2756 };
2757 x = clamp(x, ctx->width);
2758 y = clamp(y, ctx->height);
2759
2760 *ptr = (const T*)ctx->pixels;
2761 return trunc_(y)*ctx->stride + trunc_(x);
2762}
2763
2764template <typename V, typename T>
2765SI V load(const T* ptr, size_t tail) {
2766 V v = 0;
2767 switch (tail & (N-1)) {
2768 case 0: memcpy(&v, ptr, sizeof(v)); break;
2769 #if defined(__AVX2__)
2770 case 15: v[14] = ptr[14];
2771 case 14: v[13] = ptr[13];
2772 case 13: v[12] = ptr[12];
2773 case 12: memcpy(&v, ptr, 12*sizeof(T)); break;
2774 case 11: v[10] = ptr[10];
2775 case 10: v[ 9] = ptr[ 9];
2776 case 9: v[ 8] = ptr[ 8];
2777 case 8: memcpy(&v, ptr, 8*sizeof(T)); break;
2778 #endif
2779 case 7: v[ 6] = ptr[ 6];
2780 case 6: v[ 5] = ptr[ 5];
2781 case 5: v[ 4] = ptr[ 4];
2782 case 4: memcpy(&v, ptr, 4*sizeof(T)); break;
2783 case 3: v[ 2] = ptr[ 2];
2784 case 2: memcpy(&v, ptr, 2*sizeof(T)); break;
2785 case 1: v[ 0] = ptr[ 0];
2786 }
2787 return v;
2788}
2789template <typename V, typename T>
2790SI void store(T* ptr, size_t tail, V v) {
2791 switch (tail & (N-1)) {
2792 case 0: memcpy(ptr, &v, sizeof(v)); break;
2793 #if defined(__AVX2__)
2794 case 15: ptr[14] = v[14];
2795 case 14: ptr[13] = v[13];
2796 case 13: ptr[12] = v[12];
2797 case 12: memcpy(ptr, &v, 12*sizeof(T)); break;
2798 case 11: ptr[10] = v[10];
2799 case 10: ptr[ 9] = v[ 9];
2800 case 9: ptr[ 8] = v[ 8];
2801 case 8: memcpy(ptr, &v, 8*sizeof(T)); break;
2802 #endif
2803 case 7: ptr[ 6] = v[ 6];
2804 case 6: ptr[ 5] = v[ 5];
2805 case 5: ptr[ 4] = v[ 4];
2806 case 4: memcpy(ptr, &v, 4*sizeof(T)); break;
2807 case 3: ptr[ 2] = v[ 2];
2808 case 2: memcpy(ptr, &v, 2*sizeof(T)); break;
2809 case 1: ptr[ 0] = v[ 0];
2810 }
2811}
2812
2813#if defined(__AVX2__)
2814 template <typename V, typename T>
2815 SI V gather(const T* ptr, U32 ix) {
2816 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
2817 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
2818 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
2819 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
2820 }
2821
2822 template<>
2823 F gather(const float* p, U32 ix) {
2824 __m256i lo, hi;
2825 split(ix, &lo, &hi);
2826
2827 return join<F>(_mm256_i32gather_ps(p, lo, 4),
2828 _mm256_i32gather_ps(p, hi, 4));
2829 }
2830
2831 template<>
2832 U32 gather(const uint32_t* p, U32 ix) {
2833 __m256i lo, hi;
2834 split(ix, &lo, &hi);
2835
2836 return join<U32>(_mm256_i32gather_epi32(p, lo, 4),
2837 _mm256_i32gather_epi32(p, hi, 4));
2838 }
2839#else
2840 template <typename V, typename T>
2841 SI V gather(const T* ptr, U32 ix) {
2842 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
2843 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]], };
2844 }
2845#endif
2846
2847
2848// ~~~~~~ 32-bit memory loads and stores ~~~~~~ //
2849
2850SI void from_8888(U32 rgba, U16* r, U16* g, U16* b, U16* a) {
2851#if 1 && defined(__AVX2__)
2852 // Swap the middle 128-bit lanes to make _mm256_packus_epi32() in cast_U16() work out nicely.
2853 __m256i _01,_23;
2854 split(rgba, &_01, &_23);
2855 __m256i _02 = _mm256_permute2x128_si256(_01,_23, 0x20),
2856 _13 = _mm256_permute2x128_si256(_01,_23, 0x31);
2857 rgba = join<U32>(_02, _13);
2858
2859 auto cast_U16 = [](U32 v) -> U16 {
2860 __m256i _02,_13;
2861 split(v, &_02,&_13);
2862 return _mm256_packus_epi32(_02,_13);
2863 };
2864#else
2865 auto cast_U16 = [](U32 v) -> U16 {
2866 return cast<U16>(v);
2867 };
2868#endif
2869 *r = cast_U16(rgba & 65535) & 255;
2870 *g = cast_U16(rgba & 65535) >> 8;
2871 *b = cast_U16(rgba >> 16) & 255;
2872 *a = cast_U16(rgba >> 16) >> 8;
2873}
2874
2875SI void load_8888_(const uint32_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
2876#if 1 && defined(__ARM_NEON)
2877 uint8x8x4_t rgba;
2878 switch (tail & (N-1)) {
2879 case 0: rgba = vld4_u8 ((const uint8_t*)(ptr+0) ); break;
2880 case 7: rgba = vld4_lane_u8((const uint8_t*)(ptr+6), rgba, 6);
2881 case 6: rgba = vld4_lane_u8((const uint8_t*)(ptr+5), rgba, 5);
2882 case 5: rgba = vld4_lane_u8((const uint8_t*)(ptr+4), rgba, 4);
2883 case 4: rgba = vld4_lane_u8((const uint8_t*)(ptr+3), rgba, 3);
2884 case 3: rgba = vld4_lane_u8((const uint8_t*)(ptr+2), rgba, 2);
2885 case 2: rgba = vld4_lane_u8((const uint8_t*)(ptr+1), rgba, 1);
2886 case 1: rgba = vld4_lane_u8((const uint8_t*)(ptr+0), rgba, 0);
2887 }
2888 *r = cast<U16>(rgba.val[0]);
2889 *g = cast<U16>(rgba.val[1]);
2890 *b = cast<U16>(rgba.val[2]);
2891 *a = cast<U16>(rgba.val[3]);
2892#else
2893 from_8888(load<U32>(ptr, tail), r,g,b,a);
2894#endif
2895}
2896SI void store_8888_(uint32_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
2897#if 1 && defined(__ARM_NEON)
2898 uint8x8x4_t rgba = {{
2899 cast<U8>(r),
2900 cast<U8>(g),
2901 cast<U8>(b),
2902 cast<U8>(a),
2903 }};
2904 switch (tail & (N-1)) {
2905 case 0: vst4_u8 ((uint8_t*)(ptr+0), rgba ); break;
2906 case 7: vst4_lane_u8((uint8_t*)(ptr+6), rgba, 6);
2907 case 6: vst4_lane_u8((uint8_t*)(ptr+5), rgba, 5);
2908 case 5: vst4_lane_u8((uint8_t*)(ptr+4), rgba, 4);
2909 case 4: vst4_lane_u8((uint8_t*)(ptr+3), rgba, 3);
2910 case 3: vst4_lane_u8((uint8_t*)(ptr+2), rgba, 2);
2911 case 2: vst4_lane_u8((uint8_t*)(ptr+1), rgba, 1);
2912 case 1: vst4_lane_u8((uint8_t*)(ptr+0), rgba, 0);
2913 }
2914#else
2915 store(ptr, tail, cast<U32>(r | (g<<8)) << 0
2916 | cast<U32>(b | (a<<8)) << 16);
2917#endif
2918}
2919
2920STAGE_PP(load_8888, const SkJumper_MemoryCtx* ctx) {
2921 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &r,&g,&b,&a);
2922}
2923STAGE_PP(load_8888_dst, const SkJumper_MemoryCtx* ctx) {
2924 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &dr,&dg,&db,&da);
2925}
2926STAGE_PP(store_8888, const SkJumper_MemoryCtx* ctx) {
2927 store_8888_(ptr_at_xy<uint32_t>(ctx, dx,dy), tail, r,g,b,a);
2928}
2929
2930STAGE_PP(load_bgra, const SkJumper_MemoryCtx* ctx) {
2931 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &b,&g,&r,&a);
2932}
2933STAGE_PP(load_bgra_dst, const SkJumper_MemoryCtx* ctx) {
2934 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), tail, &db,&dg,&dr,&da);
2935}
2936STAGE_PP(store_bgra, const SkJumper_MemoryCtx* ctx) {
2937 store_8888_(ptr_at_xy<uint32_t>(ctx, dx,dy), tail, b,g,r,a);
2938}
2939
2940STAGE_GP(gather_8888, const SkJumper_GatherCtx* ctx) {
2941 const uint32_t* ptr;
2942 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2943 from_8888(gather<U32>(ptr, ix), &r, &g, &b, &a);
2944}
2945STAGE_GP(gather_bgra, const SkJumper_GatherCtx* ctx) {
2946 const uint32_t* ptr;
2947 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2948 from_8888(gather<U32>(ptr, ix), &b, &g, &r, &a);
2949}
2950
2951// ~~~~~~ 16-bit memory loads and stores ~~~~~~ //
2952
2953SI void from_565(U16 rgb, U16* r, U16* g, U16* b) {
2954 // Format for 565 buffers: 15|rrrrr gggggg bbbbb|0
2955 U16 R = (rgb >> 11) & 31,
2956 G = (rgb >> 5) & 63,
2957 B = (rgb >> 0) & 31;
2958
2959 // These bit replications are the same as multiplying by 255/31 or 255/63 to scale to 8-bit.
2960 *r = (R << 3) | (R >> 2);
2961 *g = (G << 2) | (G >> 4);
2962 *b = (B << 3) | (B >> 2);
2963}
2964SI void load_565_(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b) {
2965 from_565(load<U16>(ptr, tail), r,g,b);
2966}
2967SI void store_565_(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b) {
2968 // Select the top 5,6,5 bits.
2969 U16 R = r >> 3,
2970 G = g >> 2,
2971 B = b >> 3;
2972 // Pack them back into 15|rrrrr gggggg bbbbb|0.
2973 store(ptr, tail, R << 11
2974 | G << 5
2975 | B << 0);
2976}
2977
2978STAGE_PP(load_565, const SkJumper_MemoryCtx* ctx) {
2979 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &r,&g,&b);
2980 a = 255;
2981}
2982STAGE_PP(load_565_dst, const SkJumper_MemoryCtx* ctx) {
2983 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &dr,&dg,&db);
2984 da = 255;
2985}
2986STAGE_PP(store_565, const SkJumper_MemoryCtx* ctx) {
2987 store_565_(ptr_at_xy<uint16_t>(ctx, dx,dy), tail, r,g,b);
2988}
2989STAGE_GP(gather_565, const SkJumper_GatherCtx* ctx) {
2990 const uint16_t* ptr;
2991 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
2992 from_565(gather<U16>(ptr, ix), &r, &g, &b);
2993 a = 255;
2994}
2995
2996SI void from_4444(U16 rgba, U16* r, U16* g, U16* b, U16* a) {
2997 // Format for 4444 buffers: 15|rrrr gggg bbbb aaaa|0.
2998 U16 R = (rgba >> 12) & 15,
2999 G = (rgba >> 8) & 15,
3000 B = (rgba >> 4) & 15,
3001 A = (rgba >> 0) & 15;
3002
3003 // Scale [0,15] to [0,255].
3004 *r = (R << 4) | R;
3005 *g = (G << 4) | G;
3006 *b = (B << 4) | B;
3007 *a = (A << 4) | A;
3008}
3009SI void load_4444_(const uint16_t* ptr, size_t tail, U16* r, U16* g, U16* b, U16* a) {
3010 from_4444(load<U16>(ptr, tail), r,g,b,a);
3011}
3012SI void store_4444_(uint16_t* ptr, size_t tail, U16 r, U16 g, U16 b, U16 a) {
3013 // Select the top 4 bits of each.
3014 U16 R = r >> 4,
3015 G = g >> 4,
3016 B = b >> 4,
3017 A = a >> 4;
3018 // Pack them back into 15|rrrr gggg bbbb aaaa|0.
3019 store(ptr, tail, R << 12
3020 | G << 8
3021 | B << 4
3022 | A << 0);
3023}
3024
3025STAGE_PP(load_4444, const SkJumper_MemoryCtx* ctx) {
3026 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &r,&g,&b,&a);
3027}
3028STAGE_PP(load_4444_dst, const SkJumper_MemoryCtx* ctx) {
3029 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &dr,&dg,&db,&da);
3030}
3031STAGE_PP(store_4444, const SkJumper_MemoryCtx* ctx) {
3032 store_4444_(ptr_at_xy<uint16_t>(ctx, dx,dy), tail, r,g,b,a);
3033}
3034STAGE_GP(gather_4444, const SkJumper_GatherCtx* ctx) {
3035 const uint16_t* ptr;
3036 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3037 from_4444(gather<U16>(ptr, ix), &r,&g,&b,&a);
3038}
3039
3040// ~~~~~~ 8-bit memory loads and stores ~~~~~~ //
3041
3042SI U16 load_8(const uint8_t* ptr, size_t tail) {
3043 return cast<U16>(load<U8>(ptr, tail));
3044}
3045SI void store_8(uint8_t* ptr, size_t tail, U16 v) {
3046 store(ptr, tail, cast<U8>(v));
3047}
3048
3049STAGE_PP(load_a8, const SkJumper_MemoryCtx* ctx) {
3050 r = g = b = 0;
3051 a = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3052}
3053STAGE_PP(load_a8_dst, const SkJumper_MemoryCtx* ctx) {
3054 dr = dg = db = 0;
3055 da = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3056}
3057STAGE_PP(store_a8, const SkJumper_MemoryCtx* ctx) {
3058 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), tail, a);
3059}
3060STAGE_GP(gather_a8, const SkJumper_GatherCtx* ctx) {
3061 const uint8_t* ptr;
3062 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3063 r = g = b = 0;
3064 a = cast<U16>(gather<U8>(ptr, ix));
3065}
3066
3067STAGE_PP(load_g8, const SkJumper_MemoryCtx* ctx) {
3068 r = g = b = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3069 a = 255;
3070}
3071STAGE_PP(load_g8_dst, const SkJumper_MemoryCtx* ctx) {
3072 dr = dg = db = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3073 da = 255;
3074}
3075STAGE_PP(luminance_to_alpha, Ctx::None) {
3076 a = (r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
3077 r = g = b = 0;
3078}
3079STAGE_GP(gather_g8, const SkJumper_GatherCtx* ctx) {
3080 const uint8_t* ptr;
3081 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
3082 r = g = b = cast<U16>(gather<U8>(ptr, ix));
3083 a = 255;
3084}
3085
3086// ~~~~~~ Coverage scales / lerps ~~~~~~ //
3087
3088STAGE_PP(scale_1_float, const float* f) {
3089 U16 c = from_float(*f);
3090 r = div255( r * c );
3091 g = div255( g * c );
3092 b = div255( b * c );
3093 a = div255( a * c );
3094}
3095STAGE_PP(lerp_1_float, const float* f) {
3096 U16 c = from_float(*f);
3097 r = lerp(dr, r, c);
3098 g = lerp(dg, g, c);
3099 b = lerp(db, b, c);
3100 a = lerp(da, a, c);
3101}
3102
3103STAGE_PP(scale_u8, const SkJumper_MemoryCtx* ctx) {
3104 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3105 r = div255( r * c );
3106 g = div255( g * c );
3107 b = div255( b * c );
3108 a = div255( a * c );
3109}
3110STAGE_PP(lerp_u8, const SkJumper_MemoryCtx* ctx) {
3111 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy), tail);
3112 r = lerp(dr, r, c);
3113 g = lerp(dg, g, c);
3114 b = lerp(db, b, c);
3115 a = lerp(da, a, c);
3116}
3117
3118// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
3119SI U16 alpha_coverage_from_rgb_coverage(U16 a, U16 da, U16 cr, U16 cg, U16 cb) {
3120 return if_then_else(a < da, min(cr,cg,cb)
3121 , max(cr,cg,cb));
3122}
3123STAGE_PP(scale_565, const SkJumper_MemoryCtx* ctx) {
3124 U16 cr,cg,cb;
3125 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &cr,&cg,&cb);
3126 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
3127
3128 r = div255( r * cr );
3129 g = div255( g * cg );
3130 b = div255( b * cb );
3131 a = div255( a * ca );
3132}
3133STAGE_PP(lerp_565, const SkJumper_MemoryCtx* ctx) {
3134 U16 cr,cg,cb;
3135 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), tail, &cr,&cg,&cb);
3136 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
3137
3138 r = lerp(dr, r, cr);
3139 g = lerp(dg, g, cg);
3140 b = lerp(db, b, cb);
3141 a = lerp(da, a, ca);
3142}
3143
3144// ~~~~~~ Gradient stages ~~~~~~ //
3145
3146// Clamp x to [0,1], both sides inclusive (think, gradients).
3147// Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
3148SI F clamp_01(F v) { return min(max(0, v), 1); }
3149
3150STAGE_GG(clamp_x_1 , Ctx::None) { x = clamp_01(x); }
3151STAGE_GG(repeat_x_1, Ctx::None) { x = clamp_01(x - floor_(x)); }
3152STAGE_GG(mirror_x_1, Ctx::None) {
3153 auto two = [](F x){ return x+x; };
3154 x = clamp_01(abs_( (x-1.0f) - two(floor_((x-1.0f)*0.5f)) - 1.0f ));
3155}
3156
3157SI I16 cond_to_mask_16(I32 cond) { return cast<I16>(cond); }
3158
3159STAGE_GG(decal_x, SkJumper_DecalTileCtx* ctx) {
3160 auto w = ctx->limit_x;
3161 unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w)));
3162}
3163STAGE_GG(decal_y, SkJumper_DecalTileCtx* ctx) {
3164 auto h = ctx->limit_y;
3165 unaligned_store(ctx->mask, cond_to_mask_16((0 <= y) & (y < h)));
3166}
3167STAGE_GG(decal_x_and_y, SkJumper_DecalTileCtx* ctx) {
3168 auto w = ctx->limit_x;
3169 auto h = ctx->limit_y;
3170 unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w) & (0 <= y) & (y < h)));
3171}
3172STAGE_PP(check_decal_mask, SkJumper_DecalTileCtx* ctx) {
3173 auto mask = unaligned_load<U16>(ctx->mask);
3174 r = r & mask;
3175 g = g & mask;
3176 b = b & mask;
3177 a = a & mask;
3178}
3179
3180
3181SI U16 round_F_to_U16(F x) { return cast<U16>(x * 255.0f + 0.5f); }
3182
3183SI void gradient_lookup(const SkJumper_GradientCtx* c, U32 idx, F t,
3184 U16* r, U16* g, U16* b, U16* a) {
3185
3186 F fr, fg, fb, fa, br, bg, bb, ba;
3187#if defined(__AVX2__)
3188 if (c->stopCount <=8) {
3189 __m256i lo, hi;
3190 split(idx, &lo, &hi);
3191
3192 fr = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), lo),
3193 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), hi));
3194 br = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), lo),
3195 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), hi));
3196 fg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), lo),
3197 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), hi));
3198 bg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), lo),
3199 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), hi));
3200 fb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), lo),
3201 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), hi));
3202 bb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), lo),
3203 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), hi));
3204 fa = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), lo),
3205 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), hi));
3206 ba = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), lo),
3207 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), hi));
3208 } else
3209#endif
3210 {
3211 fr = gather<F>(c->fs[0], idx);
3212 fg = gather<F>(c->fs[1], idx);
3213 fb = gather<F>(c->fs[2], idx);
3214 fa = gather<F>(c->fs[3], idx);
3215 br = gather<F>(c->bs[0], idx);
3216 bg = gather<F>(c->bs[1], idx);
3217 bb = gather<F>(c->bs[2], idx);
3218 ba = gather<F>(c->bs[3], idx);
3219 }
3220 *r = round_F_to_U16(mad(t, fr, br));
3221 *g = round_F_to_U16(mad(t, fg, bg));
3222 *b = round_F_to_U16(mad(t, fb, bb));
3223 *a = round_F_to_U16(mad(t, fa, ba));
3224}
3225
3226STAGE_GP(gradient, const SkJumper_GradientCtx* c) {
3227 auto t = x;
3228 U32 idx = 0;
3229
3230 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
3231 for (size_t i = 1; i < c->stopCount; i++) {
3232 idx += if_then_else(t >= c->ts[i], U32(1), U32(0));
3233 }
3234
3235 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3236}
3237
3238STAGE_GP(evenly_spaced_gradient, const SkJumper_GradientCtx* c) {
3239 auto t = x;
3240 auto idx = trunc_(t * (c->stopCount-1));
3241 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3242}
3243
3244STAGE_GP(evenly_spaced_2_stop_gradient, const void* ctx) {
3245 // TODO: Rename Ctx SkJumper_EvenlySpaced2StopGradientCtx.
3246 struct Ctx { float f[4], b[4]; };
3247 auto c = (const Ctx*)ctx;
3248
3249 auto t = x;
3250 r = round_F_to_U16(mad(t, c->f[0], c->b[0]));
3251 g = round_F_to_U16(mad(t, c->f[1], c->b[1]));
3252 b = round_F_to_U16(mad(t, c->f[2], c->b[2]));
3253 a = round_F_to_U16(mad(t, c->f[3], c->b[3]));
3254}
3255
3256STAGE_GG(xy_to_unit_angle, Ctx::None) {
3257 F xabs = abs_(x),
3258 yabs = abs_(y);
3259
3260 F slope = min(xabs, yabs)/max(xabs, yabs);
3261 F s = slope * slope;
3262
3263 // Use a 7th degree polynomial to approximate atan.
3264 // This was generated using sollya.gforge.inria.fr.
3265 // A float optimized polynomial was generated using the following command.
3266 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
3267 F phi = slope
3268 * (0.15912117063999176025390625f + s
3269 * (-5.185396969318389892578125e-2f + s
3270 * (2.476101927459239959716796875e-2f + s
3271 * (-7.0547382347285747528076171875e-3f))));
3272
3273 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
3274 phi = if_then_else(x < 0.0f , 1.0f/2.0f - phi, phi);
3275 phi = if_then_else(y < 0.0f , 1.0f - phi , phi);
3276 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
3277 x = phi;
3278}
3279STAGE_GG(xy_to_radius, Ctx::None) {
3280 x = sqrt_(x*x + y*y);
3281}
3282
3283// ~~~~~~ Compound stages ~~~~~~ //
3284
3285STAGE_PP(srcover_rgba_8888, const SkJumper_MemoryCtx* ctx) {
3286 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3287
3288 load_8888_(ptr, tail, &dr,&dg,&db,&da);
3289 r = r + div255( dr*inv(a) );
3290 g = g + div255( dg*inv(a) );
3291 b = b + div255( db*inv(a) );
3292 a = a + div255( da*inv(a) );
3293 store_8888_(ptr, tail, r,g,b,a);
3294}
3295STAGE_PP(srcover_bgra_8888, const SkJumper_MemoryCtx* ctx) {
3296 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3297
3298 load_8888_(ptr, tail, &db,&dg,&dr,&da);
3299 r = r + div255( dr*inv(a) );
3300 g = g + div255( dg*inv(a) );
3301 b = b + div255( db*inv(a) );
3302 a = a + div255( da*inv(a) );
3303 store_8888_(ptr, tail, b,g,r,a);
3304}
3305
3306// Now we'll add null stand-ins for stages we haven't implemented in lowp.
3307// If a pipeline uses these stages, it'll boot it out of lowp into highp.
3308
3309using NotImplemented = void(*)(void);
3310
3311static NotImplemented
3312 callback, load_rgba, store_rgba,
3313 clamp_0, clamp_1,
3314 unpremul, dither,
3315 from_srgb, from_srgb_dst, to_srgb,
3316 load_f16 , load_f16_dst , store_f16 , gather_f16,
3317 load_f32 , load_f32_dst , store_f32 , gather_f32,
3318 load_1010102, load_1010102_dst, store_1010102, gather_1010102,
3319 load_u16_be, load_rgb_u16_be, store_u16_be,
3320 load_tables_u16_be, load_tables_rgb_u16_be,
3321 load_tables, byte_tables, byte_tables_rgb,
3322 colorburn, colordodge, softlight, hue, saturation, color, luminosity,
3323 matrix_3x4, matrix_4x5, matrix_4x3,
3324 parametric_r, parametric_g, parametric_b, parametric_a,
3325 table_r, table_g, table_b, table_a,
3326 gamma, gamma_dst,
3327 lab_to_xyz, rgb_to_hsl, hsl_to_rgb, clut_3D, clut_4D,
3328 gauss_a_to_rgba,
3329 mirror_x, repeat_x,
3330 mirror_y, repeat_y,
3331 negate_x,
3332 bilinear_nx, bilinear_ny, bilinear_px, bilinear_py,
3333 bicubic_n3x, bicubic_n1x, bicubic_p1x, bicubic_p3x,
3334 bicubic_n3y, bicubic_n1y, bicubic_p1y, bicubic_p3y,
3335 save_xy, accumulate,
3336 xy_to_2pt_conical_well_behaved,
3337 xy_to_2pt_conical_strip,
3338 xy_to_2pt_conical_focal_on_circle,
3339 xy_to_2pt_conical_smaller,
3340 xy_to_2pt_conical_greater,
3341 xy_to_2pt_conical_compensate_focal,
3342 alter_2pt_conical_compensate_focal,
3343 alter_2pt_conical_unswap,
3344 mask_2pt_conical_nan,
3345 mask_2pt_conical_degenerates,
3346 apply_vector_mask,
3347 bilerp_clamp_8888;
3348
3349#endif//defined(JUMPER_IS_SCALAR) controlling whether we build lowp stages
3350} // namespace lowp
3351
3352} // namespace SK_OPTS_NS
3353
3354#endif//SkRasterPipeline_opts_DEFINED