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