2015-05-24 04:32:32 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "Common/ChunkFile.h"
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-08-24 23:13:46 +00:00
|
|
|
#include "Common/Event.h"
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/Flag.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-01-13 20:14:20 +00:00
|
|
|
#include "Core/Host.h"
|
2015-01-31 10:38:23 +00:00
|
|
|
#include "VideoCommon/AsyncRequests.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/BPStructs.h"
|
2016-01-13 20:14:20 +00:00
|
|
|
#include "VideoCommon/CPMemory.h"
|
|
|
|
#include "VideoCommon/CommandProcessor.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/Fifo.h"
|
2016-01-13 20:14:20 +00:00
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
#include "VideoCommon/OpcodeDecoding.h"
|
|
|
|
#include "VideoCommon/PixelEngine.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2016-01-13 20:14:20 +00:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/VideoState.h"
|
2010-11-18 02:21:26 +00:00
|
|
|
|
2014-08-24 23:13:46 +00:00
|
|
|
static Common::Flag s_FifoShuttingDown;
|
2010-11-18 02:21:26 +00:00
|
|
|
|
|
|
|
static volatile struct
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 xfbAddr;
|
|
|
|
u32 fbWidth;
|
|
|
|
u32 fbStride;
|
|
|
|
u32 fbHeight;
|
2010-11-18 02:21:26 +00:00
|
|
|
} s_beginFieldArgs;
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
void VideoBackendBase::Video_ExitLoop()
|
2010-11-18 02:21:26 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
Fifo::ExitGpuLoop();
|
|
|
|
s_FifoShuttingDown.Set();
|
2010-11-18 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run from the CPU thread (from VideoInterface.cpp)
|
2016-10-08 10:56:28 +00:00
|
|
|
void VideoBackendBase::Video_BeginField(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
|
|
|
|
u64 ticks)
|
2010-11-18 02:21:26 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_initialized && g_ActiveConfig.bUseXFB && g_renderer)
|
|
|
|
{
|
2016-08-19 02:35:58 +00:00
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::Swap);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
AsyncRequests::Event e;
|
2016-10-08 10:56:28 +00:00
|
|
|
e.time = ticks;
|
2016-06-24 08:43:46 +00:00
|
|
|
e.type = AsyncRequests::Event::SWAP_EVENT;
|
|
|
|
|
2016-06-28 03:12:43 +00:00
|
|
|
e.swap_event.xfbAddr = xfbAddr;
|
|
|
|
e.swap_event.fbWidth = fbWidth;
|
|
|
|
e.swap_event.fbStride = fbStride;
|
|
|
|
e.swap_event.fbHeight = fbHeight;
|
2016-06-24 08:43:46 +00:00
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, false);
|
|
|
|
}
|
2010-11-18 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
u32 VideoBackendBase::Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 InputData)
|
2010-11-18 02:21:26 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!g_ActiveConfig.bEFBAccessEnable)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == POKE_COLOR || type == POKE_Z)
|
|
|
|
{
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.type = type == POKE_COLOR ? AsyncRequests::Event::EFB_POKE_COLOR :
|
|
|
|
AsyncRequests::Event::EFB_POKE_Z;
|
|
|
|
e.time = 0;
|
|
|
|
e.efb_poke.data = InputData;
|
|
|
|
e.efb_poke.x = x;
|
|
|
|
e.efb_poke.y = y;
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
u32 result;
|
|
|
|
e.type = type == PEEK_COLOR ? AsyncRequests::Event::EFB_PEEK_COLOR :
|
|
|
|
AsyncRequests::Event::EFB_PEEK_Z;
|
|
|
|
e.time = 0;
|
|
|
|
e.efb_peek.x = x;
|
|
|
|
e.efb_peek.y = y;
|
|
|
|
e.efb_peek.data = &result;
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, true);
|
|
|
|
return result;
|
|
|
|
}
|
2010-11-18 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
u32 VideoBackendBase::Video_GetQueryResult(PerfQueryType type)
|
2013-02-16 23:50:40 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!g_perf_query->ShouldEmulate())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-09 17:37:59 +00:00
|
|
|
|
2016-08-19 02:35:58 +00:00
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::PerfQuery);
|
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
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.time = 0;
|
|
|
|
e.type = AsyncRequests::Event::PERF_QUERY;
|
2015-01-31 12:09:25 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!g_perf_query->IsFlushed())
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, true);
|
2013-03-01 22:02:11 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return g_perf_query->GetQueryResult(type);
|
2013-02-16 23:50:40 +00:00
|
|
|
}
|
2011-12-31 04:16:12 +00:00
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
u16 VideoBackendBase::Video_GetBoundingBox(int index)
|
2014-11-13 22:26:49 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!g_ActiveConfig.bBBoxEnable)
|
|
|
|
{
|
|
|
|
static bool warn_once = true;
|
|
|
|
if (warn_once)
|
|
|
|
ERROR_LOG(VIDEO, "BBox shall be used but it is disabled. Please use a gameini to enable it "
|
|
|
|
"for this game.");
|
|
|
|
warn_once = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-19 02:35:58 +00:00
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::BBox);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
u16 result;
|
|
|
|
e.time = 0;
|
|
|
|
e.type = AsyncRequests::Event::BBOX_READ;
|
|
|
|
e.bbox.index = index;
|
|
|
|
e.bbox.data = &result;
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, true);
|
|
|
|
|
|
|
|
return result;
|
2014-11-13 22:26:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 20:38:11 +00:00
|
|
|
void VideoBackendBase::ShowConfig(void* parent_handle)
|
|
|
|
{
|
|
|
|
if (!m_initialized)
|
|
|
|
InitBackendInfo();
|
|
|
|
|
2016-06-26 06:06:23 +00:00
|
|
|
Host_ShowVideoConfig(parent_handle, GetDisplayName());
|
2016-01-13 20:38:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
void VideoBackendBase::InitializeShared()
|
2012-01-02 10:20:22 +00:00
|
|
|
{
|
2016-01-13 20:14:20 +00:00
|
|
|
memset(&g_main_cp_state, 0, sizeof(g_main_cp_state));
|
|
|
|
memset(&g_preprocess_cp_state, 0, sizeof(g_preprocess_cp_state));
|
|
|
|
memset(texMem, 0, TMEM_SIZE);
|
|
|
|
|
|
|
|
// Do our OSD callbacks
|
|
|
|
OSD::DoCallbacks(OSD::CallbackType::Initialization);
|
|
|
|
|
|
|
|
// do not initialize again for the config window
|
|
|
|
m_initialized = true;
|
2012-01-02 10:20:22 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_FifoShuttingDown.Clear();
|
|
|
|
memset((void*)&s_beginFieldArgs, 0, sizeof(s_beginFieldArgs));
|
|
|
|
m_invalid = false;
|
2016-01-13 20:14:20 +00:00
|
|
|
frameCount = 0;
|
|
|
|
|
|
|
|
CommandProcessor::Init();
|
|
|
|
Fifo::Init();
|
|
|
|
OpcodeDecoder::Init();
|
|
|
|
PixelEngine::Init();
|
|
|
|
BPInit();
|
|
|
|
VertexLoaderManager::Init();
|
|
|
|
IndexGenerator::Init();
|
|
|
|
VertexShaderManager::Init();
|
|
|
|
GeometryShaderManager::Init();
|
|
|
|
PixelShaderManager::Init();
|
|
|
|
|
2016-06-26 06:06:23 +00:00
|
|
|
g_Config.Load(File::GetUserPath(D_CONFIG_IDX) + "GFX.ini");
|
2016-01-13 20:14:20 +00:00
|
|
|
g_Config.GameIniLoad();
|
|
|
|
g_Config.UpdateProjectionHack();
|
|
|
|
g_Config.VerifyValidity();
|
|
|
|
UpdateActiveConfig();
|
|
|
|
|
|
|
|
// Notify the core that the video backend is ready
|
|
|
|
Host_Message(WM_USER_CREATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::ShutdownShared()
|
|
|
|
{
|
|
|
|
// Do our OSD callbacks
|
|
|
|
OSD::DoCallbacks(OSD::CallbackType::Shutdown);
|
|
|
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
|
|
|
Fifo::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::CleanupShared()
|
|
|
|
{
|
|
|
|
VertexLoaderManager::Clear();
|
2012-01-02 10:20:22 +00:00
|
|
|
}
|
|
|
|
|
2011-12-31 04:16:12 +00:00
|
|
|
// Run from the CPU thread
|
2016-01-12 08:35:24 +00:00
|
|
|
void VideoBackendBase::DoState(PointerWrap& p)
|
2011-02-08 10:37:47 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
bool software = false;
|
|
|
|
p.Do(software);
|
|
|
|
|
|
|
|
if (p.GetMode() == PointerWrap::MODE_READ && software == true)
|
|
|
|
{
|
|
|
|
// change mode to abort load of incompatible save state.
|
|
|
|
p.SetMode(PointerWrap::MODE_VERIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
VideoCommon_DoState(p);
|
|
|
|
p.DoMarker("VideoCommon");
|
|
|
|
|
|
|
|
p.Do(s_beginFieldArgs);
|
|
|
|
p.DoMarker("VideoBackendBase");
|
|
|
|
|
|
|
|
// Refresh state.
|
|
|
|
if (p.GetMode() == PointerWrap::MODE_READ)
|
|
|
|
{
|
|
|
|
m_invalid = true;
|
|
|
|
|
|
|
|
// Clear all caches that touch RAM
|
|
|
|
// (? these don't appear to touch any emulation state that gets saved. moved to on load only.)
|
|
|
|
VertexLoaderManager::MarkAllDirty();
|
|
|
|
}
|
2010-12-16 18:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
void VideoBackendBase::CheckInvalidState()
|
2013-04-24 13:21:54 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_invalid)
|
|
|
|
{
|
|
|
|
m_invalid = false;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
BPReload();
|
|
|
|
TextureCacheBase::Invalidate();
|
|
|
|
}
|
2012-12-23 12:32:23 +00:00
|
|
|
}
|