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