Skip to content

Commit

Permalink
Added predictive frame limiter. Rather than busy looping, we keep tra…
Browse files Browse the repository at this point in the history
…ck of the time it takes to render, run the game loop, and present the frame. We then compare this against the time we last presented a frame, and once it crosses the point at which we don't have enough time to run the game loop again, we run a render loop and present it. By default there is a saftey zone of 10% (frameLimitPercent 0.90). On fast machines without power saving options enabled this can be pushed as small as 2% before frames start being late during gameplay, allowing for ~0.7 millisecond worst case scenario latency with Vsync on.
  • Loading branch information
xwidghet committed Oct 19, 2016
1 parent aec2955 commit 8fbd324
Show file tree
Hide file tree
Showing 19 changed files with 240 additions and 95 deletions.
3 changes: 2 additions & 1 deletion src/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,11 @@ void Actor::Draw()
{
if( !m_bVisible ||
m_fHibernateSecondsLeft > 0 ||
this->EarlyAbortDraw() )
this->EarlyAbortDraw() || !DISPLAY->ShouldRenderFrame() )
{
return; // early abort
}

if(m_FakeParent)
{
if(!m_FakeParent->m_bVisible || m_FakeParent->m_fHibernateSecondsLeft > 0
Expand Down
3 changes: 3 additions & 0 deletions src/ActorFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ void ActorFrame::BeginDraw()

void ActorFrame::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

if( m_bClearZBuffer )
{
LuaHelpers::ReportScriptErrorFmt( "ClearZBuffer not supported on ActorFrames" );
Expand Down
3 changes: 3 additions & 0 deletions src/ActorFrameTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ void ActorFrameTexture::DrawPrimitives()
if( m_pRenderTarget == NULL )
return;

if (!DISPLAY->ShouldRenderFrame())
return;

m_pRenderTarget->BeginRenderingTo( m_bPreserveTexture );

ActorFrame::DrawPrimitives();
Expand Down
3 changes: 3 additions & 0 deletions src/ActorMultiTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ void ActorMultiTexture::SetTextureMode( int iIndex, TextureMode tm )

void ActorMultiTexture::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

Actor::SetGlobalRenderStates(); // set Actor-specified render states

RectF quadVerticies;
Expand Down
3 changes: 3 additions & 0 deletions src/ActorMultiVertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ void ActorMultiVertex::SetVertexCoords( int index, float TexCoordX, float TexCoo

void ActorMultiVertex::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

Actor::SetGlobalRenderStates(); // set Actor-specified render states

DISPLAY->ClearAllTextures();
Expand Down
3 changes: 3 additions & 0 deletions src/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ void BackgroundImpl::Update( float fDeltaTime )

void BackgroundImpl::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

if( g_fBGBrightness == 0.0f )
return;

Expand Down
3 changes: 3 additions & 0 deletions src/BeginnerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ void BeginnerHelper::DrawPrimitives()
if( !m_bInitialized )
return;

if (!DISPLAY->ShouldRenderFrame())
return;

ActorFrame::DrawPrimitives();
m_sFlash.Draw();

Expand Down
11 changes: 8 additions & 3 deletions src/BitmapText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,11 @@ void BitmapText::BuildChars()
void BitmapText::DrawChars( bool bUseStrokeTexture )
{
// bail if cropped all the way
if( m_pTempState->crop.left + m_pTempState->crop.right >= 1 ||
m_pTempState->crop.top + m_pTempState->crop.bottom >= 1 )
return;
if (m_pTempState->crop.left + m_pTempState->crop.right >= 1 ||
m_pTempState->crop.top + m_pTempState->crop.bottom >= 1 || !DISPLAY->ShouldRenderFrame())
{
return;
}

const int iNumGlyphs = m_vpFontPageTextures.size();
int iStartGlyph = lround( SCALE( m_pTempState->crop.left, 0, 1, 0, iNumGlyphs ) );
Expand Down Expand Up @@ -670,6 +672,9 @@ bool BitmapText::EarlyAbortDraw() const
// draw text at x, y using colorTop blended down to colorBottom, with size multiplied by scale
void BitmapText::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

Actor::SetGlobalRenderStates(); // set Actor-specified render states
DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Modulate );

Expand Down
3 changes: 3 additions & 0 deletions src/DancingCharacters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ void DancingCharacters::Change2DAnimState( PlayerNumber pn, int iState )

void DancingCharacters::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

DISPLAY->CameraPushMatrix();

float fPercentIntoSweep;
Expand Down
9 changes: 9 additions & 0 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ bool Model::EarlyAbortDraw() const

void Model::DrawCelShaded()
{
if (!DISPLAY->ShouldRenderFrame())
return;

// First pass: shell. We only want the backfaces for this.
DISPLAY->SetCelShaded(1);
DISPLAY->SetCullMode(CULL_FRONT);
Expand All @@ -310,6 +313,9 @@ void Model::DrawCelShaded()

void Model::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

Actor::SetGlobalRenderStates(); // set Actor-specified render states

// Don't if we're fully transparent
Expand Down Expand Up @@ -465,6 +471,9 @@ void Model::DrawPrimitives()

void Model::DrawMesh( int i ) const
{
if (!DISPLAY->ShouldRenderFrame())
return;

const msMesh *pMesh = &m_pGeometry->m_Meshes[i];

// apply mesh-specific bone (if any)
Expand Down
17 changes: 16 additions & 1 deletion src/NoteDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ bool NoteDisplay::DrawHoldsInRange(const NoteFieldRenderArgs& field_args,
const NoteColumnRenderArgs& column_args,
const vector<NoteData::TrackMap::const_iterator>& tap_set)
{
if (!DISPLAY->ShouldRenderFrame())
return false;

bool any_upcoming = false;
for(vector<NoteData::TrackMap::const_iterator>::const_iterator tapit=
tap_set.begin(); tapit != tap_set.end(); ++tapit)
Expand Down Expand Up @@ -536,6 +539,9 @@ bool NoteDisplay::DrawTapsInRange(const NoteFieldRenderArgs& field_args,
const NoteColumnRenderArgs& column_args,
const vector<NoteData::TrackMap::const_iterator>& tap_set)
{
if (!DISPLAY->ShouldRenderFrame())
return false;

bool any_upcoming= false;
// draw notes from furthest to closest
for(vector<NoteData::TrackMap::const_iterator>::const_iterator tapit=
Expand Down Expand Up @@ -726,7 +732,10 @@ struct StripBuffer
}
void Draw()
{
DISPLAY->DrawSymmetricQuadStrip( buf, v-buf );
if( DISPLAY->ShouldRenderFrame() )
{
DISPLAY->DrawSymmetricQuadStrip(buf, v - buf);
}
}
int Used() const { return v - buf; }
int Free() const { return size - Used(); }
Expand Down Expand Up @@ -1365,6 +1374,9 @@ void NoteDisplay::DrawTap(const TapNote& tn,
bool bOnSameRowAsHoldStart, bool bOnSameRowAsRollStart,
bool bIsAddition, float fPercentFadeToFail)
{
if ( !DISPLAY->ShouldRenderFrame() )
return;

Actor* pActor = NULL;
NotePart part = NotePart_Tap;
/*
Expand Down Expand Up @@ -1511,6 +1523,9 @@ void NoteColumnRenderer::UpdateReceptorGhostStuff(Actor* receptor) const

void NoteColumnRenderer::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

m_column_render_args.song_beat= m_field_render_args->player_state->GetDisplayedPosition().m_fSongBeatVisible;
m_column_render_args.pos_handler= &NCR_current.m_pos_handler;
m_column_render_args.rot_handler= &NCR_current.m_rot_handler;
Expand Down
3 changes: 3 additions & 0 deletions src/NoteField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,9 @@ void NoteField::CalcPixelsBeforeAndAfterTargets()

void NoteField::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

//LOG->Trace( "NoteField::DrawPrimitives()" );

// This should be filled in on the first update.
Expand Down
3 changes: 3 additions & 0 deletions src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,9 @@ void Player::ApplyWaitingTransforms()

void Player::DrawPrimitives()
{
if (!DISPLAY->ShouldRenderFrame())
return;

// TODO: Remove use of PlayerNumber.
PlayerNumber pn = m_pPlayerState->m_PlayerNumber;

Expand Down
Loading

0 comments on commit 8fbd324

Please sign in to comment.