From 279c657cda7772a39f318b5df68fb279bb0400b4 Mon Sep 17 00:00:00 2001 From: degasus Date: Thu, 5 Mar 2015 17:12:24 +0100 Subject: Fifo: Replace busy loop with condition variable --- Source/Core/VideoCommon/Fifo.cpp | 78 +++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 32 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index 8c7dbce49b..4ca7a3de58 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -5,6 +5,7 @@ #include "Common/Atomic.h" #include "Common/ChunkFile.h" #include "Common/CPUDetect.h" +#include "Common/Event.h" #include "Common/FPURoundMode.h" #include "Common/MemoryUtil.h" #include "Common/Thread.h" @@ -58,6 +59,9 @@ static u8* s_video_buffer_pp_read_ptr; // polls, it's just atomic. // - The pp_read_ptr is the CPU preprocessing version of the read_ptr. +static Common::Flag s_gpu_is_running; // If this one is set, the gpu loop will be called at least once again +static Common::Event s_gpu_new_work_event; + void Fifo_DoState(PointerWrap &p) { p.DoArray(s_video_buffer, FIFO_SIZE); @@ -133,11 +137,13 @@ void ExitGpuLoop() // Terminate GPU thread loop GpuRunningState = false; EmuRunningState = true; + s_gpu_new_work_event.Set(); } void EmulatorState(bool running) { EmuRunningState = running; + s_gpu_new_work_event.Set(); } void SyncGPU(SyncGPUReason reason, bool may_move_read_ptr) @@ -271,10 +277,6 @@ void RunGpuLoop() SCPFifoStruct &fifo = CommandProcessor::fifo; u32 cyclesExecuted = 0; - // If the host CPU has only two cores, idle loop instead of busy loop - // This allows a system that we are maxing out in dual core mode to do other things - bool yield_cpu = cpu_info.num_cores <= 2; - AsyncRequests::GetInstance()->SetEnable(true); AsyncRequests::GetInstance()->SetPassthrough(false); @@ -353,11 +355,15 @@ void RunGpuLoop() if (EmuRunningState) { - // NOTE(jsd): Calling SwitchToThread() on Windows 7 x64 is a hot spot, according to profiler. - // See https://docs.google.com/spreadsheet/ccc?key=0Ah4nh0yGtjrgdFpDeF9pS3V6RUotRVE3S3J4TGM1NlE#gid=0 - // for benchmark details. - if (yield_cpu) - Common::YieldCPU(); + if (s_gpu_is_running.IsSet()) + { + // reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop + s_gpu_is_running.Clear(); + } + else + { + s_gpu_new_work_event.WaitFor(std::chrono::milliseconds(100)); + } } else { @@ -386,36 +392,44 @@ bool AtBreakpoint() void RunGpu() { - if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread && - !g_use_deterministic_gpu_thread) - return; - SCPFifoStruct &fifo = CommandProcessor::fifo; - while (fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint() ) + + // execute GPU + if (!SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread || g_use_deterministic_gpu_thread) { - if (g_use_deterministic_gpu_thread) - { - ReadDataFromFifoOnCPU(fifo.CPReadPointer); - } - else + while (fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint() ) { - FPURoundMode::SaveSIMDState(); - FPURoundMode::LoadDefaultSIMDState(); - ReadDataFromFifo(fifo.CPReadPointer); - s_video_buffer_read_ptr = OpcodeDecoder_Run(DataReader(s_video_buffer_read_ptr, s_video_buffer_write_ptr), nullptr, false); - FPURoundMode::LoadSIMDState(); - } + if (g_use_deterministic_gpu_thread) + { + ReadDataFromFifoOnCPU(fifo.CPReadPointer); + } + else + { + FPURoundMode::SaveSIMDState(); + FPURoundMode::LoadDefaultSIMDState(); + ReadDataFromFifo(fifo.CPReadPointer); + s_video_buffer_read_ptr = OpcodeDecoder_Run(DataReader(s_video_buffer_read_ptr, s_video_buffer_write_ptr), nullptr, false); + FPURoundMode::LoadSIMDState(); + } - //DEBUG_LOG(COMMANDPROCESSOR, "Fifo wraps to base"); + //DEBUG_LOG(COMMANDPROCESSOR, "Fifo wraps to base"); - if (fifo.CPReadPointer == fifo.CPEnd) - fifo.CPReadPointer = fifo.CPBase; - else - fifo.CPReadPointer += 32; + if (fifo.CPReadPointer == fifo.CPEnd) + fifo.CPReadPointer = fifo.CPBase; + else + fifo.CPReadPointer += 32; - fifo.CPReadWriteDistance -= 32; + fifo.CPReadWriteDistance -= 32; + } + CommandProcessor::SetCPStatusFromGPU(); + } + + // wake up GPU thread + if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread && !s_gpu_is_running.IsSet()) + { + s_gpu_is_running.Set(); + s_gpu_new_work_event.Set(); } - CommandProcessor::SetCPStatusFromGPU(); } void Fifo_UpdateWantDeterminism(bool want) -- cgit v1.2.3 From 9bdaa00e2dc18c06dcb9882d7f0caf372ed5990d Mon Sep 17 00:00:00 2001 From: degasus Date: Sun, 29 Mar 2015 12:20:24 +0200 Subject: Fifo: use the outer loop on sync GPU --- Source/Core/VideoCommon/Fifo.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index 4ca7a3de58..5f75deb672 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -306,10 +306,15 @@ void RunGpuLoop() { CommandProcessor::SetCPStatusFromGPU(); - Common::AtomicStore(CommandProcessor::VITicks, CommandProcessor::m_cpClockOrigin); + if (!fifo.isGpuReadingData) + { + Common::AtomicStore(CommandProcessor::VITicks, CommandProcessor::m_cpClockOrigin); + } + + bool run_loop = true; // check if we are able to run this buffer - while (GpuRunningState && EmuRunningState && !CommandProcessor::interruptWaiting && fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint()) + while (GpuRunningState && EmuRunningState && run_loop && !CommandProcessor::interruptWaiting && fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint()) { fifo.isGpuReadingData = true; CommandProcessor::isPossibleWaitingSetDrawDone = fifo.bFF_GPLinkEnable ? true : false; @@ -340,6 +345,10 @@ void RunGpuLoop() if ((write_ptr - s_video_buffer_read_ptr) == 0) Common::AtomicStore(fifo.SafeCPReadPointer, fifo.CPReadPointer); } + else + { + run_loop = false; + } CommandProcessor::SetCPStatusFromGPU(); @@ -350,7 +359,8 @@ void RunGpuLoop() CommandProcessor::isPossibleWaitingSetDrawDone = false; } - fifo.isGpuReadingData = false; + // don't release the GPU running state on sync GPU waits + fifo.isGpuReadingData = !run_loop; } if (EmuRunningState) -- cgit v1.2.3 From b020ae1c5db4e2e198d1beea91c75219a251a167 Mon Sep 17 00:00:00 2001 From: degasus Date: Thu, 5 Mar 2015 21:14:46 +0100 Subject: Fifo: rewrite sync on idle skipping hack Now it's done without a busy loop --- Source/Core/VideoCommon/Fifo.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index 5f75deb672..0b65a56cd8 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -62,6 +62,9 @@ static u8* s_video_buffer_pp_read_ptr; static Common::Flag s_gpu_is_running; // If this one is set, the gpu loop will be called at least once again static Common::Event s_gpu_new_work_event; +static Common::Flag s_gpu_is_pending; // If this one is set, there might still be work to do +static Common::Event s_gpu_done_event; + void Fifo_DoState(PointerWrap &p) { p.DoArray(s_video_buffer, FIFO_SIZE); @@ -85,7 +88,6 @@ void Fifo_PauseAndLock(bool doLock, bool unpauseOnUnlock) EmulatorState(false); if (!Core::IsGPUThread()) m_csHWVidOccupied.lock(); - _dbg_assert_(COMMON, !CommandProcessor::fifo.isGpuReadingData); } else { @@ -131,9 +133,8 @@ void ExitGpuLoop() { // This should break the wait loop in CPU thread CommandProcessor::fifo.bFF_GPReadEnable = false; - SCPFifoStruct &fifo = CommandProcessor::fifo; - while (fifo.isGpuReadingData) - Common::YieldCPU(); + FlushGpu(); + // Terminate GPU thread loop GpuRunningState = false; EmuRunningState = true; @@ -317,7 +318,6 @@ void RunGpuLoop() while (GpuRunningState && EmuRunningState && run_loop && !CommandProcessor::interruptWaiting && fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint()) { fifo.isGpuReadingData = true; - CommandProcessor::isPossibleWaitingSetDrawDone = fifo.bFF_GPLinkEnable ? true : false; if (!SConfig::GetInstance().m_LocalCoreStartupParameter.bSyncGPU || Common::AtomicLoad(CommandProcessor::VITicks) > CommandProcessor::m_cpClockOrigin) { @@ -356,18 +356,21 @@ void RunGpuLoop() // If we don't, s_swapRequested or s_efbAccessRequested won't be set to false // leading the CPU thread to wait in Video_BeginField or Video_AccessEFB thus slowing things down. AsyncRequests::GetInstance()->PullEvents(); - CommandProcessor::isPossibleWaitingSetDrawDone = false; } // don't release the GPU running state on sync GPU waits fifo.isGpuReadingData = !run_loop; } + s_gpu_is_pending.Clear(); + s_gpu_done_event.Set(); + if (EmuRunningState) { if (s_gpu_is_running.IsSet()) { // reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop + s_gpu_is_pending.Set(); s_gpu_is_running.Clear(); } else @@ -393,6 +396,16 @@ void RunGpuLoop() AsyncRequests::GetInstance()->SetPassthrough(true); } +void FlushGpu() +{ + if (!SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread || g_use_deterministic_gpu_thread) + return; + + while (s_gpu_is_running.IsSet() || s_gpu_is_pending.IsSet()) + { + s_gpu_done_event.Wait(); + } +} bool AtBreakpoint() { @@ -437,6 +450,7 @@ void RunGpu() // wake up GPU thread if (SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread && !s_gpu_is_running.IsSet()) { + s_gpu_is_pending.Set(); s_gpu_is_running.Set(); s_gpu_new_work_event.Set(); } -- cgit v1.2.3 From d2c62b17445b666b81a19cb3a6c46f9ee2d1388e Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 13 Mar 2015 23:36:31 +0100 Subject: Fifo: only sleep once within every ms of emulated time --- Source/Core/VideoCommon/Fifo.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index 0b65a56cd8..add7618d5d 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -369,9 +369,13 @@ void RunGpuLoop() { if (s_gpu_is_running.IsSet()) { - // reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop - s_gpu_is_pending.Set(); - s_gpu_is_running.Clear(); + if (CommandProcessor::s_gpuMaySleep.IsSet()) + { + // Reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop + s_gpu_is_pending.Set(); + s_gpu_is_running.Clear(); + CommandProcessor::s_gpuMaySleep.Clear(); + } } else { @@ -403,6 +407,7 @@ void FlushGpu() while (s_gpu_is_running.IsSet() || s_gpu_is_pending.IsSet()) { + CommandProcessor::s_gpuMaySleep.Set(); s_gpu_done_event.Wait(); } } -- cgit v1.2.3 From b1ffd32f5f09a86598bb47c4b392b28354ed0a87 Mon Sep 17 00:00:00 2001 From: degasus Date: Sat, 14 Mar 2015 08:02:16 +0100 Subject: Fifo: only touch the SIMD state once in the single core loop --- Source/Core/VideoCommon/Fifo.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index add7618d5d..c9ac3fdbbb 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -425,6 +425,7 @@ void RunGpu() // execute GPU if (!SConfig::GetInstance().m_LocalCoreStartupParameter.bCPUThread || g_use_deterministic_gpu_thread) { + bool reset_simd_state = false; while (fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint() ) { if (g_use_deterministic_gpu_thread) @@ -433,11 +434,14 @@ void RunGpu() } else { - FPURoundMode::SaveSIMDState(); - FPURoundMode::LoadDefaultSIMDState(); + if (!reset_simd_state) + { + FPURoundMode::SaveSIMDState(); + FPURoundMode::LoadDefaultSIMDState(); + reset_simd_state = true; + } ReadDataFromFifo(fifo.CPReadPointer); s_video_buffer_read_ptr = OpcodeDecoder_Run(DataReader(s_video_buffer_read_ptr, s_video_buffer_write_ptr), nullptr, false); - FPURoundMode::LoadSIMDState(); } //DEBUG_LOG(COMMANDPROCESSOR, "Fifo wraps to base"); @@ -450,6 +454,11 @@ void RunGpu() fifo.CPReadWriteDistance -= 32; } CommandProcessor::SetCPStatusFromGPU(); + + if (reset_simd_state) + { + FPURoundMode::LoadSIMDState(); + } } // wake up GPU thread -- cgit v1.2.3 From 74795b45539ebee1c2af78fd6dca9022c3189c76 Mon Sep 17 00:00:00 2001 From: degasus Date: Sun, 29 Mar 2015 15:05:11 +0200 Subject: Fifo: rewrite Fifo_PauseAndLock This lock isn't required any more as our FlushGpu garanty to block until the GPU is idle --- Source/Core/VideoCommon/Fifo.cpp | 48 ++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'Source/Core/VideoCommon/Fifo.cpp') diff --git a/Source/Core/VideoCommon/Fifo.cpp b/Source/Core/VideoCommon/Fifo.cpp index c9ac3fdbbb..c5e49fb583 100644 --- a/Source/Core/VideoCommon/Fifo.cpp +++ b/Source/Core/VideoCommon/Fifo.cpp @@ -30,7 +30,6 @@ bool g_bSkipCurrentFrame = false; static volatile bool GpuRunningState = false; static volatile bool EmuRunningState = false; -static std::mutex m_csHWVidOccupied; // Most of this array is unlikely to be faulted in... static u8 s_fifo_aux_data[FIFO_SIZE]; @@ -86,15 +85,12 @@ void Fifo_PauseAndLock(bool doLock, bool unpauseOnUnlock) { SyncGPU(SYNC_GPU_OTHER); EmulatorState(false); - if (!Core::IsGPUThread()) - m_csHWVidOccupied.lock(); + FlushGpu(); } else { if (unpauseOnUnlock) EmulatorState(true); - if (!Core::IsGPUThread()) - m_csHWVidOccupied.unlock(); } } @@ -273,7 +269,6 @@ void ResetVideoBuffer() // Purpose: Keep the Core HW updated about the CPU-GPU distance void RunGpuLoop() { - std::lock_guard lk(m_csHWVidOccupied); GpuRunningState = true; SCPFifoStruct &fifo = CommandProcessor::fifo; u32 cyclesExecuted = 0; @@ -285,9 +280,10 @@ void RunGpuLoop() { g_video_backend->PeekMessages(); - AsyncRequests::GetInstance()->PullEvents(); - if (g_use_deterministic_gpu_thread) + if (g_use_deterministic_gpu_thread && EmuRunningState) { + AsyncRequests::GetInstance()->PullEvents(); + // All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder. u8* seen_ptr = s_video_buffer_seen_ptr; u8* write_ptr = s_video_buffer_write_ptr; @@ -303,8 +299,10 @@ void RunGpuLoop() } } } - else + else if (EmuRunningState) { + AsyncRequests::GetInstance()->PullEvents(); + CommandProcessor::SetCPStatusFromGPU(); if (!fifo.isGpuReadingData) @@ -315,7 +313,7 @@ void RunGpuLoop() bool run_loop = true; // check if we are able to run this buffer - while (GpuRunningState && EmuRunningState && run_loop && !CommandProcessor::interruptWaiting && fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint()) + while (run_loop && !CommandProcessor::interruptWaiting && fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint()) { fifo.isGpuReadingData = true; @@ -365,33 +363,19 @@ void RunGpuLoop() s_gpu_is_pending.Clear(); s_gpu_done_event.Set(); - if (EmuRunningState) + if (s_gpu_is_running.IsSet()) { - if (s_gpu_is_running.IsSet()) - { - if (CommandProcessor::s_gpuMaySleep.IsSet()) - { - // Reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop - s_gpu_is_pending.Set(); - s_gpu_is_running.Clear(); - CommandProcessor::s_gpuMaySleep.Clear(); - } - } - else + if (CommandProcessor::s_gpuMaySleep.IsSet()) { - s_gpu_new_work_event.WaitFor(std::chrono::milliseconds(100)); + // Reset the atomic flag. But as the CPU thread might have pushed some new data, we have to rerun the GPU loop + s_gpu_is_pending.Set(); + s_gpu_is_running.Clear(); + CommandProcessor::s_gpuMaySleep.Clear(); } } else { - // While the emu is paused, we still handle async requests then sleep. - while (!EmuRunningState) - { - g_video_backend->PeekMessages(); - m_csHWVidOccupied.unlock(); - Common::SleepCurrentThread(1); - m_csHWVidOccupied.lock(); - } + s_gpu_new_work_event.WaitFor(std::chrono::milliseconds(100)); } } // wake up SyncGPU if we were interrupted @@ -472,7 +456,7 @@ void RunGpu() void Fifo_UpdateWantDeterminism(bool want) { - // We are paused (or not running at all yet) and have m_csHWVidOccupied, so + // We are paused (or not running at all yet), so // it should be safe to change this. const SCoreStartupParameter& param = SConfig::GetInstance().m_LocalCoreStartupParameter; bool gpu_thread = false; -- cgit v1.2.3