| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/Fifo.h" |
| 5 | |
| 6 | #include <atomic> |
| 7 | #include <cstring> |
| 8 | |
| 9 | #include "Common/Assert.h" |
| 10 | #include "Common/BlockingLoop.h" |
| 11 | #include "Common/ChunkFile.h" |
| 12 | #include "Common/Event.h" |
| 13 | #include "Common/FPURoundMode.h" |
| 14 | #include "Common/MemoryUtil.h" |
| 15 | #include "Common/MsgHandler.h" |
| 16 | |
| 17 | #include "Core/Config/MainSettings.h" |
| 18 | #include "Core/ConfigManager.h" |
| 19 | #include "Core/CoreTiming.h" |
| 20 | #include "Core/HW/GPFifo.h" |
| 21 | #include "Core/HW/Memmap.h" |
| 22 | #include "Core/Host.h" |
| 23 | #include "Core/System.h" |
| 24 | |
| 25 | #include "VideoCommon/AsyncRequests.h" |
| 26 | #include "VideoCommon/CPMemory.h" |
| 27 | #include "VideoCommon/CommandProcessor.h" |
| 28 | #include "VideoCommon/DataReader.h" |
| 29 | #include "VideoCommon/FramebufferManager.h" |
| 30 | #include "VideoCommon/OpcodeDecoding.h" |
| 31 | #include "VideoCommon/VertexLoaderManager.h" |
| 32 | #include "VideoCommon/VertexManagerBase.h" |
| 33 | #include "VideoCommon/VideoBackendBase.h" |
| 34 | |
| 35 | namespace Fifo |
| 36 | { |
| 37 | static constexpr int GPU_TIME_SLOT_SIZE = 1000; |
| 38 | |
| 39 | FifoManager::FifoManager(Core::System& system) : m_system{system} |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | FifoManager::~FifoManager() = default; |
| 44 | |
| 45 | void FifoManager::RefreshConfig() |
| 46 | { |
| 47 | m_config_sync_gpu = Config::Get(Config::MAIN_SYNC_GPU); |
| 48 | m_config_sync_gpu_max_distance = Config::Get(Config::MAIN_SYNC_GPU_MAX_DISTANCE); |
| 49 | m_config_sync_gpu_min_distance = Config::Get(Config::MAIN_SYNC_GPU_MIN_DISTANCE); |
| 50 | m_config_sync_gpu_overclock = Config::Get(Config::MAIN_SYNC_GPU_OVERCLOCK); |
| 51 | } |
| 52 | |
| 53 | void FifoManager::DoState(PointerWrap& p) |
| 54 | { |
| 55 | p.DoArray(m_video_buffer, FIFO_SIZE); |
| 56 | u8* write_ptr = m_video_buffer_write_ptr; |
| 57 | p.DoPointer(write_ptr, m_video_buffer); |
| 58 | m_video_buffer_write_ptr = write_ptr; |
| 59 | p.DoPointer(m_video_buffer_read_ptr, m_video_buffer); |
| 60 | if (p.IsReadMode() && m_use_deterministic_gpu_thread) |
| 61 | { |
| 62 | // We're good and paused, right? |
| 63 | m_video_buffer_seen_ptr = m_video_buffer_pp_read_ptr = m_video_buffer_read_ptr; |
| 64 | } |
| 65 | |
| 66 | p.Do(m_sync_ticks); |
| 67 | p.Do(m_syncing_suspended); |
| 68 | } |
| 69 | |
| 70 | void FifoManager::PauseAndLock() |
| 71 | { |
| 72 | SyncGPU(SyncGPUReason::Other); |
| 73 | EmulatorState(false); |
| 74 | |
| 75 | if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread) |
| 76 | return; |
| 77 | |
| 78 | m_gpu_mainloop.WaitYield(std::chrono::milliseconds(100), Host_YieldToUI); |
| 79 | } |
| 80 | |
| 81 | void FifoManager::RestoreState(const bool was_running) |
| 82 | { |
| 83 | if (was_running) |
| 84 | EmulatorState(true); |
| 85 | } |
| 86 | |
| 87 | void FifoManager::Init() |
| 88 | { |
| 89 | if (!m_config_callback_id) |
| 90 | m_config_callback_id = Config::AddConfigChangedCallback([this] { RefreshConfig(); }); |
| 91 | RefreshConfig(); |
| 92 | |
| 93 | // Padded so that SIMD overreads in the vertex loader are safe |
| 94 | m_video_buffer = static_cast<u8*>(Common::AllocateMemoryPages(FIFO_SIZE + 4)); |
| 95 | ResetVideoBuffer(); |
| 96 | if (m_system.IsDualCoreMode()) |
| 97 | m_gpu_mainloop.Prepare(); |
| 98 | m_sync_ticks.store(0); |
| 99 | } |
| 100 | |
| 101 | void FifoManager::Shutdown() |
| 102 | { |
| 103 | if (m_gpu_mainloop.IsRunning()) |
| 104 | PanicAlertFmt("FIFO shutting down while active"); |
| 105 | |
| 106 | Common::FreeMemoryPages(m_video_buffer, FIFO_SIZE + 4); |
| 107 | m_video_buffer = nullptr; |
| 108 | m_video_buffer_write_ptr = nullptr; |
| 109 | m_video_buffer_pp_read_ptr = nullptr; |
| 110 | m_video_buffer_read_ptr = nullptr; |
| 111 | m_video_buffer_seen_ptr = nullptr; |
| 112 | m_fifo_aux_write_ptr = nullptr; |
| 113 | m_fifo_aux_read_ptr = nullptr; |
| 114 | |
| 115 | if (m_config_callback_id) |
| 116 | { |
| 117 | Config::RemoveConfigChangedCallback(*m_config_callback_id); |
| 118 | m_config_callback_id = std::nullopt; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // May be executed from any thread, even the graphics thread. |
| 123 | // Created to allow for self shutdown. |
| 124 | void FifoManager::ExitGpuLoop() |
| 125 | { |
| 126 | auto& command_processor = m_system.GetCommandProcessor(); |
| 127 | auto& fifo = command_processor.GetFifo(); |
| 128 | |
| 129 | // This should break the wait loop in CPU thread |
| 130 | fifo.bFF_GPReadEnable.store(0, std::memory_order_relaxed); |
| 131 | FlushGpu(); |
| 132 | |
| 133 | // Terminate GPU thread loop |
| 134 | m_emu_running_state.Set(); |
| 135 | m_gpu_mainloop.Stop(Common::BlockingLoop::StopMode::NonBlock); |
| 136 | } |
| 137 | |
| 138 | void FifoManager::EmulatorState(bool running) |
| 139 | { |
| 140 | m_emu_running_state.Set(running); |
| 141 | if (running) |
| 142 | m_gpu_mainloop.Wakeup(); |
| 143 | else |
| 144 | m_gpu_mainloop.AllowSleep(); |
| 145 | } |
| 146 | |
| 147 | void FifoManager::SyncGPU(SyncGPUReason reason, bool may_move_read_ptr) |
| 148 | { |
| 149 | if (m_use_deterministic_gpu_thread) |
| 150 | { |
| 151 | m_gpu_mainloop.Wait(); |
| 152 | if (!m_gpu_mainloop.IsRunning()) |
| 153 | return; |
| 154 | |
| 155 | // Opportunistically reset FIFOs so we don't wrap around. |
| 156 | if (may_move_read_ptr && m_fifo_aux_write_ptr != m_fifo_aux_read_ptr) |
| 157 | { |
| 158 | PanicAlertFmt("Aux FIFO not synced ({}, {})", fmt::ptr(m_fifo_aux_write_ptr), |
| 159 | fmt::ptr(m_fifo_aux_read_ptr)); |
| 160 | } |
| 161 | |
| 162 | memmove(m_fifo_aux_data, m_fifo_aux_read_ptr, m_fifo_aux_write_ptr - m_fifo_aux_read_ptr); |
| 163 | m_fifo_aux_write_ptr -= (m_fifo_aux_read_ptr - m_fifo_aux_data); |
| 164 | m_fifo_aux_read_ptr = m_fifo_aux_data; |
| 165 | |
| 166 | if (may_move_read_ptr) |
| 167 | { |
| 168 | u8* write_ptr = m_video_buffer_write_ptr; |
| 169 | |
| 170 | // what's left over in the buffer |
| 171 | size_t size = write_ptr - m_video_buffer_pp_read_ptr; |
| 172 | |
| 173 | memmove(m_video_buffer, m_video_buffer_pp_read_ptr, size); |
| 174 | // This change always decreases the pointers. We write seen_ptr |
| 175 | // after write_ptr here, and read it before in RunGpuLoop, so |
| 176 | // 'write_ptr > seen_ptr' there cannot become spuriously true. |
| 177 | m_video_buffer_write_ptr = write_ptr = m_video_buffer + size; |
| 178 | m_video_buffer_pp_read_ptr = m_video_buffer; |
| 179 | m_video_buffer_read_ptr = m_video_buffer; |
| 180 | m_video_buffer_seen_ptr = write_ptr; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void FifoManager::PushFifoAuxBuffer(const void* ptr, size_t size) |
| 186 | { |
| 187 | if (size > (size_t)(m_fifo_aux_data + FIFO_SIZE - m_fifo_aux_write_ptr)) |
| 188 | { |
| 189 | SyncGPU(SyncGPUReason::AuxSpace, /* may_move_read_ptr */ false); |
| 190 | if (!m_gpu_mainloop.IsRunning()) |
| 191 | { |
| 192 | // GPU is shutting down |
| 193 | return; |
| 194 | } |
| 195 | if (size > (size_t)(m_fifo_aux_data + FIFO_SIZE - m_fifo_aux_write_ptr)) |
| 196 | { |
| 197 | // That will sync us up to the last 32 bytes, so this short region |
| 198 | // of FIFO would have to point to a 2MB display list or something. |
| 199 | PanicAlertFmt("Absurdly large aux buffer"); |
| 200 | return; |
| 201 | } |
| 202 | } |
| 203 | memcpy(m_fifo_aux_write_ptr, ptr, size); |
| 204 | m_fifo_aux_write_ptr += size; |
| 205 | } |
| 206 | |
| 207 | void* FifoManager::PopFifoAuxBuffer(size_t size) |
| 208 | { |
| 209 | void* ret = m_fifo_aux_read_ptr; |
| 210 | m_fifo_aux_read_ptr += size; |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | // Description: RunGpuLoop() sends data through this function. |
| 215 | void FifoManager::ReadDataFromFifo(u32 read_ptr) |
| 216 | { |
| 217 | if (GPFifo::GATHER_PIPE_SIZE > |
| 218 | static_cast<size_t>(m_video_buffer + FIFO_SIZE - m_video_buffer_write_ptr)) |
| 219 | { |
| 220 | const size_t existing_len = m_video_buffer_write_ptr - m_video_buffer_read_ptr; |
| 221 | if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(FIFO_SIZE - existing_len)) |
| 222 | { |
| 223 | PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len, |
| 224 | GPFifo::GATHER_PIPE_SIZE, FIFO_SIZE); |
| 225 | return; |
| 226 | } |
| 227 | memmove(m_video_buffer, m_video_buffer_read_ptr, existing_len); |
| 228 | m_video_buffer_write_ptr = m_video_buffer + existing_len; |
| 229 | m_video_buffer_read_ptr = m_video_buffer; |
| 230 | } |
| 231 | // Copy new video instructions to m_video_buffer for future use in rendering the new picture |
| 232 | auto& memory = m_system.GetMemory(); |
| 233 | memory.CopyFromEmu(m_video_buffer_write_ptr, read_ptr, GPFifo::GATHER_PIPE_SIZE); |
| 234 | m_video_buffer_write_ptr += GPFifo::GATHER_PIPE_SIZE; |
| 235 | } |
| 236 | |
| 237 | // The deterministic_gpu_thread version. |
| 238 | void FifoManager::ReadDataFromFifoOnCPU(u32 read_ptr) |
| 239 | { |
| 240 | u8* write_ptr = m_video_buffer_write_ptr; |
| 241 | if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(m_video_buffer + FIFO_SIZE - write_ptr)) |
| 242 | { |
| 243 | // We can't wrap around while the GPU is working on the data. |
| 244 | // This should be very rare due to the reset in SyncGPU. |
| 245 | SyncGPU(SyncGPUReason::Wraparound); |
| 246 | if (!m_gpu_mainloop.IsRunning()) |
| 247 | { |
| 248 | // GPU is shutting down, so the next asserts may fail |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | if (m_video_buffer_pp_read_ptr != m_video_buffer_read_ptr) |
| 253 | { |
| 254 | PanicAlertFmt("Desynced read pointers"); |
| 255 | return; |
| 256 | } |
| 257 | write_ptr = m_video_buffer_write_ptr; |
| 258 | const size_t existing_len = write_ptr - m_video_buffer_pp_read_ptr; |
| 259 | if (GPFifo::GATHER_PIPE_SIZE > static_cast<size_t>(FIFO_SIZE - existing_len)) |
| 260 | { |
| 261 | PanicAlertFmt("FIFO out of bounds (existing {} + new {} > {})", existing_len, |
| 262 | GPFifo::GATHER_PIPE_SIZE, FIFO_SIZE); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | auto& memory = m_system.GetMemory(); |
| 267 | memory.CopyFromEmu(m_video_buffer_write_ptr, read_ptr, GPFifo::GATHER_PIPE_SIZE); |
| 268 | m_video_buffer_pp_read_ptr = OpcodeDecoder::RunFifo<true>( |
| 269 | DataReader(m_video_buffer_pp_read_ptr, write_ptr + GPFifo::GATHER_PIPE_SIZE), nullptr); |
| 270 | // This would have to be locked if the GPU thread didn't spin. |
| 271 | m_video_buffer_write_ptr = write_ptr + GPFifo::GATHER_PIPE_SIZE; |
| 272 | } |
| 273 | |
| 274 | void FifoManager::ResetVideoBuffer() |
| 275 | { |
| 276 | m_video_buffer_read_ptr = m_video_buffer; |
| 277 | m_video_buffer_write_ptr = m_video_buffer; |
| 278 | m_video_buffer_seen_ptr = m_video_buffer; |
| 279 | m_video_buffer_pp_read_ptr = m_video_buffer; |
| 280 | m_fifo_aux_write_ptr = m_fifo_aux_data; |
| 281 | m_fifo_aux_read_ptr = m_fifo_aux_data; |
| 282 | } |
| 283 | |
| 284 | // Description: Main FIFO update loop |
| 285 | // Purpose: Keep the Core HW updated about the CPU-GPU distance |
| 286 | void FifoManager::RunGpuLoop() |
| 287 | { |
| 288 | m_gpu_mainloop.Run( |
| 289 | [this] { |
| 290 | // Run events from the CPU thread. |
| 291 | AsyncRequests::GetInstance()->PullEvents(); |
| 292 | |
| 293 | // Do nothing while paused |
| 294 | if (!m_emu_running_state.IsSet()) |
| 295 | return; |
| 296 | |
| 297 | if (m_use_deterministic_gpu_thread) |
| 298 | { |
| 299 | // All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder. |
| 300 | u8* seen_ptr = m_video_buffer_seen_ptr; |
| 301 | u8* write_ptr = m_video_buffer_write_ptr; |
| 302 | // See comment in SyncGPU |
| 303 | if (write_ptr > seen_ptr) |
| 304 | { |
| 305 | m_video_buffer_read_ptr = |
| 306 | OpcodeDecoder::RunFifo(DataReader(m_video_buffer_read_ptr, write_ptr), nullptr); |
| 307 | m_video_buffer_seen_ptr = write_ptr; |
| 308 | } |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | auto& command_processor = m_system.GetCommandProcessor(); |
| 313 | auto& fifo = command_processor.GetFifo(); |
| 314 | command_processor.SetCPStatusFromGPU(); |
| 315 | |
| 316 | // check if we are able to run this buffer |
| 317 | while (!command_processor.IsInterruptWaiting() && |
| 318 | fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) && |
| 319 | fifo.CPReadWriteDistance.load(std::memory_order_relaxed) && |
| 320 | !AtBreakpoint(m_system)) |
| 321 | { |
| 322 | if (m_config_sync_gpu && m_sync_ticks.load() < m_config_sync_gpu_min_distance) |
| 323 | break; |
| 324 | |
| 325 | u32 cyclesExecuted = 0; |
| 326 | u32 readPtr = fifo.CPReadPointer.load(std::memory_order_relaxed); |
| 327 | ReadDataFromFifo(readPtr); |
| 328 | |
| 329 | if (readPtr == fifo.CPEnd.load(std::memory_order_relaxed)) |
| 330 | readPtr = fifo.CPBase.load(std::memory_order_relaxed); |
| 331 | else |
| 332 | readPtr += GPFifo::GATHER_PIPE_SIZE; |
| 333 | |
| 334 | const s32 distance = |
| 335 | static_cast<s32>(fifo.CPReadWriteDistance.load(std::memory_order_relaxed)) - |
| 336 | GPFifo::GATHER_PIPE_SIZE; |
| 337 | ASSERT_MSG(COMMANDPROCESSOR, distance >= 0, |
| 338 | "Negative fifo.CPReadWriteDistance = {} in FIFO Loop !\nThat can produce " |
| 339 | "instability in the game. Please report it.", |
| 340 | distance); |
| 341 | |
| 342 | u8* write_ptr = m_video_buffer_write_ptr; |
| 343 | m_video_buffer_read_ptr = OpcodeDecoder::RunFifo( |
| 344 | DataReader(m_video_buffer_read_ptr, write_ptr), &cyclesExecuted); |
| 345 | |
| 346 | fifo.CPReadPointer.store(readPtr, std::memory_order_relaxed); |
| 347 | fifo.CPReadWriteDistance.fetch_sub(GPFifo::GATHER_PIPE_SIZE, std::memory_order_seq_cst); |
| 348 | if ((write_ptr - m_video_buffer_read_ptr) == 0) |
| 349 | { |
| 350 | fifo.SafeCPReadPointer.store(fifo.CPReadPointer.load(std::memory_order_relaxed), |
| 351 | std::memory_order_relaxed); |
| 352 | } |
| 353 | |
| 354 | command_processor.SetCPStatusFromGPU(); |
| 355 | |
| 356 | if (m_config_sync_gpu) |
| 357 | { |
| 358 | cyclesExecuted = (int)(cyclesExecuted / m_config_sync_gpu_overclock); |
| 359 | int old = m_sync_ticks.fetch_sub(cyclesExecuted); |
| 360 | if (old >= m_config_sync_gpu_max_distance && |
| 361 | old - (int)cyclesExecuted < m_config_sync_gpu_max_distance) |
| 362 | { |
| 363 | m_sync_wakeup_event.Set(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // This call is pretty important in DualCore mode and must be called in the FIFO Loop. |
| 368 | // If we don't, s_swapRequested or s_efbAccessRequested won't be set to false |
| 369 | // leading the CPU thread to wait in Video_OutputXFB or Video_AccessEFB thus slowing |
| 370 | // things down. |
| 371 | AsyncRequests::GetInstance()->PullEvents(); |
| 372 | } |
| 373 | |
| 374 | // fast skip remaining GPU time if fifo is empty |
| 375 | if (m_sync_ticks.load() > 0) |
| 376 | { |
| 377 | int old = m_sync_ticks.exchange(0); |
| 378 | if (old >= m_config_sync_gpu_max_distance) |
| 379 | m_sync_wakeup_event.Set(); |
| 380 | } |
| 381 | |
| 382 | // The fifo is empty and it's unlikely we will get any more work in the near future. |
| 383 | // Make sure VertexManager finishes drawing any primitives it has stored in it's buffer. |
| 384 | g_vertex_manager->Flush(); |
| 385 | g_framebuffer_manager->RefreshPeekCache(); |
| 386 | } |
| 387 | }, |
| 388 | 100); |
| 389 | } |
| 390 | |
| 391 | void FifoManager::FlushGpu() |
| 392 | { |
| 393 | if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread) |
| 394 | return; |
| 395 | |
| 396 | m_gpu_mainloop.Wait(); |
| 397 | } |
| 398 | |
| 399 | void FifoManager::GpuMaySleep() |
| 400 | { |
| 401 | m_gpu_mainloop.AllowSleep(); |
| 402 | } |
| 403 | |
| 404 | bool AtBreakpoint(Core::System& system) |
| 405 | { |
| 406 | auto& command_processor = system.GetCommandProcessor(); |
| 407 | const auto& fifo = command_processor.GetFifo(); |
| 408 | return fifo.bFF_BPEnable.load(std::memory_order_relaxed) && |
| 409 | (fifo.CPReadPointer.load(std::memory_order_relaxed) == |
| 410 | fifo.CPBreakpoint.load(std::memory_order_relaxed)); |
| 411 | } |
| 412 | |
| 413 | void FifoManager::RunGpu() |
| 414 | { |
| 415 | const bool is_dual_core = m_system.IsDualCoreMode(); |
| 416 | |
| 417 | // wake up GPU thread |
| 418 | if (is_dual_core && !m_use_deterministic_gpu_thread) |
| 419 | { |
| 420 | m_gpu_mainloop.Wakeup(); |
| 421 | } |
| 422 | |
| 423 | // if the sync GPU callback is suspended, wake it up. |
| 424 | if (!is_dual_core || m_use_deterministic_gpu_thread || m_config_sync_gpu) |
| 425 | { |
| 426 | if (m_syncing_suspended) |
| 427 | { |
| 428 | m_syncing_suspended = false; |
| 429 | m_system.GetCoreTiming().ScheduleEvent(GPU_TIME_SLOT_SIZE, m_event_sync_gpu, |
| 430 | GPU_TIME_SLOT_SIZE); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | int FifoManager::RunGpuOnCpu(int ticks) |
| 436 | { |
| 437 | auto& command_processor = m_system.GetCommandProcessor(); |
| 438 | auto& fifo = command_processor.GetFifo(); |
| 439 | bool reset_simd_state = false; |
| 440 | int available_ticks = int(ticks * m_config_sync_gpu_overclock) + m_sync_ticks.load(); |
| 441 | while (fifo.bFF_GPReadEnable.load(std::memory_order_relaxed) && |
| 442 | fifo.CPReadWriteDistance.load(std::memory_order_relaxed) && !AtBreakpoint(m_system) && |
| 443 | available_ticks >= 0) |
| 444 | { |
| 445 | if (m_use_deterministic_gpu_thread) |
| 446 | { |
| 447 | ReadDataFromFifoOnCPU(fifo.CPReadPointer.load(std::memory_order_relaxed)); |
| 448 | m_gpu_mainloop.Wakeup(); |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | if (!reset_simd_state) |
| 453 | { |
| 454 | Common::FPU::SaveSIMDState(); |
| 455 | Common::FPU::LoadDefaultSIMDState(); |
| 456 | reset_simd_state = true; |
| 457 | } |
| 458 | ReadDataFromFifo(fifo.CPReadPointer.load(std::memory_order_relaxed)); |
| 459 | u32 cycles = 0; |
| 460 | m_video_buffer_read_ptr = OpcodeDecoder::RunFifo( |
| 461 | DataReader(m_video_buffer_read_ptr, m_video_buffer_write_ptr), &cycles); |
| 462 | available_ticks -= cycles; |
| 463 | } |
| 464 | |
| 465 | if (fifo.CPReadPointer.load(std::memory_order_relaxed) == |
| 466 | fifo.CPEnd.load(std::memory_order_relaxed)) |
| 467 | { |
| 468 | fifo.CPReadPointer.store(fifo.CPBase.load(std::memory_order_relaxed), |
| 469 | std::memory_order_relaxed); |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | fifo.CPReadPointer.fetch_add(GPFifo::GATHER_PIPE_SIZE, std::memory_order_relaxed); |
| 474 | } |
| 475 | |
| 476 | fifo.CPReadWriteDistance.fetch_sub(GPFifo::GATHER_PIPE_SIZE, std::memory_order_relaxed); |
| 477 | } |
| 478 | |
| 479 | command_processor.SetCPStatusFromGPU(); |
| 480 | |
| 481 | if (reset_simd_state) |
| 482 | { |
| 483 | Common::FPU::LoadSIMDState(); |
| 484 | } |
| 485 | |
| 486 | // Discard all available ticks as there is nothing to do any more. |
| 487 | m_sync_ticks.store(std::min(available_ticks, 0)); |
| 488 | |
| 489 | // If the GPU is idle, drop the handler. |
| 490 | if (available_ticks >= 0) |
| 491 | return -1; |
| 492 | |
| 493 | // Always wait at least for GPU_TIME_SLOT_SIZE cycles. |
| 494 | return -available_ticks + GPU_TIME_SLOT_SIZE; |
| 495 | } |
| 496 | |
| 497 | void FifoManager::UpdateWantDeterminism(bool want) |
| 498 | { |
| 499 | // We are paused (or not running at all yet), so |
| 500 | // it should be safe to change this. |
| 501 | bool gpu_thread = false; |
| 502 | switch (Config::GetGPUDeterminismMode()) |
| 503 | { |
| 504 | case Config::GPUDeterminismMode::Auto: |
| 505 | gpu_thread = want; |
| 506 | break; |
| 507 | case Config::GPUDeterminismMode::Disabled: |
| 508 | gpu_thread = false; |
| 509 | break; |
| 510 | case Config::GPUDeterminismMode::FakeCompletion: |
| 511 | gpu_thread = true; |
| 512 | break; |
| 513 | } |
| 514 | |
| 515 | gpu_thread = gpu_thread && m_system.IsDualCoreMode(); |
| 516 | |
| 517 | if (m_use_deterministic_gpu_thread != gpu_thread) |
| 518 | { |
| 519 | m_use_deterministic_gpu_thread = gpu_thread; |
| 520 | if (gpu_thread) |
| 521 | { |
| 522 | // These haven't been updated in non-deterministic mode. |
| 523 | m_video_buffer_seen_ptr = m_video_buffer_pp_read_ptr = m_video_buffer_read_ptr; |
| 524 | CopyPreprocessCPStateFromMain(); |
| 525 | VertexLoaderManager::MarkAllDirty(); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* This function checks the emulated CPU - GPU distance and may wake up the GPU, |
| 531 | * or block the CPU if required. It should be called by the CPU thread regularly. |
| 532 | * @ticks The gone emulated CPU time. |
| 533 | * @return A good time to call WaitForGpuThread() next. |
| 534 | */ |
| 535 | int FifoManager::WaitForGpuThread(int ticks) |
| 536 | { |
| 537 | int old = m_sync_ticks.fetch_add(ticks); |
| 538 | int now = old + ticks; |
| 539 | |
| 540 | // GPU is idle, so stop polling. |
| 541 | if (old >= 0 && m_gpu_mainloop.IsDone()) |
| 542 | return -1; |
| 543 | |
| 544 | // Wakeup GPU |
| 545 | if (old < m_config_sync_gpu_min_distance && now >= m_config_sync_gpu_min_distance) |
| 546 | RunGpu(); |
| 547 | |
| 548 | // If the GPU is still sleeping, wait for a longer time |
| 549 | if (now < m_config_sync_gpu_min_distance) |
| 550 | return GPU_TIME_SLOT_SIZE + m_config_sync_gpu_min_distance - now; |
| 551 | |
| 552 | // Wait for GPU |
| 553 | if (now >= m_config_sync_gpu_max_distance) |
| 554 | m_sync_wakeup_event.Wait(); |
| 555 | |
| 556 | return GPU_TIME_SLOT_SIZE; |
| 557 | } |
| 558 | |
| 559 | void FifoManager::SyncGPUCallback(Core::System& system, u64 ticks, s64 cyclesLate) |
| 560 | { |
| 561 | ticks += cyclesLate; |
| 562 | int next = -1; |
| 563 | |
| 564 | auto& fifo = system.GetFifo(); |
| 565 | if (!system.IsDualCoreMode() || fifo.m_use_deterministic_gpu_thread) |
| 566 | { |
| 567 | next = fifo.RunGpuOnCpu(int(ticks)); |
| 568 | } |
| 569 | else if (fifo.m_config_sync_gpu) |
| 570 | { |
| 571 | next = fifo.WaitForGpuThread(int(ticks)); |
| 572 | } |
| 573 | |
| 574 | fifo.m_syncing_suspended = next < 0; |
| 575 | if (!fifo.m_syncing_suspended) |
| 576 | system.GetCoreTiming().ScheduleEvent(next, fifo.m_event_sync_gpu, next); |
| 577 | } |
| 578 | |
| 579 | void FifoManager::SyncGPUForRegisterAccess() |
| 580 | { |
| 581 | SyncGPU(SyncGPUReason::Other); |
| 582 | |
| 583 | if (!m_system.IsDualCoreMode() || m_use_deterministic_gpu_thread) |
| 584 | RunGpuOnCpu(GPU_TIME_SLOT_SIZE); |
| 585 | else if (m_config_sync_gpu) |
| 586 | WaitForGpuThread(GPU_TIME_SLOT_SIZE); |
| 587 | } |
| 588 | |
| 589 | // Initialize GPU - CPU thread syncing, this gives us a deterministic way to start the GPU thread. |
| 590 | void FifoManager::Prepare() |
| 591 | { |
| 592 | m_event_sync_gpu = m_system.GetCoreTiming().RegisterEvent("SyncGPUCallback", SyncGPUCallback); |
| 593 | m_syncing_suspended = true; |
| 594 | } |
| 595 | } // namespace Fifo |