src/video/SDL_blit_A.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 10 Jan 2009 21:50:26 +0000
changeset 3013 8cc00819c8d6
parent 3011 8f4ed5ec2b06
child 3035 ff602fdfdedc
permissions -rw-r--r--
Reverted Bob's indent checkin
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@2262
    33
    int width = info->dst_w;
slouken@2262
    34
    int height = info->dst_h;
slouken@2262
    35
    Uint8 *src = info->src;
slouken@2267
    36
    int srcskip = info->src_skip;
slouken@2262
    37
    Uint8 *dst = info->dst;
slouken@2267
    38
    int dstskip = info->dst_skip;
slouken@1895
    39
    Uint8 *palmap = info->table;
slouken@2267
    40
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
    41
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
    42
    int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    43
slouken@2267
    44
    const unsigned A = info->a;
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@2262
    89
    int width = info->dst_w;
slouken@2262
    90
    int height = info->dst_h;
slouken@2262
    91
    Uint8 *src = info->src;
slouken@2267
    92
    int srcskip = info->src_skip;
slouken@2262
    93
    Uint8 *dst = info->dst;
slouken@2267
    94
    int dstskip = info->dst_skip;
slouken@1895
    95
    Uint8 *palmap = info->table;
slouken@2267
    96
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
    97
    SDL_PixelFormat *dstfmt = info->dst_fmt;
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@2262
   145
    int width = info->dst_w;
slouken@2262
   146
    int height = info->dst_h;
slouken@2262
   147
    Uint8 *src = info->src;
slouken@2267
   148
    int srcskip = info->src_skip;
slouken@2262
   149
    Uint8 *dst = info->dst;
slouken@2267
   150
    int dstskip = info->dst_skip;
slouken@1895
   151
    Uint8 *palmap = info->table;
slouken@2267
   152
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
   153
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
   154
    int srcbpp = srcfmt->BytesPerPixel;
slouken@2267
   155
    Uint32 ckey = info->colorkey;
slouken@0
   156
slouken@2267
   157
    const int A = info->a;
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@2262
   206
    int width = info->dst_w;
slouken@2262
   207
    int height = info->dst_h;
slouken@2262
   208
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   209
    int srcskip = info->src_skip >> 2;
slouken@2262
   210
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   211
    int dstskip = info->dst_skip >> 2;
slouken@2267
   212
    Uint32 dalpha = info->dst_fmt->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@2267
   262
    SDL_PixelFormat *df = info->dst_fmt;
slouken@1895
   263
    Uint32 chanmask = df->Rmask | df->Gmask | df->Bmask;
slouken@2267
   264
    unsigned alpha = info->a;
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@2262
   270
        int width = info->dst_w;
slouken@2262
   271
        int height = info->dst_h;
slouken@2262
   272
        Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   273
        int srcskip = info->src_skip >> 2;
slouken@2262
   274
        Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   275
        int dstskip = info->dst_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@3013
   286
            (0xff << df->Rshift) | (0xff << df->
slouken@3013
   287
                                    Gshift) | (0xff << df->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@2262
   359
    int width = info->dst_w;
slouken@2262
   360
    int height = info->dst_h;
slouken@2262
   361
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   362
    int srcskip = info->src_skip >> 2;
slouken@2262
   363
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   364
    int dstskip = info->dst_skip >> 2;
slouken@2267
   365
    SDL_PixelFormat *sf = info->src_fmt;
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@2260
   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@3013
   529
            ((srcfmt->Amask) ? RESHIFT(srcfmt->
slouken@3013
   530
                                       Ashift) : 0x10) << (dstfmt->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
{
slouken@2262
   545
    int height = info->dst_h;
slouken@2262
   546
    Uint8 *src = (Uint8 *) info->src;
slouken@2267
   547
    int srcskip = info->src_skip;
slouken@2262
   548
    Uint8 *dst = (Uint8 *) info->dst;
slouken@2267
   549
    int dstskip = info->dst_skip;
slouken@2267
   550
    SDL_PixelFormat *srcfmt = info->src_fmt;
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;
slouken@2262
   620
        int width = info->dst_w;
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
{
slouken@2262
   721
    int height = info->dst_h;
slouken@2262
   722
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   723
    int srcskip = info->src_skip >> 2;
slouken@2262
   724
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   725
    int dstskip = info->dst_skip >> 2;
slouken@2267
   726
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
   727
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@2267
   728
    unsigned sA = info->a;
icculus@1047
   729
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
   730
    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
slouken@2267
   731
    Uint32 ckey = info->colorkey;
icculus@1047
   732
    vector unsigned char mergePermute;
icculus@1047
   733
    vector unsigned char vsrcPermute;
icculus@1047
   734
    vector unsigned char vdstPermute;
icculus@1047
   735
    vector unsigned char vsdstPermute;
icculus@1047
   736
    vector unsigned char valpha;
icculus@1047
   737
    vector unsigned char valphamask;
icculus@1047
   738
    vector unsigned char vbits;
icculus@1047
   739
    vector unsigned char v0;
icculus@1047
   740
    vector unsigned short v1;
icculus@1047
   741
    vector unsigned short v8;
icculus@1047
   742
    vector unsigned int vckey;
icculus@1047
   743
    vector unsigned int vrgbmask;
icculus@1047
   744
icculus@1047
   745
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   746
    v0 = vec_splat_u8(0);
icculus@1047
   747
    v1 = vec_splat_u16(1);
icculus@1047
   748
    v8 = vec_splat_u16(8);
icculus@1047
   749
icculus@1047
   750
    /* set the alpha to 255 on the destination surf */
icculus@1047
   751
    valphamask = VEC_ALPHA_MASK();
icculus@1047
   752
icculus@1047
   753
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   754
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
   755
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
   756
icculus@1047
   757
    /* set a vector full of alpha and 255-alpha */
slouken@2291
   758
    ((unsigned char *) &valpha)[0] = sA;
icculus@1047
   759
    valpha = vec_splat(valpha, 0);
slouken@1895
   760
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
   761
icculus@1047
   762
    ckey &= rgbmask;
slouken@1895
   763
    ((unsigned int *) (char *) &vckey)[0] = ckey;
icculus@1047
   764
    vckey = vec_splat(vckey, 0);
slouken@1895
   765
    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
icculus@1047
   766
    vrgbmask = vec_splat(vrgbmask, 0);
icculus@1047
   767
slouken@1895
   768
    while (height--) {
slouken@2262
   769
        int width = info->dst_w;
icculus@1047
   770
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   771
        while (condition) { \
icculus@1162
   772
            Uint32 Pixel; \
icculus@1047
   773
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
   774
            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
icculus@1162
   775
            if(sA && Pixel != ckey) { \
icculus@1162
   776
                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
icculus@1162
   777
                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
   778
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   779
                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
   780
            } \
icculus@1162
   781
            dstp++; \
icculus@1162
   782
            srcp++; \
icculus@1047
   783
            widthvar--; \
icculus@1047
   784
        }
icculus@1047
   785
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   786
        if (width > 0) {
icculus@1047
   787
            int extrawidth = (width % 4);
icculus@1047
   788
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
   789
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
   790
            width -= extrawidth;
icculus@1047
   791
            while (width) {
icculus@1047
   792
                vector unsigned char vsel;
icculus@1047
   793
                vector unsigned char voverflow;
icculus@1047
   794
                vector unsigned char vd;
icculus@1047
   795
                vector unsigned char vd_orig;
icculus@1047
   796
icculus@1047
   797
                /* s = *srcp */
slouken@1895
   798
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
   799
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
   800
icculus@1047
   801
                /* vsel is set for items that match the key */
slouken@1895
   802
                vsel =
slouken@1895
   803
                    (vector unsigned char) vec_and((vector unsigned int) vs,
slouken@1895
   804
                                                   vrgbmask);
slouken@1895
   805
                vsel = (vector unsigned char) vec_cmpeq((vector unsigned int)
slouken@1895
   806
                                                        vsel, vckey);
icculus@1047
   807
icculus@1047
   808
                /* permute to source format */
icculus@1047
   809
                vs = vec_perm(vs, valpha, vsrcPermute);
icculus@1047
   810
icculus@1047
   811
                /* d = *dstp */
slouken@1895
   812
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
   813
                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
   814
icculus@1047
   815
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
   816
icculus@1047
   817
                /* set the alpha channel to full on */
icculus@1047
   818
                vd = vec_or(vd, valphamask);
icculus@1047
   819
icculus@1047
   820
                /* mask out color key */
icculus@1047
   821
                vd = vec_sel(vd, vd_orig, vsel);
slouken@1895
   822
icculus@1047
   823
                /* permute to dest format */
icculus@1047
   824
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
   825
icculus@1047
   826
                /* *dstp = res */
slouken@1895
   827
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
   828
icculus@1047
   829
                srcp += 4;
icculus@1047
   830
                dstp += 4;
icculus@1047
   831
                width -= 4;
icculus@1047
   832
                vs = voverflow;
icculus@1047
   833
            }
icculus@1047
   834
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   835
        }
icculus@1047
   836
#undef ONE_PIXEL_BLEND
slouken@1895
   837
icculus@1047
   838
        srcp += srcskip;
icculus@1047
   839
        dstp += dstskip;
icculus@1047
   840
    }
icculus@1047
   841
}
icculus@1047
   842
icculus@1047
   843
slouken@1895
   844
static void
slouken@1895
   845
Blit32to32PixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   846
{
slouken@2262
   847
    int width = info->dst_w;
slouken@2262
   848
    int height = info->dst_h;
slouken@2262
   849
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   850
    int srcskip = info->src_skip >> 2;
slouken@2262
   851
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   852
    int dstskip = info->dst_skip >> 2;
slouken@2267
   853
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
   854
    SDL_PixelFormat *dstfmt = info->dst_fmt;
icculus@1047
   855
    vector unsigned char mergePermute;
icculus@1047
   856
    vector unsigned char valphaPermute;
icculus@1047
   857
    vector unsigned char vsrcPermute;
icculus@1047
   858
    vector unsigned char vdstPermute;
icculus@1047
   859
    vector unsigned char vsdstPermute;
icculus@1047
   860
    vector unsigned char valphamask;
icculus@1047
   861
    vector unsigned char vpixelmask;
icculus@1047
   862
    vector unsigned char v0;
icculus@1047
   863
    vector unsigned short v1;
icculus@1047
   864
    vector unsigned short v8;
icculus@1047
   865
icculus@1047
   866
    v0 = vec_splat_u8(0);
icculus@1047
   867
    v1 = vec_splat_u16(1);
icculus@1047
   868
    v8 = vec_splat_u16(8);
icculus@1047
   869
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   870
    valphamask = VEC_ALPHA_MASK();
slouken@1895
   871
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
icculus@1047
   872
    vpixelmask = vec_nor(valphamask, v0);
icculus@1047
   873
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   874
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
   875
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
   876
slouken@1895
   877
    while (height--) {
slouken@2262
   878
        width = info->dst_w;
icculus@1047
   879
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
   880
            Uint32 Pixel; \
icculus@1047
   881
            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
icculus@1162
   882
            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
   883
            if(sA) { \
icculus@1162
   884
              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
icculus@1047
   885
              ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   886
              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
   887
            } \
icculus@1047
   888
            ++srcp; \
icculus@1047
   889
            ++dstp; \
icculus@1047
   890
            widthvar--; \
icculus@1047
   891
        }
icculus@1047
   892
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   893
        if (width > 0) {
slouken@1487
   894
            /* vsrcPermute */
slouken@1487
   895
            /* vdstPermute */
icculus@1047
   896
            int extrawidth = (width % 4);
icculus@1047
   897
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
   898
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
   899
            width -= extrawidth;
icculus@1047
   900
            while (width) {
icculus@1047
   901
                vector unsigned char voverflow;
icculus@1047
   902
                vector unsigned char vd;
icculus@1047
   903
                vector unsigned char valpha;
icculus@1047
   904
                vector unsigned char vdstalpha;
icculus@1047
   905
                /* s = *srcp */
slouken@1895
   906
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
   907
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
   908
                vs = vec_perm(vs, v0, vsrcPermute);
icculus@1047
   909
icculus@1047
   910
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
   911
icculus@1047
   912
                /* d = *dstp */
slouken@1895
   913
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
   914
                vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
   915
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
   916
icculus@1047
   917
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
   918
icculus@1047
   919
                /* set the alpha to the dest alpha */
icculus@1047
   920
                vd = vec_and(vd, vpixelmask);
icculus@1047
   921
                vd = vec_or(vd, vdstalpha);
icculus@1047
   922
                vd = vec_perm(vd, v0, vdstPermute);
icculus@1047
   923
icculus@1047
   924
                /* *dstp = res */
slouken@1895
   925
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
   926
icculus@1047
   927
                srcp += 4;
icculus@1047
   928
                dstp += 4;
icculus@1047
   929
                width -= 4;
icculus@1047
   930
                vs = voverflow;
icculus@1047
   931
icculus@1047
   932
            }
icculus@1047
   933
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   934
        }
slouken@1895
   935
        srcp += srcskip;
slouken@1895
   936
        dstp += dstskip;
icculus@1047
   937
#undef ONE_PIXEL_BLEND
slouken@1895
   938
    }
icculus@1047
   939
}
icculus@1047
   940
icculus@1047
   941
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
   942
static void
slouken@1895
   943
BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
   944
{
slouken@2262
   945
    int width = info->dst_w;
slouken@2262
   946
    int height = info->dst_h;
slouken@2262
   947
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
   948
    int srcskip = info->src_skip >> 2;
slouken@2262
   949
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
   950
    int dstskip = info->dst_skip >> 2;
icculus@1047
   951
    vector unsigned char mergePermute;
icculus@1047
   952
    vector unsigned char valphaPermute;
icculus@1047
   953
    vector unsigned char valphamask;
icculus@1047
   954
    vector unsigned char vpixelmask;
icculus@1047
   955
    vector unsigned char v0;
icculus@1047
   956
    vector unsigned short v1;
icculus@1047
   957
    vector unsigned short v8;
icculus@1047
   958
    v0 = vec_splat_u8(0);
icculus@1047
   959
    v1 = vec_splat_u16(1);
icculus@1047
   960
    v8 = vec_splat_u16(8);
icculus@1047
   961
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   962
    valphamask = VEC_ALPHA_MASK();
slouken@1895
   963
    valphaPermute = vec_and(vec_lvsl(0, (int *) NULL), vec_splat_u8(0xC));
slouken@1895
   964
slouken@1895
   965
icculus@1047
   966
    vpixelmask = vec_nor(valphamask, v0);
slouken@1895
   967
    while (height--) {
slouken@2262
   968
        width = info->dst_w;
icculus@1047
   969
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   970
        while ((condition)) { \
icculus@1047
   971
            Uint32 dalpha; \
icculus@1047
   972
            Uint32 d; \
icculus@1047
   973
            Uint32 s1; \
icculus@1047
   974
            Uint32 d1; \
icculus@1047
   975
            Uint32 s = *srcp; \
icculus@1047
   976
            Uint32 alpha = s >> 24; \
icculus@1047
   977
            if(alpha) { \
icculus@1047
   978
              if(alpha == SDL_ALPHA_OPAQUE) { \
icculus@1047
   979
                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
icculus@1047
   980
              } else { \
icculus@1047
   981
                d = *dstp; \
icculus@1047
   982
                dalpha = d & 0xff000000; \
icculus@1047
   983
                s1 = s & 0xff00ff; \
icculus@1047
   984
                d1 = d & 0xff00ff; \
icculus@1047
   985
                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
icculus@1047
   986
                s &= 0xff00; \
icculus@1047
   987
                d &= 0xff00; \
icculus@1047
   988
                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
   989
                *dstp = d1 | d | dalpha; \
icculus@1047
   990
              } \
icculus@1047
   991
            } \
icculus@1047
   992
            ++srcp; \
icculus@1047
   993
            ++dstp; \
icculus@1047
   994
            widthvar--; \
icculus@1047
   995
	    }
icculus@1047
   996
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
   997
        if (width > 0) {
icculus@1047
   998
            int extrawidth = (width % 4);
icculus@1047
   999
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1000
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1001
            width -= extrawidth;
icculus@1047
  1002
            while (width) {
icculus@1047
  1003
                vector unsigned char voverflow;
icculus@1047
  1004
                vector unsigned char vd;
icculus@1047
  1005
                vector unsigned char valpha;
icculus@1047
  1006
                vector unsigned char vdstalpha;
icculus@1047
  1007
                /* s = *srcp */
slouken@1895
  1008
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1009
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1010
icculus@1047
  1011
                valpha = vec_perm(vs, v0, valphaPermute);
slouken@1895
  1012
icculus@1047
  1013
                /* d = *dstp */
slouken@1895
  1014
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1015
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1016
icculus@1047
  1017
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1018
icculus@1047
  1019
                /* set the alpha to the dest alpha */
icculus@1047
  1020
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1021
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1022
icculus@1047
  1023
                /* *dstp = res */
slouken@1895
  1024
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1025
icculus@1047
  1026
                srcp += 4;
icculus@1047
  1027
                dstp += 4;
icculus@1047
  1028
                width -= 4;
icculus@1047
  1029
                vs = voverflow;
icculus@1047
  1030
            }
icculus@1047
  1031
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1032
        }
slouken@1895
  1033
        srcp += srcskip;
slouken@1895
  1034
        dstp += dstskip;
slouken@1895
  1035
    }
icculus@1047
  1036
#undef ONE_PIXEL_BLEND
icculus@1047
  1037
}
icculus@1047
  1038
slouken@1895
  1039
static void
slouken@1895
  1040
Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1041
{
icculus@1047
  1042
    /* XXX : 6 */
slouken@2262
  1043
    int height = info->dst_h;
slouken@2262
  1044
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1045
    int srcskip = info->src_skip >> 2;
slouken@2262
  1046
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1047
    int dstskip = info->dst_skip >> 2;
slouken@2267
  1048
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  1049
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@2267
  1050
    unsigned sA = info->a;
slouken@1895
  1051
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
  1052
    vector unsigned char mergePermute;
icculus@1047
  1053
    vector unsigned char vsrcPermute;
icculus@1047
  1054
    vector unsigned char vdstPermute;
icculus@1047
  1055
    vector unsigned char vsdstPermute;
icculus@1047
  1056
    vector unsigned char valpha;
icculus@1047
  1057
    vector unsigned char valphamask;
icculus@1047
  1058
    vector unsigned char vbits;
icculus@1047
  1059
    vector unsigned short v1;
icculus@1047
  1060
    vector unsigned short v8;
icculus@1047
  1061
icculus@1047
  1062
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1063
    v1 = vec_splat_u16(1);
icculus@1047
  1064
    v8 = vec_splat_u16(8);
icculus@1047
  1065
icculus@1047
  1066
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1067
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1068
icculus@1047
  1069
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1070
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1071
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1072
icculus@1047
  1073
    /* set a vector full of alpha and 255-alpha */
slouken@2291
  1074
    ((unsigned char *) &valpha)[0] = sA;
icculus@1047
  1075
    valpha = vec_splat(valpha, 0);
slouken@1895
  1076
    vbits = (vector unsigned char) vec_splat_s8(-1);
icculus@1047
  1077
slouken@1895
  1078
    while (height--) {
slouken@2262
  1079
        int width = info->dst_w;
icculus@1047
  1080
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1081
            Uint32 Pixel; \
icculus@1047
  1082
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
  1083
            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
icculus@1162
  1084
            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
  1085
            ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1086
            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1087
            ++srcp; \
icculus@1047
  1088
            ++dstp; \
icculus@1047
  1089
            widthvar--; \
icculus@1047
  1090
        }
icculus@1047
  1091
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1092
        if (width > 0) {
icculus@1047
  1093
            int extrawidth = (width % 4);
icculus@2086
  1094
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1095
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1096
            width -= extrawidth;
icculus@1047
  1097
            while (width) {
icculus@1047
  1098
                vector unsigned char voverflow;
icculus@1047
  1099
                vector unsigned char vd;
icculus@1047
  1100
icculus@1047
  1101
                /* s = *srcp */
slouken@1895
  1102
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1103
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1104
                vs = vec_perm(vs, valpha, vsrcPermute);
slouken@1895
  1105
icculus@1047
  1106
                /* d = *dstp */
slouken@1895
  1107
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1108
                vd = vec_perm(vd, vd, vsdstPermute);
icculus@1047
  1109
icculus@1047
  1110
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1111
icculus@1047
  1112
                /* set the alpha channel to full on */
icculus@1047
  1113
                vd = vec_or(vd, valphamask);
icculus@1047
  1114
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1115
icculus@1047
  1116
                /* *dstp = res */
slouken@1895
  1117
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1118
icculus@1047
  1119
                srcp += 4;
icculus@1047
  1120
                dstp += 4;
icculus@1047
  1121
                width -= 4;
icculus@1047
  1122
                vs = voverflow;
icculus@1047
  1123
            }
icculus@1047
  1124
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1125
        }
icculus@1047
  1126
#undef ONE_PIXEL_BLEND
slouken@1895
  1127
icculus@1047
  1128
        srcp += srcskip;
icculus@1047
  1129
        dstp += dstskip;
icculus@1047
  1130
    }
icculus@1047
  1131
icculus@1047
  1132
}
icculus@1047
  1133
icculus@1047
  1134
icculus@1047
  1135
/* fast RGB888->(A)RGB888 blending */
slouken@1895
  1136
static void
slouken@1895
  1137
BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo * info)
icculus@1047
  1138
{
slouken@2267
  1139
    unsigned alpha = info->a;
slouken@2262
  1140
    int height = info->dst_h;
slouken@2262
  1141
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1142
    int srcskip = info->src_skip >> 2;
slouken@2262
  1143
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1144
    int dstskip = info->dst_skip >> 2;
icculus@1047
  1145
    vector unsigned char mergePermute;
icculus@1047
  1146
    vector unsigned char valpha;
icculus@1047
  1147
    vector unsigned char valphamask;
icculus@1047
  1148
    vector unsigned short v1;
icculus@1047
  1149
    vector unsigned short v8;
icculus@1047
  1150
icculus@1047
  1151
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1152
    v1 = vec_splat_u16(1);
icculus@1047
  1153
    v8 = vec_splat_u16(8);
icculus@1047
  1154
icculus@1047
  1155
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1156
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1157
icculus@1047
  1158
    /* set a vector full of alpha and 255-alpha */
slouken@1895
  1159
    ((unsigned char *) &valpha)[0] = alpha;
icculus@1047
  1160
    valpha = vec_splat(valpha, 0);
icculus@1047
  1161
slouken@1895
  1162
    while (height--) {
slouken@2262
  1163
        int width = info->dst_w;
icculus@1047
  1164
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1047
  1165
            Uint32 s = *srcp; \
icculus@1047
  1166
            Uint32 d = *dstp; \
icculus@1047
  1167
            Uint32 s1 = s & 0xff00ff; \
icculus@1047
  1168
            Uint32 d1 = d & 0xff00ff; \
icculus@1047
  1169
            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
icculus@1047
  1170
                 & 0xff00ff; \
icculus@1047
  1171
            s &= 0xff00; \
icculus@1047
  1172
            d &= 0xff00; \
icculus@1047
  1173
            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1174
            *dstp = d1 | d | 0xff000000; \
icculus@1047
  1175
            ++srcp; \
icculus@1047
  1176
            ++dstp; \
icculus@1047
  1177
            widthvar--; \
icculus@1047
  1178
        }
icculus@1047
  1179
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1180
        if (width > 0) {
icculus@1047
  1181
            int extrawidth = (width % 4);
icculus@1047
  1182
            vector unsigned char valigner = VEC_ALIGNER(srcp);
slouken@1895
  1183
            vector unsigned char vs = (vector unsigned char) vec_ld(0, srcp);
icculus@1047
  1184
            width -= extrawidth;
icculus@1047
  1185
            while (width) {
icculus@1047
  1186
                vector unsigned char voverflow;
icculus@1047
  1187
                vector unsigned char vd;
icculus@1047
  1188
icculus@1047
  1189
                /* s = *srcp */
slouken@1895
  1190
                voverflow = (vector unsigned char) vec_ld(15, srcp);
icculus@1047
  1191
                vs = vec_perm(vs, voverflow, valigner);
slouken@1895
  1192
icculus@1047
  1193
                /* d = *dstp */
slouken@1895
  1194
                vd = (vector unsigned char) vec_ld(0, dstp);
icculus@1047
  1195
icculus@1047
  1196
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1197
icculus@1047
  1198
                /* set the alpha channel to full on */
icculus@1047
  1199
                vd = vec_or(vd, valphamask);
icculus@1047
  1200
icculus@1047
  1201
                /* *dstp = res */
slouken@1895
  1202
                vec_st((vector unsigned int) vd, 0, dstp);
slouken@1895
  1203
icculus@1047
  1204
                srcp += 4;
icculus@1047
  1205
                dstp += 4;
icculus@1047
  1206
                width -= 4;
icculus@1047
  1207
                vs = voverflow;
icculus@1047
  1208
            }
icculus@1047
  1209
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1210
        }
icculus@1047
  1211
#undef ONE_PIXEL_BLEND
slouken@1895
  1212
icculus@1047
  1213
        srcp += srcskip;
icculus@1047
  1214
        dstp += dstskip;
icculus@1047
  1215
    }
icculus@1047
  1216
}
slouken@1895
  1217
slouken@1795
  1218
#if __MWERKS__
slouken@1795
  1219
#pragma altivec_model off
slouken@1795
  1220
#endif
slouken@1361
  1221
#endif /* SDL_ALTIVEC_BLITTERS */
icculus@1047
  1222
slouken@1
  1223
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1895
  1224
static void
slouken@1895
  1225
BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo * info)
slouken@0
  1226
{
slouken@2262
  1227
    int width = info->dst_w;
slouken@2262
  1228
    int height = info->dst_h;
slouken@2262
  1229
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1230
    int srcskip = info->src_skip >> 2;
slouken@2262
  1231
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1232
    int dstskip = info->dst_skip >> 2;
slouken@0
  1233
slouken@1895
  1234
    while (height--) {
slouken@1895
  1235
	    /* *INDENT-OFF* */
slouken@0
  1236
	    DUFFS_LOOP4({
slouken@1
  1237
		    Uint32 s = *srcp++;
slouken@1
  1238
		    Uint32 d = *dstp;
slouken@1
  1239
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1240
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1241
	    }, width);
slouken@1895
  1242
	    /* *INDENT-ON* */
slouken@1895
  1243
        srcp += srcskip;
slouken@1895
  1244
        dstp += dstskip;
slouken@1895
  1245
    }
slouken@0
  1246
}
slouken@0
  1247
slouken@1
  1248
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1895
  1249
static void
slouken@1895
  1250
BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1251
{
slouken@2267
  1252
    unsigned alpha = info->a;
slouken@1895
  1253
    if (alpha == 128) {
slouken@1895
  1254
        BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1895
  1255
    } else {
slouken@2262
  1256
        int width = info->dst_w;
slouken@2262
  1257
        int height = info->dst_h;
slouken@2262
  1258
        Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1259
        int srcskip = info->src_skip >> 2;
slouken@2262
  1260
        Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1261
        int dstskip = info->dst_skip >> 2;
slouken@1895
  1262
        Uint32 s;
slouken@1895
  1263
        Uint32 d;
slouken@1895
  1264
        Uint32 s1;
slouken@1895
  1265
        Uint32 d1;
slouken@1
  1266
slouken@1895
  1267
        while (height--) {
slouken@1895
  1268
			/* *INDENT-OFF* */
slouken@689
  1269
			DUFFS_LOOP_DOUBLE2({
slouken@689
  1270
				/* One Pixel Blend */
slouken@1
  1271
				s = *srcp;
slouken@1
  1272
				d = *dstp;
slouken@1
  1273
				s1 = s & 0xff00ff;
slouken@1
  1274
				d1 = d & 0xff00ff;
slouken@1
  1275
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1276
				     & 0xff00ff;
slouken@1
  1277
				s &= 0xff00;
slouken@1
  1278
				d &= 0xff00;
slouken@1
  1279
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1280
				*dstp = d1 | d | 0xff000000;
slouken@1
  1281
				++srcp;
slouken@1
  1282
				++dstp;
slouken@689
  1283
			},{
slouken@689
  1284
			        /* Two Pixels Blend */
slouken@689
  1285
				s = *srcp;
slouken@689
  1286
				d = *dstp;
slouken@689
  1287
				s1 = s & 0xff00ff;
slouken@689
  1288
				d1 = d & 0xff00ff;
slouken@689
  1289
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1290
				d1 &= 0xff00ff;
slouken@689
  1291
				     
slouken@689
  1292
				s = ((s & 0xff00) >> 8) | 
slouken@689
  1293
					((srcp[1] & 0xff00) << 8);
slouken@689
  1294
				d = ((d & 0xff00) >> 8) |
slouken@689
  1295
					((dstp[1] & 0xff00) << 8);
slouken@689
  1296
				d += (s - d) * alpha >> 8;
slouken@689
  1297
				d &= 0x00ff00ff;
slouken@689
  1298
				
slouken@689
  1299
				*dstp++ = d1 | ((d << 8) & 0xff00) | 0xff000000;
slouken@689
  1300
				++srcp;
slouken@689
  1301
				
slouken@689
  1302
			        s1 = *srcp;
slouken@689
  1303
				d1 = *dstp;
slouken@689
  1304
				s1 &= 0xff00ff;
slouken@689
  1305
				d1 &= 0xff00ff;
slouken@689
  1306
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1307
				d1 &= 0xff00ff;
slouken@689
  1308
				
slouken@689
  1309
				*dstp = d1 | ((d >> 8) & 0xff00) | 0xff000000;
slouken@689
  1310
				++srcp;
slouken@689
  1311
				++dstp;
slouken@1
  1312
			}, width);
slouken@1895
  1313
			/* *INDENT-ON* */
slouken@1895
  1314
            srcp += srcskip;
slouken@1895
  1315
            dstp += dstskip;
slouken@1895
  1316
        }
slouken@1895
  1317
    }
slouken@1
  1318
}
slouken@1
  1319
slouken@0
  1320
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1321
static void
slouken@1895
  1322
BlitRGBtoRGBPixelAlpha(SDL_BlitInfo * info)
slouken@0
  1323
{
slouken@2262
  1324
    int width = info->dst_w;
slouken@2262
  1325
    int height = info->dst_h;
slouken@2262
  1326
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1327
    int srcskip = info->src_skip >> 2;
slouken@2262
  1328
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1329
    int dstskip = info->dst_skip >> 2;
slouken@0
  1330
slouken@1895
  1331
    while (height--) {
slouken@1895
  1332
	    /* *INDENT-OFF* */
slouken@0
  1333
	    DUFFS_LOOP4({
slouken@0
  1334
		Uint32 dalpha;
slouken@0
  1335
		Uint32 d;
slouken@0
  1336
		Uint32 s1;
slouken@0
  1337
		Uint32 d1;
slouken@0
  1338
		Uint32 s = *srcp;
slouken@0
  1339
		Uint32 alpha = s >> 24;
slouken@0
  1340
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1341
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1342
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1343
		   Benchmark this! */
slouken@689
  1344
		if(alpha) {   
slouken@689
  1345
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1346
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1347
		  } else {
slouken@0
  1348
		    /*
slouken@0
  1349
		     * take out the middle component (green), and process
slouken@0
  1350
		     * the other two in parallel. One multiply less.
slouken@0
  1351
		     */
slouken@0
  1352
		    d = *dstp;
slouken@0
  1353
		    dalpha = d & 0xff000000;
slouken@0
  1354
		    s1 = s & 0xff00ff;
slouken@0
  1355
		    d1 = d & 0xff00ff;
slouken@0
  1356
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1357
		    s &= 0xff00;
slouken@0
  1358
		    d &= 0xff00;
slouken@0
  1359
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1360
		    *dstp = d1 | d | dalpha;
slouken@689
  1361
		  }
slouken@0
  1362
		}
slouken@0
  1363
		++srcp;
slouken@0
  1364
		++dstp;
slouken@0
  1365
	    }, width);
slouken@1895
  1366
	    /* *INDENT-ON* */
slouken@1895
  1367
        srcp += srcskip;
slouken@1895
  1368
        dstp += dstskip;
slouken@1895
  1369
    }
slouken@0
  1370
}
slouken@0
  1371
slouken@2261
  1372
#ifdef __3dNOW__
slouken@1542
  1373
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1895
  1374
static void
slouken@1895
  1375
BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo * info)
slouken@1542
  1376
{
slouken@2262
  1377
    int width = info->dst_w;
slouken@2262
  1378
    int height = info->dst_h;
slouken@2262
  1379
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1380
    int srcskip = info->src_skip >> 2;
slouken@2262
  1381
    Uint32 *dstp = (Uint32 *) info->dst;
slouken@2267
  1382
    int dstskip = info->dst_skip >> 2;
slouken@2267
  1383
    SDL_PixelFormat *sf = info->src_fmt;
slouken@1895
  1384
    Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1895
  1385
    Uint32 amask = sf->Amask;
slouken@1895
  1386
    Uint32 ashift = sf->Ashift;
slouken@1895
  1387
    Uint64 multmask;
slouken@1542
  1388
slouken@1895
  1389
    __m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
  1390
slouken@1895
  1391
    mm_zero = _mm_setzero_si64();       /* 0 -> mm_zero */
slouken@2260
  1392
    multmask = 0xFFFF;
slouken@2255
  1393
    multmask <<= (ashift * 2);
slouken@2255
  1394
    multmask = ~multmask;
slouken@1895
  1395
    dmask = *(__m64 *) & multmask;      /* dst alpha mask -> dmask */
slouken@1895
  1396
slouken@1895
  1397
    while (height--) {
slouken@1895
  1398
	    /* *INDENT-OFF* */
slouken@1542
  1399
	    DUFFS_LOOP4({
slouken@1542
  1400
		Uint32 alpha;
slouken@1542
  1401
slouken@1542
  1402
		_m_prefetch(srcp + 16);
slouken@1542
  1403
		_m_prefetch(dstp + 16);
slouken@1542
  1404
slouken@1542
  1405
		alpha = *srcp & amask;
slouken@1542
  1406
		if (alpha == 0) {
slouken@1542
  1407
			/* do nothing */
slouken@1542
  1408
		} else if (alpha == amask) {
slouken@1542
  1409
			/* copy RGB, keep dst alpha */
slouken@1542
  1410
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
  1411
		} else {
slouken@1542
  1412
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
  1413
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
  1414
slouken@1542
  1415
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
  1416
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
  1417
slouken@1542
  1418
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
  1419
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
  1420
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  1421
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  1422
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
  1423
slouken@1542
  1424
			/* blend */		    
slouken@1542
  1425
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@1542
  1426
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@1542
  1427
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
  1428
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@1542
  1429
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
  1430
			
slouken@1542
  1431
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
  1432
		}
slouken@1542
  1433
		++srcp;
slouken@1542
  1434
		++dstp;
slouken@1542
  1435
	    }, width);
slouken@1895
  1436
	    /* *INDENT-ON* */
slouken@1895
  1437
        srcp += srcskip;
slouken@1895
  1438
        dstp += dstskip;
slouken@1895
  1439
    }
slouken@1895
  1440
    _mm_empty();
slouken@1542
  1441
}
slouken@1895
  1442
slouken@2255
  1443
#endif /* __MMX__ */
slouken@689
  1444
slouken@1
  1445
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1446
slouken@1
  1447
/* blend a single 16 bit pixel at 50% */
slouken@1
  1448
#define BLEND16_50(d, s, mask)						\
slouken@1
  1449
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1450
slouken@1
  1451
/* blend two 16 bit pixels at 50% */
slouken@1
  1452
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1453
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1454
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1455
slouken@1895
  1456
static void
slouken@1895
  1457
Blit16to16SurfaceAlpha128(SDL_BlitInfo * info, Uint16 mask)
slouken@0
  1458
{
slouken@2262
  1459
    int width = info->dst_w;
slouken@2262
  1460
    int height = info->dst_h;
slouken@2262
  1461
    Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1462
    int srcskip = info->src_skip >> 1;
slouken@2262
  1463
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1464
    int dstskip = info->dst_skip >> 1;
slouken@0
  1465
slouken@1895
  1466
    while (height--) {
slouken@1895
  1467
        if (((uintptr_t) srcp ^ (uintptr_t) dstp) & 2) {
slouken@1895
  1468
            /*
slouken@1895
  1469
             * Source and destination not aligned, pipeline it.
slouken@1895
  1470
             * This is mostly a win for big blits but no loss for
slouken@1895
  1471
             * small ones
slouken@1895
  1472
             */
slouken@1895
  1473
            Uint32 prev_sw;
slouken@1895
  1474
            int w = width;
slouken@1
  1475
slouken@1895
  1476
            /* handle odd destination */
slouken@1895
  1477
            if ((uintptr_t) dstp & 2) {
slouken@1895
  1478
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1479
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1480
                dstp++;
slouken@1895
  1481
                srcp++;
slouken@1895
  1482
                w--;
slouken@1895
  1483
            }
slouken@1895
  1484
            srcp++;             /* srcp is now 32-bit aligned */
slouken@1
  1485
slouken@1895
  1486
            /* bootstrap pipeline with first halfword */
slouken@1895
  1487
            prev_sw = ((Uint32 *) srcp)[-1];
slouken@1
  1488
slouken@1895
  1489
            while (w > 1) {
slouken@1895
  1490
                Uint32 sw, dw, s;
slouken@1895
  1491
                sw = *(Uint32 *) srcp;
slouken@1895
  1492
                dw = *(Uint32 *) dstp;
slouken@1443
  1493
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1494
                s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1495
#else
slouken@1895
  1496
                s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1497
#endif
slouken@1895
  1498
                prev_sw = sw;
slouken@1895
  1499
                *(Uint32 *) dstp = BLEND2x16_50(dw, s, mask);
slouken@1895
  1500
                dstp += 2;
slouken@1895
  1501
                srcp += 2;
slouken@1895
  1502
                w -= 2;
slouken@1895
  1503
            }
slouken@1
  1504
slouken@1895
  1505
            /* final pixel if any */
slouken@1895
  1506
            if (w) {
slouken@1895
  1507
                Uint16 d = *dstp, s;
slouken@1443
  1508
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
  1509
                s = (Uint16) prev_sw;
slouken@1443
  1510
#else
slouken@1895
  1511
                s = (Uint16) (prev_sw >> 16);
slouken@1443
  1512
#endif
slouken@1895
  1513
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1514
                srcp++;
slouken@1895
  1515
                dstp++;
slouken@1895
  1516
            }
slouken@1895
  1517
            srcp += srcskip - 1;
slouken@1895
  1518
            dstp += dstskip;
slouken@1895
  1519
        } else {
slouken@1895
  1520
            /* source and destination are aligned */
slouken@1895
  1521
            int w = width;
slouken@1
  1522
slouken@1895
  1523
            /* first odd pixel? */
slouken@1895
  1524
            if ((uintptr_t) srcp & 2) {
slouken@1895
  1525
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1526
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1527
                srcp++;
slouken@1895
  1528
                dstp++;
slouken@1895
  1529
                w--;
slouken@1895
  1530
            }
slouken@1895
  1531
            /* srcp and dstp are now 32-bit aligned */
slouken@1
  1532
slouken@1895
  1533
            while (w > 1) {
slouken@1895
  1534
                Uint32 sw = *(Uint32 *) srcp;
slouken@1895
  1535
                Uint32 dw = *(Uint32 *) dstp;
slouken@1895
  1536
                *(Uint32 *) dstp = BLEND2x16_50(dw, sw, mask);
slouken@1895
  1537
                srcp += 2;
slouken@1895
  1538
                dstp += 2;
slouken@1895
  1539
                w -= 2;
slouken@1895
  1540
            }
slouken@1
  1541
slouken@1895
  1542
            /* last odd pixel? */
slouken@1895
  1543
            if (w) {
slouken@1895
  1544
                Uint16 d = *dstp, s = *srcp;
slouken@1895
  1545
                *dstp = BLEND16_50(d, s, mask);
slouken@1895
  1546
                srcp++;
slouken@1895
  1547
                dstp++;
slouken@1895
  1548
            }
slouken@1895
  1549
            srcp += srcskip;
slouken@1895
  1550
            dstp += dstskip;
slouken@1895
  1551
        }
slouken@1895
  1552
    }
slouken@1
  1553
}
slouken@1
  1554
slouken@2255
  1555
#ifdef __MMX__
slouken@689
  1556
slouken@1542
  1557
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1558
static void
slouken@1895
  1559
Blit565to565SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1560
{
slouken@2267
  1561
    unsigned alpha = info->a;
slouken@1895
  1562
    if (alpha == 128) {
slouken@1895
  1563
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1564
    } else {
slouken@2262
  1565
        int width = info->dst_w;
slouken@2262
  1566
        int height = info->dst_h;
slouken@2262
  1567
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1568
        int srcskip = info->src_skip >> 1;
slouken@2262
  1569
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1570
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1571
        Uint32 s, d;
slouken@1542
  1572
slouken@1895
  1573
        __m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1574
slouken@1895
  1575
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1576
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1577
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1895
  1578
slouken@1895
  1579
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1580
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1581
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1582
           to reduce the number of operations */
slouken@1895
  1583
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1584
slouken@1895
  1585
        /* Setup the 565 color channel masks */
slouken@1895
  1586
        gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0);   /* MASKGREEN -> gmask */
slouken@1895
  1587
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1588
slouken@1895
  1589
        while (height--) {
slouken@1895
  1590
			/* *INDENT-OFF* */
slouken@1542
  1591
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1592
			{
slouken@1542
  1593
				s = *srcp++;
slouken@1542
  1594
				d = *dstp;
slouken@1542
  1595
				/*
slouken@1542
  1596
				 * shift out the middle component (green) to
slouken@1542
  1597
				 * the high 16 bits, and process all three RGB
slouken@1542
  1598
				 * components at the same time.
slouken@1542
  1599
				 */
slouken@1542
  1600
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1601
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1602
				d += (s - d) * alpha >> 5;
slouken@1542
  1603
				d &= 0x07e0f81f;
slouken@1546
  1604
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1605
			},{
slouken@1542
  1606
				s = *srcp++;
slouken@1542
  1607
				d = *dstp;
slouken@1542
  1608
				/*
slouken@1542
  1609
				 * shift out the middle component (green) to
slouken@1542
  1610
				 * the high 16 bits, and process all three RGB
slouken@1542
  1611
				 * components at the same time.
slouken@1542
  1612
				 */
slouken@1542
  1613
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1614
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1615
				d += (s - d) * alpha >> 5;
slouken@1542
  1616
				d &= 0x07e0f81f;
slouken@1546
  1617
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1618
				s = *srcp++;
slouken@1542
  1619
				d = *dstp;
slouken@1542
  1620
				/*
slouken@1542
  1621
				 * shift out the middle component (green) to
slouken@1542
  1622
				 * the high 16 bits, and process all three RGB
slouken@1542
  1623
				 * components at the same time.
slouken@1542
  1624
				 */
slouken@1542
  1625
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  1626
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  1627
				d += (s - d) * alpha >> 5;
slouken@1542
  1628
				d &= 0x07e0f81f;
slouken@1546
  1629
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1630
			},{
slouken@1542
  1631
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1632
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1633
slouken@1542
  1634
				/* red */
slouken@1542
  1635
				src2 = src1;
slouken@1542
  1636
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  1637
slouken@1542
  1638
				dst2 = dst1;
slouken@1542
  1639
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  1640
slouken@1542
  1641
				/* blend */
slouken@1542
  1642
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1643
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1644
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1645
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1646
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  1647
slouken@1542
  1648
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1649
slouken@1542
  1650
				/* green -- process the bits in place */
slouken@1542
  1651
				src2 = src1;
slouken@1542
  1652
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1653
slouken@1542
  1654
				dst2 = dst1;
slouken@1542
  1655
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1656
slouken@1542
  1657
				/* blend */
slouken@1542
  1658
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1659
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1660
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1661
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1662
slouken@1542
  1663
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1664
slouken@1542
  1665
				/* blue */
slouken@1542
  1666
				src2 = src1;
slouken@1542
  1667
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1668
slouken@1542
  1669
				dst2 = dst1;
slouken@1542
  1670
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1671
slouken@1542
  1672
				/* blend */
slouken@1542
  1673
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1674
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1675
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1676
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1677
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1678
slouken@1542
  1679
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1680
slouken@1542
  1681
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1682
slouken@1542
  1683
				srcp += 4;
slouken@1542
  1684
				dstp += 4;
slouken@1895
  1685
			}, width);
slouken@1895
  1686
			/* *INDENT-ON* */
slouken@1895
  1687
            srcp += srcskip;
slouken@1895
  1688
            dstp += dstskip;
slouken@1895
  1689
        }
slouken@1895
  1690
        _mm_empty();
slouken@1895
  1691
    }
slouken@1542
  1692
}
slouken@1542
  1693
slouken@1542
  1694
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1695
static void
slouken@1895
  1696
Blit555to555SurfaceAlphaMMX(SDL_BlitInfo * info)
slouken@1542
  1697
{
slouken@2267
  1698
    unsigned alpha = info->a;
slouken@1895
  1699
    if (alpha == 128) {
slouken@1895
  1700
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1701
    } else {
slouken@2262
  1702
        int width = info->dst_w;
slouken@2262
  1703
        int height = info->dst_h;
slouken@2262
  1704
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1705
        int srcskip = info->src_skip >> 1;
slouken@2262
  1706
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1707
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1708
        Uint32 s, d;
slouken@1542
  1709
slouken@1895
  1710
        __m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  1711
slouken@1895
  1712
        alpha &= ~(1 + 2 + 4);  /* cut alpha to get the exact same behaviour */
slouken@1895
  1713
        mm_alpha = _mm_set_pi32(0, alpha);      /* 0000000A -> mm_alpha */
slouken@1895
  1714
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1542
  1715
slouken@1895
  1716
        mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha);       /* 00000A0A -> mm_alpha */
slouken@1895
  1717
        mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha);       /* 0A0A0A0A -> mm_alpha */
slouken@1895
  1718
        /* position alpha to allow for mullo and mulhi on diff channels
slouken@1895
  1719
           to reduce the number of operations */
slouken@1895
  1720
        mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1895
  1721
slouken@1895
  1722
        /* Setup the 555 color channel masks */
slouken@1895
  1723
        rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00);   /* MASKRED -> rmask */
slouken@1895
  1724
        gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0);   /* MASKGREEN -> gmask */
slouken@1895
  1725
        bmask = _mm_set_pi32(0x001F001F, 0x001F001F);   /* MASKBLUE -> bmask */
slouken@1895
  1726
slouken@1895
  1727
        while (height--) {
slouken@1895
  1728
			/* *INDENT-OFF* */
slouken@1542
  1729
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1730
			{
slouken@1542
  1731
				s = *srcp++;
slouken@1542
  1732
				d = *dstp;
slouken@1542
  1733
				/*
slouken@1542
  1734
				 * shift out the middle component (green) to
slouken@1542
  1735
				 * the high 16 bits, and process all three RGB
slouken@1542
  1736
				 * components at the same time.
slouken@1542
  1737
				 */
slouken@1542
  1738
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1739
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1740
				d += (s - d) * alpha >> 5;
slouken@1542
  1741
				d &= 0x03e07c1f;
slouken@1546
  1742
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1743
			},{
slouken@1542
  1744
				s = *srcp++;
slouken@1542
  1745
				d = *dstp;
slouken@1542
  1746
				/*
slouken@1542
  1747
				 * shift out the middle component (green) to
slouken@1542
  1748
				 * the high 16 bits, and process all three RGB
slouken@1542
  1749
				 * components at the same time.
slouken@1542
  1750
				 */
slouken@1542
  1751
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1752
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1753
				d += (s - d) * alpha >> 5;
slouken@1542
  1754
				d &= 0x03e07c1f;
slouken@1546
  1755
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1756
			        s = *srcp++;
slouken@1542
  1757
				d = *dstp;
slouken@1542
  1758
				/*
slouken@1542
  1759
				 * shift out the middle component (green) to
slouken@1542
  1760
				 * the high 16 bits, and process all three RGB
slouken@1542
  1761
				 * components at the same time.
slouken@1542
  1762
				 */
slouken@1542
  1763
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  1764
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  1765
				d += (s - d) * alpha >> 5;
slouken@1542
  1766
				d &= 0x03e07c1f;
slouken@1546
  1767
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  1768
			},{
slouken@1542
  1769
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  1770
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  1771
slouken@1542
  1772
				/* red -- process the bits in place */
slouken@1542
  1773
				src2 = src1;
slouken@1542
  1774
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  1775
slouken@1542
  1776
				dst2 = dst1;
slouken@1542
  1777
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  1778
slouken@1542
  1779
				/* blend */
slouken@1542
  1780
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1781
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1782
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1783
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1784
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  1785
slouken@1542
  1786
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  1787
				
slouken@1542
  1788
				/* green -- process the bits in place */
slouken@1542
  1789
				src2 = src1;
slouken@1542
  1790
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  1791
slouken@1542
  1792
				dst2 = dst1;
slouken@1542
  1793
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  1794
slouken@1542
  1795
				/* blend */
slouken@1542
  1796
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1797
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1798
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  1799
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1800
slouken@1542
  1801
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  1802
slouken@1542
  1803
				/* blue */
slouken@1542
  1804
				src2 = src1; /* src -> src2 */
slouken@1542
  1805
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  1806
slouken@1542
  1807
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  1808
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  1809
slouken@1542
  1810
				/* blend */
slouken@1542
  1811
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  1812
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  1813
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  1814
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  1815
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  1816
slouken@1542
  1817
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  1818
slouken@1542
  1819
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  1820
slouken@1542
  1821
				srcp += 4;
slouken@1542
  1822
				dstp += 4;
slouken@1895
  1823
			}, width);
slouken@1895
  1824
			/* *INDENT-ON* */
slouken@1895
  1825
            srcp += srcskip;
slouken@1895
  1826
            dstp += dstskip;
slouken@1895
  1827
        }
slouken@1895
  1828
        _mm_empty();
slouken@1895
  1829
    }
slouken@1542
  1830
}
slouken@2255
  1831
slouken@2255
  1832
#endif /* __MMX__ */
slouken@689
  1833
slouken@1
  1834
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1895
  1835
static void
slouken@1895
  1836
Blit565to565SurfaceAlpha(SDL_BlitInfo * info)
slouken@1
  1837
{
slouken@2267
  1838
    unsigned alpha = info->a;
slouken@1895
  1839
    if (alpha == 128) {
slouken@1895
  1840
        Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1895
  1841
    } else {
slouken@2262
  1842
        int width = info->dst_w;
slouken@2262
  1843
        int height = info->dst_h;
slouken@2262
  1844
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1845
        int srcskip = info->src_skip >> 1;
slouken@2262
  1846
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1847
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1848
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@1
  1849
slouken@1895
  1850
        while (height--) {
slouken@1895
  1851
			/* *INDENT-OFF* */
slouken@1
  1852
			DUFFS_LOOP4({
slouken@1
  1853
				Uint32 s = *srcp++;
slouken@1
  1854
				Uint32 d = *dstp;
slouken@1
  1855
				/*
slouken@1
  1856
				 * shift out the middle component (green) to
slouken@1
  1857
				 * the high 16 bits, and process all three RGB
slouken@1
  1858
				 * components at the same time.
slouken@1
  1859
				 */
slouken@1
  1860
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  1861
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  1862
				d += (s - d) * alpha >> 5;
slouken@1
  1863
				d &= 0x07e0f81f;
slouken@1428
  1864
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1865
			}, width);
slouken@1895
  1866
			/* *INDENT-ON* */
slouken@1895
  1867
            srcp += srcskip;
slouken@1895
  1868
            dstp += dstskip;
slouken@1895
  1869
        }
slouken@1895
  1870
    }
slouken@0
  1871
}
slouken@0
  1872
slouken@0
  1873
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1895
  1874
static void
slouken@1895
  1875
Blit555to555SurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  1876
{
slouken@2267
  1877
    unsigned alpha = info->a;   /* downscale alpha to 5 bits */
slouken@1895
  1878
    if (alpha == 128) {
slouken@1895
  1879
        Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1895
  1880
    } else {
slouken@2262
  1881
        int width = info->dst_w;
slouken@2262
  1882
        int height = info->dst_h;
slouken@2262
  1883
        Uint16 *srcp = (Uint16 *) info->src;
slouken@2267
  1884
        int srcskip = info->src_skip >> 1;
slouken@2262
  1885
        Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1886
        int dstskip = info->dst_skip >> 1;
slouken@1895
  1887
        alpha >>= 3;            /* downscale alpha to 5 bits */
slouken@0
  1888
slouken@1895
  1889
        while (height--) {
slouken@1895
  1890
			/* *INDENT-OFF* */
slouken@1
  1891
			DUFFS_LOOP4({
slouken@1
  1892
				Uint32 s = *srcp++;
slouken@1
  1893
				Uint32 d = *dstp;
slouken@1
  1894
				/*
slouken@1
  1895
				 * shift out the middle component (green) to
slouken@1
  1896
				 * the high 16 bits, and process all three RGB
slouken@1
  1897
				 * components at the same time.
slouken@1
  1898
				 */
slouken@1
  1899
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  1900
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  1901
				d += (s - d) * alpha >> 5;
slouken@1
  1902
				d &= 0x03e07c1f;
slouken@1428
  1903
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  1904
			}, width);
slouken@1895
  1905
			/* *INDENT-ON* */
slouken@1895
  1906
            srcp += srcskip;
slouken@1895
  1907
            dstp += dstskip;
slouken@1895
  1908
        }
slouken@1895
  1909
    }
slouken@0
  1910
}
slouken@0
  1911
slouken@0
  1912
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@1895
  1913
static void
slouken@1895
  1914
BlitARGBto565PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1915
{
slouken@2262
  1916
    int width = info->dst_w;
slouken@2262
  1917
    int height = info->dst_h;
slouken@2262
  1918
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1919
    int srcskip = info->src_skip >> 2;
slouken@2262
  1920
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1921
    int dstskip = info->dst_skip >> 1;
slouken@0
  1922
slouken@1895
  1923
    while (height--) {
slouken@1895
  1924
	    /* *INDENT-OFF* */
slouken@0
  1925
	    DUFFS_LOOP4({
slouken@0
  1926
		Uint32 s = *srcp;
slouken@0
  1927
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1928
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1929
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1930
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1931
		   Benchmark this! */
slouken@689
  1932
		if(alpha) {   
slouken@689
  1933
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1934
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  1935
		  } else {
slouken@0
  1936
		    Uint32 d = *dstp;
slouken@0
  1937
		    /*
slouken@0
  1938
		     * convert source and destination to G0RAB65565
slouken@0
  1939
		     * and blend all components at the same time
slouken@0
  1940
		     */
slouken@0
  1941
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  1942
		      + (s >> 3 & 0x1f);
slouken@0
  1943
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  1944
		    d += (s - d) * alpha >> 5;
slouken@0
  1945
		    d &= 0x07e0f81f;
slouken@1428
  1946
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1947
		  }
slouken@0
  1948
		}
slouken@0
  1949
		srcp++;
slouken@0
  1950
		dstp++;
slouken@0
  1951
	    }, width);
slouken@1895
  1952
	    /* *INDENT-ON* */
slouken@1895
  1953
        srcp += srcskip;
slouken@1895
  1954
        dstp += dstskip;
slouken@1895
  1955
    }
slouken@0
  1956
}
slouken@0
  1957
slouken@0
  1958
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@1895
  1959
static void
slouken@1895
  1960
BlitARGBto555PixelAlpha(SDL_BlitInfo * info)
slouken@0
  1961
{
slouken@2262
  1962
    int width = info->dst_w;
slouken@2262
  1963
    int height = info->dst_h;
slouken@2262
  1964
    Uint32 *srcp = (Uint32 *) info->src;
slouken@2267
  1965
    int srcskip = info->src_skip >> 2;
slouken@2262
  1966
    Uint16 *dstp = (Uint16 *) info->dst;
slouken@2267
  1967
    int dstskip = info->dst_skip >> 1;
slouken@0
  1968
slouken@1895
  1969
    while (height--) {
slouken@1895
  1970
	    /* *INDENT-OFF* */
slouken@0
  1971
	    DUFFS_LOOP4({
slouken@0
  1972
		unsigned alpha;
slouken@0
  1973
		Uint32 s = *srcp;
slouken@0
  1974
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  1975
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1976
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1977
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1978
		   Benchmark this! */
slouken@689
  1979
		if(alpha) {   
slouken@689
  1980
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  1981
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  1982
		  } else {
slouken@0
  1983
		    Uint32 d = *dstp;
slouken@0
  1984
		    /*
slouken@0
  1985
		     * convert source and destination to G0RAB65565
slouken@0
  1986
		     * and blend all components at the same time
slouken@0
  1987
		     */
slouken@0
  1988
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  1989
		      + (s >> 3 & 0x1f);
slouken@0
  1990
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  1991
		    d += (s - d) * alpha >> 5;
slouken@0
  1992
		    d &= 0x03e07c1f;
slouken@1428
  1993
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  1994
		  }
slouken@0
  1995
		}
slouken@0
  1996
		srcp++;
slouken@0
  1997
		dstp++;
slouken@0
  1998
	    }, width);
slouken@1895
  1999
	    /* *INDENT-ON* */
slouken@1895
  2000
        srcp += srcskip;
slouken@1895
  2001
        dstp += dstskip;
slouken@1895
  2002
    }
slouken@0
  2003
}
slouken@0
  2004
slouken@0
  2005
/* General (slow) N->N blending with per-surface alpha */
slouken@1895
  2006
static void
slouken@1895
  2007
BlitNtoNSurfaceAlpha(SDL_BlitInfo * info)
slouken@0
  2008
{
slouken@2262
  2009
    int width = info->dst_w;
slouken@2262
  2010
    int height = info->dst_h;
slouken@2262
  2011
    Uint8 *src = info->src;
slouken@2267
  2012
    int srcskip = info->src_skip;
slouken@2262
  2013
    Uint8 *dst = info->dst;
slouken@2267
  2014
    int dstskip = info->dst_skip;
slouken@2267
  2015
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  2016
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@1895
  2017
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2018
    int dstbpp = dstfmt->BytesPerPixel;
slouken@2267
  2019
    unsigned sA = info->a;
slouken@1895
  2020
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2021
slouken@1895
  2022
    if (sA) {
slouken@1895
  2023
        while (height--) {
slouken@1895
  2024
	    /* *INDENT-OFF* */
slouken@0
  2025
	    DUFFS_LOOP4(
slouken@0
  2026
	    {
icculus@1162
  2027
		Uint32 Pixel;
slouken@0
  2028
		unsigned sR;
slouken@0
  2029
		unsigned sG;
slouken@0
  2030
		unsigned sB;
slouken@0
  2031
		unsigned dR;
slouken@0
  2032
		unsigned dG;
slouken@0
  2033
		unsigned dB;
icculus@1162
  2034
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2035
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2036
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2037
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2038
		src += srcbpp;
slouken@0
  2039
		dst += dstbpp;
slouken@0
  2040
	    },
slouken@0
  2041
	    width);
slouken@1895
  2042
	    /* *INDENT-ON* */
slouken@1895
  2043
            src += srcskip;
slouken@1895
  2044
            dst += dstskip;
slouken@1895
  2045
        }
slouken@1895
  2046
    }
slouken@0
  2047
}
slouken@0
  2048
slouken@0
  2049
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@1895
  2050
static void
slouken@1895
  2051
BlitNtoNSurfaceAlphaKey(SDL_BlitInfo * info)
slouken@0
  2052
{
slouken@2262
  2053
    int width = info->dst_w;
slouken@2262
  2054
    int height = info->dst_h;
slouken@2262
  2055
    Uint8 *src = info->src;
slouken@2267
  2056
    int srcskip = info->src_skip;
slouken@2262
  2057
    Uint8 *dst = info->dst;
slouken@2267
  2058
    int dstskip = info->dst_skip;
slouken@2267
  2059
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  2060
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@2267
  2061
    Uint32 ckey = info->colorkey;
slouken@1895
  2062
    int srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2063
    int dstbpp = dstfmt->BytesPerPixel;
slouken@2267
  2064
    unsigned sA = info->a;
slouken@1895
  2065
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2066
slouken@1895
  2067
    while (height--) {
slouken@1895
  2068
	    /* *INDENT-OFF* */
slouken@0
  2069
	    DUFFS_LOOP4(
slouken@0
  2070
	    {
icculus@1162
  2071
		Uint32 Pixel;
slouken@0
  2072
		unsigned sR;
slouken@0
  2073
		unsigned sG;
slouken@0
  2074
		unsigned sB;
slouken@0
  2075
		unsigned dR;
slouken@0
  2076
		unsigned dG;
slouken@0
  2077
		unsigned dB;
icculus@1162
  2078
		RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
icculus@1162
  2079
		if(sA && Pixel != ckey) {
icculus@1162
  2080
		    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
icculus@1162
  2081
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2082
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2083
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2084
		}
slouken@0
  2085
		src += srcbpp;
slouken@0
  2086
		dst += dstbpp;
slouken@0
  2087
	    },
slouken@0
  2088
	    width);
slouken@1895
  2089
	    /* *INDENT-ON* */
slouken@1895
  2090
        src += srcskip;
slouken@1895
  2091
        dst += dstskip;
slouken@1895
  2092
    }
slouken@0
  2093
}
slouken@0
  2094
slouken@0
  2095
/* General (slow) N->N blending with pixel alpha */
slouken@1895
  2096
static void
slouken@1895
  2097
BlitNtoNPixelAlpha(SDL_BlitInfo * info)
slouken@0
  2098
{
slouken@2262
  2099
    int width = info->dst_w;
slouken@2262
  2100
    int height = info->dst_h;
slouken@2262
  2101
    Uint8 *src = info->src;
slouken@2267
  2102
    int srcskip = info->src_skip;
slouken@2262
  2103
    Uint8 *dst = info->dst;
slouken@2267
  2104
    int dstskip = info->dst_skip;
slouken@2267
  2105
    SDL_PixelFormat *srcfmt = info->src_fmt;
slouken@2267
  2106
    SDL_PixelFormat *dstfmt = info->dst_fmt;
slouken@0
  2107
slouken@1895
  2108
    int srcbpp;
slouken@1895
  2109
    int dstbpp;
slouken@0
  2110
slouken@1895
  2111
    /* Set up some basic variables */
slouken@1895
  2112
    srcbpp = srcfmt->BytesPerPixel;
slouken@1895
  2113
    dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2114
slouken@1895
  2115
    /* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@1895
  2116
       quite right. for <8bpp source alpha, it gets them very wrong
slouken@1895
  2117
       (check all macros!)
slouken@1895
  2118
       It is unclear whether there is a good general solution that doesn't
slouken@1895
  2119
       need a branch (or a divide). */
slouken@1895
  2120
    while (height--) {
slouken@1895
  2121
	    /* *INDENT-OFF* */
slouken@0
  2122
	    DUFFS_LOOP4(
slouken@0
  2123
	    {
icculus@1162
  2124
		Uint32 Pixel;
slouken@0
  2125
		unsigned sR;
slouken@0
  2126
		unsigned sG;
slouken@0
  2127
		unsigned sB;
slouken@0
  2128
		unsigned dR;
slouken@0
  2129
		unsigned dG;
slouken@0
  2130
		unsigned dB;
slouken@0
  2131
		unsigned sA;
slouken@0
  2132
		unsigned dA;
icculus@1162
  2133
		DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
slouken@689
  2134
		if(sA) {
icculus@1162
  2135
		  DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
slouken@689
  2136
		  ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@689
  2137
		  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@689
  2138
		}
slouken@0
  2139
		src += srcbpp;
slouken@0
  2140
		dst += dstbpp;
slouken@0
  2141
	    },
slouken@0
  2142
	    width);
slouken@1895
  2143
	    /* *INDENT-ON* */
slouken@1895
  2144
        src += srcskip;
slouken@1895
  2145
        dst += dstskip;
slouken@1895
  2146
    }
slouken@0
  2147
}
slouken@0
  2148
slouken@0
  2149
slouken@2267
  2150
SDL_BlitFunc
slouken@2267
  2151
SDL_CalculateBlitA(SDL_Surface * surface)
slouken@0
  2152
{
slouken@0
  2153
    SDL_PixelFormat *sf = surface->format;
slouken@0
  2154
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
  2155
slouken@2853
  2156
    switch (surface->map->info.flags & ~SDL_COPY_RLE_MASK) {
slouken@2267
  2157
    case SDL_COPY_BLEND:
slouken@1895
  2158
        /* Per-pixel alpha blits */
slouken@1895
  2159
        switch (df->BytesPerPixel) {
slouken@1895
  2160
        case 1:
slouken@1895
  2161
            return BlitNto1PixelAlpha;
slouken@0
  2162
slouken@1895
  2163
        case 2:
slouken@1361
  2164
#if SDL_ALTIVEC_BLITTERS
slouken@1895
  2165
            if (sf->BytesPerPixel == 4
slouken@1895
  2166
                && df->Gmask == 0x7e0 && df->Bmask == 0x1f
slouken@1895
  2167
                && SDL_HasAltiVec())
slouken@1895
  2168
                return Blit32to565PixelAlphaAltivec;
slouken@1895
  2169
            else
icculus@1047
  2170
#endif
slouken@1895
  2171
                if (sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@1895
  2172
                    && sf->Gmask == 0xff00
slouken@1895
  2173
                    && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@1895
  2174
                        || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@1895
  2175
                if (df->Gmask == 0x7e0)
slouken@1895
  2176
                    return BlitARGBto565PixelAlpha;
slouken@1895
  2177
                else if (df->Gmask == 0x3e0)
slouken@1895
  2178
                    return BlitARGBto555PixelAlpha;
slouken@1895
  2179
            }
slouken@1895
  2180
            return BlitNtoNPixelAlpha;
slouken@0
  2181
slouken@1895
  2182
        case 4:
slouken@1895
  2183
            if (sf->Rmask == df->Rmask
slouken@1895
  2184
                && sf->Gmask == df->Gmask
slouken@1895
  2185
                && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@2261
  2186
#if defined(__MMX__) || defined(__3dNOW__)
slouken@1895
  2187
                if (sf->Rshift % 8 == 0
slouken@1895
  2188
                    && sf->Gshift % 8 == 0
slouken@1895
  2189
                    && sf->Bshift % 8 == 0
slouken@1895
  2190
                    && sf->Ashift % 8 == 0 && sf->Aloss == 0) {
slouken@2261
  2191
#ifdef __3dNOW__
slouken@1895
  2192
                    if (SDL_Has3DNow())
slouken@1895
  2193
                        return BlitRGBtoRGBPixelAlphaMMX3DNOW;
slouken@2261
  2194
#endif
slouken@2261
  2195
#ifdef __MMX__
slouken@1895
  2196
                    if (SDL_HasMMX())
slouken@1895
  2197
                        return BlitRGBtoRGBPixelAlphaMMX;
slouken@2261
  2198
#endif
slouken@1895
  2199
                }
slouken@2261
  2200
#endif /* __MMX__ || __3dNOW__ */
slouken@1895
  2201
                if (sf->Amask == 0xff000000) {
slouken@1617
  2202
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2203
                    if (SDL_HasAltiVec())
slouken@1895
  2204
                        return BlitRGBtoRGBPixelAlphaAltivec;
icculus@1047
  2205
#endif
slouken@1895
  2206
                    return BlitRGBtoRGBPixelAlpha;
slouken@1895
  2207
                }
slouken@1895
  2208
            }
slouken@1361
  2209
#if SDL_ALTIVEC_BLITTERS
slouken@2231
  2210
            if (sf->Amask && sf->BytesPerPixel == 4 && SDL_HasAltiVec())
slouken@1895
  2211
                return Blit32to32PixelAlphaAltivec;
slouken@1895
  2212
            else
icculus@1047
  2213
#endif
slouken@1895
  2214
                return BlitNtoNPixelAlpha;
slouken@0
  2215
slouken@1895
  2216
        case 3:
slouken@1895
  2217
        default:
slouken@1895
  2218
            return BlitNtoNPixelAlpha;
slouken@1895
  2219
        }
slouken@2267
  2220
        break;
slouken@2267
  2221
slouken@2267
  2222
    case SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
slouken@2267
  2223
        if (sf->Amask == 0) {
slouken@2267
  2224
            /* Per-surface alpha blits */
slouken@2267
  2225
            switch (df->BytesPerPixel) {
slouken@2267
  2226
            case 1:
slouken@2267
  2227
                return BlitNto1SurfaceAlpha;
slouken@2267
  2228
slouken@2267
  2229
            case 2:
slouken@2267
  2230
                if (surface->map->identity) {
slouken@2267
  2231
                    if (df->Gmask == 0x7e0) {
slouken@2267
  2232
#ifdef __MMX__
slouken@2267
  2233
                        if (SDL_HasMMX())
slouken@2267
  2234
                            return Blit565to565SurfaceAlphaMMX;
slouken@2267
  2235
                        else
slouken@2267
  2236
#endif
slouken@2267
  2237
                            return Blit565to565SurfaceAlpha;
slouken@2267
  2238
                    } else if (df->Gmask == 0x3e0) {
slouken@2267
  2239
#ifdef __MMX__
slouken@2267
  2240
                        if (SDL_HasMMX())
slouken@2267
  2241
                            return Blit555to555SurfaceAlphaMMX;
slouken@2267
  2242
                        else
slouken@2267
  2243
#endif
slouken@2267
  2244
                            return Blit555to555SurfaceAlpha;
slouken@2267
  2245
                    }
slouken@2267
  2246
                }
slouken@2267
  2247
                return BlitNtoNSurfaceAlpha;
slouken@2267
  2248
slouken@2267
  2249
            case 4:
slouken@2267
  2250
                if (sf->Rmask == df->Rmask
slouken@2267
  2251
                    && sf->Gmask == df->Gmask
slouken@2267
  2252
                    && sf->Bmask == df->Bmask && sf->BytesPerPixel == 4) {
slouken@2267
  2253
#ifdef __MMX__
slouken@2267
  2254
                    if (sf->Rshift % 8 == 0
slouken@2267
  2255
                        && sf->Gshift % 8 == 0
slouken@2267
  2256
                        && sf->Bshift % 8 == 0 && SDL_HasMMX())
slouken@2267
  2257
                        return BlitRGBtoRGBSurfaceAlphaMMX;
slouken@2267
  2258
#endif
slouken@2267
  2259
                    if ((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff) {
slouken@2267
  2260
#if SDL_ALTIVEC_BLITTERS
slouken@2267
  2261
                        if (SDL_HasAltiVec())
slouken@2267
  2262
                            return BlitRGBtoRGBSurfaceAlphaAltivec;
slouken@2267
  2263
#endif
slouken@2267
  2264
                        return BlitRGBtoRGBSurfaceAlpha;
slouken@2267
  2265
                    }
slouken@2267
  2266
                }
slouken@2267
  2267
#if SDL_ALTIVEC_BLITTERS
slouken@2267
  2268
                if ((sf->BytesPerPixel == 4) && SDL_HasAltiVec())
slouken@2267
  2269
                    return Blit32to32SurfaceAlphaAltivec;
slouken@2267
  2270
                else
slouken@2267
  2271
#endif
slouken@2267
  2272
                    return BlitNtoNSurfaceAlpha;
slouken@2267
  2273
slouken@2267
  2274
            case 3:
slouken@2267
  2275
            default:
slouken@2267
  2276
                return BlitNtoNSurfaceAlpha;
slouken@2267
  2277
            }
slouken@2267
  2278
        }
slouken@2267
  2279
        break;
slouken@2267
  2280
slouken@2267
  2281
    case SDL_COPY_COLORKEY | SDL_COPY_MODULATE_ALPHA | SDL_COPY_BLEND:
slouken@2267
  2282
        if (sf->Amask == 0) {
slouken@2267
  2283
            if (df->BytesPerPixel == 1)
slouken@2267
  2284
                return BlitNto1SurfaceAlphaKey;
slouken@2267
  2285
            else
slouken@2267
  2286
#if SDL_ALTIVEC_BLITTERS
slouken@2267
  2287
            if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
slouken@2267
  2288
                    SDL_HasAltiVec())
slouken@2267
  2289
                return Blit32to32SurfaceAlphaKeyAltivec;
slouken@2267
  2290
            else
slouken@2267
  2291
#endif
slouken@2267
  2292
                return BlitNtoNSurfaceAlphaKey;
slouken@2267
  2293
        }
slouken@2267
  2294
        break;
slouken@0
  2295
    }
slouken@2267
  2296
slouken@2267
  2297
    return NULL;
slouken@0
  2298
}
slouken@0
  2299
slouken@1895
  2300
/* vi: set ts=4 sw=4 expandtab: */