Fifo.cpp 19 KB

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