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

OpenTTD Source 14.0-RC3

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