2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:09:55 +02:00
|
|
|
// Refer to the license.txt file included.
|
2011-01-31 02:28:32 +01:00
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
|
|
|
|
2016-02-16 03:29:24 +01:00
|
|
|
#include <algorithm>
|
2018-03-18 20:24:15 +01:00
|
|
|
#include <cstring>
|
2016-02-16 03:29:24 +01:00
|
|
|
#include <memory>
|
2016-01-17 22:54:31 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Event.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
2018-09-28 06:22:18 +02:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/Core.h"
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "Core/Host.h"
|
|
|
|
|
2011-01-31 02:28:32 +01:00
|
|
|
// TODO: ugly
|
|
|
|
#ifdef _WIN32
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "VideoBackends/D3D/VideoBackend.h"
|
2019-03-28 11:35:46 +01:00
|
|
|
#include "VideoBackends/D3D12/VideoBackend.h"
|
2011-01-31 02:28:32 +01:00
|
|
|
#endif
|
2014-02-03 14:02:17 +01:00
|
|
|
#include "VideoBackends/Null/VideoBackend.h"
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "VideoBackends/OGL/VideoBackend.h"
|
|
|
|
#include "VideoBackends/Software/VideoBackend.h"
|
2016-08-13 14:57:50 +02:00
|
|
|
#include "VideoBackends/Vulkan/VideoBackend.h"
|
2011-03-16 23:48:17 +01:00
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "VideoCommon/AsyncRequests.h"
|
|
|
|
#include "VideoCommon/BPStructs.h"
|
|
|
|
#include "VideoCommon/CPMemory.h"
|
|
|
|
#include "VideoCommon/CommandProcessor.h"
|
|
|
|
#include "VideoCommon/Fifo.h"
|
|
|
|
#include "VideoCommon/GeometryShaderManager.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
#include "VideoCommon/OpcodeDecoding.h"
|
|
|
|
#include "VideoCommon/PixelEngine.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/VideoState.h"
|
2014-02-19 02:27:20 +01:00
|
|
|
|
2016-02-16 03:29:24 +01:00
|
|
|
std::vector<std::unique_ptr<VideoBackendBase>> g_available_video_backends;
|
2016-01-12 09:35:24 +01:00
|
|
|
VideoBackendBase* g_video_backend = nullptr;
|
|
|
|
static VideoBackendBase* s_default_backend = nullptr;
|
2011-01-31 02:28:32 +01:00
|
|
|
|
2011-03-19 13:58:55 +01:00
|
|
|
#ifdef _WIN32
|
2013-08-11 16:30:19 +02:00
|
|
|
#include <windows.h>
|
|
|
|
|
2014-10-04 21:12:15 +02:00
|
|
|
// Nvidia drivers >= v302 will check if the application exports a global
|
|
|
|
// variable named NvOptimusEnablement to know if it should run the app in high
|
|
|
|
// performance graphics mode or using the IGP.
|
|
|
|
extern "C" {
|
|
|
|
__declspec(dllexport) DWORD NvOptimusEnablement = 1;
|
|
|
|
}
|
2011-03-19 13:58:55 +01:00
|
|
|
#endif
|
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
void VideoBackendBase::Video_ExitLoop()
|
|
|
|
{
|
|
|
|
Fifo::ExitGpuLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run from the CPU thread (from VideoInterface.cpp)
|
2018-05-20 22:20:21 +02:00
|
|
|
void VideoBackendBase::Video_BeginField(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height,
|
2018-03-18 20:24:15 +01:00
|
|
|
u64 ticks)
|
|
|
|
{
|
|
|
|
if (m_initialized && g_renderer && !g_ActiveConfig.bImmediateXFB)
|
|
|
|
{
|
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::Swap);
|
|
|
|
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.time = ticks;
|
|
|
|
e.type = AsyncRequests::Event::SWAP_EVENT;
|
|
|
|
|
2018-05-20 22:20:21 +02:00
|
|
|
e.swap_event.xfbAddr = xfb_addr;
|
|
|
|
e.swap_event.fbWidth = fb_width;
|
|
|
|
e.swap_event.fbStride = fb_stride;
|
|
|
|
e.swap_event.fbHeight = fb_height;
|
2018-03-18 20:24:15 +01:00
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:20:21 +02:00
|
|
|
u32 VideoBackendBase::Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 data)
|
2018-03-18 20:24:15 +01:00
|
|
|
{
|
|
|
|
if (!g_ActiveConfig.bEFBAccessEnable || x >= EFB_WIDTH || y >= EFB_HEIGHT)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == EFBAccessType::PokeColor || type == EFBAccessType::PokeZ)
|
|
|
|
{
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.type = type == EFBAccessType::PokeColor ? AsyncRequests::Event::EFB_POKE_COLOR :
|
|
|
|
AsyncRequests::Event::EFB_POKE_Z;
|
|
|
|
e.time = 0;
|
2018-05-20 22:20:21 +02:00
|
|
|
e.efb_poke.data = data;
|
2018-03-18 20:24:15 +01:00
|
|
|
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 == EFBAccessType::PeekColor ? 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 VideoBackendBase::Video_GetQueryResult(PerfQueryType type)
|
|
|
|
{
|
|
|
|
if (!g_perf_query->ShouldEmulate())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::PerfQuery);
|
|
|
|
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.time = 0;
|
|
|
|
e.type = AsyncRequests::Event::PERF_QUERY;
|
|
|
|
|
|
|
|
if (!g_perf_query->IsFlushed())
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, true);
|
|
|
|
|
|
|
|
return g_perf_query->GetQueryResult(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 VideoBackendBase::Video_GetBoundingBox(int index)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
|
|
|
{
|
|
|
|
static bool warn_once = true;
|
|
|
|
if (warn_once)
|
|
|
|
{
|
|
|
|
PanicAlertT("This game requires bounding box emulation to run properly but your graphics "
|
|
|
|
"card or its drivers do not support it. As a result you will experience bugs or "
|
|
|
|
"freezes while running this game.");
|
|
|
|
}
|
|
|
|
warn_once = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Fifo::SyncGPU(Fifo::SyncGPUReason::BBox);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-12 09:35:24 +01:00
|
|
|
void VideoBackendBase::PopulateList()
|
2011-01-31 02:28:32 +01:00
|
|
|
{
|
2016-11-05 05:57:33 +01:00
|
|
|
// OGL > D3D11 > Vulkan > SW > Null
|
2016-06-24 10:43:46 +02:00
|
|
|
g_available_video_backends.push_back(std::make_unique<OGL::VideoBackend>());
|
2011-01-31 02:28:32 +01:00
|
|
|
#ifdef _WIN32
|
2016-06-24 10:43:46 +02:00
|
|
|
g_available_video_backends.push_back(std::make_unique<DX11::VideoBackend>());
|
2019-03-28 11:35:46 +01:00
|
|
|
g_available_video_backends.push_back(std::make_unique<DX12::VideoBackend>());
|
2012-12-17 21:54:20 +01:00
|
|
|
#endif
|
2016-08-13 14:57:50 +02:00
|
|
|
g_available_video_backends.push_back(std::make_unique<Vulkan::VideoBackend>());
|
2016-06-24 10:43:46 +02:00
|
|
|
g_available_video_backends.push_back(std::make_unique<SW::VideoSoftware>());
|
2014-02-03 14:02:17 +01:00
|
|
|
g_available_video_backends.push_back(std::make_unique<Null::VideoBackend>());
|
2011-02-08 15:51:53 +01:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(),
|
|
|
|
[](const auto& backend) { return backend != nullptr; });
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
if (iter == g_available_video_backends.end())
|
|
|
|
return;
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
s_default_backend = iter->get();
|
|
|
|
g_video_backend = iter->get();
|
2011-01-31 02:28:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-12 09:35:24 +01:00
|
|
|
void VideoBackendBase::ClearList()
|
2011-01-31 02:28:32 +01:00
|
|
|
{
|
2016-06-24 10:43:46 +02:00
|
|
|
g_available_video_backends.clear();
|
2011-01-31 02:28:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-12 09:35:24 +01:00
|
|
|
void VideoBackendBase::ActivateBackend(const std::string& name)
|
2011-01-31 02:28:32 +01:00
|
|
|
{
|
2016-06-24 10:43:46 +02:00
|
|
|
// If empty, set it to the default backend (expected behavior)
|
|
|
|
if (name.empty())
|
|
|
|
g_video_backend = s_default_backend;
|
2012-09-25 02:47:37 +02:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
const auto iter =
|
|
|
|
std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(),
|
|
|
|
[&name](const auto& backend) { return name == backend->GetName(); });
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
if (iter == g_available_video_backends.end())
|
|
|
|
return;
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2016-06-24 10:43:46 +02:00
|
|
|
g_video_backend = iter->get();
|
2011-01-31 02:28:32 +01:00
|
|
|
}
|
2018-03-18 20:24:15 +01:00
|
|
|
|
2018-09-28 06:22:18 +02:00
|
|
|
void VideoBackendBase::PopulateBackendInfo()
|
|
|
|
{
|
|
|
|
// If the core is running, the backend info will have been populated already.
|
|
|
|
// If we did it here, the UI thread can race with the with the GPU thread.
|
|
|
|
if (Core::IsRunning())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// We refresh the config after initializing the backend info, as system-specific settings
|
|
|
|
// such as anti-aliasing, or the selected adapter may be invalid, and should be checked.
|
|
|
|
ActivateBackend(SConfig::GetInstance().m_strVideoBackend);
|
|
|
|
g_video_backend->InitBackendInfo();
|
|
|
|
g_Config.Refresh();
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
// Run from the CPU thread
|
|
|
|
void VideoBackendBase::DoState(PointerWrap& p)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::CheckInvalidState()
|
|
|
|
{
|
|
|
|
if (m_invalid)
|
|
|
|
{
|
|
|
|
m_invalid = false;
|
|
|
|
|
|
|
|
BPReload();
|
|
|
|
g_texture_cache->Invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::InitializeShared()
|
|
|
|
{
|
|
|
|
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 not initialize again for the config window
|
|
|
|
m_initialized = true;
|
|
|
|
|
|
|
|
m_invalid = false;
|
|
|
|
|
|
|
|
CommandProcessor::Init();
|
|
|
|
Fifo::Init();
|
|
|
|
OpcodeDecoder::Init();
|
|
|
|
PixelEngine::Init();
|
|
|
|
BPInit();
|
|
|
|
VertexLoaderManager::Init();
|
|
|
|
IndexGenerator::Init();
|
|
|
|
VertexShaderManager::Init();
|
|
|
|
GeometryShaderManager::Init();
|
|
|
|
PixelShaderManager::Init();
|
|
|
|
|
2019-03-09 14:31:40 +01:00
|
|
|
g_Config.VerifyValidity();
|
2018-03-18 20:24:15 +01:00
|
|
|
UpdateActiveConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::ShutdownShared()
|
|
|
|
{
|
|
|
|
m_initialized = false;
|
|
|
|
|
|
|
|
VertexLoaderManager::Clear();
|
|
|
|
Fifo::Shutdown();
|
|
|
|
}
|