src/video/SDL_blit_A.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 4262 800f3cb78e45
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#include "SDL_video.h"
slouken@0
    25
#include "SDL_blit.h"
slouken@0
    26
icculus@3973
    27
/*
icculus@3973
    28
  In Visual C, VC6 has mmintrin.h in the "Processor Pack" add-on.
icculus@3973
    29
   Checking if _mm_free is #defined in malloc.h is is the only way to
icculus@3973
    30
   determine if the Processor Pack is installed, as far as I can tell.
icculus@3973
    31
*/
icculus@3973
    32
slouken@1542
    33
#if SDL_ASSEMBLY_ROUTINES
icculus@3973
    34
#  if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
icculus@3973
    35
#    define MMX_ASMBLIT 1
icculus@3973
    36
#    define GCC_ASMBLIT 1
icculus@3973
    37
#  elif defined(_MSC_VER) && defined(_M_IX86)
icculus@3973
    38
#    if (_MSC_VER <= 1200)  
icculus@3973
    39
#      include <malloc.h>   
icculus@3973
    40
#      if defined(_mm_free)
icculus@3973
    41
#          define HAVE_MMINTRIN_H 1
icculus@3973
    42
#      endif
icculus@3973
    43
#    else  /* Visual Studio > VC6 always has mmintrin.h */
icculus@3973
    44
#      define HAVE_MMINTRIN_H 1
icculus@3973
    45
#    endif
icculus@3973
    46
#    if HAVE_MMINTRIN_H
icculus@3973
    47
#      define MMX_ASMBLIT 1
icculus@3973
    48
#      define MSVC_ASMBLIT 1
icculus@3973
    49
#    endif
icculus@3973
    50
#  endif
slouken@1542
    51
#endif /* SDL_ASSEMBLY_ROUTINES */
slouken@880
    52
slouken@739
    53
/* Function to check the CPU flags */
slouken@739
    54
#include "SDL_cpuinfo.h"
slouken@1542
    55
#if GCC_ASMBLIT
slouken@689
    56
#include "mmx.h"
slouken@1542
    57
#elif MSVC_ASMBLIT
slouken@1542
    58
#include <mmintrin.h>
slouken@1542
    59
#include <mm3dnow.h>
slouken@689
    60
#endif
slouken@689
    61
slouken@0
    62
/* Functions to perform alpha blended blitting */
slouken@0
    63
slouken@0
    64
/* N->1 blending with per-surface alpha */
slouken@0
    65
static void BlitNto1SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
    66
{
slouken@0
    67
	int width = info->d_width;
slouken@0
    68
	int height = info->d_height;
slouken@0
    69
	Uint8 *src = info->s_pixels;
slouken@0
    70
	int srcskip = info->s_skip;
slouken@0
    71
	Uint8 *dst = info->d_pixels;
slouken@0
    72
	int dstskip = info->d_skip;
slouken@0
    73
	Uint8 *palmap = info->table;
slouken@0
    74
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
    75
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
    76
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
    77
slouken@0
    78
	const unsigned A = srcfmt->alpha;
slouken@0
    79
slouken@0
    80
	while ( height-- ) {
slouken@0
    81
	    DUFFS_LOOP4(
slouken@0
    82
	    {
icculus@1162
    83
		Uint32 Pixel;
slouken@0
    84
		unsigned sR;
slouken@0
    85
		unsigned sG;
slouken@0
    86
		unsigned sB;
slouken@0
    87
		unsigned dR;
slouken@0
    88
		unsigned dG;
slouken@0
    89
		unsigned dB;
icculus@1162
    90
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
slouken@0
    91
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
    92
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
    93
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
    94
		ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
    95
		dR &= 0xff;
slouken@0
    96
		dG &= 0xff;
slouken@0
    97
		dB &= 0xff;
slouken@0
    98
		/* Pack RGB into 8bit pixel */
slouken@0
    99
		if ( palmap == NULL ) {
slouken@0
   100
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   101
			  ((dG>>5)<<(2))|
slouken@0
   102
			  ((dB>>6)<<(0));
slouken@0
   103
		} else {
slouken@0
   104
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   105
				  ((dG>>5)<<(2))  |
slouken@0
   106
				  ((dB>>6)<<(0))];
slouken@0
   107
		}
slouken@0
   108
		dst++;
slouken@0
   109
		src += srcbpp;
slouken@0
   110
	    },
slouken@0
   111
	    width);
slouken@0
   112
	    src += srcskip;
slouken@0
   113
	    dst += dstskip;
slouken@0
   114
	}
slouken@0
   115
}
slouken@0
   116
slouken@0
   117
/* N->1 blending with pixel alpha */
slouken@0
   118
static void BlitNto1PixelAlpha(SDL_BlitInfo *info)
slouken@0
   119
{
slouken@0
   120
	int width = info->d_width;
slouken@0
   121
	int height = info->d_height;
slouken@0
   122
	Uint8 *src = info->s_pixels;
slouken@0
   123
	int srcskip = info->s_skip;
slouken@0
   124
	Uint8 *dst = info->d_pixels;
slouken@0
   125
	int dstskip = info->d_skip;
slouken@0
   126
	Uint8 *palmap = info->table;
slouken@0
   127
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   128
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   129
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   130
slouken@0
   131
	/* FIXME: fix alpha bit field expansion here too? */
slouken@0
   132
	while ( height-- ) {
slouken@0
   133
	    DUFFS_LOOP4(
slouken@0
   134
	    {
icculus@1162
   135
		Uint32 Pixel;
slouken@0
   136
		unsigned sR;
slouken@0
   137
		unsigned sG;
slouken@0
   138
		unsigned sB;
slouken@0
   139
		unsigned sA;
slouken@0
   140
		unsigned dR;
slouken@0
   141
		unsigned dG;
slouken@0
   142
		unsigned dB;
icculus@1162
   143
		DISEMBLE_RGBA(src,srcbpp,srcfmt,Pixel,sR,sG,sB,sA);
slouken@0
   144
		dR = dstfmt->palette->colors[*dst].r;
slouken@0
   145
		dG = dstfmt->palette->colors[*dst].g;
slouken@0
   146
		dB = dstfmt->palette->colors[*dst].b;
slouken@0
   147
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
   148
		dR &= 0xff;
slouken@0
   149
		dG &= 0xff;
slouken@0
   150
		dB &= 0xff;
slouken@0
   151
		/* Pack RGB into 8bit pixel */
slouken@0
   152
		if ( palmap == NULL ) {
slouken@0
   153
		    *dst =((dR>>5)<<(3+2))|
slouken@0
   154
			  ((dG>>5)<<(2))|
slouken@0
   155
			  ((dB>>6)<<(0));
slouken@0
   156
		} else {
slouken@0
   157
		    *dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   158
				  ((dG>>5)<<(2))  |
slouken@0
   159
				  ((dB>>6)<<(0))  ];
slouken@0
   160
		}
slouken@0
   161
		dst++;
slouken@0
   162
		src += srcbpp;
slouken@0
   163
	    },
slouken@0
   164
	    width);
slouken@0
   165
	    src += srcskip;
slouken@0
   166
	    dst += dstskip;
slouken@0
   167
	}
slouken@0
   168
}
slouken@0
   169
slouken@0
   170
/* colorkeyed N->1 blending with per-surface alpha */
slouken@0
   171
static void BlitNto1SurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
   172
{
slouken@0
   173
	int width = info->d_width;
slouken@0
   174
	int height = info->d_height;
slouken@0
   175
	Uint8 *src = info->s_pixels;
slouken@0
   176
	int srcskip = info->s_skip;
slouken@0
   177
	Uint8 *dst = info->d_pixels;
slouken@0
   178
	int dstskip = info->d_skip;
slouken@0
   179
	Uint8 *palmap = info->table;
slouken@0
   180
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
   181
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
   182
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
   183
	Uint32 ckey = srcfmt->colorkey;
slouken@0
   184
slouken@0
   185
	const int A = srcfmt->alpha;
slouken@0
   186
slouken@0
   187
	while ( height-- ) {
slouken@0
   188
	    DUFFS_LOOP(
slouken@0
   189
	    {
icculus@1162
   190
		Uint32 Pixel;
slouken@0
   191
		unsigned sR;
slouken@0
   192
		unsigned sG;
slouken@0
   193
		unsigned sB;
slouken@0
   194
		unsigned dR;
slouken@0
   195
		unsigned dG;
slouken@0
   196
		unsigned dB;
icculus@1162
   197
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
   198
		if ( Pixel != ckey ) {
slouken@0
   199
		    dR = dstfmt->palette->colors[*dst].r;
slouken@0
   200
		    dG = dstfmt->palette->colors[*dst].g;
slouken@0
   201
		    dB = dstfmt->palette->colors[*dst].b;
slouken@0
   202
		    ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB);
slouken@0
   203
		    dR &= 0xff;
slouken@0
   204
		    dG &= 0xff;
slouken@0
   205
		    dB &= 0xff;
slouken@0
   206
		    /* Pack RGB into 8bit pixel */
slouken@0
   207
		    if ( palmap == NULL ) {
slouken@0
   208
			*dst =((dR>>5)<<(3+2))|
slouken@0
   209
			      ((dG>>5)<<(2)) |
slouken@0
   210
			      ((dB>>6)<<(0));
slouken@0
   211
		    } else {
slouken@0
   212
			*dst = palmap[((dR>>5)<<(3+2))|
slouken@0
   213
				      ((dG>>5)<<(2))  |
slouken@0
   214
				      ((dB>>6)<<(0))  ];
slouken@0
   215
		    }
slouken@0
   216
		}
slouken@0
   217
		dst++;
slouken@0
   218
		src += srcbpp;
slouken@0
   219
	    },
slouken@0
   220
	    width);
slouken@0
   221
	    src += srcskip;
slouken@0
   222
	    dst += dstskip;
slouken@0
   223
	}
slouken@0
   224
}
slouken@0
   225
slouken@1542
   226
#if GCC_ASMBLIT
slouken@689
   227
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@689
   228
static void BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo *info)
slouken@689
   229
{
slouken@689
   230
	int width = info->d_width;
slouken@689
   231
	int height = info->d_height;
slouken@689
   232
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@689
   233
	int srcskip = info->s_skip >> 2;
slouken@689
   234
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@689
   235
	int dstskip = info->d_skip >> 2;
slouken@1542
   236
	Uint32 dalpha = info->dst->Amask;
slouken@1542
   237
	Uint8 load[8];
slouken@1542
   238
slouken@1542
   239
	*(Uint64 *)load = 0x00fefefe00fefefeULL;/* alpha128 mask */
slouken@1542
   240
	movq_m2r(*load, mm4); /* alpha128 mask -> mm4 */
slouken@1542
   241
	*(Uint64 *)load = 0x0001010100010101ULL;/* !alpha128 mask */
slouken@1542
   242
	movq_m2r(*load, mm3); /* !alpha128 mask -> mm3 */
slouken@1542
   243
	movd_m2r(dalpha, mm7); /* dst alpha mask */
slouken@1542
   244
	punpckldq_r2r(mm7, mm7); /* dst alpha mask | dst alpha mask -> mm7 */
slouken@689
   245
	while(height--) {
slouken@1542
   246
		DUFFS_LOOP_DOUBLE2(
slouken@1542
   247
		{
slouken@1542
   248
			Uint32 s = *srcp++;
slouken@1542
   249
			Uint32 d = *dstp;
slouken@1542
   250
			*dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1542
   251
				   + (s & d & 0x00010101)) | dalpha;
slouken@1542
   252
		},{
slouken@1542
   253
			movq_m2r((*dstp), mm2);/* 2 x dst -> mm2(ARGBARGB) */
slouken@1542
   254
			movq_r2r(mm2, mm6); /* 2 x dst -> mm6(ARGBARGB) */
slouken@1542
   255
slouken@1542
   256
			movq_m2r((*srcp), mm1);/* 2 x src -> mm1(ARGBARGB) */
slouken@1542
   257
			movq_r2r(mm1, mm5); /* 2 x src -> mm5(ARGBARGB) */
slouken@1542
   258
slouken@1542
   259
			pand_r2r(mm4, mm6); /* dst & mask -> mm6 */
slouken@1542
   260
			pand_r2r(mm4, mm5); /* src & mask -> mm5 */
slouken@1542
   261
			paddd_r2r(mm6, mm5); /* mm6 + mm5 -> mm5 */
slouken@1542
   262
			pand_r2r(mm1, mm2); /* src & dst -> mm2 */
slouken@1542
   263
			psrld_i2r(1, mm5); /* mm5 >> 1 -> mm5 */
slouken@1542
   264
			pand_r2r(mm3, mm2); /* mm2 & !mask -> mm2 */
slouken@1542
   265
			paddd_r2r(mm5, mm2); /* mm5 + mm2 -> mm2 */
slouken@1542
   266
			
slouken@1542
   267
			por_r2r(mm7, mm2); /* mm7(full alpha) | mm2 -> mm2 */
slouken@1542
   268
			movq_r2m(mm2, (*dstp));/* mm2 -> 2 x dst pixels */
slouken@1542
   269
			dstp += 2;
slouken@1542
   270
			srcp += 2;
slouken@1542
   271
		}, width);
slouken@1542
   272
		srcp += srcskip;
slouken@1542
   273
		dstp += dstskip;
slouken@689
   274
	}
slouken@689
   275
	emms();
slouken@689
   276
}
slouken@689
   277
slouken@689
   278
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@689
   279
static void BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@689
   280
{
slouken@1542
   281
	SDL_PixelFormat* df = info->dst;
slouken@689
   282
	unsigned alpha = info->src->alpha;
slouken@1542
   283
slouken@1542
   284
	if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1542
   285
			/* only call a128 version when R,G,B occupy lower bits */
slouken@689
   286
		BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@689
   287
	} else {
slouken@689
   288
		int width = info->d_width;
slouken@689
   289
		int height = info->d_height;
slouken@689
   290
		Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@689
   291
		int srcskip = info->s_skip >> 2;
slouken@689
   292
		Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@689
   293
		int dstskip = info->d_skip >> 2;
slouken@1542
   294
slouken@1542
   295
		pxor_r2r(mm5, mm5); /* 0 -> mm5 */
slouken@1542
   296
		/* form the alpha mult */
slouken@1542
   297
		movd_m2r(alpha, mm4); /* 0000000A -> mm4 */
slouken@1542
   298
		punpcklwd_r2r(mm4, mm4); /* 00000A0A -> mm4 */
slouken@1542
   299
		punpckldq_r2r(mm4, mm4); /* 0A0A0A0A -> mm4 */
slouken@1542
   300
		alpha = (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->Bshift);
slouken@1542
   301
		movd_m2r(alpha, mm0); /* 00000FFF -> mm0 */
slouken@1542
   302
		punpcklbw_r2r(mm0, mm0); /* 00FFFFFF -> mm0 */
slouken@1542
   303
		pand_r2r(mm0, mm4); /* 0A0A0A0A -> mm4, minus 1 chan */
slouken@1542
   304
			/* at this point mm4 can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1542
   305
		movd_m2r(df->Amask, mm7); /* dst alpha mask */
slouken@1542
   306
		punpckldq_r2r(mm7, mm7); /* dst alpha mask | dst alpha mask -> mm7 */
slouken@689
   307
		
slouken@689
   308
		while(height--) {
slouken@689
   309
			DUFFS_LOOP_DOUBLE2({
slouken@689
   310
				/* One Pixel Blend */
slouken@1542
   311
				movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   312
				movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   313
				punpcklbw_r2r(mm5, mm1); /* 0A0R0G0B -> mm1(src) */
slouken@1542
   314
				punpcklbw_r2r(mm5, mm2); /* 0A0R0G0B -> mm2(dst) */
slouken@1542
   315
slouken@1542
   316
				psubw_r2r(mm2, mm1);/* src - dst -> mm1 */
slouken@1542
   317
				pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   318
				psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1 */
slouken@1542
   319
				paddb_r2r(mm1, mm2); /* mm1 + mm2(dst) -> mm2 */
slouken@1542
   320
slouken@1542
   321
				packuswb_r2r(mm5, mm2);  /* ARGBARGB -> mm2 */
slouken@1542
   322
				por_r2r(mm7, mm2); /* mm7(full alpha) | mm2 -> mm2 */
slouken@1542
   323
				movd_r2m(mm2, *dstp);/* mm2 -> pixel */
slouken@689
   324
				++srcp;
slouken@689
   325
				++dstp;
slouken@689
   326
			},{
slouken@1542
   327
				/* Two Pixels Blend */
slouken@689
   328
				movq_m2r((*srcp), mm0);/* 2 x src -> mm0(ARGBARGB)*/
slouken@1542
   329
				movq_m2r((*dstp), mm2);/* 2 x dst -> mm2(ARGBARGB) */
slouken@1542
   330
				movq_r2r(mm0, mm1); /* 2 x src -> mm1(ARGBARGB) */
slouken@1542
   331
				movq_r2r(mm2, mm6); /* 2 x dst -> mm6(ARGBARGB) */
slouken@1542
   332
slouken@1542
   333
				punpcklbw_r2r(mm5, mm0); /* low - 0A0R0G0B -> mm0(src1) */
slouken@1542
   334
				punpckhbw_r2r(mm5, mm1); /* high - 0A0R0G0B -> mm1(src2) */
slouken@1542
   335
				punpcklbw_r2r(mm5, mm2); /* low - 0A0R0G0B -> mm2(dst1) */
slouken@1542
   336
				punpckhbw_r2r(mm5, mm6); /* high - 0A0R0G0B -> mm6(dst2) */
slouken@1542
   337
slouken@1542
   338
				psubw_r2r(mm2, mm0);/* src1 - dst1 -> mm0 */
slouken@1542
   339
				pmullw_r2r(mm4, mm0); /* mm0 * alpha -> mm0 */
slouken@1542
   340
				psrlw_i2r(8, mm0); /* mm0 >> 8 -> mm1 */
slouken@1542
   341
				paddb_r2r(mm0, mm2); /* mm0 + mm2(dst1) -> mm2 */
slouken@1542
   342
slouken@1542
   343
				psubw_r2r(mm6, mm1);/* src2 - dst2 -> mm1 */
slouken@1542
   344
				pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   345
				psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1 */
slouken@1542
   346
				paddb_r2r(mm1, mm6); /* mm1 + mm6(dst2) -> mm6 */
slouken@1542
   347
slouken@1542
   348
				packuswb_r2r(mm6, mm2);  /* ARGBARGB -> mm2 */
slouken@1542
   349
				por_r2r(mm7, mm2); /* mm7(dst alpha) | mm2 -> mm2 */
slouken@1542
   350
				
slouken@1542
   351
				movq_r2m(mm2, *dstp);/* mm2 -> 2 x pixel */
slouken@1542
   352
slouken@1542
   353
  				srcp += 2;
slouken@1542
   354
  				dstp += 2;
slouken@1542
   355
  			}, width);
slouken@689
   356
			srcp += srcskip;
slouken@689
   357
			dstp += dstskip;
slouken@689
   358
		}
slouken@689
   359
		emms();
slouken@689
   360
	}
slouken@689
   361
}
slouken@689
   362
slouken@689
   363
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@689
   364
static void BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo *info)
slouken@689
   365
{
slouken@689
   366
	int width = info->d_width;
slouken@689
   367
	int height = info->d_height;
slouken@689
   368
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@689
   369
	int srcskip = info->s_skip >> 2;
slouken@689
   370
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@689
   371
	int dstskip = info->d_skip >> 2;
slouken@1542
   372
	SDL_PixelFormat* sf = info->src;
slouken@1542
   373
	Uint32 amask = sf->Amask;
slouken@1542
   374
slouken@1542
   375
	pxor_r2r(mm6, mm6); /* 0 -> mm6 */
slouken@1542
   376
	/* form multiplication mask */
slouken@1542
   377
	movd_m2r(sf->Amask, mm7); /* 0000F000 -> mm7 */
slouken@1542
   378
	punpcklbw_r2r(mm7, mm7); /* FF000000 -> mm7 */
slouken@1542
   379
	pcmpeqb_r2r(mm0, mm0); /* FFFFFFFF -> mm0 */
slouken@1542
   380
	movq_r2r(mm0, mm3); /* FFFFFFFF -> mm3 (for later) */
slouken@1542
   381
	pxor_r2r(mm0, mm7); /* 00FFFFFF -> mm7 (mult mask) */
slouken@1542
   382
	/* form channel masks */
slouken@1542
   383
	movq_r2r(mm7, mm0); /* 00FFFFFF -> mm0 */
slouken@1542
   384
	packsswb_r2r(mm6, mm0); /* 00000FFF -> mm0 (channel mask) */
slouken@1542
   385
	packsswb_r2r(mm6, mm3); /* 0000FFFF -> mm3 */
slouken@1542
   386
	pxor_r2r(mm0, mm3); /* 0000F000 -> mm3 (~channel mask) */
slouken@1542
   387
	/* get alpha channel shift */
icculus@3931
   388
	__asm__ __volatile__ (
icculus@3931
   389
		"movd %0, %%mm5"
icculus@3931
   390
		: : "rm" ((Uint32) sf->Ashift) ); /* Ashift -> mm5 */
slouken@1542
   391
slouken@689
   392
	while(height--) {
slouken@689
   393
	    DUFFS_LOOP4({
slouken@1542
   394
		Uint32 alpha = *srcp & amask;
slouken@689
   395
		/* FIXME: Here we special-case opaque alpha since the
slouken@1542
   396
			compositioning used (>>8 instead of /255) doesn't handle
slouken@1542
   397
			it correctly. Also special-case alpha=0 for speed?
slouken@1542
   398
			Benchmark this! */
slouken@1542
   399
		if(alpha == 0) {
slouken@1542
   400
			/* do nothing */
slouken@1542
   401
		} else if(alpha == amask) {
slouken@1542
   402
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   403
			/* using MMX here to free up regular registers for other things */
slouken@1542
   404
			movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   405
			movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   406
			pand_r2r(mm0, mm1); /* src & chanmask -> mm1 */
slouken@1542
   407
			pand_r2r(mm3, mm2); /* dst & ~chanmask -> mm2 */
slouken@1542
   408
			por_r2r(mm1, mm2); /* src | dst -> mm2 */
slouken@1542
   409
			movd_r2m(mm2, (*dstp)); /* mm2 -> dst */
slouken@1542
   410
		} else {
slouken@1542
   411
			movd_m2r((*srcp), mm1);/* src(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
   412
			punpcklbw_r2r(mm6, mm1); /* 0A0R0G0B -> mm1 */
slouken@1542
   413
slouken@1542
   414
			movd_m2r((*dstp), mm2);/* dst(ARGB) -> mm2 (0000ARGB)*/
slouken@1542
   415
			punpcklbw_r2r(mm6, mm2); /* 0A0R0G0B -> mm2 */
slouken@1542
   416
slouken@1542
   417
			__asm__ __volatile__ (
slouken@1542
   418
				"movd %0, %%mm4"
slouken@1542
   419
				: : "r" (alpha) ); /* 0000A000 -> mm4 */
slouken@1542
   420
			psrld_r2r(mm5, mm4); /* mm4 >> mm5 -> mm4 (0000000A) */
slouken@1542
   421
			punpcklwd_r2r(mm4, mm4); /* 00000A0A -> mm4 */
slouken@1542
   422
			punpcklwd_r2r(mm4, mm4); /* 0A0A0A0A -> mm4 */
slouken@1542
   423
			pand_r2r(mm7, mm4); /* 000A0A0A -> mm4, preserve dst alpha on add */
slouken@1542
   424
slouken@1542
   425
			/* blend */		    
slouken@1542
   426
			psubw_r2r(mm2, mm1);/* src - dst -> mm1 */
slouken@1542
   427
			pmullw_r2r(mm4, mm1); /* mm1 * alpha -> mm1 */
slouken@1542
   428
			psrlw_i2r(8, mm1); /* mm1 >> 8 -> mm1(000R0G0B) */
slouken@1542
   429
			paddb_r2r(mm1, mm2); /* mm1 + mm2(dst) -> mm2 */
slouken@1542
   430
			
slouken@1542
   431
			packuswb_r2r(mm6, mm2);  /* 0000ARGB -> mm2 */
slouken@1542
   432
			movd_r2m(mm2, *dstp);/* mm2 -> dst */
slouken@689
   433
		}
slouken@689
   434
		++srcp;
slouken@689
   435
		++dstp;
slouken@689
   436
	    }, width);
slouken@689
   437
	    srcp += srcskip;
slouken@689
   438
	    dstp += dstskip;
slouken@689
   439
	}
slouken@689
   440
	emms();
slouken@689
   441
}
slouken@1542
   442
/* End GCC_ASMBLIT */
slouken@1542
   443
slouken@1542
   444
#elif MSVC_ASMBLIT
slouken@1542
   445
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1542
   446
static void BlitRGBtoRGBSurfaceAlpha128MMX(SDL_BlitInfo *info)
slouken@1542
   447
{
slouken@1542
   448
	int width = info->d_width;
slouken@1542
   449
	int height = info->d_height;
slouken@1542
   450
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1542
   451
	int srcskip = info->s_skip >> 2;
slouken@1542
   452
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1542
   453
	int dstskip = info->d_skip >> 2;
slouken@1542
   454
	Uint32 dalpha = info->dst->Amask;
slouken@1542
   455
slouken@1542
   456
	__m64 src1, src2, dst1, dst2, lmask, hmask, dsta;
slouken@1542
   457
	
slouken@1542
   458
	hmask = _mm_set_pi32(0x00fefefe, 0x00fefefe); /* alpha128 mask -> hmask */
slouken@1542
   459
	lmask = _mm_set_pi32(0x00010101, 0x00010101); /* !alpha128 mask -> lmask */
slouken@1542
   460
	dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
slouken@1542
   461
slouken@1542
   462
	while (height--) {
slouken@1542
   463
		int n = width;
slouken@1542
   464
		if ( n & 1 ) {
slouken@1542
   465
			Uint32 s = *srcp++;
slouken@1542
   466
			Uint32 d = *dstp;
slouken@1542
   467
			*dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1542
   468
				   + (s & d & 0x00010101)) | dalpha;
slouken@1542
   469
			n--;
slouken@1542
   470
		}
slouken@1542
   471
		
slouken@1542
   472
		for (n >>= 1; n > 0; --n) {
slouken@1542
   473
			dst1 = *(__m64*)dstp; /* 2 x dst -> dst1(ARGBARGB) */
slouken@1542
   474
			dst2 = dst1;   /* 2 x dst -> dst2(ARGBARGB) */
slouken@1542
   475
slouken@1542
   476
			src1 = *(__m64*)srcp; /* 2 x src -> src1(ARGBARGB) */
slouken@1542
   477
			src2 = src1; /* 2 x src -> src2(ARGBARGB) */
slouken@1542
   478
slouken@1542
   479
			dst2 = _mm_and_si64(dst2, hmask); /* dst & mask -> dst2 */
slouken@1542
   480
			src2 = _mm_and_si64(src2, hmask); /* src & mask -> src2 */
slouken@1542
   481
			src2 = _mm_add_pi32(src2, dst2); /* dst2 + src2 -> src2 */
slouken@1542
   482
			src2 = _mm_srli_pi32(src2, 1); /* src2 >> 1 -> src2 */
slouken@1542
   483
slouken@1542
   484
			dst1 = _mm_and_si64(dst1, src1); /* src & dst -> dst1 */
slouken@1542
   485
			dst1 = _mm_and_si64(dst1, lmask); /* dst1 & !mask -> dst1 */
slouken@1542
   486
			dst1 = _mm_add_pi32(dst1, src2); /* src2 + dst1 -> dst1 */
slouken@1542
   487
			dst1 = _mm_or_si64(dst1, dsta); /* dsta(full alpha) | dst1 -> dst1 */
slouken@1542
   488
			
slouken@1542
   489
			*(__m64*)dstp = dst1; /* dst1 -> 2 x dst pixels */
slouken@1542
   490
			dstp += 2;
slouken@1542
   491
			srcp += 2;
slouken@1542
   492
		}
slouken@1542
   493
		
slouken@1542
   494
		srcp += srcskip;
slouken@1542
   495
		dstp += dstskip;
slouken@1542
   496
	}
slouken@1542
   497
	_mm_empty();
slouken@1542
   498
}
slouken@1542
   499
slouken@1542
   500
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1542
   501
static void BlitRGBtoRGBSurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@1542
   502
{
slouken@1542
   503
	SDL_PixelFormat* df = info->dst;
slouken@1542
   504
	Uint32 chanmask = df->Rmask | df->Gmask | df->Bmask;
slouken@1542
   505
	unsigned alpha = info->src->alpha;
slouken@1542
   506
slouken@1542
   507
	if (alpha == 128 && (df->Rmask | df->Gmask | df->Bmask) == 0x00FFFFFF) {
slouken@1542
   508
			/* only call a128 version when R,G,B occupy lower bits */
slouken@1542
   509
		BlitRGBtoRGBSurfaceAlpha128MMX(info);
slouken@1542
   510
	} else {
slouken@1542
   511
		int width = info->d_width;
slouken@1542
   512
		int height = info->d_height;
slouken@1542
   513
		Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1542
   514
		int srcskip = info->s_skip >> 2;
slouken@1542
   515
		Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1542
   516
		int dstskip = info->d_skip >> 2;
slouken@1542
   517
		Uint32 dalpha = df->Amask;
slouken@1542
   518
		Uint32 amult;
slouken@1542
   519
slouken@1542
   520
		__m64 src1, src2, dst1, dst2, mm_alpha, mm_zero, dsta;
slouken@1542
   521
		
slouken@1542
   522
		mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
slouken@1542
   523
		/* form the alpha mult */
slouken@1542
   524
		amult = alpha | (alpha << 8);
slouken@1542
   525
		amult = amult | (amult << 16);
slouken@1542
   526
		chanmask = (0xff << df->Rshift) | (0xff << df->Gshift) | (0xff << df->Bshift);
slouken@1542
   527
		mm_alpha = _mm_set_pi32(0, amult & chanmask); /* 0000AAAA -> mm_alpha, minus 1 chan */
slouken@1542
   528
		mm_alpha = _mm_unpacklo_pi8(mm_alpha, mm_zero); /* 0A0A0A0A -> mm_alpha, minus 1 chan */
slouken@1542
   529
			/* at this point mm_alpha can be 000A0A0A or 0A0A0A00 or another combo */
slouken@1542
   530
		dsta = _mm_set_pi32(dalpha, dalpha); /* dst alpha mask -> dsta */
slouken@1542
   531
		
slouken@1542
   532
		while (height--) {
slouken@1542
   533
			int n = width;
slouken@1542
   534
			if (n & 1) {
slouken@1542
   535
				/* One Pixel Blend */
slouken@1542
   536
				src2 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src2 (0000ARGB)*/
slouken@1542
   537
				src2 = _mm_unpacklo_pi8(src2, mm_zero); /* 0A0R0G0B -> src2 */
slouken@1542
   538
slouken@1542
   539
				dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
   540
				dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   541
slouken@1542
   542
				src2 = _mm_sub_pi16(src2, dst1); /* src2 - dst2 -> src2 */
slouken@1542
   543
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
   544
				src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
slouken@1542
   545
				dst1 = _mm_add_pi8(src2, dst1); /* src2 + dst1 -> dst1 */
slouken@1542
   546
				
slouken@1542
   547
				dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
   548
				dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1542
   549
				*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   550
slouken@1542
   551
				++srcp;
slouken@1542
   552
				++dstp;
slouken@1542
   553
				
slouken@1542
   554
				n--;
slouken@1542
   555
			}
slouken@1542
   556
slouken@1542
   557
			for (n >>= 1; n > 0; --n) {
slouken@1542
   558
				/* Two Pixels Blend */
slouken@1542
   559
				src1 = *(__m64*)srcp; /* 2 x src -> src1(ARGBARGB)*/
slouken@1542
   560
				src2 = src1; /* 2 x src -> src2(ARGBARGB) */
slouken@1542
   561
				src1 = _mm_unpacklo_pi8(src1, mm_zero); /* low - 0A0R0G0B -> src1 */
slouken@1542
   562
				src2 = _mm_unpackhi_pi8(src2, mm_zero); /* high - 0A0R0G0B -> src2 */
slouken@1542
   563
slouken@1542
   564
				dst1 = *(__m64*)dstp;/* 2 x dst -> dst1(ARGBARGB) */
slouken@1542
   565
				dst2 = dst1; /* 2 x dst -> dst2(ARGBARGB) */
slouken@1542
   566
				dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* low - 0A0R0G0B -> dst1 */
slouken@1542
   567
				dst2 = _mm_unpackhi_pi8(dst2, mm_zero); /* high - 0A0R0G0B -> dst2 */
slouken@1542
   568
slouken@1542
   569
				src1 = _mm_sub_pi16(src1, dst1);/* src1 - dst1 -> src1 */
slouken@1542
   570
				src1 = _mm_mullo_pi16(src1, mm_alpha); /* src1 * alpha -> src1 */
slouken@1542
   571
				src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1 */
slouken@1542
   572
				dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst1) -> dst1 */
slouken@1542
   573
slouken@1542
   574
				src2 = _mm_sub_pi16(src2, dst2);/* src2 - dst2 -> src2 */
slouken@1542
   575
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
   576
				src2 = _mm_srli_pi16(src2, 8); /* src2 >> 8 -> src2 */
slouken@1542
   577
				dst2 = _mm_add_pi8(src2, dst2); /* src2 + dst2(dst2) -> dst2 */
slouken@1542
   578
				
slouken@1542
   579
				dst1 = _mm_packs_pu16(dst1, dst2); /* 0A0R0G0B(res1), 0A0R0G0B(res2) -> dst1(ARGBARGB) */
slouken@1542
   580
				dst1 = _mm_or_si64(dst1, dsta); /* dsta | dst1 -> dst1 */
slouken@1542
   581
slouken@1542
   582
				*(__m64*)dstp = dst1; /* dst1 -> 2 x pixel */
slouken@1542
   583
slouken@1542
   584
				srcp += 2;
slouken@1542
   585
				dstp += 2;
slouken@1542
   586
			}
slouken@1542
   587
			srcp += srcskip;
slouken@1542
   588
			dstp += dstskip;
slouken@1542
   589
		}
slouken@1542
   590
		_mm_empty();
slouken@1542
   591
	}
slouken@1542
   592
}
slouken@1542
   593
slouken@1542
   594
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1542
   595
static void BlitRGBtoRGBPixelAlphaMMX(SDL_BlitInfo *info)
slouken@1542
   596
{
slouken@1542
   597
	int width = info->d_width;
slouken@1542
   598
	int height = info->d_height;
slouken@1542
   599
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1542
   600
	int srcskip = info->s_skip >> 2;
slouken@1542
   601
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1542
   602
	int dstskip = info->d_skip >> 2;
slouken@1542
   603
	SDL_PixelFormat* sf = info->src;
slouken@1542
   604
	Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1542
   605
	Uint32 amask = sf->Amask;
slouken@1542
   606
	Uint32 ashift = sf->Ashift;
slouken@1542
   607
	Uint64 multmask;
slouken@1542
   608
slouken@1542
   609
	__m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
   610
slouken@1542
   611
	mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
slouken@1542
   612
	multmask = ~(0xFFFFi64 << (ashift * 2));
slouken@1542
   613
	dmask = *(__m64*) &multmask; /* dst alpha mask -> dmask */
slouken@1542
   614
slouken@1542
   615
	while(height--) {
slouken@1542
   616
		DUFFS_LOOP4({
slouken@1542
   617
		Uint32 alpha = *srcp & amask;
slouken@1542
   618
		if (alpha == 0) {
slouken@1542
   619
			/* do nothing */
slouken@1542
   620
		} else if (alpha == amask) {
slouken@1542
   621
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
   622
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
   623
		} else {
slouken@1542
   624
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
   625
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
   626
slouken@1542
   627
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
   628
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
   629
slouken@1542
   630
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
   631
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
   632
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
   633
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
   634
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
   635
slouken@1542
   636
			/* blend */		    
slouken@1542
   637
			src1 = _mm_sub_pi16(src1, dst1);/* src1 - dst1 -> src1 */
slouken@1542
   638
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src1 - dst1) * alpha -> src1 */
slouken@1542
   639
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
   640
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1 -> dst1(0A0R0G0B) */
slouken@1542
   641
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
   642
			
slouken@1542
   643
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
   644
		}
slouken@1542
   645
		++srcp;
slouken@1542
   646
		++dstp;
slouken@1542
   647
	    }, width);
slouken@1542
   648
	    srcp += srcskip;
slouken@1542
   649
	    dstp += dstskip;
slouken@1542
   650
	}
slouken@1542
   651
	_mm_empty();
slouken@1542
   652
}
slouken@1542
   653
/* End MSVC_ASMBLIT */
slouken@1542
   654
slouken@1542
   655
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
   656
slouken@1361
   657
#if SDL_ALTIVEC_BLITTERS
slouken@1795
   658
#if __MWERKS__
slouken@1795
   659
#pragma altivec_model on
slouken@1795
   660
#endif
slouken@1361
   661
#if HAVE_ALTIVEC_H
icculus@1162
   662
#include <altivec.h>
icculus@1175
   663
#endif
icculus@1047
   664
#include <assert.h>
icculus@1162
   665
slouken@1402
   666
#if (defined(__MACOSX__) && (__GNUC__ < 4))
icculus@1162
   667
    #define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   668
        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
icculus@1162
   669
    #define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   670
        (vector unsigned short) ( a,b,c,d,e,f,g,h )
icculus@1162
   671
#else
icculus@1162
   672
    #define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
icculus@1162
   673
        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
icculus@1162
   674
    #define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
icculus@1162
   675
        (vector unsigned short) { a,b,c,d,e,f,g,h }
icculus@1162
   676
#endif
icculus@1162
   677
icculus@1047
   678
#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
icculus@1047
   679
#define VECPRINT(msg, v) do { \
icculus@1047
   680
    vector unsigned int tmpvec = (vector unsigned int)(v); \
icculus@1047
   681
    unsigned int *vp = (unsigned int *)&tmpvec; \
icculus@1047
   682
    printf("%s = %08X %08X %08X %08X\n", msg, vp[0], vp[1], vp[2], vp[3]); \
icculus@1047
   683
} while (0)
icculus@1047
   684
icculus@1047
   685
/* the permuation vector that takes the high bytes out of all the appropriate shorts 
icculus@1047
   686
    (vector unsigned char)(
icculus@1047
   687
        0x00, 0x10, 0x02, 0x12,
icculus@1047
   688
        0x04, 0x14, 0x06, 0x16,
icculus@1047
   689
        0x08, 0x18, 0x0A, 0x1A,
icculus@1047
   690
        0x0C, 0x1C, 0x0E, 0x1E );
icculus@1047
   691
*/
icculus@1047
   692
#define VEC_MERGE_PERMUTE() (vec_add(vec_lvsl(0, (int*)NULL), (vector unsigned char)vec_splat_u16(0x0F)))
icculus@1047
   693
#define VEC_U32_24() (vec_add(vec_splat_u32(12), vec_splat_u32(12)))
icculus@1047
   694
#define VEC_ALPHA_MASK() ((vector unsigned char)vec_sl((vector unsigned int)vec_splat_s8(-1), VEC_U32_24()))
icculus@1047
   695
#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
icculus@1047
   696
    ? vec_lvsl(0, src) \
icculus@1047
   697
    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
icculus@1047
   698
icculus@1047
   699
   
icculus@1047
   700
#define VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1_16, v8_16) do { \
icculus@1047
   701
    /* vtemp1 contains source AAGGAAGGAAGGAAGG */ \
icculus@1047
   702
    vector unsigned short vtemp1 = vec_mule(vs, valpha); \
icculus@1047
   703
    /* vtemp2 contains source RRBBRRBBRRBBRRBB */ \
icculus@1047
   704
    vector unsigned short vtemp2 = vec_mulo(vs, valpha); \
icculus@1047
   705
    /* valpha2 is 255-alpha */ \
icculus@1047
   706
    vector unsigned char valpha2 = vec_nor(valpha, valpha); \
icculus@1047
   707
    /* vtemp3 contains dest AAGGAAGGAAGGAAGG */ \
icculus@1047
   708
    vector unsigned short vtemp3 = vec_mule(vd, valpha2); \
icculus@1047
   709
    /* vtemp4 contains dest RRBBRRBBRRBBRRBB */ \
icculus@1047
   710
    vector unsigned short vtemp4 = vec_mulo(vd, valpha2); \
icculus@1047
   711
    /* add source and dest */ \
icculus@1047
   712
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   713
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   714
    /* vtemp1 = (vtemp1 + 1) + ((vtemp1 + 1) >> 8) */ \
icculus@1047
   715
    vtemp1 = vec_add(vtemp1, v1_16); \
icculus@1047
   716
    vtemp3 = vec_sr(vtemp1, v8_16); \
icculus@1047
   717
    vtemp1 = vec_add(vtemp1, vtemp3); \
icculus@1047
   718
    /* vtemp2 = (vtemp2 + 1) + ((vtemp2 + 1) >> 8) */ \
icculus@1047
   719
    vtemp2 = vec_add(vtemp2, v1_16); \
icculus@1047
   720
    vtemp4 = vec_sr(vtemp2, v8_16); \
icculus@1047
   721
    vtemp2 = vec_add(vtemp2, vtemp4); \
icculus@1047
   722
    /* (>>8) and get ARGBARGBARGBARGB */ \
icculus@1047
   723
    vd = (vector unsigned char)vec_perm(vtemp1, vtemp2, mergePermute); \
icculus@1047
   724
} while (0)
icculus@1047
   725
 
icculus@1047
   726
/* Calculate the permute vector used for 32->32 swizzling */
icculus@1047
   727
static vector unsigned char calc_swizzle32(const SDL_PixelFormat *srcfmt,
icculus@1047
   728
                                  const SDL_PixelFormat *dstfmt)
icculus@1047
   729
{
icculus@1047
   730
    /*
icculus@1047
   731
     * We have to assume that the bits that aren't used by other
icculus@1047
   732
     *  colors is alpha, and it's one complete byte, since some formats
icculus@1047
   733
     *  leave alpha with a zero mask, but we should still swizzle the bits.
icculus@1047
   734
     */
icculus@1047
   735
    /* ARGB */
icculus@1047
   736
    const static struct SDL_PixelFormat default_pixel_format = {
icculus@1047
   737
        NULL, 0, 0,
icculus@1047
   738
        0, 0, 0, 0,
icculus@1047
   739
        16, 8, 0, 24,
icculus@1047
   740
        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
icculus@1047
   741
        0, 0};
icculus@1047
   742
    if (!srcfmt) {
icculus@1047
   743
        srcfmt = &default_pixel_format;
icculus@1047
   744
    }
icculus@1047
   745
    if (!dstfmt) {
icculus@1047
   746
        dstfmt = &default_pixel_format;
icculus@1047
   747
    }
slouken@1487
   748
    const vector unsigned char plus = VECUINT8_LITERAL
icculus@1047
   749
                                            ( 0x00, 0x00, 0x00, 0x00,
icculus@1047
   750
                                              0x04, 0x04, 0x04, 0x04,
icculus@1047
   751
                                              0x08, 0x08, 0x08, 0x08,
icculus@1047
   752
                                              0x0C, 0x0C, 0x0C, 0x0C );
icculus@1047
   753
    vector unsigned char vswiz;
icculus@1047
   754
    vector unsigned int srcvec;
icculus@1047
   755
#define RESHIFT(X) (3 - ((X) >> 3))
icculus@1047
   756
    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
icculus@1047
   757
    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
icculus@1047
   758
    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
icculus@1047
   759
    Uint32 amask;
icculus@1047
   760
    /* Use zero for alpha if either surface doesn't have alpha */
icculus@1047
   761
    if (dstfmt->Amask) {
icculus@1047
   762
        amask = ((srcfmt->Amask) ? RESHIFT(srcfmt->Ashift) : 0x10) << (dstfmt->Ashift);
icculus@1047
   763
    } else {
icculus@1047
   764
        amask = 0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^ 0xFFFFFFFF);
icculus@1047
   765
    }
icculus@1047
   766
#undef RESHIFT  
icculus@1162
   767
    ((unsigned int *)(char*)&srcvec)[0] = (rmask | gmask | bmask | amask);
icculus@1047
   768
    vswiz = vec_add(plus, (vector unsigned char)vec_splat(srcvec, 0));
icculus@1047
   769
    return(vswiz);
icculus@1047
   770
}
icculus@1047
   771
icculus@1047
   772
static void Blit32to565PixelAlphaAltivec(SDL_BlitInfo *info)
icculus@1047
   773
{
icculus@1047
   774
    int height = info->d_height;
icculus@1047
   775
    Uint8 *src = (Uint8 *)info->s_pixels;
icculus@1047
   776
    int srcskip = info->s_skip;
icculus@1047
   777
    Uint8 *dst = (Uint8 *)info->d_pixels;
icculus@1047
   778
    int dstskip = info->d_skip;
icculus@1047
   779
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   780
icculus@1047
   781
    vector unsigned char v0 = vec_splat_u8(0);
icculus@1047
   782
    vector unsigned short v8_16 = vec_splat_u16(8);
icculus@1047
   783
    vector unsigned short v1_16 = vec_splat_u16(1);
icculus@1047
   784
    vector unsigned short v2_16 = vec_splat_u16(2);
icculus@1047
   785
    vector unsigned short v3_16 = vec_splat_u16(3);
icculus@1047
   786
    vector unsigned int v8_32 = vec_splat_u32(8);
icculus@1047
   787
    vector unsigned int v16_32 = vec_add(v8_32, v8_32);
icculus@1162
   788
    vector unsigned short v3f = VECUINT16_LITERAL(
icculus@1047
   789
        0x003f, 0x003f, 0x003f, 0x003f,
icculus@1047
   790
        0x003f, 0x003f, 0x003f, 0x003f);
icculus@1162
   791
    vector unsigned short vfc = VECUINT16_LITERAL(
icculus@1047
   792
        0x00fc, 0x00fc, 0x00fc, 0x00fc,
icculus@1047
   793
        0x00fc, 0x00fc, 0x00fc, 0x00fc);
icculus@1047
   794
icculus@1047
   795
    /* 
icculus@1047
   796
        0x10 - 0x1f is the alpha
icculus@1047
   797
        0x00 - 0x0e evens are the red
icculus@1047
   798
        0x01 - 0x0f odds are zero
icculus@1047
   799
    */
icculus@1162
   800
    vector unsigned char vredalpha1 = VECUINT8_LITERAL(
icculus@1047
   801
        0x10, 0x00, 0x01, 0x01,
icculus@1047
   802
        0x10, 0x02, 0x01, 0x01,
icculus@1047
   803
        0x10, 0x04, 0x01, 0x01,
icculus@1047
   804
        0x10, 0x06, 0x01, 0x01
icculus@1047
   805
    );
icculus@1047
   806
    vector unsigned char vredalpha2 = (vector unsigned char)(
icculus@1047
   807
        vec_add((vector unsigned int)vredalpha1, vec_sl(v8_32, v16_32))
icculus@1047
   808
    );
icculus@1047
   809
    /*
icculus@1047
   810
        0x00 - 0x0f is ARxx ARxx ARxx ARxx
icculus@1047
   811
        0x11 - 0x0f odds are blue
icculus@1047
   812
    */
icculus@1162
   813
    vector unsigned char vblue1 = VECUINT8_LITERAL(
icculus@1047
   814
        0x00, 0x01, 0x02, 0x11,
icculus@1047
   815
        0x04, 0x05, 0x06, 0x13,
icculus@1047
   816
        0x08, 0x09, 0x0a, 0x15,
icculus@1047
   817
        0x0c, 0x0d, 0x0e, 0x17
icculus@1047
   818
    );
icculus@1047
   819
    vector unsigned char vblue2 = (vector unsigned char)(
icculus@1047
   820
        vec_add((vector unsigned int)vblue1, v8_32)
icculus@1047
   821
    );
icculus@1047
   822
    /*
icculus@1047
   823
        0x00 - 0x0f is ARxB ARxB ARxB ARxB
icculus@1047
   824
        0x10 - 0x0e evens are green
icculus@1047
   825
    */
icculus@1162
   826
    vector unsigned char vgreen1 = VECUINT8_LITERAL(
icculus@1047
   827
        0x00, 0x01, 0x10, 0x03,
icculus@1047
   828
        0x04, 0x05, 0x12, 0x07,
icculus@1047
   829
        0x08, 0x09, 0x14, 0x0b,
icculus@1047
   830
        0x0c, 0x0d, 0x16, 0x0f
icculus@1047
   831
    );
icculus@1047
   832
    vector unsigned char vgreen2 = (vector unsigned char)(
icculus@1047
   833
        vec_add((vector unsigned int)vgreen1, vec_sl(v8_32, v8_32))
icculus@1047
   834
    );
icculus@1162
   835
    vector unsigned char vgmerge = VECUINT8_LITERAL(
icculus@1047
   836
        0x00, 0x02, 0x00, 0x06,
icculus@1047
   837
        0x00, 0x0a, 0x00, 0x0e,
icculus@1047
   838
        0x00, 0x12, 0x00, 0x16,
icculus@1047
   839
        0x00, 0x1a, 0x00, 0x1e);
icculus@1047
   840
    vector unsigned char mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   841
    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   842
    vector unsigned char valphaPermute = vec_and(vec_lvsl(0, (int *)NULL), vec_splat_u8(0xC));
icculus@1047
   843
icculus@1047
   844
    vector unsigned short vf800 = (vector unsigned short)vec_splat_u8(-7);
icculus@1047
   845
    vf800 = vec_sl(vf800, vec_splat_u16(8));
icculus@1047
   846
icculus@1047
   847
    while(height--) {
icculus@1047
   848
        int extrawidth;
icculus@1047
   849
        vector unsigned char valigner;
icculus@1047
   850
        vector unsigned char vsrc;
icculus@1047
   851
        vector unsigned char voverflow;
icculus@1047
   852
        int width = info->d_width;
icculus@1047
   853
icculus@1047
   854
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   855
        while (condition) { \
icculus@1162
   856
            Uint32 Pixel; \
icculus@1047
   857
            unsigned sR, sG, sB, dR, dG, dB, sA; \
icculus@1162
   858
            DISEMBLE_RGBA(src, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
   859
            if(sA) { \
icculus@1047
   860
                unsigned short dstpixel = *((unsigned short *)dst); \
icculus@1047
   861
                dR = (dstpixel >> 8) & 0xf8; \
icculus@1047
   862
                dG = (dstpixel >> 3) & 0xfc; \
icculus@1047
   863
                dB = (dstpixel << 3) & 0xf8; \
icculus@1047
   864
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   865
                *((unsigned short *)dst) = ( \
icculus@1047
   866
                    ((dR & 0xf8) << 8) | ((dG & 0xfc) << 3) | (dB >> 3) \
icculus@1047
   867
                ); \
icculus@1047
   868
            } \
icculus@1047
   869
            src += 4; \
icculus@1047
   870
            dst += 2; \
icculus@1047
   871
            widthvar--; \
icculus@1047
   872
        }
icculus@1047
   873
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dst)) && (width), width);
icculus@1047
   874
        extrawidth = (width % 8);
icculus@1047
   875
        valigner = VEC_ALIGNER(src);
icculus@1047
   876
        vsrc = (vector unsigned char)vec_ld(0, src);
icculus@1047
   877
        width -= extrawidth;
icculus@1047
   878
        while (width) {
icculus@1047
   879
            vector unsigned char valpha;
icculus@1047
   880
            vector unsigned char vsrc1, vsrc2;
icculus@1047
   881
            vector unsigned char vdst1, vdst2;
icculus@1047
   882
            vector unsigned short vR, vG, vB;
icculus@1047
   883
            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
icculus@1047
   884
icculus@1047
   885
            /* Load 8 pixels from src as ARGB */
icculus@1047
   886
            voverflow = (vector unsigned char)vec_ld(15, src);
icculus@1047
   887
            vsrc = vec_perm(vsrc, voverflow, valigner);
icculus@1047
   888
            vsrc1 = vec_perm(vsrc, vsrc, vpermute);
icculus@1047
   889
            src += 16;
icculus@1047
   890
            vsrc = (vector unsigned char)vec_ld(15, src);
icculus@1047
   891
            voverflow = vec_perm(voverflow, vsrc, valigner);
icculus@1047
   892
            vsrc2 = vec_perm(voverflow, voverflow, vpermute);
icculus@1047
   893
            src += 16;
icculus@1047
   894
icculus@1047
   895
            /* Load 8 pixels from dst as XRGB */
icculus@1047
   896
            voverflow = vec_ld(0, dst);
icculus@1047
   897
            vR = vec_and((vector unsigned short)voverflow, vf800);
icculus@1047
   898
            vB = vec_sl((vector unsigned short)voverflow, v3_16);
icculus@1047
   899
            vG = vec_sl(vB, v2_16);
icculus@1047
   900
            vdst1 = (vector unsigned char)vec_perm((vector unsigned char)vR, (vector unsigned char)vR, vredalpha1);
icculus@1047
   901
            vdst1 = vec_perm(vdst1, (vector unsigned char)vB, vblue1);
icculus@1047
   902
            vdst1 = vec_perm(vdst1, (vector unsigned char)vG, vgreen1);
icculus@1047
   903
            vdst2 = (vector unsigned char)vec_perm((vector unsigned char)vR, (vector unsigned char)vR, vredalpha2);
icculus@1047
   904
            vdst2 = vec_perm(vdst2, (vector unsigned char)vB, vblue2);
icculus@1047
   905
            vdst2 = vec_perm(vdst2, (vector unsigned char)vG, vgreen2);
icculus@1047
   906
icculus@1047
   907
            /* Alpha blend 8 pixels as ARGB */
icculus@1047
   908
            valpha = vec_perm(vsrc1, v0, valphaPermute);
icculus@1047
   909
            VEC_MULTIPLY_ALPHA(vsrc1, vdst1, valpha, mergePermute, v1_16, v8_16);
icculus@1047
   910
            valpha = vec_perm(vsrc2, v0, valphaPermute);
icculus@1047
   911
            VEC_MULTIPLY_ALPHA(vsrc2, vdst2, valpha, mergePermute, v1_16, v8_16);
icculus@1047
   912
icculus@1047
   913
            /* Convert 8 pixels to 565 */
icculus@1047
   914
            vpixel = (vector unsigned short)vec_packpx((vector unsigned int)vdst1, (vector unsigned int)vdst2);
icculus@1047
   915
            vgpixel = (vector unsigned short)vec_perm(vdst1, vdst2, vgmerge);
icculus@1047
   916
            vgpixel = vec_and(vgpixel, vfc);
icculus@1047
   917
            vgpixel = vec_sl(vgpixel, v3_16);
icculus@1047
   918
            vrpixel = vec_sl(vpixel, v1_16);
icculus@1047
   919
            vrpixel = vec_and(vrpixel, vf800);
icculus@1047
   920
            vbpixel = vec_and(vpixel, v3f);
icculus@1047
   921
            vdst1 = vec_or((vector unsigned char)vrpixel, (vector unsigned char)vgpixel);
icculus@1047
   922
            vdst1 = vec_or(vdst1, (vector unsigned char)vbpixel);
icculus@1047
   923
            
icculus@1047
   924
            /* Store 8 pixels */
icculus@1047
   925
            vec_st(vdst1, 0, dst);
icculus@1047
   926
icculus@1047
   927
            width -= 8;
icculus@1047
   928
            dst += 16;
icculus@1047
   929
        }
icculus@1047
   930
        ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
   931
#undef ONE_PIXEL_BLEND
icculus@1047
   932
        src += srcskip;
icculus@1047
   933
        dst += dstskip;
icculus@1047
   934
    }
icculus@1047
   935
}
icculus@1047
   936
icculus@1047
   937
static void Blit32to32SurfaceAlphaKeyAltivec(SDL_BlitInfo *info)
icculus@1047
   938
{
icculus@1047
   939
    unsigned alpha = info->src->alpha;
icculus@1047
   940
    int height = info->d_height;
icculus@1047
   941
    Uint32 *srcp = (Uint32 *)info->s_pixels;
icculus@1047
   942
    int srcskip = info->s_skip >> 2;
icculus@1047
   943
    Uint32 *dstp = (Uint32 *)info->d_pixels;
icculus@1047
   944
    int dstskip = info->d_skip >> 2;
icculus@1047
   945
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
   946
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
   947
    unsigned sA = srcfmt->alpha;
icculus@1047
   948
    unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
   949
    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
icculus@1047
   950
    Uint32 ckey = info->src->colorkey;
icculus@1047
   951
    vector unsigned char mergePermute;
icculus@1047
   952
    vector unsigned char vsrcPermute;
icculus@1047
   953
    vector unsigned char vdstPermute;
icculus@1047
   954
    vector unsigned char vsdstPermute;
icculus@1047
   955
    vector unsigned char valpha;
icculus@1047
   956
    vector unsigned char valphamask;
icculus@1047
   957
    vector unsigned char vbits;
icculus@1047
   958
    vector unsigned char v0;
icculus@1047
   959
    vector unsigned short v1;
icculus@1047
   960
    vector unsigned short v8;
icculus@1047
   961
    vector unsigned int vckey;
icculus@1047
   962
    vector unsigned int vrgbmask;
icculus@1047
   963
icculus@1047
   964
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
   965
    v0 = vec_splat_u8(0);
icculus@1047
   966
    v1 = vec_splat_u16(1);
icculus@1047
   967
    v8 = vec_splat_u16(8);
icculus@1047
   968
icculus@1047
   969
    /* set the alpha to 255 on the destination surf */
icculus@1047
   970
    valphamask = VEC_ALPHA_MASK();
icculus@1047
   971
icculus@1047
   972
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
   973
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
   974
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
   975
icculus@1047
   976
    /* set a vector full of alpha and 255-alpha */
icculus@1047
   977
    ((unsigned char *)&valpha)[0] = alpha;
icculus@1047
   978
    valpha = vec_splat(valpha, 0);
icculus@1047
   979
    vbits = (vector unsigned char)vec_splat_s8(-1);
icculus@1047
   980
icculus@1047
   981
    ckey &= rgbmask;
icculus@1162
   982
    ((unsigned int *)(char*)&vckey)[0] = ckey;
icculus@1047
   983
    vckey = vec_splat(vckey, 0);
icculus@1162
   984
    ((unsigned int *)(char*)&vrgbmask)[0] = rgbmask;
icculus@1047
   985
    vrgbmask = vec_splat(vrgbmask, 0);
icculus@1047
   986
icculus@1047
   987
    while(height--) {
icculus@1047
   988
        int width = info->d_width;
icculus@1047
   989
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
   990
        while (condition) { \
icculus@1162
   991
            Uint32 Pixel; \
icculus@1047
   992
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
   993
            RETRIEVE_RGB_PIXEL(((Uint8 *)srcp), 4, Pixel); \
icculus@1162
   994
            if(sA && Pixel != ckey) { \
icculus@1162
   995
                RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
icculus@1162
   996
                DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
   997
                ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
   998
                ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
   999
            } \
icculus@1162
  1000
            dstp++; \
icculus@1162
  1001
            srcp++; \
icculus@1047
  1002
            widthvar--; \
icculus@1047
  1003
        }
icculus@1047
  1004
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1005
        if (width > 0) {
icculus@1047
  1006
            int extrawidth = (width % 4);
icculus@1047
  1007
            vector unsigned char valigner = VEC_ALIGNER(srcp);
icculus@1047
  1008
            vector unsigned char vs = (vector unsigned char)vec_ld(0, srcp);
icculus@1047
  1009
            width -= extrawidth;
icculus@1047
  1010
            while (width) {
icculus@1047
  1011
                vector unsigned char vsel;
icculus@1047
  1012
                vector unsigned char voverflow;
icculus@1047
  1013
                vector unsigned char vd;
icculus@1047
  1014
                vector unsigned char vd_orig;
icculus@1047
  1015
icculus@1047
  1016
                /* s = *srcp */
icculus@1047
  1017
                voverflow = (vector unsigned char)vec_ld(15, srcp);
icculus@1047
  1018
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1019
                
icculus@1047
  1020
                /* vsel is set for items that match the key */
icculus@1047
  1021
                vsel = (vector unsigned char)vec_and((vector unsigned int)vs, vrgbmask);
icculus@1047
  1022
                vsel = (vector unsigned char)vec_cmpeq((vector unsigned int)vsel, vckey);
icculus@1047
  1023
icculus@1047
  1024
                /* permute to source format */
icculus@1047
  1025
                vs = vec_perm(vs, valpha, vsrcPermute);
icculus@1047
  1026
icculus@1047
  1027
                /* d = *dstp */
icculus@1047
  1028
                vd = (vector unsigned char)vec_ld(0, dstp);
icculus@1047
  1029
                vd_orig = vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
  1030
icculus@1047
  1031
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1032
icculus@1047
  1033
                /* set the alpha channel to full on */
icculus@1047
  1034
                vd = vec_or(vd, valphamask);
icculus@1047
  1035
icculus@1047
  1036
                /* mask out color key */
icculus@1047
  1037
                vd = vec_sel(vd, vd_orig, vsel);
icculus@1047
  1038
                
icculus@1047
  1039
                /* permute to dest format */
icculus@1047
  1040
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1041
icculus@1047
  1042
                /* *dstp = res */
icculus@1047
  1043
                vec_st((vector unsigned int)vd, 0, dstp);
icculus@1047
  1044
                
icculus@1047
  1045
                srcp += 4;
icculus@1047
  1046
                dstp += 4;
icculus@1047
  1047
                width -= 4;
icculus@1047
  1048
                vs = voverflow;
icculus@1047
  1049
            }
icculus@1047
  1050
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1051
        }
icculus@1047
  1052
#undef ONE_PIXEL_BLEND
icculus@1047
  1053
 
icculus@1047
  1054
        srcp += srcskip;
icculus@1047
  1055
        dstp += dstskip;
icculus@1047
  1056
    }
icculus@1047
  1057
}
icculus@1047
  1058
icculus@1047
  1059
icculus@1047
  1060
static void Blit32to32PixelAlphaAltivec(SDL_BlitInfo *info)
icculus@1047
  1061
{
icculus@1047
  1062
    int width = info->d_width;
icculus@1047
  1063
    int height = info->d_height;
icculus@1047
  1064
    Uint32 *srcp = (Uint32 *)info->s_pixels;
icculus@1047
  1065
    int srcskip = info->s_skip >> 2;
icculus@1047
  1066
    Uint32 *dstp = (Uint32 *)info->d_pixels;
icculus@1047
  1067
    int dstskip = info->d_skip >> 2;
icculus@1047
  1068
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
  1069
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
  1070
    vector unsigned char mergePermute;
icculus@1047
  1071
    vector unsigned char valphaPermute;
icculus@1047
  1072
    vector unsigned char vsrcPermute;
icculus@1047
  1073
    vector unsigned char vdstPermute;
icculus@1047
  1074
    vector unsigned char vsdstPermute;
icculus@1047
  1075
    vector unsigned char valphamask;
icculus@1047
  1076
    vector unsigned char vpixelmask;
icculus@1047
  1077
    vector unsigned char v0;
icculus@1047
  1078
    vector unsigned short v1;
icculus@1047
  1079
    vector unsigned short v8;
icculus@1047
  1080
icculus@1047
  1081
    v0 = vec_splat_u8(0);
icculus@1047
  1082
    v1 = vec_splat_u16(1);
icculus@1047
  1083
    v8 = vec_splat_u16(8);
icculus@1047
  1084
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1085
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1086
    valphaPermute = vec_and(vec_lvsl(0, (int *)NULL), vec_splat_u8(0xC));
icculus@1047
  1087
    vpixelmask = vec_nor(valphamask, v0);
icculus@1047
  1088
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1089
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1090
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1091
icculus@1047
  1092
	while ( height-- ) {
icculus@1047
  1093
        width = info->d_width;
icculus@1047
  1094
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1095
            Uint32 Pixel; \
icculus@1047
  1096
            unsigned sR, sG, sB, dR, dG, dB, sA, dA; \
icculus@1162
  1097
            DISEMBLE_RGBA((Uint8 *)srcp, 4, srcfmt, Pixel, sR, sG, sB, sA); \
icculus@1047
  1098
            if(sA) { \
icculus@1162
  1099
              DISEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, Pixel, dR, dG, dB, dA); \
icculus@1047
  1100
              ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1101
              ASSEMBLE_RGBA((Uint8 *)dstp, 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1102
            } \
icculus@1047
  1103
            ++srcp; \
icculus@1047
  1104
            ++dstp; \
icculus@1047
  1105
            widthvar--; \
icculus@1047
  1106
        }
icculus@1047
  1107
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1108
        if (width > 0) {
slouken@1487
  1109
            /* vsrcPermute */
slouken@1487
  1110
            /* vdstPermute */
icculus@1047
  1111
            int extrawidth = (width % 4);
icculus@1047
  1112
            vector unsigned char valigner = VEC_ALIGNER(srcp);
icculus@1047
  1113
            vector unsigned char vs = (vector unsigned char)vec_ld(0, srcp);
icculus@1047
  1114
            width -= extrawidth;
icculus@1047
  1115
            while (width) {
icculus@1047
  1116
                vector unsigned char voverflow;
icculus@1047
  1117
                vector unsigned char vd;
icculus@1047
  1118
                vector unsigned char valpha;
icculus@1047
  1119
                vector unsigned char vdstalpha;
icculus@1047
  1120
                /* s = *srcp */
icculus@1047
  1121
                voverflow = (vector unsigned char)vec_ld(15, srcp);
icculus@1047
  1122
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1123
                vs = vec_perm(vs, v0, vsrcPermute);
icculus@1047
  1124
icculus@1047
  1125
                valpha = vec_perm(vs, v0, valphaPermute);
icculus@1047
  1126
                
icculus@1047
  1127
                /* d = *dstp */
icculus@1047
  1128
                vd = (vector unsigned char)vec_ld(0, dstp);
icculus@1047
  1129
                vd = vec_perm(vd, v0, vsdstPermute);
icculus@1047
  1130
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1131
icculus@1047
  1132
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1133
icculus@1047
  1134
                /* set the alpha to the dest alpha */
icculus@1047
  1135
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1136
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1137
                vd = vec_perm(vd, v0, vdstPermute);
icculus@1047
  1138
icculus@1047
  1139
                /* *dstp = res */
icculus@1047
  1140
                vec_st((vector unsigned int)vd, 0, dstp);
icculus@1047
  1141
                
icculus@1047
  1142
                srcp += 4;
icculus@1047
  1143
                dstp += 4;
icculus@1047
  1144
                width -= 4;
icculus@1047
  1145
                vs = voverflow;
icculus@1047
  1146
icculus@1047
  1147
            }
icculus@1047
  1148
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1149
        }
icculus@1047
  1150
	    srcp += srcskip;
icculus@1047
  1151
	    dstp += dstskip;
icculus@1047
  1152
#undef ONE_PIXEL_BLEND
icculus@1047
  1153
	}
icculus@1047
  1154
}
icculus@1047
  1155
icculus@1047
  1156
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
icculus@1047
  1157
static void BlitRGBtoRGBPixelAlphaAltivec(SDL_BlitInfo *info)
icculus@1047
  1158
{
icculus@1047
  1159
	int width = info->d_width;
icculus@1047
  1160
	int height = info->d_height;
icculus@1047
  1161
	Uint32 *srcp = (Uint32 *)info->s_pixels;
icculus@1047
  1162
	int srcskip = info->s_skip >> 2;
icculus@1047
  1163
	Uint32 *dstp = (Uint32 *)info->d_pixels;
icculus@1047
  1164
	int dstskip = info->d_skip >> 2;
icculus@1047
  1165
    vector unsigned char mergePermute;
icculus@1047
  1166
    vector unsigned char valphaPermute;
icculus@1047
  1167
    vector unsigned char valphamask;
icculus@1047
  1168
    vector unsigned char vpixelmask;
icculus@1047
  1169
    vector unsigned char v0;
icculus@1047
  1170
    vector unsigned short v1;
icculus@1047
  1171
    vector unsigned short v8;
icculus@1047
  1172
    v0 = vec_splat_u8(0);
icculus@1047
  1173
    v1 = vec_splat_u16(1);
icculus@1047
  1174
    v8 = vec_splat_u16(8);
icculus@1047
  1175
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1176
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1177
    valphaPermute = vec_and(vec_lvsl(0, (int *)NULL), vec_splat_u8(0xC));
icculus@1047
  1178
    
icculus@1047
  1179
 
icculus@1047
  1180
    vpixelmask = vec_nor(valphamask, v0);
icculus@1047
  1181
	while(height--) {
icculus@1047
  1182
        width = info->d_width;
icculus@1047
  1183
#define ONE_PIXEL_BLEND(condition, widthvar) \
icculus@1047
  1184
        while ((condition)) { \
icculus@1047
  1185
            Uint32 dalpha; \
icculus@1047
  1186
            Uint32 d; \
icculus@1047
  1187
            Uint32 s1; \
icculus@1047
  1188
            Uint32 d1; \
icculus@1047
  1189
            Uint32 s = *srcp; \
icculus@1047
  1190
            Uint32 alpha = s >> 24; \
icculus@1047
  1191
            if(alpha) { \
icculus@1047
  1192
              if(alpha == SDL_ALPHA_OPAQUE) { \
icculus@1047
  1193
                *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000); \
icculus@1047
  1194
              } else { \
icculus@1047
  1195
                d = *dstp; \
icculus@1047
  1196
                dalpha = d & 0xff000000; \
icculus@1047
  1197
                s1 = s & 0xff00ff; \
icculus@1047
  1198
                d1 = d & 0xff00ff; \
icculus@1047
  1199
                d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff; \
icculus@1047
  1200
                s &= 0xff00; \
icculus@1047
  1201
                d &= 0xff00; \
icculus@1047
  1202
                d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1203
                *dstp = d1 | d | dalpha; \
icculus@1047
  1204
              } \
icculus@1047
  1205
            } \
icculus@1047
  1206
            ++srcp; \
icculus@1047
  1207
            ++dstp; \
icculus@1047
  1208
            widthvar--; \
icculus@1047
  1209
	    }
icculus@1047
  1210
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1211
        if (width > 0) {
icculus@1047
  1212
            int extrawidth = (width % 4);
icculus@1047
  1213
            vector unsigned char valigner = VEC_ALIGNER(srcp);
icculus@1047
  1214
            vector unsigned char vs = (vector unsigned char)vec_ld(0, srcp);
icculus@1047
  1215
            width -= extrawidth;
icculus@1047
  1216
            while (width) {
icculus@1047
  1217
                vector unsigned char voverflow;
icculus@1047
  1218
                vector unsigned char vd;
icculus@1047
  1219
                vector unsigned char valpha;
icculus@1047
  1220
                vector unsigned char vdstalpha;
icculus@1047
  1221
                /* s = *srcp */
icculus@1047
  1222
                voverflow = (vector unsigned char)vec_ld(15, srcp);
icculus@1047
  1223
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1224
icculus@1047
  1225
                valpha = vec_perm(vs, v0, valphaPermute);
icculus@1047
  1226
                
icculus@1047
  1227
                /* d = *dstp */
icculus@1047
  1228
                vd = (vector unsigned char)vec_ld(0, dstp);
icculus@1047
  1229
                vdstalpha = vec_and(vd, valphamask);
icculus@1047
  1230
icculus@1047
  1231
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1232
icculus@1047
  1233
                /* set the alpha to the dest alpha */
icculus@1047
  1234
                vd = vec_and(vd, vpixelmask);
icculus@1047
  1235
                vd = vec_or(vd, vdstalpha);
icculus@1047
  1236
icculus@1047
  1237
                /* *dstp = res */
icculus@1047
  1238
                vec_st((vector unsigned int)vd, 0, dstp);
icculus@1047
  1239
                
icculus@1047
  1240
                srcp += 4;
icculus@1047
  1241
                dstp += 4;
icculus@1047
  1242
                width -= 4;
icculus@1047
  1243
                vs = voverflow;
icculus@1047
  1244
            }
icculus@1047
  1245
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1246
        }
icculus@1047
  1247
	    srcp += srcskip;
icculus@1047
  1248
	    dstp += dstskip;
icculus@1047
  1249
	}
icculus@1047
  1250
#undef ONE_PIXEL_BLEND
icculus@1047
  1251
}
icculus@1047
  1252
icculus@1047
  1253
static void Blit32to32SurfaceAlphaAltivec(SDL_BlitInfo *info)
icculus@1047
  1254
{
icculus@1047
  1255
    /* XXX : 6 */
icculus@1047
  1256
	unsigned alpha = info->src->alpha;
icculus@1047
  1257
    int height = info->d_height;
icculus@1047
  1258
    Uint32 *srcp = (Uint32 *)info->s_pixels;
icculus@1047
  1259
    int srcskip = info->s_skip >> 2;
icculus@1047
  1260
    Uint32 *dstp = (Uint32 *)info->d_pixels;
icculus@1047
  1261
    int dstskip = info->d_skip >> 2;
icculus@1047
  1262
    SDL_PixelFormat *srcfmt = info->src;
icculus@1047
  1263
    SDL_PixelFormat *dstfmt = info->dst;
icculus@1047
  1264
	unsigned sA = srcfmt->alpha;
icculus@1047
  1265
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
icculus@1047
  1266
    vector unsigned char mergePermute;
icculus@1047
  1267
    vector unsigned char vsrcPermute;
icculus@1047
  1268
    vector unsigned char vdstPermute;
icculus@1047
  1269
    vector unsigned char vsdstPermute;
icculus@1047
  1270
    vector unsigned char valpha;
icculus@1047
  1271
    vector unsigned char valphamask;
icculus@1047
  1272
    vector unsigned char vbits;
icculus@1047
  1273
    vector unsigned short v1;
icculus@1047
  1274
    vector unsigned short v8;
icculus@1047
  1275
icculus@1047
  1276
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1277
    v1 = vec_splat_u16(1);
icculus@1047
  1278
    v8 = vec_splat_u16(8);
icculus@1047
  1279
icculus@1047
  1280
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1281
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1282
icculus@1047
  1283
    vsrcPermute = calc_swizzle32(srcfmt, NULL);
icculus@1047
  1284
    vdstPermute = calc_swizzle32(NULL, dstfmt);
icculus@1047
  1285
    vsdstPermute = calc_swizzle32(dstfmt, NULL);
icculus@1047
  1286
icculus@1047
  1287
    /* set a vector full of alpha and 255-alpha */
icculus@1047
  1288
    ((unsigned char *)&valpha)[0] = alpha;
icculus@1047
  1289
    valpha = vec_splat(valpha, 0);
icculus@1047
  1290
    vbits = (vector unsigned char)vec_splat_s8(-1);
icculus@1047
  1291
icculus@1047
  1292
    while(height--) {
icculus@1047
  1293
        int width = info->d_width;
icculus@1047
  1294
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1162
  1295
            Uint32 Pixel; \
icculus@1047
  1296
            unsigned sR, sG, sB, dR, dG, dB; \
icculus@1162
  1297
            DISEMBLE_RGB(((Uint8 *)srcp), 4, srcfmt, Pixel, sR, sG, sB); \
icculus@1162
  1298
            DISEMBLE_RGB(((Uint8 *)dstp), 4, dstfmt, Pixel, dR, dG, dB); \
icculus@1047
  1299
            ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB); \
icculus@1047
  1300
            ASSEMBLE_RGBA(((Uint8 *)dstp), 4, dstfmt, dR, dG, dB, dA); \
icculus@1047
  1301
            ++srcp; \
icculus@1047
  1302
            ++dstp; \
icculus@1047
  1303
            widthvar--; \
icculus@1047
  1304
        }
icculus@1047
  1305
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1306
        if (width > 0) {
icculus@1047
  1307
            int extrawidth = (width % 4);
icculus@3910
  1308
            vector unsigned char valigner = VEC_ALIGNER(srcp);
icculus@1047
  1309
            vector unsigned char vs = (vector unsigned char)vec_ld(0, srcp);
icculus@1047
  1310
            width -= extrawidth;
icculus@1047
  1311
            while (width) {
icculus@1047
  1312
                vector unsigned char voverflow;
icculus@1047
  1313
                vector unsigned char vd;
icculus@1047
  1314
icculus@1047
  1315
                /* s = *srcp */
icculus@1047
  1316
                voverflow = (vector unsigned char)vec_ld(15, srcp);
icculus@1047
  1317
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1318
                vs = vec_perm(vs, valpha, vsrcPermute);
icculus@1047
  1319
                
icculus@1047
  1320
                /* d = *dstp */
icculus@1047
  1321
                vd = (vector unsigned char)vec_ld(0, dstp);
icculus@1047
  1322
                vd = vec_perm(vd, vd, vsdstPermute);
icculus@1047
  1323
icculus@1047
  1324
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1325
icculus@1047
  1326
                /* set the alpha channel to full on */
icculus@1047
  1327
                vd = vec_or(vd, valphamask);
icculus@1047
  1328
                vd = vec_perm(vd, vbits, vdstPermute);
icculus@1047
  1329
icculus@1047
  1330
                /* *dstp = res */
icculus@1047
  1331
                vec_st((vector unsigned int)vd, 0, dstp);
icculus@1047
  1332
                
icculus@1047
  1333
                srcp += 4;
icculus@1047
  1334
                dstp += 4;
icculus@1047
  1335
                width -= 4;
icculus@1047
  1336
                vs = voverflow;
icculus@1047
  1337
            }
icculus@1047
  1338
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1339
        }
icculus@1047
  1340
#undef ONE_PIXEL_BLEND
icculus@1047
  1341
 
icculus@1047
  1342
        srcp += srcskip;
icculus@1047
  1343
        dstp += dstskip;
icculus@1047
  1344
    }
icculus@1047
  1345
icculus@1047
  1346
}
icculus@1047
  1347
icculus@1047
  1348
icculus@1047
  1349
/* fast RGB888->(A)RGB888 blending */
icculus@1047
  1350
static void BlitRGBtoRGBSurfaceAlphaAltivec(SDL_BlitInfo *info)
icculus@1047
  1351
{
icculus@1047
  1352
	unsigned alpha = info->src->alpha;
icculus@1047
  1353
    int height = info->d_height;
icculus@1047
  1354
    Uint32 *srcp = (Uint32 *)info->s_pixels;
icculus@1047
  1355
    int srcskip = info->s_skip >> 2;
icculus@1047
  1356
    Uint32 *dstp = (Uint32 *)info->d_pixels;
icculus@1047
  1357
    int dstskip = info->d_skip >> 2;
icculus@1047
  1358
    vector unsigned char mergePermute;
icculus@1047
  1359
    vector unsigned char valpha;
icculus@1047
  1360
    vector unsigned char valphamask;
icculus@1047
  1361
    vector unsigned short v1;
icculus@1047
  1362
    vector unsigned short v8;
icculus@1047
  1363
icculus@1047
  1364
    mergePermute = VEC_MERGE_PERMUTE();
icculus@1047
  1365
    v1 = vec_splat_u16(1);
icculus@1047
  1366
    v8 = vec_splat_u16(8);
icculus@1047
  1367
icculus@1047
  1368
    /* set the alpha to 255 on the destination surf */
icculus@1047
  1369
    valphamask = VEC_ALPHA_MASK();
icculus@1047
  1370
icculus@1047
  1371
    /* set a vector full of alpha and 255-alpha */
icculus@1047
  1372
    ((unsigned char *)&valpha)[0] = alpha;
icculus@1047
  1373
    valpha = vec_splat(valpha, 0);
icculus@1047
  1374
icculus@1047
  1375
    while(height--) {
icculus@1047
  1376
        int width = info->d_width;
icculus@1047
  1377
#define ONE_PIXEL_BLEND(condition, widthvar) while ((condition)) { \
icculus@1047
  1378
            Uint32 s = *srcp; \
icculus@1047
  1379
            Uint32 d = *dstp; \
icculus@1047
  1380
            Uint32 s1 = s & 0xff00ff; \
icculus@1047
  1381
            Uint32 d1 = d & 0xff00ff; \
icculus@1047
  1382
            d1 = (d1 + ((s1 - d1) * alpha >> 8)) \
icculus@1047
  1383
                 & 0xff00ff; \
icculus@1047
  1384
            s &= 0xff00; \
icculus@1047
  1385
            d &= 0xff00; \
icculus@1047
  1386
            d = (d + ((s - d) * alpha >> 8)) & 0xff00; \
icculus@1047
  1387
            *dstp = d1 | d | 0xff000000; \
icculus@1047
  1388
            ++srcp; \
icculus@1047
  1389
            ++dstp; \
icculus@1047
  1390
            widthvar--; \
icculus@1047
  1391
        }
icculus@1047
  1392
        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
icculus@1047
  1393
        if (width > 0) {
icculus@1047
  1394
            int extrawidth = (width % 4);
icculus@1047
  1395
            vector unsigned char valigner = VEC_ALIGNER(srcp);
icculus@1047
  1396
            vector unsigned char vs = (vector unsigned char)vec_ld(0, srcp);
icculus@1047
  1397
            width -= extrawidth;
icculus@1047
  1398
            while (width) {
icculus@1047
  1399
                vector unsigned char voverflow;
icculus@1047
  1400
                vector unsigned char vd;
icculus@1047
  1401
icculus@1047
  1402
                /* s = *srcp */
icculus@1047
  1403
                voverflow = (vector unsigned char)vec_ld(15, srcp);
icculus@1047
  1404
                vs = vec_perm(vs, voverflow, valigner);
icculus@1047
  1405
                
icculus@1047
  1406
                /* d = *dstp */
icculus@1047
  1407
                vd = (vector unsigned char)vec_ld(0, dstp);
icculus@1047
  1408
icculus@1047
  1409
                VEC_MULTIPLY_ALPHA(vs, vd, valpha, mergePermute, v1, v8);
icculus@1047
  1410
icculus@1047
  1411
                /* set the alpha channel to full on */
icculus@1047
  1412
                vd = vec_or(vd, valphamask);
icculus@1047
  1413
icculus@1047
  1414
                /* *dstp = res */
icculus@1047
  1415
                vec_st((vector unsigned int)vd, 0, dstp);
icculus@1047
  1416
                
icculus@1047
  1417
                srcp += 4;
icculus@1047
  1418
                dstp += 4;
icculus@1047
  1419
                width -= 4;
icculus@1047
  1420
                vs = voverflow;
icculus@1047
  1421
            }
icculus@1047
  1422
            ONE_PIXEL_BLEND((extrawidth), extrawidth);
icculus@1047
  1423
        }
icculus@1047
  1424
#undef ONE_PIXEL_BLEND
icculus@1047
  1425
 
icculus@1047
  1426
        srcp += srcskip;
icculus@1047
  1427
        dstp += dstskip;
icculus@1047
  1428
    }
icculus@1047
  1429
}
slouken@1795
  1430
#if __MWERKS__
slouken@1795
  1431
#pragma altivec_model off
slouken@1795
  1432
#endif
slouken@1361
  1433
#endif /* SDL_ALTIVEC_BLITTERS */
icculus@1047
  1434
slouken@1
  1435
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1
  1436
static void BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo *info)
slouken@0
  1437
{
slouken@0
  1438
	int width = info->d_width;
slouken@0
  1439
	int height = info->d_height;
slouken@0
  1440
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  1441
	int srcskip = info->s_skip >> 2;
slouken@0
  1442
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
  1443
	int dstskip = info->d_skip >> 2;
slouken@0
  1444
slouken@0
  1445
	while(height--) {
slouken@0
  1446
	    DUFFS_LOOP4({
slouken@1
  1447
		    Uint32 s = *srcp++;
slouken@1
  1448
		    Uint32 d = *dstp;
slouken@1
  1449
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1450
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1451
	    }, width);
slouken@0
  1452
	    srcp += srcskip;
slouken@0
  1453
	    dstp += dstskip;
slouken@0
  1454
	}
slouken@0
  1455
}
slouken@0
  1456
slouken@1
  1457
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1
  1458
static void BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo *info)
slouken@1
  1459
{
slouken@1
  1460
	unsigned alpha = info->src->alpha;
slouken@1
  1461
	if(alpha == 128) {
slouken@1
  1462
		BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1
  1463
	} else {
slouken@1
  1464
		int width = info->d_width;
slouken@1
  1465
		int height = info->d_height;
slouken@1
  1466
		Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1
  1467
		int srcskip = info->s_skip >> 2;
slouken@1
  1468
		Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1
  1469
		int dstskip = info->d_skip >> 2;
slouken@689
  1470
		Uint32 s;
slouken@689
  1471
		Uint32 d;
slouken@689
  1472
		Uint32 s1;
slouken@689
  1473
		Uint32 d1;
slouken@1
  1474
slouken@1
  1475
		while(height--) {
slouken@689
  1476
			DUFFS_LOOP_DOUBLE2({
slouken@689
  1477
				/* One Pixel Blend */
slouken@1
  1478
				s = *srcp;
slouken@1
  1479
				d = *dstp;
slouken@1
  1480
				s1 = s & 0xff00ff;
slouken@1
  1481
				d1 = d & 0xff00ff;
slouken@1
  1482
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1483
				     & 0xff00ff;
slouken@1
  1484
				s &= 0xff00;
slouken@1
  1485
				d &= 0xff00;
slouken@1
  1486
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1487
				*dstp = d1 | d | 0xff000000;
slouken@1
  1488
				++srcp;
slouken@1
  1489
				++dstp;
slouken@689
  1490
			},{
slouken@689
  1491
			        /* Two Pixels Blend */
slouken@689
  1492
				s = *srcp;
slouken@689
  1493
				d = *dstp;
slouken@689
  1494
				s1 = s & 0xff00ff;
slouken@689
  1495
				d1 = d & 0xff00ff;
slouken@689
  1496
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1497
				d1 &= 0xff00ff;
slouken@689
  1498
				     
slouken@689
  1499
				s = ((s & 0xff00) >> 8) | 
slouken@689
  1500
					((srcp[1] & 0xff00) << 8);
slouken@689
  1501
				d = ((d & 0xff00) >> 8) |
slouken@689
  1502
					((dstp[1] & 0xff00) << 8);
slouken@689
  1503
				d += (s - d) * alpha >> 8;
slouken@689
  1504
				d &= 0x00ff00ff;
slouken@689
  1505
				
slouken@689
  1506
				*dstp++ = d1 | ((d << 8) & 0xff00) | 0xff000000;
slouken@689
  1507
				++srcp;
slouken@689
  1508
				
slouken@689
  1509
			        s1 = *srcp;
slouken@689
  1510
				d1 = *dstp;
slouken@689
  1511
				s1 &= 0xff00ff;
slouken@689
  1512
				d1 &= 0xff00ff;
slouken@689
  1513
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1514
				d1 &= 0xff00ff;
slouken@689
  1515
				
slouken@689
  1516
				*dstp = d1 | ((d >> 8) & 0xff00) | 0xff000000;
slouken@689
  1517
				++srcp;
slouken@689
  1518
				++dstp;
slouken@1
  1519
			}, width);
slouken@1
  1520
			srcp += srcskip;
slouken@1
  1521
			dstp += dstskip;
slouken@1
  1522
		}
slouken@1
  1523
	}
slouken@1
  1524
}
slouken@1
  1525
slouken@0
  1526
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@0
  1527
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
slouken@0
  1528
{
slouken@0
  1529
	int width = info->d_width;
slouken@0
  1530
	int height = info->d_height;
slouken@0
  1531
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  1532
	int srcskip = info->s_skip >> 2;
slouken@0
  1533
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
  1534
	int dstskip = info->d_skip >> 2;
slouken@0
  1535
slouken@0
  1536
	while(height--) {
slouken@0
  1537
	    DUFFS_LOOP4({
slouken@0
  1538
		Uint32 dalpha;
slouken@0
  1539
		Uint32 d;
slouken@0
  1540
		Uint32 s1;
slouken@0
  1541
		Uint32 d1;
slouken@0
  1542
		Uint32 s = *srcp;
slouken@0
  1543
		Uint32 alpha = s >> 24;
slouken@0
  1544
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1545
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1546
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1547
		   Benchmark this! */
slouken@689
  1548
		if(alpha) {   
slouken@689
  1549
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1550
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1551
		  } else {
slouken@0
  1552
		    /*
slouken@0
  1553
		     * take out the middle component (green), and process
slouken@0
  1554
		     * the other two in parallel. One multiply less.
slouken@0
  1555
		     */
slouken@0
  1556
		    d = *dstp;
slouken@0
  1557
		    dalpha = d & 0xff000000;
slouken@0
  1558
		    s1 = s & 0xff00ff;
slouken@0
  1559
		    d1 = d & 0xff00ff;
slouken@0
  1560
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1561
		    s &= 0xff00;
slouken@0
  1562
		    d &= 0xff00;
slouken@0
  1563
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1564
		    *dstp = d1 | d | dalpha;
slouken@689
  1565
		  }
slouken@0
  1566
		}
slouken@0
  1567
		++srcp;
slouken@0
  1568
		++dstp;
slouken@0
  1569
	    }, width);
slouken@0
  1570
	    srcp += srcskip;
slouken@0
  1571
	    dstp += dstskip;
slouken@0
  1572
	}
slouken@0
  1573
}
slouken@0
  1574
slouken@1542
  1575
#if GCC_ASMBLIT
slouken@689
  1576
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@3870
  1577
static void BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo *info)
slouken@689
  1578
{
slouken@689
  1579
	int width = info->d_width;
slouken@689
  1580
	int height = info->d_height;
slouken@689
  1581
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@689
  1582
	int srcskip = info->s_skip >> 2;
slouken@689
  1583
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@689
  1584
	int dstskip = info->d_skip >> 2;
slouken@1542
  1585
	SDL_PixelFormat* sf = info->src;
slouken@1542
  1586
	Uint32 amask = sf->Amask;
slouken@689
  1587
slouken@689
  1588
	__asm__ (
slouken@689
  1589
	/* make mm6 all zeros. */
slouken@689
  1590
	"pxor       %%mm6, %%mm6\n"
slouken@689
  1591
	
slouken@689
  1592
	/* Make a mask to preserve the alpha. */
slouken@1542
  1593
	"movd      %0, %%mm7\n\t"           /* 0000F000 -> mm7 */
slouken@1542
  1594
	"punpcklbw %%mm7, %%mm7\n\t"        /* FF000000 -> mm7 */
slouken@1542
  1595
	"pcmpeqb   %%mm4, %%mm4\n\t"        /* FFFFFFFF -> mm4 */
slouken@1542
  1596
	"movq      %%mm4, %%mm3\n\t"        /* FFFFFFFF -> mm3 (for later) */
slouken@1542
  1597
	"pxor      %%mm4, %%mm7\n\t"        /* 00FFFFFF -> mm7 (mult mask) */
slouken@689
  1598
slouken@1542
  1599
	/* form channel masks */
slouken@1542
  1600
	"movq      %%mm7, %%mm4\n\t"        /* 00FFFFFF -> mm4 */
slouken@1542
  1601
	"packsswb  %%mm6, %%mm4\n\t"        /* 00000FFF -> mm4 (channel mask) */
slouken@1542
  1602
	"packsswb  %%mm6, %%mm3\n\t"        /* 0000FFFF -> mm3 */
slouken@1542
  1603
	"pxor      %%mm4, %%mm3\n\t"        /* 0000F000 -> mm3 (~channel mask) */
slouken@1542
  1604
	
slouken@1542
  1605
	/* get alpha channel shift */
slouken@1542
  1606
	"movd      %1, %%mm5\n\t" /* Ashift -> mm5 */
slouken@1542
  1607
icculus@3931
  1608
	  : /* nothing */ : "rm" (amask), "rm" ((Uint32) sf->Ashift) );
slouken@689
  1609
slouken@689
  1610
	while(height--) {
slouken@689
  1611
slouken@689
  1612
	    DUFFS_LOOP4({
slouken@1542
  1613
		Uint32 alpha;
slouken@689
  1614
slouken@689
  1615
		__asm__ (
slouken@689
  1616
		"prefetch 64(%0)\n"
slouken@689
  1617
		"prefetch 64(%1)\n"
slouken@689
  1618
			: : "r" (srcp), "r" (dstp) );
slouken@689
  1619
slouken@1542
  1620
		alpha = *srcp & amask;
slouken@689
  1621
		/* FIXME: Here we special-case opaque alpha since the
slouken@689
  1622
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@689
  1623
		   it correctly. Also special-case alpha=0 for speed?
slouken@689
  1624
		   Benchmark this! */
slouken@1542
  1625
		if(alpha == 0) {
slouken@1542
  1626
		    /* do nothing */
slouken@1542
  1627
		}
slouken@1542
  1628
		else if(alpha == amask) {
slouken@1542
  1629
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
  1630
		    /* using MMX here to free up regular registers for other things */
slouken@1542
  1631
			    __asm__ (
slouken@1542
  1632
		    "movd      (%0),  %%mm0\n\t" /* src(ARGB) -> mm0 (0000ARGB)*/
slouken@1542
  1633
		    "movd      (%1),  %%mm1\n\t" /* dst(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
  1634
		    "pand      %%mm4, %%mm0\n\t" /* src & chanmask -> mm0 */
slouken@1542
  1635
		    "pand      %%mm3, %%mm1\n\t" /* dst & ~chanmask -> mm2 */
slouken@1542
  1636
		    "por       %%mm0, %%mm1\n\t" /* src | dst -> mm1 */
slouken@1542
  1637
		    "movd      %%mm1, (%1) \n\t" /* mm1 -> dst */
slouken@1542
  1638
slouken@1542
  1639
		     : : "r" (srcp), "r" (dstp) );
slouken@689
  1640
		} 
slouken@689
  1641
slouken@689
  1642
		else {
slouken@689
  1643
			    __asm__ (
slouken@689
  1644
		    /* load in the source, and dst. */
slouken@689
  1645
		    "movd      (%0), %%mm0\n"		    /* mm0(s) = 0 0 0 0 | As Rs Gs Bs */
slouken@689
  1646
		    "movd      (%1), %%mm1\n"		    /* mm1(d) = 0 0 0 0 | Ad Rd Gd Bd */
slouken@689
  1647
slouken@689
  1648
		    /* Move the src alpha into mm2 */
slouken@689
  1649
slouken@689
  1650
		    /* if supporting pshufw */
slouken@689
  1651
		    /*"pshufw     $0x55, %%mm0, %%mm2\n" */ /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@689
  1652
		    /*"psrlw     $8, %%mm2\n" */
slouken@689
  1653
		    
slouken@689
  1654
		    /* else: */
slouken@1542
  1655
		    "movd       %2,    %%mm2\n"
slouken@1542
  1656
		    "psrld      %%mm5, %%mm2\n"                /* mm2 = 0 0 0 0 | 0  0  0  As */
slouken@689
  1657
		    "punpcklwd	%%mm2, %%mm2\n"	            /* mm2 = 0 0 0 0 |  0 As  0  As */
slouken@689
  1658
		    "punpckldq	%%mm2, %%mm2\n"             /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@1542
  1659
		    "pand       %%mm7, %%mm2\n"              /* to preserve dest alpha */
slouken@689
  1660
slouken@689
  1661
		    /* move the colors into words. */
slouken@689
  1662
		    "punpcklbw %%mm6, %%mm0\n"		    /* mm0 = 0 As 0 Rs | 0 Gs 0 Bs */
slouken@689
  1663
		    "punpcklbw %%mm6, %%mm1\n"              /* mm0 = 0 Ad 0 Rd | 0 Gd 0 Bd */
slouken@689
  1664
slouken@689
  1665
		    /* src - dst */
slouken@689
  1666
		    "psubw    %%mm1, %%mm0\n"		    /* mm0 = As-Ad Rs-Rd | Gs-Gd  Bs-Bd */
slouken@689
  1667
slouken@689
  1668
		    /* A * (src-dst) */
slouken@1542
  1669
		    "pmullw    %%mm2, %%mm0\n"		    /* mm0 = 0*As-d As*Rs-d | As*Gs-d  As*Bs-d */
slouken@1542
  1670
		    "psrlw     $8,    %%mm0\n"		    /* mm0 = 0>>8 Rc>>8 | Gc>>8  Bc>>8 */
slouken@1542
  1671
		    "paddb     %%mm1, %%mm0\n"		    /* mm0 = 0+Ad Rc+Rd | Gc+Gd  Bc+Bd */
slouken@689
  1672
slouken@689
  1673
		    "packuswb  %%mm0, %%mm0\n"              /* mm0 =             | Ac Rc Gc Bc */
slouken@689
  1674
		    
slouken@689
  1675
		    "movd      %%mm0, (%1)\n"               /* result in mm0 */
slouken@689
  1676
slouken@1542
  1677
		     : : "r" (srcp), "r" (dstp), "r" (alpha) );
slouken@689
  1678
slouken@689
  1679
		}
slouken@689
  1680
		++srcp;
slouken@689
  1681
		++dstp;
slouken@689
  1682
	    }, width);
slouken@689
  1683
	    srcp += srcskip;
slouken@689
  1684
	    dstp += dstskip;
slouken@689
  1685
	}
slouken@689
  1686
slouken@689
  1687
	__asm__ (
slouken@689
  1688
	"emms\n"
slouken@689
  1689
		:   );
slouken@689
  1690
}
slouken@1542
  1691
/* End GCC_ASMBLIT*/
slouken@1542
  1692
slouken@1542
  1693
#elif MSVC_ASMBLIT
slouken@1542
  1694
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1542
  1695
static void BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo *info)
slouken@1542
  1696
{
slouken@1542
  1697
	int width = info->d_width;
slouken@1542
  1698
	int height = info->d_height;
slouken@1542
  1699
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1542
  1700
	int srcskip = info->s_skip >> 2;
slouken@1542
  1701
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1542
  1702
	int dstskip = info->d_skip >> 2;
slouken@1542
  1703
	SDL_PixelFormat* sf = info->src;
slouken@1542
  1704
	Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1542
  1705
	Uint32 amask = sf->Amask;
slouken@1542
  1706
	Uint32 ashift = sf->Ashift;
slouken@1542
  1707
	Uint64 multmask;
slouken@1542
  1708
	
slouken@1542
  1709
	__m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
  1710
slouken@1542
  1711
	mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
slouken@1542
  1712
	multmask = ~(0xFFFFi64 << (ashift * 2));
slouken@1542
  1713
	dmask = *(__m64*) &multmask; /* dst alpha mask -> dmask */
slouken@1542
  1714
slouken@1542
  1715
	while(height--) {
slouken@1542
  1716
	    DUFFS_LOOP4({
slouken@1542
  1717
		Uint32 alpha;
slouken@1542
  1718
slouken@1542
  1719
		_m_prefetch(srcp + 16);
slouken@1542
  1720
		_m_prefetch(dstp + 16);
slouken@1542
  1721
slouken@1542
  1722
		alpha = *srcp & amask;
slouken@1542
  1723
		if (alpha == 0) {
slouken@1542
  1724
			/* do nothing */
slouken@1542
  1725
		} else if (alpha == amask) {
slouken@1542
  1726
			/* copy RGB, keep dst alpha */
slouken@1542
  1727
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
  1728
		} else {
slouken@1542
  1729
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
  1730
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
  1731
slouken@1542
  1732
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
  1733
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
  1734
slouken@1542
  1735
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
  1736
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
  1737
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  1738
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  1739
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
  1740
slouken@1542
  1741
			/* blend */		    
slouken@1542
  1742
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@1542
  1743
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@1542
  1744
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
  1745
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@1542
  1746
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
  1747
			
slouken@1542
  1748
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
  1749
		}
slouken@1542
  1750
		++srcp;
slouken@1542
  1751
		++dstp;
slouken@1542
  1752
	    }, width);
slouken@1542
  1753
	    srcp += srcskip;
slouken@1542
  1754
	    dstp += dstskip;
slouken@1542
  1755
	}
slouken@1542
  1756
	_mm_empty();
slouken@1542
  1757
}
slouken@1542
  1758
/* End MSVC_ASMBLIT */
slouken@1542
  1759
slouken@1542
  1760
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  1761
slouken@1
  1762
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1763
slouken@1
  1764
/* blend a single 16 bit pixel at 50% */
slouken@1
  1765
#define BLEND16_50(d, s, mask)						\
slouken@1
  1766
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1767
slouken@1
  1768
/* blend two 16 bit pixels at 50% */
slouken@1
  1769
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1770
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1771
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1772
slouken@1
  1773
static void Blit16to16SurfaceAlpha128(SDL_BlitInfo *info, Uint16 mask)
slouken@0
  1774
{
slouken@0
  1775
	int width = info->d_width;
slouken@0
  1776
	int height = info->d_height;
slouken@0
  1777
	Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@0
  1778
	int srcskip = info->s_skip >> 1;
slouken@0
  1779
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  1780
	int dstskip = info->d_skip >> 1;
slouken@0
  1781
slouken@0
  1782
	while(height--) {
slouken@1456
  1783
		if(((uintptr_t)srcp ^ (uintptr_t)dstp) & 2) {
slouken@1
  1784
			/*
slouken@1
  1785
			 * Source and destination not aligned, pipeline it.
slouken@1
  1786
			 * This is mostly a win for big blits but no loss for
slouken@1
  1787
			 * small ones
slouken@1
  1788
			 */
slouken@1
  1789
			Uint32 prev_sw;
slouken@1
  1790
			int w = width;
slouken@1
  1791
slouken@1
  1792
			/* handle odd destination */
slouken@1456
  1793
			if((uintptr_t)dstp & 2) {
slouken@1
  1794
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1795
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1796
				dstp++;
slouken@1
  1797
				srcp++;
slouken@1
  1798
				w--;
slouken@1
  1799
			}
slouken@1
  1800
			srcp++;	/* srcp is now 32-bit aligned */
slouken@1
  1801
slouken@1
  1802
			/* bootstrap pipeline with first halfword */
slouken@1
  1803
			prev_sw = ((Uint32 *)srcp)[-1];
slouken@1
  1804
slouken@1
  1805
			while(w > 1) {
slouken@1
  1806
				Uint32 sw, dw, s;
slouken@1
  1807
				sw = *(Uint32 *)srcp;
slouken@1
  1808
				dw = *(Uint32 *)dstp;
slouken@1443
  1809
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1443
  1810
				s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1811
#else
slouken@1443
  1812
				s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1813
#endif
slouken@1
  1814
				prev_sw = sw;
slouken@1
  1815
				*(Uint32 *)dstp = BLEND2x16_50(dw, s, mask);
slouken@1
  1816
				dstp += 2;
slouken@1
  1817
				srcp += 2;
slouken@1
  1818
				w -= 2;
slouken@1
  1819
			}
slouken@1
  1820
slouken@1
  1821
			/* final pixel if any */
slouken@1
  1822
			if(w) {
slouken@1
  1823
				Uint16 d = *dstp, s;
slouken@1443
  1824
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1443
  1825
				s = (Uint16)prev_sw;
slouken@1443
  1826
#else
slouken@1443
  1827
				s = (Uint16)(prev_sw >> 16);
slouken@1443
  1828
#endif
slouken@1
  1829
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1830
				srcp++;
slouken@1
  1831
				dstp++;
slouken@1
  1832
			}
slouken@1
  1833
			srcp += srcskip - 1;
slouken@1
  1834
			dstp += dstskip;
slouken@1
  1835
		} else {
slouken@1
  1836
			/* source and destination are aligned */
slouken@1
  1837
			int w = width;
slouken@1
  1838
slouken@1
  1839
			/* first odd pixel? */
slouken@1456
  1840
			if((uintptr_t)srcp & 2) {
slouken@1
  1841
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1842
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1843
				srcp++;
slouken@1
  1844
				dstp++;
slouken@1
  1845
				w--;
slouken@1
  1846
			}
slouken@1
  1847
			/* srcp and dstp are now 32-bit aligned */
slouken@1
  1848
slouken@1
  1849
			while(w > 1) {
slouken@1
  1850
				Uint32 sw = *(Uint32 *)srcp;
slouken@1
  1851
				Uint32 dw = *(Uint32 *)dstp;
slouken@1
  1852
				*(Uint32 *)dstp = BLEND2x16_50(dw, sw, mask);
slouken@1
  1853
				srcp += 2;
slouken@1
  1854
				dstp += 2;
slouken@1
  1855
				w -= 2;
slouken@1
  1856
			}
slouken@1
  1857
slouken@1
  1858
			/* last odd pixel? */
slouken@1
  1859
			if(w) {
slouken@1
  1860
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1861
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1862
				srcp++;
slouken@1
  1863
				dstp++;
slouken@1
  1864
			}
slouken@1
  1865
			srcp += srcskip;
slouken@1
  1866
			dstp += dstskip;
slouken@1
  1867
		}
slouken@1
  1868
	}
slouken@1
  1869
}
slouken@1
  1870
slouken@1542
  1871
#if GCC_ASMBLIT
slouken@689
  1872
/* fast RGB565->RGB565 blending with surface alpha */
slouken@689
  1873
static void Blit565to565SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@689
  1874
{
slouken@689
  1875
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@689
  1876
	if(alpha == 128) {
slouken@689
  1877
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@689
  1878
	} else {
slouken@689
  1879
		int width = info->d_width;
slouken@689
  1880
		int height = info->d_height;
slouken@689
  1881
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@689
  1882
		int srcskip = info->s_skip >> 1;
slouken@689
  1883
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@689
  1884
		int dstskip = info->d_skip >> 1;
slouken@1542
  1885
		Uint32 s, d;
slouken@1542
  1886
		Uint8 load[8];
slouken@689
  1887
	  
slouken@689
  1888
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  1889
		*(Uint64 *)load = alpha;
slouken@689
  1890
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@689
  1891
slouken@1542
  1892
		movq_m2r(*load, mm0); /* alpha(0000000A) -> mm0 */
slouken@1542
  1893
		punpcklwd_r2r(mm0, mm0); /* 00000A0A -> mm0 */
slouken@1542
  1894
		punpcklwd_r2r(mm0, mm0); /* 0A0A0A0A -> mm0 */
slouken@1542
  1895
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  1896
		   to reduce the number of operations */
slouken@1542
  1897
		psllq_i2r(3, mm0);
slouken@689
  1898
	  
slouken@1542
  1899
		/* Setup the 565 color channel masks */
slouken@720
  1900
		*(Uint64 *)load = 0x07E007E007E007E0ULL;
slouken@689
  1901
		movq_m2r(*load, mm4); /* MASKGREEN -> mm4 */
slouken@720
  1902
		*(Uint64 *)load = 0x001F001F001F001FULL;
slouken@689
  1903
		movq_m2r(*load, mm7); /* MASKBLUE -> mm7 */
slouken@689
  1904
		while(height--) {
slouken@1542
  1905
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1906
			{
slouken@1542
  1907
				s = *srcp++;
slouken@689
  1908
				d = *dstp;
slouken@689
  1909
				/*
slouken@689
  1910
				 * shift out the middle component (green) to
slouken@689
  1911
				 * the high 16 bits, and process all three RGB
slouken@689
  1912
				 * components at the same time.
slouken@689
  1913
				 */
slouken@689
  1914
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1915
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1916
				d += (s - d) * alpha >> 5;
slouken@689
  1917
				d &= 0x07e0f81f;
slouken@689
  1918
				*dstp++ = d | d >> 16;
slouken@1542
  1919
			},{
slouken@1542
  1920
				s = *srcp++;
slouken@689
  1921
				d = *dstp;
slouken@689
  1922
				/*
slouken@689
  1923
				 * shift out the middle component (green) to
slouken@689
  1924
				 * the high 16 bits, and process all three RGB
slouken@689
  1925
				 * components at the same time.
slouken@689
  1926
				 */
slouken@689
  1927
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1928
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1929
				d += (s - d) * alpha >> 5;
slouken@689
  1930
				d &= 0x07e0f81f;
slouken@689
  1931
				*dstp++ = d | d >> 16;
slouken@1542
  1932
				s = *srcp++;
slouken@689
  1933
				d = *dstp;
slouken@689
  1934
				/*
slouken@689
  1935
				 * shift out the middle component (green) to
slouken@689
  1936
				 * the high 16 bits, and process all three RGB
slouken@689
  1937
				 * components at the same time.
slouken@689
  1938
				 */
slouken@689
  1939
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1940
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1941
				d += (s - d) * alpha >> 5;
slouken@689
  1942
				d &= 0x07e0f81f;
slouken@689
  1943
				*dstp++ = d | d >> 16;
slouken@1542
  1944
			},{
slouken@1542
  1945
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  1946
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  1947
slouken@1542
  1948
				/* red -- does not need a mask since the right shift clears
slouken@1542
  1949
				   the uninteresting bits */
slouken@1542
  1950
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  1951
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  1952
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 [000r 000r 000r 000r] */
slouken@1542
  1953
				psrlw_i2r(11, mm6); /* mm6 >> 11 -> mm6 [000r 000r 000r 000r] */
slouken@1542
  1954
slouken@1542
  1955
				/* blend */
slouken@1542
  1956
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  1957
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  1958
				/* alpha used is actually 11 bits
slouken@1542
  1959
				   11 + 5 = 16 bits, so the sign bits are lost */
slouken@1542
  1960
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  1961
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  1962
				psllw_i2r(11, mm6); /* mm6 << 11 -> mm6 */
slouken@1542
  1963
slouken@1542
  1964
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  1965
slouken@1542
  1966
				/* green -- process the bits in place */
slouken@1542
  1967
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  1968
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  1969
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  1970
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  1971
slouken@1542
  1972
				/* blend */
slouken@1542
  1973
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  1974
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  1975
				/* 11 + 11 - 16 = 6 bits, so all the lower uninteresting
slouken@1542
  1976
				   bits are gone and the sign bits present */
slouken@1542
  1977
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  1978
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  1979
slouken@1542
  1980
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  1981
slouken@1542
  1982
				/* blue */
slouken@1542
  1983
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  1984
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  1985
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  1986
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  1987
slouken@1542
  1988
				/* blend */
slouken@1542
  1989
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  1990
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  1991
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  1992
				   the interesting bits will need to be MASKed */
slouken@1542
  1993
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  1994
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  1995
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  1996
slouken@1542
  1997
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  1998
slouken@1542
  1999
				movq_r2m(mm1, *dstp); /* mm1 -> 4 dst pixels */
slouken@1542
  2000
slouken@1542
  2001
				srcp += 4;
slouken@1542
  2002
				dstp += 4;
slouken@1542
  2003
			}, width);			
slouken@689
  2004
			srcp += srcskip;
slouken@689
  2005
			dstp += dstskip;
slouken@689
  2006
		}
slouken@689
  2007
		emms();
slouken@689
  2008
	}
slouken@689
  2009
}
slouken@689
  2010
slouken@689
  2011
/* fast RGB555->RGB555 blending with surface alpha */
slouken@689
  2012
static void Blit555to555SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@689
  2013
{
slouken@689
  2014
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@689
  2015
	if(alpha == 128) {
slouken@689
  2016
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@689
  2017
	} else {
slouken@689
  2018
		int width = info->d_width;
slouken@689
  2019
		int height = info->d_height;
slouken@689
  2020
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@689
  2021
		int srcskip = info->s_skip >> 1;
slouken@689
  2022
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@689
  2023
		int dstskip = info->d_skip >> 1;
slouken@1542
  2024
		Uint32 s, d;
slouken@1542
  2025
		Uint8 load[8];
slouken@689
  2026
	  
slouken@689
  2027
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  2028
		*(Uint64 *)load = alpha;
slouken@689
  2029
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@689
  2030
slouken@1542
  2031
		movq_m2r(*load, mm0); /* alpha(0000000A) -> mm0 */
slouken@1542
  2032
		punpcklwd_r2r(mm0, mm0); /* 00000A0A -> mm0 */
slouken@1542
  2033
		punpcklwd_r2r(mm0, mm0); /* 0A0A0A0A -> mm0 */
slouken@1542
  2034
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2035
		   to reduce the number of operations */
slouken@1542
  2036
		psllq_i2r(3, mm0);
slouken@1542
  2037
slouken@1542
  2038
		/* Setup the 555 color channel masks */
slouken@720
  2039
		*(Uint64 *)load = 0x03E003E003E003E0ULL;
slouken@689
  2040
		movq_m2r(*load, mm4); /* MASKGREEN -> mm4 */
slouken@720
  2041
		*(Uint64 *)load = 0x001F001F001F001FULL;
slouken@689
  2042
		movq_m2r(*load, mm7); /* MASKBLUE -> mm7 */
slouken@689
  2043
		while(height--) {
slouken@1542
  2044
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2045
			{
slouken@1542
  2046
				s = *srcp++;
slouken@689
  2047
				d = *dstp;
slouken@689
  2048
				/*
slouken@689
  2049
				 * shift out the middle component (green) to
slouken@689
  2050
				 * the high 16 bits, and process all three RGB
slouken@689
  2051
				 * components at the same time.
slouken@689
  2052
				 */
slouken@689
  2053
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2054
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2055
				d += (s - d) * alpha >> 5;
slouken@689
  2056
				d &= 0x03e07c1f;
slouken@689
  2057
				*dstp++ = d | d >> 16;
slouken@1542
  2058
			},{
slouken@1542
  2059
				s = *srcp++;
slouken@689
  2060
				d = *dstp;
slouken@689
  2061
				/*
slouken@689
  2062
				 * shift out the middle component (green) to
slouken@689
  2063
				 * the high 16 bits, and process all three RGB
slouken@689
  2064
				 * components at the same time.
slouken@689
  2065
				 */
slouken@689
  2066
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2067
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2068
				d += (s - d) * alpha >> 5;
slouken@689
  2069
				d &= 0x03e07c1f;
slouken@689
  2070
				*dstp++ = d | d >> 16;
slouken@689
  2071
			        s = *srcp++;
slouken@689
  2072
				d = *dstp;
slouken@689
  2073
				/*
slouken@689
  2074
				 * shift out the middle component (green) to
slouken@689
  2075
				 * the high 16 bits, and process all three RGB
slouken@689
  2076
				 * components at the same time.
slouken@689
  2077
				 */
slouken@689
  2078
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2079
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2080
				d += (s - d) * alpha >> 5;
slouken@689
  2081
				d &= 0x03e07c1f;
slouken@689
  2082
				*dstp++ = d | d >> 16;
slouken@1542
  2083
			},{
slouken@1542
  2084
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  2085
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  2086
slouken@1542
  2087
				/* red -- process the bits in place */
slouken@1542
  2088
				psllq_i2r(5, mm4); /* turn MASKGREEN into MASKRED */
slouken@1542
  2089
					/* by reusing the GREEN mask we free up another mmx
slouken@1542
  2090
					   register to accumulate the result */
slouken@1542
  2091
slouken@1542
  2092
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2093
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2094
				pand_r2r(mm4, mm5); /* src & MASKRED -> mm5 */
slouken@1542
  2095
				pand_r2r(mm4, mm6); /* dst & MASKRED -> mm6 */
slouken@1542
  2096
slouken@1542
  2097
				/* blend */
slouken@1542
  2098
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2099
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2100
				/* 11 + 15 - 16 = 10 bits, uninteresting bits will be
slouken@1542
  2101
				   cleared by a MASK below */
slouken@1542
  2102
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2103
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2104
				pand_r2r(mm4, mm6); /* mm6 & MASKRED -> mm6 */
slouken@1542
  2105
slouken@1542
  2106
				psrlq_i2r(5, mm4); /* turn MASKRED back into MASKGREEN */
slouken@1542
  2107
slouken@1542
  2108
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  2109
slouken@1542
  2110
				/* green -- process the bits in place */
slouken@1542
  2111
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2112
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2113
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  2114
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  2115
slouken@1542
  2116
				/* blend */
slouken@1542
  2117
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2118
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2119
				/* 11 + 10 - 16 = 5 bits,  so all the lower uninteresting
slouken@1542
  2120
				   bits are gone and the sign bits present */
slouken@1542
  2121
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2122
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2123
slouken@1542
  2124
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  2125
slouken@1542
  2126
				/* blue */
slouken@1542
  2127
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2128
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2129
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  2130
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2131
slouken@1542
  2132
				/* blend */
slouken@1542
  2133
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2134
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2135
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  2136
				   the interesting bits will need to be MASKed */
slouken@1542
  2137
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2138
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2139
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2140
slouken@1542
  2141
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  2142
slouken@1542
  2143
				movq_r2m(mm1, *dstp);/* mm1 -> 4 dst pixels */
slouken@1542
  2144
slouken@1542
  2145
				srcp += 4;
slouken@1542
  2146
				dstp += 4;
slouken@1542
  2147
			}, width);			
slouken@689
  2148
			srcp += srcskip;
slouken@689
  2149
			dstp += dstskip;
slouken@689
  2150
		}
slouken@689
  2151
		emms();
slouken@689
  2152
	}
slouken@689
  2153
}
slouken@1542
  2154
/* End GCC_ASMBLIT */
slouken@1542
  2155
slouken@1542
  2156
#elif MSVC_ASMBLIT
slouken@1542
  2157
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1542
  2158
static void Blit565to565SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@1542
  2159
{
slouken@1542
  2160
	unsigned alpha = info->src->alpha;
slouken@1542
  2161
	if(alpha == 128) {
slouken@1542
  2162
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1542
  2163
	} else {
slouken@1542
  2164
		int width = info->d_width;
slouken@1542
  2165
		int height = info->d_height;
slouken@1542
  2166
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1542
  2167
		int srcskip = info->s_skip >> 1;
slouken@1542
  2168
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1542
  2169
		int dstskip = info->d_skip >> 1;
slouken@1542
  2170
		Uint32 s, d;
slouken@1542
  2171
	  
slouken@1542
  2172
		__m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2173
slouken@1542
  2174
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  2175
		mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
slouken@1542
  2176
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@1542
  2177
slouken@1542
  2178
		mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  2179
		mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  2180
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2181
		   to reduce the number of operations */
slouken@1542
  2182
		mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1542
  2183
	  
slouken@1542
  2184
		/* Setup the 565 color channel masks */
slouken@1542
  2185
		gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0); /* MASKGREEN -> gmask */
slouken@1542
  2186
		bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
slouken@1542
  2187
		
slouken@1542
  2188
		while(height--) {
slouken@1542
  2189
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2190
			{
slouken@1542
  2191
				s = *srcp++;
slouken@1542
  2192
				d = *dstp;
slouken@1542
  2193
				/*
slouken@1542
  2194
				 * shift out the middle component (green) to
slouken@1542
  2195
				 * the high 16 bits, and process all three RGB
slouken@1542
  2196
				 * components at the same time.
slouken@1542
  2197
				 */
slouken@1542
  2198
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2199
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2200
				d += (s - d) * alpha >> 5;
slouken@1542
  2201
				d &= 0x07e0f81f;
slouken@1546
  2202
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2203
			},{
slouken@1542
  2204
				s = *srcp++;
slouken@1542
  2205
				d = *dstp;
slouken@1542
  2206
				/*
slouken@1542
  2207
				 * shift out the middle component (green) to
slouken@1542
  2208
				 * the high 16 bits, and process all three RGB
slouken@1542
  2209
				 * components at the same time.
slouken@1542
  2210
				 */
slouken@1542
  2211
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2212
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2213
				d += (s - d) * alpha >> 5;
slouken@1542
  2214
				d &= 0x07e0f81f;
slouken@1546
  2215
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2216
				s = *srcp++;
slouken@1542
  2217
				d = *dstp;
slouken@1542
  2218
				/*
slouken@1542
  2219
				 * shift out the middle component (green) to
slouken@1542
  2220
				 * the high 16 bits, and process all three RGB
slouken@1542
  2221
				 * components at the same time.
slouken@1542
  2222
				 */
slouken@1542
  2223
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2224
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2225
				d += (s - d) * alpha >> 5;
slouken@1542
  2226
				d &= 0x07e0f81f;
slouken@1546
  2227
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2228
			},{
slouken@1542
  2229
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2230
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2231
slouken@1542
  2232
				/* red */
slouken@1542
  2233
				src2 = src1;
slouken@1542
  2234
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  2235
slouken@1542
  2236
				dst2 = dst1;
slouken@1542
  2237
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  2238
slouken@1542
  2239
				/* blend */
slouken@1542
  2240
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2241
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2242
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2243
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2244
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  2245
slouken@1542
  2246
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2247
slouken@1542
  2248
				/* green -- process the bits in place */
slouken@1542
  2249
				src2 = src1;
slouken@1542
  2250
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2251
slouken@1542
  2252
				dst2 = dst1;
slouken@1542
  2253
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2254
slouken@1542
  2255
				/* blend */
slouken@1542
  2256
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2257
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2258
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2259
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2260
slouken@1542
  2261
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2262
slouken@1542
  2263
				/* blue */
slouken@1542
  2264
				src2 = src1;
slouken@1542
  2265
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2266
slouken@1542
  2267
				dst2 = dst1;
slouken@1542
  2268
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2269
slouken@1542
  2270
				/* blend */
slouken@1542
  2271
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2272
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2273
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2274
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2275
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2276
slouken@1542
  2277
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2278
slouken@1542
  2279
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2280
slouken@1542
  2281
				srcp += 4;
slouken@1542
  2282
				dstp += 4;
slouken@1542
  2283
			}, width);			
slouken@1542
  2284
			srcp += srcskip;
slouken@1542
  2285
			dstp += dstskip;
slouken@1542
  2286
		}
slouken@1542
  2287
		_mm_empty();
slouken@1542
  2288
	}
slouken@1542
  2289
}
slouken@1542
  2290
slouken@1542
  2291
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1542
  2292
static void Blit555to555SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@1542
  2293
{
slouken@1542
  2294
	unsigned alpha = info->src->alpha;
slouken@1542
  2295
	if(alpha == 128) {
slouken@1542
  2296
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1542
  2297
	} else {
slouken@1542
  2298
		int width = info->d_width;
slouken@1542
  2299
		int height = info->d_height;
slouken@1542
  2300
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1542
  2301
		int srcskip = info->s_skip >> 1;
slouken@1542
  2302
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1542
  2303
		int dstskip = info->d_skip >> 1;
slouken@1542
  2304
		Uint32 s, d;
slouken@1542
  2305
	  
slouken@1542
  2306
		__m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2307
slouken@1542
  2308
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  2309
		mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
slouken@1542
  2310
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@1542
  2311
slouken@1542
  2312
		mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  2313
		mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  2314
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2315
		   to reduce the number of operations */
slouken@1542
  2316
		mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1542
  2317
	  
slouken@1542
  2318
		/* Setup the 555 color channel masks */
slouken@1542
  2319
		rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00); /* MASKRED -> rmask */
slouken@1542
  2320
		gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0); /* MASKGREEN -> gmask */
slouken@1542
  2321
		bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
slouken@1542
  2322
slouken@1542
  2323
		while(height--) {
slouken@1542
  2324
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2325
			{
slouken@1542
  2326
				s = *srcp++;
slouken@1542
  2327
				d = *dstp;
slouken@1542
  2328
				/*
slouken@1542
  2329
				 * shift out the middle component (green) to
slouken@1542
  2330
				 * the high 16 bits, and process all three RGB
slouken@1542
  2331
				 * components at the same time.
slouken@1542
  2332
				 */
slouken@1542
  2333
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2334
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2335
				d += (s - d) * alpha >> 5;
slouken@1542
  2336
				d &= 0x03e07c1f;
slouken@1546
  2337
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2338
			},{
slouken@1542
  2339
				s = *srcp++;
slouken@1542
  2340
				d = *dstp;
slouken@1542
  2341
				/*
slouken@1542
  2342
				 * shift out the middle component (green) to
slouken@1542
  2343
				 * the high 16 bits, and process all three RGB
slouken@1542
  2344
				 * components at the same time.
slouken@1542
  2345
				 */
slouken@1542
  2346
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2347
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2348
				d += (s - d) * alpha >> 5;
slouken@1542
  2349
				d &= 0x03e07c1f;
slouken@1546
  2350
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2351
			        s = *srcp++;
slouken@1542
  2352
				d = *dstp;
slouken@1542
  2353
				/*
slouken@1542
  2354
				 * shift out the middle component (green) to
slouken@1542
  2355
				 * the high 16 bits, and process all three RGB
slouken@1542
  2356
				 * components at the same time.
slouken@1542
  2357
				 */
slouken@1542
  2358
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2359
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2360
				d += (s - d) * alpha >> 5;
slouken@1542
  2361
				d &= 0x03e07c1f;
slouken@1546
  2362
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2363
			},{
slouken@1542
  2364
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2365
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2366
slouken@1542
  2367
				/* red -- process the bits in place */
slouken@1542
  2368
				src2 = src1;
slouken@1542
  2369
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  2370
slouken@1542
  2371
				dst2 = dst1;
slouken@1542
  2372
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  2373
slouken@1542
  2374
				/* blend */
slouken@1542
  2375
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2376
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2377
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2378
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2379
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  2380
slouken@1542
  2381
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2382
				
slouken@1542
  2383
				/* green -- process the bits in place */
slouken@1542
  2384
				src2 = src1;
slouken@1542
  2385
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2386
slouken@1542
  2387
				dst2 = dst1;
slouken@1542
  2388
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2389
slouken@1542
  2390
				/* blend */
slouken@1542
  2391
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2392
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2393
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2394
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2395
slouken@1542
  2396
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2397
slouken@1542
  2398
				/* blue */
slouken@1542
  2399
				src2 = src1; /* src -> src2 */
slouken@1542
  2400
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2401
slouken@1542
  2402
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  2403
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2404
slouken@1542
  2405
				/* blend */
slouken@1542
  2406
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2407
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2408
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2409
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2410
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2411
slouken@1542
  2412
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2413
slouken@1542
  2414
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2415
slouken@1542
  2416
				srcp += 4;
slouken@1542
  2417
				dstp += 4;
slouken@1542
  2418
			}, width);			
slouken@1542
  2419
			srcp += srcskip;
slouken@1542
  2420
			dstp += dstskip;
slouken@1542
  2421
		}
slouken@1542
  2422
		_mm_empty();
slouken@1542
  2423
	}
slouken@1542
  2424
}
slouken@1542
  2425
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  2426
slouken@1
  2427
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1
  2428
static void Blit565to565SurfaceAlpha(SDL_BlitInfo *info)
slouken@1
  2429
{
slouken@1
  2430
	unsigned alpha = info->src->alpha;
slouken@1
  2431
	if(alpha == 128) {
slouken@1
  2432
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1
  2433
	} else {
slouken@1
  2434
		int width = info->d_width;
slouken@1
  2435
		int height = info->d_height;
slouken@1
  2436
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
  2437
		int srcskip = info->s_skip >> 1;
slouken@1
  2438
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
  2439
		int dstskip = info->d_skip >> 1;
slouken@1
  2440
		alpha >>= 3;	/* downscale alpha to 5 bits */
slouken@1
  2441
slouken@1
  2442
		while(height--) {
slouken@1
  2443
			DUFFS_LOOP4({
slouken@1
  2444
				Uint32 s = *srcp++;
slouken@1
  2445
				Uint32 d = *dstp;
slouken@1
  2446
				/*
slouken@1
  2447
				 * shift out the middle component (green) to
slouken@1
  2448
				 * the high 16 bits, and process all three RGB
slouken@1
  2449
				 * components at the same time.
slouken@1
  2450
				 */
slouken@1
  2451
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  2452
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  2453
				d += (s - d) * alpha >> 5;
slouken@1
  2454
				d &= 0x07e0f81f;
slouken@1428
  2455
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2456
			}, width);
slouken@1
  2457
			srcp += srcskip;
slouken@1
  2458
			dstp += dstskip;
slouken@1
  2459
		}
slouken@0
  2460
	}
slouken@0
  2461
}
slouken@0
  2462
slouken@0
  2463
/* fast RGB555->RGB555 blending with surface alpha */
slouken@0
  2464
static void Blit555to555SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
  2465
{
slouken@1
  2466
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@1
  2467
	if(alpha == 128) {
slouken@1
  2468
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1
  2469
	} else {
slouken@1
  2470
		int width = info->d_width;
slouken@1
  2471
		int height = info->d_height;
slouken@1
  2472
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
  2473
		int srcskip = info->s_skip >> 1;
slouken@1
  2474
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
  2475
		int dstskip = info->d_skip >> 1;
slouken@1
  2476
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@0
  2477
slouken@1
  2478
		while(height--) {
slouken@1
  2479
			DUFFS_LOOP4({
slouken@1
  2480
				Uint32 s = *srcp++;
slouken@1
  2481
				Uint32 d = *dstp;
slouken@1
  2482
				/*
slouken@1
  2483
				 * shift out the middle component (green) to
slouken@1
  2484
				 * the high 16 bits, and process all three RGB
slouken@1
  2485
				 * components at the same time.
slouken@1
  2486
				 */
slouken@1
  2487
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  2488
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  2489
				d += (s - d) * alpha >> 5;
slouken@1
  2490
				d &= 0x03e07c1f;
slouken@1428
  2491
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2492
			}, width);
slouken@1
  2493
			srcp += srcskip;
slouken@1
  2494
			dstp += dstskip;
slouken@1
  2495
		}
slouken@0
  2496
	}
slouken@0
  2497
}
slouken@0
  2498
slouken@0
  2499
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@0
  2500
static void BlitARGBto565PixelAlpha(SDL_BlitInfo *info)
slouken@0
  2501
{
slouken@0
  2502
	int width = info->d_width;
slouken@0
  2503
	int height = info->d_height;
slouken@0
  2504
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  2505
	int srcskip = info->s_skip >> 2;
slouken@0
  2506
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  2507
	int dstskip = info->d_skip >> 1;
slouken@0
  2508
slouken@0
  2509
	while(height--) {
slouken@0
  2510
	    DUFFS_LOOP4({
slouken@0
  2511
		Uint32 s = *srcp;
slouken@0
  2512
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2513
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2514
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2515
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2516
		   Benchmark this! */
slouken@689
  2517
		if(alpha) {   
slouken@689
  2518
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2519
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  2520
		  } else {
slouken@0
  2521
		    Uint32 d = *dstp;
slouken@0
  2522
		    /*
slouken@0
  2523
		     * convert source and destination to G0RAB65565
slouken@0
  2524
		     * and blend all components at the same time
slouken@0
  2525
		     */
slouken@0
  2526
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  2527
		      + (s >> 3 & 0x1f);
slouken@0
  2528
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  2529
		    d += (s - d) * alpha >> 5;
slouken@0
  2530
		    d &= 0x07e0f81f;
slouken@1428
  2531
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2532
		  }
slouken@0
  2533
		}
slouken@0
  2534
		srcp++;
slouken@0
  2535
		dstp++;
slouken@0
  2536
	    }, width);
slouken@0
  2537
	    srcp += srcskip;
slouken@0
  2538
	    dstp += dstskip;
slouken@0
  2539
	}
slouken@0
  2540
}
slouken@0
  2541
slouken@0
  2542
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@0
  2543
static void BlitARGBto555PixelAlpha(SDL_BlitInfo *info)
slouken@0
  2544
{
slouken@0
  2545
	int width = info->d_width;
slouken@0
  2546
	int height = info->d_height;
slouken@0
  2547
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  2548
	int srcskip = info->s_skip >> 2;
slouken@0
  2549
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  2550
	int dstskip = info->d_skip >> 1;
slouken@0
  2551
slouken@0
  2552
	while(height--) {
slouken@0
  2553
	    DUFFS_LOOP4({
slouken@0
  2554
		unsigned alpha;
slouken@0
  2555
		Uint32 s = *srcp;
slouken@0
  2556
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2557
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2558
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2559
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2560
		   Benchmark this! */
slouken@689
  2561
		if(alpha) {   
slouken@689
  2562
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2563
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  2564
		  } else {
slouken@0
  2565
		    Uint32 d = *dstp;
slouken@0
  2566
		    /*
slouken@0
  2567
		     * convert source and destination to G0RAB65565
slouken@0
  2568
		     * and blend all components at the same time
slouken@0
  2569
		     */
slouken@0
  2570
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  2571
		      + (s >> 3 & 0x1f);
slouken@0
  2572
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  2573
		    d += (s - d) * alpha >> 5;
slouken@0
  2574
		    d &= 0x03e07c1f;
slouken@1428
  2575
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2576
		  }
slouken@0
  2577
		}
slouken@0
  2578
		srcp++;
slouken@0
  2579
		dstp++;
slouken@0
  2580
	    }, width);
slouken@0
  2581
	    srcp += srcskip;
slouken@0
  2582
	    dstp += dstskip;
slouken@0
  2583
	}
slouken@0
  2584
}
slouken@0
  2585
slouken@0
  2586
/* General (slow) N->N blending with per-surface alpha */
slouken@0
  2587
static void BlitNtoNSurfaceAlpha(SDL_BlitInfo *info)
slouken@0
  2588
{
slouken@0
  2589
	int width = info->d_width;
slouken@0
  2590
	int height = info->d_height;
slouken@0
  2591
	Uint8 *src = info->s_pixels;
slouken@0
  2592
	int srcskip = info->s_skip;
slouken@0
  2593
	Uint8 *dst = info->d_pixels;
slouken@0
  2594
	int dstskip = info->d_skip;
slouken@0
  2595
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2596
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2597
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2598
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2599
	unsigned sA = srcfmt->alpha;
slouken@0
  2600
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2601
slouken@689
  2602
	if(sA) {
slouken@689
  2603
	  while ( height-- ) {
slouken@0
  2604
	    DUFFS_LOOP4(
slouken@0
  2605
	    {
icculus@1162
  2606
		Uint32 Pixel;
slouken@0
  2607
		unsigned sR;
slouken@0
  2608
		unsigned sG;
slouken@0
  2609
		unsigned sB;
slouken@0
  2610
		unsigned dR;
slouken@0
  2611
		unsigned dG;
slouken@0
  2612
		unsigned dB;
icculus@1162
  2613
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2614
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2615
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2616
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2617
		src += srcbpp;
slouken@0
  2618
		dst += dstbpp;
slouken@0
  2619
	    },
slouken@0
  2620
	    width);
slouken@0
  2621
	    src += srcskip;
slouken@0
  2622
	    dst += dstskip;
slouken@689
  2623
	  }
slouken@0
  2624
	}
slouken@0
  2625
}
slouken@0
  2626
slouken@0
  2627
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@0
  2628
static void BlitNtoNSurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
  2629
{
slouken@0
  2630
	int width = info->d_width;
slouken@0
  2631
	int height = info->d_height;
slouken@0
  2632
	Uint8 *src = info->s_pixels;
slouken@0
  2633
	int srcskip = info->s_skip;
slouken@0
  2634
	Uint8 *dst = info->d_pixels;
slouken@0
  2635
	int dstskip = info->d_skip;
slouken@0
  2636
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2637
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2638
	Uint32 ckey = srcfmt->colorkey;
slouken@0
  2639
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2640
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2641
	unsigned sA = srcfmt->alpha;
slouken@0
  2642
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2643
slouken@0
  2644
	while ( height-- ) {
slouken@0
  2645
	    DUFFS_LOOP4(
slouken@0
  2646
	    {
icculus@1162
  2647
		Uint32 Pixel;
slouken@0
  2648
		unsigned sR;
slouken@0
  2649
		unsigned sG;
slouken@0
  2650
		unsigned sB;
slouken@0
  2651
		unsigned dR;
slouken@0
  2652
		unsigned dG;
slouken@0
  2653
		unsigned dB;
icculus@1162
  2654
		RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
icculus@1162
  2655
		if(sA && Pixel != ckey) {
icculus@1162
  2656
		    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
icculus@1162
  2657
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2658
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2659
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2660
		}
slouken@0
  2661
		src += srcbpp;
slouken@0
  2662
		dst += dstbpp;
slouken@0
  2663
	    },
slouken@0
  2664
	    width);
slouken@0
  2665
	    src += srcskip;
slouken@0
  2666
	    dst += dstskip;
slouken@0
  2667
	}
slouken@0
  2668
}
slouken@0
  2669
slouken@0
  2670
/* General (slow) N->N blending with pixel alpha */
slouken@0
  2671
static void BlitNtoNPixelAlpha(SDL_BlitInfo *info)
slouken@0
  2672
{
slouken@0
  2673
	int width = info->d_width;
slouken@0
  2674
	int height = info->d_height;
slouken@0
  2675
	Uint8 *src = info->s_pixels;
slouken@0
  2676
	int srcskip = info->s_skip;
slouken@0
  2677
	Uint8 *dst = info->d_pixels;
slouken@0
  2678
	int dstskip = info->d_skip;
slouken@0
  2679
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2680
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2681
slouken@0
  2682
	int  srcbpp;
slouken@0
  2683
	int  dstbpp;
slouken@0
  2684
slouken@0
  2685
	/* Set up some basic variables */
slouken@0
  2686
	srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2687
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2688
slouken@0
  2689
	/* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@0
  2690
	   quite right. for <8bpp source alpha, it gets them very wrong
slouken@0
  2691
	   (check all macros!)
slouken@0
  2692
	   It is unclear whether there is a good general solution that doesn't
slouken@0
  2693
	   need a branch (or a divide). */
slouken@0
  2694
	while ( height-- ) {
slouken@0
  2695
	    DUFFS_LOOP4(
slouken@0
  2696
	    {
icculus@1162
  2697
		Uint32 Pixel;
slouken@0
  2698
		unsigned sR;
slouken@0
  2699
		unsigned sG;
slouken@0
  2700
		unsigned sB;
slouken@0
  2701
		unsigned dR;
slouken@0
  2702
		unsigned dG;
slouken@0
  2703
		unsigned dB;
slouken@0
  2704
		unsigned sA;
slouken@0
  2705
		unsigned dA;
icculus@1162
  2706
		DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
slouken@689
  2707
		if(sA) {
icculus@1162
  2708
		  DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
slouken@689
  2709
		  ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@689
  2710
		  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@689
  2711
		}
slouken@0
  2712
		src += srcbpp;
slouken@0
  2713
		dst += dstbpp;
slouken@0
  2714
	    },
slouken@0
  2715
	    width);
slouken@0
  2716
	    src += srcskip;
slouken@0
  2717
	    dst += dstskip;
slouken@0
  2718
	}
slouken@0
  2719
}
slouken@0
  2720
slouken@0
  2721
slouken@0
  2722
SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int blit_index)
slouken@0
  2723
{
slouken@0
  2724
    SDL_PixelFormat *sf = surface->format;
slouken@0
  2725
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
  2726
slouken@0
  2727
    if(sf->Amask == 0) {
slouken@0
  2728
	if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
  2729
	    if(df->BytesPerPixel == 1)
slouken@0
  2730
		return BlitNto1SurfaceAlphaKey;
slouken@0
  2731
	    else
slouken@1361
  2732
#if SDL_ALTIVEC_BLITTERS
icculus@1240
  2733
	if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
icculus@1240
  2734
	    !(surface->map->dst->flags & SDL_HWSURFACE) && SDL_HasAltiVec())
icculus@1047
  2735
            return Blit32to32SurfaceAlphaKeyAltivec;
icculus@1047
  2736
        else
icculus@1047
  2737
#endif
icculus@1047
  2738
            return BlitNtoNSurfaceAlphaKey;
slouken@0
  2739
	} else {
slouken@0
  2740
	    /* Per-surface alpha blits */
slouken@0
  2741
	    switch(df->BytesPerPixel) {
slouken@0
  2742
	    case 1:
slouken@0
  2743
		return BlitNto1SurfaceAlpha;
slouken@0
  2744
slouken@0
  2745
	    case 2:
slouken@0
  2746
		if(surface->map->identity) {
slouken@0
  2747
		    if(df->Gmask == 0x7e0)
slouken@689
  2748
		    {
slouken@1361
  2749
#if MMX_ASMBLIT
slouken@739
  2750
		if(SDL_HasMMX())
slouken@689
  2751
			return Blit565to565SurfaceAlphaMMX;
slouken@689
  2752
		else
slouken@689
  2753
#endif
slouken@0
  2754
			return Blit565to565SurfaceAlpha;
slouken@689
  2755
		    }
slouken@0
  2756
		    else if(df->Gmask == 0x3e0)
slouken@689
  2757
		    {
slouken@1361
  2758
#if MMX_ASMBLIT
slouken@739
  2759
		if(SDL_HasMMX())
slouken@689
  2760
			return Blit555to555SurfaceAlphaMMX;
slouken@689
  2761
		else
slouken@689
  2762
#endif
slouken@0
  2763
			return Blit555to555SurfaceAlpha;
slouken@689
  2764
		    }
slouken@0
  2765
		}
slouken@0
  2766
		return BlitNtoNSurfaceAlpha;
slouken@0
  2767
slouken@0
  2768
	    case 4:
slouken@0
  2769
		if(sf->Rmask == df->Rmask
slouken@0
  2770
		   && sf->Gmask == df->Gmask
slouken@0
  2771
		   && sf->Bmask == df->Bmask
slouken@0
  2772
		   && sf->BytesPerPixel == 4)
slouken@689
  2773
		{
slouken@1361
  2774
#if MMX_ASMBLIT
slouken@1542
  2775
			if(sf->Rshift % 8 == 0
slouken@1542
  2776
			   && sf->Gshift % 8 == 0
slouken@1542
  2777
			   && sf->Bshift % 8 == 0
slouken@1542
  2778
			   && SDL_HasMMX())
slouken@1542
  2779
			    return BlitRGBtoRGBSurfaceAlphaMMX;
slouken@1542
  2780
#endif
slouken@1542
  2781
			if((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff)
slouken@1542
  2782
			{
slouken@1617
  2783
#if SDL_ALTIVEC_BLITTERS
slouken@1617
  2784
				if(!(surface->map->dst->flags & SDL_HWSURFACE)
slouken@1617
  2785
					&& SDL_HasAltiVec())
slouken@1542
  2786
					return BlitRGBtoRGBSurfaceAlphaAltivec;
slouken@1542
  2787
#endif
slouken@1542
  2788
				return BlitRGBtoRGBSurfaceAlpha;
slouken@1542
  2789
			}
slouken@1542
  2790
		}
slouken@1542
  2791
#if SDL_ALTIVEC_BLITTERS
slouken@1542
  2792
		if((sf->BytesPerPixel == 4) &&
slouken@1542
  2793
		   !(surface->map->dst->flags & SDL_HWSURFACE) && SDL_HasAltiVec())
slouken@1542
  2794
			return Blit32to32SurfaceAlphaAltivec;
slouken@689
  2795
		else
slouken@689
  2796
#endif
slouken@1542
  2797
			return BlitNtoNSurfaceAlpha;
slouken@0
  2798
slouken@0
  2799
	    case 3:
slouken@0
  2800
	    default:
slouken@0
  2801
		return BlitNtoNSurfaceAlpha;
slouken@0
  2802
	    }
slouken@0
  2803
	}
slouken@0
  2804
    } else {
slouken@0
  2805
	/* Per-pixel alpha blits */
slouken@0
  2806
	switch(df->BytesPerPixel) {
slouken@0
  2807
	case 1:
slouken@0
  2808
	    return BlitNto1PixelAlpha;
slouken@0
  2809
slouken@0
  2810
	case 2:
slouken@1361
  2811
#if SDL_ALTIVEC_BLITTERS
icculus@1240
  2812
	if(sf->BytesPerPixel == 4 && !(surface->map->dst->flags & SDL_HWSURFACE) &&
icculus@1047
  2813
           df->Gmask == 0x7e0 &&
icculus@1240
  2814
	   df->Bmask == 0x1f && SDL_HasAltiVec())
icculus@1047
  2815
            return Blit32to565PixelAlphaAltivec;
icculus@1047
  2816
        else
icculus@1047
  2817
#endif
slouken@0
  2818
	    if(sf->BytesPerPixel == 4 && sf->Amask == 0xff000000
slouken@0
  2819
	       && sf->Gmask == 0xff00
slouken@0
  2820
	       && ((sf->Rmask == 0xff && df->Rmask == 0x1f)
slouken@0
  2821
		   || (sf->Bmask == 0xff && df->Bmask == 0x1f))) {
slouken@0
  2822
		if(df->Gmask == 0x7e0)
slouken@0
  2823
		    return BlitARGBto565PixelAlpha;
slouken@0
  2824
		else if(df->Gmask == 0x3e0)
slouken@0
  2825
		    return BlitARGBto555PixelAlpha;
slouken@0
  2826
	    }
slouken@0
  2827
	    return BlitNtoNPixelAlpha;
slouken@0
  2828
slouken@0
  2829
	case 4:
slouken@1542
  2830
	    if(sf->Rmask == df->Rmask
slouken@0
  2831
	       && sf->Gmask == df->Gmask
slouken@0
  2832
	       && sf->Bmask == df->Bmask
slouken@0
  2833
	       && sf->BytesPerPixel == 4)
slouken@689
  2834
	    {
slouken@1361
  2835
#if MMX_ASMBLIT
slouken@1542
  2836
		if(sf->Rshift % 8 == 0
slouken@1542
  2837
		   && sf->Gshift % 8 == 0
slouken@1542
  2838
		   && sf->Bshift % 8 == 0
slouken@1542
  2839
		   && sf->Ashift % 8 == 0
slouken@1542
  2840
		   && sf->Aloss == 0)
slouken@1542
  2841
		{
slouken@1542
  2842
			if(SDL_Has3DNow())
slouken@1542
  2843
				return BlitRGBtoRGBPixelAlphaMMX3DNOW;
slouken@1542
  2844
			if(SDL_HasMMX())
slouken@1542
  2845
				return BlitRGBtoRGBPixelAlphaMMX;
slouken@1542
  2846
		}
slouken@689
  2847
#endif
slouken@1542
  2848
		if(sf->Amask == 0xff000000)
slouken@1542
  2849
		{
slouken@1617
  2850
#if SDL_ALTIVEC_BLITTERS
slouken@1617
  2851
			if(!(surface->map->dst->flags & SDL_HWSURFACE)
slouken@1617
  2852
				&& SDL_HasAltiVec())
slouken@1542
  2853
				return BlitRGBtoRGBPixelAlphaAltivec;
icculus@1047
  2854
#endif
slouken@1542
  2855
			return BlitRGBtoRGBPixelAlpha;
slouken@1542
  2856
		}
slouken@689
  2857
	    }
slouken@1361