2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2015-12-12 12:00:08 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2019-03-09 13:31:36 +00:00
|
|
|
#include "Common/DynamicLibrary.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2017-05-18 12:59:38 +00:00
|
|
|
#include "Core/Config/GraphicsSettings.h"
|
2016-11-09 00:41:38 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
2014-06-23 06:11:07 +00:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2019-02-15 01:59:50 +00:00
|
|
|
#include "VideoBackends/D3D/DXTexture.h"
|
2019-03-09 13:31:36 +00:00
|
|
|
#include "VideoBackends/D3DCommon/Common.h"
|
2014-02-19 11:14:09 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace DX11
|
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
static Common::DynamicLibrary s_d3d11_library;
|
2010-06-13 19:50:06 +00:00
|
|
|
namespace D3D
|
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
ComPtr<IDXGIFactory2> dxgi_factory;
|
|
|
|
ComPtr<ID3D11Device> device;
|
|
|
|
ComPtr<ID3D11Device1> device1;
|
|
|
|
ComPtr<ID3D11DeviceContext> context;
|
|
|
|
D3D_FEATURE_LEVEL feature_level;
|
2010-06-19 01:02:43 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
static ComPtr<ID3D11Debug> s_debug;
|
2017-11-19 18:23:56 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
static constexpr D3D_FEATURE_LEVEL s_supported_feature_levels[] = {
|
2017-11-19 18:23:56 +00:00
|
|
|
D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0};
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
static bool SupportsS3TCTextures()
|
2017-04-16 10:47:17 +00:00
|
|
|
{
|
|
|
|
UINT bc1_support, bc2_support, bc3_support;
|
2019-03-09 13:31:36 +00:00
|
|
|
if (FAILED(device->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &bc1_support)) ||
|
|
|
|
FAILED(device->CheckFormatSupport(DXGI_FORMAT_BC2_UNORM, &bc2_support)) ||
|
|
|
|
FAILED(device->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &bc3_support)))
|
2017-04-16 10:47:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((bc1_support & bc2_support & bc3_support) & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
static bool SupportsBPTCTextures()
|
2017-07-27 12:00:04 +00:00
|
|
|
{
|
|
|
|
// Currently, we only care about BC7. This could be extended to BC6H in the future.
|
|
|
|
UINT bc7_support;
|
2019-03-09 13:31:36 +00:00
|
|
|
if (FAILED(device->CheckFormatSupport(DXGI_FORMAT_BC7_UNORM, &bc7_support)))
|
2017-07-27 12:00:04 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return (bc7_support & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
bool Create(u32 adapter_index, bool enable_debug_layer)
|
2010-11-21 15:34:04 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
PFN_D3D11_CREATE_DEVICE d3d11_create_device;
|
|
|
|
if (!s_d3d11_library.Open("d3d11.dll") ||
|
|
|
|
!s_d3d11_library.GetSymbol("D3D11CreateDevice", &d3d11_create_device))
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
PanicAlertT("Failed to load d3d11.dll");
|
|
|
|
s_d3d11_library.Close();
|
2019-02-15 01:59:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
if (!D3DCommon::LoadLibraries())
|
2018-01-26 06:23:24 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
s_d3d11_library.Close();
|
2018-01-26 06:23:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
dxgi_factory = D3DCommon::CreateDXGIFactory(enable_debug_layer);
|
|
|
|
if (!dxgi_factory)
|
2018-01-26 06:23:24 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
PanicAlertT("Failed to create DXGI factory");
|
|
|
|
D3DCommon::UnloadLibraries();
|
|
|
|
s_d3d11_library.Close();
|
2018-01-26 06:23:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
|
|
|
HRESULT hr = dxgi_factory->EnumAdapters(adapter_index, &adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
WARN_LOG(VIDEO, "Adapter %u not found, using default", adapter_index);
|
|
|
|
adapter = nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creating debug devices can sometimes fail if the user doesn't have the correct
|
|
|
|
// version of the DirectX SDK. If it does, simply fallback to a non-debug device.
|
2019-03-09 13:31:36 +00:00
|
|
|
if (enable_debug_layer)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
|
|
|
|
D3D11_CREATE_DEVICE_DEBUG, s_supported_feature_levels,
|
|
|
|
static_cast<UINT>(ArraySize(s_supported_feature_levels)),
|
|
|
|
D3D11_SDK_VERSION, &device, &feature_level, &context);
|
2017-06-25 18:57:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Debugbreak on D3D error
|
2019-03-09 13:31:36 +00:00
|
|
|
if (SUCCEEDED(hr) && SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&s_debug))))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
ComPtr<ID3D11InfoQueue> info_queue;
|
|
|
|
if (SUCCEEDED(s_debug->QueryInterface(IID_PPV_ARGS(&info_queue))))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
|
|
|
|
info_queue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
D3D11_MESSAGE_ID hide[] = {D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS};
|
|
|
|
|
|
|
|
D3D11_INFO_QUEUE_FILTER filter = {};
|
|
|
|
filter.DenyList.NumIDs = sizeof(hide) / sizeof(D3D11_MESSAGE_ID);
|
|
|
|
filter.DenyList.pIDList = hide;
|
2019-03-09 13:31:36 +00:00
|
|
|
info_queue->AddStorageFilterEntries(&filter);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-09 13:31:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Debug layer requested but not available.");
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
if (!enable_debug_layer || FAILED(hr))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
hr = d3d11_create_device(adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0,
|
|
|
|
s_supported_feature_levels,
|
|
|
|
static_cast<UINT>(ArraySize(s_supported_feature_levels)),
|
|
|
|
D3D11_SDK_VERSION, &device, &feature_level, &context);
|
2017-07-09 23:02:29 +00:00
|
|
|
}
|
2017-06-25 18:57:46 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
if (FAILED(hr))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
PanicAlertT(
|
|
|
|
"Failed to initialize Direct3D.\nMake sure your video card supports at least D3D 10.0");
|
|
|
|
D3DCommon::UnloadLibraries();
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
hr = device->QueryInterface(IID_PPV_ARGS(&device1));
|
2017-09-03 06:33:47 +00:00
|
|
|
if (FAILED(hr))
|
2018-05-25 14:04:18 +00:00
|
|
|
{
|
2017-09-03 06:33:47 +00:00
|
|
|
WARN_LOG(VIDEO, "Missing Direct3D 11.1 support. Logical operations will not be supported.");
|
2018-05-25 14:04:18 +00:00
|
|
|
g_Config.backend_info.bSupportsLogicOp = false;
|
|
|
|
}
|
2017-09-03 06:33:47 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
g_Config.backend_info.bSupportsST3CTextures = SupportsS3TCTextures();
|
|
|
|
g_Config.backend_info.bSupportsBPTCTextures = SupportsBPTCTextures();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
// Features only supported with a FL11.0+ device.
|
|
|
|
const bool shader_model_5_supported = feature_level >= D3D_FEATURE_LEVEL_11_0;
|
|
|
|
g_Config.backend_info.bSupportsEarlyZ = shader_model_5_supported;
|
|
|
|
g_Config.backend_info.bSupportsBBox = shader_model_5_supported;
|
|
|
|
g_Config.backend_info.bSupportsFragmentStoresAndAtomics = shader_model_5_supported;
|
|
|
|
g_Config.backend_info.bSupportsGSInstancing = shader_model_5_supported;
|
|
|
|
g_Config.backend_info.bSupportsSSAA = shader_model_5_supported;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-29 09:55:00 +00:00
|
|
|
stateman = std::make_unique<StateManager>();
|
2019-03-09 13:31:36 +00:00
|
|
|
return true;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
void Destroy()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2019-03-29 09:55:00 +00:00
|
|
|
stateman.reset();
|
2014-06-20 01:45:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
context->ClearState();
|
2019-03-09 13:31:36 +00:00
|
|
|
context->Flush();
|
2011-06-11 19:37:21 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
context.Reset();
|
|
|
|
device1.Reset();
|
2015-02-09 12:00:42 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
auto remaining_references = device.Reset();
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_debug)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
--remaining_references; // the debug interface increases the refcount of the device, subtract
|
|
|
|
// that.
|
|
|
|
if (remaining_references)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// print out alive objects, but only if we actually have pending references
|
|
|
|
// note this will also print out internal live objects to the debug console
|
2017-11-19 18:23:56 +00:00
|
|
|
s_debug->ReportLiveDeviceObjects(D3D11_RLDO_SUMMARY | D3D11_RLDO_DETAIL);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2019-03-09 13:31:36 +00:00
|
|
|
s_debug.Reset();
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2015-02-09 12:00:42 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
if (remaining_references)
|
|
|
|
ERROR_LOG(VIDEO, "Unreleased references: %i.", remaining_references);
|
2016-06-24 08:43:46 +00:00
|
|
|
else
|
|
|
|
NOTICE_LOG(VIDEO, "Successfully released all device references!");
|
2010-06-19 01:02:43 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
D3DCommon::UnloadLibraries();
|
|
|
|
s_d3d11_library.Close();
|
2017-09-03 15:33:12 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
std::vector<u32> GetAAModes(u32 adapter_index)
|
2010-07-11 16:26:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
// Use temporary device if we don't have one already.
|
|
|
|
Common::DynamicLibrary temp_lib;
|
|
|
|
ComPtr<ID3D11Device> temp_device = device;
|
|
|
|
if (!temp_device)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
ComPtr<IDXGIFactory2> temp_dxgi_factory = D3DCommon::CreateDXGIFactory(false);
|
|
|
|
if (!temp_dxgi_factory)
|
|
|
|
return {};
|
2010-07-11 16:26:46 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
ComPtr<IDXGIAdapter> adapter;
|
|
|
|
temp_dxgi_factory->EnumAdapters(adapter_index, &adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
PFN_D3D11_CREATE_DEVICE d3d11_create_device;
|
|
|
|
if (!temp_lib.Open("d3d11.dll") ||
|
|
|
|
!temp_lib.GetSymbol("D3D11CreateDevice", &d3d11_create_device))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
HRESULT hr = d3d11_create_device(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0,
|
|
|
|
s_supported_feature_levels,
|
|
|
|
static_cast<UINT>(ArraySize(s_supported_feature_levels)),
|
|
|
|
D3D11_SDK_VERSION, &temp_device, nullptr, nullptr);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return {};
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
// NOTE: D3D 10.0 doesn't support multisampled resources which are bound as depth buffers AND
|
|
|
|
// shader resources. Thus, we can't have MSAA with 10.0 level hardware.
|
|
|
|
if (temp_device->GetFeatureLevel() == D3D_FEATURE_LEVEL_10_0)
|
|
|
|
return {};
|
2017-09-03 15:33:12 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
std::vector<u32> aa_modes;
|
|
|
|
for (u32 samples = 1; samples < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; ++samples)
|
2017-11-19 18:23:56 +00:00
|
|
|
{
|
2019-03-09 13:31:36 +00:00
|
|
|
UINT quality_levels = 0;
|
|
|
|
if (SUCCEEDED(temp_device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, samples,
|
|
|
|
&quality_levels)) &&
|
|
|
|
quality_levels > 0)
|
|
|
|
{
|
|
|
|
aa_modes.push_back(samples);
|
|
|
|
}
|
2017-11-19 18:23:56 +00:00
|
|
|
}
|
2017-06-26 19:47:34 +00:00
|
|
|
|
2019-03-09 13:31:36 +00:00
|
|
|
return aa_modes;
|
2017-09-02 18:22:18 +00:00
|
|
|
}
|
2011-01-29 20:16:51 +00:00
|
|
|
} // namespace D3D
|
|
|
|
|
2011-02-14 02:18:03 +00:00
|
|
|
} // namespace DX11
|