src/video/SDL_blit_N.c
changeset 5389 24903690f48a
parent 5262 b530ef003506
child 5407 40c9d744e595
     1.1 --- a/src/video/SDL_blit_N.c	Mon Feb 21 23:45:48 2011 -0800
     1.2 +++ b/src/video/SDL_blit_N.c	Tue Feb 22 21:44:36 2011 -0800
     1.3 @@ -28,8 +28,840 @@
     1.4  
     1.5  /* Functions to blit from N-bit surfaces to other surfaces */
     1.6  
     1.7 +#if SDL_ALTIVEC_BLITTERS
     1.8 +#define assert(X)
     1.9 +#ifdef __MACOSX__
    1.10 +#include <sys/sysctl.h>
    1.11 +static size_t
    1.12 +GetL3CacheSize(void)
    1.13 +{
    1.14 +    const char key[] = "hw.l3cachesize";
    1.15 +    u_int64_t result = 0;
    1.16 +    size_t typeSize = sizeof(result);
    1.17 +
    1.18 +
    1.19 +    int err = sysctlbyname(key, &result, &typeSize, NULL, 0);
    1.20 +    if (0 != err)
    1.21 +        return 0;
    1.22 +
    1.23 +    return result;
    1.24 +}
    1.25 +#else
    1.26 +static size_t
    1.27 +GetL3CacheSize(void)
    1.28 +{
    1.29 +    /* XXX: Just guess G4 */
    1.30 +    return 2097152;
    1.31 +}
    1.32 +#endif /* __MACOSX__ */
    1.33 +
    1.34 +#if (defined(__MACOSX__) && (__GNUC__ < 4))
    1.35 +#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
    1.36 +        (vector unsigned char) ( a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p )
    1.37 +#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
    1.38 +        (vector unsigned short) ( a,b,c,d,e,f,g,h )
    1.39 +#else
    1.40 +#define VECUINT8_LITERAL(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p) \
    1.41 +        (vector unsigned char) { a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p }
    1.42 +#define VECUINT16_LITERAL(a,b,c,d,e,f,g,h) \
    1.43 +        (vector unsigned short) { a,b,c,d,e,f,g,h }
    1.44 +#endif
    1.45 +
    1.46 +#define UNALIGNED_PTR(x) (((size_t) x) & 0x0000000F)
    1.47 +#define VSWIZZLE32(a,b,c,d) (vector unsigned char) \
    1.48 +                               ( 0x00+a, 0x00+b, 0x00+c, 0x00+d, \
    1.49 +                                 0x04+a, 0x04+b, 0x04+c, 0x04+d, \
    1.50 +                                 0x08+a, 0x08+b, 0x08+c, 0x08+d, \
    1.51 +                                 0x0C+a, 0x0C+b, 0x0C+c, 0x0C+d )
    1.52 +
    1.53 +#define MAKE8888(dstfmt, r, g, b, a)  \
    1.54 +    ( ((r<<dstfmt->Rshift)&dstfmt->Rmask) | \
    1.55 +      ((g<<dstfmt->Gshift)&dstfmt->Gmask) | \
    1.56 +      ((b<<dstfmt->Bshift)&dstfmt->Bmask) | \
    1.57 +      ((a<<dstfmt->Ashift)&dstfmt->Amask) )
    1.58 +
    1.59 +/*
    1.60 + * Data Stream Touch...Altivec cache prefetching.
    1.61 + *
    1.62 + *  Don't use this on a G5...however, the speed boost is very significant
    1.63 + *   on a G4.
    1.64 + */
    1.65 +#define DST_CHAN_SRC 1
    1.66 +#define DST_CHAN_DEST 2
    1.67 +
    1.68 +/* macro to set DST control word value... */
    1.69 +#define DST_CTRL(size, count, stride) \
    1.70 +    (((size) << 24) | ((count) << 16) | (stride))
    1.71 +
    1.72 +#define VEC_ALIGNER(src) ((UNALIGNED_PTR(src)) \
    1.73 +    ? vec_lvsl(0, src) \
    1.74 +    : vec_add(vec_lvsl(8, src), vec_splat_u8(8)))
    1.75 +
    1.76 +/* Calculate the permute vector used for 32->32 swizzling */
    1.77 +static vector unsigned char
    1.78 +calc_swizzle32(const SDL_PixelFormat * srcfmt, const SDL_PixelFormat * dstfmt)
    1.79 +{
    1.80 +    /*
    1.81 +     * We have to assume that the bits that aren't used by other
    1.82 +     *  colors is alpha, and it's one complete byte, since some formats
    1.83 +     *  leave alpha with a zero mask, but we should still swizzle the bits.
    1.84 +     */
    1.85 +    /* ARGB */
    1.86 +    const static const struct SDL_PixelFormat default_pixel_format = {
    1.87 +        NULL, 32, 4,
    1.88 +        0, 0, 0, 0,
    1.89 +        16, 8, 0, 24,
    1.90 +        0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000
    1.91 +    };
    1.92 +    if (!srcfmt) {
    1.93 +        srcfmt = &default_pixel_format;
    1.94 +    }
    1.95 +    if (!dstfmt) {
    1.96 +        dstfmt = &default_pixel_format;
    1.97 +    }
    1.98 +    const vector unsigned char plus = VECUINT8_LITERAL(0x00, 0x00, 0x00, 0x00,
    1.99 +                                                       0x04, 0x04, 0x04, 0x04,
   1.100 +                                                       0x08, 0x08, 0x08, 0x08,
   1.101 +                                                       0x0C, 0x0C, 0x0C,
   1.102 +                                                       0x0C);
   1.103 +    vector unsigned char vswiz;
   1.104 +    vector unsigned int srcvec;
   1.105 +#define RESHIFT(X) (3 - ((X) >> 3))
   1.106 +    Uint32 rmask = RESHIFT(srcfmt->Rshift) << (dstfmt->Rshift);
   1.107 +    Uint32 gmask = RESHIFT(srcfmt->Gshift) << (dstfmt->Gshift);
   1.108 +    Uint32 bmask = RESHIFT(srcfmt->Bshift) << (dstfmt->Bshift);
   1.109 +    Uint32 amask;
   1.110 +    /* Use zero for alpha if either surface doesn't have alpha */
   1.111 +    if (dstfmt->Amask) {
   1.112 +        amask =
   1.113 +            ((srcfmt->Amask) ? RESHIFT(srcfmt->
   1.114 +                                       Ashift) : 0x10) << (dstfmt->Ashift);
   1.115 +    } else {
   1.116 +        amask =
   1.117 +            0x10101010 & ((dstfmt->Rmask | dstfmt->Gmask | dstfmt->Bmask) ^
   1.118 +                          0xFFFFFFFF);
   1.119 +    }
   1.120 +#undef RESHIFT
   1.121 +    ((unsigned int *) (char *) &srcvec)[0] = (rmask | gmask | bmask | amask);
   1.122 +    vswiz = vec_add(plus, (vector unsigned char) vec_splat(srcvec, 0));
   1.123 +    return (vswiz);
   1.124 +}
   1.125 +
   1.126 +static void Blit_RGB888_RGB565(SDL_BlitInfo * info);
   1.127 +static void
   1.128 +Blit_RGB888_RGB565Altivec(SDL_BlitInfo * info)
   1.129 +{
   1.130 +    int height = info->dst_h;
   1.131 +    Uint8 *src = (Uint8 *) info->src;
   1.132 +    int srcskip = info->src_skip;
   1.133 +    Uint8 *dst = (Uint8 *) info->dst;
   1.134 +    int dstskip = info->dst_skip;
   1.135 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.136 +    vector unsigned char valpha = vec_splat_u8(0);
   1.137 +    vector unsigned char vpermute = calc_swizzle32(srcfmt, NULL);
   1.138 +    vector unsigned char vgmerge = VECUINT8_LITERAL(0x00, 0x02, 0x00, 0x06,
   1.139 +                                                    0x00, 0x0a, 0x00, 0x0e,
   1.140 +                                                    0x00, 0x12, 0x00, 0x16,
   1.141 +                                                    0x00, 0x1a, 0x00, 0x1e);
   1.142 +    vector unsigned short v1 = vec_splat_u16(1);
   1.143 +    vector unsigned short v3 = vec_splat_u16(3);
   1.144 +    vector unsigned short v3f =
   1.145 +        VECUINT16_LITERAL(0x003f, 0x003f, 0x003f, 0x003f,
   1.146 +                          0x003f, 0x003f, 0x003f, 0x003f);
   1.147 +    vector unsigned short vfc =
   1.148 +        VECUINT16_LITERAL(0x00fc, 0x00fc, 0x00fc, 0x00fc,
   1.149 +                          0x00fc, 0x00fc, 0x00fc, 0x00fc);
   1.150 +    vector unsigned short vf800 = (vector unsigned short) vec_splat_u8(-7);
   1.151 +    vf800 = vec_sl(vf800, vec_splat_u16(8));
   1.152 +
   1.153 +    while (height--) {
   1.154 +        vector unsigned char valigner;
   1.155 +        vector unsigned char voverflow;
   1.156 +        vector unsigned char vsrc;
   1.157 +
   1.158 +        int width = info->dst_w;
   1.159 +        int extrawidth;
   1.160 +
   1.161 +        /* do scalar until we can align... */
   1.162 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.163 +        while (condition) { \
   1.164 +            Uint32 Pixel; \
   1.165 +            unsigned sR, sG, sB, sA; \
   1.166 +            DISEMBLE_RGBA((Uint8 *)src, 4, srcfmt, Pixel, \
   1.167 +                          sR, sG, sB, sA); \
   1.168 +            *(Uint16 *)(dst) = (((sR << 8) & 0x0000F800) | \
   1.169 +                                ((sG << 3) & 0x000007E0) | \
   1.170 +                                ((sB >> 3) & 0x0000001F)); \
   1.171 +            dst += 2; \
   1.172 +            src += 4; \
   1.173 +            widthvar--; \
   1.174 +        }
   1.175 +
   1.176 +        ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
   1.177 +
   1.178 +        /* After all that work, here's the vector part! */
   1.179 +        extrawidth = (width % 8);       /* trailing unaligned stores */
   1.180 +        width -= extrawidth;
   1.181 +        vsrc = vec_ld(0, src);
   1.182 +        valigner = VEC_ALIGNER(src);
   1.183 +
   1.184 +        while (width) {
   1.185 +            vector unsigned short vpixel, vrpixel, vgpixel, vbpixel;
   1.186 +            vector unsigned int vsrc1, vsrc2;
   1.187 +            vector unsigned char vdst;
   1.188 +
   1.189 +            voverflow = vec_ld(15, src);
   1.190 +            vsrc = vec_perm(vsrc, voverflow, valigner);
   1.191 +            vsrc1 = (vector unsigned int) vec_perm(vsrc, valpha, vpermute);
   1.192 +            src += 16;
   1.193 +            vsrc = voverflow;
   1.194 +            voverflow = vec_ld(15, src);
   1.195 +            vsrc = vec_perm(vsrc, voverflow, valigner);
   1.196 +            vsrc2 = (vector unsigned int) vec_perm(vsrc, valpha, vpermute);
   1.197 +            /* 1555 */
   1.198 +            vpixel = (vector unsigned short) vec_packpx(vsrc1, vsrc2);
   1.199 +            vgpixel = (vector unsigned short) vec_perm(vsrc1, vsrc2, vgmerge);
   1.200 +            vgpixel = vec_and(vgpixel, vfc);
   1.201 +            vgpixel = vec_sl(vgpixel, v3);
   1.202 +            vrpixel = vec_sl(vpixel, v1);
   1.203 +            vrpixel = vec_and(vrpixel, vf800);
   1.204 +            vbpixel = vec_and(vpixel, v3f);
   1.205 +            vdst =
   1.206 +                vec_or((vector unsigned char) vrpixel,
   1.207 +                       (vector unsigned char) vgpixel);
   1.208 +            /* 565 */
   1.209 +            vdst = vec_or(vdst, (vector unsigned char) vbpixel);
   1.210 +            vec_st(vdst, 0, dst);
   1.211 +
   1.212 +            width -= 8;
   1.213 +            src += 16;
   1.214 +            dst += 16;
   1.215 +            vsrc = voverflow;
   1.216 +        }
   1.217 +
   1.218 +        assert(width == 0);
   1.219 +
   1.220 +        /* do scalar until we can align... */
   1.221 +        ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.222 +#undef ONE_PIXEL_BLEND
   1.223 +
   1.224 +        src += srcskip;         /* move to next row, accounting for pitch. */
   1.225 +        dst += dstskip;
   1.226 +    }
   1.227 +
   1.228 +
   1.229 +}
   1.230 +
   1.231 +static void
   1.232 +Blit_RGB565_32Altivec(SDL_BlitInfo * info)
   1.233 +{
   1.234 +    int height = info->dst_h;
   1.235 +    Uint8 *src = (Uint8 *) info->src;
   1.236 +    int srcskip = info->src_skip;
   1.237 +    Uint8 *dst = (Uint8 *) info->dst;
   1.238 +    int dstskip = info->dst_skip;
   1.239 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.240 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.241 +    unsigned alpha;
   1.242 +    vector unsigned char valpha;
   1.243 +    vector unsigned char vpermute;
   1.244 +    vector unsigned short vf800;
   1.245 +    vector unsigned int v8 = vec_splat_u32(8);
   1.246 +    vector unsigned int v16 = vec_add(v8, v8);
   1.247 +    vector unsigned short v2 = vec_splat_u16(2);
   1.248 +    vector unsigned short v3 = vec_splat_u16(3);
   1.249 +    /* 
   1.250 +       0x10 - 0x1f is the alpha
   1.251 +       0x00 - 0x0e evens are the red
   1.252 +       0x01 - 0x0f odds are zero
   1.253 +     */
   1.254 +    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
   1.255 +                                                       0x10, 0x02, 0x01, 0x01,
   1.256 +                                                       0x10, 0x04, 0x01, 0x01,
   1.257 +                                                       0x10, 0x06, 0x01,
   1.258 +                                                       0x01);
   1.259 +    vector unsigned char vredalpha2 =
   1.260 +        (vector unsigned
   1.261 +         char) (vec_add((vector unsigned int) vredalpha1, vec_sl(v8, v16))
   1.262 +        );
   1.263 +    /*
   1.264 +       0x00 - 0x0f is ARxx ARxx ARxx ARxx
   1.265 +       0x11 - 0x0f odds are blue
   1.266 +     */
   1.267 +    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
   1.268 +                                                   0x04, 0x05, 0x06, 0x13,
   1.269 +                                                   0x08, 0x09, 0x0a, 0x15,
   1.270 +                                                   0x0c, 0x0d, 0x0e, 0x17);
   1.271 +    vector unsigned char vblue2 =
   1.272 +        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8)
   1.273 +        );
   1.274 +    /*
   1.275 +       0x00 - 0x0f is ARxB ARxB ARxB ARxB
   1.276 +       0x10 - 0x0e evens are green
   1.277 +     */
   1.278 +    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
   1.279 +                                                    0x04, 0x05, 0x12, 0x07,
   1.280 +                                                    0x08, 0x09, 0x14, 0x0b,
   1.281 +                                                    0x0c, 0x0d, 0x16, 0x0f);
   1.282 +    vector unsigned char vgreen2 =
   1.283 +        (vector unsigned
   1.284 +         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8, v8))
   1.285 +        );
   1.286 +
   1.287 +
   1.288 +    assert(srcfmt->BytesPerPixel == 2);
   1.289 +    assert(dstfmt->BytesPerPixel == 4);
   1.290 +
   1.291 +    vf800 = (vector unsigned short) vec_splat_u8(-7);
   1.292 +    vf800 = vec_sl(vf800, vec_splat_u16(8));
   1.293 +
   1.294 +    if (dstfmt->Amask && info->a) {
   1.295 +        ((unsigned char *) &valpha)[0] = alpha = info->a;
   1.296 +        valpha = vec_splat(valpha, 0);
   1.297 +    } else {
   1.298 +        alpha = 0;
   1.299 +        valpha = vec_splat_u8(0);
   1.300 +    }
   1.301 +
   1.302 +    vpermute = calc_swizzle32(NULL, dstfmt);
   1.303 +    while (height--) {
   1.304 +        vector unsigned char valigner;
   1.305 +        vector unsigned char voverflow;
   1.306 +        vector unsigned char vsrc;
   1.307 +
   1.308 +        int width = info->dst_w;
   1.309 +        int extrawidth;
   1.310 +
   1.311 +        /* do scalar until we can align... */
   1.312 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.313 +        while (condition) { \
   1.314 +            unsigned sR, sG, sB; \
   1.315 +            unsigned short Pixel = *((unsigned short *)src); \
   1.316 +            sR = (Pixel >> 8) & 0xf8; \
   1.317 +            sG = (Pixel >> 3) & 0xfc; \
   1.318 +            sB = (Pixel << 3) & 0xf8; \
   1.319 +            ASSEMBLE_RGBA(dst, 4, dstfmt, sR, sG, sB, alpha); \
   1.320 +            src += 2; \
   1.321 +            dst += 4; \
   1.322 +            widthvar--; \
   1.323 +        }
   1.324 +        ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
   1.325 +
   1.326 +        /* After all that work, here's the vector part! */
   1.327 +        extrawidth = (width % 8);       /* trailing unaligned stores */
   1.328 +        width -= extrawidth;
   1.329 +        vsrc = vec_ld(0, src);
   1.330 +        valigner = VEC_ALIGNER(src);
   1.331 +
   1.332 +        while (width) {
   1.333 +            vector unsigned short vR, vG, vB;
   1.334 +            vector unsigned char vdst1, vdst2;
   1.335 +
   1.336 +            voverflow = vec_ld(15, src);
   1.337 +            vsrc = vec_perm(vsrc, voverflow, valigner);
   1.338 +
   1.339 +            vR = vec_and((vector unsigned short) vsrc, vf800);
   1.340 +            vB = vec_sl((vector unsigned short) vsrc, v3);
   1.341 +            vG = vec_sl(vB, v2);
   1.342 +
   1.343 +            vdst1 =
   1.344 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.345 +                                                valpha, vredalpha1);
   1.346 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
   1.347 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
   1.348 +            vdst1 = vec_perm(vdst1, valpha, vpermute);
   1.349 +            vec_st(vdst1, 0, dst);
   1.350 +
   1.351 +            vdst2 =
   1.352 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.353 +                                                valpha, vredalpha2);
   1.354 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
   1.355 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
   1.356 +            vdst2 = vec_perm(vdst2, valpha, vpermute);
   1.357 +            vec_st(vdst2, 16, dst);
   1.358 +
   1.359 +            width -= 8;
   1.360 +            dst += 32;
   1.361 +            src += 16;
   1.362 +            vsrc = voverflow;
   1.363 +        }
   1.364 +
   1.365 +        assert(width == 0);
   1.366 +
   1.367 +
   1.368 +        /* do scalar until we can align... */
   1.369 +        ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.370 +#undef ONE_PIXEL_BLEND
   1.371 +
   1.372 +        src += srcskip;         /* move to next row, accounting for pitch. */
   1.373 +        dst += dstskip;
   1.374 +    }
   1.375 +
   1.376 +}
   1.377 +
   1.378 +
   1.379 +static void
   1.380 +Blit_RGB555_32Altivec(SDL_BlitInfo * info)
   1.381 +{
   1.382 +    int height = info->dst_h;
   1.383 +    Uint8 *src = (Uint8 *) info->src;
   1.384 +    int srcskip = info->src_skip;
   1.385 +    Uint8 *dst = (Uint8 *) info->dst;
   1.386 +    int dstskip = info->dst_skip;
   1.387 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.388 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.389 +    unsigned alpha;
   1.390 +    vector unsigned char valpha;
   1.391 +    vector unsigned char vpermute;
   1.392 +    vector unsigned short vf800;
   1.393 +    vector unsigned int v8 = vec_splat_u32(8);
   1.394 +    vector unsigned int v16 = vec_add(v8, v8);
   1.395 +    vector unsigned short v1 = vec_splat_u16(1);
   1.396 +    vector unsigned short v3 = vec_splat_u16(3);
   1.397 +    /* 
   1.398 +       0x10 - 0x1f is the alpha
   1.399 +       0x00 - 0x0e evens are the red
   1.400 +       0x01 - 0x0f odds are zero
   1.401 +     */
   1.402 +    vector unsigned char vredalpha1 = VECUINT8_LITERAL(0x10, 0x00, 0x01, 0x01,
   1.403 +                                                       0x10, 0x02, 0x01, 0x01,
   1.404 +                                                       0x10, 0x04, 0x01, 0x01,
   1.405 +                                                       0x10, 0x06, 0x01,
   1.406 +                                                       0x01);
   1.407 +    vector unsigned char vredalpha2 =
   1.408 +        (vector unsigned
   1.409 +         char) (vec_add((vector unsigned int) vredalpha1, vec_sl(v8, v16))
   1.410 +        );
   1.411 +    /*
   1.412 +       0x00 - 0x0f is ARxx ARxx ARxx ARxx
   1.413 +       0x11 - 0x0f odds are blue
   1.414 +     */
   1.415 +    vector unsigned char vblue1 = VECUINT8_LITERAL(0x00, 0x01, 0x02, 0x11,
   1.416 +                                                   0x04, 0x05, 0x06, 0x13,
   1.417 +                                                   0x08, 0x09, 0x0a, 0x15,
   1.418 +                                                   0x0c, 0x0d, 0x0e, 0x17);
   1.419 +    vector unsigned char vblue2 =
   1.420 +        (vector unsigned char) (vec_add((vector unsigned int) vblue1, v8)
   1.421 +        );
   1.422 +    /*
   1.423 +       0x00 - 0x0f is ARxB ARxB ARxB ARxB
   1.424 +       0x10 - 0x0e evens are green
   1.425 +     */
   1.426 +    vector unsigned char vgreen1 = VECUINT8_LITERAL(0x00, 0x01, 0x10, 0x03,
   1.427 +                                                    0x04, 0x05, 0x12, 0x07,
   1.428 +                                                    0x08, 0x09, 0x14, 0x0b,
   1.429 +                                                    0x0c, 0x0d, 0x16, 0x0f);
   1.430 +    vector unsigned char vgreen2 =
   1.431 +        (vector unsigned
   1.432 +         char) (vec_add((vector unsigned int) vgreen1, vec_sl(v8, v8))
   1.433 +        );
   1.434 +
   1.435 +
   1.436 +    assert(srcfmt->BytesPerPixel == 2);
   1.437 +    assert(dstfmt->BytesPerPixel == 4);
   1.438 +
   1.439 +    vf800 = (vector unsigned short) vec_splat_u8(-7);
   1.440 +    vf800 = vec_sl(vf800, vec_splat_u16(8));
   1.441 +
   1.442 +    if (dstfmt->Amask && info->a) {
   1.443 +        ((unsigned char *) &valpha)[0] = alpha = info->a;
   1.444 +        valpha = vec_splat(valpha, 0);
   1.445 +    } else {
   1.446 +        alpha = 0;
   1.447 +        valpha = vec_splat_u8(0);
   1.448 +    }
   1.449 +
   1.450 +    vpermute = calc_swizzle32(NULL, dstfmt);
   1.451 +    while (height--) {
   1.452 +        vector unsigned char valigner;
   1.453 +        vector unsigned char voverflow;
   1.454 +        vector unsigned char vsrc;
   1.455 +
   1.456 +        int width = info->dst_w;
   1.457 +        int extrawidth;
   1.458 +
   1.459 +        /* do scalar until we can align... */
   1.460 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.461 +        while (condition) { \
   1.462 +            unsigned sR, sG, sB; \
   1.463 +            unsigned short Pixel = *((unsigned short *)src); \
   1.464 +            sR = (Pixel >> 7) & 0xf8; \
   1.465 +            sG = (Pixel >> 2) & 0xf8; \
   1.466 +            sB = (Pixel << 3) & 0xf8; \
   1.467 +            ASSEMBLE_RGBA(dst, 4, dstfmt, sR, sG, sB, alpha); \
   1.468 +            src += 2; \
   1.469 +            dst += 4; \
   1.470 +            widthvar--; \
   1.471 +        }
   1.472 +        ONE_PIXEL_BLEND(((UNALIGNED_PTR(dst)) && (width)), width);
   1.473 +
   1.474 +        /* After all that work, here's the vector part! */
   1.475 +        extrawidth = (width % 8);       /* trailing unaligned stores */
   1.476 +        width -= extrawidth;
   1.477 +        vsrc = vec_ld(0, src);
   1.478 +        valigner = VEC_ALIGNER(src);
   1.479 +
   1.480 +        while (width) {
   1.481 +            vector unsigned short vR, vG, vB;
   1.482 +            vector unsigned char vdst1, vdst2;
   1.483 +
   1.484 +            voverflow = vec_ld(15, src);
   1.485 +            vsrc = vec_perm(vsrc, voverflow, valigner);
   1.486 +
   1.487 +            vR = vec_and(vec_sl((vector unsigned short) vsrc, v1), vf800);
   1.488 +            vB = vec_sl((vector unsigned short) vsrc, v3);
   1.489 +            vG = vec_sl(vB, v3);
   1.490 +
   1.491 +            vdst1 =
   1.492 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.493 +                                                valpha, vredalpha1);
   1.494 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vB, vblue1);
   1.495 +            vdst1 = vec_perm(vdst1, (vector unsigned char) vG, vgreen1);
   1.496 +            vdst1 = vec_perm(vdst1, valpha, vpermute);
   1.497 +            vec_st(vdst1, 0, dst);
   1.498 +
   1.499 +            vdst2 =
   1.500 +                (vector unsigned char) vec_perm((vector unsigned char) vR,
   1.501 +                                                valpha, vredalpha2);
   1.502 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vB, vblue2);
   1.503 +            vdst2 = vec_perm(vdst2, (vector unsigned char) vG, vgreen2);
   1.504 +            vdst2 = vec_perm(vdst2, valpha, vpermute);
   1.505 +            vec_st(vdst2, 16, dst);
   1.506 +
   1.507 +            width -= 8;
   1.508 +            dst += 32;
   1.509 +            src += 16;
   1.510 +            vsrc = voverflow;
   1.511 +        }
   1.512 +
   1.513 +        assert(width == 0);
   1.514 +
   1.515 +
   1.516 +        /* do scalar until we can align... */
   1.517 +        ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.518 +#undef ONE_PIXEL_BLEND
   1.519 +
   1.520 +        src += srcskip;         /* move to next row, accounting for pitch. */
   1.521 +        dst += dstskip;
   1.522 +    }
   1.523 +
   1.524 +}
   1.525 +
   1.526 +static void BlitNtoNKey(SDL_BlitInfo * info);
   1.527 +static void BlitNtoNKeyCopyAlpha(SDL_BlitInfo * info);
   1.528 +static void
   1.529 +Blit32to32KeyAltivec(SDL_BlitInfo * info)
   1.530 +{
   1.531 +    int height = info->dst_h;
   1.532 +    Uint32 *srcp = (Uint32 *) info->src;
   1.533 +    int srcskip = info->src_skip / 4;
   1.534 +    Uint32 *dstp = (Uint32 *) info->dst;
   1.535 +    int dstskip = info->dst_skip / 4;
   1.536 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.537 +    int srcbpp = srcfmt->BytesPerPixel;
   1.538 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.539 +    int dstbpp = dstfmt->BytesPerPixel;
   1.540 +    int copy_alpha = (srcfmt->Amask && dstfmt->Amask);
   1.541 +    unsigned alpha = dstfmt->Amask ? info->a : 0;
   1.542 +    Uint32 rgbmask = srcfmt->Rmask | srcfmt->Gmask | srcfmt->Bmask;
   1.543 +    Uint32 ckey = info->colorkey;
   1.544 +    vector unsigned int valpha;
   1.545 +    vector unsigned char vpermute;
   1.546 +    vector unsigned char vzero;
   1.547 +    vector unsigned int vckey;
   1.548 +    vector unsigned int vrgbmask;
   1.549 +    vpermute = calc_swizzle32(srcfmt, dstfmt);
   1.550 +    if (info->dst_w < 16) {
   1.551 +        if (copy_alpha) {
   1.552 +            BlitNtoNKeyCopyAlpha(info);
   1.553 +        } else {
   1.554 +            BlitNtoNKey(info);
   1.555 +        }
   1.556 +        return;
   1.557 +    }
   1.558 +    vzero = vec_splat_u8(0);
   1.559 +    if (alpha) {
   1.560 +        ((unsigned char *) &valpha)[0] = (unsigned char) alpha;
   1.561 +        valpha =
   1.562 +            (vector unsigned int) vec_splat((vector unsigned char) valpha, 0);
   1.563 +    } else {
   1.564 +        valpha = (vector unsigned int) vzero;
   1.565 +    }
   1.566 +    ckey &= rgbmask;
   1.567 +    ((unsigned int *) (char *) &vckey)[0] = ckey;
   1.568 +    vckey = vec_splat(vckey, 0);
   1.569 +    ((unsigned int *) (char *) &vrgbmask)[0] = rgbmask;
   1.570 +    vrgbmask = vec_splat(vrgbmask, 0);
   1.571 +
   1.572 +    while (height--) {
   1.573 +#define ONE_PIXEL_BLEND(condition, widthvar) \
   1.574 +        if (copy_alpha) { \
   1.575 +            while (condition) { \
   1.576 +                Uint32 Pixel; \
   1.577 +                unsigned sR, sG, sB, sA; \
   1.578 +                DISEMBLE_RGBA((Uint8 *)srcp, srcbpp, srcfmt, Pixel, \
   1.579 +                          sR, sG, sB, sA); \
   1.580 +                if ( (Pixel & rgbmask) != ckey ) { \
   1.581 +                      ASSEMBLE_RGBA((Uint8 *)dstp, dstbpp, dstfmt, \
   1.582 +                            sR, sG, sB, sA); \
   1.583 +                } \
   1.584 +                dstp = (Uint32 *) (((Uint8 *) dstp) + dstbpp); \
   1.585 +                srcp = (Uint32 *) (((Uint8 *) srcp) + srcbpp); \
   1.586 +                widthvar--; \
   1.587 +            } \
   1.588 +        } else { \
   1.589 +            while (condition) { \
   1.590 +                Uint32 Pixel; \
   1.591 +                unsigned sR, sG, sB; \
   1.592 +                RETRIEVE_RGB_PIXEL((Uint8 *)srcp, srcbpp, Pixel); \
   1.593 +                if ( Pixel != ckey ) { \
   1.594 +                    RGB_FROM_PIXEL(Pixel, srcfmt, sR, sG, sB); \
   1.595 +                    ASSEMBLE_RGBA((Uint8 *)dstp, dstbpp, dstfmt, \
   1.596 +                              sR, sG, sB, alpha); \
   1.597 +                } \
   1.598 +                dstp = (Uint32 *) (((Uint8 *)dstp) + dstbpp); \
   1.599 +                srcp = (Uint32 *) (((Uint8 *)srcp) + srcbpp); \
   1.600 +                widthvar--; \
   1.601 +            } \
   1.602 +        }
   1.603 +        int width = info->dst_w;
   1.604 +        ONE_PIXEL_BLEND((UNALIGNED_PTR(dstp)) && (width), width);
   1.605 +        assert(width > 0);
   1.606 +        if (width > 0) {
   1.607 +            int extrawidth = (width % 4);
   1.608 +            vector unsigned char valigner = VEC_ALIGNER(srcp);
   1.609 +            vector unsigned int vs = vec_ld(0, srcp);
   1.610 +            width -= extrawidth;
   1.611 +            assert(width >= 4);
   1.612 +            while (width) {
   1.613 +                vector unsigned char vsel;
   1.614 +                vector unsigned int vd;
   1.615 +                vector unsigned int voverflow = vec_ld(15, srcp);
   1.616 +                /* load the source vec */
   1.617 +                vs = vec_perm(vs, voverflow, valigner);
   1.618 +                /* vsel is set for items that match the key */
   1.619 +                vsel = (vector unsigned char) vec_and(vs, vrgbmask);
   1.620 +                vsel = (vector unsigned char) vec_cmpeq(vs, vckey);
   1.621 +                /* permute the src vec to the dest format */
   1.622 +                vs = vec_perm(vs, valpha, vpermute);
   1.623 +                /* load the destination vec */
   1.624 +                vd = vec_ld(0, dstp);
   1.625 +                /* select the source and dest into vs */
   1.626 +                vd = (vector unsigned int) vec_sel((vector unsigned char) vs,
   1.627 +                                                   (vector unsigned char) vd,
   1.628 +                                                   vsel);
   1.629 +
   1.630 +                vec_st(vd, 0, dstp);
   1.631 +                srcp += 4;
   1.632 +                width -= 4;
   1.633 +                dstp += 4;
   1.634 +                vs = voverflow;
   1.635 +            }
   1.636 +            ONE_PIXEL_BLEND((extrawidth), extrawidth);
   1.637 +#undef ONE_PIXEL_BLEND
   1.638 +            srcp += srcskip;
   1.639 +            dstp += dstskip;
   1.640 +        }
   1.641 +    }
   1.642 +}
   1.643 +
   1.644 +/* Altivec code to swizzle one 32-bit surface to a different 32-bit format. */
   1.645 +/* Use this on a G5 */
   1.646 +static void
   1.647 +ConvertAltivec32to32_noprefetch(SDL_BlitInfo * info)
   1.648 +{
   1.649 +    int height = info->dst_h;
   1.650 +    Uint32 *src = (Uint32 *) info->src;
   1.651 +    int srcskip = info->src_skip / 4;
   1.652 +    Uint32 *dst = (Uint32 *) info->dst;
   1.653 +    int dstskip = info->dst_skip / 4;
   1.654 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.655 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.656 +    vector unsigned int vzero = vec_splat_u32(0);
   1.657 +    vector unsigned char vpermute = calc_swizzle32(srcfmt, dstfmt);
   1.658 +    if (dstfmt->Amask && !srcfmt->Amask) {
   1.659 +        if (info->a) {
   1.660 +            vector unsigned char valpha;
   1.661 +            ((unsigned char *) &valpha)[0] = info->a;
   1.662 +            vzero = (vector unsigned int) vec_splat(valpha, 0);
   1.663 +        }
   1.664 +    }
   1.665 +
   1.666 +    assert(srcfmt->BytesPerPixel == 4);
   1.667 +    assert(dstfmt->BytesPerPixel == 4);
   1.668 +
   1.669 +    while (height--) {
   1.670 +        vector unsigned char valigner;
   1.671 +        vector unsigned int vbits;
   1.672 +        vector unsigned int voverflow;
   1.673 +        Uint32 bits;
   1.674 +        Uint8 r, g, b, a;
   1.675 +
   1.676 +        int width = info->dst_w;
   1.677 +        int extrawidth;
   1.678 +
   1.679 +        /* do scalar until we can align... */
   1.680 +        while ((UNALIGNED_PTR(dst)) && (width)) {
   1.681 +            bits = *(src++);
   1.682 +            RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
   1.683 +            *(dst++) = MAKE8888(dstfmt, r, g, b, a);
   1.684 +            width--;
   1.685 +        }
   1.686 +
   1.687 +        /* After all that work, here's the vector part! */
   1.688 +        extrawidth = (width % 4);
   1.689 +        width -= extrawidth;
   1.690 +        valigner = VEC_ALIGNER(src);
   1.691 +        vbits = vec_ld(0, src);
   1.692 +
   1.693 +        while (width) {
   1.694 +            voverflow = vec_ld(15, src);
   1.695 +            src += 4;
   1.696 +            width -= 4;
   1.697 +            vbits = vec_perm(vbits, voverflow, valigner);       /* src is ready. */
   1.698 +            vbits = vec_perm(vbits, vzero, vpermute);   /* swizzle it. */
   1.699 +            vec_st(vbits, 0, dst);      /* store it back out. */
   1.700 +            dst += 4;
   1.701 +            vbits = voverflow;
   1.702 +        }
   1.703 +
   1.704 +        assert(width == 0);
   1.705 +
   1.706 +        /* cover pixels at the end of the row that didn't fit in 16 bytes. */
   1.707 +        while (extrawidth) {
   1.708 +            bits = *(src++);    /* max 7 pixels, don't bother with prefetch. */
   1.709 +            RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
   1.710 +            *(dst++) = MAKE8888(dstfmt, r, g, b, a);
   1.711 +            extrawidth--;
   1.712 +        }
   1.713 +
   1.714 +        src += srcskip;
   1.715 +        dst += dstskip;
   1.716 +    }
   1.717 +
   1.718 +}
   1.719 +
   1.720 +/* Altivec code to swizzle one 32-bit surface to a different 32-bit format. */
   1.721 +/* Use this on a G4 */
   1.722 +static void
   1.723 +ConvertAltivec32to32_prefetch(SDL_BlitInfo * info)
   1.724 +{
   1.725 +    const int scalar_dst_lead = sizeof(Uint32) * 4;
   1.726 +    const int vector_dst_lead = sizeof(Uint32) * 16;
   1.727 +
   1.728 +    int height = info->dst_h;
   1.729 +    Uint32 *src = (Uint32 *) info->src;
   1.730 +    int srcskip = info->src_skip / 4;
   1.731 +    Uint32 *dst = (Uint32 *) info->dst;
   1.732 +    int dstskip = info->dst_skip / 4;
   1.733 +    SDL_PixelFormat *srcfmt = info->src_fmt;
   1.734 +    SDL_PixelFormat *dstfmt = info->dst_fmt;
   1.735 +    vector unsigned int vzero = vec_splat_u32(0);
   1.736 +    vector unsigned char vpermute = calc_swizzle32(srcfmt, dstfmt);
   1.737 +    if (dstfmt->Amask && !srcfmt->Amask) {
   1.738 +        if (info->a) {
   1.739 +            vector unsigned char valpha;
   1.740 +            ((unsigned char *) &valpha)[0] = info->a;
   1.741 +            vzero = (vector unsigned int) vec_splat(valpha, 0);
   1.742 +        }
   1.743 +    }
   1.744 +
   1.745 +    assert(srcfmt->BytesPerPixel == 4);
   1.746 +    assert(dstfmt->BytesPerPixel == 4);
   1.747 +
   1.748 +    while (height--) {
   1.749 +        vector unsigned char valigner;
   1.750 +        vector unsigned int vbits;
   1.751 +        vector unsigned int voverflow;
   1.752 +        Uint32 bits;
   1.753 +        Uint8 r, g, b, a;
   1.754 +
   1.755 +        int width = info->dst_w;
   1.756 +        int extrawidth;
   1.757 +
   1.758 +        /* do scalar until we can align... */
   1.759 +        while ((UNALIGNED_PTR(dst)) && (width)) {
   1.760 +            vec_dstt(src + scalar_dst_lead, DST_CTRL(2, 32, 1024),
   1.761 +                     DST_CHAN_SRC);
   1.762 +            vec_dstst(dst + scalar_dst_lead, DST_CTRL(2, 32, 1024),
   1.763 +                      DST_CHAN_DEST);
   1.764 +            bits = *(src++);
   1.765 +            RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
   1.766 +            *(dst++) = MAKE8888(dstfmt, r, g, b, a);
   1.767 +            width--;
   1.768 +        }
   1.769 +
   1.770 +        /* After all that work, here's the vector part! */
   1.771 +        extrawidth = (width % 4);
   1.772 +        width -= extrawidth;
   1.773 +        valigner = VEC_ALIGNER(src);
   1.774 +        vbits = vec_ld(0, src);
   1.775 +
   1.776 +        while (width) {
   1.777 +            vec_dstt(src + vector_dst_lead, DST_CTRL(2, 32, 1024),
   1.778 +                     DST_CHAN_SRC);
   1.779 +            vec_dstst(dst + vector_dst_lead, DST_CTRL(2, 32, 1024),
   1.780 +                      DST_CHAN_DEST);
   1.781 +            voverflow = vec_ld(15, src);
   1.782 +            src += 4;
   1.783 +            width -= 4;
   1.784 +            vbits = vec_perm(vbits, voverflow, valigner);       /* src is ready. */
   1.785 +            vbits = vec_perm(vbits, vzero, vpermute);   /* swizzle it. */
   1.786 +            vec_st(vbits, 0, dst);      /* store it back out. */
   1.787 +            dst += 4;
   1.788 +            vbits = voverflow;
   1.789 +        }
   1.790 +
   1.791 +        assert(width == 0);
   1.792 +
   1.793 +        /* cover pixels at the end of the row that didn't fit in 16 bytes. */
   1.794 +        while (extrawidth) {
   1.795 +            bits = *(src++);    /* max 7 pixels, don't bother with prefetch. */
   1.796 +            RGBA_FROM_8888(bits, srcfmt, r, g, b, a);
   1.797 +            *(dst++) = MAKE8888(dstfmt, r, g, b, a);
   1.798 +            extrawidth--;
   1.799 +        }
   1.800 +
   1.801 +        src += srcskip;
   1.802 +        dst += dstskip;
   1.803 +    }
   1.804 +
   1.805 +    vec_dss(DST_CHAN_SRC);
   1.806 +    vec_dss(DST_CHAN_DEST);
   1.807 +}
   1.808 +
   1.809 +static Uint32
   1.810 +GetBlitFeatures(void)
   1.811 +{
   1.812 +    static Uint32 features = 0xffffffff;
   1.813 +    if (features == 0xffffffff) {
   1.814 +        /* Provide an override for testing .. */
   1.815 +        char *override = SDL_getenv("SDL_ALTIVEC_BLIT_FEATURES");
   1.816 +        if (override) {
   1.817 +            features = 0;
   1.818 +            SDL_sscanf(override, "%u", &features);
   1.819 +        } else {
   1.820 +            features = (0
   1.821 +                        /* Feature 1 is has-MMX */
   1.822 +                        | ((SDL_HasMMX())? 1 : 0)
   1.823 +                        /* Feature 2 is has-AltiVec */
   1.824 +                        | ((SDL_HasAltiVec())? 2 : 0)
   1.825 +                        /* Feature 4 is dont-use-prefetch */
   1.826 +                        /* !!!! FIXME: Check for G5 or later, not the cache size! Always prefetch on a G4. */
   1.827 +                        | ((GetL3CacheSize() == 0) ? 4 : 0)
   1.828 +                );
   1.829 +        }
   1.830 +    }
   1.831 +    return features;
   1.832 +}
   1.833 +
   1.834 +#if __MWERKS__
   1.835 +#pragma altivec_model off
   1.836 +#endif
   1.837 +#else
   1.838  /* Feature 1 is has-MMX */
   1.839  #define GetBlitFeatures() ((Uint32)(SDL_HasMMX() ? 1 : 0))
   1.840 +#endif
   1.841  
   1.842  /* This is now endian dependent */
   1.843  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.844 @@ -1508,6 +2340,15 @@
   1.845  };
   1.846  
   1.847  static const struct blit_table normal_blit_2[] = {
   1.848 +#if SDL_ALTIVEC_BLITTERS
   1.849 +    /* has-altivec */
   1.850 +    {0x0000F800, 0x000007E0, 0x0000001F, 4, 0x00000000, 0x00000000,
   1.851 +     0x00000000,
   1.852 +     2, Blit_RGB565_32Altivec, NO_ALPHA | COPY_ALPHA | SET_ALPHA},
   1.853 +    {0x00007C00, 0x000003E0, 0x0000001F, 4, 0x00000000, 0x00000000,
   1.854 +     0x00000000,
   1.855 +     2, Blit_RGB555_32Altivec, NO_ALPHA | COPY_ALPHA | SET_ALPHA},
   1.856 +#endif
   1.857      {0x0000F800, 0x000007E0, 0x0000001F, 4, 0x00FF0000, 0x0000FF00,
   1.858       0x000000FF,
   1.859       0, Blit_RGB565_ARGB8888, SET_ALPHA},
   1.860 @@ -1531,6 +2372,22 @@
   1.861  };
   1.862  
   1.863  static const struct blit_table normal_blit_4[] = {
   1.864 +#if SDL_ALTIVEC_BLITTERS
   1.865 +    /* has-altivec | dont-use-prefetch */
   1.866 +    {0x00000000, 0x00000000, 0x00000000, 4, 0x00000000, 0x00000000,
   1.867 +     0x00000000,
   1.868 +     6, ConvertAltivec32to32_noprefetch,
   1.869 +     NO_ALPHA | COPY_ALPHA | SET_ALPHA},
   1.870 +    /* has-altivec */
   1.871 +    {0x00000000, 0x00000000, 0x00000000, 4, 0x00000000, 0x00000000,
   1.872 +     0x00000000,
   1.873 +     2, ConvertAltivec32to32_prefetch,
   1.874 +     NO_ALPHA | COPY_ALPHA | SET_ALPHA},
   1.875 +    /* has-altivec */
   1.876 +    {0x00000000, 0x00000000, 0x00000000, 2, 0x0000F800, 0x000007E0,
   1.877 +     0x0000001F,
   1.878 +     2, Blit_RGB888_RGB565Altivec, NO_ALPHA},
   1.879 +#endif
   1.880      {0x00FF0000, 0x0000FF00, 0x000000FF, 2, 0x0000F800, 0x000007E0,
   1.881       0x0000001F,
   1.882       0, Blit_RGB888_RGB565, NO_ALPHA},
   1.883 @@ -1628,6 +2485,12 @@
   1.884          else if (dstfmt->BytesPerPixel == 1)
   1.885              return BlitNto1Key;
   1.886          else {
   1.887 +#if SDL_ALTIVEC_BLITTERS
   1.888 +            if ((srcfmt->BytesPerPixel == 4) && (dstfmt->BytesPerPixel == 4)
   1.889 +                && SDL_HasAltiVec()) {
   1.890 +                return Blit32to32KeyAltivec;
   1.891 +            } else
   1.892 +#endif
   1.893              if (srcfmt->Amask && dstfmt->Amask) {
   1.894                  return BlitNtoNKeyCopyAlpha;
   1.895              } else {