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/>.
10 /** @file 32bpp_sse2.hpp SSE2 32 bpp blitter. */
12 #ifndef BLITTER_32BPP_SSE2_HPP
13 #define BLITTER_32BPP_SSE2_HPP
17 #include "32bpp_simple.hpp"
18 #include "emmintrin.h"
20 #define META_LENGTH 2 ///< Number of uint32 inserted before each line of pixels in a sprite.
21 #define MARGIN_NORMAL_THRESHOLD (zoom == ZOOM_LVL_OUT_32X ? 8 : 4) ///< Minimum width to use margins with BM_NORMAL.
22 #define MARGIN_REMAP_THRESHOLD 4 ///< Minimum width to use margins with BM_COLOUR_REMAP.
25 #define ALIGN(n) __declspec(align(n))
27 #define ALIGN(n) __attribute__ ((aligned (n)))
30 typedef union ALIGN(16) um128i
{
38 #define CLEAR_HIGH_BYTE_MASK _mm_setr_epi8(-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0)
39 #define ALPHA_CONTROL_MASK _mm_setr_epi8( 6, 7, 6, 7, 6, 7, -1, -1, 14, 15, 14, 15, 14, 15, -1, -1)
40 #define PACK_LOW_CONTROL_MASK _mm_setr_epi8( 0, 2, 4, -1, 8, 10, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1)
41 #define PACK_HIGH_CONTROL_MASK _mm_setr_epi8(-1, -1, -1, -1, -1, -1, -1, -1, 0, 2, 4, -1, 8, 10, 12, -1)
42 #define BRIGHTNESS_LOW_CONTROL_MASK _mm_setr_epi8( 1, 2, 1, 2, 1, 2, 0, 2, 3, 2, 3, 2, 3, 2, 0, 2)
43 #define BRIGHTNESS_DIV_CLEANER _mm_setr_epi8(-1, 1, -1, 1, -1, 1, -1, 0, -1, 1, -1, 1, -1, 1, -1, 0)
44 #define OVERBRIGHT_PRESENCE_MASK _mm_setr_epi8( 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0)
45 #define OVERBRIGHT_VALUE_MASK _mm_setr_epi8(-1, 0, -1, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, 0, 0)
46 #define OVERBRIGHT_CONTROL_MASK _mm_setr_epi8( 0, 1, 0, 1, 0, 1, 7, 7, 2, 3, 2, 3, 2, 3, 7, 7)
47 #define TRANSPARENT_NOM_BASE _mm_setr_epi16(256, 256, 256, 256, 256, 256, 256, 256)
49 #define EXTR32(m_from, m_rank) (*(um128i*) &m_from).m128i_u32[m_rank]
50 #define EXTR64(m_from, m_rank) (*(um128i*) &m_from).m128i_u64[m_rank]
51 #define INSR32(m_val, m_into, m_rank) { \
52 (*(um128i*) &m_into).m128i = _mm_insert_epi16((*(um128i*) &m_into).m128i, m_val, (m_rank)*2); \
53 (*(um128i*) &m_into).m128i = _mm_insert_epi16((*(um128i*) &m_into).m128i, (m_val) >> 16, (m_rank)*2 + 1); \
55 #define INSR64(m_val, m_into, m_rank) (*(um128i*) &m_into).m128i_u64[m_rank] = (m_val)
58 #define LOAD64(m_val, m_into) m_into = _mm_cvtsi64_si128(m_val);
60 #define LOAD64(m_val, m_into) INSR64(m_val, m_into, 0)
63 /* PUT_ALPHA_IN_FRONT_OF_RGB is redefined in 32bpp_ssse3.hpp. */
64 #define PUT_ALPHA_IN_FRONT_OF_RGB(m_from, m_into) \
65 m_into = _mm_shufflelo_epi16(m_from, 0x3F); /* PSHUFLW, put alpha1 in front of each rgb1 */ \
66 m_into = _mm_shufflehi_epi16(m_into, 0x3F); /* PSHUFHW, put alpha2 in front of each rgb2 */
68 /* PACK_AB_WITHOUT_SATURATION is redefined in 32bpp_ssse3.hpp. */
69 #define PACK_AB_WITHOUT_SATURATION(m_from, m_into) \
70 m_from = _mm_and_si128(m_from, clear_hi); /* PAND, wipe high bytes to keep low bytes when packing */ \
71 m_into = _mm_packus_epi16(m_from, m_from); /* PACKUSWB, pack 2 colours (with saturation) */
73 /* Alpha blend 2 pixels. */
74 #define ALPHA_BLEND_2() { \
75 __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128()); /* PUNPCKLBW, expand each uint8 into uint16 */ \
76 __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128()); \
78 __m128i alphaAB = _mm_cmpgt_epi16(srcAB, _mm_setzero_si128()); /* PCMPGTW, if (alpha > 0) a++; */ \
79 alphaAB = _mm_srli_epi16(alphaAB, 15); \
80 alphaAB = _mm_add_epi16(alphaAB, srcAB); \
81 PUT_ALPHA_IN_FRONT_OF_RGB(alphaAB, alphaAB); \
83 srcAB = _mm_sub_epi16(srcAB, dstAB); /* PSUBW, (r - Cr) */ \
84 srcAB = _mm_mullo_epi16(srcAB, alphaAB); /* PMULLW, a*(r - Cr) */ \
85 srcAB = _mm_srli_epi16(srcAB, 8); /* PSRLW, a*(r - Cr)/256 */ \
86 srcAB = _mm_add_epi16(srcAB, dstAB); /* PADDW, a*(r - Cr)/256 + Cr */ \
87 PACK_AB_WITHOUT_SATURATION(srcAB, srcABCD); \
91 * rgb = rgb * ((256/4) * 4 - (alpha/4)) / ((256/4) * 4)
94 __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128()); \
95 __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128()); \
96 __m128i PUT_ALPHA_IN_FRONT_OF_RGB(srcAB, alphaAB); \
97 alphaAB = _mm_srli_epi16(alphaAB, 2); /* Reduce to 64 levels of shades so the max value fits in 16 bits. */ \
98 __m128i nom = _mm_sub_epi16(tr_nom_base, alphaAB); \
99 dstAB = _mm_mullo_epi16(dstAB, nom); \
100 dstAB = _mm_srli_epi16(dstAB, 8); \
101 dstAB = _mm_packus_epi16(dstAB, dstAB);
103 /** Base methods for 32bpp SSE blitters. */
104 class Blitter_32bppSSE_Base
{
106 virtual ~Blitter_32bppSSE_Base() {}
112 assert_compile(sizeof(MapValue
) == 2);
114 /** Helper for creating specialised functions for specific optimisations. */
116 RM_WITH_SKIP
, ///< Use normal code for skipping empty pixels.
117 RM_WITH_MARGIN
, ///< Use cached number of empty pixels at begin and end of line to reduce work.
118 RM_NONE
, ///< No specialisation.
121 /** Helper for creating specialised functions for the case where the sprite width is odd or even. */
123 BT_EVEN
, ///< An even number of pixels in the width; no need for a special case for the last pixel.
124 BT_ODD
, ///< An odd number of pixels in the width; special case for the last pixel.
125 BT_NONE
, ///< No specialisation for either case.
128 /** Data stored about a (single) sprite. */
130 uint32 sprite_offset
; ///< The offset to the sprite data.
131 uint32 mv_offset
; ///< The offset to the map value data.
132 uint16 sprite_line_size
; ///< The size of a single line (pitch).
133 uint16 sprite_width
; ///< The width of the sprite.
136 SpriteInfo infos
[ZOOM_LVL_COUNT
];
137 byte data
[]; ///< Data, all zoomlevels.
140 Sprite
*Encode(const SpriteLoader::Sprite
*sprite
, AllocatorProc
*allocator
);
141 virtual Colour
AdjustBrightness(Colour colour
, uint8 brightness
) = 0;
144 /** The SSE2 32 bpp blitter (without palette animation). */
145 class Blitter_32bppSSE2
: public Blitter_32bppSimple
, public Blitter_32bppSSE_Base
{
147 virtual Colour
AdjustBrightness(Colour colour
, uint8 brightness
);
148 static Colour
ReallyAdjustBrightness(Colour colour
, uint8 brightness
);
149 /* virtual */ void Draw(Blitter::BlitterParams
*bp
, BlitterMode mode
, ZoomLevel zoom
);
150 template <BlitterMode mode
, Blitter_32bppSSE_Base::ReadMode read_mode
, Blitter_32bppSSE_Base::BlockType bt_last
>
151 void Draw(const Blitter::BlitterParams
*bp
, ZoomLevel zoom
);
153 /* virtual */ Sprite
*Encode(const SpriteLoader::Sprite
*sprite
, AllocatorProc
*allocator
) {
154 return Blitter_32bppSSE_Base::Encode(sprite
, allocator
);
157 /* virtual */ const char *GetName() { return "32bpp-sse2"; }
160 /** Factory for the SSE2 32 bpp blitter (without palette animation). */
161 class FBlitter_32bppSSE2
: public BlitterFactory
{
163 FBlitter_32bppSSE2() : BlitterFactory("32bpp-sse2", "32bpp SSE2 Blitter (no palette animation)", HasCPUIDFlag(1, 3, 26)) {}
164 /* virtual */ Blitter
*CreateInstance() { return new Blitter_32bppSSE2(); }
167 #endif /* WITH_SSE */
168 #endif /* BLITTER_32BPP_SSE2_HPP */