recording: Reposition inputRecordingControl function execution in vsync

* No longer calls HandleFrameAdvanceAndPausing() more than once per frame. No need to increment frameCountTracker.
* Splits framelocking into its own function (which could harmlessly get called more than once per frame).
This commit is contained in:
sonicfind 2020-12-15 23:19:58 -06:00 committed by refractionpcsx2
parent b7d01b4889
commit b5b74388f4
4 changed files with 79 additions and 67 deletions

View File

@ -477,6 +477,15 @@ static __fi void frameLimit()
static __fi void VSyncStart(u32 sCycle)
{
#ifndef DISABLE_RECORDING
if (g_Conf->EmuOptions.EnableRecordingTools)
{
// It is imperative that any frame locking that must happen occurs before Vsync is started
// Not doing so would sacrifice a frame of a savestate-based recording when loading any savestate
g_InputRecordingControls.HandleFrameCountLocking();
}
#endif
frameLimit(); // limit FPS
gsPostVsyncStart(); // MUST be after framelimit; doing so before causes funk with frame times!
@ -531,13 +540,18 @@ static __fi void GSVSync()
static __fi void VSyncEnd(u32 sCycle)
{
#ifndef DISABLE_RECORDING
if (g_Conf->EmuOptions.EnableRecordingTools)
{
g_InputRecordingControls.HandleFrameAdvanceAndPausing();
}
#endif
if(EmuConfig.Trace.Enabled && EmuConfig.Trace.EE.m_EnableAll)
SysTrace.EE.Counters.Write( " ================ EE COUNTER VSYNC END (frame: %d) ================", g_FrameCount );
g_FrameCount++;
hwIntcIrq(INTC_VBLANK_E); // HW Irq
psxVBlankEnd(); // psxCounters vBlank End
if (gates) rcntEndGate(true, sCycle); // Counters End Gate Code
@ -616,12 +630,6 @@ __fi void rcntUpdate_vSync()
}
else // VSYNC end / VRENDER begin
{
#ifndef DISABLE_RECORDING
if (g_Conf->EmuOptions.EnableRecordingTools)
{
g_InputRecordingControls.HandleFrameAdvanceAndPausing();
}
#endif
VSyncStart(vsyncCounter.sCycle);
vsyncCounter.sCycle += vSyncInfo.Render;

View File

@ -31,65 +31,34 @@ InputRecordingControls g_InputRecordingControls;
void InputRecordingControls::HandleFrameAdvanceAndPausing()
{
// This function can be called multiple times per frame via Counters::rcntUpdate_vSync,
// often this is twice per frame but this may vary as Counters.cpp mentions the
// vsync timing can change.
//
// When loading a recording file or rebooting with a recording active, lock will be enabled.
// Emulation will be forced into and remain in a paused state until the transition in progress
// has completed - signaled when g_framecount and frameCountTracker are equal.
// We do not want to increment/advance anything while the frame lock mechanism is active
if (frameLock)
{
if (g_FrameCount == frameCountTracker)
{
frameLock = false;
Resume();
}
else if (!emulationCurrentlyPaused && CoreThread.IsOpen() && CoreThread.IsRunning())
{
pauseEmulation = true;
resumeEmulation = false;
emulationCurrentlyPaused = true;
CoreThread.PauseSelf();
}
return;
}
// As a safeguard, use the global g_FrameCount to know when the frame counter has truly changed.
//
// NOTE - Do not mutate g_FrameCount or use it's value to set the input recording's internal frame counter
else if (frameCountTracker != g_FrameCount)
{
frameCountTracker = g_FrameCount;
g_InputRecording.IncrementFrameCounter();
}
else
{
if (pauseEmulation)
{
emulationCurrentlyPaused = true;
CoreThread.PauseSelf();
}
return;
}
if (switchToReplay)
{
g_InputRecording.SetToReplayMode();
switchToReplay = false;
}
if ((g_InputRecording.IsReplaying() &&
g_InputRecording.GetFrameCounter() >= g_InputRecording.GetInputRecordingData().GetTotalFrames()) ||
g_InputRecording.GetFrameCounter() == INT_MAX)
pauseEmulation = true;
// If we haven't yet advanced atleast a single frame from when we paused, setup things to be paused
if (frameAdvancing && frameAdvanceMarker < g_InputRecording.GetFrameCounter())
if (frameAdvancing)
{
frameAdvancing = false;
pauseEmulation = true;
}
if (g_InputRecording.IsActive())
{
g_InputRecording.IncrementFrameCounter();
if (switchToReplay)
{
g_InputRecording.SetToReplayMode();
switchToReplay = false;
}
if (!pauseEmulation &&
g_InputRecording.IsReplaying() &&
g_InputRecording.GetFrameCounter() >= g_InputRecording.GetInputRecordingData().GetTotalFrames())
{
pauseEmulation = true;
}
}
// Pause emulation if we need to (either due to frame advancing, or pause has been explicitly toggled on)
if (pauseEmulation && CoreThread.IsOpen() && CoreThread.IsRunning())
{
@ -98,9 +67,36 @@ void InputRecordingControls::HandleFrameAdvanceAndPausing()
}
}
void InputRecordingControls::HandleFrameCountLocking()
{
// Explicit frame locking
if (frameLock)
{
if (g_FrameCount == frameCountTracker)
{
frameLock = false;
Resume();
}
else if (!emulationCurrentlyPaused && CoreThread.IsOpen() && CoreThread.IsRunning())
{
emulationCurrentlyPaused = true;
CoreThread.PauseSelf();
}
}
// Soft frame locking due to a savestate
//
// This must be done here and not somewhere else as emulation would advance a single frame
// after loading a savestate even with emulation already paused
else if (g_FrameCount == frameCountTracker && pauseEmulation)
{
emulationCurrentlyPaused = true;
CoreThread.PauseSelf();
}
}
void InputRecordingControls::ResumeCoreThreadIfStarted()
{
if (resumeEmulation && CoreThread.IsOpen() && CoreThread.IsPaused())
if (resumeEmulation && CoreThread.IsOpen())
{
CoreThread.Resume();
resumeEmulation = false;
@ -116,7 +112,6 @@ void InputRecordingControls::FrameAdvance()
g_InputRecording.SetToRecordMode();
return;
}
frameAdvanceMarker = g_InputRecording.GetFrameCounter();
frameAdvancing = true;
Resume();
}
@ -180,8 +175,8 @@ void InputRecordingControls::TogglePause()
g_InputRecording.SetToRecordMode();
return;
}
resumeEmulation = pauseEmulation;
pauseEmulation = !pauseEmulation;
resumeEmulation = !pauseEmulation;
inputRec::log(pauseEmulation ? "Paused Emulation" : "Resumed Emulation");
}
@ -204,7 +199,6 @@ void InputRecordingControls::Lock(u32 frame)
{
frameLock = true;
frameCountTracker = frame;
resumeEmulation = false;
//Ensures that g_frameCount can be used to resume emulation after a fast/full boot
if (!g_InputRecording.GetInputRecordingData().FromSaveState())
g_FrameCount = frame + 1;

View File

@ -20,14 +20,21 @@
class InputRecordingControls
{
public:
// Intended to be called at the end of each frame, but will no-op if the frame count has not
// truly incremented
// Intended to be called at the end of each frame, but will no-op if frame lock is active
//
// Increments the input recording's frame counter and will pause emulation if:
// Will pause emulation if:
// - The InputRecordingControls::FrameAdvance was hit on the previous frame
// - Emulation was explicitly paused using InputRecordingControls::TogglePause
// - We are replaying an input recording and have hit the end
void HandleFrameAdvanceAndPausing();
// When loading a recording file or booting with a recording active, lock will be enabled.
// Emulation will be forced into and remain in a paused state until the transition in progress
// has completed - signaled when g_framecount and frameCountTracker are equal
//
// Additonally, this function will ensure emulation stays paused after loading a savestate
void HandleFrameCountLocking();
// Called much more frequently than HandleFrameAdvanceAndPausing, instead of being per frame
// this hooks into pcsx2's main App event handler as it has to be able to resume emulation
// when drawing frames has compltely stopped

View File

@ -1183,7 +1183,10 @@ void Pcsx2App::SysExecute( CDVD_SourceType cdvdsrc, const wxString& elf_override
{
SysExecutorThread.PostEvent( new SysExecEvent_Execute(cdvdsrc, elf_override) );
#ifndef DISABLE_RECORDING
g_InputRecording.RecordingReset();
if (g_Conf->EmuOptions.EnableRecordingTools)
{
g_InputRecording.RecordingReset();
}
#endif
}