Files
dolphin/Source/Core/VideoCommon/Fifo.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

604 lines
19 KiB
C++
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2008-12-08 05:30:24 +00:00
#include "VideoCommon/Fifo.h"
#include <atomic>
2016-01-17 16:54:31 -05:00
#include <cstring>
2016-01-17 16:54:31 -05:00
#include "Common/Assert.h"
2015-05-27 20:53:09 +02:00
#include "Common/BlockingLoop.h"
2014-02-17 05:18:15 -05:00
#include "Common/ChunkFile.h"
#include "Common/Event.h"
#include "Common/FPURoundMode.h"
2014-02-17 05:18:15 -05:00
#include "Common/MemoryUtil.h"
2016-01-17 16:54:31 -05:00
#include "Common/MsgHandler.h"
2014-02-17 05:18:15 -05:00
#include "Core/Config/MainSettings.h"
2014-09-09 00:24:49 -04:00
#include "Core/ConfigManager.h"
2016-01-19 00:08:18 +01:00
#include "Core/CoreTiming.h"
#include "Core/HW/GPFifo.h"
2014-02-17 05:18:15 -05:00
#include "Core/HW/Memmap.h"
#include "Core/Host.h"
#include "Core/System.h"
2014-02-17 05:18:15 -05:00
#include "VideoCommon/AsyncRequests.h"
#include "VideoCommon/CPMemory.h"
2014-02-17 05:18:15 -05:00
#include "VideoCommon/CommandProcessor.h"
2014-07-08 16:49:33 +02:00
#include "VideoCommon/DataReader.h"
#include "VideoCommon/FramebufferManager.h"
2014-02-17 05:18:15 -05:00
#include "VideoCommon/OpcodeDecoding.h"
#include "VideoCommon/VertexLoaderManager.h"
2015-05-27 20:53:09 +02:00
#include "VideoCommon/VertexManagerBase.h"
#include "VideoCommon/VideoBackendBase.h"
2008-12-08 05:30:24 +00:00
2016-01-12 22:44:58 +01:00
namespace Fifo
{
2016-01-10 12:18:45 +01:00
static constexpr int GPU_TIME_SLOT_SIZE = 1000;
2015-12-25 16:07:00 -05:00
FifoManager::FifoManager(Core::System& system) : m_system{system}
{
}
FifoManager::~FifoManager() = default;
2015-05-27 20:53:09 +02:00
void FifoManager::RefreshConfig()
{
m_config_sync_gpu = Config::Get(Config::MAIN_SYNC_GPU);
m_config_sync_gpu_max_distance = Config::Get(Config::MAIN_SYNC_GPU_MAX_DISTANCE);
m_config_sync_gpu_min_distance = Config::Get(Config::MAIN_SYNC_GPU_MIN_DISTANCE);
m_config_sync_gpu_overclock = Config::Get(Config::MAIN_SYNC_GPU_OVERCLOCK);
}
void FifoManager::DoState(PointerWrap& p)
{
p.DoArray(m_video_buffer, FIFO_SIZE);
u8* write_ptr = m_video_buffer_write_ptr;
p.DoPointer(write_ptr, m_video_buffer);
m_video_buffer_write_ptr = write_ptr;
p.DoPointer(m_video_buffer_read_ptr, m_video_buffer);
if (p.IsReadMode() && m_use_deterministic_gpu_thread)
2014-08-27 22:56:19 -04:00
{
// We're good and paused, right?
m_video_buffer_seen_ptr = m_video_buffer_pp_read_ptr = m_video_buffer_read_ptr;
2014-08-27 22:56:19 -04:00
}
p.Do(m_sync_ticks);
p.Do(m_syncing_suspended);
}
2008-12-08 05:30:24 +00:00
void FifoManager::PauseAndLock(bool do_lock, bool unpause_on_unlock)
{
if (do_lock)
{
2016-08-18 22:35:58 -04:00
SyncGPU(SyncGPUReason::Other);
EmulatorState(false);
if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread)
return;
m_gpu_mainloop.WaitYield(std::chrono::milliseconds(100), Host_YieldToUI);
}
else
{
if (unpause_on_unlock)
EmulatorState(true);
}
}
void FifoManager::Init()
{
if (!m_config_callback_id)
m_config_callback_id = Config::AddConfigChangedCallback([this] { RefreshConfig(); });
RefreshConfig();
// Padded so that SIMD overreads in the vertex loader are safe
m_video_buffer = static_cast<u8*>(Common::AllocateMemoryPages(FIFO_SIZE + 4));
2014-08-27 22:56:19 -04:00
ResetVideoBuffer();
if (m_system.IsDualCoreMode())
m_gpu_mainloop.Prepare();
m_sync_ticks.store(0);
2008-07-12 17:40:22 +00:00
}
2008-12-08 05:30:24 +00:00
void FifoManager::Shutdown()
2008-07-12 17:40:22 +00:00
{
if (m_gpu_mainloop.IsRunning())
2020-11-13 22:33:26 -05:00
PanicAlertFmt("FIFO shutting down while active");
Common::FreeMemoryPages(m_video_buffer, FIFO_SIZE + 4);
m_video_buffer = nullptr;
m_video_buffer_write_ptr = nullptr;
m_video_buffer_pp_read_ptr = nullptr;
m_video_buffer_read_ptr = nullptr;
m_video_buffer_seen_ptr = nullptr;
m_fifo_aux_write_ptr = nullptr;
m_fifo_aux_read_ptr = nullptr;
if (m_config_callback_id)
{
Config::RemoveConfigChangedCallback(*m_config_callback_id);
m_config_callback_id = std::nullopt;
}
2008-07-12 17:40:22 +00:00
}
2008-12-08 05:30:24 +00:00
2009-03-07 23:34:16 +00:00
// May be executed from any thread, even the graphics thread.
// Created to allow for self shutdown.
void FifoManager::ExitGpuLoop()
{
auto& command_processor = m_system.GetCommandProcessor();
auto& fifo = command_processor.GetFifo();
2010-01-16 22:37:38 +00:00
// This should break the wait loop in CPU thread
fifo.bFF_GPReadEnable.store(0, std::memory_order_relaxed);
FlushGpu();
2015-03-05 21:14:46 +01:00
2010-01-16 22:37:38 +00:00
// Terminate GPU thread loop
m_emu_running_state.Set();
m_gpu_mainloop.Stop(Common::BlockingLoop::StopMode::NonBlock);
2010-01-03 16:04:40 +00:00
}
void FifoManager::EmulatorState(bool running)
2010-01-03 16:04:40 +00:00
{
m_emu_running_state.Set(running);
if (running)
m_gpu_mainloop.Wakeup();
else
m_gpu_mainloop.AllowSleep();
2009-03-07 23:34:16 +00:00
}
void FifoManager::SyncGPU(SyncGPUReason reason, bool may_move_read_ptr)
2014-08-27 22:56:19 -04:00
{
if (m_use_deterministic_gpu_thread)
2014-08-27 22:56:19 -04:00
{
m_gpu_mainloop.Wait();
if (!m_gpu_mainloop.IsRunning())
2014-08-27 22:56:19 -04:00
return;
// Opportunistically reset FIFOs so we don't wrap around.
if (may_move_read_ptr && m_fifo_aux_write_ptr != m_fifo_aux_read_ptr)
2020-11-13 22:33:26 -05:00
{
PanicAlertFmt("Aux FIFO not synced ({}, {})", fmt::ptr(m_fifo_aux_write_ptr),
fmt::ptr(m_fifo_aux_read_ptr));
2020-11-13 22:33:26 -05:00
}
2014-08-27 22:56:19 -04:00
memmove(m_fifo_aux_data, m_fifo_aux_read_ptr, m_fifo_aux_write_ptr - m_fifo_aux_read_ptr);
m_fifo_aux_write_ptr -= (m_fifo_aux_read_ptr - m_fifo_aux_data);
m_fifo_aux_read_ptr = m_fifo_aux_data;
2014-08-27 22:56:19 -04:00
if (may_move_read_ptr)
{
u8* write_ptr = m_video_buffer_write_ptr;
2015-05-27 20:53:09 +02:00
2014-08-27 22:56:19 -04:00
// what's left over in the buffer
size_t size = write_ptr - m_video_buffer_pp_read_ptr;
2014-08-27 22:56:19 -04:00
memmove(m_video_buffer, m_video_buffer_pp_read_ptr, size);
2014-08-27 22:56:19 -04:00
// This change always decreases the pointers. We write seen_ptr
// after write_ptr here, and read it before in RunGpuLoop, so
// 'write_ptr > seen_ptr' there cannot become spuriously true.
m_video_buffer_write_ptr = write_ptr = m_video_buffer + size;
m_video_buffer_pp_read_ptr = m_video_buffer;
m_video_buffer_read_ptr = m_video_buffer;
m_video_buffer_seen_ptr = write_ptr;
2014-08-27 22:56:19 -04:00
}
}
}
void FifoManager::PushFifoAuxBuffer(const void* ptr, size_t size)
2014-08-27 22:56:19 -04:00
{
if (size > (size_t)(m_fifo_aux_data + FIFO_SIZE - m_fifo_aux_write_ptr))
2014-08-27 22:56:19 -04:00
{
2016-08-18 22:35:58 -04:00
SyncGPU(SyncGPUReason::AuxSpace, /* may_move_read_ptr */ false);
if (!m_gpu_mainloop.IsRunning())
{
// GPU is shutting down
return;
}
if (size > (size_t)(m_fifo_aux_data + FIFO_SIZE - m_fifo_aux_write_ptr))
2014-08-27 22:56:19 -04:00
{
// That will sync us up to the last 32 bytes, so this short region
// of FIFO would have to point to a 2MB display list or something.
2020-11-13 22:33:26 -05:00
PanicAlertFmt("Absurdly large aux buffer");
2014-08-27 22:56:19 -04:00
return;
}
}
memcpy(m_fifo_aux_write_ptr, ptr, size);
m_fifo_aux_write_ptr += size;
2014-08-27 22:56:19 -04:00
}
void* FifoManager::PopFifoAuxBuffer(size_t size)
2014-08-27 22:56:19 -04:00
{
void* ret = m_fifo_aux_read_ptr;
m_fifo_aux_read_ptr += size;
2014-08-27 22:56:19 -04:00
return ret;
}
// Description: RunGpuLoop() sends data through this function.
void FifoManager::ReadDataFromFifo(u32 read_ptr)
{
if (GPFifo::GATHER_PIPE_SIZE >
static_cast<size_t>(m_video_buffer + FIFO_SIZE - m_video_buffer_write_ptr))
{
const size_t existing_len = m_video_buffer_write_ptr - m_video_buffer_read_ptr;
if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(FIFO_SIZE - existing_len))
{
PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len,
GPFifo::GATHER_PIPE_SIZE, FIFO_SIZE);
return;
}
memmove(m_video_buffer, m_video_buffer_read_ptr, existing_len);
m_video_buffer_write_ptr = m_video_buffer + existing_len;
m_video_buffer_read_ptr = m_video_buffer;
}
// Copy new video instructions to m_video_buffer for future use in rendering the new picture
auto& memory = m_system.GetMemory();
memory.CopyFromEmu(m_video_buffer_write_ptr, read_ptr, GPFifo::GATHER_PIPE_SIZE);
m_video_buffer_write_ptr += GPFifo::GATHER_PIPE_SIZE;
}
2014-08-27 22:56:19 -04:00
// The deterministic_gpu_thread version.
void FifoManager::ReadDataFromFifoOnCPU(u32 read_ptr)
2014-08-27 22:56:19 -04:00
{
u8* write_ptr = m_video_buffer_write_ptr;
if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(m_video_buffer + FIFO_SIZE - write_ptr))
2014-08-27 22:56:19 -04:00
{
// We can't wrap around while the GPU is working on the data.
// This should be very rare due to the reset in SyncGPU.
2016-08-18 22:35:58 -04:00
SyncGPU(SyncGPUReason::Wraparound);
if (!m_gpu_mainloop.IsRunning())
{
2015-05-27 20:53:09 +02:00
// GPU is shutting down, so the next asserts may fail
return;
}
if (m_video_buffer_pp_read_ptr != m_video_buffer_read_ptr)
2014-08-27 22:56:19 -04:00
{
2020-11-13 22:33:26 -05:00
PanicAlertFmt("Desynced read pointers");
2014-08-27 22:56:19 -04:00
return;
}
write_ptr = m_video_buffer_write_ptr;
const size_t existing_len = write_ptr - m_video_buffer_pp_read_ptr;
if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(FIFO_SIZE - existing_len))
2014-08-27 22:56:19 -04:00
{
PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len,
GPFifo::GATHER_PIPE_SIZE, FIFO_SIZE);
2014-08-27 22:56:19 -04:00
return;
}
}
auto& memory = m_system.GetMemory();
memory.CopyFromEmu(m_video_buffer_write_ptr, read_ptr, GPFifo::GATHER_PIPE_SIZE);
m_video_buffer_pp_read_ptr = OpcodeDecoder::RunFifo<true>(
DataReader(m_video_buffer_pp_read_ptr, write_ptr + GPFifo::GATHER_PIPE_SIZE), nullptr);
2014-08-27 22:56:19 -04:00
// This would have to be locked if the GPU thread didn't spin.
m_video_buffer_write_ptr = write_ptr + GPFifo::GATHER_PIPE_SIZE;
2014-08-27 22:56:19 -04:00
}
void FifoManager::ResetVideoBuffer()
{
m_video_buffer_read_ptr = m_video_buffer;
m_video_buffer_write_ptr = m_video_buffer;
m_video_buffer_seen_ptr = m_video_buffer;
m_video_buffer_pp_read_ptr = m_video_buffer;
m_fifo_aux_write_ptr = m_fifo_aux_data;
m_fifo_aux_read_ptr = m_fifo_aux_data;
}
2010-01-23 21:06:12 +00:00
// Description: Main FIFO update loop
// Purpose: Keep the Core HW updated about the CPU-GPU distance
void FifoManager::RunGpuLoop()
2008-07-12 17:40:22 +00:00
{
AsyncRequests::GetInstance()->SetEnable(true);
AsyncRequests::GetInstance()->SetPassthrough(false);
2008-12-08 05:30:24 +00:00
m_gpu_mainloop.Run(
[this] {
// Run events from the CPU thread.
AsyncRequests::GetInstance()->PullEvents();
2015-05-27 20:53:09 +02:00
// Do nothing while paused
if (!m_emu_running_state.IsSet())
2013-03-13 18:23:16 -05:00
return;
if (m_use_deterministic_gpu_thread)
{
// All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder.
u8* seen_ptr = m_video_buffer_seen_ptr;
u8* write_ptr = m_video_buffer_write_ptr;
// See comment in SyncGPU
if (write_ptr > seen_ptr)
{
m_video_buffer_read_ptr =
OpcodeDecoder::RunFifo(DataReader(m_video_buffer_read_ptr, write_ptr), nullptr);
m_video_buffer_seen_ptr = write_ptr;
}
}
else
2010-01-23 21:06:12 +00:00
{
auto& command_processor = m_system.GetCommandProcessor();
auto& fifo = command_processor.GetFifo();
command_processor.SetCPStatusFromGPU();
2015-03-29 15:05:11 +02:00
// check if we are able to run this buffer
while (!command_processor.IsInterruptWaiting() &&
2021-05-13 19:30:30 +02:00
fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) &&
fifo.CPReadWriteDistance.load(std::memory_order_relaxed) &&
!AtBreakpoint(m_system))
{
if (m_config_sync_gpu && m_sync_ticks.load() < m_config_sync_gpu_min_distance)
break;
2015-06-03 23:21:46 +02:00
u32 cyclesExecuted = 0;
2021-05-13 18:44:59 +02:00
u32 readPtr = fifo.CPReadPointer.load(std::memory_order_relaxed);
ReadDataFromFifo(readPtr);
2014-08-27 22:56:19 -04:00
2021-05-13 18:44:59 +02:00
if (readPtr == fifo.CPEnd.load(std::memory_order_relaxed))
readPtr = fifo.CPBase.load(std::memory_order_relaxed);
else
readPtr += GPFifo::GATHER_PIPE_SIZE;
2014-08-27 22:56:19 -04:00
const s32 distance =
static_cast<s32>(fifo.CPReadWriteDistance.load(std::memory_order_relaxed)) -
GPFifo::GATHER_PIPE_SIZE;
ASSERT_MSG(COMMANDPROCESSOR, distance >= 0,
2021-11-10 14:42:49 -08:00
"Negative fifo.CPReadWriteDistance = {} in FIFO Loop !\nThat can produce "
2018-03-14 20:34:35 -04:00
"instability in the game. Please report it.",
distance);
2015-06-03 23:21:46 +02:00
u8* write_ptr = m_video_buffer_write_ptr;
m_video_buffer_read_ptr = OpcodeDecoder::RunFifo(
DataReader(m_video_buffer_read_ptr, write_ptr), &cyclesExecuted);
2015-06-03 23:21:46 +02:00
2021-05-13 18:44:59 +02:00
fifo.CPReadPointer.store(readPtr, std::memory_order_relaxed);
fifo.CPReadWriteDistance.fetch_sub(GPFifo::GATHER_PIPE_SIZE, std::memory_order_seq_cst);
if ((write_ptr - m_video_buffer_read_ptr) == 0)
2021-05-13 18:44:59 +02:00
{
fifo.SafeCPReadPointer.store(fifo.CPReadPointer.load(std::memory_order_relaxed),
std::memory_order_relaxed);
}
2015-06-03 23:21:46 +02:00
command_processor.SetCPStatusFromGPU();
2014-08-27 22:56:19 -04:00
if (m_config_sync_gpu)
{
cyclesExecuted = (int)(cyclesExecuted / m_config_sync_gpu_overclock);
int old = m_sync_ticks.fetch_sub(cyclesExecuted);
if (old >= m_config_sync_gpu_max_distance &&
old - (int)cyclesExecuted < m_config_sync_gpu_max_distance)
{
m_sync_wakeup_event.Set();
}
}
2014-08-27 22:56:19 -04:00
2015-06-03 23:21:46 +02:00
// This call is pretty important in DualCore mode and must be called in the FIFO Loop.
// If we don't, s_swapRequested or s_efbAccessRequested won't be set to false
2021-08-03 18:55:38 -04:00
// leading the CPU thread to wait in Video_OutputXFB or Video_AccessEFB thus slowing
2014-08-27 22:56:19 -04:00
// things down.
2015-06-03 23:21:46 +02:00
AsyncRequests::GetInstance()->PullEvents();
}
2014-08-27 22:56:19 -04:00
// fast skip remaining GPU time if fifo is empty
if (m_sync_ticks.load() > 0)
{
int old = m_sync_ticks.exchange(0);
if (old >= m_config_sync_gpu_max_distance)
m_sync_wakeup_event.Set();
2014-08-27 22:56:19 -04:00
}
2015-06-03 23:21:46 +02:00
// The fifo is empty and it's unlikely we will get any more work in the near future.
// Make sure VertexManager finishes drawing any primitives it has stored in it's buffer.
g_vertex_manager->Flush();
g_framebuffer_manager->RefreshPeekCache();
2015-06-03 23:21:46 +02:00
}
},
100);
2015-06-03 23:21:46 +02:00
AsyncRequests::GetInstance()->SetEnable(false);
AsyncRequests::GetInstance()->SetPassthrough(true);
2008-07-12 17:40:22 +00:00
}
void FifoManager::FlushGpu()
2015-03-05 21:14:46 +01:00
{
if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread)
2015-03-05 21:14:46 +01:00
return;
m_gpu_mainloop.Wait();
2015-05-27 20:53:09 +02:00
}
void FifoManager::GpuMaySleep()
2015-05-27 20:53:09 +02:00
{
m_gpu_mainloop.AllowSleep();
2015-03-05 21:14:46 +01:00
}
bool AtBreakpoint(Core::System& system)
{
auto& command_processor = system.GetCommandProcessor();
const auto& fifo = command_processor.GetFifo();
2021-05-13 19:30:30 +02:00
return fifo.bFF_BPEnable.load(std::memory_order_relaxed) &&
(fifo.CPReadPointer.load(std::memory_order_relaxed) ==
fifo.CPBreakpoint.load(std::memory_order_relaxed));
}
void FifoManager::RunGpu()
{
const bool is_dual_core = m_system.IsDualCoreMode();
// wake up GPU thread
if (is_dual_core && !m_use_deterministic_gpu_thread)
{
m_gpu_mainloop.Wakeup();
}
2016-01-10 12:18:45 +01:00
// if the sync GPU callback is suspended, wake it up.
if (!is_dual_core || m_use_deterministic_gpu_thread || m_config_sync_gpu)
2016-01-10 12:18:45 +01:00
{
if (m_syncing_suspended)
2016-01-10 12:18:45 +01:00
{
m_syncing_suspended = false;
m_system.GetCoreTiming().ScheduleEvent(GPU_TIME_SLOT_SIZE, m_event_sync_gpu,
GPU_TIME_SLOT_SIZE);
2016-01-10 12:18:45 +01:00
}
}
}
int FifoManager::RunGpuOnCpu(int ticks)
2016-01-10 12:18:45 +01:00
{
auto& command_processor = m_system.GetCommandProcessor();
auto& fifo = command_processor.GetFifo();
2016-01-10 12:18:45 +01:00
bool reset_simd_state = false;
int available_ticks = int(ticks * m_config_sync_gpu_overclock) + m_sync_ticks.load();
2021-05-13 19:30:30 +02:00
while (fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) &&
fifo.CPReadWriteDistance.load(std::memory_order_relaxed) && !AtBreakpoint(m_system) &&
2021-05-13 19:30:30 +02:00
available_ticks >= 0)
2016-01-10 12:18:45 +01:00
{
if (m_use_deterministic_gpu_thread)
2016-01-10 12:18:45 +01:00
{
ReadDataFromFifoOnCPU(fifo.CPReadPointer.load(std::memory_order_relaxed));
m_gpu_mainloop.Wakeup();
2016-01-10 12:18:45 +01:00
}
else
{
if (!reset_simd_state)
{
Common::FPU::SaveSIMDState();
Common::FPU::LoadDefaultSIMDState();
2016-01-10 12:18:45 +01:00
reset_simd_state = true;
}
ReadDataFromFifo(fifo.CPReadPointer.load(std::memory_order_relaxed));
2016-01-10 12:18:45 +01:00
u32 cycles = 0;
m_video_buffer_read_ptr = OpcodeDecoder::RunFifo(
DataReader(m_video_buffer_read_ptr, m_video_buffer_write_ptr), &cycles);
2016-01-10 12:18:45 +01:00
available_ticks -= cycles;
}
2021-05-13 18:44:59 +02:00
if (fifo.CPReadPointer.load(std::memory_order_relaxed) ==
fifo.CPEnd.load(std::memory_order_relaxed))
{
fifo.CPReadPointer.store(fifo.CPBase.load(std::memory_order_relaxed),
std::memory_order_relaxed);
}
2016-01-10 12:18:45 +01:00
else
2021-05-13 18:44:59 +02:00
{
fifo.CPReadPointer.fetch_add(GPFifo::GATHER_PIPE_SIZE, std::memory_order_relaxed);
2021-05-13 18:44:59 +02:00
}
2016-01-10 12:18:45 +01:00
fifo.CPReadWriteDistance.fetch_sub(GPFifo::GATHER_PIPE_SIZE, std::memory_order_relaxed);
2016-01-10 12:18:45 +01:00
}
command_processor.SetCPStatusFromGPU();
2016-01-10 12:18:45 +01:00
if (reset_simd_state)
{
Common::FPU::LoadSIMDState();
2016-01-10 12:18:45 +01:00
}
// Discard all available ticks as there is nothing to do any more.
m_sync_ticks.store(std::min(available_ticks, 0));
2016-01-10 12:18:45 +01:00
// If the GPU is idle, drop the handler.
if (available_ticks >= 0)
return -1;
// Always wait at least for GPU_TIME_SLOT_SIZE cycles.
return -available_ticks + GPU_TIME_SLOT_SIZE;
}
void FifoManager::UpdateWantDeterminism(bool want)
{
2015-03-29 15:05:11 +02:00
// We are paused (or not running at all yet), so
// it should be safe to change this.
bool gpu_thread = false;
switch (Config::GetGPUDeterminismMode())
{
case Config::GPUDeterminismMode::Auto:
2014-09-06 17:43:43 -04:00
gpu_thread = want;
break;
case Config::GPUDeterminismMode::Disabled:
2014-09-06 17:43:43 -04:00
gpu_thread = false;
break;
case Config::GPUDeterminismMode::FakeCompletion:
2014-09-06 17:43:43 -04:00
gpu_thread = true;
break;
}
gpu_thread = gpu_thread && m_system.IsDualCoreMode();
2014-09-06 17:43:43 -04:00
if (m_use_deterministic_gpu_thread != gpu_thread)
2014-09-06 17:43:43 -04:00
{
m_use_deterministic_gpu_thread = gpu_thread;
2014-09-06 17:43:43 -04:00
if (gpu_thread)
{
// These haven't been updated in non-deterministic mode.
m_video_buffer_seen_ptr = m_video_buffer_pp_read_ptr = m_video_buffer_read_ptr;
2014-09-06 17:43:43 -04:00
CopyPreprocessCPStateFromMain();
VertexLoaderManager::MarkAllDirty();
}
}
}
2015-06-03 23:21:46 +02:00
2016-01-20 19:42:37 +01:00
/* This function checks the emulated CPU - GPU distance and may wake up the GPU,
2016-01-10 12:18:45 +01:00
* or block the CPU if required. It should be called by the CPU thread regularly.
2016-01-20 19:42:37 +01:00
* @ticks The gone emulated CPU time.
2016-01-10 12:18:45 +01:00
* @return A good time to call WaitForGpuThread() next.
2016-01-20 19:42:37 +01:00
*/
int FifoManager::WaitForGpuThread(int ticks)
2015-06-03 23:21:46 +02:00
{
int old = m_sync_ticks.fetch_add(ticks);
2016-10-01 13:16:50 +02:00
int now = old + ticks;
// GPU is idle, so stop polling.
if (old >= 0 && m_gpu_mainloop.IsDone())
2016-10-01 13:16:50 +02:00
return -1;
2015-06-03 23:21:46 +02:00
2016-01-20 19:42:37 +01:00
// Wakeup GPU
if (old < m_config_sync_gpu_min_distance && now >= m_config_sync_gpu_min_distance)
RunGpu();
2015-06-03 23:21:46 +02:00
2016-10-01 13:16:50 +02:00
// If the GPU is still sleeping, wait for a longer time
if (now < m_config_sync_gpu_min_distance)
return GPU_TIME_SLOT_SIZE + m_config_sync_gpu_min_distance - now;
2015-06-03 23:21:46 +02:00
2016-10-01 13:16:50 +02:00
// Wait for GPU
if (now >= m_config_sync_gpu_max_distance)
m_sync_wakeup_event.Wait();
2016-10-01 13:16:50 +02:00
return GPU_TIME_SLOT_SIZE;
2015-06-03 23:21:46 +02:00
}
2016-01-12 22:44:58 +01:00
void FifoManager::SyncGPUCallback(Core::System& system, u64 ticks, s64 cyclesLate)
2016-01-19 00:08:18 +01:00
{
2016-01-10 12:18:45 +01:00
ticks += cyclesLate;
int next = -1;
2016-01-19 00:08:18 +01:00
auto& fifo = system.GetFifo();
if (!system.IsDualCoreMode() || fifo.m_use_deterministic_gpu_thread)
2016-01-10 12:18:45 +01:00
{
next = fifo.RunGpuOnCpu(int(ticks));
2016-01-10 12:18:45 +01:00
}
else if (fifo.m_config_sync_gpu)
2016-01-10 12:18:45 +01:00
{
next = fifo.WaitForGpuThread(int(ticks));
2016-01-10 12:18:45 +01:00
}
fifo.m_syncing_suspended = next < 0;
if (!fifo.m_syncing_suspended)
system.GetCoreTiming().ScheduleEvent(next, fifo.m_event_sync_gpu, next);
2016-01-19 00:08:18 +01:00
}
void FifoManager::SyncGPUForRegisterAccess()
{
SyncGPU(SyncGPUReason::Other);
if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread)
RunGpuOnCpu(GPU_TIME_SLOT_SIZE);
else if (m_config_sync_gpu)
WaitForGpuThread(GPU_TIME_SLOT_SIZE);
}
2016-01-20 19:42:37 +01:00
// Initialize GPU - CPU thread syncing, this gives us a deterministic way to start the GPU thread.
void FifoManager::Prepare()
2016-01-19 00:08:18 +01:00
{
m_event_sync_gpu = m_system.GetCoreTiming().RegisterEvent("SyncGPUCallback", SyncGPUCallback);
m_syncing_suspended = true;
2016-01-19 00:08:18 +01:00
}
2019-05-05 23:48:12 +00:00
} // namespace Fifo