OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 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 #include "SkOpts.h" | |
9 #define SK_OPTS_NS sk_avx2 | |
10 | |
11 #ifndef SK_SUPPORT_LEGACY_X86_BLITS | |
12 | |
13 namespace sk_avx2 { | |
14 | |
15 // AVX2 has masked loads and stores. We'll use them for N<4 pixels. | |
16 static __m128i mask(int n) { | |
17 static const int masks[][4] = { | |
18 { 0, 0, 0, 0}, | |
19 {~0, 0, 0, 0}, | |
20 {~0,~0, 0, 0}, | |
21 {~0,~0,~0, 0}, | |
22 }; | |
23 return _mm_load_si128((const __m128i*)masks+n); | |
24 } | |
25 | |
26 // Load 8, 4, or 1-3 constant pixels or coverages (4x replicated). | |
27 static __m256i next8( uint32_t val) { return _mm256_set1_epi32(val); } | |
28 static __m128i next4( uint32_t val) { return _mm_set1_epi32(val); } | |
29 static __m128i tail(int, uint32_t val) { return _mm_set1_epi32(val); } | |
30 | |
31 static __m256i next8( uint8_t val) { return _mm256_set1_epi8(val); } | |
32 static __m128i next4( uint8_t val) { return _mm_set1_epi8(val); } | |
33 static __m128i tail(int, uint8_t val) { return _mm_set1_epi8(val); } | |
34 | |
35 // Load 8, 4, or 1-3 variable pixels or coverages (4x replicated). | |
36 // next8() and next4() increment their pointer past what they just read. tail()
doesn't bother. | |
37 static __m256i next8(const uint32_t*& ptr) { | |
38 auto r = _mm256_loadu_si256((const __m256i*)ptr); | |
39 ptr += 8; | |
40 return r; | |
41 } | |
42 static __m128i next4(const uint32_t*& ptr) { | |
43 auto r = _mm_loadu_si128((const __m128i*)ptr); | |
44 ptr += 4; | |
45 return r; | |
46 } | |
47 static __m128i tail(int n, const uint32_t* ptr) { | |
48 return _mm_maskload_epi32((const int*)ptr, mask(n)); | |
49 } | |
50 | |
51 static __m256i next8(const uint8_t*& ptr) { | |
52 auto r = _mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*)ptr)); | |
53 r = _mm256_shuffle_epi8(r, _mm256_setr_epi8(0,0,0,0, 4,4,4,4, 8,8,8,8, 12,12
,12,12, | |
54 0,0,0,0, 4,4,4,4, 8,8,8,8, 12,12
,12,12)); | |
55 ptr += 8; | |
56 return r; | |
57 } | |
58 static __m128i next4(const uint8_t*& ptr) { | |
59 auto r = _mm_shuffle_epi8(_mm_cvtsi32_si128(*(const uint32_t*)ptr), | |
60 _mm_setr_epi8(0,0,0,0, 1,1,1,1, 2,2,2,2, 3,3,3,3))
; | |
61 ptr += 4; | |
62 return r; | |
63 } | |
64 static __m128i tail(int n, const uint8_t* ptr) { | |
65 uint32_t x = 0; | |
66 switch (n) { | |
67 case 3: x |= (uint32_t)ptr[2] << 16; | |
68 case 2: x |= (uint32_t)ptr[1] << 8; | |
69 case 1: x |= (uint32_t)ptr[0] << 0; | |
70 } | |
71 auto p = (const uint8_t*)&x; | |
72 return next4(p); | |
73 } | |
74 | |
75 // For i = 0...n, tgt = fn(dst,src,cov), where Dst,Src,and Cov can be constants
or arrays. | |
76 template <typename Dst, typename Src, typename Cov, typename Fn> | |
77 static void loop(int n, uint32_t* t, const Dst dst, const Src src, const Cov cov
, Fn&& fn) { | |
78 // We don't want to muck with the callers' pointers, so we make them const a
nd copy here. | |
79 Dst d = dst; | |
80 Src s = src; | |
81 Cov c = cov; | |
82 | |
83 // Writing this as a single while-loop helps hoist loop invariants from fn. | |
84 while (n) { | |
85 if (n >= 8) { | |
86 _mm256_storeu_si256((__m256i*)t, fn(next8(d), next8(s), next8(c))); | |
87 t += 8; | |
88 n -= 8; | |
89 continue; | |
90 } | |
91 if (n >= 4) { | |
92 _mm_storeu_si128((__m128i*)t, fn(next4(d), next4(s), next4(c))); | |
93 t += 4; | |
94 n -= 4; | |
95 } | |
96 if (n) { | |
97 _mm_maskstore_epi32((int*)t, mask(n), fn(tail(n,d), tail(n,s), tail(
n,c))); | |
98 } | |
99 return; | |
100 } | |
101 } | |
102 | |
103 // packed
// | |
104 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~ // | |
105 // unpacked
// | |
106 | |
107 // Everything on the packed side of the squiggly line deals with densely packed
8-bit data, | |
108 // e.g [ BGRA bgra ... ] for pixels or [ CCCC cccc ... ] for coverage. | |
109 // | |
110 // Everything on the unpacked side of the squiggly line deals with unpacked 8-bi
t data, | |
111 // e.g. [ B_G_ R_A_ b_g_ r_a_ ... ] for pixels or [ C_C_ C_C_ c_c_ c_c_ ... ] fo
r coverage, | |
112 // where _ is a zero byte. | |
113 // | |
114 // Adapt<Fn> / adapt(fn) allow the two sides to interoperate, | |
115 // by unpacking arguments, calling fn, then packing the results. | |
116 // | |
117 // This lets us write most of our code in terms of unpacked inputs (considerably
simpler) | |
118 // and all the packing and unpacking is handled automatically. | |
119 | |
120 template <typename Fn> | |
121 struct Adapt { | |
122 Fn fn; | |
123 | |
124 __m256i operator()(__m256i d, __m256i s, __m256i c) { | |
125 auto lo = [](__m256i x) { return _mm256_unpacklo_epi8(x, _mm256_setzero_
si256()); }; | |
126 auto hi = [](__m256i x) { return _mm256_unpackhi_epi8(x, _mm256_setzero_
si256()); }; | |
127 return _mm256_packus_epi16(fn(lo(d), lo(s), lo(c)), | |
128 fn(hi(d), hi(s), hi(c))); | |
129 } | |
130 | |
131 __m128i operator()(__m128i d, __m128i s, __m128i c) { | |
132 auto unpack = [](__m128i x) { return _mm256_cvtepu8_epi16(x); }; | |
133 auto pack = [](__m256i x) { | |
134 auto x01 = x, | |
135 x23 = _mm256_permute4x64_epi64(x, 0xe); // 0b1110 | |
136 return _mm256_castsi256_si128(_mm256_packus_epi16(x01, x23)); | |
137 }; | |
138 return pack(fn(unpack(d), unpack(s), unpack(c))); | |
139 } | |
140 }; | |
141 | |
142 template <typename Fn> | |
143 static Adapt<Fn> adapt(Fn&& fn) { return { fn }; } | |
144 | |
145 // These helpers all work exclusively with unpacked 8-bit values, | |
146 // except div255() which is 16-bit -> unpacked 8-bit, and mul255() which is the
reverse. | |
147 | |
148 // Divide by 255 with rounding. | |
149 // (x+127)/255 == ((x+128)*257)>>16. | |
150 // Sometimes we can be more efficient by breaking this into two parts. | |
151 static __m256i div255_part1(__m256i x) { return _mm256_add_epi16 (x, _mm256_set
1_epi16(128)); } | |
152 static __m256i div255_part2(__m256i x) { return _mm256_mulhi_epu16(x, _mm256_set
1_epi16(257)); } | |
153 static __m256i div255(__m256i x) { return div255_part2(div255_part1(x)); } | |
154 | |
155 // (x*y+127)/255, a byte multiply. | |
156 static __m256i scale(__m256i x, __m256i y) { return div255(_mm256_mullo_epi16(x,
y)); } | |
157 | |
158 // (255 * x). | |
159 static __m256i mul255(__m256i x) { return _mm256_sub_epi16(_mm256_slli_epi16(x,
8), x); } | |
160 | |
161 // (255 - x). | |
162 static __m256i inv(__m256i x) { return _mm256_xor_si256(_mm256_set1_epi16(0x00ff
), x); } | |
163 | |
164 // ARGB argb ... -> AAAA aaaa ... | |
165 static __m256i alphas(__m256i px) { | |
166 const int a = 2 * (SK_A32_SHIFT/8); // SK_A32_SHIFT is typically 24, so thi
s is typically 6. | |
167 const int _ = ~0; | |
168 return _mm256_shuffle_epi8(px, _mm256_setr_epi8(a+0,_,a+0,_,a+0,_,a+0,_, | |
169 a+8,_,a+8,_,a+8,_,a+8,_, | |
170 a+0,_,a+0,_,a+0,_,a+0,_, | |
171 a+8,_,a+8,_,a+8,_,a+8,_)); | |
172 } | |
173 | |
174 | |
175 // SrcOver, with a constant source and full coverage. | |
176 static void blit_row_color32(SkPMColor* tgt, const SkPMColor* dst, int n, SkPMCo
lor src) { | |
177 // We want to calculate s + (d * inv(alphas(s)) + 127)/255. | |
178 // We'd generally do that div255 as s + ((d * inv(alphas(s)) + 128)*257)>>16
. | |
179 | |
180 // But we can go one step further to ((s*255 + 128 + d*inv(alphas(s)))*257)>
>16. | |
181 // This lets us hoist (s*255+128) and inv(alphas(s)) out of the loop. | |
182 auto s = _mm256_cvtepu8_epi16(_mm_set1_epi32(src)), | |
183 s_255_128 = div255_part1(mul255(s)), | |
184 A = inv(alphas(s)); | |
185 | |
186 const uint8_t cov = 0xff; | |
187 loop(n, tgt, dst, src, cov, adapt([=](__m256i d, __m256i, __m256i) { | |
188 return div255_part2(_mm256_add_epi16(s_255_128, _mm256_mullo_epi16(d, A)
)); | |
189 })); | |
190 } | |
191 | |
192 // SrcOver, with a constant source and variable coverage. | |
193 // If the source is opaque, SrcOver becomes Src. | |
194 static void blit_mask_d32_a8(SkPMColor* dst, size_t dstRB, | |
195 const SkAlpha* cov, size_t covRB, | |
196 SkColor color, int w, int h) { | |
197 if (SkColorGetA(color) == 0xFF) { | |
198 const SkPMColor src = SkSwizzle_BGRA_to_PMColor(color); | |
199 while (h --> 0) { | |
200 loop(w, dst, (const SkPMColor*)dst, src, cov, | |
201 adapt([](__m256i d, __m256i s, __m256i c) { | |
202 // Src blend mode: a simple lerp from d to s by c. | |
203 // TODO: try a pmaddubsw version? | |
204 return div255(_mm256_add_epi16(_mm256_mullo_epi16(inv(c),d), | |
205 _mm256_mullo_epi16( c ,s))); | |
206 })); | |
207 dst += dstRB / sizeof(*dst); | |
208 cov += covRB / sizeof(*cov); | |
209 } | |
210 } else { | |
211 const SkPMColor src = SkPreMultiplyColor(color); | |
212 while (h --> 0) { | |
213 loop(w, dst, (const SkPMColor*)dst, src, cov, | |
214 adapt([](__m256i d, __m256i s, __m256i c) { | |
215 // SrcOver blend mode, with coverage folded into source alpha. | |
216 auto sc = scale(s,c), | |
217 AC = inv(alphas(sc)); | |
218 return _mm256_add_epi16(sc, scale(d,AC)); | |
219 })); | |
220 dst += dstRB / sizeof(*dst); | |
221 cov += covRB / sizeof(*cov); | |
222 } | |
223 } | |
224 } | |
225 | |
226 } // namespace sk_avx2 | |
227 | |
228 #endif | |
229 | |
230 namespace SkOpts { | |
231 void Init_avx2() { | |
232 #ifndef SK_SUPPORT_LEGACY_X86_BLITS | |
233 blit_row_color32 = sk_avx2::blit_row_color32; | |
234 blit_mask_d32_a8 = sk_avx2::blit_mask_d32_a8; | |
235 #endif | |
236 } | |
237 } | |
OLD | NEW |