1 /*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkRasterPipeline_opts_DEFINED
9 #define SkRasterPipeline_opts_DEFINED
10
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkMalloc.h"
13 #include "include/private/base/SkSpan_impl.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "modules/skcms/skcms.h"
16 #include "src/base/SkUtils.h" // unaligned_{load,store}
17 #include "src/core/SkRasterPipeline.h"
18 #include "src/core/SkRasterPipelineContextUtils.h"
19 #include "src/shaders/SkPerlinNoiseShaderType.h"
20 #include "src/sksl/tracing/SkSLTraceHook.h"
21
22 #include <cstdint>
23 #include <cmath>
24 #include <type_traits>
25
26 // Every function in this file should be marked static and inline using SI.
27 #if defined(__clang__) || defined(__GNUC__)
28 #define SI __attribute__((always_inline)) static inline
29 #else
30 #define SI static inline
31 #endif
32
33 #if defined(__clang__)
34 #define SK_UNROLL _Pragma("unroll")
35 #else
36 #define SK_UNROLL
37 #endif
38
39 #if defined(__clang__)
40 template <int N, typename T> using Vec = T __attribute__((ext_vector_type(N)));
41 #elif defined(__GNUC__)
42 // Unfortunately, GCC does not allow us to omit the struct. This will not compile:
43 // template <int N, typename T> using Vec = T __attribute__((vector_size(N*sizeof(T))));
44 template <int N, typename T> struct VecHelper {
45 typedef T __attribute__((vector_size(N * sizeof(T)))) V;
46 };
47 template <int N, typename T> using Vec = typename VecHelper<N, T>::V;
48 #endif
49
50 template <typename Dst, typename Src>
widen_cast(const Src & src)51 SI Dst widen_cast(const Src& src) {
52 static_assert(sizeof(Dst) > sizeof(Src));
53 static_assert(std::is_trivially_copyable<Dst>::value);
54 static_assert(std::is_trivially_copyable<Src>::value);
55 Dst dst;
56 memcpy(&dst, &src, sizeof(Src));
57 return dst;
58 }
59
60 struct Ctx {
61 SkRasterPipelineStage* fStage;
62
63 template <typename T>
64 operator T*() {
65 return (T*)fStage->ctx;
66 }
67 };
68
69 using NoCtx = const void*;
70
71 #if defined(SKRP_CPU_SCALAR) || defined(SKRP_CPU_NEON) || defined(SKRP_CPU_HSW) || \
72 defined(SKRP_CPU_SKX) || defined(SKRP_CPU_AVX) || defined(SKRP_CPU_SSE41) || \
73 defined(SKRP_CPU_SSE2)
74 // Honor the existing setting
75 #elif !defined(__clang__) && !defined(__GNUC__)
76 #define SKRP_CPU_SCALAR
77 #elif defined(SK_ARM_HAS_NEON)
78 #define SKRP_CPU_NEON
79 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SKX
80 #define SKRP_CPU_SKX
81 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2
82 #define SKRP_CPU_HSW
83 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX
84 #define SKRP_CPU_AVX
85 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE41
86 #define SKRP_CPU_SSE41
87 #elif SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2
88 #define SKRP_CPU_SSE2
89 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LASX
90 #define SKRP_CPU_LASX
91 #elif SK_CPU_LSX_LEVEL >= SK_CPU_LSX_LEVEL_LSX
92 #define SKRP_CPU_LSX
93 #else
94 #define SKRP_CPU_SCALAR
95 #endif
96
97 #if defined(SKRP_CPU_SCALAR)
98 #include <math.h>
99 #elif defined(SKRP_CPU_NEON)
100 #include <arm_neon.h>
101 #elif defined(SKRP_CPU_LASX)
102 #include <lasxintrin.h>
103 #include <lsxintrin.h>
104 #elif defined(SKRP_CPU_LSX)
105 #include <lsxintrin.h>
106 #else
107 #include <immintrin.h>
108 #endif
109
110 // Notes:
111 // * rcp_fast and rcp_precise both produce a reciprocal, but rcp_fast is an estimate with at least
112 // 12 bits of precision while rcp_precise should be accurate for float size. For ARM rcp_precise
113 // requires 2 Newton-Raphson refinement steps because its estimate has 8 bit precision, and for
114 // Intel this requires one additional step because its estimate has 12 bit precision.
115 //
116 // * Don't call rcp_approx or rsqrt_approx directly; only use rcp_fast and rsqrt.
117
118 namespace SK_OPTS_NS {
119 #if defined(SKRP_CPU_SCALAR)
120 // This path should lead to portable scalar code.
121 using F = float ;
122 using I32 = int32_t;
123 using U64 = uint64_t;
124 using U32 = uint32_t;
125 using U16 = uint16_t;
126 using U8 = uint8_t ;
127
min(F a,F b)128 SI F min(F a, F b) { return fminf(a,b); }
min(I32 a,I32 b)129 SI I32 min(I32 a, I32 b) { return a < b ? a : b; }
min(U32 a,U32 b)130 SI U32 min(U32 a, U32 b) { return a < b ? a : b; }
max(F a,F b)131 SI F max(F a, F b) { return fmaxf(a,b); }
max(I32 a,I32 b)132 SI I32 max(I32 a, I32 b) { return a > b ? a : b; }
max(U32 a,U32 b)133 SI U32 max(U32 a, U32 b) { return a > b ? a : b; }
134
mad(F f,F m,F a)135 SI F mad(F f, F m, F a) { return a+f*m; }
nmad(F f,F m,F a)136 SI F nmad(F f, F m, F a) { return a-f*m; }
abs_(F v)137 SI F abs_ (F v) { return fabsf(v); }
abs_(I32 v)138 SI I32 abs_ (I32 v) { return v < 0 ? -v : v; }
floor_(F v)139 SI F floor_(F v) { return floorf(v); }
ceil_(F v)140 SI F ceil_(F v) { return ceilf(v); }
rcp_approx(F v)141 SI F rcp_approx(F v) { return 1.0f / v; } // use rcp_fast instead
rsqrt_approx(F v)142 SI F rsqrt_approx(F v) { return 1.0f / sqrtf(v); }
sqrt_(F v)143 SI F sqrt_ (F v) { return sqrtf(v); }
rcp_precise(F v)144 SI F rcp_precise (F v) { return 1.0f / v; }
145
iround(F v)146 SI I32 iround(F v) { return (I32)(v + 0.5f); }
round(F v)147 SI U32 round(F v) { return (U32)(v + 0.5f); }
round(F v,F scale)148 SI U32 round(F v, F scale) { return (U32)(v*scale + 0.5f); }
pack(U32 v)149 SI U16 pack(U32 v) { return (U16)v; }
pack(U16 v)150 SI U8 pack(U16 v) { return (U8)v; }
151
if_then_else(I32 c,F t,F e)152 SI F if_then_else(I32 c, F t, F e) { return c ? t : e; }
if_then_else(I32 c,I32 t,I32 e)153 SI I32 if_then_else(I32 c, I32 t, I32 e) { return c ? t : e; }
154
any(I32 c)155 SI bool any(I32 c) { return c != 0; }
all(I32 c)156 SI bool all(I32 c) { return c != 0; }
157
158 template <typename T>
gather(const T * p,U32 ix)159 SI T gather(const T* p, U32 ix) { return p[ix]; }
160
scatter_masked(I32 src,int * dst,U32 ix,I32 mask)161 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
162 dst[ix] = mask ? src : dst[ix];
163 }
164
load2(const uint16_t * ptr,U16 * r,U16 * g)165 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
166 *r = ptr[0];
167 *g = ptr[1];
168 }
store2(uint16_t * ptr,U16 r,U16 g)169 SI void store2(uint16_t* ptr, U16 r, U16 g) {
170 ptr[0] = r;
171 ptr[1] = g;
172 }
load4(const uint16_t * ptr,U16 * r,U16 * g,U16 * b,U16 * a)173 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
174 *r = ptr[0];
175 *g = ptr[1];
176 *b = ptr[2];
177 *a = ptr[3];
178 }
store4(uint16_t * ptr,U16 r,U16 g,U16 b,U16 a)179 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
180 ptr[0] = r;
181 ptr[1] = g;
182 ptr[2] = b;
183 ptr[3] = a;
184 }
185
load4(const float * ptr,F * r,F * g,F * b,F * a)186 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
187 *r = ptr[0];
188 *g = ptr[1];
189 *b = ptr[2];
190 *a = ptr[3];
191 }
store4(float * ptr,F r,F g,F b,F a)192 SI void store4(float* ptr, F r, F g, F b, F a) {
193 ptr[0] = r;
194 ptr[1] = g;
195 ptr[2] = b;
196 ptr[3] = a;
197 }
198
199 #elif defined(SKRP_CPU_NEON)
200 template <typename T> using V = Vec<4, T>;
201 using F = V<float >;
202 using I32 = V< int32_t>;
203 using U64 = V<uint64_t>;
204 using U32 = V<uint32_t>;
205 using U16 = V<uint16_t>;
206 using U8 = V<uint8_t >;
207
208 // We polyfill a few routines that Clang doesn't build into ext_vector_types.
209 SI F min(F a, F b) { return vminq_f32(a,b); }
210 SI I32 min(I32 a, I32 b) { return vminq_s32(a,b); }
211 SI U32 min(U32 a, U32 b) { return vminq_u32(a,b); }
212 SI F max(F a, F b) { return vmaxq_f32(a,b); }
213 SI I32 max(I32 a, I32 b) { return vmaxq_s32(a,b); }
214 SI U32 max(U32 a, U32 b) { return vmaxq_u32(a,b); }
215
216 SI F abs_ (F v) { return vabsq_f32(v); }
217 SI I32 abs_ (I32 v) { return vabsq_s32(v); }
218 SI F rcp_approx(F v) { auto e = vrecpeq_f32(v); return vrecpsq_f32 (v,e ) * e; }
219 SI F rcp_precise(F v) { auto e = rcp_approx(v); return vrecpsq_f32 (v,e ) * e; }
220 SI F rsqrt_approx(F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; }
221
222 SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); }
223 SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); }
224
225 SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); }
226 SI I32 if_then_else(I32 c, I32 t, I32 e) { return vbslq_s32((U32)c,t,e); }
227
228 #if defined(SK_CPU_ARM64)
229 SI bool any(I32 c) { return vmaxvq_u32((U32)c) != 0; }
230 SI bool all(I32 c) { return vminvq_u32((U32)c) != 0; }
231
232 SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); }
233 SI F nmad(F f, F m, F a) { return vfmsq_f32(a,f,m); }
234 SI F floor_(F v) { return vrndmq_f32(v); }
235 SI F ceil_(F v) { return vrndpq_f32(v); }
236 SI F sqrt_(F v) { return vsqrtq_f32(v); }
237 SI I32 iround(F v) { return vcvtnq_s32_f32(v); }
238 SI U32 round(F v) { return vcvtnq_u32_f32(v); }
239 SI U32 round(F v, F scale) { return vcvtnq_u32_f32(v*scale); }
240 #else
241 SI bool any(I32 c) { return c[0] | c[1] | c[2] | c[3]; }
242 SI bool all(I32 c) { return c[0] & c[1] & c[2] & c[3]; }
243
244 SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); }
245 SI F nmad(F f, F m, F a) { return vmlsq_f32(a,f,m); }
246
247 SI F floor_(F v) {
248 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
249 return roundtrip - if_then_else(roundtrip > v, F() + 1, F());
250 }
251
252 SI F ceil_(F v) {
253 F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v));
254 return roundtrip + if_then_else(roundtrip < v, F() + 1, F());
255 }
256
257 SI F sqrt_(F v) {
258 auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v).
259 e *= vrsqrtsq_f32(v,e*e);
260 e *= vrsqrtsq_f32(v,e*e);
261 return v*e; // sqrt(v) == v*rsqrt(v).
262 }
263
264 SI I32 iround(F v) {
265 return vcvtq_s32_f32(v + 0.5f);
266 }
267
268 SI U32 round(F v) {
269 return vcvtq_u32_f32(v + 0.5f);
270 }
271
272 SI U32 round(F v, F scale) {
273 return vcvtq_u32_f32(mad(v, scale, F() + 0.5f));
274 }
275 #endif
276
277 template <typename T>
278 SI V<T> gather(const T* p, U32 ix) {
279 return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
280 }
281 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
282 I32 before = gather(dst, ix);
283 I32 after = if_then_else(mask, src, before);
284 dst[ix[0]] = after[0];
285 dst[ix[1]] = after[1];
286 dst[ix[2]] = after[2];
287 dst[ix[3]] = after[3];
288 }
289 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
290 uint16x4x2_t rg = vld2_u16(ptr);
291 *r = rg.val[0];
292 *g = rg.val[1];
293 }
294 SI void store2(uint16_t* ptr, U16 r, U16 g) {
295 vst2_u16(ptr, (uint16x4x2_t{{r,g}}));
296 }
297 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
298 uint16x4x4_t rgba = vld4_u16(ptr);
299 *r = rgba.val[0];
300 *g = rgba.val[1];
301 *b = rgba.val[2];
302 *a = rgba.val[3];
303 }
304
305 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
306 vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}}));
307 }
308 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
309 float32x4x4_t rgba = vld4q_f32(ptr);
310 *r = rgba.val[0];
311 *g = rgba.val[1];
312 *b = rgba.val[2];
313 *a = rgba.val[3];
314 }
315 SI void store4(float* ptr, F r, F g, F b, F a) {
316 vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}}));
317 }
318
319 #elif defined(SKRP_CPU_SKX)
320 template <typename T> using V = Vec<16, T>;
321 using F = V<float >;
322 using I32 = V< int32_t>;
323 using U64 = V<uint64_t>;
324 using U32 = V<uint32_t>;
325 using U16 = V<uint16_t>;
326 using U8 = V<uint8_t >;
327
328 SI F mad(F f, F m, F a) { return _mm512_fmadd_ps(f, m, a); }
329 SI F nmad(F f, F m, F a) { return _mm512_fnmadd_ps(f, m, a); }
330 SI F min(F a, F b) { return _mm512_min_ps(a,b); }
331 SI I32 min(I32 a, I32 b) { return (I32)_mm512_min_epi32((__m512i)a,(__m512i)b); }
332 SI U32 min(U32 a, U32 b) { return (U32)_mm512_min_epu32((__m512i)a,(__m512i)b); }
333 SI F max(F a, F b) { return _mm512_max_ps(a,b); }
334 SI I32 max(I32 a, I32 b) { return (I32)_mm512_max_epi32((__m512i)a,(__m512i)b); }
335 SI U32 max(U32 a, U32 b) { return (U32)_mm512_max_epu32((__m512i)a,(__m512i)b); }
336 SI F abs_ (F v) { return _mm512_and_ps(v, _mm512_sub_ps(_mm512_setzero(), v)); }
337 SI I32 abs_ (I32 v) { return (I32)_mm512_abs_epi32((__m512i)v); }
338 SI F floor_(F v) { return _mm512_floor_ps(v); }
339 SI F ceil_(F v) { return _mm512_ceil_ps(v); }
340 SI F rcp_approx(F v) { return _mm512_rcp14_ps (v); }
341 SI F rsqrt_approx (F v) { return _mm512_rsqrt14_ps(v); }
342 SI F sqrt_ (F v) { return _mm512_sqrt_ps (v); }
343 SI F rcp_precise (F v) {
344 F e = rcp_approx(v);
345 return _mm512_fnmadd_ps(v, e, _mm512_set1_ps(2.0f)) * e;
346 }
347 SI I32 iround(F v) { return (I32)_mm512_cvtps_epi32(v); }
348 SI U32 round(F v) { return (U32)_mm512_cvtps_epi32(v); }
349 SI U32 round(F v, F scale) { return (U32)_mm512_cvtps_epi32(v*scale); }
350 SI U16 pack(U32 v) {
351 __m256i rst = _mm256_packus_epi32(_mm512_castsi512_si256((__m512i)v),
352 _mm512_extracti64x4_epi64((__m512i)v, 1));
353 return (U16)_mm256_permutex_epi64(rst, 216);
354 }
355 SI U8 pack(U16 v) {
356 __m256i rst = _mm256_packus_epi16((__m256i)v, (__m256i)v);
357 return (U8)_mm256_castsi256_si128(_mm256_permute4x64_epi64(rst, 8));
358 }
359 SI F if_then_else(I32 c, F t, F e) {
360 __m512i mask = _mm512_set1_epi32(0x80000000);
361 __m512i aa = _mm512_and_si512((__m512i)c, mask);
362 return _mm512_mask_blend_ps(_mm512_test_epi32_mask(aa, aa),e,t);
363 }
364 SI I32 if_then_else(I32 c, I32 t, I32 e) {
365 __m512i mask = _mm512_set1_epi32(0x80000000);
366 __m512i aa = _mm512_and_si512((__m512i)c, mask);
367 return (I32)_mm512_mask_blend_epi32(_mm512_test_epi32_mask(aa, aa),(__m512i)e,(__m512i)t);
368 }
369 SI bool any(I32 c) {
370 __mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c);
371 return mask32 != 0;
372 }
373 SI bool all(I32 c) {
374 __mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c);
375 return mask32 == 0xffff;
376 }
377 template <typename T>
378 SI V<T> gather(const T* p, U32 ix) {
379 return V<T>{ p[ix[ 0]], p[ix[ 1]], p[ix[ 2]], p[ix[ 3]],
380 p[ix[ 4]], p[ix[ 5]], p[ix[ 6]], p[ix[ 7]],
381 p[ix[ 8]], p[ix[ 9]], p[ix[10]], p[ix[11]],
382 p[ix[12]], p[ix[13]], p[ix[14]], p[ix[15]] };
383 }
384 SI F gather(const float* p, U32 ix) { return _mm512_i32gather_ps((__m512i)ix, p, 4); }
385 SI U32 gather(const uint32_t* p, U32 ix) {
386 return (U32)_mm512_i32gather_epi32((__m512i)ix, p, 4); }
387 SI U64 gather(const uint64_t* p, U32 ix) {
388 __m512i parts[] = {
389 _mm512_i32gather_epi64(_mm512_castsi512_si256((__m512i)ix), p, 8),
390 _mm512_i32gather_epi64(_mm512_extracti32x8_epi32((__m512i)ix, 1), p, 8),
391 };
392 return sk_bit_cast<U64>(parts);
393 }
394 template <typename V, typename S>
395 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
396 V before = gather(dst, ix);
397 V after = if_then_else(mask, src, before);
398 dst[ix[0]] = after[0];
399 dst[ix[1]] = after[1];
400 dst[ix[2]] = after[2];
401 dst[ix[3]] = after[3];
402 dst[ix[4]] = after[4];
403 dst[ix[5]] = after[5];
404 dst[ix[6]] = after[6];
405 dst[ix[7]] = after[7];
406 dst[ix[8]] = after[8];
407 dst[ix[9]] = after[9];
408 dst[ix[10]] = after[10];
409 dst[ix[11]] = after[11];
410 dst[ix[12]] = after[12];
411 dst[ix[13]] = after[13];
412 dst[ix[14]] = after[14];
413 dst[ix[15]] = after[15];
414 }
415
416 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
417 __m256i _01234567 = _mm256_loadu_si256(((const __m256i*)ptr) + 0);
418 __m256i _89abcdef = _mm256_loadu_si256(((const __m256i*)ptr) + 1);
419
420 *r = (U16)_mm256_permute4x64_epi64(_mm256_packs_epi32(_mm256_srai_epi32(_mm256_slli_epi32
421 (_01234567, 16), 16), _mm256_srai_epi32(_mm256_slli_epi32(_89abcdef, 16), 16)), 216);
422 *g = (U16)_mm256_permute4x64_epi64(_mm256_packs_epi32(_mm256_srai_epi32(_01234567, 16),
423 _mm256_srai_epi32(_89abcdef, 16)), 216);
424 }
425 SI void store2(uint16_t* ptr, U16 r, U16 g) {
426 __m256i _01234567 = _mm256_unpacklo_epi16((__m256i)r, (__m256i)g);
427 __m256i _89abcdef = _mm256_unpackhi_epi16((__m256i)r, (__m256i)g);
428 __m512i combinedVector = _mm512_inserti64x4(_mm512_castsi256_si512(_01234567),
429 _89abcdef, 1);
430 __m512i aa = _mm512_permutexvar_epi64(_mm512_setr_epi64(0,1,4,5,2,3,6,7), combinedVector);
431 _01234567 = _mm512_castsi512_si256(aa);
432 _89abcdef = _mm512_extracti64x4_epi64(aa, 1);
433
434 _mm256_storeu_si256((__m256i*)ptr + 0, _01234567);
435 _mm256_storeu_si256((__m256i*)ptr + 1, _89abcdef);
436 }
437
438 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
439 __m256i _0123 = _mm256_loadu_si256((const __m256i*)ptr),
440 _4567 = _mm256_loadu_si256(((const __m256i*)ptr) + 1),
441 _89ab = _mm256_loadu_si256(((const __m256i*)ptr) + 2),
442 _cdef = _mm256_loadu_si256(((const __m256i*)ptr) + 3);
443
444 auto a0 = _mm256_unpacklo_epi16(_0123, _4567),
445 a1 = _mm256_unpackhi_epi16(_0123, _4567),
446 b0 = _mm256_unpacklo_epi16(a0, a1),
447 b1 = _mm256_unpackhi_epi16(a0, a1),
448 a2 = _mm256_unpacklo_epi16(_89ab, _cdef),
449 a3 = _mm256_unpackhi_epi16(_89ab, _cdef),
450 b2 = _mm256_unpacklo_epi16(a2, a3),
451 b3 = _mm256_unpackhi_epi16(a2, a3),
452 rr = _mm256_unpacklo_epi64(b0, b2),
453 gg = _mm256_unpackhi_epi64(b0, b2),
454 bb = _mm256_unpacklo_epi64(b1, b3),
455 aa = _mm256_unpackhi_epi64(b1, b3);
456
457 *r = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), rr);
458 *g = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), gg);
459 *b = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), bb);
460 *a = (U16)_mm256_permutexvar_epi32(_mm256_setr_epi32(0,4,1,5,2,6,3,7), aa);
461 }
462 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
463 auto rg012389ab = _mm256_unpacklo_epi16((__m256i)r, (__m256i)g),
464 rg4567cdef = _mm256_unpackhi_epi16((__m256i)r, (__m256i)g),
465 ba012389ab = _mm256_unpacklo_epi16((__m256i)b, (__m256i)a),
466 ba4567cdef = _mm256_unpackhi_epi16((__m256i)b, (__m256i)a);
467
468 auto _0189 = _mm256_unpacklo_epi32(rg012389ab, ba012389ab),
469 _23ab = _mm256_unpackhi_epi32(rg012389ab, ba012389ab),
470 _45cd = _mm256_unpacklo_epi32(rg4567cdef, ba4567cdef),
471 _67ef = _mm256_unpackhi_epi32(rg4567cdef, ba4567cdef);
472
473 auto _ab23 = _mm256_permutex_epi64(_23ab, 78),
474 _0123 = _mm256_blend_epi32(_0189, _ab23, 0xf0),
475 _89ab = _mm256_permutex_epi64(_mm256_blend_epi32(_0189, _ab23, 0x0f), 78),
476 _ef67 = _mm256_permutex_epi64(_67ef, 78),
477 _4567 = _mm256_blend_epi32(_45cd, _ef67, 0xf0),
478 _cdef = _mm256_permutex_epi64(_mm256_blend_epi32(_45cd, _ef67, 0x0f), 78);
479
480 _mm256_storeu_si256((__m256i*)ptr, _0123);
481 _mm256_storeu_si256((__m256i*)ptr + 1, _4567);
482 _mm256_storeu_si256((__m256i*)ptr + 2, _89ab);
483 _mm256_storeu_si256((__m256i*)ptr + 3, _cdef);
484 }
485
486 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
487 F _048c, _159d, _26ae, _37bf;
488
489 _048c = _mm512_castps128_ps512(_mm_loadu_ps(ptr) );
490 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+16), 1);
491 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+32), 2);
492 _048c = _mm512_insertf32x4(_048c, _mm_loadu_ps(ptr+48), 3);
493 _159d = _mm512_castps128_ps512(_mm_loadu_ps(ptr+4) );
494 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+20), 1);
495 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+36), 2);
496 _159d = _mm512_insertf32x4(_159d, _mm_loadu_ps(ptr+52), 3);
497 _26ae = _mm512_castps128_ps512(_mm_loadu_ps(ptr+8) );
498 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+24), 1);
499 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+40), 2);
500 _26ae = _mm512_insertf32x4(_26ae, _mm_loadu_ps(ptr+56), 3);
501 _37bf = _mm512_castps128_ps512(_mm_loadu_ps(ptr+12) );
502 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+28), 1);
503 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+44), 2);
504 _37bf = _mm512_insertf32x4(_37bf, _mm_loadu_ps(ptr+60), 3);
505
506 F rg02468acf = _mm512_unpacklo_ps(_048c, _26ae),
507 ba02468acf = _mm512_unpackhi_ps(_048c, _26ae),
508 rg13579bde = _mm512_unpacklo_ps(_159d, _37bf),
509 ba13579bde = _mm512_unpackhi_ps(_159d, _37bf);
510
511 *r = (F)_mm512_unpacklo_ps(rg02468acf, rg13579bde);
512 *g = (F)_mm512_unpackhi_ps(rg02468acf, rg13579bde);
513 *b = (F)_mm512_unpacklo_ps(ba02468acf, ba13579bde);
514 *a = (F)_mm512_unpackhi_ps(ba02468acf, ba13579bde);
515 }
516
517 SI void store4(float* ptr, F r, F g, F b, F a) {
518 F rg014589cd = _mm512_unpacklo_ps(r, g),
519 rg2367abef = _mm512_unpackhi_ps(r, g),
520 ba014589cd = _mm512_unpacklo_ps(b, a),
521 ba2367abef = _mm512_unpackhi_ps(b, a);
522
523 F _048c = (F)_mm512_unpacklo_pd((__m512d)rg014589cd, (__m512d)ba014589cd),
524 _26ae = (F)_mm512_unpacklo_pd((__m512d)rg2367abef, (__m512d)ba2367abef),
525 _159d = (F)_mm512_unpackhi_pd((__m512d)rg014589cd, (__m512d)ba014589cd),
526 _37bf = (F)_mm512_unpackhi_pd((__m512d)rg2367abef, (__m512d)ba2367abef);
527
528 F _ae26 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_26ae),
529 _bf37 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_37bf),
530 _8c04 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_048c),
531 _9d15 = (F)_mm512_permutexvar_pd(_mm512_setr_epi64(4,5,6,7,0,1,2,3), (__m512d)_159d);
532
533 __m512i index = _mm512_setr_epi32(4,5,6,7,0,1,2,3,12,13,14,15,8,9,10,11);
534 F _0426 = (F)_mm512_permutex2var_pd((__m512d)_048c, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
535 (__m512d)_ae26),
536 _1537 = (F)_mm512_permutex2var_pd((__m512d)_159d, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
537 (__m512d)_bf37),
538 _5173 = _mm512_permutexvar_ps(index, _1537),
539 _0123 = (F)_mm512_permutex2var_pd((__m512d)_0426, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
540 (__m512d)_5173);
541
542 F _5476 = (F)_mm512_permutex2var_pd((__m512d)_5173, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
543 (__m512d)_0426),
544 _4567 = _mm512_permutexvar_ps(index, _5476),
545 _8cae = (F)_mm512_permutex2var_pd((__m512d)_8c04, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
546 (__m512d)_26ae),
547 _9dbf = (F)_mm512_permutex2var_pd((__m512d)_9d15, _mm512_setr_epi64(0,1,2,3,12,13,14,15),
548 (__m512d)_37bf),
549 _d9fb = _mm512_permutexvar_ps(index, _9dbf),
550 _89ab = (F)_mm512_permutex2var_pd((__m512d)_8cae, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
551 (__m512d)_d9fb),
552 _dcfe = (F)_mm512_permutex2var_pd((__m512d)_d9fb, _mm512_setr_epi64(0,1,10,11,4,5,14,15),
553 (__m512d)_8cae),
554 _cdef = _mm512_permutexvar_ps(index, _dcfe);
555
556 _mm512_storeu_ps(ptr+0, _0123);
557 _mm512_storeu_ps(ptr+16, _4567);
558 _mm512_storeu_ps(ptr+32, _89ab);
559 _mm512_storeu_ps(ptr+48, _cdef);
560 }
561
562 #elif defined(SKRP_CPU_HSW)
563 // These are __m256 and __m256i, but friendlier and strongly-typed.
564 template <typename T> using V = Vec<8, T>;
565 using F = V<float >;
566 using I32 = V< int32_t>;
567 using U64 = V<uint64_t>;
568 using U32 = V<uint32_t>;
569 using U16 = V<uint16_t>;
570 using U8 = V<uint8_t >;
571
572 SI F mad(F f, F m, F a) { return _mm256_fmadd_ps(f, m, a); }
573 SI F nmad(F f, F m, F a) { return _mm256_fnmadd_ps(f, m, a); }
574
575 SI F min(F a, F b) { return _mm256_min_ps(a,b); }
576 SI I32 min(I32 a, I32 b) { return (I32)_mm256_min_epi32((__m256i)a,(__m256i)b); }
577 SI U32 min(U32 a, U32 b) { return (U32)_mm256_min_epu32((__m256i)a,(__m256i)b); }
578 SI F max(F a, F b) { return _mm256_max_ps(a,b); }
579 SI I32 max(I32 a, I32 b) { return (I32)_mm256_max_epi32((__m256i)a,(__m256i)b); }
580 SI U32 max(U32 a, U32 b) { return (U32)_mm256_max_epu32((__m256i)a,(__m256i)b); }
581
582 SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); }
583 SI I32 abs_ (I32 v) { return (I32)_mm256_abs_epi32((__m256i)v); }
584 SI F floor_(F v) { return _mm256_floor_ps(v); }
585 SI F ceil_(F v) { return _mm256_ceil_ps(v); }
586 SI F rcp_approx(F v) { return _mm256_rcp_ps (v); } // use rcp_fast instead
587 SI F rsqrt_approx(F v) { return _mm256_rsqrt_ps(v); }
588 SI F sqrt_ (F v) { return _mm256_sqrt_ps (v); }
589 SI F rcp_precise (F v) {
590 F e = rcp_approx(v);
591 return _mm256_fnmadd_ps(v, e, _mm256_set1_ps(2.0f)) * e;
592 }
593
594 SI I32 iround(F v) { return (I32)_mm256_cvtps_epi32(v); }
595 SI U32 round(F v) { return (U32)_mm256_cvtps_epi32(v); }
596 SI U32 round(F v, F scale) { return (U32)_mm256_cvtps_epi32(v*scale); }
597 SI U16 pack(U32 v) {
598 return (U16)_mm_packus_epi32(_mm256_extractf128_si256((__m256i)v, 0),
599 _mm256_extractf128_si256((__m256i)v, 1));
600 }
601 SI U8 pack(U16 v) {
602 auto r = _mm_packus_epi16((__m128i)v,(__m128i)v);
603 return sk_unaligned_load<U8>(&r);
604 }
605
606 SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e, t, (__m256)c); }
607 SI I32 if_then_else(I32 c, I32 t, I32 e) {
608 return (I32)_mm256_blendv_ps((__m256)e, (__m256)t, (__m256)c);
609 }
610
611 // NOTE: This version of 'all' only works with mask values (true == all bits set)
612 SI bool any(I32 c) { return !_mm256_testz_si256((__m256i)c, _mm256_set1_epi32(-1)); }
613 SI bool all(I32 c) { return _mm256_testc_si256((__m256i)c, _mm256_set1_epi32(-1)); }
614
615 template <typename T>
616 SI V<T> gather(const T* p, U32 ix) {
617 return V<T>{ p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
618 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
619 }
620 SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps(p, (__m256i)ix, 4); }
621 SI U32 gather(const uint32_t* p, U32 ix) {
622 return (U32)_mm256_i32gather_epi32((const int*)p, (__m256i)ix, 4);
623 }
624 SI U64 gather(const uint64_t* p, U32 ix) {
625 __m256i parts[] = {
626 _mm256_i32gather_epi64(
627 (const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 0), 8),
628 _mm256_i32gather_epi64(
629 (const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 1), 8),
630 };
631 return sk_bit_cast<U64>(parts);
632 }
633 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
634 I32 before = gather(dst, ix);
635 I32 after = if_then_else(mask, src, before);
636 dst[ix[0]] = after[0];
637 dst[ix[1]] = after[1];
638 dst[ix[2]] = after[2];
639 dst[ix[3]] = after[3];
640 dst[ix[4]] = after[4];
641 dst[ix[5]] = after[5];
642 dst[ix[6]] = after[6];
643 dst[ix[7]] = after[7];
644 }
645
646 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
647 __m128i _0123 = _mm_loadu_si128(((const __m128i*)ptr) + 0),
648 _4567 = _mm_loadu_si128(((const __m128i*)ptr) + 1);
649 *r = (U16)_mm_packs_epi32(_mm_srai_epi32(_mm_slli_epi32(_0123, 16), 16),
650 _mm_srai_epi32(_mm_slli_epi32(_4567, 16), 16));
651 *g = (U16)_mm_packs_epi32(_mm_srai_epi32(_0123, 16),
652 _mm_srai_epi32(_4567, 16));
653 }
654 SI void store2(uint16_t* ptr, U16 r, U16 g) {
655 auto _0123 = _mm_unpacklo_epi16((__m128i)r, (__m128i)g),
656 _4567 = _mm_unpackhi_epi16((__m128i)r, (__m128i)g);
657 _mm_storeu_si128((__m128i*)ptr + 0, _0123);
658 _mm_storeu_si128((__m128i*)ptr + 1, _4567);
659 }
660
661 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
662 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0),
663 _23 = _mm_loadu_si128(((const __m128i*)ptr) + 1),
664 _45 = _mm_loadu_si128(((const __m128i*)ptr) + 2),
665 _67 = _mm_loadu_si128(((const __m128i*)ptr) + 3);
666
667 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
668 _13 = _mm_unpackhi_epi16(_01, _23), // r1 r3 g1 g3 b1 b3 a1 a3
669 _46 = _mm_unpacklo_epi16(_45, _67),
670 _57 = _mm_unpackhi_epi16(_45, _67);
671
672 auto rg0123 = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
673 ba0123 = _mm_unpackhi_epi16(_02, _13), // b0 b1 b2 b3 a0 a1 a2 a3
674 rg4567 = _mm_unpacklo_epi16(_46, _57),
675 ba4567 = _mm_unpackhi_epi16(_46, _57);
676
677 *r = (U16)_mm_unpacklo_epi64(rg0123, rg4567);
678 *g = (U16)_mm_unpackhi_epi64(rg0123, rg4567);
679 *b = (U16)_mm_unpacklo_epi64(ba0123, ba4567);
680 *a = (U16)_mm_unpackhi_epi64(ba0123, ba4567);
681 }
682 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
683 auto rg0123 = _mm_unpacklo_epi16((__m128i)r, (__m128i)g), // r0 g0 r1 g1 r2 g2 r3 g3
684 rg4567 = _mm_unpackhi_epi16((__m128i)r, (__m128i)g), // r4 g4 r5 g5 r6 g6 r7 g7
685 ba0123 = _mm_unpacklo_epi16((__m128i)b, (__m128i)a),
686 ba4567 = _mm_unpackhi_epi16((__m128i)b, (__m128i)a);
687
688 auto _01 = _mm_unpacklo_epi32(rg0123, ba0123),
689 _23 = _mm_unpackhi_epi32(rg0123, ba0123),
690 _45 = _mm_unpacklo_epi32(rg4567, ba4567),
691 _67 = _mm_unpackhi_epi32(rg4567, ba4567);
692
693 _mm_storeu_si128((__m128i*)ptr + 0, _01);
694 _mm_storeu_si128((__m128i*)ptr + 1, _23);
695 _mm_storeu_si128((__m128i*)ptr + 2, _45);
696 _mm_storeu_si128((__m128i*)ptr + 3, _67);
697 }
698
699 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
700 F _04 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 0)),
701 _15 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 4)),
702 _26 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+ 8)),
703 _37 = _mm256_castps128_ps256(_mm_loadu_ps(ptr+12));
704 _04 = _mm256_insertf128_ps(_04, _mm_loadu_ps(ptr+16), 1);
705 _15 = _mm256_insertf128_ps(_15, _mm_loadu_ps(ptr+20), 1);
706 _26 = _mm256_insertf128_ps(_26, _mm_loadu_ps(ptr+24), 1);
707 _37 = _mm256_insertf128_ps(_37, _mm_loadu_ps(ptr+28), 1);
708
709 F rg0145 = _mm256_unpacklo_ps(_04,_15), // r0 r1 g0 g1 | r4 r5 g4 g5
710 ba0145 = _mm256_unpackhi_ps(_04,_15),
711 rg2367 = _mm256_unpacklo_ps(_26,_37),
712 ba2367 = _mm256_unpackhi_ps(_26,_37);
713
714 *r = (F)_mm256_unpacklo_pd((__m256d)rg0145, (__m256d)rg2367);
715 *g = (F)_mm256_unpackhi_pd((__m256d)rg0145, (__m256d)rg2367);
716 *b = (F)_mm256_unpacklo_pd((__m256d)ba0145, (__m256d)ba2367);
717 *a = (F)_mm256_unpackhi_pd((__m256d)ba0145, (__m256d)ba2367);
718 }
719 SI void store4(float* ptr, F r, F g, F b, F a) {
720 F rg0145 = _mm256_unpacklo_ps(r, g), // r0 g0 r1 g1 | r4 g4 r5 g5
721 rg2367 = _mm256_unpackhi_ps(r, g), // r2 ... | r6 ...
722 ba0145 = _mm256_unpacklo_ps(b, a), // b0 a0 b1 a1 | b4 a4 b5 a5
723 ba2367 = _mm256_unpackhi_ps(b, a); // b2 ... | b6 ...
724
725 F _04 = (F)_mm256_unpacklo_pd((__m256d)rg0145, (__m256d)ba0145),// r0 g0 b0 a0 | r4 g4 b4 a4
726 _15 = (F)_mm256_unpackhi_pd((__m256d)rg0145, (__m256d)ba0145),// r1 ... | r5 ...
727 _26 = (F)_mm256_unpacklo_pd((__m256d)rg2367, (__m256d)ba2367),// r2 ... | r6 ...
728 _37 = (F)_mm256_unpackhi_pd((__m256d)rg2367, (__m256d)ba2367);// r3 ... | r7 ...
729
730 F _01 = _mm256_permute2f128_ps(_04, _15, 32), // 32 == 0010 0000 == lo, lo
731 _23 = _mm256_permute2f128_ps(_26, _37, 32),
732 _45 = _mm256_permute2f128_ps(_04, _15, 49), // 49 == 0011 0001 == hi, hi
733 _67 = _mm256_permute2f128_ps(_26, _37, 49);
734 _mm256_storeu_ps(ptr+ 0, _01);
735 _mm256_storeu_ps(ptr+ 8, _23);
736 _mm256_storeu_ps(ptr+16, _45);
737 _mm256_storeu_ps(ptr+24, _67);
738 }
739
740 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
741 template <typename T> using V = Vec<4, T>;
742 using F = V<float >;
743 using I32 = V< int32_t>;
744 using U64 = V<uint64_t>;
745 using U32 = V<uint32_t>;
746 using U16 = V<uint16_t>;
747 using U8 = V<uint8_t >;
748
749 SI F if_then_else(I32 c, F t, F e) {
750 return _mm_or_ps(_mm_and_ps((__m128)c, t), _mm_andnot_ps((__m128)c, e));
751 }
752 SI I32 if_then_else(I32 c, I32 t, I32 e) {
753 return (I32)_mm_or_ps(_mm_and_ps((__m128)c, (__m128)t),
754 _mm_andnot_ps((__m128)c, (__m128)e));
755 }
756
757 SI F min(F a, F b) { return _mm_min_ps(a,b); }
758 SI F max(F a, F b) { return _mm_max_ps(a,b); }
759 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
760 SI I32 min(I32 a, I32 b) { return (I32)_mm_min_epi32((__m128i)a,(__m128i)b); }
761 SI U32 min(U32 a, U32 b) { return (U32)_mm_min_epu32((__m128i)a,(__m128i)b); }
762 SI I32 max(I32 a, I32 b) { return (I32)_mm_max_epi32((__m128i)a,(__m128i)b); }
763 SI U32 max(U32 a, U32 b) { return (U32)_mm_max_epu32((__m128i)a,(__m128i)b); }
764 #else
765 SI I32 min(I32 a, I32 b) { return if_then_else(a < b, a, b); }
766 SI I32 max(I32 a, I32 b) { return if_then_else(a > b, a, b); }
767 SI U32 min(U32 a, U32 b) {
768 return sk_bit_cast<U32>(if_then_else(a < b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
769 }
770 SI U32 max(U32 a, U32 b) {
771 return sk_bit_cast<U32>(if_then_else(a > b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
772 }
773 #endif
774
775 SI F mad(F f, F m, F a) { return a+f*m; }
776 SI F nmad(F f, F m, F a) { return a-f*m; }
777 SI F abs_(F v) { return _mm_and_ps(v, 0-v); }
778 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
779 SI I32 abs_(I32 v) { return (I32)_mm_abs_epi32((__m128i)v); }
780 #else
781 SI I32 abs_(I32 v) { return max(v, -v); }
782 #endif
783 SI F rcp_approx(F v) { return _mm_rcp_ps (v); } // use rcp_fast instead
784 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * (2.0f - v * e); }
785 SI F rsqrt_approx(F v) { return _mm_rsqrt_ps(v); }
786 SI F sqrt_(F v) { return _mm_sqrt_ps (v); }
787
788 SI I32 iround(F v) { return (I32)_mm_cvtps_epi32(v); }
789 SI U32 round(F v) { return (U32)_mm_cvtps_epi32(v); }
790 SI U32 round(F v, F scale) { return (U32)_mm_cvtps_epi32(v*scale); }
791
792 SI U16 pack(U32 v) {
793 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
794 auto p = _mm_packus_epi32((__m128i)v,(__m128i)v);
795 #else
796 // Sign extend so that _mm_packs_epi32() does the pack we want.
797 auto p = _mm_srai_epi32(_mm_slli_epi32((__m128i)v, 16), 16);
798 p = _mm_packs_epi32(p,p);
799 #endif
800 return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
801 }
802 SI U8 pack(U16 v) {
803 auto r = widen_cast<__m128i>(v);
804 r = _mm_packus_epi16(r,r);
805 return sk_unaligned_load<U8>(&r);
806 }
807
808 // NOTE: This only checks the top bit of each lane, and is incorrect with non-mask values.
809 SI bool any(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) != 0b0000; }
810 SI bool all(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) == 0b1111; }
811
812 SI F floor_(F v) {
813 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
814 return _mm_floor_ps(v);
815 #else
816 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
817 return roundtrip - if_then_else(roundtrip > v, F() + 1, F() + 0);
818 #endif
819 }
820
821 SI F ceil_(F v) {
822 #if defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
823 return _mm_ceil_ps(v);
824 #else
825 F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v));
826 return roundtrip + if_then_else(roundtrip < v, F() + 1, F() + 0);
827 #endif
828 }
829
830 template <typename T>
831 SI V<T> gather(const T* p, U32 ix) {
832 return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
833 }
834 SI void scatter_masked(I32 src, int* dst, U32 ix, I32 mask) {
835 I32 before = gather(dst, ix);
836 I32 after = if_then_else(mask, src, before);
837 dst[ix[0]] = after[0];
838 dst[ix[1]] = after[1];
839 dst[ix[2]] = after[2];
840 dst[ix[3]] = after[3];
841 }
842 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
843 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0); // r0 g0 r1 g1 r2 g2 r3 g3
844 auto rg01_23 = _mm_shufflelo_epi16(_01, 0xD8); // r0 r1 g0 g1 r2 g2 r3 g3
845 auto rg = _mm_shufflehi_epi16(rg01_23, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3
846
847 auto R = _mm_shuffle_epi32(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3
848 auto G = _mm_shuffle_epi32(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3
849 *r = sk_unaligned_load<U16>(&R);
850 *g = sk_unaligned_load<U16>(&G);
851 }
852 SI void store2(uint16_t* ptr, U16 r, U16 g) {
853 __m128i rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g));
854 _mm_storeu_si128((__m128i*)ptr + 0, rg);
855 }
856
857 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
858 __m128i _01 = _mm_loadu_si128(((const __m128i*)ptr) + 0), // r0 g0 b0 a0 r1 g1 b1 a1
859 _23 = _mm_loadu_si128(((const __m128i*)ptr) + 1); // r2 g2 b2 a2 r3 g3 b3 a3
860
861 auto _02 = _mm_unpacklo_epi16(_01, _23), // r0 r2 g0 g2 b0 b2 a0 a2
862 _13 = _mm_unpackhi_epi16(_01, _23); // r1 r3 g1 g3 b1 b3 a1 a3
863
864 auto rg = _mm_unpacklo_epi16(_02, _13), // r0 r1 r2 r3 g0 g1 g2 g3
865 ba = _mm_unpackhi_epi16(_02, _13); // b0 b1 b2 b3 a0 a1 a2 a3
866
867 *r = sk_unaligned_load<U16>((uint16_t*)&rg + 0);
868 *g = sk_unaligned_load<U16>((uint16_t*)&rg + 4);
869 *b = sk_unaligned_load<U16>((uint16_t*)&ba + 0);
870 *a = sk_unaligned_load<U16>((uint16_t*)&ba + 4);
871 }
872
873 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
874 auto rg = _mm_unpacklo_epi16(widen_cast<__m128i>(r), widen_cast<__m128i>(g)),
875 ba = _mm_unpacklo_epi16(widen_cast<__m128i>(b), widen_cast<__m128i>(a));
876
877 _mm_storeu_si128((__m128i*)ptr + 0, _mm_unpacklo_epi32(rg, ba));
878 _mm_storeu_si128((__m128i*)ptr + 1, _mm_unpackhi_epi32(rg, ba));
879 }
880
881 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
882 F _0 = _mm_loadu_ps(ptr + 0),
883 _1 = _mm_loadu_ps(ptr + 4),
884 _2 = _mm_loadu_ps(ptr + 8),
885 _3 = _mm_loadu_ps(ptr +12);
886 _MM_TRANSPOSE4_PS(_0,_1,_2,_3);
887 *r = _0;
888 *g = _1;
889 *b = _2;
890 *a = _3;
891 }
892
893 SI void store4(float* ptr, F r, F g, F b, F a) {
894 _MM_TRANSPOSE4_PS(r,g,b,a);
895 _mm_storeu_ps(ptr + 0, r);
896 _mm_storeu_ps(ptr + 4, g);
897 _mm_storeu_ps(ptr + 8, b);
898 _mm_storeu_ps(ptr +12, a);
899 }
900
901 #elif defined(SKRP_CPU_LASX)
902 // These are __m256 and __m256i, but friendlier and strongly-typed.
903 template <typename T> using V = Vec<8, T>;
904 using F = V<float >;
905 using I32 = V<int32_t>;
906 using U64 = V<uint64_t>;
907 using U32 = V<uint32_t>;
908 using U16 = V<uint16_t>;
909 using U8 = V<uint8_t >;
910
911 SI __m128i emulate_lasx_d_xr2vr_l(__m256i a) {
912 v4i64 tmp = a;
913 v2i64 al = {tmp[0], tmp[1]};
914 return (__m128i)al;
915 }
916
917 SI __m128i emulate_lasx_d_xr2vr_h(__m256i a) {
918 v4i64 tmp = a;
919 v2i64 ah = {tmp[2], tmp[3]};
920 return (__m128i)ah;
921 }
922
923 SI F if_then_else(I32 c, F t, F e) {
924 return sk_bit_cast<Vec<8,float>>(__lasx_xvbitsel_v(sk_bit_cast<__m256i>(e),
925 sk_bit_cast<__m256i>(t),
926 sk_bit_cast<__m256i>(c)));
927 }
928
929 SI I32 if_then_else(I32 c, I32 t, I32 e) {
930 return sk_bit_cast<Vec<8,int32_t>>(__lasx_xvbitsel_v(sk_bit_cast<__m256i>(e),
931 sk_bit_cast<__m256i>(t),
932 sk_bit_cast<__m256i>(c)));
933 }
934
935 SI F min(F a, F b) { return __lasx_xvfmin_s(a,b); }
936 SI F max(F a, F b) { return __lasx_xvfmax_s(a,b); }
937 SI I32 min(I32 a, I32 b) { return __lasx_xvmin_w(a,b); }
938 SI U32 min(U32 a, U32 b) { return __lasx_xvmin_wu(a,b); }
939 SI I32 max(I32 a, I32 b) { return __lasx_xvmax_w(a,b); }
940 SI U32 max(U32 a, U32 b) { return __lasx_xvmax_wu(a,b); }
941
942 SI F mad(F f, F m, F a) { return __lasx_xvfmadd_s(f, m, a); }
943 SI F nmad(F f, F m, F a) { return __lasx_xvfmadd_s(-f, m, a); }
944 SI F abs_ (F v) { return (F)__lasx_xvand_v((I32)v, (I32)(0-v)); }
945 SI I32 abs_(I32 v) { return max(v, -v); }
946 SI F rcp_approx(F v) { return __lasx_xvfrecip_s(v); }
947 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
948 SI F rsqrt_approx (F v) { return __lasx_xvfrsqrt_s(v); }
949 SI F sqrt_(F v) { return __lasx_xvfsqrt_s(v); }
950
951 SI U32 iround(F v) {
952 F t = F() + 0.5f;
953 return __lasx_xvftintrz_w_s(v + t);
954 }
955
956 SI U32 round(F v) {
957 F t = F() + 0.5f;
958 return __lasx_xvftintrz_w_s(v + t);
959 }
960
961 SI U32 round(F v, F scale) {
962 F t = F() + 0.5f;
963 return __lasx_xvftintrz_w_s(mad(v, scale, t));
964 }
965
966 SI U16 pack(U32 v) {
967 return __lsx_vpickev_h(__lsx_vsat_wu(emulate_lasx_d_xr2vr_h(v), 15),
968 __lsx_vsat_wu(emulate_lasx_d_xr2vr_l(v), 15));
969 }
970
971 SI U8 pack(U16 v) {
972 __m128i tmp = __lsx_vsat_hu(v, 7);
973 auto r = __lsx_vpickev_b(tmp, tmp);
974 return sk_unaligned_load<U8>(&r);
975 }
976
977 SI bool any(I32 c){
978 v8i32 retv = (v8i32)__lasx_xvmskltz_w(__lasx_xvslt_wu(__lasx_xvldi(0), c));
979 return (retv[0] | retv[4]) != 0b0000;
980 }
981
982 SI bool all(I32 c){
983 v8i32 retv = (v8i32)__lasx_xvmskltz_w(__lasx_xvslt_wu(__lasx_xvldi(0), c));
984 return (retv[0] & retv[4]) == 0b1111;
985 }
986
987 SI F floor_(F v) {
988 return __lasx_xvfrintrm_s(v);
989 }
990
991 SI F ceil_(F v) {
992 return __lasx_xvfrintrp_s(v);
993 }
994
995 template <typename T>
996 SI V<T> gather(const T* p, U32 ix) {
997 return V<T>{ p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
998 p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
999 }
1000
1001 template <typename V, typename S>
1002 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
1003 V before = gather(dst, ix);
1004 V after = if_then_else(mask, src, before);
1005 dst[ix[0]] = after[0];
1006 dst[ix[1]] = after[1];
1007 dst[ix[2]] = after[2];
1008 dst[ix[3]] = after[3];
1009 dst[ix[4]] = after[4];
1010 dst[ix[5]] = after[5];
1011 dst[ix[6]] = after[6];
1012 dst[ix[7]] = after[7];
1013 }
1014
1015 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
1016 U16 _0123 = __lsx_vld(ptr, 0),
1017 _4567 = __lsx_vld(ptr, 16);
1018 *r = __lsx_vpickev_h(__lsx_vsat_w(__lsx_vsrai_w(__lsx_vslli_w(_4567, 16), 16), 15),
1019 __lsx_vsat_w(__lsx_vsrai_w(__lsx_vslli_w(_0123, 16), 16), 15));
1020 *g = __lsx_vpickev_h(__lsx_vsat_w(__lsx_vsrai_w(_4567, 16), 15),
1021 __lsx_vsat_w(__lsx_vsrai_w(_0123, 16), 15));
1022 }
1023 SI void store2(uint16_t* ptr, U16 r, U16 g) {
1024 auto _0123 = __lsx_vilvl_h(g, r),
1025 _4567 = __lsx_vilvh_h(g, r);
1026 __lsx_vst(_0123, ptr, 0);
1027 __lsx_vst(_4567, ptr, 16);
1028 }
1029
1030 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
1031 __m128i _01 = __lsx_vld(ptr, 0),
1032 _23 = __lsx_vld(ptr, 16),
1033 _45 = __lsx_vld(ptr, 32),
1034 _67 = __lsx_vld(ptr, 48);
1035
1036 auto _02 = __lsx_vilvl_h(_23, _01), // r0 r2 g0 g2 b0 b2 a0 a2
1037 _13 = __lsx_vilvh_h(_23, _01), // r1 r3 g1 g3 b1 b3 a1 a3
1038 _46 = __lsx_vilvl_h(_67, _45),
1039 _57 = __lsx_vilvh_h(_67, _45);
1040
1041 auto rg0123 = __lsx_vilvl_h(_13, _02), // r0 r1 r2 r3 g0 g1 g2 g3
1042 ba0123 = __lsx_vilvh_h(_13, _02), // b0 b1 b2 b3 a0 a1 a2 a3
1043 rg4567 = __lsx_vilvl_h(_57, _46),
1044 ba4567 = __lsx_vilvh_h(_57, _46);
1045
1046 *r = __lsx_vilvl_d(rg4567, rg0123);
1047 *g = __lsx_vilvh_d(rg4567, rg0123);
1048 *b = __lsx_vilvl_d(ba4567, ba0123);
1049 *a = __lsx_vilvh_d(ba4567, ba0123);
1050 }
1051
1052 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
1053 auto rg0123 = __lsx_vilvl_h(g, r), // r0 g0 r1 g1 r2 g2 r3 g3
1054 rg4567 = __lsx_vilvh_h(g, r), // r4 g4 r5 g5 r6 g6 r7 g7
1055 ba0123 = __lsx_vilvl_h(a, b),
1056 ba4567 = __lsx_vilvh_h(a, b);
1057
1058 auto _01 =__lsx_vilvl_w(ba0123, rg0123),
1059 _23 =__lsx_vilvh_w(ba0123, rg0123),
1060 _45 =__lsx_vilvl_w(ba4567, rg4567),
1061 _67 =__lsx_vilvh_w(ba4567, rg4567);
1062
1063 __lsx_vst(_01, ptr, 0);
1064 __lsx_vst(_23, ptr, 16);
1065 __lsx_vst(_45, ptr, 32);
1066 __lsx_vst(_67, ptr, 48);
1067 }
1068
1069 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
1070 F _04 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 0), __lasx_xvld(ptr, 64), 0x02);
1071 F _15 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 16), __lasx_xvld(ptr, 80), 0x02);
1072 F _26 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 32), __lasx_xvld(ptr, 96), 0x02);
1073 F _37 = (F)__lasx_xvpermi_q(__lasx_xvld(ptr, 48), __lasx_xvld(ptr, 112), 0x02);
1074
1075 F rg0145 = (F)__lasx_xvilvl_w((__m256i)_15, (__m256i)_04), // r0 r1 g0 g1 | r4 r5 g4 g5
1076 ba0145 = (F)__lasx_xvilvh_w((__m256i)_15, (__m256i)_04),
1077 rg2367 = (F)__lasx_xvilvl_w((__m256i)_37, (__m256i)_26),
1078 ba2367 = (F)__lasx_xvilvh_w((__m256i)_37, (__m256i)_26);
1079
1080 *r = (F)__lasx_xvilvl_d((__m256i)rg2367, (__m256i)rg0145);
1081 *g = (F)__lasx_xvilvh_d((__m256i)rg2367, (__m256i)rg0145);
1082 *b = (F)__lasx_xvilvl_d((__m256i)ba2367, (__m256i)ba0145);
1083 *a = (F)__lasx_xvilvh_d((__m256i)ba2367, (__m256i)ba0145);
1084 }
1085 SI void store4(float* ptr, F r, F g, F b, F a) {
1086 F rg0145 = (F)__lasx_xvilvl_w((__m256i)g, (__m256i)r), // r0 g0 r1 g1 | r4 g4 r5 g5
1087 rg2367 = (F)__lasx_xvilvh_w((__m256i)g, (__m256i)r), // r2 ... | r6 ...
1088 ba0145 = (F)__lasx_xvilvl_w((__m256i)a, (__m256i)b), // b0 a0 b1 a1 | b4 a4 b5 a5
1089 ba2367 = (F)__lasx_xvilvh_w((__m256i)a, (__m256i)b); // b2 ... | b6 ...
1090
1091 F _04 = (F)__lasx_xvilvl_d((__m256i)ba0145, (__m256i)rg0145), // r0 g0 b0 a0 | r4 g4 b4 a4
1092 _15 = (F)__lasx_xvilvh_d((__m256i)ba0145, (__m256i)rg0145), // r1 ... | r5 ...
1093 _26 = (F)__lasx_xvilvl_d((__m256i)ba2367, (__m256i)rg2367), // r2 ... | r6 ...
1094 _37 = (F)__lasx_xvilvh_d((__m256i)ba2367, (__m256i)rg2367); // r3 ... | r7 ...
1095
1096 F _01 = (F)__lasx_xvpermi_q((__m256i)_04, (__m256i)_15, 0x02),
1097 _23 = (F)__lasx_xvpermi_q((__m256i)_26, (__m256i)_37, 0x02),
1098 _45 = (F)__lasx_xvpermi_q((__m256i)_04, (__m256i)_15, 0x13),
1099 _67 = (F)__lasx_xvpermi_q((__m256i)_26, (__m256i)_37, 0x13);
1100 __lasx_xvst(_01, ptr, 0);
1101 __lasx_xvst(_23, ptr, 32);
1102 __lasx_xvst(_45, ptr, 64);
1103 __lasx_xvst(_67, ptr, 96);
1104 }
1105
1106 #elif defined(SKRP_CPU_LSX)
1107 template <typename T> using V = Vec<4, T>;
1108 using F = V<float >;
1109 using I32 = V<int32_t >;
1110 using U64 = V<uint64_t>;
1111 using U32 = V<uint32_t>;
1112 using U16 = V<uint16_t>;
1113 using U8 = V<uint8_t >;
1114
1115 #define _LSX_TRANSPOSE4_S(row0, row1, row2, row3) \
1116 do { \
1117 __m128 __t0 = (__m128)__lsx_vilvl_w ((__m128i)row1, (__m128i)row0); \
1118 __m128 __t1 = (__m128)__lsx_vilvl_w ((__m128i)row3, (__m128i)row2); \
1119 __m128 __t2 = (__m128)__lsx_vilvh_w ((__m128i)row1, (__m128i)row0); \
1120 __m128 __t3 = (__m128)__lsx_vilvh_w ((__m128i)row3, (__m128i)row2); \
1121 (row0) = (__m128)__lsx_vilvl_d ((__m128i)__t1, (__m128i)__t0); \
1122 (row1) = (__m128)__lsx_vilvh_d ((__m128i)__t1, (__m128i)__t0); \
1123 (row2) = (__m128)__lsx_vilvl_d ((__m128i)__t3, (__m128i)__t2); \
1124 (row3) = (__m128)__lsx_vilvh_d ((__m128i)__t3, (__m128i)__t2); \
1125 } while (0)
1126
1127 SI F if_then_else(I32 c, F t, F e) {
1128 return sk_bit_cast<Vec<4,float>>(__lsx_vbitsel_v(sk_bit_cast<__m128i>(e),
1129 sk_bit_cast<__m128i>(t),
1130 sk_bit_cast<__m128i>(c)));
1131 }
1132
1133 SI I32 if_then_else(I32 c, I32 t, I32 e) {
1134 return sk_bit_cast<Vec<4,int32_t>>(__lsx_vbitsel_v(sk_bit_cast<__m128i>(e),
1135 sk_bit_cast<__m128i>(t),
1136 sk_bit_cast<__m128i>(c)));
1137 }
1138
1139 SI F min(F a, F b) { return __lsx_vfmin_s(a,b); }
1140 SI F max(F a, F b) { return __lsx_vfmax_s(a,b); }
1141 SI I32 min(I32 a, I32 b) { return __lsx_vmin_w(a,b); }
1142 SI U32 min(U32 a, U32 b) { return __lsx_vmin_wu(a,b); }
1143 SI I32 max(I32 a, I32 b) { return __lsx_vmax_w(a,b); }
1144 SI U32 max(U32 a, U32 b) { return __lsx_vmax_wu(a,b); }
1145
1146 SI F mad(F f, F m, F a) { return __lsx_vfmadd_s(f, m, a); }
1147 SI F nmad(F f, F m, F a) { return __lsx_vfmadd_s(-f, m, a); }
1148 SI F abs_(F v) { return (F)__lsx_vand_v((I32)v, (I32)(0-v)); }
1149 SI I32 abs_(I32 v) { return max(v, -v); }
1150 SI F rcp_approx (F v) { return __lsx_vfrecip_s(v); }
1151 SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
1152 SI F rsqrt_approx (F v) { return __lsx_vfrsqrt_s(v); }
1153 SI F sqrt_(F v) { return __lsx_vfsqrt_s (v); }
1154
1155 SI U32 iround(F v) {
1156 F t = F() + 0.5f;
1157 return __lsx_vftintrz_w_s(v + t); }
1158
1159 SI U32 round(F v) {
1160 F t = F() + 0.5f;
1161 return __lsx_vftintrz_w_s(v + t); }
1162
1163 SI U32 round(F v, F scale) {
1164 F t = F() + 0.5f;
1165 return __lsx_vftintrz_w_s(mad(v, scale, t)); }
1166
1167 SI U16 pack(U32 v) {
1168 __m128i tmp = __lsx_vsat_wu(v, 15);
1169 auto p = __lsx_vpickev_h(tmp, tmp);
1170 return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
1171 }
1172
1173 SI U8 pack(U16 v) {
1174 auto r = widen_cast<__m128i>(v);
1175 __m128i tmp = __lsx_vsat_hu(r, 7);
1176 r = __lsx_vpickev_b(tmp, tmp);
1177 return sk_unaligned_load<U8>(&r);
1178 }
1179
1180 SI bool any(I32 c){
1181 v4i32 retv = (v4i32)__lsx_vmskltz_w(__lsx_vslt_wu(__lsx_vldi(0), c));
1182 return retv[0] != 0b0000;
1183 }
1184
1185 SI bool all(I32 c){
1186 v4i32 retv = (v4i32)__lsx_vmskltz_w(__lsx_vslt_wu(__lsx_vldi(0), c));
1187 return retv[0] == 0b1111;
1188 }
1189
1190 SI F floor_(F v) {
1191 return __lsx_vfrintrm_s(v);
1192 }
1193
1194 SI F ceil_(F v) {
1195 return __lsx_vfrintrp_s(v);
1196 }
1197
1198 template <typename T>
1199 SI V<T> gather(const T* p, U32 ix) {
1200 return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
1201 }
1202 // Using 'int*' prevents data from passing through floating-point registers.
1203 SI F gather(const int* p, int ix0, int ix1, int ix2, int ix3) {
1204 F ret = {0.0};
1205 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix0], 0);
1206 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix1], 1);
1207 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix2], 2);
1208 ret = (F)__lsx_vinsgr2vr_w(ret, p[ix3], 3);
1209 return ret;
1210 }
1211
1212 template <typename V, typename S>
1213 SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
1214 V before = gather(dst, ix);
1215 V after = if_then_else(mask, src, before);
1216 dst[ix[0]] = after[0];
1217 dst[ix[1]] = after[1];
1218 dst[ix[2]] = after[2];
1219 dst[ix[3]] = after[3];
1220 }
1221
1222 SI void load2(const uint16_t* ptr, U16* r, U16* g) {
1223 __m128i _01 = __lsx_vld(ptr, 0); // r0 g0 r1 g1 r2 g2 r3 g3
1224 auto rg = __lsx_vshuf4i_h(_01, 0xD8); // r0 r1 g0 g1 r2 r3 g2 g3
1225
1226 auto R = __lsx_vshuf4i_w(rg, 0x88); // r0 r1 r2 r3 r0 r1 r2 r3
1227 auto G = __lsx_vshuf4i_w(rg, 0xDD); // g0 g1 g2 g3 g0 g1 g2 g3
1228 *r = sk_unaligned_load<U16>(&R);
1229 *g = sk_unaligned_load<U16>(&G);
1230 }
1231
1232 SI void store2(uint16_t* ptr, U16 r, U16 g) {
1233 U32 rg = __lsx_vilvl_h(widen_cast<__m128i>(g), widen_cast<__m128i>(r));
1234 __lsx_vst(rg, ptr, 0);
1235 }
1236
1237 SI void load4(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
1238 __m128i _01 = __lsx_vld(ptr, 0), // r0 g0 b0 a0 r1 g1 b1 a1
1239 _23 = __lsx_vld(ptr, 16); // r2 g2 b2 a2 r3 g3 b3 a3
1240
1241 auto _02 = __lsx_vilvl_h(_23, _01), // r0 r2 g0 g2 b0 b2 a0 a2
1242 _13 = __lsx_vilvh_h(_23, _01); // r1 r3 g1 g3 b1 b3 a1 a3
1243
1244 auto rg = __lsx_vilvl_h(_13, _02), // r0 r1 r2 r3 g0 g1 g2 g3
1245 ba = __lsx_vilvh_h(_13, _02); // b0 b1 b2 b3 a0 a1 a2 a3
1246
1247 *r = sk_unaligned_load<U16>((uint16_t*)&rg + 0);
1248 *g = sk_unaligned_load<U16>((uint16_t*)&rg + 4);
1249 *b = sk_unaligned_load<U16>((uint16_t*)&ba + 0);
1250 *a = sk_unaligned_load<U16>((uint16_t*)&ba + 4);
1251 }
1252
1253 SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
1254 auto rg = __lsx_vilvl_h(widen_cast<__m128i>(g), widen_cast<__m128i>(r)),
1255 ba = __lsx_vilvl_h(widen_cast<__m128i>(a), widen_cast<__m128i>(b));
1256
1257 __lsx_vst(__lsx_vilvl_w(ba, rg), ptr, 0);
1258 __lsx_vst(__lsx_vilvh_w(ba, rg), ptr, 16);
1259 }
1260
1261 SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
1262 F _0 = (F)__lsx_vld(ptr, 0),
1263 _1 = (F)__lsx_vld(ptr, 16),
1264 _2 = (F)__lsx_vld(ptr, 32),
1265 _3 = (F)__lsx_vld(ptr, 48);
1266 _LSX_TRANSPOSE4_S(_0,_1,_2,_3);
1267 *r = _0;
1268 *g = _1;
1269 *b = _2;
1270 *a = _3;
1271 }
1272
1273 SI void store4(float* ptr, F r, F g, F b, F a) {
1274 _LSX_TRANSPOSE4_S(r,g,b,a);
1275 __lsx_vst(r, ptr, 0);
1276 __lsx_vst(g, ptr, 16);
1277 __lsx_vst(b, ptr, 32);
1278 __lsx_vst(a, ptr, 48);
1279 }
1280
1281 #endif
1282
1283 // Helpers to do scalar -> vector promotion on GCC (clang does this automatically)
1284 // We need to subtract (not add) zero to keep float conversion zero-cost. See:
1285 // https://stackoverflow.com/q/48255293
1286 //
1287 // The GCC implementation should be usable everywhere, but Mac clang (only) complains that the
1288 // expressions make these functions not constexpr.
1289 //
1290 // Further: We can't use the subtract-zero version in scalar mode. There, the subtraction will
1291 // really happen (at least at low optimization levels), which can alter the bit pattern of NaNs.
1292 // Because F_() is used when copying uniforms (even integer uniforms), this can corrupt values.
1293 // The vector subtraction of zero doesn't appear to ever alter NaN bit patterns.
1294 #if defined(__clang__) || defined(SKRP_CPU_SCALAR)
F_(float x)1295 SI constexpr F F_(float x) { return x; }
I32_(int32_t x)1296 SI constexpr I32 I32_(int32_t x) { return x; }
U32_(uint32_t x)1297 SI constexpr U32 U32_(uint32_t x) { return x; }
1298 #else
F_(float x)1299 SI constexpr F F_(float x) { return x - F(); }
I32_(int32_t x)1300 SI constexpr I32 I32_(int32_t x) { return x + I32(); }
U32_(uint32_t x)1301 SI constexpr U32 U32_(uint32_t x) { return x + U32(); }
1302 #endif
1303
1304 // Extremely helpful literals:
1305 static constexpr F F0 = F_(0.0f),
1306 F1 = F_(1.0f);
1307
1308 #if !defined(SKRP_CPU_SCALAR)
min(F a,float b)1309 SI F min(F a, float b) { return min(a, F_(b)); }
min(float a,F b)1310 SI F min(float a, F b) { return min(F_(a), b); }
max(F a,float b)1311 SI F max(F a, float b) { return max(a, F_(b)); }
max(float a,F b)1312 SI F max(float a, F b) { return max(F_(a), b); }
1313
mad(F f,F m,float a)1314 SI F mad(F f, F m, float a) { return mad(f, m, F_(a)); }
mad(F f,float m,F a)1315 SI F mad(F f, float m, F a) { return mad(f, F_(m), a); }
mad(F f,float m,float a)1316 SI F mad(F f, float m, float a) { return mad(f, F_(m), F_(a)); }
mad(float f,F m,F a)1317 SI F mad(float f, F m, F a) { return mad(F_(f), m, a); }
mad(float f,F m,float a)1318 SI F mad(float f, F m, float a) { return mad(F_(f), m, F_(a)); }
mad(float f,float m,F a)1319 SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a); }
1320
nmad(F f,F m,float a)1321 SI F nmad(F f, F m, float a) { return nmad(f, m, F_(a)); }
nmad(F f,float m,F a)1322 SI F nmad(F f, float m, F a) { return nmad(f, F_(m), a); }
nmad(F f,float m,float a)1323 SI F nmad(F f, float m, float a) { return nmad(f, F_(m), F_(a)); }
nmad(float f,F m,F a)1324 SI F nmad(float f, F m, F a) { return nmad(F_(f), m, a); }
nmad(float f,F m,float a)1325 SI F nmad(float f, F m, float a) { return nmad(F_(f), m, F_(a)); }
nmad(float f,float m,F a)1326 SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a); }
1327 #endif
1328
1329 // We need to be a careful with casts.
1330 // (F)x means cast x to float in the portable path, but bit_cast x to float in the others.
1331 // These named casts and bit_cast() are always what they seem to be.
1332 #if defined(SKRP_CPU_SCALAR)
cast(U32 v)1333 SI F cast (U32 v) { return (F)v; }
cast64(U64 v)1334 SI F cast64(U64 v) { return (F)v; }
trunc_(F v)1335 SI U32 trunc_(F v) { return (U32)v; }
expand(U16 v)1336 SI U32 expand(U16 v) { return (U32)v; }
expand(U8 v)1337 SI U32 expand(U8 v) { return (U32)v; }
1338 #else
cast(U32 v)1339 SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); }
cast64(U64 v)1340 SI F cast64(U64 v) { return __builtin_convertvector( v, F); }
trunc_(F v)1341 SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); }
expand(U16 v)1342 SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); }
expand(U8 v)1343 SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); }
1344 #endif
1345
1346 #if !defined(SKRP_CPU_SCALAR)
if_then_else(I32 c,F t,float e)1347 SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
if_then_else(I32 c,float t,F e)1348 SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
if_then_else(I32 c,float t,float e)1349 SI F if_then_else(I32 c, float t, float e) { return if_then_else(c, F_(t), F_(e)); }
1350 #endif
1351
fract(F v)1352 SI F fract(F v) { return v - floor_(v); }
1353
1354 // See http://www.machinedlearnings.com/2011/06/fast-approximate-logarithm-exponential.html
approx_log2(F x)1355 SI F approx_log2(F x) {
1356 // e - 127 is a fair approximation of log2(x) in its own right...
1357 F e = cast(sk_bit_cast<U32>(x)) * (1.0f / (1<<23));
1358
1359 // ... but using the mantissa to refine its error is _much_ better.
1360 F m = sk_bit_cast<F>((sk_bit_cast<U32>(x) & 0x007fffff) | 0x3f000000);
1361
1362 return nmad(m, 1.498030302f, e - 124.225514990f) - 1.725879990f / (0.3520887068f + m);
1363 }
1364
approx_log(F x)1365 SI F approx_log(F x) {
1366 const float ln2 = 0.69314718f;
1367 return ln2 * approx_log2(x);
1368 }
1369
approx_pow2(F x)1370 SI F approx_pow2(F x) {
1371 constexpr float kInfinityBits = 0x7f800000;
1372
1373 F f = fract(x);
1374 F approx = nmad(f, 1.490129070f, x + 121.274057500f);
1375 approx += 27.728023300f / (4.84252568f - f);
1376 approx *= 1.0f * (1<<23);
1377 approx = min(max(approx, F0), F_(kInfinityBits)); // guard against underflow/overflow
1378
1379 return sk_bit_cast<F>(round(approx));
1380 }
1381
approx_exp(F x)1382 SI F approx_exp(F x) {
1383 const float log2_e = 1.4426950408889634074f;
1384 return approx_pow2(log2_e * x);
1385 }
1386
approx_powf(F x,F y)1387 SI F approx_powf(F x, F y) {
1388 return if_then_else((x == 0)|(x == 1), x
1389 , approx_pow2(approx_log2(x) * y));
1390 }
1391 #if !defined(SKRP_CPU_SCALAR)
approx_powf(F x,float y)1392 SI F approx_powf(F x, float y) { return approx_powf(x, F_(y)); }
1393 #endif
1394
from_half(U16 h)1395 SI F from_half(U16 h) {
1396 #if defined(SKRP_CPU_NEON) && defined(SK_CPU_ARM64)
1397 return vcvt_f32_f16((float16x4_t)h);
1398
1399 #elif defined(SKRP_CPU_SKX)
1400 return _mm512_cvtph_ps((__m256i)h);
1401
1402 #elif defined(SKRP_CPU_HSW)
1403 return _mm256_cvtph_ps((__m128i)h);
1404
1405 #else
1406 // Remember, a half is 1-5-10 (sign-exponent-mantissa) with 15 exponent bias.
1407 U32 sem = expand(h),
1408 s = sem & 0x8000,
1409 em = sem ^ s;
1410
1411 // Convert to 1-8-23 float with 127 bias, flushing denorm halfs (including zero) to zero.
1412 auto denorm = (I32)em < 0x0400; // I32 comparison is often quicker, and always safe here.
1413 return if_then_else(denorm, F0
1414 , sk_bit_cast<F>( (s<<16) + (em<<13) + ((127-15)<<23) ));
1415 #endif
1416 }
1417
to_half(F f)1418 SI U16 to_half(F f) {
1419 #if defined(SKRP_CPU_NEON) && defined(SK_CPU_ARM64)
1420 return (U16)vcvt_f16_f32(f);
1421
1422 #elif defined(SKRP_CPU_SKX)
1423 return (U16)_mm512_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
1424
1425 #elif defined(SKRP_CPU_HSW)
1426 return (U16)_mm256_cvtps_ph(f, _MM_FROUND_CUR_DIRECTION);
1427
1428 #else
1429 // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
1430 U32 sem = sk_bit_cast<U32>(f),
1431 s = sem & 0x80000000,
1432 em = sem ^ s;
1433
1434 // Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero.
1435 auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here.
1436 return pack((U32)if_then_else(denorm, I32_(0)
1437 , (I32)((s>>16) + (em>>13) - ((127-15)<<10))));
1438 #endif
1439 }
1440
patch_memory_contexts(SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,size_t dx,size_t dy,size_t tail)1441 static void patch_memory_contexts(SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,
1442 size_t dx, size_t dy, size_t tail) {
1443 for (SkRasterPipeline_MemoryCtxPatch& patch : memoryCtxPatches) {
1444 SkRasterPipeline_MemoryCtx* ctx = patch.info.context;
1445
1446 const ptrdiff_t offset = patch.info.bytesPerPixel * (dy * ctx->stride + dx);
1447 if (patch.info.load) {
1448 void* ctxData = SkTAddOffset<void>(ctx->pixels, offset);
1449 memcpy(patch.scratch, ctxData, patch.info.bytesPerPixel * tail);
1450 }
1451
1452 SkASSERT(patch.backup == nullptr);
1453 void* scratchFakeBase = SkTAddOffset<void>(patch.scratch, -offset);
1454 patch.backup = ctx->pixels;
1455 ctx->pixels = scratchFakeBase;
1456 }
1457 }
1458
restore_memory_contexts(SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,size_t dx,size_t dy,size_t tail)1459 static void restore_memory_contexts(SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,
1460 size_t dx, size_t dy, size_t tail) {
1461 for (SkRasterPipeline_MemoryCtxPatch& patch : memoryCtxPatches) {
1462 SkRasterPipeline_MemoryCtx* ctx = patch.info.context;
1463
1464 SkASSERT(patch.backup != nullptr);
1465 ctx->pixels = patch.backup;
1466 patch.backup = nullptr;
1467
1468 const ptrdiff_t offset = patch.info.bytesPerPixel * (dy * ctx->stride + dx);
1469 if (patch.info.store) {
1470 void* ctxData = SkTAddOffset<void>(ctx->pixels, offset);
1471 memcpy(ctxData, patch.scratch, patch.info.bytesPerPixel * tail);
1472 }
1473 }
1474 }
1475
1476 #if defined(SKRP_CPU_SCALAR) || defined(SKRP_CPU_SSE2)
1477 // In scalar and SSE2 mode, we always use precise math so we can have more predictable results.
1478 // Chrome will use the SSE2 implementation when --disable-skia-runtime-opts is set. (b/40042946)
rcp_fast(F v)1479 SI F rcp_fast(F v) { return rcp_precise(v); }
rsqrt(F v)1480 SI F rsqrt(F v) { return rcp_precise(sqrt_(v)); }
1481 #else
rcp_fast(F v)1482 SI F rcp_fast(F v) { return rcp_approx(v); }
rsqrt(F v)1483 SI F rsqrt(F v) { return rsqrt_approx(v); }
1484 #endif
1485
1486 // Our fundamental vector depth is our pixel stride.
1487 static constexpr size_t N = sizeof(F) / sizeof(float);
1488
1489 // We're finally going to get to what a Stage function looks like!
1490
1491 // Any custom ABI to use for all (non-externally-facing) stage functions?
1492 // Also decide here whether to use narrow (compromise) or wide (ideal) stages.
1493 #if defined(SK_CPU_ARM32) && defined(SKRP_CPU_NEON)
1494 // This lets us pass vectors more efficiently on 32-bit ARM.
1495 // We can still only pass 16 floats, so best as 4x {r,g,b,a}.
1496 #define ABI __attribute__((pcs("aapcs-vfp")))
1497 #define SKRP_NARROW_STAGES 1
1498 #elif defined(_MSC_VER)
1499 // Even if not vectorized, this lets us pass {r,g,b,a} as registers,
1500 // instead of {b,a} on the stack. Narrow stages work best for __vectorcall.
1501 #define ABI __vectorcall
1502 #define SKRP_NARROW_STAGES 1
1503 #elif defined(__x86_64__) || defined(SK_CPU_ARM64) || defined(SK_CPU_LOONGARCH)
1504 // These platforms are ideal for wider stages, and their default ABI is ideal.
1505 #define ABI
1506 #define SKRP_NARROW_STAGES 0
1507 #else
1508 // 32-bit or unknown... shunt them down the narrow path.
1509 // Odds are these have few registers and are better off there.
1510 #define ABI
1511 #define SKRP_NARROW_STAGES 1
1512 #endif
1513
1514 #if SKRP_NARROW_STAGES
1515 struct Params {
1516 size_t dx, dy;
1517 std::byte* base;
1518 F dr,dg,db,da;
1519 };
1520 using Stage = void(ABI*)(Params*, SkRasterPipelineStage* program, F r, F g, F b, F a);
1521 #else
1522 using Stage = void(ABI*)(SkRasterPipelineStage* program, size_t dx, size_t dy,
1523 std::byte* base, F,F,F,F, F,F,F,F);
1524 #endif
1525
start_pipeline(size_t dx,size_t dy,size_t xlimit,size_t ylimit,SkRasterPipelineStage * program,SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,uint8_t * tailPointer)1526 static void start_pipeline(size_t dx, size_t dy,
1527 size_t xlimit, size_t ylimit,
1528 SkRasterPipelineStage* program,
1529 SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,
1530 uint8_t* tailPointer) {
1531 uint8_t unreferencedTail;
1532 if (!tailPointer) {
1533 tailPointer = &unreferencedTail;
1534 }
1535 auto start = (Stage)program->fn;
1536 const size_t x0 = dx;
1537 std::byte* const base = nullptr;
1538 for (; dy < ylimit; dy++) {
1539 #if SKRP_NARROW_STAGES
1540 Params params = { x0,dy,base, F0,F0,F0,F0 };
1541 while (params.dx + N <= xlimit) {
1542 start(¶ms,program, F0,F0,F0,F0);
1543 params.dx += N;
1544 }
1545 if (size_t tail = xlimit - params.dx) {
1546 *tailPointer = tail;
1547 patch_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
1548 start(¶ms,program, F0,F0,F0,F0);
1549 restore_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
1550 *tailPointer = 0xFF;
1551 }
1552 #else
1553 dx = x0;
1554 while (dx + N <= xlimit) {
1555 start(program,dx,dy,base, F0,F0,F0,F0, F0,F0,F0,F0);
1556 dx += N;
1557 }
1558 if (size_t tail = xlimit - dx) {
1559 *tailPointer = tail;
1560 patch_memory_contexts(memoryCtxPatches, dx, dy, tail);
1561 start(program,dx,dy,base, F0,F0,F0,F0, F0,F0,F0,F0);
1562 restore_memory_contexts(memoryCtxPatches, dx, dy, tail);
1563 *tailPointer = 0xFF;
1564 }
1565 #endif
1566 }
1567 }
1568
1569 #if SK_HAS_MUSTTAIL
1570 #define SKRP_MUSTTAIL [[clang::musttail]]
1571 #else
1572 #define SKRP_MUSTTAIL
1573 #endif
1574
1575 #if SKRP_NARROW_STAGES
1576 #define DECLARE_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
1577 SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, std::byte*& base, \
1578 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
1579 static void ABI name(Params* params, SkRasterPipelineStage* program, \
1580 F r, F g, F b, F a) { \
1581 OFFSET name##_k(Ctx{program}, params->dx,params->dy,params->base, \
1582 r,g,b,a, params->dr, params->dg, params->db, params->da); \
1583 INC; \
1584 auto fn = (Stage)program->fn; \
1585 MUSTTAIL return fn(params, program, r,g,b,a); \
1586 } \
1587 SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, std::byte*& base, \
1588 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1589 #else
1590 #define DECLARE_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
1591 SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, std::byte*& base, \
1592 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da); \
1593 static void ABI name(SkRasterPipelineStage* program, size_t dx, size_t dy, \
1594 std::byte* base, F r, F g, F b, F a, F dr, F dg, F db, F da) { \
1595 OFFSET name##_k(Ctx{program}, dx,dy,base, r,g,b,a, dr,dg,db,da); \
1596 INC; \
1597 auto fn = (Stage)program->fn; \
1598 MUSTTAIL return fn(program, dx,dy,base, r,g,b,a, dr,dg,db,da); \
1599 } \
1600 SI STAGE_RET name##_k(ARG, size_t dx, size_t dy, std::byte*& base, \
1601 F& r, F& g, F& b, F& a, F& dr, F& dg, F& db, F& da)
1602 #endif
1603
1604 // A typical stage returns void, always increments the program counter by 1, and lets the optimizer
1605 // decide whether or not tail-calling is appropriate.
1606 #define STAGE(name, arg) \
1607 DECLARE_STAGE(name, arg, void, ++program, /*no offset*/, /*no musttail*/)
1608
1609 // A tail stage returns void, always increments the program counter by 1, and uses tail-calling.
1610 // Tail-calling is necessary in SkSL-generated programs, which can be thousands of ops long, and
1611 // could overflow the stack (particularly in debug).
1612 #define STAGE_TAIL(name, arg) \
1613 DECLARE_STAGE(name, arg, void, ++program, /*no offset*/, SKRP_MUSTTAIL)
1614
1615 // A branch stage returns an integer, which is added directly to the program counter, and tailcalls.
1616 #define STAGE_BRANCH(name, arg) \
1617 DECLARE_STAGE(name, arg, int, /*no increment*/, program +=, SKRP_MUSTTAIL)
1618
1619 // just_return() is a simple no-op stage that only exists to end the chain,
1620 // returning back up to start_pipeline(), and from there to the caller.
1621 #if SKRP_NARROW_STAGES
just_return(Params *,SkRasterPipelineStage *,F,F,F,F)1622 static void ABI just_return(Params*, SkRasterPipelineStage*, F,F,F,F) {}
1623 #else
just_return(SkRasterPipelineStage *,size_t,size_t,std::byte *,F,F,F,F,F,F,F,F)1624 static void ABI just_return(SkRasterPipelineStage*, size_t,size_t, std::byte*,
1625 F,F,F,F, F,F,F,F) {}
1626 #endif
1627
1628 // Note that in release builds, most stages consume no stack (thanks to tail call optimization).
1629 // However: certain builds (especially with non-clang compilers) may fail to optimize tail
1630 // calls, resulting in actual stack frames being generated.
1631 //
1632 // stack_checkpoint() and stack_rewind() are special stages that can be used to manage stack growth.
1633 // If a pipeline contains a stack_checkpoint, followed by any number of stack_rewind (at any point),
1634 // the C++ stack will be reset to the state it was at when the stack_checkpoint was initially hit.
1635 //
1636 // All instances of stack_rewind (as well as the one instance of stack_checkpoint near the start of
1637 // a pipeline) share a single context (of type SkRasterPipeline_RewindCtx). That context holds the
1638 // full state of the mutable registers that are normally passed to the next stage in the program.
1639 //
1640 // stack_rewind is the only stage other than just_return that actually returns (rather than jumping
1641 // to the next stage in the program). Before it does so, it stashes all of the registers in the
1642 // context. This includes the updated `program` pointer. Unlike stages that tail call exactly once,
1643 // stack_checkpoint calls the next stage in the program repeatedly, as long as the `program` in the
1644 // context is overwritten (i.e., as long as a stack_rewind was the reason the pipeline returned,
1645 // rather than a just_return).
1646 //
1647 // Normally, just_return is the only stage that returns, and no other stage does anything after a
1648 // subsequent (called) stage returns, so the stack just unwinds all the way to start_pipeline.
1649 // With stack_checkpoint on the stack, any stack_rewind stages will return all the way up to the
1650 // stack_checkpoint. That grabs the values that would have been passed to the next stage (from the
1651 // context), and continues the linear execution of stages, but has reclaimed all of the stack frames
1652 // pushed before the stack_rewind before doing so.
1653 #if SKRP_NARROW_STAGES
stack_checkpoint(Params * params,SkRasterPipelineStage * program,F r,F g,F b,F a)1654 static void ABI stack_checkpoint(Params* params, SkRasterPipelineStage* program,
1655 F r, F g, F b, F a) {
1656 SkRasterPipeline_RewindCtx* ctx = Ctx{program};
1657 while (program) {
1658 auto next = (Stage)(++program)->fn;
1659
1660 ctx->stage = nullptr;
1661 next(params, program, r, g, b, a);
1662 program = ctx->stage;
1663
1664 if (program) {
1665 r = sk_unaligned_load<F>(ctx->r );
1666 g = sk_unaligned_load<F>(ctx->g );
1667 b = sk_unaligned_load<F>(ctx->b );
1668 a = sk_unaligned_load<F>(ctx->a );
1669 params->dr = sk_unaligned_load<F>(ctx->dr);
1670 params->dg = sk_unaligned_load<F>(ctx->dg);
1671 params->db = sk_unaligned_load<F>(ctx->db);
1672 params->da = sk_unaligned_load<F>(ctx->da);
1673 params->base = ctx->base;
1674 }
1675 }
1676 }
stack_rewind(Params * params,SkRasterPipelineStage * program,F r,F g,F b,F a)1677 static void ABI stack_rewind(Params* params, SkRasterPipelineStage* program,
1678 F r, F g, F b, F a) {
1679 SkRasterPipeline_RewindCtx* ctx = Ctx{program};
1680 sk_unaligned_store(ctx->r , r );
1681 sk_unaligned_store(ctx->g , g );
1682 sk_unaligned_store(ctx->b , b );
1683 sk_unaligned_store(ctx->a , a );
1684 sk_unaligned_store(ctx->dr, params->dr);
1685 sk_unaligned_store(ctx->dg, params->dg);
1686 sk_unaligned_store(ctx->db, params->db);
1687 sk_unaligned_store(ctx->da, params->da);
1688 ctx->base = params->base;
1689 ctx->stage = program;
1690 }
1691 #else
stack_checkpoint(SkRasterPipelineStage * program,size_t dx,size_t dy,std::byte * base,F r,F g,F b,F a,F dr,F dg,F db,F da)1692 static void ABI stack_checkpoint(SkRasterPipelineStage* program,
1693 size_t dx, size_t dy, std::byte* base,
1694 F r, F g, F b, F a, F dr, F dg, F db, F da) {
1695 SkRasterPipeline_RewindCtx* ctx = Ctx{program};
1696 while (program) {
1697 auto next = (Stage)(++program)->fn;
1698
1699 ctx->stage = nullptr;
1700 next(program, dx, dy, base, r, g, b, a, dr, dg, db, da);
1701 program = ctx->stage;
1702
1703 if (program) {
1704 r = sk_unaligned_load<F>(ctx->r );
1705 g = sk_unaligned_load<F>(ctx->g );
1706 b = sk_unaligned_load<F>(ctx->b );
1707 a = sk_unaligned_load<F>(ctx->a );
1708 dr = sk_unaligned_load<F>(ctx->dr);
1709 dg = sk_unaligned_load<F>(ctx->dg);
1710 db = sk_unaligned_load<F>(ctx->db);
1711 da = sk_unaligned_load<F>(ctx->da);
1712 base = ctx->base;
1713 }
1714 }
1715 }
stack_rewind(SkRasterPipelineStage * program,size_t dx,size_t dy,std::byte * base,F r,F g,F b,F a,F dr,F dg,F db,F da)1716 static void ABI stack_rewind(SkRasterPipelineStage* program,
1717 size_t dx, size_t dy, std::byte* base,
1718 F r, F g, F b, F a, F dr, F dg, F db, F da) {
1719 SkRasterPipeline_RewindCtx* ctx = Ctx{program};
1720 sk_unaligned_store(ctx->r , r );
1721 sk_unaligned_store(ctx->g , g );
1722 sk_unaligned_store(ctx->b , b );
1723 sk_unaligned_store(ctx->a , a );
1724 sk_unaligned_store(ctx->dr, dr);
1725 sk_unaligned_store(ctx->dg, dg);
1726 sk_unaligned_store(ctx->db, db);
1727 sk_unaligned_store(ctx->da, da);
1728 ctx->base = base;
1729 ctx->stage = program;
1730 }
1731 #endif
1732
1733
1734 // We could start defining normal Stages now. But first, some helper functions.
1735
1736 template <typename V, typename T>
load(const T * src)1737 SI V load(const T* src) {
1738 return sk_unaligned_load<V>(src);
1739 }
1740
1741 template <typename V, typename T>
store(T * dst,V v)1742 SI void store(T* dst, V v) {
1743 sk_unaligned_store(dst, v);
1744 }
1745
from_byte(U8 b)1746 SI F from_byte(U8 b) {
1747 return cast(expand(b)) * (1/255.0f);
1748 }
from_short(U16 s)1749 SI F from_short(U16 s) {
1750 return cast(expand(s)) * (1/65535.0f);
1751 }
from_565(U16 _565,F * r,F * g,F * b)1752 SI void from_565(U16 _565, F* r, F* g, F* b) {
1753 U32 wide = expand(_565);
1754 *r = cast(wide & (31<<11)) * (1.0f / (31<<11));
1755 *g = cast(wide & (63<< 5)) * (1.0f / (63<< 5));
1756 *b = cast(wide & (31<< 0)) * (1.0f / (31<< 0));
1757 }
from_4444(U16 _4444,F * r,F * g,F * b,F * a)1758 SI void from_4444(U16 _4444, F* r, F* g, F* b, F* a) {
1759 U32 wide = expand(_4444);
1760 *r = cast(wide & (15<<12)) * (1.0f / (15<<12));
1761 *g = cast(wide & (15<< 8)) * (1.0f / (15<< 8));
1762 *b = cast(wide & (15<< 4)) * (1.0f / (15<< 4));
1763 *a = cast(wide & (15<< 0)) * (1.0f / (15<< 0));
1764 }
from_8888(U32 _8888,F * r,F * g,F * b,F * a)1765 SI void from_8888(U32 _8888, F* r, F* g, F* b, F* a) {
1766 *r = cast((_8888 ) & 0xff) * (1/255.0f);
1767 *g = cast((_8888 >> 8) & 0xff) * (1/255.0f);
1768 *b = cast((_8888 >> 16) & 0xff) * (1/255.0f);
1769 *a = cast((_8888 >> 24) ) * (1/255.0f);
1770 }
from_88(U16 _88,F * r,F * g)1771 SI void from_88(U16 _88, F* r, F* g) {
1772 U32 wide = expand(_88);
1773 *r = cast((wide ) & 0xff) * (1/255.0f);
1774 *g = cast((wide >> 8) & 0xff) * (1/255.0f);
1775 }
from_1010102(U32 rgba,F * r,F * g,F * b,F * a)1776 SI void from_1010102(U32 rgba, F* r, F* g, F* b, F* a) {
1777 *r = cast((rgba ) & 0x3ff) * (1/1023.0f);
1778 *g = cast((rgba >> 10) & 0x3ff) * (1/1023.0f);
1779 *b = cast((rgba >> 20) & 0x3ff) * (1/1023.0f);
1780 *a = cast((rgba >> 30) ) * (1/ 3.0f);
1781 }
from_1010102_xr(U32 rgba,F * r,F * g,F * b,F * a)1782 SI void from_1010102_xr(U32 rgba, F* r, F* g, F* b, F* a) {
1783 static constexpr float min = -0.752941f;
1784 static constexpr float max = 1.25098f;
1785 static constexpr float range = max - min;
1786 *r = cast((rgba ) & 0x3ff) * (1/1023.0f) * range + min;
1787 *g = cast((rgba >> 10) & 0x3ff) * (1/1023.0f) * range + min;
1788 *b = cast((rgba >> 20) & 0x3ff) * (1/1023.0f) * range + min;
1789 *a = cast((rgba >> 30) ) * (1/ 3.0f);
1790 }
from_10101010_xr(U64 _10x6,F * r,F * g,F * b,F * a)1791 SI void from_10101010_xr(U64 _10x6, F* r, F* g, F* b, F* a) {
1792 *r = (cast64((_10x6 >> 6) & 0x3ff) - 384.f) / 510.f;
1793 *g = (cast64((_10x6 >> 22) & 0x3ff) - 384.f) / 510.f;
1794 *b = (cast64((_10x6 >> 38) & 0x3ff) - 384.f) / 510.f;
1795 *a = (cast64((_10x6 >> 54) & 0x3ff) - 384.f) / 510.f;
1796 }
from_10x6(U64 _10x6,F * r,F * g,F * b,F * a)1797 SI void from_10x6(U64 _10x6, F* r, F* g, F* b, F* a) {
1798 *r = cast64((_10x6 >> 6) & 0x3ff) * (1/1023.0f);
1799 *g = cast64((_10x6 >> 22) & 0x3ff) * (1/1023.0f);
1800 *b = cast64((_10x6 >> 38) & 0x3ff) * (1/1023.0f);
1801 *a = cast64((_10x6 >> 54) & 0x3ff) * (1/1023.0f);
1802 }
from_1616(U32 _1616,F * r,F * g)1803 SI void from_1616(U32 _1616, F* r, F* g) {
1804 *r = cast((_1616 ) & 0xffff) * (1/65535.0f);
1805 *g = cast((_1616 >> 16) & 0xffff) * (1/65535.0f);
1806 }
from_16161616(U64 _16161616,F * r,F * g,F * b,F * a)1807 SI void from_16161616(U64 _16161616, F* r, F* g, F* b, F* a) {
1808 *r = cast64((_16161616 ) & 0xffff) * (1/65535.0f);
1809 *g = cast64((_16161616 >> 16) & 0xffff) * (1/65535.0f);
1810 *b = cast64((_16161616 >> 32) & 0xffff) * (1/65535.0f);
1811 *a = cast64((_16161616 >> 48) & 0xffff) * (1/65535.0f);
1812 }
1813
1814 // Used by load_ and store_ stages to get to the right (dx,dy) starting point of contiguous memory.
1815 template <typename T>
ptr_at_xy(const SkRasterPipeline_MemoryCtx * ctx,size_t dx,size_t dy)1816 SI T* ptr_at_xy(const SkRasterPipeline_MemoryCtx* ctx, size_t dx, size_t dy) {
1817 return (T*)ctx->pixels + dy*ctx->stride + dx;
1818 }
1819
1820 // clamp v to [0,limit).
clamp(F v,F limit)1821 SI F clamp(F v, F limit) {
1822 F inclusive = sk_bit_cast<F>(sk_bit_cast<U32>(limit) - 1); // Exclusive -> inclusive.
1823 return min(max(0.0f, v), inclusive);
1824 }
1825
1826 // clamp to (0,limit).
clamp_ex(F v,float limit)1827 SI F clamp_ex(F v, float limit) {
1828 const F inclusiveZ = F_(std::numeric_limits<float>::min()),
1829 inclusiveL = sk_bit_cast<F>( sk_bit_cast<U32>(F_(limit)) - 1 );
1830 return min(max(inclusiveZ, v), inclusiveL);
1831 }
1832
1833 // Polynomial approximation of degree 5 for sin(x * 2 * pi) in the range [-1/4, 1/4]
1834 // Adapted from https://github.com/google/swiftshader/blob/master/docs/Sin-Cos-Optimization.pdf
sin5q_(F x)1835 SI F sin5q_(F x) {
1836 // A * x + B * x^3 + C * x^5
1837 // Exact at x = 0, 1/12, 1/6, 1/4, and their negatives,
1838 // which correspond to x * 2 * pi = 0, pi/6, pi/3, pi/2
1839 constexpr float A = 6.28230858f;
1840 constexpr float B = -41.1693687f;
1841 constexpr float C = 74.4388885f;
1842 F x2 = x * x;
1843 return x * mad(mad(x2, C, B), x2, A);
1844 }
1845
sin_(F x)1846 SI F sin_(F x) {
1847 constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
1848 x = mad(x, -one_over_pi2, 0.25f);
1849 x = 0.25f - abs_(x - floor_(x + 0.5f));
1850 return sin5q_(x);
1851 }
1852
cos_(F x)1853 SI F cos_(F x) {
1854 constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
1855 x *= one_over_pi2;
1856 x = 0.25f - abs_(x - floor_(x + 0.5f));
1857 return sin5q_(x);
1858 }
1859
1860 /* "GENERATING ACCURATE VALUES FOR THE TANGENT FUNCTION"
1861 https://mae.ufl.edu/~uhk/ACCURATE-TANGENT.pdf
1862
1863 approx = x + (1/3)x^3 + (2/15)x^5 + (17/315)x^7 + (62/2835)x^9
1864
1865 Some simplifications:
1866 1. tan(x) is periodic, -PI/2 < x < PI/2
1867 2. tan(x) is odd, so tan(-x) = -tan(x)
1868 3. Our polynomial approximation is best near zero, so we use the following identity
1869 tan(x) + tan(y)
1870 tan(x + y) = -----------------
1871 1 - tan(x)*tan(y)
1872 tan(PI/4) = 1
1873
1874 So for x > PI/8, we do the following refactor:
1875 x' = x - PI/4
1876
1877 1 + tan(x')
1878 tan(x) = ------------
1879 1 - tan(x')
1880 */
tan_(F x)1881 SI F tan_(F x) {
1882 constexpr float Pi = SK_FloatPI;
1883 // periodic between -pi/2 ... pi/2
1884 // shift to 0...Pi, scale 1/Pi to get into 0...1, then fract, scale-up, shift-back
1885 x = mad(fract(mad(x, 1/Pi, 0.5f)), Pi, -Pi/2);
1886
1887 I32 neg = (x < 0.0f);
1888 x = if_then_else(neg, -x, x);
1889
1890 // minimize total error by shifting if x > pi/8
1891 I32 use_quotient = (x > (Pi/8));
1892 x = if_then_else(use_quotient, x - (Pi/4), x);
1893
1894 // 9th order poly = 4th order(x^2) * x
1895 const float c4 = 62 / 2835.0f;
1896 const float c3 = 17 / 315.0f;
1897 const float c2 = 2 / 15.0f;
1898 const float c1 = 1 / 3.0f;
1899 const float c0 = 1.0f;
1900 F x2 = x * x;
1901 x *= mad(x2, mad(x2, mad(x2, mad(x2, c4, c3), c2), c1), c0);
1902 x = if_then_else(use_quotient, (1+x)/(1-x), x);
1903 x = if_then_else(neg, -x, x);
1904 return x;
1905 }
1906
1907 /* Use 4th order polynomial approximation from https://arachnoid.com/polysolve/
1908 with 129 values of x,atan(x) for x:[0...1]
1909 This only works for 0 <= x <= 1
1910 */
approx_atan_unit(F x)1911 SI F approx_atan_unit(F x) {
1912 // y = 0.14130025741326729 x⁴
1913 // - 0.34312835980675116 x³
1914 // - 0.016172900528248768 x²
1915 // + 1.00376969762003850 x
1916 // - 0.00014758242182738969
1917 const float c4 = 0.14130025741326729f;
1918 const float c3 = -0.34312835980675116f;
1919 const float c2 = -0.016172900528248768f;
1920 const float c1 = 1.0037696976200385f;
1921 const float c0 = -0.00014758242182738969f;
1922 return mad(x, mad(x, mad(x, mad(x, c4, c3), c2), c1), c0);
1923 }
1924
1925 // Use identity atan(x) = pi/2 - atan(1/x) for x > 1
atan_(F x)1926 SI F atan_(F x) {
1927 I32 neg = (x < 0.0f);
1928 x = if_then_else(neg, -x, x);
1929 I32 flip = (x > 1.0f);
1930 x = if_then_else(flip, 1/x, x);
1931 x = approx_atan_unit(x);
1932 x = if_then_else(flip, SK_FloatPI/2 - x, x);
1933 x = if_then_else(neg, -x, x);
1934 return x;
1935 }
1936
1937 // Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun:
1938 // https://books.google.com/books/content?id=ZboM5tOFWtsC&pg=PA81&img=1&zoom=3&hl=en&bul=1&sig=ACfU3U2M75tG_iGVOS92eQspr14LTq02Nw&ci=0%2C15%2C999%2C1279&edge=0
1939 // http://screen/8YGJxUGFQ49bVX6
asin_(F x)1940 SI F asin_(F x) {
1941 I32 neg = (x < 0.0f);
1942 x = if_then_else(neg, -x, x);
1943 const float c3 = -0.0187293f;
1944 const float c2 = 0.0742610f;
1945 const float c1 = -0.2121144f;
1946 const float c0 = 1.5707288f;
1947 F poly = mad(x, mad(x, mad(x, c3, c2), c1), c0);
1948
1949 F sqrt_result = { 0.0f };
1950 for (int32_t i = 0; i < 4; ++i) { // 4 is a 4-element vector
1951 sqrt_result[i] = std::sqrt(1.0f - x[i]);
1952 }
1953
1954 x = nmad(sqrt_result, poly, SK_FloatPI/2);
1955 x = if_then_else(neg, -x, x);
1956 return x;
1957 }
1958
acos_(F x)1959 SI F acos_(F x) {
1960 return SK_FloatPI/2 - asin_(x);
1961 }
1962
1963 /* Use identity atan(x) = pi/2 - atan(1/x) for x > 1
1964 By swapping y,x to ensure the ratio is <= 1, we can safely call atan_unit()
1965 which avoids a 2nd divide instruction if we had instead called atan().
1966 */
atan2_(F y0,F x0)1967 SI F atan2_(F y0, F x0) {
1968 I32 flip = (abs_(y0) > abs_(x0));
1969 F y = if_then_else(flip, x0, y0);
1970 F x = if_then_else(flip, y0, x0);
1971 F arg = y/x;
1972
1973 I32 neg = (arg < 0.0f);
1974 arg = if_then_else(neg, -arg, arg);
1975
1976 F r = approx_atan_unit(arg);
1977 r = if_then_else(flip, SK_FloatPI/2 - r, r);
1978 r = if_then_else(neg, -r, r);
1979
1980 // handle quadrant distinctions
1981 r = if_then_else((y0 >= 0) & (x0 < 0), r + SK_FloatPI, r);
1982 r = if_then_else((y0 < 0) & (x0 <= 0), r - SK_FloatPI, r);
1983 // Note: we don't try to handle 0,0 or infinities
1984 return r;
1985 }
1986
1987 // Used by gather_ stages to calculate the base pointer and a vector of indices to load.
1988 template <typename T>
ix_and_ptr(T ** ptr,const SkRasterPipeline_GatherCtx * ctx,F x,F y)1989 SI U32 ix_and_ptr(T** ptr, const SkRasterPipeline_GatherCtx* ctx, F x, F y) {
1990 // We use exclusive clamp so that our min value is > 0 because ULP subtraction using U32 would
1991 // produce a NaN if applied to +0.f.
1992 x = clamp_ex(x, ctx->width );
1993 y = clamp_ex(y, ctx->height);
1994 x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
1995 y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
1996 *ptr = (const T*)ctx->pixels;
1997 return trunc_(y)*ctx->stride + trunc_(x);
1998 }
1999
2000 // We often have a nominally [0,1] float value we need to scale and convert to an integer,
2001 // whether for a table lookup or to pack back down into bytes for storage.
2002 //
2003 // In practice, especially when dealing with interesting color spaces, that notionally
2004 // [0,1] float may be out of [0,1] range. Unorms cannot represent that, so we must clamp.
2005 //
2006 // You can adjust the expected input to [0,bias] by tweaking that parameter.
2007 SI U32 to_unorm(F v, float scale, float bias = 1.0f) {
2008 // Any time we use round() we probably want to use to_unorm().
2009 return round(min(max(0.0f, v), bias), F_(scale));
2010 }
2011
cond_to_mask(I32 cond)2012 SI I32 cond_to_mask(I32 cond) {
2013 #if defined(SKRP_CPU_SCALAR)
2014 // In scalar mode, conditions are bools (0 or 1), but we want to store and operate on masks
2015 // (eg, using bitwise operations to select values).
2016 return if_then_else(cond, I32(~0), I32(0));
2017 #else
2018 // In SIMD mode, our various instruction sets already represent conditions as masks.
2019 return cond;
2020 #endif
2021 }
2022
2023 #if defined(SKRP_CPU_SCALAR)
2024 // In scalar mode, `data` only contains a single lane.
select_lane(uint32_t data,int)2025 SI uint32_t select_lane(uint32_t data, int /*lane*/) { return data; }
select_lane(int32_t data,int)2026 SI int32_t select_lane( int32_t data, int /*lane*/) { return data; }
2027 #else
2028 // In SIMD mode, `data` contains a vector of lanes.
select_lane(U32 data,int lane)2029 SI uint32_t select_lane(U32 data, int lane) { return data[lane]; }
select_lane(I32 data,int lane)2030 SI int32_t select_lane(I32 data, int lane) { return data[lane]; }
2031 #endif
2032
2033 // Now finally, normal Stages!
2034
STAGE(seed_shader,NoCtx)2035 STAGE(seed_shader, NoCtx) {
2036 static constexpr float iota[] = {
2037 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
2038 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
2039 };
2040 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
2041
2042 // It's important for speed to explicitly cast(dx) and cast(dy),
2043 // which has the effect of splatting them to vectors before converting to floats.
2044 // On Intel this breaks a data dependency on previous loop iterations' registers.
2045 r = cast(U32_(dx)) + sk_unaligned_load<F>(iota);
2046 g = cast(U32_(dy)) + 0.5f;
2047 b = F1; // This is w=1 for matrix multiplies by the device coords.
2048 a = F0;
2049 }
2050
STAGE(dither,const float * rate)2051 STAGE(dither, const float* rate) {
2052 // Get [(dx,dy), (dx+1,dy), (dx+2,dy), ...] loaded up in integer vectors.
2053 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
2054 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
2055
2056 U32 X = U32_(dx) + sk_unaligned_load<U32>(iota),
2057 Y = U32_(dy);
2058
2059 // We're doing 8x8 ordered dithering, see https://en.wikipedia.org/wiki/Ordered_dithering.
2060 // In this case n=8 and we're using the matrix that looks like 1/64 x [ 0 48 12 60 ... ].
2061
2062 // We only need X and X^Y from here on, so it's easier to just think of that as "Y".
2063 Y ^= X;
2064
2065 // We'll mix the bottom 3 bits of each of X and Y to make 6 bits,
2066 // for 2^6 == 64 == 8x8 matrix values. If X=abc and Y=def, we make fcebda.
2067 U32 M = (Y & 1) << 5 | (X & 1) << 4
2068 | (Y & 2) << 2 | (X & 2) << 1
2069 | (Y & 4) >> 1 | (X & 4) >> 2;
2070
2071 // Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as 0.5-epsilon.
2072 // We want to make sure our dither is less than 0.5 in either direction to keep exact values
2073 // like 0 and 1 unchanged after rounding.
2074 F dither = mad(cast(M), 2/128.0f, -63/128.0f);
2075
2076 r = mad(dither, *rate, r);
2077 g = mad(dither, *rate, g);
2078 b = mad(dither, *rate, b);
2079
2080 r = max(0.0f, min(r, a));
2081 g = max(0.0f, min(g, a));
2082 b = max(0.0f, min(b, a));
2083 }
2084
2085 // load 4 floats from memory, and splat them into r,g,b,a
STAGE(uniform_color,const SkRasterPipeline_UniformColorCtx * c)2086 STAGE(uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
2087 r = F_(c->r);
2088 g = F_(c->g);
2089 b = F_(c->b);
2090 a = F_(c->a);
2091 }
STAGE(unbounded_uniform_color,const SkRasterPipeline_UniformColorCtx * c)2092 STAGE(unbounded_uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
2093 r = F_(c->r);
2094 g = F_(c->g);
2095 b = F_(c->b);
2096 a = F_(c->a);
2097 }
2098 // load 4 floats from memory, and splat them into dr,dg,db,da
STAGE(uniform_color_dst,const SkRasterPipeline_UniformColorCtx * c)2099 STAGE(uniform_color_dst, const SkRasterPipeline_UniformColorCtx* c) {
2100 dr = F_(c->r);
2101 dg = F_(c->g);
2102 db = F_(c->b);
2103 da = F_(c->a);
2104 }
2105
2106 // splats opaque-black into r,g,b,a
STAGE(black_color,NoCtx)2107 STAGE(black_color, NoCtx) {
2108 r = g = b = F0;
2109 a = F1;
2110 }
2111
STAGE(white_color,NoCtx)2112 STAGE(white_color, NoCtx) {
2113 r = g = b = a = F1;
2114 }
2115
2116 // load registers r,g,b,a from context (mirrors store_src)
STAGE(load_src,const float * ptr)2117 STAGE(load_src, const float* ptr) {
2118 r = sk_unaligned_load<F>(ptr + 0*N);
2119 g = sk_unaligned_load<F>(ptr + 1*N);
2120 b = sk_unaligned_load<F>(ptr + 2*N);
2121 a = sk_unaligned_load<F>(ptr + 3*N);
2122 }
2123
2124 // store registers r,g,b,a into context (mirrors load_src)
STAGE(store_src,float * ptr)2125 STAGE(store_src, float* ptr) {
2126 sk_unaligned_store(ptr + 0*N, r);
2127 sk_unaligned_store(ptr + 1*N, g);
2128 sk_unaligned_store(ptr + 2*N, b);
2129 sk_unaligned_store(ptr + 3*N, a);
2130 }
2131 // store registers r,g into context
STAGE(store_src_rg,float * ptr)2132 STAGE(store_src_rg, float* ptr) {
2133 sk_unaligned_store(ptr + 0*N, r);
2134 sk_unaligned_store(ptr + 1*N, g);
2135 }
2136 // load registers r,g from context
STAGE(load_src_rg,float * ptr)2137 STAGE(load_src_rg, float* ptr) {
2138 r = sk_unaligned_load<F>(ptr + 0*N);
2139 g = sk_unaligned_load<F>(ptr + 1*N);
2140 }
2141 // store register a into context
STAGE(store_src_a,float * ptr)2142 STAGE(store_src_a, float* ptr) {
2143 sk_unaligned_store(ptr, a);
2144 }
2145
2146 // load registers dr,dg,db,da from context (mirrors store_dst)
STAGE(load_dst,const float * ptr)2147 STAGE(load_dst, const float* ptr) {
2148 dr = sk_unaligned_load<F>(ptr + 0*N);
2149 dg = sk_unaligned_load<F>(ptr + 1*N);
2150 db = sk_unaligned_load<F>(ptr + 2*N);
2151 da = sk_unaligned_load<F>(ptr + 3*N);
2152 }
2153
2154 // store registers dr,dg,db,da into context (mirrors load_dst)
STAGE(store_dst,float * ptr)2155 STAGE(store_dst, float* ptr) {
2156 sk_unaligned_store(ptr + 0*N, dr);
2157 sk_unaligned_store(ptr + 1*N, dg);
2158 sk_unaligned_store(ptr + 2*N, db);
2159 sk_unaligned_store(ptr + 3*N, da);
2160 }
2161
2162 // Most blend modes apply the same logic to each channel.
2163 #define BLEND_MODE(name) \
2164 SI F name##_channel(F s, F d, F sa, F da); \
2165 STAGE(name, NoCtx) { \
2166 r = name##_channel(r,dr,a,da); \
2167 g = name##_channel(g,dg,a,da); \
2168 b = name##_channel(b,db,a,da); \
2169 a = name##_channel(a,da,a,da); \
2170 } \
2171 SI F name##_channel(F s, F d, F sa, F da)
2172
inv(F x)2173 SI F inv(F x) { return 1.0f - x; }
two(F x)2174 SI F two(F x) { return x + x; }
2175
BLEND_MODE(clear)2176 BLEND_MODE(clear) { return F0; }
BLEND_MODE(srcatop)2177 BLEND_MODE(srcatop) { return mad(s, da, d*inv(sa)); }
BLEND_MODE(dstatop)2178 BLEND_MODE(dstatop) { return mad(d, sa, s*inv(da)); }
BLEND_MODE(srcin)2179 BLEND_MODE(srcin) { return s * da; }
BLEND_MODE(dstin)2180 BLEND_MODE(dstin) { return d * sa; }
BLEND_MODE(srcout)2181 BLEND_MODE(srcout) { return s * inv(da); }
BLEND_MODE(dstout)2182 BLEND_MODE(dstout) { return d * inv(sa); }
BLEND_MODE(srcover)2183 BLEND_MODE(srcover) { return mad(d, inv(sa), s); }
BLEND_MODE(dstover)2184 BLEND_MODE(dstover) { return mad(s, inv(da), d); }
2185
BLEND_MODE(modulate)2186 BLEND_MODE(modulate) { return s*d; }
BLEND_MODE(multiply)2187 BLEND_MODE(multiply) { return mad(s, d, mad(s, inv(da), d*inv(sa))); }
BLEND_MODE(plus_)2188 BLEND_MODE(plus_) { return min(s + d, 1.0f); } // We can clamp to either 1 or sa.
BLEND_MODE(screen)2189 BLEND_MODE(screen) { return nmad(s, d, s + d); }
BLEND_MODE(xor_)2190 BLEND_MODE(xor_) { return mad(s, inv(da), d*inv(sa)); }
2191 #undef BLEND_MODE
2192
2193 // Most other blend modes apply the same logic to colors, and srcover to alpha.
2194 #define BLEND_MODE(name) \
2195 SI F name##_channel(F s, F d, F sa, F da); \
2196 STAGE(name, NoCtx) { \
2197 r = name##_channel(r,dr,a,da); \
2198 g = name##_channel(g,dg,a,da); \
2199 b = name##_channel(b,db,a,da); \
2200 a = mad(da, inv(a), a); \
2201 } \
2202 SI F name##_channel(F s, F d, F sa, F da)
2203
BLEND_MODE(darken)2204 BLEND_MODE(darken) { return s + d - max(s*da, d*sa) ; }
BLEND_MODE(lighten)2205 BLEND_MODE(lighten) { return s + d - min(s*da, d*sa) ; }
BLEND_MODE(difference)2206 BLEND_MODE(difference) { return s + d - two(min(s*da, d*sa)); }
BLEND_MODE(exclusion)2207 BLEND_MODE(exclusion) { return s + d - two(s*d); }
2208
BLEND_MODE(colorburn)2209 BLEND_MODE(colorburn) {
2210 return if_then_else(d == da, d + s*inv(da),
2211 if_then_else(s == 0, /* s + */ d*inv(sa),
2212 sa*(da - min(da, (da-d)*sa*rcp_fast(s))) + s*inv(da) + d*inv(sa)));
2213 }
BLEND_MODE(colordodge)2214 BLEND_MODE(colordodge) {
2215 return if_then_else(d == 0, /* d + */ s*inv(da),
2216 if_then_else(s == sa, s + d*inv(sa),
2217 sa*min(da, (d*sa)*rcp_fast(sa - s)) + s*inv(da) + d*inv(sa)));
2218 }
BLEND_MODE(hardlight)2219 BLEND_MODE(hardlight) {
2220 return s*inv(da) + d*inv(sa)
2221 + if_then_else(two(s) <= sa, two(s*d), sa*da - two((da-d)*(sa-s)));
2222 }
BLEND_MODE(overlay)2223 BLEND_MODE(overlay) {
2224 return s*inv(da) + d*inv(sa)
2225 + if_then_else(two(d) <= da, two(s*d), sa*da - two((da-d)*(sa-s)));
2226 }
2227
BLEND_MODE(softlight)2228 BLEND_MODE(softlight) {
2229 F m = if_then_else(da > 0, d / da, 0.0f),
2230 s2 = two(s),
2231 m4 = two(two(m));
2232
2233 // The logic forks three ways:
2234 // 1. dark src?
2235 // 2. light src, dark dst?
2236 // 3. light src, light dst?
2237 F darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1.
2238 darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2.
2239 liteDst = sqrt_(m) - m,
2240 liteSrc = d*sa + da*(s2 - sa) * if_then_else(two(two(d)) <= da, darkDst, liteDst); // 2 or 3?
2241 return s*inv(da) + d*inv(sa) + if_then_else(s2 <= sa, darkSrc, liteSrc); // 1 or (2 or 3)?
2242 }
2243 #undef BLEND_MODE
2244
2245 // We're basing our implemenation of non-separable blend modes on
2246 // https://www.w3.org/TR/compositing-1/#blendingnonseparable.
2247 // and
2248 // https://www.khronos.org/registry/OpenGL/specs/es/3.2/es_spec_3.2.pdf
2249 // They're equivalent, but ES' math has been better simplified.
2250 //
2251 // Anything extra we add beyond that is to make the math work with premul inputs.
2252
sat(F r,F g,F b)2253 SI F sat(F r, F g, F b) { return max(r, max(g,b)) - min(r, min(g,b)); }
lum(F r,F g,F b)2254 SI F lum(F r, F g, F b) { return mad(r, 0.30f, mad(g, 0.59f, b*0.11f)); }
2255
set_sat(F * r,F * g,F * b,F s)2256 SI void set_sat(F* r, F* g, F* b, F s) {
2257 F mn = min(*r, min(*g,*b)),
2258 mx = max(*r, max(*g,*b)),
2259 sat = mx - mn;
2260
2261 // Map min channel to 0, max channel to s, and scale the middle proportionally.
2262 s = if_then_else(sat == 0.0f, 0.0f, s * rcp_fast(sat));
2263 *r = (*r - mn) * s;
2264 *g = (*g - mn) * s;
2265 *b = (*b - mn) * s;
2266 }
set_lum(F * r,F * g,F * b,F l)2267 SI void set_lum(F* r, F* g, F* b, F l) {
2268 F diff = l - lum(*r, *g, *b);
2269 *r += diff;
2270 *g += diff;
2271 *b += diff;
2272 }
clip_channel(F c,F l,I32 clip_low,I32 clip_high,F mn_scale,F mx_scale)2273 SI F clip_channel(F c, F l, I32 clip_low, I32 clip_high, F mn_scale, F mx_scale) {
2274 c = if_then_else(clip_low, mad(mn_scale, c - l, l), c);
2275 c = if_then_else(clip_high, mad(mx_scale, c - l, l), c);
2276 c = max(c, 0.0f); // Sometimes without this we may dip just a little negative.
2277 return c;
2278 }
clip_color(F * r,F * g,F * b,F a)2279 SI void clip_color(F* r, F* g, F* b, F a) {
2280 F mn = min(*r, min(*g, *b)),
2281 mx = max(*r, max(*g, *b)),
2282 l = lum(*r, *g, *b),
2283 mn_scale = ( l) * rcp_fast(l - mn),
2284 mx_scale = (a - l) * rcp_fast(mx - l);
2285 I32 clip_low = cond_to_mask(mn < 0 && l != mn),
2286 clip_high = cond_to_mask(mx > a && l != mx);
2287
2288 *r = clip_channel(*r, l, clip_low, clip_high, mn_scale, mx_scale);
2289 *g = clip_channel(*g, l, clip_low, clip_high, mn_scale, mx_scale);
2290 *b = clip_channel(*b, l, clip_low, clip_high, mn_scale, mx_scale);
2291 }
2292
STAGE(hue,NoCtx)2293 STAGE(hue, NoCtx) {
2294 F R = r*a,
2295 G = g*a,
2296 B = b*a;
2297
2298 set_sat(&R, &G, &B, sat(dr,dg,db)*a);
2299 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
2300 clip_color(&R,&G,&B, a*da);
2301
2302 r = mad(r, inv(da), mad(dr, inv(a), R));
2303 g = mad(g, inv(da), mad(dg, inv(a), G));
2304 b = mad(b, inv(da), mad(db, inv(a), B));
2305 a = a + nmad(a, da, da);
2306 }
STAGE(saturation,NoCtx)2307 STAGE(saturation, NoCtx) {
2308 F R = dr*a,
2309 G = dg*a,
2310 B = db*a;
2311
2312 set_sat(&R, &G, &B, sat( r, g, b)*da);
2313 set_lum(&R, &G, &B, lum(dr,dg,db)* a); // (This is not redundant.)
2314 clip_color(&R,&G,&B, a*da);
2315
2316 r = mad(r, inv(da), mad(dr, inv(a), R));
2317 g = mad(g, inv(da), mad(dg, inv(a), G));
2318 b = mad(b, inv(da), mad(db, inv(a), B));
2319 a = a + nmad(a, da, da);
2320 }
STAGE(color,NoCtx)2321 STAGE(color, NoCtx) {
2322 F R = r*da,
2323 G = g*da,
2324 B = b*da;
2325
2326 set_lum(&R, &G, &B, lum(dr,dg,db)*a);
2327 clip_color(&R,&G,&B, a*da);
2328
2329 r = mad(r, inv(da), mad(dr, inv(a), R));
2330 g = mad(g, inv(da), mad(dg, inv(a), G));
2331 b = mad(b, inv(da), mad(db, inv(a), B));
2332 a = a + nmad(a, da, da);
2333 }
STAGE(luminosity,NoCtx)2334 STAGE(luminosity, NoCtx) {
2335 F R = dr*a,
2336 G = dg*a,
2337 B = db*a;
2338
2339 set_lum(&R, &G, &B, lum(r,g,b)*da);
2340 clip_color(&R,&G,&B, a*da);
2341
2342 r = mad(r, inv(da), mad(dr, inv(a), R));
2343 g = mad(g, inv(da), mad(dg, inv(a), G));
2344 b = mad(b, inv(da), mad(db, inv(a), B));
2345 a = a + nmad(a, da, da);
2346 }
2347
STAGE(srcover_rgba_8888,const SkRasterPipeline_MemoryCtx * ctx)2348 STAGE(srcover_rgba_8888, const SkRasterPipeline_MemoryCtx* ctx) {
2349 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2350
2351 U32 dst = load<U32>(ptr);
2352 dr = cast((dst ) & 0xff);
2353 dg = cast((dst >> 8) & 0xff);
2354 db = cast((dst >> 16) & 0xff);
2355 da = cast((dst >> 24) );
2356 // {dr,dg,db,da} are in [0,255]
2357 // { r, g, b, a} are in [0, 1] (but may be out of gamut)
2358
2359 r = mad(dr, inv(a), r*255.0f);
2360 g = mad(dg, inv(a), g*255.0f);
2361 b = mad(db, inv(a), b*255.0f);
2362 a = mad(da, inv(a), a*255.0f);
2363 // { r, g, b, a} are now in [0,255] (but may be out of gamut)
2364
2365 // to_unorm() clamps back to gamut. Scaling by 1 since we're already 255-biased.
2366 dst = to_unorm(r, 1, 255)
2367 | to_unorm(g, 1, 255) << 8
2368 | to_unorm(b, 1, 255) << 16
2369 | to_unorm(a, 1, 255) << 24;
2370 store(ptr, dst);
2371 }
2372
clamp_01_(F v)2373 SI F clamp_01_(F v) { return min(max(0.0f, v), 1.0f); }
2374
STAGE(clamp_01,NoCtx)2375 STAGE(clamp_01, NoCtx) {
2376 r = clamp_01_(r);
2377 g = clamp_01_(g);
2378 b = clamp_01_(b);
2379 a = clamp_01_(a);
2380 }
2381
STAGE(clamp_a_01,NoCtx)2382 STAGE(clamp_a_01, NoCtx) {
2383 a = clamp_01_(a);
2384 }
2385
STAGE(clamp_gamut,NoCtx)2386 STAGE(clamp_gamut, NoCtx) {
2387 a = min(max(a, 0.0f), 1.0f);
2388 r = min(max(r, 0.0f), a);
2389 g = min(max(g, 0.0f), a);
2390 b = min(max(b, 0.0f), a);
2391 }
2392
STAGE(set_rgb,const float * rgb)2393 STAGE(set_rgb, const float* rgb) {
2394 r = F_(rgb[0]);
2395 g = F_(rgb[1]);
2396 b = F_(rgb[2]);
2397 }
2398
STAGE(unbounded_set_rgb,const float * rgb)2399 STAGE(unbounded_set_rgb, const float* rgb) {
2400 r = F_(rgb[0]);
2401 g = F_(rgb[1]);
2402 b = F_(rgb[2]);
2403 }
2404
STAGE(swap_rb,NoCtx)2405 STAGE(swap_rb, NoCtx) {
2406 auto tmp = r;
2407 r = b;
2408 b = tmp;
2409 }
STAGE(swap_rb_dst,NoCtx)2410 STAGE(swap_rb_dst, NoCtx) {
2411 auto tmp = dr;
2412 dr = db;
2413 db = tmp;
2414 }
2415
STAGE(move_src_dst,NoCtx)2416 STAGE(move_src_dst, NoCtx) {
2417 dr = r;
2418 dg = g;
2419 db = b;
2420 da = a;
2421 }
STAGE(move_dst_src,NoCtx)2422 STAGE(move_dst_src, NoCtx) {
2423 r = dr;
2424 g = dg;
2425 b = db;
2426 a = da;
2427 }
STAGE(swap_src_dst,NoCtx)2428 STAGE(swap_src_dst, NoCtx) {
2429 std::swap(r, dr);
2430 std::swap(g, dg);
2431 std::swap(b, db);
2432 std::swap(a, da);
2433 }
2434
STAGE(premul,NoCtx)2435 STAGE(premul, NoCtx) {
2436 r = r * a;
2437 g = g * a;
2438 b = b * a;
2439 }
STAGE(premul_dst,NoCtx)2440 STAGE(premul_dst, NoCtx) {
2441 dr = dr * da;
2442 dg = dg * da;
2443 db = db * da;
2444 }
STAGE(unpremul,NoCtx)2445 STAGE(unpremul, NoCtx) {
2446 float inf = sk_bit_cast<float>(0x7f800000);
2447 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
2448 r *= scale;
2449 g *= scale;
2450 b *= scale;
2451 }
STAGE(unpremul_polar,NoCtx)2452 STAGE(unpremul_polar, NoCtx) {
2453 float inf = sk_bit_cast<float>(0x7f800000);
2454 auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
2455 g *= scale;
2456 b *= scale;
2457 }
2458
STAGE(force_opaque,NoCtx)2459 STAGE(force_opaque , NoCtx) { a = F1; }
STAGE(force_opaque_dst,NoCtx)2460 STAGE(force_opaque_dst, NoCtx) { da = F1; }
2461
STAGE(rgb_to_hsl,NoCtx)2462 STAGE(rgb_to_hsl, NoCtx) {
2463 F mx = max(r, max(g,b)),
2464 mn = min(r, min(g,b)),
2465 d = mx - mn,
2466 d_rcp = 1.0f / d;
2467
2468 F h = (1/6.0f) *
2469 if_then_else(mx == mn, 0.0f,
2470 if_then_else(mx == r, (g-b)*d_rcp + if_then_else(g < b, 6.0f, 0.0f),
2471 if_then_else(mx == g, (b-r)*d_rcp + 2.0f,
2472 (r-g)*d_rcp + 4.0f)));
2473
2474 F l = (mx + mn) * 0.5f;
2475 F s = if_then_else(mx == mn, 0.0f,
2476 d / if_then_else(l > 0.5f, 2.0f-mx-mn, mx+mn));
2477
2478 r = h;
2479 g = s;
2480 b = l;
2481 }
STAGE(hsl_to_rgb,NoCtx)2482 STAGE(hsl_to_rgb, NoCtx) {
2483 // See GrRGBToHSLFilterEffect.fp
2484
2485 F h = r,
2486 s = g,
2487 l = b,
2488 c = (1.0f - abs_(2.0f * l - 1)) * s;
2489
2490 auto hue_to_rgb = [&](F hue) {
2491 F q = clamp_01_(abs_(fract(hue) * 6.0f - 3.0f) - 1.0f);
2492 return (q - 0.5f) * c + l;
2493 };
2494
2495 r = hue_to_rgb(h + 0.0f/3.0f);
2496 g = hue_to_rgb(h + 2.0f/3.0f);
2497 b = hue_to_rgb(h + 1.0f/3.0f);
2498 }
2499
2500 // Color conversion functions used in gradient interpolation, based on
2501 // https://www.w3.org/TR/css-color-4/#color-conversion-code
STAGE(css_lab_to_xyz,NoCtx)2502 STAGE(css_lab_to_xyz, NoCtx) {
2503 constexpr float k = 24389 / 27.0f;
2504 constexpr float e = 216 / 24389.0f;
2505
2506 F f[3];
2507 f[1] = (r + 16) * (1 / 116.0f);
2508 f[0] = (g * (1 / 500.0f)) + f[1];
2509 f[2] = f[1] - (b * (1 / 200.0f));
2510
2511 F f_cubed[3] = { f[0]*f[0]*f[0], f[1]*f[1]*f[1], f[2]*f[2]*f[2] };
2512
2513 F xyz[3] = {
2514 if_then_else(f_cubed[0] > e, f_cubed[0], (116 * f[0] - 16) * (1 / k)),
2515 if_then_else(r > k * e, f_cubed[1], r * (1 / k)),
2516 if_then_else(f_cubed[2] > e, f_cubed[2], (116 * f[2] - 16) * (1 / k))
2517 };
2518
2519 constexpr float D50[3] = { 0.3457f / 0.3585f, 1.0f, (1.0f - 0.3457f - 0.3585f) / 0.3585f };
2520 r = xyz[0]*D50[0];
2521 g = xyz[1]*D50[1];
2522 b = xyz[2]*D50[2];
2523 }
2524
STAGE(css_oklab_to_linear_srgb,NoCtx)2525 STAGE(css_oklab_to_linear_srgb, NoCtx) {
2526 F l_ = r + 0.3963377774f * g + 0.2158037573f * b,
2527 m_ = r - 0.1055613458f * g - 0.0638541728f * b,
2528 s_ = r - 0.0894841775f * g - 1.2914855480f * b;
2529
2530 F l = l_*l_*l_,
2531 m = m_*m_*m_,
2532 s = s_*s_*s_;
2533
2534 r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
2535 g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
2536 b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
2537 }
2538
STAGE(css_oklab_gamut_map_to_linear_srgb,NoCtx)2539 STAGE(css_oklab_gamut_map_to_linear_srgb, NoCtx) {
2540 // TODO(https://crbug.com/1508329): Add support for gamut mapping.
2541 // Return a greyscale value, so that accidental use is obvious.
2542 F l_ = r,
2543 m_ = r,
2544 s_ = r;
2545
2546 F l = l_*l_*l_,
2547 m = m_*m_*m_,
2548 s = s_*s_*s_;
2549
2550 r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
2551 g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
2552 b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
2553 }
2554
2555 // Skia stores all polar colors with hue in the first component, so this "LCH -> Lab" transform
2556 // actually takes "HCL". This is also used to do the same polar transform for OkHCL to OkLAB.
2557 // See similar comments & logic in SkGradientBaseShader.cpp.
STAGE(css_hcl_to_lab,NoCtx)2558 STAGE(css_hcl_to_lab, NoCtx) {
2559 F H = r,
2560 C = g,
2561 L = b;
2562
2563 F hueRadians = H * (SK_FloatPI / 180);
2564
2565 r = L;
2566 g = C * cos_(hueRadians);
2567 b = C * sin_(hueRadians);
2568 }
2569
mod_(F x,float y)2570 SI F mod_(F x, float y) {
2571 return nmad(y, floor_(x * (1 / y)), x);
2572 }
2573
2574 struct RGB { F r, g, b; };
2575
css_hsl_to_srgb_(F h,F s,F l)2576 SI RGB css_hsl_to_srgb_(F h, F s, F l) {
2577 h = mod_(h, 360);
2578
2579 s *= 0.01f;
2580 l *= 0.01f;
2581
2582 F k[3] = {
2583 mod_(0 + h * (1 / 30.0f), 12),
2584 mod_(8 + h * (1 / 30.0f), 12),
2585 mod_(4 + h * (1 / 30.0f), 12)
2586 };
2587 F a = s * min(l, 1 - l);
2588 return {
2589 l - a * max(-1.0f, min(min(k[0] - 3.0f, 9.0f - k[0]), 1.0f)),
2590 l - a * max(-1.0f, min(min(k[1] - 3.0f, 9.0f - k[1]), 1.0f)),
2591 l - a * max(-1.0f, min(min(k[2] - 3.0f, 9.0f - k[2]), 1.0f))
2592 };
2593 }
2594
STAGE(css_hsl_to_srgb,NoCtx)2595 STAGE(css_hsl_to_srgb, NoCtx) {
2596 RGB rgb = css_hsl_to_srgb_(r, g, b);
2597 r = rgb.r;
2598 g = rgb.g;
2599 b = rgb.b;
2600 }
2601
STAGE(css_hwb_to_srgb,NoCtx)2602 STAGE(css_hwb_to_srgb, NoCtx) {
2603 g *= 0.01f;
2604 b *= 0.01f;
2605
2606 F gray = g / (g + b);
2607
2608 RGB rgb = css_hsl_to_srgb_(r, F_(100.0f), F_(50.0f));
2609 rgb.r = rgb.r * (1 - g - b) + g;
2610 rgb.g = rgb.g * (1 - g - b) + g;
2611 rgb.b = rgb.b * (1 - g - b) + g;
2612
2613 auto isGray = (g + b) >= 1;
2614
2615 r = if_then_else(isGray, gray, rgb.r);
2616 g = if_then_else(isGray, gray, rgb.g);
2617 b = if_then_else(isGray, gray, rgb.b);
2618 }
2619
2620 // Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
alpha_coverage_from_rgb_coverage(F a,F da,F cr,F cg,F cb)2621 SI F alpha_coverage_from_rgb_coverage(F a, F da, F cr, F cg, F cb) {
2622 return if_then_else(a < da, min(cr, min(cg,cb))
2623 , max(cr, max(cg,cb)));
2624 }
2625
STAGE(scale_1_float,const float * c)2626 STAGE(scale_1_float, const float* c) {
2627 r = r * *c;
2628 g = g * *c;
2629 b = b * *c;
2630 a = a * *c;
2631 }
STAGE(scale_u8,const SkRasterPipeline_MemoryCtx * ctx)2632 STAGE(scale_u8, const SkRasterPipeline_MemoryCtx* ctx) {
2633 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2634
2635 auto scales = load<U8>(ptr);
2636 auto c = from_byte(scales);
2637
2638 r = r * c;
2639 g = g * c;
2640 b = b * c;
2641 a = a * c;
2642 }
STAGE(scale_565,const SkRasterPipeline_MemoryCtx * ctx)2643 STAGE(scale_565, const SkRasterPipeline_MemoryCtx* ctx) {
2644 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2645
2646 F cr,cg,cb;
2647 from_565(load<U16>(ptr), &cr, &cg, &cb);
2648
2649 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
2650
2651 r = r * cr;
2652 g = g * cg;
2653 b = b * cb;
2654 a = a * ca;
2655 }
2656
lerp(F from,F to,F t)2657 SI F lerp(F from, F to, F t) {
2658 return mad(to-from, t, from);
2659 }
2660
STAGE(lerp_1_float,const float * c)2661 STAGE(lerp_1_float, const float* c) {
2662 r = lerp(dr, r, F_(*c));
2663 g = lerp(dg, g, F_(*c));
2664 b = lerp(db, b, F_(*c));
2665 a = lerp(da, a, F_(*c));
2666 }
STAGE(scale_native,const float scales[])2667 STAGE(scale_native, const float scales[]) {
2668 auto c = sk_unaligned_load<F>(scales);
2669 r = r * c;
2670 g = g * c;
2671 b = b * c;
2672 a = a * c;
2673 }
STAGE(lerp_native,const float scales[])2674 STAGE(lerp_native, const float scales[]) {
2675 auto c = sk_unaligned_load<F>(scales);
2676 r = lerp(dr, r, c);
2677 g = lerp(dg, g, c);
2678 b = lerp(db, b, c);
2679 a = lerp(da, a, c);
2680 }
STAGE(lerp_u8,const SkRasterPipeline_MemoryCtx * ctx)2681 STAGE(lerp_u8, const SkRasterPipeline_MemoryCtx* ctx) {
2682 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2683
2684 auto scales = load<U8>(ptr);
2685 auto c = from_byte(scales);
2686
2687 r = lerp(dr, r, c);
2688 g = lerp(dg, g, c);
2689 b = lerp(db, b, c);
2690 a = lerp(da, a, c);
2691 }
STAGE(lerp_565,const SkRasterPipeline_MemoryCtx * ctx)2692 STAGE(lerp_565, const SkRasterPipeline_MemoryCtx* ctx) {
2693 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2694
2695 F cr,cg,cb;
2696 from_565(load<U16>(ptr), &cr, &cg, &cb);
2697
2698 F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
2699
2700 r = lerp(dr, r, cr);
2701 g = lerp(dg, g, cg);
2702 b = lerp(db, b, cb);
2703 a = lerp(da, a, ca);
2704 }
2705
STAGE(emboss,const SkRasterPipeline_EmbossCtx * ctx)2706 STAGE(emboss, const SkRasterPipeline_EmbossCtx* ctx) {
2707 auto mptr = ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy),
2708 aptr = ptr_at_xy<const uint8_t>(&ctx->add, dx,dy);
2709
2710 F mul = from_byte(load<U8>(mptr)),
2711 add = from_byte(load<U8>(aptr));
2712
2713 r = mad(r, mul, add);
2714 g = mad(g, mul, add);
2715 b = mad(b, mul, add);
2716 }
2717
STAGE(byte_tables,const SkRasterPipeline_TablesCtx * tables)2718 STAGE(byte_tables, const SkRasterPipeline_TablesCtx* tables) {
2719 r = from_byte(gather(tables->r, to_unorm(r, 255)));
2720 g = from_byte(gather(tables->g, to_unorm(g, 255)));
2721 b = from_byte(gather(tables->b, to_unorm(b, 255)));
2722 a = from_byte(gather(tables->a, to_unorm(a, 255)));
2723 }
2724
strip_sign(F x,U32 * sign)2725 SI F strip_sign(F x, U32* sign) {
2726 U32 bits = sk_bit_cast<U32>(x);
2727 *sign = bits & 0x80000000;
2728 return sk_bit_cast<F>(bits ^ *sign);
2729 }
2730
apply_sign(F x,U32 sign)2731 SI F apply_sign(F x, U32 sign) {
2732 return sk_bit_cast<F>(sign | sk_bit_cast<U32>(x));
2733 }
2734
STAGE(parametric,const skcms_TransferFunction * ctx)2735 STAGE(parametric, const skcms_TransferFunction* ctx) {
2736 auto fn = [&](F v) {
2737 U32 sign;
2738 v = strip_sign(v, &sign);
2739
2740 F r = if_then_else(v <= ctx->d, mad(ctx->c, v, ctx->f)
2741 , approx_powf(mad(ctx->a, v, ctx->b), ctx->g) + ctx->e);
2742 return apply_sign(r, sign);
2743 };
2744 r = fn(r);
2745 g = fn(g);
2746 b = fn(b);
2747 }
2748
STAGE(gamma_,const float * G)2749 STAGE(gamma_, const float* G) {
2750 auto fn = [&](F v) {
2751 U32 sign;
2752 v = strip_sign(v, &sign);
2753 return apply_sign(approx_powf(v, *G), sign);
2754 };
2755 r = fn(r);
2756 g = fn(g);
2757 b = fn(b);
2758 }
2759
STAGE(PQish,const skcms_TransferFunction * ctx)2760 STAGE(PQish, const skcms_TransferFunction* ctx) {
2761 auto fn = [&](F v) {
2762 U32 sign;
2763 v = strip_sign(v, &sign);
2764
2765 F r = approx_powf(max(mad(ctx->b, approx_powf(v, ctx->c), ctx->a), 0.0f)
2766 / (mad(ctx->e, approx_powf(v, ctx->c), ctx->d)),
2767 ctx->f);
2768
2769 return apply_sign(r, sign);
2770 };
2771 r = fn(r);
2772 g = fn(g);
2773 b = fn(b);
2774 }
2775
STAGE(HLGish,const skcms_TransferFunction * ctx)2776 STAGE(HLGish, const skcms_TransferFunction* ctx) {
2777 auto fn = [&](F v) {
2778 U32 sign;
2779 v = strip_sign(v, &sign);
2780
2781 const float R = ctx->a, G = ctx->b,
2782 a = ctx->c, b = ctx->d, c = ctx->e,
2783 K = ctx->f + 1.0f;
2784
2785 F r = if_then_else(v*R <= 1, approx_powf(v*R, G)
2786 , approx_exp((v-c)*a) + b);
2787
2788 return K * apply_sign(r, sign);
2789 };
2790 r = fn(r);
2791 g = fn(g);
2792 b = fn(b);
2793 }
2794
STAGE(HLGinvish,const skcms_TransferFunction * ctx)2795 STAGE(HLGinvish, const skcms_TransferFunction* ctx) {
2796 auto fn = [&](F v) {
2797 U32 sign;
2798 v = strip_sign(v, &sign);
2799
2800 const float R = ctx->a, G = ctx->b,
2801 a = ctx->c, b = ctx->d, c = ctx->e,
2802 K = ctx->f + 1.0f;
2803
2804 v /= K;
2805 F r = if_then_else(v <= 1, R * approx_powf(v, G)
2806 , a * approx_log(v - b) + c);
2807
2808 return apply_sign(r, sign);
2809 };
2810 r = fn(r);
2811 g = fn(g);
2812 b = fn(b);
2813 }
2814
STAGE(load_a8,const SkRasterPipeline_MemoryCtx * ctx)2815 STAGE(load_a8, const SkRasterPipeline_MemoryCtx* ctx) {
2816 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2817
2818 r = g = b = F0;
2819 a = from_byte(load<U8>(ptr));
2820 }
STAGE(load_a8_dst,const SkRasterPipeline_MemoryCtx * ctx)2821 STAGE(load_a8_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2822 auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
2823
2824 dr = dg = db = F0;
2825 da = from_byte(load<U8>(ptr));
2826 }
STAGE(gather_a8,const SkRasterPipeline_GatherCtx * ctx)2827 STAGE(gather_a8, const SkRasterPipeline_GatherCtx* ctx) {
2828 const uint8_t* ptr;
2829 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2830 r = g = b = F0;
2831 a = from_byte(gather(ptr, ix));
2832 }
STAGE(store_a8,const SkRasterPipeline_MemoryCtx * ctx)2833 STAGE(store_a8, const SkRasterPipeline_MemoryCtx* ctx) {
2834 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
2835
2836 U8 packed = pack(pack(to_unorm(a, 255)));
2837 store(ptr, packed);
2838 }
STAGE(store_r8,const SkRasterPipeline_MemoryCtx * ctx)2839 STAGE(store_r8, const SkRasterPipeline_MemoryCtx* ctx) {
2840 auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
2841
2842 U8 packed = pack(pack(to_unorm(r, 255)));
2843 store(ptr, packed);
2844 }
2845
STAGE(load_565,const SkRasterPipeline_MemoryCtx * ctx)2846 STAGE(load_565, const SkRasterPipeline_MemoryCtx* ctx) {
2847 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2848
2849 from_565(load<U16>(ptr), &r,&g,&b);
2850 a = F1;
2851 }
STAGE(load_565_dst,const SkRasterPipeline_MemoryCtx * ctx)2852 STAGE(load_565_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2853 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2854
2855 from_565(load<U16>(ptr), &dr,&dg,&db);
2856 da = F1;
2857 }
STAGE(gather_565,const SkRasterPipeline_GatherCtx * ctx)2858 STAGE(gather_565, const SkRasterPipeline_GatherCtx* ctx) {
2859 const uint16_t* ptr;
2860 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2861 from_565(gather(ptr, ix), &r,&g,&b);
2862 a = F1;
2863 }
STAGE(store_565,const SkRasterPipeline_MemoryCtx * ctx)2864 STAGE(store_565, const SkRasterPipeline_MemoryCtx* ctx) {
2865 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2866
2867 U16 px = pack( to_unorm(r, 31) << 11
2868 | to_unorm(g, 63) << 5
2869 | to_unorm(b, 31) );
2870 store(ptr, px);
2871 }
2872
STAGE(load_4444,const SkRasterPipeline_MemoryCtx * ctx)2873 STAGE(load_4444, const SkRasterPipeline_MemoryCtx* ctx) {
2874 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2875 from_4444(load<U16>(ptr), &r,&g,&b,&a);
2876 }
STAGE(load_4444_dst,const SkRasterPipeline_MemoryCtx * ctx)2877 STAGE(load_4444_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2878 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2879 from_4444(load<U16>(ptr), &dr,&dg,&db,&da);
2880 }
STAGE(gather_4444,const SkRasterPipeline_GatherCtx * ctx)2881 STAGE(gather_4444, const SkRasterPipeline_GatherCtx* ctx) {
2882 const uint16_t* ptr;
2883 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2884 from_4444(gather(ptr, ix), &r,&g,&b,&a);
2885 }
STAGE(store_4444,const SkRasterPipeline_MemoryCtx * ctx)2886 STAGE(store_4444, const SkRasterPipeline_MemoryCtx* ctx) {
2887 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2888 U16 px = pack( to_unorm(r, 15) << 12
2889 | to_unorm(g, 15) << 8
2890 | to_unorm(b, 15) << 4
2891 | to_unorm(a, 15) );
2892 store(ptr, px);
2893 }
2894
STAGE(load_8888,const SkRasterPipeline_MemoryCtx * ctx)2895 STAGE(load_8888, const SkRasterPipeline_MemoryCtx* ctx) {
2896 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2897 from_8888(load<U32>(ptr), &r,&g,&b,&a);
2898 }
STAGE(load_8888_dst,const SkRasterPipeline_MemoryCtx * ctx)2899 STAGE(load_8888_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2900 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
2901 from_8888(load<U32>(ptr), &dr,&dg,&db,&da);
2902 }
STAGE(gather_8888,const SkRasterPipeline_GatherCtx * ctx)2903 STAGE(gather_8888, const SkRasterPipeline_GatherCtx* ctx) {
2904 const uint32_t* ptr;
2905 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
2906 from_8888(gather(ptr, ix), &r,&g,&b,&a);
2907 }
STAGE(store_8888,const SkRasterPipeline_MemoryCtx * ctx)2908 STAGE(store_8888, const SkRasterPipeline_MemoryCtx* ctx) {
2909 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2910
2911 U32 px = to_unorm(r, 255)
2912 | to_unorm(g, 255) << 8
2913 | to_unorm(b, 255) << 16
2914 | to_unorm(a, 255) << 24;
2915 store(ptr, px);
2916 }
2917
STAGE(load_rg88,const SkRasterPipeline_MemoryCtx * ctx)2918 STAGE(load_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
2919 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2920 from_88(load<U16>(ptr), &r, &g);
2921 b = F0;
2922 a = F1;
2923 }
STAGE(load_rg88_dst,const SkRasterPipeline_MemoryCtx * ctx)2924 STAGE(load_rg88_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2925 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2926 from_88(load<U16>(ptr), &dr, &dg);
2927 db = F0;
2928 da = F1;
2929 }
STAGE(gather_rg88,const SkRasterPipeline_GatherCtx * ctx)2930 STAGE(gather_rg88, const SkRasterPipeline_GatherCtx* ctx) {
2931 const uint16_t* ptr;
2932 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2933 from_88(gather(ptr, ix), &r, &g);
2934 b = F0;
2935 a = F1;
2936 }
STAGE(store_rg88,const SkRasterPipeline_MemoryCtx * ctx)2937 STAGE(store_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
2938 auto ptr = ptr_at_xy<uint16_t>(ctx, dx, dy);
2939 U16 px = pack( to_unorm(r, 255) | to_unorm(g, 255) << 8 );
2940 store(ptr, px);
2941 }
2942
STAGE(load_a16,const SkRasterPipeline_MemoryCtx * ctx)2943 STAGE(load_a16, const SkRasterPipeline_MemoryCtx* ctx) {
2944 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
2945 r = g = b = F0;
2946 a = from_short(load<U16>(ptr));
2947 }
STAGE(load_a16_dst,const SkRasterPipeline_MemoryCtx * ctx)2948 STAGE(load_a16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2949 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
2950 dr = dg = db = F0;
2951 da = from_short(load<U16>(ptr));
2952 }
STAGE(gather_a16,const SkRasterPipeline_GatherCtx * ctx)2953 STAGE(gather_a16, const SkRasterPipeline_GatherCtx* ctx) {
2954 const uint16_t* ptr;
2955 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2956 r = g = b = F0;
2957 a = from_short(gather(ptr, ix));
2958 }
STAGE(store_a16,const SkRasterPipeline_MemoryCtx * ctx)2959 STAGE(store_a16, const SkRasterPipeline_MemoryCtx* ctx) {
2960 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
2961
2962 U16 px = pack(to_unorm(a, 65535));
2963 store(ptr, px);
2964 }
2965
STAGE(load_rg1616,const SkRasterPipeline_MemoryCtx * ctx)2966 STAGE(load_rg1616, const SkRasterPipeline_MemoryCtx* ctx) {
2967 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
2968 b = F0;
2969 a = F1;
2970 from_1616(load<U32>(ptr), &r,&g);
2971 }
STAGE(load_rg1616_dst,const SkRasterPipeline_MemoryCtx * ctx)2972 STAGE(load_rg1616_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2973 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
2974 from_1616(load<U32>(ptr), &dr, &dg);
2975 db = F0;
2976 da = F1;
2977 }
STAGE(gather_rg1616,const SkRasterPipeline_GatherCtx * ctx)2978 STAGE(gather_rg1616, const SkRasterPipeline_GatherCtx* ctx) {
2979 const uint32_t* ptr;
2980 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
2981 from_1616(gather(ptr, ix), &r, &g);
2982 b = F0;
2983 a = F1;
2984 }
STAGE(store_rg1616,const SkRasterPipeline_MemoryCtx * ctx)2985 STAGE(store_rg1616, const SkRasterPipeline_MemoryCtx* ctx) {
2986 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
2987
2988 U32 px = to_unorm(r, 65535)
2989 | to_unorm(g, 65535) << 16;
2990 store(ptr, px);
2991 }
2992
STAGE(load_16161616,const SkRasterPipeline_MemoryCtx * ctx)2993 STAGE(load_16161616, const SkRasterPipeline_MemoryCtx* ctx) {
2994 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
2995 from_16161616(load<U64>(ptr), &r,&g, &b, &a);
2996 }
STAGE(load_16161616_dst,const SkRasterPipeline_MemoryCtx * ctx)2997 STAGE(load_16161616_dst, const SkRasterPipeline_MemoryCtx* ctx) {
2998 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
2999 from_16161616(load<U64>(ptr), &dr, &dg, &db, &da);
3000 }
STAGE(gather_16161616,const SkRasterPipeline_GatherCtx * ctx)3001 STAGE(gather_16161616, const SkRasterPipeline_GatherCtx* ctx) {
3002 const uint64_t* ptr;
3003 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3004 from_16161616(gather(ptr, ix), &r, &g, &b, &a);
3005 }
STAGE(store_16161616,const SkRasterPipeline_MemoryCtx * ctx)3006 STAGE(store_16161616, const SkRasterPipeline_MemoryCtx* ctx) {
3007 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3008
3009 U16 R = pack(to_unorm(r, 65535)),
3010 G = pack(to_unorm(g, 65535)),
3011 B = pack(to_unorm(b, 65535)),
3012 A = pack(to_unorm(a, 65535));
3013
3014 store4(ptr, R,G,B,A);
3015 }
3016
STAGE(load_10x6,const SkRasterPipeline_MemoryCtx * ctx)3017 STAGE(load_10x6, const SkRasterPipeline_MemoryCtx* ctx) {
3018 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3019 from_10x6(load<U64>(ptr), &r,&g, &b, &a);
3020 }
STAGE(load_10x6_dst,const SkRasterPipeline_MemoryCtx * ctx)3021 STAGE(load_10x6_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3022 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3023 from_10x6(load<U64>(ptr), &dr, &dg, &db, &da);
3024 }
STAGE(gather_10x6,const SkRasterPipeline_GatherCtx * ctx)3025 STAGE(gather_10x6, const SkRasterPipeline_GatherCtx* ctx) {
3026 const uint64_t* ptr;
3027 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3028 from_10x6(gather(ptr, ix), &r, &g, &b, &a);
3029 }
STAGE(store_10x6,const SkRasterPipeline_MemoryCtx * ctx)3030 STAGE(store_10x6, const SkRasterPipeline_MemoryCtx* ctx) {
3031 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3032
3033 U16 R = pack(to_unorm(r, 1023)) << 6,
3034 G = pack(to_unorm(g, 1023)) << 6,
3035 B = pack(to_unorm(b, 1023)) << 6,
3036 A = pack(to_unorm(a, 1023)) << 6;
3037
3038 store4(ptr, R,G,B,A);
3039 }
3040
3041
STAGE(load_1010102,const SkRasterPipeline_MemoryCtx * ctx)3042 STAGE(load_1010102, const SkRasterPipeline_MemoryCtx* ctx) {
3043 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3044 from_1010102(load<U32>(ptr), &r,&g,&b,&a);
3045 }
STAGE(load_1010102_dst,const SkRasterPipeline_MemoryCtx * ctx)3046 STAGE(load_1010102_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3047 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3048 from_1010102(load<U32>(ptr), &dr,&dg,&db,&da);
3049 }
STAGE(load_1010102_xr,const SkRasterPipeline_MemoryCtx * ctx)3050 STAGE(load_1010102_xr, const SkRasterPipeline_MemoryCtx* ctx) {
3051 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3052 from_1010102_xr(load<U32>(ptr), &r,&g,&b,&a);
3053 }
STAGE(load_1010102_xr_dst,const SkRasterPipeline_MemoryCtx * ctx)3054 STAGE(load_1010102_xr_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3055 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx,dy);
3056 from_1010102_xr(load<U32>(ptr), &dr,&dg,&db,&da);
3057 }
STAGE(gather_1010102,const SkRasterPipeline_GatherCtx * ctx)3058 STAGE(gather_1010102, const SkRasterPipeline_GatherCtx* ctx) {
3059 const uint32_t* ptr;
3060 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3061 from_1010102(gather(ptr, ix), &r,&g,&b,&a);
3062 }
STAGE(gather_1010102_xr,const SkRasterPipeline_GatherCtx * ctx)3063 STAGE(gather_1010102_xr, const SkRasterPipeline_GatherCtx* ctx) {
3064 const uint32_t* ptr;
3065 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3066 from_1010102_xr(gather(ptr, ix), &r,&g,&b,&a);
3067 }
STAGE(gather_10101010_xr,const SkRasterPipeline_GatherCtx * ctx)3068 STAGE(gather_10101010_xr, const SkRasterPipeline_GatherCtx* ctx) {
3069 const uint64_t* ptr;
3070 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3071 from_10101010_xr(gather(ptr, ix), &r, &g, &b, &a);
3072 }
STAGE(load_10101010_xr,const SkRasterPipeline_MemoryCtx * ctx)3073 STAGE(load_10101010_xr, const SkRasterPipeline_MemoryCtx* ctx) {
3074 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3075 from_10101010_xr(load<U64>(ptr), &r,&g, &b, &a);
3076 }
STAGE(load_10101010_xr_dst,const SkRasterPipeline_MemoryCtx * ctx)3077 STAGE(load_10101010_xr_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3078 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx, dy);
3079 from_10101010_xr(load<U64>(ptr), &dr, &dg, &db, &da);
3080 }
STAGE(store_10101010_xr,const SkRasterPipeline_MemoryCtx * ctx)3081 STAGE(store_10101010_xr, const SkRasterPipeline_MemoryCtx* ctx) {
3082 static constexpr float min = -0.752941f;
3083 static constexpr float max = 1.25098f;
3084 static constexpr float range = max - min;
3085 auto ptr = ptr_at_xy<uint16_t>(ctx, 4*dx,4*dy);
3086
3087 U16 R = pack(to_unorm((r - min) / range, 1023)) << 6,
3088 G = pack(to_unorm((g - min) / range, 1023)) << 6,
3089 B = pack(to_unorm((b - min) / range, 1023)) << 6,
3090 A = pack(to_unorm((a - min) / range, 1023)) << 6;
3091
3092 store4(ptr, R,G,B,A);
3093 }
STAGE(store_1010102,const SkRasterPipeline_MemoryCtx * ctx)3094 STAGE(store_1010102, const SkRasterPipeline_MemoryCtx* ctx) {
3095 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3096
3097 U32 px = to_unorm(r, 1023)
3098 | to_unorm(g, 1023) << 10
3099 | to_unorm(b, 1023) << 20
3100 | to_unorm(a, 3) << 30;
3101 store(ptr, px);
3102 }
STAGE(store_1010102_xr,const SkRasterPipeline_MemoryCtx * ctx)3103 STAGE(store_1010102_xr, const SkRasterPipeline_MemoryCtx* ctx) {
3104 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
3105 static constexpr float min = -0.752941f;
3106 static constexpr float max = 1.25098f;
3107 static constexpr float range = max - min;
3108 U32 px = to_unorm((r - min) / range, 1023)
3109 | to_unorm((g - min) / range, 1023) << 10
3110 | to_unorm((b - min) / range, 1023) << 20
3111 | to_unorm(a, 3) << 30;
3112 store(ptr, px);
3113 }
3114
STAGE(load_f16,const SkRasterPipeline_MemoryCtx * ctx)3115 STAGE(load_f16, const SkRasterPipeline_MemoryCtx* ctx) {
3116 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
3117
3118 U16 R,G,B,A;
3119 load4((const uint16_t*)ptr, &R,&G,&B,&A);
3120 r = from_half(R);
3121 g = from_half(G);
3122 b = from_half(B);
3123 a = from_half(A);
3124 }
STAGE(load_f16_dst,const SkRasterPipeline_MemoryCtx * ctx)3125 STAGE(load_f16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3126 auto ptr = ptr_at_xy<const uint64_t>(ctx, dx,dy);
3127
3128 U16 R,G,B,A;
3129 load4((const uint16_t*)ptr, &R,&G,&B,&A);
3130 dr = from_half(R);
3131 dg = from_half(G);
3132 db = from_half(B);
3133 da = from_half(A);
3134 }
STAGE(gather_f16,const SkRasterPipeline_GatherCtx * ctx)3135 STAGE(gather_f16, const SkRasterPipeline_GatherCtx* ctx) {
3136 const uint64_t* ptr;
3137 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3138 auto px = gather(ptr, ix);
3139
3140 U16 R,G,B,A;
3141 load4((const uint16_t*)&px, &R,&G,&B,&A);
3142 r = from_half(R);
3143 g = from_half(G);
3144 b = from_half(B);
3145 a = from_half(A);
3146 }
STAGE(store_f16,const SkRasterPipeline_MemoryCtx * ctx)3147 STAGE(store_f16, const SkRasterPipeline_MemoryCtx* ctx) {
3148 auto ptr = ptr_at_xy<uint64_t>(ctx, dx,dy);
3149 store4((uint16_t*)ptr, to_half(r)
3150 , to_half(g)
3151 , to_half(b)
3152 , to_half(a));
3153 }
3154
STAGE(load_af16,const SkRasterPipeline_MemoryCtx * ctx)3155 STAGE(load_af16, const SkRasterPipeline_MemoryCtx* ctx) {
3156 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
3157
3158 U16 A = load<U16>((const uint16_t*)ptr);
3159 r = F0;
3160 g = F0;
3161 b = F0;
3162 a = from_half(A);
3163 }
STAGE(load_af16_dst,const SkRasterPipeline_MemoryCtx * ctx)3164 STAGE(load_af16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3165 auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
3166
3167 U16 A = load<U16>((const uint16_t*)ptr);
3168 dr = dg = db = F0;
3169 da = from_half(A);
3170 }
STAGE(gather_af16,const SkRasterPipeline_GatherCtx * ctx)3171 STAGE(gather_af16, const SkRasterPipeline_GatherCtx* ctx) {
3172 const uint16_t* ptr;
3173 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3174 r = g = b = F0;
3175 a = from_half(gather(ptr, ix));
3176 }
STAGE(store_af16,const SkRasterPipeline_MemoryCtx * ctx)3177 STAGE(store_af16, const SkRasterPipeline_MemoryCtx* ctx) {
3178 auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
3179 store(ptr, to_half(a));
3180 }
3181
STAGE(load_rgf16,const SkRasterPipeline_MemoryCtx * ctx)3182 STAGE(load_rgf16, const SkRasterPipeline_MemoryCtx* ctx) {
3183 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3184
3185 U16 R,G;
3186 load2((const uint16_t*)ptr, &R, &G);
3187 r = from_half(R);
3188 g = from_half(G);
3189 b = F0;
3190 a = F1;
3191 }
STAGE(load_rgf16_dst,const SkRasterPipeline_MemoryCtx * ctx)3192 STAGE(load_rgf16_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3193 auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
3194
3195 U16 R,G;
3196 load2((const uint16_t*)ptr, &R, &G);
3197 dr = from_half(R);
3198 dg = from_half(G);
3199 db = F0;
3200 da = F1;
3201 }
STAGE(gather_rgf16,const SkRasterPipeline_GatherCtx * ctx)3202 STAGE(gather_rgf16, const SkRasterPipeline_GatherCtx* ctx) {
3203 const uint32_t* ptr;
3204 U32 ix = ix_and_ptr(&ptr, ctx, r, g);
3205 auto px = gather(ptr, ix);
3206
3207 U16 R,G;
3208 load2((const uint16_t*)&px, &R, &G);
3209 r = from_half(R);
3210 g = from_half(G);
3211 b = F0;
3212 a = F1;
3213 }
STAGE(store_rgf16,const SkRasterPipeline_MemoryCtx * ctx)3214 STAGE(store_rgf16, const SkRasterPipeline_MemoryCtx* ctx) {
3215 auto ptr = ptr_at_xy<uint32_t>(ctx, dx, dy);
3216 store2((uint16_t*)ptr, to_half(r)
3217 , to_half(g));
3218 }
3219
STAGE(load_f32,const SkRasterPipeline_MemoryCtx * ctx)3220 STAGE(load_f32, const SkRasterPipeline_MemoryCtx* ctx) {
3221 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
3222 load4(ptr, &r,&g,&b,&a);
3223 }
STAGE(load_f32_dst,const SkRasterPipeline_MemoryCtx * ctx)3224 STAGE(load_f32_dst, const SkRasterPipeline_MemoryCtx* ctx) {
3225 auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
3226 load4(ptr, &dr,&dg,&db,&da);
3227 }
STAGE(gather_f32,const SkRasterPipeline_GatherCtx * ctx)3228 STAGE(gather_f32, const SkRasterPipeline_GatherCtx* ctx) {
3229 const float* ptr;
3230 U32 ix = ix_and_ptr(&ptr, ctx, r,g);
3231 r = gather(ptr, 4*ix + 0);
3232 g = gather(ptr, 4*ix + 1);
3233 b = gather(ptr, 4*ix + 2);
3234 a = gather(ptr, 4*ix + 3);
3235 }
STAGE(store_f32,const SkRasterPipeline_MemoryCtx * ctx)3236 STAGE(store_f32, const SkRasterPipeline_MemoryCtx* ctx) {
3237 auto ptr = ptr_at_xy<float>(ctx, 4*dx,4*dy);
3238 store4(ptr, r,g,b,a);
3239 }
3240
exclusive_repeat(F v,const SkRasterPipeline_TileCtx * ctx)3241 SI F exclusive_repeat(F v, const SkRasterPipeline_TileCtx* ctx) {
3242 return v - floor_(v*ctx->invScale)*ctx->scale;
3243 }
exclusive_mirror(F v,const SkRasterPipeline_TileCtx * ctx)3244 SI F exclusive_mirror(F v, const SkRasterPipeline_TileCtx* ctx) {
3245 auto limit = ctx->scale;
3246 auto invLimit = ctx->invScale;
3247
3248 // This is "repeat" over the range 0..2*limit
3249 auto u = v - floor_(v*invLimit*0.5f)*2*limit;
3250 // s will be 0 when moving forward (e.g. [0, limit)) and 1 when moving backward (e.g.
3251 // [limit, 2*limit)).
3252 auto s = floor_(u*invLimit);
3253 // This is the mirror result.
3254 auto m = u - 2*s*(u - limit);
3255 // Apply a bias to m if moving backwards so that we snap consistently at exact integer coords in
3256 // the logical infinite image. This is tested by mirror_tile GM. Note that all values
3257 // that have a non-zero bias applied are > 0.
3258 auto biasInUlps = trunc_(s);
3259 return sk_bit_cast<F>(sk_bit_cast<U32>(m) + ctx->mirrorBiasDir*biasInUlps);
3260 }
3261 // Tile x or y to [0,limit) == [0,limit - 1 ulp] (think, sampling from images).
3262 // The gather stages will hard clamp the output of these stages to [0,limit)...
3263 // we just need to do the basic repeat or mirroring.
STAGE(repeat_x,const SkRasterPipeline_TileCtx * ctx)3264 STAGE(repeat_x, const SkRasterPipeline_TileCtx* ctx) { r = exclusive_repeat(r, ctx); }
STAGE(repeat_y,const SkRasterPipeline_TileCtx * ctx)3265 STAGE(repeat_y, const SkRasterPipeline_TileCtx* ctx) { g = exclusive_repeat(g, ctx); }
STAGE(mirror_x,const SkRasterPipeline_TileCtx * ctx)3266 STAGE(mirror_x, const SkRasterPipeline_TileCtx* ctx) { r = exclusive_mirror(r, ctx); }
STAGE(mirror_y,const SkRasterPipeline_TileCtx * ctx)3267 STAGE(mirror_y, const SkRasterPipeline_TileCtx* ctx) { g = exclusive_mirror(g, ctx); }
3268
STAGE(clamp_x_1,NoCtx)3269 STAGE( clamp_x_1, NoCtx) { r = clamp_01_(r); }
STAGE(repeat_x_1,NoCtx)3270 STAGE(repeat_x_1, NoCtx) { r = clamp_01_(r - floor_(r)); }
STAGE(mirror_x_1,NoCtx)3271 STAGE(mirror_x_1, NoCtx) { r = clamp_01_(abs_( (r-1.0f) - two(floor_((r-1.0f)*0.5f)) - 1.0f )); }
3272
STAGE(clamp_x_and_y,const SkRasterPipeline_CoordClampCtx * ctx)3273 STAGE(clamp_x_and_y, const SkRasterPipeline_CoordClampCtx* ctx) {
3274 r = min(ctx->max_x, max(ctx->min_x, r));
3275 g = min(ctx->max_y, max(ctx->min_y, g));
3276 }
3277
3278 // Decal stores a 32bit mask after checking the coordinate (x and/or y) against its domain:
3279 // mask == 0x00000000 if the coordinate(s) are out of bounds
3280 // mask == 0xFFFFFFFF if the coordinate(s) are in bounds
3281 // After the gather stage, the r,g,b,a values are AND'd with this mask, setting them to 0
3282 // if either of the coordinates were out of bounds.
3283
STAGE(decal_x,SkRasterPipeline_DecalTileCtx * ctx)3284 STAGE(decal_x, SkRasterPipeline_DecalTileCtx* ctx) {
3285 auto w = ctx->limit_x;
3286 auto e = ctx->inclusiveEdge_x;
3287 auto cond = ((0 < r) & (r < w)) | (r == e);
3288 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3289 }
STAGE(decal_y,SkRasterPipeline_DecalTileCtx * ctx)3290 STAGE(decal_y, SkRasterPipeline_DecalTileCtx* ctx) {
3291 auto h = ctx->limit_y;
3292 auto e = ctx->inclusiveEdge_y;
3293 auto cond = ((0 < g) & (g < h)) | (g == e);
3294 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3295 }
STAGE(decal_x_and_y,SkRasterPipeline_DecalTileCtx * ctx)3296 STAGE(decal_x_and_y, SkRasterPipeline_DecalTileCtx* ctx) {
3297 auto w = ctx->limit_x;
3298 auto h = ctx->limit_y;
3299 auto ex = ctx->inclusiveEdge_x;
3300 auto ey = ctx->inclusiveEdge_y;
3301 auto cond = (((0 < r) & (r < w)) | (r == ex))
3302 & (((0 < g) & (g < h)) | (g == ey));
3303 sk_unaligned_store(ctx->mask, cond_to_mask(cond));
3304 }
STAGE(check_decal_mask,SkRasterPipeline_DecalTileCtx * ctx)3305 STAGE(check_decal_mask, SkRasterPipeline_DecalTileCtx* ctx) {
3306 auto mask = sk_unaligned_load<U32>(ctx->mask);
3307 r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
3308 g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
3309 b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
3310 a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
3311 }
3312
STAGE(alpha_to_gray,NoCtx)3313 STAGE(alpha_to_gray, NoCtx) {
3314 r = g = b = a;
3315 a = F1;
3316 }
STAGE(alpha_to_gray_dst,NoCtx)3317 STAGE(alpha_to_gray_dst, NoCtx) {
3318 dr = dg = db = da;
3319 da = F1;
3320 }
STAGE(alpha_to_red,NoCtx)3321 STAGE(alpha_to_red, NoCtx) {
3322 r = a;
3323 a = F1;
3324 }
STAGE(alpha_to_red_dst,NoCtx)3325 STAGE(alpha_to_red_dst, NoCtx) {
3326 dr = da;
3327 da = F1;
3328 }
3329
STAGE(bt709_luminance_or_luma_to_alpha,NoCtx)3330 STAGE(bt709_luminance_or_luma_to_alpha, NoCtx) {
3331 a = r*0.2126f + g*0.7152f + b*0.0722f;
3332 r = g = b = F0;
3333 }
STAGE(bt709_luminance_or_luma_to_rgb,NoCtx)3334 STAGE(bt709_luminance_or_luma_to_rgb, NoCtx) {
3335 r = g = b = r*0.2126f + g*0.7152f + b*0.0722f;
3336 }
3337
STAGE(matrix_translate,const float * m)3338 STAGE(matrix_translate, const float* m) {
3339 r += m[0];
3340 g += m[1];
3341 }
STAGE(matrix_scale_translate,const float * m)3342 STAGE(matrix_scale_translate, const float* m) {
3343 r = mad(r,m[0], m[2]);
3344 g = mad(g,m[1], m[3]);
3345 }
STAGE(matrix_2x3,const float * m)3346 STAGE(matrix_2x3, const float* m) {
3347 auto R = mad(r,m[0], mad(g,m[1], m[2])),
3348 G = mad(r,m[3], mad(g,m[4], m[5]));
3349 r = R;
3350 g = G;
3351 }
STAGE(matrix_3x3,const float * m)3352 STAGE(matrix_3x3, const float* m) {
3353 auto R = mad(r,m[0], mad(g,m[3], b*m[6])),
3354 G = mad(r,m[1], mad(g,m[4], b*m[7])),
3355 B = mad(r,m[2], mad(g,m[5], b*m[8]));
3356 r = R;
3357 g = G;
3358 b = B;
3359 }
STAGE(matrix_3x4,const float * m)3360 STAGE(matrix_3x4, const float* m) {
3361 auto R = mad(r,m[0], mad(g,m[3], mad(b,m[6], m[ 9]))),
3362 G = mad(r,m[1], mad(g,m[4], mad(b,m[7], m[10]))),
3363 B = mad(r,m[2], mad(g,m[5], mad(b,m[8], m[11])));
3364 r = R;
3365 g = G;
3366 b = B;
3367 }
STAGE(matrix_4x5,const float * m)3368 STAGE(matrix_4x5, const float* m) {
3369 auto R = mad(r,m[ 0], mad(g,m[ 1], mad(b,m[ 2], mad(a,m[ 3], m[ 4])))),
3370 G = mad(r,m[ 5], mad(g,m[ 6], mad(b,m[ 7], mad(a,m[ 8], m[ 9])))),
3371 B = mad(r,m[10], mad(g,m[11], mad(b,m[12], mad(a,m[13], m[14])))),
3372 A = mad(r,m[15], mad(g,m[16], mad(b,m[17], mad(a,m[18], m[19]))));
3373 r = R;
3374 g = G;
3375 b = B;
3376 a = A;
3377 }
STAGE(matrix_4x3,const float * m)3378 STAGE(matrix_4x3, const float* m) {
3379 auto X = r,
3380 Y = g;
3381
3382 r = mad(X, m[0], mad(Y, m[4], m[ 8]));
3383 g = mad(X, m[1], mad(Y, m[5], m[ 9]));
3384 b = mad(X, m[2], mad(Y, m[6], m[10]));
3385 a = mad(X, m[3], mad(Y, m[7], m[11]));
3386 }
STAGE(matrix_perspective,const float * m)3387 STAGE(matrix_perspective, const float* m) {
3388 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
3389 auto R = mad(r,m[0], mad(g,m[1], m[2])),
3390 G = mad(r,m[3], mad(g,m[4], m[5])),
3391 Z = mad(r,m[6], mad(g,m[7], m[8]));
3392 r = R * rcp_precise(Z);
3393 g = G * rcp_precise(Z);
3394 }
3395
gradient_lookup(const SkRasterPipeline_GradientCtx * c,U32 idx,F t,F * r,F * g,F * b,F * a)3396 SI void gradient_lookup(const SkRasterPipeline_GradientCtx* c, U32 idx, F t,
3397 F* r, F* g, F* b, F* a) {
3398 F fr, br, fg, bg, fb, bb, fa, ba;
3399 #if defined(SKRP_CPU_HSW)
3400 if (c->stopCount <=8) {
3401 fr = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), (__m256i)idx);
3402 br = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), (__m256i)idx);
3403 fg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), (__m256i)idx);
3404 bg = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), (__m256i)idx);
3405 fb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), (__m256i)idx);
3406 bb = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), (__m256i)idx);
3407 fa = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), (__m256i)idx);
3408 ba = _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), (__m256i)idx);
3409 } else
3410 #elif defined(SKRP_CPU_LASX)
3411 if (c->stopCount <= 8) {
3412 fr = (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[0], 0), idx);
3413 br = (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[0], 0), idx);
3414 fg = (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[1], 0), idx);
3415 bg = (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[1], 0), idx);
3416 fb = (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[2], 0), idx);
3417 bb = (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[2], 0), idx);
3418 fa = (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[3], 0), idx);
3419 ba = (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[3], 0), idx);
3420 } else
3421 #elif defined(SKRP_CPU_LSX)
3422 if (c->stopCount <= 4) {
3423 __m128i zero = __lsx_vldi(0);
3424 fr = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->fs[0], 0));
3425 br = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->bs[0], 0));
3426 fg = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->fs[1], 0));
3427 bg = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->bs[1], 0));
3428 fb = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->fs[2], 0));
3429 bb = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->bs[2], 0));
3430 fa = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->fs[3], 0));
3431 ba = (__m128)__lsx_vshuf_w(idx, zero, __lsx_vld(c->bs[3], 0));
3432 } else
3433 #endif
3434 {
3435 #if defined(SKRP_CPU_LSX)
3436 // This can reduce some vpickve2gr instructions.
3437 int i0 = __lsx_vpickve2gr_w(idx, 0);
3438 int i1 = __lsx_vpickve2gr_w(idx, 1);
3439 int i2 = __lsx_vpickve2gr_w(idx, 2);
3440 int i3 = __lsx_vpickve2gr_w(idx, 3);
3441 fr = gather((int *)c->fs[0], i0, i1, i2, i3);
3442 br = gather((int *)c->bs[0], i0, i1, i2, i3);
3443 fg = gather((int *)c->fs[1], i0, i1, i2, i3);
3444 bg = gather((int *)c->bs[1], i0, i1, i2, i3);
3445 fb = gather((int *)c->fs[2], i0, i1, i2, i3);
3446 bb = gather((int *)c->bs[2], i0, i1, i2, i3);
3447 fa = gather((int *)c->fs[3], i0, i1, i2, i3);
3448 ba = gather((int *)c->bs[3], i0, i1, i2, i3);
3449 #else
3450 fr = gather(c->fs[0], idx);
3451 br = gather(c->bs[0], idx);
3452 fg = gather(c->fs[1], idx);
3453 bg = gather(c->bs[1], idx);
3454 fb = gather(c->fs[2], idx);
3455 bb = gather(c->bs[2], idx);
3456 fa = gather(c->fs[3], idx);
3457 ba = gather(c->bs[3], idx);
3458 #endif
3459 }
3460
3461 *r = mad(t, fr, br);
3462 *g = mad(t, fg, bg);
3463 *b = mad(t, fb, bb);
3464 *a = mad(t, fa, ba);
3465 }
3466
STAGE(evenly_spaced_gradient,const SkRasterPipeline_GradientCtx * c)3467 STAGE(evenly_spaced_gradient, const SkRasterPipeline_GradientCtx* c) {
3468 auto t = r;
3469 auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
3470 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3471 }
3472
STAGE(gradient,const SkRasterPipeline_GradientCtx * c)3473 STAGE(gradient, const SkRasterPipeline_GradientCtx* c) {
3474 auto t = r;
3475 U32 idx = U32_(0);
3476
3477 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
3478 for (size_t i = 1; i < c->stopCount; i++) {
3479 idx += (U32)if_then_else(t >= c->ts[i], I32_(1), I32_(0));
3480 }
3481
3482 gradient_lookup(c, idx, t, &r, &g, &b, &a);
3483 }
3484
STAGE(evenly_spaced_2_stop_gradient,const SkRasterPipeline_EvenlySpaced2StopGradientCtx * c)3485 STAGE(evenly_spaced_2_stop_gradient, const SkRasterPipeline_EvenlySpaced2StopGradientCtx* c) {
3486 auto t = r;
3487 r = mad(t, c->f[0], c->b[0]);
3488 g = mad(t, c->f[1], c->b[1]);
3489 b = mad(t, c->f[2], c->b[2]);
3490 a = mad(t, c->f[3], c->b[3]);
3491 }
3492
STAGE(xy_to_unit_angle,NoCtx)3493 STAGE(xy_to_unit_angle, NoCtx) {
3494 F X = r,
3495 Y = g;
3496 F xabs = abs_(X),
3497 yabs = abs_(Y);
3498
3499 F slope = min(xabs, yabs)/max(xabs, yabs);
3500 F s = slope * slope;
3501
3502 // Use a 7th degree polynomial to approximate atan.
3503 // This was generated using sollya.gforge.inria.fr.
3504 // A float optimized polynomial was generated using the following command.
3505 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
3506 F phi = slope
3507 * (0.15912117063999176025390625f + s
3508 * (-5.185396969318389892578125e-2f + s
3509 * (2.476101927459239959716796875e-2f + s
3510 * (-7.0547382347285747528076171875e-3f))));
3511
3512 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
3513 phi = if_then_else(X < 0.0f , 1.0f/2.0f - phi, phi);
3514 phi = if_then_else(Y < 0.0f , 1.0f - phi , phi);
3515 phi = if_then_else(phi != phi , 0.0f , phi); // Check for NaN.
3516 r = phi;
3517 }
3518
STAGE(xy_to_radius,NoCtx)3519 STAGE(xy_to_radius, NoCtx) {
3520 F X2 = r * r,
3521 Y2 = g * g;
3522 r = sqrt_(X2 + Y2);
3523 }
3524
3525 // Please see https://skia.org/dev/design/conical for how our 2pt conical shader works.
3526
STAGE(negate_x,NoCtx)3527 STAGE(negate_x, NoCtx) { r = -r; }
3528
STAGE(xy_to_2pt_conical_strip,const SkRasterPipeline_2PtConicalCtx * ctx)3529 STAGE(xy_to_2pt_conical_strip, const SkRasterPipeline_2PtConicalCtx* ctx) {
3530 F x = r, y = g, &t = r;
3531 t = x + sqrt_(ctx->fP0 - y*y); // ctx->fP0 = r0 * r0
3532 }
3533
STAGE(xy_to_2pt_conical_focal_on_circle,NoCtx)3534 STAGE(xy_to_2pt_conical_focal_on_circle, NoCtx) {
3535 F x = r, y = g, &t = r;
3536 t = x + y*y / x; // (x^2 + y^2) / x
3537 }
3538
STAGE(xy_to_2pt_conical_well_behaved,const SkRasterPipeline_2PtConicalCtx * ctx)3539 STAGE(xy_to_2pt_conical_well_behaved, const SkRasterPipeline_2PtConicalCtx* ctx) {
3540 F x = r, y = g, &t = r;
3541 t = sqrt_(x*x + y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3542 }
3543
STAGE(xy_to_2pt_conical_greater,const SkRasterPipeline_2PtConicalCtx * ctx)3544 STAGE(xy_to_2pt_conical_greater, const SkRasterPipeline_2PtConicalCtx* ctx) {
3545 F x = r, y = g, &t = r;
3546 t = sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3547 }
3548
STAGE(xy_to_2pt_conical_smaller,const SkRasterPipeline_2PtConicalCtx * ctx)3549 STAGE(xy_to_2pt_conical_smaller, const SkRasterPipeline_2PtConicalCtx* ctx) {
3550 F x = r, y = g, &t = r;
3551 t = -sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
3552 }
3553
STAGE(alter_2pt_conical_compensate_focal,const SkRasterPipeline_2PtConicalCtx * ctx)3554 STAGE(alter_2pt_conical_compensate_focal, const SkRasterPipeline_2PtConicalCtx* ctx) {
3555 F& t = r;
3556 t = t + ctx->fP1; // ctx->fP1 = f
3557 }
3558
STAGE(alter_2pt_conical_unswap,NoCtx)3559 STAGE(alter_2pt_conical_unswap, NoCtx) {
3560 F& t = r;
3561 t = 1 - t;
3562 }
3563
STAGE(mask_2pt_conical_nan,SkRasterPipeline_2PtConicalCtx * c)3564 STAGE(mask_2pt_conical_nan, SkRasterPipeline_2PtConicalCtx* c) {
3565 F& t = r;
3566 auto is_degenerate = (t != t); // NaN
3567 t = if_then_else(is_degenerate, F0, t);
3568 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
3569 }
3570
STAGE(mask_2pt_conical_degenerates,SkRasterPipeline_2PtConicalCtx * c)3571 STAGE(mask_2pt_conical_degenerates, SkRasterPipeline_2PtConicalCtx* c) {
3572 F& t = r;
3573 auto is_degenerate = (t <= 0) | (t != t);
3574 t = if_then_else(is_degenerate, F0, t);
3575 sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
3576 }
3577
STAGE(apply_vector_mask,const uint32_t * ctx)3578 STAGE(apply_vector_mask, const uint32_t* ctx) {
3579 const U32 mask = sk_unaligned_load<U32>(ctx);
3580 r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
3581 g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
3582 b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
3583 a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
3584 }
3585
save_xy(F * r,F * g,SkRasterPipeline_SamplerCtx * c)3586 SI void save_xy(F* r, F* g, SkRasterPipeline_SamplerCtx* c) {
3587 // Whether bilinear or bicubic, all sample points are at the same fractional offset (fx,fy).
3588 // They're either the 4 corners of a logical 1x1 pixel or the 16 corners of a 3x3 grid
3589 // surrounding (x,y) at (0.5,0.5) off-center.
3590 F fx = fract(*r + 0.5f),
3591 fy = fract(*g + 0.5f);
3592
3593 // Samplers will need to load x and fx, or y and fy.
3594 sk_unaligned_store(c->x, *r);
3595 sk_unaligned_store(c->y, *g);
3596 sk_unaligned_store(c->fx, fx);
3597 sk_unaligned_store(c->fy, fy);
3598 }
3599
STAGE(accumulate,const SkRasterPipeline_SamplerCtx * c)3600 STAGE(accumulate, const SkRasterPipeline_SamplerCtx* c) {
3601 // Bilinear and bicubic filters are both separable, so we produce independent contributions
3602 // from x and y, multiplying them together here to get each pixel's total scale factor.
3603 auto scale = sk_unaligned_load<F>(c->scalex)
3604 * sk_unaligned_load<F>(c->scaley);
3605 dr = mad(scale, r, dr);
3606 dg = mad(scale, g, dg);
3607 db = mad(scale, b, db);
3608 da = mad(scale, a, da);
3609 }
3610
3611 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
3612 // are combined in direct proportion to their area overlapping that logical query pixel.
3613 // At positive offsets, the x-axis contribution to that rectangle is fx, or (1-fx) at negative x.
3614 // The y-axis is symmetric.
3615
3616 template <int kScale>
bilinear_x(SkRasterPipeline_SamplerCtx * ctx,F * x)3617 SI void bilinear_x(SkRasterPipeline_SamplerCtx* ctx, F* x) {
3618 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
3619 F fx = sk_unaligned_load<F>(ctx->fx);
3620
3621 F scalex;
3622 if (kScale == -1) { scalex = 1.0f - fx; }
3623 if (kScale == +1) { scalex = fx; }
3624 sk_unaligned_store(ctx->scalex, scalex);
3625 }
3626 template <int kScale>
bilinear_y(SkRasterPipeline_SamplerCtx * ctx,F * y)3627 SI void bilinear_y(SkRasterPipeline_SamplerCtx* ctx, F* y) {
3628 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
3629 F fy = sk_unaligned_load<F>(ctx->fy);
3630
3631 F scaley;
3632 if (kScale == -1) { scaley = 1.0f - fy; }
3633 if (kScale == +1) { scaley = fy; }
3634 sk_unaligned_store(ctx->scaley, scaley);
3635 }
3636
STAGE(bilinear_setup,SkRasterPipeline_SamplerCtx * ctx)3637 STAGE(bilinear_setup, SkRasterPipeline_SamplerCtx* ctx) {
3638 save_xy(&r, &g, ctx);
3639 // Init for accumulate
3640 dr = dg = db = da = F0;
3641 }
3642
STAGE(bilinear_nx,SkRasterPipeline_SamplerCtx * ctx)3643 STAGE(bilinear_nx, SkRasterPipeline_SamplerCtx* ctx) { bilinear_x<-1>(ctx, &r); }
STAGE(bilinear_px,SkRasterPipeline_SamplerCtx * ctx)3644 STAGE(bilinear_px, SkRasterPipeline_SamplerCtx* ctx) { bilinear_x<+1>(ctx, &r); }
STAGE(bilinear_ny,SkRasterPipeline_SamplerCtx * ctx)3645 STAGE(bilinear_ny, SkRasterPipeline_SamplerCtx* ctx) { bilinear_y<-1>(ctx, &g); }
STAGE(bilinear_py,SkRasterPipeline_SamplerCtx * ctx)3646 STAGE(bilinear_py, SkRasterPipeline_SamplerCtx* ctx) { bilinear_y<+1>(ctx, &g); }
3647
3648
3649 // In bicubic interpolation, the 16 pixels and +/- 0.5 and +/- 1.5 offsets from the sample
3650 // pixel center are combined with a non-uniform cubic filter, with higher values near the center.
3651 //
3652 // This helper computes the total weight along one axis (our bicubic filter is separable), given one
3653 // column of the sampling matrix, and a fractional pixel offset. See SkCubicResampler for details.
3654
bicubic_wts(F t,float A,float B,float C,float D)3655 SI F bicubic_wts(F t, float A, float B, float C, float D) {
3656 return mad(t, mad(t, mad(t, D, C), B), A);
3657 }
3658
3659 template <int kScale>
bicubic_x(SkRasterPipeline_SamplerCtx * ctx,F * x)3660 SI void bicubic_x(SkRasterPipeline_SamplerCtx* ctx, F* x) {
3661 *x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
3662
3663 F scalex;
3664 if (kScale == -3) { scalex = sk_unaligned_load<F>(ctx->wx[0]); }
3665 if (kScale == -1) { scalex = sk_unaligned_load<F>(ctx->wx[1]); }
3666 if (kScale == +1) { scalex = sk_unaligned_load<F>(ctx->wx[2]); }
3667 if (kScale == +3) { scalex = sk_unaligned_load<F>(ctx->wx[3]); }
3668 sk_unaligned_store(ctx->scalex, scalex);
3669 }
3670 template <int kScale>
bicubic_y(SkRasterPipeline_SamplerCtx * ctx,F * y)3671 SI void bicubic_y(SkRasterPipeline_SamplerCtx* ctx, F* y) {
3672 *y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
3673
3674 F scaley;
3675 if (kScale == -3) { scaley = sk_unaligned_load<F>(ctx->wy[0]); }
3676 if (kScale == -1) { scaley = sk_unaligned_load<F>(ctx->wy[1]); }
3677 if (kScale == +1) { scaley = sk_unaligned_load<F>(ctx->wy[2]); }
3678 if (kScale == +3) { scaley = sk_unaligned_load<F>(ctx->wy[3]); }
3679 sk_unaligned_store(ctx->scaley, scaley);
3680 }
3681
STAGE(bicubic_setup,SkRasterPipeline_SamplerCtx * ctx)3682 STAGE(bicubic_setup, SkRasterPipeline_SamplerCtx* ctx) {
3683 save_xy(&r, &g, ctx);
3684
3685 const float* w = ctx->weights;
3686
3687 F fx = sk_unaligned_load<F>(ctx->fx);
3688 sk_unaligned_store(ctx->wx[0], bicubic_wts(fx, w[0], w[4], w[ 8], w[12]));
3689 sk_unaligned_store(ctx->wx[1], bicubic_wts(fx, w[1], w[5], w[ 9], w[13]));
3690 sk_unaligned_store(ctx->wx[2], bicubic_wts(fx, w[2], w[6], w[10], w[14]));
3691 sk_unaligned_store(ctx->wx[3], bicubic_wts(fx, w[3], w[7], w[11], w[15]));
3692
3693 F fy = sk_unaligned_load<F>(ctx->fy);
3694 sk_unaligned_store(ctx->wy[0], bicubic_wts(fy, w[0], w[4], w[ 8], w[12]));
3695 sk_unaligned_store(ctx->wy[1], bicubic_wts(fy, w[1], w[5], w[ 9], w[13]));
3696 sk_unaligned_store(ctx->wy[2], bicubic_wts(fy, w[2], w[6], w[10], w[14]));
3697 sk_unaligned_store(ctx->wy[3], bicubic_wts(fy, w[3], w[7], w[11], w[15]));
3698
3699 // Init for accumulate
3700 dr = dg = db = da = F0;
3701 }
3702
STAGE(bicubic_n3x,SkRasterPipeline_SamplerCtx * ctx)3703 STAGE(bicubic_n3x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<-3>(ctx, &r); }
STAGE(bicubic_n1x,SkRasterPipeline_SamplerCtx * ctx)3704 STAGE(bicubic_n1x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<-1>(ctx, &r); }
STAGE(bicubic_p1x,SkRasterPipeline_SamplerCtx * ctx)3705 STAGE(bicubic_p1x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<+1>(ctx, &r); }
STAGE(bicubic_p3x,SkRasterPipeline_SamplerCtx * ctx)3706 STAGE(bicubic_p3x, SkRasterPipeline_SamplerCtx* ctx) { bicubic_x<+3>(ctx, &r); }
3707
STAGE(bicubic_n3y,SkRasterPipeline_SamplerCtx * ctx)3708 STAGE(bicubic_n3y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<-3>(ctx, &g); }
STAGE(bicubic_n1y,SkRasterPipeline_SamplerCtx * ctx)3709 STAGE(bicubic_n1y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<-1>(ctx, &g); }
STAGE(bicubic_p1y,SkRasterPipeline_SamplerCtx * ctx)3710 STAGE(bicubic_p1y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<+1>(ctx, &g); }
STAGE(bicubic_p3y,SkRasterPipeline_SamplerCtx * ctx)3711 STAGE(bicubic_p3y, SkRasterPipeline_SamplerCtx* ctx) { bicubic_y<+3>(ctx, &g); }
3712
compute_perlin_vector(U32 sample,F x,F y)3713 SI F compute_perlin_vector(U32 sample, F x, F y) {
3714 // We're relying on the packing of uint16s within a uint32, which will vary based on endianness.
3715 #ifdef SK_CPU_BENDIAN
3716 U32 sampleLo = sample >> 16;
3717 U32 sampleHi = sample & 0xFFFF;
3718 #else
3719 U32 sampleLo = sample & 0xFFFF;
3720 U32 sampleHi = sample >> 16;
3721 #endif
3722
3723 // Convert 32-bit sample value into two floats in the [-1..1] range.
3724 F vecX = mad(cast(sampleLo), 2.0f / 65535.0f, -1.0f);
3725 F vecY = mad(cast(sampleHi), 2.0f / 65535.0f, -1.0f);
3726
3727 // Return the dot of the sample and the passed-in vector.
3728 return mad(vecX, x,
3729 vecY * y);
3730 }
3731
STAGE(perlin_noise,SkRasterPipeline_PerlinNoiseCtx * ctx)3732 STAGE(perlin_noise, SkRasterPipeline_PerlinNoiseCtx* ctx) {
3733 F noiseVecX = (r + 0.5) * ctx->baseFrequencyX;
3734 F noiseVecY = (g + 0.5) * ctx->baseFrequencyY;
3735 r = g = b = a = F0;
3736 F stitchDataX = F_(ctx->stitchDataInX);
3737 F stitchDataY = F_(ctx->stitchDataInY);
3738 F ratio = F1;
3739
3740 for (int octave = 0; octave < ctx->numOctaves; ++octave) {
3741 // Calculate noise coordinates. (Roughly $noise_helper in Graphite)
3742 F floorValX = floor_(noiseVecX);
3743 F floorValY = floor_(noiseVecY);
3744 F ceilValX = floorValX + 1.0f;
3745 F ceilValY = floorValY + 1.0f;
3746 F fractValX = noiseVecX - floorValX;
3747 F fractValY = noiseVecY - floorValY;
3748
3749 if (ctx->stitching) {
3750 // If we are stitching, wrap the coordinates to the stitch position.
3751 floorValX -= sk_bit_cast<F>(cond_to_mask(floorValX >= stitchDataX) &
3752 sk_bit_cast<I32>(stitchDataX));
3753 floorValY -= sk_bit_cast<F>(cond_to_mask(floorValY >= stitchDataY) &
3754 sk_bit_cast<I32>(stitchDataY));
3755 ceilValX -= sk_bit_cast<F>(cond_to_mask(ceilValX >= stitchDataX) &
3756 sk_bit_cast<I32>(stitchDataX));
3757 ceilValY -= sk_bit_cast<F>(cond_to_mask(ceilValY >= stitchDataY) &
3758 sk_bit_cast<I32>(stitchDataY));
3759 }
3760
3761 U32 latticeLookup = (U32)(iround(floorValX)) & 0xFF;
3762 F latticeIdxX = cast(expand(gather(ctx->latticeSelector, latticeLookup)));
3763 latticeLookup = (U32)(iround(ceilValX)) & 0xFF;
3764 F latticeIdxY = cast(expand(gather(ctx->latticeSelector, latticeLookup)));
3765
3766 U32 b00 = (U32)(iround(latticeIdxX + floorValY)) & 0xFF;
3767 U32 b10 = (U32)(iround(latticeIdxY + floorValY)) & 0xFF;
3768 U32 b01 = (U32)(iround(latticeIdxX + ceilValY)) & 0xFF;
3769 U32 b11 = (U32)(iround(latticeIdxY + ceilValY)) & 0xFF;
3770
3771 // Calculate noise colors. (Roughly $noise_function in Graphite)
3772 // Apply Hermite interpolation to the fractional value.
3773 F smoothX = fractValX * fractValX * (3.0f - 2.0f * fractValX);
3774 F smoothY = fractValY * fractValY * (3.0f - 2.0f * fractValY);
3775
3776 F color[4];
3777 const uint32_t* channelNoiseData = reinterpret_cast<const uint32_t*>(ctx->noiseData);
3778 for (int channel = 0; channel < 4; ++channel) {
3779 U32 sample00 = gather(channelNoiseData, b00);
3780 U32 sample10 = gather(channelNoiseData, b10);
3781 U32 sample01 = gather(channelNoiseData, b01);
3782 U32 sample11 = gather(channelNoiseData, b11);
3783 channelNoiseData += 256;
3784
3785 F u = compute_perlin_vector(sample00, fractValX, fractValY);
3786 F v = compute_perlin_vector(sample10, fractValX - 1.0f, fractValY);
3787 F A = lerp(u, v, smoothX);
3788
3789 u = compute_perlin_vector(sample01, fractValX, fractValY - 1.0f);
3790 v = compute_perlin_vector(sample11, fractValX - 1.0f, fractValY - 1.0f);
3791 F B = lerp(u, v, smoothX);
3792
3793 color[channel] = lerp(A, B, smoothY);
3794 }
3795
3796 if (ctx->noiseType != SkPerlinNoiseShaderType::kFractalNoise) {
3797 // For kTurbulence the result is: abs(noise[-1,1])
3798 color[0] = abs_(color[0]);
3799 color[1] = abs_(color[1]);
3800 color[2] = abs_(color[2]);
3801 color[3] = abs_(color[3]);
3802 }
3803
3804 r = mad(color[0], ratio, r);
3805 g = mad(color[1], ratio, g);
3806 b = mad(color[2], ratio, b);
3807 a = mad(color[3], ratio, a);
3808
3809 // Scale inputs for the next round.
3810 noiseVecX *= 2.0f;
3811 noiseVecY *= 2.0f;
3812 stitchDataX *= 2.0f;
3813 stitchDataY *= 2.0f;
3814 ratio *= 0.5f;
3815 }
3816
3817 if (ctx->noiseType == SkPerlinNoiseShaderType::kFractalNoise) {
3818 // For kFractalNoise the result is: noise[-1,1] * 0.5 + 0.5
3819 r = mad(r, 0.5f, 0.5f);
3820 g = mad(g, 0.5f, 0.5f);
3821 b = mad(b, 0.5f, 0.5f);
3822 a = mad(a, 0.5f, 0.5f);
3823 }
3824
3825 r = clamp_01_(r) * a;
3826 g = clamp_01_(g) * a;
3827 b = clamp_01_(b) * a;
3828 a = clamp_01_(a);
3829 }
3830
STAGE(mipmap_linear_init,SkRasterPipeline_MipmapCtx * ctx)3831 STAGE(mipmap_linear_init, SkRasterPipeline_MipmapCtx* ctx) {
3832 sk_unaligned_store(ctx->x, r);
3833 sk_unaligned_store(ctx->y, g);
3834 }
3835
STAGE(mipmap_linear_update,SkRasterPipeline_MipmapCtx * ctx)3836 STAGE(mipmap_linear_update, SkRasterPipeline_MipmapCtx* ctx) {
3837 sk_unaligned_store(ctx->r, r);
3838 sk_unaligned_store(ctx->g, g);
3839 sk_unaligned_store(ctx->b, b);
3840 sk_unaligned_store(ctx->a, a);
3841
3842 r = sk_unaligned_load<F>(ctx->x) * ctx->scaleX;
3843 g = sk_unaligned_load<F>(ctx->y) * ctx->scaleY;
3844 }
3845
STAGE(mipmap_linear_finish,SkRasterPipeline_MipmapCtx * ctx)3846 STAGE(mipmap_linear_finish, SkRasterPipeline_MipmapCtx* ctx) {
3847 r = lerp(sk_unaligned_load<F>(ctx->r), r, F_(ctx->lowerWeight));
3848 g = lerp(sk_unaligned_load<F>(ctx->g), g, F_(ctx->lowerWeight));
3849 b = lerp(sk_unaligned_load<F>(ctx->b), b, F_(ctx->lowerWeight));
3850 a = lerp(sk_unaligned_load<F>(ctx->a), a, F_(ctx->lowerWeight));
3851 }
3852
STAGE(callback,SkRasterPipeline_CallbackCtx * c)3853 STAGE(callback, SkRasterPipeline_CallbackCtx* c) {
3854 store4(c->rgba, r,g,b,a);
3855 c->fn(c, N);
3856 load4(c->read_from, &r,&g,&b,&a);
3857 }
3858
STAGE_TAIL(set_base_pointer,std::byte * p)3859 STAGE_TAIL(set_base_pointer, std::byte* p) {
3860 base = p;
3861 }
3862
3863 // All control flow stages used by SkSL maintain some state in the common registers:
3864 // r: condition mask
3865 // g: loop mask
3866 // b: return mask
3867 // a: execution mask (intersection of all three masks)
3868 // After updating r/g/b, you must invoke update_execution_mask().
3869 #define execution_mask() sk_bit_cast<I32>(a)
3870 #define update_execution_mask() a = sk_bit_cast<F>(sk_bit_cast<I32>(r) & \
3871 sk_bit_cast<I32>(g) & \
3872 sk_bit_cast<I32>(b))
3873
STAGE_TAIL(init_lane_masks,SkRasterPipeline_InitLaneMasksCtx * ctx)3874 STAGE_TAIL(init_lane_masks, SkRasterPipeline_InitLaneMasksCtx* ctx) {
3875 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
3876 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
3877
3878 I32 mask = cond_to_mask(sk_unaligned_load<U32>(iota) < *ctx->tail);
3879 r = g = b = a = sk_bit_cast<F>(mask);
3880 }
3881
STAGE_TAIL(store_device_xy01,F * dst)3882 STAGE_TAIL(store_device_xy01, F* dst) {
3883 // This is very similar to `seed_shader + store_src`, but b/a are backwards.
3884 // (sk_FragCoord actually puts w=1 in the w slot.)
3885 static constexpr float iota[] = {
3886 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
3887 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
3888 };
3889 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
3890
3891 dst[0] = cast(U32_(dx)) + sk_unaligned_load<F>(iota);
3892 dst[1] = cast(U32_(dy)) + 0.5f;
3893 dst[2] = F0;
3894 dst[3] = F1;
3895 }
3896
STAGE_TAIL(exchange_src,F * rgba)3897 STAGE_TAIL(exchange_src, F* rgba) {
3898 // Swaps r,g,b,a registers with the values at `rgba`.
3899 F temp[4] = {r, g, b, a};
3900 r = rgba[0];
3901 rgba[0] = temp[0];
3902 g = rgba[1];
3903 rgba[1] = temp[1];
3904 b = rgba[2];
3905 rgba[2] = temp[2];
3906 a = rgba[3];
3907 rgba[3] = temp[3];
3908 }
3909
STAGE_TAIL(load_condition_mask,F * ctx)3910 STAGE_TAIL(load_condition_mask, F* ctx) {
3911 r = sk_unaligned_load<F>(ctx);
3912 update_execution_mask();
3913 }
3914
STAGE_TAIL(store_condition_mask,F * ctx)3915 STAGE_TAIL(store_condition_mask, F* ctx) {
3916 sk_unaligned_store(ctx, r);
3917 }
3918
STAGE_TAIL(merge_condition_mask,I32 * ptr)3919 STAGE_TAIL(merge_condition_mask, I32* ptr) {
3920 // Set the condition-mask to the intersection of two adjacent masks at the pointer.
3921 r = sk_bit_cast<F>(ptr[0] & ptr[1]);
3922 update_execution_mask();
3923 }
3924
STAGE_TAIL(merge_inv_condition_mask,I32 * ptr)3925 STAGE_TAIL(merge_inv_condition_mask, I32* ptr) {
3926 // Set the condition-mask to the intersection of the first mask and the inverse of the second.
3927 r = sk_bit_cast<F>(ptr[0] & ~ptr[1]);
3928 update_execution_mask();
3929 }
3930
STAGE_TAIL(load_loop_mask,F * ctx)3931 STAGE_TAIL(load_loop_mask, F* ctx) {
3932 g = sk_unaligned_load<F>(ctx);
3933 update_execution_mask();
3934 }
3935
STAGE_TAIL(store_loop_mask,F * ctx)3936 STAGE_TAIL(store_loop_mask, F* ctx) {
3937 sk_unaligned_store(ctx, g);
3938 }
3939
STAGE_TAIL(mask_off_loop_mask,NoCtx)3940 STAGE_TAIL(mask_off_loop_mask, NoCtx) {
3941 // We encountered a break statement. If a lane was active, it should be masked off now, and stay
3942 // masked-off until the termination of the loop.
3943 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
3944 update_execution_mask();
3945 }
3946
STAGE_TAIL(reenable_loop_mask,I32 * ptr)3947 STAGE_TAIL(reenable_loop_mask, I32* ptr) {
3948 // Set the loop-mask to the union of the current loop-mask with the mask at the pointer.
3949 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | ptr[0]);
3950 update_execution_mask();
3951 }
3952
STAGE_TAIL(merge_loop_mask,I32 * ptr)3953 STAGE_TAIL(merge_loop_mask, I32* ptr) {
3954 // Set the loop-mask to the intersection of the current loop-mask with the mask at the pointer.
3955 // (Note: this behavior subtly differs from merge_condition_mask!)
3956 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ptr[0]);
3957 update_execution_mask();
3958 }
3959
STAGE_TAIL(continue_op,I32 * continueMask)3960 STAGE_TAIL(continue_op, I32* continueMask) {
3961 // Set any currently-executing lanes in the continue-mask to true.
3962 *continueMask |= execution_mask();
3963
3964 // Disable any currently-executing lanes from the loop mask. (Just like `mask_off_loop_mask`.)
3965 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
3966 update_execution_mask();
3967 }
3968
STAGE_TAIL(case_op,SkRasterPipeline_CaseOpCtx * packed)3969 STAGE_TAIL(case_op, SkRasterPipeline_CaseOpCtx* packed) {
3970 auto ctx = SkRPCtxUtils::Unpack(packed);
3971
3972 // Check each lane to see if the case value matches the expectation.
3973 I32* actualValue = (I32*)(base + ctx.offset);
3974 I32 caseMatches = cond_to_mask(*actualValue == ctx.expectedValue);
3975
3976 // In lanes where we found a match, enable the loop mask...
3977 g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | caseMatches);
3978 update_execution_mask();
3979
3980 // ... and clear the default-case mask.
3981 I32* defaultMask = actualValue + 1;
3982 *defaultMask &= ~caseMatches;
3983 }
3984
STAGE_TAIL(load_return_mask,F * ctx)3985 STAGE_TAIL(load_return_mask, F* ctx) {
3986 b = sk_unaligned_load<F>(ctx);
3987 update_execution_mask();
3988 }
3989
STAGE_TAIL(store_return_mask,F * ctx)3990 STAGE_TAIL(store_return_mask, F* ctx) {
3991 sk_unaligned_store(ctx, b);
3992 }
3993
STAGE_TAIL(mask_off_return_mask,NoCtx)3994 STAGE_TAIL(mask_off_return_mask, NoCtx) {
3995 // We encountered a return statement. If a lane was active, it should be masked off now, and
3996 // stay masked-off until the end of the function.
3997 b = sk_bit_cast<F>(sk_bit_cast<I32>(b) & ~execution_mask());
3998 update_execution_mask();
3999 }
4000
STAGE_BRANCH(branch_if_all_lanes_active,SkRasterPipeline_BranchIfAllLanesActiveCtx * ctx)4001 STAGE_BRANCH(branch_if_all_lanes_active, SkRasterPipeline_BranchIfAllLanesActiveCtx* ctx) {
4002 uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
4003 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
4004
4005 I32 tailLanes = cond_to_mask(*ctx->tail <= sk_unaligned_load<U32>(iota));
4006 return all(execution_mask() | tailLanes) ? ctx->offset : 1;
4007 }
4008
STAGE_BRANCH(branch_if_any_lanes_active,SkRasterPipeline_BranchCtx * ctx)4009 STAGE_BRANCH(branch_if_any_lanes_active, SkRasterPipeline_BranchCtx* ctx) {
4010 return any(execution_mask()) ? ctx->offset : 1;
4011 }
4012
STAGE_BRANCH(branch_if_no_lanes_active,SkRasterPipeline_BranchCtx * ctx)4013 STAGE_BRANCH(branch_if_no_lanes_active, SkRasterPipeline_BranchCtx* ctx) {
4014 return any(execution_mask()) ? 1 : ctx->offset;
4015 }
4016
STAGE_BRANCH(jump,SkRasterPipeline_BranchCtx * ctx)4017 STAGE_BRANCH(jump, SkRasterPipeline_BranchCtx* ctx) {
4018 return ctx->offset;
4019 }
4020
STAGE_BRANCH(branch_if_no_active_lanes_eq,SkRasterPipeline_BranchIfEqualCtx * ctx)4021 STAGE_BRANCH(branch_if_no_active_lanes_eq, SkRasterPipeline_BranchIfEqualCtx* ctx) {
4022 // Compare each lane against the expected value...
4023 I32 match = cond_to_mask(*(const I32*)ctx->ptr == ctx->value);
4024 // ... but mask off lanes that aren't executing.
4025 match &= execution_mask();
4026 // If any lanes matched, don't take the branch.
4027 return any(match) ? 1 : ctx->offset;
4028 }
4029
STAGE_TAIL(trace_line,SkRasterPipeline_TraceLineCtx * ctx)4030 STAGE_TAIL(trace_line, SkRasterPipeline_TraceLineCtx* ctx) {
4031 const I32* traceMask = (const I32*)ctx->traceMask;
4032 if (any(execution_mask() & *traceMask)) {
4033 ctx->traceHook->line(ctx->lineNumber);
4034 }
4035 }
4036
STAGE_TAIL(trace_enter,SkRasterPipeline_TraceFuncCtx * ctx)4037 STAGE_TAIL(trace_enter, SkRasterPipeline_TraceFuncCtx* ctx) {
4038 const I32* traceMask = (const I32*)ctx->traceMask;
4039 if (any(execution_mask() & *traceMask)) {
4040 ctx->traceHook->enter(ctx->funcIdx);
4041 }
4042 }
4043
STAGE_TAIL(trace_exit,SkRasterPipeline_TraceFuncCtx * ctx)4044 STAGE_TAIL(trace_exit, SkRasterPipeline_TraceFuncCtx* ctx) {
4045 const I32* traceMask = (const I32*)ctx->traceMask;
4046 if (any(execution_mask() & *traceMask)) {
4047 ctx->traceHook->exit(ctx->funcIdx);
4048 }
4049 }
4050
STAGE_TAIL(trace_scope,SkRasterPipeline_TraceScopeCtx * ctx)4051 STAGE_TAIL(trace_scope, SkRasterPipeline_TraceScopeCtx* ctx) {
4052 // Note that trace_scope intentionally does not incorporate the execution mask. Otherwise, the
4053 // scopes would become unbalanced if the execution mask changed in the middle of a block. The
4054 // caller is responsible for providing a combined trace- and execution-mask.
4055 const I32* traceMask = (const I32*)ctx->traceMask;
4056 if (any(*traceMask)) {
4057 ctx->traceHook->scope(ctx->delta);
4058 }
4059 }
4060
STAGE_TAIL(trace_var,SkRasterPipeline_TraceVarCtx * ctx)4061 STAGE_TAIL(trace_var, SkRasterPipeline_TraceVarCtx* ctx) {
4062 const I32* traceMask = (const I32*)ctx->traceMask;
4063 I32 mask = execution_mask() & *traceMask;
4064 if (any(mask)) {
4065 for (size_t lane = 0; lane < N; ++lane) {
4066 if (select_lane(mask, lane)) {
4067 const I32* data = (const I32*)ctx->data;
4068 int slotIdx = ctx->slotIdx, numSlots = ctx->numSlots;
4069 if (ctx->indirectOffset) {
4070 // If this was an indirect store, apply the indirect-offset to the data pointer.
4071 uint32_t indirectOffset = select_lane(*(const U32*)ctx->indirectOffset, lane);
4072 indirectOffset = std::min<uint32_t>(indirectOffset, ctx->indirectLimit);
4073 data += indirectOffset;
4074 slotIdx += indirectOffset;
4075 }
4076 while (numSlots--) {
4077 ctx->traceHook->var(slotIdx, select_lane(*data, lane));
4078 ++slotIdx;
4079 ++data;
4080 }
4081 break;
4082 }
4083 }
4084 }
4085 }
4086
STAGE_TAIL(copy_uniform,SkRasterPipeline_UniformCtx * ctx)4087 STAGE_TAIL(copy_uniform, SkRasterPipeline_UniformCtx* ctx) {
4088 const int* src = ctx->src;
4089 I32* dst = (I32*)ctx->dst;
4090 dst[0] = I32_(src[0]);
4091 }
STAGE_TAIL(copy_2_uniforms,SkRasterPipeline_UniformCtx * ctx)4092 STAGE_TAIL(copy_2_uniforms, SkRasterPipeline_UniformCtx* ctx) {
4093 const int* src = ctx->src;
4094 I32* dst = (I32*)ctx->dst;
4095 dst[0] = I32_(src[0]);
4096 dst[1] = I32_(src[1]);
4097 }
STAGE_TAIL(copy_3_uniforms,SkRasterPipeline_UniformCtx * ctx)4098 STAGE_TAIL(copy_3_uniforms, SkRasterPipeline_UniformCtx* ctx) {
4099 const int* src = ctx->src;
4100 I32* dst = (I32*)ctx->dst;
4101 dst[0] = I32_(src[0]);
4102 dst[1] = I32_(src[1]);
4103 dst[2] = I32_(src[2]);
4104 }
STAGE_TAIL(copy_4_uniforms,SkRasterPipeline_UniformCtx * ctx)4105 STAGE_TAIL(copy_4_uniforms, SkRasterPipeline_UniformCtx* ctx) {
4106 const int* src = ctx->src;
4107 I32* dst = (I32*)ctx->dst;
4108 dst[0] = I32_(src[0]);
4109 dst[1] = I32_(src[1]);
4110 dst[2] = I32_(src[2]);
4111 dst[3] = I32_(src[3]);
4112 }
4113
STAGE_TAIL(copy_constant,SkRasterPipeline_ConstantCtx * packed)4114 STAGE_TAIL(copy_constant, SkRasterPipeline_ConstantCtx* packed) {
4115 auto ctx = SkRPCtxUtils::Unpack(packed);
4116 I32* dst = (I32*)(base + ctx.dst);
4117 I32 value = I32_(ctx.value);
4118 dst[0] = value;
4119 }
STAGE_TAIL(splat_2_constants,SkRasterPipeline_ConstantCtx * packed)4120 STAGE_TAIL(splat_2_constants, SkRasterPipeline_ConstantCtx* packed) {
4121 auto ctx = SkRPCtxUtils::Unpack(packed);
4122 I32* dst = (I32*)(base + ctx.dst);
4123 I32 value = I32_(ctx.value);
4124 dst[0] = dst[1] = value;
4125 }
STAGE_TAIL(splat_3_constants,SkRasterPipeline_ConstantCtx * packed)4126 STAGE_TAIL(splat_3_constants, SkRasterPipeline_ConstantCtx* packed) {
4127 auto ctx = SkRPCtxUtils::Unpack(packed);
4128 I32* dst = (I32*)(base + ctx.dst);
4129 I32 value = I32_(ctx.value);
4130 dst[0] = dst[1] = dst[2] = value;
4131 }
STAGE_TAIL(splat_4_constants,SkRasterPipeline_ConstantCtx * packed)4132 STAGE_TAIL(splat_4_constants, SkRasterPipeline_ConstantCtx* packed) {
4133 auto ctx = SkRPCtxUtils::Unpack(packed);
4134 I32* dst = (I32*)(base + ctx.dst);
4135 I32 value = I32_(ctx.value);
4136 dst[0] = dst[1] = dst[2] = dst[3] = value;
4137 }
4138
4139 template <int NumSlots>
copy_n_slots_unmasked_fn(SkRasterPipeline_BinaryOpCtx * packed,std::byte * base)4140 SI void copy_n_slots_unmasked_fn(SkRasterPipeline_BinaryOpCtx* packed, std::byte* base) {
4141 auto ctx = SkRPCtxUtils::Unpack(packed);
4142 F* dst = (F*)(base + ctx.dst);
4143 F* src = (F*)(base + ctx.src);
4144 memcpy(dst, src, sizeof(F) * NumSlots);
4145 }
4146
STAGE_TAIL(copy_slot_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4147 STAGE_TAIL(copy_slot_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4148 copy_n_slots_unmasked_fn<1>(packed, base);
4149 }
STAGE_TAIL(copy_2_slots_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4150 STAGE_TAIL(copy_2_slots_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4151 copy_n_slots_unmasked_fn<2>(packed, base);
4152 }
STAGE_TAIL(copy_3_slots_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4153 STAGE_TAIL(copy_3_slots_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4154 copy_n_slots_unmasked_fn<3>(packed, base);
4155 }
STAGE_TAIL(copy_4_slots_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4156 STAGE_TAIL(copy_4_slots_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4157 copy_n_slots_unmasked_fn<4>(packed, base);
4158 }
4159
4160 template <int NumSlots>
copy_n_immutable_unmasked_fn(SkRasterPipeline_BinaryOpCtx * packed,std::byte * base)4161 SI void copy_n_immutable_unmasked_fn(SkRasterPipeline_BinaryOpCtx* packed, std::byte* base) {
4162 auto ctx = SkRPCtxUtils::Unpack(packed);
4163
4164 // Load the scalar values.
4165 float* src = (float*)(base + ctx.src);
4166 float values[NumSlots];
4167 SK_UNROLL for (int index = 0; index < NumSlots; ++index) {
4168 values[index] = src[index];
4169 }
4170 // Broadcast the scalars into the destination.
4171 F* dst = (F*)(base + ctx.dst);
4172 SK_UNROLL for (int index = 0; index < NumSlots; ++index) {
4173 dst[index] = F_(values[index]);
4174 }
4175 }
4176
STAGE_TAIL(copy_immutable_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4177 STAGE_TAIL(copy_immutable_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4178 copy_n_immutable_unmasked_fn<1>(packed, base);
4179 }
STAGE_TAIL(copy_2_immutables_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4180 STAGE_TAIL(copy_2_immutables_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4181 copy_n_immutable_unmasked_fn<2>(packed, base);
4182 }
STAGE_TAIL(copy_3_immutables_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4183 STAGE_TAIL(copy_3_immutables_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4184 copy_n_immutable_unmasked_fn<3>(packed, base);
4185 }
STAGE_TAIL(copy_4_immutables_unmasked,SkRasterPipeline_BinaryOpCtx * packed)4186 STAGE_TAIL(copy_4_immutables_unmasked, SkRasterPipeline_BinaryOpCtx* packed) {
4187 copy_n_immutable_unmasked_fn<4>(packed, base);
4188 }
4189
4190 template <int NumSlots>
copy_n_slots_masked_fn(SkRasterPipeline_BinaryOpCtx * packed,std::byte * base,I32 mask)4191 SI void copy_n_slots_masked_fn(SkRasterPipeline_BinaryOpCtx* packed, std::byte* base, I32 mask) {
4192 auto ctx = SkRPCtxUtils::Unpack(packed);
4193 I32* dst = (I32*)(base + ctx.dst);
4194 I32* src = (I32*)(base + ctx.src);
4195 SK_UNROLL for (int count = 0; count < NumSlots; ++count) {
4196 *dst = if_then_else(mask, *src, *dst);
4197 dst += 1;
4198 src += 1;
4199 }
4200 }
4201
STAGE_TAIL(copy_slot_masked,SkRasterPipeline_BinaryOpCtx * packed)4202 STAGE_TAIL(copy_slot_masked, SkRasterPipeline_BinaryOpCtx* packed) {
4203 copy_n_slots_masked_fn<1>(packed, base, execution_mask());
4204 }
STAGE_TAIL(copy_2_slots_masked,SkRasterPipeline_BinaryOpCtx * packed)4205 STAGE_TAIL(copy_2_slots_masked, SkRasterPipeline_BinaryOpCtx* packed) {
4206 copy_n_slots_masked_fn<2>(packed, base, execution_mask());
4207 }
STAGE_TAIL(copy_3_slots_masked,SkRasterPipeline_BinaryOpCtx * packed)4208 STAGE_TAIL(copy_3_slots_masked, SkRasterPipeline_BinaryOpCtx* packed) {
4209 copy_n_slots_masked_fn<3>(packed, base, execution_mask());
4210 }
STAGE_TAIL(copy_4_slots_masked,SkRasterPipeline_BinaryOpCtx * packed)4211 STAGE_TAIL(copy_4_slots_masked, SkRasterPipeline_BinaryOpCtx* packed) {
4212 copy_n_slots_masked_fn<4>(packed, base, execution_mask());
4213 }
4214
4215 template <int LoopCount, typename OffsetType>
shuffle_fn(std::byte * ptr,OffsetType * offsets,int numSlots)4216 SI void shuffle_fn(std::byte* ptr, OffsetType* offsets, int numSlots) {
4217 F scratch[16];
4218 SK_UNROLL for (int count = 0; count < LoopCount; ++count) {
4219 scratch[count] = *(F*)(ptr + offsets[count]);
4220 }
4221 // Surprisingly, this switch generates significantly better code than a memcpy (on x86-64) when
4222 // the number of slots is unknown at compile time, and generates roughly identical code when the
4223 // number of slots is hardcoded. Using a switch allows `scratch` to live in ymm0-ymm15 instead
4224 // of being written out to the stack and then read back in. Also, the intrinsic memcpy assumes
4225 // that `numSlots` could be arbitrarily large, and so it emits more code than we need.
4226 F* dst = (F*)ptr;
4227 switch (numSlots) {
4228 case 16: dst[15] = scratch[15]; [[fallthrough]];
4229 case 15: dst[14] = scratch[14]; [[fallthrough]];
4230 case 14: dst[13] = scratch[13]; [[fallthrough]];
4231 case 13: dst[12] = scratch[12]; [[fallthrough]];
4232 case 12: dst[11] = scratch[11]; [[fallthrough]];
4233 case 11: dst[10] = scratch[10]; [[fallthrough]];
4234 case 10: dst[ 9] = scratch[ 9]; [[fallthrough]];
4235 case 9: dst[ 8] = scratch[ 8]; [[fallthrough]];
4236 case 8: dst[ 7] = scratch[ 7]; [[fallthrough]];
4237 case 7: dst[ 6] = scratch[ 6]; [[fallthrough]];
4238 case 6: dst[ 5] = scratch[ 5]; [[fallthrough]];
4239 case 5: dst[ 4] = scratch[ 4]; [[fallthrough]];
4240 case 4: dst[ 3] = scratch[ 3]; [[fallthrough]];
4241 case 3: dst[ 2] = scratch[ 2]; [[fallthrough]];
4242 case 2: dst[ 1] = scratch[ 1]; [[fallthrough]];
4243 case 1: dst[ 0] = scratch[ 0];
4244 }
4245 }
4246
4247 template <int N>
small_swizzle_fn(SkRasterPipeline_SwizzleCtx * packed,std::byte * base)4248 SI void small_swizzle_fn(SkRasterPipeline_SwizzleCtx* packed, std::byte* base) {
4249 auto ctx = SkRPCtxUtils::Unpack(packed);
4250 shuffle_fn<N>(base + ctx.dst, ctx.offsets, N);
4251 }
4252
STAGE_TAIL(swizzle_1,SkRasterPipeline_SwizzleCtx * packed)4253 STAGE_TAIL(swizzle_1, SkRasterPipeline_SwizzleCtx* packed) {
4254 small_swizzle_fn<1>(packed, base);
4255 }
STAGE_TAIL(swizzle_2,SkRasterPipeline_SwizzleCtx * packed)4256 STAGE_TAIL(swizzle_2, SkRasterPipeline_SwizzleCtx* packed) {
4257 small_swizzle_fn<2>(packed, base);
4258 }
STAGE_TAIL(swizzle_3,SkRasterPipeline_SwizzleCtx * packed)4259 STAGE_TAIL(swizzle_3, SkRasterPipeline_SwizzleCtx* packed) {
4260 small_swizzle_fn<3>(packed, base);
4261 }
STAGE_TAIL(swizzle_4,SkRasterPipeline_SwizzleCtx * packed)4262 STAGE_TAIL(swizzle_4, SkRasterPipeline_SwizzleCtx* packed) {
4263 small_swizzle_fn<4>(packed, base);
4264 }
STAGE_TAIL(shuffle,SkRasterPipeline_ShuffleCtx * ctx)4265 STAGE_TAIL(shuffle, SkRasterPipeline_ShuffleCtx* ctx) {
4266 shuffle_fn<16>((std::byte*)ctx->ptr, ctx->offsets, ctx->count);
4267 }
4268
4269 template <int NumSlots>
swizzle_copy_masked_fn(I32 * dst,const I32 * src,uint16_t * offsets,I32 mask)4270 SI void swizzle_copy_masked_fn(I32* dst, const I32* src, uint16_t* offsets, I32 mask) {
4271 std::byte* dstB = (std::byte*)dst;
4272 SK_UNROLL for (int count = 0; count < NumSlots; ++count) {
4273 I32* dstS = (I32*)(dstB + *offsets);
4274 *dstS = if_then_else(mask, *src, *dstS);
4275 offsets += 1;
4276 src += 1;
4277 }
4278 }
4279
STAGE_TAIL(swizzle_copy_slot_masked,SkRasterPipeline_SwizzleCopyCtx * ctx)4280 STAGE_TAIL(swizzle_copy_slot_masked, SkRasterPipeline_SwizzleCopyCtx* ctx) {
4281 swizzle_copy_masked_fn<1>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4282 }
STAGE_TAIL(swizzle_copy_2_slots_masked,SkRasterPipeline_SwizzleCopyCtx * ctx)4283 STAGE_TAIL(swizzle_copy_2_slots_masked, SkRasterPipeline_SwizzleCopyCtx* ctx) {
4284 swizzle_copy_masked_fn<2>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4285 }
STAGE_TAIL(swizzle_copy_3_slots_masked,SkRasterPipeline_SwizzleCopyCtx * ctx)4286 STAGE_TAIL(swizzle_copy_3_slots_masked, SkRasterPipeline_SwizzleCopyCtx* ctx) {
4287 swizzle_copy_masked_fn<3>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4288 }
STAGE_TAIL(swizzle_copy_4_slots_masked,SkRasterPipeline_SwizzleCopyCtx * ctx)4289 STAGE_TAIL(swizzle_copy_4_slots_masked, SkRasterPipeline_SwizzleCopyCtx* ctx) {
4290 swizzle_copy_masked_fn<4>((I32*)ctx->dst, (const I32*)ctx->src, ctx->offsets, execution_mask());
4291 }
4292
STAGE_TAIL(copy_from_indirect_unmasked,SkRasterPipeline_CopyIndirectCtx * ctx)4293 STAGE_TAIL(copy_from_indirect_unmasked, SkRasterPipeline_CopyIndirectCtx* ctx) {
4294 // Clamp the indirect offsets to stay within the limit.
4295 U32 offsets = *(const U32*)ctx->indirectOffset;
4296 offsets = min(offsets, U32_(ctx->indirectLimit));
4297
4298 // Scale up the offsets to account for the N lanes per value.
4299 offsets *= N;
4300
4301 // Adjust the offsets forward so that they fetch from the correct lane.
4302 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4303 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
4304 offsets += sk_unaligned_load<U32>(iota);
4305
4306 // Use gather to perform indirect lookups; write the results into `dst`.
4307 const int* src = ctx->src;
4308 I32* dst = (I32*)ctx->dst;
4309 I32* end = dst + ctx->slots;
4310 do {
4311 *dst = gather(src, offsets);
4312 dst += 1;
4313 src += N;
4314 } while (dst != end);
4315 }
4316
STAGE_TAIL(copy_from_indirect_uniform_unmasked,SkRasterPipeline_CopyIndirectCtx * ctx)4317 STAGE_TAIL(copy_from_indirect_uniform_unmasked, SkRasterPipeline_CopyIndirectCtx* ctx) {
4318 // Clamp the indirect offsets to stay within the limit.
4319 U32 offsets = *(const U32*)ctx->indirectOffset;
4320 offsets = min(offsets, U32_(ctx->indirectLimit));
4321
4322 // Use gather to perform indirect lookups; write the results into `dst`.
4323 const int* src = ctx->src;
4324 I32* dst = (I32*)ctx->dst;
4325 I32* end = dst + ctx->slots;
4326 do {
4327 *dst = gather(src, offsets);
4328 dst += 1;
4329 src += 1;
4330 } while (dst != end);
4331 }
4332
STAGE_TAIL(copy_to_indirect_masked,SkRasterPipeline_CopyIndirectCtx * ctx)4333 STAGE_TAIL(copy_to_indirect_masked, SkRasterPipeline_CopyIndirectCtx* ctx) {
4334 // Clamp the indirect offsets to stay within the limit.
4335 U32 offsets = *(const U32*)ctx->indirectOffset;
4336 offsets = min(offsets, U32_(ctx->indirectLimit));
4337
4338 // Scale up the offsets to account for the N lanes per value.
4339 offsets *= N;
4340
4341 // Adjust the offsets forward so that they store into the correct lane.
4342 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4343 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
4344 offsets += sk_unaligned_load<U32>(iota);
4345
4346 // Perform indirect, masked writes into `dst`.
4347 const I32* src = (const I32*)ctx->src;
4348 const I32* end = src + ctx->slots;
4349 int* dst = ctx->dst;
4350 I32 mask = execution_mask();
4351 do {
4352 scatter_masked(*src, dst, offsets, mask);
4353 dst += N;
4354 src += 1;
4355 } while (src != end);
4356 }
4357
STAGE_TAIL(swizzle_copy_to_indirect_masked,SkRasterPipeline_SwizzleCopyIndirectCtx * ctx)4358 STAGE_TAIL(swizzle_copy_to_indirect_masked, SkRasterPipeline_SwizzleCopyIndirectCtx* ctx) {
4359 // Clamp the indirect offsets to stay within the limit.
4360 U32 offsets = *(const U32*)ctx->indirectOffset;
4361 offsets = min(offsets, U32_(ctx->indirectLimit));
4362
4363 // Scale up the offsets to account for the N lanes per value.
4364 offsets *= N;
4365
4366 // Adjust the offsets forward so that they store into the correct lane.
4367 static constexpr uint32_t iota[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
4368 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride_highp);
4369 offsets += sk_unaligned_load<U32>(iota);
4370
4371 // Perform indirect, masked, swizzled writes into `dst`.
4372 const I32* src = (const I32*)ctx->src;
4373 const I32* end = src + ctx->slots;
4374 std::byte* dstB = (std::byte*)ctx->dst;
4375 const uint16_t* swizzle = ctx->offsets;
4376 I32 mask = execution_mask();
4377 do {
4378 int* dst = (int*)(dstB + *swizzle);
4379 scatter_masked(*src, dst, offsets, mask);
4380 swizzle += 1;
4381 src += 1;
4382 } while (src != end);
4383 }
4384
4385 // Unary operations take a single input, and overwrite it with their output.
4386 // Unlike binary or ternary operations, we provide variations of 1-4 slots, but don't provide
4387 // an arbitrary-width "n-slot" variation; the Builder can chain together longer sequences manually.
4388 template <typename T, void (*ApplyFn)(T*)>
apply_adjacent_unary(T * dst,T * end)4389 SI void apply_adjacent_unary(T* dst, T* end) {
4390 do {
4391 ApplyFn(dst);
4392 dst += 1;
4393 } while (dst != end);
4394 }
4395
4396 #if defined(SKRP_CPU_SCALAR)
4397 template <typename T>
cast_to_float_from_fn(T * dst)4398 SI void cast_to_float_from_fn(T* dst) {
4399 *dst = sk_bit_cast<T>((F)*dst);
4400 }
cast_to_int_from_fn(F * dst)4401 SI void cast_to_int_from_fn(F* dst) {
4402 *dst = sk_bit_cast<F>((I32)*dst);
4403 }
cast_to_uint_from_fn(F * dst)4404 SI void cast_to_uint_from_fn(F* dst) {
4405 *dst = sk_bit_cast<F>((U32)*dst);
4406 }
4407 #else
4408 template <typename T>
cast_to_float_from_fn(T * dst)4409 SI void cast_to_float_from_fn(T* dst) {
4410 *dst = sk_bit_cast<T>(__builtin_convertvector(*dst, F));
4411 }
cast_to_int_from_fn(F * dst)4412 SI void cast_to_int_from_fn(F* dst) {
4413 *dst = sk_bit_cast<F>(__builtin_convertvector(*dst, I32));
4414 }
cast_to_uint_from_fn(F * dst)4415 SI void cast_to_uint_from_fn(F* dst) {
4416 *dst = sk_bit_cast<F>(__builtin_convertvector(*dst, U32));
4417 }
4418 #endif
4419
abs_fn(I32 * dst)4420 SI void abs_fn(I32* dst) {
4421 *dst = abs_(*dst);
4422 }
4423
floor_fn(F * dst)4424 SI void floor_fn(F* dst) {
4425 *dst = floor_(*dst);
4426 }
4427
ceil_fn(F * dst)4428 SI void ceil_fn(F* dst) {
4429 *dst = ceil_(*dst);
4430 }
4431
invsqrt_fn(F * dst)4432 SI void invsqrt_fn(F* dst) {
4433 *dst = rsqrt(*dst);
4434 }
4435
4436 #define DECLARE_UNARY_FLOAT(name) \
4437 STAGE_TAIL(name##_float, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 1); } \
4438 STAGE_TAIL(name##_2_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 2); } \
4439 STAGE_TAIL(name##_3_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 3); } \
4440 STAGE_TAIL(name##_4_floats, F* dst) { apply_adjacent_unary<F, &name##_fn>(dst, dst + 4); }
4441
4442 #define DECLARE_UNARY_INT(name) \
4443 STAGE_TAIL(name##_int, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 1); } \
4444 STAGE_TAIL(name##_2_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 2); } \
4445 STAGE_TAIL(name##_3_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 3); } \
4446 STAGE_TAIL(name##_4_ints, I32* dst) { apply_adjacent_unary<I32, &name##_fn>(dst, dst + 4); }
4447
4448 #define DECLARE_UNARY_UINT(name) \
4449 STAGE_TAIL(name##_uint, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 1); } \
4450 STAGE_TAIL(name##_2_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 2); } \
4451 STAGE_TAIL(name##_3_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 3); } \
4452 STAGE_TAIL(name##_4_uints, U32* dst) { apply_adjacent_unary<U32, &name##_fn>(dst, dst + 4); }
4453
DECLARE_UNARY_UINT(cast_to_float_from)4454 DECLARE_UNARY_INT(cast_to_float_from) DECLARE_UNARY_UINT(cast_to_float_from)
4455 DECLARE_UNARY_FLOAT(cast_to_int_from)
4456 DECLARE_UNARY_FLOAT(cast_to_uint_from)
4457 DECLARE_UNARY_FLOAT(floor)
4458 DECLARE_UNARY_FLOAT(ceil)
4459 DECLARE_UNARY_FLOAT(invsqrt)
4460 DECLARE_UNARY_INT(abs)
4461
4462 #undef DECLARE_UNARY_FLOAT
4463 #undef DECLARE_UNARY_INT
4464 #undef DECLARE_UNARY_UINT
4465
4466 // For complex unary ops, we only provide a 1-slot version to reduce code bloat.
4467 STAGE_TAIL(sin_float, F* dst) { *dst = sin_(*dst); }
STAGE_TAIL(cos_float,F * dst)4468 STAGE_TAIL(cos_float, F* dst) { *dst = cos_(*dst); }
STAGE_TAIL(tan_float,F * dst)4469 STAGE_TAIL(tan_float, F* dst) { *dst = tan_(*dst); }
STAGE_TAIL(asin_float,F * dst)4470 STAGE_TAIL(asin_float, F* dst) { *dst = asin_(*dst); }
STAGE_TAIL(acos_float,F * dst)4471 STAGE_TAIL(acos_float, F* dst) { *dst = acos_(*dst); }
STAGE_TAIL(atan_float,F * dst)4472 STAGE_TAIL(atan_float, F* dst) { *dst = atan_(*dst); }
STAGE_TAIL(sqrt_float,F * dst)4473 STAGE_TAIL(sqrt_float, F* dst) { *dst = sqrt_(*dst); }
STAGE_TAIL(exp_float,F * dst)4474 STAGE_TAIL(exp_float, F* dst) { *dst = approx_exp(*dst); }
STAGE_TAIL(exp2_float,F * dst)4475 STAGE_TAIL(exp2_float, F* dst) { *dst = approx_pow2(*dst); }
STAGE_TAIL(log_float,F * dst)4476 STAGE_TAIL(log_float, F* dst) { *dst = approx_log(*dst); }
STAGE_TAIL(log2_float,F * dst)4477 STAGE_TAIL(log2_float, F* dst) { *dst = approx_log2(*dst); }
4478
STAGE_TAIL(inverse_mat2,F * dst)4479 STAGE_TAIL(inverse_mat2, F* dst) {
4480 F a00 = dst[0], a01 = dst[1],
4481 a10 = dst[2], a11 = dst[3];
4482 F det = nmad(a01, a10, a00 * a11),
4483 invdet = rcp_precise(det);
4484 dst[0] = invdet * a11;
4485 dst[1] = -invdet * a01;
4486 dst[2] = -invdet * a10;
4487 dst[3] = invdet * a00;
4488 }
4489
STAGE_TAIL(inverse_mat3,F * dst)4490 STAGE_TAIL(inverse_mat3, F* dst) {
4491 F a00 = dst[0], a01 = dst[1], a02 = dst[2],
4492 a10 = dst[3], a11 = dst[4], a12 = dst[5],
4493 a20 = dst[6], a21 = dst[7], a22 = dst[8];
4494 F b01 = nmad(a12, a21, a22 * a11),
4495 b11 = nmad(a22, a10, a12 * a20),
4496 b21 = nmad(a11, a20, a21 * a10);
4497 F det = mad(a00, b01, mad(a01, b11, a02 * b21)),
4498 invdet = rcp_precise(det);
4499 dst[0] = invdet * b01;
4500 dst[1] = invdet * nmad(a22, a01, a02 * a21);
4501 dst[2] = invdet * nmad(a02, a11, a12 * a01);
4502 dst[3] = invdet * b11;
4503 dst[4] = invdet * nmad(a02, a20, a22 * a00);
4504 dst[5] = invdet * nmad(a12, a00, a02 * a10);
4505 dst[6] = invdet * b21;
4506 dst[7] = invdet * nmad(a21, a00, a01 * a20);
4507 dst[8] = invdet * nmad(a01, a10, a11 * a00);
4508 }
4509
STAGE_TAIL(inverse_mat4,F * dst)4510 STAGE_TAIL(inverse_mat4, F* dst) {
4511 F a00 = dst[0], a01 = dst[1], a02 = dst[2], a03 = dst[3],
4512 a10 = dst[4], a11 = dst[5], a12 = dst[6], a13 = dst[7],
4513 a20 = dst[8], a21 = dst[9], a22 = dst[10], a23 = dst[11],
4514 a30 = dst[12], a31 = dst[13], a32 = dst[14], a33 = dst[15];
4515 F b00 = nmad(a01, a10, a00 * a11),
4516 b01 = nmad(a02, a10, a00 * a12),
4517 b02 = nmad(a03, a10, a00 * a13),
4518 b03 = nmad(a02, a11, a01 * a12),
4519 b04 = nmad(a03, a11, a01 * a13),
4520 b05 = nmad(a03, a12, a02 * a13),
4521 b06 = nmad(a21, a30, a20 * a31),
4522 b07 = nmad(a22, a30, a20 * a32),
4523 b08 = nmad(a23, a30, a20 * a33),
4524 b09 = nmad(a22, a31, a21 * a32),
4525 b10 = nmad(a23, a31, a21 * a33),
4526 b11 = nmad(a23, a32, a22 * a33),
4527 det = mad(b00, b11, b05 * b06) + mad(b02, b09, b03 * b08) - mad(b01, b10, b04 * b07),
4528 invdet = rcp_precise(det);
4529 b00 *= invdet;
4530 b01 *= invdet;
4531 b02 *= invdet;
4532 b03 *= invdet;
4533 b04 *= invdet;
4534 b05 *= invdet;
4535 b06 *= invdet;
4536 b07 *= invdet;
4537 b08 *= invdet;
4538 b09 *= invdet;
4539 b10 *= invdet;
4540 b11 *= invdet;
4541 dst[0] = mad(a13, b09, nmad(a12, b10, a11*b11));
4542 dst[1] = nmad(a03, b09, nmad(a01, b11, a02*b10));
4543 dst[2] = mad(a33, b03, nmad(a32, b04, a31*b05));
4544 dst[3] = nmad(a23, b03, nmad(a21, b05, a22*b04));
4545 dst[4] = nmad(a13, b07, nmad(a10, b11, a12*b08));
4546 dst[5] = mad(a03, b07, nmad(a02, b08, a00*b11));
4547 dst[6] = nmad(a33, b01, nmad(a30, b05, a32*b02));
4548 dst[7] = mad(a23, b01, nmad(a22, b02, a20*b05));
4549 dst[8] = mad(a13, b06, nmad(a11, b08, a10*b10));
4550 dst[9] = nmad(a03, b06, nmad(a00, b10, a01*b08));
4551 dst[10] = mad(a33, b00, nmad(a31, b02, a30*b04));
4552 dst[11] = nmad(a23, b00, nmad(a20, b04, a21*b02));
4553 dst[12] = nmad(a12, b06, nmad(a10, b09, a11*b07));
4554 dst[13] = mad(a02, b06, nmad(a01, b07, a00*b09));
4555 dst[14] = nmad(a32, b00, nmad(a30, b03, a31*b01));
4556 dst[15] = mad(a22, b00, nmad(a21, b01, a20*b03));
4557 }
4558
4559 // Binary operations take two adjacent inputs, and write their output in the first position.
4560 template <typename T, void (*ApplyFn)(T*, T*)>
apply_adjacent_binary(T * dst,T * src)4561 SI void apply_adjacent_binary(T* dst, T* src) {
4562 T* end = src;
4563 do {
4564 ApplyFn(dst, src);
4565 dst += 1;
4566 src += 1;
4567 } while (dst != end);
4568 }
4569
4570 template <typename T, void (*ApplyFn)(T*, T*)>
apply_adjacent_binary_packed(SkRasterPipeline_BinaryOpCtx * packed,std::byte * base)4571 SI void apply_adjacent_binary_packed(SkRasterPipeline_BinaryOpCtx* packed, std::byte* base) {
4572 auto ctx = SkRPCtxUtils::Unpack(packed);
4573 std::byte* dst = base + ctx.dst;
4574 std::byte* src = base + ctx.src;
4575 apply_adjacent_binary<T, ApplyFn>((T*)dst, (T*)src);
4576 }
4577
4578 template <int N, typename V, typename S, void (*ApplyFn)(V*, V*)>
apply_binary_immediate(SkRasterPipeline_ConstantCtx * packed,std::byte * base)4579 SI void apply_binary_immediate(SkRasterPipeline_ConstantCtx* packed, std::byte* base) {
4580 auto ctx = SkRPCtxUtils::Unpack(packed);
4581 V* dst = (V*)(base + ctx.dst); // get a pointer to the destination
4582 S scalar = sk_bit_cast<S>(ctx.value); // bit-pun the constant value as desired
4583 V src = scalar - V(); // broadcast the constant value into a vector
4584 SK_UNROLL for (int index = 0; index < N; ++index) {
4585 ApplyFn(dst, &src); // perform the operation
4586 dst += 1;
4587 }
4588 }
4589
4590 template <typename T>
add_fn(T * dst,T * src)4591 SI void add_fn(T* dst, T* src) {
4592 *dst += *src;
4593 }
4594
4595 template <typename T>
sub_fn(T * dst,T * src)4596 SI void sub_fn(T* dst, T* src) {
4597 *dst -= *src;
4598 }
4599
4600 template <typename T>
mul_fn(T * dst,T * src)4601 SI void mul_fn(T* dst, T* src) {
4602 *dst *= *src;
4603 }
4604
4605 template <typename T>
div_fn(T * dst,T * src)4606 SI void div_fn(T* dst, T* src) {
4607 T divisor = *src;
4608 if constexpr (!std::is_same_v<T, F>) {
4609 // We will crash if we integer-divide against zero. Convert 0 to ~0 to avoid this.
4610 divisor |= (T)cond_to_mask(divisor == 0);
4611 }
4612 *dst /= divisor;
4613 }
4614
bitwise_and_fn(I32 * dst,I32 * src)4615 SI void bitwise_and_fn(I32* dst, I32* src) {
4616 *dst &= *src;
4617 }
4618
bitwise_or_fn(I32 * dst,I32 * src)4619 SI void bitwise_or_fn(I32* dst, I32* src) {
4620 *dst |= *src;
4621 }
4622
bitwise_xor_fn(I32 * dst,I32 * src)4623 SI void bitwise_xor_fn(I32* dst, I32* src) {
4624 *dst ^= *src;
4625 }
4626
4627 template <typename T>
max_fn(T * dst,T * src)4628 SI void max_fn(T* dst, T* src) {
4629 *dst = max(*dst, *src);
4630 }
4631
4632 template <typename T>
min_fn(T * dst,T * src)4633 SI void min_fn(T* dst, T* src) {
4634 *dst = min(*dst, *src);
4635 }
4636
4637 template <typename T>
cmplt_fn(T * dst,T * src)4638 SI void cmplt_fn(T* dst, T* src) {
4639 static_assert(sizeof(T) == sizeof(I32));
4640 I32 result = cond_to_mask(*dst < *src);
4641 memcpy(dst, &result, sizeof(I32));
4642 }
4643
4644 template <typename T>
cmple_fn(T * dst,T * src)4645 SI void cmple_fn(T* dst, T* src) {
4646 static_assert(sizeof(T) == sizeof(I32));
4647 I32 result = cond_to_mask(*dst <= *src);
4648 memcpy(dst, &result, sizeof(I32));
4649 }
4650
4651 template <typename T>
cmpeq_fn(T * dst,T * src)4652 SI void cmpeq_fn(T* dst, T* src) {
4653 static_assert(sizeof(T) == sizeof(I32));
4654 I32 result = cond_to_mask(*dst == *src);
4655 memcpy(dst, &result, sizeof(I32));
4656 }
4657
4658 template <typename T>
cmpne_fn(T * dst,T * src)4659 SI void cmpne_fn(T* dst, T* src) {
4660 static_assert(sizeof(T) == sizeof(I32));
4661 I32 result = cond_to_mask(*dst != *src);
4662 memcpy(dst, &result, sizeof(I32));
4663 }
4664
atan2_fn(F * dst,F * src)4665 SI void atan2_fn(F* dst, F* src) {
4666 *dst = atan2_(*dst, *src);
4667 }
4668
pow_fn(F * dst,F * src)4669 SI void pow_fn(F* dst, F* src) {
4670 *dst = approx_powf(*dst, *src);
4671 }
4672
mod_fn(F * dst,F * src)4673 SI void mod_fn(F* dst, F* src) {
4674 *dst = nmad(*src, floor_(*dst / *src), *dst);
4675 }
4676
4677 #define DECLARE_N_WAY_BINARY_FLOAT(name) \
4678 STAGE_TAIL(name##_n_floats, SkRasterPipeline_BinaryOpCtx* packed) { \
4679 apply_adjacent_binary_packed<F, &name##_fn>(packed, base); \
4680 }
4681
4682 #define DECLARE_BINARY_FLOAT(name) \
4683 STAGE_TAIL(name##_float, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 1); } \
4684 STAGE_TAIL(name##_2_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 2); } \
4685 STAGE_TAIL(name##_3_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 3); } \
4686 STAGE_TAIL(name##_4_floats, F* dst) { apply_adjacent_binary<F, &name##_fn>(dst, dst + 4); } \
4687 DECLARE_N_WAY_BINARY_FLOAT(name)
4688
4689 #define DECLARE_N_WAY_BINARY_INT(name) \
4690 STAGE_TAIL(name##_n_ints, SkRasterPipeline_BinaryOpCtx* packed) { \
4691 apply_adjacent_binary_packed<I32, &name##_fn>(packed, base); \
4692 }
4693
4694 #define DECLARE_BINARY_INT(name) \
4695 STAGE_TAIL(name##_int, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 1); } \
4696 STAGE_TAIL(name##_2_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 2); } \
4697 STAGE_TAIL(name##_3_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 3); } \
4698 STAGE_TAIL(name##_4_ints, I32* dst) { apply_adjacent_binary<I32, &name##_fn>(dst, dst + 4); } \
4699 DECLARE_N_WAY_BINARY_INT(name)
4700
4701 #define DECLARE_N_WAY_BINARY_UINT(name) \
4702 STAGE_TAIL(name##_n_uints, SkRasterPipeline_BinaryOpCtx* packed) { \
4703 apply_adjacent_binary_packed<U32, &name##_fn>(packed, base); \
4704 }
4705
4706 #define DECLARE_BINARY_UINT(name) \
4707 STAGE_TAIL(name##_uint, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 1); } \
4708 STAGE_TAIL(name##_2_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 2); } \
4709 STAGE_TAIL(name##_3_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 3); } \
4710 STAGE_TAIL(name##_4_uints, U32* dst) { apply_adjacent_binary<U32, &name##_fn>(dst, dst + 4); } \
4711 DECLARE_N_WAY_BINARY_UINT(name)
4712
4713 // Many ops reuse the int stages when performing uint arithmetic, since they're equivalent on a
4714 // two's-complement machine. (Even multiplication is equivalent in the lower 32 bits.)
DECLARE_BINARY_INT(add)4715 DECLARE_BINARY_FLOAT(add) DECLARE_BINARY_INT(add)
4716 DECLARE_BINARY_FLOAT(sub) DECLARE_BINARY_INT(sub)
4717 DECLARE_BINARY_FLOAT(mul) DECLARE_BINARY_INT(mul)
4718 DECLARE_BINARY_FLOAT(div) DECLARE_BINARY_INT(div) DECLARE_BINARY_UINT(div)
4719 DECLARE_BINARY_INT(bitwise_and)
4720 DECLARE_BINARY_INT(bitwise_or)
4721 DECLARE_BINARY_INT(bitwise_xor)
4722 DECLARE_BINARY_FLOAT(mod)
4723 DECLARE_BINARY_FLOAT(min) DECLARE_BINARY_INT(min) DECLARE_BINARY_UINT(min)
4724 DECLARE_BINARY_FLOAT(max) DECLARE_BINARY_INT(max) DECLARE_BINARY_UINT(max)
4725 DECLARE_BINARY_FLOAT(cmplt) DECLARE_BINARY_INT(cmplt) DECLARE_BINARY_UINT(cmplt)
4726 DECLARE_BINARY_FLOAT(cmple) DECLARE_BINARY_INT(cmple) DECLARE_BINARY_UINT(cmple)
4727 DECLARE_BINARY_FLOAT(cmpeq) DECLARE_BINARY_INT(cmpeq)
4728 DECLARE_BINARY_FLOAT(cmpne) DECLARE_BINARY_INT(cmpne)
4729
4730 // Sufficiently complex ops only provide an N-way version, to avoid code bloat from the dedicated
4731 // 1-4 slot versions.
4732 DECLARE_N_WAY_BINARY_FLOAT(atan2)
4733 DECLARE_N_WAY_BINARY_FLOAT(pow)
4734
4735 // Some ops have an optimized version when the right-side is an immediate value.
4736 #define DECLARE_IMM_BINARY_FLOAT(name) \
4737 STAGE_TAIL(name##_imm_float, SkRasterPipeline_ConstantCtx* packed) { \
4738 apply_binary_immediate<1, F, float, &name##_fn>(packed, base); \
4739 }
4740 #define DECLARE_IMM_BINARY_INT(name) \
4741 STAGE_TAIL(name##_imm_int, SkRasterPipeline_ConstantCtx* packed) { \
4742 apply_binary_immediate<1, I32, int32_t, &name##_fn>(packed, base); \
4743 }
4744 #define DECLARE_MULTI_IMM_BINARY_INT(name) \
4745 STAGE_TAIL(name##_imm_int, SkRasterPipeline_ConstantCtx* packed) { \
4746 apply_binary_immediate<1, I32, int32_t, &name##_fn>(packed, base); \
4747 } \
4748 STAGE_TAIL(name##_imm_2_ints, SkRasterPipeline_ConstantCtx* packed) { \
4749 apply_binary_immediate<2, I32, int32_t, &name##_fn>(packed, base); \
4750 } \
4751 STAGE_TAIL(name##_imm_3_ints, SkRasterPipeline_ConstantCtx* packed) { \
4752 apply_binary_immediate<3, I32, int32_t, &name##_fn>(packed, base); \
4753 } \
4754 STAGE_TAIL(name##_imm_4_ints, SkRasterPipeline_ConstantCtx* packed) { \
4755 apply_binary_immediate<4, I32, int32_t, &name##_fn>(packed, base); \
4756 }
4757 #define DECLARE_IMM_BINARY_UINT(name) \
4758 STAGE_TAIL(name##_imm_uint, SkRasterPipeline_ConstantCtx* packed) { \
4759 apply_binary_immediate<1, U32, uint32_t, &name##_fn>(packed, base); \
4760 }
4761
4762 DECLARE_IMM_BINARY_FLOAT(add) DECLARE_IMM_BINARY_INT(add)
4763 DECLARE_IMM_BINARY_FLOAT(mul) DECLARE_IMM_BINARY_INT(mul)
4764 DECLARE_MULTI_IMM_BINARY_INT(bitwise_and)
4765 DECLARE_IMM_BINARY_FLOAT(max)
4766 DECLARE_IMM_BINARY_FLOAT(min)
4767 DECLARE_IMM_BINARY_INT(bitwise_xor)
4768 DECLARE_IMM_BINARY_FLOAT(cmplt) DECLARE_IMM_BINARY_INT(cmplt) DECLARE_IMM_BINARY_UINT(cmplt)
4769 DECLARE_IMM_BINARY_FLOAT(cmple) DECLARE_IMM_BINARY_INT(cmple) DECLARE_IMM_BINARY_UINT(cmple)
4770 DECLARE_IMM_BINARY_FLOAT(cmpeq) DECLARE_IMM_BINARY_INT(cmpeq)
4771 DECLARE_IMM_BINARY_FLOAT(cmpne) DECLARE_IMM_BINARY_INT(cmpne)
4772
4773 #undef DECLARE_MULTI_IMM_BINARY_INT
4774 #undef DECLARE_IMM_BINARY_FLOAT
4775 #undef DECLARE_IMM_BINARY_INT
4776 #undef DECLARE_IMM_BINARY_UINT
4777 #undef DECLARE_BINARY_FLOAT
4778 #undef DECLARE_BINARY_INT
4779 #undef DECLARE_BINARY_UINT
4780 #undef DECLARE_N_WAY_BINARY_FLOAT
4781 #undef DECLARE_N_WAY_BINARY_INT
4782 #undef DECLARE_N_WAY_BINARY_UINT
4783
4784 // Dots can be represented with multiply and add ops, but they are so foundational that it's worth
4785 // having dedicated ops.
4786 STAGE_TAIL(dot_2_floats, F* dst) {
4787 dst[0] = mad(dst[0], dst[2],
4788 dst[1] * dst[3]);
4789 }
4790
STAGE_TAIL(dot_3_floats,F * dst)4791 STAGE_TAIL(dot_3_floats, F* dst) {
4792 dst[0] = mad(dst[0], dst[3],
4793 mad(dst[1], dst[4],
4794 dst[2] * dst[5]));
4795 }
4796
STAGE_TAIL(dot_4_floats,F * dst)4797 STAGE_TAIL(dot_4_floats, F* dst) {
4798 dst[0] = mad(dst[0], dst[4],
4799 mad(dst[1], dst[5],
4800 mad(dst[2], dst[6],
4801 dst[3] * dst[7])));
4802 }
4803
4804 // MxM, VxM and MxV multiplication all use matrix_multiply. Vectors are treated like a matrix with a
4805 // single column or row.
4806 template <int N>
matrix_multiply(SkRasterPipeline_MatrixMultiplyCtx * packed,std::byte * base)4807 SI void matrix_multiply(SkRasterPipeline_MatrixMultiplyCtx* packed, std::byte* base) {
4808 auto ctx = SkRPCtxUtils::Unpack(packed);
4809
4810 int outColumns = ctx.rightColumns,
4811 outRows = ctx.leftRows;
4812
4813 SkASSERT(outColumns >= 1);
4814 SkASSERT(outRows >= 1);
4815 SkASSERT(outColumns <= 4);
4816 SkASSERT(outRows <= 4);
4817
4818 SkASSERT(ctx.leftColumns == ctx.rightRows);
4819 SkASSERT(N == ctx.leftColumns); // N should match the result width
4820
4821 #if !defined(SKRP_CPU_SCALAR)
4822 // This prevents Clang from generating early-out checks for zero-sized matrices.
4823 SK_ASSUME(outColumns >= 1);
4824 SK_ASSUME(outRows >= 1);
4825 SK_ASSUME(outColumns <= 4);
4826 SK_ASSUME(outRows <= 4);
4827 #endif
4828
4829 // Get pointers to the adjacent left- and right-matrices.
4830 F* resultMtx = (F*)(base + ctx.dst);
4831 F* leftMtx = &resultMtx[ctx.rightColumns * ctx.leftRows];
4832 F* rightMtx = &leftMtx[N * ctx.leftRows];
4833
4834 // Emit each matrix element.
4835 for (int c = 0; c < outColumns; ++c) {
4836 for (int r = 0; r < outRows; ++r) {
4837 // Dot a vector from leftMtx[*][r] with rightMtx[c][*].
4838 F* leftRow = &leftMtx [r];
4839 F* rightColumn = &rightMtx[c * N];
4840
4841 F element = *leftRow * *rightColumn;
4842 for (int idx = 1; idx < N; ++idx) {
4843 leftRow += outRows;
4844 rightColumn += 1;
4845 element = mad(*leftRow, *rightColumn, element);
4846 }
4847
4848 *resultMtx++ = element;
4849 }
4850 }
4851 }
4852
STAGE_TAIL(matrix_multiply_2,SkRasterPipeline_MatrixMultiplyCtx * packed)4853 STAGE_TAIL(matrix_multiply_2, SkRasterPipeline_MatrixMultiplyCtx* packed) {
4854 matrix_multiply<2>(packed, base);
4855 }
4856
STAGE_TAIL(matrix_multiply_3,SkRasterPipeline_MatrixMultiplyCtx * packed)4857 STAGE_TAIL(matrix_multiply_3, SkRasterPipeline_MatrixMultiplyCtx* packed) {
4858 matrix_multiply<3>(packed, base);
4859 }
4860
STAGE_TAIL(matrix_multiply_4,SkRasterPipeline_MatrixMultiplyCtx * packed)4861 STAGE_TAIL(matrix_multiply_4, SkRasterPipeline_MatrixMultiplyCtx* packed) {
4862 matrix_multiply<4>(packed, base);
4863 }
4864
4865 // Refract always operates on 4-wide incident and normal vectors; for narrower inputs, the code
4866 // generator fills in the input columns with zero, and discards the extra output columns.
STAGE_TAIL(refract_4_floats,F * dst)4867 STAGE_TAIL(refract_4_floats, F* dst) {
4868 // Algorithm adapted from https://registry.khronos.org/OpenGL-Refpages/gl4/html/refract.xhtml
4869 F *incident = dst + 0;
4870 F *normal = dst + 4;
4871 F eta = dst[8];
4872
4873 F dotNI = mad(normal[0], incident[0],
4874 mad(normal[1], incident[1],
4875 mad(normal[2], incident[2],
4876 normal[3] * incident[3])));
4877
4878 F k = 1.0 - eta * eta * (1.0 - dotNI * dotNI);
4879 F sqrt_k = sqrt_(k);
4880
4881 for (int idx = 0; idx < 4; ++idx) {
4882 dst[idx] = if_then_else(k >= 0,
4883 eta * incident[idx] - (eta * dotNI + sqrt_k) * normal[idx],
4884 0.0);
4885 }
4886 }
4887
4888 // Ternary operations work like binary ops (see immediately above) but take two source inputs.
4889 template <typename T, void (*ApplyFn)(T*, T*, T*)>
apply_adjacent_ternary(T * dst,T * src0,T * src1)4890 SI void apply_adjacent_ternary(T* dst, T* src0, T* src1) {
4891 int count = src0 - dst;
4892 #if !defined(SKRP_CPU_SCALAR)
4893 SK_ASSUME(count >= 1);
4894 #endif
4895
4896 for (int index = 0; index < count; ++index) {
4897 ApplyFn(dst, src0, src1);
4898 dst += 1;
4899 src0 += 1;
4900 src1 += 1;
4901 }
4902 }
4903
4904 template <typename T, void (*ApplyFn)(T*, T*, T*)>
apply_adjacent_ternary_packed(SkRasterPipeline_TernaryOpCtx * packed,std::byte * base)4905 SI void apply_adjacent_ternary_packed(SkRasterPipeline_TernaryOpCtx* packed, std::byte* base) {
4906 auto ctx = SkRPCtxUtils::Unpack(packed);
4907 std::byte* dst = base + ctx.dst;
4908 std::byte* src0 = dst + ctx.delta;
4909 std::byte* src1 = src0 + ctx.delta;
4910 apply_adjacent_ternary<T, ApplyFn>((T*)dst, (T*)src0, (T*)src1);
4911 }
4912
mix_fn(F * a,F * x,F * y)4913 SI void mix_fn(F* a, F* x, F* y) {
4914 // We reorder the arguments here to match lerp's GLSL-style order (interpolation point last).
4915 *a = lerp(*x, *y, *a);
4916 }
4917
mix_fn(I32 * a,I32 * x,I32 * y)4918 SI void mix_fn(I32* a, I32* x, I32* y) {
4919 // We reorder the arguments here to match if_then_else's expected order (y before x).
4920 *a = if_then_else(*a, *y, *x);
4921 }
4922
smoothstep_fn(F * edge0,F * edge1,F * x)4923 SI void smoothstep_fn(F* edge0, F* edge1, F* x) {
4924 F t = clamp_01_((*x - *edge0) / (*edge1 - *edge0));
4925 *edge0 = t * t * (3.0 - 2.0 * t);
4926 }
4927
4928 #define DECLARE_N_WAY_TERNARY_FLOAT(name) \
4929 STAGE_TAIL(name##_n_floats, SkRasterPipeline_TernaryOpCtx* packed) { \
4930 apply_adjacent_ternary_packed<F, &name##_fn>(packed, base); \
4931 }
4932
4933 #define DECLARE_TERNARY_FLOAT(name) \
4934 STAGE_TAIL(name##_float, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+1, p+2); } \
4935 STAGE_TAIL(name##_2_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+2, p+4); } \
4936 STAGE_TAIL(name##_3_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+3, p+6); } \
4937 STAGE_TAIL(name##_4_floats, F* p) { apply_adjacent_ternary<F, &name##_fn>(p, p+4, p+8); } \
4938 DECLARE_N_WAY_TERNARY_FLOAT(name)
4939
4940 #define DECLARE_TERNARY_INT(name) \
4941 STAGE_TAIL(name##_int, I32* p) { apply_adjacent_ternary<I32, &name##_fn>(p, p+1, p+2); } \
4942 STAGE_TAIL(name##_2_ints, I32* p) { apply_adjacent_ternary<I32, &name##_fn>(p, p+2, p+4); } \
4943 STAGE_TAIL(name##_3_ints, I32* p) { apply_adjacent_ternary<I32, &name##_fn>(p, p+3, p+6); } \
4944 STAGE_TAIL(name##_4_ints, I32* p) { apply_adjacent_ternary<I32, &name##_fn>(p, p+4, p+8); } \
4945 STAGE_TAIL(name##_n_ints, SkRasterPipeline_TernaryOpCtx* packed) { \
4946 apply_adjacent_ternary_packed<I32, &name##_fn>(packed, base); \
4947 }
4948
4949 DECLARE_N_WAY_TERNARY_FLOAT(smoothstep)
DECLARE_TERNARY_FLOAT(mix)4950 DECLARE_TERNARY_FLOAT(mix)
4951 DECLARE_TERNARY_INT(mix)
4952
4953 #undef DECLARE_N_WAY_TERNARY_FLOAT
4954 #undef DECLARE_TERNARY_FLOAT
4955 #undef DECLARE_TERNARY_INT
4956
4957 STAGE(gauss_a_to_rgba, NoCtx) {
4958 // x = 1 - x;
4959 // exp(-x * x * 4) - 0.018f;
4960 // ... now approximate with quartic
4961 //
4962 const float c4 = -2.26661229133605957031f;
4963 const float c3 = 2.89795351028442382812f;
4964 const float c2 = 0.21345567703247070312f;
4965 const float c1 = 0.15489584207534790039f;
4966 const float c0 = 0.00030726194381713867f;
4967 a = mad(a, mad(a, mad(a, mad(a, c4, c3), c2), c1), c0);
4968 r = a;
4969 g = a;
4970 b = a;
4971 }
4972
4973 // A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
STAGE(bilerp_clamp_8888,const SkRasterPipeline_GatherCtx * ctx)4974 STAGE(bilerp_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
4975 // (cx,cy) are the center of our sample.
4976 F cx = r,
4977 cy = g;
4978
4979 // All sample points are at the same fractional offset (fx,fy).
4980 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
4981 F fx = fract(cx + 0.5f),
4982 fy = fract(cy + 0.5f);
4983
4984 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
4985 r = g = b = a = F0;
4986
4987 for (float py = -0.5f; py <= +0.5f; py += 1.0f)
4988 for (float px = -0.5f; px <= +0.5f; px += 1.0f) {
4989 // (x,y) are the coordinates of this sample point.
4990 F x = cx + px,
4991 y = cy + py;
4992
4993 // ix_and_ptr() will clamp to the image's bounds for us.
4994 const uint32_t* ptr;
4995 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
4996
4997 F sr,sg,sb,sa;
4998 from_8888(gather(ptr, ix), &sr,&sg,&sb,&sa);
4999
5000 // In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
5001 // are combined in direct proportion to their area overlapping that logical query pixel.
5002 // At positive offsets, the x-axis contribution to that rectangle is fx,
5003 // or (1-fx) at negative x. Same deal for y.
5004 F sx = (px > 0) ? fx : 1.0f - fx,
5005 sy = (py > 0) ? fy : 1.0f - fy,
5006 area = sx * sy;
5007
5008 r += sr * area;
5009 g += sg * area;
5010 b += sb * area;
5011 a += sa * area;
5012 }
5013 }
5014
5015 // A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
STAGE(bicubic_clamp_8888,const SkRasterPipeline_GatherCtx * ctx)5016 STAGE(bicubic_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
5017 // (cx,cy) are the center of our sample.
5018 F cx = r,
5019 cy = g;
5020
5021 // All sample points are at the same fractional offset (fx,fy).
5022 // They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
5023 F fx = fract(cx + 0.5f),
5024 fy = fract(cy + 0.5f);
5025
5026 // We'll accumulate the color of all four samples into {r,g,b,a} directly.
5027 r = g = b = a = F0;
5028
5029 const float* w = ctx->weights;
5030 const F scaley[4] = {bicubic_wts(fy, w[0], w[4], w[ 8], w[12]),
5031 bicubic_wts(fy, w[1], w[5], w[ 9], w[13]),
5032 bicubic_wts(fy, w[2], w[6], w[10], w[14]),
5033 bicubic_wts(fy, w[3], w[7], w[11], w[15])};
5034 const F scalex[4] = {bicubic_wts(fx, w[0], w[4], w[ 8], w[12]),
5035 bicubic_wts(fx, w[1], w[5], w[ 9], w[13]),
5036 bicubic_wts(fx, w[2], w[6], w[10], w[14]),
5037 bicubic_wts(fx, w[3], w[7], w[11], w[15])};
5038
5039 F sample_y = cy - 1.5f;
5040 for (int yy = 0; yy <= 3; ++yy) {
5041 F sample_x = cx - 1.5f;
5042 for (int xx = 0; xx <= 3; ++xx) {
5043 F scale = scalex[xx] * scaley[yy];
5044
5045 // ix_and_ptr() will clamp to the image's bounds for us.
5046 const uint32_t* ptr;
5047 U32 ix = ix_and_ptr(&ptr, ctx, sample_x, sample_y);
5048
5049 F sr,sg,sb,sa;
5050 from_8888(gather(ptr, ix), &sr,&sg,&sb,&sa);
5051
5052 r = mad(scale, sr, r);
5053 g = mad(scale, sg, g);
5054 b = mad(scale, sb, b);
5055 a = mad(scale, sa, a);
5056
5057 sample_x += 1;
5058 }
5059 sample_y += 1;
5060 }
5061 }
5062
5063 // ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
5064
STAGE(swizzle,void * ctx)5065 STAGE(swizzle, void* ctx) {
5066 auto ir = r, ig = g, ib = b, ia = a;
5067 F* o[] = {&r, &g, &b, &a};
5068 char swiz[4];
5069 memcpy(swiz, &ctx, sizeof(swiz));
5070
5071 for (int i = 0; i < 4; ++i) {
5072 switch (swiz[i]) {
5073 case 'r': *o[i] = ir; break;
5074 case 'g': *o[i] = ig; break;
5075 case 'b': *o[i] = ib; break;
5076 case 'a': *o[i] = ia; break;
5077 case '0': *o[i] = F0; break;
5078 case '1': *o[i] = F1; break;
5079 default: break;
5080 }
5081 }
5082 }
5083
5084 namespace lowp {
5085 #if defined(SKRP_CPU_SCALAR) || defined(SK_ENABLE_OPTIMIZE_SIZE) || \
5086 defined(SK_BUILD_FOR_GOOGLE3) || defined(SK_DISABLE_LOWP_RASTER_PIPELINE)
5087 // We don't bother generating the lowp stages if we are:
5088 // - ... in scalar mode (MSVC, old clang, etc...)
5089 // - ... trying to save code size
5090 // - ... building for Google3. (No justification for this, but changing it would be painful).
5091 // - ... explicitly disabling it. This is currently just used by Flutter.
5092 //
5093 // Having nullptr for every stage will cause SkRasterPipeline to always use the highp stages.
5094 #define M(st) static void (*st)(void) = nullptr;
5095 SK_RASTER_PIPELINE_OPS_LOWP(M)
5096 #undef M
5097 static void (*just_return)(void) = nullptr;
5098
start_pipeline(size_t,size_t,size_t,size_t,SkRasterPipelineStage *,SkSpan<SkRasterPipeline_MemoryCtxPatch>,uint8_t * tailPointer)5099 static void start_pipeline(size_t,size_t,size_t,size_t, SkRasterPipelineStage*,
5100 SkSpan<SkRasterPipeline_MemoryCtxPatch>,
5101 uint8_t* tailPointer) {}
5102
5103 #else // We are compiling vector code with Clang... let's make some lowp stages!
5104
5105 #if defined(SKRP_CPU_SKX) || defined(SKRP_CPU_HSW) || defined(SKRP_CPU_LASX)
5106 template <typename T> using V = Vec<16, T>;
5107 #else
5108 template <typename T> using V = Vec<8, T>;
5109 #endif
5110
5111 using U8 = V<uint8_t >;
5112 using U16 = V<uint16_t>;
5113 using I16 = V< int16_t>;
5114 using I32 = V< int32_t>;
5115 using U32 = V<uint32_t>;
5116 using I64 = V< int64_t>;
5117 using U64 = V<uint64_t>;
5118 using F = V<float >;
5119
5120 static constexpr size_t N = sizeof(U16) / sizeof(uint16_t);
5121
5122 // Promotion helpers (for GCC)
5123 #if defined(__clang__)
5124 SI constexpr U16 U16_(uint16_t x) { return x; }
5125 SI constexpr I32 I32_( int32_t x) { return x; }
5126 SI constexpr U32 U32_(uint32_t x) { return x; }
5127 SI constexpr F F_ (float x) { return x; }
5128 #else
5129 SI constexpr U16 U16_(uint16_t x) { return x + U16(); }
5130 SI constexpr I32 I32_( int32_t x) { return x + I32(); }
5131 SI constexpr U32 U32_(uint32_t x) { return x + U32(); }
5132 SI constexpr F F_ (float x) { return x - F (); }
5133 #endif
5134
5135 static constexpr U16 U16_0 = U16_(0),
5136 U16_255 = U16_(255);
5137
5138 // Once again, some platforms benefit from a restricted Stage calling convention,
5139 // but others can pass tons and tons of registers and we're happy to exploit that.
5140 // It's exactly the same decision and implementation strategy as the F stages above.
5141 #if SKRP_NARROW_STAGES
5142 struct Params {
5143 size_t dx, dy;
5144 U16 dr,dg,db,da;
5145 };
5146 using Stage = void (ABI*)(Params*, SkRasterPipelineStage* program, U16 r, U16 g, U16 b, U16 a);
5147 #else
5148 using Stage = void (ABI*)(SkRasterPipelineStage* program,
5149 size_t dx, size_t dy,
5150 U16 r, U16 g, U16 b, U16 a,
5151 U16 dr, U16 dg, U16 db, U16 da);
5152 #endif
5153
5154 static void start_pipeline(size_t x0, size_t y0,
5155 size_t xlimit, size_t ylimit,
5156 SkRasterPipelineStage* program,
5157 SkSpan<SkRasterPipeline_MemoryCtxPatch> memoryCtxPatches,
5158 uint8_t* tailPointer) {
5159 uint8_t unreferencedTail;
5160 if (!tailPointer) {
5161 tailPointer = &unreferencedTail;
5162 }
5163 auto start = (Stage)program->fn;
5164 for (size_t dy = y0; dy < ylimit; dy++) {
5165 #if SKRP_NARROW_STAGES
5166 Params params = { x0,dy, U16_0,U16_0,U16_0,U16_0 };
5167 for (; params.dx + N <= xlimit; params.dx += N) {
5168 start(¶ms, program, U16_0,U16_0,U16_0,U16_0);
5169 }
5170 if (size_t tail = xlimit - params.dx) {
5171 *tailPointer = tail;
5172 patch_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
5173 start(¶ms, program, U16_0,U16_0,U16_0,U16_0);
5174 restore_memory_contexts(memoryCtxPatches, params.dx, dy, tail);
5175 *tailPointer = 0xFF;
5176 }
5177 #else
5178 size_t dx = x0;
5179 for (; dx + N <= xlimit; dx += N) {
5180 start(program, dx,dy, U16_0,U16_0,U16_0,U16_0, U16_0,U16_0,U16_0,U16_0);
5181 }
5182 if (size_t tail = xlimit - dx) {
5183 *tailPointer = tail;
5184 patch_memory_contexts(memoryCtxPatches, dx, dy, tail);
5185 start(program, dx,dy, U16_0,U16_0,U16_0,U16_0, U16_0,U16_0,U16_0,U16_0);
5186 restore_memory_contexts(memoryCtxPatches, dx, dy, tail);
5187 *tailPointer = 0xFF;
5188 }
5189 #endif
5190 }
5191 }
5192
5193 #if SKRP_NARROW_STAGES
5194 static void ABI just_return(Params*, SkRasterPipelineStage*, U16,U16,U16,U16) {}
5195 #else
5196 static void ABI just_return(SkRasterPipelineStage*, size_t,size_t,
5197 U16,U16,U16,U16, U16,U16,U16,U16) {}
5198 #endif
5199
5200 // All stages use the same function call ABI to chain into each other, but there are three types:
5201 // GG: geometry in, geometry out -- think, a matrix
5202 // GP: geometry in, pixels out. -- think, a memory gather
5203 // PP: pixels in, pixels out. -- think, a blend mode
5204 //
5205 // (Some stages ignore their inputs or produce no logical output. That's perfectly fine.)
5206 //
5207 // These three STAGE_ macros let you define each type of stage,
5208 // and will have (x,y) geometry and/or (r,g,b,a, dr,dg,db,da) pixel arguments as appropriate.
5209
5210 #if SKRP_NARROW_STAGES
5211 #define STAGE_GG(name, ARG) \
5212 SI void name##_k(ARG, size_t dx, size_t dy, F& x, F& y); \
5213 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5214 U16 r, U16 g, U16 b, U16 a) { \
5215 auto x = join<F>(r,g), \
5216 y = join<F>(b,a); \
5217 name##_k(Ctx{program}, params->dx,params->dy, x,y); \
5218 split(x, &r,&g); \
5219 split(y, &b,&a); \
5220 auto fn = (Stage)(++program)->fn; \
5221 fn(params, program, r,g,b,a); \
5222 } \
5223 SI void name##_k(ARG, size_t dx, size_t dy, F& x, F& y)
5224
5225 #define STAGE_GP(name, ARG) \
5226 SI void name##_k(ARG, size_t dx, size_t dy, F x, F y, \
5227 U16& r, U16& g, U16& b, U16& a, \
5228 U16& dr, U16& dg, U16& db, U16& da); \
5229 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5230 U16 r, U16 g, U16 b, U16 a) { \
5231 auto x = join<F>(r,g), \
5232 y = join<F>(b,a); \
5233 name##_k(Ctx{program}, params->dx,params->dy, x,y, r,g,b,a, \
5234 params->dr,params->dg,params->db,params->da); \
5235 auto fn = (Stage)(++program)->fn; \
5236 fn(params, program, r,g,b,a); \
5237 } \
5238 SI void name##_k(ARG, size_t dx, size_t dy, F x, F y, \
5239 U16& r, U16& g, U16& b, U16& a, \
5240 U16& dr, U16& dg, U16& db, U16& da)
5241
5242 #define STAGE_PP(name, ARG) \
5243 SI void name##_k(ARG, size_t dx, size_t dy, \
5244 U16& r, U16& g, U16& b, U16& a, \
5245 U16& dr, U16& dg, U16& db, U16& da); \
5246 static void ABI name(Params* params, SkRasterPipelineStage* program, \
5247 U16 r, U16 g, U16 b, U16 a) { \
5248 name##_k(Ctx{program}, params->dx,params->dy, r,g,b,a, \
5249 params->dr,params->dg,params->db,params->da); \
5250 auto fn = (Stage)(++program)->fn; \
5251 fn(params, program, r,g,b,a); \
5252 } \
5253 SI void name##_k(ARG, size_t dx, size_t dy, \
5254 U16& r, U16& g, U16& b, U16& a, \
5255 U16& dr, U16& dg, U16& db, U16& da)
5256 #else
5257 #define STAGE_GG(name, ARG) \
5258 SI void name##_k(ARG, size_t dx, size_t dy, F& x, F& y); \
5259 static void ABI name(SkRasterPipelineStage* program, \
5260 size_t dx, size_t dy, \
5261 U16 r, U16 g, U16 b, U16 a, \
5262 U16 dr, U16 dg, U16 db, U16 da) { \
5263 auto x = join<F>(r,g), \
5264 y = join<F>(b,a); \
5265 name##_k(Ctx{program}, dx,dy, x,y); \
5266 split(x, &r,&g); \
5267 split(y, &b,&a); \
5268 auto fn = (Stage)(++program)->fn; \
5269 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5270 } \
5271 SI void name##_k(ARG, size_t dx, size_t dy, F& x, F& y)
5272
5273 #define STAGE_GP(name, ARG) \
5274 SI void name##_k(ARG, size_t dx, size_t dy, F x, F y, \
5275 U16& r, U16& g, U16& b, U16& a, \
5276 U16& dr, U16& dg, U16& db, U16& da); \
5277 static void ABI name(SkRasterPipelineStage* program, \
5278 size_t dx, size_t dy, \
5279 U16 r, U16 g, U16 b, U16 a, \
5280 U16 dr, U16 dg, U16 db, U16 da) { \
5281 auto x = join<F>(r,g), \
5282 y = join<F>(b,a); \
5283 name##_k(Ctx{program}, dx,dy, x,y, r,g,b,a, dr,dg,db,da); \
5284 auto fn = (Stage)(++program)->fn; \
5285 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5286 } \
5287 SI void name##_k(ARG, size_t dx, size_t dy, F x, F y, \
5288 U16& r, U16& g, U16& b, U16& a, \
5289 U16& dr, U16& dg, U16& db, U16& da)
5290
5291 #define STAGE_PP(name, ARG) \
5292 SI void name##_k(ARG, size_t dx, size_t dy, \
5293 U16& r, U16& g, U16& b, U16& a, \
5294 U16& dr, U16& dg, U16& db, U16& da); \
5295 static void ABI name(SkRasterPipelineStage* program, \
5296 size_t dx, size_t dy, \
5297 U16 r, U16 g, U16 b, U16 a, \
5298 U16 dr, U16 dg, U16 db, U16 da) { \
5299 name##_k(Ctx{program}, dx,dy, r,g,b,a, dr,dg,db,da); \
5300 auto fn = (Stage)(++program)->fn; \
5301 fn(program, dx,dy, r,g,b,a, dr,dg,db,da); \
5302 } \
5303 SI void name##_k(ARG, size_t dx, size_t dy, \
5304 U16& r, U16& g, U16& b, U16& a, \
5305 U16& dr, U16& dg, U16& db, U16& da)
5306 #endif
5307
5308 // ~~~~~~ Commonly used helper functions ~~~~~~ //
5309
5310 /**
5311 * Helpers to to properly rounded division (by 255). The ideal answer we want to compute is slow,
5312 * thanks to a division by a non-power of two:
5313 * [1] (v + 127) / 255
5314 *
5315 * There is a two-step process that computes the correct answer for all inputs:
5316 * [2] (v + 128 + ((v + 128) >> 8)) >> 8
5317 *
5318 * There is also a single iteration approximation, but it's wrong (+-1) ~25% of the time:
5319 * [3] (v + 255) >> 8;
5320 *
5321 * We offer two different implementations here, depending on the requirements of the calling stage.
5322 */
5323
5324 /**
5325 * div255 favors speed over accuracy. It uses formula [2] on NEON (where we can compute it as fast
5326 * as [3]), and uses [3] elsewhere.
5327 */
5328 SI U16 div255(U16 v) {
5329 #if defined(SKRP_CPU_NEON)
5330 // With NEON we can compute [2] just as fast as [3], so let's be correct.
5331 // First we compute v + ((v+128)>>8), then one more round of (...+128)>>8 to finish up:
5332 return vrshrq_n_u16(vrsraq_n_u16(v, v, 8), 8);
5333 #else
5334 // Otherwise, use [3], which is never wrong by more than 1:
5335 return (v+255)/256;
5336 #endif
5337 }
5338
5339 /**
5340 * div255_accurate guarantees the right answer on all platforms, at the expense of performance.
5341 */
5342 SI U16 div255_accurate(U16 v) {
5343 #if defined(SKRP_CPU_NEON)
5344 // Our NEON implementation of div255 is already correct for all inputs:
5345 return div255(v);
5346 #else
5347 // This is [2] (the same formulation as NEON), but written without the benefit of intrinsics:
5348 v += 128;
5349 return (v+(v/256))/256;
5350 #endif
5351 }
5352
5353 SI U16 inv(U16 v) { return 255-v; }
5354
5355 SI U16 if_then_else(I16 c, U16 t, U16 e) {
5356 return (t & sk_bit_cast<U16>(c)) | (e & sk_bit_cast<U16>(~c));
5357 }
5358 SI U32 if_then_else(I32 c, U32 t, U32 e) {
5359 return (t & sk_bit_cast<U32>(c)) | (e & sk_bit_cast<U32>(~c));
5360 }
5361
5362 SI U16 max(U16 x, U16 y) { return if_then_else(x < y, y, x); }
5363 SI U16 min(U16 x, U16 y) { return if_then_else(x < y, x, y); }
5364
5365 SI U16 max(U16 a, uint16_t b) { return max( a , U16_(b)); }
5366 SI U16 max(uint16_t a, U16 b) { return max(U16_(a), b ); }
5367 SI U16 min(U16 a, uint16_t b) { return min( a , U16_(b)); }
5368 SI U16 min(uint16_t a, U16 b) { return min(U16_(a), b ); }
5369
5370 SI U16 from_float(float f) { return U16_(f * 255.0f + 0.5f); }
5371
5372 SI U16 lerp(U16 from, U16 to, U16 t) { return div255( from*inv(t) + to*t ); }
5373
5374 template <typename D, typename S>
5375 SI D cast(S src) {
5376 return __builtin_convertvector(src, D);
5377 }
5378
5379 template <typename D, typename S>
5380 SI void split(S v, D* lo, D* hi) {
5381 static_assert(2*sizeof(D) == sizeof(S), "");
5382 memcpy(lo, (const char*)&v + 0*sizeof(D), sizeof(D));
5383 memcpy(hi, (const char*)&v + 1*sizeof(D), sizeof(D));
5384 }
5385 template <typename D, typename S>
5386 SI D join(S lo, S hi) {
5387 static_assert(sizeof(D) == 2*sizeof(S), "");
5388 D v;
5389 memcpy((char*)&v + 0*sizeof(S), &lo, sizeof(S));
5390 memcpy((char*)&v + 1*sizeof(S), &hi, sizeof(S));
5391 return v;
5392 }
5393
5394 SI F if_then_else(I32 c, F t, F e) {
5395 return sk_bit_cast<F>( (sk_bit_cast<I32>(t) & c) | (sk_bit_cast<I32>(e) & ~c) );
5396 }
5397 SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
5398 SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
5399
5400 SI F max(F x, F y) { return if_then_else(x < y, y, x); }
5401 SI F min(F x, F y) { return if_then_else(x < y, x, y); }
5402
5403 SI F max(F a, float b) { return max( a , F_(b)); }
5404 SI F max(float a, F b) { return max(F_(a), b ); }
5405 SI F min(F a, float b) { return min( a , F_(b)); }
5406 SI F min(float a, F b) { return min(F_(a), b ); }
5407
5408 SI I32 if_then_else(I32 c, I32 t, I32 e) {
5409 return (t & c) | (e & ~c);
5410 }
5411 SI I32 max(I32 x, I32 y) { return if_then_else(x < y, y, x); }
5412 SI I32 min(I32 x, I32 y) { return if_then_else(x < y, x, y); }
5413
5414 SI I32 max(I32 a, int32_t b) { return max( a , I32_(b)); }
5415 SI I32 max(int32_t a, I32 b) { return max(I32_(a), b ); }
5416 SI I32 min(I32 a, int32_t b) { return min( a , I32_(b)); }
5417 SI I32 min(int32_t a, I32 b) { return min(I32_(a), b ); }
5418
5419 SI F mad(F f, F m, F a) { return a+f*m; }
5420 SI F mad(F f, F m, float a) { return mad( f , m , F_(a)); }
5421 SI F mad(F f, float m, F a) { return mad( f , F_(m), a ); }
5422 SI F mad(F f, float m, float a) { return mad( f , F_(m), F_(a)); }
5423 SI F mad(float f, F m, F a) { return mad(F_(f), m , a ); }
5424 SI F mad(float f, F m, float a) { return mad(F_(f), m , F_(a)); }
5425 SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a ); }
5426
5427 SI F nmad(F f, F m, F a) { return a-f*m; }
5428 SI F nmad(F f, F m, float a) { return nmad( f , m , F_(a)); }
5429 SI F nmad(F f, float m, F a) { return nmad( f , F_(m), a ); }
5430 SI F nmad(F f, float m, float a) { return nmad( f , F_(m), F_(a)); }
5431 SI F nmad(float f, F m, F a) { return nmad(F_(f), m , a ); }
5432 SI F nmad(float f, F m, float a) { return nmad(F_(f), m , F_(a)); }
5433 SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a ); }
5434
5435 SI U32 trunc_(F x) { return (U32)cast<I32>(x); }
5436
5437 // Use approximate instructions and one Newton-Raphson step to calculate 1/x.
5438 SI F rcp_precise(F x) {
5439 #if defined(SKRP_CPU_SKX)
5440 F e = _mm512_rcp14_ps(x);
5441 return _mm512_fnmadd_ps(x, e, _mm512_set1_ps(2.0f)) * e;
5442 #elif defined(SKRP_CPU_HSW)
5443 __m256 lo,hi;
5444 split(x, &lo,&hi);
5445 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5446 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5447 __m128 lo,hi;
5448 split(x, &lo,&hi);
5449 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5450 #elif defined(SKRP_CPU_NEON)
5451 float32x4_t lo,hi;
5452 split(x, &lo,&hi);
5453 return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
5454 #elif defined(SKRP_CPU_LASX)
5455 __m256 lo,hi;
5456 split(x, &lo,&hi);
5457 return join<F>(__lasx_xvfrecip_s(lo), __lasx_xvfrecip_s(hi));
5458 #elif defined(SKRP_CPU_LSX)
5459 __m128 lo,hi;
5460 split(x, &lo,&hi);
5461 return join<F>(__lsx_vfrecip_s(lo), __lsx_vfrecip_s(hi));
5462 #else
5463 return 1.0f / x;
5464 #endif
5465 }
5466 SI F sqrt_(F x) {
5467 #if defined(SKRP_CPU_SKX)
5468 return _mm512_sqrt_ps(x);
5469 #elif defined(SKRP_CPU_HSW)
5470 __m256 lo,hi;
5471 split(x, &lo,&hi);
5472 return join<F>(_mm256_sqrt_ps(lo), _mm256_sqrt_ps(hi));
5473 #elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5474 __m128 lo,hi;
5475 split(x, &lo,&hi);
5476 return join<F>(_mm_sqrt_ps(lo), _mm_sqrt_ps(hi));
5477 #elif defined(SK_CPU_ARM64)
5478 float32x4_t lo,hi;
5479 split(x, &lo,&hi);
5480 return join<F>(vsqrtq_f32(lo), vsqrtq_f32(hi));
5481 #elif defined(SKRP_CPU_NEON)
5482 auto sqrt = [](float32x4_t v) {
5483 auto est = vrsqrteq_f32(v); // Estimate and two refinement steps for est = rsqrt(v).
5484 est *= vrsqrtsq_f32(v,est*est);
5485 est *= vrsqrtsq_f32(v,est*est);
5486 return v*est; // sqrt(v) == v*rsqrt(v).
5487 };
5488 float32x4_t lo,hi;
5489 split(x, &lo,&hi);
5490 return join<F>(sqrt(lo), sqrt(hi));
5491 #elif defined(SKRP_CPU_LASX)
5492 __m256 lo,hi;
5493 split(x, &lo,&hi);
5494 return join<F>(__lasx_xvfsqrt_s(lo), __lasx_xvfsqrt_s(hi));
5495 #elif defined(SKRP_CPU_LSX)
5496 __m128 lo,hi;
5497 split(x, &lo,&hi);
5498 return join<F>(__lsx_vfsqrt_s(lo), __lsx_vfsqrt_s(hi));
5499 #else
5500 return F{
5501 sqrtf(x[0]), sqrtf(x[1]), sqrtf(x[2]), sqrtf(x[3]),
5502 sqrtf(x[4]), sqrtf(x[5]), sqrtf(x[6]), sqrtf(x[7]),
5503 };
5504 #endif
5505 }
5506
5507 SI F floor_(F x) {
5508 #if defined(SK_CPU_ARM64)
5509 float32x4_t lo,hi;
5510 split(x, &lo,&hi);
5511 return join<F>(vrndmq_f32(lo), vrndmq_f32(hi));
5512 #elif defined(SKRP_CPU_SKX)
5513 return _mm512_floor_ps(x);
5514 #elif defined(SKRP_CPU_HSW)
5515 __m256 lo,hi;
5516 split(x, &lo,&hi);
5517 return join<F>(_mm256_floor_ps(lo), _mm256_floor_ps(hi));
5518 #elif defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5519 __m128 lo,hi;
5520 split(x, &lo,&hi);
5521 return join<F>(_mm_floor_ps(lo), _mm_floor_ps(hi));
5522 #elif defined(SKRP_CPU_LASX)
5523 __m256 lo,hi;
5524 split(x, &lo,&hi);
5525 return join<F>(__lasx_xvfrintrm_s(lo), __lasx_xvfrintrm_s(hi));
5526 #elif defined(SKRP_CPU_LSX)
5527 __m128 lo,hi;
5528 split(x, &lo,&hi);
5529 return join<F>(__lsx_vfrintrm_s(lo), __lsx_vfrintrm_s(hi));
5530 #else
5531 F roundtrip = cast<F>(cast<I32>(x));
5532 return roundtrip - if_then_else(roundtrip > x, F_(1), F_(0));
5533 #endif
5534 }
5535
5536 // scaled_mult interprets a and b as number on [-1, 1) which are numbers in Q15 format. Functionally
5537 // this multiply is:
5538 // (2 * a * b + (1 << 15)) >> 16
5539 // The result is a number on [-1, 1).
5540 // Note: on neon this is a saturating multiply while the others are not.
5541 SI I16 scaled_mult(I16 a, I16 b) {
5542 #if defined(SKRP_CPU_SKX)
5543 return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
5544 #elif defined(SKRP_CPU_HSW)
5545 return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
5546 #elif defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
5547 return (I16)_mm_mulhrs_epi16((__m128i)a, (__m128i)b);
5548 #elif defined(SK_CPU_ARM64)
5549 return vqrdmulhq_s16(a, b);
5550 #elif defined(SKRP_CPU_NEON)
5551 return vqrdmulhq_s16(a, b);
5552 #elif defined(SKRP_CPU_LASX)
5553 I16 res = __lasx_xvmuh_h(a, b);
5554 return __lasx_xvslli_h(res, 1);
5555 #elif defined(SKRP_CPU_LSX)
5556 I16 res = __lsx_vmuh_h(a, b);
5557 return __lsx_vslli_h(res, 1);
5558 #else
5559 const I32 roundingTerm = I32_(1 << 14);
5560 return cast<I16>((cast<I32>(a) * cast<I32>(b) + roundingTerm) >> 15);
5561 #endif
5562 }
5563
5564 // This sum is to support lerp where the result will always be a positive number. In general,
5565 // a sum like this would require an additional bit, but because we know the range of the result
5566 // we know that the extra bit will always be zero.
5567 SI U16 constrained_add(I16 a, U16 b) {
5568 #if defined(SK_DEBUG)
5569 for (size_t i = 0; i < N; i++) {
5570 // Ensure that a + b is on the interval [0, UINT16_MAX]
5571 int ia = a[i],
5572 ib = b[i];
5573 // Use 65535 here because fuchsia's compiler evaluates UINT16_MAX - ib, which is
5574 // 65536U - ib, as an uint32_t instead of an int32_t. This was forcing ia to be
5575 // interpreted as an uint32_t.
5576 SkASSERT(-ib <= ia && ia <= 65535 - ib);
5577 }
5578 #endif
5579 return b + sk_bit_cast<U16>(a);
5580 }
5581
5582 SI F fract(F x) { return x - floor_(x); }
5583 SI F abs_(F x) { return sk_bit_cast<F>( sk_bit_cast<I32>(x) & 0x7fffffff ); }
5584
5585 // ~~~~~~ Basic / misc. stages ~~~~~~ //
5586
5587 STAGE_GG(seed_shader, NoCtx) {
5588 #if defined(SKRP_CPU_LSX)
5589 __m128 val1 = {0.5f, 1.5f, 2.5f, 3.5f};
5590 __m128 val2 = {4.5f, 5.5f, 6.5f, 7.5f};
5591 __m128 val3 = {0.5f, 0.5f, 0.5f, 0.5f};
5592
5593 __m128i v_d = __lsx_vreplgr2vr_w(dx);
5594
5595 __m128 f_d = __lsx_vffint_s_w(v_d);
5596 val1 = __lsx_vfadd_s(val1, f_d);
5597 val2 = __lsx_vfadd_s(val2, f_d);
5598 x = join<F>(val1, val2);
5599
5600 v_d = __lsx_vreplgr2vr_w(dy);
5601 f_d = __lsx_vffint_s_w(v_d);
5602 val3 = __lsx_vfadd_s(val3, f_d);
5603 y = join<F>(val3, val3);
5604 #else
5605 static constexpr float iota[] = {
5606 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f,
5607 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
5608 };
5609 static_assert(std::size(iota) >= SkRasterPipeline_kMaxStride);
5610
5611 x = cast<F>(I32_(dx)) + sk_unaligned_load<F>(iota);
5612 y = cast<F>(I32_(dy)) + 0.5f;
5613 #endif
5614 }
5615
5616 STAGE_GG(matrix_translate, const float* m) {
5617 x += m[0];
5618 y += m[1];
5619 }
5620 STAGE_GG(matrix_scale_translate, const float* m) {
5621 x = mad(x,m[0], m[2]);
5622 y = mad(y,m[1], m[3]);
5623 }
5624 STAGE_GG(matrix_2x3, const float* m) {
5625 auto X = mad(x,m[0], mad(y,m[1], m[2])),
5626 Y = mad(x,m[3], mad(y,m[4], m[5]));
5627 x = X;
5628 y = Y;
5629 }
5630 STAGE_GG(matrix_perspective, const float* m) {
5631 // N.B. Unlike the other matrix_ stages, this matrix is row-major.
5632 auto X = mad(x,m[0], mad(y,m[1], m[2])),
5633 Y = mad(x,m[3], mad(y,m[4], m[5])),
5634 Z = mad(x,m[6], mad(y,m[7], m[8]));
5635 x = X * rcp_precise(Z);
5636 y = Y * rcp_precise(Z);
5637 }
5638
5639 STAGE_PP(uniform_color, const SkRasterPipeline_UniformColorCtx* c) {
5640 r = U16_(c->rgba[0]);
5641 g = U16_(c->rgba[1]);
5642 b = U16_(c->rgba[2]);
5643 a = U16_(c->rgba[3]);
5644 }
5645 STAGE_PP(uniform_color_dst, const SkRasterPipeline_UniformColorCtx* c) {
5646 dr = U16_(c->rgba[0]);
5647 dg = U16_(c->rgba[1]);
5648 db = U16_(c->rgba[2]);
5649 da = U16_(c->rgba[3]);
5650 }
5651 STAGE_PP(black_color, NoCtx) { r = g = b = U16_0; a = U16_255; }
5652 STAGE_PP(white_color, NoCtx) { r = g = b = U16_255; a = U16_255; }
5653
5654 STAGE_PP(set_rgb, const float rgb[3]) {
5655 r = from_float(rgb[0]);
5656 g = from_float(rgb[1]);
5657 b = from_float(rgb[2]);
5658 }
5659
5660 // No need to clamp against 0 here (values are unsigned)
5661 STAGE_PP(clamp_01, NoCtx) {
5662 r = min(r, 255);
5663 g = min(g, 255);
5664 b = min(b, 255);
5665 a = min(a, 255);
5666 }
5667
5668 STAGE_PP(clamp_a_01, NoCtx) {
5669 a = min(a, 255);
5670 }
5671
5672 STAGE_PP(clamp_gamut, NoCtx) {
5673 a = min(a, 255);
5674 r = min(r, a);
5675 g = min(g, a);
5676 b = min(b, a);
5677 }
5678
5679 STAGE_PP(premul, NoCtx) {
5680 r = div255_accurate(r * a);
5681 g = div255_accurate(g * a);
5682 b = div255_accurate(b * a);
5683 }
5684 STAGE_PP(premul_dst, NoCtx) {
5685 dr = div255_accurate(dr * da);
5686 dg = div255_accurate(dg * da);
5687 db = div255_accurate(db * da);
5688 }
5689
5690 STAGE_PP(force_opaque , NoCtx) { a = U16_255; }
5691 STAGE_PP(force_opaque_dst, NoCtx) { da = U16_255; }
5692
5693 STAGE_PP(swap_rb, NoCtx) {
5694 auto tmp = r;
5695 r = b;
5696 b = tmp;
5697 }
5698 STAGE_PP(swap_rb_dst, NoCtx) {
5699 auto tmp = dr;
5700 dr = db;
5701 db = tmp;
5702 }
5703
5704 STAGE_PP(move_src_dst, NoCtx) {
5705 dr = r;
5706 dg = g;
5707 db = b;
5708 da = a;
5709 }
5710
5711 STAGE_PP(move_dst_src, NoCtx) {
5712 r = dr;
5713 g = dg;
5714 b = db;
5715 a = da;
5716 }
5717
5718 STAGE_PP(swap_src_dst, NoCtx) {
5719 std::swap(r, dr);
5720 std::swap(g, dg);
5721 std::swap(b, db);
5722 std::swap(a, da);
5723 }
5724
5725 // ~~~~~~ Blend modes ~~~~~~ //
5726
5727 // The same logic applied to all 4 channels.
5728 #define BLEND_MODE(name) \
5729 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
5730 STAGE_PP(name, NoCtx) { \
5731 r = name##_channel(r,dr,a,da); \
5732 g = name##_channel(g,dg,a,da); \
5733 b = name##_channel(b,db,a,da); \
5734 a = name##_channel(a,da,a,da); \
5735 } \
5736 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
5737
5738 #if defined(SK_USE_INACCURATE_DIV255_IN_BLEND)
5739 BLEND_MODE(clear) { return U16_0; }
5740 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
5741 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
5742 BLEND_MODE(srcin) { return div255( s*da ); }
5743 BLEND_MODE(dstin) { return div255( d*sa ); }
5744 BLEND_MODE(srcout) { return div255( s*inv(da) ); }
5745 BLEND_MODE(dstout) { return div255( d*inv(sa) ); }
5746 BLEND_MODE(srcover) { return s + div255( d*inv(sa) ); }
5747 BLEND_MODE(dstover) { return d + div255( s*inv(da) ); }
5748 BLEND_MODE(modulate) { return div255( s*d ); }
5749 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
5750 BLEND_MODE(plus_) { return min(s+d, 255); }
5751 BLEND_MODE(screen) { return s + d - div255( s*d ); }
5752 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
5753 #else
5754 BLEND_MODE(clear) { return U16_0; }
5755 BLEND_MODE(srcatop) { return div255( s*da + d*inv(sa) ); }
5756 BLEND_MODE(dstatop) { return div255( d*sa + s*inv(da) ); }
5757 BLEND_MODE(srcin) { return div255_accurate( s*da ); }
5758 BLEND_MODE(dstin) { return div255_accurate( d*sa ); }
5759 BLEND_MODE(srcout) { return div255_accurate( s*inv(da) ); }
5760 BLEND_MODE(dstout) { return div255_accurate( d*inv(sa) ); }
5761 BLEND_MODE(srcover) { return s + div255_accurate( d*inv(sa) ); }
5762 BLEND_MODE(dstover) { return d + div255_accurate( s*inv(da) ); }
5763 BLEND_MODE(modulate) { return div255_accurate( s*d ); }
5764 BLEND_MODE(multiply) { return div255( s*inv(da) + d*inv(sa) + s*d ); }
5765 BLEND_MODE(plus_) { return min(s+d, 255); }
5766 BLEND_MODE(screen) { return s + d - div255_accurate( s*d ); }
5767 BLEND_MODE(xor_) { return div255( s*inv(da) + d*inv(sa) ); }
5768 #endif
5769 #undef BLEND_MODE
5770
5771 // The same logic applied to color, and srcover for alpha.
5772 #define BLEND_MODE(name) \
5773 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
5774 STAGE_PP(name, NoCtx) { \
5775 r = name##_channel(r,dr,a,da); \
5776 g = name##_channel(g,dg,a,da); \
5777 b = name##_channel(b,db,a,da); \
5778 a = a + div255( da*inv(a) ); \
5779 } \
5780 SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
5781
5782 BLEND_MODE(darken) { return s + d - div255( max(s*da, d*sa) ); }
5783 BLEND_MODE(lighten) { return s + d - div255( min(s*da, d*sa) ); }
5784 BLEND_MODE(difference) { return s + d - 2*div255( min(s*da, d*sa) ); }
5785 BLEND_MODE(exclusion) { return s + d - 2*div255( s*d ); }
5786
5787 BLEND_MODE(hardlight) {
5788 return div255( s*inv(da) + d*inv(sa) +
5789 if_then_else(2*s <= sa, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
5790 }
5791 BLEND_MODE(overlay) {
5792 return div255( s*inv(da) + d*inv(sa) +
5793 if_then_else(2*d <= da, 2*s*d, sa*da - 2*(sa-s)*(da-d)) );
5794 }
5795 #undef BLEND_MODE
5796
5797 // ~~~~~~ Helpers for interacting with memory ~~~~~~ //
5798
5799 template <typename T>
5800 SI T* ptr_at_xy(const SkRasterPipeline_MemoryCtx* ctx, size_t dx, size_t dy) {
5801 return (T*)ctx->pixels + dy*ctx->stride + dx;
5802 }
5803
5804 template <typename T>
5805 SI U32 ix_and_ptr(T** ptr, const SkRasterPipeline_GatherCtx* ctx, F x, F y) {
5806 // Exclusive -> inclusive.
5807 const F w = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->width ) - 1)),
5808 h = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->height) - 1));
5809
5810 const F z = F_(std::numeric_limits<float>::min());
5811
5812 x = min(max(z, x), w);
5813 y = min(max(z, y), h);
5814
5815 x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
5816 y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
5817
5818 *ptr = (const T*)ctx->pixels;
5819 return trunc_(y)*ctx->stride + trunc_(x);
5820 }
5821
5822 template <typename T>
5823 SI U32 ix_and_ptr(T** ptr, const SkRasterPipeline_GatherCtx* ctx, I32 x, I32 y) {
5824 // This flag doesn't make sense when the coords are integers.
5825 SkASSERT(ctx->roundDownAtInteger == 0);
5826 // Exclusive -> inclusive.
5827 const I32 w = I32_( ctx->width - 1),
5828 h = I32_(ctx->height - 1);
5829
5830 U32 ax = cast<U32>(min(max(0, x), w)),
5831 ay = cast<U32>(min(max(0, y), h));
5832
5833 *ptr = (const T*)ctx->pixels;
5834 return ay * ctx->stride + ax;
5835 }
5836
5837 template <typename V, typename T>
5838 SI V load(const T* ptr) {
5839 V v;
5840 memcpy(&v, ptr, sizeof(v));
5841 return v;
5842 }
5843 template <typename V, typename T>
5844 SI void store(T* ptr, V v) {
5845 memcpy(ptr, &v, sizeof(v));
5846 }
5847
5848 #if defined(SKRP_CPU_SKX)
5849 template <typename V, typename T>
5850 SI V gather(const T* ptr, U32 ix) {
5851 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5852 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
5853 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
5854 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
5855 }
5856
5857 template<>
5858 F gather(const float* ptr, U32 ix) {
5859 return _mm512_i32gather_ps((__m512i)ix, ptr, 4);
5860 }
5861
5862 template<>
5863 U32 gather(const uint32_t* ptr, U32 ix) {
5864 return (U32)_mm512_i32gather_epi32((__m512i)ix, ptr, 4);
5865 }
5866
5867 #elif defined(SKRP_CPU_HSW)
5868 template <typename V, typename T>
5869 SI V gather(const T* ptr, U32 ix) {
5870 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5871 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
5872 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
5873 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
5874 }
5875
5876 template<>
5877 F gather(const float* ptr, U32 ix) {
5878 __m256i lo, hi;
5879 split(ix, &lo, &hi);
5880
5881 return join<F>(_mm256_i32gather_ps(ptr, lo, 4),
5882 _mm256_i32gather_ps(ptr, hi, 4));
5883 }
5884
5885 template<>
5886 U32 gather(const uint32_t* ptr, U32 ix) {
5887 __m256i lo, hi;
5888 split(ix, &lo, &hi);
5889
5890 return join<U32>(_mm256_i32gather_epi32((const int*)ptr, lo, 4),
5891 _mm256_i32gather_epi32((const int*)ptr, hi, 4));
5892 }
5893 #elif defined(SKRP_CPU_LASX)
5894 template <typename V, typename T>
5895 SI V gather(const T* ptr, U32 ix) {
5896 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5897 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]],
5898 ptr[ix[ 8]], ptr[ix[ 9]], ptr[ix[10]], ptr[ix[11]],
5899 ptr[ix[12]], ptr[ix[13]], ptr[ix[14]], ptr[ix[15]], };
5900 }
5901 #else
5902 template <typename V, typename T>
5903 SI V gather(const T* ptr, U32 ix) {
5904 return V{ ptr[ix[ 0]], ptr[ix[ 1]], ptr[ix[ 2]], ptr[ix[ 3]],
5905 ptr[ix[ 4]], ptr[ix[ 5]], ptr[ix[ 6]], ptr[ix[ 7]], };
5906 }
5907 #endif
5908
5909
5910 // ~~~~~~ 32-bit memory loads and stores ~~~~~~ //
5911
5912 SI void from_8888(U32 rgba, U16* r, U16* g, U16* b, U16* a) {
5913 #if defined(SKRP_CPU_SKX)
5914 rgba = (U32)_mm512_permutexvar_epi64(_mm512_setr_epi64(0,1,4,5,2,3,6,7), (__m512i)rgba);
5915 auto cast_U16 = [](U32 v) -> U16 {
5916 return (U16)_mm256_packus_epi32(_mm512_castsi512_si256((__m512i)v),
5917 _mm512_extracti64x4_epi64((__m512i)v, 1));
5918 };
5919 #elif defined(SKRP_CPU_HSW)
5920 // Swap the middle 128-bit lanes to make _mm256_packus_epi32() in cast_U16() work out nicely.
5921 __m256i _01,_23;
5922 split(rgba, &_01, &_23);
5923 __m256i _02 = _mm256_permute2x128_si256(_01,_23, 0x20),
5924 _13 = _mm256_permute2x128_si256(_01,_23, 0x31);
5925 rgba = join<U32>(_02, _13);
5926
5927 auto cast_U16 = [](U32 v) -> U16 {
5928 __m256i _02,_13;
5929 split(v, &_02,&_13);
5930 return (U16)_mm256_packus_epi32(_02,_13);
5931 };
5932 #elif defined(SKRP_CPU_LASX)
5933 __m256i _01, _23;
5934 split(rgba, &_01, &_23);
5935 __m256i _02 = __lasx_xvpermi_q(_01, _23, 0x02),
5936 _13 = __lasx_xvpermi_q(_01, _23, 0x13);
5937 rgba = join<U32>(_02, _13);
5938
5939 auto cast_U16 = [](U32 v) -> U16 {
5940 __m256i _02,_13;
5941 split(v, &_02,&_13);
5942 __m256i tmp0 = __lasx_xvsat_wu(_02, 15);
5943 __m256i tmp1 = __lasx_xvsat_wu(_13, 15);
5944 return __lasx_xvpickev_h(tmp1, tmp0);
5945 };
5946 #elif defined(SKRP_CPU_LSX)
5947 __m128i _01, _23, rg, ba;
5948 split(rgba, &_01, &_23);
5949 rg = __lsx_vpickev_h(_23, _01);
5950 ba = __lsx_vpickod_h(_23, _01);
5951
5952 __m128i mask_00ff = __lsx_vreplgr2vr_h(0xff);
5953
5954 *r = __lsx_vand_v(rg, mask_00ff);
5955 *g = __lsx_vsrli_h(rg, 8);
5956 *b = __lsx_vand_v(ba, mask_00ff);
5957 *a = __lsx_vsrli_h(ba, 8);
5958 #else
5959 auto cast_U16 = [](U32 v) -> U16 {
5960 return cast<U16>(v);
5961 };
5962 #endif
5963 #if !defined(SKRP_CPU_LSX)
5964 *r = cast_U16(rgba & 65535) & 255;
5965 *g = cast_U16(rgba & 65535) >> 8;
5966 *b = cast_U16(rgba >> 16) & 255;
5967 *a = cast_U16(rgba >> 16) >> 8;
5968 #endif
5969 }
5970
5971 SI void load_8888_(const uint32_t* ptr, U16* r, U16* g, U16* b, U16* a) {
5972 #if 1 && defined(SKRP_CPU_NEON)
5973 uint8x8x4_t rgba = vld4_u8((const uint8_t*)(ptr));
5974 *r = cast<U16>(rgba.val[0]);
5975 *g = cast<U16>(rgba.val[1]);
5976 *b = cast<U16>(rgba.val[2]);
5977 *a = cast<U16>(rgba.val[3]);
5978 #else
5979 from_8888(load<U32>(ptr), r,g,b,a);
5980 #endif
5981 }
5982 SI void store_8888_(uint32_t* ptr, U16 r, U16 g, U16 b, U16 a) {
5983 #if defined(SKRP_CPU_LSX)
5984 __m128i mask = __lsx_vreplgr2vr_h(255);
5985 r = __lsx_vmin_hu(r, mask);
5986 g = __lsx_vmin_hu(g, mask);
5987 b = __lsx_vmin_hu(b, mask);
5988 a = __lsx_vmin_hu(a, mask);
5989
5990 g = __lsx_vslli_h(g, 8);
5991 r = r | g;
5992 a = __lsx_vslli_h(a, 8);
5993 a = a | b;
5994
5995 __m128i r_lo = __lsx_vsllwil_wu_hu(r, 0);
5996 __m128i r_hi = __lsx_vexth_wu_hu(r);
5997 __m128i a_lo = __lsx_vsllwil_wu_hu(a, 0);
5998 __m128i a_hi = __lsx_vexth_wu_hu(a);
5999
6000 a_lo = __lsx_vslli_w(a_lo, 16);
6001 a_hi = __lsx_vslli_w(a_hi, 16);
6002
6003 r = r_lo | a_lo;
6004 a = r_hi | a_hi;
6005 store(ptr, join<U32>(r, a));
6006 #else
6007 r = min(r, 255);
6008 g = min(g, 255);
6009 b = min(b, 255);
6010 a = min(a, 255);
6011
6012 #if 1 && defined(SKRP_CPU_NEON)
6013 uint8x8x4_t rgba = {{
6014 cast<U8>(r),
6015 cast<U8>(g),
6016 cast<U8>(b),
6017 cast<U8>(a),
6018 }};
6019 vst4_u8((uint8_t*)(ptr), rgba);
6020 #else
6021 store(ptr, cast<U32>(r | (g<<8)) << 0
6022 | cast<U32>(b | (a<<8)) << 16);
6023 #endif
6024 #endif
6025 }
6026
6027 STAGE_PP(load_8888, const SkRasterPipeline_MemoryCtx* ctx) {
6028 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), &r,&g,&b,&a);
6029 }
6030 STAGE_PP(load_8888_dst, const SkRasterPipeline_MemoryCtx* ctx) {
6031 load_8888_(ptr_at_xy<const uint32_t>(ctx, dx,dy), &dr,&dg,&db,&da);
6032 }
6033 STAGE_PP(store_8888, const SkRasterPipeline_MemoryCtx* ctx) {
6034 store_8888_(ptr_at_xy<uint32_t>(ctx, dx,dy), r,g,b,a);
6035 }
6036 STAGE_GP(gather_8888, const SkRasterPipeline_GatherCtx* ctx) {
6037 const uint32_t* ptr;
6038 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6039 from_8888(gather<U32>(ptr, ix), &r, &g, &b, &a);
6040 }
6041
6042 // ~~~~~~ 16-bit memory loads and stores ~~~~~~ //
6043
6044 SI void from_565(U16 rgb, U16* r, U16* g, U16* b) {
6045 // Format for 565 buffers: 15|rrrrr gggggg bbbbb|0
6046 U16 R = (rgb >> 11) & 31,
6047 G = (rgb >> 5) & 63,
6048 B = (rgb >> 0) & 31;
6049
6050 // These bit replications are the same as multiplying by 255/31 or 255/63 to scale to 8-bit.
6051 *r = (R << 3) | (R >> 2);
6052 *g = (G << 2) | (G >> 4);
6053 *b = (B << 3) | (B >> 2);
6054 }
6055 SI void load_565_(const uint16_t* ptr, U16* r, U16* g, U16* b) {
6056 from_565(load<U16>(ptr), r,g,b);
6057 }
6058 SI void store_565_(uint16_t* ptr, U16 r, U16 g, U16 b) {
6059 r = min(r, 255);
6060 g = min(g, 255);
6061 b = min(b, 255);
6062
6063 // Round from [0,255] to [0,31] or [0,63], as if x * (31/255.0f) + 0.5f.
6064 // (Don't feel like you need to find some fundamental truth in these...
6065 // they were brute-force searched.)
6066 U16 R = (r * 9 + 36) / 74, // 9/74 ≈ 31/255, plus 36/74, about half.
6067 G = (g * 21 + 42) / 85, // 21/85 = 63/255 exactly.
6068 B = (b * 9 + 36) / 74;
6069 // Pack them back into 15|rrrrr gggggg bbbbb|0.
6070 store(ptr, R << 11
6071 | G << 5
6072 | B << 0);
6073 }
6074
6075 STAGE_PP(load_565, const SkRasterPipeline_MemoryCtx* ctx) {
6076 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &r,&g,&b);
6077 a = U16_255;
6078 }
6079 STAGE_PP(load_565_dst, const SkRasterPipeline_MemoryCtx* ctx) {
6080 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &dr,&dg,&db);
6081 da = U16_255;
6082 }
6083 STAGE_PP(store_565, const SkRasterPipeline_MemoryCtx* ctx) {
6084 store_565_(ptr_at_xy<uint16_t>(ctx, dx,dy), r,g,b);
6085 }
6086 STAGE_GP(gather_565, const SkRasterPipeline_GatherCtx* ctx) {
6087 const uint16_t* ptr;
6088 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6089 from_565(gather<U16>(ptr, ix), &r, &g, &b);
6090 a = U16_255;
6091 }
6092
6093 SI void from_4444(U16 rgba, U16* r, U16* g, U16* b, U16* a) {
6094 // Format for 4444 buffers: 15|rrrr gggg bbbb aaaa|0.
6095 U16 R = (rgba >> 12) & 15,
6096 G = (rgba >> 8) & 15,
6097 B = (rgba >> 4) & 15,
6098 A = (rgba >> 0) & 15;
6099
6100 // Scale [0,15] to [0,255].
6101 *r = (R << 4) | R;
6102 *g = (G << 4) | G;
6103 *b = (B << 4) | B;
6104 *a = (A << 4) | A;
6105 }
6106 SI void load_4444_(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
6107 from_4444(load<U16>(ptr), r,g,b,a);
6108 }
6109 SI void store_4444_(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
6110 r = min(r, 255);
6111 g = min(g, 255);
6112 b = min(b, 255);
6113 a = min(a, 255);
6114
6115 // Round from [0,255] to [0,15], producing the same value as (x*(15/255.0f) + 0.5f).
6116 U16 R = (r + 8) / 17,
6117 G = (g + 8) / 17,
6118 B = (b + 8) / 17,
6119 A = (a + 8) / 17;
6120 // Pack them back into 15|rrrr gggg bbbb aaaa|0.
6121 store(ptr, R << 12
6122 | G << 8
6123 | B << 4
6124 | A << 0);
6125 }
6126
6127 STAGE_PP(load_4444, const SkRasterPipeline_MemoryCtx* ctx) {
6128 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &r,&g,&b,&a);
6129 }
6130 STAGE_PP(load_4444_dst, const SkRasterPipeline_MemoryCtx* ctx) {
6131 load_4444_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &dr,&dg,&db,&da);
6132 }
6133 STAGE_PP(store_4444, const SkRasterPipeline_MemoryCtx* ctx) {
6134 store_4444_(ptr_at_xy<uint16_t>(ctx, dx,dy), r,g,b,a);
6135 }
6136 STAGE_GP(gather_4444, const SkRasterPipeline_GatherCtx* ctx) {
6137 const uint16_t* ptr;
6138 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6139 from_4444(gather<U16>(ptr, ix), &r,&g,&b,&a);
6140 }
6141
6142 SI void from_88(U16 rg, U16* r, U16* g) {
6143 *r = (rg & 0xFF);
6144 *g = (rg >> 8);
6145 }
6146
6147 SI void load_88_(const uint16_t* ptr, U16* r, U16* g) {
6148 #if 1 && defined(SKRP_CPU_NEON)
6149 uint8x8x2_t rg = vld2_u8((const uint8_t*)(ptr));
6150 *r = cast<U16>(rg.val[0]);
6151 *g = cast<U16>(rg.val[1]);
6152 #else
6153 from_88(load<U16>(ptr), r,g);
6154 #endif
6155 }
6156
6157 SI void store_88_(uint16_t* ptr, U16 r, U16 g) {
6158 r = min(r, 255);
6159 g = min(g, 255);
6160
6161 #if 1 && defined(SKRP_CPU_NEON)
6162 uint8x8x2_t rg = {{
6163 cast<U8>(r),
6164 cast<U8>(g),
6165 }};
6166 vst2_u8((uint8_t*)(ptr), rg);
6167 #else
6168 store(ptr, cast<U16>(r | (g<<8)) << 0);
6169 #endif
6170 }
6171
6172 STAGE_PP(load_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
6173 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), &r, &g);
6174 b = U16_0;
6175 a = U16_255;
6176 }
6177 STAGE_PP(load_rg88_dst, const SkRasterPipeline_MemoryCtx* ctx) {
6178 load_88_(ptr_at_xy<const uint16_t>(ctx, dx, dy), &dr, &dg);
6179 db = U16_0;
6180 da = U16_255;
6181 }
6182 STAGE_PP(store_rg88, const SkRasterPipeline_MemoryCtx* ctx) {
6183 store_88_(ptr_at_xy<uint16_t>(ctx, dx, dy), r, g);
6184 }
6185 STAGE_GP(gather_rg88, const SkRasterPipeline_GatherCtx* ctx) {
6186 const uint16_t* ptr;
6187 U32 ix = ix_and_ptr(&ptr, ctx, x, y);
6188 from_88(gather<U16>(ptr, ix), &r, &g);
6189 b = U16_0;
6190 a = U16_255;
6191 }
6192
6193 // ~~~~~~ 8-bit memory loads and stores ~~~~~~ //
6194
6195 SI U16 load_8(const uint8_t* ptr) {
6196 return cast<U16>(load<U8>(ptr));
6197 }
6198 SI void store_8(uint8_t* ptr, U16 v) {
6199 v = min(v, 255);
6200 store(ptr, cast<U8>(v));
6201 }
6202
6203 STAGE_PP(load_a8, const SkRasterPipeline_MemoryCtx* ctx) {
6204 r = g = b = U16_0;
6205 a = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6206 }
6207 STAGE_PP(load_a8_dst, const SkRasterPipeline_MemoryCtx* ctx) {
6208 dr = dg = db = U16_0;
6209 da = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6210 }
6211 STAGE_PP(store_a8, const SkRasterPipeline_MemoryCtx* ctx) {
6212 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), a);
6213 }
6214 STAGE_GP(gather_a8, const SkRasterPipeline_GatherCtx* ctx) {
6215 const uint8_t* ptr;
6216 U32 ix = ix_and_ptr(&ptr, ctx, x,y);
6217 r = g = b = U16_0;
6218 a = cast<U16>(gather<U8>(ptr, ix));
6219 }
6220 STAGE_PP(store_r8, const SkRasterPipeline_MemoryCtx* ctx) {
6221 store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), r);
6222 }
6223
6224 STAGE_PP(alpha_to_gray, NoCtx) {
6225 r = g = b = a;
6226 a = U16_255;
6227 }
6228 STAGE_PP(alpha_to_gray_dst, NoCtx) {
6229 dr = dg = db = da;
6230 da = U16_255;
6231 }
6232 STAGE_PP(alpha_to_red, NoCtx) {
6233 r = a;
6234 a = U16_255;
6235 }
6236 STAGE_PP(alpha_to_red_dst, NoCtx) {
6237 dr = da;
6238 da = U16_255;
6239 }
6240
6241 STAGE_PP(bt709_luminance_or_luma_to_alpha, NoCtx) {
6242 a = (r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
6243 r = g = b = U16_0;
6244 }
6245 STAGE_PP(bt709_luminance_or_luma_to_rgb, NoCtx) {
6246 r = g = b =(r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
6247 }
6248
6249 // ~~~~~~ Coverage scales / lerps ~~~~~~ //
6250
6251 STAGE_PP(load_src, const uint16_t* ptr) {
6252 r = sk_unaligned_load<U16>(ptr + 0*N);
6253 g = sk_unaligned_load<U16>(ptr + 1*N);
6254 b = sk_unaligned_load<U16>(ptr + 2*N);
6255 a = sk_unaligned_load<U16>(ptr + 3*N);
6256 }
6257 STAGE_PP(store_src, uint16_t* ptr) {
6258 sk_unaligned_store(ptr + 0*N, r);
6259 sk_unaligned_store(ptr + 1*N, g);
6260 sk_unaligned_store(ptr + 2*N, b);
6261 sk_unaligned_store(ptr + 3*N, a);
6262 }
6263 STAGE_PP(store_src_a, uint16_t* ptr) {
6264 sk_unaligned_store(ptr, a);
6265 }
6266 STAGE_PP(load_dst, const uint16_t* ptr) {
6267 dr = sk_unaligned_load<U16>(ptr + 0*N);
6268 dg = sk_unaligned_load<U16>(ptr + 1*N);
6269 db = sk_unaligned_load<U16>(ptr + 2*N);
6270 da = sk_unaligned_load<U16>(ptr + 3*N);
6271 }
6272 STAGE_PP(store_dst, uint16_t* ptr) {
6273 sk_unaligned_store(ptr + 0*N, dr);
6274 sk_unaligned_store(ptr + 1*N, dg);
6275 sk_unaligned_store(ptr + 2*N, db);
6276 sk_unaligned_store(ptr + 3*N, da);
6277 }
6278
6279 // ~~~~~~ Coverage scales / lerps ~~~~~~ //
6280
6281 STAGE_PP(scale_1_float, const float* f) {
6282 U16 c = from_float(*f);
6283 r = div255( r * c );
6284 g = div255( g * c );
6285 b = div255( b * c );
6286 a = div255( a * c );
6287 }
6288 STAGE_PP(lerp_1_float, const float* f) {
6289 U16 c = from_float(*f);
6290 r = lerp(dr, r, c);
6291 g = lerp(dg, g, c);
6292 b = lerp(db, b, c);
6293 a = lerp(da, a, c);
6294 }
6295 STAGE_PP(scale_native, const uint16_t scales[]) {
6296 auto c = sk_unaligned_load<U16>(scales);
6297 r = div255( r * c );
6298 g = div255( g * c );
6299 b = div255( b * c );
6300 a = div255( a * c );
6301 }
6302
6303 STAGE_PP(lerp_native, const uint16_t scales[]) {
6304 auto c = sk_unaligned_load<U16>(scales);
6305 r = lerp(dr, r, c);
6306 g = lerp(dg, g, c);
6307 b = lerp(db, b, c);
6308 a = lerp(da, a, c);
6309 }
6310
6311 STAGE_PP(scale_u8, const SkRasterPipeline_MemoryCtx* ctx) {
6312 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6313 r = div255( r * c );
6314 g = div255( g * c );
6315 b = div255( b * c );
6316 a = div255( a * c );
6317 }
6318 STAGE_PP(lerp_u8, const SkRasterPipeline_MemoryCtx* ctx) {
6319 U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
6320 r = lerp(dr, r, c);
6321 g = lerp(dg, g, c);
6322 b = lerp(db, b, c);
6323 a = lerp(da, a, c);
6324 }
6325
6326 // Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
6327 SI U16 alpha_coverage_from_rgb_coverage(U16 a, U16 da, U16 cr, U16 cg, U16 cb) {
6328 return if_then_else(a < da, min(cr, min(cg,cb))
6329 , max(cr, max(cg,cb)));
6330 }
6331 STAGE_PP(scale_565, const SkRasterPipeline_MemoryCtx* ctx) {
6332 U16 cr,cg,cb;
6333 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
6334 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
6335
6336 r = div255( r * cr );
6337 g = div255( g * cg );
6338 b = div255( b * cb );
6339 a = div255( a * ca );
6340 }
6341 STAGE_PP(lerp_565, const SkRasterPipeline_MemoryCtx* ctx) {
6342 U16 cr,cg,cb;
6343 load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
6344 U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
6345
6346 r = lerp(dr, r, cr);
6347 g = lerp(dg, g, cg);
6348 b = lerp(db, b, cb);
6349 a = lerp(da, a, ca);
6350 }
6351
6352 STAGE_PP(emboss, const SkRasterPipeline_EmbossCtx* ctx) {
6353 U16 mul = load_8(ptr_at_xy<const uint8_t>(&ctx->mul, dx,dy)),
6354 add = load_8(ptr_at_xy<const uint8_t>(&ctx->add, dx,dy));
6355
6356 r = min(div255(r*mul) + add, a);
6357 g = min(div255(g*mul) + add, a);
6358 b = min(div255(b*mul) + add, a);
6359 }
6360
6361
6362 // ~~~~~~ Gradient stages ~~~~~~ //
6363
6364 // Clamp x to [0,1], both sides inclusive (think, gradients).
6365 // Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
6366 SI F clamp_01_(F v) { return min(max(0, v), 1); }
6367
6368 STAGE_GG(clamp_x_1 , NoCtx) { x = clamp_01_(x); }
6369 STAGE_GG(repeat_x_1, NoCtx) { x = clamp_01_(x - floor_(x)); }
6370 STAGE_GG(mirror_x_1, NoCtx) {
6371 auto two = [](F x){ return x+x; };
6372 x = clamp_01_(abs_( (x-1.0f) - two(floor_((x-1.0f)*0.5f)) - 1.0f ));
6373 }
6374
6375 SI I16 cond_to_mask_16(I32 cond) { return cast<I16>(cond); }
6376
6377 STAGE_GG(decal_x, SkRasterPipeline_DecalTileCtx* ctx) {
6378 auto w = ctx->limit_x;
6379 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w)));
6380 }
6381 STAGE_GG(decal_y, SkRasterPipeline_DecalTileCtx* ctx) {
6382 auto h = ctx->limit_y;
6383 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= y) & (y < h)));
6384 }
6385 STAGE_GG(decal_x_and_y, SkRasterPipeline_DecalTileCtx* ctx) {
6386 auto w = ctx->limit_x;
6387 auto h = ctx->limit_y;
6388 sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w) & (0 <= y) & (y < h)));
6389 }
6390 STAGE_GG(clamp_x_and_y, SkRasterPipeline_CoordClampCtx* ctx) {
6391 x = min(ctx->max_x, max(ctx->min_x, x));
6392 y = min(ctx->max_y, max(ctx->min_y, y));
6393 }
6394 STAGE_PP(check_decal_mask, SkRasterPipeline_DecalTileCtx* ctx) {
6395 auto mask = sk_unaligned_load<U16>(ctx->mask);
6396 r = r & mask;
6397 g = g & mask;
6398 b = b & mask;
6399 a = a & mask;
6400 }
6401
6402 SI void round_F_to_U16(F R, F G, F B, F A, U16* r, U16* g, U16* b, U16* a) {
6403 auto round_color = [](F x) { return cast<U16>(x * 255.0f + 0.5f); };
6404
6405 *r = round_color(min(max(0, R), 1));
6406 *g = round_color(min(max(0, G), 1));
6407 *b = round_color(min(max(0, B), 1));
6408 *a = round_color(A); // we assume alpha is already in [0,1].
6409 }
6410
6411 SI void gradient_lookup(const SkRasterPipeline_GradientCtx* c, U32 idx, F t,
6412 U16* r, U16* g, U16* b, U16* a) {
6413
6414 F fr, fg, fb, fa, br, bg, bb, ba;
6415 #if defined(SKRP_CPU_HSW)
6416 if (c->stopCount <=8) {
6417 __m256i lo, hi;
6418 split(idx, &lo, &hi);
6419
6420 fr = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), lo),
6421 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[0]), hi));
6422 br = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), lo),
6423 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[0]), hi));
6424 fg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), lo),
6425 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[1]), hi));
6426 bg = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), lo),
6427 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[1]), hi));
6428 fb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), lo),
6429 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[2]), hi));
6430 bb = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), lo),
6431 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[2]), hi));
6432 fa = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), lo),
6433 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->fs[3]), hi));
6434 ba = join<F>(_mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), lo),
6435 _mm256_permutevar8x32_ps(_mm256_loadu_ps(c->bs[3]), hi));
6436 } else
6437 #elif defined(SKRP_CPU_LASX)
6438 if (c->stopCount <= 8) {
6439 __m256i lo, hi;
6440 split(idx, &lo, &hi);
6441
6442 fr = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[0], 0), lo),
6443 (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[0], 0), hi));
6444 br = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[0], 0), lo),
6445 (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[0], 0), hi));
6446 fg = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[1], 0), lo),
6447 (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[1], 0), hi));
6448 bg = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[1], 0), lo),
6449 (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[1], 0), hi));
6450 fb = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[2], 0), lo),
6451 (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[2], 0), hi));
6452 bb = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[2], 0), lo),
6453 (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[2], 0), hi));
6454 fa = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[3], 0), lo),
6455 (__m256)__lasx_xvperm_w(__lasx_xvld(c->fs[3], 0), hi));
6456 ba = join<F>((__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[3], 0), lo),
6457 (__m256)__lasx_xvperm_w(__lasx_xvld(c->bs[3], 0), hi));
6458 } else
6459 #elif defined(SKRP_CPU_LSX)
6460 if (c->stopCount <= 4) {
6461 __m128i lo, hi;
6462 split(idx, &lo, &hi);
6463 __m128i zero = __lsx_vldi(0);
6464 fr = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->fs[0], 0)),
6465 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->fs[0], 0)));
6466 br = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->bs[0], 0)),
6467 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->bs[0], 0)));
6468 fg = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->fs[1], 0)),
6469 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->fs[1], 0)));
6470 bg = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->bs[1], 0)),
6471 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->bs[1], 0)));
6472 fb = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->fs[2], 0)),
6473 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->fs[2], 0)));
6474 bb = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->bs[2], 0)),
6475 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->bs[2], 0)));
6476 fa = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->fs[3], 0)),
6477 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->fs[3], 0)));
6478 ba = join<F>((__m128)__lsx_vshuf_w(lo, zero, __lsx_vld(c->bs[3], 0)),
6479 (__m128)__lsx_vshuf_w(hi, zero, __lsx_vld(c->bs[3], 0)));
6480 } else
6481 #endif
6482 {
6483 fr = gather<F>(c->fs[0], idx);
6484 fg = gather<F>(c->fs[1], idx);
6485 fb = gather<F>(c->fs[2], idx);
6486 fa = gather<F>(c->fs[3], idx);
6487 br = gather<F>(c->bs[0], idx);
6488 bg = gather<F>(c->bs[1], idx);
6489 bb = gather<F>(c->bs[2], idx);
6490 ba = gather<F>(c->bs[3], idx);
6491 }
6492 round_F_to_U16(mad(t, fr, br),
6493 mad(t, fg, bg),
6494 mad(t, fb, bb),
6495 mad(t, fa, ba),
6496 r,g,b,a);
6497 }
6498
6499 STAGE_GP(gradient, const SkRasterPipeline_GradientCtx* c) {
6500 auto t = x;
6501 U32 idx = U32_(0);
6502
6503 // N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
6504 for (size_t i = 1; i < c->stopCount; i++) {
6505 idx += if_then_else(t >= c->ts[i], U32_(1), U32_(0));
6506 }
6507
6508 gradient_lookup(c, idx, t, &r, &g, &b, &a);
6509 }
6510
6511 STAGE_GP(evenly_spaced_gradient, const SkRasterPipeline_GradientCtx* c) {
6512 auto t = x;
6513 auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
6514 gradient_lookup(c, idx, t, &r, &g, &b, &a);
6515 }
6516
6517 STAGE_GP(evenly_spaced_2_stop_gradient, const SkRasterPipeline_EvenlySpaced2StopGradientCtx* c) {
6518 auto t = x;
6519 round_F_to_U16(mad(t, c->f[0], c->b[0]),
6520 mad(t, c->f[1], c->b[1]),
6521 mad(t, c->f[2], c->b[2]),
6522 mad(t, c->f[3], c->b[3]),
6523 &r,&g,&b,&a);
6524 }
6525
6526 STAGE_GP(bilerp_clamp_8888, const SkRasterPipeline_GatherCtx* ctx) {
6527 // Quantize sample point and transform into lerp coordinates converting them to 16.16 fixed
6528 // point number.
6529 #if defined(SKRP_CPU_LSX)
6530 __m128 _01, _23, _45, _67;
6531 v4f32 v_tmp1 = {0.5f, 0.5f, 0.5f, 0.5f};
6532 v4f32 v_tmp2 = {65536.0f, 65536.0f, 65536.0f, 65536.0f};
6533 split(x, &_01,&_23);
6534 split(y, &_45,&_67);
6535 __m128 val1 = __lsx_vfmadd_s((__m128)v_tmp2, _01, (__m128)v_tmp1);
6536 __m128 val2 = __lsx_vfmadd_s((__m128)v_tmp2, _23, (__m128)v_tmp1);
6537 __m128 val3 = __lsx_vfmadd_s((__m128)v_tmp2, _45, (__m128)v_tmp1);
6538 __m128 val4 = __lsx_vfmadd_s((__m128)v_tmp2, _67, (__m128)v_tmp1);
6539 I32 qx = cast<I32>((join<F>(__lsx_vfrintrm_s(val1), __lsx_vfrintrm_s(val2)))) - 32768,
6540 qy = cast<I32>((join<F>(__lsx_vfrintrm_s(val3), __lsx_vfrintrm_s(val4)))) - 32768;
6541 #else
6542 I32 qx = cast<I32>(floor_(65536.0f * x + 0.5f)) - 32768,
6543 qy = cast<I32>(floor_(65536.0f * y + 0.5f)) - 32768;
6544 #endif
6545
6546 // Calculate screen coordinates sx & sy by flooring qx and qy.
6547 I32 sx = qx >> 16,
6548 sy = qy >> 16;
6549
6550 // We are going to perform a change of parameters for qx on [0, 1) to tx on [-1, 1).
6551 // This will put tx in Q15 format for use with q_mult.
6552 // Calculate tx and ty on the interval of [-1, 1). Give {qx} and {qy} are on the interval
6553 // [0, 1), where {v} is fract(v), we can transform to tx in the following manner ty follows
6554 // the same math:
6555 // tx = 2 * {qx} - 1, so
6556 // {qx} = (tx + 1) / 2.
6557 // Calculate {qx} - 1 and {qy} - 1 where the {} operation is handled by the cast, and the - 1
6558 // is handled by the ^ 0x8000, dividing by 2 is deferred and handled in lerpX and lerpY in
6559 // order to use the full 16-bit resolution.
6560 #if defined(SKRP_CPU_LSX)
6561 __m128i qx_lo, qx_hi, qy_lo, qy_hi;
6562 split(qx, &qx_lo, &qx_hi);
6563 split(qy, &qy_lo, &qy_hi);
6564 __m128i temp = __lsx_vreplgr2vr_w(0x8000);
6565 qx_lo = __lsx_vxor_v(qx_lo, temp);
6566 qx_hi = __lsx_vxor_v(qx_hi, temp);
6567 qy_lo = __lsx_vxor_v(qy_lo, temp);
6568 qy_hi = __lsx_vxor_v(qy_hi, temp);
6569
6570 I16 tx = __lsx_vpickev_h(qx_hi, qx_lo);
6571 I16 ty = __lsx_vpickev_h(qy_hi, qy_lo);
6572 #else
6573 I16 tx = cast<I16>(qx ^ 0x8000),
6574 ty = cast<I16>(qy ^ 0x8000);
6575 #endif
6576
6577 // Substituting the {qx} by the equation for tx from above into the lerp equation where v is
6578 // the lerped value:
6579 // v = {qx}*(R - L) + L,
6580 // v = 1/2*(tx + 1)*(R - L) + L
6581 // 2 * v = (tx + 1)*(R - L) + 2*L
6582 // = tx*R - tx*L + R - L + 2*L
6583 // = tx*(R - L) + (R + L).
6584 // Since R and L are on [0, 255] we need them on the interval [0, 1/2] to get them into form
6585 // for Q15_mult. If L and R where in 16.16 format, this would be done by dividing by 2^9. In
6586 // code, we can multiply by 2^7 to get the value directly.
6587 // 2 * v = tx*(R - L) + (R + L)
6588 // 2^-9 * 2 * v = tx*(R - L)*2^-9 + (R + L)*2^-9
6589 // 2^-8 * v = 2^-9 * (tx*(R - L) + (R + L))
6590 // v = 1/2 * (tx*(R - L) + (R + L))
6591 auto lerpX = [&](U16 left, U16 right) -> U16 {
6592 I16 width = (I16)(right - left) << 7;
6593 U16 middle = (right + left) << 7;
6594 // The constrained_add is the most subtle part of lerp. The first term is on the interval
6595 // [-1, 1), and the second term is on the interval is on the interval [0, 1) because
6596 // both terms are too high by a factor of 2 which will be handled below. (Both R and L are
6597 // on [0, 1/2), but the sum R + L is on the interval [0, 1).) Generally, the sum below
6598 // should overflow, but because we know that sum produces an output on the
6599 // interval [0, 1) we know that the extra bit that would be needed will always be 0. So
6600 // we need to be careful to treat this sum as an unsigned positive number in the divide
6601 // by 2 below. Add +1 for rounding.
6602 U16 v2 = constrained_add(scaled_mult(tx, width), middle) + 1;
6603 // Divide by 2 to calculate v and at the same time bring the intermediate value onto the
6604 // interval [0, 1/2] to set up for the lerpY.
6605 return v2 >> 1;
6606 };
6607
6608 const uint32_t* ptr;
6609 U32 ix = ix_and_ptr(&ptr, ctx, sx, sy);
6610 U16 leftR, leftG, leftB, leftA;
6611 from_8888(gather<U32>(ptr, ix), &leftR,&leftG,&leftB,&leftA);
6612
6613 ix = ix_and_ptr(&ptr, ctx, sx+1, sy);
6614 U16 rightR, rightG, rightB, rightA;
6615 from_8888(gather<U32>(ptr, ix), &rightR,&rightG,&rightB,&rightA);
6616
6617 U16 topR = lerpX(leftR, rightR),
6618 topG = lerpX(leftG, rightG),
6619 topB = lerpX(leftB, rightB),
6620 topA = lerpX(leftA, rightA);
6621
6622 ix = ix_and_ptr(&ptr, ctx, sx, sy+1);
6623 from_8888(gather<U32>(ptr, ix), &leftR,&leftG,&leftB,&leftA);
6624
6625 ix = ix_and_ptr(&ptr, ctx, sx+1, sy+1);
6626 from_8888(gather<U32>(ptr, ix), &rightR,&rightG,&rightB,&rightA);
6627
6628 U16 bottomR = lerpX(leftR, rightR),
6629 bottomG = lerpX(leftG, rightG),
6630 bottomB = lerpX(leftB, rightB),
6631 bottomA = lerpX(leftA, rightA);
6632
6633 // lerpY plays the same mathematical tricks as lerpX, but the final divide is by 256 resulting
6634 // in a value on [0, 255].
6635 auto lerpY = [&](U16 top, U16 bottom) -> U16 {
6636 I16 width = (I16)bottom - (I16)top;
6637 U16 middle = bottom + top;
6638 // Add + 0x80 for rounding.
6639 U16 blend = constrained_add(scaled_mult(ty, width), middle) + 0x80;
6640
6641 return blend >> 8;
6642 };
6643
6644 r = lerpY(topR, bottomR);
6645 g = lerpY(topG, bottomG);
6646 b = lerpY(topB, bottomB);
6647 a = lerpY(topA, bottomA);
6648 }
6649
6650 STAGE_GG(xy_to_unit_angle, NoCtx) {
6651 F xabs = abs_(x),
6652 yabs = abs_(y);
6653
6654 F slope = min(xabs, yabs)/max(xabs, yabs);
6655 F s = slope * slope;
6656
6657 // Use a 7th degree polynomial to approximate atan.
6658 // This was generated using sollya.gforge.inria.fr.
6659 // A float optimized polynomial was generated using the following command.
6660 // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
6661 F phi = slope
6662 * (0.15912117063999176025390625f + s
6663 * (-5.185396969318389892578125e-2f + s
6664 * (2.476101927459239959716796875e-2f + s
6665 * (-7.0547382347285747528076171875e-3f))));
6666
6667 phi = if_then_else(xabs < yabs, 1.0f/4.0f - phi, phi);
6668 phi = if_then_else(x < 0.0f , 1.0f/2.0f - phi, phi);
6669 phi = if_then_else(y < 0.0f , 1.0f - phi , phi);
6670 phi = if_then_else(phi != phi , 0 , phi); // Check for NaN.
6671 x = phi;
6672 }
6673 STAGE_GG(xy_to_radius, NoCtx) {
6674 x = sqrt_(x*x + y*y);
6675 }
6676
6677 // ~~~~~~ Compound stages ~~~~~~ //
6678
6679 STAGE_PP(srcover_rgba_8888, const SkRasterPipeline_MemoryCtx* ctx) {
6680 auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
6681
6682 load_8888_(ptr, &dr,&dg,&db,&da);
6683 r = r + div255( dr*inv(a) );
6684 g = g + div255( dg*inv(a) );
6685 b = b + div255( db*inv(a) );
6686 a = a + div255( da*inv(a) );
6687 store_8888_(ptr, r,g,b,a);
6688 }
6689
6690 // ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
6691
6692 STAGE_PP(swizzle, void* ctx) {
6693 auto ir = r, ig = g, ib = b, ia = a;
6694 U16* o[] = {&r, &g, &b, &a};
6695 char swiz[4];
6696 memcpy(swiz, &ctx, sizeof(swiz));
6697
6698 for (int i = 0; i < 4; ++i) {
6699 switch (swiz[i]) {
6700 case 'r': *o[i] = ir; break;
6701 case 'g': *o[i] = ig; break;
6702 case 'b': *o[i] = ib; break;
6703 case 'a': *o[i] = ia; break;
6704 case '0': *o[i] = U16_0; break;
6705 case '1': *o[i] = U16_255; break;
6706 default: break;
6707 }
6708 }
6709 }
6710
6711 #endif//defined(SKRP_CPU_SCALAR) controlling whether we build lowp stages
6712 } // namespace lowp
6713
6714 /* This gives us SK_OPTS::lowp::N if lowp::N has been set, or SK_OPTS::N if it hasn't. */
6715 namespace lowp { static constexpr size_t lowp_N = N; }
6716
6717 /** Allow outside code to access the Raster Pipeline pixel stride. */
raster_pipeline_lowp_stride()6718 constexpr size_t raster_pipeline_lowp_stride() { return lowp::lowp_N; }
raster_pipeline_highp_stride()6719 constexpr size_t raster_pipeline_highp_stride() { return N; }
6720
6721 } // namespace SK_OPTS_NS
6722
6723 #undef SI
6724
6725 #endif//SkRasterPipeline_opts_DEFINED
6726