src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Aug 2007 06:21:58 +0000
changeset 2262 bee005ace1bf
parent 2261 c20476d7d7b3
child 2263 900c35d8e8fd
permissions -rw-r--r--
Work in progress: merging new texture features into SDL blit system
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
#ifndef _SDL_blit_h
slouken@0
    25
#define _SDL_blit_h
slouken@0
    26
slouken@2249
    27
#ifdef __MMX__
slouken@2249
    28
#include <mmintrin.h>
slouken@2249
    29
#endif
slouken@2261
    30
#ifdef __3dNOW__
slouken@2261
    31
#include <mm3dnow.h>
slouken@2261
    32
#endif
slouken@2249
    33
#ifdef __SSE__
slouken@2249
    34
#include <xmmintrin.h>
slouken@2249
    35
#endif
slouken@2262
    36
#ifdef __SSE2__
slouken@2262
    37
#include <emmintrin.h>
slouken@2262
    38
#endif
slouken@2249
    39
slouken@2250
    40
#include "SDL_cpuinfo.h"
slouken@0
    41
#include "SDL_endian.h"
slouken@0
    42
slouken@2262
    43
/* SDL blit copy flags */
slouken@2262
    44
#define SDL_COPY_MODULATE_COLOR     0x0001
slouken@2262
    45
#define SDL_COPY_MODULATE_ALPHA     0x0002
slouken@2262
    46
#define SDL_COPY_MASK               0x0010
slouken@2262
    47
#define SDL_COPY_BLEND              0x0020
slouken@2262
    48
#define SDL_COPY_ADD                0x0040
slouken@2262
    49
#define SDL_COPY_MOD                0x0080
slouken@2262
    50
#define SDL_COPY_COLORKEY           0x0100
slouken@2262
    51
#define SDL_COPY_NEAREST            0x0200
slouken@2262
    52
slouken@2262
    53
/* SDL blit CPU flags */
slouken@2262
    54
#define SDL_CPU_ANY                 0x0000
slouken@2262
    55
#define SDL_CPU_MMX                 0x0001
slouken@2262
    56
#define SDL_CPU_3DNOW               0x0002
slouken@2262
    57
#define SDL_CPU_SSE                 0x0004
slouken@2262
    58
#define SDL_CPU_SSE2                0x0008
slouken@2262
    59
#define SDL_CPU_ALTIVEC_PREFETCH    0x0010
slouken@2262
    60
#define SDL_CPU_ALTIVEC_NOPREFETCH  0x0020
slouken@2262
    61
slouken@2262
    62
typedef struct {
slouken@2262
    63
    Uint8 *src;
slouken@2262
    64
    int src_w, src_h;
slouken@2262
    65
    int src_pitch;
slouken@2262
    66
    Uint8 *dst;
slouken@2262
    67
    int dst_w, dst_h;
slouken@2262
    68
    int dst_pitch;
slouken@2262
    69
    SDL_PixelFormat *src_fmt;
slouken@2262
    70
    SDL_PixelFormat *dst_fmt;
slouken@1895
    71
    Uint8 *table;
slouken@2262
    72
    int flags;
slouken@2262
    73
    Uint32 colorkey;
slouken@2262
    74
    Uint8 r, g, b, a;
slouken@0
    75
} SDL_BlitInfo;
slouken@0
    76
slouken@2262
    77
typedef void (SDLCALL * SDL_BlitFunc)(SDL_BlitInfo *info);
slouken@2262
    78
slouken@2262
    79
typedef struct {
slouken@2262
    80
    Uint32 src_format;
slouken@2262
    81
    Uint32 dst_format;
slouken@2262
    82
    int flags;
slouken@2262
    83
    int cpu;
slouken@2262
    84
    SDL_BlitFunc func;
slouken@2262
    85
} SDL_BlitFuncEntry;
slouken@0
    86
slouken@0
    87
/* Blit mapping definition */
slouken@1895
    88
typedef struct SDL_BlitMap
slouken@1895
    89
{
slouken@1895
    90
    SDL_Surface *dst;
slouken@1895
    91
    int identity;
slouken@2257
    92
    SDL_blit blit;
slouken@2257
    93
    void *data;
slouken@2262
    94
    SDL_BlitInfo info;
slouken@0
    95
slouken@1895
    96
    /* the version count matches the destination; mismatch indicates
slouken@1895
    97
       an invalid mapping */
slouken@1895
    98
    unsigned int format_version;
slouken@0
    99
} SDL_BlitMap;
slouken@0
   100
slouken@0
   101
/* Functions found in SDL_blit.c */
slouken@1895
   102
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
   103
slouken@0
   104
/*
slouken@0
   105
 * Useful macros for blitting routines
slouken@0
   106
 */
slouken@0
   107
slouken@2249
   108
#if defined(__GNUC__)
slouken@2249
   109
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   110
#elif defined(_MSC_VER)
slouken@2251
   111
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   112
#else
slouken@2249
   113
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   114
#endif
slouken@2249
   115
slouken@0
   116
#define FORMAT_EQUAL(A, B)						\
slouken@0
   117
    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
slouken@0
   118
     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
slouken@0
   119
slouken@0
   120
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@0
   121
/* FIXME: rescale values to 0..255 here? */
icculus@1162
   122
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   123
{									\
icculus@1162
   124
	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
icculus@1162
   125
	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
icculus@1162
   126
	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
slouken@0
   127
}
icculus@1162
   128
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   129
{									\
icculus@1162
   130
	r = (((Pixel&0xF800)>>11)<<3);		 			\
icculus@1162
   131
	g = (((Pixel&0x07E0)>>5)<<2); 					\
icculus@1162
   132
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   133
}
icculus@1162
   134
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   135
{									\
icculus@1162
   136
	r = (((Pixel&0x7C00)>>10)<<3);		 			\
icculus@1162
   137
	g = (((Pixel&0x03E0)>>5)<<3); 					\
icculus@1162
   138
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   139
}
icculus@1162
   140
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   141
{									\
icculus@1162
   142
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   143
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   144
	b = (Pixel&0xFF);			 			\
slouken@0
   145
}
icculus@1162
   146
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   147
do {									   \
slouken@0
   148
	switch (bpp) {							   \
slouken@0
   149
		case 2:							   \
icculus@1162
   150
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   151
		break;							   \
slouken@0
   152
									   \
slouken@0
   153
		case 3: {						   \
slouken@0
   154
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@0
   155
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   156
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   157
			} else {					   \
icculus@1162
   158
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   159
			}						   \
slouken@0
   160
		}							   \
slouken@0
   161
		break;							   \
slouken@0
   162
									   \
slouken@0
   163
		case 4:							   \
icculus@1162
   164
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   165
		break;							   \
slouken@0
   166
									   \
slouken@0
   167
		default:						   \
icculus@1162
   168
			Pixel = 0; /* appease gcc */			   \
slouken@0
   169
		break;							   \
slouken@0
   170
	}								   \
slouken@0
   171
} while(0)
slouken@0
   172
icculus@1162
   173
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   174
do {									   \
slouken@0
   175
	switch (bpp) {							   \
slouken@0
   176
		case 2:							   \
icculus@1162
   177
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   178
		break;							   \
slouken@0
   179
									   \
slouken@0
   180
		case 3: {						   \
slouken@0
   181
		        Uint8 *B = (Uint8 *)buf;			   \
slouken@0
   182
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   183
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   184
			} else {					   \
icculus@1162
   185
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   186
			}						   \
slouken@0
   187
		}							   \
slouken@0
   188
		break;							   \
slouken@0
   189
									   \
slouken@0
   190
		case 4:							   \
icculus@1162
   191
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   192
		break;							   \
slouken@0
   193
									   \
slouken@0
   194
	        default:						   \
icculus@1162
   195
		        Pixel = 0;	/* prevent gcc from complaining */ \
slouken@0
   196
		break;							   \
slouken@0
   197
	}								   \
icculus@1162
   198
	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
slouken@0
   199
} while(0)
slouken@0
   200
slouken@0
   201
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   202
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   203
{									\
icculus@1162
   204
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   205
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   206
		((b>>fmt->Bloss)<<fmt->Bshift);				\
slouken@0
   207
}
icculus@1162
   208
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   209
{									\
icculus@1162
   210
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   211
}
icculus@1162
   212
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   213
{									\
icculus@1162
   214
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   215
}
icculus@1162
   216
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   217
{									\
icculus@1162
   218
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   219
}
slouken@0
   220
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   221
{									\
slouken@0
   222
	switch (bpp) {							\
slouken@0
   223
		case 2: {						\
icculus@1162
   224
			Uint16 Pixel;					\
slouken@0
   225
									\
icculus@1162
   226
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   227
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   228
		}							\
slouken@0
   229
		break;							\
slouken@0
   230
									\
slouken@0
   231
		case 3: {						\
slouken@0
   232
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   233
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   234
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   235
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   236
			} else {					\
slouken@0
   237
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   238
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   239
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   240
			}						\
slouken@0
   241
		}							\
slouken@0
   242
		break;							\
slouken@0
   243
									\
slouken@0
   244
		case 4: {						\
icculus@1162
   245
			Uint32 Pixel;					\
slouken@0
   246
									\
icculus@1162
   247
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   248
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   249
		}							\
slouken@0
   250
		break;							\
slouken@0
   251
	}								\
slouken@0
   252
}
slouken@0
   253
#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
slouken@0
   254
{									\
slouken@0
   255
	switch (bpp) {							\
slouken@0
   256
		case 2: {						\
slouken@0
   257
			Uint16 *bufp;					\
icculus@1162
   258
			Uint16 Pixel;					\
slouken@0
   259
									\
slouken@0
   260
			bufp = (Uint16 *)buf;				\
icculus@1162
   261
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   262
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   263
		}							\
slouken@0
   264
		break;							\
slouken@0
   265
									\
slouken@0
   266
		case 3: {						\
slouken@0
   267
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   268
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   269
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   270
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   271
			} else {					\
slouken@0
   272
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   273
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   274
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   275
			}						\
slouken@0
   276
		}							\
slouken@0
   277
		break;							\
slouken@0
   278
									\
slouken@0
   279
		case 4: {						\
slouken@0
   280
			Uint32 *bufp;					\
icculus@1162
   281
			Uint32 Pixel;					\
slouken@0
   282
									\
slouken@0
   283
			bufp = (Uint32 *)buf;				\
icculus@1162
   284
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   285
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   286
		}							\
slouken@0
   287
		break;							\
slouken@0
   288
	}								\
slouken@0
   289
}
slouken@0
   290
slouken@0
   291
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   292
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   293
{									\
icculus@1162
   294
	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
icculus@1162
   295
	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
icculus@1162
   296
	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
icculus@1162
   297
	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
slouken@0
   298
}
icculus@1162
   299
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   300
{						\
icculus@1162
   301
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   302
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   303
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   304
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   305
}
icculus@1162
   306
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   307
{									\
icculus@1162
   308
	r = (Pixel>>24);						\
icculus@1162
   309
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   310
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   311
	a = (Pixel&0xFF);						\
slouken@0
   312
}
icculus@1162
   313
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   314
{									\
icculus@1162
   315
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   316
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   317
	b = (Pixel&0xFF);						\
icculus@1162
   318
	a = (Pixel>>24);						\
slouken@0
   319
}
icculus@1162
   320
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   321
{									\
icculus@1162
   322
	r = (Pixel&0xFF);						\
icculus@1162
   323
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   324
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   325
	a = (Pixel>>24);						\
slouken@0
   326
}
icculus@1162
   327
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   328
do {									   \
slouken@0
   329
	switch (bpp) {							   \
slouken@0
   330
		case 2:							   \
icculus@1162
   331
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   332
		break;							   \
slouken@0
   333
									   \
slouken@0
   334
		case 3:	{/* FIXME: broken code (no alpha) */		   \
slouken@0
   335
		        Uint8 *b = (Uint8 *)buf;			   \
slouken@0
   336
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   337
			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
slouken@0
   338
			} else {					   \
icculus@1162
   339
			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
slouken@0
   340
			}						   \
slouken@0
   341
		}							   \
slouken@0
   342
		break;							   \
slouken@0
   343
									   \
slouken@0
   344
		case 4:							   \
icculus@1162
   345
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   346
		break;							   \
slouken@0
   347
									   \
slouken@0
   348
		default:						   \
icculus@1162
   349
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   350
		break;							   \
slouken@0
   351
	}								   \
icculus@1162
   352
	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
icculus@1162
   353
	Pixel &= ~fmt->Amask;						   \
slouken@0
   354
} while(0)
slouken@0
   355
slouken@0
   356
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   357
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   358
{									\
icculus@1162
   359
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   360
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   361
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   362
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   363
}
slouken@0
   364
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   365
{									\
slouken@0
   366
	switch (bpp) {							\
slouken@0
   367
		case 2: {						\
icculus@1162
   368
			Uint16 Pixel;					\
slouken@0
   369
									\
icculus@1162
   370
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   371
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   372
		}							\
slouken@0
   373
		break;							\
slouken@0
   374
									\
slouken@0
   375
		case 3: { /* FIXME: broken code (no alpha) */		\
slouken@0
   376
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   377
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   378
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   379
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   380
			} else {					\
slouken@0
   381
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   382
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   383
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   384
			}						\
slouken@0
   385
		}							\
slouken@0
   386
		break;							\
slouken@0
   387
									\
slouken@0
   388
		case 4: {						\
icculus@1162
   389
			Uint32 Pixel;					\
slouken@0
   390
									\
icculus@1162
   391
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   392
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   393
		}							\
slouken@0
   394
		break;							\
slouken@0
   395
	}								\
slouken@0
   396
}
slouken@0
   397
icculus@1162
   398
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   399
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   400
do {						\
slouken@0
   401
	dR = (((sR-dR)*(A))>>8)+dR;		\
slouken@0
   402
	dG = (((sG-dG)*(A))>>8)+dG;		\
slouken@0
   403
	dB = (((sB-dB)*(A))>>8)+dB;		\
slouken@0
   404
} while(0)
slouken@0
   405
icculus@1162
   406
/* Blend the RGB values of two Pixels based on a source alpha value */
icculus@1047
   407
#define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
icculus@1047
   408
do {						\
icculus@1047
   409
    unsigned tR, tG, tB, tA; \
icculus@1047
   410
    tA = 255 - sA; \
icculus@1047
   411
    tR = 1 + (sR * sA) + (dR * tA); \
icculus@1047
   412
    dR = (tR + (tR >> 8)) >> 8; \
icculus@1047
   413
    tG = 1 + (sG * sA) + (dG * tA); \
icculus@1047
   414
    dG = (tG + (tG >> 8)) >> 8; \
icculus@1047
   415
    tB = 1 + (sB * sA) + (dB * tA); \
icculus@1047
   416
    dB = (tB + (tB >> 8)) >> 8; \
icculus@1047
   417
} while(0)
icculus@1047
   418
icculus@1047
   419
slouken@0
   420
/* This is a very useful loop for optimizing blitters */
slouken@553
   421
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   422
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   423
#else
slouken@0
   424
#define USE_DUFFS_LOOP
slouken@553
   425
#endif
slouken@0
   426
#ifdef USE_DUFFS_LOOP
slouken@0
   427
slouken@0
   428
/* 8-times unrolled loop */
slouken@0
   429
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   430
{ int n = (width+7)/8;							\
slouken@91
   431
	switch (width & 7) {						\
slouken@0
   432
	case 0: do {	pixel_copy_increment;				\
slouken@0
   433
	case 7:		pixel_copy_increment;				\
slouken@0
   434
	case 6:		pixel_copy_increment;				\
slouken@0
   435
	case 5:		pixel_copy_increment;				\
slouken@0
   436
	case 4:		pixel_copy_increment;				\
slouken@0
   437
	case 3:		pixel_copy_increment;				\
slouken@0
   438
	case 2:		pixel_copy_increment;				\
slouken@0
   439
	case 1:		pixel_copy_increment;				\
slouken@0
   440
		} while ( --n > 0 );					\
slouken@0
   441
	}								\
slouken@0
   442
}
slouken@0
   443
slouken@0
   444
/* 4-times unrolled loop */
slouken@0
   445
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   446
{ int n = (width+3)/4;							\
slouken@91
   447
	switch (width & 3) {						\
slouken@0
   448
	case 0: do {	pixel_copy_increment;				\
slouken@0
   449
	case 3:		pixel_copy_increment;				\
slouken@0
   450
	case 2:		pixel_copy_increment;				\
slouken@0
   451
	case 1:		pixel_copy_increment;				\
slouken@0
   452
		} while ( --n > 0 );					\
slouken@0
   453
	}								\
slouken@0
   454
}
slouken@0
   455
slouken@689
   456
/* 2 - times unrolled loop */
slouken@689
   457
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   458
				double_pixel_copy_increment, width)	\
slouken@689
   459
{ int n, w = width;							\
slouken@689
   460
	if( w & 1 ) {							\
slouken@689
   461
	    pixel_copy_increment;					\
slouken@689
   462
	    w--;							\
slouken@689
   463
	}								\
slouken@689
   464
	if ( w > 0 )	{						\
slouken@689
   465
	    n = ( w + 2) / 4;						\
slouken@689
   466
	    switch( w & 2 ) {						\
slouken@689
   467
	    case 0: do {	double_pixel_copy_increment;		\
slouken@689
   468
	    case 2:		double_pixel_copy_increment;		\
slouken@689
   469
		    } while ( --n > 0 );					\
slouken@689
   470
	    }								\
slouken@689
   471
	}								\
slouken@689
   472
}
slouken@689
   473
slouken@689
   474
/* 2 - times unrolled loop 4 pixels */
slouken@689
   475
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   476
				double_pixel_copy_increment,		\
slouken@689
   477
				quatro_pixel_copy_increment, width)	\
slouken@689
   478
{ int n, w = width;								\
slouken@689
   479
        if(w & 1) {							\
slouken@689
   480
	  pixel_copy_increment;						\
slouken@689
   481
	  w--;								\
slouken@689
   482
	}								\
slouken@689
   483
	if(w & 2) {							\
slouken@689
   484
	  double_pixel_copy_increment;					\
slouken@689
   485
	  w -= 2;							\
slouken@689
   486
	}								\
slouken@689
   487
	if ( w > 0 ) {							\
slouken@689
   488
	    n = ( w + 7 ) / 8;						\
slouken@689
   489
	    switch( w & 4 ) {						\
slouken@689
   490
	    case 0: do {	quatro_pixel_copy_increment;		\
slouken@689
   491
	    case 4:		quatro_pixel_copy_increment;		\
slouken@689
   492
		    } while ( --n > 0 );					\
slouken@689
   493
	    }								\
slouken@689
   494
	}								\
slouken@689
   495
}
slouken@689
   496
slouken@0
   497
/* Use the 8-times version of the loop by default */
slouken@0
   498
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   499
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   500
slouken@0
   501
#else
slouken@0
   502
slouken@0
   503
/* Don't use Duff's device to unroll loops */
slouken@689
   504
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   505
			 double_pixel_copy_increment, width)		\
slouken@689
   506
{ int n = width;								\
slouken@689
   507
    if( n & 1 ) {							\
slouken@689
   508
	pixel_copy_increment;						\
slouken@689
   509
	n--;								\
slouken@689
   510
    }									\
slouken@689
   511
    n=n>>1;								\
slouken@689
   512
    for(; n > 0; --n) {   						\
slouken@689
   513
	double_pixel_copy_increment;					\
slouken@689
   514
    }									\
slouken@689
   515
}
slouken@689
   516
slouken@689
   517
/* Don't use Duff's device to unroll loops */
slouken@689
   518
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   519
				double_pixel_copy_increment,		\
slouken@689
   520
				quatro_pixel_copy_increment, width)	\
slouken@689
   521
{ int n = width;								\
slouken@689
   522
        if(n & 1) {							\
slouken@689
   523
	  pixel_copy_increment;						\
slouken@689
   524
	  n--;								\
slouken@689
   525
	}								\
slouken@689
   526
	if(n & 2) {							\
slouken@689
   527
	  double_pixel_copy_increment;					\
slouken@689
   528
	  n -= 2;							\
slouken@689
   529
	}								\
slouken@689
   530
	n=n>>2;								\
slouken@689
   531
	for(; n > 0; --n) {   						\
slouken@689
   532
	  quatro_pixel_copy_increment;					\
slouken@689
   533
        }								\
slouken@689
   534
}
slouken@689
   535
slouken@689
   536
/* Don't use Duff's device to unroll loops */
slouken@0
   537
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   538
{ int n;								\
slouken@0
   539
	for ( n=width; n > 0; --n ) {					\
slouken@0
   540
		pixel_copy_increment;					\
slouken@0
   541
	}								\
slouken@0
   542
}
slouken@0
   543
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   544
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   545
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   546
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   547
slouken@0
   548
#endif /* USE_DUFFS_LOOP */
slouken@0
   549
slouken@0
   550
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   551
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   552
#pragma warning(disable: 4550)
slouken@0
   553
#endif
slouken@0
   554
slouken@0
   555
#endif /* _SDL_blit_h */
slouken@1895
   556
/* vi: set ts=4 sw=4 expandtab: */