src/video/SDL_blit_A.c
author Ozkan Sezer <sezeroz@gmail.com>
Sun, 14 Oct 2018 15:25:04 +0300
branchSDL-1.2
changeset 12325 c4f2eeda176f
parent 6137 4720145f848b
permissions -rw-r--r--
remove symlink for libSDL-1.0.so.0 from the rpm spec file.

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