2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2011-01-31 01:28:32 +00:00
|
|
|
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
|
|
|
|
2016-02-16 02:29:24 +00:00
|
|
|
#include <algorithm>
|
2018-03-18 19:24:15 +00:00
|
|
|
#include <cstring>
|
2016-02-16 02:29:24 +00:00
|
|
|
#include <memory>
|
2016-01-17 21:54:31 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-11-14 03:33:26 +00:00
|
|
|
#include <fmt/format.h>
|
2020-01-24 08:29:38 +00:00
|
|
|
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Common/Event.h"
|
2020-11-04 19:59:34 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2020-01-24 08:29:38 +00:00
|
|
|
|
2020-05-03 23:21:51 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-09-28 04:22:18 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
|
|
|
#include "Core/Core.h"
|
2022-01-07 02:50:18 +00:00
|
|
|
#include "Core/System.h"
|
2018-03-18 19:24:15 +00:00
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
// TODO: ugly
|
|
|
|
#ifdef _WIN32
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/VideoBackend.h"
|
2019-03-28 10:35:46 +00:00
|
|
|
#include "VideoBackends/D3D12/VideoBackend.h"
|
2011-01-31 01:28:32 +00:00
|
|
|
#endif
|
2014-02-03 13:02:17 +00:00
|
|
|
#include "VideoBackends/Null/VideoBackend.h"
|
2019-11-26 04:31:45 +00:00
|
|
|
#ifdef HAS_OPENGL
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/OGL/VideoBackend.h"
|
|
|
|
#include "VideoBackends/Software/VideoBackend.h"
|
2019-11-26 04:31:45 +00:00
|
|
|
#endif
|
2020-10-23 21:51:10 +00:00
|
|
|
#ifdef HAS_VULKAN
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/VideoBackend.h"
|
2020-10-23 21:51:10 +00:00
|
|
|
#endif
|
2022-06-01 09:58:13 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include "VideoBackends/Metal/VideoBackend.h"
|
|
|
|
#endif
|
2011-03-16 22:48:17 +00:00
|
|
|
|
2018-03-18 19:24:15 +00: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"
|
2021-10-10 02:49:59 +00:00
|
|
|
#include "VideoCommon/TMEM.h"
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2019-06-29 08:35:12 +00:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
2019-07-17 00:18:48 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2018-03-18 19:24:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
#include "VideoCommon/VideoState.h"
|
2014-02-19 01:27:20 +00:00
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
VideoBackendBase* g_video_backend = nullptr;
|
2011-01-31 01:28:32 +00:00
|
|
|
|
2011-03-19 12:58:55 +00:00
|
|
|
#ifdef _WIN32
|
2013-08-11 14:30:19 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2014-10-04 19:12:15 +00: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 05:09:34 +00:00
|
|
|
// AMD drivers >= 13.35 do the same, but for the variable
|
|
|
|
// named AmdPowerXpressRequestHighPerformance instead.
|
2014-10-04 19:12:15 +00:00
|
|
|
extern "C" {
|
|
|
|
__declspec(dllexport) DWORD NvOptimusEnablement = 1;
|
2020-09-10 05:09:34 +00:00
|
|
|
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
|
2014-10-04 19:12:15 +00:00
|
|
|
}
|
2011-03-19 12:58:55 +00:00
|
|
|
#endif
|
|
|
|
|
2020-01-24 08:29:38 +00: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 19:24:15 +00:00
|
|
|
void VideoBackendBase::Video_ExitLoop()
|
|
|
|
{
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
2022-12-09 21:59:11 +00:00
|
|
|
system.GetFifo().ExitGpuLoop(system);
|
2018-03-18 19:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run from the CPU thread (from VideoInterface.cpp)
|
2021-08-03 22:55:38 +00:00
|
|
|
void VideoBackendBase::Video_OutputXFB(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height,
|
|
|
|
u64 ticks)
|
2018-03-18 19:24:15 +00:00
|
|
|
{
|
|
|
|
if (m_initialized && g_renderer && !g_ActiveConfig.bImmediateXFB)
|
|
|
|
{
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.GetFifo().SyncGPU(Fifo::SyncGPUReason::Swap);
|
2018-03-18 19:24:15 +00:00
|
|
|
|
|
|
|
AsyncRequests::Event e;
|
|
|
|
e.time = ticks;
|
|
|
|
e.type = AsyncRequests::Event::SWAP_EVENT;
|
|
|
|
|
2018-05-20 20:20:21 +00: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 19:24:15 +00:00
|
|
|
AsyncRequests::GetInstance()->PushEvent(e, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 20:20:21 +00:00
|
|
|
u32 VideoBackendBase::Video_AccessEFB(EFBAccessType type, u32 x, u32 y, u32 data)
|
2018-03-18 19:24:15 +00: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 20:20:21 +00:00
|
|
|
e.efb_poke.data = data;
|
2018-03-18 19:24:15 +00: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;
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.GetFifo().SyncGPU(Fifo::SyncGPUReason::PerfQuery);
|
2018-03-18 19:24:15 +00:00
|
|
|
|
|
|
|
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 03:33:26 +00: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 19:24:15 +00:00
|
|
|
}
|
|
|
|
warn_once = false;
|
|
|
|
}
|
2021-05-31 20:31:27 +00:00
|
|
|
else if (!g_ActiveConfig.backend_info.bSupportsBBox)
|
2018-03-18 19:24:15 +00:00
|
|
|
{
|
|
|
|
static bool warn_once = true;
|
|
|
|
if (warn_once)
|
|
|
|
{
|
2020-11-14 03:33:26 +00: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 19:24:15 +00:00
|
|
|
}
|
|
|
|
warn_once = false;
|
|
|
|
}
|
|
|
|
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
system.GetFifo().SyncGPU(Fifo::SyncGPUReason::BBox);
|
2018-03-18 19:24:15 +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;
|
|
|
|
}
|
|
|
|
|
2020-10-21 19:34:38 +00:00
|
|
|
static VideoBackendBase* GetDefaultVideoBackend()
|
|
|
|
{
|
|
|
|
const auto& backends = VideoBackendBase::GetAvailableBackends();
|
|
|
|
if (backends.empty())
|
|
|
|
return nullptr;
|
|
|
|
return backends.front().get();
|
|
|
|
}
|
|
|
|
|
2020-09-06 10:56:45 +00:00
|
|
|
std::string VideoBackendBase::GetDefaultBackendName()
|
|
|
|
{
|
2020-10-21 19:41:28 +00:00
|
|
|
auto* default_backend = GetDefaultVideoBackend();
|
|
|
|
return default_backend ? default_backend->GetName() : "";
|
2020-09-06 10:56:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 19:34:38 +00:00
|
|
|
const std::vector<std::unique_ptr<VideoBackendBase>>& VideoBackendBase::GetAvailableBackends()
|
2011-01-31 01:28:32 +00:00
|
|
|
{
|
2020-10-21 19:34:38 +00:00
|
|
|
static auto s_available_backends = [] {
|
|
|
|
std::vector<std::unique_ptr<VideoBackendBase>> backends;
|
|
|
|
|
|
|
|
// OGL > D3D11 > D3D12 > Vulkan > SW > Null
|
2022-06-02 02:49:56 +00:00
|
|
|
// On macOS, we prefer Vulkan over OpenGL due to OpenGL support being deprecated by Apple.
|
2019-11-26 04:31:45 +00:00
|
|
|
#ifdef HAS_OPENGL
|
2020-10-21 19:34:38 +00:00
|
|
|
backends.push_back(std::make_unique<OGL::VideoBackend>());
|
2019-11-26 04:31:45 +00:00
|
|
|
#endif
|
2011-01-31 01:28:32 +00:00
|
|
|
#ifdef _WIN32
|
2020-10-21 19:34:38 +00:00
|
|
|
backends.push_back(std::make_unique<DX11::VideoBackend>());
|
|
|
|
backends.push_back(std::make_unique<DX12::VideoBackend>());
|
2012-12-17 20:54:20 +00:00
|
|
|
#endif
|
2022-09-19 16:28:34 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
backends.push_back(std::make_unique<Metal::VideoBackend>());
|
|
|
|
#endif
|
2020-10-23 21:51:10 +00:00
|
|
|
#ifdef HAS_VULKAN
|
2021-07-25 20:11:01 +00:00
|
|
|
#ifdef __APPLE__
|
2022-06-02 02:49:56 +00:00
|
|
|
// Emplace the Vulkan backend at the beginning so it takes precedence over OpenGL.
|
|
|
|
backends.emplace(backends.begin(), std::make_unique<Vulkan::VideoBackend>());
|
|
|
|
#else
|
|
|
|
backends.push_back(std::make_unique<Vulkan::VideoBackend>());
|
2021-07-25 20:11:01 +00:00
|
|
|
#endif
|
2018-09-03 16:09:23 +00:00
|
|
|
#endif
|
2019-11-26 04:31:45 +00:00
|
|
|
#ifdef HAS_OPENGL
|
2020-10-21 19:34:38 +00:00
|
|
|
backends.push_back(std::make_unique<SW::VideoSoftware>());
|
2019-11-26 04:31:45 +00:00
|
|
|
#endif
|
2020-10-21 19:34:38 +00:00
|
|
|
backends.push_back(std::make_unique<Null::VideoBackend>());
|
2016-02-16 02:29:24 +00:00
|
|
|
|
2020-10-21 19:34:38 +00:00
|
|
|
if (!backends.empty())
|
|
|
|
g_video_backend = backends.front().get();
|
2016-02-16 02:29:24 +00:00
|
|
|
|
2020-10-21 19:34:38 +00:00
|
|
|
return backends;
|
|
|
|
}();
|
|
|
|
return s_available_backends;
|
2011-01-31 01:28:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 08:35:24 +00:00
|
|
|
void VideoBackendBase::ActivateBackend(const std::string& name)
|
2011-01-31 01:28:32 +00:00
|
|
|
{
|
2016-02-16 02:29:24 +00:00
|
|
|
// If empty, set it to the default backend (expected behavior)
|
|
|
|
if (name.empty())
|
2020-10-21 19:34:38 +00:00
|
|
|
g_video_backend = GetDefaultVideoBackend();
|
2012-09-25 00:47:37 +00:00
|
|
|
|
2020-10-21 19:34:38 +00: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 02:29:24 +00:00
|
|
|
|
2020-10-21 19:34:38 +00:00
|
|
|
if (iter == backends.end())
|
2016-02-16 02:29:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
g_video_backend = iter->get();
|
2011-01-31 01:28:32 +00:00
|
|
|
}
|
2018-03-18 19:24:15 +00:00
|
|
|
|
2018-09-28 04:22:18 +00:00
|
|
|
void VideoBackendBase::PopulateBackendInfo()
|
|
|
|
{
|
2022-05-16 23:59:23 +00:00
|
|
|
g_Config.Refresh();
|
2022-07-13 07:55:54 +00:00
|
|
|
// Reset backend_info so if the backend forgets to initialize something it doesn't end up using
|
|
|
|
// a value from the previously used renderer
|
|
|
|
g_Config.backend_info = {};
|
2020-05-03 23:21:51 +00:00
|
|
|
ActivateBackend(Config::Get(Config::MAIN_GFX_BACKEND));
|
2022-10-23 00:48:50 +00:00
|
|
|
g_Config.backend_info.DisplayName = g_video_backend->GetDisplayName();
|
2018-09-28 04:22:18 +00:00
|
|
|
g_video_backend->InitBackendInfo();
|
2022-05-16 23:59:23 +00:00
|
|
|
// We validate 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.
|
|
|
|
g_Config.VerifyValidity();
|
2018-09-28 04:22:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 23:21:51 +00: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 19:24:15 +00:00
|
|
|
void VideoBackendBase::DoState(PointerWrap& p)
|
2019-06-29 08:35:12 +00:00
|
|
|
{
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
if (!system.IsDualCoreMode())
|
2019-06-29 08:35:12 +00:00
|
|
|
{
|
2019-06-29 09:27:53 +00:00
|
|
|
VideoCommon_DoState(p);
|
2019-06-29 08:35:12 +00: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.
|
2022-12-09 19:01:25 +00:00
|
|
|
system.GetFifo().GpuMaySleep();
|
2019-06-29 08:35:12 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 19:24:15 +00:00
|
|
|
void VideoBackendBase::InitializeShared()
|
|
|
|
{
|
2020-12-16 14:01:20 +00: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 19:24:15 +00:00
|
|
|
memset(texMem, 0, TMEM_SIZE);
|
|
|
|
|
|
|
|
// do not initialize again for the config window
|
|
|
|
m_initialized = true;
|
|
|
|
|
2022-11-27 12:50:50 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
auto& command_processor = system.GetCommandProcessor();
|
|
|
|
command_processor.Init(system);
|
2022-12-09 21:59:11 +00:00
|
|
|
system.GetFifo().Init(system);
|
2022-12-10 16:10:14 +00:00
|
|
|
system.GetPixelEngine().Init(system);
|
2018-03-18 19:24:15 +00:00
|
|
|
BPInit();
|
|
|
|
VertexLoaderManager::Init();
|
2022-12-28 14:38:46 +00:00
|
|
|
system.GetVertexShaderManager().Init();
|
2022-12-29 14:27:48 +00:00
|
|
|
system.GetGeometryShaderManager().Init();
|
2022-12-27 16:42:02 +00:00
|
|
|
system.GetPixelShaderManager().Init();
|
2021-10-10 02:49:59 +00:00
|
|
|
TMEM::Init();
|
2018-03-18 19:24:15 +00:00
|
|
|
|
2019-03-09 13:31:40 +00:00
|
|
|
g_Config.VerifyValidity();
|
2018-03-18 19:24:15 +00:00
|
|
|
UpdateActiveConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoBackendBase::ShutdownShared()
|
|
|
|
{
|
|
|
|
m_initialized = false;
|
|
|
|
|
2022-12-09 19:01:25 +00:00
|
|
|
auto& system = Core::System::GetInstance();
|
2018-03-18 19:24:15 +00:00
|
|
|
VertexLoaderManager::Clear();
|
2022-12-09 19:01:25 +00:00
|
|
|
system.GetFifo().Shutdown();
|
2018-03-18 19:24:15 +00:00
|
|
|
}
|