diff options
-rw-r--r-- | projects/openttd_vs100.vcxproj | 2 | ||||
-rw-r--r-- | projects/openttd_vs100.vcxproj.filters | 6 | ||||
-rw-r--r-- | projects/openttd_vs80.vcproj | 8 | ||||
-rw-r--r-- | projects/openttd_vs90.vcproj | 8 | ||||
-rw-r--r-- | source.list | 2 | ||||
-rw-r--r-- | src/blitter/32bpp_ssse3.cpp | 262 | ||||
-rw-r--r-- | src/blitter/32bpp_ssse3.hpp | 94 |
7 files changed, 382 insertions, 0 deletions
diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 4c46cdda7..95b3d0c90 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -1123,6 +1123,8 @@ <ClInclude Include="..\src\blitter\32bpp_simple.hpp" /> <ClCompile Include="..\src\blitter\32bpp_sse2.cpp" /> <ClInclude Include="..\src\blitter\32bpp_sse2.hpp" /> + <ClCompile Include="..\src\blitter\32bpp_ssse3.cpp" /> + <ClInclude Include="..\src\blitter\32bpp_ssse3.hpp" /> <ClCompile Include="..\src\blitter\8bpp_base.cpp" /> <ClInclude Include="..\src\blitter\8bpp_base.hpp" /> <ClCompile Include="..\src\blitter\8bpp_optimized.cpp" /> diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index 04ad15dcb..326ba3da4 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -2598,6 +2598,12 @@ <ClInclude Include="..\src\blitter\32bpp_sse2.hpp"> <Filter>Blitters</Filter> </ClInclude> + <ClCompile Include="..\src\blitter\32bpp_ssse3.cpp"> + <Filter>Blitters</Filter> + </ClCompile> + <ClInclude Include="..\src\blitter\32bpp_ssse3.hpp"> + <Filter>Blitters</Filter> + </ClInclude> <ClCompile Include="..\src\blitter\8bpp_base.cpp"> <Filter>Blitters</Filter> </ClCompile> diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index ad3f6b8ca..fea983c83 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -3835,6 +3835,14 @@ > </File> <File + RelativePath=".\..\src\blitter\32bpp_ssse3.cpp" + > + </File> + <File + RelativePath=".\..\src\blitter\32bpp_ssse3.hpp" + > + </File> + <File RelativePath=".\..\src\blitter\8bpp_base.cpp" > </File> diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index b6d47b154..1333456d5 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -3832,6 +3832,14 @@ > </File> <File + RelativePath=".\..\src\blitter\32bpp_ssse3.cpp" + > + </File> + <File + RelativePath=".\..\src\blitter\32bpp_ssse3.hpp" + > + </File> + <File RelativePath=".\..\src\blitter\8bpp_base.cpp" > </File> diff --git a/source.list b/source.list index 6d81d5a74..7cfb4f62d 100644 --- a/source.list +++ b/source.list @@ -910,6 +910,8 @@ blitter/32bpp_simple.hpp #if WITH_SSE blitter/32bpp_sse2.cpp blitter/32bpp_sse2.hpp +blitter/32bpp_ssse3.cpp +blitter/32bpp_ssse3.hpp #end blitter/8bpp_base.cpp blitter/8bpp_base.hpp diff --git a/src/blitter/32bpp_ssse3.cpp b/src/blitter/32bpp_ssse3.cpp new file mode 100644 index 000000000..eac4e8c11 --- /dev/null +++ b/src/blitter/32bpp_ssse3.cpp @@ -0,0 +1,262 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file 32bpp_ssse3.cpp Implementation of the SSSE3 32 bpp blitter. */ + +#ifdef WITH_SSE + +#include "../stdafx.h" +#include "../zoom_func.h" +#include "../settings_type.h" +#include "32bpp_ssse3.hpp" + +/** Instantiation of the SSSE3 32bpp blitter factory. */ +static FBlitter_32bppSSSE3 iFBlitter_32bppSSSE3; + +#if defined(__GNUC__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wunused-variable" +#endif +/** + * Draws a sprite to a (screen) buffer. It is templated to allow faster operation. + * + * @tparam mode blitter mode + * @param bp further blitting parameters + * @param zoom zoom level at which we are drawing + */ +template <BlitterMode mode, Blitter_32bppSSE2::ReadMode read_mode, Blitter_32bppSSE2::BlockType bt_last> +inline void Blitter_32bppSSSE3::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom) +{ + const byte * const remap = bp->remap; + Colour *dst_line = (Colour *) bp->dst + bp->top * bp->pitch + bp->left; + int effective_width = bp->width; + + /* Find where to start reading in the source sprite */ + const SpriteData * const sd = (const SpriteData *) bp->sprite; + const SpriteInfo * const si = &sd->infos[zoom]; + const MapValue *src_mv_line = (const MapValue *) &sd->data[si->mv_offset] + bp->skip_top * si->sprite_width; + const Colour *src_rgba_line = (const Colour *) ((const byte *) &sd->data[si->sprite_offset] + bp->skip_top * si->sprite_line_size); + + if (read_mode != RM_WITH_MARGIN) { + src_rgba_line += bp->skip_left; + src_mv_line += bp->skip_left; + } + + /* Load these variables into register before loop. */ + const __m128i a_cm = ALPHA_CONTROL_MASK; + const __m128i pack_hi_cm = PACK_HIGH_CONTROL_MASK; + const __m128i briAB_cm = BRIGHTNESS_LOW_CONTROL_MASK; + const __m128i div_cleaner = BRIGHTNESS_DIV_CLEANER; + const __m128i ob_check = OVERBRIGHT_PRESENCE_MASK; + const __m128i ob_mask = OVERBRIGHT_VALUE_MASK; + const __m128i ob_cm = OVERBRIGHT_CONTROL_MASK; + + for (int y = bp->height; y != 0; y--) { + Colour *dst = dst_line; + const Colour *src = src_rgba_line + META_LENGTH; + const MapValue *src_mv = src_mv_line; + + switch (mode) { + default: { + switch (read_mode) { + case RM_WITH_MARGIN: { + src += src_rgba_line[0].data; + dst += src_rgba_line[0].data; + const int width_diff = si->sprite_width - bp->width; + effective_width = bp->width - (int) src_rgba_line[0].data; + const int delta_diff = (int) src_rgba_line[1].data - width_diff; + const int new_width = effective_width - (delta_diff & ~1); + effective_width = delta_diff > 0 ? new_width : effective_width; + if (effective_width <= 0) break; + /* FALLTHROUGH */ + } + + case RM_WITH_SKIP: { + __m128i srcABCD = _mm_loadu_si128((const __m128i*) src); + __m128i dstABCD = _mm_loadu_si128((__m128i*) dst); + for (uint x = (uint) effective_width / 2; x > 0; x--) { + ALPHA_BLEND_2(pack_hi_cm); + /* With high repack, srcABCD have its 2 blended pixels like: [S0 S1 S2 S3] -> [-- -- BS0 BS1] + * dstABCD shuffled: [D0 D1 D2 D3] -> [D2 D3 D0 D0] + * PALIGNR takes what's in (): [-- -- (BS0 BS1] [D2 D3) D0 D0] + */ + dstABCD = _mm_shuffle_epi32(dstABCD, 0x0E); + srcABCD = _mm_alignr_epi8(dstABCD, srcABCD, 8); + Colour *old_dst = dst; + src += 2; + dst += 2; + /* It is VERY important to read next data before it gets invalidated in cpu cache. */ + dstABCD = _mm_loadu_si128((__m128i*) dst); + _mm_storeu_si128((__m128i *) old_dst, srcABCD); + srcABCD = _mm_loadu_si128((const __m128i*) src); + } + if (bt_last == BT_ODD) { + ALPHA_BLEND_2(pack_hi_cm); + (*dst).data = EXTR32(srcABCD, 2); + } + break; + } + + default: NOT_REACHED(); + } + break; + } + case BM_COLOUR_REMAP: { + switch (read_mode) { + case RM_WITH_MARGIN: { + src += src_rgba_line[0].data; + src_mv += src_rgba_line[0].data; + dst += src_rgba_line[0].data; + const int width_diff = si->sprite_width - bp->width; + effective_width = bp->width - (int) src_rgba_line[0].data; + const int delta_diff = (int) src_rgba_line[1].data - width_diff; + const int nd = effective_width - delta_diff; + effective_width = delta_diff > 0 ? nd : effective_width; + if (effective_width <= 0) break; + /* FALLTHROUGH */ + } + + case RM_WITH_SKIP: { + __m128i srcABCD = _mm_loadu_si128((const __m128i*) src); + __m128i dstABCD = _mm_loadu_si128((__m128i*) dst); + uint32 mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv)); + + for (uint x = (uint) effective_width / 2; x > 0; x--) { + /* Remap colours. */ + if (mvX2 & 0x00FF00FF) { + /* Written so the compiler uses CMOV. */ + const Colour src0 = src[0]; + const uint m0 = (byte) mvX2; + const uint r0 = remap[m0]; + const Colour c0map = (this->LookupColourInPalette(r0).data & 0x00FFFFFF) | (src0.data & 0xFF000000); + Colour c0 = 0; // Use alpha of 0 to keep dst as is. + c0 = r0 == 0 ? c0 : c0map; + c0 = m0 != 0 ? c0 : src0; + INSR32(c0.data, srcABCD, 0); + + const Colour src1 = src[1]; + const uint m1 = (byte) (mvX2 >> 16); + const uint r1 = remap[m1]; + const Colour c1map = (this->LookupColourInPalette(r1).data & 0x00FFFFFF) | (src1.data & 0xFF000000); + Colour c1 = 0; + c1 = r1 == 0 ? c1 : c1map; + c1 = m1 != 0 ? c1 : src1; + INSR32(c1.data, srcABCD, 1); + + if ((mvX2 & 0xFF00FF00) != 0x80008000) { + ADJUST_BRIGHTNESS_2(srcABCD, mvX2); + } + } + + /* Blend colours. */ + ALPHA_BLEND_2(pack_hi_cm); + dstABCD = _mm_shuffle_epi32(dstABCD, 0x0E); + srcABCD = _mm_alignr_epi8(dstABCD, srcABCD, 8); + Colour *old_dst = dst; + dst += 2; + src += 2; + src_mv += 2; + dstABCD = _mm_loadu_si128((__m128i*) dst); + _mm_storeu_si128((__m128i *) old_dst, srcABCD); + mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv)); + srcABCD = _mm_loadu_si128((const __m128i*) src); + } + + if (effective_width & 1) { + /* In case the m-channel is zero, do not remap this pixel in any way */ + if (src_mv->m == 0) { + if (src->a < 255) { + ALPHA_BLEND_2(pack_hi_cm); + (*dst).data = EXTR32(srcABCD, 2); + } else { + *dst = src->data; + } + } else { + const uint r = remap[src_mv->m]; + if (r != 0) { + Colour remapped_colour = AdjustBrightness(this->LookupColourInPalette(r), src_mv->v); + if (src->a < 255) { + remapped_colour.a = src->a; + INSR32(remapped_colour.data, srcABCD, 0); + ALPHA_BLEND_2(pack_hi_cm); + (*dst).data = EXTR32(srcABCD, 2); + } else + *dst = remapped_colour; + } + } + } + break; + } + + default: NOT_REACHED(); + } + src_mv_line += si->sprite_width; + break; + } + case BM_TRANSPARENT: + /* Make the current colour a bit more black, so it looks like this image is transparent */ + for (int x = bp->width; x != 0; x--) { + if (src->a == 255) { + *dst = MakeTransparent(*dst, 3, 4); + } else { + *dst = MakeTransparent(*dst, (256 * 4 - src->a), 256 * 4); + } + dst++; + src++; + } + break; + } + + src_rgba_line = (const Colour*) ((const byte*) src_rgba_line + si->sprite_line_size); + dst_line += bp->pitch; + } +} +#if defined(__GNUC__) + #pragma GCC diagnostic pop +#endif + +/** + * Draws a sprite to a (screen) buffer. Calls adequate templated function. + * + * @param bp further blitting parameters + * @param mode blitter mode + * @param zoom zoom level at which we are drawing + */ +void Blitter_32bppSSSE3::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) +{ + switch (mode) { + case BM_NORMAL: { + const BlockType bt_last = (BlockType) (bp->width & 1); + if (bp->skip_left != 0 || bp->width <= MARGIN_NORMAL_THRESHOLD) { + switch (bt_last) { + case BT_EVEN: Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN>(bp, zoom); return; + case BT_ODD: Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD>(bp, zoom); return; + default: NOT_REACHED(); + } + } else { + switch (bt_last) { + case BT_EVEN: Draw<BM_NORMAL, RM_WITH_MARGIN, BT_EVEN>(bp, zoom); return; + case BT_ODD: Draw<BM_NORMAL, RM_WITH_MARGIN, BT_ODD>(bp, zoom); return; + default: NOT_REACHED(); + } + } + break; + } + case BM_COLOUR_REMAP: + if (bp->skip_left != 0 || bp->width <= MARGIN_REMAP_THRESHOLD) { + Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE>(bp, zoom); return; + } else { + Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE>(bp, zoom); return; + } + case BM_TRANSPARENT: Draw<BM_TRANSPARENT, RM_NONE, BT_NONE>(bp, zoom); return; + default: NOT_REACHED(); + } +} + +#endif /* WITH_SSE */ diff --git a/src/blitter/32bpp_ssse3.hpp b/src/blitter/32bpp_ssse3.hpp new file mode 100644 index 000000000..7ea1fbd8b --- /dev/null +++ b/src/blitter/32bpp_ssse3.hpp @@ -0,0 +1,94 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * 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. + * 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. + * 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/>. + */ + +/** @file 32bpp_ssse3.hpp SSSE3 32 bpp blitter. */ + +#ifndef BLITTER_32BPP_SSSE3_HPP +#define BLITTER_32BPP_SSSE3_HPP + +#ifdef WITH_SSE + +#include "32bpp_sse2.hpp" +#include "tmmintrin.h" + +/* Alpha blend 2 pixels. */ +#undef ALPHA_BLEND_2 +#define ALPHA_BLEND_2(m_pack_mask) { \ + __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128()); /* PUNPCKLBW, expand each uint8 into uint16 */ \ + __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128()); \ + \ + __m128i alphaAB = _mm_cmpgt_epi16(srcAB, _mm_setzero_si128()); /* PCMPGTW, if (alpha > 0) a++; */ \ + alphaAB = _mm_srli_epi16(alphaAB, 15); \ + alphaAB = _mm_add_epi16(alphaAB, srcAB); \ + alphaAB = _mm_shuffle_epi8(alphaAB, a_cm); /* PSHUFB, put alpha in front of each rgb */ \ + \ + srcAB = _mm_sub_epi16(srcAB, dstAB); /* PSUBW, (r - Cr) */ \ + srcAB = _mm_mullo_epi16(srcAB, alphaAB); /* PMULLW, a*(r - Cr) */ \ + srcAB = _mm_srli_epi16(srcAB, 8); /* PSRLW, a*(r - Cr)/256 */ \ + srcAB = _mm_add_epi16(srcAB, dstAB); /* PADDW, a*(r - Cr)/256 + Cr */ \ + srcABCD = _mm_shuffle_epi8(srcAB, m_pack_mask); /* PSHUFB, pack 2 Colour (without saturation) */ \ +} + +/* Adjust brightness of 2 pixels. */ +#define ADJUST_BRIGHTNESS_2(colourX2, brightnessX2) \ + /* The following dataflow differs from the one of AdjustBrightness() only for alpha. + * In order to keep alpha in colAB, insert a 1 in a unused brightness byte (a*1->a). + * OK, not a 1 but DEFAULT_BRIGHTNESS to compensate the div. + */ \ + brightnessX2 &= 0xFF00FF00; \ + brightnessX2 += DEFAULT_BRIGHTNESS; \ + \ + __m128i zero = _mm_setzero_si128(); \ + __m128i colAB = _mm_unpacklo_epi8(colourX2, zero); \ + \ + __m128i briAB; \ + INSR64(brightnessX2, briAB, 0); \ + briAB = _mm_shuffle_epi8(briAB, briAB_cm); /* DEFAULT_BRIGHTNESS in 0, 0x00 in 2. */ \ + colAB = _mm_mullo_epi16(colAB, briAB); \ + __m128i colAB_ob = _mm_srli_epi16(colAB, 8+7); \ + colAB = _mm_srli_epi16(colAB, 7); \ + \ + /* Sum overbright. + * Maximum for each rgb is 508 => 9 bits. The highest bit tells if there is overbright. + * -255 is changed in -256 so we just have to take the 8 lower bits into account. + */ \ + colAB = _mm_and_si128(colAB, div_cleaner); \ + colAB_ob = _mm_and_si128(colAB_ob, ob_check); \ + colAB_ob = _mm_mullo_epi16(colAB_ob, ob_mask); \ + colAB_ob = _mm_and_si128(colAB_ob, colAB); \ + __m128i obAB = _mm_hadd_epi16(_mm_hadd_epi16(colAB_ob, zero), zero); \ + \ + obAB = _mm_srli_epi16(obAB, 1); /* Reduce overbright strength. */ \ + obAB = _mm_shuffle_epi8(obAB, ob_cm); \ + __m128i retAB = ob_mask; /* ob_mask is equal to white. */ \ + retAB = _mm_subs_epu16(retAB, colAB); /* (255 - rgb) */ \ + retAB = _mm_mullo_epi16(retAB, obAB); /* ob*(255 - rgb) */ \ + retAB = _mm_srli_epi16(retAB, 8); /* ob*(255 - rgb)/256 */ \ + retAB = _mm_add_epi16(retAB, colAB); /* ob*(255 - rgb)/256 + rgb */ \ + \ + colourX2 = _mm_packus_epi16(retAB, retAB); + +/** The SSSE3 32 bpp blitter (without palette animation). */ +class Blitter_32bppSSSE3 : public Blitter_32bppSSE2 { +public: + /* virtual */ void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom); + template <BlitterMode mode, ReadMode read_mode, BlockType bt_last> + void Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom); + /* virtual */ const char *GetName() { return "32bpp-ssse3"; } +}; + +/** Factory for the SSSE3 32 bpp blitter (without palette animation). */ +class FBlitter_32bppSSSE3: public BlitterFactory { +public: + FBlitter_32bppSSSE3() : BlitterFactory("32bpp-ssse3", "32bpp SSSE3 Blitter (no palette animation)", HasCPUIDFlag(1, 2, 9)) {} + /* virtual */ Blitter *CreateInstance() { return new Blitter_32bppSSSE3(); } +}; + +#endif /* WITH_SSE */ +#endif /* BLITTER_32BPP_SSSE3_HPP */ |