Skip to content

Commit

Permalink
fixed rain effect for PPC; added PPC Altivec graphic routines; added …
Browse files Browse the repository at this point in the history
…SSE2 alpha-blend graphic routine
  • Loading branch information
sonozaki committed Jun 21, 2009
1 parent e9094b2 commit 7cdbb73
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 56 deletions.
70 changes: 46 additions & 24 deletions AnimationInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#endif

#if defined(USE_PPC_GFX)
#include "graphics_maltivec.h"
#include "graphics_altivec.h"
#endif

#include <math.h>
Expand Down Expand Up @@ -277,8 +277,8 @@ void AnimationInfo::blendOnSurface( SDL_Surface *dst_surface, int dst_x, int dst
Uint32* srcmax = (Uint32*)image_surface->pixels +
image_surface->w * image_surface->h;

for (int i=0 ; i<dst_rect.h ; i++){
for (int j=0 ; j<dst_rect.w ; j++, src_buffer++, dst_buffer++){
for (int i=dst_rect.h ; i ; --i){
for (int j=dst_rect.w ; j ; --j, src_buffer++, dst_buffer++){
// If we've run out of source area, ignore the remainder.
if (src_buffer >= srcmax) goto break2;
SET_PIXEL(*src_buffer, 0xff);
Expand All @@ -295,19 +295,24 @@ void AnimationInfo::blendOnSurface( SDL_Surface *dst_surface, int dst_x, int dst
Uint32* srcmax = (Uint32*)image_surface->pixels +
image_surface->w * image_surface->h;

for (int i=0 ; i<dst_rect.h ; i++){
for (int j=0 ; j<dst_rect.w ; j++, src_buffer++, dst_buffer++){
for (int i=dst_rect.h ; i ; --i){
#ifdef BPP16
for (int j=dst_rect.w ; j ; --j, src_buffer++, dst_buffer++){
// If we've run out of source area, ignore the remainder.
if (src_buffer >= srcmax) goto break2;
BLEND_PIXEL();
}
src_buffer += total_width - dst_rect.w;
#ifdef BPP16
dst_buffer += dst_surface->w - dst_rect.w;
alphap += image_surface->w - dst_rect.w;
#else
alphap += (image_surface->w - dst_rect.w)*4;
if (src_buffer >= srcmax) goto break2;
int length = dst_rect.w;
imageFilterBlend(dst_buffer, src_buffer, alphap, alpha, length);
src_buffer += total_width;
dst_buffer += dst_surface->w;
alphap += (image_surface->w)*4;
#endif
dst_buffer += dst_surface->w - dst_rect.w;
}
}
#ifndef BPP16
Expand All @@ -330,8 +335,8 @@ void AnimationInfo::blendOnSurface( SDL_Surface *dst_surface, int dst_x, int dst
Uint32* srcmax = (Uint32*)image_surface->pixels +
image_surface->w * image_surface->h;

for (int i=0 ; i<dst_rect.h ; i++){
for (int j=0 ; j<dst_rect.w ; j++, src_buffer++, dst_buffer++){
for (int i=dst_rect.h ; i ; --i){
for (int j=dst_rect.w ; j ; --j, src_buffer++, dst_buffer++){
// If we've run out of source area, ignore the remainder.
if (src_buffer >= srcmax) goto break2;
ADDBLEND_PIXEL();
Expand Down Expand Up @@ -360,8 +365,8 @@ void AnimationInfo::blendOnSurface( SDL_Surface *dst_surface, int dst_x, int dst
Uint32* srcmax = (Uint32*)image_surface->pixels +
image_surface->w * image_surface->h;

for (int i=0 ; i<dst_rect.h ; i++){
for (int j=0 ; j<dst_rect.w ; j++, src_buffer++, dst_buffer++){
for (int i=dst_rect.h ; i ; --i){
for (int j=dst_rect.w ; j ; --j, src_buffer++, dst_buffer++){
// If we've run out of source area, ignore the remainder.
if (src_buffer >= srcmax) goto break2;
SUBBLEND_PIXEL();
Expand Down Expand Up @@ -862,8 +867,7 @@ unsigned int AnimationInfo::getCpufuncs()
void AnimationInfo::imageFilterMean(unsigned char *src1, unsigned char *src2, unsigned char *dst, int length)
{
#if defined(USE_PPC_GFX)
int n = length;
BASIC_MEAN()
imageFilterMean_Altivec(src1, src2, dst, length);
#elif defined(USE_X86_GFX)

#ifndef MACOSX
Expand All @@ -881,16 +885,15 @@ void AnimationInfo::imageFilterMean(unsigned char *src1, unsigned char *src2, un
#endif // !MACOSX

#else // no special gfx handling
int n = length;
BASIC_MEAN()
int n = length + 1;
BASIC_MEAN();
#endif
}

void AnimationInfo::imageFilterAddTo(unsigned char *dst, unsigned char *src, int length)
{
#if defined(USE_PPC_GFX)
int n = length;
BASIC_ADDTO()
imageFilterAddTo_Altivec(dst, src, length);
#elif defined(USE_X86_GFX)

#ifndef MACOSX
Expand All @@ -908,16 +911,15 @@ void AnimationInfo::imageFilterAddTo(unsigned char *dst, unsigned char *src, int
#endif // !MACOSX

#else // no special gfx handling
int n = length;
BASIC_ADDTO()
int n = length + 1;
BASIC_ADDTO();
#endif
}

void AnimationInfo::imageFilterSubFrom(unsigned char *dst, unsigned char *src, int length)
{
#if defined(USE_PPC_GFX)
int n = length;
BASIC_SUBFROM()
imageFilterSubFrom_Altivec(dst, src, length);
#elif defined(USE_X86_GFX)

#ifndef MACOSX
Expand All @@ -935,8 +937,28 @@ void AnimationInfo::imageFilterSubFrom(unsigned char *dst, unsigned char *src, i
#endif // !MACOSX

#else // no special gfx handling
int n = length;
BASIC_SUBFROM()
int n = length + 1;
BASIC_SUBFROM();
#endif
}


void AnimationInfo::imageFilterBlend(Uint32 *dst_buffer, Uint32 *src_buffer, Uint8 *alphap, int alpha, int length)
{
#if defined(USE_X86_GFX)
#ifndef MACOSX
if (cpufuncs & CPUF_X86_SSE2) {
#endif // !MACOSX

imageFilterBlend_SSE2(dst_buffer, src_buffer, alphap, alpha, length);

#ifndef MACOSX
}
#endif // !MACOSX

#else // no special gfx handling
int n = length + 1;
BASIC_BLEND();
#endif
}

1 change: 1 addition & 0 deletions AnimationInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class AnimationInfo{
static void imageFilterMean(unsigned char *src1, unsigned char *src2, unsigned char *dst, int length);
static void imageFilterAddTo(unsigned char *dst, unsigned char *src, int length);
static void imageFilterSubFrom(unsigned char *dst, unsigned char *src, int length);
static void imageFilterBlend(Uint32 *dst_buffer, Uint32 *src_buffer, Uint8 *alphap, int alpha, int length);
};

#endif // __ANIMATION_INFO_H__
7 changes: 0 additions & 7 deletions Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,10 @@
#include <stdlib.h>
#include <math.h>

#if SDL_BYTEORDER == SDL_LIL_ENDIAN
#define RMASK 0x00ff0000
#define GMASK 0x0000ff00
#define BMASK 0x000000ff
#define AMASK 0xff000000
#else
#define RMASK 0x0000ff00
#define GMASK 0x00ff0000
#define BMASK 0xff000000
#define AMASK 0x000000ff
#endif

#define MAX_SPRITE_NUM 1000

Expand Down
117 changes: 117 additions & 0 deletions graphics_altivec.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* -*- C++ -*-
*
* graphics_altivec.cpp - graphics routines using PPC Altivec cpu functionality
*
* Copyright (c) 2009 Mion. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

// Based upon routines provided by Roto

#ifdef USE_PPC_GFX

#include <altivec.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#include "graphics_common.h"


void imageFilterMean_Altivec(unsigned char *src1, unsigned char *src2, unsigned char *dst, int length)
{
int n = length;

// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
MEAN_PIXEL();
--n; ++dst; ++src1; ++src2;
}

// Do bulk of processing using Altivec (find the mean of 16 8-bit unsigned integers, with saturation)
vector unsigned char rshft = vec_splat_u8(0x1);
while(n >= 16) {
vector unsigned char s1 = vec_ld(0,src1);
s1 = vec_sr(s1, rshft); // shift right 1
vector unsigned char s2 = vec_ld(0,src2);
s2 = vec_sr(s2, rshft); // shift right 1
vector unsigned char r = vec_adds(s1, s2);
vec_st(r,0,dst);

n -= 16; src1 += 16; src2 += 16; dst += 16;
}

// If any bytes are left over, deal with them individually
++n;
BASIC_MEAN();
}


void imageFilterAddTo_Altivec(unsigned char *dst, unsigned char *src, int length)
{
int n = length;

// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
ADDTO_PIXEL();
--n; ++dst; ++src;
}

// Do bulk of processing using Altivec (add 16 8-bit unsigned integers, with saturation)
while(n >= 16) {
vector unsigned char s = vec_ld(0,src);
vector unsigned char d = vec_ld(0,dst);
vector unsigned char r = vec_adds(d, s);
vec_st(r,0,dst);

n -= 16; src += 16; dst += 16;
}

// If any bytes are left over, deal with them individually
++n;
BASIC_ADDTO();
}


void imageFilterSubFrom_Altivec(unsigned char *dst, unsigned char *src, int length)
{
int n = length;

// Compute first few values so we're on a 16-byte boundary in dst
while( (((long)dst & 0xF) > 0) && (n > 0) ) {
SUBFROM_PIXEL();
--n; ++dst; ++src;
}

// Do bulk of processing using Altivec (sub 16 8-bit unsigned integers, with saturation)
while(n >= 16) {
vector unsigned char s = vec_ld(0,src);
vector unsigned char d = vec_ld(0,dst);
vector unsigned char r = vec_subs(d, s);
vec_st(r,0,dst);

n -= 16; src += 16; dst += 16;
}

// If any bytes are left over, deal with them individually
++n;
BASIC_SUBFROM();
}

#endif


28 changes: 28 additions & 0 deletions graphics_altivec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* -*- C++ -*-
*
* graphics_altivec.h - graphics routines using PPC Altivec cpu functionality
*
* Copyright (c) 2009 Mion. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifdef USE_PPC_GFX

void imageFilterMean_Altivec(unsigned char *src1, unsigned char *src2, unsigned char *dst, int length);
void imageFilterAddTo_Altivec(unsigned char *dst, unsigned char *src, int length);
void imageFilterSubFrom_Altivec(unsigned char *dst, unsigned char *src, int length);

#endif
46 changes: 40 additions & 6 deletions graphics_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,60 @@
#endif


#define BASIC_BLEND(){\
while(--n > 0) { \
BLEND_PIXEL(); \
++dst_buffer, ++src_buffer; \
} \
}

#define BASIC_ADDBLEND(){\
while(--n > 0) { \
ADDBLEND_PIXEL(); \
++dst_buffer, ++src_buffer; \
} \
}

#define BASIC_SUBBLEND(){\
while(--n > 0) { \
SUBBLEND_PIXEL(); \
++dst_buffer, ++src_buffer; \
} \
}


#define MEAN_PIXEL(){\
int result = ((int)(*src1) + (int)(*src2)) / 2; \
(*dst) = result; \
}

#define BASIC_MEAN(){\
while (--n > 0) { \
int result = ((int)(*src1) + (int)(*src2)) / 2; \
(*dst) = result; \
MEAN_PIXEL(); \
++dst; ++src1; ++src2; \
} \
}

#define ADDTO_PIXEL(){\
int result = (*dst) + (*src); \
(*dst) = (result < 255) ? result : 255; \
}

#define BASIC_ADDTO(){\
while (--n > 0) { \
int result = (*dst) + (*src); \
(*dst) = (result < 255) ? result : 255; \
ADDTO_PIXEL(); \
++dst, ++src; \
} \
}

#define SUBFROM_PIXEL(){\
int result = (*dst) - (*src); \
(*dst) = (result > 0) ? result : 0; \
}

#define BASIC_SUBFROM(){\
while(--n > 0) { \
int result = (*dst) - (*src); \
(*dst) = (result > 0) ? result : 0; \
SUBFROM_PIXEL(); \
++dst, ++src; \
} \
}
Expand Down
Loading

0 comments on commit 7cdbb73

Please sign in to comment.