# 32bpp_ssse3.cpp

Go to the documentation of this file.

    00001 /* $Id: 32bpp_ssse3.cpp 26222 2014-01-03 18:03:14Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifdef WITH_SSE
    00013
    00014 #include "../stdafx.h"
    00015 #include "../zoom_func.h"
    00016 #include "../settings_type.h"
    00017 #include "32bpp_ssse3.hpp"
    00018
    00020 static FBlitter_32bppSSSE3 iFBlitter_32bppSSSE3;
    00021
    00022 #if defined(__GNUC__)
    00023   #pragma GCC diagnostic push
    00024   #pragma GCC diagnostic ignored "-Wunused-variable"
    00025 #endif
    00026
    00033 template <BlitterMode mode, Blitter_32bppSSE2::ReadMode read_mode, Blitter_32bppSSE2::BlockType bt_last>
    00034 inline void Blitter_32bppSSSE3::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
    00035 {
    00036   const byte * const remap = bp->remap;
    00037   Colour *dst_line = (Colour *) bp->dst + bp->top * bp->pitch + bp->left;
    00038   int effective_width = bp->width;
    00039
    00040   /* Find where to start reading in the source sprite */
    00041   const SpriteData * const sd = (const SpriteData *) bp->sprite;
    00042   const SpriteInfo * const si = &sd->infos[zoom];
    00043   const MapValue *src_mv_line = (const MapValue *) &sd->data[si->mv_offset] + bp->skip_top * si->sprite_width;
    00044   const Colour *src_rgba_line = (const Colour *) ((const byte *) &sd->data[si->sprite_offset] + bp->skip_top * si->sprite_line_size);
    00045
    00046   if (read_mode != RM_WITH_MARGIN) {
    00047     src_rgba_line += bp->skip_left;
    00048     src_mv_line += bp->skip_left;
    00049   }
    00050
    00051   /* Load these variables into register before loop. */
    00052   const __m128i a_cm        = ALPHA_CONTROL_MASK;
    00053   const __m128i pack_hi_cm  = PACK_HIGH_CONTROL_MASK;
    00054   const __m128i briAB_cm    = BRIGHTNESS_LOW_CONTROL_MASK;
    00055   const __m128i div_cleaner = BRIGHTNESS_DIV_CLEANER;
    00056   const __m128i ob_check    = OVERBRIGHT_PRESENCE_MASK;
    00057   const __m128i ob_mask     = OVERBRIGHT_VALUE_MASK;
    00058   const __m128i ob_cm       = OVERBRIGHT_CONTROL_MASK;
    00059   const __m128i tr_nom_base = TRANSPARENT_NOM_BASE;
    00060
    00061   for (int y = bp->height; y != 0; y--) {
    00062     Colour *dst = dst_line;
    00063     const Colour *src = src_rgba_line + META_LENGTH;
    00064     const MapValue *src_mv = src_mv_line;
    00065
    00066     switch (mode) {
    00067       default: {
    00068         switch (read_mode) {
    00069           case RM_WITH_MARGIN: {
    00070             src += src_rgba_line[0].data;
    00071             dst += src_rgba_line[0].data;
    00072             const int width_diff = si->sprite_width - bp->width;
    00073             effective_width = bp->width - (int) src_rgba_line[0].data;
    00074             const int delta_diff = (int) src_rgba_line[1].data - width_diff;
    00075             const int new_width = effective_width - (delta_diff & ~1);
    00076             effective_width = delta_diff > 0 ? new_width : effective_width;
    00077             if (effective_width <= 0) break;
    00078             /* FALLTHROUGH */
    00079           }
    00080
    00081           case RM_WITH_SKIP: {
    00082             __m128i srcABCD = _mm_loadu_si128((const __m128i*) src);
    00083             __m128i dstABCD = _mm_loadu_si128((__m128i*) dst);
    00084             for (uint x = (uint) effective_width / 2; x > 0; x--) {
    00085               ALPHA_BLEND_2(pack_hi_cm);
    00086               /* With high repack, srcABCD have its 2 blended pixels like: [S0 S1 S2 S3] -> [-- -- BS0 BS1]
    00087                * dstABCD shuffled: [D0 D1 D2 D3] -> [D2 D3 D0 D0]
    00088                * PALIGNR takes what's in (): [-- -- (BS0 BS1] [D2 D3) D0 D0]
    00089                */
    00090               dstABCD = _mm_shuffle_epi32(dstABCD, 0x0E);
    00091               srcABCD = _mm_alignr_epi8(dstABCD, srcABCD, 8);
    00092               Colour *old_dst = dst;
    00093               src += 2;
    00094               dst += 2;
    00095               /* It is VERY important to read next data before it gets invalidated in cpu cache. */
    00096               dstABCD = _mm_loadu_si128((__m128i*) dst);
    00097               _mm_storeu_si128((__m128i *) old_dst, srcABCD);
    00098               srcABCD = _mm_loadu_si128((const __m128i*) src);
    00099             }
    00100             if (bt_last == BT_ODD) {
    00101               ALPHA_BLEND_2(pack_hi_cm);
    00102               (*dst).data = EXTR32(srcABCD, 2);
    00103             }
    00104             break;
    00105           }
    00106
    00107           default: NOT_REACHED();
    00108         }
    00109         break;
    00110       }
    00111       case BM_COLOUR_REMAP: {
    00112         switch (read_mode) {
    00113           case RM_WITH_MARGIN: {
    00114             src += src_rgba_line[0].data;
    00115             src_mv += src_rgba_line[0].data;
    00116             dst += src_rgba_line[0].data;
    00117             const int width_diff = si->sprite_width - bp->width;
    00118             effective_width = bp->width - (int) src_rgba_line[0].data;
    00119             const int delta_diff = (int) src_rgba_line[1].data - width_diff;
    00120             const int nd = effective_width - delta_diff;
    00121             effective_width = delta_diff > 0 ? nd : effective_width;
    00122             if (effective_width <= 0) break;
    00123             /* FALLTHROUGH */
    00124           }
    00125
    00126           case RM_WITH_SKIP: {
    00127             __m128i srcABCD = _mm_loadu_si128((const __m128i*) src);
    00128             __m128i dstABCD = _mm_loadu_si128((__m128i*) dst);
    00129             uint32 mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv));
    00130
    00131             for (uint x = (uint) effective_width / 2; x > 0; x--) {
    00132               /* Remap colours. */
    00133               if (mvX2 & 0x00FF00FF) {
    00134                 /* Written so the compiler uses CMOV. */
    00135                 const Colour src0 = src[0];
    00136                 const uint m0 = (byte) mvX2;
    00137                 const uint r0 = remap[m0];
    00138                 const Colour c0map = (this->LookupColourInPalette(r0).data & 0x00FFFFFF) | (src0.data & 0xFF000000);
    00139                 Colour c0 = 0; // Use alpha of 0 to keep dst as is.
    00140                 c0 = r0 == 0 ? c0 : c0map;
    00141                 c0 = m0 != 0 ? c0 : src0;
    00142                 INSR32(c0.data, srcABCD, 0);
    00143
    00144                 const Colour src1 = src[1];
    00145                 const uint m1 = (byte) (mvX2 >> 16);
    00146                 const uint r1 = remap[m1];
    00147                 const Colour c1map = (this->LookupColourInPalette(r1).data & 0x00FFFFFF) | (src1.data & 0xFF000000);
    00148                 Colour c1 = 0;
    00149                 c1 = r1 == 0 ? c1 : c1map;
    00150                 c1 = m1 != 0 ? c1 : src1;
    00151                 INSR32(c1.data, srcABCD, 1);
    00152
    00153                 if ((mvX2 & 0xFF00FF00) != 0x80008000) {
    00154                   ADJUST_BRIGHTNESS_2(srcABCD, mvX2);
    00155                 }
    00156               }
    00157
    00158               /* Blend colours. */
    00159               ALPHA_BLEND_2(pack_hi_cm);
    00160               dstABCD = _mm_shuffle_epi32(dstABCD, 0x0E);
    00161               srcABCD = _mm_alignr_epi8(dstABCD, srcABCD, 8);
    00162               Colour *old_dst = dst;
    00163               dst += 2;
    00164               src += 2;
    00165               src_mv += 2;
    00166               dstABCD = _mm_loadu_si128((__m128i*) dst);
    00167               _mm_storeu_si128((__m128i *) old_dst, srcABCD);
    00168               mvX2 = *((uint32 *) const_cast<MapValue *>(src_mv));
    00169               srcABCD = _mm_loadu_si128((const __m128i*) src);
    00170             }
    00171
    00172             if (effective_width & 1) {
    00173               /* In case the m-channel is zero, do not remap this pixel in any way */
    00174               if (src_mv->m == 0) {
    00175                 if (src->a < 255) {
    00176                   ALPHA_BLEND_2(pack_hi_cm);
    00177                   (*dst).data = EXTR32(srcABCD, 2);
    00178                 } else {
    00179                   *dst = src->data;
    00180                 }
    00181               } else {
    00182                 const uint r = remap[src_mv->m];
    00183                 if (r != 0) {
    00184                   Colour remapped_colour = AdjustBrightness(this->LookupColourInPalette(r), src_mv->v);
    00185                   if (src->a < 255) {
    00186                     remapped_colour.a = src->a;
    00187                     INSR32(remapped_colour.data, srcABCD, 0);
    00188                     ALPHA_BLEND_2(pack_hi_cm);
    00189                     (*dst).data = EXTR32(srcABCD, 2);
    00190                   } else
    00191                     *dst = remapped_colour;
    00192                 }
    00193               }
    00194             }
    00195             break;
    00196           }
    00197
    00198           default: NOT_REACHED();
    00199         }
    00200         src_mv_line += si->sprite_width;
    00201         break;
    00202       }
    00203       case BM_TRANSPARENT: {
    00204         /* Make the current colour a bit more black, so it looks like this image is transparent.
    00205          * rgb = rgb * ((256/4) * 4 - (alpha/4)) / ((256/4) * 4)
    00206          */
    00207         __m128i srcABCD = _mm_loadu_si128((const __m128i*) src);
    00208         __m128i dstABCD = _mm_loadu_si128((__m128i*) dst);
    00209         for (uint x = (uint) bp->width / 2; x > 0; x--) {
    00210           __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128());
    00211           __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128());
    00212           __m128i dstCD = _mm_unpackhi_epi8(dstABCD, _mm_setzero_si128());
    00213           __m128i alphaAB = _mm_shuffle_epi8(srcAB, a_cm);
    00214           alphaAB = _mm_srli_epi16(alphaAB, 2); // Reduce to 64 levels of shades so the max value fits in 16 bits.
    00215           __m128i nom = _mm_sub_epi16(tr_nom_base, alphaAB);
    00216           dstAB = _mm_mullo_epi16(dstAB, nom);
    00217           dstAB = _mm_srli_epi16(dstAB, 8);
    00218           dstAB = _mm_packus_epi16(dstAB, dstCD);
    00219           Colour *old_dst = dst;
    00220           src += 2;
    00221           dst += 2;
    00222           dstABCD = _mm_loadu_si128((__m128i*) dst);
    00223           _mm_storeu_si128((__m128i *) old_dst, dstAB);
    00224           srcABCD = _mm_loadu_si128((const __m128i*) src);
    00225         }
    00226         if (bp->width & 1) {
    00227           __m128i srcAB = _mm_unpacklo_epi8(srcABCD, _mm_setzero_si128());
    00228           __m128i dstAB = _mm_unpacklo_epi8(dstABCD, _mm_setzero_si128());
    00229           __m128i alphaAB = _mm_shuffle_epi8(srcAB, a_cm);
    00230           alphaAB = _mm_srli_epi16(alphaAB, 2);
    00231           __m128i nom = _mm_sub_epi16(tr_nom_base, alphaAB);
    00232           dstAB = _mm_mullo_epi16(dstAB, nom);
    00233           dstAB = _mm_srli_epi16(dstAB, 8);
    00234           dstAB = _mm_packus_epi16(dstAB, dstAB);
    00235           (*dst).data = EXTR32(dstAB, 0);
    00236         }
    00237         break;
    00238       }
    00239     }
    00240
    00241     src_rgba_line = (const Colour*) ((const byte*) src_rgba_line + si->sprite_line_size);
    00242     dst_line += bp->pitch;
    00243   }
    00244 }
    00245 #if defined(__GNUC__)
    00246   #pragma GCC diagnostic pop
    00247 #endif
    00248
    00256 void Blitter_32bppSSSE3::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
    00257 {
    00258   switch (mode) {
    00259     case BM_NORMAL: {
    00260       const BlockType bt_last = (BlockType) (bp->width & 1);
    00261       if (bp->skip_left != 0 || bp->width <= MARGIN_NORMAL_THRESHOLD) {
    00262         switch (bt_last) {
    00263           case BT_EVEN: Draw<BM_NORMAL, RM_WITH_SKIP, BT_EVEN>(bp, zoom); return;
    00264           case BT_ODD:  Draw<BM_NORMAL, RM_WITH_SKIP, BT_ODD>(bp, zoom); return;
    00265           default: NOT_REACHED();
    00266         }
    00267       } else {
    00268         switch (bt_last) {
    00269           case BT_EVEN: Draw<BM_NORMAL, RM_WITH_MARGIN, BT_EVEN>(bp, zoom); return;
    00270           case BT_ODD:  Draw<BM_NORMAL, RM_WITH_MARGIN, BT_ODD>(bp, zoom); return;
    00271           default: NOT_REACHED();
    00272         }
    00273       }
    00274       break;
    00275     }
    00276     case BM_COLOUR_REMAP:
    00277       if (bp->skip_left != 0 || bp->width <= MARGIN_REMAP_THRESHOLD) {
    00278         Draw<BM_COLOUR_REMAP, RM_WITH_SKIP, BT_NONE>(bp, zoom); return;
    00279       } else {
    00280         Draw<BM_COLOUR_REMAP, RM_WITH_MARGIN, BT_NONE>(bp, zoom); return;
    00281       }
    00282     case BM_TRANSPARENT:  Draw<BM_TRANSPARENT, RM_NONE, BT_NONE>(bp, zoom); return;
    00283     default: NOT_REACHED();
    00284   }
    00285 }
    00286
    00287 #endif /* WITH_SSE */