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

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

32bpp_anim_sse4.cpp
1 /* $Id: 32bpp_anim_sse4.cpp 26969 2014-10-06 18:45:51Z rubidium $ */
2
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 */
9
12 #ifdef WITH_SSE
13
14 #include "../stdafx.h"
15 #include "../video/video_driver.hpp"
16 #include "../table/sprites.h"
17 #include "32bpp_anim_sse4.hpp"
18 #include "32bpp_sse_func.hpp"
19
20 #include "../safeguards.h"
21
23 static FBlitter_32bppSSE4_Anim iFBlitter_32bppSSE4_Anim;
24
32 IGNORE_UNINITIALIZED_WARNING_START
33 template <BlitterMode mode, Blitter_32bppSSE2::ReadMode read_mode, Blitter_32bppSSE2::BlockType bt_last, bool translucent, bool animated>
34 inline void Blitter_32bppSSE4_Anim::Draw(const Blitter::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 *anim_line = this->anim_buf + ((uint32 *)bp->dst - (uint32 *)_screen.dst_ptr) + bp->top * this->anim_buf_width + 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
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);
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 } else if (src->a == 255) {
161 *anim = *(const uint16*) src_mv;
162 *dst = (src_mv->m >= PALETTE_ANIM_START) ? AdjustBrightneSSE(LookupColourInPalette(src_mv->m), src_mv->v) : *src;
163 } else {
164 *anim = 0;
165 __m128i srcABCD;
166 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
167 if (src_mv->m >= PALETTE_ANIM_START) {
168 Colour colour = AdjustBrightneSSE(LookupColourInPalette(src_mv->m), src_mv->v);
169 colour.a = src->a;
170 srcABCD = _mm_cvtsi32_si128(colour.data);
171 } else {
172 srcABCD = _mm_cvtsi32_si128(src->data);
173 }
174 dst->data = _mm_cvtsi128_si32(AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm));
175 }
176 }
177 break;
178
179 case BM_COLOUR_REMAP:
180 for (uint x = (uint) effective_width / 2; x != 0; x--) {
181 uint32 mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv));
182 __m128i srcABCD = _mm_loadl_epi64((const __m128i*) src);
183 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
184
185 /* Remap colours. */
186 const uint m0 = (byte) mvX2;
187 const uint r0 = remap[m0];
188 const uint m1 = (byte) (mvX2 >> 16);
189 const uint r1 = remap[m1];
190 if (mvX2 & 0x00FF00FF) {
191 #define CMOV_REMAP(m_colour, m_colour_init, m_src, m_m) \
192 /* Written so the compiler uses CMOV. */ \
193 Colour m_colour = m_colour_init; \
194 { \
195 const Colour srcm = (Colour) (m_src); \
196 const uint m = (byte) (m_m); \
197 const uint r = remap[m]; \
198 const Colour cmap = (this->LookupColourInPalette(r).data & 0x00FFFFFF) | (srcm.data & 0xFF000000); \
199 m_colour = r == 0 ? m_colour : cmap; \
200 m_colour = m != 0 ? m_colour : srcm; \
201 }
202 #ifdef _SQ64
203 uint64 srcs = _mm_cvtsi128_si64(srcABCD);
204 uint64 dsts;
205 if (animated) dsts = _mm_cvtsi128_si64(dstABCD);
206 uint64 remapped_src = 0;
207 CMOV_REMAP(c0, animated ? dsts : 0, srcs, mvX2);
208 remapped_src = c0.data;
209 CMOV_REMAP(c1, animated ? dsts >> 32 : 0, srcs >> 32, mvX2 >> 16);
210 remapped_src |= (uint64) c1.data << 32;
211 srcABCD = _mm_cvtsi64_si128(remapped_src);
212 #else
213 Colour remapped_src[2];
214 CMOV_REMAP(c0, animated ? _mm_cvtsi128_si32(dstABCD) : 0, _mm_cvtsi128_si32(srcABCD), mvX2);
215 remapped_src[0] = c0.data;
216 CMOV_REMAP(c1, animated ? dst[1] : 0, src[1], mvX2 >> 16);
217 remapped_src[1] = c1.data;
218 srcABCD = _mm_loadl_epi64((__m128i*) &remapped_src);
219 #endif
220
221 if ((mvX2 & 0xFF00FF00) != 0x80008000) srcABCD = AdjustBrightnessOfTwoPixels(srcABCD, mvX2);
222 }
223
224 /* Update anim buffer. */
225 if (animated) {
226 const byte a0 = src[0].a;
227 const byte a1 = src[1].a;
228 uint32 anim01 = mvX2 & 0xFF00FF00;
229 if (a0 == 255) {
230 anim01 |= r0;
231 if (a1 == 255) {
232 *(uint32*) anim = anim01 | (r1 << 16);
233 goto bmcr_full_opacity;
234 }
235 } else if (a0 == 0) {
236 if (a1 == 0) {
237 goto bmcr_full_transparency;
238 } else {
239 if (a1 == 255) {
240 anim[1] = r1 | (anim01 >> 16);
241 }
242 goto bmcr_alpha_blend;
243 }
244 }
245 if (a1 > 0) {
246 if (a1 == 255) anim01 |= r1 << 16;
247 *(uint32*) anim = anim01;
248 } else {
249 anim[0] = (uint16) anim01;
250 }
251 } else {
252 if (src[0].a) anim[0] = 0;
253 if (src[1].a) anim[1] = 0;
254 }
255
256 /* Blend colours. */
257 bmcr_alpha_blend:
258 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm);
259 bmcr_full_opacity:
260 _mm_storel_epi64((__m128i *) dst, srcABCD);
261 bmcr_full_transparency:
262 src_mv += 2;
263 dst += 2;
264 src += 2;
265 anim += 2;
266 }
267
268 if ((bt_last == BT_NONE && effective_width & 1) || bt_last == BT_ODD) {
269 /* In case the m-channel is zero, do not remap this pixel in any way. */
270 __m128i srcABCD;
271 if (src->a == 0) break;
272 if (src_mv->m) {
273 const uint r = remap[src_mv->m];
274 *anim = (animated && src->a == 255) ? r | ((uint16) src_mv->v << 8 ) : 0;
275 if (r != 0) {
276 Colour remapped_colour = AdjustBrightneSSE(this->LookupColourInPalette(r), src_mv->v);
277 if (src->a == 255) {
278 *dst = remapped_colour;
279 } else {
280 remapped_colour.a = src->a;
281 srcABCD = _mm_cvtsi32_si128(remapped_colour.data);
282 goto bmcr_alpha_blend_single;
283 }
284 }
285 } else {
286 *anim = 0;
287 srcABCD = _mm_cvtsi32_si128(src->data);
288 if (src->a < 255) {
289 bmcr_alpha_blend_single:
290 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
291 srcABCD = AlphaBlendTwoPixels(srcABCD, dstABCD, a_cm, pack_low_cm);
292 }
293 dst->data = _mm_cvtsi128_si32(srcABCD);
294 }
295 }
296 break;
297
298 case BM_TRANSPARENT:
299 /* Make the current colour a bit more black, so it looks like this image is transparent. */
300 for (uint x = (uint) bp->width / 2; x > 0; x--) {
301 __m128i srcABCD = _mm_loadl_epi64((const __m128i*) src);
302 __m128i dstABCD = _mm_loadl_epi64((__m128i*) dst);
303 _mm_storel_epi64((__m128i *) dst, DarkenTwoPixels(srcABCD, dstABCD, a_cm, tr_nom_base));
304 src += 2;
305 dst += 2;
306 anim += 2;
307 if (src[-2].a) anim[-2] = 0;
308 if (src[-1].a) anim[-1] = 0;
309 }
310
311 if ((bt_last == BT_NONE && bp->width & 1) || bt_last == BT_ODD) {
312 __m128i srcABCD = _mm_cvtsi32_si128(src->data);
313 __m128i dstABCD = _mm_cvtsi32_si128(dst->data);
314 dst->data = _mm_cvtsi128_si32(DarkenTwoPixels(srcABCD, dstABCD, a_cm, tr_nom_base));
315 if (src[0].a) anim[0] = 0;
316 }
317 break;
318
319 case BM_CRASH_REMAP:
320 for (uint x = (uint) bp->width; x > 0; x--) {
321 if (src_mv->m == 0) {
322 if (src->a != 0) {
323 uint8 g = MakeDark(src->r, src->g, src->b);
324 *dst = ComposeColourRGBA(g, g, g, src->a, *dst);
325 *anim = 0;
326 }
327 } else {
328 uint r = remap[src_mv->m];
329 if (r != 0) *dst = ComposeColourPANoCheck(this->AdjustBrightness(this->LookupColourInPalette(r), src_mv->v), src->a, *dst);
330 }
331 src_mv++;
332 dst++;
333 src++;
334 anim++;
335 }
336 break;
337
338 case BM_BLACK_REMAP:
339 for (uint x = (uint) bp->width; x > 0; x--) {
340 if (src->a != 0) {
341 *dst = Colour(0, 0, 0);
342 *anim = 0;
343 }
344 src_mv++;
345 dst++;
346 src++;
347 anim++;
348 }
349 break;
350 }
351
352 next_line:
353 if (mode != BM_TRANSPARENT) src_mv_line += si->sprite_width;
354 src_rgba_line = (const Colour*) ((const byte*) src_rgba_line + si->sprite_line_size);
355 dst_line += bp->pitch;
356 anim_line += this->anim_buf_width;
357 }
358 }
359 IGNORE_UNINITIALIZED_WARNING_STOP
360
368 void Blitter_32bppSSE4_Anim::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
369 {
370 const Blitter_32bppSSE_Base::SpriteFlags sprite_flags = ((const Blitter_32bppSSE_Base::SpriteData *) bp->sprite)->flags;
371 switch (mode) {
372 default: {
373 bm_normal:
374 if (bp->skip_left != 0 || bp->width <= MARGIN_NORMAL_THRESHOLD) {
375 const BlockType bt_last = (BlockType) (bp->width & 1);
376 if (bt_last == BT_EVEN) {
377 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN, true, false>(bp, zoom);
378 else Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN, true, true>(bp, zoom);
379 } else {
380 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD, true, false>(bp, zoom);
381 else Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD, true, true>(bp, zoom);
382 }
383 } else {
384 #ifdef _SQ64
385 if (sprite_flags & SF_TRANSLUCENT) {
386 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
387 else Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
388 } else {
389 if (sprite_flags & SF_NO_ANIM) Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, false, false>(bp, zoom);
390 else Draw<BM_NORMAL, RM_WITH_MARGIN, BT_NONE, false, true>(bp, zoom);
391 }
392 #else
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 #endif
396 }
397 break;
398 }
399 case BM_COLOUR_REMAP:
400 if (sprite_flags & SF_NO_REMAP) goto bm_normal;
401 if (bp->skip_left != 0 || bp->width <= MARGIN_REMAP_THRESHOLD) {
402 if (sprite_flags & SF_NO_ANIM) Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true, false>(bp, zoom);
403 else Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE, true, true>(bp, zoom);
404 } else {
405 if (sprite_flags & SF_NO_ANIM) Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, false>(bp, zoom);
406 else Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE, true, true>(bp, zoom);
407 }
408 break;
409 case BM_TRANSPARENT: Draw<BM_TRANSPARENT, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
410 case BM_CRASH_REMAP: Draw<BM_CRASH_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
411 case BM_BLACK_REMAP: Draw<BM_BLACK_REMAP, RM_NONE, BT_NONE, true, true>(bp, zoom); return;
412 }
413 }
414
415 #endif /* WITH_SSE */