2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
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>
|
|
|
|
|
2020-11-14 04:33:26 +01:00
|
|
|
#include <fmt/format.h>
|
2020-01-24 09:29:38 +01:00
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
2020-05-04 01:21:51 +02:00
|
|
|
#include "Common/Config/Config.h"
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "Common/Event.h"
|
2020-11-04 20:59:34 +01:00
|
|
|
#include "Common/FileUtil.h"
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "Common/Logging/Log.h"
|
2020-01-24 09:29:38 +01:00
|
|
|
|
2020-05-04 01:21:51 +02:00
|
|
|
#include "Core/Config/MainSettings.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
|
|
|
|
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"
|
2019-11-26 05:31:45 +01:00
|
|
|
#ifdef HAS_OPENGL
|
2014-02-17 11:18:15 +01:00
|
|
|
#include "VideoBackends/OGL/VideoBackend.h"
|
|
|
|
#include "VideoBackends/Software/VideoBackend.h"
|
2019-11-26 05:31:45 +01:00
|
|
|
#endif
|
2020-10-23 23:51:10 +02:00
|
|
|
#ifdef HAS_VULKAN
|
2016-08-13 14:57:50 +02:00
|
|
|
#include "VideoBackends/Vulkan/VideoBackend.h"
|
2020-10-23 23:51:10 +02:00
|
|
|
#endif
|
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/OpcodeDecoding.h"
|
|
|
|
#include "VideoCommon/PixelEngine.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
|
|
|
#include "VideoCommon/RenderBase.h"
|
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2019-06-29 10:35:12 +02:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2019-07-17 02:18:48 +02:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2018-03-18 20:24:15 +01:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/VideoState.h"
|
2014-02-19 02:27:20 +01:00
|
|
|
|
2016-01-12 09:35:24 +01:00
|
|
|
VideoBackendBase* g_video_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.
|
2020-09-10 07:09:34 +02:00
|
|
|
// AMD drivers >= 13.35 do the same, but for the variable
|
|
|
|
// named AmdPowerXpressRequestHighPerformance instead.
|
2014-10-04 21:12:15 +02:00
|
|
|
extern "C" {
|
|
|
|
__declspec(dllexport) DWORD NvOptimusEnablement = 1;
|
2020-09-10 07:09:34 +02:00
|
|
|
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
|
2014-10-04 21:12:15 +02:00
|
|
|
}
|
2011-03-19 13:58:55 +01:00
|
|
|
#endif
|
|
|
|
|
2020-01-24 09:29:38 +01:00
|
|
|
std::string VideoBackendBase::BadShaderFilename(const char* shader_stage, int counter)
|
|
|
|
{
|
|
|
|
return fmt::format("{}bad_{}_{}_{}.txt", File::GetUserPath(D_DUMP_IDX), shader_stage,
|
|
|
|
g_video_backend->GetName(), counter);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-11-14 04:33:26 +01:00
|
|
|
ERROR_LOG_FMT(VIDEO,
|
|
|
|
"BBox shall be used but it is disabled. Please use a gameini to enable it "
|
|
|
|
"for this game.");
|
2018-03-18 20:24:15 +01:00
|
|
|
}
|
|
|
|
warn_once = false;
|
|
|
|
}
|
2021-05-31 22:31:27 +02:00
|
|
|
else if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
2018-03-18 20:24:15 +01:00
|
|
|
{
|
|
|
|
static bool warn_once = true;
|
|
|
|
if (warn_once)
|
|
|
|
{
|
2020-11-14 04:33:26 +01:00
|
|
|
PanicAlertFmtT(
|
|
|
|
"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.");
|
2018-03-18 20:24:15 +01:00
|
|
|
}
|
|
|
|
warn_once = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
static VideoBackendBase* GetDefaultVideoBackend()
|
|
|
|
{
|
|
|
|
const auto& backends = VideoBackendBase::GetAvailableBackends();
|
|
|
|
if (backends.empty())
|
|
|
|
return nullptr;
|
|
|
|
return backends.front().get();
|
|
|
|
}
|
|
|
|
|
2020-09-06 12:56:45 +02:00
|
|
|
std::string VideoBackendBase::GetDefaultBackendName()
|
|
|
|
{
|
2020-10-21 21:41:28 +02:00
|
|
|
auto* default_backend = GetDefaultVideoBackend();
|
|
|
|
return default_backend ? default_backend->GetName() : "";
|
2020-09-06 12:56:45 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
const std::vector<std::unique_ptr<VideoBackendBase>>& VideoBackendBase::GetAvailableBackends()
|
2011-01-31 02:28:32 +01:00
|
|
|
{
|
2020-10-21 21:34:38 +02:00
|
|
|
static auto s_available_backends = [] {
|
|
|
|
std::vector<std::unique_ptr<VideoBackendBase>> backends;
|
|
|
|
|
|
|
|
// OGL > D3D11 > D3D12 > Vulkan > SW > Null
|
2019-11-26 05:31:45 +01:00
|
|
|
#ifdef HAS_OPENGL
|
2020-10-21 21:34:38 +02:00
|
|
|
backends.push_back(std::make_unique<OGL::VideoBackend>());
|
2019-11-26 05:31:45 +01:00
|
|
|
#endif
|
2011-01-31 02:28:32 +01:00
|
|
|
#ifdef _WIN32
|
2020-10-21 21:34:38 +02:00
|
|
|
backends.push_back(std::make_unique<DX11::VideoBackend>());
|
|
|
|
backends.push_back(std::make_unique<DX12::VideoBackend>());
|
2012-12-17 21:54:20 +01:00
|
|
|
#endif
|
2020-10-23 23:51:10 +02:00
|
|
|
#ifdef HAS_VULKAN
|
2020-10-21 21:34:38 +02:00
|
|
|
backends.push_back(std::make_unique<Vulkan::VideoBackend>());
|
2018-09-03 18:09:23 +02:00
|
|
|
#endif
|
2019-11-26 05:31:45 +01:00
|
|
|
#ifdef HAS_OPENGL
|
2020-10-21 21:34:38 +02:00
|
|
|
backends.push_back(std::make_unique<SW::VideoSoftware>());
|
2019-11-26 05:31:45 +01:00
|
|
|
#endif
|
2020-10-21 21:34:38 +02:00
|
|
|
backends.push_back(std::make_unique<Null::VideoBackend>());
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
if (!backends.empty())
|
|
|
|
g_video_backend = backends.front().get();
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
return backends;
|
|
|
|
}();
|
|
|
|
return s_available_backends;
|
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-02-16 03:29:24 +01:00
|
|
|
// If empty, set it to the default backend (expected behavior)
|
|
|
|
if (name.empty())
|
2020-10-21 21:34:38 +02:00
|
|
|
g_video_backend = GetDefaultVideoBackend();
|
2012-09-25 02:47:37 +02:00
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
const auto& backends = GetAvailableBackends();
|
|
|
|
const auto iter = std::find_if(backends.begin(), backends.end(), [&name](const auto& backend) {
|
|
|
|
return name == backend->GetName();
|
|
|
|
});
|
2016-02-16 03:29:24 +01:00
|
|
|
|
2020-10-21 21:34:38 +02:00
|
|
|
if (iter == backends.end())
|
2016-02-16 03:29:24 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
// 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.
|
2020-05-04 01:21:51 +02:00
|
|
|
ActivateBackend(Config::Get(Config::MAIN_GFX_BACKEND));
|
2018-09-28 06:22:18 +02:00
|
|
|
g_video_backend->InitBackendInfo();
|
|
|
|
g_Config.Refresh();
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:21:51 +02:00
|
|
|
void VideoBackendBase::PopulateBackendInfoFromUI()
|
|
|
|
{
|
|
|
|
// 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())
|
|
|
|
PopulateBackendInfo();
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
void VideoBackendBase::DoState(PointerWrap& p)
|
2019-06-29 10:35:12 +02:00
|
|
|
{
|
|
|
|
if (!SConfig::GetInstance().bCPUThread)
|
|
|
|
{
|
2019-06-29 11:27:53 +02:00
|
|
|
VideoCommon_DoState(p);
|
2019-06-29 10:35:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncRequests::Event ev = {};
|
|
|
|
ev.do_save_state.p = &p;
|
|
|
|
ev.type = AsyncRequests::Event::DO_SAVE_STATE;
|
|
|
|
AsyncRequests::GetInstance()->PushEvent(ev, true);
|
|
|
|
|
|
|
|
// Let the GPU thread sleep after loading the state, so we're not spinning if paused after loading
|
|
|
|
// a state. The next GP burst will wake it up again.
|
|
|
|
Fifo::GpuMaySleep();
|
|
|
|
}
|
|
|
|
|
2018-03-18 20:24:15 +01:00
|
|
|
void VideoBackendBase::InitializeShared()
|
|
|
|
{
|
2020-12-16 15:01:20 +01:00
|
|
|
memset(reinterpret_cast<u8*>(&g_main_cp_state), 0, sizeof(g_main_cp_state));
|
|
|
|
memset(reinterpret_cast<u8*>(&g_preprocess_cp_state), 0, sizeof(g_preprocess_cp_state));
|
2018-03-18 20:24:15 +01:00
|
|
|
memset(texMem, 0, TMEM_SIZE);
|
|
|
|
|
|
|
|
// do not initialize again for the config window
|
|
|
|
m_initialized = true;
|
|
|
|
|
|
|
|
CommandProcessor::Init();
|
|
|
|
Fifo::Init();
|
|
|
|
OpcodeDecoder::Init();
|
|
|
|
PixelEngine::Init();
|
|
|
|
BPInit();
|
|
|
|
VertexLoaderManager::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();
|
|
|
|
}
|