Skip to content

Latest commit

 

History

History
2437 lines (2276 loc) · 79.2 KB

SDL_blit_N.c

File metadata and controls

2437 lines (2276 loc) · 79.2 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Jan 4, 2004
Jan 4, 2004
3
Copyright (C) 1997-2004 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
22
23
24
25
26
27
28
29
30
31
32
33
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <stdio.h>
#include "SDL_types.h"
#include "SDL_video.h"
#include "SDL_blit.h"
#include "SDL_byteorder.h"
Nov 18, 2003
Nov 18, 2003
34
#include "SDL_cpuinfo.h"
Apr 26, 2001
Apr 26, 2001
35
36
37
/* Functions to blit from N-bit surfaces to other surfaces */
Apr 17, 2005
Apr 17, 2005
38
#ifdef USE_ALTIVEC_BLITTERS
Nov 17, 2005
Nov 17, 2005
39
#ifdef HAVE_ALTIVEC_H
Oct 20, 2005
Oct 20, 2005
40
#include <altivec.h>
Nov 17, 2005
Nov 17, 2005
41
#endif
Apr 17, 2005
Apr 17, 2005
42
#include <assert.h>
Oct 20, 2005
Oct 20, 2005
43
#include <stdlib.h>
Apr 17, 2005
Apr 17, 2005
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifdef MACOSX
#include <sys/sysctl.h>
static size_t GetL3CacheSize( void )
{
const char key[] = "hw.l3cachesize";
u_int64_t result = 0;
size_t typeSize = sizeof( result );
int err = sysctlbyname( key, &result, &typeSize, NULL, 0 );
if( 0 != err ) return 0;
return result;
}
#else
static size_t GetL3CacheSize( void )
{
/* XXX: Just guess G4 */
return 2097152;
}
#endif /* MACOSX */
Oct 20, 2005
Oct 20, 2005
66
67
68
69
70
71
72
73
74
75
76
77
#if ((defined MACOSX) && (__GNUC__ < 4))
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
(vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
(vector unsigned short) ( a,b,c,d,e,f,g,h )
#else
#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
(vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
(vector unsigned short) { a,b,c,d,e,f,g,h }
#endif
Apr 17, 2005
Apr 17, 2005
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
#define VSWIZZLE32(a,b,c,d) (vector unsigned char) \
( 0x00+a, 0x00+b, 0x00+c, 0x00+d, \
0x04+a, 0x04+b, 0x04+c, 0x04+d, \
0x08+a, 0x08+b, 0x08+c, 0x08+d, \
0x0C+a, 0x0C+b, 0x0C+c, 0x0C+d )
#define MAKE8888(dstfmt, r, g, b, a) \
( ((r<<dstfmt->Rshift)&dstfmt->Rmask) | \
((g<<dstfmt->Gshift)&dstfmt->Gmask) | \
((b<<dstfmt->Bshift)&dstfmt->Bmask) | \
((a<<dstfmt->Ashift)&dstfmt->Amask) )
/*
* Data Stream Touch...Altivec cache prefetching.
*
* Don't use this on a G5...however, the speed boost is very significant
* on a G4.
*/
#define DST_CHAN_SRC 1
#define DST_CHAN_DEST 2
/* macro to set DST control word value... */
#define DST_CTRL(size, count, stride) \
(((size) << 24) | ((count) << 16) | (stride))
#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
? vec_lvsl(0, src) \
: vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
/* Calculate the permute vector used for 32->32 swizzling */
static vector unsigned char calc_swizzle32(const SDL_PixelFormat *srcfmt,
const SDL_PixelFormat *dstfmt)
{
/*
* We have to assume that the bits that aren't used by other
* colors is alpha, and it's one complete byte, since some formats
* leave alpha with a zero mask, but we should still swizzle the bits.
*/
/* ARGB */
const static struct SDL_PixelFormat default_pixel_format = {
NULL, 0, 0,
0, 0, 0, 0,
16, 8, 0, 24,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000,
0, 0};
if (!srcfmt) {
srcfmt = &default_pixel_format;
}
if (!dstfmt) {
dstfmt = &default_pixel_format;
}
Oct 20, 2005
Oct 20, 2005
130
131
vector unsigned char plus = VECUINT8_LITERAL(
0x00, 0x00, 0x00, 0x00,
Apr 17, 2005
Apr 17, 2005
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
0x04, 0x04, 0x04, 0x04,
0x08, 0x08, 0x08, 0x08,
0x0C, 0x0C, 0x0C, 0x0C );
vector unsigned char vswiz;
vector unsigned int srcvec;
#define RESHIFT(X) (3 - ((X) >> 3))
Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
Uint32 amask;
/* Use zero for alpha if either surface doesn't have alpha */
if (dstfmt->Amask) {
amask = ((srcfmt->Amask) ? RESHIFT(srcfmt->Ashift) : 0x10) << (dstfmt->Ashift);
} else {
amask = 0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^ 0xFFFFFFFF);
}
#undef RESHIFT
Oct 20, 2005
Oct 20, 2005
149
((unsigned int *)(char*)&srcvec)[0] = (rmask | gmask | bmask | amask);
Apr 17, 2005
Apr 17, 2005
150
151
152
153
154
155
156
157
158
159
160
161
162
163
vswiz = vec_add(plus, (vector unsigned char)vec_splat(srcvec, 0));
return(vswiz);
}
static void Blit_RGB888_RGB565(SDL_BlitInfo *info);
static void Blit_RGB888_RGB565Altivec(SDL_BlitInfo *info) {
int height = info->d_height;
Uint8 *src = (Uint8 *) info->s_pixels;
int srcskip = info->s_skip;
Uint8 *dst = (Uint8 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
vector unsigned char valpha = vec_splat_u8(0);
vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
Oct 20, 2005
Oct 20, 2005
164
vector unsigned char vgmerge = VECUINT8_LITERAL(
Apr 17, 2005
Apr 17, 2005
165
166
167
168
169
170
0x00, 0x02, 0x00, 0x06,
0x00, 0x0a, 0x00, 0x0e,
0x00, 0x12, 0x00, 0x16,
0x00, 0x1a, 0x00, 0x1e);
vector unsigned short v1 = vec_splat_u16(1);
vector unsigned short v3 = vec_splat_u16(3);
Oct 20, 2005
Oct 20, 2005
171
vector unsigned short v3f = VECUINT16_LITERAL(
Apr 17, 2005
Apr 17, 2005
172
173
0x003f, 0x003f, 0x003f, 0x003f,
0x003f, 0x003f, 0x003f, 0x003f);
Oct 20, 2005
Oct 20, 2005
174
vector unsigned short vfc = VECUINT16_LITERAL(
Apr 17, 2005
Apr 17, 2005
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
0x00fc, 0x00fc, 0x00fc, 0x00fc,
0x00fc, 0x00fc, 0x00fc, 0x00fc);
vector unsigned short vf800 = (vector unsigned short)vec_splat_u8(-7);
vf800 = vec_sl(vf800, vec_splat_u16(8));
while (height--) {
vector unsigned char valigner;
vector unsigned char voverflow;
vector unsigned char vsrc;
int width = info->d_width;
int extrawidth;
/* do scalar until we can align... */
#define ONE_PIXEL_BLEND(condition, widthvar) \
while (condition) { \
Oct 20, 2005
Oct 20, 2005
191
Uint32 Pixel; \
Apr 17, 2005
Apr 17, 2005
192
unsigned sR, sG, sB, sA; \
Oct 20, 2005
Oct 20, 2005
193
DISEMBLE_RGBA((Uint8 *)src, 4, srcfmt, Pixel, \
Apr 17, 2005
Apr 17, 2005
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
sR, sG, sB, sA); \
*(Uint16 *)(dst) = (((sR << 8) & 0x0000F800) | \
((sG << 3) & 0x000007E0) | \
((sB >> 3) & 0x0000001F)); \
dst += 2; \
src += 4; \
widthvar--; \
}
ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
/* After all that work, here's the vector part! */
extrawidth = (width % 8); /* trailing unaligned stores */
width -= extrawidth;
vsrc = vec_ld(0, src);
valigner = VEC_ALIGNER(src);
while (width) {
vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
vector unsigned int vsrc1, vsrc2;
vector unsigned char vdst;
voverflow = vec_ld(15, src);
vsrc = vec_perm(vsrc, voverflow, valigner);
vsrc1 = (vector unsigned int)vec_perm(vsrc, valpha, vpermute);
src += 16;
vsrc = voverflow;
voverflow = vec_ld(15, src);
vsrc = vec_perm(vsrc, voverflow, valigner);
vsrc2 = (vector unsigned int)vec_perm(vsrc, valpha, vpermute);
/* 1555 */
vpixel = (vector unsigned short)vec_packpx(vsrc1, vsrc2);
vgpixel = (vector unsigned short)vec_perm(vsrc1, vsrc2, vgmerge);
vgpixel = vec_and(vgpixel, vfc);
vgpixel = vec_sl(vgpixel, v3);
vrpixel = vec_sl(vpixel, v1);
vrpixel = vec_and(vrpixel, vf800);
vbpixel = vec_and(vpixel, v3f);
vdst = vec_or((vector unsigned char)vrpixel, (vector unsigned char)vgpixel);
/* 565 */
vdst = vec_or(vdst, (vector unsigned char)vbpixel);
vec_st(vdst, 0, dst);
width -= 8;
src += 16;
dst += 16;
vsrc = voverflow;
}
assert(width == 0);
/* do scalar until we can align... */
ONE_PIXEL_BLEND((extrawidth), extrawidth);
#undef ONE_PIXEL_BLEND
src += srcskip; /* move to next row, accounting for pitch. */
dst += dstskip;
}
}
static void Blit_RGB565_32Altivec(SDL_BlitInfo *info) {
int height = info->d_height;
Uint8 *src = (Uint8 *) info->s_pixels;
int srcskip = info->s_skip;
Uint8 *dst = (Uint8 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
SDL_PixelFormat *dstfmt = info->dst;
unsigned alpha;
vector unsigned char valpha;
vector unsigned char vpermute;
vector unsigned short vf800;
vector unsigned int v8 = vec_splat_u32(8);
vector unsigned int v16 = vec_add(v8, v8);
vector unsigned short v2 = vec_splat_u16(2);
vector unsigned short v3 = vec_splat_u16(3);
/*
0x10 - 0x1f is the alpha
0x00 - 0x0e evens are the red
0x01 - 0x0f odds are zero
*/
Oct 20, 2005
Oct 20, 2005
278
vector unsigned char vredalpha1 = VECUINT8_LITERAL(
Apr 17, 2005
Apr 17, 2005
279
280
281
282
283
0x10, 0x00, 0x01, 0x01,
0x10, 0x02, 0x01, 0x01,
0x10, 0x04, 0x01, 0x01,
0x10, 0x06, 0x01, 0x01
);
Oct 20, 2005
Oct 20, 2005
284
vector unsigned char vredalpha2 = (vector unsigned char) (
Apr 17, 2005
Apr 17, 2005
285
286
287
288
289
290
vec_add((vector unsigned int)vredalpha1, vec_sl(v8, v16))
);
/*
0x00 - 0x0f is ARxx ARxx ARxx ARxx
0x11 - 0x0f odds are blue
*/
Oct 20, 2005
Oct 20, 2005
291
vector unsigned char vblue1 = VECUINT8_LITERAL(
Apr 17, 2005
Apr 17, 2005
292
293
294
295
296
297
298
299
300
301
302
303
0x00, 0x01, 0x02, 0x11,
0x04, 0x05, 0x06, 0x13,
0x08, 0x09, 0x0a, 0x15,
0x0c, 0x0d, 0x0e, 0x17
);
vector unsigned char vblue2 = (vector unsigned char)(
vec_add((vector unsigned int)vblue1, v8)
);
/*
0x00 - 0x0f is ARxB ARxB ARxB ARxB
0x10 - 0x0e evens are green
*/
Oct 20, 2005
Oct 20, 2005
304
vector unsigned char vgreen1 = VECUINT8_LITERAL(
Apr 17, 2005
Apr 17, 2005
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
0x00, 0x01, 0x10, 0x03,
0x04, 0x05, 0x12, 0x07,
0x08, 0x09, 0x14, 0x0b,
0x0c, 0x0d, 0x16, 0x0f
);
vector unsigned char vgreen2 = (vector unsigned char)(
vec_add((vector unsigned int)vgreen1, vec_sl(v8, v8))
);
assert(srcfmt->BytesPerPixel == 2);
assert(dstfmt->BytesPerPixel == 4);
vf800 = (vector unsigned short)vec_splat_u8(-7);
vf800 = vec_sl(vf800, vec_splat_u16(8));
if (dstfmt->Amask && srcfmt->alpha) {
((unsigned char *)&valpha)[0] = alpha = srcfmt->alpha;
valpha = vec_splat(valpha, 0);
} else {
alpha = 0;
valpha = vec_splat_u8(0);
}
vpermute = calc_swizzle32(NULL, dstfmt);
while (height--) {
vector unsigned char valigner;
vector unsigned char voverflow;
vector unsigned char vsrc;
int width = info->d_width;
int extrawidth;
/* do scalar until we can align... */
#define ONE_PIXEL_BLEND(condition, widthvar) \
while (condition) { \
unsigned sR, sG, sB; \
Oct 20, 2005
Oct 20, 2005
342
343
344
345
unsigned short Pixel = *((unsigned short *)src); \
sR = (Pixel >> 8) & 0xf8; \
sG = (Pixel >> 3) & 0xfc; \
sB = (Pixel << 3) & 0xf8; \
Apr 17, 2005
Apr 17, 2005
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
ASSEMBLE_RGBA(dst, 4, dstfmt, sR, sG, sB, alpha); \
src += 2; \
dst += 4; \
widthvar--; \
}
ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
/* After all that work, here's the vector part! */
extrawidth = (width % 8); /* trailing unaligned stores */
width -= extrawidth;
vsrc = vec_ld(0, src);
valigner = VEC_ALIGNER(src);
while (width) {
vector unsigned short vR, vG, vB;
vector unsigned char vdst1, vdst2;
voverflow = vec_ld(15, src);
vsrc = vec_perm(vsrc, voverflow, valigner);
vR = vec_and((vector unsigned short)vsrc, vf800);
vB = vec_sl((vector unsigned short)vsrc, v3);
vG = vec_sl(vB, v2);
vdst1 = (vector unsigned char)vec_perm((vector unsigned char)vR, valpha, vredalpha1);
vdst1 = vec_perm(vdst1, (vector unsigned char)vB, vblue1);
vdst1 = vec_perm(vdst1, (vector unsigned char)vG, vgreen1);
vdst1 = vec_perm(vdst1, valpha, vpermute);
vec_st(vdst1, 0, dst);
vdst2 = (vector unsigned char)vec_perm((vector unsigned char)vR, valpha, vredalpha2);
vdst2 = vec_perm(vdst2, (vector unsigned char)vB, vblue2);
vdst2 = vec_perm(vdst2, (vector unsigned char)vG, vgreen2);
vdst2 = vec_perm(vdst2, valpha, vpermute);
vec_st(vdst2, 16, dst);
width -= 8;
dst += 32;
src += 16;
vsrc = voverflow;
}
assert(width == 0);
/* do scalar until we can align... */
ONE_PIXEL_BLEND((extrawidth), extrawidth);
#undef ONE_PIXEL_BLEND
src += srcskip; /* move to next row, accounting for pitch. */
dst += dstskip;
}
}
Sep 8, 2005
Sep 8, 2005
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
static void Blit_RGB555_32Altivec(SDL_BlitInfo *info) {
int height = info->d_height;
Uint8 *src = (Uint8 *) info->s_pixels;
int srcskip = info->s_skip;
Uint8 *dst = (Uint8 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
SDL_PixelFormat *dstfmt = info->dst;
unsigned alpha;
vector unsigned char valpha;
vector unsigned char vpermute;
vector unsigned short vf800;
vector unsigned int v8 = vec_splat_u32(8);
vector unsigned int v16 = vec_add(v8, v8);
vector unsigned short v1 = vec_splat_u16(1);
vector unsigned short v3 = vec_splat_u16(3);
/*
0x10 - 0x1f is the alpha
0x00 - 0x0e evens are the red
0x01 - 0x0f odds are zero
*/
Oct 20, 2005
Oct 20, 2005
423
vector unsigned char vredalpha1 = VECUINT8_LITERAL(
Sep 8, 2005
Sep 8, 2005
424
425
426
427
428
429
430
431
432
433
434
435
0x10, 0x00, 0x01, 0x01,
0x10, 0x02, 0x01, 0x01,
0x10, 0x04, 0x01, 0x01,
0x10, 0x06, 0x01, 0x01
);
vector unsigned char vredalpha2 = (vector unsigned char)(
vec_add((vector unsigned int)vredalpha1, vec_sl(v8, v16))
);
/*
0x00 - 0x0f is ARxx ARxx ARxx ARxx
0x11 - 0x0f odds are blue
*/
Oct 20, 2005
Oct 20, 2005
436
vector unsigned char vblue1 = VECUINT8_LITERAL(
Sep 8, 2005
Sep 8, 2005
437
438
439
440
441
442
443
444
445
446
447
448
0x00, 0x01, 0x02, 0x11,
0x04, 0x05, 0x06, 0x13,
0x08, 0x09, 0x0a, 0x15,
0x0c, 0x0d, 0x0e, 0x17
);
vector unsigned char vblue2 = (vector unsigned char)(
vec_add((vector unsigned int)vblue1, v8)
);
/*
0x00 - 0x0f is ARxB ARxB ARxB ARxB
0x10 - 0x0e evens are green
*/
Oct 20, 2005
Oct 20, 2005
449
vector unsigned char vgreen1 = VECUINT8_LITERAL(
Sep 8, 2005
Sep 8, 2005
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
0x00, 0x01, 0x10, 0x03,
0x04, 0x05, 0x12, 0x07,
0x08, 0x09, 0x14, 0x0b,
0x0c, 0x0d, 0x16, 0x0f
);
vector unsigned char vgreen2 = (vector unsigned char)(
vec_add((vector unsigned int)vgreen1, vec_sl(v8, v8))
);
assert(srcfmt->BytesPerPixel == 2);
assert(dstfmt->BytesPerPixel == 4);
vf800 = (vector unsigned short)vec_splat_u8(-7);
vf800 = vec_sl(vf800, vec_splat_u16(8));
if (dstfmt->Amask && srcfmt->alpha) {
((unsigned char *)&valpha)[0] = alpha = srcfmt->alpha;
valpha = vec_splat(valpha, 0);
} else {
alpha = 0;
valpha = vec_splat_u8(0);
}
vpermute = calc_swizzle32(NULL, dstfmt);
while (height--) {
vector unsigned char valigner;
vector unsigned char voverflow;
vector unsigned char vsrc;
int width = info->d_width;
int extrawidth;
/* do scalar until we can align... */
#define ONE_PIXEL_BLEND(condition, widthvar) \
while (condition) { \
unsigned sR, sG, sB; \
Oct 20, 2005
Oct 20, 2005
487
488
489
490
unsigned short Pixel = *((unsigned short *)src); \
sR = (Pixel >> 7) & 0xf8; \
sG = (Pixel >> 2) & 0xf8; \
sB = (Pixel << 3) & 0xf8; \
Sep 8, 2005
Sep 8, 2005
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
ASSEMBLE_RGBA(dst, 4, dstfmt, sR, sG, sB, alpha); \
src += 2; \
dst += 4; \
widthvar--; \
}
ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
/* After all that work, here's the vector part! */
extrawidth = (width % 8); /* trailing unaligned stores */
width -= extrawidth;
vsrc = vec_ld(0, src);
valigner = VEC_ALIGNER(src);
while (width) {
vector unsigned short vR, vG, vB;
vector unsigned char vdst1, vdst2;
voverflow = vec_ld(15, src);
vsrc = vec_perm(vsrc, voverflow, valigner);
vR = vec_and(vec_sl((vector unsigned short)vsrc,v1), vf800);
vB = vec_sl((vector unsigned short)vsrc, v3);
vG = vec_sl(vB, v3);
vdst1 = (vector unsigned char)vec_perm((vector unsigned char)vR, valpha, vredalpha1);
vdst1 = vec_perm(vdst1, (vector unsigned char)vB, vblue1);
vdst1 = vec_perm(vdst1, (vector unsigned char)vG, vgreen1);
vdst1 = vec_perm(vdst1, valpha, vpermute);
vec_st(vdst1, 0, dst);
vdst2 = (vector unsigned char)vec_perm((vector unsigned char)vR, valpha, vredalpha2);
vdst2 = vec_perm(vdst2, (vector unsigned char)vB, vblue2);
vdst2 = vec_perm(vdst2, (vector unsigned char)vG, vgreen2);
vdst2 = vec_perm(vdst2, valpha, vpermute);
vec_st(vdst2, 16, dst);
width -= 8;
dst += 32;
src += 16;
vsrc = voverflow;
}
assert(width == 0);
/* do scalar until we can align... */
ONE_PIXEL_BLEND((extrawidth), extrawidth);
#undef ONE_PIXEL_BLEND
src += srcskip; /* move to next row, accounting for pitch. */
dst += dstskip;
}
}
Apr 17, 2005
Apr 17, 2005
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
static void BlitNtoNKey(SDL_BlitInfo *info);
static void BlitNtoNKeyCopyAlpha(SDL_BlitInfo *info);
static void Blit32to32KeyAltivec(SDL_BlitInfo *info)
{
int height = info->d_height;
Uint32 *srcp = (Uint32 *) info->s_pixels;
int srcskip = info->s_skip;
Uint32 *dstp = (Uint32 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
int srcbpp = srcfmt->BytesPerPixel;
SDL_PixelFormat *dstfmt = info->dst;
int dstbpp = dstfmt->BytesPerPixel;
int copy_alpha = (srcfmt->Amask && dstfmt->Amask);
unsigned alpha = dstfmt->Amask ? srcfmt->alpha : 0;
Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
Uint32 ckey = info->src->colorkey;
vector unsigned int valpha;
vector unsigned char vpermute;
vector unsigned char vzero;
vector unsigned int vckey;
vector unsigned int vrgbmask;
vpermute = calc_swizzle32(srcfmt, dstfmt);
if (info->d_width < 16) {
if(copy_alpha) {
return BlitNtoNKeyCopyAlpha(info);
} else {
return BlitNtoNKey(info);
}
}
vzero = vec_splat_u8(0);
if (alpha) {
((unsigned char *)&valpha)[0] = (unsigned char)alpha;
valpha = (vector unsigned int)vec_splat((vector unsigned char)valpha, 0);
} else {
valpha = (vector unsigned int)vzero;
}
ckey &= rgbmask;
Oct 20, 2005
Oct 20, 2005
584
((unsigned int *)(char*)&vckey)[0] = ckey;
Apr 17, 2005
Apr 17, 2005
585
vckey = vec_splat(vckey, 0);
Oct 20, 2005
Oct 20, 2005
586
((unsigned int *)(char*)&vrgbmask)[0] = rgbmask;
Apr 17, 2005
Apr 17, 2005
587
588
589
590
591
592
vrgbmask = vec_splat(vrgbmask, 0);
while (height--) {
#define ONE_PIXEL_BLEND(condition, widthvar) \
if (copy_alpha) { \
while (condition) { \
Oct 20, 2005
Oct 20, 2005
593
Uint32 Pixel; \
Apr 17, 2005
Apr 17, 2005
594
unsigned sR, sG, sB, sA; \
Oct 20, 2005
Oct 20, 2005
595
DISEMBLE_RGBA((Uint8 *)srcp, srcbpp, srcfmt, Pixel, \
Apr 17, 2005
Apr 17, 2005
596
sR, sG, sB, sA); \
Oct 20, 2005
Oct 20, 2005
597
if ( (Pixel & rgbmask) != ckey ) { \
Apr 17, 2005
Apr 17, 2005
598
599
600
ASSEMBLE_RGBA((Uint8 *)dstp, dstbpp, dstfmt, \
sR, sG, sB, sA); \
} \
Oct 20, 2005
Oct 20, 2005
601
602
dstp = (Uint32 *) (((Uint8 *) dstp) + dstbpp); \
srcp = (Uint32 *) (((Uint8 *) srcp) + srcbpp); \
Apr 17, 2005
Apr 17, 2005
603
604
605
606
widthvar--; \
} \
} else { \
while (condition) { \
Oct 20, 2005
Oct 20, 2005
607
Uint32 Pixel; \
Apr 17, 2005
Apr 17, 2005
608
unsigned sR, sG, sB; \
Oct 20, 2005
Oct 20, 2005
609
610
611
RETRIEVE_RGB_PIXEL((Uint8 *)srcp, srcbpp, Pixel); \
if ( Pixel != ckey ) { \
RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
Apr 17, 2005
Apr 17, 2005
612
613
614
ASSEMBLE_RGBA((Uint8 *)dstp, dstbpp, dstfmt, \
sR, sG, sB, alpha); \
} \
Oct 20, 2005
Oct 20, 2005
615
616
dstp = (Uint32 *) (((Uint8 *)dstp) + dstbpp); \
srcp = (Uint32 *) (((Uint8 *)srcp) + srcbpp); \
Apr 17, 2005
Apr 17, 2005
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
widthvar--; \
} \
}
int width = info->d_width;
ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
assert(width > 0);
if (width > 0) {
int extrawidth = (width % 4);
vector unsigned char valigner = VEC_ALIGNER(srcp);
vector unsigned int vs = vec_ld(0, srcp);
width -= extrawidth;
assert(width >= 4);
while (width) {
vector unsigned char vsel;
vector unsigned int vd;
vector unsigned int voverflow = vec_ld(15, srcp);
/* load the source vec */
vs = vec_perm(vs, voverflow, valigner);
/* vsel is set for items that match the key */
vsel = (vector unsigned char)vec_and(vs, vrgbmask);
vsel = (vector unsigned char)vec_cmpeq(vs, vckey);
/* permute the src vec to the dest format */
vs = vec_perm(vs, valpha, vpermute);
/* load the destination vec */
vd = vec_ld(0, dstp);
/* select the source and dest into vs */
vd = (vector unsigned int)vec_sel((vector unsigned char)vs, (vector unsigned char)vd, vsel);
vec_st(vd, 0, dstp);
srcp += 4;
width -= 4;
dstp += 4;
vs = voverflow;
}
ONE_PIXEL_BLEND((extrawidth), extrawidth);
#undef ONE_PIXEL_BLEND
srcp += srcskip >> 2;
dstp += dstskip >> 2;
}
}
}
/* Altivec code to swizzle one 32-bit surface to a different 32-bit format. */
/* Use this on a G5 */
static void ConvertAltivec32to32_noprefetch(SDL_BlitInfo *info)
{
int height = info->d_height;
Uint32 *src = (Uint32 *) info->s_pixels;
int srcskip = info->s_skip;
Uint32 *dst = (Uint32 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
int srcbpp = srcfmt->BytesPerPixel;
SDL_PixelFormat *dstfmt = info->dst;
int dstbpp = dstfmt->BytesPerPixel;
vector unsigned int vzero = vec_splat_u32(0);
vector unsigned char vpermute = calc_swizzle32(srcfmt, dstfmt);
if (dstfmt->Amask && !srcfmt->Amask) {
if (srcfmt->alpha) {
vector unsigned char valpha;
((unsigned char *)&valpha)[0] = srcfmt->alpha;
vzero = (vector unsigned int)vec_splat(valpha, 0);
}
}
assert(srcbpp == 4);
assert(dstbpp == 4);
while (height--) {
vector unsigned char valigner;
vector unsigned int vbits;
vector unsigned int voverflow;
Uint32 bits;
Uint8 r, g, b, a;
int width = info->d_width;
int extrawidth;
/* do scalar until we can align... */
while ((UNALIGNED_PTR(dst)) && (width)) {
bits = *(src++);
RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
*(dst++) = MAKE8888(dstfmt, r, g, b, a);
width--;
}
/* After all that work, here's the vector part! */
extrawidth = (width % 4);
width -= extrawidth;
valigner = VEC_ALIGNER(src);
vbits = vec_ld(0, src);
while (width) {
voverflow = vec_ld(15, src);
src += 4;
width -= 4;
vbits = vec_perm(vbits, voverflow, valigner); /* src is ready. */
vbits = vec_perm(vbits, vzero, vpermute); /* swizzle it. */
vec_st(vbits, 0, dst); /* store it back out. */
dst += 4;
vbits = voverflow;
}
assert(width == 0);
/* cover pixels at the end of the row that didn't fit in 16 bytes. */
while (extrawidth) {
bits = *(src++); /* max 7 pixels, don't bother with prefetch. */
RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
*(dst++) = MAKE8888(dstfmt, r, g, b, a);
extrawidth--;
}
src += srcskip >> 2; /* move to next row, accounting for pitch. */
dst += dstskip >> 2;
}
}
/* Altivec code to swizzle one 32-bit surface to a different 32-bit format. */
/* Use this on a G4 */
static void ConvertAltivec32to32_prefetch(SDL_BlitInfo *info)
{
const int scalar_dst_lead = sizeof (Uint32) * 4;
const int vector_dst_lead = sizeof (Uint32) * 16;
int height = info->d_height;
Uint32 *src = (Uint32 *) info->s_pixels;
int srcskip = info->s_skip;
Uint32 *dst = (Uint32 *) info->d_pixels;
int dstskip = info->d_skip;
SDL_PixelFormat *srcfmt = info->src;
int srcbpp = srcfmt->BytesPerPixel;
SDL_PixelFormat *dstfmt = info->dst;
int dstbpp = dstfmt->BytesPerPixel;
vector unsigned int vzero = vec_splat_u32(0);
vector unsigned char vpermute = calc_swizzle32(srcfmt, dstfmt);
if (dstfmt->Amask && !srcfmt->Amask) {
if (srcfmt->alpha) {
vector unsigned char valpha;
((unsigned char *)&valpha)[0] = srcfmt->alpha;
vzero = (vector unsigned int)vec_splat(valpha, 0);
}
}
assert(srcbpp == 4);
assert(dstbpp == 4);
while (height--) {
vector unsigned char valigner;
vector unsigned int vbits;
vector unsigned int voverflow;
Uint32 bits;
Uint8 r, g, b, a;
int width = info->d_width;
int extrawidth;
/* do scalar until we can align... */
while ((UNALIGNED_PTR(dst)) && (width)) {
vec_dstt(src+scalar_dst_lead, DST_CTRL(2,32,1024), DST_CHAN_SRC);
vec_dstst(dst+scalar_dst_lead, DST_CTRL(2,32,1024), DST_CHAN_DEST);
bits = *(src++);
RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
*(dst++) = MAKE8888(dstfmt, r, g, b, a);
width--;
}
/* After all that work, here's the vector part! */
extrawidth = (width % 4);
width -= extrawidth;
valigner = VEC_ALIGNER(src);
vbits = vec_ld(0, src);
while (width) {
vec_dstt(src+vector_dst_lead, DST_CTRL(2,32,1024), DST_CHAN_SRC);
vec_dstst(dst+vector_dst_lead, DST_CTRL(2,32,1024), DST_CHAN_DEST);
voverflow = vec_ld(15, src);
src += 4;
width -= 4;
vbits = vec_perm(vbits, voverflow, valigner); /* src is ready. */
vbits = vec_perm(vbits, vzero, vpermute); /* swizzle it. */
vec_st(vbits, 0, dst); /* store it back out. */
dst += 4;
vbits = voverflow;
}
assert(width == 0);
/* cover pixels at the end of the row that didn't fit in 16 bytes. */
while (extrawidth) {
bits = *(src++); /* max 7 pixels, don't bother with prefetch. */
RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
*(dst++) = MAKE8888(dstfmt, r, g, b, a);
extrawidth--;
}
src += srcskip >> 2; /* move to next row, accounting for pitch. */
dst += dstskip >> 2;
}
vec_dss(DST_CHAN_SRC);
vec_dss(DST_CHAN_DEST);
}
static Uint32 GetBlitFeatures( void )
{
static Uint32 features = 0xffffffff;
if (features == 0xffffffff) {
/* Provide an override for testing .. */
char *override = getenv("SDL_ALTIVEC_BLIT_FEATURES");
if (override) {
features = 0;
sscanf(override, "%u", &features);
} else {
features = ( 0
/* Feature 1 is has-MMX */
| ((SDL_HasMMX()) ? 1 : 0)
/* Feature 2 is has-AltiVec */
| ((SDL_HasAltiVec()) ? 2 : 0)
/* Feature 4 is dont-use-prefetch */
Oct 20, 2005
Oct 20, 2005
838
/* !!!! FIXME: Check for G5 or later, not the cache size! Always prefetch on a G4. */
Apr 17, 2005
Apr 17, 2005
839
840
841
842
843
844
845
846
847
848
849
| ((GetL3CacheSize() == 0) ? 4 : 0)
);
}
}
return features;
}
#else
/* Feature 1 is has-MMX */
#define GetBlitFeatures() ((Uint32)(SDL_HasMMX() ? 1 : 0))
#endif
Apr 20, 2005
Apr 20, 2005
850
851
852
853
854
855
856
857
858
/* This is now endian dependent */
#if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
#define HI 1
#define LO 0
#else /* ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) */
#define HI 0
#define LO 1
#endif
Apr 26, 2001
Apr 26, 2001
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
#ifdef USE_ASMBLIT
/* Heheheh, we coerce Hermes into using SDL blit information */
#define X86_ASSEMBLER
#define HermesConverterInterface SDL_BlitInfo
#define HermesClearInterface void
#define STACKCALL
#include "HeadMMX.h"
#include "HeadX86.h"
#else
/* Special optimized blit for RGB 8-8-8 --> RGB 3-3-2 */
#define RGB888_RGB332(dst, src) { \
dst = (((src)&0x00E00000)>>16)| \
(((src)&0x0000E000)>>11)| \
(((src)&0x000000C0)>>6); \
}
static void Blit_RGB888_index8(SDL_BlitInfo *info)
{
#ifndef USE_DUFFS_LOOP
int c;
#endif
int width, height;
Uint32 *src;
Jul 7, 2001
Jul 7, 2001
885
886
const Uint8 *map;
Uint8 *dst;
Apr 26, 2001
Apr 26, 2001
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
int srcskip, dstskip;
/* Set up some basic variables */
width = info->d_width;
height = info->d_height;
src = (Uint32 *)info->s_pixels;
srcskip = info->s_skip/4;
dst = info->d_pixels;
dstskip = info->d_skip;
map = info->table;
if ( map == NULL ) {
while ( height-- ) {
#ifdef USE_DUFFS_LOOP
DUFFS_LOOP(
RGB888_RGB332(*dst++, *src);
, width);
#else
for ( c=width/4; c; --c ) {
/* Pack RGB into 8bit pixel */
++src;
RGB888_RGB332(*dst++, *src);
++src;
RGB888_RGB332(*dst++, *src);
++src;
RGB888_RGB332(*dst++, *src);
++src;
}
Jul 7, 2001
Jul 7, 2001
915
switch ( width & 3 ) {
Apr 26, 2001
Apr 26, 2001
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
case 3:
RGB888_RGB332(*dst++, *src);
++src;
case 2:
RGB888_RGB332(*dst++, *src);
++src;
case 1:
RGB888_RGB332(*dst++, *src);
++src;
}
#endif /* USE_DUFFS_LOOP */
src += srcskip;
dst += dstskip;
}
} else {
Oct 20, 2005
Oct 20, 2005
931
int Pixel;
Apr 26, 2001
Apr 26, 2001
932
933
934
935
while ( height-- ) {
#ifdef USE_DUFFS_LOOP
DUFFS_LOOP(
Oct 20, 2005
Oct 20, 2005
936
937
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
938
939
940
941
942
++src;
, width);
#else
for ( c=width/4; c; --c ) {
/* Pack RGB into 8bit pixel */
Oct 20, 2005
Oct 20, 2005
943
944
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
945
++src;
Oct 20, 2005
Oct 20, 2005
946
947
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
948
++src;
Oct 20, 2005
Oct 20, 2005
949
950
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
951
++src;
Oct 20, 2005
Oct 20, 2005
952
953
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
954
955
++src;
}
Jul 7, 2001
Jul 7, 2001
956
switch ( width & 3 ) {
Apr 26, 2001
Apr 26, 2001
957
case 3:
Oct 20, 2005
Oct 20, 2005
958
959
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
960
961
++src;
case 2:
Oct 20, 2005
Oct 20, 2005
962
963
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
964
965
++src;
case 1:
Oct 20, 2005
Oct 20, 2005
966
967
RGB888_RGB332(Pixel, *src);
*dst++ = map[Pixel];
Apr 26, 2001
Apr 26, 2001
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
++src;
}
#endif /* USE_DUFFS_LOOP */
src += srcskip;
dst += dstskip;
}
}
}
/* Special optimized blit for RGB 8-8-8 --> RGB 5-5-5 */
#define RGB888_RGB555(dst, src) { \
*(Uint16 *)(dst) = (((*src)&0x00F80000)>>9)| \
(((*src)&0x0000F800)>>6)| \
(((*src)&0x000000F8)>>3); \
}
#define RGB888_RGB555_TWO(dst, src) { \
*(Uint32 *)(dst) = (((((src[HI])&0x00F80000)>>9)| \
(((src[HI])&0x0000F800)>>6)| \
(((src[HI])&0x000000F8)>>3))<<16)| \
(((src[LO])&0x00F80000)>>9)| \
(((src[LO])&0x0000F800)>>6)| \
(((src[LO])&0x000000F8)>>3); \
}
static void Blit_RGB888_RGB555(SDL_BlitInfo *info)
{
#ifndef USE_DUFFS_LOOP
int c;
#endif
int width, height;
Uint32 *src;
Uint16 *dst;
int srcskip, dstskip;
/* Set up some basic variables */