2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
#include <atomic>
|
2016-01-17 14:54:31 -07:00
|
|
|
#include <cstddef>
|
2022-12-09 12:01:25 -07:00
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
#include "Common/BlockingLoop.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2023-08-16 13:37:12 -06:00
|
|
|
#include "Common/Config/Config.h"
|
2022-12-09 12:01:25 -07:00
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Flag.h"
|
2009-12-16 10:05:30 -07:00
|
|
|
|
|
|
|
class PointerWrap;
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
namespace Core
|
|
|
|
{
|
|
|
|
class System;
|
|
|
|
}
|
|
|
|
namespace CoreTiming
|
2016-01-12 14:44:58 -07:00
|
|
|
{
|
2022-12-09 12:01:25 -07:00
|
|
|
struct EventType;
|
|
|
|
}
|
2009-02-20 15:04:52 -07:00
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
namespace Fifo
|
|
|
|
{
|
Add the 'desynced GPU thread' mode.
It's a relatively big commit (less big with -w), but it's hard to test
any of this separately...
The basic problem is that in netplay or movies, the state of the CPU
must be deterministic, including when the game receives notification
that the GPU has processed FIFO data. Dual core mode notifies the game
whenever the GPU thread actually gets around to doing the work, so it
isn't deterministic. Single core mode is because it notifies the game
'instantly' (after processing the data synchronously), but it's too slow
for many systems and games.
My old dc-netplay branch worked as follows: everything worked as normal
except the state of the CP registers was a lie, and the CPU thread only
delivered results when idle detection triggered (waiting for the GPU if
they weren't ready at that point). Usually, a game is idle iff all the
work for the frame has been done, except for a small amount of work
depending on the GPU result, so neither the CPU or the GPU waiting on
the other affected performance much. However, it's possible that the
game could be waiting for some earlier interrupt, and any of several
games which, for whatever reason, never went into a detectable idle
(even when I tried to improve the detection) would never receive results
at all. (The current method should have better compatibility, but it
also has slightly higher overhead and breaks some other things, so I
want to reimplement this, hopefully with less impact on the code, in the
future.)
With this commit, the basic idea is that the CPU thread acts as if the
work has been done instantly, like single core mode, but actually hands
it off asynchronously to the GPU thread (after backing up some data that
the game might change in memory before it's actually done). Since the
work isn't done, any feedback from the GPU to the CPU, such as real
XFB/EFB copies (virtual are OK), EFB pokes, performance queries, etc. is
broken; but most games work with these options disabled, and there is no
need to try to detect what the CPU thread is doing.
Technically: when the flag g_use_deterministic_gpu_thread (currently
stuck on) is on, the CPU thread calls RunGpu like in single core mode.
This function synchronously copies the data from the FIFO to the
internal video buffer and updates the CP registers, interrupts, etc.
However, instead of the regular ReadDataFromFifo followed by running the
opcode decoder, it runs ReadDataFromFifoOnCPU ->
OpcodeDecoder_Preprocess, which relatively quickly scans through the
FIFO data, detects SetFinish calls etc., which are immediately fired,
and saves certain associated data from memory (e.g. display lists) in
AuxBuffers (a parallel stream to the main FIFO, which is a bit slow at
the moment), before handing the data off to the GPU thread to actually
render. That makes up the bulk of this commit.
In various circumstances, including the aforementioned EFB pokes and
performance queries as well as swap requests (i.e. the end of a frame -
we don't want the CPU potentially pumping out frames too quickly and the
GPU falling behind*), SyncGPU is called to wait for actual completion.
The overhead mainly comes from OpcodeDecoder_Preprocess (which is,
again, synchronous), as well as the actual copying.
Currently, display lists and such are escrowed from main memory even
though they usually won't change over the course of a frame, and
textures are not even though they might, resulting in a small chance of
graphical glitches. When the texture locking (i.e. fault on write) code
lands, I can make this all correct and maybe a little faster.
* This suggests an alternate determinism method of just delaying results
until a short time before the end of each frame. For all I know this
might mostly work - I haven't tried it - but if any significant work
hinges on the competion of render to texture etc., the frame will be
missed.
2014-08-27 20:56:19 -06:00
|
|
|
// Used for diagnostics.
|
2016-08-18 20:35:58 -06:00
|
|
|
enum class SyncGPUReason
|
Add the 'desynced GPU thread' mode.
It's a relatively big commit (less big with -w), but it's hard to test
any of this separately...
The basic problem is that in netplay or movies, the state of the CPU
must be deterministic, including when the game receives notification
that the GPU has processed FIFO data. Dual core mode notifies the game
whenever the GPU thread actually gets around to doing the work, so it
isn't deterministic. Single core mode is because it notifies the game
'instantly' (after processing the data synchronously), but it's too slow
for many systems and games.
My old dc-netplay branch worked as follows: everything worked as normal
except the state of the CP registers was a lie, and the CPU thread only
delivered results when idle detection triggered (waiting for the GPU if
they weren't ready at that point). Usually, a game is idle iff all the
work for the frame has been done, except for a small amount of work
depending on the GPU result, so neither the CPU or the GPU waiting on
the other affected performance much. However, it's possible that the
game could be waiting for some earlier interrupt, and any of several
games which, for whatever reason, never went into a detectable idle
(even when I tried to improve the detection) would never receive results
at all. (The current method should have better compatibility, but it
also has slightly higher overhead and breaks some other things, so I
want to reimplement this, hopefully with less impact on the code, in the
future.)
With this commit, the basic idea is that the CPU thread acts as if the
work has been done instantly, like single core mode, but actually hands
it off asynchronously to the GPU thread (after backing up some data that
the game might change in memory before it's actually done). Since the
work isn't done, any feedback from the GPU to the CPU, such as real
XFB/EFB copies (virtual are OK), EFB pokes, performance queries, etc. is
broken; but most games work with these options disabled, and there is no
need to try to detect what the CPU thread is doing.
Technically: when the flag g_use_deterministic_gpu_thread (currently
stuck on) is on, the CPU thread calls RunGpu like in single core mode.
This function synchronously copies the data from the FIFO to the
internal video buffer and updates the CP registers, interrupts, etc.
However, instead of the regular ReadDataFromFifo followed by running the
opcode decoder, it runs ReadDataFromFifoOnCPU ->
OpcodeDecoder_Preprocess, which relatively quickly scans through the
FIFO data, detects SetFinish calls etc., which are immediately fired,
and saves certain associated data from memory (e.g. display lists) in
AuxBuffers (a parallel stream to the main FIFO, which is a bit slow at
the moment), before handing the data off to the GPU thread to actually
render. That makes up the bulk of this commit.
In various circumstances, including the aforementioned EFB pokes and
performance queries as well as swap requests (i.e. the end of a frame -
we don't want the CPU potentially pumping out frames too quickly and the
GPU falling behind*), SyncGPU is called to wait for actual completion.
The overhead mainly comes from OpcodeDecoder_Preprocess (which is,
again, synchronous), as well as the actual copying.
Currently, display lists and such are escrowed from main memory even
though they usually won't change over the course of a frame, and
textures are not even though they might, resulting in a small chance of
graphical glitches. When the texture locking (i.e. fault on write) code
lands, I can make this all correct and maybe a little faster.
* This suggests an alternate determinism method of just delaying results
until a short time before the end of each frame. For all I know this
might mostly work - I haven't tried it - but if any significant work
hinges on the competion of render to texture etc., the frame will be
missed.
2014-08-27 20:56:19 -06:00
|
|
|
{
|
2016-08-18 20:35:58 -06:00
|
|
|
Other,
|
|
|
|
Wraparound,
|
|
|
|
EFBPoke,
|
|
|
|
PerfQuery,
|
|
|
|
BBox,
|
|
|
|
Swap,
|
|
|
|
AuxSpace,
|
Add the 'desynced GPU thread' mode.
It's a relatively big commit (less big with -w), but it's hard to test
any of this separately...
The basic problem is that in netplay or movies, the state of the CPU
must be deterministic, including when the game receives notification
that the GPU has processed FIFO data. Dual core mode notifies the game
whenever the GPU thread actually gets around to doing the work, so it
isn't deterministic. Single core mode is because it notifies the game
'instantly' (after processing the data synchronously), but it's too slow
for many systems and games.
My old dc-netplay branch worked as follows: everything worked as normal
except the state of the CP registers was a lie, and the CPU thread only
delivered results when idle detection triggered (waiting for the GPU if
they weren't ready at that point). Usually, a game is idle iff all the
work for the frame has been done, except for a small amount of work
depending on the GPU result, so neither the CPU or the GPU waiting on
the other affected performance much. However, it's possible that the
game could be waiting for some earlier interrupt, and any of several
games which, for whatever reason, never went into a detectable idle
(even when I tried to improve the detection) would never receive results
at all. (The current method should have better compatibility, but it
also has slightly higher overhead and breaks some other things, so I
want to reimplement this, hopefully with less impact on the code, in the
future.)
With this commit, the basic idea is that the CPU thread acts as if the
work has been done instantly, like single core mode, but actually hands
it off asynchronously to the GPU thread (after backing up some data that
the game might change in memory before it's actually done). Since the
work isn't done, any feedback from the GPU to the CPU, such as real
XFB/EFB copies (virtual are OK), EFB pokes, performance queries, etc. is
broken; but most games work with these options disabled, and there is no
need to try to detect what the CPU thread is doing.
Technically: when the flag g_use_deterministic_gpu_thread (currently
stuck on) is on, the CPU thread calls RunGpu like in single core mode.
This function synchronously copies the data from the FIFO to the
internal video buffer and updates the CP registers, interrupts, etc.
However, instead of the regular ReadDataFromFifo followed by running the
opcode decoder, it runs ReadDataFromFifoOnCPU ->
OpcodeDecoder_Preprocess, which relatively quickly scans through the
FIFO data, detects SetFinish calls etc., which are immediately fired,
and saves certain associated data from memory (e.g. display lists) in
AuxBuffers (a parallel stream to the main FIFO, which is a bit slow at
the moment), before handing the data off to the GPU thread to actually
render. That makes up the bulk of this commit.
In various circumstances, including the aforementioned EFB pokes and
performance queries as well as swap requests (i.e. the end of a frame -
we don't want the CPU potentially pumping out frames too quickly and the
GPU falling behind*), SyncGPU is called to wait for actual completion.
The overhead mainly comes from OpcodeDecoder_Preprocess (which is,
again, synchronous), as well as the actual copying.
Currently, display lists and such are escrowed from main memory even
though they usually won't change over the course of a frame, and
textures are not even though they might, resulting in a small chance of
graphical glitches. When the texture locking (i.e. fault on write) code
lands, I can make this all correct and maybe a little faster.
* This suggests an alternate determinism method of just delaying results
until a short time before the end of each frame. For all I know this
might mostly work - I haven't tried it - but if any significant work
hinges on the competion of render to texture etc., the frame will be
missed.
2014-08-27 20:56:19 -06:00
|
|
|
};
|
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
class FifoManager final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FifoManager();
|
|
|
|
FifoManager(const FifoManager& other) = delete;
|
|
|
|
FifoManager(FifoManager&& other) = delete;
|
|
|
|
FifoManager& operator=(const FifoManager& other) = delete;
|
|
|
|
FifoManager& operator=(FifoManager&& other) = delete;
|
|
|
|
~FifoManager();
|
|
|
|
|
2022-12-09 14:59:11 -07:00
|
|
|
void Init(Core::System& system);
|
2022-12-09 12:01:25 -07:00
|
|
|
void Shutdown();
|
2022-12-09 14:59:11 -07:00
|
|
|
void Prepare(Core::System& system); // Must be called from the CPU thread.
|
2022-12-09 12:01:25 -07:00
|
|
|
void DoState(PointerWrap& f);
|
2022-12-09 14:59:11 -07:00
|
|
|
void PauseAndLock(Core::System& system, bool doLock, bool unpauseOnUnlock);
|
|
|
|
void UpdateWantDeterminism(Core::System& system, bool want);
|
2022-12-09 12:01:25 -07:00
|
|
|
bool UseDeterministicGPUThread() const { return m_use_deterministic_gpu_thread; }
|
2023-02-19 08:30:06 -07:00
|
|
|
bool UseSyncGPU() const { return m_config_sync_gpu; }
|
2022-12-09 12:01:25 -07:00
|
|
|
|
|
|
|
// In deterministic GPU thread mode this waits for the GPU to be done with pending work.
|
|
|
|
void SyncGPU(SyncGPUReason reason, bool may_move_read_ptr = true);
|
|
|
|
|
|
|
|
// In single core mode, this runs the GPU for a single slice.
|
|
|
|
// In dual core mode, this synchronizes with the GPU thread.
|
2022-12-09 14:59:11 -07:00
|
|
|
void SyncGPUForRegisterAccess(Core::System& system);
|
2022-12-09 12:01:25 -07:00
|
|
|
|
|
|
|
void PushFifoAuxBuffer(const void* ptr, size_t size);
|
|
|
|
void* PopFifoAuxBuffer(size_t size);
|
2021-04-16 23:54:50 -06:00
|
|
|
|
2022-12-09 14:59:11 -07:00
|
|
|
void FlushGpu(Core::System& system);
|
|
|
|
void RunGpu(Core::System& system);
|
2022-12-09 12:01:25 -07:00
|
|
|
void GpuMaySleep();
|
2022-12-09 14:59:11 -07:00
|
|
|
void RunGpuLoop(Core::System& system);
|
|
|
|
void ExitGpuLoop(Core::System& system);
|
2022-12-09 12:01:25 -07:00
|
|
|
void EmulatorState(bool running);
|
|
|
|
void ResetVideoBuffer();
|
Add the 'desynced GPU thread' mode.
It's a relatively big commit (less big with -w), but it's hard to test
any of this separately...
The basic problem is that in netplay or movies, the state of the CPU
must be deterministic, including when the game receives notification
that the GPU has processed FIFO data. Dual core mode notifies the game
whenever the GPU thread actually gets around to doing the work, so it
isn't deterministic. Single core mode is because it notifies the game
'instantly' (after processing the data synchronously), but it's too slow
for many systems and games.
My old dc-netplay branch worked as follows: everything worked as normal
except the state of the CP registers was a lie, and the CPU thread only
delivered results when idle detection triggered (waiting for the GPU if
they weren't ready at that point). Usually, a game is idle iff all the
work for the frame has been done, except for a small amount of work
depending on the GPU result, so neither the CPU or the GPU waiting on
the other affected performance much. However, it's possible that the
game could be waiting for some earlier interrupt, and any of several
games which, for whatever reason, never went into a detectable idle
(even when I tried to improve the detection) would never receive results
at all. (The current method should have better compatibility, but it
also has slightly higher overhead and breaks some other things, so I
want to reimplement this, hopefully with less impact on the code, in the
future.)
With this commit, the basic idea is that the CPU thread acts as if the
work has been done instantly, like single core mode, but actually hands
it off asynchronously to the GPU thread (after backing up some data that
the game might change in memory before it's actually done). Since the
work isn't done, any feedback from the GPU to the CPU, such as real
XFB/EFB copies (virtual are OK), EFB pokes, performance queries, etc. is
broken; but most games work with these options disabled, and there is no
need to try to detect what the CPU thread is doing.
Technically: when the flag g_use_deterministic_gpu_thread (currently
stuck on) is on, the CPU thread calls RunGpu like in single core mode.
This function synchronously copies the data from the FIFO to the
internal video buffer and updates the CP registers, interrupts, etc.
However, instead of the regular ReadDataFromFifo followed by running the
opcode decoder, it runs ReadDataFromFifoOnCPU ->
OpcodeDecoder_Preprocess, which relatively quickly scans through the
FIFO data, detects SetFinish calls etc., which are immediately fired,
and saves certain associated data from memory (e.g. display lists) in
AuxBuffers (a parallel stream to the main FIFO, which is a bit slow at
the moment), before handing the data off to the GPU thread to actually
render. That makes up the bulk of this commit.
In various circumstances, including the aforementioned EFB pokes and
performance queries as well as swap requests (i.e. the end of a frame -
we don't want the CPU potentially pumping out frames too quickly and the
GPU falling behind*), SyncGPU is called to wait for actual completion.
The overhead mainly comes from OpcodeDecoder_Preprocess (which is,
again, synchronous), as well as the actual copying.
Currently, display lists and such are escrowed from main memory even
though they usually won't change over the course of a frame, and
textures are not even though they might, resulting in a small chance of
graphical glitches. When the texture locking (i.e. fault on write) code
lands, I can make this all correct and maybe a little faster.
* This suggests an alternate determinism method of just delaying results
until a short time before the end of each frame. For all I know this
might mostly work - I haven't tried it - but if any significant work
hinges on the competion of render to texture etc., the frame will be
missed.
2014-08-27 20:56:19 -06:00
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
private:
|
|
|
|
void RefreshConfig();
|
2022-12-09 14:59:11 -07:00
|
|
|
void ReadDataFromFifo(Core::System& system, u32 readPtr);
|
|
|
|
void ReadDataFromFifoOnCPU(Core::System& system, u32 readPtr);
|
|
|
|
int RunGpuOnCpu(Core::System& system, int ticks);
|
|
|
|
int WaitForGpuThread(Core::System& system, int ticks);
|
2022-12-09 12:01:25 -07:00
|
|
|
static void SyncGPUCallback(Core::System& system, u64 ticks, s64 cyclesLate);
|
2016-01-12 14:44:58 -07:00
|
|
|
|
2022-12-09 12:01:25 -07:00
|
|
|
static constexpr u32 FIFO_SIZE = 2 * 1024 * 1024;
|
|
|
|
|
|
|
|
Common::BlockingLoop m_gpu_mainloop;
|
|
|
|
|
|
|
|
Common::Flag m_emu_running_state;
|
|
|
|
|
|
|
|
// Most of this array is unlikely to be faulted in...
|
|
|
|
u8 m_fifo_aux_data[FIFO_SIZE]{};
|
|
|
|
u8* m_fifo_aux_write_ptr = nullptr;
|
|
|
|
u8* m_fifo_aux_read_ptr = nullptr;
|
|
|
|
|
|
|
|
// This could be in SConfig, but it depends on multiple settings
|
|
|
|
// and can change at runtime.
|
|
|
|
bool m_use_deterministic_gpu_thread = false;
|
|
|
|
|
|
|
|
CoreTiming::EventType* m_event_sync_gpu = nullptr;
|
|
|
|
|
|
|
|
// STATE_TO_SAVE
|
|
|
|
u8* m_video_buffer = nullptr;
|
|
|
|
u8* m_video_buffer_read_ptr = nullptr;
|
|
|
|
std::atomic<u8*> m_video_buffer_write_ptr = nullptr;
|
|
|
|
std::atomic<u8*> m_video_buffer_seen_ptr = nullptr;
|
|
|
|
u8* m_video_buffer_pp_read_ptr = nullptr;
|
|
|
|
// 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,
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
std::atomic<int> m_sync_ticks = 0;
|
|
|
|
bool m_syncing_suspended = false;
|
|
|
|
Common::Event m_sync_wakeup_event;
|
|
|
|
|
2023-08-16 13:37:12 -06:00
|
|
|
std::optional<Config::ConfigChangedCallbackID> m_config_callback_id = std::nullopt;
|
2022-12-09 12:01:25 -07:00
|
|
|
bool m_config_sync_gpu = false;
|
|
|
|
int m_config_sync_gpu_max_distance = 0;
|
|
|
|
int m_config_sync_gpu_min_distance = 0;
|
|
|
|
float m_config_sync_gpu_overclock = 0.0f;
|
|
|
|
};
|
2022-12-09 14:59:11 -07:00
|
|
|
|
|
|
|
bool AtBreakpoint(Core::System& system);
|
2016-01-23 21:20:13 -07:00
|
|
|
} // namespace Fifo
|