10 #ifndef BLITTER_32BPP_SSE_FUNC_HPP
11 #define BLITTER_32BPP_SSE_FUNC_HPP
15 GNU_TARGET(SSE_TARGET)
16 inline void InsertFirstUint32(
const uint32_t value, __m128i &into)
18 #if (SSE_VERSION >= 4)
19 into = _mm_insert_epi32(into, value, 0);
21 into = _mm_insert_epi16(into, value, 0);
22 into = _mm_insert_epi16(into, value >> 16, 1);
26 GNU_TARGET(SSE_TARGET)
27 inline void InsertSecondUint32(
const uint32_t value, __m128i &into)
29 #if (SSE_VERSION >= 4)
30 into = _mm_insert_epi32(into, value, 1);
32 into = _mm_insert_epi16(into, value, 2);
33 into = _mm_insert_epi16(into, value >> 16, 3);
37 GNU_TARGET(SSE_TARGET)
38 inline void LoadUint64(
const uint64_t value, __m128i &into)
40 #ifdef POINTER_IS_64BIT
41 into = _mm_cvtsi64_si128(value);
43 #if (SSE_VERSION >= 4)
44 into = _mm_cvtsi32_si128(value);
45 InsertSecondUint32(value >> 32, into);
47 (*(um128i*) &into).m128i_u64[0] = value;
52 GNU_TARGET(SSE_TARGET)
53 inline __m128i PackUnsaturated(__m128i from,
const __m128i &mask)
55 #if (SSE_VERSION == 2)
56 from = _mm_and_si128(from, mask);
57 return _mm_packus_epi16(from, from);
59 return _mm_shuffle_epi8(from, mask);
63 GNU_TARGET(SSE_TARGET)
64 inline __m128i DistributeAlpha(
const __m128i from,
const __m128i &mask)
66 #if (SSE_VERSION == 2)
67 __m128i alphaAB = _mm_shufflelo_epi16(from, 0x3F);
68 alphaAB = _mm_shufflehi_epi16(alphaAB, 0x3F);
69 return _mm_andnot_si128(mask, alphaAB);
71 return _mm_shuffle_epi8(from, mask);
75 GNU_TARGET(SSE_TARGET)
76 inline __m128i AlphaBlendTwoPixels(__m128i src, __m128i dst,
const __m128i &distribution_mask,
const __m128i &pack_mask,
const __m128i &alpha_mask)
78 __m128i srcAB = _mm_unpacklo_epi8(src, _mm_setzero_si128());
79 __m128i dstAB = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
81 __m128i alphaMaskAB = _mm_cmpgt_epi16(srcAB, _mm_setzero_si128());
82 __m128i alphaAB = _mm_sub_epi16(srcAB, alphaMaskAB);
83 alphaAB = DistributeAlpha(alphaAB, distribution_mask);
85 srcAB = _mm_sub_epi16(srcAB, dstAB);
86 srcAB = _mm_mullo_epi16(srcAB, alphaAB);
87 srcAB = _mm_srli_epi16(srcAB, 8);
88 srcAB = _mm_add_epi16(srcAB, dstAB);
90 alphaMaskAB = _mm_and_si128(alphaMaskAB, alpha_mask);
91 srcAB = _mm_or_si128(srcAB, alphaMaskAB);
93 return PackUnsaturated(srcAB, pack_mask);
99 GNU_TARGET(SSE_TARGET)
100 inline __m128i DarkenTwoPixels(__m128i src, __m128i dst,
const __m128i &distribution_mask,
const __m128i &tr_nom_base)
102 __m128i srcAB = _mm_unpacklo_epi8(src, _mm_setzero_si128());
103 __m128i dstAB = _mm_unpacklo_epi8(dst, _mm_setzero_si128());
104 __m128i alphaAB = DistributeAlpha(srcAB, distribution_mask);
105 alphaAB = _mm_srli_epi16(alphaAB, 2);
106 __m128i nom = _mm_sub_epi16(tr_nom_base, alphaAB);
107 dstAB = _mm_mullo_epi16(dstAB, nom);
108 dstAB = _mm_srli_epi16(dstAB, 8);
109 return _mm_packus_epi16(dstAB, dstAB);
112 IGNORE_UNINITIALIZED_WARNING_START
113 GNU_TARGET(SSE_TARGET)
114 static Colour ReallyAdjustBrightness(
Colour colour, uint8_t brightness)
116 uint64_t c16 = colour.b | (uint64_t) colour.g << 16 | (uint64_t) colour.r << 32;
118 uint64_t c16_ob = c16;
119 c16 /= Blitter_32bppBase::DEFAULT_BRIGHTNESS;
120 c16 &= 0x01FF01FF01FFULL;
123 c16_ob = (((c16_ob >> (8 + 7)) & 0x0100010001ULL) * 0xFF) & c16;
124 const uint ob = ((uint16_t) c16_ob + (uint16_t) (c16_ob >> 16) + (uint16_t) (c16_ob >> 32)) / 2;
126 const uint32_t alpha32 = colour.
data & 0xFF000000;
128 LoadUint64(c16, ret);
130 __m128i ob128 = _mm_cvtsi32_si128(ob);
131 ob128 = _mm_shufflelo_epi16(ob128, 0xC0);
132 __m128i white = OVERBRIGHT_VALUE_MASK;
134 ret = _mm_subs_epu16(white, c128);
135 ret = _mm_mullo_epi16(ret, ob128);
136 ret = _mm_srli_epi16(ret, 8);
137 ret = _mm_add_epi16(ret, c128);
140 ret = _mm_packus_epi16(ret, ret);
141 return alpha32 | _mm_cvtsi128_si32(ret);
143 IGNORE_UNINITIALIZED_WARNING_STOP
148 inline Colour AdjustBrightneSSE(
Colour colour, uint8_t brightness)
151 if (brightness == Blitter_32bppBase::DEFAULT_BRIGHTNESS)
return colour;
153 return ReallyAdjustBrightness(colour, brightness);
156 GNU_TARGET(SSE_TARGET)
157 inline __m128i AdjustBrightnessOfTwoPixels([[maybe_unused]] __m128i from, [[maybe_unused]] uint32_t brightness)
159 #if (SSE_VERSION < 3)
166 brightness &= 0xFF00FF00;
167 brightness += Blitter_32bppBase::DEFAULT_BRIGHTNESS;
169 __m128i colAB = _mm_unpacklo_epi8(from, _mm_setzero_si128());
170 __m128i briAB = _mm_cvtsi32_si128(brightness);
171 briAB = _mm_shuffle_epi8(briAB, BRIGHTNESS_LOW_CONTROL_MASK);
172 colAB = _mm_mullo_epi16(colAB, briAB);
173 __m128i colAB_ob = _mm_srli_epi16(colAB, 8 + 7);
174 colAB = _mm_srli_epi16(colAB, 7);
180 colAB = _mm_and_si128(colAB, BRIGHTNESS_DIV_CLEANER);
181 colAB_ob = _mm_and_si128(colAB_ob, OVERBRIGHT_PRESENCE_MASK);
182 colAB_ob = _mm_mullo_epi16(colAB_ob, OVERBRIGHT_VALUE_MASK);
183 colAB_ob = _mm_and_si128(colAB_ob, colAB);
184 __m128i obAB = _mm_hadd_epi16(_mm_hadd_epi16(colAB_ob, _mm_setzero_si128()), _mm_setzero_si128());
186 obAB = _mm_srli_epi16(obAB, 1);
187 obAB = _mm_shuffle_epi8(obAB, OVERBRIGHT_CONTROL_MASK);
188 __m128i retAB = OVERBRIGHT_VALUE_MASK;
189 retAB = _mm_subs_epu16(retAB, colAB);
190 retAB = _mm_mullo_epi16(retAB, obAB);
191 retAB = _mm_srli_epi16(retAB, 8);
192 retAB = _mm_add_epi16(retAB, colAB);
194 return _mm_packus_epi16(retAB, retAB);
198 #if FULL_ANIMATION == 0
206 IGNORE_UNINITIALIZED_WARNING_START
207 template <BlitterMode mode, Blitter_32bppSSE2::ReadMode read_mode, Blitter_32bppSSE2::BlockType bt_last,
bool translucent>
208 GNU_TARGET(SSE_TARGET)
209 #if (SSE_VERSION == 2)
211 #elif (SSE_VERSION == 3)
213 #elif (SSE_VERSION == 4)
217 const byte *
const remap = bp->
remap;
219 int effective_width = bp->
width;
222 const SpriteData *
const sd = (
const SpriteData *) bp->
sprite;
223 const SpriteInfo *
const si = &sd->infos[zoom];
224 const MapValue *src_mv_line = (
const MapValue *) &sd->data[si->mv_offset] + bp->
skip_top * si->sprite_width;
225 const Colour *src_rgba_line = (
const Colour *) ((
const byte *) &sd->data[si->sprite_offset] + bp->
skip_top * si->sprite_line_size);
227 if (read_mode != RM_WITH_MARGIN) {
231 const MapValue *src_mv = src_mv_line;
234 const __m128i alpha_and = ALPHA_AND_MASK;
235 #define ALPHA_BLEND_PARAM_3 alpha_and
236 #if (SSE_VERSION == 2)
237 const __m128i clear_hi = CLEAR_HIGH_BYTE_MASK;
238 #define ALPHA_BLEND_PARAM_1 alpha_and
239 #define ALPHA_BLEND_PARAM_2 clear_hi
240 #define DARKEN_PARAM_1 tr_nom_base
241 #define DARKEN_PARAM_2 tr_nom_base
243 const __m128i a_cm = ALPHA_CONTROL_MASK;
244 const __m128i pack_low_cm = PACK_LOW_CONTROL_MASK;
245 #define ALPHA_BLEND_PARAM_1 a_cm
246 #define ALPHA_BLEND_PARAM_2 pack_low_cm
247 #define DARKEN_PARAM_1 a_cm
248 #define DARKEN_PARAM_2 tr_nom_base
250 const __m128i tr_nom_base = TRANSPARENT_NOM_BASE;
252 for (
int y = bp->
height; y != 0; y--) {
254 const Colour *src = src_rgba_line + META_LENGTH;
257 if (read_mode == RM_WITH_MARGIN) {
258 assert(bt_last == BT_NONE);
259 src += src_rgba_line[0].
data;
260 dst += src_rgba_line[0].
data;
262 const int width_diff = si->sprite_width - bp->
width;
263 effective_width = bp->
width - (int) src_rgba_line[0].data;
264 const int delta_diff = (int) src_rgba_line[1].data - width_diff;
265 const int new_width = effective_width - delta_diff;
266 effective_width = delta_diff > 0 ? new_width : effective_width;
267 if (effective_width <= 0)
goto next_line;
273 for (uint x = (uint) effective_width; x > 0; x--) {
274 if (src->
a) *dst = *src;
281 for (uint x = (uint) effective_width / 2; x > 0; x--) {
282 __m128i srcABCD = _mm_loadl_epi64((
const __m128i*) src);
283 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
284 _mm_storel_epi64((__m128i*) dst, AlphaBlendTwoPixels(srcABCD, dstABCD, ALPHA_BLEND_PARAM_1, ALPHA_BLEND_PARAM_2, ALPHA_BLEND_PARAM_3));
289 if ((bt_last == BT_NONE && effective_width & 1) || bt_last == BT_ODD) {
290 __m128i srcABCD = _mm_cvtsi32_si128(src->
data);
291 __m128i dstABCD = _mm_cvtsi32_si128(dst->
data);
292 dst->
data = _mm_cvtsi128_si32(AlphaBlendTwoPixels(srcABCD, dstABCD, ALPHA_BLEND_PARAM_1, ALPHA_BLEND_PARAM_2, ALPHA_BLEND_PARAM_3));
297 #if (SSE_VERSION >= 3)
298 for (uint x = (uint) effective_width / 2; x > 0; x--) {
299 __m128i srcABCD = _mm_loadl_epi64((
const __m128i*) src);
300 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
301 uint32_t mvX2 = *((uint32_t *)
const_cast<MapValue *
>(src_mv));
304 if (mvX2 & 0x00FF00FF) {
305 #define CMOV_REMAP(m_colour, m_colour_init, m_src, m_m) \
307 Colour m_colour = m_colour_init; \
309 const Colour srcm = (Colour) (m_src); \
310 const uint m = (byte) (m_m); \
311 const uint r = remap[m]; \
312 const Colour cmap = (this->LookupColourInPalette(r).data & 0x00FFFFFF) | (srcm.data & 0xFF000000); \
313 m_colour = r == 0 ? m_colour : cmap; \
314 m_colour = m != 0 ? m_colour : srcm; \
316 #ifdef POINTER_IS_64BIT
317 uint64_t srcs = _mm_cvtsi128_si64(srcABCD);
318 uint64_t remapped_src = 0;
319 CMOV_REMAP(c0, 0, srcs, mvX2);
320 remapped_src = c0.data;
321 CMOV_REMAP(c1, 0, srcs >> 32, mvX2 >> 16);
322 remapped_src |= (uint64_t) c1.data << 32;
323 srcABCD = _mm_cvtsi64_si128(remapped_src);
326 CMOV_REMAP(c0, 0, _mm_cvtsi128_si32(srcABCD), mvX2);
327 remapped_src[0] = c0.
data;
328 CMOV_REMAP(c1, 0, src[1], mvX2 >> 16);
329 remapped_src[1] = c1.
data;
330 srcABCD = _mm_loadl_epi64((__m128i*) &remapped_src);
333 if ((mvX2 & 0xFF00FF00) != 0x80008000) srcABCD = AdjustBrightnessOfTwoPixels(srcABCD, mvX2);
337 _mm_storel_epi64((__m128i *) dst, AlphaBlendTwoPixels(srcABCD, dstABCD, ALPHA_BLEND_PARAM_1, ALPHA_BLEND_PARAM_2, ALPHA_BLEND_PARAM_3));
343 if ((bt_last == BT_NONE && effective_width & 1) || bt_last == BT_ODD) {
345 for (uint x = (uint) effective_width; x > 0; x--) {
350 const uint r = remap[src_mv->m];
352 Colour remapped_colour = AdjustBrightneSSE(this->LookupColourInPalette(r), src_mv->v);
354 *dst = remapped_colour;
356 remapped_colour.
a = src->
a;
357 srcABCD = _mm_cvtsi32_si128(remapped_colour.
data);
358 goto bmcr_alpha_blend_single;
362 srcABCD = _mm_cvtsi32_si128(src->
data);
364 bmcr_alpha_blend_single:
365 __m128i dstABCD = _mm_cvtsi32_si128(dst->
data);
366 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, ALPHA_BLEND_PARAM_1, ALPHA_BLEND_PARAM_2, ALPHA_BLEND_PARAM_3);
368 dst->
data = _mm_cvtsi128_si32(srcABCD);
370 #if (SSE_VERSION == 2)
380 for (uint x = (uint) bp->
width / 2; x > 0; x--) {
381 __m128i srcABCD = _mm_loadl_epi64((
const __m128i*) src);
382 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
383 _mm_storel_epi64((__m128i *) dst, DarkenTwoPixels(srcABCD, dstABCD, DARKEN_PARAM_1, DARKEN_PARAM_2));
388 if ((bt_last == BT_NONE && bp->
width & 1) || bt_last == BT_ODD) {
389 __m128i srcABCD = _mm_cvtsi32_si128(src->
data);
390 __m128i dstABCD = _mm_cvtsi32_si128(dst->
data);
391 dst->
data = _mm_cvtsi128_si32(DarkenTwoPixels(srcABCD, dstABCD, DARKEN_PARAM_1, DARKEN_PARAM_2));
397 for (uint x = (uint) bp->
width; x > 0; x--) {
408 for (uint x = (uint) bp->
width; x > 0; x--) {
409 if (src_mv->m == 0) {
411 uint8_t g = MakeDark(src->r, src->g, src->b);
412 *dst = ComposeColourRGBA(g, g, g, src->
a, *dst);
415 uint r = remap[src_mv->m];
416 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), src_mv->v), src->
a, *dst);
425 for (uint x = (uint) bp->
width; x > 0; x--) {
438 src_rgba_line = (
const Colour*) ((
const byte*) src_rgba_line + si->sprite_line_size);
439 dst_line += bp->
pitch;
442 IGNORE_UNINITIALIZED_WARNING_STOP
451 #if (SSE_VERSION == 2)
453 #elif (SSE_VERSION == 3)
455 #elif (SSE_VERSION == 4)
463 const BlockType bt_last = (BlockType) (bp->
width & 1);
465 default: Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN, true>(bp, zoom);
return;
466 case BT_ODD: Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD, true>(bp, zoom);
return;
469 if (((
const Blitter_32bppSSE_Base::SpriteData *) bp->
sprite)->flags & SF_TRANSLUCENT) {
470 Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true>(bp, zoom);
472 Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, false>(bp, zoom);
479 if (((
const Blitter_32bppSSE_Base::SpriteData *) bp->
sprite)->flags & SF_NO_REMAP)
goto bm_normal;
481 Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true>(bp, zoom);
return;
483 Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true>(bp, zoom);
return;
485 case BM_TRANSPARENT: Draw<BM_TRANSPARENT, RM_NONE, BT_NONE, true>(bp, zoom);
return;
486 case BM_TRANSPARENT_REMAP: Draw<BM_TRANSPARENT_REMAP, RM_NONE, BT_NONE, true>(bp, zoom);
return;
487 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, RM_NONE, BT_NONE, true>(bp, zoom);
return;
488 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, RM_NONE, BT_NONE, true>(bp, zoom);
return;