src/video/SDL_blit_A.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 16 Aug 2007 22:18:53 +0000
changeset 2255 17b2369756be
parent 2232 6630fefab312
child 2257 340942cfda48
permissions -rw-r--r--
Use MMX intrinsics over GCC inline assembly
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
slouken@0
    27
/* Functions to perform alpha blended blitting */
slouken@0
    28
slouken@0
    29
/* N->1 blending with per-surface alpha */
slouken@1895
    30
static void
slouken@1895
    31
BlitNto1SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
    32
{
slouken@1895
    33
    int width = info->d_width;
slouken@1895
    34
    int height = info->d_height;
slouken@1895
    35
    Uint8 *src = info->s_pixels;
slouken@1895
    36
    int srcskip = info->s_skip;
slouken@1895
    37
    Uint8 *dst = info->d_pixels;
slouken@1895
    38
    int dstskip = info->d_skip;
slouken@1895
    39
    Uint8 *palmap = info->table;
slouken@1895
    40
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
    41
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
    42
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    43
slouken@1895
    44
    const unsigned A = srcfmt->alpha;
slouken@0
    45
slouken@1895
    46
    while (height--) {
slouken@1895
    47
	    /* *INDENT-OFF* */
slouken@0
    48
	    DUFFS_LOOP4(
slouken@0
    49
	    {
icculus@1162
    50
		Uint32 Pixel;
slouken@0
    51
		unsigned sR;
slouken@0
    52
		unsigned sG;
slouken@0
    53
		unsigned sB;
slouken@0
    54
		unsigned dR;
slouken@0
    55
		unsigned dG;
slouken@0
    56
		unsigned dB;
icculus@1162
    57
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
slouken@0
    58
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
    59
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
    60
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
    61
		ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
    62
		dR &= 0xff;
slouken@0
    63
		dG &= 0xff;
slouken@0
    64
		dB &= 0xff;
slouken@0
    65
		/* Pack RGB into 8bit pixel */
slouken@0
    66
		if ( palmap == NULL ) {
slouken@0
    67
		    *dst =((dR>>5)<<(3+2))|
slouken@0
    68
			  ((dG>>5)<<(2))|
slouken@0
    69
			  ((dB>>6)<<(0));
slouken@0
    70
		} else {
slouken@0
    71
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
    72
				  ((dG>>5)<<(2))  |
slouken@0
    73
				  ((dB>>6)<<(0))];
slouken@0
    74
		}
slouken@0
    75
		dst++;
slouken@0
    76
		src += srcbpp;
slouken@0
    77
	    },
slouken@0
    78
	    width);
slouken@1895
    79
	    /* *INDENT-ON* */
slouken@1895
    80
        src += srcskip;
slouken@1895
    81
        dst += dstskip;
slouken@1895
    82
    }
slouken@0
    83
}
slouken@0
    84
slouken@0
    85
/* N->1 blending with pixel alpha */
slouken@1895
    86
static void
slouken@1895
    87
BlitNto1PixelAlpha(SDL_BlitInfo * info)
slouken@0
    88
{
slouken@1895
    89
    int width = info->d_width;
slouken@1895
    90
    int height = info->d_height;
slouken@1895
    91
    Uint8 *src = info->s_pixels;
slouken@1895
    92
    int srcskip = info->s_skip;
slouken@1895
    93
    Uint8 *dst = info->d_pixels;
slouken@1895
    94
    int dstskip = info->d_skip;
slouken@1895
    95
    Uint8 *palmap = info->table;
slouken@1895
    96
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
    97
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
    98
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    99
slouken@1895
   100
    /* FIXME: fix alpha bit field expansion here too? */
slouken@1895
   101
    while (height--) {
slouken@1895
   102
	    /* *INDENT-OFF* */
slouken@0
   103
	    DUFFS_LOOP4(
slouken@0
   104
	    {
icculus@1162
   105
		Uint32 Pixel;
slouken@0
   106
		unsigned sR;
slouken@0
   107
		unsigned sG;
slouken@0
   108
		unsigned sB;
slouken@0
   109
		unsigned sA;
slouken@0
   110
		unsigned dR;
slouken@0
   111
		unsigned dG;
slouken@0
   112
		unsigned dB;
icculus@1162
   113
		DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
slouken@0
   114
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
   115
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
   116
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
   117
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   118
		dR &= 0xff;
slouken@0
   119
		dG &= 0xff;
slouken@0
   120
		dB &= 0xff;
slouken@0
   121
		/* Pack RGB into 8bit pixel */
slouken@0
   122
		if ( palmap == NULL ) {
slouken@0
   123
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   124
			  ((dG>>5)<<(2))|
slouken@0
   125
			  ((dB>>6)<<(0));
slouken@0
   126
		} else {
slouken@0
   127
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   128
				  ((dG>>5)<<(2))  |
slouken@0
   129
				  ((dB>>6)<<(0))  ];
slouken@0
   130
		}
slouken@0
   131
		dst++;
slouken@0
   132
		src += srcbpp;
slouken@0
   133
	    },
slouken@0
   134
	    width);
slouken@1895
   135
	    /* *INDENT-ON* */
slouken@1895
   136
        src += srcskip;
slouken@1895
   137
        dst += dstskip;
slouken@1895
   138
    }
slouken@0
   139
}
slouken@0
   140
slouken@0
   141
/* colorkeyed N->1 blending with per-surface alpha */
slouken@1895
   142
static void
slouken@1895
   143
BlitNto1SurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
   144
{
slouken@1895
   145
    int width = info->d_width;
slouken@1895
   146
    int height = info->d_height;
slouken@1895
   147
    Uint8 *src = info->s_pixels;
slouken@1895
   148
    int srcskip = info->s_skip;
slouken@1895
   149
    Uint8 *dst = info->d_pixels;
slouken@1895
   150
    int dstskip = info->d_skip;
slouken@1895
   151
    Uint8 *palmap = info->table;
slouken@1895
   152
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
   153
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
   154
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
   155
    Uint32 ckey = srcfmt->colorkey;
slouken@0
   156
slouken@1895
   157
    const int A = srcfmt->alpha;
slouken@0
   158
slouken@1895
   159
    while (height--) {
slouken@1895
   160
	    /* *INDENT-OFF* */
slouken@0
   161
	    DUFFS_LOOP(
slouken@0
   162
	    {
icculus@1162
   163
		Uint32 Pixel;
slouken@0
   164
		unsigned sR;
slouken@0
   165
		unsigned sG;
slouken@0
   166
		unsigned sB;
slouken@0
   167
		unsigned dR;
slouken@0
   168
		unsigned dG;
slouken@0
   169
		unsigned dB;
icculus@1162
   170
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
   171
		if ( Pixel != ckey ) {
slouken@0
   172
		    dR = dstfmt->palette->colors[*dst].r;
slouken@0
   173
		    dG = dstfmt->palette->colors[*dst].g;
slouken@0
   174
		    dB = dstfmt->palette->colors[*dst].b;
slouken@0
   175
		    ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   176
		    dR &= 0xff;
slouken@0
   177
		    dG &= 0xff;
slouken@0
   178
		    dB &= 0xff;
slouken@0
   179
		    /* Pack RGB into 8bit pixel */
slouken@0
   180
		    if ( palmap == NULL ) {
slouken@0
   181
			*dst =((dR>>5)<<(3+2))|
slouken@0
   182
			      ((dG>>5)<<(2)) |
slouken@0
   183
			      ((dB>>6)<<(0));
slouken@0
   184
		    } else {
slouken@0
   185
			*dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   186
				      ((dG>>5)<<(2))  |
slouken@0
   187
				      ((dB>>6)<<(0))  ];
slouken@0
   188
		    }
slouken@0
   189
		}
slouken@0
   190
		dst++;
slouken@0
   191
		src += srcbpp;
slouken@0
   192
	    },
slouken@0
   193
	    width);
slouken@1895
   194
	    /* *INDENT-ON* */
slouken@1895
   195
        src += srcskip;
slouken@1895
   196
        dst += dstskip;
slouken@1895
   197
    }
slouken@0
   198
}
slouken@0
   199
slouken@2255
   200
#ifdef __MMX__
slouken@1542
   201
slouken@1542
   202
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
   203
static void
slouken@1895
   204
BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo * info)
slouken@1542
   205
{
slouken@1895
   206
    int width = info->d_width;
slouken@1895
   207
    int height = info->d_height;
slouken@1895
   208
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   209
    int srcskip = info->s_skip >> 2;
slouken@1895
   210
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   211
    int dstskip = info->d_skip >> 2;
slouken@1895
   212
    Uint32 dalpha = info->dst->Amask;
slouken@1542
   213
slouken@1895
   214
    __m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
slouken@1542
   215
slouken@1895
   216
    hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe);       /* alpha128 mask -> hmask */
slouken@1895
   217
    lmask = _mm_set_pi32(0x00010101, 0x00010101);       /* !alpha128 mask -> lmask */
slouken@1895
   218
    dsta = _mm_set_pi32(dalpha, dalpha);        /* dst alpha mask -> dsta */
slouken@1542
   219
slouken@1895
   220
    while (height--) {
slouken@1895
   221
        int n = width;
slouken@1895
   222
        if (n & 1) {
slouken@1895
   223
            Uint32 s = *srcp++;
slouken@1895
   224
            Uint32 d = *dstp;
slouken@1895
   225
            *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1895
   226
                       + (s & d & 0x00010101)) | dalpha;
slouken@1895
   227
            n--;
slouken@1895
   228
        }
slouken@1542
   229
slouken@1895
   230
        for (n >>= 1; n > 0; --n) {
slouken@1895
   231
            dst1 = *(__m64 *) dstp;     /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   232
            dst2 = dst1;        /* 2 x dst -> dst2(ARGBARGB) */
slouken@1542
   233
slouken@1895
   234
            src1 = *(__m64 *) srcp;     /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   235
            src2 = src1;        /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   236
slouken@1895
   237
            dst2 = _mm_and_si64(dst2, hmask);   /* dst & mask -> dst2 */
slouken@1895
   238
            src2 = _mm_and_si64(src2, hmask);   /* src & mask -> src2 */
slouken@1895
   239
            src2 = _mm_add_pi32(src2, dst2);    /* dst2 + src2 -> src2 */
slouken@1895
   240
            src2 = _mm_srli_pi32(src2, 1);      /* src2 >> 1 -> src2 */
slouken@1895
   241
slouken@1895
   242
            dst1 = _mm_and_si64(dst1, src1);    /* src & dst -> dst1 */
slouken@1895
   243
            dst1 = _mm_and_si64(dst1, lmask);   /* dst1 & !mask -> dst1 */
slouken@1895
   244
            dst1 = _mm_add_pi32(dst1, src2);    /* src2 + dst1 -> dst1 */
slouken@1895
   245
            dst1 = _mm_or_si64(dst1, dsta);     /* dsta(full alpha) | dst1 -> dst1 */
slouken@1895
   246
slouken@1895
   247
            *(__m64 *) dstp = dst1;     /* dst1 -> 2 x dst pixels */
slouken@1895
   248
            dstp += 2;
slouken@1895
   249
            srcp += 2;
slouken@1895
   250
        }
slouken@1895
   251
slouken@1895
   252
        srcp += srcskip;
slouken@1895
   253
        dstp += dstskip;
slouken@1895
   254
    }
slouken@1895
   255
    _mm_empty();
slouken@1542
   256
}
slouken@1542
   257
slouken@1542
   258
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
   259
static void
slouken@1895
   260
BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   261
{
slouken@1895
   262
    SDL_PixelFormat *df = info->dst;
slouken@1895
   263
    Uint32 chanmask = df->Rmask | df->Gmask | df->Bmask;
slouken@1895
   264
    unsigned alpha = info->src->alpha;
slouken@1542
   265
slouken@1895
   266
    if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1895
   267
        /* only call a128 version when R,G,B occupy lower bits */
slouken@1895
   268
        BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@1895
   269
    } else {
slouken@1895
   270
        int width = info->d_width;
slouken@1895
   271
        int height = info->d_height;
slouken@1895
   272
        Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   273
        int srcskip = info->s_skip >> 2;
slouken@1895
   274
        Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   275
        int dstskip = info->d_skip >> 2;
slouken@1895
   276
        Uint32 dalpha = df->Amask;
slouken@1895
   277
        Uint32 amult;
slouken@1542
   278
slouken@1895
   279
        __m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
slouken@1542
   280
slouken@1895
   281
        mm_zero = _mm_setzero_si64();   /* 0 -> mm_zero */
slouken@1895
   282
        /* form the alpha mult */
slouken@1895
   283
        amult = alpha | (alpha << 8);
slouken@1895
   284
        amult = amult | (amult << 16);
slouken@1895
   285
        chanmask =
slouken@1895
   286
            (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->
slouken@1895
   287
                                                           Bshift);
slouken@1895
   288
        mm_alpha = _mm_set_pi32(0, amult & chanmask);   /* 0000AAAA -> mm_alpha, minus 1 chan */
slouken@1895
   289
        mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
slouken@1895
   290
        /* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1895
   291
        dsta = _mm_set_pi32(dalpha, dalpha);    /* dst alpha mask -> dsta */
slouken@1542
   292
slouken@1895
   293
        while (height--) {
slouken@1895
   294
            int n = width;
slouken@1895
   295
            if (n & 1) {
slouken@1895
   296
                /* One Pixel Blend */
slouken@1895
   297
                src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB) */
slouken@1895
   298
                src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
slouken@1542
   299
slouken@1895
   300
                dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB) */
slouken@1895
   301
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   302
slouken@1895
   303
                src2 = _mm_sub_pi16(src2, dst1);        /* src2 - dst2 -> src2 */
slouken@1895
   304
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   305
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   306
                dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
slouken@1542
   307
slouken@1895
   308
                dst1 = _mm_packs_pu16(dst1, mm_zero);   /* 0000ARGB -> dst1 */
slouken@1895
   309
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1895
   310
                *dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   311
slouken@1895
   312
                ++srcp;
slouken@1895
   313
                ++dstp;
slouken@1542
   314
slouken@1895
   315
                n--;
slouken@1895
   316
            }
slouken@1542
   317
slouken@1895
   318
            for (n >>= 1; n > 0; --n) {
slouken@1895
   319
                /* Two Pixels Blend */
slouken@1895
   320
                src1 = *(__m64 *) srcp; /* 2 x src -> src1(ARGBARGB) */
slouken@1895
   321
                src2 = src1;    /* 2 x src -> src2(ARGBARGB) */
slouken@1895
   322
                src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
slouken@1895
   323
                src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
slouken@1542
   324
slouken@1895
   325
                dst1 = *(__m64 *) dstp; /* 2 x dst -> dst1(ARGBARGB) */
slouken@1895
   326
                dst2 = dst1;    /* 2 x dst -> dst2(ARGBARGB) */
slouken@1895
   327
                dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
slouken@1895
   328
                dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
slouken@1895
   329
slouken@1895
   330
                src1 = _mm_sub_pi16(src1, dst1);        /* src1 - dst1 -> src1 */
slouken@1895
   331
                src1 = _mm_mullo_pi16(src1, mm_alpha);  /* src1 * alpha -> src1 */
slouken@1895
   332
                src1 = _mm_srli_pi16(src1, 8);  /* src1 >> 8 -> src1 */
slouken@1895
   333
                dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
slouken@1895
   334
slouken@1895
   335
                src2 = _mm_sub_pi16(src2, dst2);        /* src2 - dst2 -> src2 */
slouken@1895
   336
                src2 = _mm_mullo_pi16(src2, mm_alpha);  /* src2 * alpha -> src2 */
slouken@1895
   337
                src2 = _mm_srli_pi16(src2, 8);  /* src2 >> 8 -> src2 */
slouken@1895
   338
                dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
slouken@1895
   339
slouken@1895
   340
                dst1 = _mm_packs_pu16(dst1, dst2);      /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
slouken@1895
   341
                dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1895
   342
slouken@1895
   343
                *(__m64 *) dstp = dst1; /* dst1 -> 2 x pixel */
slouken@1895
   344
slouken@1895
   345
                srcp += 2;
slouken@1895
   346
                dstp += 2;
slouken@1895
   347
            }
slouken@1895
   348
            srcp += srcskip;
slouken@1895
   349
            dstp += dstskip;
slouken@1895
   350
        }
slouken@1895
   351
        _mm_empty();
slouken@1895
   352
    }
slouken@1542
   353
}
slouken@1542
   354
slouken@1542
   355
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   356
static void
slouken@1895
   357
BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo * info)
slouken@1542
   358
{
slouken@1895
   359
    int width = info->d_width;
slouken@1895
   360
    int height = info->d_height;
slouken@1895
   361
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   362
    int srcskip = info->s_skip >> 2;
slouken@1895
   363
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   364
    int dstskip = info->d_skip >> 2;
slouken@1895
   365
    SDL_PixelFormat *sf = info->src;
slouken@1895
   366
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
   367
    Uint32 amask = sf->Amask;
slouken@1895
   368
    Uint32 ashift = sf->Ashift;
slouken@1895
   369
    Uint64 multmask;
slouken@1542
   370
slouken@1895
   371
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
   372
slouken@1895
   373
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@2255
   374
	multmask = 0xFFFF;
slouken@2255
   375
    multmask <<= (ashift * 2);
slouken@2255
   376
    multmask = ~multmask;
slouken@1895
   377
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1542
   378
slouken@1895
   379
    while (height--) {
slouken@1895
   380
		/* *INDENT-OFF* */
slouken@1542
   381
		DUFFS_LOOP4({
slouken@1542
   382
		Uint32 alpha = *srcp & amask;
slouken@1542
   383
		if (alpha == 0) {
slouken@1542
   384
			/* do nothing */
slouken@1542
   385
		} else if (alpha == amask) {
slouken@1542
   386
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   387
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
   388
		} else {
slouken@1542
   389
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
   390
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
   391
slouken@1542
   392
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
   393
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   394
slouken@1542
   395
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
   396
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
   397
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
   398
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
   399
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
   400
slouken@1542
   401
			/* blend */		    
slouken@1542
   402
			src1 = _mm_sub_pi16(src1, dst1);/* src1 - dst1 -> src1 */
slouken@1542
   403
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src1 - dst1) * alpha -> src1 */
slouken@1542
   404
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
   405
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1 -> dst1(0A0R0G0B) */
slouken@1542
   406
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
   407
			
slouken@1542
   408
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   409
		}
slouken@1542
   410
		++srcp;
slouken@1542
   411
		++dstp;
slouken@1542
   412
	    }, width);
slouken@1895
   413
		/* *INDENT-ON* */
slouken@1895
   414
        srcp += srcskip;
slouken@1895
   415
        dstp += dstskip;
slouken@1895
   416
    }
slouken@1895
   417
    _mm_empty();
slouken@1542
   418
}
slouken@1895
   419
slouken@2255
   420
#endif /* __MMX__ */
slouken@689
   421
slouken@1361
   422
#if SDL_ALTIVEC_BLITTERS
slouken@1795
   423
#if __MWERKS__
slouken@1795
   424
#pragma altivec_model on
slouken@1795
   425
#endif
slouken@1361
   426
#if HAVE_ALTIVEC_H
icculus@1162
   427
#include <altivec.h>
icculus@1175
   428
#endif
icculus@1047
   429
#include <assert.h>
icculus@1162
   430
slouken@1402
   431
#if (defined(__MACOSX__) && (__GNUC__ < 4))
slouken@1895
   432
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   433
        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
slouken@1895
   434
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   435
        (vector unsigned short) ( a,b,c,d,e,f,g,h )
icculus@1162
   436
#else
slouken@1895
   437
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   438
        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
slouken@1895
   439
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   440
        (vector unsigned short) { a,b,c,d,e,f,g,h }
icculus@1162
   441
#endif
icculus@1162
   442
icculus@1047
   443
#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
icculus@1047
   444
#define VECPRINT(msg, v) do { \
icculus@1047
   445
    vector unsigned int tmpvec = (vector unsigned int)(v); \
icculus@1047
   446
    unsigned int *vp = (unsigned int *)&tmpvec; \
icculus@1047
   447
    printf("%s = %08X %08X %08X %08X\n", msg, vp[0], vp[1], vp[2], vp[3]); \
icculus@1047
   448
} while (0)
icculus@1047
   449
icculus@1047
   450
/* the permuation vector that takes the high bytes out of all the appropriate shorts 
icculus@1047
   451
    (vector unsigned char)(
icculus@1047
   452
        0x00, 0x10, 0x02, 0x12,
icculus@1047
   453
        0x04, 0x14, 0x06, 0x16,
icculus@1047
   454
        0x08, 0x18, 0x0A, 0x1A,
icculus@1047
   455
        0x0C, 0x1C, 0x0E, 0x1E );
icculus@1047
   456
*/
icculus@1047
   457
#define VEC_MERGE_PERMUTE() (vec_add(vec_lvsl(0, (int*)NULL), (vector unsigned char)vec_splat_u16(0x0F)))
icculus@1047
   458
#define VEC_U32_24() (vec_add(vec_splat_u32(12), vec_splat_u32(12)))
icculus@1047
   459
#define VEC_ALPHA_MASK() ((vector unsigned char)vec_sl((vector unsigned int)vec_splat_s8(-1), VEC_U32_24()))
icculus@1047
   460
#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
icculus@1047
   461
    ? vec_lvsl(0, src) \
icculus@1047
   462
    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
icculus@1047
   463
slouken@1895
   464
icculus@1047
   465
#define VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1_16, v8_16) do { \
icculus@1047
   466
    /* vtemp1 contains source AAGGAAGGAAGGAAGG */ \
icculus@1047
   467
    vector unsigned short vtemp1 = vec_mule(vs, valpha); \
icculus@1047
   468
    /* vtemp2 contains source RRBBRRBBRRBBRRBB */ \
icculus@1047
   469
    vector unsigned short vtemp2 = vec_mulo(vs, valpha); \
icculus@1047
   470
    /* valpha2 is 255-alpha */ \
icculus@1047
   471
    vector unsigned char valpha2 = vec_nor(valpha, valpha); \
icculus@1047
   472
    /* vtemp3 contains dest AAGGAAGGAAGGAAGG */ \
icculus@1047
   473
    vector unsigned short vtemp3 = vec_mule(vd, valpha2); \
icculus@1047
   474
    /* vtemp4 contains dest RRBBRRBBRRBBRRBB */ \
icculus@1047
   475
    vector unsigned short vtemp4 = vec_mulo(vd, valpha2); \
icculus@1047
   476
    /* add source and dest */ \
icculus@1047
   477
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   478
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   479
    /* vtemp1 = (vtemp1 + 1) + ((vtemp1 + 1) >> 8) */ \
icculus@1047
   480
    vtemp1 = vec_add(vtemp1, v1_16); \
icculus@1047
   481
    vtemp3 = vec_sr(vtemp1, v8_16); \
icculus@1047
   482
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   483
    /* vtemp2 = (vtemp2 + 1) + ((vtemp2 + 1) >> 8) */ \
icculus@1047
   484
    vtemp2 = vec_add(vtemp2, v1_16); \
icculus@1047
   485
    vtemp4 = vec_sr(vtemp2, v8_16); \
icculus@1047
   486
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   487
    /* (>>8) and get ARGBARGBARGBARGB */ \
icculus@1047
   488
    vd = (vector unsigned char)vec_perm(vtemp1, vtemp2, mergePermute); \
icculus@1047
   489
} while (0)
slouken@1895
   490
icculus@1047
   491
/* Calculate the permute vector used for 32->32 swizzling */
slouken@1895
   492
static vector unsigned char
slouken@1895
   493
calc_swizzle32(const SDL_PixelFormat * srcfmt, const SDL_PixelFormat * dstfmt)
icculus@1047
   494
{
icculus@1047
   495
    /*
icculus@1047
   496
     * We have to assume that the bits that aren't used by other
icculus@1047
   497
     *  colors is alpha, and it's one complete byte, since some formats
icculus@1047
   498
     *  leave alpha with a zero mask, but we should still swizzle the bits.
icculus@1047
   499
     */
icculus@1047
   500
    /* ARGB */
icculus@1047
   501
    const static struct SDL_PixelFormat default_pixel_format = {
icculus@1047
   502
        NULL, 0, 0,
icculus@1047
   503
        0, 0, 0, 0,
icculus@1047
   504
        16, 8, 0, 24,
icculus@1047
   505
        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
slouken@1895
   506
        0, 0
slouken@1895
   507
    };
icculus@1047
   508
    if (!srcfmt) {
icculus@1047
   509
        srcfmt = &default_pixel_format;
icculus@1047
   510
    }
icculus@1047
   511
    if (!dstfmt) {
icculus@1047
   512
        dstfmt = &default_pixel_format;
icculus@1047
   513
    }
slouken@1895
   514
    const vector unsigned char plus = VECUINT8_LITERAL(0x00, 0x00, 0x00, 0x00,
slouken@1895
   515
                                                       0x04, 0x04, 0x04, 0x04,
slouken@1895
   516
                                                       0x08, 0x08, 0x08, 0x08,
slouken@1895
   517
                                                       0x0C, 0x0C, 0x0C,
slouken@1895
   518
                                                       0x0C);
icculus@1047
   519
    vector unsigned char vswiz;
icculus@1047
   520
    vector unsigned int srcvec;
icculus@1047
   521
#define RESHIFT(X) (3 - ((X) >> 3))
icculus@1047
   522
    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
icculus@1047
   523
    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
icculus@1047
   524
    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
icculus@1047
   525
    Uint32 amask;
icculus@1047
   526
    /* Use zero for alpha if either surface doesn't have alpha */
icculus@1047
   527
    if (dstfmt->Amask) {
slouken@1895
   528
        amask =
slouken@1895
   529
            ((srcfmt->Amask) ? RESHIFT(srcfmt->Ashift) : 0x10) << (dstfmt->
slouken@1895
   530
                                                                   Ashift);
icculus@1047
   531
    } else {
slouken@1895
   532
        amask =
slouken@1895
   533
            0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^
slouken@1895
   534
                          0xFFFFFFFF);
icculus@1047
   535
    }
slouken@1895
   536
#undef RESHIFT
slouken@1895
   537
    ((unsigned int *) (char *) &srcvec)[0] = (rmask | gmask | bmask | amask);
slouken@1895
   538
    vswiz = vec_add(plus, (vector unsigned char) vec_splat(srcvec, 0));
slouken@1895
   539
    return (vswiz);
icculus@1047
   540
}
icculus@1047
   541
slouken@1895
   542
static void
slouken@1895
   543
Blit32to565PixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   544
{
icculus@1047
   545
    int height = info->d_height;
slouken@1895
   546
    Uint8 *src = (Uint8 *) info->s_pixels;
icculus@1047
   547
    int srcskip = info->s_skip;
slouken@1895
   548
    Uint8 *dst = (Uint8 *) info->d_pixels;
icculus@1047
   549
    int dstskip = info->d_skip;
icculus@1047
   550
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   551
icculus@1047
   552
    vector unsigned char v0 = vec_splat_u8(0);
icculus@1047
   553
    vector unsigned short v8_16 = vec_splat_u16(8);
icculus@1047
   554
    vector unsigned short v1_16 = vec_splat_u16(1);
icculus@1047
   555
    vector unsigned short v2_16 = vec_splat_u16(2);
icculus@1047
   556
    vector unsigned short v3_16 = vec_splat_u16(3);
icculus@1047
   557
    vector unsigned int v8_32 = vec_splat_u32(8);
icculus@1047
   558
    vector unsigned int v16_32 = vec_add(v8_32, v8_32);
slouken@1895
   559
    vector unsigned short v3f =
slouken@1895
   560
        VECUINT16_LITERAL(0x003f, 0x003f, 0x003f, 0x003f,
slouken@1895
   561
                          0x003f, 0x003f, 0x003f, 0x003f);
slouken@1895
   562
    vector unsigned short vfc =
slouken@1895
   563
        VECUINT16_LITERAL(0x00fc, 0x00fc, 0x00fc, 0x00fc,
slouken@1895
   564
                          0x00fc, 0x00fc, 0x00fc, 0x00fc);
icculus@1047
   565
icculus@1047
   566
    /* 
slouken@1895
   567
       0x10 - 0x1f is the alpha
slouken@1895
   568
       0x00 - 0x0e evens are the red
slouken@1895
   569
       0x01 - 0x0f odds are zero
slouken@1895
   570
     */
slouken@1895
   571
    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
slouken@1895
   572
                                                       0x10, 0x02, 0x01, 0x01,
slouken@1895
   573
                                                       0x10, 0x04, 0x01, 0x01,
slouken@1895
   574
                                                       0x10, 0x06, 0x01,
slouken@1895
   575
                                                       0x01);
slouken@1895
   576
    vector unsigned char vredalpha2 =
slouken@1895
   577
        (vector unsigned char) (vec_add((vector unsigned int) vredalpha1,
slouken@1895
   578
                                        vec_sl(v8_32, v16_32))
slouken@1895
   579
        );
icculus@1047
   580
    /*
slouken@1895
   581
       0x00 - 0x0f is ARxx ARxx ARxx ARxx
slouken@1895
   582
       0x11 - 0x0f odds are blue
slouken@1895
   583
     */
slouken@1895
   584
    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
slouken@1895
   585
                                                   0x04, 0x05, 0x06, 0x13,
slouken@1895
   586
                                                   0x08, 0x09, 0x0a, 0x15,
slouken@1895
   587
                                                   0x0c, 0x0d, 0x0e, 0x17);
slouken@1895
   588
    vector unsigned char vblue2 =
slouken@1895
   589
        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8_32)
slouken@1895
   590
        );
icculus@1047
   591
    /*
slouken@1895
   592
       0x00 - 0x0f is ARxB ARxB ARxB ARxB
slouken@1895
   593
       0x10 - 0x0e evens are green
slouken@1895
   594
     */
slouken@1895
   595
    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
slouken@1895
   596
                                                    0x04, 0x05, 0x12, 0x07,
slouken@1895
   597
                                                    0x08, 0x09, 0x14, 0x0b,
slouken@1895
   598
                                                    0x0c, 0x0d, 0x16, 0x0f);
slouken@1895
   599
    vector unsigned char vgreen2 =
slouken@1895
   600
        (vector unsigned
slouken@1895
   601
         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8_32, v8_32))
slouken@1895
   602
        );
slouken@1895
   603
    vector unsigned char vgmerge = VECUINT8_LITERAL(0x00, 0x02, 0x00, 0x06,
slouken@1895
   604
                                                    0x00, 0x0a, 0x00, 0x0e,
slouken@1895
   605
                                                    0x00, 0x12, 0x00, 0x16,
slouken@1895
   606
                                                    0x00, 0x1a, 0x00, 0x1e);
icculus@1047
   607
    vector unsigned char mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   608
    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
slouken@1895
   609
    vector unsigned char valphaPermute =
slouken@1895
   610
        vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
icculus@1047
   611
slouken@1895
   612
    vector unsigned short vf800 = (vector unsigned short) vec_splat_u8(-7);
icculus@1047
   613
    vf800 = vec_sl(vf800, vec_splat_u16(8));
icculus@1047
   614
slouken@1895
   615
    while (height--) {
icculus@1047
   616
        int extrawidth;
icculus@1047
   617
        vector unsigned char valigner;
icculus@1047
   618
        vector unsigned char vsrc;
icculus@1047
   619
        vector unsigned char voverflow;
icculus@1047
   620
        int width = info->d_width;
icculus@1047
   621
icculus@1047
   622
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   623
        while (condition) { \
icculus@1162
   624
            Uint32 Pixel; \
icculus@1047
   625
            unsigned sR, sG, sB, dR, dG, dB, sA; \
icculus@1162
   626
            DISEMBLE_RGBA(src, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
   627
            if(sA) { \
icculus@1047
   628
                unsigned short dstpixel = *((unsigned short *)dst); \
icculus@1047
   629
                dR = (dstpixel >> 8) & 0xf8; \
icculus@1047
   630
                dG = (dstpixel >> 3) & 0xfc; \
icculus@1047
   631
                dB = (dstpixel << 3) & 0xf8; \
icculus@1047
   632
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   633
                *((unsigned short *)dst) = ( \
icculus@1047
   634
                    ((dR & 0xf8) << 8) | ((dG & 0xfc) << 3) | (dB >> 3) \
icculus@1047
   635
                ); \
icculus@1047
   636
            } \
icculus@1047
   637
            src += 4; \
icculus@1047
   638
            dst += 2; \
icculus@1047
   639
            widthvar--; \
icculus@1047
   640
        }
icculus@1047
   641
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dst)) && (width), width);
icculus@1047
   642
        extrawidth = (width % 8);
icculus@1047
   643
        valigner = VEC_ALIGNER(src);
slouken@1895
   644
        vsrc = (vector unsigned char) vec_ld(0, src);
icculus@1047
   645
        width -= extrawidth;
icculus@1047
   646
        while (width) {
icculus@1047
   647
            vector unsigned char valpha;
icculus@1047
   648
            vector unsigned char vsrc1, vsrc2;
icculus@1047
   649
            vector unsigned char vdst1, vdst2;
icculus@1047
   650
            vector unsigned short vR, vG, vB;
icculus@1047
   651
            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
icculus@1047
   652
icculus@1047
   653
            /* Load 8 pixels from src as ARGB */
slouken@1895
   654
            voverflow = (vector unsigned char) vec_ld(15, src);
icculus@1047
   655
            vsrc = vec_perm(vsrc, voverflow, valigner);
icculus@1047
   656
            vsrc1 = vec_perm(vsrc, vsrc, vpermute);
icculus@1047
   657
            src += 16;
slouken@1895
   658
            vsrc = (vector unsigned char) vec_ld(15, src);
icculus@1047
   659
            voverflow = vec_perm(voverflow, vsrc, valigner);
icculus@1047
   660
            vsrc2 = vec_perm(voverflow, voverflow, vpermute);
icculus@1047
   661
            src += 16;
icculus@1047
   662
icculus@1047
   663
            /* Load 8 pixels from dst as XRGB */
icculus@1047
   664
            voverflow = vec_ld(0, dst);
slouken@1895
   665
            vR = vec_and((vector unsigned short) voverflow, vf800);
slouken@1895
   666
            vB = vec_sl((vector unsigned short) voverflow, v3_16);
icculus@1047
   667
            vG = vec_sl(vB, v2_16);
slouken@1895
   668
            vdst1 =
slouken@1895
   669
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@1895
   670
                                                (vector unsigned char) vR,
slouken@1895
   671
                                                vredalpha1);
slouken@1895
   672
            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
slouken@1895
   673
            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
slouken@1895
   674
            vdst2 =
slouken@1895
   675
                (vector unsigned char) vec_perm((vector unsigned char) vR,
slouken@1895
   676
                                                (vector unsigned char) vR,
slouken@1895
   677
                                                vredalpha2);
slouken@1895
   678
            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
slouken@1895
   679
            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
icculus@1047
   680
icculus@1047
   681
            /* Alpha blend 8 pixels as ARGB */
icculus@1047
   682
            valpha = vec_perm(vsrc1, v0, valphaPermute);
slouken@1895
   683
            VEC_MULTIPLY_ALPHA(vsrc1, vdst1, valpha, mergePermute, v1_16,
slouken@1895
   684
                               v8_16);
icculus@1047
   685
            valpha = vec_perm(vsrc2, v0, valphaPermute);
slouken@1895
   686
            VEC_MULTIPLY_ALPHA(vsrc2, vdst2, valpha, mergePermute, v1_16,
slouken@1895
   687
                               v8_16);
icculus@1047
   688
icculus@1047
   689
            /* Convert 8 pixels to 565 */
slouken@1895
   690
            vpixel = (vector unsigned short) vec_packpx((vector unsigned int)
slouken@1895
   691
                                                        vdst1,
slouken@1895
   692
                                                        (vector unsigned int)
slouken@1895
   693
                                                        vdst2);
slouken@1895
   694
            vgpixel = (vector unsigned short) vec_perm(vdst1, vdst2, vgmerge);
icculus@1047
   695
            vgpixel = vec_and(vgpixel, vfc);
icculus@1047
   696
            vgpixel = vec_sl(vgpixel, v3_16);
icculus@1047
   697
            vrpixel = vec_sl(vpixel, v1_16);
icculus@1047
   698
            vrpixel = vec_and(vrpixel, vf800);
icculus@1047
   699
            vbpixel = vec_and(vpixel, v3f);
slouken@1895
   700
            vdst1 =
slouken@1895
   701
                vec_or((vector unsigned char) vrpixel,
slouken@1895
   702
                       (vector unsigned char) vgpixel);
slouken@1895
   703
            vdst1 = vec_or(vdst1, (vector unsigned char) vbpixel);
slouken@1895
   704
icculus@1047
   705
            /* Store 8 pixels */
icculus@1047
   706
            vec_st(vdst1, 0, dst);
icculus@1047
   707
icculus@1047
   708
            width -= 8;
icculus@1047
   709
            dst += 16;
icculus@1047
   710
        }
icculus@1047
   711
        ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   712
#undef ONE_PIXEL_BLEND
icculus@1047
   713
        src += srcskip;
icculus@1047
   714
        dst += dstskip;
icculus@1047
   715
    }
icculus@1047
   716
}
icculus@1047
   717
slouken@1895
   718
static void
slouken@1895
   719
Blit32to32SurfaceAlphaKeyAltivec(SDL_BlitInfo * info)
icculus@1047
   720
{
icculus@1047
   721
    unsigned alpha = info->src->alpha;
icculus@1047
   722
    int height = info->d_height;
slouken@1895
   723
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
   724
    int srcskip = info->s_skip >> 2;
slouken@1895
   725
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
   726
    int dstskip = info->d_skip >> 2;
icculus@1047
   727
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   728
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
   729
    unsigned sA = srcfmt->alpha;
icculus@1047
   730
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
   731
    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
icculus@1047
   732
    Uint32 ckey = info->src->colorkey;
icculus@1047
   733
    vector unsigned char mergePermute;
icculus@1047
   734
    vector unsigned char vsrcPermute;
icculus@1047
   735
    vector unsigned char vdstPermute;
icculus@1047
   736
    vector unsigned char vsdstPermute;
icculus@1047
   737
    vector unsigned char valpha;
icculus@1047
   738
    vector unsigned char valphamask;
icculus@1047
   739
    vector unsigned char vbits;
icculus@1047
   740
    vector unsigned char v0;
icculus@1047
   741
    vector unsigned short v1;
icculus@1047
   742
    vector unsigned short v8;
icculus@1047
   743
    vector unsigned int vckey;
icculus@1047
   744
    vector unsigned int vrgbmask;
icculus@1047
   745
icculus@1047
   746
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   747
    v0 = vec_splat_u8(0);
icculus@1047
   748
    v1 = vec_splat_u16(1);
icculus@1047
   749
    v8 = vec_splat_u16(8);
icculus@1047
   750
icculus@1047
   751
    /* set the alpha to 255 on the destination surf */
icculus@1047
   752
    valphamask = VEC_ALPHA_MASK();
icculus@1047
   753
icculus@1047
   754
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   755
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
   756
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
   757
icculus@1047
   758
    /* set a vector full of alpha and 255-alpha */
slouken@1895
   759
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
   760
    valpha = vec_splat(valpha, 0);
slouken@1895
   761
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
   762
icculus@1047
   763
    ckey &= rgbmask;
slouken@1895
   764
    ((unsigned int *) (char *) &vckey)[0] = ckey;
icculus@1047
   765
    vckey = vec_splat(vckey, 0);
slouken@1895
   766
    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
icculus@1047
   767
    vrgbmask = vec_splat(vrgbmask, 0);
icculus@1047
   768
slouken@1895
   769
    while (height--) {
icculus@1047
   770
        int width = info->d_width;
icculus@1047
   771
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   772
        while (condition) { \
icculus@1162
   773
            Uint32 Pixel; \
icculus@1047
   774
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
   775
            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
icculus@1162
   776
            if(sA && Pixel != ckey) { \
icculus@1162
   777
                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
icculus@1162
   778
                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
   779
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   780
                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
   781
            } \
icculus@1162
   782
            dstp++; \
icculus@1162
   783
            srcp++; \
icculus@1047
   784
            widthvar--; \
icculus@1047
   785
        }
icculus@1047
   786
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   787
        if (width > 0) {
icculus@1047
   788
            int extrawidth = (width % 4);
icculus@1047
   789
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
   790
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
   791
            width -= extrawidth;
icculus@1047
   792
            while (width) {
icculus@1047
   793
                vector unsigned char vsel;
icculus@1047
   794
                vector unsigned char voverflow;
icculus@1047
   795
                vector unsigned char vd;
icculus@1047
   796
                vector unsigned char vd_orig;
icculus@1047
   797
icculus@1047
   798
                /* s = *srcp */
slouken@1895
   799
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
   800
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
   801
icculus@1047
   802
                /* vsel is set for items that match the key */
slouken@1895
   803
                vsel =
slouken@1895
   804
                    (vector unsigned char) vec_and((vector unsigned int) vs,
slouken@1895
   805
                                                   vrgbmask);
slouken@1895
   806
                vsel = (vector unsigned char) vec_cmpeq((vector unsigned int)
slouken@1895
   807
                                                        vsel, vckey);
icculus@1047
   808
icculus@1047
   809
                /* permute to source format */
icculus@1047
   810
                vs = vec_perm(vs, valpha, vsrcPermute);
icculus@1047
   811
icculus@1047
   812
                /* d = *dstp */
slouken@1895
   813
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
   814
                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
   815
icculus@1047
   816
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
   817
icculus@1047
   818
                /* set the alpha channel to full on */
icculus@1047
   819
                vd = vec_or(vd, valphamask);
icculus@1047
   820
icculus@1047
   821
                /* mask out color key */
icculus@1047
   822
                vd = vec_sel(vd, vd_orig, vsel);
slouken@1895
   823
icculus@1047
   824
                /* permute to dest format */
icculus@1047
   825
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
   826
icculus@1047
   827
                /* *dstp = res */
slouken@1895
   828
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
   829
icculus@1047
   830
                srcp += 4;
icculus@1047
   831
                dstp += 4;
icculus@1047
   832
                width -= 4;
icculus@1047
   833
                vs = voverflow;
icculus@1047
   834
            }
icculus@1047
   835
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   836
        }
icculus@1047
   837
#undef ONE_PIXEL_BLEND
slouken@1895
   838
icculus@1047
   839
        srcp += srcskip;
icculus@1047
   840
        dstp += dstskip;
icculus@1047
   841
    }
icculus@1047
   842
}
icculus@1047
   843
icculus@1047
   844
slouken@1895
   845
static void
slouken@1895
   846
Blit32to32PixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   847
{
icculus@1047
   848
    int width = info->d_width;
icculus@1047
   849
    int height = info->d_height;
slouken@1895
   850
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
   851
    int srcskip = info->s_skip >> 2;
slouken@1895
   852
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
   853
    int dstskip = info->d_skip >> 2;
icculus@1047
   854
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   855
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
   856
    vector unsigned char mergePermute;
icculus@1047
   857
    vector unsigned char valphaPermute;
icculus@1047
   858
    vector unsigned char vsrcPermute;
icculus@1047
   859
    vector unsigned char vdstPermute;
icculus@1047
   860
    vector unsigned char vsdstPermute;
icculus@1047
   861
    vector unsigned char valphamask;
icculus@1047
   862
    vector unsigned char vpixelmask;
icculus@1047
   863
    vector unsigned char v0;
icculus@1047
   864
    vector unsigned short v1;
icculus@1047
   865
    vector unsigned short v8;
icculus@1047
   866
icculus@1047
   867
    v0 = vec_splat_u8(0);
icculus@1047
   868
    v1 = vec_splat_u16(1);
icculus@1047
   869
    v8 = vec_splat_u16(8);
icculus@1047
   870
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   871
    valphamask = VEC_ALPHA_MASK();
slouken@1895
   872
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
icculus@1047
   873
    vpixelmask = vec_nor(valphamask, v0);
icculus@1047
   874
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   875
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
   876
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
   877
slouken@1895
   878
    while (height--) {
icculus@1047
   879
        width = info->d_width;
icculus@1047
   880
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
   881
            Uint32 Pixel; \
icculus@1047
   882
            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
icculus@1162
   883
            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
   884
            if(sA) { \
icculus@1162
   885
              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
icculus@1047
   886
              ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   887
              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
   888
            } \
icculus@1047
   889
            ++srcp; \
icculus@1047
   890
            ++dstp; \
icculus@1047
   891
            widthvar--; \
icculus@1047
   892
        }
icculus@1047
   893
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   894
        if (width > 0) {
slouken@1487
   895
            /* vsrcPermute */
slouken@1487
   896
            /* vdstPermute */
icculus@1047
   897
            int extrawidth = (width % 4);
icculus@1047
   898
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
   899
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
   900
            width -= extrawidth;
icculus@1047
   901
            while (width) {
icculus@1047
   902
                vector unsigned char voverflow;
icculus@1047
   903
                vector unsigned char vd;
icculus@1047
   904
                vector unsigned char valpha;
icculus@1047
   905
                vector unsigned char vdstalpha;
icculus@1047
   906
                /* s = *srcp */
slouken@1895
   907
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
   908
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
   909
                vs = vec_perm(vs, v0, vsrcPermute);
icculus@1047
   910
icculus@1047
   911
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
   912
icculus@1047
   913
                /* d = *dstp */
slouken@1895
   914
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
   915
                vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
   916
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
   917
icculus@1047
   918
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
   919
icculus@1047
   920
                /* set the alpha to the dest alpha */
icculus@1047
   921
                vd = vec_and(vd, vpixelmask);
icculus@1047
   922
                vd = vec_or(vd, vdstalpha);
icculus@1047
   923
                vd = vec_perm(vd, v0, vdstPermute);
icculus@1047
   924
icculus@1047
   925
                /* *dstp = res */
slouken@1895
   926
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
   927
icculus@1047
   928
                srcp += 4;
icculus@1047
   929
                dstp += 4;
icculus@1047
   930
                width -= 4;
icculus@1047
   931
                vs = voverflow;
icculus@1047
   932
icculus@1047
   933
            }
icculus@1047
   934
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   935
        }
slouken@1895
   936
        srcp += srcskip;
slouken@1895
   937
        dstp += dstskip;
icculus@1047
   938
#undef ONE_PIXEL_BLEND
slouken@1895
   939
    }
icculus@1047
   940
}
icculus@1047
   941
icculus@1047
   942
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   943
static void
slouken@1895
   944
BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   945
{
slouken@1895
   946
    int width = info->d_width;
slouken@1895
   947
    int height = info->d_height;
slouken@1895
   948
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
   949
    int srcskip = info->s_skip >> 2;
slouken@1895
   950
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
   951
    int dstskip = info->d_skip >> 2;
icculus@1047
   952
    vector unsigned char mergePermute;
icculus@1047
   953
    vector unsigned char valphaPermute;
icculus@1047
   954
    vector unsigned char valphamask;
icculus@1047
   955
    vector unsigned char vpixelmask;
icculus@1047
   956
    vector unsigned char v0;
icculus@1047
   957
    vector unsigned short v1;
icculus@1047
   958
    vector unsigned short v8;
icculus@1047
   959
    v0 = vec_splat_u8(0);
icculus@1047
   960
    v1 = vec_splat_u16(1);
icculus@1047
   961
    v8 = vec_splat_u16(8);
icculus@1047
   962
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   963
    valphamask = VEC_ALPHA_MASK();
slouken@1895
   964
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@1895
   965
slouken@1895
   966
icculus@1047
   967
    vpixelmask = vec_nor(valphamask, v0);
slouken@1895
   968
    while (height--) {
icculus@1047
   969
        width = info->d_width;
icculus@1047
   970
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   971
        while ((condition)) { \
icculus@1047
   972
            Uint32 dalpha; \
icculus@1047
   973
            Uint32 d; \
icculus@1047
   974
            Uint32 s1; \
icculus@1047
   975
            Uint32 d1; \
icculus@1047
   976
            Uint32 s = *srcp; \
icculus@1047
   977
            Uint32 alpha = s >> 24; \
icculus@1047
   978
            if(alpha) { \
icculus@1047
   979
              if(alpha == SDL_ALPHA_OPAQUE) { \
icculus@1047
   980
                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
icculus@1047
   981
              } else { \
icculus@1047
   982
                d = *dstp; \
icculus@1047
   983
                dalpha = d & 0xff000000; \
icculus@1047
   984
                s1 = s & 0xff00ff; \
icculus@1047
   985
                d1 = d & 0xff00ff; \
icculus@1047
   986
                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
icculus@1047
   987
                s &= 0xff00; \
icculus@1047
   988
                d &= 0xff00; \
icculus@1047
   989
                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
   990
                *dstp = d1 | d | dalpha; \
icculus@1047
   991
              } \
icculus@1047
   992
            } \
icculus@1047
   993
            ++srcp; \
icculus@1047
   994
            ++dstp; \
icculus@1047
   995
            widthvar--; \
icculus@1047
   996
	    }
icculus@1047
   997
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   998
        if (width > 0) {
icculus@1047
   999
            int extrawidth = (width % 4);
icculus@1047
  1000
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1001
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1002
            width -= extrawidth;
icculus@1047
  1003
            while (width) {
icculus@1047
  1004
                vector unsigned char voverflow;
icculus@1047
  1005
                vector unsigned char vd;
icculus@1047
  1006
                vector unsigned char valpha;
icculus@1047
  1007
                vector unsigned char vdstalpha;
icculus@1047
  1008
                /* s = *srcp */
slouken@1895
  1009
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1010
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1011
icculus@1047
  1012
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
  1013
icculus@1047
  1014
                /* d = *dstp */
slouken@1895
  1015
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1016
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1017
icculus@1047
  1018
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1019
icculus@1047
  1020
                /* set the alpha to the dest alpha */
icculus@1047
  1021
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1022
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1023
icculus@1047
  1024
                /* *dstp = res */
slouken@1895
  1025
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1026
icculus@1047
  1027
                srcp += 4;
icculus@1047
  1028
                dstp += 4;
icculus@1047
  1029
                width -= 4;
icculus@1047
  1030
                vs = voverflow;
icculus@1047
  1031
            }
icculus@1047
  1032
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1033
        }
slouken@1895
  1034
        srcp += srcskip;
slouken@1895
  1035
        dstp += dstskip;
slouken@1895
  1036
    }
icculus@1047
  1037
#undef ONE_PIXEL_BLEND
icculus@1047
  1038
}
icculus@1047
  1039
slouken@1895
  1040
static void
slouken@1895
  1041
Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1042
{
icculus@1047
  1043
    /* XXX : 6 */
slouken@1895
  1044
    unsigned alpha = info->src->alpha;
icculus@1047
  1045
    int height = info->d_height;
slouken@1895
  1046
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
  1047
    int srcskip = info->s_skip >> 2;
slouken@1895
  1048
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
  1049
    int dstskip = info->d_skip >> 2;
icculus@1047
  1050
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
  1051
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  1052
    unsigned sA = srcfmt->alpha;
slouken@1895
  1053
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
  1054
    vector unsigned char mergePermute;
icculus@1047
  1055
    vector unsigned char vsrcPermute;
icculus@1047
  1056
    vector unsigned char vdstPermute;
icculus@1047
  1057
    vector unsigned char vsdstPermute;
icculus@1047
  1058
    vector unsigned char valpha;
icculus@1047
  1059
    vector unsigned char valphamask;
icculus@1047
  1060
    vector unsigned char vbits;
icculus@1047
  1061
    vector unsigned short v1;
icculus@1047
  1062
    vector unsigned short v8;
icculus@1047
  1063
icculus@1047
  1064
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1065
    v1 = vec_splat_u16(1);
icculus@1047
  1066
    v8 = vec_splat_u16(8);
icculus@1047
  1067
icculus@1047
  1068
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1069
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1070
icculus@1047
  1071
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1072
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1073
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1074
icculus@1047
  1075
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1076
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1077
    valpha = vec_splat(valpha, 0);
slouken@1895
  1078
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
  1079
slouken@1895
  1080
    while (height--) {
icculus@1047
  1081
        int width = info->d_width;
icculus@1047
  1082
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1083
            Uint32 Pixel; \
icculus@1047
  1084
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
  1085
            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
icculus@1162
  1086
            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
  1087
            ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1088
            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1089
            ++srcp; \
icculus@1047
  1090
            ++dstp; \
icculus@1047
  1091
            widthvar--; \
icculus@1047
  1092
        }
icculus@1047
  1093
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1094
        if (width > 0) {
icculus@1047
  1095
            int extrawidth = (width % 4);
icculus@2086
  1096
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1097
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1098
            width -= extrawidth;
icculus@1047
  1099
            while (width) {
icculus@1047
  1100
                vector unsigned char voverflow;
icculus@1047
  1101
                vector unsigned char vd;
icculus@1047
  1102
icculus@1047
  1103
                /* s = *srcp */
slouken@1895
  1104
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1105
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1106
                vs = vec_perm(vs, valpha, vsrcPermute);
slouken@1895
  1107
icculus@1047
  1108
                /* d = *dstp */
slouken@1895
  1109
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1110
                vd = vec_perm(vd, vd, vsdstPermute);
icculus@1047
  1111
icculus@1047
  1112
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1113
icculus@1047
  1114
                /* set the alpha channel to full on */
icculus@1047
  1115
                vd = vec_or(vd, valphamask);
icculus@1047
  1116
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1117
icculus@1047
  1118
                /* *dstp = res */
slouken@1895
  1119
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1120
icculus@1047
  1121
                srcp += 4;
icculus@1047
  1122
                dstp += 4;
icculus@1047
  1123
                width -= 4;
icculus@1047
  1124
                vs = voverflow;
icculus@1047
  1125
            }
icculus@1047
  1126
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1127
        }
icculus@1047
  1128
#undef ONE_PIXEL_BLEND
slouken@1895
  1129
icculus@1047
  1130
        srcp += srcskip;
icculus@1047
  1131
        dstp += dstskip;
icculus@1047
  1132
    }
icculus@1047
  1133
icculus@1047
  1134
}
icculus@1047
  1135
icculus@1047
  1136
icculus@1047
  1137
/* fast RGB888->(A)RGB888 blending */
slouken@1895
  1138
static void
slouken@1895
  1139
BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1140
{
slouken@1895
  1141
    unsigned alpha = info->src->alpha;
icculus@1047
  1142
    int height = info->d_height;
slouken@1895
  1143
    Uint32 *srcp = (Uint32 *) info->s_pixels;
icculus@1047
  1144
    int srcskip = info->s_skip >> 2;
slouken@1895
  1145
    Uint32 *dstp = (Uint32 *) info->d_pixels;
icculus@1047
  1146
    int dstskip = info->d_skip >> 2;
icculus@1047
  1147
    vector unsigned char mergePermute;
icculus@1047
  1148
    vector unsigned char valpha;
icculus@1047
  1149
    vector unsigned char valphamask;
icculus@1047
  1150
    vector unsigned short v1;
icculus@1047
  1151
    vector unsigned short v8;
icculus@1047
  1152
icculus@1047
  1153
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1154
    v1 = vec_splat_u16(1);
icculus@1047
  1155
    v8 = vec_splat_u16(8);
icculus@1047
  1156
icculus@1047
  1157
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1158
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1159
icculus@1047
  1160
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1161
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1162
    valpha = vec_splat(valpha, 0);
icculus@1047
  1163
slouken@1895
  1164
    while (height--) {
icculus@1047
  1165
        int width = info->d_width;
icculus@1047
  1166
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1047
  1167
            Uint32 s = *srcp; \
icculus@1047
  1168
            Uint32 d = *dstp; \
icculus@1047
  1169
            Uint32 s1 = s & 0xff00ff; \
icculus@1047
  1170
            Uint32 d1 = d & 0xff00ff; \
icculus@1047
  1171
            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
icculus@1047
  1172
                 & 0xff00ff; \
icculus@1047
  1173
            s &= 0xff00; \
icculus@1047
  1174
            d &= 0xff00; \
icculus@1047
  1175
            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1176
            *dstp = d1 | d | 0xff000000; \
icculus@1047
  1177
            ++srcp; \
icculus@1047
  1178
            ++dstp; \
icculus@1047
  1179
            widthvar--; \
icculus@1047
  1180
        }
icculus@1047
  1181
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1182
        if (width > 0) {
icculus@1047
  1183
            int extrawidth = (width % 4);
icculus@1047
  1184
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1185
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1186
            width -= extrawidth;
icculus@1047
  1187
            while (width) {
icculus@1047
  1188
                vector unsigned char voverflow;
icculus@1047
  1189
                vector unsigned char vd;
icculus@1047
  1190
icculus@1047
  1191
                /* s = *srcp */
slouken@1895
  1192
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1193
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
  1194
icculus@1047
  1195
                /* d = *dstp */
slouken@1895
  1196
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1197
icculus@1047
  1198
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1199
icculus@1047
  1200
                /* set the alpha channel to full on */
icculus@1047
  1201
                vd = vec_or(vd, valphamask);
icculus@1047
  1202
icculus@1047
  1203
                /* *dstp = res */
slouken@1895
  1204
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1205
icculus@1047
  1206
                srcp += 4;
icculus@1047
  1207
                dstp += 4;
icculus@1047
  1208
                width -= 4;
icculus@1047
  1209
                vs = voverflow;
icculus@1047
  1210
            }
icculus@1047
  1211
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1212
        }
icculus@1047
  1213
#undef ONE_PIXEL_BLEND
slouken@1895
  1214
icculus@1047
  1215
        srcp += srcskip;
icculus@1047
  1216
        dstp += dstskip;
icculus@1047
  1217
    }
icculus@1047
  1218
}
slouken@1895
  1219
slouken@1795
  1220
#if __MWERKS__
slouken@1795
  1221
#pragma altivec_model off
slouken@1795
  1222
#endif
slouken@1361
  1223
#endif /* SDL_ALTIVEC_BLITTERS */
icculus@1047
  1224
slouken@1
  1225
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
  1226
static void
slouken@1895
  1227
BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
slouken@0
  1228
{
slouken@1895
  1229
    int width = info->d_width;
slouken@1895
  1230
    int height = info->d_height;
slouken@1895
  1231
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1232
    int srcskip = info->s_skip >> 2;
slouken@1895
  1233
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1234
    int dstskip = info->d_skip >> 2;
slouken@0
  1235
slouken@1895
  1236
    while (height--) {
slouken@1895
  1237
	    /* *INDENT-OFF* */
slouken@0
  1238
	    DUFFS_LOOP4({
slouken@1
  1239
		    Uint32 s = *srcp++;
slouken@1
  1240
		    Uint32 d = *dstp;
slouken@1
  1241
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1242
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1243
	    }, width);
slouken@1895
  1244
	    /* *INDENT-ON* */
slouken@1895
  1245
        srcp += srcskip;
slouken@1895
  1246
        dstp += dstskip;
slouken@1895
  1247
    }
slouken@0
  1248
}
slouken@0
  1249
slouken@1
  1250
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
  1251
static void
slouken@1895
  1252
BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1253
{
slouken@1895
  1254
    unsigned alpha = info->src->alpha;
slouken@1895
  1255
    if (alpha == 128) {
slouken@1895
  1256
        BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1895
  1257
    } else {
slouken@1895
  1258
        int width = info->d_width;
slouken@1895
  1259
        int height = info->d_height;
slouken@1895
  1260
        Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1261
        int srcskip = info->s_skip >> 2;
slouken@1895
  1262
        Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1263
        int dstskip = info->d_skip >> 2;
slouken@1895
  1264
        Uint32 s;
slouken@1895
  1265
        Uint32 d;
slouken@1895
  1266
        Uint32 s1;
slouken@1895
  1267
        Uint32 d1;
slouken@1
  1268
slouken@1895
  1269
        while (height--) {
slouken@1895
  1270
			/* *INDENT-OFF* */
slouken@689
  1271
			DUFFS_LOOP_DOUBLE2({
slouken@689
  1272
				/* One Pixel Blend */
slouken@1
  1273
				s = *srcp;
slouken@1
  1274
				d = *dstp;
slouken@1
  1275
				s1 = s & 0xff00ff;
slouken@1
  1276
				d1 = d & 0xff00ff;
slouken@1
  1277
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1278
				     & 0xff00ff;
slouken@1
  1279
				s &= 0xff00;
slouken@1
  1280
				d &= 0xff00;
slouken@1
  1281
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1282
				*dstp = d1 | d | 0xff000000;
slouken@1
  1283
				++srcp;
slouken@1
  1284
				++dstp;
slouken@689
  1285
			},{
slouken@689
  1286
			        /* Two Pixels Blend */
slouken@689
  1287
				s = *srcp;
slouken@689
  1288
				d = *dstp;
slouken@689
  1289
				s1 = s & 0xff00ff;
slouken@689
  1290
				d1 = d & 0xff00ff;
slouken@689
  1291
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1292
				d1 &= 0xff00ff;
slouken@689
  1293
				     
slouken@689
  1294
				s = ((s & 0xff00) >> 8) | 
slouken@689
  1295
					((srcp[1] & 0xff00) << 8);
slouken@689
  1296
				d = ((d & 0xff00) >> 8) |
slouken@689
  1297
					((dstp[1] & 0xff00) << 8);
slouken@689
  1298
				d += (s - d) * alpha >> 8;
slouken@689
  1299
				d &= 0x00ff00ff;
slouken@689
  1300
				
slouken@689
  1301
				*dstp++ = d1 | ((d << 8) & 0xff00) | 0xff000000;
slouken@689
  1302
				++srcp;
slouken@689
  1303
				
slouken@689
  1304
			        s1 = *srcp;
slouken@689
  1305
				d1 = *dstp;
slouken@689
  1306
				s1 &= 0xff00ff;
slouken@689
  1307
				d1 &= 0xff00ff;
slouken@689
  1308
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1309
				d1 &= 0xff00ff;
slouken@689
  1310
				
slouken@689
  1311
				*dstp = d1 | ((d >> 8) & 0xff00) | 0xff000000;
slouken@689
  1312
				++srcp;
slouken@689
  1313
				++dstp;
slouken@1
  1314
			}, width);
slouken@1895
  1315
			/* *INDENT-ON* */
slouken@1895
  1316
            srcp += srcskip;
slouken@1895
  1317
            dstp += dstskip;
slouken@1895
  1318
        }
slouken@1895
  1319
    }
slouken@1
  1320
}
slouken@1
  1321
slouken@0
  1322
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1323
static void
slouken@1895
  1324
BlitRGBtoRGBPixelAlpha(SDL_BlitInfo * info)
slouken@0
  1325
{
slouken@1895
  1326
    int width = info->d_width;
slouken@1895
  1327
    int height = info->d_height;
slouken@1895
  1328
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1329
    int srcskip = info->s_skip >> 2;
slouken@1895
  1330
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1331
    int dstskip = info->d_skip >> 2;
slouken@0
  1332
slouken@1895
  1333
    while (height--) {
slouken@1895
  1334
	    /* *INDENT-OFF* */
slouken@0
  1335
	    DUFFS_LOOP4({
slouken@0
  1336
		Uint32 dalpha;
slouken@0
  1337
		Uint32 d;
slouken@0
  1338
		Uint32 s1;
slouken@0
  1339
		Uint32 d1;
slouken@0
  1340
		Uint32 s = *srcp;
slouken@0
  1341
		Uint32 alpha = s >> 24;
slouken@0
  1342
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1343
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1344
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1345
		   Benchmark this! */
slouken@689
  1346
		if(alpha) {   
slouken@689
  1347
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1348
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1349
		  } else {
slouken@0
  1350
		    /*
slouken@0
  1351
		     * take out the middle component (green), and process
slouken@0
  1352
		     * the other two in parallel. One multiply less.
slouken@0
  1353
		     */
slouken@0
  1354
		    d = *dstp;
slouken@0
  1355
		    dalpha = d & 0xff000000;
slouken@0
  1356
		    s1 = s & 0xff00ff;
slouken@0
  1357
		    d1 = d & 0xff00ff;
slouken@0
  1358
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1359
		    s &= 0xff00;
slouken@0
  1360
		    d &= 0xff00;
slouken@0
  1361
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1362
		    *dstp = d1 | d | dalpha;
slouken@689
  1363
		  }
slouken@0
  1364
		}
slouken@0
  1365
		++srcp;
slouken@0
  1366
		++dstp;
slouken@0
  1367
	    }, width);
slouken@1895
  1368
	    /* *INDENT-ON* */
slouken@1895
  1369
        srcp += srcskip;
slouken@1895
  1370
        dstp += dstskip;
slouken@1895
  1371
    }
slouken@0
  1372
}
slouken@0
  1373
slouken@2255
  1374
#ifdef __MMX__
slouken@1542
  1375
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1376
static void
slouken@1895
  1377
BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
slouken@1542
  1378
{
slouken@1895
  1379
    int width = info->d_width;
slouken@1895
  1380
    int height = info->d_height;
slouken@1895
  1381
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1382
    int srcskip = info->s_skip >> 2;
slouken@1895
  1383
    Uint32 *dstp = (Uint32 *) info->d_pixels;
slouken@1895
  1384
    int dstskip = info->d_skip >> 2;
slouken@1895
  1385
    SDL_PixelFormat *sf = info->src;
slouken@1895
  1386
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
  1387
    Uint32 amask = sf->Amask;
slouken@1895
  1388
    Uint32 ashift = sf->Ashift;
slouken@1895
  1389
    Uint64 multmask;
slouken@1542
  1390
slouken@1895
  1391
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
  1392
slouken@1895
  1393
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@2255
  1394
	multmask = 0xFFFF;
slouken@2255
  1395
    multmask <<= (ashift * 2);
slouken@2255
  1396
    multmask = ~multmask;
slouken@1895
  1397
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1895
  1398
slouken@1895
  1399
    while (height--) {
slouken@1895
  1400
	    /* *INDENT-OFF* */
slouken@1542
  1401
	    DUFFS_LOOP4({
slouken@1542
  1402
		Uint32 alpha;
slouken@1542
  1403
slouken@1542
  1404
		_m_prefetch(srcp + 16);
slouken@1542
  1405
		_m_prefetch(dstp + 16);
slouken@1542
  1406
slouken@1542
  1407
		alpha = *srcp & amask;
slouken@1542
  1408
		if (alpha == 0) {
slouken@1542
  1409
			/* do nothing */
slouken@1542
  1410
		} else if (alpha == amask) {
slouken@1542
  1411
			/* copy RGB, keep dst alpha */
slouken@1542
  1412
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
  1413
		} else {
slouken@1542
  1414
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
  1415
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
  1416
slouken@1542
  1417
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
  1418
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
  1419
slouken@1542
  1420
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
  1421
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
  1422
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  1423
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  1424
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
  1425
slouken@1542
  1426
			/* blend */		    
slouken@1542
  1427
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@1542
  1428
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@1542
  1429
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
  1430
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@1542
  1431
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
  1432
			
slouken@1542
  1433
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
  1434
		}
slouken@1542
  1435
		++srcp;
slouken@1542
  1436
		++dstp;
slouken@1542
  1437
	    }, width);
slouken@1895
  1438
	    /* *INDENT-ON* */
slouken@1895
  1439
        srcp += srcskip;
slouken@1895
  1440
        dstp += dstskip;
slouken@1895
  1441
    }
slouken@1895
  1442
    _mm_empty();
slouken@1542
  1443
}
slouken@1895
  1444
slouken@2255
  1445
#endif /* __MMX__ */
slouken@689
  1446
slouken@1
  1447
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1448
slouken@1
  1449
/* blend a single 16 bit pixel at 50% */
slouken@1
  1450
#define BLEND16_50(d, s, mask)						\
slouken@1
  1451
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1452
slouken@1
  1453
/* blend two 16 bit pixels at 50% */
slouken@1
  1454
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1455
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1456
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1457
slouken@1895
  1458
static void
slouken@1895
  1459
Blit16to16SurfaceAlpha128(SDL_BlitInfo * info, Uint16 mask)
slouken@0
  1460
{
slouken@1895
  1461
    int width = info->d_width;
slouken@1895
  1462
    int height = info->d_height;
slouken@1895
  1463
    Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1464
    int srcskip = info->s_skip >> 1;
slouken@1895
  1465
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1466
    int dstskip = info->d_skip >> 1;
slouken@0
  1467
slouken@1895
  1468
    while (height--) {
slouken@1895
  1469
        if (((uintptr_t) srcp ^ (uintptr_t) dstp) & 2) {
slouken@1895
  1470
            /*
slouken@1895
  1471
             * Source and destination not aligned, pipeline it.
slouken@1895
  1472
             * This is mostly a win for big blits but no loss for
slouken@1895
  1473
             * small ones
slouken@1895
  1474
             */
slouken@1895
  1475
            Uint32 prev_sw;
slouken@1895
  1476
            int w = width;
slouken@1
  1477
slouken@1895
  1478
            /* handle odd destination */
slouken@1895
  1479
            if ((uintptr_t) dstp & 2) {
slouken@1895
  1480
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1481
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1482
                dstp++;
slouken@1895
  1483
                srcp++;
slouken@1895
  1484
                w--;
slouken@1895
  1485
            }
slouken@1895
  1486
            srcp++;             /* srcp is now 32-bit aligned */
slouken@1
  1487
slouken@1895
  1488
            /* bootstrap pipeline with first halfword */
slouken@1895
  1489
            prev_sw = ((Uint32 *) srcp)[-1];
slouken@1
  1490
slouken@1895
  1491
            while (w > 1) {
slouken@1895
  1492
                Uint32 sw, dw, s;
slouken@1895
  1493
                sw = *(Uint32 *) srcp;
slouken@1895
  1494
                dw = *(Uint32 *) dstp;
slouken@1443
  1495
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1496
                s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1497
#else
slouken@1895
  1498
                s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1499
#endif
slouken@1895
  1500
                prev_sw = sw;
slouken@1895
  1501
                *(Uint32 *) dstp = BLEND2x16_50(dw, s, mask);
slouken@1895
  1502
                dstp += 2;
slouken@1895
  1503
                srcp += 2;
slouken@1895
  1504
                w -= 2;
slouken@1895
  1505
            }
slouken@1
  1506
slouken@1895
  1507
            /* final pixel if any */
slouken@1895
  1508
            if (w) {
slouken@1895
  1509
                Uint16 d = *dstp, s;
slouken@1443
  1510
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1511
                s = (Uint16) prev_sw;
slouken@1443
  1512
#else
slouken@1895
  1513
                s = (Uint16) (prev_sw >> 16);
slouken@1443
  1514
#endif
slouken@1895
  1515
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1516
                srcp++;
slouken@1895
  1517
                dstp++;
slouken@1895
  1518
            }
slouken@1895
  1519
            srcp += srcskip - 1;
slouken@1895
  1520
            dstp += dstskip;
slouken@1895
  1521
        } else {
slouken@1895
  1522
            /* source and destination are aligned */
slouken@1895
  1523
            int w = width;
slouken@1
  1524
slouken@1895
  1525
            /* first odd pixel? */
slouken@1895
  1526
            if ((uintptr_t) srcp & 2) {
slouken@1895
  1527
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1528
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1529
                srcp++;
slouken@1895
  1530
                dstp++;
slouken@1895
  1531
                w--;
slouken@1895
  1532
            }
slouken@1895
  1533
            /* srcp and dstp are now 32-bit aligned */
slouken@1
  1534
slouken@1895
  1535
            while (w > 1) {
slouken@1895
  1536
                Uint32 sw = *(Uint32 *) srcp;
slouken@1895
  1537
                Uint32 dw = *(Uint32 *) dstp;
slouken@1895
  1538
                *(Uint32 *) dstp = BLEND2x16_50(dw, sw, mask);
slouken@1895
  1539
                srcp += 2;
slouken@1895
  1540
                dstp += 2;
slouken@1895
  1541
                w -= 2;
slouken@1895
  1542
            }
slouken@1
  1543
slouken@1895
  1544
            /* last odd pixel? */
slouken@1895
  1545
            if (w) {
slouken@1895
  1546
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1547
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1548
                srcp++;
slouken@1895
  1549
                dstp++;
slouken@1895
  1550
            }
slouken@1895
  1551
            srcp += srcskip;
slouken@1895
  1552
            dstp += dstskip;
slouken@1895
  1553
        }
slouken@1895
  1554
    }
slouken@1
  1555
}
slouken@1
  1556
slouken@2255
  1557
#ifdef __MMX__
slouken@689
  1558
slouken@1542
  1559
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1560
static void
slouken@1895
  1561
Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1562
{
slouken@1895
  1563
    unsigned alpha = info->src->alpha;
slouken@1895
  1564
    if (alpha == 128) {
slouken@1895
  1565
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1566
    } else {
slouken@1895
  1567
        int width = info->d_width;
slouken@1895
  1568
        int height = info->d_height;
slouken@1895
  1569
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1570
        int srcskip = info->s_skip >> 1;
slouken@1895
  1571
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1572
        int dstskip = info->d_skip >> 1;
slouken@1895
  1573
        Uint32 s, d;
slouken@1542
  1574
slouken@1895
  1575
        __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1576
slouken@1895
  1577
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1578
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1579
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1895
  1580
slouken@1895
  1581
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1582
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1583
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1584
           to reduce the number of operations */
slouken@1895
  1585
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1586
slouken@1895
  1587
        /* Setup the 565 color channel masks */
slouken@1895
  1588
        gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0);   /* MASKGREEN -> gmask */
slouken@1895
  1589
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1590
slouken@1895
  1591
        while (height--) {
slouken@1895
  1592
			/* *INDENT-OFF* */
slouken@1542
  1593
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1594
			{
slouken@1542
  1595
				s = *srcp++;
slouken@1542
  1596
				d = *dstp;
slouken@1542
  1597
				/*
slouken@1542
  1598
				 * shift out the middle component (green) to
slouken@1542
  1599
				 * the high 16 bits, and process all three RGB
slouken@1542
  1600
				 * components at the same time.
slouken@1542
  1601
				 */
slouken@1542
  1602
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1603
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1604
				d += (s - d) * alpha >> 5;
slouken@1542
  1605
				d &= 0x07e0f81f;
slouken@1546
  1606
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1607
			},{
slouken@1542
  1608
				s = *srcp++;
slouken@1542
  1609
				d = *dstp;
slouken@1542
  1610
				/*
slouken@1542
  1611
				 * shift out the middle component (green) to
slouken@1542
  1612
				 * the high 16 bits, and process all three RGB
slouken@1542
  1613
				 * components at the same time.
slouken@1542
  1614
				 */
slouken@1542
  1615
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1616
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1617
				d += (s - d) * alpha >> 5;
slouken@1542
  1618
				d &= 0x07e0f81f;
slouken@1546
  1619
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1620
				s = *srcp++;
slouken@1542
  1621
				d = *dstp;
slouken@1542
  1622
				/*
slouken@1542
  1623
				 * shift out the middle component (green) to
slouken@1542
  1624
				 * the high 16 bits, and process all three RGB
slouken@1542
  1625
				 * components at the same time.
slouken@1542
  1626
				 */
slouken@1542
  1627
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1628
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1629
				d += (s - d) * alpha >> 5;
slouken@1542
  1630
				d &= 0x07e0f81f;
slouken@1546
  1631
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1632
			},{
slouken@1542
  1633
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1634
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1635
slouken@1542
  1636
				/* red */
slouken@1542
  1637
				src2 = src1;
slouken@1542
  1638
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  1639
slouken@1542
  1640
				dst2 = dst1;
slouken@1542
  1641
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  1642
slouken@1542
  1643
				/* blend */
slouken@1542
  1644
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1645
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1646
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1647
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1648
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  1649
slouken@1542
  1650
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1651
slouken@1542
  1652
				/* green -- process the bits in place */
slouken@1542
  1653
				src2 = src1;
slouken@1542
  1654
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1655
slouken@1542
  1656
				dst2 = dst1;
slouken@1542
  1657
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1658
slouken@1542
  1659
				/* blend */
slouken@1542
  1660
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1661
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1662
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1663
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1664
slouken@1542
  1665
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1666
slouken@1542
  1667
				/* blue */
slouken@1542
  1668
				src2 = src1;
slouken@1542
  1669
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1670
slouken@1542
  1671
				dst2 = dst1;
slouken@1542
  1672
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1673
slouken@1542
  1674
				/* blend */
slouken@1542
  1675
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1676
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1677
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1678
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1679
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1680
slouken@1542
  1681
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1682
slouken@1542
  1683
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1684
slouken@1542
  1685
				srcp += 4;
slouken@1542
  1686
				dstp += 4;
slouken@1895
  1687
			}, width);
slouken@1895
  1688
			/* *INDENT-ON* */
slouken@1895
  1689
            srcp += srcskip;
slouken@1895
  1690
            dstp += dstskip;
slouken@1895
  1691
        }
slouken@1895
  1692
        _mm_empty();
slouken@1895
  1693
    }
slouken@1542
  1694
}
slouken@1542
  1695
slouken@1542
  1696
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1697
static void
slouken@1895
  1698
Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1699
{
slouken@1895
  1700
    unsigned alpha = info->src->alpha;
slouken@1895
  1701
    if (alpha == 128) {
slouken@1895
  1702
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1703
    } else {
slouken@1895
  1704
        int width = info->d_width;
slouken@1895
  1705
        int height = info->d_height;
slouken@1895
  1706
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1707
        int srcskip = info->s_skip >> 1;
slouken@1895
  1708
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1709
        int dstskip = info->d_skip >> 1;
slouken@1895
  1710
        Uint32 s, d;
slouken@1542
  1711
slouken@1895
  1712
        __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1713
slouken@1895
  1714
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1715
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1716
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  1717
slouken@1895
  1718
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1719
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1720
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1721
           to reduce the number of operations */
slouken@1895
  1722
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1723
slouken@1895
  1724
        /* Setup the 555 color channel masks */
slouken@1895
  1725
        rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00);   /* MASKRED -> rmask */
slouken@1895
  1726
        gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0);   /* MASKGREEN -> gmask */
slouken@1895
  1727
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1728
slouken@1895
  1729
        while (height--) {
slouken@1895
  1730
			/* *INDENT-OFF* */
slouken@1542
  1731
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1732
			{
slouken@1542
  1733
				s = *srcp++;
slouken@1542
  1734
				d = *dstp;
slouken@1542
  1735
				/*
slouken@1542
  1736
				 * shift out the middle component (green) to
slouken@1542
  1737
				 * the high 16 bits, and process all three RGB
slouken@1542
  1738
				 * components at the same time.
slouken@1542
  1739
				 */
slouken@1542
  1740
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1741
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1742
				d += (s - d) * alpha >> 5;
slouken@1542
  1743
				d &= 0x03e07c1f;
slouken@1546
  1744
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1745
			},{
slouken@1542
  1746
				s = *srcp++;
slouken@1542
  1747
				d = *dstp;
slouken@1542
  1748
				/*
slouken@1542
  1749
				 * shift out the middle component (green) to
slouken@1542
  1750
				 * the high 16 bits, and process all three RGB
slouken@1542
  1751
				 * components at the same time.
slouken@1542
  1752
				 */
slouken@1542
  1753
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1754
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1755
				d += (s - d) * alpha >> 5;
slouken@1542
  1756
				d &= 0x03e07c1f;
slouken@1546
  1757
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1758
			        s = *srcp++;
slouken@1542
  1759
				d = *dstp;
slouken@1542
  1760
				/*
slouken@1542
  1761
				 * shift out the middle component (green) to
slouken@1542
  1762
				 * the high 16 bits, and process all three RGB
slouken@1542
  1763
				 * components at the same time.
slouken@1542
  1764
				 */
slouken@1542
  1765
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1766
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1767
				d += (s - d) * alpha >> 5;
slouken@1542
  1768
				d &= 0x03e07c1f;
slouken@1546
  1769
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1770
			},{
slouken@1542
  1771
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1772
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1773
slouken@1542
  1774
				/* red -- process the bits in place */
slouken@1542
  1775
				src2 = src1;
slouken@1542
  1776
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  1777
slouken@1542
  1778
				dst2 = dst1;
slouken@1542
  1779
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  1780
slouken@1542
  1781
				/* blend */
slouken@1542
  1782
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1783
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1784
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1785
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1786
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  1787
slouken@1542
  1788
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1789
				
slouken@1542
  1790
				/* green -- process the bits in place */
slouken@1542
  1791
				src2 = src1;
slouken@1542
  1792
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1793
slouken@1542
  1794
				dst2 = dst1;
slouken@1542
  1795
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1796
slouken@1542
  1797
				/* blend */
slouken@1542
  1798
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1799
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1800
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1801
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1802
slouken@1542
  1803
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1804
slouken@1542
  1805
				/* blue */
slouken@1542
  1806
				src2 = src1; /* src -> src2 */
slouken@1542
  1807
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1808
slouken@1542
  1809
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  1810
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1811
slouken@1542
  1812
				/* blend */
slouken@1542
  1813
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1814
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1815
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1816
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1817
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1818
slouken@1542
  1819
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1820
slouken@1542
  1821
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1822
slouken@1542
  1823
				srcp += 4;
slouken@1542
  1824
				dstp += 4;
slouken@1895
  1825
			}, width);
slouken@1895
  1826
			/* *INDENT-ON* */
slouken@1895
  1827
            srcp += srcskip;
slouken@1895
  1828
            dstp += dstskip;
slouken@1895
  1829
        }
slouken@1895
  1830
        _mm_empty();
slouken@1895
  1831
    }
slouken@1542
  1832
}
slouken@2255
  1833
slouken@2255
  1834
#endif /* __MMX__ */
slouken@689
  1835
slouken@1
  1836
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1837
static void
slouken@1895
  1838
Blit565to565SurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1839
{
slouken@1895
  1840
    unsigned alpha = info->src->alpha;
slouken@1895
  1841
    if (alpha == 128) {
slouken@1895
  1842
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1843
    } else {
slouken@1895
  1844
        int width = info->d_width;
slouken@1895
  1845
        int height = info->d_height;
slouken@1895
  1846
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1847
        int srcskip = info->s_skip >> 1;
slouken@1895
  1848
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1849
        int dstskip = info->d_skip >> 1;
slouken@1895
  1850
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1
  1851
slouken@1895
  1852
        while (height--) {
slouken@1895
  1853
			/* *INDENT-OFF* */
slouken@1
  1854
			DUFFS_LOOP4({
slouken@1
  1855
				Uint32 s = *srcp++;
slouken@1
  1856
				Uint32 d = *dstp;
slouken@1
  1857
				/*
slouken@1
  1858
				 * shift out the middle component (green) to
slouken@1
  1859
				 * the high 16 bits, and process all three RGB
slouken@1
  1860
				 * components at the same time.
slouken@1
  1861
				 */
slouken@1
  1862
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  1863
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  1864
				d += (s - d) * alpha >> 5;
slouken@1
  1865
				d &= 0x07e0f81f;
slouken@1428
  1866
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1867
			}, width);
slouken@1895
  1868
			/* *INDENT-ON* */
slouken@1895
  1869
            srcp += srcskip;
slouken@1895
  1870
            dstp += dstskip;
slouken@1895
  1871
        }
slouken@1895
  1872
    }
slouken@0
  1873
}
slouken@0
  1874
slouken@0
  1875
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1876
static void
slouken@1895
  1877
Blit555to555SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  1878
{
slouken@1895
  1879
    unsigned alpha = info->src->alpha;  /* downscale alpha to 5 bits */
slouken@1895
  1880
    if (alpha == 128) {
slouken@1895
  1881
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1882
    } else {
slouken@1895
  1883
        int width = info->d_width;
slouken@1895
  1884
        int height = info->d_height;
slouken@1895
  1885
        Uint16 *srcp = (Uint16 *) info->s_pixels;
slouken@1895
  1886
        int srcskip = info->s_skip >> 1;
slouken@1895
  1887
        Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1888
        int dstskip = info->d_skip >> 1;
slouken@1895
  1889
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@0
  1890
slouken@1895
  1891
        while (height--) {
slouken@1895
  1892
			/* *INDENT-OFF* */
slouken@1
  1893
			DUFFS_LOOP4({
slouken@1
  1894
				Uint32 s = *srcp++;
slouken@1
  1895
				Uint32 d = *dstp;
slouken@1
  1896
				/*
slouken@1
  1897
				 * shift out the middle component (green) to
slouken@1
  1898
				 * the high 16 bits, and process all three RGB
slouken@1
  1899
				 * components at the same time.
slouken@1
  1900
				 */
slouken@1
  1901
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  1902
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  1903
				d += (s - d) * alpha >> 5;
slouken@1
  1904
				d &= 0x03e07c1f;
slouken@1428
  1905
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1906
			}, width);
slouken@1895
  1907
			/* *INDENT-ON* */
slouken@1895
  1908
            srcp += srcskip;
slouken@1895
  1909
            dstp += dstskip;
slouken@1895
  1910
        }
slouken@1895
  1911
    }
slouken@0
  1912
}
slouken@0
  1913
slouken@0
  1914
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@1895
  1915
static void
slouken@1895
  1916
BlitARGBto565PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1917
{
slouken@1895
  1918
    int width = info->d_width;
slouken@1895
  1919
    int height = info->d_height;
slouken@1895
  1920
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1921
    int srcskip = info->s_skip >> 2;
slouken@1895
  1922
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1923
    int dstskip = info->d_skip >> 1;
slouken@0
  1924
slouken@1895
  1925
    while (height--) {
slouken@1895
  1926
	    /* *INDENT-OFF* */
slouken@0
  1927
	    DUFFS_LOOP4({
slouken@0
  1928
		Uint32 s = *srcp;
slouken@0
  1929
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1930
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1931
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1932
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1933
		   Benchmark this! */
slouken@689
  1934
		if(alpha) {   
slouken@689
  1935
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1936
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  1937
		  } else {
slouken@0
  1938
		    Uint32 d = *dstp;
slouken@0
  1939
		    /*
slouken@0
  1940
		     * convert source and destination to G0RAB65565
slouken@0
  1941
		     * and blend all components at the same time
slouken@0
  1942
		     */
slouken@0
  1943
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  1944
		      + (s >> 3 & 0x1f);
slouken@0
  1945
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  1946
		    d += (s - d) * alpha >> 5;
slouken@0
  1947
		    d &= 0x07e0f81f;
slouken@1428
  1948
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1949
		  }
slouken@0
  1950
		}
slouken@0
  1951
		srcp++;
slouken@0
  1952
		dstp++;
slouken@0
  1953
	    }, width);
slouken@1895
  1954
	    /* *INDENT-ON* */
slouken@1895
  1955
        srcp += srcskip;
slouken@1895
  1956
        dstp += dstskip;
slouken@1895
  1957
    }
slouken@0
  1958
}
slouken@0
  1959
slouken@0
  1960
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@1895
  1961
static void
slouken@1895
  1962
BlitARGBto555PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1963
{
slouken@1895
  1964
    int width = info->d_width;
slouken@1895
  1965
    int height = info->d_height;
slouken@1895
  1966
    Uint32 *srcp = (Uint32 *) info->s_pixels;
slouken@1895
  1967
    int srcskip = info->s_skip >> 2;
slouken@1895
  1968
    Uint16 *dstp = (Uint16 *) info->d_pixels;
slouken@1895
  1969
    int dstskip = info->d_skip >> 1;
slouken@0
  1970
slouken@1895
  1971
    while (height--) {
slouken@1895
  1972
	    /* *INDENT-OFF* */
slouken@0
  1973
	    DUFFS_LOOP4({
slouken@0
  1974
		unsigned alpha;
slouken@0
  1975
		Uint32 s = *srcp;
slouken@0
  1976
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1977
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1978
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1979
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1980
		   Benchmark this! */
slouken@689
  1981
		if(alpha) {   
slouken@689
  1982
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1983
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  1984
		  } else {
slouken@0
  1985
		    Uint32 d = *dstp;
slouken@0
  1986
		    /*
slouken@0
  1987
		     * convert source and destination to G0RAB65565
slouken@0
  1988
		     * and blend all components at the same time
slouken@0
  1989
		     */
slouken@0
  1990
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  1991
		      + (s >> 3 & 0x1f);
slouken@0
  1992
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  1993
		    d += (s - d) * alpha >> 5;
slouken@0
  1994
		    d &= 0x03e07c1f;
slouken@1428
  1995
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1996
		  }
slouken@0
  1997
		}
slouken@0
  1998
		srcp++;
slouken@0
  1999
		dstp++;
slouken@0
  2000
	    }, width);
slouken@1895
  2001
	    /* *INDENT-ON* */
slouken@1895
  2002
        srcp += srcskip;
slouken@1895
  2003
        dstp += dstskip;
slouken@1895
  2004
    }
slouken@0
  2005
}
slouken@0
  2006
slouken@0
  2007
/* General (slow) N->N blending with per-surface alpha */
slouken@1895
  2008
static void
slouken@1895
  2009
BlitNtoNSurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  2010
{
slouken@1895
  2011
    int width = info->d_width;
slouken@1895
  2012
    int height = info->d_height;
slouken@1895
  2013
    Uint8 *src = info->s_pixels;
slouken@1895
  2014
    int srcskip = info->s_skip;
slouken@1895
  2015
    Uint8 *dst = info->d_pixels;
slouken@1895
  2016
    int dstskip = info->d_skip;
slouken@1895
  2017
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
  2018
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  2019
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2020
    int dstbpp = dstfmt->BytesPerPixel;
slouken@1895
  2021
    unsigned sA = srcfmt->alpha;
slouken@1895
  2022
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2023
slouken@1895
  2024
    if (sA) {
slouken@1895
  2025
        while (height--) {
slouken@1895
  2026
	    /* *INDENT-OFF* */
slouken@0
  2027
	    DUFFS_LOOP4(
slouken@0
  2028
	    {
icculus@1162
  2029
		Uint32 Pixel;
slouken@0
  2030
		unsigned sR;
slouken@0
  2031
		unsigned sG;
slouken@0
  2032
		unsigned sB;
slouken@0
  2033
		unsigned dR;
slouken@0
  2034
		unsigned dG;
slouken@0
  2035
		unsigned dB;
icculus@1162
  2036
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2037
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2038
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2039
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2040
		src += srcbpp;
slouken@0
  2041
		dst += dstbpp;
slouken@0
  2042
	    },
slouken@0
  2043
	    width);
slouken@1895
  2044
	    /* *INDENT-ON* */
slouken@1895
  2045
            src += srcskip;
slouken@1895
  2046
            dst += dstskip;
slouken@1895
  2047
        }
slouken@1895
  2048
    }
slouken@0
  2049
}
slouken@0
  2050
slouken@0
  2051
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@1895
  2052
static void
slouken@1895
  2053
BlitNtoNSurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
  2054
{
slouken@1895
  2055
    int width = info->d_width;
slouken@1895
  2056
    int height = info->d_height;
slouken@1895
  2057
    Uint8 *src = info->s_pixels;
slouken@1895
  2058
    int srcskip = info->s_skip;
slouken@1895
  2059
    Uint8 *dst = info->d_pixels;
slouken@1895
  2060
    int dstskip = info->d_skip;
slouken@1895
  2061
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
  2062
    SDL_PixelFormat *dstfmt = info->dst;
slouken@1895
  2063
    Uint32 ckey = srcfmt->colorkey;
slouken@1895
  2064
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2065
    int dstbpp = dstfmt->BytesPerPixel;
slouken@1895
  2066
    unsigned sA = srcfmt->alpha;
slouken@1895
  2067
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2068
slouken@1895
  2069
    while (height--) {
slouken@1895
  2070
	    /* *INDENT-OFF* */
slouken@0
  2071
	    DUFFS_LOOP4(
slouken@0
  2072
	    {
icculus@1162
  2073
		Uint32 Pixel;
slouken@0
  2074
		unsigned sR;
slouken@0
  2075
		unsigned sG;
slouken@0
  2076
		unsigned sB;
slouken@0
  2077
		unsigned dR;
slouken@0
  2078
		unsigned dG;
slouken@0
  2079
		unsigned dB;
icculus@1162
  2080
		RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
icculus@1162
  2081
		if(sA && Pixel != ckey) {
icculus@1162
  2082
		    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
icculus@1162
  2083
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2084
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2085
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2086
		}
slouken@0
  2087
		src += srcbpp;
slouken@0
  2088
		dst += dstbpp;
slouken@0
  2089
	    },
slouken@0
  2090
	    width);
slouken@1895
  2091
	    /* *INDENT-ON* */
slouken@1895
  2092
        src += srcskip;
slouken@1895
  2093
        dst += dstskip;
slouken@1895
  2094
    }
slouken@0
  2095
}
slouken@0
  2096
slouken@0
  2097
/* General (slow) N->N blending with pixel alpha */
slouken@1895
  2098
static void
slouken@1895
  2099
BlitNtoNPixelAlpha(SDL_BlitInfo * info)
slouken@0
  2100
{
slouken@1895
  2101
    int width = info->d_width;
slouken@1895
  2102
    int height = info->d_height;
slouken@1895
  2103
    Uint8 *src = info->s_pixels;
slouken@1895
  2104
    int srcskip = info->s_skip;
slouken@1895
  2105
    Uint8 *dst = info->d_pixels;
slouken@1895
  2106
    int dstskip = info->d_skip;
slouken@1895
  2107
    SDL_PixelFormat *srcfmt = info->src;
slouken@1895
  2108
    SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2109
slouken@1895
  2110
    int srcbpp;
slouken@1895
  2111
    int dstbpp;
slouken@0
  2112
slouken@1895
  2113
    /* Set up some basic variables */
slouken@1895
  2114
    srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2115
    dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2116
slouken@1895
  2117
    /* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@1895
  2118
       quite right. for <8bpp source alpha, it gets them very wrong
slouken@1895
  2119
       (check all macros!)
slouken@1895
  2120
       It is unclear whether there is a good general solution that doesn't
slouken@1895
  2121
       need a branch (or a divide). */
slouken@1895
  2122
    while (height--) {
slouken@1895
  2123
	    /* *INDENT-OFF* */
slouken@0
  2124
	    DUFFS_LOOP4(
slouken@0
  2125
	    {
icculus@1162
  2126
		Uint32 Pixel;
slouken@0
  2127
		unsigned sR;
slouken@0
  2128
		unsigned sG;
slouken@0
  2129
		unsigned sB;
slouken@0
  2130
		unsigned dR;
slouken@0
  2131
		unsigned dG;
slouken@0
  2132
		unsigned dB;
slouken@0
  2133
		unsigned sA;
slouken@0
  2134
		unsigned dA;
icculus@1162
  2135
		DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
slouken@689
  2136
		if(sA) {
icculus@1162
  2137
		  DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
slouken@689
  2138
		  ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@689
  2139
		  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@689
  2140
		}
slouken@0
  2141
		src += srcbpp;
slouken@0
  2142
		dst += dstbpp;
slouken@0
  2143
	    },
slouken@0
  2144
	    width);
slouken@1895
  2145
	    /* *INDENT-ON* */
slouken@1895
  2146
        src += srcskip;
slouken@1895
  2147
        dst += dstskip;
slouken@1895
  2148
    }
slouken@0
  2149
}
slouken@0
  2150
slouken@0
  2151
slouken@1895
  2152
SDL_loblit
slouken@1895
  2153
SDL_CalculateAlphaBlit(SDL_Surface * surface, int blit_index)
slouken@0
  2154
{
slouken@0
  2155
    SDL_PixelFormat *sf = surface->format;
slouken@0
  2156
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
  2157
slouken@1895
  2158
    if (sf->Amask == 0) {
slouken@1895
  2159
        if ((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@1895
  2160
            if (df->BytesPerPixel == 1)
slouken@1895
  2161
                return BlitNto1SurfaceAlphaKey;
slouken@1895
  2162
            else
slouken@1361
  2163
#if SDL_ALTIVEC_BLITTERS
slouken@2232
  2164
            if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
slouken@2231
  2165
                    SDL_HasAltiVec())
slouken@1895
  2166
                return Blit32to32SurfaceAlphaKeyAltivec;
slouken@1895
  2167
            else
icculus@1047
  2168
#endif
slouken@1895
  2169
                return BlitNtoNSurfaceAlphaKey;
slouken@1895
  2170
        } else {
slouken@1895
  2171
            /* Per-surface alpha blits */
slouken@1895
  2172
            switch (df->BytesPerPixel) {
slouken@1895
  2173
            case 1:
slouken@1895
  2174
                return BlitNto1SurfaceAlpha;
slouken@0
  2175
slouken@1895
  2176
            case 2:
slouken@1895
  2177
                if (surface->map->identity) {
slouken@1895
  2178
                    if (df->Gmask == 0x7e0) {
slouken@2255
  2179
#ifdef __MMX__
slouken@1895
  2180
                        if (SDL_HasMMX())
slouken@1895
  2181
                            return Blit565to565SurfaceAlphaMMX;
slouken@1895
  2182
                        else
slouken@689
  2183
#endif
slouken@1895
  2184
                            return Blit565to565SurfaceAlpha;
slouken@1895
  2185
                    } else if (df->Gmask == 0x3e0) {
slouken@2255
  2186
#ifdef __MMX__
slouken@1895
  2187
                        if (SDL_HasMMX())
slouken@1895
  2188
                            return Blit555to555SurfaceAlphaMMX;
slouken@1895
  2189
                        else
slouken@689
  2190
#endif
slouken@1895
  2191
                            return Blit555to555SurfaceAlpha;
slouken@1895
  2192
                    }
slouken@1895
  2193
                }
slouken@1895
  2194
                return BlitNtoNSurfaceAlpha;
slouken@0
  2195
slouken@1895
  2196
            case 4:
slouken@1895
  2197
                if (sf->Rmask == df->Rmask
slouken@1895
  2198
                    && sf->Gmask == df->Gmask
slouken@1895
  2199
                    && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@2255
  2200
#ifdef __MMX__
slouken@1895
  2201
                    if (sf->Rshift % 8 == 0
slouken@1895
  2202
                        && sf->Gshift % 8 == 0
slouken@1895
  2203
                        && sf->Bshift % 8 == 0 && SDL_HasMMX())
slouken@1895
  2204
                        return BlitRGBtoRGBSurfaceAlphaMMX;
slouken@1542
  2205
#endif
slouken@1895
  2206
                    if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
slouken@1617
  2207
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2208
                        if (SDL_HasAltiVec())
slouken@1895
  2209
                            return BlitRGBtoRGBSurfaceAlphaAltivec;
slouken@1542
  2210
#endif
slouken@1895
  2211
                        return BlitRGBtoRGBSurfaceAlpha;
slouken@1895
  2212
                    }
slouken@1895
  2213
                }
slouken@1542
  2214
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2215
                if ((sf->BytesPerPixel == 4) && SDL_HasAltiVec())
slouken@1895
  2216
                    return Blit32to32SurfaceAlphaAltivec;
slouken@1895
  2217
                else
slouken@689
  2218
#endif
slouken@1895
  2219
                    return BlitNtoNSurfaceAlpha;
slouken@0
  2220
slouken@1895
  2221
            case 3:
slouken@1895
  2222
            default:
slouken@1895
  2223
                return BlitNtoNSurfaceAlpha;
slouken@1895
  2224
            }
slouken@1895
  2225
        }
slouken@0
  2226
    } else {
slouken@1895
  2227
        /* Per-pixel alpha blits */
slouken@1895
  2228
        switch (df->BytesPerPixel) {
slouken@1895
  2229
        case 1:
slouken@1895
  2230
            return BlitNto1PixelAlpha;
slouken@0
  2231
slouken@1895
  2232
        case 2:
slouken@1361
  2233
#if SDL_ALTIVEC_BLITTERS
slouken@1895
  2234
            if (sf->BytesPerPixel == 4
slouken@1895
  2235
                && df->Gmask == 0x7e0 && df->Bmask == 0x1f
slouken@1895
  2236
                && SDL_HasAltiVec())
slouken@1895
  2237
                return Blit32to565PixelAlphaAltivec;
slouken@1895
  2238
            else
icculus@1047
  2239
#endif
slouken@1895
  2240
                if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@1895
  2241
                    && sf->Gmask == 0xff00
slouken@1895
  2242
                    && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@1895
  2243
                        || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@1895
  2244
                if (df->Gmask == 0x7e0)
slouken@1895
  2245
                    return BlitARGBto565PixelAlpha;
slouken@1895
  2246
                else if (df->Gmask == 0x3e0)
slouken@1895
  2247
                    return BlitARGBto555PixelAlpha;
slouken@1895
  2248
            }
slouken@1895
  2249
            return BlitNtoNPixelAlpha;
slouken@0
  2250
slouken@1895
  2251
        case 4:
slouken@1895
  2252
            if (sf->Rmask == df->Rmask
slouken@1895
  2253
                && sf->Gmask == df->Gmask
slouken@1895
  2254
                && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@2255
  2255
#ifdef __MMX__
slouken@1895
  2256
                if (sf->Rshift % 8 == 0
slouken@1895
  2257
                    && sf->Gshift % 8 == 0
slouken@1895
  2258
                    && sf->Bshift % 8 == 0
slouken@1895
  2259
                    && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
slouken@1895
  2260
                    if (SDL_Has3DNow())
slouken@1895
  2261
                        return BlitRGBtoRGBPixelAlphaMMX3DNOW;
slouken@1895
  2262
                    if (SDL_HasMMX())
slouken@1895
  2263
                        return BlitRGBtoRGBPixelAlphaMMX;
slouken@1895
  2264
                }
slouken@689
  2265
#endif
slouken@1895
  2266
                if (sf->Amask == 0xff000000) {
slouken@1617
  2267
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2268
                    if (SDL_HasAltiVec())
slouken@1895
  2269
                        return BlitRGBtoRGBPixelAlphaAltivec;
icculus@1047
  2270
#endif
slouken@1895
  2271
                    return BlitRGBtoRGBPixelAlpha;
slouken@1895
  2272
                }
slouken@1895
  2273
            }
slouken@1361
  2274
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2275
            if (sf->Amask && sf->BytesPerPixel == 4 && SDL_HasAltiVec())
slouken@1895
  2276
                return Blit32to32PixelAlphaAltivec;
slouken@1895
  2277
            else
icculus@1047
  2278
#endif
slouken@1895
  2279
                return BlitNtoNPixelAlpha;
slouken@0
  2280
slouken@1895
  2281
        case 3:
slouken@1895
  2282
        default:
slouken@1895
  2283
            return BlitNtoNPixelAlpha;
slouken@1895
  2284
        }
slouken@0
  2285
    }
slouken@0
  2286
}
slouken@0
  2287
slouken@1895
  2288
/* vi: set ts=4 sw=4 expandtab: */