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