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.

610 lines
19 KiB
C++
Raw Normal View History

// Copyright 2008 Dolphin Emulator Project
2015-05-18 01:08:10 +02:00
// Licensed under GPLv2+
// Refer to the license.txt file included.
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
2014-09-09 00:24:49 -04:00
#include "Core/ConfigManager.h"
2016-01-19 00:08:18 +01:00
#include "Core/CoreTiming.h"
2014-02-17 05:18:15 -05:00
#include "Core/HW/Memmap.h"
#include "Core/Host.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"
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
{
2015-12-25 16:07:00 -05:00
static constexpr u32 FIFO_SIZE = 2 * 1024 * 1024;
2016-01-10 12:18:45 +01:00
static constexpr int GPU_TIME_SLOT_SIZE = 1000;
2015-12-25 16:07:00 -05:00
2015-05-27 20:53:09 +02:00
static Common::BlockingLoop s_gpu_mainloop;
static Common::Flag s_emu_running_state;
2014-08-27 22:56:19 -04:00
// Most of this array is unlikely to be faulted in...
static u8 s_fifo_aux_data[FIFO_SIZE];
static u8* s_fifo_aux_write_ptr;
static u8* s_fifo_aux_read_ptr;
// This could be in SConfig, but it depends on multiple settings
// and can change at runtime.
static bool s_use_deterministic_gpu_thread;
2014-08-27 22:56:19 -04:00
static CoreTiming::EventType* s_event_sync_gpu;
2016-01-19 00:08:18 +01:00
2014-08-27 22:56:19 -04:00
// STATE_TO_SAVE
static u8* s_video_buffer;
2014-11-26 22:12:54 +01:00
static u8* s_video_buffer_read_ptr;
2014-08-27 22:56:19 -04:00
static std::atomic<u8*> s_video_buffer_write_ptr;
static std::atomic<u8*> s_video_buffer_seen_ptr;
2014-11-26 22:12:54 +01:00
static u8* s_video_buffer_pp_read_ptr;
2014-08-27 22:56:19 -04:00
// The read_ptr is always owned by the GPU thread. In normal mode, so is the
// write_ptr, despite it being atomic. In deterministic GPU thread mode,
2014-08-27 22:56:19 -04:00
// things get a bit more complicated:
// - The seen_ptr is written by the GPU thread, and points to what it's already
// processed as much of as possible - in the case of a partial command which
// caused it to stop, not the same as the read ptr. It's written by the GPU,
// under the lock, and updating the cond.
// - The write_ptr is written by the CPU thread after it copies data from the
// FIFO. Maybe someday it will be under the lock. For now, because RunGpuLoop
// polls, it's just atomic.
// - The pp_read_ptr is the CPU preprocessing version of the read_ptr.
2015-06-03 23:21:46 +02:00
static std::atomic<int> s_sync_ticks;
2016-01-10 12:18:45 +01:00
static bool s_syncing_suspended;
2015-06-03 23:21:46 +02:00
static Common::Event s_sync_wakeup_event;
2016-01-12 22:44:58 +01:00
void DoState(PointerWrap& p)
2008-12-08 05:30:24 +00:00
{
p.DoArray(s_video_buffer, FIFO_SIZE);
2014-08-27 22:56:19 -04:00
u8* write_ptr = s_video_buffer_write_ptr;
p.DoPointer(write_ptr, s_video_buffer);
s_video_buffer_write_ptr = write_ptr;
2014-11-26 22:12:54 +01:00
p.DoPointer(s_video_buffer_read_ptr, s_video_buffer);
if (p.mode == PointerWrap::MODE_READ && s_use_deterministic_gpu_thread)
2014-08-27 22:56:19 -04:00
{
// We're good and paused, right?
2014-11-26 22:12:54 +01:00
s_video_buffer_seen_ptr = s_video_buffer_pp_read_ptr = s_video_buffer_read_ptr;
2014-08-27 22:56:19 -04:00
}
2016-01-10 12:18:45 +01:00
p.Do(s_sync_ticks);
2016-10-11 18:27:46 +02:00
p.Do(s_syncing_suspended);
2008-12-08 05:30:24 +00:00
}
2016-01-12 22:44:58 +01:00
void PauseAndLock(bool doLock, bool unpauseOnUnlock)
{
if (doLock)
{
2016-08-18 22:35:58 -04:00
SyncGPU(SyncGPUReason::Other);
EmulatorState(false);
const SConfig& param = SConfig::GetInstance();
if (!param.bCPUThread || s_use_deterministic_gpu_thread)
return;
s_gpu_mainloop.WaitYield(std::chrono::milliseconds(100), Host_YieldToUI);
}
else
{
if (unpauseOnUnlock)
EmulatorState(true);
}
}
2016-01-12 22:44:58 +01:00
void Init()
{
// Padded so that SIMD overreads in the vertex loader are safe
2016-08-07 13:03:07 -04:00
s_video_buffer = static_cast<u8*>(Common::AllocateMemoryPages(FIFO_SIZE + 4));
2014-08-27 22:56:19 -04:00
ResetVideoBuffer();
if (SConfig::GetInstance().bCPUThread)
2015-05-27 20:53:09 +02:00
s_gpu_mainloop.Prepare();
2015-06-03 23:21:46 +02:00
s_sync_ticks.store(0);
2008-12-08 05:30:24 +00:00
}
2016-01-12 22:44:58 +01:00
void Shutdown()
2008-12-08 05:30:24 +00:00
{
2015-05-27 20:53:09 +02:00
if (s_gpu_mainloop.IsRunning())
2020-11-13 22:33:26 -05:00
PanicAlertFmt("FIFO shutting down while active");
2016-08-07 13:03:07 -04:00
Common::FreeMemoryPages(s_video_buffer, FIFO_SIZE + 4);
s_video_buffer = nullptr;
2014-08-27 22:56:19 -04:00
s_video_buffer_write_ptr = nullptr;
2014-11-26 22:12:54 +01:00
s_video_buffer_pp_read_ptr = nullptr;
s_video_buffer_read_ptr = nullptr;
2014-08-27 22:56:19 -04:00
s_video_buffer_seen_ptr = nullptr;
s_fifo_aux_write_ptr = nullptr;
s_fifo_aux_read_ptr = nullptr;
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 ExitGpuLoop()
{
2010-01-16 22:37:38 +00:00
// This should break the wait loop in CPU thread
CommandProcessor::fifo.bFF_GPReadEnable = false;
2015-03-05 21:14:46 +01:00
FlushGpu();
2010-01-16 22:37:38 +00:00
// Terminate GPU thread loop
s_emu_running_state.Set();
s_gpu_mainloop.Stop(s_gpu_mainloop.kNonBlock);
2010-01-03 16:04:40 +00:00
}
void EmulatorState(bool running)
2010-01-03 16:04:40 +00:00
{
s_emu_running_state.Set(running);
if (running)
s_gpu_mainloop.Wakeup();
else
s_gpu_mainloop.AllowSleep();
2009-03-07 23:34:16 +00:00
}
2014-08-27 22:56:19 -04:00
void SyncGPU(SyncGPUReason reason, bool may_move_read_ptr)
{
if (s_use_deterministic_gpu_thread)
2014-08-27 22:56:19 -04:00
{
2015-05-27 20:53:09 +02:00
s_gpu_mainloop.Wait();
if (!s_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 && s_fifo_aux_write_ptr != s_fifo_aux_read_ptr)
2020-11-13 22:33:26 -05:00
{
PanicAlertFmt("Aux FIFO not synced ({}, {})", fmt::ptr(s_fifo_aux_write_ptr),
fmt::ptr(s_fifo_aux_read_ptr));
}
2014-08-27 22:56:19 -04:00
memmove(s_fifo_aux_data, s_fifo_aux_read_ptr, s_fifo_aux_write_ptr - s_fifo_aux_read_ptr);
s_fifo_aux_write_ptr -= (s_fifo_aux_read_ptr - s_fifo_aux_data);
s_fifo_aux_read_ptr = s_fifo_aux_data;
if (may_move_read_ptr)
{
2015-05-27 20:53:09 +02:00
u8* write_ptr = s_video_buffer_write_ptr;
2014-08-27 22:56:19 -04:00
// what's left over in the buffer
2014-11-26 22:12:54 +01:00
size_t size = write_ptr - s_video_buffer_pp_read_ptr;
2014-08-27 22:56:19 -04:00
2014-11-26 22:12:54 +01:00
memmove(s_video_buffer, s_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.
s_video_buffer_write_ptr = write_ptr = s_video_buffer + size;
2014-11-26 22:12:54 +01:00
s_video_buffer_pp_read_ptr = s_video_buffer;
s_video_buffer_read_ptr = s_video_buffer;
2014-08-27 22:56:19 -04:00
s_video_buffer_seen_ptr = write_ptr;
}
}
}
2017-03-26 20:38:19 -04:00
void PushFifoAuxBuffer(const void* ptr, size_t size)
2014-08-27 22:56:19 -04:00
{
if (size > (size_t)(s_fifo_aux_data + FIFO_SIZE - s_fifo_aux_write_ptr))
{
2016-08-18 22:35:58 -04:00
SyncGPU(SyncGPUReason::AuxSpace, /* may_move_read_ptr */ false);
2015-05-27 20:53:09 +02:00
if (!s_gpu_mainloop.IsRunning())
{
// GPU is shutting down
return;
}
2014-08-27 22:56:19 -04:00
if (size > (size_t)(s_fifo_aux_data + FIFO_SIZE - s_fifo_aux_write_ptr))
{
// 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(s_fifo_aux_write_ptr, ptr, size);
s_fifo_aux_write_ptr += size;
}
void* PopFifoAuxBuffer(size_t size)
{
void* ret = s_fifo_aux_read_ptr;
s_fifo_aux_read_ptr += size;
return ret;
}
// Description: RunGpuLoop() sends data through this function.
2014-11-01 12:24:43 +01:00
static void ReadDataFromFifo(u32 readPtr)
{
2020-11-13 22:33:26 -05:00
constexpr size_t len = 32;
if (len > static_cast<size_t>(s_video_buffer + FIFO_SIZE - s_video_buffer_write_ptr))
{
2020-11-13 22:33:26 -05:00
const size_t existing_len = s_video_buffer_write_ptr - s_video_buffer_read_ptr;
if (len > static_cast<size_t>(FIFO_SIZE - existing_len))
{
2020-11-13 22:33:26 -05:00
PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len, len, FIFO_SIZE);
return;
}
2014-11-26 22:12:54 +01:00
memmove(s_video_buffer, s_video_buffer_read_ptr, existing_len);
s_video_buffer_write_ptr = s_video_buffer + existing_len;
2014-11-26 22:12:54 +01:00
s_video_buffer_read_ptr = s_video_buffer;
}
// Copy new video instructions to s_video_buffer for future use in rendering the new picture
2014-11-01 12:24:43 +01:00
Memory::CopyFromEmu(s_video_buffer_write_ptr, readPtr, len);
s_video_buffer_write_ptr += len;
}
2014-08-27 22:56:19 -04:00
// The deterministic_gpu_thread version.
2014-11-01 12:24:43 +01:00
static void ReadDataFromFifoOnCPU(u32 readPtr)
2014-08-27 22:56:19 -04:00
{
2020-11-13 22:33:26 -05:00
constexpr size_t len = 32;
2014-08-27 22:56:19 -04:00
u8* write_ptr = s_video_buffer_write_ptr;
2020-11-13 22:33:26 -05:00
if (len > static_cast<size_t>(s_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);
2015-05-27 20:53:09 +02:00
if (!s_gpu_mainloop.IsRunning())
{
2015-05-27 20:53:09 +02:00
// GPU is shutting down, so the next asserts may fail
return;
}
2014-11-26 22:12:54 +01:00
if (s_video_buffer_pp_read_ptr != s_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 = s_video_buffer_write_ptr;
2020-11-13 22:33:26 -05:00
const size_t existing_len = write_ptr - s_video_buffer_pp_read_ptr;
if (len > static_cast<size_t>(FIFO_SIZE - existing_len))
2014-08-27 22:56:19 -04:00
{
2020-11-13 22:33:26 -05:00
PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len, len, FIFO_SIZE);
2014-08-27 22:56:19 -04:00
return;
}
}
2014-11-01 12:24:43 +01:00
Memory::CopyFromEmu(s_video_buffer_write_ptr, readPtr, len);
2016-01-24 01:29:44 -05:00
s_video_buffer_pp_read_ptr = OpcodeDecoder::Run<true>(
DataReader(s_video_buffer_pp_read_ptr, write_ptr + len), nullptr, false);
2014-08-27 22:56:19 -04:00
// This would have to be locked if the GPU thread didn't spin.
s_video_buffer_write_ptr = write_ptr + len;
}
void ResetVideoBuffer()
{
2014-11-26 22:12:54 +01:00
s_video_buffer_read_ptr = s_video_buffer;
s_video_buffer_write_ptr = s_video_buffer;
2014-08-27 22:56:19 -04:00
s_video_buffer_seen_ptr = s_video_buffer;
2014-11-26 22:12:54 +01:00
s_video_buffer_pp_read_ptr = s_video_buffer;
2014-08-27 22:56:19 -04:00
s_fifo_aux_write_ptr = s_fifo_aux_data;
s_fifo_aux_read_ptr = s_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 RunGpuLoop()
2008-12-08 05:30:24 +00:00
{
AsyncRequests::GetInstance()->SetEnable(true);
AsyncRequests::GetInstance()->SetPassthrough(false);
2008-12-08 05:30:24 +00:00
s_gpu_mainloop.Run(
[] {
const SConfig& param = SConfig::GetInstance();
// Run events from the CPU thread.
AsyncRequests::GetInstance()->PullEvents();
2015-05-27 20:53:09 +02:00
// Do nothing while paused
if (!s_emu_running_state.IsSet())
2013-03-13 18:23:16 -05:00
return;
2015-05-27 20:53:09 +02:00
if (s_use_deterministic_gpu_thread)
{
// All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder.
2014-08-27 22:56:19 -04:00
u8* seen_ptr = s_video_buffer_seen_ptr;
u8* write_ptr = s_video_buffer_write_ptr;
// See comment in SyncGPU
if (write_ptr > seen_ptr)
{
2016-01-24 01:29:44 -05:00
s_video_buffer_read_ptr =
OpcodeDecoder::Run(DataReader(s_video_buffer_read_ptr, write_ptr), nullptr, false);
2015-05-27 20:53:09 +02:00
s_video_buffer_seen_ptr = write_ptr;
}
}
else
2010-01-23 21:06:12 +00:00
{
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
2015-03-29 15:05:11 +02:00
CommandProcessor::SetCPStatusFromGPU();
// check if we are able to run this buffer
while (!CommandProcessor::IsInterruptWaiting() && fifo.bFF_GPReadEnable &&
2021-05-13 18:44:59 +02:00
fifo.CPReadWriteDistance.load(std::memory_order_relaxed) && !AtBreakpoint())
{
if (param.bSyncGPU && s_sync_ticks.load() < param.iSyncGpuMinDistance)
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);
2015-06-03 23:21:46 +02:00
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
2015-06-03 23:21:46 +02:00
readPtr += 32;
2014-08-27 22:56:19 -04:00
2021-05-13 18:44:59 +02:00
ASSERT_MSG(COMMANDPROCESSOR,
(s32)fifo.CPReadWriteDistance.load(std::memory_order_relaxed) - 32 >= 0,
2018-03-14 20:34:35 -04:00
"Negative fifo.CPReadWriteDistance = %i in FIFO Loop !\nThat can produce "
"instability in the game. Please report it.",
2021-05-13 18:44:59 +02:00
fifo.CPReadWriteDistance.load(std::memory_order_relaxed) - 32);
2015-06-03 23:21:46 +02:00
u8* write_ptr = s_video_buffer_write_ptr;
s_video_buffer_read_ptr = OpcodeDecoder::Run(
DataReader(s_video_buffer_read_ptr, write_ptr), &cyclesExecuted, false);
2021-05-13 18:44:59 +02:00
fifo.CPReadPointer.store(readPtr, std::memory_order_relaxed);
fifo.CPReadWriteDistance.fetch_sub(32, std::memory_order_seq_cst);
2016-01-24 01:29:44 -05:00
if ((write_ptr - s_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
CommandProcessor::SetCPStatusFromGPU();
2014-08-27 22:56:19 -04:00
if (param.bSyncGPU)
{
2014-08-27 22:56:19 -04:00
cyclesExecuted = (int)(cyclesExecuted / param.fSyncGpuOverclock);
int old = s_sync_ticks.fetch_sub(cyclesExecuted);
2016-10-01 13:16:50 +02:00
if (old >= param.iSyncGpuMaxDistance &&
old - (int)cyclesExecuted < param.iSyncGpuMaxDistance)
2014-08-27 22:56:19 -04:00
s_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
// leading the CPU thread to wait in Video_BeginField 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 (s_sync_ticks.load() > 0)
{
2014-08-27 22:56:19 -04:00
int old = s_sync_ticks.exchange(0);
2016-10-01 13:16:50 +02:00
if (old >= param.iSyncGpuMaxDistance)
s_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();
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-12-08 05:30:24 +00:00
}
2015-03-05 21:14:46 +01:00
void FlushGpu()
{
const SConfig& param = SConfig::GetInstance();
2015-06-03 23:21:46 +02:00
if (!param.bCPUThread || s_use_deterministic_gpu_thread)
2015-03-05 21:14:46 +01:00
return;
2015-05-27 20:53:09 +02:00
s_gpu_mainloop.Wait();
}
void GpuMaySleep()
{
s_gpu_mainloop.AllowSleep();
2015-03-05 21:14:46 +01:00
}
bool AtBreakpoint()
{
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
2021-05-13 18:44:59 +02:00
return fifo.bFF_BPEnable && (fifo.CPReadPointer.load(std::memory_order_relaxed) ==
fifo.CPBreakpoint.load(std::memory_order_relaxed));
}
void RunGpu()
{
const SConfig& param = SConfig::GetInstance();
// wake up GPU thread
2016-01-10 12:18:45 +01:00
if (param.bCPUThread && !s_use_deterministic_gpu_thread)
{
2015-05-27 20:53:09 +02:00
s_gpu_mainloop.Wakeup();
}
2016-01-10 12:18:45 +01:00
// if the sync GPU callback is suspended, wake it up.
if (!SConfig::GetInstance().bCPUThread || s_use_deterministic_gpu_thread ||
SConfig::GetInstance().bSyncGPU)
{
if (s_syncing_suspended)
{
s_syncing_suspended = false;
CoreTiming::ScheduleEvent(GPU_TIME_SLOT_SIZE, s_event_sync_gpu, GPU_TIME_SLOT_SIZE);
}
}
}
static int RunGpuOnCpu(int ticks)
{
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
2016-01-10 12:18:45 +01:00
bool reset_simd_state = false;
int available_ticks = int(ticks * SConfig::GetInstance().fSyncGpuOverclock) + s_sync_ticks.load();
2021-05-13 18:44:59 +02:00
while (fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance.load(std::memory_order_relaxed) &&
!AtBreakpoint() && available_ticks >= 0)
2016-01-10 12:18:45 +01:00
{
if (s_use_deterministic_gpu_thread)
{
2021-05-13 18:44:59 +02:00
ReadDataFromFifoOnCPU(fifo.CPReadPointer.load(std::memory_order_relaxed));
2016-01-10 12:18:45 +01:00
s_gpu_mainloop.Wakeup();
}
else
{
if (!reset_simd_state)
{
FPURoundMode::SaveSIMDState();
FPURoundMode::LoadDefaultSIMDState();
reset_simd_state = true;
}
2021-05-13 18:44:59 +02:00
ReadDataFromFifo(fifo.CPReadPointer.load(std::memory_order_relaxed));
2016-01-10 12:18:45 +01:00
u32 cycles = 0;
s_video_buffer_read_ptr = OpcodeDecoder::Run(
DataReader(s_video_buffer_read_ptr, s_video_buffer_write_ptr), &cycles, false);
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(32, std::memory_order_relaxed);
}
2016-01-10 12:18:45 +01:00
2021-05-13 18:44:59 +02:00
fifo.CPReadWriteDistance.fetch_sub(32, std::memory_order_relaxed);
2016-01-10 12:18:45 +01:00
}
CommandProcessor::SetCPStatusFromGPU();
if (reset_simd_state)
{
FPURoundMode::LoadSIMDState();
}
// Discard all available ticks as there is nothing to do any more.
s_sync_ticks.store(std::min(available_ticks, 0));
// 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;
}
2016-01-12 22:44:58 +01:00
void 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.
const SConfig& param = SConfig::GetInstance();
bool gpu_thread = false;
2014-09-06 17:43:43 -04:00
switch (param.m_GPUDeterminismMode)
{
case GPUDeterminismMode::Auto:
2014-09-06 17:43:43 -04:00
gpu_thread = want;
break;
case GPUDeterminismMode::Disabled:
2014-09-06 17:43:43 -04:00
gpu_thread = false;
break;
case GPUDeterminismMode::FakeCompletion:
2014-09-06 17:43:43 -04:00
gpu_thread = true;
break;
}
2015-06-03 23:21:46 +02:00
gpu_thread = gpu_thread && param.bCPUThread;
2014-09-06 17:43:43 -04:00
if (s_use_deterministic_gpu_thread != gpu_thread)
2014-09-06 17:43:43 -04:00
{
s_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.
2014-11-26 22:12:54 +01:00
s_video_buffer_seen_ptr = s_video_buffer_pp_read_ptr = s_video_buffer_read_ptr;
2014-09-06 17:43:43 -04:00
CopyPreprocessCPStateFromMain();
VertexLoaderManager::MarkAllDirty();
}
}
}
2015-06-03 23:21:46 +02:00
bool UseDeterministicGPUThread()
{
return s_use_deterministic_gpu_thread;
}
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
*/
2016-01-10 12:18:45 +01:00
static int WaitForGpuThread(int ticks)
2015-06-03 23:21:46 +02:00
{
const SConfig& param = SConfig::GetInstance();
2015-06-03 23:21:46 +02:00
2016-10-01 13:16:50 +02:00
int old = s_sync_ticks.fetch_add(ticks);
int now = old + ticks;
// GPU is idle, so stop polling.
if (old >= 0 && s_gpu_mainloop.IsDone())
return -1;
2015-06-03 23:21:46 +02:00
2016-01-20 19:42:37 +01:00
// Wakeup GPU
2016-10-01 13:16:50 +02:00
if (old < param.iSyncGpuMinDistance && now >= param.iSyncGpuMinDistance)
2015-06-03 23:21:46 +02:00
RunGpu();
2016-10-01 13:16:50 +02:00
// If the GPU is still sleeping, wait for a longer time
if (now < param.iSyncGpuMinDistance)
return GPU_TIME_SLOT_SIZE + param.iSyncGpuMinDistance - now;
2015-06-03 23:21:46 +02:00
2016-10-01 13:16:50 +02:00
// Wait for GPU
if (now >= param.iSyncGpuMaxDistance)
s_sync_wakeup_event.Wait();
return GPU_TIME_SLOT_SIZE;
2015-06-03 23:21:46 +02:00
}
2016-01-12 22:44:58 +01:00
2016-01-10 12:18:45 +01:00
static void SyncGPUCallback(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
2016-01-10 12:18:45 +01:00
if (!SConfig::GetInstance().bCPUThread || s_use_deterministic_gpu_thread)
{
next = RunGpuOnCpu((int)ticks);
}
else if (SConfig::GetInstance().bSyncGPU)
{
next = WaitForGpuThread((int)ticks);
}
s_syncing_suspended = next < 0;
if (!s_syncing_suspended)
CoreTiming::ScheduleEvent(next, s_event_sync_gpu, next);
2016-01-19 00:08:18 +01:00
}
void SyncGPUForRegisterAccess()
{
SyncGPU(SyncGPUReason::Other);
if (!SConfig::GetInstance().bCPUThread || s_use_deterministic_gpu_thread)
RunGpuOnCpu(GPU_TIME_SLOT_SIZE);
else if (SConfig::GetInstance().bSyncGPU)
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.
2016-01-19 00:08:18 +01:00
void Prepare()
{
2016-01-10 12:18:45 +01:00
s_event_sync_gpu = CoreTiming::RegisterEvent("SyncGPUCallback", SyncGPUCallback);
s_syncing_suspended = true;
2016-01-19 00:08:18 +01:00
}
2019-05-05 23:48:12 +00:00
} // namespace Fifo