// Every function in this file should be marked static and inline using SI. #ifdefined(__clang__) || defined(__GNUC__) #define SI __attribute__((always_inline)) static inline #else #define SI static inline #endif
// Why does RasterPipeline have its own SIMD wrapper and is not using SkVx? SkVx is designed // for keeping things simple, e.g. so you can put vectors in classes. SkVx has a very simple, // predictable, memory layout - they are equivalent to a struct with an array of n values. // Unfortunately, because of that, they will not pass in registers. A core design principle of // SkRP is to have the 8 parameters passed into a stage be actual hardware registers (for // optimal performance). #ifdefined(__clang__) template <int N, typename T> using Vec = T __attribute__((ext_vector_type(N))); #elifdefined(__GNUC__) #ifndef __has_builtin #define SKRP_CPU_SCALAR #elif !__has_builtin(__builtin_convertvector) #define SKRP_CPU_SCALAR #endif
// Unfortunately, GCC does not allow us to omit the struct. This will not compile: // template <int N, typename T> using Vec = T __attribute__((vector_size(N*sizeof(T)))); template <int N, typename T> struct VecHelper { typedef T __attribute__((vector_size(N * sizeof(T)))) V;
}; template <int N, typename T> using Vec = typename VecHelper<N, T>::V; #endif
// Notes: // * rcp_fast and rcp_precise both produce a reciprocal, but rcp_fast is an estimate with at least // 12 bits of precision while rcp_precise should be accurate for float size. For ARM rcp_precise // requires 2 Newton-Raphson refinement steps because its estimate has 8 bit precision, and for // Intel this requires one additional step because its estimate has 12 bit precision. // // * Don't call rcp_approx or rsqrt_approx directly; only use rcp_fast and rsqrt.
namespace SK_OPTS_NS { #ifdefined(SKRP_CPU_SCALAR) // This path should lead to portable scalar code. using F = float ; using I32 = int32_t; using U64 = uint64_t; using U32 = uint32_t; using U16 = uint16_t; using U8 = uint8_t ;
SI F min(F a, F b) { return fminf(a,b); }
SI I32 min(I32 a, I32 b) { return a < b ? a : b; }
SI U32 min(U32 a, U32 b) { return a < b ? a : b; }
SI F max(F a, F b) { return fmaxf(a,b); }
SI I32 max(I32 a, I32 b) { return a > b ? a : b; }
SI U32 max(U32 a, U32 b) { return a > b ? a : b; }
SI F mad(F f, F m, F a) { return a+f*m; }
SI F nmad(F f, F m, F a) { return a-f*m; }
SI F abs_ (F v) { return fabsf(v); }
SI I32 abs_ (I32 v) { return v < 0 ? -v : v; }
SI F floor_(F v) { return floorf(v); }
SI F ceil_(F v) { return ceilf(v); }
SI F rcp_approx(F v) { return1.0f / v; } // use rcp_fast instead
SI F rsqrt_approx(F v) { return1.0f / sqrtf(v); }
SI F sqrt_ (F v) { return sqrtf(v); }
SI F rcp_precise (F v) { return1.0f / v; }
SI I32 iround(F v) { return (I32)(v + 0.5f); }
SI U32 round(F v) { return (U32)(v + 0.5f); }
SI U16 pack(U32 v) { return (U16)v; }
SI U8 pack(U16 v) { return (U8)v; }
SI F if_then_else(I32 c, F t, F e) { return c ? t : e; }
SI I32 if_then_else(I32 c, I32 t, I32 e) { return c ? t : e; }
SI bool any(I32 c) { return c != 0; }
SI bool all(I32 c) { return c != 0; }
template <typename T>
SI T gather(const T* p, U32 ix) { return p[ix]; } template <typename T>
SI T gather_unaligned(const T* p, U32 ix) { return gather<T>(p, ix); }
SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
*r = ptr[0];
*g = ptr[1];
*b = ptr[2];
*a = ptr[3];
}
SI void store4(float* ptr, F r, F g, F b, F a) {
ptr[0] = r;
ptr[1] = g;
ptr[2] = b;
ptr[3] = a;
}
#elifdefined(SKRP_CPU_NEON) template <typename T> using V = Vec<4, T>; using F = V<float >; using I32 = V< int32_t>; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
// We polyfill a few routines that Clang doesn't build into ext_vector_types.
SI F min(F a, F b) { return vminq_f32(a,b); }
SI I32 min(I32 a, I32 b) { return vminq_s32(a,b); }
SI U32 min(U32 a, U32 b) { return vminq_u32(a,b); }
SI F max(F a, F b) { return vmaxq_f32(a,b); }
SI I32 max(I32 a, I32 b) { return vmaxq_s32(a,b); }
SI U32 max(U32 a, U32 b) { return vmaxq_u32(a,b); }
SI F abs_ (F v) { return vabsq_f32(v); }
SI I32 abs_ (I32 v) { return vabsq_s32(v); }
SI F rcp_approx(F v) { auto e = vrecpeq_f32(v); return vrecpsq_f32 (v,e ) * e; }
SI F rcp_precise(F v) { auto e = rcp_approx(v); return vrecpsq_f32 (v,e ) * e; }
SI F rsqrt_approx(F v) { auto e = vrsqrteq_f32(v); return vrsqrtsq_f32(v,e*e) * e; }
SI U16 pack(U32 v) { return __builtin_convertvector(v, U16); }
SI U8 pack(U16 v) { return __builtin_convertvector(v, U8); }
SI F if_then_else(I32 c, F t, F e) { return vbslq_f32((U32)c,t,e); }
SI I32 if_then_else(I32 c, I32 t, I32 e) { return vbslq_s32((U32)c,t,e); }
#ifdefined(SK_CPU_ARM64)
SI bool any(I32 c) { return vmaxvq_u32((U32)c) != 0; }
SI bool all(I32 c) { return vminvq_u32((U32)c) != 0; }
SI F mad(F f, F m, F a) { return vfmaq_f32(a,f,m); }
SI F nmad(F f, F m, F a) { return vfmsq_f32(a,f,m); }
SI F floor_(F v) { return vrndmq_f32(v); }
SI F ceil_(F v) { return vrndpq_f32(v); }
SI F sqrt_(F v) { return vsqrtq_f32(v); }
SI I32 iround(F v) { return vcvtnq_s32_f32(v); }
SI U32 round(F v) { return vcvtnq_u32_f32(v); } #else
SI bool any(I32 c) { return c[0] | c[1] | c[2] | c[3]; }
SI bool all(I32 c) { return c[0] & c[1] & c[2] & c[3]; }
SI F mad(F f, F m, F a) { return vmlaq_f32(a,f,m); }
SI F nmad(F f, F m, F a) { return vmlsq_f32(a,f,m); }
SI F floor_(F v) {
F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v)); return roundtrip - if_then_else(roundtrip > v, F() + 1, F());
}
SI F ceil_(F v) {
F roundtrip = vcvtq_f32_s32(vcvtq_s32_f32(v)); return roundtrip + if_then_else(roundtrip < v, F() + 1, F());
}
SI F sqrt_(F v) { auto e = vrsqrteq_f32(v); // Estimate and two refinement steps for e = rsqrt(v).
e *= vrsqrtsq_f32(v,e*e);
e *= vrsqrtsq_f32(v,e*e); return v*e; // sqrt(v) == v*rsqrt(v).
}
SI I32 iround(F v) { return vcvtq_s32_f32(v + 0.5f);
}
template <typename T>
SI V<T> gather(const T* ptr, U32 ix) { // The compiler assumes ptr is aligned, which caused crashes on some // arm32 chips because a register was marked as "aligned to 32 bits" // incorrectly. skbug.com/409859319
SkASSERTF(reinterpret_cast<uintptr_t>(ptr) % alignof(T) == 0, "Should use gather_unaligned"); return V<T>{ptr[ix[0]], ptr[ix[1]], ptr[ix[2]], ptr[ix[3]]};
} template <typename T>
SI V<T> gather_unaligned(const T* ptr, U32 ix) { // This tells the compiler ptr might not be aligned appropriately, so // it generates better assembly. typedef T __attribute__ ((aligned (1))) unaligned_ptr; const unaligned_ptr* uptr = static_cast<const unaligned_ptr*>(ptr); return V<T>{uptr[ix[0]], uptr[ix[1]], uptr[ix[2]], uptr[ix[3]]};
}
SI void store4(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
vst4_u16(ptr, (uint16x4x4_t{{r,g,b,a}}));
}
SI void load4(const float* ptr, F* r, F* g, F* b, F* a) {
float32x4x4_t rgba = vld4q_f32(ptr);
*r = rgba.val[0];
*g = rgba.val[1];
*b = rgba.val[2];
*a = rgba.val[3];
}
SI void store4(float* ptr, F r, F g, F b, F a) {
vst4q_f32(ptr, (float32x4x4_t{{r,g,b,a}}));
}
#elifdefined(SKRP_CPU_ML4) template <typename T> using V = Vec<16, T>; using F = V<float >; using I32 = V< int32_t>; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
SI F mad(F f, F m, F a) { return _mm512_fmadd_ps(f, m, a); }
SI F nmad(F f, F m, F a) { return _mm512_fnmadd_ps(f, m, a); }
SI F min(F a, F b) { return _mm512_min_ps(a,b); }
SI I32 min(I32 a, I32 b) { return (I32)_mm512_min_epi32((__m512i)a,(__m512i)b); }
SI U32 min(U32 a, U32 b) { return (U32)_mm512_min_epu32((__m512i)a,(__m512i)b); }
SI F max(F a, F b) { return _mm512_max_ps(a,b); }
SI I32 max(I32 a, I32 b) { return (I32)_mm512_max_epi32((__m512i)a,(__m512i)b); }
SI U32 max(U32 a, U32 b) { return (U32)_mm512_max_epu32((__m512i)a,(__m512i)b); }
SI F abs_ (F v) { return _mm512_and_ps(v, _mm512_sub_ps(_mm512_setzero(), v)); }
SI I32 abs_ (I32 v) { return (I32)_mm512_abs_epi32((__m512i)v); }
SI F floor_(F v) { return _mm512_floor_ps(v); }
SI F ceil_(F v) { return _mm512_ceil_ps(v); }
SI F rcp_approx(F v) { return _mm512_rcp14_ps (v); }
SI F rsqrt_approx (F v) { return _mm512_rsqrt14_ps(v); }
SI F sqrt_ (F v) { return _mm512_sqrt_ps (v); }
SI F rcp_precise (F v) {
F e = rcp_approx(v); return _mm512_fnmadd_ps(v, e, _mm512_set1_ps(2.0f)) * e;
}
SI I32 iround(F v) { return (I32)_mm512_cvtps_epi32(v); }
SI U32 round(F v) { return (U32)_mm512_cvtps_epi32(v); }
SI U16 pack(U32 v) {
__m256i rst = _mm256_packus_epi32(_mm512_castsi512_si256((__m512i)v),
_mm512_extracti64x4_epi64((__m512i)v, 1)); return (U16)_mm256_permutex_epi64(rst, 216);
}
SI U8 pack(U16 v) {
__m256i rst = _mm256_packus_epi16((__m256i)v, (__m256i)v); return (U8)_mm256_castsi256_si128(_mm256_permute4x64_epi64(rst, 8));
}
SI F if_then_else(I32 c, F t, F e) {
__m512i mask = _mm512_set1_epi32(0x80000000);
__m512i aa = _mm512_and_si512((__m512i)c, mask); return _mm512_mask_blend_ps(_mm512_test_epi32_mask(aa, aa),e,t);
}
SI I32 if_then_else(I32 c, I32 t, I32 e) {
__m512i mask = _mm512_set1_epi32(0x80000000);
__m512i aa = _mm512_and_si512((__m512i)c, mask); return (I32)_mm512_mask_blend_epi32(_mm512_test_epi32_mask(aa, aa),(__m512i)e,(__m512i)t);
}
SI bool any(I32 c) {
__mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c); return mask32 != 0;
}
SI bool all(I32 c) {
__mmask16 mask32 = _mm512_test_epi32_mask((__m512i)c, (__m512i)c); return mask32 == 0xffff;
} template <typename T>
SI V<T> gather(const T* p, U32 ix) { return V<T>{ p[ix[ 0]], p[ix[ 1]], p[ix[ 2]], p[ix[ 3]],
p[ix[ 4]], p[ix[ 5]], p[ix[ 6]], p[ix[ 7]],
p[ix[ 8]], p[ix[ 9]], p[ix[10]], p[ix[11]],
p[ix[12]], p[ix[13]], p[ix[14]], p[ix[15]] };
}
SI F gather(const float* p, U32 ix) { return _mm512_i32gather_ps((__m512i)ix, p, 4); }
SI U32 gather(const uint32_t* p, U32 ix) { return (U32)_mm512_i32gather_epi32((__m512i)ix, p, 4); }
SI U64 gather(const uint64_t* p, U32 ix) {
__m512i parts[] = {
_mm512_i32gather_epi64(_mm512_castsi512_si256((__m512i)ix), p, 8),
_mm512_i32gather_epi64(_mm512_extracti32x8_epi32((__m512i)ix, 1), p, 8),
}; return sk_bit_cast<U64>(parts);
} template <typename T>
SI V<T> gather_unaligned(const T* p, U32 ix) { return gather(p, ix);
}
SI void store4(float* ptr, F r, F g, F b, F a) {
F rg014589cd = _mm512_unpacklo_ps(r, g),
rg2367abef = _mm512_unpackhi_ps(r, g),
ba014589cd = _mm512_unpacklo_ps(b, a),
ba2367abef = _mm512_unpackhi_ps(b, a);
#elifdefined(SKRP_CPU_AVX2) // These are __m256 and __m256i, but friendlier and strongly-typed. template <typename T> using V = Vec<8, T>; using F = V<float >; using I32 = V< int32_t>; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
SI F mad(F f, F m, F a) { return _mm256_fmadd_ps(f, m, a); }
SI F nmad(F f, F m, F a) { return _mm256_fnmadd_ps(f, m, a); }
SI F min(F a, F b) { return _mm256_min_ps(a,b); }
SI I32 min(I32 a, I32 b) { return (I32)_mm256_min_epi32((__m256i)a,(__m256i)b); }
SI U32 min(U32 a, U32 b) { return (U32)_mm256_min_epu32((__m256i)a,(__m256i)b); }
SI F max(F a, F b) { return _mm256_max_ps(a,b); }
SI I32 max(I32 a, I32 b) { return (I32)_mm256_max_epi32((__m256i)a,(__m256i)b); }
SI U32 max(U32 a, U32 b) { return (U32)_mm256_max_epu32((__m256i)a,(__m256i)b); }
SI F abs_ (F v) { return _mm256_and_ps(v, 0-v); }
SI I32 abs_ (I32 v) { return (I32)_mm256_abs_epi32((__m256i)v); }
SI F floor_(F v) { return _mm256_floor_ps(v); }
SI F ceil_(F v) { return _mm256_ceil_ps(v); }
SI F rcp_approx(F v) { return _mm256_rcp_ps (v); } // use rcp_fast instead
SI F rsqrt_approx(F v) { return _mm256_rsqrt_ps(v); }
SI F sqrt_ (F v) { return _mm256_sqrt_ps (v); }
SI F rcp_precise (F v) {
F e = rcp_approx(v); return _mm256_fnmadd_ps(v, e, _mm256_set1_ps(2.0f)) * e;
}
SI I32 iround(F v) { return (I32)_mm256_cvtps_epi32(v); }
SI U32 round(F v) { return (U32)_mm256_cvtps_epi32(v); }
SI U16 pack(U32 v) { return (U16)_mm_packus_epi32(_mm256_extractf128_si256((__m256i)v, 0),
_mm256_extractf128_si256((__m256i)v, 1));
}
SI U8 pack(U16 v) { auto r = _mm_packus_epi16((__m128i)v,(__m128i)v); return sk_unaligned_load<U8>(&r);
}
SI F if_then_else(I32 c, F t, F e) { return _mm256_blendv_ps(e, t, (__m256)c); }
SI I32 if_then_else(I32 c, I32 t, I32 e) { return (I32)_mm256_blendv_ps((__m256)e, (__m256)t, (__m256)c);
}
// NOTE: This version of 'all' only works with mask values (true == all bits set)
SI bool any(I32 c) { return !_mm256_testz_si256((__m256i)c, _mm256_set1_epi32(-1)); }
SI bool all(I32 c) { return _mm256_testc_si256((__m256i)c, _mm256_set1_epi32(-1)); }
template <typename T>
SI V<T> gather(const T* p, U32 ix) { return V<T>{ p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]],
p[ix[4]], p[ix[5]], p[ix[6]], p[ix[7]], };
}
SI F gather(const float* p, U32 ix) { return _mm256_i32gather_ps(p, (__m256i)ix, 4); }
SI U32 gather(const uint32_t* p, U32 ix) { return (U32)_mm256_i32gather_epi32((const int*)p, (__m256i)ix, 4);
}
SI U64 gather(const uint64_t* p, U32 ix) {
__m256i parts[] = {
_mm256_i32gather_epi64(
(const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 0), 8),
_mm256_i32gather_epi64(
(const long long int*)p, _mm256_extracti128_si256((__m256i)ix, 1), 8),
}; return sk_bit_cast<U64>(parts);
} template <typename T>
SI V<T> gather_unaligned(const T* p, U32 ix) { return gather(p, ix);
}
#elifdefined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX) template <typename T> using V = Vec<4, T>; using F = V<float >; using I32 = V< int32_t>; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
SI F if_then_else(I32 c, F t, F e) { return _mm_or_ps(_mm_and_ps((__m128)c, t), _mm_andnot_ps((__m128)c, e));
}
SI I32 if_then_else(I32 c, I32 t, I32 e) { return (I32)_mm_or_ps(_mm_and_ps((__m128)c, (__m128)t),
_mm_andnot_ps((__m128)c, (__m128)e));
}
SI F min(F a, F b) { return _mm_min_ps(a,b); }
SI F max(F a, F b) { return _mm_max_ps(a,b); } #ifdefined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
SI I32 min(I32 a, I32 b) { return (I32)_mm_min_epi32((__m128i)a,(__m128i)b); }
SI U32 min(U32 a, U32 b) { return (U32)_mm_min_epu32((__m128i)a,(__m128i)b); }
SI I32 max(I32 a, I32 b) { return (I32)_mm_max_epi32((__m128i)a,(__m128i)b); }
SI U32 max(U32 a, U32 b) { return (U32)_mm_max_epu32((__m128i)a,(__m128i)b); } #else
SI I32 min(I32 a, I32 b) { return if_then_else(a < b, a, b); }
SI I32 max(I32 a, I32 b) { return if_then_else(a > b, a, b); }
SI U32 min(U32 a, U32 b) { return sk_bit_cast<U32>(if_then_else(a < b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
}
SI U32 max(U32 a, U32 b) { return sk_bit_cast<U32>(if_then_else(a > b, sk_bit_cast<I32>(a), sk_bit_cast<I32>(b)));
} #endif
SI F mad(F f, F m, F a) { return a+f*m; }
SI F nmad(F f, F m, F a) { return a-f*m; }
SI F abs_(F v) { return _mm_and_ps(v, 0-v); } #ifdefined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
SI I32 abs_(I32 v) { return (I32)_mm_abs_epi32((__m128i)v); } #else
SI I32 abs_(I32 v) { return max(v, -v); } #endif
SI F rcp_approx(F v) { return _mm_rcp_ps (v); } // use rcp_fast instead
SI F rcp_precise (F v) { F e = rcp_approx(v); return e * (2.0f - v * e); }
SI F rsqrt_approx(F v) { return _mm_rsqrt_ps(v); }
SI F sqrt_(F v) { return _mm_sqrt_ps (v); }
SI I32 iround(F v) { return (I32)_mm_cvtps_epi32(v); }
SI U32 round(F v) { return (U32)_mm_cvtps_epi32(v); }
SI U16 pack(U32 v) { #ifdefined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX) auto p = _mm_packus_epi32((__m128i)v,(__m128i)v); #else // Sign extend so that _mm_packs_epi32() does the pack we want. auto p = _mm_srai_epi32(_mm_slli_epi32((__m128i)v, 16), 16);
p = _mm_packs_epi32(p,p); #endif return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
}
SI U8 pack(U16 v) { auto r = widen_cast<__m128i>(v);
r = _mm_packus_epi16(r,r); return sk_unaligned_load<U8>(&r);
}
// NOTE: This only checks the top bit of each lane, and is incorrect with non-mask values.
SI bool any(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) != 0b0000; }
SI bool all(I32 c) { return _mm_movemask_ps(sk_bit_cast<F>(c)) == 0b1111; }
SI F floor_(F v) { #ifdefined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX) return _mm_floor_ps(v); #else
F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v)); return roundtrip - if_then_else(roundtrip > v, F() + 1, F() + 0); #endif
}
SI F ceil_(F v) { #ifdefined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX) return _mm_ceil_ps(v); #else
F roundtrip = _mm_cvtepi32_ps(_mm_cvttps_epi32(v)); return roundtrip + if_then_else(roundtrip < v, F() + 1, F() + 0); #endif
}
SI void store4(float* ptr, F r, F g, F b, F a) {
_MM_TRANSPOSE4_PS(r,g,b,a);
_mm_storeu_ps(ptr + 0, r);
_mm_storeu_ps(ptr + 4, g);
_mm_storeu_ps(ptr + 8, b);
_mm_storeu_ps(ptr +12, a);
}
#elifdefined(SKRP_CPU_LASX) // These are __m256 and __m256i, but friendlier and strongly-typed. template <typename T> using V = Vec<8, T>; using F = V<float >; using I32 = V<int32_t>; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
SI __m128i emulate_lasx_d_xr2vr_l(__m256i a) {
v4i64 tmp = a;
v2i64 al = {tmp[0], tmp[1]}; return (__m128i)al;
}
SI __m128i emulate_lasx_d_xr2vr_h(__m256i a) {
v4i64 tmp = a;
v2i64 ah = {tmp[2], tmp[3]}; return (__m128i)ah;
}
SI F if_then_else(I32 c, F t, F e) { return sk_bit_cast<Vec<8,float>>(__lasx_xvbitsel_v(sk_bit_cast<__m256i>(e),
sk_bit_cast<__m256i>(t),
sk_bit_cast<__m256i>(c)));
}
SI F min(F a, F b) { return __lasx_xvfmin_s(a,b); }
SI F max(F a, F b) { return __lasx_xvfmax_s(a,b); }
SI I32 min(I32 a, I32 b) { return __lasx_xvmin_w(a,b); }
SI U32 min(U32 a, U32 b) { return __lasx_xvmin_wu(a,b); }
SI I32 max(I32 a, I32 b) { return __lasx_xvmax_w(a,b); }
SI U32 max(U32 a, U32 b) { return __lasx_xvmax_wu(a,b); }
SI F mad(F f, F m, F a) { return __lasx_xvfmadd_s(f, m, a); }
SI F nmad(F f, F m, F a) { return __lasx_xvfmadd_s(-f, m, a); }
SI F abs_ (F v) { return (F)__lasx_xvand_v((I32)v, (I32)(0-v)); }
SI I32 abs_(I32 v) { return max(v, -v); }
SI F rcp_approx(F v) { return __lasx_xvfrecip_s(v); }
SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
SI F rsqrt_approx (F v) { return __lasx_xvfrsqrt_s(v); }
SI F sqrt_(F v) { return __lasx_xvfsqrt_s(v); }
SI U32 iround(F v) {
F t = F() + 0.5f; return __lasx_xvftintrz_w_s(v + t);
}
SI U32 round(F v) {
F t = F() + 0.5f; return __lasx_xvftintrz_w_s(v + t);
}
#elifdefined(SKRP_CPU_LSX) template <typename T> using V = Vec<4, T>; using F = V<float >; using I32 = V<int32_t >; using U64 = V<uint64_t>; using U32 = V<uint32_t>; using U16 = V<uint16_t>; using U8 = V<uint8_t >;
SI F if_then_else(I32 c, F t, F e) { return sk_bit_cast<Vec<4,float>>(__lsx_vbitsel_v(sk_bit_cast<__m128i>(e),
sk_bit_cast<__m128i>(t),
sk_bit_cast<__m128i>(c)));
}
SI F min(F a, F b) { return __lsx_vfmin_s(a,b); }
SI F max(F a, F b) { return __lsx_vfmax_s(a,b); }
SI I32 min(I32 a, I32 b) { return __lsx_vmin_w(a,b); }
SI U32 min(U32 a, U32 b) { return __lsx_vmin_wu(a,b); }
SI I32 max(I32 a, I32 b) { return __lsx_vmax_w(a,b); }
SI U32 max(U32 a, U32 b) { return __lsx_vmax_wu(a,b); }
SI F mad(F f, F m, F a) { return __lsx_vfmadd_s(f, m, a); }
SI F nmad(F f, F m, F a) { return __lsx_vfmadd_s(-f, m, a); }
SI F abs_(F v) { return (F)__lsx_vand_v((I32)v, (I32)(0-v)); }
SI I32 abs_(I32 v) { return max(v, -v); }
SI F rcp_approx (F v) { return __lsx_vfrecip_s(v); }
SI F rcp_precise (F v) { F e = rcp_approx(v); return e * nmad(v, e, F() + 2.0f); }
SI F rsqrt_approx (F v) { return __lsx_vfrsqrt_s(v); }
SI F sqrt_(F v) { return __lsx_vfsqrt_s (v); }
SI U32 iround(F v) {
F t = F() + 0.5f; return __lsx_vftintrz_w_s(v + t); }
SI U32 round(F v) {
F t = F() + 0.5f; return __lsx_vftintrz_w_s(v + t); }
SI U16 pack(U32 v) {
__m128i tmp = __lsx_vsat_wu(v, 15); auto p = __lsx_vpickev_h(tmp, tmp); return sk_unaligned_load<U16>(&p); // We have two copies. Return (the lower) one.
}
SI U8 pack(U16 v) { auto r = widen_cast<__m128i>(v);
__m128i tmp = __lsx_vsat_hu(r, 7);
r = __lsx_vpickev_b(tmp, tmp); return sk_unaligned_load<U8>(&r);
}
template <typename T>
SI V<T> gather(const T* p, U32 ix) { return V<T>{p[ix[0]], p[ix[1]], p[ix[2]], p[ix[3]]};
} // Using 'int*' prevents data from passing through floating-point registers.
SI F gather(const int* p, int ix0, int ix1, int ix2, int ix3) {
F ret = {0.0};
ret = (F)__lsx_vinsgr2vr_w(ret, p[ix0], 0);
ret = (F)__lsx_vinsgr2vr_w(ret, p[ix1], 1);
ret = (F)__lsx_vinsgr2vr_w(ret, p[ix2], 2);
ret = (F)__lsx_vinsgr2vr_w(ret, p[ix3], 3); return ret;
} template <typename T>
SI V<T> gather_unaligned(const T* p, U32 ix) { return gather(p, ix);
}
template <typename V, typename S>
SI void scatter_masked(V src, S* dst, U32 ix, I32 mask) {
V before = gather(dst, ix);
V after = if_then_else(mask, src, before);
dst[ix[0]] = after[0];
dst[ix[1]] = after[1];
dst[ix[2]] = after[2];
dst[ix[3]] = after[3];
}
SI void store4(float* ptr, F r, F g, F b, F a) {
_LSX_TRANSPOSE4_S(r,g,b,a);
__lsx_vst(r, ptr, 0);
__lsx_vst(g, ptr, 16);
__lsx_vst(b, ptr, 32);
__lsx_vst(a, ptr, 48);
}
#endif
// Helpers to do scalar -> vector promotion on GCC (clang does this automatically) // We need to subtract (not add) zero to keep float conversion zero-cost. See: // https://stackoverflow.com/q/48255293 // // The GCC implementation should be usable everywhere, but Mac clang (only) complains that the // expressions make these functions not constexpr. // // Further: We can't use the subtract-zero version in scalar mode. There, the subtraction will // really happen (at least at low optimization levels), which can alter the bit pattern of NaNs. // Because F_() is used when copying uniforms (even integer uniforms), this can corrupt values. // The vector subtraction of zero doesn't appear to ever alter NaN bit patterns. #ifdefined(__clang__) || defined(SKRP_CPU_SCALAR)
SI constexpr F F_(float x) { return x; }
SI constexpr I32 I32_(int32_t x) { return x; }
SI constexpr U32 U32_(uint32_t x) { return x; } #else
SI constexpr F F_(float x) { return x - F(); }
SI constexpr I32 I32_(int32_t x) { return x + I32(); }
SI constexpr U32 U32_(uint32_t x) { return x + U32(); } #endif
// Extremely helpful literals: static constexpr F F0 = F_(0.0f),
F1 = F_(1.0f);
#if !defined(SKRP_CPU_SCALAR)
SI F min(F a, float b) { return min(a, F_(b)); }
SI F min(float a, F b) { return min(F_(a), b); }
SI F max(F a, float b) { return max(a, F_(b)); }
SI F max(float a, F b) { return max(F_(a), b); }
SI F mad(F f, F m, float a) { return mad(f, m, F_(a)); }
SI F mad(F f, float m, F a) { return mad(f, F_(m), a); }
SI F mad(F f, float m, float a) { return mad(f, F_(m), F_(a)); }
SI F mad(float f, F m, F a) { return mad(F_(f), m, a); }
SI F mad(float f, F m, float a) { return mad(F_(f), m, F_(a)); }
SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a); }
SI F nmad(F f, F m, float a) { return nmad(f, m, F_(a)); }
SI F nmad(F f, float m, F a) { return nmad(f, F_(m), a); }
SI F nmad(F f, float m, float a) { return nmad(f, F_(m), F_(a)); }
SI F nmad(float f, F m, F a) { return nmad(F_(f), m, a); }
SI F nmad(float f, F m, float a) { return nmad(F_(f), m, F_(a)); }
SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a); } #endif
// We need to be a careful with casts. // (F)x means cast x to float in the portable path, but bit_cast x to float in the others. // These named casts and bit_cast() are always what they seem to be. #ifdefined(SKRP_CPU_SCALAR)
SI F cast (U32 v) { return (F)v; }
SI F cast64(U64 v) { return (F)v; }
SI U32 trunc_(F v) { return (U32)v; }
SI U32 expand(U16 v) { return (U32)v; }
SI U32 expand(U8 v) { return (U32)v; } #else
SI F cast (U32 v) { return __builtin_convertvector((I32)v, F); }
SI F cast64(U64 v) { return __builtin_convertvector( v, F); }
SI U32 trunc_(F v) { return (U32)__builtin_convertvector( v, I32); }
SI U32 expand(U16 v) { return __builtin_convertvector( v, U32); }
SI U32 expand(U8 v) { return __builtin_convertvector( v, U32); } #endif
#if !defined(SKRP_CPU_SCALAR)
SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
SI F if_then_else(I32 c, float t, float e) { return if_then_else(c, F_(t), F_(e)); } #endif
#else // Remember, a float is 1-8-23 (sign-exponent-mantissa) with 127 exponent bias.
U32 sem = sk_bit_cast<U32>(f),
s = sem & 0x80000000,
em = sem ^ s;
// Convert to 1-5-10 half with 15 bias, flushing denorm halfs (including zero) to zero. auto denorm = (I32)em < 0x38800000; // I32 comparison is often quicker, and always safe here. return pack((U32)if_then_else(denorm, I32_(0)
, (I32)((s>>16) + (em>>13) - ((127-15)<<10)))); #endif
}
#ifdefined(SKRP_CPU_SCALAR) || defined(SKRP_CPU_SSE2) // In scalar and SSE2 mode, we always use precise math so we can have more predictable results. // Chrome will use the SSE2 implementation when --disable-skia-runtime-opts is set. (b/40042946)
SI F rcp_fast(F v) { return rcp_precise(v); }
SI F rsqrt(F v) { return rcp_precise(sqrt_(v)); } #else
SI F rcp_fast(F v) { return rcp_approx(v); }
SI F rsqrt(F v) { return rsqrt_approx(v); } #endif
// Our fundamental vector depth is our pixel stride. static constexpr size_t N = sizeof(F) / sizeof(float);
// We're finally going to get to what a Stage function looks like!
// Any custom ABI to use for all (non-externally-facing) stage functions? // Also decide here whether to use narrow (compromise) or wide (ideal) stages. #ifdefined(SK_CPU_ARM32) && defined(SKRP_CPU_NEON) // This lets us pass vectors more efficiently on 32-bit ARM. // We can still only pass 16 floats, so best as 4x {r,g,b,a}. #define ABI __attribute__((pcs("aapcs-vfp"))) #define SKRP_NARROW_STAGES 1 #elifdefined(_MSC_VER) // Even if not vectorized, this lets us pass {r,g,b,a} as registers, // instead of {b,a} on the stack. Narrow stages work best for __vectorcall. #define ABI __vectorcall #define SKRP_NARROW_STAGES 1 #elifdefined(__x86_64__) || defined(SK_CPU_ARM64) || defined(SK_CPU_LOONGARCH) // These platforms are ideal for wider stages, and their default ABI is ideal. #define ABI #define SKRP_NARROW_STAGES 0 #else // 32-bit or unknown... shunt them down the narrow path. // Odds are these have few registers and are better off there. #define ABI #define SKRP_NARROW_STAGES 1 #endif
#if SKRP_NARROW_STAGES struct Params {
size_t dx, dy;
std::byte* base;
F dr,dg,db,da;
}; using Stage = void(ABI*)(Params*, SkRasterPipelineStage* program, F r, F g, F b, F a); #else using Stage = void(ABI*)(SkRasterPipelineStage* program, const size_t dx, const size_t dy,
std::byte* base, F,F,F,F, F,F,F,F); #endif
#if SKRP_NARROW_STAGES #define DECLARE_HIGHP_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
SI STAGE_RET name##_k(ARG, 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); \ staticvoid ABI name(Params* params, SkRasterPipelineStage* program, \
F r, F g, F b, F a) { \
OFFSET name##_k(Ctx{program}, params->dx,params->dy,params->base, \
r,g,b,a, params->dr, params->dg, params->db, params->da); \
INC; \ auto fn = (Stage)program->fn; \
MUSTTAIL return fn(params, program, r,g,b,a); \
} \
SI STAGE_RET name##_k(ARG, 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) #else #define DECLARE_HIGHP_STAGE(name, ARG, STAGE_RET, INC, OFFSET, MUSTTAIL) \
SI STAGE_RET name##_k(ARG, 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); \ staticvoid ABI name(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) { \
OFFSET name##_k(Ctx{program}, dx,dy,base, r,g,b,a, dr,dg,db,da); \
INC; \ auto fn = (Stage)program->fn; \
MUSTTAIL return fn(program, dx,dy,base, r,g,b,a, dr,dg,db,da); \
} \
SI STAGE_RET name##_k(ARG, 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) #endif
// A typical stage returns void, always increments the program counter by 1, and lets the optimizer // decide whether or not tail-calling is appropriate. #define HIGHP_STAGE(name, arg) \
DECLARE_HIGHP_STAGE(name, arg, void, ++program, /*no offset*/, /*no musttail*/)
// A tail stage returns void, always increments the program counter by 1, and uses tail-calling. // Tail-calling is necessary in SkSL-generated programs, which can be thousands of ops long, and // could overflow the stack (particularly in debug). #define HIGHP_TAIL_STAGE(name, arg) \
DECLARE_HIGHP_STAGE(name, arg, void, ++program, /*no offset*/, SKRP_MUSTTAIL)
// A branch stage returns an integer, which is added directly to the program counter, and tailcalls. #define HIGHP_BRANCH_STAGE(name, arg) \
DECLARE_HIGHP_STAGE(name, arg, int, /*no increment*/, program +=, SKRP_MUSTTAIL)
// just_return() is a simple no-op stage that only exists to end the chain, // returning back up to start_pipeline(), and from there to the caller. #if SKRP_NARROW_STAGES staticvoid ABI just_return(Params*, SkRasterPipelineStage*, F,F,F,F) {} #else staticvoid ABI just_return(SkRasterPipelineStage*, size_t,size_t, std::byte*,
F,F,F,F, F,F,F,F) {} #endif
// Note that in release builds, most stages consume no stack (thanks to tail call optimization). // However: certain builds (especially with non-clang compilers) may fail to optimize tail // calls, resulting in actual stack frames being generated. // // stack_checkpoint() and stack_rewind() are special stages that can be used to manage stack growth. // If a pipeline contains a stack_checkpoint, followed by any number of stack_rewind (at any point), // the C++ stack will be reset to the state it was at when the stack_checkpoint was initially hit. // // All instances of stack_rewind (as well as the one instance of stack_checkpoint near the start of // a pipeline) share a single context (of type SkRasterPipelineContexts::RewindCtx). That context holds the // full state of the mutable registers that are normally passed to the next stage in the program. // // stack_rewind is the only stage other than just_return that actually returns (rather than jumping // to the next stage in the program). Before it does so, it stashes all of the registers in the // context. This includes the updated `program` pointer. Unlike stages that tail call exactly once, // stack_checkpoint calls the next stage in the program repeatedly, as long as the `program` in the // context is overwritten (i.e., as long as a stack_rewind was the reason the pipeline returned, // rather than a just_return). // // Normally, just_return is the only stage that returns, and no other stage does anything after a // subsequent (called) stage returns, so the stack just unwinds all the way to start_pipeline. // With stack_checkpoint on the stack, any stack_rewind stages will return all the way up to the // stack_checkpoint. That grabs the values that would have been passed to the next stage (from the // context), and continues the linear execution of stages, but has reclaimed all of the stack frames // pushed before the stack_rewind before doing so. #if SKRP_NARROW_STAGES staticvoid ABI stack_checkpoint(Params* params, SkRasterPipelineStage* program,
F r, F g, F b, F a) {
SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program}; while (program) { auto next = (Stage)(++program)->fn;
if (program) {
r = sk_unaligned_load<F>(ctx->r );
g = sk_unaligned_load<F>(ctx->g );
b = sk_unaligned_load<F>(ctx->b );
a = sk_unaligned_load<F>(ctx->a );
params->dr = sk_unaligned_load<F>(ctx->dr);
params->dg = sk_unaligned_load<F>(ctx->dg);
params->db = sk_unaligned_load<F>(ctx->db);
params->da = sk_unaligned_load<F>(ctx->da);
params->base = ctx->base;
}
}
} staticvoid ABI stack_rewind(Params* params, SkRasterPipelineStage* program,
F r, F g, F b, F a) {
SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
sk_unaligned_store(ctx->r , r );
sk_unaligned_store(ctx->g , g );
sk_unaligned_store(ctx->b , b );
sk_unaligned_store(ctx->a , a );
sk_unaligned_store(ctx->dr, params->dr);
sk_unaligned_store(ctx->dg, params->dg);
sk_unaligned_store(ctx->db, params->db);
sk_unaligned_store(ctx->da, params->da);
ctx->base = params->base;
ctx->stage = program;
} #else staticvoid ABI 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) {
SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program}; while (program) { auto next = (Stage)(++program)->fn;
ctx->stage = nullptr;
next(program, dx, dy, base, r, g, b, a, dr, dg, db, da);
program = ctx->stage;
if (program) {
r = sk_unaligned_load<F>(ctx->r );
g = sk_unaligned_load<F>(ctx->g );
b = sk_unaligned_load<F>(ctx->b );
a = sk_unaligned_load<F>(ctx->a );
dr = sk_unaligned_load<F>(ctx->dr);
dg = sk_unaligned_load<F>(ctx->dg);
db = sk_unaligned_load<F>(ctx->db);
da = sk_unaligned_load<F>(ctx->da);
base = ctx->base;
}
}
} staticvoid ABI 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) {
SkRasterPipelineContexts::RewindCtx* ctx = Ctx{program};
sk_unaligned_store(ctx->r , r );
sk_unaligned_store(ctx->g , g );
sk_unaligned_store(ctx->b , b );
sk_unaligned_store(ctx->a , a );
sk_unaligned_store(ctx->dr, dr);
sk_unaligned_store(ctx->dg, dg);
sk_unaligned_store(ctx->db, db);
sk_unaligned_store(ctx->da, da);
ctx->base = base;
ctx->stage = program;
} #endif
// We could start defining normal Stages now. But first, some helper functions.
template <typename V, typename T>
SI V load(const T* src) { return sk_unaligned_load<V>(src);
}
template <typename V, typename T>
SI void store(T* dst, V v) {
sk_unaligned_store(dst, v);
}
// Used by load_ and store_ stages to get to the right (dx,dy) starting point of contiguous memory. template <typename T>
SI T* ptr_at_xy(const SkRasterPipelineContexts::MemoryCtx* ctx, const size_t dx, const size_t dy) { return (T*)ctx->pixels + dy*ctx->stride + dx;
}
// clamp v to [0,limit).
SI F clamp(F v, F limit) {
F inclusive = sk_bit_cast<F>(sk_bit_cast<U32>(limit) - 1); // Exclusive -> inclusive. return min(max(0.0f, v), inclusive);
}
// clamp to (0,limit).
SI F clamp_ex(F v, float limit) { const F inclusiveZ = F_(std::numeric_limits<float>::min()),
inclusiveL = sk_bit_cast<F>( sk_bit_cast<U32>(F_(limit)) - 1 ); return min(max(inclusiveZ, v), inclusiveL);
}
// Polynomial approximation of degree 5 for sin(x * 2 * pi) in the range [-1/4, 1/4] // Adapted from https://github.com/google/swiftshader/blob/master/docs/Sin-Cos-Optimization.pdf
SI F sin5q_(F x) { // A * x + B * x^3 + C * x^5 // Exact at x = 0, 1/12, 1/6, 1/4, and their negatives, // which correspond to x * 2 * pi = 0, pi/6, pi/3, pi/2
constexpr float A = 6.28230858f;
constexpr float B = -41.1693687f;
constexpr float C = 74.4388885f;
F x2 = x * x; return x * mad(mad(x2, C, B), x2, A);
}
SI F sin_(F x) {
constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
x = mad(x, -one_over_pi2, 0.25f);
x = 0.25f - abs_(x - floor_(x + 0.5f)); return sin5q_(x);
}
SI F cos_(F x) {
constexpr float one_over_pi2 = 1 / (2 * SK_FloatPI);
x *= one_over_pi2;
x = 0.25f - abs_(x - floor_(x + 0.5f)); return sin5q_(x);
}
/* "GENERATING ACCURATE VALUES FOR THE TANGENT FUNCTION" https://mae.ufl.edu/~uhk/ACCURATE-TANGENT.pdf
1+tan(x') tan(x)=------------ 1-tan(x')
*/
SI F tan_(F x) {
constexpr float Pi = SK_FloatPI; // periodic between -pi/2 ... pi/2 // shift to 0...Pi, scale 1/Pi to get into 0...1, then fract, scale-up, shift-back
x = mad(fract(mad(x, 1/Pi, 0.5f)), Pi, -Pi/2);
// Use identity atan(x) = pi/2 - atan(1/x) for x > 1
SI F atan_(F x) {
I32 neg = (x < 0.0f);
x = if_then_else(neg, -x, x);
I32 flip = (x > 1.0f);
x = if_then_else(flip, 1/x, x);
x = approx_atan_unit(x);
x = if_then_else(flip, SK_FloatPI/2 - x, x);
x = if_then_else(neg, -x, x); return x;
}
// Handbook of Mathematical Functions, by Milton Abramowitz and Irene Stegun: // 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 // http://screen/8YGJxUGFQ49bVX6
SI F asin_(F x) {
I32 neg = (x < 0.0f);
x = if_then_else(neg, -x, x); const float c3 = -0.0187293f; const float c2 = 0.0742610f; const float c1 = -0.2121144f; const float c0 = 1.5707288f;
F poly = mad(x, mad(x, mad(x, c3, c2), c1), c0);
x = nmad(sqrt_(1 - x), poly, SK_FloatPI/2);
x = if_then_else(neg, -x, x); return x;
}
SI F acos_(F x) { return SK_FloatPI/2 - asin_(x);
}
/* Use identity atan(x) = pi/2 - atan(1/x) for x > 1 Byswappingy,xtoensuretheratiois<=1,wecansafelycallatan_unit() whichavoidsa2nddivideinstructionifwehadinsteadcalledatan().
*/
SI F atan2_(F y0, F x0) {
I32 flip = (abs_(y0) > abs_(x0));
F y = if_then_else(flip, x0, y0);
F x = if_then_else(flip, y0, x0);
F arg = y/x;
F r = approx_atan_unit(arg);
r = if_then_else(flip, SK_FloatPI/2 - r, r);
r = if_then_else(neg, -r, r);
// handle quadrant distinctions
r = if_then_else((y0 >= 0) & (x0 < 0), r + SK_FloatPI, r);
r = if_then_else((y0 < 0) & (x0 <= 0), r - SK_FloatPI, r); // Note: we don't try to handle 0,0 or infinities return r;
}
// Used by gather_ stages to calculate the base pointer and a vector of indices to load. template <typename T>
SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, F x, F y) { // We use exclusive clamp so that our min value is > 0 because ULP subtraction using U32 would // produce a NaN if applied to +0.f.
x = clamp_ex(x, ctx->width );
y = clamp_ex(y, ctx->height);
x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
*ptr = (const T*)ctx->pixels; return trunc_(y)*ctx->stride + trunc_(x);
}
// We often have a nominally [0,1] float value we need to scale and convert to an integer, // whether for a table lookup or to pack back down into bytes for storage. The floating point // value is mapped to an integer using the equation "v * scale + bias". // // In practice, especially when dealing with interesting color spaces, that notionally // [0,1] float may be out of [0,1] range. Unorms cannot represent that, so we must clamp to // [0,maxI] after the bias and scale has been applied to `v`. This allows callers that explicitly // support negative float values (extended range) to still pack to a unorm. // // In most cases bias is 0 and the max value equals `scale`, but you can adjust the expected input // by tweaking `maxI` relative to `scale`.
SI U32 to_unorm(F v, float scale, float bias, int maxI) { // Any time we use round() we probably want to use to_unorm(). return round(min(max(0.0f, mad(v, scale, bias)), (float) maxI));
}
SI U32 to_unorm(F v, int scale) { return to_unorm(v, (float) scale, /*bias=*/0.f, /*maxI=*/scale);
}
SI I32 cond_to_mask(I32 cond) { #ifdefined(SKRP_CPU_SCALAR) // In scalar mode, conditions are bools (0 or 1), but we want to store and operate on masks // (eg, using bitwise operations to select values). return if_then_else(cond, I32(~0), I32(0)); #else // In SIMD mode, our various instruction sets already represent conditions as masks. return cond; #endif
}
#ifdefined(SKRP_CPU_SCALAR) // In scalar mode, `data` only contains a single lane.
SI uint32_t select_lane(uint32_t data, int /*lane*/) { return data; }
SI int32_t select_lane( int32_t data, int /*lane*/) { return data; } #else // In SIMD mode, `data` contains a vector of lanes.
SI uint32_t select_lane(U32 data, int lane) { return data[lane]; }
SI int32_t select_lane(I32 data, int lane) { return data[lane]; } #endif
// It's important for speed to explicitly cast(dx) and cast(dy), // which has the effect of splatting them to vectors before converting to floats. // On Intel this breaks a data dependency on previous loop iterations' registers.
r = cast(U32_(dx)) + sk_unaligned_load<F>(iota);
g = cast(U32_(dy)) + 0.5f;
b = F1; // This is w=1 for matrix multiplies by the device coords.
a = F0;
}
HIGHP_STAGE(dither, const float* rate) { // Get [(dx,dy), (dx+1,dy), (dx+2,dy), ...] loaded up in integer vectors.
uint32_t iota[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
U32 X = U32_(dx) + sk_unaligned_load<U32>(iota),
Y = U32_(dy);
// We only need X and X^Y from here on, so it's easier to just think of that as "Y".
Y ^= X;
// We'll mix the bottom 3 bits of each of X and Y to make 6 bits, // for 2^6 == 64 == 8x8 matrix values. If X=abc and Y=def, we make fcebda.
U32 M = (Y & 1) << 5 | (X & 1) << 4
| (Y & 2) << 2 | (X & 2) << 1
| (Y & 4) >> 1 | (X & 4) >> 2;
// Scale that dither to [0,1), then (-0.5,+0.5), here using 63/128 = 0.4921875 as 0.5-epsilon. // We want to make sure our dither is less than 0.5 in either direction to keep exact values // like 0 and 1 unchanged after rounding.
F dither = mad(cast(M), 2/128.0f, -63/128.0f);
r = mad(dither, *rate, r);
g = mad(dither, *rate, g);
b = mad(dither, *rate, b);
r = max(0.0f, min(r, a));
g = max(0.0f, min(g, a));
b = max(0.0f, min(b, a));
}
// load 4 floats from memory, and splat them into r,g,b,a
HIGHP_STAGE(uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
r = F_(c->r);
g = F_(c->g);
b = F_(c->b);
a = F_(c->a);
}
HIGHP_STAGE(unbounded_uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
r = F_(c->r);
g = F_(c->g);
b = F_(c->b);
a = F_(c->a);
} // load 4 floats from memory, and splat them into dr,dg,db,da
HIGHP_STAGE(uniform_color_dst, const SkRasterPipelineContexts::UniformColorCtx* c) {
dr = F_(c->r);
dg = F_(c->g);
db = F_(c->b);
da = F_(c->a);
}
// splats opaque-black into r,g,b,a
HIGHP_STAGE(black_color, NoCtx) {
r = g = b = F0;
a = F1;
}
HIGHP_STAGE(white_color, NoCtx) {
r = g = b = a = F1;
}
// load registers r,g,b,a from context (mirrors store_src)
HIGHP_STAGE(load_src, const float* ptr) {
r = sk_unaligned_load<F>(ptr + 0*N);
g = sk_unaligned_load<F>(ptr + 1*N);
b = sk_unaligned_load<F>(ptr + 2*N);
a = sk_unaligned_load<F>(ptr + 3*N);
}
// store registers r,g,b,a into context (mirrors load_src)
HIGHP_STAGE(store_src, float* ptr) {
sk_unaligned_store(ptr + 0*N, r);
sk_unaligned_store(ptr + 1*N, g);
sk_unaligned_store(ptr + 2*N, b);
sk_unaligned_store(ptr + 3*N, a);
} // store registers r,g into context
HIGHP_STAGE(store_src_rg, float* ptr) {
sk_unaligned_store(ptr + 0*N, r);
sk_unaligned_store(ptr + 1*N, g);
} // load registers r,g from context
HIGHP_STAGE(load_src_rg, float* ptr) {
r = sk_unaligned_load<F>(ptr + 0*N);
g = sk_unaligned_load<F>(ptr + 1*N);
} // store register a into context
HIGHP_STAGE(store_src_a, float* ptr) {
sk_unaligned_store(ptr, a);
}
// load registers dr,dg,db,da from context (mirrors store_dst)
HIGHP_STAGE(load_dst, const float* ptr) {
dr = sk_unaligned_load<F>(ptr + 0*N);
dg = sk_unaligned_load<F>(ptr + 1*N);
db = sk_unaligned_load<F>(ptr + 2*N);
da = sk_unaligned_load<F>(ptr + 3*N);
}
// Most blend modes apply the same logic to each channel. #define BLEND_MODE(name) \
SI F name##_channel(F s, F d, F sa, F da); \
HIGHP_STAGE(name, NoCtx) { \
r = name##_channel(r,dr,a,da); \
g = name##_channel(g,dg,a,da); \
b = name##_channel(b,db,a,da); \
a = name##_channel(a,da,a,da); \
} \
SI F name##_channel(F s, F d, F sa, F da)
SI F inv(F x) { return1.0f - x; }
SI F two(F x) { return x + x; }
BLEND_MODE(clear) { return F0; }
BLEND_MODE(srcatop) { return mad(s, da, d*inv(sa)); }
BLEND_MODE(dstatop) { return mad(d, sa, s*inv(da)); }
BLEND_MODE(srcin) { return s * da; }
BLEND_MODE(dstin) { return d * sa; }
BLEND_MODE(srcout) { return s * inv(da); }
BLEND_MODE(dstout) { return d * inv(sa); }
BLEND_MODE(srcover) { return mad(d, inv(sa), s); }
BLEND_MODE(dstover) { return mad(s, inv(da), d); }
BLEND_MODE(modulate) { return s*d; }
BLEND_MODE(multiply) { return mad(s, d, mad(s, inv(da), d*inv(sa))); }
BLEND_MODE(plus_) { return min(s + d, 1.0f); } // We can clamp to either 1 or sa.
BLEND_MODE(screen) { return nmad(s, d, s + d); }
BLEND_MODE(xor_) { return mad(s, inv(da), d*inv(sa)); } #undef BLEND_MODE
// Most other blend modes apply the same logic to colors, and srcover to alpha. #define BLEND_MODE(name) \
SI F name##_channel(F s, F d, F sa, F da); \
HIGHP_STAGE(name, NoCtx) { \
r = name##_channel(r,dr,a,da); \
g = name##_channel(g,dg,a,da); \
b = name##_channel(b,db,a,da); \
a = mad(da, inv(a), a); \
} \
SI F name##_channel(F s, F d, F sa, F da)
BLEND_MODE(darken) { return s + d - max(s*da, d*sa) ; }
BLEND_MODE(lighten) { return s + d - min(s*da, d*sa) ; }
BLEND_MODE(difference) { return s + d - two(min(s*da, d*sa)); }
BLEND_MODE(exclusion) { return s + d - two(s*d); }
SI F sat(F r, F g, F b) { return max(r, max(g,b)) - min(r, min(g,b)); }
SI F lum(F r, F g, F b) { return mad(r, 0.30f, mad(g, 0.59f, b*0.11f)); }
SI void set_sat(F* r, F* g, F* b, F s) {
F mn = min(*r, min(*g,*b)),
mx = max(*r, max(*g,*b)),
sat = mx - mn;
// Map min channel to 0, max channel to s, and scale the middle proportionally.
s = if_then_else(sat == 0.0f, 0.0f, s * rcp_fast(sat));
*r = (*r - mn) * s;
*g = (*g - mn) * s;
*b = (*b - mn) * s;
}
SI void set_lum(F* r, F* g, F* b, F l) {
F diff = l - lum(*r, *g, *b);
*r += diff;
*g += diff;
*b += diff;
}
SI F clip_channel(F c, F l, I32 clip_low, I32 clip_high, F mn_scale, F mx_scale) {
c = if_then_else(clip_low, mad(mn_scale, c - l, l), c);
c = if_then_else(clip_high, mad(mx_scale, c - l, l), c);
c = max(c, 0.0f); // Sometimes without this we may dip just a little negative. return c;
}
SI void clip_color(F* r, F* g, F* b, F a) {
F mn = min(*r, min(*g, *b)),
mx = max(*r, max(*g, *b)),
l = lum(*r, *g, *b),
mn_scale = ( l) * rcp_fast(l - mn),
mx_scale = (a - l) * rcp_fast(mx - l);
I32 clip_low = cond_to_mask(mn < 0 && l != mn),
clip_high = cond_to_mask(mx > a && l != mx);
r = mad(r, inv(da), mad(dr, inv(a), R));
g = mad(g, inv(da), mad(dg, inv(a), G));
b = mad(b, inv(da), mad(db, inv(a), B));
a = a + nmad(a, da, da);
}
HIGHP_STAGE(saturation, NoCtx) {
F R = dr*a,
G = dg*a,
B = db*a;
set_sat(&R, &G, &B, sat( r, g, b)*da);
set_lum(&R, &G, &B, lum(dr,dg,db)* a); // (This is not redundant.)
clip_color(&R,&G,&B, a*da);
r = mad(r, inv(da), mad(dr, inv(a), R));
g = mad(g, inv(da), mad(dg, inv(a), G));
b = mad(b, inv(da), mad(db, inv(a), B));
a = a + nmad(a, da, da);
}
HIGHP_STAGE(color, NoCtx) {
F R = r*da,
G = g*da,
B = b*da;
r = mad(r, inv(da), mad(dr, inv(a), R));
g = mad(g, inv(da), mad(dg, inv(a), G));
b = mad(b, inv(da), mad(db, inv(a), B));
a = a + nmad(a, da, da);
}
HIGHP_STAGE(luminosity, NoCtx) {
F R = dr*a,
G = dg*a,
B = db*a;
r = mad(r, inv(da), mad(dr, inv(a), R));
g = mad(g, inv(da), mad(dg, inv(a), G));
b = mad(b, inv(da), mad(db, inv(a), B));
a = a + nmad(a, da, da);
}
HIGHP_STAGE(srcover_rgba_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
U32 dst = load<U32>(ptr);
dr = cast((dst ) & 0xff);
dg = cast((dst >> 8) & 0xff);
db = cast((dst >> 16) & 0xff);
da = cast((dst >> 24) ); // {dr,dg,db,da} are in [0,255] // { r, g, b, a} are in [0, 1] (but may be out of gamut)
r = mad(dr, inv(a), r*255.0f);
g = mad(dg, inv(a), g*255.0f);
b = mad(db, inv(a), b*255.0f);
a = mad(da, inv(a), a*255.0f); // { r, g, b, a} are now in [0,255] (but may be out of gamut)
SI F clamp_01_(F v) { return min(max(0.0f, v), 1.0f); }
HIGHP_STAGE(clamp_01, NoCtx) {
r = clamp_01_(r);
g = clamp_01_(g);
b = clamp_01_(b);
a = clamp_01_(a);
}
HIGHP_STAGE(clamp_a_01, NoCtx) {
a = clamp_01_(a);
}
HIGHP_STAGE(clamp_gamut, NoCtx) {
a = min(max(a, 0.0f), 1.0f);
r = min(max(r, 0.0f), a);
g = min(max(g, 0.0f), a);
b = min(max(b, 0.0f), a);
}
HIGHP_STAGE(set_rgb, const float* rgb) {
r = F_(rgb[0]);
g = F_(rgb[1]);
b = F_(rgb[2]);
}
HIGHP_STAGE(unbounded_set_rgb, const float* rgb) {
r = F_(rgb[0]);
g = F_(rgb[1]);
b = F_(rgb[2]);
}
HIGHP_STAGE(swap_rb, NoCtx) { auto tmp = r;
r = b;
b = tmp;
}
HIGHP_STAGE(swap_rb_dst, NoCtx) { auto tmp = dr;
dr = db;
db = tmp;
}
HIGHP_STAGE(move_src_dst, NoCtx) {
dr = r;
dg = g;
db = b;
da = a;
}
HIGHP_STAGE(move_dst_src, NoCtx) {
r = dr;
g = dg;
b = db;
a = da;
}
HIGHP_STAGE(swap_src_dst, NoCtx) {
std::swap(r, dr);
std::swap(g, dg);
std::swap(b, db);
std::swap(a, da);
}
HIGHP_STAGE(premul, NoCtx) {
r = r * a;
g = g * a;
b = b * a;
}
HIGHP_STAGE(premul_dst, NoCtx) {
dr = dr * da;
dg = dg * da;
db = db * da;
}
HIGHP_STAGE(unpremul, NoCtx) {
float inf = sk_bit_cast<float>(0x7f800000); auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
r *= scale;
g *= scale;
b *= scale;
}
HIGHP_STAGE(unpremul_polar, NoCtx) {
float inf = sk_bit_cast<float>(0x7f800000); auto scale = if_then_else(1.0f/a < inf, 1.0f/a, 0.0f);
g *= scale;
b *= scale;
}
HIGHP_STAGE(force_opaque , NoCtx) { a = F1; }
HIGHP_STAGE(force_opaque_dst, NoCtx) { da = F1; }
F l = (mx + mn) * 0.5f;
F s = if_then_else(mx == mn, 0.0f,
d / if_then_else(l > 0.5f, 2.0f-mx-mn, mx+mn));
r = h;
g = s;
b = l;
}
HIGHP_STAGE(hsl_to_rgb, NoCtx) { // See GrRGBToHSLFilterEffect.fp
F h = r,
s = g,
l = b,
c = (1.0f - abs_(2.0f * l - 1)) * s;
auto hue_to_rgb = [&](F hue) {
F q = clamp_01_(abs_(fract(hue) * 6.0f - 3.0f) - 1.0f); return (q - 0.5f) * c + l;
};
r = hue_to_rgb(h + 0.0f/3.0f);
g = hue_to_rgb(h + 2.0f/3.0f);
b = hue_to_rgb(h + 1.0f/3.0f);
}
// Color conversion functions used in gradient interpolation, based on // https://www.w3.org/TR/css-color-4/#color-conversion-code
HIGHP_STAGE(css_lab_to_xyz, NoCtx) {
constexpr float k = 24389 / 27.0f;
constexpr float e = 216 / 24389.0f;
F f_cubed[3] = { f[0]*f[0]*f[0], f[1]*f[1]*f[1], f[2]*f[2]*f[2] };
F xyz[3] = {
if_then_else(f_cubed[0] > e, f_cubed[0], (116 * f[0] - 16) * (1 / k)),
if_then_else(r > k * e, f_cubed[1], r * (1 / k)),
if_then_else(f_cubed[2] > e, f_cubed[2], (116 * f[2] - 16) * (1 / k))
};
constexpr float D50[3] = { 0.3457f / 0.3585f, 1.0f, (1.0f - 0.3457f - 0.3585f) / 0.3585f };
r = xyz[0]*D50[0];
g = xyz[1]*D50[1];
b = xyz[2]*D50[2];
}
HIGHP_STAGE(css_oklab_to_linear_srgb, NoCtx) {
F l_ = r + 0.3963377774f * g + 0.2158037573f * b,
m_ = r - 0.1055613458f * g - 0.0638541728f * b,
s_ = r - 0.0894841775f * g - 1.2914855480f * b;
F l = l_*l_*l_,
m = m_*m_*m_,
s = s_*s_*s_;
r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
}
HIGHP_STAGE(css_oklab_gamut_map_to_linear_srgb, NoCtx) { // TODO(https://crbug.com/1508329): Add support for gamut mapping. // Return a greyscale value, so that accidental use is obvious.
F l_ = r,
m_ = r,
s_ = r;
F l = l_*l_*l_,
m = m_*m_*m_,
s = s_*s_*s_;
r = +4.0767416621f * l - 3.3077115913f * m + 0.2309699292f * s;
g = -1.2684380046f * l + 2.6097574011f * m - 0.3413193965f * s;
b = -0.0041960863f * l - 0.7034186147f * m + 1.7076147010f * s;
}
// Skia stores all polar colors with hue in the first component, so this "LCH -> Lab" transform // actually takes "HCL". This is also used to do the same polar transform for OkHCL to OkLAB. // See similar comments & logic in SkGradientBaseShader.cpp.
HIGHP_STAGE(css_hcl_to_lab, NoCtx) {
F H = r,
C = g,
L = b;
F hueRadians = H * (SK_FloatPI / 180);
r = L;
g = C * cos_(hueRadians);
b = C * sin_(hueRadians);
}
SI F mod_(F x, float y) { return nmad(y, floor_(x * (1 / y)), x);
}
struct RGB { F r, g, b; };
SI RGB css_hsl_to_srgb_(F h, F s, F l) {
h = mod_(h, 360);
s *= 0.01f;
l *= 0.01f;
F k[3] = {
mod_(0 + h * (1 / 30.0f), 12),
mod_(8 + h * (1 / 30.0f), 12),
mod_(4 + h * (1 / 30.0f), 12)
};
F a = s * min(l, 1 - l); return {
l - a * max(-1.0f, min(min(k[0] - 3.0f, 9.0f - k[0]), 1.0f)),
l - a * max(-1.0f, min(min(k[1] - 3.0f, 9.0f - k[1]), 1.0f)),
l - a * max(-1.0f, min(min(k[2] - 3.0f, 9.0f - k[2]), 1.0f))
};
}
HIGHP_STAGE(css_hsl_to_srgb, NoCtx) {
RGB rgb = css_hsl_to_srgb_(r, g, b);
r = rgb.r;
g = rgb.g;
b = rgb.b;
}
HIGHP_STAGE(css_hwb_to_srgb, NoCtx) {
g *= 0.01f;
b *= 0.01f;
F gray = g / (g + b);
RGB rgb = css_hsl_to_srgb_(r, F_(100.0f), F_(50.0f));
rgb.r = rgb.r * (1 - g - b) + g;
rgb.g = rgb.g * (1 - g - b) + g;
rgb.b = rgb.b * (1 - g - b) + g;
auto isGray = (g + b) >= 1;
r = if_then_else(isGray, gray, rgb.r);
g = if_then_else(isGray, gray, rgb.g);
b = if_then_else(isGray, gray, rgb.b);
}
// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
SI F alpha_coverage_from_rgb_coverage(F a, F da, F cr, F cg, F cb) { return if_then_else(a < da, min(cr, min(cg,cb))
, max(cr, max(cg,cb)));
}
HIGHP_STAGE(scale_1_float, const float* c) {
r = r * *c;
g = g * *c;
b = b * *c;
a = a * *c;
}
HIGHP_STAGE(scale_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
auto scales = load<U8>(ptr); auto c = from_byte(scales);
r = r * c;
g = g * c;
b = b * c;
a = a * c;
}
HIGHP_STAGE(scale_565, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
F cr,cg,cb;
from_565(load<U16>(ptr), &cr, &cg, &cb);
F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
r = r * cr;
g = g * cg;
b = b * cb;
a = a * ca;
}
SI F lerp(F from, F to, F t) { return mad(to-from, t, from);
}
HIGHP_STAGE(lerp_1_float, const float* c) {
r = lerp(dr, r, F_(*c));
g = lerp(dg, g, F_(*c));
b = lerp(db, b, F_(*c));
a = lerp(da, a, F_(*c));
}
HIGHP_STAGE(scale_native, const float scales[]) { auto c = sk_unaligned_load<F>(scales);
r = r * c;
g = g * c;
b = b * c;
a = a * c;
}
HIGHP_STAGE(lerp_native, const float scales[]) { auto c = sk_unaligned_load<F>(scales);
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
HIGHP_STAGE(lerp_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
auto scales = load<U8>(ptr); auto c = from_byte(scales);
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
HIGHP_STAGE(lerp_565, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint16_t>(ctx, dx,dy);
F cr,cg,cb;
from_565(load<U16>(ptr), &cr, &cg, &cb);
F ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
r = lerp(dr, r, cr);
g = lerp(dg, g, cg);
b = lerp(db, b, cb);
a = lerp(da, a, ca);
}
SI F apply_sign(F x, U32 sign) { return sk_bit_cast<F>(sign | sk_bit_cast<U32>(x));
}
HIGHP_STAGE(parametric, const skcms_TransferFunction* ctx) { auto fn = [&](F v) {
U32 sign;
v = strip_sign(v, &sign);
F r = if_then_else(v <= ctx->d, mad(ctx->c, v, ctx->f)
, approx_powf(mad(ctx->a, v, ctx->b), ctx->g) + ctx->e); return apply_sign(r, sign);
};
r = fn(r);
g = fn(g);
b = fn(b);
}
HIGHP_STAGE(gamma_, const float* G) { auto fn = [&](F v) {
U32 sign;
v = strip_sign(v, &sign); return apply_sign(approx_powf(v, *G), sign);
};
r = fn(r);
g = fn(g);
b = fn(b);
}
HIGHP_STAGE(PQish, const skcms_TransferFunction* ctx) { auto fn = [&](F v) {
U32 sign;
v = strip_sign(v, &sign);
F r = approx_powf(max(mad(ctx->b, approx_powf(v, ctx->c), ctx->a), 0.0f)
/ (mad(ctx->e, approx_powf(v, ctx->c), ctx->d)),
ctx->f);
return apply_sign(r, sign);
};
r = fn(r);
g = fn(g);
b = fn(b);
}
HIGHP_STAGE(HLGish, const skcms_TransferFunction* ctx) { auto fn = [&](F v) {
U32 sign;
v = strip_sign(v, &sign);
const float R = ctx->a, G = ctx->b,
a = ctx->c, b = ctx->d, c = ctx->e,
K = ctx->f + 1.0f;
F r = if_then_else(v*R <= 1, approx_powf(v*R, G)
, approx_exp((v-c)*a) + b);
return K * apply_sign(r, sign);
};
r = fn(r);
g = fn(g);
b = fn(b);
}
HIGHP_STAGE(HLGinvish, const skcms_TransferFunction* ctx) { auto fn = [&](F v) {
U32 sign;
v = strip_sign(v, &sign);
const float R = ctx->a, G = ctx->b,
a = ctx->c, b = ctx->d, c = ctx->e,
K = ctx->f + 1.0f;
v /= K;
F r = if_then_else(v <= 1, R * approx_powf(v, G)
, a * approx_log(v - b) + c);
return apply_sign(r, sign);
};
r = fn(r);
g = fn(g);
b = fn(b);
}
HIGHP_STAGE(ootf, const float* ctx) {
F Y = ctx[0] * r + ctx[1] * g + ctx[2] * b;
U32 sign;
Y = strip_sign(Y, &sign);
F Y_to_gamma_minus_one = apply_sign(approx_powf(Y, ctx[3]), sign);
r = r * Y_to_gamma_minus_one;
g = g * Y_to_gamma_minus_one;
b = b * Y_to_gamma_minus_one;
}
r = g = b = F0;
a = from_byte(load<U8>(ptr));
}
HIGHP_STAGE(load_a8_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint8_t>(ctx, dx,dy);
dr = dg = db = F0;
da = from_byte(load<U8>(ptr));
}
HIGHP_STAGE(gather_a8, const SkRasterPipelineContexts::GatherCtx* ctx) { const uint8_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, r,g);
r = g = b = F0;
a = from_byte(gather(ptr, ix));
}
HIGHP_STAGE(store_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint8_t>(ctx, dx,dy);
// This is the inverse of from_10101010_xr, e.g. (v * 510 + 384)
U16 R = pack(to_unorm(r, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
G = pack(to_unorm(g, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
B = pack(to_unorm(b, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6,
A = pack(to_unorm(a, /*scale=*/510, /*bias=*/384, /*maxI=*/1023)) << 6;
U16 A = load<U16>((const uint16_t*)ptr);
r = F0;
g = F0;
b = F0;
a = from_half(A);
}
HIGHP_STAGE(load_af16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
U16 A = load<U16>((const uint16_t*)ptr);
dr = dg = db = F0;
da = from_half(A);
}
HIGHP_STAGE(gather_af16, const SkRasterPipelineContexts::GatherCtx* ctx) { const uint16_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, r, g);
r = g = b = F0;
a = from_half(gather_unaligned(ptr, ix));
}
HIGHP_STAGE(store_af16, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
store(ptr, to_half(a));
}
U16 R = load<U16>((const uint16_t*)ptr);
r = from_half(R);
g = b = F0;
a = F1;
}
HIGHP_STAGE(load_rf16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint16_t>(ctx, dx, dy);
U16 R = load<U16>((const uint16_t*)ptr);
dr = from_half(R);
dg = db = F0;
da = F1;
}
HIGHP_STAGE(gather_rf16, const SkRasterPipelineContexts::GatherCtx* ctx) { const uint16_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, r, g);
r = from_half(gather_unaligned(ptr, ix));
g = b = F0;
a = F1;
}
HIGHP_STAGE(store_rf16, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint16_t>(ctx, dx,dy);
store(ptr, to_half(r));
}
U16 R,G;
load2((const uint16_t*)ptr, &R, &G);
r = from_half(R);
g = from_half(G);
b = F0;
a = F1;
}
HIGHP_STAGE(load_rgf16_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const uint32_t>(ctx, dx, dy);
U16 R,G;
load2((const uint16_t*)ptr, &R, &G);
dr = from_half(R);
dg = from_half(G);
db = F0;
da = F1;
}
HIGHP_STAGE(gather_rgf16, const SkRasterPipelineContexts::GatherCtx* ctx) { const uint32_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, r, g); auto px = gather_unaligned(ptr, ix);
U16 R,G;
load2((const uint16_t*)&px, &R, &G);
r = from_half(R);
g = from_half(G);
b = F0;
a = F1;
}
HIGHP_STAGE(store_rgf16, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx, dy);
store2((uint16_t*)ptr, to_half(r)
, to_half(g));
}
HIGHP_STAGE(load_f32, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
load4(ptr, &r,&g,&b,&a);
}
HIGHP_STAGE(load_f32_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<const float>(ctx, 4*dx,4*dy);
load4(ptr, &dr,&dg,&db,&da);
}
HIGHP_STAGE(gather_f32, const SkRasterPipelineContexts::GatherCtx* ctx) { const float* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, r,g);
r = gather_unaligned(ptr, 4*ix + 0);
g = gather_unaligned(ptr, 4*ix + 1);
b = gather_unaligned(ptr, 4*ix + 2);
a = gather_unaligned(ptr, 4*ix + 3);
}
HIGHP_STAGE(store_f32, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<float>(ctx, 4*dx,4*dy);
store4(ptr, r,g,b,a);
}
SI F exclusive_repeat(F v, const SkRasterPipelineContexts::TileCtx* ctx) { return v - floor_(v*ctx->invScale)*ctx->scale;
}
SI F exclusive_mirror(F v, const SkRasterPipelineContexts::TileCtx* ctx) { auto limit = ctx->scale; auto invLimit = ctx->invScale;
// This is "repeat" over the range 0..2*limit auto u = v - floor_(v*invLimit*0.5f)*2*limit; // s will be 0 when moving forward (e.g. [0, limit)) and 1 when moving backward (e.g. // [limit, 2*limit)). auto s = floor_(u*invLimit); // This is the mirror result. auto m = u - 2*s*(u - limit); // Apply a bias to m if moving backwards so that we snap consistently at exact integer coords in // the logical infinite image. This is tested by mirror_tile GM. Note that all values // that have a non-zero bias applied are > 0. auto biasInUlps = trunc_(s); return sk_bit_cast<F>(sk_bit_cast<U32>(m) + ctx->mirrorBiasDir*biasInUlps);
} // Tile x or y to [0,limit) == [0,limit - 1 ulp] (think, sampling from images). // The gather stages will hard clamp the output of these stages to [0,limit)... // we just need to do the basic repeat or mirroring.
HIGHP_STAGE(repeat_x, const SkRasterPipelineContexts::TileCtx* ctx) {
r = exclusive_repeat(r, ctx);
}
HIGHP_STAGE(repeat_y, const SkRasterPipelineContexts::TileCtx* ctx) {
g = exclusive_repeat(g, ctx);
}
HIGHP_STAGE(mirror_x, const SkRasterPipelineContexts::TileCtx* ctx) {
r = exclusive_mirror(r, ctx);
}
HIGHP_STAGE(mirror_y, const SkRasterPipelineContexts::TileCtx* ctx) {
g = exclusive_mirror(g, ctx);
}
HIGHP_STAGE( clamp_x_1, NoCtx) { r = clamp_01_(r); }
HIGHP_STAGE(repeat_x_1, NoCtx) { r = clamp_01_(r - floor_(r)); }
HIGHP_STAGE(mirror_x_1, NoCtx) { r = clamp_01_(abs_( (r-1.0f) - two(floor_((r-1.0f)*0.5f)) - 1.0f )); }
HIGHP_STAGE(clamp_x_and_y, const SkRasterPipelineContexts::CoordClampCtx* ctx) {
r = min(ctx->max_x, max(ctx->min_x, r));
g = min(ctx->max_y, max(ctx->min_y, g));
}
// Decal stores a 32bit mask after checking the coordinate (x and/or y) against its domain: // mask == 0x00000000 if the coordinate(s) are out of bounds // mask == 0xFFFFFFFF if the coordinate(s) are in bounds // After the gather stage, the r,g,b,a values are AND'd with this mask, setting them to 0 // if either of the coordinates were out of bounds.
HIGHP_STAGE(decal_x, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto w = ctx->limit_x; auto e = ctx->inclusiveEdge_x; auto cond = ((0 < r) & (r < w)) | (r == e);
sk_unaligned_store(ctx->mask, cond_to_mask(cond));
}
HIGHP_STAGE(decal_y, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto h = ctx->limit_y; auto e = ctx->inclusiveEdge_y; auto cond = ((0 < g) & (g < h)) | (g == e);
sk_unaligned_store(ctx->mask, cond_to_mask(cond));
}
HIGHP_STAGE(decal_x_and_y, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto w = ctx->limit_x; auto h = ctx->limit_y; auto ex = ctx->inclusiveEdge_x; auto ey = ctx->inclusiveEdge_y; auto cond = (((0 < r) & (r < w)) | (r == ex))
& (((0 < g) & (g < h)) | (g == ey));
sk_unaligned_store(ctx->mask, cond_to_mask(cond));
}
HIGHP_STAGE(check_decal_mask, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto mask = sk_unaligned_load<U32>(ctx->mask);
r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
}
HIGHP_STAGE(alpha_to_gray, NoCtx) {
r = g = b = a;
a = F1;
}
HIGHP_STAGE(alpha_to_gray_dst, NoCtx) {
dr = dg = db = da;
da = F1;
}
HIGHP_STAGE(alpha_to_red, NoCtx) {
r = a;
a = F1;
}
HIGHP_STAGE(alpha_to_red_dst, NoCtx) {
dr = da;
da = F1;
}
HIGHP_STAGE(bt709_luminance_or_luma_to_alpha, NoCtx) {
a = r*0.2126f + g*0.7152f + b*0.0722f;
r = g = b = F0;
}
HIGHP_STAGE(bt709_luminance_or_luma_to_rgb, NoCtx) {
r = g = b = r*0.2126f + g*0.7152f + b*0.0722f;
}
HIGHP_STAGE(matrix_translate, const float* m) {
r += m[0];
g += m[1];
}
HIGHP_STAGE(matrix_scale_translate, const float* m) {
r = mad(r,m[0], m[2]);
g = mad(g,m[1], m[3]);
}
HIGHP_STAGE(matrix_2x3, const float* m) { auto R = mad(r,m[0], mad(g,m[1], m[2])),
G = mad(r,m[3], mad(g,m[4], m[5]));
r = R;
g = G;
}
HIGHP_STAGE(matrix_3x3, const float* m) { auto R = mad(r,m[0], mad(g,m[3], b*m[6])),
G = mad(r,m[1], mad(g,m[4], b*m[7])),
B = mad(r,m[2], mad(g,m[5], b*m[8]));
r = R;
g = G;
b = B;
}
HIGHP_STAGE(matrix_3x4, const float* m) { auto R = mad(r,m[0], mad(g,m[3], mad(b,m[6], m[ 9]))),
G = mad(r,m[1], mad(g,m[4], mad(b,m[7], m[10]))),
B = mad(r,m[2], mad(g,m[5], mad(b,m[8], m[11])));
r = R;
g = G;
b = B;
}
HIGHP_STAGE(matrix_4x5, const float* m) { auto R = mad(r,m[ 0], mad(g,m[ 1], mad(b,m[ 2], mad(a,m[ 3], m[ 4])))),
G = mad(r,m[ 5], mad(g,m[ 6], mad(b,m[ 7], mad(a,m[ 8], m[ 9])))),
B = mad(r,m[10], mad(g,m[11], mad(b,m[12], mad(a,m[13], m[14])))),
A = mad(r,m[15], mad(g,m[16], mad(b,m[17], mad(a,m[18], m[19]))));
r = R;
g = G;
b = B;
a = A;
}
HIGHP_STAGE(matrix_4x3, const float* m) { auto X = r,
Y = g;
r = mad(X, m[0], mad(Y, m[4], m[ 8]));
g = mad(X, m[1], mad(Y, m[5], m[ 9]));
b = mad(X, m[2], mad(Y, m[6], m[10]));
a = mad(X, m[3], mad(Y, m[7], m[11]));
}
HIGHP_STAGE(matrix_perspective, const float* m) { // N.B. Unlike the other matrix_ stages, this matrix is row-major. auto R = mad(r,m[0], mad(g,m[1], m[2])),
G = mad(r,m[3], mad(g,m[4], m[5])),
Z = mad(r,m[6], mad(g,m[7], m[8]));
r = R * rcp_precise(Z);
g = G * rcp_precise(Z);
}
HIGHP_STAGE(evenly_spaced_gradient, const SkRasterPipelineContexts::GradientCtx* c) { auto t = r; auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
gradient_lookup(c, idx, t, &r, &g, &b, &a);
}
HIGHP_STAGE(gradient, const SkRasterPipelineContexts::GradientCtx* c) { auto t = r;
U32 idx = U32_(0);
// N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
for (size_t i = 1; i < c->stopCount; i++) {
idx += (U32)if_then_else(t >= c->ts[i], I32_(1), I32_(0));
}
gradient_lookup(c, idx, t, &r, &g, &b, &a);
}
HIGHP_STAGE(evenly_spaced_2_stop_gradient, const SkRasterPipelineContexts::EvenlySpaced2StopGradientCtx* c) { auto t = r;
r = mad(t, c->factor[0], c->bias[0]);
g = mad(t, c->factor[1], c->bias[1]);
b = mad(t, c->factor[2], c->bias[2]);
a = mad(t, c->factor[3], c->bias[3]);
}
HIGHP_STAGE(xy_to_unit_angle, NoCtx) {
F X = r,
Y = g;
F xabs = abs_(X),
yabs = abs_(Y);
F slope = min(xabs, yabs)/max(xabs, yabs);
F s = slope * slope;
// Use a 7th degree polynomial to approximate atan. // This was generated using sollya.gforge.inria.fr. // A float optimized polynomial was generated using the following command. // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
F phi = slope
* (0.15912117063999176025390625f + s
* (-5.185396969318389892578125e-2f + s
* (2.476101927459239959716796875e-2f + s
* (-7.0547382347285747528076171875e-3f))));
HIGHP_STAGE(xy_to_2pt_conical_strip, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
F x = r, y = g, &t = r;
t = x + sqrt_(ctx->fP0 - y*y); // ctx->fP0 = r0 * r0
}
HIGHP_STAGE(xy_to_2pt_conical_focal_on_circle, NoCtx) {
F x = r, y = g, &t = r;
t = x + y*y / x; // (x^2 + y^2) / x
}
HIGHP_STAGE(xy_to_2pt_conical_well_behaved, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
F x = r, y = g, &t = r;
t = sqrt_(x*x + y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
}
HIGHP_STAGE(xy_to_2pt_conical_greater, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
F x = r, y = g, &t = r;
t = sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
}
HIGHP_STAGE(xy_to_2pt_conical_smaller, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
F x = r, y = g, &t = r;
t = -sqrt_(x*x - y*y) - x * ctx->fP0; // ctx->fP0 = 1/r1
}
HIGHP_STAGE(alter_2pt_conical_compensate_focal, const SkRasterPipelineContexts::Conical2PtCtx* ctx) {
F& t = r;
t = t + ctx->fP1; // ctx->fP1 = f
}
HIGHP_STAGE(alter_2pt_conical_unswap, NoCtx) {
F& t = r;
t = 1 - t;
}
HIGHP_STAGE(mask_2pt_conical_nan, SkRasterPipelineContexts::Conical2PtCtx* c) {
F& t = r; auto is_degenerate = (t != t); // NaN
t = if_then_else(is_degenerate, F0, t);
sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
}
HIGHP_STAGE(mask_2pt_conical_degenerates, SkRasterPipelineContexts::Conical2PtCtx* c) {
F& t = r; auto is_degenerate = (t <= 0) | (t != t);
t = if_then_else(is_degenerate, F0, t);
sk_unaligned_store(&c->fMask, cond_to_mask(!is_degenerate));
}
HIGHP_STAGE(apply_vector_mask, const uint32_t* ctx) { const U32 mask = sk_unaligned_load<U32>(ctx);
r = sk_bit_cast<F>(sk_bit_cast<U32>(r) & mask);
g = sk_bit_cast<F>(sk_bit_cast<U32>(g) & mask);
b = sk_bit_cast<F>(sk_bit_cast<U32>(b) & mask);
a = sk_bit_cast<F>(sk_bit_cast<U32>(a) & mask);
}
SI void save_xy(F* r, F* g, SkRasterPipelineContexts::SamplerCtx* c) { // Whether bilinear or bicubic, all sample points are at the same fractional offset (fx,fy). // They're either the 4 corners of a logical 1x1 pixel or the 16 corners of a 3x3 grid // surrounding (x,y) at (0.5,0.5) off-center.
F fx = fract(*r + 0.5f),
fy = fract(*g + 0.5f);
// Samplers will need to load x and fx, or y and fy.
sk_unaligned_store(c->x, *r);
sk_unaligned_store(c->y, *g);
sk_unaligned_store(c->fx, fx);
sk_unaligned_store(c->fy, fy);
}
HIGHP_STAGE(accumulate, const SkRasterPipelineContexts::SamplerCtx* c) { // Bilinear and bicubic filters are both separable, so we produce independent contributions // from x and y, multiplying them together here to get each pixel's total scale factor. auto scale = sk_unaligned_load<F>(c->scalex)
* sk_unaligned_load<F>(c->scaley);
dr = mad(scale, r, dr);
dg = mad(scale, g, dg);
db = mad(scale, b, db);
da = mad(scale, a, da);
}
// In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center // are combined in direct proportion to their area overlapping that logical query pixel. // At positive offsets, the x-axis contribution to that rectangle is fx, or (1-fx) at negative x. // The y-axis is symmetric.
template <int kScale>
SI void bilinear_x(SkRasterPipelineContexts::SamplerCtx* ctx, F* x) {
*x = sk_unaligned_load<F>(ctx->x) + (kScale * 0.5f);
F fx = sk_unaligned_load<F>(ctx->fx);
F scalex;
if (kScale == -1) { scalex = 1.0f - fx; }
if (kScale == +1) { scalex = fx; }
sk_unaligned_store(ctx->scalex, scalex);
} template <int kScale>
SI void bilinear_y(SkRasterPipelineContexts::SamplerCtx* ctx, F* y) {
*y = sk_unaligned_load<F>(ctx->y) + (kScale * 0.5f);
F fy = sk_unaligned_load<F>(ctx->fy);
F scaley;
if (kScale == -1) { scaley = 1.0f - fy; }
if (kScale == +1) { scaley = fy; }
sk_unaligned_store(ctx->scaley, scaley);
}
HIGHP_STAGE(bilinear_setup, SkRasterPipelineContexts::SamplerCtx* ctx) {
save_xy(&r, &g, ctx); // Init for accumulate
dr = dg = db = da = F0;
}
// In bicubic interpolation, the 16 pixels and +/- 0.5 and +/- 1.5 offsets from the sample // pixel center are combined with a non-uniform cubic filter, with higher values near the center. // // This helper computes the total weight along one axis (our bicubic filter is separable), given one // column of the sampling matrix, and a fractional pixel offset. See SkCubicResampler for details.
SI F bicubic_wts(F t, float A, float B, float C, float D) { return mad(t, mad(t, mad(t, D, C), B), A);
}
SI F compute_perlin_vector(U32 sample, F x, F y) { // We're relying on the packing of uint16s within a uint32, which will vary based on endianness. #ifdef SK_CPU_BENDIAN
U32 sampleLo = sample >> 16;
U32 sampleHi = sample & 0xFFFF; #else
U32 sampleLo = sample & 0xFFFF;
U32 sampleHi = sample >> 16; #endif
// Convert 32-bit sample value into two floats in the [-1..1] range.
F vecX = mad(cast(sampleLo), 2.0f / 65535.0f, -1.0f);
F vecY = mad(cast(sampleHi), 2.0f / 65535.0f, -1.0f);
// Return the dot of the sample and the passed-in vector. return mad(vecX, x,
vecY * y);
}
HIGHP_STAGE(perlin_noise, SkRasterPipelineContexts::PerlinNoiseCtx* ctx) {
F noiseVecX = (r + 0.5) * ctx->baseFrequencyX;
F noiseVecY = (g + 0.5) * ctx->baseFrequencyY;
r = g = b = a = F0;
F stitchDataX = F_(ctx->stitchDataInX);
F stitchDataY = F_(ctx->stitchDataInY);
F ratio = F1;
for (int octave = 0; octave < ctx->numOctaves; ++octave) { // Calculate noise coordinates. (Roughly $noise_helper in Graphite)
F floorValX = floor_(noiseVecX);
F floorValY = floor_(noiseVecY);
F ceilValX = floorValX + 1.0f;
F ceilValY = floorValY + 1.0f;
F fractValX = noiseVecX - floorValX;
F fractValY = noiseVecY - floorValY;
if (ctx->stitching) { // If we are stitching, wrap the coordinates to the stitch position.
floorValX -= sk_bit_cast<F>(cond_to_mask(floorValX >= stitchDataX) &
sk_bit_cast<I32>(stitchDataX));
floorValY -= sk_bit_cast<F>(cond_to_mask(floorValY >= stitchDataY) &
sk_bit_cast<I32>(stitchDataY));
ceilValX -= sk_bit_cast<F>(cond_to_mask(ceilValX >= stitchDataX) &
sk_bit_cast<I32>(stitchDataX));
ceilValY -= sk_bit_cast<F>(cond_to_mask(ceilValY >= stitchDataY) &
sk_bit_cast<I32>(stitchDataY));
}
F u = compute_perlin_vector(sample00, fractValX, fractValY);
F v = compute_perlin_vector(sample10, fractValX - 1.0f, fractValY);
F A = lerp(u, v, smoothX);
u = compute_perlin_vector(sample01, fractValX, fractValY - 1.0f);
v = compute_perlin_vector(sample11, fractValX - 1.0f, fractValY - 1.0f);
F B = lerp(u, v, smoothX);
color[channel] = lerp(A, B, smoothY);
}
if (ctx->noiseType != SkPerlinNoiseShaderType::kFractalNoise) {
// For kTurbulence the result is: abs(noise[-1,1])
color[0] = abs_(color[0]);
color[1] = abs_(color[1]);
color[2] = abs_(color[2]);
color[3] = abs_(color[3]);
}
r = mad(color[0], ratio, r);
g = mad(color[1], ratio, g);
b = mad(color[2], ratio, b);
a = mad(color[3], ratio, a);
// Scale inputs for the next round.
noiseVecX *= 2.0f;
noiseVecY *= 2.0f;
stitchDataX *= 2.0f;
stitchDataY *= 2.0f;
ratio *= 0.5f;
}
if (ctx->noiseType == SkPerlinNoiseShaderType::kFractalNoise) {
// For kFractalNoise the result is: noise[-1,1] * 0.5 + 0.5
r = mad(r, 0.5f, 0.5f);
g = mad(g, 0.5f, 0.5f);
b = mad(b, 0.5f, 0.5f);
a = mad(a, 0.5f, 0.5f);
}
r = clamp_01_(r) * a;
g = clamp_01_(g) * a;
b = clamp_01_(b) * a;
a = clamp_01_(a);
}
r = sk_unaligned_load<F>(ctx->x) * ctx->scaleX;
g = sk_unaligned_load<F>(ctx->y) * ctx->scaleY;
}
HIGHP_STAGE(mipmap_linear_finish, SkRasterPipelineContexts::MipmapCtx* ctx) {
r = lerp(sk_unaligned_load<F>(ctx->r), r, F_(ctx->lowerWeight));
g = lerp(sk_unaligned_load<F>(ctx->g), g, F_(ctx->lowerWeight));
b = lerp(sk_unaligned_load<F>(ctx->b), b, F_(ctx->lowerWeight));
a = lerp(sk_unaligned_load<F>(ctx->a), a, F_(ctx->lowerWeight));
}
HIGHP_TAIL_STAGE(set_base_pointer, std::byte* p) {
base = p;
}
// All control flow stages used by SkSL maintain some state in the common registers:
// r: condition mask
// g: loop mask
// b: return mask
// a: execution mask (intersection of all three masks)
// After updating r/g/b, you must invoke update_execution_mask().
#define execution_mask() sk_bit_cast<I32>(a)
#define update_execution_mask() a = sk_bit_cast<F>(sk_bit_cast<I32>(r) & \
sk_bit_cast<I32>(g) & \
sk_bit_cast<I32>(b))
I32 mask = cond_to_mask(sk_unaligned_load<U32>(iota) < *ctx->tail);
r = g = b = a = sk_bit_cast<F>(mask);
}
HIGHP_TAIL_STAGE(store_device_xy01, F* dst) {
// This is very similar to `seed_shader + store_src`, but b/a are backwards.
// (sk_FragCoord actually puts w=1 in the w slot.)
static constexpr float iota[] = { 0.5f, 1.5f, 2.5f, 3.5f, 4.5f, 5.5f, 6.5f, 7.5f, 8.5f, 9.5f,10.5f,11.5f,12.5f,13.5f,14.5f,15.5f,
};
static_assert(std::size(iota) >= SkRasterPipelineContexts::kMaxStride_highp);
HIGHP_TAIL_STAGE(merge_condition_mask, I32* ptr) {
// Set the condition-mask to the intersection of two adjacent masks at the pointer.
r = sk_bit_cast<F>(ptr[0] & ptr[1]);
update_execution_mask();
}
HIGHP_TAIL_STAGE(merge_inv_condition_mask, I32* ptr) {
// Set the condition-mask to the intersection of the first mask and the inverse of the second.
r = sk_bit_cast<F>(ptr[0] & ~ptr[1]);
update_execution_mask();
}
HIGHP_TAIL_STAGE(load_loop_mask, F* ctx) {
g = sk_unaligned_load<F>(ctx);
update_execution_mask();
}
HIGHP_TAIL_STAGE(mask_off_loop_mask, NoCtx) {
// We encountered a break statement. If a lane was active, it should be masked off now, and stay
// masked-off until the termination of the loop.
g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
update_execution_mask();
}
HIGHP_TAIL_STAGE(reenable_loop_mask, I32* ptr) {
// Set the loop-mask to the union of the current loop-mask with the mask at the pointer.
g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | ptr[0]);
update_execution_mask();
}
HIGHP_TAIL_STAGE(merge_loop_mask, I32* ptr) {
// Set the loop-mask to the intersection of the current loop-mask with the mask at the pointer.
// (Note: this behavior subtly differs from merge_condition_mask!)
g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ptr[0]);
update_execution_mask();
}
HIGHP_TAIL_STAGE(continue_op, I32* continueMask) {
// Set any currently-executing lanes in the continue-mask to true.
*continueMask |= execution_mask();
// Disable any currently-executing lanes from the loop mask. (Just like `mask_off_loop_mask`.)
g = sk_bit_cast<F>(sk_bit_cast<I32>(g) & ~execution_mask());
update_execution_mask();
}
HIGHP_TAIL_STAGE(case_op, SkRasterPipelineContexts::CaseOpCtx* packed) {
auto ctx = SkRPCtxUtils::Unpack(packed);
// Check each lane to see if the case value matches the expectation.
I32* actualValue = (I32*)(base + ctx.offset);
I32 caseMatches = cond_to_mask(*actualValue == ctx.expectedValue);
// In lanes where we found a match, enable the loop mask...
g = sk_bit_cast<F>(sk_bit_cast<I32>(g) | caseMatches);
update_execution_mask();
// ... and clear the default-case mask.
I32* defaultMask = actualValue + 1;
*defaultMask &= ~caseMatches;
}
HIGHP_TAIL_STAGE(load_return_mask, F* ctx) {
b = sk_unaligned_load<F>(ctx);
update_execution_mask();
}
HIGHP_TAIL_STAGE(mask_off_return_mask, NoCtx) {
// We encountered a return statement. If a lane was active, it should be masked off now, and
// stay masked-off until the end of the function.
b = sk_bit_cast<F>(sk_bit_cast<I32>(b) & ~execution_mask());
update_execution_mask();
}
HIGHP_BRANCH_STAGE(branch_if_no_active_lanes_eq, SkRasterPipelineContexts::BranchIfEqualCtx* ctx) {
// Compare each lane against the expected value...
I32 match = cond_to_mask(*(const I32*)ctx->ptr == ctx->value);
// ... but mask off lanes that aren't executing.
match &= execution_mask();
// If any lanes matched, don't take the branch.
return any(match) ? 1 : ctx->offset;
}
HIGHP_TAIL_STAGE(trace_scope, SkRasterPipelineContexts::TraceScopeCtx* ctx) {
// Note that trace_scope intentionally does not incorporate the execution mask. Otherwise, the
// scopes would become unbalanced if the execution mask changed in the middle of a block. The
// caller is responsible for providing a combined trace- and execution-mask.
const I32* traceMask = (const I32*)ctx->traceMask;
if (any(*traceMask)) {
ctx->traceHook->scope(ctx->delta);
}
}
HIGHP_TAIL_STAGE(trace_var, SkRasterPipelineContexts::TraceVarCtx* ctx) {
const I32* traceMask = (const I32*)ctx->traceMask;
I32 mask = execution_mask() & *traceMask;
if (any(mask)) {
for (size_t lane = 0; lane < N; ++lane) {
if (select_lane(mask, lane)) {
const I32* data = (const I32*)ctx->data;
int slotIdx = ctx->slotIdx, numSlots = ctx->numSlots;
if (ctx->indirectOffset) {
// If this was an indirect store, apply the indirect-offset to the data pointer.
uint32_t indirectOffset = select_lane(*(const U32*)ctx->indirectOffset, lane);
indirectOffset = std::min<uint32_t>(indirectOffset, ctx->indirectLimit);
data += indirectOffset;
slotIdx += indirectOffset;
}
while (numSlots--) {
ctx->traceHook->var(slotIdx, select_lane(*data, lane));
++slotIdx;
++data;
}
break;
}
}
}
}
template <int LoopCount, typename OffsetType>
SI void shuffle_fn(std::byte* ptr, OffsetType* offsets, int numSlots) {
F scratch[16];
SK_UNROLL for (int count = 0; count < LoopCount; ++count) {
scratch[count] = *(F*)(ptr + offsets[count]);
}
// Surprisingly, this switch generates significantly better code than a memcpy (on x86-64) when
// the number of slots is unknown at compile time, and generates roughly identical code when the
// number of slots is hardcoded. Using a switch allows `scratch` to live in ymm0-ymm15 instead
// of being written out to the stack and then read back in. Also, the intrinsic memcpy assumes
// that `numSlots` could be arbitrarily large, and so it emits more code than we need.
F* dst = (F*)ptr;
switch (numSlots) {
case 16: dst[15] = scratch[15]; [[fallthrough]];
case 15: dst[14] = scratch[14]; [[fallthrough]];
case 14: dst[13] = scratch[13]; [[fallthrough]];
case 13: dst[12] = scratch[12]; [[fallthrough]];
case 12: dst[11] = scratch[11]; [[fallthrough]];
case 11: dst[10] = scratch[10]; [[fallthrough]];
case 10: dst[ 9] = scratch[ 9]; [[fallthrough]];
case 9: dst[ 8] = scratch[ 8]; [[fallthrough]];
case 8: dst[ 7] = scratch[ 7]; [[fallthrough]];
case 7: dst[ 6] = scratch[ 6]; [[fallthrough]];
case 6: dst[ 5] = scratch[ 5]; [[fallthrough]];
case 5: dst[ 4] = scratch[ 4]; [[fallthrough]];
case 4: dst[ 3] = scratch[ 3]; [[fallthrough]];
case 3: dst[ 2] = scratch[ 2]; [[fallthrough]];
case 2: dst[ 1] = scratch[ 1]; [[fallthrough]];
case 1: dst[ 0] = scratch[ 0];
}
}
template <int N>
SI void small_swizzle_fn(SkRasterPipelineContexts::SwizzleCtx* packed, std::byte* base) {
auto ctx = SkRPCtxUtils::Unpack(packed);
shuffle_fn<N>(base + ctx.dst, ctx.offsets, N);
}
// Unary operations take a single input, and overwrite it with their output.
// Unlike binary or ternary operations, we provide variations of 1-4 slots, but don't provide
// an arbitrary-width "n-slot" variation; the Builder can chain together longer sequences manually.
template <typename T, void (*ApplyFn)(T*)>
SI void apply_adjacent_unary(T* dst, T* end) {
do {
ApplyFn(dst);
dst += 1;
} while (dst != end);
}
// Binary operations take two adjacent inputs, and write their output in the first position.
template <typename T, void (*ApplyFn)(T*, T*)>
SI void apply_adjacent_binary(T* dst, T* src) {
T* end = src;
do {
ApplyFn(dst, src);
dst += 1;
src += 1;
} while (dst != end);
}
template <typename T, void (*ApplyFn)(T*, T*)>
SI void apply_adjacent_binary_packed(SkRasterPipelineContexts::BinaryOpCtx* packed,
std::byte* base) {
auto ctx = SkRPCtxUtils::Unpack(packed);
std::byte* dst = base + ctx.dst;
std::byte* src = base + ctx.src;
apply_adjacent_binary<T, ApplyFn>((T*)dst, (T*)src);
}
template <int N, typename V, typename S, void (*ApplyFn)(V*, V*)>
SI void apply_binary_immediate(SkRasterPipelineContexts::ConstantCtx* packed, std::byte* base) {
auto ctx = SkRPCtxUtils::Unpack(packed);
V* dst = (V*)(base + ctx.dst); // get a pointer to the destination
S scalar = sk_bit_cast<S>(ctx.value); // bit-pun the constant value as desired
V src = scalar - V(); // broadcast the constant value into a vector
SK_UNROLL for (int index = 0; index < N; ++index) {
ApplyFn(dst, &src); // perform the operation
dst += 1;
}
}
SI void div_fn(I32* dst, I32* src) {
I32 divisor = *src;
// Integer division crashes when we divide by 0, but we can divide by -1 to not crash (the
// result will be non-sensical). The mask will be 0xFFFFFFF if true, which happens to be -1.
divisor |= ((I32)cond_to_mask(divisor == 0));
// Dividing by -1 works for all numerators *except* INT_MIN, so we can add -1 once more if
// we are in that case.
divisor += ((I32)cond_to_mask(divisor == -1 && *dst == std::numeric_limits<int32_t>::lowest()));
*dst /= divisor;
}
SI void div_fn(U32* dst, U32* src) {
U32 divisor = *src;
// Integer division crashes when we divide by 0, but we can divide by something else to not
// crash (the result will be non-sensical). The mask will be 0xFFFFFFF if true.
divisor |= ((U32)cond_to_mask(divisor == 0));
*dst /= divisor;
}
// Many ops reuse the int stages when performing uint arithmetic, since they're equivalent on a
// two's-complement machine. (Even multiplication is equivalent in the lower 32 bits.)
DECLARE_BINARY_FLOAT(add) DECLARE_BINARY_INT(add)
DECLARE_BINARY_FLOAT(sub) DECLARE_BINARY_INT(sub)
DECLARE_BINARY_FLOAT(mul) DECLARE_BINARY_INT(mul)
DECLARE_BINARY_FLOAT(div) DECLARE_BINARY_INT(div) DECLARE_BINARY_UINT(div)
DECLARE_BINARY_INT(bitwise_and)
DECLARE_BINARY_INT(bitwise_or)
DECLARE_BINARY_INT(bitwise_xor)
DECLARE_BINARY_FLOAT(mod)
DECLARE_BINARY_FLOAT(min) DECLARE_BINARY_INT(min) DECLARE_BINARY_UINT(min)
DECLARE_BINARY_FLOAT(max) DECLARE_BINARY_INT(max) DECLARE_BINARY_UINT(max)
DECLARE_BINARY_FLOAT(cmplt) DECLARE_BINARY_INT(cmplt) DECLARE_BINARY_UINT(cmplt)
DECLARE_BINARY_FLOAT(cmple) DECLARE_BINARY_INT(cmple) DECLARE_BINARY_UINT(cmple)
DECLARE_BINARY_FLOAT(cmpeq) DECLARE_BINARY_INT(cmpeq)
DECLARE_BINARY_FLOAT(cmpne) DECLARE_BINARY_INT(cmpne)
// Sufficiently complex ops only provide an N-way version, to avoid code bloat from the dedicated
// 1-4 slot versions.
DECLARE_N_WAY_BINARY_FLOAT(atan2)
DECLARE_N_WAY_BINARY_FLOAT(pow)
// Dots can be represented with multiply and add ops, but they are so foundational that it's worth
// having dedicated ops.
HIGHP_TAIL_STAGE(dot_2_floats, F* dst) {
dst[0] = mad(dst[0], dst[2],
dst[1] * dst[3]);
}
// MxM, VxM and MxV multiplication all use matrix_multiply. Vectors are treated like a matrix with a
// single column or row.
template <int N>
SI void matrix_multiply(SkRasterPipelineContexts::MatrixMultiplyCtx* packed, std::byte* base) {
auto ctx = SkRPCtxUtils::Unpack(packed);
int outColumns = ctx.rightColumns,
outRows = ctx.leftRows;
SkASSERT(ctx.leftColumns == ctx.rightRows);
SkASSERT(N == ctx.leftColumns); // N should match the result width
#if !defined(SKRP_CPU_SCALAR)
// This prevents Clang from generating early-out checks for zero-sized matrices.
SK_ASSUME(outColumns >= 1);
SK_ASSUME(outRows >= 1);
SK_ASSUME(outColumns <= 4);
SK_ASSUME(outRows <= 4);
#endif
// Get pointers to the adjacent left- and right-matrices.
F* resultMtx = (F*)(base + ctx.dst);
F* leftMtx = &resultMtx[ctx.rightColumns * ctx.leftRows];
F* rightMtx = &leftMtx[N * ctx.leftRows];
// Emit each matrix element.
for (int c = 0; c < outColumns; ++c) {
for (int r = 0; r < outRows; ++r) {
// Dot a vector from leftMtx[*][r] with rightMtx[c][*].
F* leftRow = &leftMtx [r];
F* rightColumn = &rightMtx[c * N];
F element = *leftRow * *rightColumn;
for (int idx = 1; idx < N; ++idx) {
leftRow += outRows;
rightColumn += 1;
element = mad(*leftRow, *rightColumn, element);
}
// Refract always operates on 4-wide incident and normal vectors; for narrower inputs, the code
// generator fills in the input columns with zero, and discards the extra output columns.
HIGHP_TAIL_STAGE(refract_4_floats, F* dst) {
// Algorithm adapted from https://registry.khronos.org/OpenGL-Refpages/gl4/html/refract.xhtml
F *incident = dst + 0;
F *normal = dst + 4;
F eta = dst[8];
SI void mix_fn(F* a, F* x, F* y) {
// We reorder the arguments here to match lerp's GLSL-style order (interpolation point last).
*a = lerp(*x, *y, *a);
}
SI void mix_fn(I32* a, I32* x, I32* y) {
// We reorder the arguments here to match if_then_else's expected order (y before x).
*a = if_then_else(*a, *y, *x);
}
SI void smoothstep_fn(F* edge0, F* edge1, F* x) {
F t = clamp_01_((*x - *edge0) / (*edge1 - *edge0));
*edge0 = t * t * (3.0 - 2.0 * t);
}
HIGHP_STAGE(gauss_a_to_rgba, NoCtx) {
// x = 1 - x;
// exp(-x * x * 4) - 0.018f;
// ... now approximate with quartic
//
const float c4 = -2.26661229133605957031f;
const float c3 = 2.89795351028442382812f;
const float c2 = 0.21345567703247070312f;
const float c1 = 0.15489584207534790039f;
const float c0 = 0.00030726194381713867f;
a = mad(a, mad(a, mad(a, mad(a, c4, c3), c2), c1), c0);
r = a;
g = a;
b = a;
}
SI void bilerp_clamp_large(const SkRasterPipelineContexts::GatherCtx* ctx,
F* r, F* g, F* b, F* a) {
// (cx,cy) are the center of our sample.
F cx = *r,
cy = *g;
// All sample points are at the same fractional offset (fx,fy).
// They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
F fx = fract(cx + 0.5f),
fy = fract(cy + 0.5f);
// We'll accumulate the color of all four samples into {r,g,b,a} directly.
*r = *g = *b = *a = F0;
for (float py = -0.5f; py <= +0.5f; py += 1.0f)
for (float px = -0.5f; px <= +0.5f; px += 1.0f) {
// (x,y) are the coordinates of this sample point.
F x = cx + px,
y = cy + py;
// ix_and_ptr() will clamp to the image's bounds for us.
const uint32_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, x,y);
F sr,sg,sb,sa;
from_8888(gather_unaligned(ptr, ix), &sr,&sg,&sb,&sa);
// In bilinear interpolation, the 4 pixels at +/- 0.5 offsets from the sample pixel center
// are combined in direct proportion to their area overlapping that logical query pixel.
// At positive offsets, the x-axis contribution to that rectangle is fx,
// or (1-fx) at negative x. Same deal for y.
F sx = (px > 0) ? fx : 1.0f - fx,
sy = (py > 0) ? fy : 1.0f - fy,
area = sx * sy;
// A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
// This version exists to allow a shader to force high precision.
HIGHP_STAGE(bilerp_clamp_8888_force_highp, const SkRasterPipelineContexts::GatherCtx* ctx) {
bilerp_clamp_large(ctx, &r, &g, &b, &a);
}
// A specialized fused image shader for clamp-x, clamp-y, non-sRGB sampling.
HIGHP_STAGE(bicubic_clamp_8888, const SkRasterPipelineContexts::GatherCtx* ctx) {
// (cx,cy) are the center of our sample.
F cx = r,
cy = g;
// All sample points are at the same fractional offset (fx,fy).
// They're the 4 corners of a logical 1x1 pixel surrounding (x,y) at (0.5,0.5) offsets.
F fx = fract(cx + 0.5f),
fy = fract(cy + 0.5f);
// We'll accumulate the color of all four samples into {r,g,b,a} directly.
r = g = b = a = F0;
F sample_y = cy - 1.5f;
for (int yy = 0; yy <= 3; ++yy) {
F sample_x = cx - 1.5f;
for (int xx = 0; xx <= 3; ++xx) {
F scale = scalex[xx] * scaley[yy];
// ix_and_ptr() will clamp to the image's bounds for us.
const uint32_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, sample_x, sample_y);
F sr,sg,sb,sa;
from_8888(gather_unaligned(ptr, ix), &sr,&sg,&sb,&sa);
r = mad(scale, sr, r);
g = mad(scale, sg, g);
b = mad(scale, sb, b);
a = mad(scale, sa, a);
sample_x += 1;
}
sample_y += 1;
}
}
// ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
HIGHP_STAGE(swizzle, void* ctx) {
auto ir = r, ig = g, ib = b, ia = a;
F* o[] = {&r, &g, &b, &a};
char swiz[4];
memcpy(swiz, &ctx, sizeof(swiz));
for (int i = 0; i < 4; ++i) {
switch (swiz[i]) {
case 'r': *o[i] = ir; break;
case 'g': *o[i] = ig; break;
case 'b': *o[i] = ib; break;
case 'a': *o[i] = ia; break;
case '0': *o[i] = F0; break;
case '1': *o[i] = F1; break;
default: break;
}
}
}
namespace lowp {
#if defined(SKRP_CPU_SCALAR) || defined(SK_ENABLE_OPTIMIZE_SIZE) || \
defined(SK_DISABLE_LOWP_RASTER_PIPELINE)
// We don't bother generating the lowp stages if we are:
// - ... in scalar mode (MSVC, old clang, etc...)
// - ... trying to save code size
// - ... explicitly disabling it. This is currently used by Flutter and Google3.
//
// Having nullptr for every stage will cause SkRasterPipeline to always use the highp stages.
#define M(st) static void (*st)(void) = nullptr;
SK_RASTER_PIPELINE_OPS_LOWP(M)
#undef M
static void (*just_return)(void) = nullptr;
#else // We are compiling vector code with Clang... let's make some lowp stages!
#if defined(SKRP_CPU_ML4) || defined(SKRP_CPU_AVX2) || defined(SKRP_CPU_LASX)
template <typename T> using V = Vec<16, T>;
#else
template <typename T> using V = Vec<8, T>;
#endif
using U8 = V<uint8_t >;
using U16 = V<uint16_t>;
using I16 = V< int16_t>;
using I32 = V< int32_t>;
using U32 = V<uint32_t>;
using I64 = V< int64_t>;
using U64 = V<uint64_t>;
using F = V<float >;
static constexpr size_t N = sizeof(U16) / sizeof(uint16_t);
// Promotion helpers (for GCC)
#if defined(__clang__)
SI constexpr U16 U16_(uint16_t x) { return x; }
SI constexpr I32 I32_( int32_t x) { return x; }
SI constexpr U32 U32_(uint32_t x) { return x; }
SI constexpr F F_ (float x) { return x; }
#else
SI constexpr U16 U16_(uint16_t x) { return x + U16(); }
SI constexpr I32 I32_( int32_t x) { return x + I32(); }
SI constexpr U32 U32_(uint32_t x) { return x + U32(); }
SI constexpr F F_ (float x) { return x - F (); }
#endif
#if SKRP_NARROW_STAGES
static void ABI just_return(Params*, SkRasterPipelineStage*, U16,U16,U16,U16) {}
#else
static void ABI just_return(SkRasterPipelineStage*, size_t,size_t,
U16,U16,U16,U16, U16,U16,U16,U16) {}
#endif
// All stages use the same function call ABI to chain into each other, but there are three types:
// GG: geometry in, geometry out -- think, a matrix
// GP: geometry in, pixels out. -- think, a memory gather
// PP: pixels in, pixels out. -- think, a blend mode
//
// (Some stages ignore their inputs or produce no logical output. That's perfectly fine.)
//
// These three LOWP_STAGE_ macros let you define each type of stage,
// and will have (x,y) geometry and/or (r,g,b,a, dr,dg,db,da) pixel arguments as appropriate.
//
// Why does the LOWP version have 3 versions of a stage while HIGHP only has 1?
// We don't want to lose precision on the x and y coordinates, so we fuse the rg and ba
// registers before passing them in (and need to know if we have to split that super
// register or not).
// ~~~~~~ Commonly used helper functions ~~~~~~ //
/**
* Helpers to to properly rounded division (by 255). The ideal answer we want to compute is slow,
* thanks to a division by a non-power of two:
* [1] (v + 127) / 255
*
* There is a two-step process that computes the correct answer for all inputs:
* [2] (v + 128 + ((v + 128) >> 8)) >> 8
*
* There is also a single iteration approximation, but it's wrong (+-1) ~25% of the time:
* [3] (v + 255) >> 8;
*
* We offer two different implementations here, depending on the requirements of the calling stage.
*/
/**
* div255 favors speed over accuracy. It uses formula [2] on NEON (where we can compute it as fast
* as [3]), and uses [3] elsewhere.
*/
SI U16 div255(U16 v) {
#if defined(SKRP_CPU_NEON)
// With NEON we can compute [2] just as fast as [3], so let's be correct.
// First we compute v + ((v+128)>>8), then one more round of (...+128)>>8 to finish up:
return vrshrq_n_u16(vrsraq_n_u16(v, v, 8), 8);
#else
// Otherwise, use [3], which is never wrong by more than 1:
return (v+255)/256;
#endif
}
/**
* div255_accurate guarantees the right answer on all platforms, at the expense of performance.
*/
SI U16 div255_accurate(U16 v) {
#if defined(SKRP_CPU_NEON)
// Our NEON implementation of div255 is already correct for all inputs:
return div255(v);
#else
// This is [2] (the same formulation as NEON), but written without the benefit of intrinsics:
v += 128;
return (v+(v/256))/256;
#endif
}
SI U16 inv(U16 v) { return 255-v; }
SI U16 if_then_else(I16 c, U16 t, U16 e) {
return (t & sk_bit_cast<U16>(c)) | (e & sk_bit_cast<U16>(~c));
}
SI U32 if_then_else(I32 c, U32 t, U32 e) {
return (t & sk_bit_cast<U32>(c)) | (e & sk_bit_cast<U32>(~c));
}
SI U16 max(U16 x, U16 y) { return if_then_else(x < y, y, x); }
SI U16 min(U16 x, U16 y) { return if_then_else(x < y, x, y); }
SI U16 max(U16 a, uint16_t b) { return max( a , U16_(b)); }
SI U16 max(uint16_t a, U16 b) { return max(U16_(a), b ); }
SI U16 min(U16 a, uint16_t b) { return min( a , U16_(b)); }
SI U16 min(uint16_t a, U16 b) { return min(U16_(a), b ); }
SI F if_then_else(I32 c, F t, F e) {
return sk_bit_cast<F>( (sk_bit_cast<I32>(t) & c) | (sk_bit_cast<I32>(e) & ~c) );
}
SI F if_then_else(I32 c, F t, float e) { return if_then_else(c, t , F_(e)); }
SI F if_then_else(I32 c, float t, F e) { return if_then_else(c, F_(t), e ); }
SI F max(F x, F y) { return if_then_else(x < y, y, x); }
SI F min(F x, F y) { return if_then_else(x < y, x, y); }
SI F max(F a, float b) { return max( a , F_(b)); }
SI F max(float a, F b) { return max(F_(a), b ); }
SI F min(F a, float b) { return min( a , F_(b)); }
SI F min(float a, F b) { return min(F_(a), b ); }
SI I32 if_then_else(I32 c, I32 t, I32 e) {
return (t & c) | (e & ~c);
}
#if defined(SKRP_CPU_AVX2)
// Some compilers did not vectorize this, so explicitly call the intrinsics
SI I32 max(I32 x, I32 y) {
__m256i x_lo, x_hi, y_lo, y_hi;
split(x, &x_lo, &x_hi);
split(y, &y_lo, &y_hi);
return join<I32>(_mm256_max_epi32(x_lo, y_lo), _mm256_max_epi32(x_hi, y_hi));
}
SI I32 min(I32 x, I32 y) {
__m256i x_lo, x_hi, y_lo, y_hi;
split(x, &x_lo, &x_hi);
split(y, &y_lo, &y_hi);
return join<I32>(_mm256_min_epi32(x_lo, y_lo), _mm256_min_epi32(x_hi, y_hi));
}
#elif defined(SKRP_CPU_NEON)
// TODO(kjlubick) make sure NEON code is handled well.
SI I32 max(I32 x, I32 y) { return if_then_else(x < y, y, x); }
SI I32 min(I32 x, I32 y) { return if_then_else(x < y, x, y); }
#else
SI I32 max(I32 x, I32 y) { return if_then_else(x < y, y, x); }
SI I32 min(I32 x, I32 y) { return if_then_else(x < y, x, y); }
#endif
SI I32 max(I32 a, int32_t b) { return max( a , I32_(b)); }
SI I32 max(int32_t a, I32 b) { return max(I32_(a), b ); }
SI I32 min(I32 a, int32_t b) { return min( a , I32_(b)); }
SI I32 min(int32_t a, I32 b) { return min(I32_(a), b ); }
SI F mad(F f, F m, F a) { return a+f*m; }
SI F mad(F f, F m, float a) { return mad( f , m , F_(a)); }
SI F mad(F f, float m, F a) { return mad( f , F_(m), a ); }
SI F mad(F f, float m, float a) { return mad( f , F_(m), F_(a)); }
SI F mad(float f, F m, F a) { return mad(F_(f), m , a ); }
SI F mad(float f, F m, float a) { return mad(F_(f), m , F_(a)); }
SI F mad(float f, float m, F a) { return mad(F_(f), F_(m), a ); }
SI F nmad(F f, F m, F a) { return a-f*m; }
SI F nmad(F f, F m, float a) { return nmad( f , m , F_(a)); }
SI F nmad(F f, float m, F a) { return nmad( f , F_(m), a ); }
SI F nmad(F f, float m, float a) { return nmad( f , F_(m), F_(a)); }
SI F nmad(float f, F m, F a) { return nmad(F_(f), m , a ); }
SI F nmad(float f, F m, float a) { return nmad(F_(f), m , F_(a)); }
SI F nmad(float f, float m, F a) { return nmad(F_(f), F_(m), a ); }
SI U32 trunc_(F x) { return (U32)cast<I32>(x); }
// Use approximate instructions and one Newton-Raphson step to calculate 1/x.
SI F rcp_precise(F x) {
#if defined(SKRP_CPU_ML4)
F e = _mm512_rcp14_ps(x);
return _mm512_fnmadd_ps(x, e, _mm512_set1_ps(2.0f)) * e;
#elif defined(SKRP_CPU_AVX2)
__m256 lo,hi;
split(x, &lo,&hi);
return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
#elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
__m128 lo,hi;
split(x, &lo,&hi);
return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
#elif defined(SKRP_CPU_NEON)
float32x4_t lo,hi;
split(x, &lo,&hi);
return join<F>(SK_OPTS_NS::rcp_precise(lo), SK_OPTS_NS::rcp_precise(hi));
#elif defined(SKRP_CPU_LASX)
__m256 lo,hi;
split(x, &lo,&hi);
return join<F>(__lasx_xvfrecip_s(lo), __lasx_xvfrecip_s(hi));
#elif defined(SKRP_CPU_LSX)
__m128 lo,hi;
split(x, &lo,&hi);
return join<F>(__lsx_vfrecip_s(lo), __lsx_vfrecip_s(hi));
#else
return 1.0f / x;
#endif
}
SI F sqrt_(F x) {
#if defined(SKRP_CPU_ML4)
return _mm512_sqrt_ps(x);
#elif defined(SKRP_CPU_AVX2)
__m256 lo,hi;
split(x, &lo,&hi);
return join<F>(_mm256_sqrt_ps(lo), _mm256_sqrt_ps(hi));
#elif defined(SKRP_CPU_SSE2) || defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
__m128 lo,hi;
split(x, &lo,&hi);
return join<F>(_mm_sqrt_ps(lo), _mm_sqrt_ps(hi));
#elif defined(SK_CPU_ARM64)
float32x4_t lo,hi;
split(x, &lo,&hi);
return join<F>(vsqrtq_f32(lo), vsqrtq_f32(hi));
#elif defined(SKRP_CPU_NEON)
auto sqrt = [](float32x4_t v) {
auto est = vrsqrteq_f32(v); // Estimate and two refinement steps for est = rsqrt(v).
est *= vrsqrtsq_f32(v,est*est);
est *= vrsqrtsq_f32(v,est*est);
return v*est; // sqrt(v) == v*rsqrt(v).
};
float32x4_t lo,hi;
split(x, &lo,&hi);
return join<F>(sqrt(lo), sqrt(hi));
#elif defined(SKRP_CPU_LASX)
__m256 lo,hi;
split(x, &lo,&hi);
return join<F>(__lasx_xvfsqrt_s(lo), __lasx_xvfsqrt_s(hi));
#elif defined(SKRP_CPU_LSX)
__m128 lo,hi;
split(x, &lo,&hi);
return join<F>(__lsx_vfsqrt_s(lo), __lsx_vfsqrt_s(hi));
#else
return F{
sqrtf(x[0]), sqrtf(x[1]), sqrtf(x[2]), sqrtf(x[3]),
sqrtf(x[4]), sqrtf(x[5]), sqrtf(x[6]), sqrtf(x[7]),
};
#endif
}
// scaled_mult interprets a and b as number on [-1, 1) which are numbers in Q15 format. Functionally
// this multiply is:
// (2 * a * b + (1 << 15)) >> 16
// The result is a number on [-1, 1).
// Note: on neon this is a saturating multiply while the others are not.
SI I16 scaled_mult(I16 a, I16 b) {
#if defined(SKRP_CPU_ML4)
return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
#elif defined(SKRP_CPU_AVX2)
return (I16)_mm256_mulhrs_epi16((__m256i)a, (__m256i)b);
#elif defined(SKRP_CPU_SSE41) || defined(SKRP_CPU_AVX)
return (I16)_mm_mulhrs_epi16((__m128i)a, (__m128i)b);
#elif defined(SK_CPU_ARM64)
return vqrdmulhq_s16(a, b);
#elif defined(SKRP_CPU_NEON)
return vqrdmulhq_s16(a, b);
#elif defined(SKRP_CPU_LASX)
I16 res = __lasx_xvmuh_h(a, b);
return __lasx_xvslli_h(res, 1);
#elif defined(SKRP_CPU_LSX)
I16 res = __lsx_vmuh_h(a, b);
return __lsx_vslli_h(res, 1);
#else
const I32 roundingTerm = I32_(1 << 14);
return cast<I16>((cast<I32>(a) * cast<I32>(b) + roundingTerm) >> 15);
#endif
}
// This sum is to support lerp where the result will always be a positive number. In general,
// a sum like this would require an additional bit, but because we know the range of the result
// we know that the extra bit will always be zero.
SI U16 constrained_add(I16 a, U16 b) {
#if defined(SK_DEBUG)
for (size_t i = 0; i < N; i++) {
// Ensure that a + b is on the interval [0, UINT16_MAX]
int ia = a[i],
ib = b[i];
// Use 65535 here because fuchsia's compiler evaluates UINT16_MAX - ib, which is
// 65536U - ib, as an uint32_t instead of an int32_t. This was forcing ia to be
// interpreted as an uint32_t.
SkASSERT(-ib <= ia && ia <= 65535 - ib);
}
#endif
return b + sk_bit_cast<U16>(a);
}
SI F fract(F x) { return x - floor_(x); }
SI F abs_(F x) { return sk_bit_cast<F>( sk_bit_cast<I32>(x) & 0x7fffffff ); }
x = cast<F>(I32_(dx)) + sk_unaligned_load<F>(iota);
y = cast<F>(I32_(dy)) + 0.5f;
#endif
}
LOWP_STAGE_GG(matrix_translate, const float* m) {
x += m[0];
y += m[1];
}
LOWP_STAGE_GG(matrix_scale_translate, const float* m) {
x = mad(x,m[0], m[2]);
y = mad(y,m[1], m[3]);
}
LOWP_STAGE_GG(matrix_2x3, const float* m) {
auto X = mad(x,m[0], mad(y,m[1], m[2])),
Y = mad(x,m[3], mad(y,m[4], m[5]));
x = X;
y = Y;
}
LOWP_STAGE_GG(matrix_perspective, const float* m) {
// N.B. Unlike the other matrix_ stages, this matrix is row-major.
auto X = mad(x,m[0], mad(y,m[1], m[2])),
Y = mad(x,m[3], mad(y,m[4], m[5])),
Z = mad(x,m[6], mad(y,m[7], m[8]));
x = X * rcp_precise(Z);
y = Y * rcp_precise(Z);
}
LOWP_STAGE_PP(uniform_color, const SkRasterPipelineContexts::UniformColorCtx* c) {
r = U16_(c->rgba[0]);
g = U16_(c->rgba[1]);
b = U16_(c->rgba[2]);
a = U16_(c->rgba[3]);
}
LOWP_STAGE_PP(uniform_color_dst, const SkRasterPipelineContexts::UniformColorCtx* c) {
dr = U16_(c->rgba[0]);
dg = U16_(c->rgba[1]);
db = U16_(c->rgba[2]);
da = U16_(c->rgba[3]);
}
LOWP_STAGE_PP(black_color, NoCtx) { r = g = b = U16_0; a = U16_255; }
LOWP_STAGE_PP(white_color, NoCtx) { r = g = b = U16_255; a = U16_255; }
LOWP_STAGE_PP(set_rgb, const float rgb[3]) {
r = from_float(rgb[0]);
g = from_float(rgb[1]);
b = from_float(rgb[2]);
}
// No need to clamp against 0 here (values are unsigned)
LOWP_STAGE_PP(clamp_01, NoCtx) {
r = min(r, 255);
g = min(g, 255);
b = min(b, 255);
a = min(a, 255);
}
LOWP_STAGE_PP(clamp_a_01, NoCtx) {
a = min(a, 255);
}
LOWP_STAGE_PP(clamp_gamut, NoCtx) {
a = min(a, 255);
r = min(r, a);
g = min(g, a);
b = min(b, a);
}
LOWP_STAGE_PP(premul, NoCtx) {
r = div255_accurate(r * a);
g = div255_accurate(g * a);
b = div255_accurate(b * a);
}
LOWP_STAGE_PP(premul_dst, NoCtx) {
dr = div255_accurate(dr * da);
dg = div255_accurate(dg * da);
db = div255_accurate(db * da);
}
LOWP_STAGE_PP(force_opaque , NoCtx) { a = U16_255; }
LOWP_STAGE_PP(force_opaque_dst, NoCtx) { da = U16_255; }
LOWP_STAGE_PP(swap_rb, NoCtx) {
auto tmp = r;
r = b;
b = tmp;
}
LOWP_STAGE_PP(swap_rb_dst, NoCtx) {
auto tmp = dr;
dr = db;
db = tmp;
}
LOWP_STAGE_PP(move_src_dst, NoCtx) {
dr = r;
dg = g;
db = b;
da = a;
}
LOWP_STAGE_PP(move_dst_src, NoCtx) {
r = dr;
g = dg;
b = db;
a = da;
}
// The same logic applied to all 4 channels.
#define BLEND_MODE(name) \
SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
LOWP_STAGE_PP(name, NoCtx) { \
r = name##_channel(r,dr,a,da); \
g = name##_channel(g,dg,a,da); \
b = name##_channel(b,db,a,da); \
a = name##_channel(a,da,a,da); \
} \
SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
// The same logic applied to color, and srcover for alpha.
#define BLEND_MODE(name) \
SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da); \
LOWP_STAGE_PP(name, NoCtx) { \
r = name##_channel(r,dr,a,da); \
g = name##_channel(g,dg,a,da); \
b = name##_channel(b,db,a,da); \
a = a + div255( da*inv(a) ); \
} \
SI U16 name##_channel(U16 s, U16 d, U16 sa, U16 da)
BLEND_MODE(darken) { return s + d - div255( max(s*da, d*sa) ); }
BLEND_MODE(lighten) { return s + d - div255( min(s*da, d*sa) ); }
BLEND_MODE(difference) { return s + d - 2*div255( min(s*da, d*sa) ); }
BLEND_MODE(exclusion) { return s + d - 2*div255( s*d ); }
template <typename T>
SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, F x, F y) {
// Exclusive -> inclusive.
const F w = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->width ) - 1)),
h = F_(sk_bit_cast<float>( sk_bit_cast<uint32_t>(ctx->height) - 1));
const F z = F_(std::numeric_limits<float>::min());
x = min(max(z, x), w);
y = min(max(z, y), h);
x = sk_bit_cast<F>(sk_bit_cast<U32>(x) - (uint32_t)ctx->roundDownAtInteger);
y = sk_bit_cast<F>(sk_bit_cast<U32>(y) - (uint32_t)ctx->roundDownAtInteger);
template <typename T>
SI U32 ix_and_ptr(T** ptr, const SkRasterPipelineContexts::GatherCtx* ctx, I32 x, I32 y) {
// This flag doesn't make sense when the coords are integers.
SkASSERT(ctx->roundDownAtInteger == 0);
// Exclusive -> inclusive.
const I32 w = I32_( ctx->width - 1),
h = I32_(ctx->height - 1);
// ~~~~~~ 16-bit memory loads and stores ~~~~~~ //
SI void from_565(U16 rgb, U16* r, U16* g, U16* b) {
// Format for 565 buffers: 15|rrrrr gggggg bbbbb|0
U16 R = (rgb >> 11) & 31,
G = (rgb >> 5) & 63,
B = (rgb >> 0) & 31;
// These bit replications are the same as multiplying by 255/31 or 255/63 to scale to 8-bit.
*r = (R << 3) | (R >> 2);
*g = (G << 2) | (G >> 4);
*b = (B << 3) | (B >> 2);
}
SI void load_565_(const uint16_t* ptr, U16* r, U16* g, U16* b) {
from_565(load<U16>(ptr), r,g,b);
}
SI void store_565_(uint16_t* ptr, U16 r, U16 g, U16 b) {
r = min(r, 255);
g = min(g, 255);
b = min(b, 255);
// Round from [0,255] to [0,31] or [0,63], as if x * (31/255.0f) + 0.5f.
// (Don't feel like you need to find some fundamental truth in these...
// they were brute-force searched.)
U16 R = (r * 9 + 36) / 74, // 9/74 ≈ 31/255, plus 36/74, about half.
G = (g * 21 + 42) / 85, // 21/85 = 63/255 exactly.
B = (b * 9 + 36) / 74;
// Pack them back into 15|rrrrr gggggg bbbbb|0.
store(ptr, R << 11
| G << 5
| B << 0);
}
SI void from_4444(U16 rgba, U16* r, U16* g, U16* b, U16* a) {
// Format for 4444 buffers: 15|rrrr gggg bbbb aaaa|0.
U16 R = (rgba >> 12) & 15,
G = (rgba >> 8) & 15,
B = (rgba >> 4) & 15,
A = (rgba >> 0) & 15;
// Scale [0,15] to [0,255].
*r = (R << 4) | R;
*g = (G << 4) | G;
*b = (B << 4) | B;
*a = (A << 4) | A;
}
SI void load_4444_(const uint16_t* ptr, U16* r, U16* g, U16* b, U16* a) {
from_4444(load<U16>(ptr), r,g,b,a);
}
SI void store_4444_(uint16_t* ptr, U16 r, U16 g, U16 b, U16 a) {
r = min(r, 255);
g = min(g, 255);
b = min(b, 255);
a = min(a, 255);
// Round from [0,255] to [0,15], producing the same value as (x*(15/255.0f) + 0.5f).
U16 R = (r + 8) / 17,
G = (g + 8) / 17,
B = (b + 8) / 17,
A = (a + 8) / 17;
// Pack them back into 15|rrrr gggg bbbb aaaa|0.
store(ptr, R << 12
| G << 8
| B << 4
| A << 0);
}
SI U16 load_8(const uint8_t* ptr) {
return cast<U16>(load<U8>(ptr));
}
SI void store_8(uint8_t* ptr, U16 v) {
v = min(v, 255);
store(ptr, cast<U8>(v));
}
LOWP_STAGE_PP(load_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
r = g = b = U16_0;
a = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
}
LOWP_STAGE_PP(load_a8_dst, const SkRasterPipelineContexts::MemoryCtx* ctx) {
dr = dg = db = U16_0;
da = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
}
LOWP_STAGE_PP(store_a8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), a);
}
LOWP_STAGE_GP(gather_a8, const SkRasterPipelineContexts::GatherCtx* ctx) {
const uint8_t* ptr;
U32 ix = ix_and_ptr(&ptr, ctx, x,y);
r = g = b = U16_0;
a = cast<U16>(gather<U8>(ptr, ix));
}
LOWP_STAGE_PP(store_r8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
store_8(ptr_at_xy<uint8_t>(ctx, dx,dy), r);
}
LOWP_STAGE_PP(alpha_to_gray, NoCtx) {
r = g = b = a;
a = U16_255;
}
LOWP_STAGE_PP(alpha_to_gray_dst, NoCtx) {
dr = dg = db = da;
da = U16_255;
}
LOWP_STAGE_PP(alpha_to_red, NoCtx) {
r = a;
a = U16_255;
}
LOWP_STAGE_PP(alpha_to_red_dst, NoCtx) {
dr = da;
da = U16_255;
}
LOWP_STAGE_PP(bt709_luminance_or_luma_to_alpha, NoCtx) {
a = (r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
r = g = b = U16_0;
}
LOWP_STAGE_PP(bt709_luminance_or_luma_to_rgb, NoCtx) {
r = g = b =(r*54 + g*183 + b*19)/256; // 0.2126, 0.7152, 0.0722 with 256 denominator.
}
LOWP_STAGE_PP(scale_1_float, const float* f) {
U16 c = from_float(*f);
r = div255( r * c );
g = div255( g * c );
b = div255( b * c );
a = div255( a * c );
}
LOWP_STAGE_PP(lerp_1_float, const float* f) {
U16 c = from_float(*f);
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
LOWP_STAGE_PP(scale_native, const uint16_t scales[]) {
auto c = sk_unaligned_load<U16>(scales);
r = div255( r * c );
g = div255( g * c );
b = div255( b * c );
a = div255( a * c );
}
LOWP_STAGE_PP(lerp_native, const uint16_t scales[]) {
auto c = sk_unaligned_load<U16>(scales);
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
LOWP_STAGE_PP(scale_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
r = div255( r * c );
g = div255( g * c );
b = div255( b * c );
a = div255( a * c );
}
LOWP_STAGE_PP(lerp_u8, const SkRasterPipelineContexts::MemoryCtx* ctx) {
U16 c = load_8(ptr_at_xy<const uint8_t>(ctx, dx,dy));
r = lerp(dr, r, c);
g = lerp(dg, g, c);
b = lerp(db, b, c);
a = lerp(da, a, c);
}
// Derive alpha's coverage from rgb coverage and the values of src and dst alpha.
SI U16 alpha_coverage_from_rgb_coverage(U16 a, U16 da, U16 cr, U16 cg, U16 cb) {
return if_then_else(a < da, min(cr, min(cg,cb))
, max(cr, max(cg,cb)));
}
LOWP_STAGE_PP(scale_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
U16 cr,cg,cb;
load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
r = div255( r * cr );
g = div255( g * cg );
b = div255( b * cb );
a = div255( a * ca );
}
LOWP_STAGE_PP(lerp_565, const SkRasterPipelineContexts::MemoryCtx* ctx) {
U16 cr,cg,cb;
load_565_(ptr_at_xy<const uint16_t>(ctx, dx,dy), &cr,&cg,&cb);
U16 ca = alpha_coverage_from_rgb_coverage(a,da, cr,cg,cb);
r = lerp(dr, r, cr);
g = lerp(dg, g, cg);
b = lerp(db, b, cb);
a = lerp(da, a, ca);
}
r = min(div255(r*mul) + add, a);
g = min(div255(g*mul) + add, a);
b = min(div255(b*mul) + add, a);
}
// ~~~~~~ Gradient stages ~~~~~~ //
// Clamp x to [0,1], both sides inclusive (think, gradients). // Even repeat and mirror funnel through a clamp to handle bad inputs like +Inf, NaN.
SI F clamp_01_(F v) { return min(max(0, v), 1); }
LOWP_STAGE_GG(clamp_x_1 , NoCtx) { x = clamp_01_(x); }
LOWP_STAGE_GG(repeat_x_1, NoCtx) { x = clamp_01_(x - floor_(x)); }
LOWP_STAGE_GG(mirror_x_1, NoCtx) { auto two = [](F x){ return x+x; };
x = clamp_01_(abs_( (x-1.0f) - two(floor_((x-1.0f)*0.5f)) - 1.0f ));
}
SI I16 cond_to_mask_16(I32 cond) { return cast<I16>(cond); }
LOWP_STAGE_GG(decal_x, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto w = ctx->limit_x;
sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w)));
}
LOWP_STAGE_GG(decal_y, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto h = ctx->limit_y;
sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= y) & (y < h)));
}
LOWP_STAGE_GG(decal_x_and_y, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto w = ctx->limit_x; auto h = ctx->limit_y;
sk_unaligned_store(ctx->mask, cond_to_mask_16((0 <= x) & (x < w) & (0 <= y) & (y < h)));
}
LOWP_STAGE_GG(clamp_x_and_y, SkRasterPipelineContexts::CoordClampCtx* ctx) {
x = min(ctx->max_x, max(ctx->min_x, x));
y = min(ctx->max_y, max(ctx->min_y, y));
}
LOWP_STAGE_PP(check_decal_mask, SkRasterPipelineContexts::DecalTileCtx* ctx) { auto mask = sk_unaligned_load<U16>(ctx->mask);
r = r & mask;
g = g & mask;
b = b & mask;
a = a & mask;
}
SI void round_F_to_U16(F R, F G, F B, F A, U16* r, U16* g, U16* b, U16* a) { auto round_color = [](F x) { return cast<U16>(x * 255.0f + 0.5f); };
*r = round_color(min(max(0, R), 1));
*g = round_color(min(max(0, G), 1));
*b = round_color(min(max(0, B), 1));
*a = round_color(A); // we assume alpha is already in [0,1].
}
SI void gradient_lookup(const SkRasterPipelineContexts::GradientCtx* c,
U32 idx,
F t,
U16* r,
U16* g,
U16* b,
U16* a) {
F fr, fg, fb, fa, br, bg, bb, ba; #ifdefined(SKRP_CPU_AVX2)
if (c->stopCount <=8) {
__m256i lo, hi;
split(idx, &lo, &hi);
LOWP_STAGE_GP(gradient, const SkRasterPipelineContexts::GradientCtx* c) { auto t = x;
U32 idx = U32_(0);
// N.B. The loop starts at 1 because idx 0 is the color to use before the first stop.
for (size_t i = 1; i < c->stopCount; i++) {
idx += if_then_else(t >= c->ts[i], U32_(1), U32_(0));
}
gradient_lookup(c, idx, t, &r, &g, &b, &a);
}
LOWP_STAGE_GP(evenly_spaced_gradient, const SkRasterPipelineContexts::GradientCtx* c) { auto t = x; auto idx = trunc_(t * static_cast<float>(c->stopCount-1));
gradient_lookup(c, idx, t, &r, &g, &b, &a);
}
LOWP_STAGE_GP(evenly_spaced_2_stop_gradient, const SkRasterPipelineContexts::EvenlySpaced2StopGradientCtx* c) { auto t = x;
round_F_to_U16(mad(t, c->factor[0], c->bias[0]),
mad(t, c->factor[1], c->bias[1]),
mad(t, c->factor[2], c->bias[2]),
mad(t, c->factor[3], c->bias[3]),
&r,&g,&b,&a);
}
// Calculate screen coordinates sx & sy by flooring qx and qy.
I32 sx = qx >> 16,
sy = qy >> 16;
// We are going to perform a change of parameters for qx on [0, 1) to tx on [-1, 1). // This will put tx in Q15 format for use with q_mult. // Calculate tx and ty on the interval of [-1, 1). Give {qx} and {qy} are on the interval // [0, 1), where {v} is fract(v), we can transform to tx in the following manner ty follows // the same math: // tx = 2 * {qx} - 1, so // {qx} = (tx + 1) / 2. // Calculate {qx} - 1 and {qy} - 1 where the {} operation is handled by the cast, and the - 1 // is handled by the ^ 0x8000, dividing by 2 is deferred and handled in lerpX and lerpY in // order to use the full 16-bit resolution. #ifdefined(SKRP_CPU_LSX)
__m128i qx_lo, qx_hi, qy_lo, qy_hi;
split(qx, &qx_lo, &qx_hi);
split(qy, &qy_lo, &qy_hi);
__m128i temp = __lsx_vreplgr2vr_w(0x8000);
qx_lo = __lsx_vxor_v(qx_lo, temp);
qx_hi = __lsx_vxor_v(qx_hi, temp);
qy_lo = __lsx_vxor_v(qy_lo, temp);
qy_hi = __lsx_vxor_v(qy_hi, temp);
// Substituting the {qx} by the equation for tx from above into the lerp equation where v is // the lerped value: // v = {qx}*(R - L) + L, // v = 1/2*(tx + 1)*(R - L) + L // 2 * v = (tx + 1)*(R - L) + 2*L // = tx*R - tx*L + R - L + 2*L // = tx*(R - L) + (R + L). // Since R and L are on [0, 255] we need them on the interval [0, 1/2] to get them into form // for Q15_mult. If L and R where in 16.16 format, this would be done by dividing by 2^9. In // code, we can multiply by 2^7 to get the value directly. // 2 * v = tx*(R - L) + (R + L) // 2^-9 * 2 * v = tx*(R - L)*2^-9 + (R + L)*2^-9 // 2^-8 * v = 2^-9 * (tx*(R - L) + (R + L)) // v = 1/2 * (tx*(R - L) + (R + L)) auto lerpX = [&](U16 left, U16 right) -> U16 {
I16 width = (I16)(right - left) << 7;
U16 middle = (right + left) << 7; // The constrained_add is the most subtle part of lerp. The first term is on the interval // [-1, 1), and the second term is on the interval is on the interval [0, 1) because // both terms are too high by a factor of 2 which will be handled below. (Both R and L are // on [0, 1/2), but the sum R + L is on the interval [0, 1).) Generally, the sum below // should overflow, but because we know that sum produces an output on the // interval [0, 1) we know that the extra bit that would be needed will always be 0. So // we need to be careful to treat this sum as an unsigned positive number in the divide // by 2 below. Add +1 for rounding.
U16 v2 = constrained_add(scaled_mult(tx, width), middle) + 1; // Divide by 2 to calculate v and at the same time bring the intermediate value onto the // interval [0, 1/2] to set up for the lerpY. return v2 >> 1;
};
// lerpY plays the same mathematical tricks as lerpX, but the final divide is by 256 resulting // in a value on [0, 255]. auto lerpY = [&](U16 top, U16 bottom) -> U16 {
I16 width = (I16)bottom - (I16)top;
U16 middle = bottom + top; // Add + 0x80 for rounding.
U16 blend = constrained_add(scaled_mult(ty, width), middle) + 0x80;
return blend >> 8;
};
r = lerpY(topR, bottomR);
g = lerpY(topG, bottomG);
b = lerpY(topB, bottomB);
a = lerpY(topA, bottomA);
}
LOWP_STAGE_GG(xy_to_unit_angle, NoCtx) {
F xabs = abs_(x),
yabs = abs_(y);
F slope = min(xabs, yabs)/max(xabs, yabs);
F s = slope * slope;
// Use a 7th degree polynomial to approximate atan. // This was generated using sollya.gforge.inria.fr. // A float optimized polynomial was generated using the following command. // P1 = fpminimax((1/(2*Pi))*atan(x),[|1,3,5,7|],[|24...|],[2^(-40),1],relative);
F phi = slope
* (0.15912117063999176025390625f + s
* (-5.185396969318389892578125e-2f + s
* (2.476101927459239959716796875e-2f + s
* (-7.0547382347285747528076171875e-3f))));
LOWP_STAGE_PP(srcover_rgba_8888, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
load_8888_(ptr, &dr,&dg,&db,&da);
r = r + div255( dr*inv(a) );
g = g + div255( dg*inv(a) );
b = b + div255( db*inv(a) );
a = a + div255( da*inv(a) );
store_8888_(ptr, r,g,b,a);
}
// ~~~~~~ skgpu::Swizzle stage ~~~~~~ //
LOWP_STAGE_PP(swizzle, void* ctx) { auto ir = r, ig = g, ib = b, ia = a;
U16* o[] = {&r, &g, &b, &a};
char swiz[4];
memcpy(swiz, &ctx, sizeof(swiz));
// These debug stages are meant to be used with SkRasterPipelineVisualizer functions // to look at certain lanes in a pipeline at various times. // There are 3 flavors of functions for lowp, 2 for highp // debug_L_255: clamps the value of the lane to [0, 255] and puts it into // a channel of an appropriate color to visualize that component. // debug_L: Converts the value of the lane to 12.8 fixed point such that it is // easier to understand when using Viewer's debugger. A number like // 532.5 will be displayed as 0x02 0x14 0x80 0xFF. 532 -> hex is 214 // 0.5 is 0x80 / 0x100 and 0xFF indicates positive (0x00 is negative). // Note on lowp there will be no fractional components nor negative numbers. // debug_x/y: (lowp only) visualizes the lane, which is a 32 bit float (combined from // rg or ba) as 12.8 fixed point (outlined above).
LOWP_STAGE_PP(debug_r_255, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
auto px = cast<U32>(min(r, 255));
px |= 0xFF000000; // make opaque
store(ptr, px);
}
LOWP_STAGE_PP(debug_g_255, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
auto px = cast<U32>(min(g, 255)) << 8;
px |= 0xFF000000; // make opaque
store(ptr, px);
}
LOWP_STAGE_PP(debug_b_255, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
auto px = cast<U32>(min(b, 255)) << 16;
px |= 0xFF000000; // make opaque
store(ptr, px);
}
LOWP_STAGE_PP(debug_a_255, const SkRasterPipelineContexts::MemoryCtx* ctx) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy);
// Note that there won't be negative numbers nor fractional points.
SI void lowp_fixed_point(const SkRasterPipelineContexts::MemoryCtx* ctx, const size_t dx, const size_t dy, const U16 lane) { auto ptr = ptr_at_xy<uint32_t>(ctx, dx,dy); // Flip the byte ordering so it aligns with the fixed point used above auto px = cast<U32>((lane & 0xFF00) >> 8 |
(lane & 0x00FF) << 8);
store(ptr, px);
}
HIGHP_STAGE(debug_r, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, r);
}
HIGHP_STAGE(debug_g, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, g);
}
HIGHP_STAGE(debug_b, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, b);
}
HIGHP_STAGE(debug_a, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, a);
} // The special handling of x and y only make sense in lowp mode. If they are // called in highp mode (e.g. someone made a lowp pipelin and then added something // which required highp), we'll just treat x and y as r and g respectively (we have // to define *some* behavior or this won't link).
HIGHP_STAGE(debug_x, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, r);
}
HIGHP_STAGE(debug_y, const SkRasterPipelineContexts::MemoryCtx* ctx) {
highp_fixed_point(ctx, dx, dy, g);
}
/* This gives us SK_OPTS::lowp::N if lowp::N has been set, or SK_OPTS::N if it hasn't. */
namespace lowp { static constexpr size_t lowp_N = N; }
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.