2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2017-09-02 17:23:26 +00:00
|
|
|
#include "VideoCommon/Fifo.h"
|
|
|
|
|
2015-05-27 04:42:56 +00:00
|
|
|
#include <atomic>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <cstring>
|
2015-05-27 04:42:56 +00:00
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/Atomic.h"
|
2015-05-27 18:53:09 +00:00
|
|
|
#include "Common/BlockingLoop.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
2015-03-05 16:12:24 +00:00
|
|
|
#include "Common/Event.h"
|
2014-02-20 03:11:52 +00:00
|
|
|
#include "Common/FPURoundMode.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/MemoryUtil.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2014-09-09 04:24:49 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2016-01-18 23:08:18 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/Memmap.h"
|
2016-11-10 16:55:21 +00:00
|
|
|
#include "Core/Host.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2015-01-31 10:38:23 +00:00
|
|
|
#include "VideoCommon/AsyncRequests.h"
|
2014-09-06 21:26:40 +00:00
|
|
|
#include "VideoCommon/CPMemory.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "VideoCommon/CommandProcessor.h"
|
2014-07-08 14:49:33 +00:00
|
|
|
#include "VideoCommon/DataReader.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/OpcodeDecoding.h"
|
2014-09-06 21:26:40 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2015-05-27 18:53:09 +00:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2017-09-02 17:23:26 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2016-01-12 21:44:58 +00:00
|
|
|
namespace Fifo
|
|
|
|
{
|
2015-12-25 21:07:00 +00:00
|
|
|
static constexpr u32 FIFO_SIZE = 2 * 1024 * 1024;
|
2016-01-10 11:18:45 +00:00
|
|
|
static constexpr int GPU_TIME_SLOT_SIZE = 1000;
|
2015-12-25 21:07:00 +00:00
|
|
|
|
2015-05-27 18:53:09 +00:00
|
|
|
static Common::BlockingLoop s_gpu_mainloop;
|
|
|
|
|
2016-08-05 14:04:39 +00:00
|
|
|
static Common::Flag s_emu_running_state;
|
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-28 02:56:19 +00: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;
|
|
|
|
|
2016-01-24 04:31:13 +00:00
|
|
|
// This could be in SConfig, but it depends on multiple settings
|
|
|
|
// and can change at runtime.
|
|
|
|
static bool s_use_deterministic_gpu_thread;
|
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-28 02:56:19 +00:00
|
|
|
|
2016-09-01 10:54:18 +00:00
|
|
|
static CoreTiming::EventType* s_event_sync_gpu;
|
2016-01-18 23:08:18 +00:00
|
|
|
|
2009-02-20 22:04:52 +00:00
|
|
|
// STATE_TO_SAVE
|
2014-08-26 17:37:32 +00:00
|
|
|
static u8* s_video_buffer;
|
2014-11-26 21:12:54 +00:00
|
|
|
static u8* s_video_buffer_read_ptr;
|
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-28 02:56:19 +00:00
|
|
|
static std::atomic<u8*> s_video_buffer_write_ptr;
|
|
|
|
static std::atomic<u8*> s_video_buffer_seen_ptr;
|
2014-11-26 21:12:54 +00:00
|
|
|
static u8* s_video_buffer_pp_read_ptr;
|
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-28 02:56:19 +00:00
|
|
|
// The read_ptr is always owned by the GPU thread. In normal mode, so is the
|
2016-01-24 04:31:13 +00:00
|
|
|
// write_ptr, despite it being atomic. In deterministic GPU thread mode,
|
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-28 02:56:19 +00: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.
|
2009-02-20 22:04:52 +00:00
|
|
|
|
2015-06-03 21:21:46 +00:00
|
|
|
static std::atomic<int> s_sync_ticks;
|
2016-01-10 11:18:45 +00:00
|
|
|
static bool s_syncing_suspended;
|
2015-06-03 21:21:46 +00:00
|
|
|
static Common::Event s_sync_wakeup_event;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void DoState(PointerWrap& p)
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
p.DoArray(s_video_buffer, FIFO_SIZE);
|
|
|
|
u8* write_ptr = s_video_buffer_write_ptr;
|
|
|
|
p.DoPointer(write_ptr, s_video_buffer);
|
|
|
|
s_video_buffer_write_ptr = write_ptr;
|
|
|
|
p.DoPointer(s_video_buffer_read_ptr, s_video_buffer);
|
|
|
|
if (p.mode == PointerWrap::MODE_READ && s_use_deterministic_gpu_thread)
|
|
|
|
{
|
|
|
|
// We're good and paused, right?
|
|
|
|
s_video_buffer_seen_ptr = s_video_buffer_pp_read_ptr = s_video_buffer_read_ptr;
|
|
|
|
}
|
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
p.Do(s_sync_ticks);
|
2016-10-11 16:27:46 +00:00
|
|
|
p.Do(s_syncing_suspended);
|
2011-12-31 04:16:12 +00:00
|
|
|
}
|
2009-07-03 10:00:09 +00:00
|
|
|
|
2016-01-12 21:44:58 +00:00
|
|
|
void PauseAndLock(bool doLock, bool unpauseOnUnlock)
|
2011-12-31 04:16:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (doLock)
|
|
|
|
{
|
2016-08-19 02:35:58 +00:00
|
|
|
SyncGPU(SyncGPUReason::Other);
|
2016-06-24 08:43:46 +00:00
|
|
|
EmulatorState(false);
|
2016-11-10 16:55:21 +00:00
|
|
|
|
|
|
|
const SConfig& param = SConfig::GetInstance();
|
|
|
|
|
|
|
|
if (!param.bCPUThread || s_use_deterministic_gpu_thread)
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_gpu_mainloop.WaitYield(std::chrono::milliseconds(100), Host_YieldToUI);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unpauseOnUnlock)
|
|
|
|
EmulatorState(true);
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 21:44:58 +00:00
|
|
|
void Init()
|
2013-03-20 01:51:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// Padded so that SIMD overreads in the vertex loader are safe
|
2016-08-07 17:03:07 +00:00
|
|
|
s_video_buffer = static_cast<u8*>(Common::AllocateMemoryPages(FIFO_SIZE + 4));
|
2016-06-24 08:43:46 +00:00
|
|
|
ResetVideoBuffer();
|
|
|
|
if (SConfig::GetInstance().bCPUThread)
|
|
|
|
s_gpu_mainloop.Prepare();
|
|
|
|
s_sync_ticks.store(0);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 21:44:58 +00:00
|
|
|
void Shutdown()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_gpu_mainloop.IsRunning())
|
|
|
|
PanicAlert("Fifo shutting down while active");
|
|
|
|
|
2016-08-07 17:03:07 +00:00
|
|
|
Common::FreeMemoryPages(s_video_buffer, FIFO_SIZE + 4);
|
2016-06-24 08:43:46 +00:00
|
|
|
s_video_buffer = nullptr;
|
|
|
|
s_video_buffer_write_ptr = nullptr;
|
|
|
|
s_video_buffer_pp_read_ptr = nullptr;
|
|
|
|
s_video_buffer_read_ptr = nullptr;
|
|
|
|
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.
|
Big Fifo Commit Part2: Now the fifo is more stable than my first commit, so is time...
- ReImplementing Single Core Mode like Dual Core Mode Style.
- Stage 1: My goal is, we have the Fifo, CommandProccessor code the more clear, maintenible and documented possible. When I quit dolphin I want any developer can continue with the work only reading the code.
* Big Refactoring: A lot of functions was changed the names, and modularized.
Now the FifoLoop and CatchUpGPU does not exist, was replaced by RunGpu() and RunGpuLoop().
The general idea is modeling the code like the real HW. The fifo is only a buffer where the Write Gather Pipe write the commands and from the Graphic Processor read these.
* Big Clean UP a lot of obsolete code and comments was deleted, like DcFakeWachDog, "Fifo very soon hack", etc.
In the stage 2, I will refactoring more code doing emphasis in the division of CommandProcessor, Fifo, Gpu Emulation. Beside I will comment all functions and variables in the code (Don't worry I will ask for English help for this part ;) )
Please test a lot SC mode and DC mode :)
Thank you so much for testing always and the patience. I don't like broke your favorite game but... you must believe me this part is very sensible, I only try to contribute for have a better and stable dolphin emulator.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7185 8ced0084-cf51-0410-be5f-012b33b47a6e
2011-02-17 04:25:21 +00:00
|
|
|
void ExitGpuLoop()
|
2009-08-09 11:03:58 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// This should break the wait loop in CPU thread
|
|
|
|
CommandProcessor::fifo.bFF_GPReadEnable = false;
|
|
|
|
FlushGpu();
|
2015-03-05 20:14:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Terminate GPU thread loop
|
2016-08-05 14:04:39 +00:00
|
|
|
s_emu_running_state.Set();
|
2017-06-22 12:42:14 +00:00
|
|
|
s_gpu_mainloop.Stop(s_gpu_mainloop.kNonBlock);
|
2010-01-03 16:04:40 +00:00
|
|
|
}
|
|
|
|
|
Big Fifo Commit Part2: Now the fifo is more stable than my first commit, so is time...
- ReImplementing Single Core Mode like Dual Core Mode Style.
- Stage 1: My goal is, we have the Fifo, CommandProccessor code the more clear, maintenible and documented possible. When I quit dolphin I want any developer can continue with the work only reading the code.
* Big Refactoring: A lot of functions was changed the names, and modularized.
Now the FifoLoop and CatchUpGPU does not exist, was replaced by RunGpu() and RunGpuLoop().
The general idea is modeling the code like the real HW. The fifo is only a buffer where the Write Gather Pipe write the commands and from the Graphic Processor read these.
* Big Clean UP a lot of obsolete code and comments was deleted, like DcFakeWachDog, "Fifo very soon hack", etc.
In the stage 2, I will refactoring more code doing emphasis in the division of CommandProcessor, Fifo, Gpu Emulation. Beside I will comment all functions and variables in the code (Don't worry I will ask for English help for this part ;) )
Please test a lot SC mode and DC mode :)
Thank you so much for testing always and the patience. I don't like broke your favorite game but... you must believe me this part is very sensible, I only try to contribute for have a better and stable dolphin emulator.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7185 8ced0084-cf51-0410-be5f-012b33b47a6e
2011-02-17 04:25:21 +00:00
|
|
|
void EmulatorState(bool running)
|
2010-01-03 16:04:40 +00:00
|
|
|
{
|
2016-08-05 14:04:39 +00:00
|
|
|
s_emu_running_state.Set(running);
|
2016-07-15 00:20:04 +00:00
|
|
|
if (running)
|
|
|
|
s_gpu_mainloop.Wakeup();
|
|
|
|
else
|
|
|
|
s_gpu_mainloop.AllowSleep();
|
2009-03-07 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
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-28 02:56:19 +00:00
|
|
|
void SyncGPU(SyncGPUReason reason, bool may_move_read_ptr)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (s_use_deterministic_gpu_thread)
|
|
|
|
{
|
|
|
|
s_gpu_mainloop.Wait();
|
|
|
|
if (!s_gpu_mainloop.IsRunning())
|
|
|
|
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)
|
|
|
|
PanicAlert("aux fifo not synced (%p, %p)", s_fifo_aux_write_ptr, s_fifo_aux_read_ptr);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
u8* write_ptr = s_video_buffer_write_ptr;
|
|
|
|
|
|
|
|
// what's left over in the buffer
|
|
|
|
size_t size = write_ptr - s_video_buffer_pp_read_ptr;
|
|
|
|
|
|
|
|
memmove(s_video_buffer, s_video_buffer_pp_read_ptr, size);
|
|
|
|
// 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;
|
|
|
|
s_video_buffer_pp_read_ptr = s_video_buffer;
|
|
|
|
s_video_buffer_read_ptr = s_video_buffer;
|
|
|
|
s_video_buffer_seen_ptr = write_ptr;
|
|
|
|
}
|
|
|
|
}
|
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-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 00:38:19 +00:00
|
|
|
void PushFifoAuxBuffer(const void* ptr, size_t size)
|
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-28 02:56:19 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (size > (size_t)(s_fifo_aux_data + FIFO_SIZE - s_fifo_aux_write_ptr))
|
|
|
|
{
|
2016-08-19 02:35:58 +00:00
|
|
|
SyncGPU(SyncGPUReason::AuxSpace, /* may_move_read_ptr */ false);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!s_gpu_mainloop.IsRunning())
|
|
|
|
{
|
|
|
|
// GPU is shutting down
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
PanicAlert("absurdly large aux buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memcpy(s_fifo_aux_write_ptr, ptr, size);
|
|
|
|
s_fifo_aux_write_ptr += size;
|
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-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void* PopFifoAuxBuffer(size_t size)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
void* ret = s_fifo_aux_read_ptr;
|
|
|
|
s_fifo_aux_read_ptr += size;
|
|
|
|
return ret;
|
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-28 02:56:19 +00:00
|
|
|
}
|
2011-02-10 04:47:02 +00:00
|
|
|
|
Big Fifo Commit Part2: Now the fifo is more stable than my first commit, so is time...
- ReImplementing Single Core Mode like Dual Core Mode Style.
- Stage 1: My goal is, we have the Fifo, CommandProccessor code the more clear, maintenible and documented possible. When I quit dolphin I want any developer can continue with the work only reading the code.
* Big Refactoring: A lot of functions was changed the names, and modularized.
Now the FifoLoop and CatchUpGPU does not exist, was replaced by RunGpu() and RunGpuLoop().
The general idea is modeling the code like the real HW. The fifo is only a buffer where the Write Gather Pipe write the commands and from the Graphic Processor read these.
* Big Clean UP a lot of obsolete code and comments was deleted, like DcFakeWachDog, "Fifo very soon hack", etc.
In the stage 2, I will refactoring more code doing emphasis in the division of CommandProcessor, Fifo, Gpu Emulation. Beside I will comment all functions and variables in the code (Don't worry I will ask for English help for this part ;) )
Please test a lot SC mode and DC mode :)
Thank you so much for testing always and the patience. I don't like broke your favorite game but... you must believe me this part is very sensible, I only try to contribute for have a better and stable dolphin emulator.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7185 8ced0084-cf51-0410-be5f-012b33b47a6e
2011-02-17 04:25:21 +00:00
|
|
|
// Description: RunGpuLoop() sends data through this function.
|
2014-11-01 11:24:43 +00:00
|
|
|
static void ReadDataFromFifo(u32 readPtr)
|
2009-06-15 04:30:02 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
size_t len = 32;
|
|
|
|
if (len > (size_t)(s_video_buffer + FIFO_SIZE - s_video_buffer_write_ptr))
|
|
|
|
{
|
|
|
|
size_t existing_len = s_video_buffer_write_ptr - s_video_buffer_read_ptr;
|
|
|
|
if (len > (size_t)(FIFO_SIZE - existing_len))
|
|
|
|
{
|
2017-03-28 14:35:19 +00:00
|
|
|
PanicAlert("FIFO out of bounds (existing %zu + new %zu > %u)", existing_len, len, FIFO_SIZE);
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memmove(s_video_buffer, s_video_buffer_read_ptr, existing_len);
|
|
|
|
s_video_buffer_write_ptr = s_video_buffer + existing_len;
|
|
|
|
s_video_buffer_read_ptr = s_video_buffer;
|
|
|
|
}
|
|
|
|
// Copy new video instructions to s_video_buffer for future use in rendering the new picture
|
|
|
|
Memory::CopyFromEmu(s_video_buffer_write_ptr, readPtr, len);
|
|
|
|
s_video_buffer_write_ptr += len;
|
2009-06-15 04:30:02 +00:00
|
|
|
}
|
|
|
|
|
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-28 02:56:19 +00:00
|
|
|
// The deterministic_gpu_thread version.
|
2014-11-01 11:24:43 +00:00
|
|
|
static void ReadDataFromFifoOnCPU(u32 readPtr)
|
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-28 02:56:19 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
size_t len = 32;
|
|
|
|
u8* write_ptr = s_video_buffer_write_ptr;
|
|
|
|
if (len > (size_t)(s_video_buffer + FIFO_SIZE - write_ptr))
|
|
|
|
{
|
|
|
|
// 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-19 02:35:58 +00:00
|
|
|
SyncGPU(SyncGPUReason::Wraparound);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!s_gpu_mainloop.IsRunning())
|
|
|
|
{
|
|
|
|
// GPU is shutting down, so the next asserts may fail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_video_buffer_pp_read_ptr != s_video_buffer_read_ptr)
|
|
|
|
{
|
|
|
|
PanicAlert("desynced read pointers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
write_ptr = s_video_buffer_write_ptr;
|
|
|
|
size_t existing_len = write_ptr - s_video_buffer_pp_read_ptr;
|
|
|
|
if (len > (size_t)(FIFO_SIZE - existing_len))
|
|
|
|
{
|
2017-03-28 14:35:19 +00:00
|
|
|
PanicAlert("FIFO out of bounds (existing %zu + new %zu > %u)", existing_len, len, FIFO_SIZE);
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Memory::CopyFromEmu(s_video_buffer_write_ptr, readPtr, len);
|
|
|
|
s_video_buffer_pp_read_ptr = OpcodeDecoder::Run<true>(
|
|
|
|
DataReader(s_video_buffer_pp_read_ptr, write_ptr + len), nullptr, false);
|
|
|
|
// This would have to be locked if the GPU thread didn't spin.
|
|
|
|
s_video_buffer_write_ptr = write_ptr + len;
|
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-28 02:56:19 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 21:26:46 +00:00
|
|
|
void ResetVideoBuffer()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_video_buffer_read_ptr = s_video_buffer;
|
|
|
|
s_video_buffer_write_ptr = s_video_buffer;
|
|
|
|
s_video_buffer_seen_ptr = s_video_buffer;
|
|
|
|
s_video_buffer_pp_read_ptr = s_video_buffer;
|
|
|
|
s_fifo_aux_write_ptr = s_fifo_aux_data;
|
|
|
|
s_fifo_aux_read_ptr = s_fifo_aux_data;
|
2011-01-29 21:26:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-23 21:06:12 +00:00
|
|
|
// Description: Main FIFO update loop
|
|
|
|
// Purpose: Keep the Core HW updated about the CPU-GPU distance
|
Big Fifo Commit Part2: Now the fifo is more stable than my first commit, so is time...
- ReImplementing Single Core Mode like Dual Core Mode Style.
- Stage 1: My goal is, we have the Fifo, CommandProccessor code the more clear, maintenible and documented possible. When I quit dolphin I want any developer can continue with the work only reading the code.
* Big Refactoring: A lot of functions was changed the names, and modularized.
Now the FifoLoop and CatchUpGPU does not exist, was replaced by RunGpu() and RunGpuLoop().
The general idea is modeling the code like the real HW. The fifo is only a buffer where the Write Gather Pipe write the commands and from the Graphic Processor read these.
* Big Clean UP a lot of obsolete code and comments was deleted, like DcFakeWachDog, "Fifo very soon hack", etc.
In the stage 2, I will refactoring more code doing emphasis in the division of CommandProcessor, Fifo, Gpu Emulation. Beside I will comment all functions and variables in the code (Don't worry I will ask for English help for this part ;) )
Please test a lot SC mode and DC mode :)
Thank you so much for testing always and the patience. I don't like broke your favorite game but... you must believe me this part is very sensible, I only try to contribute for have a better and stable dolphin emulator.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7185 8ced0084-cf51-0410-be5f-012b33b47a6e
2011-02-17 04:25:21 +00:00
|
|
|
void RunGpuLoop()
|
2008-12-08 05:30:24 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
AsyncRequests::GetInstance()->SetEnable(true);
|
|
|
|
AsyncRequests::GetInstance()->SetPassthrough(false);
|
|
|
|
|
|
|
|
s_gpu_mainloop.Run(
|
|
|
|
[] {
|
|
|
|
const SConfig& param = SConfig::GetInstance();
|
|
|
|
|
|
|
|
// Do nothing while paused
|
2016-08-05 14:04:39 +00:00
|
|
|
if (!s_emu_running_state.IsSet())
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (s_use_deterministic_gpu_thread)
|
|
|
|
{
|
|
|
|
AsyncRequests::GetInstance()->PullEvents();
|
|
|
|
|
|
|
|
// All the fifo/CP stuff is on the CPU. We just need to run the opcode decoder.
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
s_video_buffer_read_ptr =
|
|
|
|
OpcodeDecoder::Run(DataReader(s_video_buffer_read_ptr, write_ptr), nullptr, false);
|
|
|
|
s_video_buffer_seen_ptr = write_ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-27 08:44:31 +00:00
|
|
|
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
AsyncRequests::GetInstance()->PullEvents();
|
|
|
|
|
|
|
|
CommandProcessor::SetCPStatusFromGPU();
|
|
|
|
|
|
|
|
// check if we are able to run this buffer
|
|
|
|
while (!CommandProcessor::IsInterruptWaiting() && fifo.bFF_GPReadEnable &&
|
|
|
|
fifo.CPReadWriteDistance && !AtBreakpoint())
|
|
|
|
{
|
|
|
|
if (param.bSyncGPU && s_sync_ticks.load() < param.iSyncGpuMinDistance)
|
|
|
|
break;
|
|
|
|
|
|
|
|
u32 cyclesExecuted = 0;
|
|
|
|
u32 readPtr = fifo.CPReadPointer;
|
|
|
|
ReadDataFromFifo(readPtr);
|
|
|
|
|
|
|
|
if (readPtr == fifo.CPEnd)
|
|
|
|
readPtr = fifo.CPBase;
|
|
|
|
else
|
|
|
|
readPtr += 32;
|
|
|
|
|
|
|
|
_assert_msg_(COMMANDPROCESSOR, (s32)fifo.CPReadWriteDistance - 32 >= 0,
|
|
|
|
"Negative fifo.CPReadWriteDistance = %i in FIFO Loop !\nThat can produce "
|
|
|
|
"instability in the game. Please report it.",
|
|
|
|
fifo.CPReadWriteDistance - 32);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Common::AtomicStore(fifo.CPReadPointer, readPtr);
|
2017-06-07 11:33:15 +00:00
|
|
|
Common::AtomicAdd(fifo.CPReadWriteDistance, static_cast<u32>(-32));
|
2016-06-24 08:43:46 +00:00
|
|
|
if ((write_ptr - s_video_buffer_read_ptr) == 0)
|
|
|
|
Common::AtomicStore(fifo.SafeCPReadPointer, fifo.CPReadPointer);
|
|
|
|
|
|
|
|
CommandProcessor::SetCPStatusFromGPU();
|
|
|
|
|
|
|
|
if (param.bSyncGPU)
|
|
|
|
{
|
|
|
|
cyclesExecuted = (int)(cyclesExecuted / param.fSyncGpuOverclock);
|
|
|
|
int old = s_sync_ticks.fetch_sub(cyclesExecuted);
|
2016-10-01 11:16:50 +00:00
|
|
|
if (old >= param.iSyncGpuMaxDistance &&
|
|
|
|
old - (int)cyclesExecuted < param.iSyncGpuMaxDistance)
|
2016-06-24 08:43:46 +00:00
|
|
|
s_sync_wakeup_event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// things down.
|
|
|
|
AsyncRequests::GetInstance()->PullEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
// fast skip remaining GPU time if fifo is empty
|
|
|
|
if (s_sync_ticks.load() > 0)
|
|
|
|
{
|
|
|
|
int old = s_sync_ticks.exchange(0);
|
2016-10-01 11:16:50 +00:00
|
|
|
if (old >= param.iSyncGpuMaxDistance)
|
2016-06-24 08:43:46 +00:00
|
|
|
s_sync_wakeup_event.Set();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2016-08-22 03:02:37 +00:00
|
|
|
g_vertex_manager->Flush();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
100);
|
|
|
|
|
|
|
|
AsyncRequests::GetInstance()->SetEnable(false);
|
|
|
|
AsyncRequests::GetInstance()->SetPassthrough(true);
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-06-15 06:39:26 +00:00
|
|
|
|
2015-03-05 20:14:46 +00:00
|
|
|
void FlushGpu()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& param = SConfig::GetInstance();
|
2015-06-03 21:21:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!param.bCPUThread || s_use_deterministic_gpu_thread)
|
|
|
|
return;
|
2015-03-05 20:14:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_gpu_mainloop.Wait();
|
2015-05-27 18:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GpuMaySleep()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_gpu_mainloop.AllowSleep();
|
2015-03-05 20:14:46 +00:00
|
|
|
}
|
2010-12-11 12:42:55 +00:00
|
|
|
|
|
|
|
bool AtBreakpoint()
|
|
|
|
{
|
2017-01-27 08:44:31 +00:00
|
|
|
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
|
2016-06-24 08:43:46 +00:00
|
|
|
return fifo.bFF_BPEnable && (fifo.CPReadPointer == fifo.CPBreakpoint);
|
2010-12-11 12:42:55 +00:00
|
|
|
}
|
Big Fifo Commit Part2: Now the fifo is more stable than my first commit, so is time...
- ReImplementing Single Core Mode like Dual Core Mode Style.
- Stage 1: My goal is, we have the Fifo, CommandProccessor code the more clear, maintenible and documented possible. When I quit dolphin I want any developer can continue with the work only reading the code.
* Big Refactoring: A lot of functions was changed the names, and modularized.
Now the FifoLoop and CatchUpGPU does not exist, was replaced by RunGpu() and RunGpuLoop().
The general idea is modeling the code like the real HW. The fifo is only a buffer where the Write Gather Pipe write the commands and from the Graphic Processor read these.
* Big Clean UP a lot of obsolete code and comments was deleted, like DcFakeWachDog, "Fifo very soon hack", etc.
In the stage 2, I will refactoring more code doing emphasis in the division of CommandProcessor, Fifo, Gpu Emulation. Beside I will comment all functions and variables in the code (Don't worry I will ask for English help for this part ;) )
Please test a lot SC mode and DC mode :)
Thank you so much for testing always and the patience. I don't like broke your favorite game but... you must believe me this part is very sensible, I only try to contribute for have a better and stable dolphin emulator.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@7185 8ced0084-cf51-0410-be5f-012b33b47a6e
2011-02-17 04:25:21 +00:00
|
|
|
|
|
|
|
void RunGpu()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& param = SConfig::GetInstance();
|
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
// wake up GPU thread
|
|
|
|
if (param.bCPUThread && !s_use_deterministic_gpu_thread)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-01-10 11:18:45 +00:00
|
|
|
s_gpu_mainloop.Wakeup();
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-10 11:18:45 +00: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);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-01-10 11:18:45 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
static int RunGpuOnCpu(int ticks)
|
|
|
|
{
|
2017-01-27 08:44:31 +00:00
|
|
|
CommandProcessor::SCPFifoStruct& fifo = CommandProcessor::fifo;
|
2016-01-10 11:18:45 +00:00
|
|
|
bool reset_simd_state = false;
|
|
|
|
int available_ticks = int(ticks * SConfig::GetInstance().fSyncGpuOverclock) + s_sync_ticks.load();
|
|
|
|
while (fifo.bFF_GPReadEnable && fifo.CPReadWriteDistance && !AtBreakpoint() &&
|
|
|
|
available_ticks >= 0)
|
|
|
|
{
|
|
|
|
if (s_use_deterministic_gpu_thread)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-01-10 11:18:45 +00:00
|
|
|
ReadDataFromFifoOnCPU(fifo.CPReadPointer);
|
|
|
|
s_gpu_mainloop.Wakeup();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-01-10 11:18:45 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!reset_simd_state)
|
|
|
|
{
|
|
|
|
FPURoundMode::SaveSIMDState();
|
|
|
|
FPURoundMode::LoadDefaultSIMDState();
|
|
|
|
reset_simd_state = true;
|
|
|
|
}
|
|
|
|
ReadDataFromFifo(fifo.CPReadPointer);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fifo.CPReadPointer == fifo.CPEnd)
|
|
|
|
fifo.CPReadPointer = fifo.CPBase;
|
|
|
|
else
|
|
|
|
fifo.CPReadPointer += 32;
|
|
|
|
|
|
|
|
fifo.CPReadWriteDistance -= 32;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
CommandProcessor::SetCPStatusFromGPU();
|
|
|
|
|
|
|
|
if (reset_simd_state)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-01-10 11:18:45 +00:00
|
|
|
FPURoundMode::LoadSIMDState();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-01-10 11:18:45 +00:00
|
|
|
|
|
|
|
// 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;
|
2011-02-18 23:52:14 +00:00
|
|
|
}
|
2014-09-06 21:26:40 +00:00
|
|
|
|
2016-01-12 21:44:58 +00:00
|
|
|
void UpdateWantDeterminism(bool want)
|
2014-09-06 21:26:40 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00: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;
|
|
|
|
switch (param.m_GPUDeterminismMode)
|
|
|
|
{
|
|
|
|
case GPU_DETERMINISM_AUTO:
|
|
|
|
gpu_thread = want;
|
|
|
|
break;
|
|
|
|
case GPU_DETERMINISM_NONE:
|
|
|
|
gpu_thread = false;
|
|
|
|
break;
|
|
|
|
case GPU_DETERMINISM_FAKE_COMPLETION:
|
|
|
|
gpu_thread = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpu_thread = gpu_thread && param.bCPUThread;
|
|
|
|
|
|
|
|
if (s_use_deterministic_gpu_thread != gpu_thread)
|
|
|
|
{
|
|
|
|
s_use_deterministic_gpu_thread = gpu_thread;
|
|
|
|
if (gpu_thread)
|
|
|
|
{
|
|
|
|
// These haven't been updated in non-deterministic mode.
|
|
|
|
s_video_buffer_seen_ptr = s_video_buffer_pp_read_ptr = s_video_buffer_read_ptr;
|
|
|
|
CopyPreprocessCPStateFromMain();
|
|
|
|
VertexLoaderManager::MarkAllDirty();
|
|
|
|
}
|
|
|
|
}
|
2014-09-06 21:26:40 +00:00
|
|
|
}
|
2015-06-03 21:21:46 +00:00
|
|
|
|
2016-01-24 04:31:13 +00:00
|
|
|
bool UseDeterministicGPUThread()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return s_use_deterministic_gpu_thread;
|
2016-01-24 04:31:13 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 18:42:37 +00:00
|
|
|
/* This function checks the emulated CPU - GPU distance and may wake up the GPU,
|
2016-01-10 11:18:45 +00:00
|
|
|
* or block the CPU if required. It should be called by the CPU thread regularly.
|
2016-01-20 18:42:37 +00:00
|
|
|
* @ticks The gone emulated CPU time.
|
2016-01-10 11:18:45 +00:00
|
|
|
* @return A good time to call WaitForGpuThread() next.
|
2016-01-20 18:42:37 +00:00
|
|
|
*/
|
2016-01-10 11:18:45 +00:00
|
|
|
static int WaitForGpuThread(int ticks)
|
2015-06-03 21:21:46 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
const SConfig& param = SConfig::GetInstance();
|
|
|
|
|
2016-10-01 11:16:50 +00: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;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
// Wakeup GPU
|
2016-10-01 11:16:50 +00:00
|
|
|
if (old < param.iSyncGpuMinDistance && now >= param.iSyncGpuMinDistance)
|
2016-06-24 08:43:46 +00:00
|
|
|
RunGpu();
|
|
|
|
|
2016-10-01 11:16:50 +00:00
|
|
|
// If the GPU is still sleeping, wait for a longer time
|
|
|
|
if (now < param.iSyncGpuMinDistance)
|
|
|
|
return GPU_TIME_SLOT_SIZE + param.iSyncGpuMinDistance - now;
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Wait for GPU
|
2016-10-01 11:16:50 +00:00
|
|
|
if (now >= param.iSyncGpuMaxDistance)
|
|
|
|
s_sync_wakeup_event.Wait();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-10-01 11:16:50 +00:00
|
|
|
return GPU_TIME_SLOT_SIZE;
|
2015-06-03 21:21:46 +00:00
|
|
|
}
|
2016-01-12 21:44:58 +00:00
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
static void SyncGPUCallback(u64 ticks, s64 cyclesLate)
|
2016-01-18 23:08:18 +00:00
|
|
|
{
|
2016-01-10 11:18:45 +00:00
|
|
|
ticks += cyclesLate;
|
|
|
|
int next = -1;
|
|
|
|
|
|
|
|
if (!SConfig::GetInstance().bCPUThread || s_use_deterministic_gpu_thread)
|
|
|
|
{
|
|
|
|
next = RunGpuOnCpu((int)ticks);
|
|
|
|
}
|
|
|
|
else if (SConfig::GetInstance().bSyncGPU)
|
|
|
|
{
|
|
|
|
next = WaitForGpuThread((int)ticks);
|
|
|
|
}
|
2016-01-18 23:08:18 +00:00
|
|
|
|
2016-01-10 11:18:45 +00:00
|
|
|
s_syncing_suspended = next < 0;
|
|
|
|
if (!s_syncing_suspended)
|
|
|
|
CoreTiming::ScheduleEvent(next, s_event_sync_gpu, next);
|
2016-01-18 23:08:18 +00:00
|
|
|
}
|
|
|
|
|
2016-01-20 18:42:37 +00:00
|
|
|
// Initialize GPU - CPU thread syncing, this gives us a deterministic way to start the GPU thread.
|
2016-01-18 23:08:18 +00:00
|
|
|
void Prepare()
|
|
|
|
{
|
2016-01-10 11:18:45 +00:00
|
|
|
s_event_sync_gpu = CoreTiming::RegisterEvent("SyncGPUCallback", SyncGPUCallback);
|
|
|
|
s_syncing_suspended = true;
|
2016-01-18 23:08:18 +00:00
|
|
|
}
|
2016-01-12 21:44:58 +00:00
|
|
|
}
|