Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / blitter / 32bpp_ssse3.hpp
blob55fa87ec81f81e8cfc98f5cd4c9a798ee7d88995
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD 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, version 2.
6 * OpenTTD 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.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file 32bpp_ssse3.hpp SSSE3 32 bpp blitter. */
12 #ifndef BLITTER_32BPP_SSSE3_HPP
13 #define BLITTER_32BPP_SSSE3_HPP
15 #ifdef WITH_SSE
17 #include "32bpp_sse2.hpp"
18 #include "tmmintrin.h"
20 /* Alpha blend 2 pixels. */
21 #undef ALPHA_BLEND_2
22 #define ALPHA_BLEND_2(m_pack_mask) { \
23 __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128()); /* PUNPCKLBW, expand each uint8 into uint16 */ \
24 __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128()); \
26 __m128i alphaAB = _mm_cmpgt_epi16(srcAB, _mm_setzero_si128()); /* PCMPGTW, if (alpha > 0) a++; */ \
27 alphaAB = _mm_srli_epi16(alphaAB, 15); \
28 alphaAB = _mm_add_epi16(alphaAB, srcAB); \
29 alphaAB = _mm_shuffle_epi8(alphaAB, a_cm); /* PSHUFB, put alpha in front of each rgb */ \
31 srcAB = _mm_sub_epi16(srcAB, dstAB); /* PSUBW, (r - Cr) */ \
32 srcAB = _mm_mullo_epi16(srcAB, alphaAB); /* PMULLW, a*(r - Cr) */ \
33 srcAB = _mm_srli_epi16(srcAB, 8); /* PSRLW, a*(r - Cr)/256 */ \
34 srcAB = _mm_add_epi16(srcAB, dstAB); /* PADDW, a*(r - Cr)/256 + Cr */ \
35 srcABCD = _mm_shuffle_epi8(srcAB, m_pack_mask); /* PSHUFB, pack 2 Colour (without saturation) */ \
38 /* Adjust brightness of 2 pixels. */
39 #define ADJUST_BRIGHTNESS_2(colourX2, brightnessX2) \
40 /* The following dataflow differs from the one of AdjustBrightness() only for alpha.
41 * In order to keep alpha in colAB, insert a 1 in a unused brightness byte (a*1->a).
42 * OK, not a 1 but DEFAULT_BRIGHTNESS to compensate the div.
43 */ \
44 brightnessX2 &= 0xFF00FF00; \
45 brightnessX2 += DEFAULT_BRIGHTNESS; \
47 __m128i zero = _mm_setzero_si128(); \
48 __m128i colAB = _mm_unpacklo_epi8(colourX2, zero); \
50 __m128i briAB = _mm_cvtsi32_si128(brightnessX2); \
51 briAB = _mm_shuffle_epi8(briAB, briAB_cm); /* DEFAULT_BRIGHTNESS in 0, 0x00 in 2. */ \
52 colAB = _mm_mullo_epi16(colAB, briAB); \
53 __m128i colAB_ob = _mm_srli_epi16(colAB, 8+7); \
54 colAB = _mm_srli_epi16(colAB, 7); \
56 /* Sum overbright.
57 * Maximum for each rgb is 508 => 9 bits. The highest bit tells if there is overbright.
58 * -255 is changed in -256 so we just have to take the 8 lower bits into account.
59 */ \
60 colAB = _mm_and_si128(colAB, div_cleaner); \
61 colAB_ob = _mm_and_si128(colAB_ob, ob_check); \
62 colAB_ob = _mm_mullo_epi16(colAB_ob, ob_mask); \
63 colAB_ob = _mm_and_si128(colAB_ob, colAB); \
64 __m128i obAB = _mm_hadd_epi16(_mm_hadd_epi16(colAB_ob, zero), zero); \
66 obAB = _mm_srli_epi16(obAB, 1); /* Reduce overbright strength. */ \
67 obAB = _mm_shuffle_epi8(obAB, ob_cm); \
68 __m128i retAB = ob_mask; /* ob_mask is equal to white. */ \
69 retAB = _mm_subs_epu16(retAB, colAB); /* (255 - rgb) */ \
70 retAB = _mm_mullo_epi16(retAB, obAB); /* ob*(255 - rgb) */ \
71 retAB = _mm_srli_epi16(retAB, 8); /* ob*(255 - rgb)/256 */ \
72 retAB = _mm_add_epi16(retAB, colAB); /* ob*(255 - rgb)/256 + rgb */ \
74 colourX2 = _mm_packus_epi16(retAB, retAB);
76 /** The SSSE3 32 bpp blitter (without palette animation). */
77 class Blitter_32bppSSSE3 : public Blitter_32bppSSE2 {
78 public:
79 /* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom);
80 template <BlitterMode mode, ReadMode read_mode, BlockType bt_last>
81 void Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom);
82 /* virtual */ const char *GetName() { return "32bpp-ssse3"; }
85 /** Factory for the SSSE3 32 bpp blitter (without palette animation). */
86 class FBlitter_32bppSSSE3: public BlitterFactory {
87 public:
88 FBlitter_32bppSSSE3() : BlitterFactory("32bpp-ssse3", "32bpp SSSE3 Blitter (no palette animation)", HasCPUIDFlag(1, 2, 9)) {}
89 /* virtual */ Blitter *CreateInstance() { return new Blitter_32bppSSSE3(); }
92 #endif /* WITH_SSE */
93 #endif /* BLITTER_32BPP_SSSE3_HPP */