-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics_sse2.cpp
284 lines (234 loc) · 8.92 KB
/
graphics_sse2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* -*- C++ -*-
*
* graphics_sse2.cpp - graphics routines using X86 SSE2 cpu functionality
*
* Copyright (c) 2009-2011 "Uncle" Mion Sonozaki
*
* UncleMion@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
* or write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Based upon routines provided by Roto
#ifdef USE_X86_GFX
#include <emmintrin.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "graphics_sum.h"
#include "graphics_blend.h"
namespace ons_gfx {
int imageFilterMean_SSE2(unsigned char *src1, unsigned char *src2, unsigned char *dst, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
MEAN_PIXEL();
--n; ++dst; ++src1; ++src2;
}
// Do bulk of processing using SSE2 (find the mean of 16 8-bit unsigned integers, with saturation)
__m128i mask = _mm_set1_epi8(0x7F);
while(n >= 16) {
__m128i s1 = _mm_loadu_si128((__m128i*)src1);
s1 = _mm_srli_epi16(s1, 1); // shift right 1
s1 = _mm_and_si128(s1, mask); // apply byte-mask
__m128i s2 = _mm_loadu_si128((__m128i*)src2);
s2 = _mm_srli_epi16(s2, 1); // shift right 1
s2 = _mm_and_si128(s2, mask); // apply byte-mask
__m128i r = _mm_adds_epu8(s1, s2);
_mm_store_si128((__m128i*)dst, r);
n -= 16; src1 += 16; src2 += 16; dst += 16;
}
// If any bytes are left over, deal with them individually
++n;
BASIC_MEAN();
return length - n;
}
int imageFilterAddTo_SSE2(unsigned char *dst, unsigned char *src, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
ADDTO_PIXEL();
--n; ++dst; ++src;
}
// Do bulk of processing using SSE2 (add 16 8-bit unsigned integers, with saturation)
while(n >= 16) {
__m128i s = _mm_loadu_si128((__m128i*)src);
__m128i d = _mm_load_si128((__m128i*)dst);
__m128i r = _mm_adds_epu8(s, d);
_mm_store_si128((__m128i*)dst, r);
n -= 16; src += 16; dst += 16;
}
// If any bytes are left over, deal with them individually
++n;
BASIC_ADDTO();
return length - n;
}
void imageFilterSubFrom_SSE2(unsigned char *dst, unsigned char *src, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
SUBFROM_PIXEL();
--n; ++dst; ++src;
}
// Do bulk of processing using SSE2 (sub 16 8-bit unsigned integers, with saturation)
while(n >= 16) {
__m128i s = _mm_loadu_si128((__m128i*)src);
__m128i d = _mm_load_si128((__m128i*)dst);
__m128i r = _mm_subs_epu8(d, s);
_mm_store_si128((__m128i*)dst, r);
n -= 16; src += 16; dst += 16;
}
// If any bytes are left over, deal with them individually
++n;
BASIC_SUBFROM();
}
// basic bitmasks 0x00FF00FF, 0x000000FF
static const __m128i rbmask = _mm_set1_epi32(0x00FF00FF);
static const __m128i bmask = _mm_srli_epi32(rbmask, 16);
static inline __m128i alphaBlendCore_SSE2(__m128i src1, __m128i src2, __m128i d_a)
{
// rb = (src2_argb & rbmask) * alpha1
__m128i rb = _mm_and_si128(src2, rbmask);
rb = _mm_mullo_epi16(d_a, rb);
// g = ((src2_argb >> 8) & bmask) * alpha1
src2 = _mm_srli_epi32(src2, 8);
__m128i g = _mm_and_si128(src2, bmask);
g = _mm_mullo_epi16(d_a, g);
// alpha2 = alpha1 ^ rbmask
d_a = _mm_xor_si128(d_a, rbmask);
// rb += (src1_argb & rbmask) * alpha2
__m128i tmp = _mm_and_si128(src1, rbmask);
tmp = _mm_mullo_epi16(d_a, tmp);
rb = _mm_add_epi32(rb, tmp);
// rb = (rb >> 8) & rbmask
rb = _mm_srli_epi32(rb, 8);
rb = _mm_and_si128(rb, rbmask);
// g += ((src1_argb >> 8) & bmask) * alpha2
src1 = _mm_srli_epi32(src1, 8);
tmp = _mm_and_si128(src1, bmask);
tmp = _mm_mullo_epi16(d_a, tmp);
g = _mm_add_epi32(g, tmp);
// g = g & (bmask << 8)
tmp =_mm_slli_epi32(bmask, 8);
g = _mm_and_si128(g, tmp);
// dst_argb = rb | g
return _mm_or_si128(rb, g);
}
int imageFilterBlend_SSE2(Uint32 *dst_buffer, Uint32 *src_buffer, Uint8 *alphap, int alpha, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst_buffer
while( (((long)dst_buffer & 0xF) > 0) && (n > 0) ) {
BLEND_PIXEL();
--n; ++dst_buffer; ++src_buffer;
}
// Do bulk of processing using SSE2 (process 4 32bit (BGRA) pixels)
while(n >= 4) {
// alpha1 = ((src_argb >> 24) * alpha) >> 8
__m128i a = _mm_set1_epi32(alpha);
__m128i buf = _mm_loadu_si128((__m128i*)src_buffer);
__m128i tmp = _mm_srli_epi32(buf, 24);
a = _mm_mullo_epi16(a, tmp);
a = _mm_srli_epi32(a, 8);
// double-up alpha1 (0x000000vv -> 0x00vv00vv)
tmp = _mm_slli_epi32(a, 16);
a = _mm_or_si128(a, tmp);
tmp = _mm_load_si128((__m128i*)dst_buffer);
__m128i dst = alphaBlendCore_SSE2(tmp, buf, a);
_mm_store_si128((__m128i*)dst_buffer, dst);
n -= 4; src_buffer += 4; dst_buffer += 4; alphap += 16;
}
// If any pixels are left over, deal with them individually
++n;
BASIC_BLEND();
return length - n;
}
int imageFilterEffectBlend_SSE2(Uint32 *dst_buffer, Uint32 *src1_buffer, Uint32 *src2_buffer, Uint32 mask2, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst_buffer
while( (((long)dst_buffer & 0xF) > 0) && (n > 0) ) {
BLEND_EFFECT_PIXEL();
--n; ++dst_buffer; ++src1_buffer; ++src2_buffer;
}
// Do bulk of processing using SSE2 (process 4 32bit (BGRA) pixels)
// load alpha1, double-up (0x000000vv -> 0x00vv00vv)
__m128i a = _mm_set1_epi32(mask2);
__m128i tmp = _mm_slli_epi32(a, 16);
a = _mm_or_si128(a, tmp);
while(n >= 4) {
tmp = _mm_loadu_si128((__m128i*)src1_buffer);
__m128i buf = _mm_loadu_si128((__m128i*)src2_buffer);
__m128i dst = alphaBlendCore_SSE2(tmp, buf, a);
_mm_store_si128((__m128i*)dst_buffer, dst);
n -= 4; dst_buffer += 4; src1_buffer += 4; src2_buffer += 4;
}
// If any pixels are left over, deal with them individually
++n;
while(--n > 0) {
BLEND_EFFECT_PIXEL();
++dst_buffer, ++src1_buffer, ++src2_buffer;
}
return length - n;
}
int imageFilterEffectMaskBlend_SSE2(Uint32 *dst_buffer, Uint32 *src1_buffer, Uint32 *src2_buffer, Uint32 *mask_buffer, Uint32 overflow_mask, Uint32 mask_value, int length)
{
int n = length;
// Compute first few values so we're on a 16-byte boundary in dst_buffer
while( (((long)dst_buffer & 0xF) > 0) && (n > 0) ) {
BLEND_EFFECT_MASK_PIXEL();
--n; ++dst_buffer; ++src1_buffer; ++src2_buffer; ++mask_buffer;
}
// Do bulk of processing using SSE2 (process 4 32bit (BGRA) pixels)
__m128i over = bmask;
if (overflow_mask == 0xFFFFFFFF)
over = _mm_xor_si128(bmask, bmask);
while(n >= 4) {
//if (mask_value > (mask & BMASK))
// alpha1 = subs(mask_value,(mask & BMASK)
// if (alpha1 > over) alpha1 = BMASK
//else alpha1 = 0
__m128i a = _mm_set1_epi32(mask_value);
__m128i buf = _mm_loadu_si128((__m128i*)mask_buffer);
buf = _mm_and_si128(buf, bmask);
__m128i tmp = _mm_cmpgt_epi32(a, buf);
tmp = _mm_and_si128(tmp, bmask);
a = _mm_subs_epu16(a, buf);
buf = _mm_cmpgt_epi32(a, over);
a = _mm_or_si128(a, buf);
a = _mm_and_si128(a, tmp);
// double-up alpha1 (0x000000vv -> 0x00vv00vv)
tmp = _mm_slli_epi32(a, 16);
a = _mm_or_si128(a, tmp);
tmp = _mm_loadu_si128((__m128i*)src1_buffer);
buf = _mm_loadu_si128((__m128i*)src2_buffer);
__m128i dst = alphaBlendCore_SSE2(tmp, buf, a);
_mm_store_si128((__m128i*)dst_buffer, dst);
n -= 4; dst_buffer += 4; src1_buffer += 4; src2_buffer += 4; mask_buffer += 4;
}
// If any pixels are left over, deal with them individually
++n;
while(--n > 0) {
BLEND_EFFECT_MASK_PIXEL();
++dst_buffer, ++src1_buffer, ++src2_buffer; ++mask_buffer;
}
return length - n;
}
}//namespace ons_gfx
#endif //USE_X86_GFX