src/video/SDL_blit.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Aug 2007 00:54:53 +0000
changeset 2257 340942cfda48
parent 2251 292bee385630
child 2260 202ddfd1cfb1
permissions -rw-r--r--
Moved the colorkey and per-surface alpha into the blit info,
in preparation for support for general color channel modulation.

Removed and consolidated some data in the blit info.
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@2249
    30
#ifdef __SSE__
slouken@2249
    31
#include <xmmintrin.h>
slouken@2249
    32
#endif
slouken@2249
    33
slouken@2250
    34
#include "SDL_cpuinfo.h"
slouken@0
    35
#include "SDL_endian.h"
slouken@0
    36
slouken@0
    37
/* The structure passed to the low level blit functions */
slouken@1895
    38
typedef struct
slouken@1895
    39
{
slouken@1895
    40
    Uint8 *s_pixels;
slouken@1895
    41
    int s_width;
slouken@1895
    42
    int s_height;
slouken@1895
    43
    int s_skip;
slouken@1895
    44
    Uint8 *d_pixels;
slouken@1895
    45
    int d_width;
slouken@1895
    46
    int d_height;
slouken@1895
    47
    int d_skip;
slouken@1895
    48
    SDL_PixelFormat *src;
slouken@1895
    49
    Uint8 *table;
slouken@1895
    50
    SDL_PixelFormat *dst;
slouken@2257
    51
    Uint32 ckey, cmod;
slouken@0
    52
} SDL_BlitInfo;
slouken@0
    53
slouken@0
    54
/* The type definition for the low level blit functions */
slouken@1895
    55
typedef void (*SDL_loblit) (SDL_BlitInfo * info);
slouken@0
    56
slouken@0
    57
/* Blit mapping definition */
slouken@1895
    58
typedef struct SDL_BlitMap
slouken@1895
    59
{
slouken@1895
    60
    SDL_Surface *dst;
slouken@1895
    61
    int identity;
slouken@1895
    62
    Uint8 *table;
slouken@2257
    63
    SDL_blit blit;
slouken@2257
    64
    void *data;
slouken@2257
    65
    Uint32 ckey;    /* colorkey */
slouken@2257
    66
    Uint32 cmod;    /* ARGB modulation */
slouken@0
    67
slouken@1895
    68
    /* the version count matches the destination; mismatch indicates
slouken@1895
    69
       an invalid mapping */
slouken@1895
    70
    unsigned int format_version;
slouken@0
    71
} SDL_BlitMap;
slouken@0
    72
slouken@2247
    73
#define SDL_BLIT_ANY                0x00000000
slouken@2247
    74
#define SDL_BLIT_MMX                0x00000001
slouken@2247
    75
#define SDL_BLIT_SSE                0x00000002
slouken@2247
    76
#define SDL_BLIT_ALTIVEC_PREFETCH   0x00000004
slouken@2247
    77
#define SDL_BLIT_ALTIVEC_NOPREFETCH 0x00000008
slouken@2247
    78
slouken@2247
    79
typedef struct SDL_BlitEntry
slouken@2247
    80
{
slouken@2247
    81
    Uint32 features;
slouken@2247
    82
    SDL_loblit blit;
slouken@2247
    83
} SDL_BlitEntry;
slouken@0
    84
slouken@0
    85
/* Functions found in SDL_blit.c */
slouken@1895
    86
extern int SDL_CalculateBlit(SDL_Surface * surface);
slouken@0
    87
slouken@0
    88
/* Functions found in SDL_blit_{0,1,N,A}.c */
slouken@1895
    89
extern SDL_loblit SDL_CalculateBlit0(SDL_Surface * surface, int complex);
slouken@1895
    90
extern SDL_loblit SDL_CalculateBlit1(SDL_Surface * surface, int complex);
slouken@1895
    91
extern SDL_loblit SDL_CalculateBlitN(SDL_Surface * surface, int complex);
slouken@1895
    92
extern SDL_loblit SDL_CalculateAlphaBlit(SDL_Surface * surface, int complex);
slouken@0
    93
slouken@0
    94
/*
slouken@0
    95
 * Useful macros for blitting routines
slouken@0
    96
 */
slouken@0
    97
slouken@2249
    98
#if defined(__GNUC__)
slouken@2249
    99
#define DECLARE_ALIGNED(t,v,a)  t __attribute__((aligned(a))) v
slouken@2249
   100
#elif defined(_MSC_VER)
slouken@2251
   101
#define DECLARE_ALIGNED(t,v,a)  __declspec(align(a)) t v
slouken@2249
   102
#else
slouken@2249
   103
#define DECLARE_ALIGNED(t,v,a)  t v
slouken@2249
   104
#endif
slouken@2249
   105
slouken@0
   106
#define FORMAT_EQUAL(A, B)						\
slouken@0
   107
    ((A)->BitsPerPixel == (B)->BitsPerPixel				\
slouken@0
   108
     && ((A)->Rmask == (B)->Rmask) && ((A)->Amask == (B)->Amask))
slouken@0
   109
slouken@0
   110
/* Load pixel of the specified format from a buffer and get its R-G-B values */
slouken@0
   111
/* FIXME: rescale values to 0..255 here? */
icculus@1162
   112
#define RGB_FROM_PIXEL(Pixel, fmt, r, g, b)				\
slouken@0
   113
{									\
icculus@1162
   114
	r = (((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss); 		\
icculus@1162
   115
	g = (((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss); 		\
icculus@1162
   116
	b = (((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss); 		\
slouken@0
   117
}
icculus@1162
   118
#define RGB_FROM_RGB565(Pixel, r, g, b)					\
slouken@0
   119
{									\
icculus@1162
   120
	r = (((Pixel&0xF800)>>11)<<3);		 			\
icculus@1162
   121
	g = (((Pixel&0x07E0)>>5)<<2); 					\
icculus@1162
   122
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   123
}
icculus@1162
   124
#define RGB_FROM_RGB555(Pixel, r, g, b)					\
slouken@0
   125
{									\
icculus@1162
   126
	r = (((Pixel&0x7C00)>>10)<<3);		 			\
icculus@1162
   127
	g = (((Pixel&0x03E0)>>5)<<3); 					\
icculus@1162
   128
	b = ((Pixel&0x001F)<<3); 					\
slouken@0
   129
}
icculus@1162
   130
#define RGB_FROM_RGB888(Pixel, r, g, b)					\
slouken@0
   131
{									\
icculus@1162
   132
	r = ((Pixel&0xFF0000)>>16);		 			\
icculus@1162
   133
	g = ((Pixel&0xFF00)>>8);		 			\
icculus@1162
   134
	b = (Pixel&0xFF);			 			\
slouken@0
   135
}
icculus@1162
   136
#define RETRIEVE_RGB_PIXEL(buf, bpp, Pixel)				   \
slouken@0
   137
do {									   \
slouken@0
   138
	switch (bpp) {							   \
slouken@0
   139
		case 2:							   \
icculus@1162
   140
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   141
		break;							   \
slouken@0
   142
									   \
slouken@0
   143
		case 3: {						   \
slouken@0
   144
		        Uint8 *B = (Uint8 *)(buf);			   \
slouken@0
   145
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   146
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   147
			} else {					   \
icculus@1162
   148
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   149
			}						   \
slouken@0
   150
		}							   \
slouken@0
   151
		break;							   \
slouken@0
   152
									   \
slouken@0
   153
		case 4:							   \
icculus@1162
   154
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   155
		break;							   \
slouken@0
   156
									   \
slouken@0
   157
		default:						   \
icculus@1162
   158
			Pixel = 0; /* appease gcc */			   \
slouken@0
   159
		break;							   \
slouken@0
   160
	}								   \
slouken@0
   161
} while(0)
slouken@0
   162
icculus@1162
   163
#define DISEMBLE_RGB(buf, bpp, fmt, Pixel, r, g, b)			   \
slouken@0
   164
do {									   \
slouken@0
   165
	switch (bpp) {							   \
slouken@0
   166
		case 2:							   \
icculus@1162
   167
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   168
		break;							   \
slouken@0
   169
									   \
slouken@0
   170
		case 3: {						   \
slouken@0
   171
		        Uint8 *B = (Uint8 *)buf;			   \
slouken@0
   172
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   173
			        Pixel = B[0] + (B[1] << 8) + (B[2] << 16); \
slouken@0
   174
			} else {					   \
icculus@1162
   175
			        Pixel = (B[0] << 16) + (B[1] << 8) + B[2]; \
slouken@0
   176
			}						   \
slouken@0
   177
		}							   \
slouken@0
   178
		break;							   \
slouken@0
   179
									   \
slouken@0
   180
		case 4:							   \
icculus@1162
   181
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   182
		break;							   \
slouken@0
   183
									   \
slouken@0
   184
	        default:						   \
icculus@1162
   185
		        Pixel = 0;	/* prevent gcc from complaining */ \
slouken@0
   186
		break;							   \
slouken@0
   187
	}								   \
icculus@1162
   188
	RGB_FROM_PIXEL(Pixel, fmt, r, g, b);				   \
slouken@0
   189
} while(0)
slouken@0
   190
slouken@0
   191
/* Assemble R-G-B values into a specified pixel format and store them */
icculus@1162
   192
#define PIXEL_FROM_RGB(Pixel, fmt, r, g, b)				\
slouken@0
   193
{									\
icculus@1162
   194
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   195
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   196
		((b>>fmt->Bloss)<<fmt->Bshift);				\
slouken@0
   197
}
icculus@1162
   198
#define RGB565_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   199
{									\
icculus@1162
   200
	Pixel = ((r>>3)<<11)|((g>>2)<<5)|(b>>3);			\
slouken@0
   201
}
icculus@1162
   202
#define RGB555_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   203
{									\
icculus@1162
   204
	Pixel = ((r>>3)<<10)|((g>>3)<<5)|(b>>3);			\
slouken@0
   205
}
icculus@1162
   206
#define RGB888_FROM_RGB(Pixel, r, g, b)					\
slouken@0
   207
{									\
icculus@1162
   208
	Pixel = (r<<16)|(g<<8)|b;					\
slouken@0
   209
}
slouken@0
   210
#define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b) 				\
slouken@0
   211
{									\
slouken@0
   212
	switch (bpp) {							\
slouken@0
   213
		case 2: {						\
icculus@1162
   214
			Uint16 Pixel;					\
slouken@0
   215
									\
icculus@1162
   216
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   217
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   218
		}							\
slouken@0
   219
		break;							\
slouken@0
   220
									\
slouken@0
   221
		case 3: {						\
slouken@0
   222
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   223
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   224
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   225
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   226
			} else {					\
slouken@0
   227
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   228
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   229
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   230
			}						\
slouken@0
   231
		}							\
slouken@0
   232
		break;							\
slouken@0
   233
									\
slouken@0
   234
		case 4: {						\
icculus@1162
   235
			Uint32 Pixel;					\
slouken@0
   236
									\
icculus@1162
   237
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   238
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   239
		}							\
slouken@0
   240
		break;							\
slouken@0
   241
	}								\
slouken@0
   242
}
slouken@0
   243
#define ASSEMBLE_RGB_AMASK(buf, bpp, fmt, r, g, b, Amask)		\
slouken@0
   244
{									\
slouken@0
   245
	switch (bpp) {							\
slouken@0
   246
		case 2: {						\
slouken@0
   247
			Uint16 *bufp;					\
icculus@1162
   248
			Uint16 Pixel;					\
slouken@0
   249
									\
slouken@0
   250
			bufp = (Uint16 *)buf;				\
icculus@1162
   251
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   252
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   253
		}							\
slouken@0
   254
		break;							\
slouken@0
   255
									\
slouken@0
   256
		case 3: {						\
slouken@0
   257
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   258
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   259
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   260
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   261
			} else {					\
slouken@0
   262
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   263
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   264
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   265
			}						\
slouken@0
   266
		}							\
slouken@0
   267
		break;							\
slouken@0
   268
									\
slouken@0
   269
		case 4: {						\
slouken@0
   270
			Uint32 *bufp;					\
icculus@1162
   271
			Uint32 Pixel;					\
slouken@0
   272
									\
slouken@0
   273
			bufp = (Uint32 *)buf;				\
icculus@1162
   274
			PIXEL_FROM_RGB(Pixel, fmt, r, g, b);		\
icculus@1162
   275
			*bufp = Pixel | (*bufp & Amask);		\
slouken@0
   276
		}							\
slouken@0
   277
		break;							\
slouken@0
   278
	}								\
slouken@0
   279
}
slouken@0
   280
slouken@0
   281
/* FIXME: Should we rescale alpha into 0..255 here? */
icculus@1162
   282
#define RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a)				\
slouken@0
   283
{									\
icculus@1162
   284
	r = ((Pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
icculus@1162
   285
	g = ((Pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
icculus@1162
   286
	b = ((Pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
icculus@1162
   287
	a = ((Pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
slouken@0
   288
}
icculus@1162
   289
#define RGBA_FROM_8888(Pixel, fmt, r, g, b, a)	\
slouken@0
   290
{						\
icculus@1162
   291
	r = (Pixel&fmt->Rmask)>>fmt->Rshift;	\
icculus@1162
   292
	g = (Pixel&fmt->Gmask)>>fmt->Gshift;	\
icculus@1162
   293
	b = (Pixel&fmt->Bmask)>>fmt->Bshift;	\
icculus@1162
   294
	a = (Pixel&fmt->Amask)>>fmt->Ashift;	\
slouken@0
   295
}
icculus@1162
   296
#define RGBA_FROM_RGBA8888(Pixel, r, g, b, a)				\
slouken@0
   297
{									\
icculus@1162
   298
	r = (Pixel>>24);						\
icculus@1162
   299
	g = ((Pixel>>16)&0xFF);						\
icculus@1162
   300
	b = ((Pixel>>8)&0xFF);						\
icculus@1162
   301
	a = (Pixel&0xFF);						\
slouken@0
   302
}
icculus@1162
   303
#define RGBA_FROM_ARGB8888(Pixel, r, g, b, a)				\
slouken@0
   304
{									\
icculus@1162
   305
	r = ((Pixel>>16)&0xFF);						\
icculus@1162
   306
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   307
	b = (Pixel&0xFF);						\
icculus@1162
   308
	a = (Pixel>>24);						\
slouken@0
   309
}
icculus@1162
   310
#define RGBA_FROM_ABGR8888(Pixel, r, g, b, a)				\
slouken@0
   311
{									\
icculus@1162
   312
	r = (Pixel&0xFF);						\
icculus@1162
   313
	g = ((Pixel>>8)&0xFF);						\
icculus@1162
   314
	b = ((Pixel>>16)&0xFF);						\
icculus@1162
   315
	a = (Pixel>>24);						\
slouken@0
   316
}
icculus@1162
   317
#define DISEMBLE_RGBA(buf, bpp, fmt, Pixel, r, g, b, a)			   \
slouken@0
   318
do {									   \
slouken@0
   319
	switch (bpp) {							   \
slouken@0
   320
		case 2:							   \
icculus@1162
   321
			Pixel = *((Uint16 *)(buf));			   \
slouken@0
   322
		break;							   \
slouken@0
   323
									   \
slouken@0
   324
		case 3:	{/* FIXME: broken code (no alpha) */		   \
slouken@0
   325
		        Uint8 *b = (Uint8 *)buf;			   \
slouken@0
   326
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		   \
icculus@1162
   327
			        Pixel = b[0] + (b[1] << 8) + (b[2] << 16); \
slouken@0
   328
			} else {					   \
icculus@1162
   329
			        Pixel = (b[0] << 16) + (b[1] << 8) + b[2]; \
slouken@0
   330
			}						   \
slouken@0
   331
		}							   \
slouken@0
   332
		break;							   \
slouken@0
   333
									   \
slouken@0
   334
		case 4:							   \
icculus@1162
   335
			Pixel = *((Uint32 *)(buf));			   \
slouken@0
   336
		break;							   \
slouken@0
   337
									   \
slouken@0
   338
		default:						   \
icculus@1162
   339
		        Pixel = 0; /* stop gcc complaints */		   \
slouken@0
   340
		break;							   \
slouken@0
   341
	}								   \
icculus@1162
   342
	RGBA_FROM_PIXEL(Pixel, fmt, r, g, b, a);			   \
icculus@1162
   343
	Pixel &= ~fmt->Amask;						   \
slouken@0
   344
} while(0)
slouken@0
   345
slouken@0
   346
/* FIXME: this isn't correct, especially for Alpha (maximum != 255) */
icculus@1162
   347
#define PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a)				\
slouken@0
   348
{									\
icculus@1162
   349
	Pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
slouken@0
   350
		((g>>fmt->Gloss)<<fmt->Gshift)|				\
slouken@0
   351
		((b>>fmt->Bloss)<<fmt->Bshift)|				\
slouken@535
   352
		((a>>fmt->Aloss)<<fmt->Ashift);				\
slouken@0
   353
}
slouken@0
   354
#define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
slouken@0
   355
{									\
slouken@0
   356
	switch (bpp) {							\
slouken@0
   357
		case 2: {						\
icculus@1162
   358
			Uint16 Pixel;					\
slouken@0
   359
									\
icculus@1162
   360
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   361
			*((Uint16 *)(buf)) = Pixel;			\
slouken@0
   362
		}							\
slouken@0
   363
		break;							\
slouken@0
   364
									\
slouken@0
   365
		case 3: { /* FIXME: broken code (no alpha) */		\
slouken@0
   366
                        if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {		\
slouken@0
   367
			        *((buf)+fmt->Rshift/8) = r;		\
slouken@0
   368
				*((buf)+fmt->Gshift/8) = g;		\
slouken@0
   369
				*((buf)+fmt->Bshift/8) = b;		\
slouken@0
   370
			} else {					\
slouken@0
   371
			        *((buf)+2-fmt->Rshift/8) = r;		\
slouken@0
   372
				*((buf)+2-fmt->Gshift/8) = g;		\
slouken@0
   373
				*((buf)+2-fmt->Bshift/8) = b;		\
slouken@0
   374
			}						\
slouken@0
   375
		}							\
slouken@0
   376
		break;							\
slouken@0
   377
									\
slouken@0
   378
		case 4: {						\
icculus@1162
   379
			Uint32 Pixel;					\
slouken@0
   380
									\
icculus@1162
   381
			PIXEL_FROM_RGBA(Pixel, fmt, r, g, b, a);	\
icculus@1162
   382
			*((Uint32 *)(buf)) = Pixel;			\
slouken@0
   383
		}							\
slouken@0
   384
		break;							\
slouken@0
   385
	}								\
slouken@0
   386
}
slouken@0
   387
icculus@1162
   388
/* Blend the RGB values of two Pixels based on a source alpha value */
slouken@0
   389
#define ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
slouken@0
   390
do {						\
slouken@0
   391
	dR = (((sR-dR)*(A))>>8)+dR;		\
slouken@0
   392
	dG = (((sG-dG)*(A))>>8)+dG;		\
slouken@0
   393
	dB = (((sB-dB)*(A))>>8)+dB;		\
slouken@0
   394
} while(0)
slouken@0
   395
icculus@1162
   396
/* Blend the RGB values of two Pixels based on a source alpha value */
icculus@1047
   397
#define ACCURATE_ALPHA_BLEND(sR, sG, sB, sA, dR, dG, dB)	\
icculus@1047
   398
do {						\
icculus@1047
   399
    unsigned tR, tG, tB, tA; \
icculus@1047
   400
    tA = 255 - sA; \
icculus@1047
   401
    tR = 1 + (sR * sA) + (dR * tA); \
icculus@1047
   402
    dR = (tR + (tR >> 8)) >> 8; \
icculus@1047
   403
    tG = 1 + (sG * sA) + (dG * tA); \
icculus@1047
   404
    dG = (tG + (tG >> 8)) >> 8; \
icculus@1047
   405
    tB = 1 + (sB * sA) + (dB * tA); \
icculus@1047
   406
    dB = (tB + (tB >> 8)) >> 8; \
icculus@1047
   407
} while(0)
icculus@1047
   408
icculus@1047
   409
slouken@0
   410
/* This is a very useful loop for optimizing blitters */
slouken@553
   411
#if defined(_MSC_VER) && (_MSC_VER == 1300)
slouken@553
   412
/* There's a bug in the Visual C++ 7 optimizer when compiling this code */
slouken@553
   413
#else
slouken@0
   414
#define USE_DUFFS_LOOP
slouken@553
   415
#endif
slouken@0
   416
#ifdef USE_DUFFS_LOOP
slouken@0
   417
slouken@0
   418
/* 8-times unrolled loop */
slouken@0
   419
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   420
{ int n = (width+7)/8;							\
slouken@91
   421
	switch (width & 7) {						\
slouken@0
   422
	case 0: do {	pixel_copy_increment;				\
slouken@0
   423
	case 7:		pixel_copy_increment;				\
slouken@0
   424
	case 6:		pixel_copy_increment;				\
slouken@0
   425
	case 5:		pixel_copy_increment;				\
slouken@0
   426
	case 4:		pixel_copy_increment;				\
slouken@0
   427
	case 3:		pixel_copy_increment;				\
slouken@0
   428
	case 2:		pixel_copy_increment;				\
slouken@0
   429
	case 1:		pixel_copy_increment;				\
slouken@0
   430
		} while ( --n > 0 );					\
slouken@0
   431
	}								\
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
/* 4-times unrolled loop */
slouken@0
   435
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   436
{ int n = (width+3)/4;							\
slouken@91
   437
	switch (width & 3) {						\
slouken@0
   438
	case 0: do {	pixel_copy_increment;				\
slouken@0
   439
	case 3:		pixel_copy_increment;				\
slouken@0
   440
	case 2:		pixel_copy_increment;				\
slouken@0
   441
	case 1:		pixel_copy_increment;				\
slouken@0
   442
		} while ( --n > 0 );					\
slouken@0
   443
	}								\
slouken@0
   444
}
slouken@0
   445
slouken@689
   446
/* 2 - times unrolled loop */
slouken@689
   447
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   448
				double_pixel_copy_increment, width)	\
slouken@689
   449
{ int n, w = width;							\
slouken@689
   450
	if( w & 1 ) {							\
slouken@689
   451
	    pixel_copy_increment;					\
slouken@689
   452
	    w--;							\
slouken@689
   453
	}								\
slouken@689
   454
	if ( w > 0 )	{						\
slouken@689
   455
	    n = ( w + 2) / 4;						\
slouken@689
   456
	    switch( w & 2 ) {						\
slouken@689
   457
	    case 0: do {	double_pixel_copy_increment;		\
slouken@689
   458
	    case 2:		double_pixel_copy_increment;		\
slouken@689
   459
		    } while ( --n > 0 );					\
slouken@689
   460
	    }								\
slouken@689
   461
	}								\
slouken@689
   462
}
slouken@689
   463
slouken@689
   464
/* 2 - times unrolled loop 4 pixels */
slouken@689
   465
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   466
				double_pixel_copy_increment,		\
slouken@689
   467
				quatro_pixel_copy_increment, width)	\
slouken@689
   468
{ int n, w = width;								\
slouken@689
   469
        if(w & 1) {							\
slouken@689
   470
	  pixel_copy_increment;						\
slouken@689
   471
	  w--;								\
slouken@689
   472
	}								\
slouken@689
   473
	if(w & 2) {							\
slouken@689
   474
	  double_pixel_copy_increment;					\
slouken@689
   475
	  w -= 2;							\
slouken@689
   476
	}								\
slouken@689
   477
	if ( w > 0 ) {							\
slouken@689
   478
	    n = ( w + 7 ) / 8;						\
slouken@689
   479
	    switch( w & 4 ) {						\
slouken@689
   480
	    case 0: do {	quatro_pixel_copy_increment;		\
slouken@689
   481
	    case 4:		quatro_pixel_copy_increment;		\
slouken@689
   482
		    } while ( --n > 0 );					\
slouken@689
   483
	    }								\
slouken@689
   484
	}								\
slouken@689
   485
}
slouken@689
   486
slouken@0
   487
/* Use the 8-times version of the loop by default */
slouken@0
   488
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   489
	DUFFS_LOOP8(pixel_copy_increment, width)
slouken@0
   490
slouken@0
   491
#else
slouken@0
   492
slouken@0
   493
/* Don't use Duff's device to unroll loops */
slouken@689
   494
#define DUFFS_LOOP_DOUBLE2(pixel_copy_increment,			\
slouken@689
   495
			 double_pixel_copy_increment, width)		\
slouken@689
   496
{ int n = width;								\
slouken@689
   497
    if( n & 1 ) {							\
slouken@689
   498
	pixel_copy_increment;						\
slouken@689
   499
	n--;								\
slouken@689
   500
    }									\
slouken@689
   501
    n=n>>1;								\
slouken@689
   502
    for(; n > 0; --n) {   						\
slouken@689
   503
	double_pixel_copy_increment;					\
slouken@689
   504
    }									\
slouken@689
   505
}
slouken@689
   506
slouken@689
   507
/* Don't use Duff's device to unroll loops */
slouken@689
   508
#define DUFFS_LOOP_QUATRO2(pixel_copy_increment,			\
slouken@689
   509
				double_pixel_copy_increment,		\
slouken@689
   510
				quatro_pixel_copy_increment, width)	\
slouken@689
   511
{ int n = width;								\
slouken@689
   512
        if(n & 1) {							\
slouken@689
   513
	  pixel_copy_increment;						\
slouken@689
   514
	  n--;								\
slouken@689
   515
	}								\
slouken@689
   516
	if(n & 2) {							\
slouken@689
   517
	  double_pixel_copy_increment;					\
slouken@689
   518
	  n -= 2;							\
slouken@689
   519
	}								\
slouken@689
   520
	n=n>>2;								\
slouken@689
   521
	for(; n > 0; --n) {   						\
slouken@689
   522
	  quatro_pixel_copy_increment;					\
slouken@689
   523
        }								\
slouken@689
   524
}
slouken@689
   525
slouken@689
   526
/* Don't use Duff's device to unroll loops */
slouken@0
   527
#define DUFFS_LOOP(pixel_copy_increment, width)				\
slouken@0
   528
{ int n;								\
slouken@0
   529
	for ( n=width; n > 0; --n ) {					\
slouken@0
   530
		pixel_copy_increment;					\
slouken@0
   531
	}								\
slouken@0
   532
}
slouken@0
   533
#define DUFFS_LOOP8(pixel_copy_increment, width)			\
slouken@0
   534
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   535
#define DUFFS_LOOP4(pixel_copy_increment, width)			\
slouken@0
   536
	DUFFS_LOOP(pixel_copy_increment, width)
slouken@0
   537
slouken@0
   538
#endif /* USE_DUFFS_LOOP */
slouken@0
   539
slouken@0
   540
/* Prevent Visual C++ 6.0 from printing out stupid warnings */
slouken@0
   541
#if defined(_MSC_VER) && (_MSC_VER >= 600)
slouken@0
   542
#pragma warning(disable: 4550)
slouken@0
   543
#endif
slouken@0
   544
slouken@0
   545
#endif /* _SDL_blit_h */
slouken@1895
   546
/* vi: set ts=4 sw=4 expandtab: */