src/video/SDL_blit_A.c
author Ben Avison <bavison@riscosopen.org>
Thu, 31 Oct 2019 14:00:28 +0300
branchSDL-1.2
changeset 13221 3705e81df6ff
parent 13220 0ae1ddca5e85
permissions -rw-r--r--
ARM: NEON assembly optimization for function BlitARGBto565PixelAlpha
---
src/video/SDL_blit_A.c | 31 ++++++++--
src/video/arm/pixman-arm-neon-asm.S | 88 +++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 5 deletions(-)
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
bavison@13213
  1436
#if SDL_ARM_SIMD_BLITTERS
bavison@13214
  1437
void BlitARGBto565PixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
bavison@13214
  1438
bavison@13214
  1439
static void BlitARGBto565PixelAlphaARMSIMD(SDL_BlitInfo *info)
bavison@13214
  1440
{
bavison@13214
  1441
	int32_t width = info->d_width;
bavison@13214
  1442
	int32_t height = info->d_height;
bavison@13214
  1443
	uint16_t *dstp = (uint16_t *)info->d_pixels;
bavison@13214
  1444
	int32_t dststride = width + (info->d_skip >> 1);
bavison@13214
  1445
	uint32_t *srcp = (uint32_t *)info->s_pixels;
bavison@13214
  1446
	int32_t srcstride = width + (info->s_skip >> 2);
bavison@13214
  1447
bavison@13214
  1448
	BlitARGBto565PixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
bavison@13214
  1449
}
bavison@13214
  1450
bavison@13213
  1451
void BlitRGBtoRGBPixelAlphaARMSIMDAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
bavison@13213
  1452
bavison@13213
  1453
static void BlitRGBtoRGBPixelAlphaARMSIMD(SDL_BlitInfo *info)
bavison@13213
  1454
{
bavison@13213
  1455
	int32_t width = info->d_width;
bavison@13213
  1456
	int32_t height = info->d_height;
bavison@13213
  1457
	uint32_t *dstp = (uint32_t *)info->d_pixels;
bavison@13213
  1458
	int32_t dststride = width + (info->d_skip >> 2);
bavison@13213
  1459
	uint32_t *srcp = (uint32_t *)info->s_pixels;
bavison@13213
  1460
	int32_t srcstride = width + (info->s_skip >> 2);
bavison@13213
  1461
bavison@13213
  1462
	BlitRGBtoRGBPixelAlphaARMSIMDAsm(width, height, dstp, dststride, srcp, srcstride);
bavison@13213
  1463
}
bavison@13213
  1464
#endif
bavison@13213
  1465
bavison@13220
  1466
#if SDL_ARM_NEON_BLITTERS
bavison@13221
  1467
void BlitARGBto565PixelAlphaARMNEONAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
bavison@13221
  1468
bavison@13221
  1469
static void BlitARGBto565PixelAlphaARMNEON(SDL_BlitInfo *info)
bavison@13221
  1470
{
bavison@13221
  1471
    int32_t width = info->d_width;
bavison@13221
  1472
    int32_t height = info->d_height;
bavison@13221
  1473
    uint16_t *dstp = (uint16_t *)info->d_pixels;
bavison@13221
  1474
    int32_t dststride = width + (info->d_skip >> 1);
bavison@13221
  1475
    uint32_t *srcp = (uint32_t *)info->s_pixels;
bavison@13221
  1476
    int32_t srcstride = width + (info->s_skip >> 2);
bavison@13221
  1477
bavison@13221
  1478
    BlitARGBto565PixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
bavison@13221
  1479
}
bavison@13221
  1480
bavison@13220
  1481
void BlitRGBtoRGBPixelAlphaARMNEONAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t *src, int32_t src_stride);
bavison@13220
  1482
bavison@13220
  1483
static void BlitRGBtoRGBPixelAlphaARMNEON(SDL_BlitInfo *info)
bavison@13220
  1484
{
bavison@13220
  1485
	int32_t width = info->d_width;
bavison@13220
  1486
	int32_t height = info->d_height;
bavison@13220
  1487
	uint32_t *dstp = (uint32_t *)info->d_pixels;
bavison@13220
  1488
	int32_t dststride = width + (info->d_skip >> 2);
bavison@13220
  1489
	uint32_t *srcp = (uint32_t *)info->s_pixels;
bavison@13220
  1490
	int32_t srcstride = width + (info->s_skip >> 2);
bavison@13220
  1491
bavison@13220
  1492
	BlitRGBtoRGBPixelAlphaARMNEONAsm(width, height, dstp, dststride, srcp, srcstride);
bavison@13220
  1493
}
bavison@13220
  1494
#endif
bavison@13220
  1495
slouken@1
  1496
/* fast RGB888->(A)RGB888 blending with surface alpha=128 special case */
slouken@1
  1497
static void BlitRGBtoRGBSurfaceAlpha128(SDL_BlitInfo *info)
slouken@0
  1498
{
slouken@0
  1499
	int width = info->d_width;
slouken@0
  1500
	int height = info->d_height;
slouken@0
  1501
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  1502
	int srcskip = info->s_skip >> 2;
slouken@0
  1503
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
  1504
	int dstskip = info->d_skip >> 2;
slouken@0
  1505
slouken@0
  1506
	while(height--) {
slouken@0
  1507
	    DUFFS_LOOP4({
slouken@1
  1508
		    Uint32 s = *srcp++;
slouken@1
  1509
		    Uint32 d = *dstp;
slouken@1
  1510
		    *dstp++ = ((((s & 0x00fefefe) + (d & 0x00fefefe)) >> 1)
slouken@1
  1511
			       + (s & d & 0x00010101)) | 0xff000000;
slouken@0
  1512
	    }, width);
slouken@0
  1513
	    srcp += srcskip;
slouken@0
  1514
	    dstp += dstskip;
slouken@0
  1515
	}
slouken@0
  1516
}
slouken@0
  1517
slouken@1
  1518
/* fast RGB888->(A)RGB888 blending with surface alpha */
slouken@1
  1519
static void BlitRGBtoRGBSurfaceAlpha(SDL_BlitInfo *info)
slouken@1
  1520
{
slouken@1
  1521
	unsigned alpha = info->src->alpha;
slouken@1
  1522
	if(alpha == 128) {
slouken@1
  1523
		BlitRGBtoRGBSurfaceAlpha128(info);
slouken@1
  1524
	} else {
slouken@1
  1525
		int width = info->d_width;
slouken@1
  1526
		int height = info->d_height;
slouken@1
  1527
		Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1
  1528
		int srcskip = info->s_skip >> 2;
slouken@1
  1529
		Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1
  1530
		int dstskip = info->d_skip >> 2;
slouken@689
  1531
		Uint32 s;
slouken@689
  1532
		Uint32 d;
slouken@689
  1533
		Uint32 s1;
slouken@689
  1534
		Uint32 d1;
slouken@1
  1535
slouken@1
  1536
		while(height--) {
slouken@689
  1537
			DUFFS_LOOP_DOUBLE2({
slouken@689
  1538
				/* One Pixel Blend */
slouken@1
  1539
				s = *srcp;
slouken@1
  1540
				d = *dstp;
slouken@1
  1541
				s1 = s & 0xff00ff;
slouken@1
  1542
				d1 = d & 0xff00ff;
slouken@1
  1543
				d1 = (d1 + ((s1 - d1) * alpha >> 8))
slouken@1
  1544
				     & 0xff00ff;
slouken@1
  1545
				s &= 0xff00;
slouken@1
  1546
				d &= 0xff00;
slouken@1
  1547
				d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@1
  1548
				*dstp = d1 | d | 0xff000000;
slouken@1
  1549
				++srcp;
slouken@1
  1550
				++dstp;
slouken@689
  1551
			},{
slouken@689
  1552
			        /* Two Pixels Blend */
slouken@689
  1553
				s = *srcp;
slouken@689
  1554
				d = *dstp;
slouken@689
  1555
				s1 = s & 0xff00ff;
slouken@689
  1556
				d1 = d & 0xff00ff;
slouken@689
  1557
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1558
				d1 &= 0xff00ff;
slouken@689
  1559
				     
slouken@689
  1560
				s = ((s & 0xff00) >> 8) | 
slouken@689
  1561
					((srcp[1] & 0xff00) << 8);
slouken@689
  1562
				d = ((d & 0xff00) >> 8) |
slouken@689
  1563
					((dstp[1] & 0xff00) << 8);
slouken@689
  1564
				d += (s - d) * alpha >> 8;
slouken@689
  1565
				d &= 0x00ff00ff;
slouken@689
  1566
				
slouken@689
  1567
				*dstp++ = d1 | ((d << 8) & 0xff00) | 0xff000000;
slouken@689
  1568
				++srcp;
slouken@689
  1569
				
slouken@689
  1570
			        s1 = *srcp;
slouken@689
  1571
				d1 = *dstp;
slouken@689
  1572
				s1 &= 0xff00ff;
slouken@689
  1573
				d1 &= 0xff00ff;
slouken@689
  1574
				d1 += (s1 - d1) * alpha >> 8;
slouken@689
  1575
				d1 &= 0xff00ff;
slouken@689
  1576
				
slouken@689
  1577
				*dstp = d1 | ((d >> 8) & 0xff00) | 0xff000000;
slouken@689
  1578
				++srcp;
slouken@689
  1579
				++dstp;
slouken@1
  1580
			}, width);
slouken@1
  1581
			srcp += srcskip;
slouken@1
  1582
			dstp += dstskip;
slouken@1
  1583
		}
slouken@1
  1584
	}
slouken@1
  1585
}
slouken@1
  1586
slouken@0
  1587
/* fast ARGB888->(A)RGB888 blending with pixel alpha */
slouken@0
  1588
static void BlitRGBtoRGBPixelAlpha(SDL_BlitInfo *info)
slouken@0
  1589
{
slouken@0
  1590
	int width = info->d_width;
slouken@0
  1591
	int height = info->d_height;
slouken@0
  1592
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  1593
	int srcskip = info->s_skip >> 2;
slouken@0
  1594
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@0
  1595
	int dstskip = info->d_skip >> 2;
slouken@0
  1596
slouken@0
  1597
	while(height--) {
slouken@0
  1598
	    DUFFS_LOOP4({
slouken@0
  1599
		Uint32 dalpha;
slouken@0
  1600
		Uint32 d;
slouken@0
  1601
		Uint32 s1;
slouken@0
  1602
		Uint32 d1;
slouken@0
  1603
		Uint32 s = *srcp;
slouken@0
  1604
		Uint32 alpha = s >> 24;
slouken@0
  1605
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  1606
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  1607
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  1608
		   Benchmark this! */
slouken@689
  1609
		if(alpha) {   
slouken@689
  1610
		  if(alpha == SDL_ALPHA_OPAQUE) {
slouken@0
  1611
		    *dstp = (s & 0x00ffffff) | (*dstp & 0xff000000);
slouken@689
  1612
		  } else {
slouken@0
  1613
		    /*
slouken@0
  1614
		     * take out the middle component (green), and process
slouken@0
  1615
		     * the other two in parallel. One multiply less.
slouken@0
  1616
		     */
slouken@0
  1617
		    d = *dstp;
slouken@0
  1618
		    dalpha = d & 0xff000000;
slouken@0
  1619
		    s1 = s & 0xff00ff;
slouken@0
  1620
		    d1 = d & 0xff00ff;
slouken@0
  1621
		    d1 = (d1 + ((s1 - d1) * alpha >> 8)) & 0xff00ff;
slouken@0
  1622
		    s &= 0xff00;
slouken@0
  1623
		    d &= 0xff00;
slouken@0
  1624
		    d = (d + ((s - d) * alpha >> 8)) & 0xff00;
slouken@0
  1625
		    *dstp = d1 | d | dalpha;
slouken@689
  1626
		  }
slouken@0
  1627
		}
slouken@0
  1628
		++srcp;
slouken@0
  1629
		++dstp;
slouken@0
  1630
	    }, width);
slouken@0
  1631
	    srcp += srcskip;
slouken@0
  1632
	    dstp += dstskip;
slouken@0
  1633
	}
slouken@0
  1634
}
slouken@0
  1635
slouken@1542
  1636
#if GCC_ASMBLIT
slouken@689
  1637
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@3870
  1638
static void BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo *info)
slouken@689
  1639
{
slouken@689
  1640
	int width = info->d_width;
slouken@689
  1641
	int height = info->d_height;
slouken@689
  1642
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@689
  1643
	int srcskip = info->s_skip >> 2;
slouken@689
  1644
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@689
  1645
	int dstskip = info->d_skip >> 2;
slouken@1542
  1646
	SDL_PixelFormat* sf = info->src;
slouken@1542
  1647
	Uint32 amask = sf->Amask;
slouken@689
  1648
slouken@689
  1649
	__asm__ (
slouken@689
  1650
	/* make mm6 all zeros. */
slouken@689
  1651
	"pxor       %%mm6, %%mm6\n"
slouken@689
  1652
	
slouken@689
  1653
	/* Make a mask to preserve the alpha. */
slouken@1542
  1654
	"movd      %0, %%mm7\n\t"           /* 0000F000 -> mm7 */
slouken@1542
  1655
	"punpcklbw %%mm7, %%mm7\n\t"        /* FF000000 -> mm7 */
slouken@1542
  1656
	"pcmpeqb   %%mm4, %%mm4\n\t"        /* FFFFFFFF -> mm4 */
slouken@1542
  1657
	"movq      %%mm4, %%mm3\n\t"        /* FFFFFFFF -> mm3 (for later) */
slouken@1542
  1658
	"pxor      %%mm4, %%mm7\n\t"        /* 00FFFFFF -> mm7 (mult mask) */
slouken@689
  1659
slouken@1542
  1660
	/* form channel masks */
slouken@1542
  1661
	"movq      %%mm7, %%mm4\n\t"        /* 00FFFFFF -> mm4 */
slouken@1542
  1662
	"packsswb  %%mm6, %%mm4\n\t"        /* 00000FFF -> mm4 (channel mask) */
slouken@1542
  1663
	"packsswb  %%mm6, %%mm3\n\t"        /* 0000FFFF -> mm3 */
slouken@1542
  1664
	"pxor      %%mm4, %%mm3\n\t"        /* 0000F000 -> mm3 (~channel mask) */
slouken@1542
  1665
	
slouken@1542
  1666
	/* get alpha channel shift */
slouken@1542
  1667
	"movd      %1, %%mm5\n\t" /* Ashift -> mm5 */
slouken@1542
  1668
icculus@3931
  1669
	  : /* nothing */ : "rm" (amask), "rm" ((Uint32) sf->Ashift) );
slouken@689
  1670
slouken@689
  1671
	while(height--) {
slouken@689
  1672
slouken@689
  1673
	    DUFFS_LOOP4({
slouken@1542
  1674
		Uint32 alpha;
slouken@689
  1675
slouken@689
  1676
		__asm__ (
slouken@689
  1677
		"prefetch 64(%0)\n"
slouken@689
  1678
		"prefetch 64(%1)\n"
slouken@689
  1679
			: : "r" (srcp), "r" (dstp) );
slouken@689
  1680
slouken@1542
  1681
		alpha = *srcp & amask;
slouken@689
  1682
		/* FIXME: Here we special-case opaque alpha since the
slouken@689
  1683
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@689
  1684
		   it correctly. Also special-case alpha=0 for speed?
slouken@689
  1685
		   Benchmark this! */
slouken@1542
  1686
		if(alpha == 0) {
slouken@1542
  1687
		    /* do nothing */
slouken@1542
  1688
		}
slouken@1542
  1689
		else if(alpha == amask) {
slouken@1542
  1690
			/* opaque alpha -- copy RGB, keep dst alpha */
slouken@1542
  1691
		    /* using MMX here to free up regular registers for other things */
slouken@1542
  1692
			    __asm__ (
slouken@1542
  1693
		    "movd      (%0),  %%mm0\n\t" /* src(ARGB) -> mm0 (0000ARGB)*/
slouken@1542
  1694
		    "movd      (%1),  %%mm1\n\t" /* dst(ARGB) -> mm1 (0000ARGB)*/
slouken@1542
  1695
		    "pand      %%mm4, %%mm0\n\t" /* src & chanmask -> mm0 */
slouken@1542
  1696
		    "pand      %%mm3, %%mm1\n\t" /* dst & ~chanmask -> mm2 */
slouken@1542
  1697
		    "por       %%mm0, %%mm1\n\t" /* src | dst -> mm1 */
slouken@1542
  1698
		    "movd      %%mm1, (%1) \n\t" /* mm1 -> dst */
slouken@1542
  1699
slouken@1542
  1700
		     : : "r" (srcp), "r" (dstp) );
slouken@689
  1701
		} 
slouken@689
  1702
slouken@689
  1703
		else {
slouken@689
  1704
			    __asm__ (
slouken@689
  1705
		    /* load in the source, and dst. */
slouken@689
  1706
		    "movd      (%0), %%mm0\n"		    /* mm0(s) = 0 0 0 0 | As Rs Gs Bs */
slouken@689
  1707
		    "movd      (%1), %%mm1\n"		    /* mm1(d) = 0 0 0 0 | Ad Rd Gd Bd */
slouken@689
  1708
slouken@689
  1709
		    /* Move the src alpha into mm2 */
slouken@689
  1710
slouken@689
  1711
		    /* if supporting pshufw */
slouken@689
  1712
		    /*"pshufw     $0x55, %%mm0, %%mm2\n" */ /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@689
  1713
		    /*"psrlw     $8, %%mm2\n" */
slouken@689
  1714
		    
slouken@689
  1715
		    /* else: */
slouken@1542
  1716
		    "movd       %2,    %%mm2\n"
slouken@1542
  1717
		    "psrld      %%mm5, %%mm2\n"                /* mm2 = 0 0 0 0 | 0  0  0  As */
slouken@689
  1718
		    "punpcklwd	%%mm2, %%mm2\n"	            /* mm2 = 0 0 0 0 |  0 As  0  As */
slouken@689
  1719
		    "punpckldq	%%mm2, %%mm2\n"             /* mm2 = 0 As 0 As |  0 As  0  As */
slouken@1542
  1720
		    "pand       %%mm7, %%mm2\n"              /* to preserve dest alpha */
slouken@689
  1721
slouken@689
  1722
		    /* move the colors into words. */
slouken@689
  1723
		    "punpcklbw %%mm6, %%mm0\n"		    /* mm0 = 0 As 0 Rs | 0 Gs 0 Bs */
slouken@689
  1724
		    "punpcklbw %%mm6, %%mm1\n"              /* mm0 = 0 Ad 0 Rd | 0 Gd 0 Bd */
slouken@689
  1725
slouken@689
  1726
		    /* src - dst */
slouken@689
  1727
		    "psubw    %%mm1, %%mm0\n"		    /* mm0 = As-Ad Rs-Rd | Gs-Gd  Bs-Bd */
slouken@689
  1728
slouken@689
  1729
		    /* A * (src-dst) */
slouken@1542
  1730
		    "pmullw    %%mm2, %%mm0\n"		    /* mm0 = 0*As-d As*Rs-d | As*Gs-d  As*Bs-d */
slouken@1542
  1731
		    "psrlw     $8,    %%mm0\n"		    /* mm0 = 0>>8 Rc>>8 | Gc>>8  Bc>>8 */
slouken@1542
  1732
		    "paddb     %%mm1, %%mm0\n"		    /* mm0 = 0+Ad Rc+Rd | Gc+Gd  Bc+Bd */
slouken@689
  1733
slouken@689
  1734
		    "packuswb  %%mm0, %%mm0\n"              /* mm0 =             | Ac Rc Gc Bc */
slouken@689
  1735
		    
slouken@689
  1736
		    "movd      %%mm0, (%1)\n"               /* result in mm0 */
slouken@689
  1737
slouken@1542
  1738
		     : : "r" (srcp), "r" (dstp), "r" (alpha) );
slouken@689
  1739
slouken@689
  1740
		}
slouken@689
  1741
		++srcp;
slouken@689
  1742
		++dstp;
slouken@689
  1743
	    }, width);
slouken@689
  1744
	    srcp += srcskip;
slouken@689
  1745
	    dstp += dstskip;
slouken@689
  1746
	}
slouken@689
  1747
slouken@689
  1748
	__asm__ (
slouken@689
  1749
	"emms\n"
slouken@689
  1750
		:   );
slouken@689
  1751
}
slouken@1542
  1752
/* End GCC_ASMBLIT*/
slouken@1542
  1753
slouken@1542
  1754
#elif MSVC_ASMBLIT
slouken@1542
  1755
/* fast (as in MMX with prefetch) ARGB888->(A)RGB888 blending with pixel alpha */
slouken@1542
  1756
static void BlitRGBtoRGBPixelAlphaMMX3DNOW(SDL_BlitInfo *info)
slouken@1542
  1757
{
slouken@1542
  1758
	int width = info->d_width;
slouken@1542
  1759
	int height = info->d_height;
slouken@1542
  1760
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@1542
  1761
	int srcskip = info->s_skip >> 2;
slouken@1542
  1762
	Uint32 *dstp = (Uint32 *)info->d_pixels;
slouken@1542
  1763
	int dstskip = info->d_skip >> 2;
slouken@1542
  1764
	SDL_PixelFormat* sf = info->src;
slouken@1542
  1765
	Uint32 chanmask = sf->Rmask | sf->Gmask | sf->Bmask;
slouken@1542
  1766
	Uint32 amask = sf->Amask;
slouken@1542
  1767
	Uint32 ashift = sf->Ashift;
slouken@1542
  1768
	Uint64 multmask;
slouken@1542
  1769
	
slouken@1542
  1770
	__m64 src1, dst1, mm_alpha, mm_zero, dmask;
slouken@1542
  1771
slouken@1542
  1772
	mm_zero = _mm_setzero_si64(); /* 0 -> mm_zero */
slouken@1542
  1773
	multmask = ~(0xFFFFi64 << (ashift * 2));
slouken@1542
  1774
	dmask = *(__m64*) &multmask; /* dst alpha mask -> dmask */
slouken@1542
  1775
slouken@1542
  1776
	while(height--) {
slouken@1542
  1777
	    DUFFS_LOOP4({
slouken@1542
  1778
		Uint32 alpha;
slouken@1542
  1779
slouken@1542
  1780
		_m_prefetch(srcp + 16);
slouken@1542
  1781
		_m_prefetch(dstp + 16);
slouken@1542
  1782
slouken@1542
  1783
		alpha = *srcp & amask;
slouken@1542
  1784
		if (alpha == 0) {
slouken@1542
  1785
			/* do nothing */
slouken@1542
  1786
		} else if (alpha == amask) {
slouken@1542
  1787
			/* copy RGB, keep dst alpha */
slouken@1542
  1788
			*dstp = (*srcp & chanmask) | (*dstp & ~chanmask);
slouken@1542
  1789
		} else {
slouken@1542
  1790
			src1 = _mm_cvtsi32_si64(*srcp); /* src(ARGB) -> src1 (0000ARGB)*/
slouken@1542
  1791
			src1 = _mm_unpacklo_pi8(src1, mm_zero); /* 0A0R0G0B -> src1 */
slouken@1542
  1792
slouken@1542
  1793
			dst1 = _mm_cvtsi32_si64(*dstp); /* dst(ARGB) -> dst1 (0000ARGB)*/
slouken@1542
  1794
			dst1 = _mm_unpacklo_pi8(dst1, mm_zero); /* 0A0R0G0B -> dst1 */
slouken@1542
  1795
slouken@1542
  1796
			mm_alpha = _mm_cvtsi32_si64(alpha); /* alpha -> mm_alpha (0000000A) */
slouken@1542
  1797
			mm_alpha = _mm_srli_si64(mm_alpha, ashift); /* mm_alpha >> ashift -> mm_alpha(0000000A) */
slouken@1542
  1798
			mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  1799
			mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  1800
			mm_alpha = _mm_and_si64(mm_alpha, dmask); /* 000A0A0A -> mm_alpha, preserve dst alpha on add */
slouken@1542
  1801
slouken@1542
  1802
			/* blend */		    
slouken@1542
  1803
			src1 = _mm_sub_pi16(src1, dst1);/* src - dst -> src1 */
slouken@1542
  1804
			src1 = _mm_mullo_pi16(src1, mm_alpha); /* (src - dst) * alpha -> src1 */
slouken@1542
  1805
			src1 = _mm_srli_pi16(src1, 8); /* src1 >> 8 -> src1(000R0G0B) */
slouken@1542
  1806
			dst1 = _mm_add_pi8(src1, dst1); /* src1 + dst1(dst) -> dst1(0A0R0G0B) */
slouken@1542
  1807
			dst1 = _mm_packs_pu16(dst1, mm_zero);  /* 0000ARGB -> dst1 */
slouken@1542
  1808
			
slouken@1542
  1809
			*dstp = _mm_cvtsi64_si32(dst1); /* dst1 -> pixel */
slouken@1542
  1810
		}
slouken@1542
  1811
		++srcp;
slouken@1542
  1812
		++dstp;
slouken@1542
  1813
	    }, width);
slouken@1542
  1814
	    srcp += srcskip;
slouken@1542
  1815
	    dstp += dstskip;
slouken@1542
  1816
	}
slouken@1542
  1817
	_mm_empty();
slouken@1542
  1818
}
slouken@1542
  1819
/* End MSVC_ASMBLIT */
slouken@1542
  1820
slouken@1542
  1821
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  1822
slouken@1
  1823
/* 16bpp special case for per-surface alpha=50%: blend 2 pixels in parallel */
slouken@1
  1824
slouken@1
  1825
/* blend a single 16 bit pixel at 50% */
slouken@1
  1826
#define BLEND16_50(d, s, mask)						\
slouken@1
  1827
	((((s & mask) + (d & mask)) >> 1) + (s & d & (~mask & 0xffff)))
slouken@1
  1828
slouken@1
  1829
/* blend two 16 bit pixels at 50% */
slouken@1
  1830
#define BLEND2x16_50(d, s, mask)					     \
slouken@1
  1831
	(((s & (mask | mask << 16)) >> 1) + ((d & (mask | mask << 16)) >> 1) \
slouken@1
  1832
	 + (s & d & (~(mask | mask << 16))))
slouken@1
  1833
slouken@1
  1834
static void Blit16to16SurfaceAlpha128(SDL_BlitInfo *info, Uint16 mask)
slouken@0
  1835
{
slouken@0
  1836
	int width = info->d_width;
slouken@0
  1837
	int height = info->d_height;
slouken@0
  1838
	Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@0
  1839
	int srcskip = info->s_skip >> 1;
slouken@0
  1840
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  1841
	int dstskip = info->d_skip >> 1;
slouken@0
  1842
slouken@0
  1843
	while(height--) {
slouken@1456
  1844
		if(((uintptr_t)srcp ^ (uintptr_t)dstp) & 2) {
slouken@1
  1845
			/*
slouken@1
  1846
			 * Source and destination not aligned, pipeline it.
slouken@1
  1847
			 * This is mostly a win for big blits but no loss for
slouken@1
  1848
			 * small ones
slouken@1
  1849
			 */
slouken@1
  1850
			Uint32 prev_sw;
slouken@1
  1851
			int w = width;
slouken@1
  1852
slouken@1
  1853
			/* handle odd destination */
slouken@1456
  1854
			if((uintptr_t)dstp & 2) {
slouken@1
  1855
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1856
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1857
				dstp++;
slouken@1
  1858
				srcp++;
slouken@1
  1859
				w--;
slouken@1
  1860
			}
slouken@1
  1861
			srcp++;	/* srcp is now 32-bit aligned */
slouken@1
  1862
slouken@1
  1863
			/* bootstrap pipeline with first halfword */
slouken@1
  1864
			prev_sw = ((Uint32 *)srcp)[-1];
slouken@1
  1865
slouken@1
  1866
			while(w > 1) {
slouken@1
  1867
				Uint32 sw, dw, s;
slouken@1
  1868
				sw = *(Uint32 *)srcp;
slouken@1
  1869
				dw = *(Uint32 *)dstp;
slouken@1443
  1870
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1443
  1871
				s = (prev_sw << 16) + (sw >> 16);
slouken@1443
  1872
#else
slouken@1443
  1873
				s = (prev_sw >> 16) + (sw << 16);
slouken@1443
  1874
#endif
slouken@1
  1875
				prev_sw = sw;
slouken@1
  1876
				*(Uint32 *)dstp = BLEND2x16_50(dw, s, mask);
slouken@1
  1877
				dstp += 2;
slouken@1
  1878
				srcp += 2;
slouken@1
  1879
				w -= 2;
slouken@1
  1880
			}
slouken@1
  1881
slouken@1
  1882
			/* final pixel if any */
slouken@1
  1883
			if(w) {
slouken@1
  1884
				Uint16 d = *dstp, s;
slouken@1443
  1885
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1443
  1886
				s = (Uint16)prev_sw;
slouken@1443
  1887
#else
slouken@1443
  1888
				s = (Uint16)(prev_sw >> 16);
slouken@1443
  1889
#endif
slouken@1
  1890
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1891
				srcp++;
slouken@1
  1892
				dstp++;
slouken@1
  1893
			}
slouken@1
  1894
			srcp += srcskip - 1;
slouken@1
  1895
			dstp += dstskip;
slouken@1
  1896
		} else {
slouken@1
  1897
			/* source and destination are aligned */
slouken@1
  1898
			int w = width;
slouken@1
  1899
slouken@1
  1900
			/* first odd pixel? */
slouken@1456
  1901
			if((uintptr_t)srcp & 2) {
slouken@1
  1902
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1903
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1904
				srcp++;
slouken@1
  1905
				dstp++;
slouken@1
  1906
				w--;
slouken@1
  1907
			}
slouken@1
  1908
			/* srcp and dstp are now 32-bit aligned */
slouken@1
  1909
slouken@1
  1910
			while(w > 1) {
slouken@1
  1911
				Uint32 sw = *(Uint32 *)srcp;
slouken@1
  1912
				Uint32 dw = *(Uint32 *)dstp;
slouken@1
  1913
				*(Uint32 *)dstp = BLEND2x16_50(dw, sw, mask);
slouken@1
  1914
				srcp += 2;
slouken@1
  1915
				dstp += 2;
slouken@1
  1916
				w -= 2;
slouken@1
  1917
			}
slouken@1
  1918
slouken@1
  1919
			/* last odd pixel? */
slouken@1
  1920
			if(w) {
slouken@1
  1921
				Uint16 d = *dstp, s = *srcp;
slouken@1
  1922
				*dstp = BLEND16_50(d, s, mask);
slouken@1
  1923
				srcp++;
slouken@1
  1924
				dstp++;
slouken@1
  1925
			}
slouken@1
  1926
			srcp += srcskip;
slouken@1
  1927
			dstp += dstskip;
slouken@1
  1928
		}
slouken@1
  1929
	}
slouken@1
  1930
}
slouken@1
  1931
slouken@1542
  1932
#if GCC_ASMBLIT
slouken@689
  1933
/* fast RGB565->RGB565 blending with surface alpha */
slouken@689
  1934
static void Blit565to565SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@689
  1935
{
slouken@689
  1936
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@689
  1937
	if(alpha == 128) {
slouken@689
  1938
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@689
  1939
	} else {
slouken@689
  1940
		int width = info->d_width;
slouken@689
  1941
		int height = info->d_height;
slouken@689
  1942
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@689
  1943
		int srcskip = info->s_skip >> 1;
slouken@689
  1944
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@689
  1945
		int dstskip = info->d_skip >> 1;
slouken@1542
  1946
		Uint32 s, d;
icculus@4262
  1947
		Uint64 load;
slouken@689
  1948
	  
slouken@689
  1949
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
icculus@4262
  1950
		load = alpha;
slouken@689
  1951
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@689
  1952
icculus@4262
  1953
		movq_m2r(load, mm0); /* alpha(0000000A) -> mm0 */
slouken@1542
  1954
		punpcklwd_r2r(mm0, mm0); /* 00000A0A -> mm0 */
slouken@1542
  1955
		punpcklwd_r2r(mm0, mm0); /* 0A0A0A0A -> mm0 */
slouken@1542
  1956
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  1957
		   to reduce the number of operations */
slouken@1542
  1958
		psllq_i2r(3, mm0);
slouken@689
  1959
	  
slouken@1542
  1960
		/* Setup the 565 color channel masks */
icculus@4262
  1961
		load = 0x07E007E007E007E0ULL;
icculus@4262
  1962
		movq_m2r(load, mm4); /* MASKGREEN -> mm4 */
icculus@4262
  1963
		load = 0x001F001F001F001FULL;
icculus@4262
  1964
		movq_m2r(load, mm7); /* MASKBLUE -> mm7 */
slouken@689
  1965
		while(height--) {
slouken@1542
  1966
			DUFFS_LOOP_QUATRO2(
slouken@1542
  1967
			{
slouken@1542
  1968
				s = *srcp++;
slouken@689
  1969
				d = *dstp;
slouken@689
  1970
				/*
slouken@689
  1971
				 * shift out the middle component (green) to
slouken@689
  1972
				 * the high 16 bits, and process all three RGB
slouken@689
  1973
				 * components at the same time.
slouken@689
  1974
				 */
slouken@689
  1975
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1976
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1977
				d += (s - d) * alpha >> 5;
slouken@689
  1978
				d &= 0x07e0f81f;
slouken@689
  1979
				*dstp++ = d | d >> 16;
slouken@1542
  1980
			},{
slouken@1542
  1981
				s = *srcp++;
slouken@689
  1982
				d = *dstp;
slouken@689
  1983
				/*
slouken@689
  1984
				 * shift out the middle component (green) to
slouken@689
  1985
				 * the high 16 bits, and process all three RGB
slouken@689
  1986
				 * components at the same time.
slouken@689
  1987
				 */
slouken@689
  1988
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  1989
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  1990
				d += (s - d) * alpha >> 5;
slouken@689
  1991
				d &= 0x07e0f81f;
slouken@689
  1992
				*dstp++ = d | d >> 16;
slouken@1542
  1993
				s = *srcp++;
slouken@689
  1994
				d = *dstp;
slouken@689
  1995
				/*
slouken@689
  1996
				 * shift out the middle component (green) to
slouken@689
  1997
				 * the high 16 bits, and process all three RGB
slouken@689
  1998
				 * components at the same time.
slouken@689
  1999
				 */
slouken@689
  2000
				s = (s | s << 16) & 0x07e0f81f;
slouken@689
  2001
				d = (d | d << 16) & 0x07e0f81f;
slouken@689
  2002
				d += (s - d) * alpha >> 5;
slouken@689
  2003
				d &= 0x07e0f81f;
slouken@689
  2004
				*dstp++ = d | d >> 16;
slouken@1542
  2005
			},{
slouken@1542
  2006
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  2007
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  2008
slouken@1542
  2009
				/* red -- does not need a mask since the right shift clears
slouken@1542
  2010
				   the uninteresting bits */
slouken@1542
  2011
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2012
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2013
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 [000r 000r 000r 000r] */
slouken@1542
  2014
				psrlw_i2r(11, mm6); /* mm6 >> 11 -> mm6 [000r 000r 000r 000r] */
slouken@1542
  2015
slouken@1542
  2016
				/* blend */
slouken@1542
  2017
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2018
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2019
				/* alpha used is actually 11 bits
slouken@1542
  2020
				   11 + 5 = 16 bits, so the sign bits are lost */
slouken@1542
  2021
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2022
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2023
				psllw_i2r(11, mm6); /* mm6 << 11 -> mm6 */
slouken@1542
  2024
slouken@1542
  2025
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  2026
slouken@1542
  2027
				/* green -- process the bits in place */
slouken@1542
  2028
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2029
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2030
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  2031
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  2032
slouken@1542
  2033
				/* blend */
slouken@1542
  2034
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2035
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2036
				/* 11 + 11 - 16 = 6 bits, so all the lower uninteresting
slouken@1542
  2037
				   bits are gone and the sign bits present */
slouken@1542
  2038
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2039
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2040
slouken@1542
  2041
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  2042
slouken@1542
  2043
				/* blue */
slouken@1542
  2044
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2045
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2046
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  2047
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2048
slouken@1542
  2049
				/* blend */
slouken@1542
  2050
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2051
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2052
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  2053
				   the interesting bits will need to be MASKed */
slouken@1542
  2054
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2055
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2056
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2057
slouken@1542
  2058
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  2059
slouken@1542
  2060
				movq_r2m(mm1, *dstp); /* mm1 -> 4 dst pixels */
slouken@1542
  2061
slouken@1542
  2062
				srcp += 4;
slouken@1542
  2063
				dstp += 4;
slouken@1542
  2064
			}, width);			
slouken@689
  2065
			srcp += srcskip;
slouken@689
  2066
			dstp += dstskip;
slouken@689
  2067
		}
slouken@689
  2068
		emms();
slouken@689
  2069
	}
slouken@689
  2070
}
slouken@689
  2071
slouken@689
  2072
/* fast RGB555->RGB555 blending with surface alpha */
slouken@689
  2073
static void Blit555to555SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@689
  2074
{
slouken@689
  2075
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@689
  2076
	if(alpha == 128) {
slouken@689
  2077
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@689
  2078
	} else {
slouken@689
  2079
		int width = info->d_width;
slouken@689
  2080
		int height = info->d_height;
slouken@689
  2081
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@689
  2082
		int srcskip = info->s_skip >> 1;
slouken@689
  2083
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@689
  2084
		int dstskip = info->d_skip >> 1;
slouken@1542
  2085
		Uint32 s, d;
icculus@4262
  2086
		Uint64 load;
slouken@689
  2087
	  
slouken@689
  2088
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
icculus@4262
  2089
		load = alpha;
slouken@689
  2090
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@689
  2091
icculus@4262
  2092
		movq_m2r(load, mm0); /* alpha(0000000A) -> mm0 */
slouken@1542
  2093
		punpcklwd_r2r(mm0, mm0); /* 00000A0A -> mm0 */
slouken@1542
  2094
		punpcklwd_r2r(mm0, mm0); /* 0A0A0A0A -> mm0 */
slouken@1542
  2095
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2096
		   to reduce the number of operations */
slouken@1542
  2097
		psllq_i2r(3, mm0);
slouken@1542
  2098
slouken@1542
  2099
		/* Setup the 555 color channel masks */
icculus@4262
  2100
		load = 0x03E003E003E003E0ULL;
icculus@4262
  2101
		movq_m2r(load, mm4); /* MASKGREEN -> mm4 */
icculus@4262
  2102
		load = 0x001F001F001F001FULL;
icculus@4262
  2103
		movq_m2r(load, mm7); /* MASKBLUE -> mm7 */
slouken@689
  2104
		while(height--) {
slouken@1542
  2105
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2106
			{
slouken@1542
  2107
				s = *srcp++;
slouken@689
  2108
				d = *dstp;
slouken@689
  2109
				/*
slouken@689
  2110
				 * shift out the middle component (green) to
slouken@689
  2111
				 * the high 16 bits, and process all three RGB
slouken@689
  2112
				 * components at the same time.
slouken@689
  2113
				 */
slouken@689
  2114
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2115
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2116
				d += (s - d) * alpha >> 5;
slouken@689
  2117
				d &= 0x03e07c1f;
slouken@689
  2118
				*dstp++ = d | d >> 16;
slouken@1542
  2119
			},{
slouken@1542
  2120
				s = *srcp++;
slouken@689
  2121
				d = *dstp;
slouken@689
  2122
				/*
slouken@689
  2123
				 * shift out the middle component (green) to
slouken@689
  2124
				 * the high 16 bits, and process all three RGB
slouken@689
  2125
				 * components at the same time.
slouken@689
  2126
				 */
slouken@689
  2127
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2128
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2129
				d += (s - d) * alpha >> 5;
slouken@689
  2130
				d &= 0x03e07c1f;
slouken@689
  2131
				*dstp++ = d | d >> 16;
slouken@689
  2132
			        s = *srcp++;
slouken@689
  2133
				d = *dstp;
slouken@689
  2134
				/*
slouken@689
  2135
				 * shift out the middle component (green) to
slouken@689
  2136
				 * the high 16 bits, and process all three RGB
slouken@689
  2137
				 * components at the same time.
slouken@689
  2138
				 */
slouken@689
  2139
				s = (s | s << 16) & 0x03e07c1f;
slouken@689
  2140
				d = (d | d << 16) & 0x03e07c1f;
slouken@689
  2141
				d += (s - d) * alpha >> 5;
slouken@689
  2142
				d &= 0x03e07c1f;
slouken@689
  2143
				*dstp++ = d | d >> 16;
slouken@1542
  2144
			},{
slouken@1542
  2145
				movq_m2r((*srcp), mm2);/* 4 src pixels -> mm2 */
slouken@1542
  2146
				movq_m2r((*dstp), mm3);/* 4 dst pixels -> mm3 */
slouken@1542
  2147
slouken@1542
  2148
				/* red -- process the bits in place */
slouken@1542
  2149
				psllq_i2r(5, mm4); /* turn MASKGREEN into MASKRED */
slouken@1542
  2150
					/* by reusing the GREEN mask we free up another mmx
slouken@1542
  2151
					   register to accumulate the result */
slouken@1542
  2152
slouken@1542
  2153
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2154
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2155
				pand_r2r(mm4, mm5); /* src & MASKRED -> mm5 */
slouken@1542
  2156
				pand_r2r(mm4, mm6); /* dst & MASKRED -> mm6 */
slouken@1542
  2157
slouken@1542
  2158
				/* blend */
slouken@1542
  2159
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2160
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2161
				/* 11 + 15 - 16 = 10 bits, uninteresting bits will be
slouken@1542
  2162
				   cleared by a MASK below */
slouken@1542
  2163
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2164
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2165
				pand_r2r(mm4, mm6); /* mm6 & MASKRED -> mm6 */
slouken@1542
  2166
slouken@1542
  2167
				psrlq_i2r(5, mm4); /* turn MASKRED back into MASKGREEN */
slouken@1542
  2168
slouken@1542
  2169
				movq_r2r(mm6, mm1); /* save new reds in dsts */
slouken@1542
  2170
slouken@1542
  2171
				/* green -- process the bits in place */
slouken@1542
  2172
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2173
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2174
				pand_r2r(mm4, mm5); /* src & MASKGREEN -> mm5 */
slouken@1542
  2175
				pand_r2r(mm4, mm6); /* dst & MASKGREEN -> mm6 */
slouken@1542
  2176
slouken@1542
  2177
				/* blend */
slouken@1542
  2178
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2179
				pmulhw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2180
				/* 11 + 10 - 16 = 5 bits,  so all the lower uninteresting
slouken@1542
  2181
				   bits are gone and the sign bits present */
slouken@1542
  2182
				psllw_i2r(5, mm5); /* mm5 << 5 -> mm5 */
slouken@1542
  2183
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2184
slouken@1542
  2185
				por_r2r(mm6, mm1); /* save new greens in dsts */
slouken@1542
  2186
slouken@1542
  2187
				/* blue */
slouken@1542
  2188
				movq_r2r(mm2, mm5); /* src -> mm5 */
slouken@1542
  2189
				movq_r2r(mm3, mm6); /* dst -> mm6 */
slouken@1542
  2190
				pand_r2r(mm7, mm5); /* src & MASKBLUE -> mm5[000b 000b 000b 000b] */
slouken@1542
  2191
				pand_r2r(mm7, mm6); /* dst & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2192
slouken@1542
  2193
				/* blend */
slouken@1542
  2194
				psubw_r2r(mm6, mm5);/* src - dst -> mm5 */
slouken@1542
  2195
				pmullw_r2r(mm0, mm5); /* mm5 * alpha -> mm5 */
slouken@1542
  2196
				/* 11 + 5 = 16 bits, so the sign bits are lost and
slouken@1542
  2197
				   the interesting bits will need to be MASKed */
slouken@1542
  2198
				psrlw_i2r(11, mm5); /* mm5 >> 11 -> mm5 */
slouken@1542
  2199
				paddw_r2r(mm5, mm6); /* mm5 + mm6(dst) -> mm6 */
slouken@1542
  2200
				pand_r2r(mm7, mm6); /* mm6 & MASKBLUE -> mm6[000b 000b 000b 000b] */
slouken@1542
  2201
slouken@1542
  2202
				por_r2r(mm6, mm1); /* save new blues in dsts */
slouken@1542
  2203
slouken@1542
  2204
				movq_r2m(mm1, *dstp);/* mm1 -> 4 dst pixels */
slouken@1542
  2205
slouken@1542
  2206
				srcp += 4;
slouken@1542
  2207
				dstp += 4;
slouken@1542
  2208
			}, width);			
slouken@689
  2209
			srcp += srcskip;
slouken@689
  2210
			dstp += dstskip;
slouken@689
  2211
		}
slouken@689
  2212
		emms();
slouken@689
  2213
	}
slouken@689
  2214
}
slouken@1542
  2215
/* End GCC_ASMBLIT */
slouken@1542
  2216
slouken@1542
  2217
#elif MSVC_ASMBLIT
slouken@1542
  2218
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1542
  2219
static void Blit565to565SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@1542
  2220
{
slouken@1542
  2221
	unsigned alpha = info->src->alpha;
slouken@1542
  2222
	if(alpha == 128) {
slouken@1542
  2223
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1542
  2224
	} else {
slouken@1542
  2225
		int width = info->d_width;
slouken@1542
  2226
		int height = info->d_height;
slouken@1542
  2227
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1542
  2228
		int srcskip = info->s_skip >> 1;
slouken@1542
  2229
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1542
  2230
		int dstskip = info->d_skip >> 1;
slouken@1542
  2231
		Uint32 s, d;
slouken@1542
  2232
	  
slouken@1542
  2233
		__m64 src1, dst1, src2, dst2, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2234
slouken@1542
  2235
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  2236
		mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
slouken@1542
  2237
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@1542
  2238
slouken@1542
  2239
		mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  2240
		mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  2241
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2242
		   to reduce the number of operations */
slouken@1542
  2243
		mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1542
  2244
	  
slouken@1542
  2245
		/* Setup the 565 color channel masks */
slouken@1542
  2246
		gmask = _mm_set_pi32(0x07E007E0, 0x07E007E0); /* MASKGREEN -> gmask */
slouken@1542
  2247
		bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
slouken@1542
  2248
		
slouken@1542
  2249
		while(height--) {
slouken@1542
  2250
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2251
			{
slouken@1542
  2252
				s = *srcp++;
slouken@1542
  2253
				d = *dstp;
slouken@1542
  2254
				/*
slouken@1542
  2255
				 * shift out the middle component (green) to
slouken@1542
  2256
				 * the high 16 bits, and process all three RGB
slouken@1542
  2257
				 * components at the same time.
slouken@1542
  2258
				 */
slouken@1542
  2259
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2260
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2261
				d += (s - d) * alpha >> 5;
slouken@1542
  2262
				d &= 0x07e0f81f;
slouken@1546
  2263
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2264
			},{
slouken@1542
  2265
				s = *srcp++;
slouken@1542
  2266
				d = *dstp;
slouken@1542
  2267
				/*
slouken@1542
  2268
				 * shift out the middle component (green) to
slouken@1542
  2269
				 * the high 16 bits, and process all three RGB
slouken@1542
  2270
				 * components at the same time.
slouken@1542
  2271
				 */
slouken@1542
  2272
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2273
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2274
				d += (s - d) * alpha >> 5;
slouken@1542
  2275
				d &= 0x07e0f81f;
slouken@1546
  2276
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2277
				s = *srcp++;
slouken@1542
  2278
				d = *dstp;
slouken@1542
  2279
				/*
slouken@1542
  2280
				 * shift out the middle component (green) to
slouken@1542
  2281
				 * the high 16 bits, and process all three RGB
slouken@1542
  2282
				 * components at the same time.
slouken@1542
  2283
				 */
slouken@1542
  2284
				s = (s | s << 16) & 0x07e0f81f;
slouken@1542
  2285
				d = (d | d << 16) & 0x07e0f81f;
slouken@1542
  2286
				d += (s - d) * alpha >> 5;
slouken@1542
  2287
				d &= 0x07e0f81f;
slouken@1546
  2288
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2289
			},{
slouken@1542
  2290
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2291
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2292
slouken@1542
  2293
				/* red */
slouken@1542
  2294
				src2 = src1;
slouken@1542
  2295
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 [000r 000r 000r 000r] */
slouken@1542
  2296
slouken@1542
  2297
				dst2 = dst1;
slouken@1542
  2298
				dst2 = _mm_srli_pi16(dst2, 11); /* dst2 >> 11 -> dst2 [000r 000r 000r 000r] */
slouken@1542
  2299
slouken@1542
  2300
				/* blend */
slouken@1542
  2301
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2302
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2303
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2304
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2305
				dst2 = _mm_slli_pi16(dst2, 11); /* dst2 << 11 -> dst2 */
slouken@1542
  2306
slouken@1542
  2307
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2308
slouken@1542
  2309
				/* green -- process the bits in place */
slouken@1542
  2310
				src2 = src1;
slouken@1542
  2311
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2312
slouken@1542
  2313
				dst2 = dst1;
slouken@1542
  2314
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2315
slouken@1542
  2316
				/* blend */
slouken@1542
  2317
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2318
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2319
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2320
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2321
slouken@1542
  2322
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2323
slouken@1542
  2324
				/* blue */
slouken@1542
  2325
				src2 = src1;
slouken@1542
  2326
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2327
slouken@1542
  2328
				dst2 = dst1;
slouken@1542
  2329
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2330
slouken@1542
  2331
				/* blend */
slouken@1542
  2332
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2333
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2334
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2335
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2336
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2337
slouken@1542
  2338
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2339
slouken@1542
  2340
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2341
slouken@1542
  2342
				srcp += 4;
slouken@1542
  2343
				dstp += 4;
slouken@1542
  2344
			}, width);			
slouken@1542
  2345
			srcp += srcskip;
slouken@1542
  2346
			dstp += dstskip;
slouken@1542
  2347
		}
slouken@1542
  2348
		_mm_empty();
slouken@1542
  2349
	}
slouken@1542
  2350
}
slouken@1542
  2351
slouken@1542
  2352
/* fast RGB555->RGB555 blending with surface alpha */
slouken@1542
  2353
static void Blit555to555SurfaceAlphaMMX(SDL_BlitInfo *info)
slouken@1542
  2354
{
slouken@1542
  2355
	unsigned alpha = info->src->alpha;
slouken@1542
  2356
	if(alpha == 128) {
slouken@1542
  2357
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1542
  2358
	} else {
slouken@1542
  2359
		int width = info->d_width;
slouken@1542
  2360
		int height = info->d_height;
slouken@1542
  2361
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1542
  2362
		int srcskip = info->s_skip >> 1;
slouken@1542
  2363
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1542
  2364
		int dstskip = info->d_skip >> 1;
slouken@1542
  2365
		Uint32 s, d;
slouken@1542
  2366
	  
slouken@1542
  2367
		__m64 src1, dst1, src2, dst2, rmask, gmask, bmask, mm_res, mm_alpha;
slouken@1542
  2368
slouken@1542
  2369
		alpha &= ~(1+2+4);		/* cut alpha to get the exact same behaviour */
slouken@1542
  2370
		mm_alpha = _mm_set_pi32(0, alpha); /* 0000000A -> mm_alpha */
slouken@1542
  2371
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@1542
  2372
slouken@1542
  2373
		mm_alpha = _mm_unpacklo_pi16(mm_alpha, mm_alpha); /* 00000A0A -> mm_alpha */
slouken@1542
  2374
		mm_alpha = _mm_unpacklo_pi32(mm_alpha, mm_alpha); /* 0A0A0A0A -> mm_alpha */
slouken@1542
  2375
		/* position alpha to allow for mullo and mulhi on diff channels
slouken@1542
  2376
		   to reduce the number of operations */
slouken@1542
  2377
		mm_alpha = _mm_slli_si64(mm_alpha, 3);
slouken@1542
  2378
	  
slouken@1542
  2379
		/* Setup the 555 color channel masks */
slouken@1542
  2380
		rmask = _mm_set_pi32(0x7C007C00, 0x7C007C00); /* MASKRED -> rmask */
slouken@1542
  2381
		gmask = _mm_set_pi32(0x03E003E0, 0x03E003E0); /* MASKGREEN -> gmask */
slouken@1542
  2382
		bmask = _mm_set_pi32(0x001F001F, 0x001F001F); /* MASKBLUE -> bmask */
slouken@1542
  2383
slouken@1542
  2384
		while(height--) {
slouken@1542
  2385
			DUFFS_LOOP_QUATRO2(
slouken@1542
  2386
			{
slouken@1542
  2387
				s = *srcp++;
slouken@1542
  2388
				d = *dstp;
slouken@1542
  2389
				/*
slouken@1542
  2390
				 * shift out the middle component (green) to
slouken@1542
  2391
				 * the high 16 bits, and process all three RGB
slouken@1542
  2392
				 * components at the same time.
slouken@1542
  2393
				 */
slouken@1542
  2394
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2395
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2396
				d += (s - d) * alpha >> 5;
slouken@1542
  2397
				d &= 0x03e07c1f;
slouken@1546
  2398
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2399
			},{
slouken@1542
  2400
				s = *srcp++;
slouken@1542
  2401
				d = *dstp;
slouken@1542
  2402
				/*
slouken@1542
  2403
				 * shift out the middle component (green) to
slouken@1542
  2404
				 * the high 16 bits, and process all three RGB
slouken@1542
  2405
				 * components at the same time.
slouken@1542
  2406
				 */
slouken@1542
  2407
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2408
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2409
				d += (s - d) * alpha >> 5;
slouken@1542
  2410
				d &= 0x03e07c1f;
slouken@1546
  2411
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2412
			        s = *srcp++;
slouken@1542
  2413
				d = *dstp;
slouken@1542
  2414
				/*
slouken@1542
  2415
				 * shift out the middle component (green) to
slouken@1542
  2416
				 * the high 16 bits, and process all three RGB
slouken@1542
  2417
				 * components at the same time.
slouken@1542
  2418
				 */
slouken@1542
  2419
				s = (s | s << 16) & 0x03e07c1f;
slouken@1542
  2420
				d = (d | d << 16) & 0x03e07c1f;
slouken@1542
  2421
				d += (s - d) * alpha >> 5;
slouken@1542
  2422
				d &= 0x03e07c1f;
slouken@1546
  2423
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1542
  2424
			},{
slouken@1542
  2425
				src1 = *(__m64*)srcp; /* 4 src pixels -> src1 */
slouken@1542
  2426
				dst1 = *(__m64*)dstp; /* 4 dst pixels -> dst1 */
slouken@1542
  2427
slouken@1542
  2428
				/* red -- process the bits in place */
slouken@1542
  2429
				src2 = src1;
slouken@1542
  2430
				src2 = _mm_and_si64(src2, rmask); /* src & MASKRED -> src2 */
slouken@1542
  2431
slouken@1542
  2432
				dst2 = dst1;
slouken@1542
  2433
				dst2 = _mm_and_si64(dst2, rmask); /* dst & MASKRED -> dst2 */
slouken@1542
  2434
slouken@1542
  2435
				/* blend */
slouken@1542
  2436
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2437
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2438
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2439
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2440
				dst2 = _mm_and_si64(dst2, rmask); /* dst2 & MASKRED -> dst2 */
slouken@1542
  2441
slouken@1542
  2442
				mm_res = dst2; /* RED -> mm_res */
slouken@1542
  2443
				
slouken@1542
  2444
				/* green -- process the bits in place */
slouken@1542
  2445
				src2 = src1;
slouken@1542
  2446
				src2 = _mm_and_si64(src2, gmask); /* src & MASKGREEN -> src2 */
slouken@1542
  2447
slouken@1542
  2448
				dst2 = dst1;
slouken@1542
  2449
				dst2 = _mm_and_si64(dst2, gmask); /* dst & MASKGREEN -> dst2 */
slouken@1542
  2450
slouken@1542
  2451
				/* blend */
slouken@1542
  2452
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2453
				src2 = _mm_mulhi_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2454
				src2 = _mm_slli_pi16(src2, 5); /* src2 << 5 -> src2 */
slouken@1542
  2455
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2456
slouken@1542
  2457
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN -> mm_res */
slouken@1542
  2458
slouken@1542
  2459
				/* blue */
slouken@1542
  2460
				src2 = src1; /* src -> src2 */
slouken@1542
  2461
				src2 = _mm_and_si64(src2, bmask); /* src & MASKBLUE -> src2[000b 000b 000b 000b] */
slouken@1542
  2462
slouken@1542
  2463
				dst2 = dst1; /* dst -> dst2 */
slouken@1542
  2464
				dst2 = _mm_and_si64(dst2, bmask); /* dst & MASKBLUE -> dst2[000b 000b 000b 000b] */
slouken@1542
  2465
slouken@1542
  2466
				/* blend */
slouken@1542
  2467
				src2 = _mm_sub_pi16(src2, dst2);/* src - dst -> src2 */
slouken@1542
  2468
				src2 = _mm_mullo_pi16(src2, mm_alpha); /* src2 * alpha -> src2 */
slouken@1542
  2469
				src2 = _mm_srli_pi16(src2, 11); /* src2 >> 11 -> src2 */
slouken@1542
  2470
				dst2 = _mm_add_pi16(src2, dst2); /* src2 + dst2 -> dst2 */
slouken@1542
  2471
				dst2 = _mm_and_si64(dst2, bmask); /* dst2 & MASKBLUE -> dst2 */
slouken@1542
  2472
slouken@1542
  2473
				mm_res = _mm_or_si64(mm_res, dst2); /* RED | GREEN | BLUE -> mm_res */
slouken@1542
  2474
slouken@1542
  2475
				*(__m64*)dstp = mm_res; /* mm_res -> 4 dst pixels */
slouken@1542
  2476
slouken@1542
  2477
				srcp += 4;
slouken@1542
  2478
				dstp += 4;
slouken@1542
  2479
			}, width);			
slouken@1542
  2480
			srcp += srcskip;
slouken@1542
  2481
			dstp += dstskip;
slouken@1542
  2482
		}
slouken@1542
  2483
		_mm_empty();
slouken@1542
  2484
	}
slouken@1542
  2485
}
slouken@1542
  2486
#endif /* GCC_ASMBLIT, MSVC_ASMBLIT */
slouken@689
  2487
slouken@1
  2488
/* fast RGB565->RGB565 blending with surface alpha */
slouken@1
  2489
static void Blit565to565SurfaceAlpha(SDL_BlitInfo *info)
slouken@1
  2490
{
slouken@1
  2491
	unsigned alpha = info->src->alpha;
slouken@1
  2492
	if(alpha == 128) {
slouken@1
  2493
		Blit16to16SurfaceAlpha128(info, 0xf7de);
slouken@1
  2494
	} else {
slouken@1
  2495
		int width = info->d_width;
slouken@1
  2496
		int height = info->d_height;
slouken@1
  2497
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
  2498
		int srcskip = info->s_skip >> 1;
slouken@1
  2499
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
  2500
		int dstskip = info->d_skip >> 1;
slouken@1
  2501
		alpha >>= 3;	/* downscale alpha to 5 bits */
slouken@1
  2502
slouken@1
  2503
		while(height--) {
slouken@1
  2504
			DUFFS_LOOP4({
slouken@1
  2505
				Uint32 s = *srcp++;
slouken@1
  2506
				Uint32 d = *dstp;
slouken@1
  2507
				/*
slouken@1
  2508
				 * shift out the middle component (green) to
slouken@1
  2509
				 * the high 16 bits, and process all three RGB
slouken@1
  2510
				 * components at the same time.
slouken@1
  2511
				 */
slouken@1
  2512
				s = (s | s << 16) & 0x07e0f81f;
slouken@1
  2513
				d = (d | d << 16) & 0x07e0f81f;
slouken@1
  2514
				d += (s - d) * alpha >> 5;
slouken@1
  2515
				d &= 0x07e0f81f;
slouken@1428
  2516
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2517
			}, width);
slouken@1
  2518
			srcp += srcskip;
slouken@1
  2519
			dstp += dstskip;
slouken@1
  2520
		}
slouken@0
  2521
	}
slouken@0
  2522
}
slouken@0
  2523
slouken@0
  2524
/* fast RGB555->RGB555 blending with surface alpha */
slouken@0
  2525
static void Blit555to555SurfaceAlpha(SDL_BlitInfo *info)
slouken@0
  2526
{
slouken@1
  2527
	unsigned alpha = info->src->alpha; /* downscale alpha to 5 bits */
slouken@1
  2528
	if(alpha == 128) {
slouken@1
  2529
		Blit16to16SurfaceAlpha128(info, 0xfbde);
slouken@1
  2530
	} else {
slouken@1
  2531
		int width = info->d_width;
slouken@1
  2532
		int height = info->d_height;
slouken@1
  2533
		Uint16 *srcp = (Uint16 *)info->s_pixels;
slouken@1
  2534
		int srcskip = info->s_skip >> 1;
slouken@1
  2535
		Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@1
  2536
		int dstskip = info->d_skip >> 1;
slouken@1
  2537
		alpha >>= 3;		/* downscale alpha to 5 bits */
slouken@0
  2538
slouken@1
  2539
		while(height--) {
slouken@1
  2540
			DUFFS_LOOP4({
slouken@1
  2541
				Uint32 s = *srcp++;
slouken@1
  2542
				Uint32 d = *dstp;
slouken@1
  2543
				/*
slouken@1
  2544
				 * shift out the middle component (green) to
slouken@1
  2545
				 * the high 16 bits, and process all three RGB
slouken@1
  2546
				 * components at the same time.
slouken@1
  2547
				 */
slouken@1
  2548
				s = (s | s << 16) & 0x03e07c1f;
slouken@1
  2549
				d = (d | d << 16) & 0x03e07c1f;
slouken@1
  2550
				d += (s - d) * alpha >> 5;
slouken@1
  2551
				d &= 0x03e07c1f;
slouken@1428
  2552
				*dstp++ = (Uint16)(d | d >> 16);
slouken@1
  2553
			}, width);
slouken@1
  2554
			srcp += srcskip;
slouken@1
  2555
			dstp += dstskip;
slouken@1
  2556
		}
slouken@0
  2557
	}
slouken@0
  2558
}
slouken@0
  2559
slouken@0
  2560
/* fast ARGB8888->RGB565 blending with pixel alpha */
slouken@0
  2561
static void BlitARGBto565PixelAlpha(SDL_BlitInfo *info)
slouken@0
  2562
{
slouken@0
  2563
	int width = info->d_width;
slouken@0
  2564
	int height = info->d_height;
slouken@0
  2565
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  2566
	int srcskip = info->s_skip >> 2;
slouken@0
  2567
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  2568
	int dstskip = info->d_skip >> 1;
slouken@0
  2569
slouken@0
  2570
	while(height--) {
slouken@0
  2571
	    DUFFS_LOOP4({
slouken@0
  2572
		Uint32 s = *srcp;
slouken@0
  2573
		unsigned alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2574
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2575
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2576
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2577
		   Benchmark this! */
slouken@689
  2578
		if(alpha) {   
slouken@689
  2579
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2580
		    *dstp = (Uint16)((s >> 8 & 0xf800) + (s >> 5 & 0x7e0) + (s >> 3  & 0x1f));
slouken@689
  2581
		  } else {
slouken@0
  2582
		    Uint32 d = *dstp;
slouken@0
  2583
		    /*
slouken@0
  2584
		     * convert source and destination to G0RAB65565
slouken@0
  2585
		     * and blend all components at the same time
slouken@0
  2586
		     */
slouken@0
  2587
		    s = ((s & 0xfc00) << 11) + (s >> 8 & 0xf800)
slouken@0
  2588
		      + (s >> 3 & 0x1f);
slouken@0
  2589
		    d = (d | d << 16) & 0x07e0f81f;
slouken@0
  2590
		    d += (s - d) * alpha >> 5;
slouken@0
  2591
		    d &= 0x07e0f81f;
slouken@1428
  2592
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2593
		  }
slouken@0
  2594
		}
slouken@0
  2595
		srcp++;
slouken@0
  2596
		dstp++;
slouken@0
  2597
	    }, width);
slouken@0
  2598
	    srcp += srcskip;
slouken@0
  2599
	    dstp += dstskip;
slouken@0
  2600
	}
slouken@0
  2601
}
slouken@0
  2602
slouken@0
  2603
/* fast ARGB8888->RGB555 blending with pixel alpha */
slouken@0
  2604
static void BlitARGBto555PixelAlpha(SDL_BlitInfo *info)
slouken@0
  2605
{
slouken@0
  2606
	int width = info->d_width;
slouken@0
  2607
	int height = info->d_height;
slouken@0
  2608
	Uint32 *srcp = (Uint32 *)info->s_pixels;
slouken@0
  2609
	int srcskip = info->s_skip >> 2;
slouken@0
  2610
	Uint16 *dstp = (Uint16 *)info->d_pixels;
slouken@0
  2611
	int dstskip = info->d_skip >> 1;
slouken@0
  2612
slouken@0
  2613
	while(height--) {
slouken@0
  2614
	    DUFFS_LOOP4({
slouken@0
  2615
		unsigned alpha;
slouken@0
  2616
		Uint32 s = *srcp;
slouken@0
  2617
		alpha = s >> 27; /* downscale alpha to 5 bits */
slouken@0
  2618
		/* FIXME: Here we special-case opaque alpha since the
slouken@0
  2619
		   compositioning used (>>8 instead of /255) doesn't handle
slouken@0
  2620
		   it correctly. Also special-case alpha=0 for speed?
slouken@0
  2621
		   Benchmark this! */
slouken@689
  2622
		if(alpha) {   
slouken@689
  2623
		  if(alpha == (SDL_ALPHA_OPAQUE >> 3)) {
slouken@1428
  2624
		    *dstp = (Uint16)((s >> 9 & 0x7c00) + (s >> 6 & 0x3e0) + (s >> 3  & 0x1f));
slouken@689
  2625
		  } else {
slouken@0
  2626
		    Uint32 d = *dstp;
slouken@0
  2627
		    /*
slouken@0
  2628
		     * convert source and destination to G0RAB65565
slouken@0
  2629
		     * and blend all components at the same time
slouken@0
  2630
		     */
slouken@0
  2631
		    s = ((s & 0xf800) << 10) + (s >> 9 & 0x7c00)
slouken@0
  2632
		      + (s >> 3 & 0x1f);
slouken@0
  2633
		    d = (d | d << 16) & 0x03e07c1f;
slouken@0
  2634
		    d += (s - d) * alpha >> 5;
slouken@0
  2635
		    d &= 0x03e07c1f;
slouken@1428
  2636
		    *dstp = (Uint16)(d | d >> 16);
slouken@689
  2637
		  }
slouken@0
  2638
		}
slouken@0
  2639
		srcp++;
slouken@0
  2640
		dstp++;
slouken@0
  2641
	    }, width);
slouken@0
  2642
	    srcp += srcskip;
slouken@0
  2643
	    dstp += dstskip;
slouken@0
  2644
	}
slouken@0
  2645
}
slouken@0
  2646
slouken@0
  2647
/* General (slow) N->N blending with per-surface alpha */
slouken@0
  2648
static void BlitNtoNSurfaceAlpha(SDL_BlitInfo *info)
slouken@0
  2649
{
slouken@0
  2650
	int width = info->d_width;
slouken@0
  2651
	int height = info->d_height;
slouken@0
  2652
	Uint8 *src = info->s_pixels;
slouken@0
  2653
	int srcskip = info->s_skip;
slouken@0
  2654
	Uint8 *dst = info->d_pixels;
slouken@0
  2655
	int dstskip = info->d_skip;
slouken@0
  2656
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2657
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2658
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2659
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2660
	unsigned sA = srcfmt->alpha;
slouken@0
  2661
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2662
slouken@689
  2663
	if(sA) {
slouken@689
  2664
	  while ( height-- ) {
slouken@0
  2665
	    DUFFS_LOOP4(
slouken@0
  2666
	    {
icculus@1162
  2667
		Uint32 Pixel;
slouken@0
  2668
		unsigned sR;
slouken@0
  2669
		unsigned sG;
slouken@0
  2670
		unsigned sB;
slouken@0
  2671
		unsigned dR;
slouken@0
  2672
		unsigned dG;
slouken@0
  2673
		unsigned dB;
icculus@1162
  2674
		DISEMBLE_RGB(src, srcbpp, srcfmt, Pixel, sR, sG, sB);
icculus@1162
  2675
		DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2676
		ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2677
		ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2678
		src += srcbpp;
slouken@0
  2679
		dst += dstbpp;
slouken@0
  2680
	    },
slouken@0
  2681
	    width);
slouken@0
  2682
	    src += srcskip;
slouken@0
  2683
	    dst += dstskip;
slouken@689
  2684
	  }
slouken@0
  2685
	}
slouken@0
  2686
}
slouken@0
  2687
slouken@0
  2688
/* General (slow) colorkeyed N->N blending with per-surface alpha */
slouken@0
  2689
static void BlitNtoNSurfaceAlphaKey(SDL_BlitInfo *info)
slouken@0
  2690
{
slouken@0
  2691
	int width = info->d_width;
slouken@0
  2692
	int height = info->d_height;
slouken@0
  2693
	Uint8 *src = info->s_pixels;
slouken@0
  2694
	int srcskip = info->s_skip;
slouken@0
  2695
	Uint8 *dst = info->d_pixels;
slouken@0
  2696
	int dstskip = info->d_skip;
slouken@0
  2697
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2698
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2699
	Uint32 ckey = srcfmt->colorkey;
slouken@0
  2700
	int srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2701
	int dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2702
	unsigned sA = srcfmt->alpha;
slouken@0
  2703
	unsigned dA = dstfmt->Amask ? SDL_ALPHA_OPAQUE : 0;
slouken@0
  2704
slouken@0
  2705
	while ( height-- ) {
slouken@0
  2706
	    DUFFS_LOOP4(
slouken@0
  2707
	    {
icculus@1162
  2708
		Uint32 Pixel;
slouken@0
  2709
		unsigned sR;
slouken@0
  2710
		unsigned sG;
slouken@0
  2711
		unsigned sB;
slouken@0
  2712
		unsigned dR;
slouken@0
  2713
		unsigned dG;
slouken@0
  2714
		unsigned dB;
icculus@1162
  2715
		RETRIEVE_RGB_PIXEL(src, srcbpp, Pixel);
icculus@1162
  2716
		if(sA && Pixel != ckey) {
icculus@1162
  2717
		    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB);
icculus@1162
  2718
		    DISEMBLE_RGB(dst, dstbpp, dstfmt, Pixel, dR, dG, dB);
slouken@0
  2719
		    ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@0
  2720
		    ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@0
  2721
		}
slouken@0
  2722
		src += srcbpp;
slouken@0
  2723
		dst += dstbpp;
slouken@0
  2724
	    },
slouken@0
  2725
	    width);
slouken@0
  2726
	    src += srcskip;
slouken@0
  2727
	    dst += dstskip;
slouken@0
  2728
	}
slouken@0
  2729
}
slouken@0
  2730
slouken@0
  2731
/* General (slow) N->N blending with pixel alpha */
slouken@0
  2732
static void BlitNtoNPixelAlpha(SDL_BlitInfo *info)
slouken@0
  2733
{
slouken@0
  2734
	int width = info->d_width;
slouken@0
  2735
	int height = info->d_height;
slouken@0
  2736
	Uint8 *src = info->s_pixels;
slouken@0
  2737
	int srcskip = info->s_skip;
slouken@0
  2738
	Uint8 *dst = info->d_pixels;
slouken@0
  2739
	int dstskip = info->d_skip;
slouken@0
  2740
	SDL_PixelFormat *srcfmt = info->src;
slouken@0
  2741
	SDL_PixelFormat *dstfmt = info->dst;
slouken@0
  2742
slouken@0
  2743
	int  srcbpp;
slouken@0
  2744
	int  dstbpp;
slouken@0
  2745
slouken@0
  2746
	/* Set up some basic variables */
slouken@0
  2747
	srcbpp = srcfmt->BytesPerPixel;
slouken@0
  2748
	dstbpp = dstfmt->BytesPerPixel;
slouken@0
  2749
slouken@0
  2750
	/* FIXME: for 8bpp source alpha, this doesn't get opaque values
slouken@0
  2751
	   quite right. for <8bpp source alpha, it gets them very wrong
slouken@0
  2752
	   (check all macros!)
slouken@0
  2753
	   It is unclear whether there is a good general solution that doesn't
slouken@0
  2754
	   need a branch (or a divide). */
slouken@0
  2755
	while ( height-- ) {
slouken@0
  2756
	    DUFFS_LOOP4(
slouken@0
  2757
	    {
icculus@1162
  2758
		Uint32 Pixel;
slouken@0
  2759
		unsigned sR;
slouken@0
  2760
		unsigned sG;
slouken@0
  2761
		unsigned sB;
slouken@0
  2762
		unsigned dR;
slouken@0
  2763
		unsigned dG;
slouken@0
  2764
		unsigned dB;
slouken@0
  2765
		unsigned sA;
slouken@0
  2766
		unsigned dA;
icculus@1162
  2767
		DISEMBLE_RGBA(src, srcbpp, srcfmt, Pixel, sR, sG, sB, sA);
slouken@689
  2768
		if(sA) {
icculus@1162
  2769
		  DISEMBLE_RGBA(dst, dstbpp, dstfmt, Pixel, dR, dG, dB, dA);
slouken@689
  2770
		  ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB);
slouken@689
  2771
		  ASSEMBLE_RGBA(dst, dstbpp, dstfmt, dR, dG, dB, dA);
slouken@689
  2772
		}
slouken@0
  2773
		src += srcbpp;
slouken@0
  2774
		dst += dstbpp;
slouken@0
  2775
	    },
slouken@0
  2776
	    width);
slouken@0
  2777
	    src += srcskip;
slouken@0
  2778
	    dst += dstskip;
slouken@0
  2779
	}
slouken@0
  2780
}
slouken@0
  2781
slouken@0
  2782
slouken@0
  2783
SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface *surface, int blit_index)
slouken@0
  2784
{
slouken@0
  2785
    SDL_PixelFormat *sf = surface->format;
slouken@0
  2786
    SDL_PixelFormat *df = surface->map->dst->format;
slouken@0
  2787
slouken@0
  2788
    if(sf->Amask == 0) {
slouken@0
  2789
	if((surface->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
slouken@0
  2790
	    if(df->BytesPerPixel == 1)
slouken@0
  2791
		return BlitNto1SurfaceAlphaKey;
slouken@0
  2792
	    else
slouken@1361
  2793
#if SDL_ALTIVEC_BLITTERS
icculus@1240
  2794
	if (sf->BytesPerPixel == 4 && df->BytesPerPixel == 4 &&
icculus@1240
  2795
	    !(surface->map->dst->flags & SDL_HWSURFACE) && SDL_HasAltiVec())
icculus@1047
  2796
            return Blit32to32SurfaceAlphaKeyAltivec;
icculus@1047
  2797
        else
icculus@1047
  2798
#endif
icculus@1047
  2799
            return BlitNtoNSurfaceAlphaKey;
slouken@0
  2800
	} else {
slouken@0
  2801
	    /* Per-surface alpha blits */
slouken@0
  2802
	    switch(df->BytesPerPixel) {
slouken@0
  2803
	    case 1:
slouken@0
  2804
		return BlitNto1SurfaceAlpha;
slouken@0
  2805
slouken@0
  2806
	    case 2:
slouken@0
  2807
		if(surface->map->identity) {
slouken@0
  2808
		    if(df->Gmask == 0x7e0)
slouken@689
  2809
		    {
slouken@1361
  2810
#if MMX_ASMBLIT
slouken@739
  2811
		if(SDL_HasMMX())
slouken@689
  2812
			return Blit565to565SurfaceAlphaMMX;
slouken@689
  2813
		else
slouken@689
  2814
#endif
slouken@0
  2815
			return Blit565to565SurfaceAlpha;
slouken@689
  2816
		    }
slouken@0
  2817
		    else if(df->Gmask == 0x3e0)
slouken@689
  2818
		    {
slouken@1361
  2819
#if MMX_ASMBLIT
slouken@739
  2820
		if(SDL_HasMMX())
slouken@689
  2821
			return Blit555to555SurfaceAlphaMMX;
slouken@689
  2822
		else
slouken@689
  2823
#endif
slouken@0
  2824
			return Blit555to555SurfaceAlpha;
slouken@689
  2825
		    }
slouken@0
  2826
		}
slouken@0
  2827
		return BlitNtoNSurfaceAlpha;
slouken@0
  2828
slouken@0
  2829
	    case 4:
slouken@0
  2830
		if(sf->Rmask == df->Rmask
slouken@0
  2831
		   && sf->Gmask == df->Gmask
slouken@0
  2832
		   && sf->Bmask == df->Bmask
slouken@0
  2833
		   && sf->BytesPerPixel == 4)
slouken@689
  2834
		{
slouken@1361
  2835
#if MMX_ASMBLIT
slouken@1542
  2836
			if(sf->Rshift % 8 == 0
slouken@1542
  2837
			   && sf->Gshift % 8 == 0
slouken@1542
  2838
			   && sf->Bshift % 8 == 0
slouken@1542
  2839
			   && SDL_HasMMX())
slouken@1542
  2840
			    return BlitRGBtoRGBSurfaceAlphaMMX;
slouken@1542
  2841
#endif
slouken@1542
  2842
			if((sf->Rmask | sf->Gmask | sf->Bmask) == 0xffffff)
slouken@1542
  2843
			{
slouken@1617
  2844
#if SDL_ALTIVEC_BLITTERS
slouken@1617
  2845
				if(!(surface->map->dst->flags & SDL_HWSURFACE)
slouken@1617
  2846
					&& SDL_HasAltiVec())
slouken@1542