Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkRasterPipeline_opts_DEFINED |
| 9 | #define SkRasterPipeline_opts_DEFINED |
| 10 | |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 11 | #include "SkColorPriv.h" |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 12 | #include "SkColorLookUpTable.h" |
Matt Sarett | db4d406 | 2016-11-16 16:07:15 -0500 | [diff] [blame] | 13 | #include "SkColorSpaceXform_A2B.h" |
| 14 | #include "SkColorSpaceXformPriv.h" |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 15 | #include "SkHalf.h" |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 16 | #include "SkImageShaderContext.h" |
Mike Klein | a0c4c34 | 2016-11-29 13:58:49 -0500 | [diff] [blame] | 17 | #include "SkMSAN.h" |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 18 | #include "SkPM4f.h" |
mtklein | 125b2aa | 2016-11-04 13:41:34 -0700 | [diff] [blame] | 19 | #include "SkPM4fPriv.h" |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 20 | #include "SkRasterPipeline.h" |
| 21 | #include "SkSRGB.h" |
mtklein | 125b2aa | 2016-11-04 13:41:34 -0700 | [diff] [blame] | 22 | #include "SkUtils.h" |
Mike Klein | 2878e76 | 2016-10-19 21:05:17 -0400 | [diff] [blame] | 23 | #include <utility> |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 24 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 25 | namespace { |
| 26 | |
Mike Klein | 2878e76 | 2016-10-19 21:05:17 -0400 | [diff] [blame] | 27 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2 |
| 28 | static constexpr int N = 8; |
| 29 | #else |
| 30 | static constexpr int N = 4; |
| 31 | #endif |
| 32 | |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 33 | using SkNf = SkNx<N, float>; |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 34 | using SkNi = SkNx<N, int32_t>; |
| 35 | using SkNu = SkNx<N, uint32_t>; |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 36 | using SkNh = SkNx<N, uint16_t>; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 37 | using SkNb = SkNx<N, uint8_t>; |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 38 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 39 | struct Stage; |
Mike Klein | bddd234 | 2016-11-29 12:46:58 -0500 | [diff] [blame] | 40 | using Fn = void(SK_VECTORCALL *)(Stage*, size_t x_tail, SkNf,SkNf,SkNf,SkNf, |
| 41 | SkNf,SkNf,SkNf,SkNf); |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 42 | struct Stage { Fn next; void* ctx; }; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 43 | |
Mike Klein | bddd234 | 2016-11-29 12:46:58 -0500 | [diff] [blame] | 44 | // x_tail encodes two values x and tail as x*N+tail, where 0 <= tail < N. |
| 45 | // x is the induction variable we're walking along, incrementing by N each step. |
| 46 | // tail == 0 means work with a full N pixels; otherwise use only the low tail pixels. |
| 47 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 48 | } // namespace |
Mike Klein | 2878e76 | 2016-10-19 21:05:17 -0400 | [diff] [blame] | 49 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 50 | #define SI static inline |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 51 | |
Mike Klein | 49372e6 | 2016-10-20 18:05:23 -0400 | [diff] [blame] | 52 | // Stages are logically a pipeline, and physically are contiguous in an array. |
| 53 | // To get to the next stage, we just increment our pointer to the next array element. |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 54 | SI void SK_VECTORCALL next(Stage* st, size_t x_tail, SkNf r, SkNf g, SkNf b, SkNf a, |
| 55 | SkNf dr, SkNf dg, SkNf db, SkNf da) { |
| 56 | st->next(st+1, x_tail, r,g,b,a, dr,dg,db,da); |
Mike Klein | 49372e6 | 2016-10-20 18:05:23 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 59 | // Stages defined below always call next. |
| 60 | // This is always the last stage, a backstop that actually returns to the caller when done. |
| 61 | SI void SK_VECTORCALL just_return(Stage*, size_t, SkNf, SkNf, SkNf, SkNf, |
| 62 | SkNf, SkNf, SkNf, SkNf) {} |
| 63 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 64 | #define STAGE(name) \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 65 | static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \ |
| 66 | SkNf& r, SkNf& g, SkNf& b, SkNf& a, \ |
| 67 | SkNf& dr, SkNf& dg, SkNf& db, SkNf& da); \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 68 | SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 69 | SkNf r, SkNf g, SkNf b, SkNf a, \ |
| 70 | SkNf dr, SkNf dg, SkNf db, SkNf da) { \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 71 | name##_kernel(st->ctx, x_tail/N, x_tail%N, r,g,b,a, dr,dg,db,da); \ |
| 72 | next(st, x_tail, r,g,b,a, dr,dg,db,da); \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 73 | } \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 74 | static SK_ALWAYS_INLINE void name##_kernel(void* ctx, size_t x, size_t tail, \ |
| 75 | SkNf& r, SkNf& g, SkNf& b, SkNf& a, \ |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 76 | SkNf& dr, SkNf& dg, SkNf& db, SkNf& da) |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 77 | |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 78 | |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 79 | // Many xfermodes apply the same logic to each channel. |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 80 | #define RGBA_XFERMODE(name) \ |
| 81 | static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \ |
| 82 | const SkNf& d, const SkNf& da); \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 83 | SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 84 | SkNf r, SkNf g, SkNf b, SkNf a, \ |
| 85 | SkNf dr, SkNf dg, SkNf db, SkNf da) { \ |
| 86 | r = name##_kernel(r,a,dr,da); \ |
| 87 | g = name##_kernel(g,a,dg,da); \ |
| 88 | b = name##_kernel(b,a,db,da); \ |
| 89 | a = name##_kernel(a,a,da,da); \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 90 | next(st, x_tail, r,g,b,a, dr,dg,db,da); \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 91 | } \ |
| 92 | static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \ |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 93 | const SkNf& d, const SkNf& da) |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 94 | |
| 95 | // Most of the rest apply the same logic to color channels and use srcover's alpha logic. |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 96 | #define RGB_XFERMODE(name) \ |
| 97 | static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \ |
| 98 | const SkNf& d, const SkNf& da); \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 99 | SI void SK_VECTORCALL name(Stage* st, size_t x_tail, \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 100 | SkNf r, SkNf g, SkNf b, SkNf a, \ |
| 101 | SkNf dr, SkNf dg, SkNf db, SkNf da) { \ |
| 102 | r = name##_kernel(r,a,dr,da); \ |
| 103 | g = name##_kernel(g,a,dg,da); \ |
| 104 | b = name##_kernel(b,a,db,da); \ |
| 105 | a = a + (da * (1.0f-a)); \ |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 106 | next(st, x_tail, r,g,b,a, dr,dg,db,da); \ |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 107 | } \ |
| 108 | static SK_ALWAYS_INLINE SkNf name##_kernel(const SkNf& s, const SkNf& sa, \ |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 109 | const SkNf& d, const SkNf& da) |
| 110 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 111 | template <typename T> |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 112 | SI SkNx<N,T> load(size_t tail, const T* src) { |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 113 | if (tail) { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 114 | T buf[8] = {0}; |
| 115 | switch (tail & (N-1)) { |
| 116 | case 7: buf[6] = src[6]; |
| 117 | case 6: buf[5] = src[5]; |
| 118 | case 5: buf[4] = src[4]; |
| 119 | case 4: buf[3] = src[3]; |
| 120 | case 3: buf[2] = src[2]; |
| 121 | case 2: buf[1] = src[1]; |
| 122 | } |
| 123 | buf[0] = src[0]; |
| 124 | return SkNx<N,T>::Load(buf); |
| 125 | } |
| 126 | return SkNx<N,T>::Load(src); |
| 127 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 128 | template <typename T> |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 129 | SI SkNx<N,T> gather(size_t tail, const T* src, const SkNi& offset) { |
| 130 | if (tail) { |
| 131 | T buf[8] = {0}; |
| 132 | switch (tail & (N-1)) { |
| 133 | case 7: buf[6] = src[offset[6]]; |
| 134 | case 6: buf[5] = src[offset[5]]; |
| 135 | case 5: buf[4] = src[offset[4]]; |
| 136 | case 4: buf[3] = src[offset[3]]; |
| 137 | case 3: buf[2] = src[offset[2]]; |
| 138 | case 2: buf[1] = src[offset[1]]; |
| 139 | } |
| 140 | buf[0] = src[offset[0]]; |
| 141 | return SkNx<N,T>::Load(buf); |
| 142 | } |
| 143 | T buf[8]; |
| 144 | for (size_t i = 0; i < N; i++) { |
| 145 | buf[i] = src[offset[i]]; |
| 146 | } |
| 147 | return SkNx<N,T>::Load(buf); |
| 148 | } |
| 149 | template <typename T> |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 150 | SI void store(size_t tail, const SkNx<N,T>& v, T* dst) { |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 151 | if (tail) { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 152 | switch (tail & (N-1)) { |
| 153 | case 7: dst[6] = v[6]; |
| 154 | case 6: dst[5] = v[5]; |
| 155 | case 5: dst[4] = v[4]; |
| 156 | case 4: dst[3] = v[3]; |
| 157 | case 3: dst[2] = v[2]; |
| 158 | case 2: dst[1] = v[1]; |
| 159 | } |
| 160 | dst[0] = v[0]; |
| 161 | return; |
| 162 | } |
| 163 | v.store(dst); |
| 164 | } |
| 165 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 166 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_AVX2 |
| 167 | SI __m256i mask(size_t tail) { |
| 168 | static const int masks[][8] = { |
| 169 | {~0,~0,~0,~0, ~0,~0,~0,~0 }, // remember, tail == 0 ~~> load all N |
| 170 | {~0, 0, 0, 0, 0, 0, 0, 0 }, |
| 171 | {~0,~0, 0, 0, 0, 0, 0, 0 }, |
| 172 | {~0,~0,~0, 0, 0, 0, 0, 0 }, |
| 173 | {~0,~0,~0,~0, 0, 0, 0, 0 }, |
| 174 | {~0,~0,~0,~0, ~0, 0, 0, 0 }, |
| 175 | {~0,~0,~0,~0, ~0,~0, 0, 0 }, |
| 176 | {~0,~0,~0,~0, ~0,~0,~0, 0 }, |
| 177 | }; |
| 178 | return SkNi::Load(masks + tail).fVec; |
| 179 | } |
| 180 | |
| 181 | SI SkNi load(size_t tail, const int32_t* src) { |
| 182 | return tail ? _mm256_maskload_epi32((const int*)src, mask(tail)) |
| 183 | : SkNi::Load(src); |
| 184 | } |
| 185 | SI SkNu load(size_t tail, const uint32_t* src) { |
| 186 | return tail ? _mm256_maskload_epi32((const int*)src, mask(tail)) |
| 187 | : SkNu::Load(src); |
| 188 | } |
| 189 | SI SkNi gather(size_t tail, const int32_t* src, const SkNi& offset) { |
| 190 | return _mm256_mask_i32gather_epi32(SkNi(0).fVec, |
| 191 | (const int*)src, offset.fVec, mask(tail), 4); |
| 192 | } |
| 193 | SI SkNu gather(size_t tail, const uint32_t* src, const SkNi& offset) { |
| 194 | return _mm256_mask_i32gather_epi32(SkNi(0).fVec, |
| 195 | (const int*)src, offset.fVec, mask(tail), 4); |
| 196 | } |
Mike Klein | a0c4c34 | 2016-11-29 13:58:49 -0500 | [diff] [blame] | 197 | |
| 198 | static const char* bug = "I don't think MSAN understands maskstore."; |
| 199 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 200 | SI void store(size_t tail, const SkNi& v, int32_t* dst) { |
Mike Klein | a0c4c34 | 2016-11-29 13:58:49 -0500 | [diff] [blame] | 201 | if (tail) { |
| 202 | _mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec); |
| 203 | return sk_msan_mark_initialized(dst, dst+tail, bug); |
| 204 | } |
| 205 | v.store(dst); |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 206 | } |
| 207 | SI void store(size_t tail, const SkNu& v, uint32_t* dst) { |
Mike Klein | a0c4c34 | 2016-11-29 13:58:49 -0500 | [diff] [blame] | 208 | if (tail) { |
| 209 | _mm256_maskstore_epi32((int*)dst, mask(tail), v.fVec); |
| 210 | return sk_msan_mark_initialized(dst, dst+tail, bug); |
| 211 | } |
| 212 | v.store(dst); |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 213 | } |
| 214 | #endif |
| 215 | |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 216 | SI void from_8888(const SkNu& _8888, SkNf* r, SkNf* g, SkNf* b, SkNf* a) { |
| 217 | auto to_float = [](const SkNu& v) { return SkNx_cast<float>(SkNi::Load(&v)); }; |
| 218 | *r = (1/255.0f)*to_float((_8888 >> 0) & 0xff); |
| 219 | *g = (1/255.0f)*to_float((_8888 >> 8) & 0xff); |
| 220 | *b = (1/255.0f)*to_float((_8888 >> 16) & 0xff); |
| 221 | *a = (1/255.0f)*to_float( _8888 >> 24 ); |
| 222 | } |
Mike Klein | 6b77f1c | 2016-11-22 15:50:12 -0500 | [diff] [blame] | 223 | SI void from_4444(const SkNh& _4444, SkNf* r, SkNf* g, SkNf* b, SkNf* a) { |
| 224 | auto _32_bit = SkNx_cast<int>(_4444); |
| 225 | |
| 226 | *r = SkNx_cast<float>(_32_bit & (0xF << SK_R4444_SHIFT)) * (1.0f / (0xF << SK_R4444_SHIFT)); |
| 227 | *g = SkNx_cast<float>(_32_bit & (0xF << SK_G4444_SHIFT)) * (1.0f / (0xF << SK_G4444_SHIFT)); |
| 228 | *b = SkNx_cast<float>(_32_bit & (0xF << SK_B4444_SHIFT)) * (1.0f / (0xF << SK_B4444_SHIFT)); |
| 229 | *a = SkNx_cast<float>(_32_bit & (0xF << SK_A4444_SHIFT)) * (1.0f / (0xF << SK_A4444_SHIFT)); |
| 230 | } |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 231 | SI void from_565(const SkNh& _565, SkNf* r, SkNf* g, SkNf* b) { |
| 232 | auto _32_bit = SkNx_cast<int>(_565); |
| 233 | |
| 234 | *r = SkNx_cast<float>(_32_bit & SK_R16_MASK_IN_PLACE) * (1.0f / SK_R16_MASK_IN_PLACE); |
| 235 | *g = SkNx_cast<float>(_32_bit & SK_G16_MASK_IN_PLACE) * (1.0f / SK_G16_MASK_IN_PLACE); |
| 236 | *b = SkNx_cast<float>(_32_bit & SK_B16_MASK_IN_PLACE) * (1.0f / SK_B16_MASK_IN_PLACE); |
| 237 | } |
| 238 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 239 | STAGE(trace) { |
Mike Klein | a9312fd | 2016-11-16 13:38:15 -0500 | [diff] [blame] | 240 | SkDebugf("%s\n", (const char*)ctx); |
| 241 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 242 | STAGE(registers) { |
Mike Klein | a9312fd | 2016-11-16 13:38:15 -0500 | [diff] [blame] | 243 | auto print = [](const char* name, const SkNf& v) { |
| 244 | SkDebugf("%s:", name); |
| 245 | for (int i = 0; i < N; i++) { |
| 246 | SkDebugf(" %g", v[i]); |
| 247 | } |
| 248 | SkDebugf("\n"); |
| 249 | }; |
| 250 | print(" r", r); |
| 251 | print(" g", g); |
| 252 | print(" b", b); |
| 253 | print(" a", a); |
| 254 | print("dr", dr); |
| 255 | print("dg", dg); |
| 256 | print("db", db); |
| 257 | print("da", da); |
| 258 | } |
| 259 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 260 | STAGE(clamp_0) { |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 261 | a = SkNf::Max(a, 0.0f); |
| 262 | r = SkNf::Max(r, 0.0f); |
| 263 | g = SkNf::Max(g, 0.0f); |
| 264 | b = SkNf::Max(b, 0.0f); |
| 265 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 266 | STAGE(clamp_a) { |
Mike Klein | 130863e | 2016-10-27 11:29:36 -0400 | [diff] [blame] | 267 | a = SkNf::Min(a, 1.0f); |
| 268 | r = SkNf::Min(r, a); |
| 269 | g = SkNf::Min(g, a); |
| 270 | b = SkNf::Min(b, a); |
| 271 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 272 | STAGE(clamp_1) { |
Matt Sarett | db4d406 | 2016-11-16 16:07:15 -0500 | [diff] [blame] | 273 | a = SkNf::Min(a, 1.0f); |
| 274 | r = SkNf::Min(r, 1.0f); |
| 275 | g = SkNf::Min(g, 1.0f); |
| 276 | b = SkNf::Min(b, 1.0f); |
| 277 | } |
| 278 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 279 | STAGE(unpremul) { |
Mike Klein | 5a13011 | 2016-11-28 09:48:31 -0500 | [diff] [blame] | 280 | auto scale = (a == 0.0f).thenElse(0.0f, 1.0f/a); |
| 281 | r *= scale; |
| 282 | g *= scale; |
| 283 | b *= scale; |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 284 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 285 | STAGE(premul) { |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 286 | r *= a; |
| 287 | g *= a; |
| 288 | b *= a; |
| 289 | } |
| 290 | |
Mike Klein | 7a14734 | 2016-11-29 15:33:39 -0500 | [diff] [blame^] | 291 | STAGE(set_rgb) { |
| 292 | auto rgb = (const float*)ctx; |
| 293 | r = rgb[0]; |
| 294 | g = rgb[1]; |
| 295 | b = rgb[2]; |
| 296 | } |
| 297 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 298 | STAGE(move_src_dst) { |
Mike Klein | c509341 | 2016-11-04 16:36:39 -0400 | [diff] [blame] | 299 | dr = r; |
| 300 | dg = g; |
| 301 | db = b; |
| 302 | da = a; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 303 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 304 | STAGE(move_dst_src) { |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 305 | r = dr; |
| 306 | g = dg; |
| 307 | b = db; |
| 308 | a = da; |
Mike Klein | fb191da | 2016-11-15 13:20:33 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 311 | STAGE(swap_rb) { SkTSwap( r, b); } |
| 312 | STAGE(swap_rb_d) { SkTSwap(dr, db); } |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 313 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 314 | STAGE(from_srgb) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 315 | r = sk_linear_from_srgb_math(r); |
| 316 | g = sk_linear_from_srgb_math(g); |
| 317 | b = sk_linear_from_srgb_math(b); |
| 318 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 319 | STAGE(from_srgb_d) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 320 | dr = sk_linear_from_srgb_math(dr); |
| 321 | dg = sk_linear_from_srgb_math(dg); |
| 322 | db = sk_linear_from_srgb_math(db); |
| 323 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 324 | STAGE(to_srgb) { |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 325 | r = sk_linear_to_srgb_needs_round(r); |
| 326 | g = sk_linear_to_srgb_needs_round(g); |
| 327 | b = sk_linear_to_srgb_needs_round(b); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 328 | } |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 329 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 330 | // The default shader produces a constant color (from the SkPaint). |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 331 | STAGE(constant_color) { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 332 | auto color = (const SkPM4f*)ctx; |
| 333 | r = color->r(); |
| 334 | g = color->g(); |
| 335 | b = color->b(); |
| 336 | a = color->a(); |
| 337 | } |
| 338 | |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 339 | // s' = sc for a constant c. |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 340 | STAGE(scale_constant_float) { |
Mike Klein | 6686617 | 2016-11-03 12:22:01 -0400 | [diff] [blame] | 341 | SkNf c = *(const float*)ctx; |
| 342 | |
| 343 | r *= c; |
| 344 | g *= c; |
| 345 | b *= c; |
| 346 | a *= c; |
| 347 | } |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 348 | // s' = sc for 8-bit c. |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 349 | STAGE(scale_u8) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 350 | auto ptr = *(const uint8_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 351 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 352 | SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f); |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 353 | r = r*c; |
| 354 | g = g*c; |
| 355 | b = b*c; |
| 356 | a = a*c; |
| 357 | } |
| 358 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 359 | SI SkNf lerp(const SkNf& from, const SkNf& to, const SkNf& cov) { |
| 360 | return SkNx_fma(to-from, cov, from); |
| 361 | } |
| 362 | |
| 363 | // s' = d(1-c) + sc, for a constant c. |
| 364 | STAGE(lerp_constant_float) { |
| 365 | SkNf c = *(const float*)ctx; |
| 366 | |
| 367 | r = lerp(dr, r, c); |
| 368 | g = lerp(dg, g, c); |
| 369 | b = lerp(db, b, c); |
| 370 | a = lerp(da, a, c); |
| 371 | } |
| 372 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 373 | // s' = d(1-c) + sc for 8-bit c. |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 374 | STAGE(lerp_u8) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 375 | auto ptr = *(const uint8_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 376 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 377 | SkNf c = SkNx_cast<float>(load(tail, ptr)) * (1/255.0f); |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 378 | r = lerp(dr, r, c); |
| 379 | g = lerp(dg, g, c); |
| 380 | b = lerp(db, b, c); |
| 381 | a = lerp(da, a, c); |
| 382 | } |
| 383 | |
| 384 | // s' = d(1-c) + sc for 565 c. |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 385 | STAGE(lerp_565) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 386 | auto ptr = *(const uint16_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 387 | SkNf cr, cg, cb; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 388 | from_565(load(tail, ptr), &cr, &cg, &cb); |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 389 | |
| 390 | r = lerp(dr, r, cr); |
| 391 | g = lerp(dg, g, cg); |
| 392 | b = lerp(db, b, cb); |
| 393 | a = 1.0f; |
| 394 | } |
| 395 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 396 | STAGE(load_565) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 397 | auto ptr = *(const uint16_t**)ctx + x; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 398 | from_565(load(tail, ptr), &r,&g,&b); |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 399 | a = 1.0f; |
| 400 | } |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 401 | STAGE(load_565_d) { |
| 402 | auto ptr = *(const uint16_t**)ctx + x; |
| 403 | from_565(load(tail, ptr), &dr,&dg,&db); |
| 404 | da = 1.0f; |
| 405 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 406 | STAGE(store_565) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 407 | auto ptr = *(uint16_t**)ctx + x; |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 408 | store(tail, SkNx_cast<uint16_t>( SkNx_cast<int>(r*SK_R16_MASK + 0.5f) << SK_R16_SHIFT |
| 409 | | SkNx_cast<int>(g*SK_G16_MASK + 0.5f) << SK_G16_SHIFT |
| 410 | | SkNx_cast<int>(b*SK_B16_MASK + 0.5f) << SK_B16_SHIFT), ptr); |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 411 | } |
| 412 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 413 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 414 | STAGE(load_f16) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 415 | auto ptr = *(const uint64_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 416 | |
| 417 | SkNh rh, gh, bh, ah; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 418 | if (tail) { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 419 | uint64_t buf[8] = {0}; |
| 420 | switch (tail & (N-1)) { |
| 421 | case 7: buf[6] = ptr[6]; |
| 422 | case 6: buf[5] = ptr[5]; |
| 423 | case 5: buf[4] = ptr[4]; |
| 424 | case 4: buf[3] = ptr[3]; |
| 425 | case 3: buf[2] = ptr[2]; |
| 426 | case 2: buf[1] = ptr[1]; |
| 427 | } |
| 428 | buf[0] = ptr[0]; |
| 429 | SkNh::Load4(buf, &rh, &gh, &bh, &ah); |
| 430 | } else { |
| 431 | SkNh::Load4(ptr, &rh, &gh, &bh, &ah); |
| 432 | } |
| 433 | |
| 434 | r = SkHalfToFloat_finite_ftz(rh); |
| 435 | g = SkHalfToFloat_finite_ftz(gh); |
| 436 | b = SkHalfToFloat_finite_ftz(bh); |
| 437 | a = SkHalfToFloat_finite_ftz(ah); |
| 438 | } |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 439 | STAGE(load_f16_d) { |
| 440 | auto ptr = *(const uint64_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 441 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 442 | SkNh rh, gh, bh, ah; |
| 443 | if (tail) { |
| 444 | uint64_t buf[8] = {0}; |
| 445 | switch (tail & (N-1)) { |
| 446 | case 7: buf[6] = ptr[6]; |
| 447 | case 6: buf[5] = ptr[5]; |
| 448 | case 5: buf[4] = ptr[4]; |
| 449 | case 4: buf[3] = ptr[3]; |
| 450 | case 3: buf[2] = ptr[2]; |
| 451 | case 2: buf[1] = ptr[1]; |
| 452 | } |
| 453 | buf[0] = ptr[0]; |
| 454 | SkNh::Load4(buf, &rh, &gh, &bh, &ah); |
| 455 | } else { |
| 456 | SkNh::Load4(ptr, &rh, &gh, &bh, &ah); |
| 457 | } |
| 458 | |
| 459 | dr = SkHalfToFloat_finite_ftz(rh); |
| 460 | dg = SkHalfToFloat_finite_ftz(gh); |
| 461 | db = SkHalfToFloat_finite_ftz(bh); |
| 462 | da = SkHalfToFloat_finite_ftz(ah); |
| 463 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 464 | STAGE(store_f16) { |
Mike Klein | bd3fe47 | 2016-10-25 15:43:46 -0400 | [diff] [blame] | 465 | auto ptr = *(uint64_t**)ctx + x; |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 466 | |
| 467 | uint64_t buf[8]; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 468 | SkNh::Store4(tail ? buf : ptr, SkFloatToHalf_finite_ftz(r), |
| 469 | SkFloatToHalf_finite_ftz(g), |
| 470 | SkFloatToHalf_finite_ftz(b), |
| 471 | SkFloatToHalf_finite_ftz(a)); |
| 472 | if (tail) { |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 473 | switch (tail & (N-1)) { |
| 474 | case 7: ptr[6] = buf[6]; |
| 475 | case 6: ptr[5] = buf[5]; |
| 476 | case 5: ptr[4] = buf[4]; |
| 477 | case 4: ptr[3] = buf[3]; |
| 478 | case 3: ptr[2] = buf[2]; |
| 479 | case 2: ptr[1] = buf[1]; |
| 480 | } |
| 481 | ptr[0] = buf[0]; |
| 482 | } |
| 483 | } |
| 484 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 485 | STAGE(store_f32) { |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 486 | auto ptr = *(SkPM4f**)ctx + x; |
| 487 | |
| 488 | SkPM4f buf[8]; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 489 | SkNf::Store4(tail ? buf : ptr, r,g,b,a); |
| 490 | if (tail) { |
mtklein | a4a4488 | 2016-11-04 13:20:07 -0700 | [diff] [blame] | 491 | switch (tail & (N-1)) { |
| 492 | case 7: ptr[6] = buf[6]; |
| 493 | case 6: ptr[5] = buf[5]; |
| 494 | case 5: ptr[4] = buf[4]; |
| 495 | case 4: ptr[3] = buf[3]; |
| 496 | case 3: ptr[2] = buf[2]; |
| 497 | case 2: ptr[1] = buf[1]; |
| 498 | } |
| 499 | ptr[0] = buf[0]; |
| 500 | } |
| 501 | } |
| 502 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 503 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 504 | STAGE(load_8888) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 505 | auto ptr = *(const uint32_t**)ctx + x; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 506 | from_8888(load(tail, ptr), &r, &g, &b, &a); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 507 | } |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 508 | STAGE(load_8888_d) { |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 509 | auto ptr = *(const uint32_t**)ctx + x; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 510 | from_8888(load(tail, ptr), &dr, &dg, &db, &da); |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 511 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 512 | STAGE(store_8888) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 513 | auto ptr = *(uint32_t**)ctx + x; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 514 | store(tail, ( SkNx_cast<int>(255.0f * r + 0.5f) << 0 |
| 515 | | SkNx_cast<int>(255.0f * g + 0.5f) << 8 |
| 516 | | SkNx_cast<int>(255.0f * b + 0.5f) << 16 |
| 517 | | SkNx_cast<int>(255.0f * a + 0.5f) << 24 ), (int*)ptr); |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 520 | SI SkNf inv(const SkNf& x) { return 1.0f - x; } |
| 521 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 522 | RGBA_XFERMODE(clear) { return 0.0f; } |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 523 | RGBA_XFERMODE(srcatop) { return s*da + d*inv(sa); } |
| 524 | RGBA_XFERMODE(srcin) { return s * da; } |
| 525 | RGBA_XFERMODE(srcout) { return s * inv(da); } |
| 526 | RGBA_XFERMODE(srcover) { return SkNx_fma(d, inv(sa), s); } |
| 527 | RGBA_XFERMODE(dstatop) { return srcatop_kernel(d,da,s,sa); } |
| 528 | RGBA_XFERMODE(dstin) { return srcin_kernel (d,da,s,sa); } |
| 529 | RGBA_XFERMODE(dstout) { return srcout_kernel (d,da,s,sa); } |
| 530 | RGBA_XFERMODE(dstover) { return srcover_kernel(d,da,s,sa); } |
| 531 | |
| 532 | RGBA_XFERMODE(modulate) { return s*d; } |
| 533 | RGBA_XFERMODE(multiply) { return s*inv(da) + d*inv(sa) + s*d; } |
| 534 | RGBA_XFERMODE(plus_) { return s + d; } |
| 535 | RGBA_XFERMODE(screen) { return s + d - s*d; } |
| 536 | RGBA_XFERMODE(xor_) { return s*inv(da) + d*inv(sa); } |
| 537 | |
| 538 | RGB_XFERMODE(colorburn) { |
| 539 | return (d == da ).thenElse(d + s*inv(da), |
| 540 | (s == 0.0f).thenElse(s + d*inv(sa), |
| 541 | sa*(da - SkNf::Min(da, (da-d)*sa/s)) + s*inv(da) + d*inv(sa))); |
| 542 | } |
| 543 | RGB_XFERMODE(colordodge) { |
| 544 | return (d == 0.0f).thenElse(d + s*inv(da), |
| 545 | (s == sa ).thenElse(s + d*inv(sa), |
| 546 | sa*SkNf::Min(da, (d*sa)/(sa - s)) + s*inv(da) + d*inv(sa))); |
| 547 | } |
| 548 | RGB_XFERMODE(darken) { return s + d - SkNf::Max(s*da, d*sa); } |
| 549 | RGB_XFERMODE(difference) { return s + d - 2.0f*SkNf::Min(s*da,d*sa); } |
| 550 | RGB_XFERMODE(exclusion) { return s + d - 2.0f*s*d; } |
| 551 | RGB_XFERMODE(hardlight) { |
| 552 | return s*inv(da) + d*inv(sa) |
| 553 | + (2.0f*s <= sa).thenElse(2.0f*s*d, sa*da - 2.0f*(da-d)*(sa-s)); |
| 554 | } |
| 555 | RGB_XFERMODE(lighten) { return s + d - SkNf::Min(s*da, d*sa); } |
| 556 | RGB_XFERMODE(overlay) { return hardlight_kernel(d,da,s,sa); } |
| 557 | RGB_XFERMODE(softlight) { |
| 558 | SkNf m = (da > 0.0f).thenElse(d / da, 0.0f), |
| 559 | s2 = 2.0f*s, |
| 560 | m4 = 4.0f*m; |
| 561 | |
| 562 | // The logic forks three ways: |
| 563 | // 1. dark src? |
| 564 | // 2. light src, dark dst? |
| 565 | // 3. light src, light dst? |
| 566 | SkNf darkSrc = d*(sa + (s2 - sa)*(1.0f - m)), // Used in case 1. |
| 567 | darkDst = (m4*m4 + m4)*(m - 1.0f) + 7.0f*m, // Used in case 2. |
| 568 | liteDst = m.rsqrt().invert() - m, // Used in case 3. |
| 569 | liteSrc = d*sa + da*(s2 - sa) * (4.0f*d <= da).thenElse(darkDst, liteDst); // 2 or 3? |
| 570 | return s*inv(da) + d*inv(sa) + (s2 <= sa).thenElse(darkSrc, liteSrc); // 1 or (2 or 3)? |
| 571 | } |
| 572 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 573 | STAGE(luminance_to_alpha) { |
Mike Klein | 1f49f26 | 2016-10-31 19:49:27 -0400 | [diff] [blame] | 574 | a = SK_LUM_COEFF_R*r + SK_LUM_COEFF_G*g + SK_LUM_COEFF_B*b; |
| 575 | r = g = b = 0; |
| 576 | } |
| 577 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 578 | STAGE(matrix_2x3) { |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 579 | auto m = (const float*)ctx; |
| 580 | |
| 581 | auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); }; |
| 582 | auto R = fma(r,m[0], fma(g,m[2], m[4])), |
| 583 | G = fma(r,m[1], fma(g,m[3], m[5])); |
| 584 | r = R; |
| 585 | g = G; |
| 586 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 587 | STAGE(matrix_3x4) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 588 | auto m = (const float*)ctx; |
| 589 | |
| 590 | auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); }; |
| 591 | auto R = fma(r,m[0], fma(g,m[3], fma(b,m[6], m[ 9]))), |
| 592 | G = fma(r,m[1], fma(g,m[4], fma(b,m[7], m[10]))), |
| 593 | B = fma(r,m[2], fma(g,m[5], fma(b,m[8], m[11]))); |
| 594 | r = R; |
| 595 | g = G; |
| 596 | b = B; |
| 597 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 598 | STAGE(matrix_4x5) { |
Mike Klein | eea7c16 | 2016-11-03 10:20:35 -0400 | [diff] [blame] | 599 | auto m = (const float*)ctx; |
| 600 | |
| 601 | auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); }; |
| 602 | auto R = fma(r,m[0], fma(g,m[4], fma(b,m[ 8], fma(a,m[12], m[16])))), |
| 603 | G = fma(r,m[1], fma(g,m[5], fma(b,m[ 9], fma(a,m[13], m[17])))), |
| 604 | B = fma(r,m[2], fma(g,m[6], fma(b,m[10], fma(a,m[14], m[18])))), |
| 605 | A = fma(r,m[3], fma(g,m[7], fma(b,m[11], fma(a,m[15], m[19])))); |
| 606 | r = R; |
| 607 | g = G; |
| 608 | b = B; |
| 609 | a = A; |
| 610 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 611 | STAGE(matrix_perspective) { |
Mike Klein | c01e7df | 2016-11-17 16:27:10 -0500 | [diff] [blame] | 612 | // N.B. unlike the matrix_NxM stages, this takes a row-major matrix. |
| 613 | auto m = (const float*)ctx; |
| 614 | |
| 615 | auto fma = [](const SkNf& f, const SkNf& m, const SkNf& a) { return SkNx_fma(f,m,a); }; |
| 616 | auto R = fma(r,m[0], fma(g,m[1], m[2])), |
| 617 | G = fma(r,m[3], fma(g,m[4], m[5])), |
| 618 | Z = fma(r,m[6], fma(g,m[7], m[8])); |
| 619 | r = R * Z.invert(); |
| 620 | g = G * Z.invert(); |
| 621 | } |
| 622 | |
| 623 | |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 624 | SI SkNf parametric(const SkNf& v, const SkColorSpaceTransferFn& p) { |
| 625 | float result[N]; // Unconstrained powf() doesn't vectorize well... |
| 626 | for (int i = 0; i < N; i++) { |
| 627 | float s = v[i]; |
| 628 | result[i] = (s <= p.fD) ? p.fE * s + p.fF |
| 629 | : powf(s * p.fA + p.fB, p.fG) + p.fC; |
| 630 | } |
| 631 | return SkNf::Load(result); |
| 632 | } |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 633 | STAGE(parametric_r) { r = parametric(r, *(const SkColorSpaceTransferFn*)ctx); } |
| 634 | STAGE(parametric_g) { g = parametric(g, *(const SkColorSpaceTransferFn*)ctx); } |
| 635 | STAGE(parametric_b) { b = parametric(b, *(const SkColorSpaceTransferFn*)ctx); } |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 636 | |
Matt Sarett | db4d406 | 2016-11-16 16:07:15 -0500 | [diff] [blame] | 637 | SI SkNf table(const SkNf& v, const SkTableTransferFn& table) { |
| 638 | float result[N]; |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 639 | for (int i = 0; i < N; i++) { |
Matt Sarett | db4d406 | 2016-11-16 16:07:15 -0500 | [diff] [blame] | 640 | result[i] = interp_lut(v[i], table.fData, table.fSize); |
Mike Klein | cfcf624 | 2016-11-16 09:01:30 -0500 | [diff] [blame] | 641 | } |
| 642 | return SkNf::Load(result); |
| 643 | } |
Mike Klein | 729b582 | 2016-11-28 18:23:23 -0500 | [diff] [blame] | 644 | STAGE(table_r) { r = table(r, *(const SkTableTransferFn*)ctx); } |
| 645 | STAGE(table_g) { g = table(g, *(const SkTableTransferFn*)ctx); } |
| 646 | STAGE(table_b) { b = table(b, *(const SkTableTransferFn*)ctx); } |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 647 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 648 | STAGE(color_lookup_table) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 649 | const SkColorLookUpTable* colorLUT = (const SkColorLookUpTable*)ctx; |
| 650 | float rgb[3]; |
| 651 | float result[3][N]; |
| 652 | for (int i = 0; i < N; ++i) { |
| 653 | rgb[0] = r[i]; |
| 654 | rgb[1] = g[i]; |
| 655 | rgb[2] = b[i]; |
| 656 | colorLUT->interp3D(rgb, rgb); |
| 657 | result[0][i] = rgb[0]; |
| 658 | result[1][i] = rgb[1]; |
| 659 | result[2][i] = rgb[2]; |
| 660 | } |
| 661 | r = SkNf::Load(result[0]); |
| 662 | g = SkNf::Load(result[1]); |
| 663 | b = SkNf::Load(result[2]); |
| 664 | } |
| 665 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 666 | STAGE(lab_to_xyz) { |
raftias | 2563601 | 2016-11-11 15:27:39 -0800 | [diff] [blame] | 667 | const auto lab_l = r * 100.0f; |
| 668 | const auto lab_a = g * 255.0f - 128.0f; |
| 669 | const auto lab_b = b * 255.0f - 128.0f; |
| 670 | auto Y = (lab_l + 16.0f) * (1/116.0f); |
| 671 | auto X = lab_a * (1/500.0f) + Y; |
| 672 | auto Z = Y - (lab_b * (1/200.0f)); |
| 673 | |
| 674 | const auto X3 = X*X*X; |
| 675 | X = (X3 > 0.008856f).thenElse(X3, (X - (16/116.0f)) * (1/7.787f)); |
| 676 | const auto Y3 = Y*Y*Y; |
| 677 | Y = (Y3 > 0.008856f).thenElse(Y3, (Y - (16/116.0f)) * (1/7.787f)); |
| 678 | const auto Z3 = Z*Z*Z; |
| 679 | Z = (Z3 > 0.008856f).thenElse(Z3, (Z - (16/116.0f)) * (1/7.787f)); |
| 680 | |
| 681 | // adjust to D50 illuminant |
| 682 | X *= 0.96422f; |
| 683 | Y *= 1.00000f; |
| 684 | Z *= 0.82521f; |
| 685 | |
| 686 | r = X; |
| 687 | g = Y; |
| 688 | b = Z; |
| 689 | } |
| 690 | |
Mike Klein | b273fc4 | 2016-11-17 15:42:22 -0500 | [diff] [blame] | 691 | SI SkNf assert_in_tile(const SkNf& v, float limit) { |
| 692 | for (int i = 0; i < N; i++) { |
| 693 | SkASSERT(0 <= v[i] && v[i] < limit); |
| 694 | } |
| 695 | return v; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 696 | } |
Mike Klein | b273fc4 | 2016-11-17 15:42:22 -0500 | [diff] [blame] | 697 | |
| 698 | SI SkNf clamp(const SkNf& v, float limit) { |
| 699 | SkNf result = SkNf::Max(0, SkNf::Min(v, limit - 0.5f)); |
| 700 | return assert_in_tile(result, limit); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 701 | } |
Mike Klein | b273fc4 | 2016-11-17 15:42:22 -0500 | [diff] [blame] | 702 | SI SkNf repeat(const SkNf& v, float limit) { |
| 703 | SkNf result = v - (v/limit).floor()*limit; |
Mike Klein | b273fc4 | 2016-11-17 15:42:22 -0500 | [diff] [blame] | 704 | // For small negative v, (v/limit).floor()*limit can dominate v in the subtraction, |
| 705 | // which leaves result == limit. We want result < limit, so clamp it one ULP. |
| 706 | result = SkNf::Min(result, nextafterf(limit, 0)); |
Mike Klein | b273fc4 | 2016-11-17 15:42:22 -0500 | [diff] [blame] | 707 | return assert_in_tile(result, limit); |
| 708 | } |
Mike Klein | 2e35e8a | 2016-11-18 15:47:22 -0500 | [diff] [blame] | 709 | SI SkNf mirror(const SkNf& v, float l/*imit*/) { |
| 710 | SkNf result = ((v - l) - ((v - l) / (2*l)).floor()*(2*l) - l).abs(); |
| 711 | // Same deal as repeat. |
| 712 | result = SkNf::Min(result, nextafterf(l, 0)); |
| 713 | return assert_in_tile(result, l); |
| 714 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 715 | STAGE( clamp_x) { r = clamp (r, *(const int*)ctx); } |
| 716 | STAGE(repeat_x) { r = repeat(r, *(const int*)ctx); } |
| 717 | STAGE(mirror_x) { r = mirror(r, *(const int*)ctx); } |
| 718 | STAGE( clamp_y) { g = clamp (g, *(const int*)ctx); } |
| 719 | STAGE(repeat_y) { g = repeat(g, *(const int*)ctx); } |
| 720 | STAGE(mirror_y) { g = mirror(g, *(const int*)ctx); } |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 721 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 722 | STAGE(top_left) { |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 723 | auto sc = (SkImageShaderContext*)ctx; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 724 | |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 725 | r.store(sc->x); |
| 726 | g.store(sc->y); |
| 727 | |
| 728 | r -= 0.5f; |
| 729 | g -= 0.5f; |
| 730 | |
| 731 | auto fx = r - r.floor(), |
| 732 | fy = g - g.floor(); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 733 | ((1.0f - fx) * (1.0f - fy)).store(sc->scale); |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 734 | }; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 735 | STAGE(top_right) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 736 | auto sc = (SkImageShaderContext*)ctx; |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 737 | |
| 738 | r = SkNf::Load(sc->x) + 0.5f; |
| 739 | g = SkNf::Load(sc->y) - 0.5f; |
| 740 | |
| 741 | auto fx = r - r.floor(), |
| 742 | fy = g - g.floor(); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 743 | (fx * (1.0f - fy)).store(sc->scale); |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 744 | }; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 745 | STAGE(bottom_left) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 746 | auto sc = (SkImageShaderContext*)ctx; |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 747 | |
| 748 | r = SkNf::Load(sc->x) - 0.5f; |
| 749 | g = SkNf::Load(sc->y) + 0.5f; |
| 750 | |
| 751 | auto fx = r - r.floor(), |
| 752 | fy = g - g.floor(); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 753 | ((1.0f - fx) * fy).store(sc->scale); |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 754 | }; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 755 | STAGE(bottom_right) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 756 | auto sc = (SkImageShaderContext*)ctx; |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 757 | |
| 758 | r = SkNf::Load(sc->x) + 0.5f; |
| 759 | g = SkNf::Load(sc->y) + 0.5f; |
| 760 | |
| 761 | auto fx = r - r.floor(), |
| 762 | fy = g - g.floor(); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 763 | (fx * fy).store(sc->scale); |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 764 | }; |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 765 | STAGE(accumulate) { |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 766 | auto sc = (const SkImageShaderContext*)ctx; |
| 767 | |
| 768 | auto scale = SkNf::Load(sc->scale); |
Mike Klein | c51c18f | 2016-11-28 23:17:19 -0500 | [diff] [blame] | 769 | dr = SkNx_fma(scale, r, dr); |
| 770 | dg = SkNx_fma(scale, g, dg); |
| 771 | db = SkNx_fma(scale, b, db); |
| 772 | da = SkNx_fma(scale, a, da); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 773 | } |
| 774 | |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 775 | template <typename T> |
| 776 | SI SkNi offset_and_ptr(T** ptr, const void* ctx, const SkNf& x, const SkNf& y) { |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 777 | auto sc = (const SkImageShaderContext*)ctx; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 778 | |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 779 | SkNi ix = SkNx_cast<int>(x), |
| 780 | iy = SkNx_cast<int>(y); |
Mike Klein | 46e66a2 | 2016-11-21 16:19:34 -0500 | [diff] [blame] | 781 | SkNi offset = iy*sc->stride + ix; |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 782 | |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 783 | *ptr = (const T*)sc->pixels; |
| 784 | return offset; |
| 785 | } |
| 786 | |
Mike Klein | 7a14734 | 2016-11-29 15:33:39 -0500 | [diff] [blame^] | 787 | STAGE(gather_a8) { |
| 788 | const uint8_t* p; |
| 789 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 790 | |
| 791 | r = g = b = 0.0f; |
| 792 | a = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f); |
| 793 | } |
Mike Klein | f7657e9 | 2016-11-29 12:57:22 -0500 | [diff] [blame] | 794 | STAGE(gather_i8) { |
| 795 | auto sc = (const SkImageShaderContext*)ctx; |
| 796 | const uint8_t* p; |
| 797 | SkNi offset = offset_and_ptr(&p, sc, r, g); |
| 798 | |
| 799 | SkNi ix = SkNx_cast<int>(gather(tail, p, offset)); |
| 800 | from_8888(gather(tail, sc->ctable->readColors(), ix), &r, &g, &b, &a); |
| 801 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 802 | STAGE(gather_g8) { |
Mike Klein | 6b77f1c | 2016-11-22 15:50:12 -0500 | [diff] [blame] | 803 | const uint8_t* p; |
| 804 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 805 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 806 | r = g = b = SkNx_cast<float>(gather(tail, p, offset)) * (1/255.0f); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 807 | a = 1.0f; |
Mike Klein | 6b77f1c | 2016-11-22 15:50:12 -0500 | [diff] [blame] | 808 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 809 | STAGE(gather_565) { |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 810 | const uint16_t* p; |
| 811 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 812 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 813 | from_565(gather(tail, p, offset), &r, &g, &b); |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 814 | a = 1.0f; |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 815 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 816 | STAGE(gather_4444) { |
Mike Klein | cb5338c | 2016-11-22 14:58:45 -0500 | [diff] [blame] | 817 | const uint16_t* p; |
| 818 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 819 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 820 | from_4444(gather(tail, p, offset), &r, &g, &b, &a); |
Mike Klein | cb5338c | 2016-11-22 14:58:45 -0500 | [diff] [blame] | 821 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 822 | STAGE(gather_8888) { |
Mike Klein | cb5338c | 2016-11-22 14:58:45 -0500 | [diff] [blame] | 823 | const uint32_t* p; |
| 824 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 825 | |
Mike Klein | 56b5079 | 2016-11-29 08:14:49 -0500 | [diff] [blame] | 826 | from_8888(gather(tail, p, offset), &r, &g, &b, &a); |
Mike Klein | cb5338c | 2016-11-22 14:58:45 -0500 | [diff] [blame] | 827 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 828 | STAGE(gather_f16) { |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 829 | const uint64_t* p; |
| 830 | SkNi offset = offset_and_ptr(&p, ctx, r, g); |
| 831 | |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 832 | // f16 -> f32 conversion works best with tightly packed f16s, |
| 833 | // so we gather each component rather than using gather(). |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 834 | uint16_t R[N], G[N], B[N], A[N]; |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 835 | size_t n = tail ? tail : N; |
| 836 | for (size_t i = 0; i < n; i++) { |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 837 | uint64_t rgba = p[offset[i]]; |
| 838 | R[i] = rgba >> 0; |
| 839 | G[i] = rgba >> 16; |
| 840 | B[i] = rgba >> 32; |
| 841 | A[i] = rgba >> 48; |
| 842 | } |
Mike Klein | d5de013 | 2016-11-28 09:33:02 -0500 | [diff] [blame] | 843 | for (size_t i = n; i < N; i++) { |
| 844 | R[i] = G[i] = B[i] = A[i] = 0; |
| 845 | } |
Mike Klein | b04c352 | 2016-11-28 11:55:58 -0500 | [diff] [blame] | 846 | r = SkHalfToFloat_finite_ftz(SkNh::Load(R)); |
| 847 | g = SkHalfToFloat_finite_ftz(SkNh::Load(G)); |
| 848 | b = SkHalfToFloat_finite_ftz(SkNh::Load(B)); |
| 849 | a = SkHalfToFloat_finite_ftz(SkNh::Load(A)); |
Mike Klein | cb2c12b | 2016-11-22 13:22:48 -0500 | [diff] [blame] | 850 | } |
| 851 | |
Mike Klein | 06a65e2 | 2016-11-17 12:39:09 -0500 | [diff] [blame] | 852 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 853 | SI Fn enum_to_Fn(SkRasterPipeline::StockStage st) { |
| 854 | switch (st) { |
| 855 | #define M(stage) case SkRasterPipeline::stage: return stage; |
| 856 | SK_RASTER_PIPELINE_STAGES(M) |
| 857 | #undef M |
| 858 | } |
| 859 | SkASSERT(false); |
| 860 | return just_return; |
| 861 | } |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 862 | |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 863 | namespace SK_OPTS_NS { |
| 864 | |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 865 | struct Memset16 { |
| 866 | uint16_t** dst; |
| 867 | uint16_t val; |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 868 | void operator()(size_t x, size_t, size_t n) { sk_memset16(*dst + x, val, n); } |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 869 | }; |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 870 | struct Memset32 { |
| 871 | uint32_t** dst; |
| 872 | uint32_t val; |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 873 | void operator()(size_t x, size_t, size_t n) { sk_memset32(*dst + x, val, n); } |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 874 | }; |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 875 | struct Memset64 { |
| 876 | uint64_t** dst; |
| 877 | uint64_t val; |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 878 | void operator()(size_t x, size_t, size_t n) { sk_memset64(*dst + x, val, n); } |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 879 | }; |
| 880 | |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 881 | SI std::function<void(size_t, size_t, size_t)> |
| 882 | compile_pipeline(const SkRasterPipeline::Stage* stages, int nstages) { |
mtklein | 125b2aa | 2016-11-04 13:41:34 -0700 | [diff] [blame] | 883 | if (nstages == 2 && stages[0].stage == SkRasterPipeline::constant_color) { |
| 884 | SkPM4f src = *(const SkPM4f*)stages[0].ctx; |
| 885 | void* dst = stages[1].ctx; |
| 886 | switch (stages[1].stage) { |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 887 | case SkRasterPipeline::store_565: |
| 888 | return Memset16{(uint16_t**)dst, SkPackRGB16(src.r() * SK_R16_MASK + 0.5f, |
| 889 | src.g() * SK_G16_MASK + 0.5f, |
| 890 | src.b() * SK_B16_MASK + 0.5f)}; |
Mike Klein | e03339a | 2016-11-28 13:24:27 -0500 | [diff] [blame] | 891 | case SkRasterPipeline::store_8888: |
| 892 | return Memset32{(uint32_t**)dst, Sk4f_toL32(src.to4f())}; |
mtklein | 125b2aa | 2016-11-04 13:41:34 -0700 | [diff] [blame] | 893 | |
Mike Klein | ad48a70 | 2016-11-07 17:16:21 -0500 | [diff] [blame] | 894 | case SkRasterPipeline::store_f16: |
| 895 | return Memset64{(uint64_t**)dst, src.toF16()}; |
mtklein | 125b2aa | 2016-11-04 13:41:34 -0700 | [diff] [blame] | 896 | |
| 897 | default: break; |
| 898 | } |
| 899 | } |
| 900 | |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 901 | struct Compiled { |
| 902 | Compiled(const SkRasterPipeline::Stage* stages, int nstages) { |
| 903 | if (nstages == 0) { |
| 904 | return; |
| 905 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 906 | fStart = enum_to_Fn(stages[0].stage); |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 907 | for (int i = 0; i < nstages-1; i++) { |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 908 | fStages[i].next = enum_to_Fn(stages[i+1].stage); |
| 909 | fStages[i].ctx = stages[i].ctx; |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 910 | } |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 911 | fStages[nstages-1].next = just_return; |
| 912 | fStages[nstages-1].ctx = stages[nstages-1].ctx; |
Mike Klein | 050ffa9 | 2016-10-20 16:20:46 -0400 | [diff] [blame] | 913 | } |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 914 | |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 915 | void operator()(size_t x, size_t y, size_t n) { |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 916 | float dx[] = { 0,1,2,3,4,5,6,7 }; |
Mike Klein | 0f91ea4 | 2016-11-15 10:31:38 -0500 | [diff] [blame] | 917 | SkNf X = SkNf(x) + SkNf::Load(dx) + 0.5f, |
Mike Klein | f7f883b | 2016-11-21 15:09:45 -0500 | [diff] [blame] | 918 | Y = SkNf(y) + 0.5f, |
| 919 | _0 = SkNf(0), |
| 920 | _1 = SkNf(1); |
Mike Klein | af49b19 | 2016-11-15 08:52:04 -0500 | [diff] [blame] | 921 | |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 922 | while (n >= N) { |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 923 | fStart(fStages, x*N, X,Y,_1,_0, _0,_0,_0,_0); |
Mike Klein | 0f91ea4 | 2016-11-15 10:31:38 -0500 | [diff] [blame] | 924 | X += (float)N; |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 925 | x += N; |
| 926 | n -= N; |
| 927 | } |
| 928 | if (n) { |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 929 | fStart(fStages, x*N+n, X,Y,_1,_0, _0,_0,_0,_0); |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 930 | } |
Mike Klein | 050ffa9 | 2016-10-20 16:20:46 -0400 | [diff] [blame] | 931 | } |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 932 | |
Mike Klein | 2cbc33d | 2016-11-28 16:30:30 -0500 | [diff] [blame] | 933 | Fn fStart = just_return; |
| 934 | Stage fStages[SkRasterPipeline::kMaxStages]; |
Mike Klein | e9f74b8 | 2016-10-25 13:31:21 -0400 | [diff] [blame] | 935 | |
| 936 | } fn { stages, nstages }; |
| 937 | return fn; |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 938 | } |
| 939 | |
Mike Klein | aebfb45 | 2016-10-25 10:27:33 -0400 | [diff] [blame] | 940 | } // namespace SK_OPTS_NS |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 941 | |
Mike Klein | 04adfda | 2016-10-12 09:52:55 -0400 | [diff] [blame] | 942 | #undef SI |
| 943 | #undef STAGE |
| 944 | #undef RGBA_XFERMODE |
| 945 | #undef RGB_XFERMODE |
Mike Klein | 9161ef0 | 2016-10-04 14:03:27 -0400 | [diff] [blame] | 946 | |
Mike Klein | baaf8ad | 2016-09-29 09:04:15 -0400 | [diff] [blame] | 947 | #endif//SkRasterPipeline_opts_DEFINED |