From 7ca20486efa9f9e9f517a8a531f325870c0a6544 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Tue, 22 Mar 2005 00:58:05 +0000 Subject: [PATCH] use sin lookup table to fix terrible performance with float and tipsy performance on some archs --- stepmania/src/Actor.cpp | 22 +++--- stepmania/src/ArrowEffects.cpp | 18 ++--- stepmania/src/GrooveRadar.cpp | 8 +-- stepmania/src/LifeMeterBar.cpp | 2 +- stepmania/src/MusicWheel.cpp | 8 +-- stepmania/src/NoteField.cpp | 8 +-- stepmania/src/RageDisplay.cpp | 4 +- stepmania/src/RageMath.cpp | 121 ++++++++++++++++++++++----------- stepmania/src/RageMath.h | 3 + 9 files changed, 120 insertions(+), 74 deletions(-) diff --git a/stepmania/src/Actor.cpp b/stepmania/src/Actor.cpp index 3cbc18d0eb..2baf004df8 100644 --- a/stepmania/src/Actor.cpp +++ b/stepmania/src/Actor.cpp @@ -193,7 +193,7 @@ void Actor::BeginDraw() // set the world matrix and calculate actor properties ssprintf("%f", fPercentThroughEffect) ); bool bBlinkOn = fPercentThroughEffect > 0.5f; - float fPercentBetweenColors = sinf( (fPercentThroughEffect + 0.25f) * 2 * PI ) / 2 + 0.5f; + float fPercentBetweenColors = RageFastSin( (fPercentThroughEffect + 0.25f) * 2 * PI ) / 2 + 0.5f; ASSERT_M( fPercentBetweenColors >= 0 && fPercentBetweenColors <= 1, ssprintf("%f, %f", fPercentBetweenColors, fPercentThroughEffect) ); float fOriginalAlpha = m_tempState.diffuse[0].a; @@ -225,15 +225,15 @@ void Actor::BeginDraw() // set the world matrix and calculate actor properties break; case rainbow: m_tempState.diffuse[0] = RageColor( - cosf( fPercentBetweenColors*2*PI ) * 0.5f + 0.5f, - cosf( fPercentBetweenColors*2*PI + PI * 2.0f / 3.0f ) * 0.5f + 0.5f, - cosf( fPercentBetweenColors*2*PI + PI * 4.0f / 3.0f) * 0.5f + 0.5f, + RageFastCos( fPercentBetweenColors*2*PI ) * 0.5f + 0.5f, + RageFastCos( fPercentBetweenColors*2*PI + PI * 2.0f / 3.0f ) * 0.5f + 0.5f, + RageFastCos( fPercentBetweenColors*2*PI + PI * 4.0f / 3.0f) * 0.5f + 0.5f, fOriginalAlpha ); for( int i=1; i<4; i++ ) m_tempState.diffuse[i] = m_tempState.diffuse[0]; break; case wag: - m_tempState.rotation += m_vEffectMagnitude * sinf( fPercentThroughEffect * 2.0f * PI ); + m_tempState.rotation += m_vEffectMagnitude * RageFastSin( fPercentThroughEffect * 2.0f * PI ); break; case spin: // nothing needs to be here @@ -245,7 +245,7 @@ void Actor::BeginDraw() // set the world matrix and calculate actor properties break; case bounce: { - float fPercentOffset = sinf( fPercentThroughEffect*PI ); + float fPercentOffset = RageFastSin( fPercentThroughEffect*PI ); m_tempState.pos += m_vEffectMagnitude * fPercentOffset; m_tempState.pos.x = roundf( m_tempState.pos.x ); m_tempState.pos.y = roundf( m_tempState.pos.y ); @@ -254,7 +254,7 @@ void Actor::BeginDraw() // set the world matrix and calculate actor properties break; case bob: { - float fPercentOffset = sinf( fPercentThroughEffect*PI*2 ); + float fPercentOffset = RageFastSin( fPercentThroughEffect*PI*2 ); m_tempState.pos += m_vEffectMagnitude * fPercentOffset; m_tempState.pos.x = roundf( m_tempState.pos.x ); m_tempState.pos.y = roundf( m_tempState.pos.y ); @@ -265,7 +265,7 @@ void Actor::BeginDraw() // set the world matrix and calculate actor properties { float fMinZoom = m_vEffectMagnitude[0]; float fMaxZoom = m_vEffectMagnitude[1]; - float fPercentOffset = sinf( fPercentThroughEffect*PI ); + float fPercentOffset = RageFastSin( fPercentThroughEffect*PI ); float fZoom = SCALE( fPercentOffset, 0.f, 1.f, fMinZoom, fMaxZoom ); m_tempState.scale *= fZoom; @@ -392,9 +392,9 @@ void Actor::UpdateTweening( float fDeltaTime ) case TWEEN_LINEAR: fPercentAlongPath = fPercentThroughTween; break; case TWEEN_ACCELERATE: fPercentAlongPath = fPercentThroughTween * fPercentThroughTween; break; case TWEEN_DECELERATE: fPercentAlongPath = 1 - (1-fPercentThroughTween) * (1-fPercentThroughTween); break; - case TWEEN_BOUNCE_BEGIN:fPercentAlongPath = 1 - sinf( 1.1f + fPercentThroughTween*(PI-1.1f) ) / 0.89f; break; - case TWEEN_BOUNCE_END: fPercentAlongPath = sinf( 1.1f + (1-fPercentThroughTween)*(PI-1.1f) ) / 0.89f; break; - case TWEEN_SPRING: fPercentAlongPath = 1 - cosf( fPercentThroughTween*PI*2.5f )/(1+fPercentThroughTween*3); break; + case TWEEN_BOUNCE_BEGIN:fPercentAlongPath = 1 - RageFastSin( 1.1f + fPercentThroughTween*(PI-1.1f) ) / 0.89f; break; + case TWEEN_BOUNCE_END: fPercentAlongPath = RageFastSin( 1.1f + (1-fPercentThroughTween)*(PI-1.1f) ) / 0.89f; break; + case TWEEN_SPRING: fPercentAlongPath = 1 - RageFastCos( fPercentThroughTween*PI*2.5f )/(1+fPercentThroughTween*3); break; default: ASSERT(0); } diff --git a/stepmania/src/ArrowEffects.cpp b/stepmania/src/ArrowEffects.cpp index fddefb680c..cb0b0a9538 100644 --- a/stepmania/src/ArrowEffects.cpp +++ b/stepmania/src/ArrowEffects.cpp @@ -79,7 +79,7 @@ float ArrowEffects::GetYOffset( const PlayerState* pPlayerState, int iCol, float fYAdjust += fBrakeYAdjust; } if( fAccels[PlayerOptions::ACCEL_WAVE] > 0 ) - fYAdjust += fAccels[PlayerOptions::ACCEL_WAVE] * 20.0f*sinf( fYOffset/38.0f ); + fYAdjust += fAccels[PlayerOptions::ACCEL_WAVE] * 20.0f*RageFastSin( fYOffset/38.0f ); fYOffset += fYAdjust; @@ -113,7 +113,7 @@ float ArrowEffects::GetYOffset( const PlayerState* pPlayerState, int iCol, float g_fExpandSeconds += timerExpand.GetDeltaTime(); else timerExpand.GetDeltaTime(); // throw away - float fExpandMultiplier = SCALE( cosf(g_fExpandSeconds*3), -1, 1, 0.5f, 1.5f ); + float fExpandMultiplier = SCALE( RageFastCos(g_fExpandSeconds*3), -1, 1, 0.5f, 1.5f ); fScrollSpeed *= SCALE( fAccels[PlayerOptions::ACCEL_EXPAND], 0.f, 1.f, 1.f, fExpandMultiplier ); } @@ -153,7 +153,7 @@ float ArrowEffects::GetYPos( const PlayerState* pPlayerState, int iCol, float fY const float* fEffects = pPlayerState->m_CurrentPlayerOptions.m_fEffects; if( fEffects[PlayerOptions::EFFECT_TIPSY] > 0 ) - f += fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*1.8f) * ARROW_SIZE*0.4f ); + f += fEffects[PlayerOptions::EFFECT_TIPSY] * ( RageFastCos( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*1.8f) * ARROW_SIZE*0.4f ); return f; } @@ -163,7 +163,7 @@ float ArrowEffects::GetYOffsetFromYPos( const PlayerState* pPlayerState, int iCo const float* fEffects = pPlayerState->m_CurrentPlayerOptions.m_fEffects; if( fEffects[PlayerOptions::EFFECT_TIPSY] > 0 ) - f -= fEffects[PlayerOptions::EFFECT_TIPSY] * ( cosf( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*2.f) * ARROW_SIZE*0.4f ); + f -= fEffects[PlayerOptions::EFFECT_TIPSY] * ( RageFastCos( RageTimer::GetTimeSinceStartFast()*1.2f + iCol*2.f) * ARROW_SIZE*0.4f ); float fShift, fScale; ArrowGetReverseShiftAndScale( pPlayerState, iCol, fYReverseOffsetPixels, fShift, fScale ); @@ -212,13 +212,13 @@ float ArrowEffects::GetXPos( const PlayerState* pPlayerState, int iColNum, float float fRads = acosf( fPositionBetween ); fRads += fYOffset * 6 / SCREEN_HEIGHT; - const float fAdjustedPixelOffset = SCALE( cosf(fRads), -1, 1, fMinX, fMaxX ); + const float fAdjustedPixelOffset = SCALE( RageFastCos(fRads), -1, 1, fMinX, fMaxX ); fPixelOffsetFromCenter += (fAdjustedPixelOffset - fRealPixelOffset) * fEffects[PlayerOptions::EFFECT_TORNADO]; } if( fEffects[PlayerOptions::EFFECT_DRUNK] > 0 ) - fPixelOffsetFromCenter += fEffects[PlayerOptions::EFFECT_DRUNK] * ( cosf( RageTimer::GetTimeSinceStartFast() + iColNum*0.2f + fYOffset*10/SCREEN_HEIGHT) * ARROW_SIZE*0.5f ); + fPixelOffsetFromCenter += fEffects[PlayerOptions::EFFECT_DRUNK] * ( RageFastCos( RageTimer::GetTimeSinceStartFast() + iColNum*0.2f + fYOffset*10/SCREEN_HEIGHT) * ARROW_SIZE*0.5f ); if( fEffects[PlayerOptions::EFFECT_FLIP] > 0 ) { // TODO: Don't index by PlayerNumber. @@ -270,7 +270,7 @@ float ArrowEffects::GetXPos( const PlayerState* pPlayerState, int iColNum, float if( bEvenBeat ) fAmount *= -1; - const float fShift = 20.0f*fAmount*sinf( fYOffset / 15.0f + PI/2.0f ); + const float fShift = 20.0f*fAmount*RageFastSin( fYOffset / 15.0f + PI/2.0f ); fPixelOffsetFromCenter += fEffects[PlayerOptions::EFFECT_BEAT] * fShift; } while(0); @@ -377,7 +377,7 @@ float ArrowGetPercentVisible( const PlayerState* pPlayerState, int iCol, float f fVisibleAdjust -= fAppearances[PlayerOptions::APPEARANCE_STEALTH]; if( fAppearances[PlayerOptions::APPEARANCE_BLINK] > 0 ) { - float f = sinf(RageTimer::GetTimeSinceStartFast()*10); + float f = RageFastSin(RageTimer::GetTimeSinceStartFast()*10); f = Quantize( f, 0.3333f ); fVisibleAdjust += SCALE( f, 0, 1, -1, 0 ); } @@ -432,7 +432,7 @@ float ArrowEffects::GetZPos( const PlayerState* pPlayerState, int iCol, float fY const float* fEffects = pPlayerState->m_CurrentPlayerOptions.m_fEffects; if( fEffects[PlayerOptions::EFFECT_BUMPY] > 0 ) - fZPos += fEffects[PlayerOptions::EFFECT_BUMPY] * 40*sinf( fYOffset/16.0f ); + fZPos += fEffects[PlayerOptions::EFFECT_BUMPY] * 40*RageFastSin( fYOffset/16.0f ); return fZPos; } diff --git a/stepmania/src/GrooveRadar.cpp b/stepmania/src/GrooveRadar.cpp index caca5898bf..dd1901e2d2 100644 --- a/stepmania/src/GrooveRadar.cpp +++ b/stepmania/src/GrooveRadar.cpp @@ -144,8 +144,8 @@ void GrooveRadar::GrooveRadarValueMap::DrawPrimitives() const float fDistFromCenter = ( m_fValuesOld[p][c] * (1-m_PercentTowardNew[p]) + m_fValuesNew[p][c] * m_PercentTowardNew[p] + 0.07f ) * fRadius; const float fRotation = RADAR_VALUE_ROTATION(i); - const float fX = cosf(fRotation) * fDistFromCenter; - const float fY = -sinf(fRotation) * fDistFromCenter; + const float fX = RageFastCos(fRotation) * fDistFromCenter; + const float fY = -RageFastSin(fRotation) * fDistFromCenter; v[1+i].p = RageVector3( fX, fY, 0 ); v[1+i].c = v[0].c; @@ -162,8 +162,8 @@ void GrooveRadar::GrooveRadarValueMap::DrawPrimitives() const float fDistFromCenter = ( m_fValuesOld[p][c] * (1-m_PercentTowardNew[p]) + m_fValuesNew[p][c] * m_PercentTowardNew[p] + 0.07f ) * fRadius; const float fRotation = RADAR_VALUE_ROTATION(i); - const float fX = cosf(fRotation) * fDistFromCenter; - const float fY = -sinf(fRotation) * fDistFromCenter; + const float fX = RageFastCos(fRotation) * fDistFromCenter; + const float fY = -RageFastSin(fRotation) * fDistFromCenter; v[i].p = RageVector3( fX, fY, 0 ); v[i].c = PLAYER_COLOR.GetValue( p ); diff --git a/stepmania/src/LifeMeterBar.cpp b/stepmania/src/LifeMeterBar.cpp index 4bd79e2201..637f91d4ee 100644 --- a/stepmania/src/LifeMeterBar.cpp +++ b/stepmania/src/LifeMeterBar.cpp @@ -514,7 +514,7 @@ void LifeMeterBar::DrawPrimitives() m_pStream->m_fPassingAlpha = m_fPassingAlpha; m_pStream->m_fHotAlpha = m_fHotAlpha; - float fPercentRed = (m_fTrailingLifePercentageGetNumTracks(); c++ ) // for each arrow column { diff --git a/stepmania/src/RageDisplay.cpp b/stepmania/src/RageDisplay.cpp index b5760c7d17..96e3de6e31 100644 --- a/stepmania/src/RageDisplay.cpp +++ b/stepmania/src/RageDisplay.cpp @@ -166,8 +166,8 @@ void RageDisplay::DrawCircleInternal( const RageSpriteVertex &p, float radius ) for(int i = 0; i < subdivisions+1; ++i) { const float fRotation = float(i) / subdivisions * 2*PI; - const float fX = cosf(fRotation) * radius; - const float fY = -sinf(fRotation) * radius; + const float fX = RageFastCos(fRotation) * radius; + const float fY = -RageFastSin(fRotation) * radius; v[1+i] = v[0]; v[1+i].p.x += fX; v[1+i].p.y += fY; diff --git a/stepmania/src/RageMath.cpp b/stepmania/src/RageMath.cpp index 3931f07804..fe2ab3683a 100644 --- a/stepmania/src/RageMath.cpp +++ b/stepmania/src/RageMath.cpp @@ -196,10 +196,10 @@ void RageMatrixRotationX( RageMatrix* pOut, float theta ) theta *= PI/180; RageMatrixIdentity(pOut); - pOut->m[1][1] = cosf(theta); + pOut->m[1][1] = RageFastCos(theta); pOut->m[2][2] = pOut->m[1][1]; - pOut->m[2][1] = sinf(theta); + pOut->m[2][1] = RageFastSin(theta); pOut->m[1][2] = -pOut->m[2][1]; } @@ -208,10 +208,10 @@ void RageMatrixRotationY( RageMatrix* pOut, float theta ) theta *= PI/180; RageMatrixIdentity(pOut); - pOut->m[0][0] = cosf(theta); + pOut->m[0][0] = RageFastCos(theta); pOut->m[2][2] = pOut->m[0][0]; - pOut->m[0][2] = sinf(theta); + pOut->m[0][2] = RageFastSin(theta); pOut->m[2][0] = -pOut->m[0][2]; } @@ -220,10 +220,10 @@ void RageMatrixRotationZ( RageMatrix* pOut, float theta ) theta *= PI/180; RageMatrixIdentity(pOut); - pOut->m[0][0] = cosf(theta); + pOut->m[0][0] = RageFastCos(theta); pOut->m[1][1] = pOut->m[0][0]; - pOut->m[0][1] = sinf(theta); + pOut->m[0][1] = RageFastSin(theta); pOut->m[1][0] = -pOut->m[0][1]; } @@ -236,12 +236,12 @@ void RageMatrixRotationXYZ( RageMatrix* pOut, float rX, float rY, float rZ ) rY *= PI/180; rZ *= PI/180; - const float cX = cosf(rX); - const float sX = sinf(rX); - const float cY = cosf(rY); - const float sY = sinf(rY); - const float cZ = cosf(rZ); - const float sZ = sinf(rZ); + const float cX = RageFastCos(rX); + const float sX = RageFastSin(rX); + const float cY = RageFastCos(rY); + const float sY = RageFastSin(rY); + const float cZ = RageFastCos(rZ); + const float sZ = RageFastSin(rZ); /* * X*Y: @@ -309,8 +309,8 @@ RageVector4 RageQuatFromH(float theta ) theta *= PI/180.0f; theta /= 2.0f; theta *= -1; - const float c = cosf(theta); - const float s = sinf(theta); + const float c = RageFastCos(theta); + const float s = RageFastSin(theta); return RageVector4(0, s, 0, c); } @@ -320,8 +320,8 @@ RageVector4 RageQuatFromP(float theta ) theta *= PI/180.0f; theta /= 2.0f; theta *= -1; - const float c = cosf(theta); - const float s = sinf(theta); + const float c = RageFastCos(theta); + const float s = RageFastSin(theta); return RageVector4(s, 0, 0, c); } @@ -331,8 +331,8 @@ RageVector4 RageQuatFromR(float theta ) theta *= PI/180.0f; theta /= 2.0f; theta *= -1; - const float c = cosf(theta); - const float s = sinf(theta); + const float c = RageFastCos(theta); + const float s = RageFastSin(theta); return RageVector4(0, 0, s, c); } @@ -347,12 +347,12 @@ void RageQuatFromHPR(RageVector4* pOut, RageVector3 hpr ) hpr /= 180.0f; hpr /= 2.0f; - const float sX = sinf(hpr.x); - const float cX = cosf(hpr.x); - const float sY = sinf(hpr.y); - const float cY = cosf(hpr.y); - const float sZ = sinf(hpr.z); - const float cZ = cosf(hpr.z); + const float sX = RageFastSin(hpr.x); + const float cX = RageFastCos(hpr.x); + const float sY = RageFastSin(hpr.y); + const float cY = RageFastCos(hpr.y); + const float sZ = RageFastSin(hpr.z); + const float cZ = RageFastCos(hpr.z); pOut->w = cX * cY * cZ + sX * sY * sZ; pOut->x = sX * cY * cZ - cX * sY * sZ; @@ -375,12 +375,12 @@ void RageQuatFromPRH(RageVector4* pOut, RageVector3 prh ) /* Set cX to the cosine of the angle we want to rotate on the X axis, * and so on. Here, hpr.z (roll) rotates on the Z axis, hpr.x (heading) * on Y, and hpr.y (pitch) on X. */ - const float sX = sinf(prh.y); - const float cX = cosf(prh.y); - const float sY = sinf(prh.x); - const float cY = cosf(prh.x); - const float sZ = sinf(prh.z); - const float cZ = cosf(prh.z); + const float sX = RageFastSin(prh.y); + const float cX = RageFastCos(prh.y); + const float sY = RageFastSin(prh.x); + const float cY = RageFastCos(prh.x); + const float sZ = RageFastSin(prh.z); + const float cZ = RageFastCos(prh.z); pOut->w = cX * cY * cZ + sX * sY * sZ; pOut->x = sX * cY * cZ - cX * sY * sZ; @@ -439,9 +439,9 @@ void RageQuatSlerp(RageVector4 *pOut, const RageVector4 &from, const RageVector4 { // standard case (slerp) float omega = acosf(cosom); - float sinom = sinf(omega); - scale0 = sinf((1.0f - t) * omega) / sinom; - scale1 = sinf(t * omega) / sinom; + float sinom = RageFastSin(omega); + scale0 = RageFastSin((1.0f - t) * omega) / sinom; + scale1 = RageFastSin(t * omega) / sinom; } else { // "from" and "to" quaternions are very close // ... so we can do a linear interpolation @@ -497,12 +497,12 @@ void RageMatrixAngles( RageMatrix* pOut, const RageVector3 &angles ) { const RageVector3 angles_radians( angles * 2*PI / 360 ); - const float sy = sinf( angles_radians[2] ); - const float cy = cosf( angles_radians[2] ); - const float sp = sinf( angles_radians[1] ); - const float cp = cosf( angles_radians[1] ); - const float sr = sinf( angles_radians[0] ); - const float cr = cosf( angles_radians[0] ); + const float sy = RageFastSin( angles_radians[2] ); + const float cy = RageFastCos( angles_radians[2] ); + const float sp = RageFastSin( angles_radians[1] ); + const float cp = RageFastCos( angles_radians[1] ); + const float sr = RageFastSin( angles_radians[0] ); + const float cr = RageFastCos( angles_radians[0] ); RageMatrixIdentity( pOut ); @@ -526,6 +526,49 @@ void RageMatrixTranspose( RageMatrix* pOut, const RageMatrix* pIn ) pOut->m[j][i] = pIn->m[i][j]; } +float RageFastSin( float x ) +{ + // from 0 to PI + // sizeof(table) == 4096 == one page of memory in Windows + static float table[1024]; + + static bool bInited = false; + if( !bInited ) + { + bInited = true; + for( int i=0; i=0 && i= ARRAYSIZE(table) ) // PI <= i < 2*PI + { + // sin(x) == -sin(2*PI-x) + i = ARRAYSIZE(table)*2 - 1 - i; // mirror about ARRAYSIZE(table) + DEBUG_ASSERT( i>=0 && i