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