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