doom3-bfg/neo/framework/common_frame.cpp

858 lines
26 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code 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 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "../idlib/precompiled.h"
#pragma hdrstop
#include "Common_local.h"
#include "../renderer/Image.h"
#include "../renderer/ImageOpts.h"
#include "../../doomclassic/doom/doomlib.h"
#include "../../doomclassic/doom/globaldata.h"
/*
New for tech4x:
Unlike previous SMP work, the actual GPU command drawing is done in the main thread, which avoids the
OpenGL problems with needing windows to be created by the same thread that creates the context, as well
as the issues with passing context ownership back and forth on the 360.
The game tic and the generation of the draw command list is now run in a separate thread, and overlapped
with the interpretation of the previous draw command list.
While the game tic should be nicely contained, the draw command generation winds through the user interface
code, and is potentially hazardous. For now, the overlap will be restricted to the renderer back end,
which should also be nicely contained.
*/
#define DEFAULT_FIXED_TIC "0"
#define DEFAULT_NO_SLEEP "0"
idCVar com_deltaTimeClamp( "com_deltaTimeClamp", "50", CVAR_INTEGER, "don't process more than this time in a single frame" );
idCVar com_fixedTic( "com_fixedTic", DEFAULT_FIXED_TIC, CVAR_BOOL, "run a single game frame per render frame" );
idCVar com_noSleep( "com_noSleep", DEFAULT_NO_SLEEP, CVAR_BOOL, "don't sleep if the game is running too fast" );
idCVar com_smp( "com_smp", "1", CVAR_BOOL | CVAR_SYSTEM | CVAR_NOCHEAT, "run the game and draw code in a separate thread" );
2012-11-26 18:58:24 +00:00
idCVar com_aviDemoSamples( "com_aviDemoSamples", "16", CVAR_SYSTEM, "" );
idCVar com_aviDemoWidth( "com_aviDemoWidth", "256", CVAR_SYSTEM, "" );
idCVar com_aviDemoHeight( "com_aviDemoHeight", "256", CVAR_SYSTEM, "" );
idCVar com_skipGameDraw( "com_skipGameDraw", "0", CVAR_SYSTEM | CVAR_BOOL, "" );
idCVar com_sleepGame( "com_sleepGame", "0", CVAR_SYSTEM | CVAR_INTEGER, "intentionally add a sleep in the game time" );
idCVar com_sleepDraw( "com_sleepDraw", "0", CVAR_SYSTEM | CVAR_INTEGER, "intentionally add a sleep in the draw time" );
idCVar com_sleepRender( "com_sleepRender", "0", CVAR_SYSTEM | CVAR_INTEGER, "intentionally add a sleep in the render time" );
idCVar net_drawDebugHud( "net_drawDebugHud", "0", CVAR_SYSTEM | CVAR_INTEGER, "0 = None, 1 = Hud 1, 2 = Hud 2, 3 = Snapshots" );
idCVar timescale( "timescale", "1", CVAR_SYSTEM | CVAR_FLOAT, "Number of game frames to run per render frame", 0.001f, 100.0f );
extern idCVar in_useJoystick;
extern idCVar in_joystickRumble;
/*
===============
idGameThread::Run
Run in a background thread for performance, but can also
be called directly in the foreground thread for comparison.
===============
*/
int idGameThread::Run()
{
2012-11-26 18:58:24 +00:00
commonLocal.frameTiming.startGameTime = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
// debugging tool to test frame dropping behavior
if( com_sleepGame.GetInteger() )
{
2012-11-26 18:58:24 +00:00
Sys_Sleep( com_sleepGame.GetInteger() );
}
if( numGameFrames == 0 )
{
2012-11-26 18:58:24 +00:00
// Ensure there's no stale gameReturn data from a paused game
ret = gameReturn_t();
}
if( isClient )
{
2012-11-26 18:58:24 +00:00
// run the game logic
for( int i = 0; i < numGameFrames; i++ )
{
2012-11-26 18:58:24 +00:00
SCOPED_PROFILE_EVENT( "Client Prediction" );
if( userCmdMgr )
{
2012-11-26 18:58:24 +00:00
game->ClientRunFrame( *userCmdMgr, ( i == numGameFrames - 1 ), ret );
}
if( ret.syncNextGameFrame || ret.sessionCommand[0] != 0 )
{
2012-11-26 18:58:24 +00:00
break;
}
}
}
else
{
2012-11-26 18:58:24 +00:00
// run the game logic
for( int i = 0; i < numGameFrames; i++ )
{
2012-11-26 18:58:24 +00:00
SCOPED_PROFILE_EVENT( "GameTic" );
if( userCmdMgr )
{
2012-11-26 18:58:24 +00:00
game->RunFrame( *userCmdMgr, ret );
}
if( ret.syncNextGameFrame || ret.sessionCommand[0] != 0 )
{
2012-11-26 18:58:24 +00:00
break;
}
}
}
2012-11-26 18:58:24 +00:00
// we should have consumed all of our usercmds
if( userCmdMgr )
{
if( userCmdMgr->HasUserCmdForPlayer( game->GetLocalClientNum() ) && common->GetCurrentGame() == DOOM3_BFG )
{
2012-11-26 18:58:24 +00:00
idLib::Printf( "idGameThread::Run: didn't consume all usercmds\n" );
}
}
2012-11-26 18:58:24 +00:00
commonLocal.frameTiming.finishGameTime = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
SetThreadGameTime( ( commonLocal.frameTiming.finishGameTime - commonLocal.frameTiming.startGameTime ) / 1000 );
2012-11-26 18:58:24 +00:00
// build render commands and geometry
{
SCOPED_PROFILE_EVENT( "Draw" );
commonLocal.Draw();
}
2012-11-26 18:58:24 +00:00
commonLocal.frameTiming.finishDrawTime = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
SetThreadRenderTime( ( commonLocal.frameTiming.finishDrawTime - commonLocal.frameTiming.finishGameTime ) / 1000 );
2012-11-26 18:58:24 +00:00
SetThreadTotalTime( ( commonLocal.frameTiming.finishDrawTime - commonLocal.frameTiming.startGameTime ) / 1000 );
2012-11-26 18:58:24 +00:00
return 0;
}
/*
===============
idGameThread::RunGameAndDraw
===============
*/
gameReturn_t idGameThread::RunGameAndDraw( int numGameFrames_, idUserCmdMgr& userCmdMgr_, bool isClient_, int startGameFrame )
{
2012-11-26 18:58:24 +00:00
// this should always immediately return
this->WaitForThread();
2012-11-26 18:58:24 +00:00
// save the usercmds for the background thread to pick up
userCmdMgr = &userCmdMgr_;
2012-11-26 18:58:24 +00:00
isClient = isClient_;
2012-11-26 18:58:24 +00:00
// grab the return value created by the last thread execution
gameReturn_t latchedRet = ret;
2012-11-26 18:58:24 +00:00
numGameFrames = numGameFrames_;
2012-11-26 18:58:24 +00:00
// start the thread going
if( com_smp.GetBool() == false )
{
2012-11-26 18:58:24 +00:00
// run it in the main thread so PIX profiling catches everything
Run();
}
else
{
2012-11-26 18:58:24 +00:00
this->SignalWork();
}
2012-11-26 18:58:24 +00:00
// return the latched result while the thread runs in the background
return latchedRet;
}
/*
===============
idCommonLocal::DrawWipeModel
Draw the fade material over everything that has been drawn
===============
*/
void idCommonLocal::DrawWipeModel()
{
2012-11-26 18:58:24 +00:00
if( wipeStartTime >= wipeStopTime )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
int currentTime = Sys_Milliseconds();
if( !wipeHold && currentTime > wipeStopTime )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
float fade = ( float )( currentTime - wipeStartTime ) / ( wipeStopTime - wipeStartTime );
renderSystem->SetColor4( 1, 1, 1, fade );
renderSystem->DrawStretchPic( 0, 0, 640, 480, 0, 0, 1, 1, wipeMaterial );
}
/*
===============
idCommonLocal::Draw
===============
*/
void idCommonLocal::Draw()
{
2012-11-26 18:58:24 +00:00
// debugging tool to test frame dropping behavior
if( com_sleepDraw.GetInteger() )
{
2012-11-26 18:58:24 +00:00
Sys_Sleep( com_sleepDraw.GetInteger() );
}
if( loadGUI != NULL )
{
2012-11-26 18:58:24 +00:00
loadGUI->Render( renderSystem, Sys_Milliseconds() );
}
else if( currentGame == DOOM_CLASSIC || currentGame == DOOM2_CLASSIC )
{
2012-11-26 18:58:24 +00:00
const float sysWidth = renderSystem->GetWidth() * renderSystem->GetPixelAspect();
const float sysHeight = renderSystem->GetHeight();
const float sysAspect = sysWidth / sysHeight;
const float doomAspect = 4.0f / 3.0f;
const float adjustment = sysAspect / doomAspect;
const float barHeight = ( adjustment >= 1.0f ) ? 0.0f : ( 1.0f - adjustment ) * ( float )SCREEN_HEIGHT * 0.25f;
const float barWidth = ( adjustment <= 1.0f ) ? 0.0f : ( adjustment - 1.0f ) * ( float )SCREEN_WIDTH * 0.25f;
if( barHeight > 0.0f )
{
2012-11-26 18:58:24 +00:00
renderSystem->SetColor( colorBlack );
renderSystem->DrawStretchPic( 0, 0, SCREEN_WIDTH, barHeight, 0, 0, 1, 1, whiteMaterial );
renderSystem->DrawStretchPic( 0, SCREEN_HEIGHT - barHeight, SCREEN_WIDTH, barHeight, 0, 0, 1, 1, whiteMaterial );
}
if( barWidth > 0.0f )
{
2012-11-26 18:58:24 +00:00
renderSystem->SetColor( colorBlack );
renderSystem->DrawStretchPic( 0, 0, barWidth, SCREEN_HEIGHT, 0, 0, 1, 1, whiteMaterial );
renderSystem->DrawStretchPic( SCREEN_WIDTH - barWidth, 0, barWidth, SCREEN_HEIGHT, 0, 0, 1, 1, whiteMaterial );
}
renderSystem->SetColor4( 1, 1, 1, 1 );
renderSystem->DrawStretchPic( barWidth, barHeight, SCREEN_WIDTH - barWidth * 2.0f, SCREEN_HEIGHT - barHeight * 2.0f, 0, 0, 1, 1, doomClassicMaterial );
}
else if( game && game->Shell_IsActive() )
{
2012-11-26 18:58:24 +00:00
bool gameDraw = game->Draw( game->GetLocalClientNum() );
if( !gameDraw )
{
2012-11-26 18:58:24 +00:00
renderSystem->SetColor( colorBlack );
renderSystem->DrawStretchPic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 1, 1, whiteMaterial );
}
game->Shell_Render();
}
else if( readDemo )
{
2012-11-26 18:58:24 +00:00
renderWorld->RenderScene( &currentDemoRenderView );
renderSystem->DrawDemoPics();
}
else if( mapSpawned )
{
2012-11-26 18:58:24 +00:00
bool gameDraw = false;
// normal drawing for both single and multi player
if( !com_skipGameDraw.GetBool() && Game()->GetLocalClientNum() >= 0 )
{
2012-11-26 18:58:24 +00:00
// draw the game view
int start = Sys_Milliseconds();
if( game )
{
2012-11-26 18:58:24 +00:00
gameDraw = game->Draw( Game()->GetLocalClientNum() );
}
int end = Sys_Milliseconds();
time_gameDraw += ( end - start ); // note time used for com_speeds
}
if( !gameDraw )
{
2012-11-26 18:58:24 +00:00
renderSystem->SetColor( colorBlack );
renderSystem->DrawStretchPic( 0, 0, 640, 480, 0, 0, 1, 1, whiteMaterial );
}
2012-11-26 18:58:24 +00:00
// save off the 2D drawing from the game
if( writeDemo )
{
2012-11-26 18:58:24 +00:00
renderSystem->WriteDemoPics();
}
}
else
{
2012-11-26 18:58:24 +00:00
renderSystem->SetColor4( 0, 0, 0, 1 );
renderSystem->DrawStretchPic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 1, 1, whiteMaterial );
}
2012-11-26 18:58:24 +00:00
{
SCOPED_PROFILE_EVENT( "Post-Draw" );
2012-11-26 18:58:24 +00:00
// draw the wipe material on top of this if it hasn't completed yet
DrawWipeModel();
2012-11-26 18:58:24 +00:00
Dialog().Render( loadGUI != NULL );
2012-11-26 18:58:24 +00:00
// draw the half console / notify console on top of everything
console->Draw( false );
}
}
/*
===============
idCommonLocal::UpdateScreen
This is an out-of-sequence screen update, not the normal game rendering
===============
*/
void idCommonLocal::UpdateScreen( bool captureToImage )
{
if( insideUpdateScreen )
{
2012-11-26 18:58:24 +00:00
return;
}
insideUpdateScreen = true;
2012-11-26 18:58:24 +00:00
// make sure the game / draw thread has completed
gameThread.WaitForThread();
2012-11-26 18:58:24 +00:00
// release the mouse capture back to the desktop
Sys_GrabMouseCursor( false );
2012-11-26 18:58:24 +00:00
// build all the draw commands without running a new game tic
Draw();
if( captureToImage )
{
2012-11-26 18:58:24 +00:00
renderSystem->CaptureRenderToImage( "_currentRender", false );
}
2012-11-26 18:58:24 +00:00
// this should exit right after vsync, with the GPU idle and ready to draw
const emptyCommand_t* cmd = renderSystem->SwapCommandBuffers( &time_frontend, &time_backend, &time_shadows, &time_gpu );
2012-11-26 18:58:24 +00:00
// get the GPU busy with new commands
renderSystem->RenderCommandBuffers( cmd );
2012-11-26 18:58:24 +00:00
insideUpdateScreen = false;
}
/*
================
idCommonLocal::ProcessGameReturn
================
*/
void idCommonLocal::ProcessGameReturn( const gameReturn_t& ret )
{
2012-11-26 18:58:24 +00:00
// set joystick rumble
if( in_useJoystick.GetBool() && in_joystickRumble.GetBool() && !game->Shell_IsActive() && session->GetSignInManager().GetMasterInputDevice() >= 0 )
{
2012-11-26 18:58:24 +00:00
Sys_SetRumble( session->GetSignInManager().GetMasterInputDevice(), ret.vibrationLow, ret.vibrationHigh ); // Only set the rumble on the active controller
}
else
{
for( int i = 0; i < MAX_INPUT_DEVICES; i++ )
{
2012-11-26 18:58:24 +00:00
Sys_SetRumble( i, 0, 0 );
}
}
2012-11-26 18:58:24 +00:00
syncNextGameFrame = ret.syncNextGameFrame;
if( ret.sessionCommand[0] )
{
2012-11-26 18:58:24 +00:00
idCmdArgs args;
2012-11-26 18:58:24 +00:00
args.TokenizeString( ret.sessionCommand, false );
if( !idStr::Icmp( args.Argv( 0 ), "map" ) )
{
2012-11-26 18:58:24 +00:00
MoveToNewMap( args.Argv( 1 ), false );
}
else if( !idStr::Icmp( args.Argv( 0 ), "devmap" ) )
{
2012-11-26 18:58:24 +00:00
MoveToNewMap( args.Argv( 1 ), true );
}
else if( !idStr::Icmp( args.Argv( 0 ), "died" ) )
{
if( !IsMultiplayer() )
{
2012-11-26 18:58:24 +00:00
game->Shell_Show( true );
}
}
else if( !idStr::Icmp( args.Argv( 0 ), "disconnect" ) )
{
2012-11-26 18:58:24 +00:00
cmdSystem->BufferCommandText( CMD_EXEC_INSERT, "stoprecording ; disconnect" );
}
else if( !idStr::Icmp( args.Argv( 0 ), "endOfDemo" ) )
{
2012-11-26 18:58:24 +00:00
cmdSystem->BufferCommandText( CMD_EXEC_NOW, "endOfDemo" );
}
}
}
extern idCVar com_forceGenericSIMD;
/*
=================
idCommonLocal::Frame
=================
*/
void idCommonLocal::Frame()
{
try
{
2012-11-26 18:58:24 +00:00
SCOPED_PROFILE_EVENT( "Common::Frame" );
2012-11-26 18:58:24 +00:00
// This is the only place this is incremented
idLib::frameNumber++;
2012-11-26 18:58:24 +00:00
// allow changing SIMD usage on the fly
if( com_forceGenericSIMD.IsModified() )
{
2012-11-26 18:58:24 +00:00
idSIMD::InitProcessor( "doom", com_forceGenericSIMD.GetBool() );
com_forceGenericSIMD.ClearModified();
}
2012-11-26 18:58:24 +00:00
// Do the actual switch between Doom 3 and the classics here so
// that things don't get confused in the middle of the frame.
PerformGameSwitch();
2012-11-26 18:58:24 +00:00
// pump all the events
Sys_GenerateEvents();
2012-11-26 18:58:24 +00:00
// write config file if anything changed
WriteConfiguration();
2012-11-26 18:58:24 +00:00
eventLoop->RunEventLoop();
2012-11-26 18:58:24 +00:00
// Activate the shell if it's been requested
if( showShellRequested && game )
{
2012-11-26 18:58:24 +00:00
game->Shell_Show( true );
showShellRequested = false;
}
2012-11-26 18:58:24 +00:00
// if the console or another gui is down, we don't need to hold the mouse cursor
bool chatting = false;
if( console->Active() || Dialog().IsDialogActive() || session->IsSystemUIShowing() || ( game && game->InhibitControls() && !IsPlayingDoomClassic() ) )
{
2012-11-26 18:58:24 +00:00
Sys_GrabMouseCursor( false );
usercmdGen->InhibitUsercmd( INHIBIT_SESSION, true );
chatting = true;
}
else
{
2012-11-26 18:58:24 +00:00
Sys_GrabMouseCursor( true );
usercmdGen->InhibitUsercmd( INHIBIT_SESSION, false );
}
2012-11-26 18:58:24 +00:00
const bool pauseGame = ( !mapSpawned || ( !IsMultiplayer() && ( Dialog().IsDialogPausing() || session->IsSystemUIShowing() || ( game && game->Shell_IsActive() ) ) ) ) && !IsPlayingDoomClassic();
2012-11-26 18:58:24 +00:00
// save the screenshot and audio from the last draw if needed
if( aviCaptureMode )
{
idStr name = va( "demos/%s/%s_%05i.tga", aviDemoShortName.c_str(), aviDemoShortName.c_str(), aviDemoFrameCount++ );
2012-11-26 18:58:24 +00:00
renderSystem->TakeScreenshot( com_aviDemoWidth.GetInteger(), com_aviDemoHeight.GetInteger(), name, com_aviDemoSamples.GetInteger(), NULL );
2012-11-26 18:58:24 +00:00
// remove any printed lines at the top before taking the screenshot
console->ClearNotifyLines();
2012-11-26 18:58:24 +00:00
// this will call Draw, possibly multiple times if com_aviDemoSamples is > 1
renderSystem->TakeScreenshot( com_aviDemoWidth.GetInteger(), com_aviDemoHeight.GetInteger(), name, com_aviDemoSamples.GetInteger(), NULL );
}
2012-11-26 18:58:24 +00:00
//--------------------------------------------
// wait for the GPU to finish drawing
//
// It is imporant to minimize the time spent between this
// section and the call to renderSystem->RenderCommandBuffers(),
// because the GPU is completely idle.
//--------------------------------------------
// this should exit right after vsync, with the GPU idle and ready to draw
// This may block if the GPU isn't finished renderng the previous frame.
frameTiming.startSyncTime = Sys_Microseconds();
const emptyCommand_t* renderCommands = NULL;
if( com_smp.GetBool() )
{
2012-11-26 18:58:24 +00:00
renderCommands = renderSystem->SwapCommandBuffers( &time_frontend, &time_backend, &time_shadows, &time_gpu );
}
else
{
2012-11-26 18:58:24 +00:00
// the GPU will stay idle through command generation for minimal
// input latency
renderSystem->SwapCommandBuffers_FinishRendering( &time_frontend, &time_backend, &time_shadows, &time_gpu );
}
frameTiming.finishSyncTime = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
//--------------------------------------------
// Determine how many game tics we are going to run,
// now that the previous frame is completely finished.
//
// It is important that any waiting on the GPU be done
// before this, or there will be a bad stuttering when
// dropping frames for performance management.
//--------------------------------------------
2012-11-26 18:58:24 +00:00
// input:
// thisFrameTime
// com_noSleep
// com_engineHz
// com_fixedTic
// com_deltaTimeClamp
// IsMultiplayer
//
// in/out state:
// gameFrame
// gameTimeResidual
// lastFrameTime
// syncNextFrame
//
// Output:
// numGameFrames
2012-11-26 18:58:24 +00:00
// How many game frames to run
int numGameFrames = 0;
for( ;; )
{
2012-11-26 18:58:24 +00:00
const int thisFrameTime = Sys_Milliseconds();
static int lastFrameTime = thisFrameTime; // initialized only the first time
const int deltaMilliseconds = thisFrameTime - lastFrameTime;
lastFrameTime = thisFrameTime;
2012-11-26 18:58:24 +00:00
// if there was a large gap in time since the last frame, or the frame
// rate is very very low, limit the number of frames we will run
const int clampedDeltaMilliseconds = Min( deltaMilliseconds, com_deltaTimeClamp.GetInteger() );
2012-11-26 18:58:24 +00:00
gameTimeResidual += clampedDeltaMilliseconds * timescale.GetFloat();
2012-11-26 18:58:24 +00:00
// don't run any frames when paused
if( pauseGame )
{
2012-11-26 18:58:24 +00:00
gameFrame++;
gameTimeResidual = 0;
break;
}
2012-11-26 18:58:24 +00:00
// debug cvar to force multiple game tics
if( com_fixedTic.GetInteger() > 0 )
{
2012-11-26 18:58:24 +00:00
numGameFrames = com_fixedTic.GetInteger();
gameFrame += numGameFrames;
gameTimeResidual = 0;
break;
}
if( syncNextGameFrame )
{
2012-11-26 18:58:24 +00:00
// don't sleep at all
syncNextGameFrame = false;
gameFrame++;
numGameFrames++;
gameTimeResidual = 0;
break;
}
for( ;; )
{
2012-11-26 18:58:24 +00:00
// How much time to wait before running the next frame,
// based on com_engineHz
const int frameDelay = FRAME_TO_MSEC( gameFrame + 1 ) - FRAME_TO_MSEC( gameFrame );
if( gameTimeResidual < frameDelay )
{
2012-11-26 18:58:24 +00:00
break;
}
gameTimeResidual -= frameDelay;
gameFrame++;
numGameFrames++;
// if there is enough residual left, we may run additional frames
}
if( numGameFrames > 0 )
{
2012-11-26 18:58:24 +00:00
// ready to actually run them
break;
}
2012-11-26 18:58:24 +00:00
// if we are vsyncing, we always want to run at least one game
// frame and never sleep, which might happen due to scheduling issues
// if we were just looking at real time.
if( com_noSleep.GetBool() )
{
2012-11-26 18:58:24 +00:00
numGameFrames = 1;
gameFrame += numGameFrames;
gameTimeResidual = 0;
break;
}
2012-11-26 18:58:24 +00:00
// not enough time has passed to run a frame, as might happen if
// we don't have vsync on, or the monitor is running at 120hz while
// com_engineHz is 60, so sleep a bit and check again
Sys_Sleep( 0 );
}
2012-11-26 18:58:24 +00:00
//--------------------------------------------
// It would be better to push as much of this as possible
// either before or after the renderSystem->SwapCommandBuffers(),
// because the GPU is completely idle.
//--------------------------------------------
2012-11-26 18:58:24 +00:00
// Update session and syncronize to the new session state after sleeping
session->UpdateSignInManager();
session->Pump();
session->ProcessSnapAckQueue();
if( session->GetState() == idSession::LOADING )
{
2012-11-26 18:58:24 +00:00
// If the session reports we should be loading a map, load it!
ExecuteMapChange();
mapSpawnData.savegameFile = NULL;
mapSpawnData.persistentPlayerInfo.Clear();
return;
}
else if( session->GetState() != idSession::INGAME && mapSpawned )
{
2012-11-26 18:58:24 +00:00
// If the game is running, but the session reports we are not in a game, disconnect
// This happens when a server disconnects us or we sign out
LeaveGame();
return;
}
if( mapSpawned && !pauseGame )
{
if( IsClient() )
{
2012-11-26 18:58:24 +00:00
RunNetworkSnapshotFrame();
}
}
2012-11-26 18:58:24 +00:00
ExecuteReliableMessages();
2012-11-26 18:58:24 +00:00
// send frame and mouse events to active guis
GuiFrameEvents();
2012-11-26 18:58:24 +00:00
//--------------------------------------------
// Prepare usercmds and kick off the game processing
// in a background thread
//--------------------------------------------
2012-11-26 18:58:24 +00:00
// get the previous usercmd for bypassed head tracking transform
const usercmd_t previousCmd = usercmdGen->GetCurrentUsercmd();
2012-11-26 18:58:24 +00:00
// build a new usercmd
int deviceNum = session->GetSignInManager().GetMasterInputDevice();
usercmdGen->BuildCurrentUsercmd( deviceNum );
if( deviceNum == -1 )
{
for( int i = 0; i < MAX_INPUT_DEVICES; i++ )
{
2012-11-26 18:58:24 +00:00
Sys_PollJoystickInputEvents( i );
Sys_EndJoystickInputEvents();
}
}
if( pauseGame )
{
2012-11-26 18:58:24 +00:00
usercmdGen->Clear();
}
2012-11-26 18:58:24 +00:00
usercmd_t newCmd = usercmdGen->GetCurrentUsercmd();
2012-11-26 18:58:24 +00:00
// Store server game time - don't let time go past last SS time in case we are extrapolating
if( IsClient() )
{
2012-11-26 18:58:24 +00:00
newCmd.serverGameMilliseconds = std::min( Game()->GetServerGameTimeMs(), Game()->GetSSEndTime() );
}
else
{
2012-11-26 18:58:24 +00:00
newCmd.serverGameMilliseconds = Game()->GetServerGameTimeMs();
}
2012-11-26 18:58:24 +00:00
userCmdMgr.MakeReadPtrCurrentForPlayer( Game()->GetLocalClientNum() );
2012-11-26 18:58:24 +00:00
// Stuff a copy of this userCmd for each game frame we are going to run.
// Ideally, the usercmds would be built in another thread so you could
// still get 60hz control accuracy when the game is running slower.
for( int i = 0 ; i < numGameFrames ; i++ )
{
newCmd.clientGameMilliseconds = FRAME_TO_MSEC( gameFrame - numGameFrames + i + 1 );
2012-11-26 18:58:24 +00:00
userCmdMgr.PutUserCmdForPlayer( game->GetLocalClientNum(), newCmd );
}
2012-11-26 18:58:24 +00:00
// If we're in Doom or Doom 2, run tics and upload the new texture.
if( ( GetCurrentGame() == DOOM_CLASSIC || GetCurrentGame() == DOOM2_CLASSIC ) && !( Dialog().IsDialogPausing() || session->IsSystemUIShowing() ) )
{
2012-11-26 18:58:24 +00:00
RunDoomClassicFrame();
}
// start the game / draw command generation thread going in the background
gameReturn_t ret = gameThread.RunGameAndDraw( numGameFrames, userCmdMgr, IsClient(), gameFrame - numGameFrames );
if( !com_smp.GetBool() )
{
2012-11-26 18:58:24 +00:00
// in non-smp mode, run the commands we just generated, instead of
// frame-delayed ones from a background thread
renderCommands = renderSystem->SwapCommandBuffers_FinishCommandBuffers();
}
2012-11-26 18:58:24 +00:00
//----------------------------------------
// Run the render back end, getting the GPU busy with new commands
// ASAP to minimize the pipeline bubble.
//----------------------------------------
frameTiming.startRenderTime = Sys_Microseconds();
renderSystem->RenderCommandBuffers( renderCommands );
if( com_sleepRender.GetInteger() > 0 )
{
2012-11-26 18:58:24 +00:00
// debug tool to test frame adaption
Sys_Sleep( com_sleepRender.GetInteger() );
}
frameTiming.finishRenderTime = Sys_Microseconds();
2012-11-26 18:58:24 +00:00
// make sure the game / draw thread has completed
// This may block if the game is taking longer than the render back end
gameThread.WaitForThread();
2012-11-26 18:58:24 +00:00
// Send local usermds to the server.
// This happens after the game frame has run so that prediction data is up to date.
SendUsercmds( Game()->GetLocalClientNum() );
2012-11-26 18:58:24 +00:00
// Now that we have an updated game frame, we can send out new snapshots to our clients
session->Pump(); // Pump to get updated usercmds to relay
SendSnapshots();
2012-11-26 18:58:24 +00:00
// Render the sound system using the latest commands from the game thread
if( pauseGame )
{
2012-11-26 18:58:24 +00:00
soundWorld->Pause();
soundSystem->SetPlayingSoundWorld( menuSoundWorld );
}
else
{
2012-11-26 18:58:24 +00:00
soundWorld->UnPause();
soundSystem->SetPlayingSoundWorld( soundWorld );
}
soundSystem->Render();
2012-11-26 18:58:24 +00:00
// process the game return for map changes, etc
ProcessGameReturn( ret );
idLobbyBase& lobby = session->GetActivePlatformLobbyBase();
if( lobby.HasActivePeers() )
{
if( net_drawDebugHud.GetInteger() == 1 )
{
2012-11-26 18:58:24 +00:00
lobby.DrawDebugNetworkHUD();
}
if( net_drawDebugHud.GetInteger() == 2 )
{
2012-11-26 18:58:24 +00:00
lobby.DrawDebugNetworkHUD2();
}
lobby.DrawDebugNetworkHUD_ServerSnapshotMetrics( net_drawDebugHud.GetInteger() == 3 );
}
2012-11-26 18:58:24 +00:00
// report timing information
if( com_speeds.GetBool() )
{
2012-11-26 18:58:24 +00:00
static int lastTime = Sys_Milliseconds();
int nowTime = Sys_Milliseconds();
int com_frameMsec = nowTime - lastTime;
lastTime = nowTime;
Printf( "frame:%d all:%3d gfr:%3d rf:%3lld bk:%3lld\n", idLib::frameNumber, com_frameMsec, time_gameFrame, time_frontend / 1000, time_backend / 1000 );
time_gameFrame = 0;
time_gameDraw = 0;
}
2012-11-26 18:58:24 +00:00
// the FPU stack better be empty at this point or some bad code or compiler bug left values on the stack
if( !Sys_FPU_StackIsEmpty() )
{
2012-11-26 18:58:24 +00:00
Printf( Sys_FPU_GetState() );
FatalError( "idCommon::Frame: the FPU stack is not empty at the end of the frame\n" );
}
2012-11-26 18:58:24 +00:00
mainFrameTiming = frameTiming;
2012-11-26 18:58:24 +00:00
session->GetSaveGameManager().Pump();
}
catch( idException& )
{
2012-11-26 18:58:24 +00:00
return; // an ERP_DROP was thrown
}
}
/*
=================
idCommonLocal::RunDoomClassicFrame
=================
*/
void idCommonLocal::RunDoomClassicFrame()
{
2012-11-26 18:58:24 +00:00
static int doomTics = 0;
if( DoomLib::expansionDirty )
{
2012-11-26 18:58:24 +00:00
// re-Initialize the Doom Engine.
DoomLib::Interface.Shutdown();
DoomLib::Interface.Startup( 1, false );
DoomLib::expansionDirty = false;
}
if( DoomLib::Interface.Frame( doomTics, &userCmdMgr ) )
{
Globals* data = ( Globals* )DoomLib::GetGlobalData( 0 );
2012-11-26 18:58:24 +00:00
idArray< unsigned int, 256 > palette;
std::copy( data->XColorMap, data->XColorMap + palette.Num(), palette.Ptr() );
2012-11-26 18:58:24 +00:00
// Do the palette lookup.
for( int row = 0; row < DOOMCLASSIC_RENDERHEIGHT; ++row )
{
for( int column = 0; column < DOOMCLASSIC_RENDERWIDTH; ++column )
{
2012-11-26 18:58:24 +00:00
const int doomScreenPixelIndex = row * DOOMCLASSIC_RENDERWIDTH + column;
const byte paletteIndex = data->screens[0][doomScreenPixelIndex];
const unsigned int paletteColor = palette[paletteIndex];
const byte red = ( paletteColor & 0xFF000000 ) >> 24;
const byte green = ( paletteColor & 0x00FF0000 ) >> 16;
const byte blue = ( paletteColor & 0x0000FF00 ) >> 8;
2012-11-26 18:58:24 +00:00
const int imageDataPixelIndex = row * DOOMCLASSIC_RENDERWIDTH * DOOMCLASSIC_BYTES_PER_PIXEL + column * DOOMCLASSIC_BYTES_PER_PIXEL;
doomClassicImageData[imageDataPixelIndex] = red;
doomClassicImageData[imageDataPixelIndex + 1] = green;
doomClassicImageData[imageDataPixelIndex + 2] = blue;
doomClassicImageData[imageDataPixelIndex + 3] = 255;
}
}
}
2012-11-26 18:58:24 +00:00
renderSystem->UploadImage( "_doomClassic", doomClassicImageData.Ptr(), DOOMCLASSIC_RENDERWIDTH, DOOMCLASSIC_RENDERHEIGHT );
doomTics++;
}