OpenTTD Source: src/blitter/32bpp_anim_sse4.cpp Source File

OpenTTD Source 12.0-beta1

32bpp_anim_sse4.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifdef WITH_SSE
11
12 #include "../stdafx.h"
13 #include "../video/video_driver.hpp"
14 #include "../table/sprites.h"
15 #include "32bpp_anim_sse4.hpp"
16 #include "32bpp_sse_func.hpp"
17
18 #include "../safeguards.h"
19
21 static FBlitter_32bppSSE4_Anim iFBlitter_32bppSSE4_Anim;
22
30 IGNORE_UNINITIALIZED_WARNING_START
31 template <BlitterMode mode, Blitter_32bppSSE2::ReadMode read_mode, Blitter_32bppSSE2::BlockType bt_last, bool translucent, bool animated>
32 inline void Blitter_32bppSSE4_Anim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
33 {
34 const byte * const remap = bp->remap;
35 Colour *dst_line = (Colour *) bp->dst + bp->top * bp->pitch + bp->left;
36 uint16 *anim_line = this->anim_buf + this->ScreenToAnimOffset((uint32 *)bp->dst) + bp->top * this->anim_buf_pitch + bp->left;
37 int effective_width = bp->width;
38
39 /* Find where to start reading in the source sprite. */
40 const Blitter_32bppSSE_Base::SpriteData * const sd = (const Blitter_32bppSSE_Base::SpriteData *) bp->sprite;
41 const SpriteInfo * const si = &sd->infos[zoom];
42 const MapValue *src_mv_line = (const MapValue *) &sd->data[si->mv_offset] + bp->skip_top * si->sprite_width;
43 const Colour *src_rgba_line = (const Colour *) ((const byte *) &sd->data[si->sprite_offset] + bp->skip_top * si->sprite_line_size);
44
45 if (read_mode != RM_WITH_MARGIN) {
46 src_rgba_line += bp->skip_left;
47 src_mv_line += bp->skip_left;
48 }
49 const MapValue *src_mv = src_mv_line;
50
51 /* Load these variables into register before loop. */
52 const __m128i a_cm = ALPHA_CONTROL_MASK;
53 const __m128i pack_low_cm = PACK_LOW_CONTROL_MASK;
54 const __m128i tr_nom_base = TRANSPARENT_NOM_BASE;
55
56 for (int y = bp->height; y != 0; y--) {
57 Colour *dst = dst_line;
58 const Colour *src = src_rgba_line + META_LENGTH;
59 if (mode != BM_TRANSPARENT) src_mv = src_mv_line;
60 uint16 *anim = anim_line;
61
62 if (read_mode == RM_WITH_MARGIN) {
63 assert(bt_last == BT_NONE); // or you must ensure block type is preserved
64 anim += src_rgba_line[0].data;
65 src += src_rgba_line[0].data;
66 dst += src_rgba_line[0].data;
67 if (mode != BM_TRANSPARENT) src_mv += src_rgba_line[0].data;
68 const int width_diff = si->sprite_width - bp->width;
69 effective_width = bp->width - (int) src_rgba_line[0].data;
70 const int delta_diff = (int) src_rgba_line[1].data - width_diff;
71 const int new_width = effective_width - delta_diff;
72 effective_width = delta_diff > 0 ? new_width : effective_width;
73 if (effective_width <= 0) goto next_line;
74 }
75
76 switch (mode) {
77 default:
78 if (!translucent) {
79 for (uint x = (uint) effective_width; x > 0; x--) {
80 if (src->a) {
81 if (animated) {
82 *anim = *(const uint16*) src_mv;
83 *dst = (src_mv->m >= PALETTE_ANIM_START) ? AdjustBrightneSSE(this->LookupColourInPalette(src_mv->m), src_mv->v) : src->data;
84 } else {
85 *anim = 0;
86 *dst = *src;
87 }
88 }
89 if (animated) src_mv++;
90 anim++;
91 src++;
92 dst++;
93 }
94 break;
95 }
96
97 for (uint x = (uint) effective_width/2; x != 0; x--) {
98 uint32 mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv));
99 __m128i srcABCD = _mm_loadl_epi64((const __m128i*) src);
100 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
101
102 if (animated) {
103 /* Remap colours. */
104 const byte m0 = mvX2;
105 if (m0 >= PALETTE_ANIM_START) {
106 const Colour c0 = (this->LookupColourInPalette(m0).data & 0x00FFFFFF) | (src[0].data & 0xFF000000);
107 InsertFirstUint32(AdjustBrightneSSE(c0, (byte) (mvX2 >> 8)).data, srcABCD);
108 }
109 const byte m1 = mvX2 >> 16;
110 if (m1 >= PALETTE_ANIM_START) {
111 const Colour c1 = (this->LookupColourInPalette(m1).data & 0x00FFFFFF) | (src[1].data & 0xFF000000);
112 InsertSecondUint32(AdjustBrightneSSE(c1, (byte) (mvX2 >> 24)).data, srcABCD);
113 }
114
115 /* Update anim buffer. */
116 const byte a0 = src[0].a;
117 const byte a1 = src[1].a;
118 uint32 anim01 = 0;
119 if (a0 == 255) {
120 if (a1 == 255) {
121 *(uint32*) anim = mvX2;
122 goto bmno_full_opacity;
123 }
124 anim01 = (uint16) mvX2;
125 } else if (a0 == 0) {
126 if (a1 == 0) {
127 goto bmno_full_transparency;
128 } else {
129 if (a1 == 255) anim[1] = (uint16) (mvX2 >> 16);
130 goto bmno_alpha_blend;
131 }
132 }
133 if (a1 > 0) {
134 if (a1 == 255) anim01 |= mvX2 & 0xFFFF0000;
135 *(uint32*) anim = anim01;
136 } else {
137 anim[0] = (uint16) anim01;
138 }
139 } else {
140 if (src[0].a) anim[0] = 0;
141 if (src[1].a) anim[1] = 0;
142 }
143
144 /* Blend colours. */
145 bmno_alpha_blend:
146 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm);
147 bmno_full_opacity:
148 _mm_storel_epi64((__m128i *) dst, srcABCD);
149 bmno_full_transparency:
150 src_mv += 2;
151 src += 2;
152 anim += 2;
153 dst += 2;
154 }
155
156 if ((bt_last == BT_NONE && effective_width & 1) || bt_last == BT_ODD) {
157 if (src->a == 0) {
158 /* Complete transparency. */
159 } else if (src->a == 255) {
160 *anim = *(const uint16*) src_mv;
161 *dst = (src_mv->m >= PALETTE_ANIM_START) ? AdjustBrightneSSE(LookupColourInPalette(src_mv->m), src_mv->v) : *src;
162 } else {
163 *anim = 0;
164 __m128i srcABCD;
165 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
166 if (src_mv->m >= PALETTE_ANIM_START) {
167 Colour colour = AdjustBrightneSSE(LookupColourInPalette(src_mv->m), src_mv->v);
168 colour.a = src->a;
169 srcABCD = _mm_cvtsi32_si128(colour.data);
170 } else {
171 srcABCD = _mm_cvtsi32_si128(src->data);
172 }
173 dst->data = _mm_cvtsi128_si32(AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm));
174 }
175 }
176 break;
177
178 case BM_COLOUR_REMAP:
179 for (uint x = (uint) effective_width / 2; x != 0; x--) {
180 uint32 mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv));
181 __m128i srcABCD = _mm_loadl_epi64((const __m128i*) src);
182 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
183
184 /* Remap colours. */
185 const uint m0 = (byte) mvX2;
186 const uint r0 = remap[m0];
187 const uint m1 = (byte) (mvX2 >> 16);
188 const uint r1 = remap[m1];
189 if (mvX2 & 0x00FF00FF) {
190 #define CMOV_REMAP(m_colour, m_colour_init, m_src, m_m) \
191 /* Written so the compiler uses CMOV. */ \
192 Colour m_colour = m_colour_init; \
193 { \
194 const Colour srcm = (Colour) (m_src); \
195 const uint m = (byte) (m_m); \
196 const uint r = remap[m]; \
197 const Colour cmap = (this->LookupColourInPalette(r).data & 0x00FFFFFF) | (srcm.data & 0xFF000000); \
198 m_colour = r == 0 ? m_colour : cmap; \
199 m_colour = m != 0 ? m_colour : srcm; \
200 }
201 #ifdef POINTER_IS_64BIT
202 uint64 srcs = _mm_cvtsi128_si64(srcABCD);
203 uint64 dsts;
204 if (animated) dsts = _mm_cvtsi128_si64(dstABCD);
205 uint64 remapped_src = 0;
206 CMOV_REMAP(c0, animated ? dsts : 0, srcs, mvX2);
207 remapped_src = c0.data;
208 CMOV_REMAP(c1, animated ? dsts >> 32 : 0, srcs >> 32, mvX2 >> 16);
209 remapped_src |= (uint64) c1.data << 32;
210 srcABCD = _mm_cvtsi64_si128(remapped_src);
211 #else
212 Colour remapped_src[2];
213 CMOV_REMAP(c0, animated ? _mm_cvtsi128_si32(dstABCD) : 0, _mm_cvtsi128_si32(srcABCD), mvX2);
214 remapped_src[0] = c0.data;
215 CMOV_REMAP(c1, animated ? dst[1] : 0, src[1], mvX2 >> 16);
216 remapped_src[1] = c1.data;
217 srcABCD = _mm_loadl_epi64((__m128i*) &remapped_src);
218 #endif
219
220 if ((mvX2 & 0xFF00FF00) != 0x80008000) srcABCD = AdjustBrightnessOfTwoPixels(srcABCD, mvX2);
221 }
222
223 /* Update anim buffer. */
224 if (animated) {
225 const byte a0 = src[0].a;
226 const byte a1 = src[1].a;
227 uint32 anim01 = mvX2 & 0xFF00FF00;
228 if (a0 == 255) {
229 anim01 |= r0;
230 if (a1 == 255) {
231 *(uint32*) anim = anim01 | (r1 << 16);
232 goto bmcr_full_opacity;
233 }
234 } else if (a0 == 0) {
235 if (a1 == 0) {
236 goto bmcr_full_transparency;
237 } else {
238 if (a1 == 255) {
239 anim[1] = r1 | (anim01 >> 16);
240 }
241 goto bmcr_alpha_blend;
242 }
243 }
244 if (a1 > 0) {
245 if (a1 == 255) anim01 |= r1 << 16;
246 *(uint32*) anim = anim01;
247 } else {
248 anim[0] = (uint16) anim01;
249 }
250 } else {
251 if (src[0].a) anim[0] = 0;
252 if (src[1].a) anim[1] = 0;
253 }
254
255 /* Blend colours. */
256 bmcr_alpha_blend:
257 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm);
258 bmcr_full_opacity:
259 _mm_storel_epi64((__m128i *) dst, srcABCD);
260 bmcr_full_transparency:
261 src_mv += 2;
262 dst += 2;
263 src += 2;
264 anim += 2;
265 }
266
267 if ((bt_last == BT_NONE && effective_width & 1) || bt_last == BT_ODD) {
268 /* In case the m-channel is zero, do not remap this pixel in any way. */
269 __m128i srcABCD;
270 if (src->a == 0) break;
271 if (src_mv->m) {
272 const uint r = remap[src_mv->m];
273 *anim = (animated && src->a == 255) ? r | ((uint16) src_mv->v << 8 ) : 0;
274 if (r != 0) {
275 Colour remapped_colour = AdjustBrightneSSE(this->LookupColourInPalette(r), src_mv->v);
276 if (src->a == 255) {
277 *dst = remapped_colour;
278 } else {
279 remapped_colour.a = src->a;
280 srcABCD = _mm_cvtsi32_si128(remapped_colour.data);
281 goto bmcr_alpha_blend_single;
282 }
283 }
284 } else {
285 *anim = 0;
286 srcABCD = _mm_cvtsi32_si128(src->data);
287 if (src->a < 255) {
288 bmcr_alpha_blend_single:
289 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
290 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm);
291 }
292 dst->data = _mm_cvtsi128_si32(srcABCD);
293 }
294 }
295 break;
296
297 case BM_TRANSPARENT:
298 /* Make the current colour a bit more black, so it looks like this image is transparent. */
299 for (uint x = (uint) bp->width / 2; x > 0; x--) {
300 __m128i srcABCD = _mm_loadl_epi64((const __m128i*) src);
301 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
302 _mm_storel_epi64((__m128i *) dst, DarkenTwoPixels(srcABCD, dstABCD, a_cm, tr_nom_base));
303 src += 2;
304 dst += 2;
305 anim += 2;
306 if (src[-2].a) anim[-2] = 0;
307 if (src[-1].a) anim[-1] = 0;
308 }
309
310 if ((bt_last == BT_NONE && bp->width & 1) || bt_last == BT_ODD) {
311 __m128i srcABCD = _mm_cvtsi32_si128(src->data);
312 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
313 dst->data = _mm_cvtsi128_si32(DarkenTwoPixels(srcABCD, dstABCD, a_cm, tr_nom_base));
314 if (src[0].a) anim[0] = 0;
315 }
316 break;
317
318 case BM_CRASH_REMAP:
319 for (uint x = (uint) bp->width; x > 0; x--) {
320 if (src_mv->m == 0) {
321 if (src->a != 0) {
322 uint8 g = MakeDark(src->r, src->g, src->b);
323 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
324 *anim = 0;
325 }
326 } else {
327 uint r = remap[src_mv->m];
328 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), src_mv->v), src->a, *dst);
329 }
330 src_mv++;
331 dst++;
332 src++;
333 anim++;
334 }
335 break;
336
337 case BM_BLACK_REMAP:
338 for (uint x = (uint) bp->width; x > 0; x--) {
339 if (src->a != 0) {
340 *dst = Colour(0, 0, 0);
341 *anim = 0;
342 }
343 src_mv++;
344 dst++;
345 src++;
346 anim++;
347 }
348 break;
349 }
350
351 next_line:
352 if (mode != BM_TRANSPARENT) src_mv_line += si->sprite_width;
353 src_rgba_line = (const Colour*) ((const byte*) src_rgba_line + si->sprite_line_size);
354 dst_line += bp->pitch;
355 anim_line += this->anim_buf_pitch;
356 }
357 }
358 IGNORE_UNINITIALIZED_WARNING_STOP
359
367 void Blitter_32bppSSE4_Anim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
368 {
369 const Blitter_32bppSSE_Base::SpriteFlags sprite_flags = ((const Blitter_32bppSSE_Base::SpriteData *) bp->sprite)->flags;
370 switch (mode) {
371 default: {
372 bm_normal:
373 if (bp->skip_left != 0 || bp->width <= MARGIN_NORMAL_THRESHOLD) {
374 const BlockType bt_last = (BlockType) (bp->width & 1);
375 if (bt_last == BT_EVEN) {
376 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN, true, false>(bp, zoom);
377 else Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN, true, true>(bp, zoom);
378 } else {
379 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD, true, false>(bp, zoom);
380 else Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD, true, true>(bp, zoom);
381 }
382 } else {
383 #ifdef POINTER_IS_64BIT
384 if (sprite_flags & SF_TRANSLUCENT) {
385 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
386 else Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
387 } else {
388 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, false, false>(bp, zoom);
389 else Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, false, true>(bp, zoom);
390 }
391 #else
392 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
393 else Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
394 #endif
395 }
396 break;
397 }
398 case BM_COLOUR_REMAP:
399 if (sprite_flags & SF_NO_REMAP) goto bm_normal;
400 if (bp->skip_left != 0 || bp->width <= MARGIN_REMAP_THRESHOLD) {
401 if (sprite_flags & SF_NO_ANIM) Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true, false>(bp, zoom);
402 else Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true, true>(bp, zoom);
403 } else {
404 if (sprite_flags & SF_NO_ANIM) Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
405 else Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
406 }
407 break;
408 case BM_TRANSPARENT: Draw<BM_TRANSPARENT, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
409 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
410 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
411 }
412 }
413
414 #endif /* WITH_SSE */
Colour::data
uint32 data
Conversion of the channel information to a 32 bit number.
Definition: gfx_type.h:164
Blitter::BlitterParams::top
int top
The top offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:42
BM_TRANSPARENT
@ BM_TRANSPARENT
Perform transparency colour remapping.
Definition: base.hpp:20
Blitter::BlitterParams::skip_left
int skip_left
How much pixels of the source to skip on the left (based on zoom of dst)
Definition: base.hpp:35
BlitterMode
BlitterMode
The modes of blitting we can do.
Definition: base.hpp:17
Blitter::BlitterParams::width
int width
The width in pixels that needs to be drawn to dst.
Definition: base.hpp:37
Blitter::BlitterParams::dst
void * dst
Destination buffer.
Definition: base.hpp:44
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
Blitter::BlitterParams::pitch
int pitch
The pitch of the destination buffer.
Definition: base.hpp:45
Blitter::BlitterParams::sprite
const void * sprite
Pointer to the sprite how ever the encoder stored it.
Definition: base.hpp:32
BM_COLOUR_REMAP
@ BM_COLOUR_REMAP
Perform a colour remapping.
Definition: base.hpp:19
BM_CRASH_REMAP
@ BM_CRASH_REMAP
Perform a crash remapping.
Definition: base.hpp:21
BM_BLACK_REMAP
@ BM_BLACK_REMAP
Perform remapping to a completely blackened sprite.
Definition: base.hpp:22
PALETTE_ANIM_START
@ PALETTE_ANIM_START
Index in the _palettes array from which all animations are taking places (table/palettes....
Definition: gfx_type.h:282
Colour
Structure to access the alpha, red, green, and blue channels from a 32 bit number.
Definition: gfx_type.h:163
Colour::a
uint8 a
colour channels in LE order
Definition: gfx_type.h:171
Blitter::BlitterParams::left
int left
The left offset in the 'dst' in pixels to start drawing.
Definition: base.hpp:41
32bpp_sse_func.hpp
32bpp_anim_sse4.hpp
Blitter::BlitterParams
Parameters related to blitting.
Definition: base.hpp:31
Blitter::BlitterParams::height
int height
The height in pixels that needs to be drawn to dst.
Definition: base.hpp:38
Blitter::BlitterParams::skip_top
int skip_top
How much pixels of the source to skip on the top (based on zoom of dst)
Definition: base.hpp:36
Blitter::BlitterParams::remap
const byte * remap
XXX – Temporary storage for remap array.
Definition: base.hpp:33