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

OpenTTD Source 1.11.0-RC1

32bpp_anim_sse2.cpp
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 "32bpp_anim_sse2.hpp"
15 #include "32bpp_sse_func.hpp"
16
17 #include "../safeguards.h"
18
20 static FBlitter_32bppSSE2_Anim iFBlitter_32bppSSE2_Anim;
21
22 void Blitter_32bppSSE2_Anim::PaletteAnimate(const Palette &palette)
23 {
24 assert(!_screen_disable_anim);
25
26 this->palette = palette;
27 /* If first_dirty is 0, it is for 8bpp indication to send the new
28 * palette. However, only the animation colours might possibly change.
29 * Especially when going between toyland and non-toyland. */
30 assert(this->palette.first_dirty == PALETTE_ANIM_START || this->palette.first_dirty == 0);
31
32 const uint16 *anim = this->anim_buf;
33 Colour *dst = (Colour *)_screen.dst_ptr;
34
35 bool screen_dirty = false;
36
37 /* Let's walk the anim buffer and try to find the pixels */
38 const int width = this->anim_buf_width;
39 const int screen_pitch = _screen.pitch;
40 const int anim_pitch = this->anim_buf_pitch;
41 __m128i anim_cmp = _mm_set1_epi16(PALETTE_ANIM_START - 1);
42 __m128i brightness_cmp = _mm_set1_epi16(Blitter_32bppBase::DEFAULT_BRIGHTNESS);
43 __m128i colour_mask = _mm_set1_epi16(0xFF);
44 for (int y = this->anim_buf_height; y != 0 ; y--) {
45 Colour *next_dst_ln = dst + screen_pitch;
46 const uint16 *next_anim_ln = anim + anim_pitch;
47 int x = width;
48 while (x > 0) {
49 __m128i data = _mm_load_si128((const __m128i *) anim);
50
51 /* low bytes only, shifted into high positions */
52 __m128i colour_data = _mm_and_si128(data, colour_mask);
53
54 /* test if any colour >= PALETTE_ANIM_START */
55 int colour_cmp_result = _mm_movemask_epi8(_mm_cmpgt_epi16(colour_data, anim_cmp));
56 if (colour_cmp_result) {
57 /* test if any brightness is unexpected */
58 if (x < 8 || colour_cmp_result != 0xFFFF ||
59 _mm_movemask_epi8(_mm_cmpeq_epi16(_mm_srli_epi16(data, 8), brightness_cmp)) != 0xFFFF) {
60 /* slow path: < 8 pixels left or unexpected brightnesses */
61 for (int z = std::min<int>(x, 8); z != 0 ; z--) {
62 int value = _mm_extract_epi16(data, 0);
63 uint8 colour = GB(value, 0, 8);
64 if (colour >= PALETTE_ANIM_START) {
65 /* Update this pixel */
66 *dst = AdjustBrightneSSE(LookupColourInPalette(colour), GB(value, 8, 8));
67 screen_dirty = true;
68 }
69 data = _mm_srli_si128(data, 2);
70 dst++;
71 }
72 } else {
73 /* medium path: 8 pixels to animate all of expected brightnesses */
74 for (int z = 0; z < 8; z++) {
75 *dst = LookupColourInPalette(_mm_extract_epi16(colour_data, 0));
76 colour_data = _mm_srli_si128(colour_data, 2);
77 dst++;
78 }
79 screen_dirty = true;
80 }
81 } else {
82 /* fast path, no animation */
83 dst += 8;
84 }
85 anim += 8;
86 x -= 8;
87 }
88 dst = next_dst_ln;
89 anim = next_anim_ln;
90 }
91
92 if (screen_dirty) {
93 /* Make sure the backend redraws the whole screen */
94 VideoDriver::GetInstance()->MakeDirty(0, 0, _screen.width, _screen.height);
95 }
96 }
97
98 #endif /* WITH_SSE */
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
VideoDriver::MakeDirty
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
_screen_disable_anim
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition: gfx.cpp:43
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:187
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
32bpp_sse_func.hpp
Palette
Information about the currently used palette.
Definition: gfx_type.h:313