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"
|
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"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoBackends/D3D/D3DTexture.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
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
using D3DREFLECT = HRESULT(WINAPI*)(LPCVOID, SIZE_T, REFIID, void**);
|
|
|
|
|
|
|
|
static HINSTANCE s_d3d_compiler_dll;
|
|
|
|
static int s_d3dcompiler_dll_ref;
|
|
|
|
static D3DREFLECT s_d3d_reflect;
|
2014-03-09 20:14:26 +00:00
|
|
|
pD3DCompile PD3DCompile = nullptr;
|
2011-02-26 23:41:02 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
CREATEDXGIFACTORY PCreateDXGIFactory = nullptr;
|
2017-11-19 18:23:56 +00:00
|
|
|
static HINSTANCE s_dxgi_dll;
|
|
|
|
static int s_dxgi_dll_ref;
|
2010-11-21 15:34:04 +00:00
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
static D3D11CREATEDEVICE s_d3d11_create_device;
|
|
|
|
static HINSTANCE s_d3d_dll;
|
|
|
|
static int s_d3d_dll_ref;
|
2010-11-21 15:34:04 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
namespace D3D
|
|
|
|
{
|
2014-03-09 20:14:26 +00:00
|
|
|
ID3D11Device* device = nullptr;
|
2017-09-03 06:33:47 +00:00
|
|
|
ID3D11Device1* device1 = nullptr;
|
2014-03-09 20:14:26 +00:00
|
|
|
ID3D11DeviceContext* context = nullptr;
|
2018-01-26 06:23:24 +00:00
|
|
|
IDXGISwapChain1* swapchain = nullptr;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
static IDXGIFactory2* s_dxgi_factory;
|
2017-11-19 18:23:56 +00:00
|
|
|
static ID3D11Debug* s_debug;
|
|
|
|
static D3D_FEATURE_LEVEL s_featlevel;
|
|
|
|
static D3DTexture2D* s_backbuf;
|
2010-11-23 19:58:02 +00:00
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
static std::vector<DXGI_SAMPLE_DESC> s_aa_modes; // supported AA modes of the current adapter
|
2010-06-19 01:02:43 +00:00
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
static bool s_bgra_textures_supported;
|
|
|
|
static bool s_allow_tearing_supported;
|
|
|
|
|
|
|
|
constexpr UINT NUM_SUPPORTED_FEATURE_LEVELS = 3;
|
|
|
|
constexpr D3D_FEATURE_LEVEL supported_feature_levels[NUM_SUPPORTED_FEATURE_LEVELS] = {
|
|
|
|
D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0};
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-11-23 19:58:02 +00:00
|
|
|
HRESULT LoadDXGI()
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_dxgi_dll_ref++ > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_dxgi_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
2017-11-19 18:23:56 +00:00
|
|
|
s_dxgi_dll = LoadLibraryA("dxgi.dll");
|
|
|
|
if (!s_dxgi_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
MessageBoxA(nullptr, "Failed to load dxgi.dll", "Critical error", MB_OK | MB_ICONERROR);
|
2017-11-19 18:23:56 +00:00
|
|
|
--s_dxgi_dll_ref;
|
2016-06-24 08:43:46 +00:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
2017-09-03 11:55:07 +00:00
|
|
|
|
|
|
|
// Even though we use IDXGIFactory2 we use CreateDXGIFactory1 to create it to maintain
|
|
|
|
// compatibility with Windows 7
|
2017-11-19 18:23:56 +00:00
|
|
|
PCreateDXGIFactory = (CREATEDXGIFACTORY)GetProcAddress(s_dxgi_dll, "CreateDXGIFactory1");
|
2016-06-24 08:43:46 +00:00
|
|
|
if (PCreateDXGIFactory == nullptr)
|
2017-09-03 11:55:07 +00:00
|
|
|
MessageBoxA(nullptr, "GetProcAddress failed for CreateDXGIFactory1!", "Critical error",
|
2016-06-24 08:43:46 +00:00
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
|
|
|
|
return S_OK;
|
2010-11-21 15:34:04 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 19:58:02 +00:00
|
|
|
HRESULT LoadD3D()
|
2010-11-21 15:34:04 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3d_dll_ref++ > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3d_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
2017-11-19 18:23:56 +00:00
|
|
|
s_d3d_dll = LoadLibraryA("d3d11.dll");
|
|
|
|
if (!s_d3d_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
MessageBoxA(nullptr, "Failed to load d3d11.dll", "Critical error", MB_OK | MB_ICONERROR);
|
2017-11-19 18:23:56 +00:00
|
|
|
--s_d3d_dll_ref;
|
2016-06-24 08:43:46 +00:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
2017-11-19 18:23:56 +00:00
|
|
|
s_d3d11_create_device = (D3D11CREATEDEVICE)GetProcAddress(s_d3d_dll, "D3D11CreateDevice");
|
|
|
|
if (s_d3d11_create_device == nullptr)
|
2016-06-24 08:43:46 +00:00
|
|
|
MessageBoxA(nullptr, "GetProcAddress failed for D3D11CreateDevice!", "Critical error",
|
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
|
|
|
|
return S_OK;
|
2010-11-21 15:34:04 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
HRESULT LoadD3DCompiler()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3dcompiler_dll_ref++ > 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3d_compiler_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
return S_OK;
|
|
|
|
|
2017-08-02 09:36:45 +00:00
|
|
|
// The older version of the D3D compiler cannot compile our ubershaders without various
|
|
|
|
// graphical issues. D3DCOMPILER_DLL_A should point to d3dcompiler_47.dll, so if this fails
|
|
|
|
// to load, inform the user that they need to update their system.
|
2017-11-19 18:23:56 +00:00
|
|
|
s_d3d_compiler_dll = LoadLibraryA(D3DCOMPILER_DLL_A);
|
|
|
|
if (!s_d3d_compiler_dll)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-08-02 09:36:45 +00:00
|
|
|
PanicAlertT("Failed to load %s. If you are using Windows 7, try installing the "
|
|
|
|
"KB4019990 update package.",
|
|
|
|
D3DCOMPILER_DLL_A);
|
|
|
|
return E_FAIL;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
s_d3d_reflect = (D3DREFLECT)GetProcAddress(s_d3d_compiler_dll, "D3DReflect");
|
|
|
|
if (s_d3d_reflect == nullptr)
|
2016-06-24 08:43:46 +00:00
|
|
|
MessageBoxA(nullptr, "GetProcAddress failed for D3DReflect!", "Critical error",
|
|
|
|
MB_OK | MB_ICONERROR);
|
2017-11-19 18:23:56 +00:00
|
|
|
PD3DCompile = (pD3DCompile)GetProcAddress(s_d3d_compiler_dll, "D3DCompile");
|
2016-06-24 08:43:46 +00:00
|
|
|
if (PD3DCompile == nullptr)
|
|
|
|
MessageBoxA(nullptr, "GetProcAddress failed for D3DCompile!", "Critical error",
|
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
|
|
|
|
return S_OK;
|
2011-02-26 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
2010-11-23 19:58:02 +00:00
|
|
|
void UnloadDXGI()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (!s_dxgi_dll_ref)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2017-11-19 18:23:56 +00:00
|
|
|
if (--s_dxgi_dll_ref != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_dxgi_dll)
|
|
|
|
FreeLibrary(s_dxgi_dll);
|
|
|
|
s_dxgi_dll = nullptr;
|
2016-06-24 08:43:46 +00:00
|
|
|
PCreateDXGIFactory = nullptr;
|
2010-11-23 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void UnloadD3D()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (!s_d3d_dll_ref)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2017-11-19 18:23:56 +00:00
|
|
|
if (--s_d3d_dll_ref != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3d_dll)
|
|
|
|
FreeLibrary(s_d3d_dll);
|
|
|
|
s_d3d_dll = nullptr;
|
|
|
|
s_d3d11_create_device = nullptr;
|
2010-11-23 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
2011-02-26 23:41:02 +00:00
|
|
|
void UnloadD3DCompiler()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (!s_d3dcompiler_dll_ref)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
2017-11-19 18:23:56 +00:00
|
|
|
if (--s_d3dcompiler_dll_ref != 0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_d3d_compiler_dll)
|
|
|
|
FreeLibrary(s_d3d_compiler_dll);
|
|
|
|
s_d3d_compiler_dll = nullptr;
|
|
|
|
s_d3d_reflect = nullptr;
|
2011-02-26 23:41:02 +00:00
|
|
|
}
|
|
|
|
|
2011-03-30 07:17:23 +00:00
|
|
|
std::vector<DXGI_SAMPLE_DESC> EnumAAModes(IDXGIAdapter* adapter)
|
2010-11-23 19:58:02 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
std::vector<DXGI_SAMPLE_DESC> _aa_modes;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
ID3D11Device* _device;
|
|
|
|
ID3D11DeviceContext* _context;
|
|
|
|
D3D_FEATURE_LEVEL feat_level;
|
2017-11-19 18:23:56 +00:00
|
|
|
HRESULT hr = s_d3d11_create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0,
|
|
|
|
supported_feature_levels, NUM_SUPPORTED_FEATURE_LEVELS,
|
|
|
|
D3D11_SDK_VERSION, &_device, &feat_level, &_context);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr) || feat_level == D3D_FEATURE_LEVEL_10_0)
|
|
|
|
{
|
|
|
|
DXGI_SAMPLE_DESC desc;
|
|
|
|
desc.Count = 1;
|
|
|
|
desc.Quality = 0;
|
|
|
|
_aa_modes.push_back(desc);
|
|
|
|
SAFE_RELEASE(_context);
|
|
|
|
SAFE_RELEASE(_device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int samples = 0; samples < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; ++samples)
|
|
|
|
{
|
|
|
|
UINT quality_levels = 0;
|
|
|
|
_device->CheckMultisampleQualityLevels(DXGI_FORMAT_R8G8B8A8_UNORM, samples, &quality_levels);
|
|
|
|
|
|
|
|
DXGI_SAMPLE_DESC desc;
|
|
|
|
desc.Count = samples;
|
|
|
|
desc.Quality = 0;
|
|
|
|
|
|
|
|
if (quality_levels > 0)
|
|
|
|
_aa_modes.push_back(desc);
|
|
|
|
}
|
|
|
|
_context->Release();
|
|
|
|
_device->Release();
|
|
|
|
}
|
|
|
|
return _aa_modes;
|
2010-11-23 19:58:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-22 12:38:09 +00:00
|
|
|
D3D_FEATURE_LEVEL GetFeatureLevel(IDXGIAdapter* adapter)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
D3D_FEATURE_LEVEL feat_level = D3D_FEATURE_LEVEL_9_1;
|
2017-11-19 18:23:56 +00:00
|
|
|
s_d3d11_create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, supported_feature_levels,
|
|
|
|
NUM_SUPPORTED_FEATURE_LEVELS, D3D11_SDK_VERSION, nullptr, &feat_level,
|
|
|
|
nullptr);
|
2016-06-24 08:43:46 +00:00
|
|
|
return feat_level;
|
2013-07-22 12:38:09 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 11:30:57 +00:00
|
|
|
static bool SupportsS3TCTextures(ID3D11Device* dev)
|
2017-04-16 10:47:17 +00:00
|
|
|
{
|
|
|
|
UINT bc1_support, bc2_support, bc3_support;
|
2017-06-07 11:30:57 +00:00
|
|
|
if (FAILED(dev->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &bc1_support)) ||
|
|
|
|
FAILED(dev->CheckFormatSupport(DXGI_FORMAT_BC2_UNORM, &bc2_support)) ||
|
|
|
|
FAILED(dev->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;
|
|
|
|
}
|
|
|
|
|
2017-07-27 12:00:04 +00:00
|
|
|
static bool SupportsBPTCTextures(ID3D11Device* dev)
|
|
|
|
{
|
|
|
|
// Currently, we only care about BC7. This could be extended to BC6H in the future.
|
|
|
|
UINT bc7_support;
|
|
|
|
if (FAILED(dev->CheckFormatSupport(DXGI_FORMAT_BC7_UNORM, &bc7_support)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (bc7_support & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0;
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
static bool CreateSwapChainTextures()
|
2010-11-21 15:34:04 +00:00
|
|
|
{
|
2018-01-26 06:23:24 +00:00
|
|
|
ID3D11Texture2D* buf;
|
|
|
|
HRESULT hr = swapchain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&buf);
|
|
|
|
CHECK(SUCCEEDED(hr), "GetBuffer for swap chain failed with HRESULT %08X", hr);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
s_backbuf = new D3DTexture2D(buf, D3D11_BIND_RENDER_TARGET);
|
|
|
|
SAFE_RELEASE(buf);
|
|
|
|
SetDebugObjectName(s_backbuf->GetTex(), "backbuffer texture");
|
|
|
|
SetDebugObjectName(s_backbuf->GetRTV(), "backbuffer render target view");
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
static bool CreateSwapChain(HWND hWnd)
|
|
|
|
{
|
|
|
|
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc = {};
|
|
|
|
swap_chain_desc.BufferCount = 2;
|
|
|
|
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
|
|
|
swap_chain_desc.SampleDesc.Count = 1;
|
|
|
|
swap_chain_desc.SampleDesc.Quality = 0;
|
|
|
|
swap_chain_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
swap_chain_desc.Scaling = DXGI_SCALING_STRETCH;
|
|
|
|
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
|
|
|
swap_chain_desc.Stereo = g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer;
|
|
|
|
|
|
|
|
// This flag is necessary if we want to use a flip-model swapchain without locking the framerate
|
|
|
|
swap_chain_desc.Flags = s_allow_tearing_supported ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
|
|
|
|
|
|
|
|
HRESULT hr = s_dxgi_factory->CreateSwapChainForHwnd(device, hWnd, &swap_chain_desc, nullptr,
|
|
|
|
nullptr, &swapchain);
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
// Flip-model discard swapchains aren't supported on Windows 8, so here we fall back to
|
|
|
|
// a sequential swapchain
|
|
|
|
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
|
|
|
|
hr = s_dxgi_factory->CreateSwapChainForHwnd(device, hWnd, &swap_chain_desc, nullptr, nullptr,
|
|
|
|
&swapchain);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
// Flip-model swapchains aren't supported on Windows 7, so here we fall back to a legacy
|
|
|
|
// BitBlt-model swapchain
|
|
|
|
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
|
|
|
hr = s_dxgi_factory->CreateSwapChainForHwnd(device, hWnd, &swap_chain_desc, nullptr, nullptr,
|
|
|
|
&swapchain);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create swap chain with HRESULT %08X", hr);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CreateSwapChainTextures())
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(swapchain);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT Create(HWND wnd)
|
|
|
|
{
|
|
|
|
HRESULT hr = LoadDXGI();
|
2016-06-24 08:43:46 +00:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
hr = LoadD3D();
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
hr = LoadD3DCompiler();
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
UnloadDXGI();
|
|
|
|
UnloadD3D();
|
|
|
|
UnloadD3DCompiler();
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
hr = PCreateDXGIFactory(__uuidof(IDXGIFactory2), (void**)&s_dxgi_factory);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
MessageBox(wnd, _T("Failed to create IDXGIFactory object"), _T("Dolphin Direct3D 11 backend"),
|
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
|
2017-06-25 18:57:46 +00:00
|
|
|
IDXGIAdapter* adapter;
|
2018-01-26 06:23:24 +00:00
|
|
|
hr = s_dxgi_factory->EnumAdapters(g_ActiveConfig.iAdapter, &adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
// try using the first one
|
2018-01-26 06:23:24 +00:00
|
|
|
hr = s_dxgi_factory->EnumAdapters(0, &adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
MessageBox(wnd, _T("Failed to enumerate adapters"), _T("Dolphin Direct3D 11 backend"),
|
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get supported AA modes
|
2017-11-19 18:23:56 +00:00
|
|
|
s_aa_modes = EnumAAModes(adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (std::find_if(s_aa_modes.begin(), s_aa_modes.end(), [](const DXGI_SAMPLE_DESC& desc) {
|
2016-06-24 08:43:46 +00:00
|
|
|
return desc.Count == g_Config.iMultisamples;
|
2017-11-19 18:23:56 +00:00
|
|
|
}) == s_aa_modes.end())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-06-07 11:30:39 +00:00
|
|
|
Config::SetCurrent(Config::GFX_MSAA, UINT32_C(1));
|
2016-06-24 08:43:46 +00:00
|
|
|
UpdateActiveConfig();
|
|
|
|
}
|
|
|
|
|
2017-09-03 15:33:12 +00:00
|
|
|
// Check support for allow tearing, we query the interface for backwards compatibility
|
|
|
|
UINT allow_tearing = FALSE;
|
|
|
|
IDXGIFactory5* factory5;
|
2018-01-26 06:23:24 +00:00
|
|
|
hr = s_dxgi_factory->QueryInterface(&factory5);
|
2017-09-03 15:33:12 +00:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
hr = factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING, &allow_tearing,
|
|
|
|
sizeof(allow_tearing));
|
|
|
|
factory5->Release();
|
|
|
|
}
|
2017-11-19 18:23:56 +00:00
|
|
|
s_allow_tearing_supported = SUCCEEDED(hr) && allow_tearing;
|
2017-09-03 15:33:12 +00:00
|
|
|
|
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.
|
2017-04-29 14:39:52 +00:00
|
|
|
if (g_Config.bEnableValidationLayer)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
hr = s_d3d11_create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, D3D11_CREATE_DEVICE_DEBUG,
|
|
|
|
supported_feature_levels, NUM_SUPPORTED_FEATURE_LEVELS,
|
|
|
|
D3D11_SDK_VERSION, &device, &s_featlevel, &context);
|
2017-06-25 18:57:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Debugbreak on D3D error
|
2017-11-19 18:23:56 +00:00
|
|
|
if (SUCCEEDED(hr) && SUCCEEDED(device->QueryInterface(__uuidof(ID3D11Debug), (void**)&s_debug)))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
ID3D11InfoQueue* infoQueue = nullptr;
|
2017-11-19 18:23:56 +00:00
|
|
|
if (SUCCEEDED(s_debug->QueryInterface(__uuidof(ID3D11InfoQueue), (void**)&infoQueue)))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
|
|
|
|
infoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
|
|
|
|
|
|
|
|
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;
|
|
|
|
infoQueue->AddStorageFilterEntries(&filter);
|
|
|
|
infoQueue->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-29 14:39:52 +00:00
|
|
|
if (!g_Config.bEnableValidationLayer || FAILED(hr))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
hr = s_d3d11_create_device(adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0,
|
|
|
|
supported_feature_levels, NUM_SUPPORTED_FEATURE_LEVELS,
|
|
|
|
D3D11_SDK_VERSION, &device, &s_featlevel, &context);
|
2017-07-09 23:02:29 +00:00
|
|
|
}
|
2017-06-25 18:57:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
SAFE_RELEASE(adapter);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
if (FAILED(hr) || (wnd && !CreateSwapChain(wnd)))
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
MessageBox(
|
|
|
|
wnd,
|
|
|
|
_T("Failed to initialize Direct3D.\nMake sure your video card supports at least D3D 10.0"),
|
|
|
|
_T("Dolphin Direct3D 11 backend"), MB_OK | MB_ICONERROR);
|
|
|
|
SAFE_RELEASE(device);
|
|
|
|
SAFE_RELEASE(context);
|
2018-01-26 06:23:24 +00:00
|
|
|
SAFE_RELEASE(s_dxgi_factory);
|
2016-06-24 08:43:46 +00:00
|
|
|
return E_FAIL;
|
|
|
|
}
|
|
|
|
|
2017-09-03 06:33:47 +00:00
|
|
|
hr = device->QueryInterface<ID3D11Device1>(&device1);
|
|
|
|
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
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
// BGRA textures are easier to deal with in TextureCache, but might not be supported
|
|
|
|
UINT format_support;
|
|
|
|
device->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &format_support);
|
|
|
|
s_bgra_textures_supported = (format_support & D3D11_FORMAT_SUPPORT_TEXTURE2D) != 0;
|
|
|
|
g_Config.backend_info.bSupportsST3CTextures = SupportsS3TCTextures(device);
|
|
|
|
g_Config.backend_info.bSupportsBPTCTextures = SupportsBPTCTextures(device);
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// prevent DXGI from responding to Alt+Enter, unfortunately DXGI_MWA_NO_ALT_ENTER
|
|
|
|
// does not work so we disable all monitoring of window messages. However this
|
|
|
|
// may make it more difficult for DXGI to handle display mode changes.
|
2018-01-26 06:23:24 +00:00
|
|
|
hr = s_dxgi_factory->MakeWindowAssociation(wnd, DXGI_MWA_NO_WINDOW_CHANGES);
|
2016-06-24 08:43:46 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
MessageBox(wnd, _T("Failed to associate the window"), _T("Dolphin Direct3D 11 backend"),
|
|
|
|
MB_OK | MB_ICONERROR);
|
|
|
|
|
2017-09-02 18:22:18 +00:00
|
|
|
SetDebugObjectName(context, "device context");
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
stateman = new StateManager;
|
|
|
|
return S_OK;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Close()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// we can't release the swapchain while in fullscreen.
|
2018-01-26 06:23:24 +00:00
|
|
|
if (swapchain)
|
|
|
|
swapchain->SetFullscreenState(false, nullptr);
|
2014-06-20 01:45:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// release all bound resources
|
|
|
|
context->ClearState();
|
2017-11-19 18:23:56 +00:00
|
|
|
SAFE_RELEASE(s_backbuf);
|
2018-01-26 06:23:24 +00:00
|
|
|
SAFE_RELEASE(swapchain);
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_DELETE(stateman);
|
|
|
|
context->Flush(); // immediately destroy device objects
|
2011-06-11 19:37:21 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
SAFE_RELEASE(context);
|
2017-09-03 06:33:47 +00:00
|
|
|
SAFE_RELEASE(device1);
|
2016-06-24 08:43:46 +00:00
|
|
|
ULONG references = device->Release();
|
2015-02-09 12:00:42 +00:00
|
|
|
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_debug)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
--references; // the debug interface increases the refcount of the device, subtract that.
|
|
|
|
if (references)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
2017-11-19 18:23:56 +00:00
|
|
|
SAFE_RELEASE(s_debug)
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2015-02-09 12:00:42 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (references)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Unreleased references: %i.", references);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NOTICE_LOG(VIDEO, "Successfully released all device references!");
|
|
|
|
}
|
|
|
|
device = nullptr;
|
|
|
|
|
|
|
|
// unload DLLs
|
|
|
|
UnloadD3D();
|
|
|
|
UnloadDXGI();
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
const char* VertexShaderVersionString()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_featlevel == D3D_FEATURE_LEVEL_11_0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "vs_5_0";
|
2017-11-19 18:23:56 +00:00
|
|
|
else if (s_featlevel == D3D_FEATURE_LEVEL_10_1)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "vs_4_1";
|
|
|
|
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/
|
|
|
|
return "vs_4_0";
|
2010-11-27 11:11:05 +00:00
|
|
|
}
|
|
|
|
|
2011-03-14 09:38:29 +00:00
|
|
|
const char* GeometryShaderVersionString()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_featlevel == D3D_FEATURE_LEVEL_11_0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "gs_5_0";
|
2017-11-19 18:23:56 +00:00
|
|
|
else if (s_featlevel == D3D_FEATURE_LEVEL_10_1)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "gs_4_1";
|
|
|
|
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/
|
|
|
|
return "gs_4_0";
|
2011-03-14 09:38:29 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
const char* PixelShaderVersionString()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
if (s_featlevel == D3D_FEATURE_LEVEL_11_0)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "ps_5_0";
|
2017-11-19 18:23:56 +00:00
|
|
|
else if (s_featlevel == D3D_FEATURE_LEVEL_10_1)
|
2016-06-24 08:43:46 +00:00
|
|
|
return "ps_4_1";
|
|
|
|
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/
|
|
|
|
return "ps_4_0";
|
2010-11-27 11:11:05 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2018-02-09 12:59:51 +00:00
|
|
|
const char* ComputeShaderVersionString()
|
|
|
|
{
|
|
|
|
if (s_featlevel == D3D_FEATURE_LEVEL_11_0)
|
|
|
|
return "cs_5_0";
|
|
|
|
else if (s_featlevel == D3D_FEATURE_LEVEL_10_1)
|
|
|
|
return "cs_4_1";
|
|
|
|
else /*if(featlevel == D3D_FEATURE_LEVEL_10_0)*/
|
|
|
|
return "cs_4_0";
|
|
|
|
}
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
D3DTexture2D* GetBackBuffer()
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
return s_backbuf;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
bool BGRATexturesSupported()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
return s_bgra_textures_supported;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-06-19 01:02:43 +00:00
|
|
|
|
2017-09-03 15:33:12 +00:00
|
|
|
bool AllowTearingSupported()
|
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
return s_allow_tearing_supported;
|
2017-09-03 15:33:12 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// Returns the maximum width/height of a texture. This value only depends upon the feature level in
|
|
|
|
// DX11
|
2017-03-10 13:40:52 +00:00
|
|
|
u32 GetMaxTextureSize(D3D_FEATURE_LEVEL feature_level)
|
2010-07-11 16:26:46 +00:00
|
|
|
{
|
2017-03-10 13:40:52 +00:00
|
|
|
switch (feature_level)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
case D3D_FEATURE_LEVEL_11_0:
|
|
|
|
return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION;
|
2010-07-11 16:26:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
case D3D_FEATURE_LEVEL_10_1:
|
|
|
|
case D3D_FEATURE_LEVEL_10_0:
|
|
|
|
return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION;
|
2010-07-11 16:26:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
case D3D_FEATURE_LEVEL_9_3:
|
|
|
|
return 4096;
|
2010-07-11 16:26:46 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
case D3D_FEATURE_LEVEL_9_2:
|
|
|
|
case D3D_FEATURE_LEVEL_9_1:
|
|
|
|
return 2048;
|
2010-07-17 11:42:28 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2010-07-11 16:26:46 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
void Reset(HWND new_wnd)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2017-11-19 18:23:56 +00:00
|
|
|
SAFE_RELEASE(s_backbuf);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
if (swapchain)
|
|
|
|
{
|
|
|
|
if (GetFullscreenState())
|
|
|
|
swapchain->SetFullscreenState(FALSE, nullptr);
|
|
|
|
SAFE_RELEASE(swapchain);
|
|
|
|
}
|
2017-09-03 15:33:12 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
if (new_wnd)
|
|
|
|
CreateSwapChain(new_wnd);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
void ResizeSwapChain()
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(s_backbuf);
|
|
|
|
const UINT swap_chain_flags = AllowTearingSupported() ? DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING : 0;
|
|
|
|
swapchain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_R8G8B8A8_UNORM, swap_chain_flags);
|
|
|
|
if (!CreateSwapChainTextures())
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2018-01-26 06:23:24 +00:00
|
|
|
PanicAlert("Failed to get swap chain textures");
|
|
|
|
SAFE_RELEASE(swapchain);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Present()
|
|
|
|
{
|
2017-07-09 23:02:05 +00:00
|
|
|
UINT present_flags = 0;
|
2017-09-03 15:33:12 +00:00
|
|
|
|
|
|
|
// When using sync interval 0, it is recommended to always pass the tearing
|
|
|
|
// flag when it is supported, even when presenting in windowed mode.
|
|
|
|
// However, this flag cannot be used if the app is in fullscreen mode as a
|
|
|
|
// result of calling SetFullscreenState.
|
|
|
|
if (AllowTearingSupported() && !g_ActiveConfig.IsVSync() && !GetFullscreenState())
|
|
|
|
present_flags |= DXGI_PRESENT_ALLOW_TEARING;
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
if (swapchain->IsTemporaryMonoSupported() && g_ActiveConfig.stereo_mode != StereoMode::QuadBuffer)
|
2017-11-19 18:23:56 +00:00
|
|
|
{
|
2017-09-03 15:33:12 +00:00
|
|
|
present_flags |= DXGI_PRESENT_STEREO_TEMPORARY_MONO;
|
2017-11-19 18:23:56 +00:00
|
|
|
}
|
2017-06-26 19:47:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// TODO: Is 1 the correct value for vsyncing?
|
2018-01-26 06:23:24 +00:00
|
|
|
swapchain->Present(static_cast<UINT>(g_ActiveConfig.IsVSync()), present_flags);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2014-07-26 11:00:49 +00:00
|
|
|
HRESULT SetFullscreenState(bool enable_fullscreen)
|
|
|
|
{
|
2018-01-26 06:23:24 +00:00
|
|
|
return swapchain->SetFullscreenState(enable_fullscreen, nullptr);
|
2014-07-26 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 00:41:38 +00:00
|
|
|
bool GetFullscreenState()
|
2014-07-26 11:00:49 +00:00
|
|
|
{
|
2016-11-09 00:41:38 +00:00
|
|
|
BOOL state = FALSE;
|
2018-01-26 06:23:24 +00:00
|
|
|
swapchain->GetFullscreenState(&state, nullptr);
|
2016-11-09 00:41:38 +00:00
|
|
|
return !!state;
|
2014-07-26 11:00:49 +00:00
|
|
|
}
|
|
|
|
|
2017-09-02 18:22:18 +00:00
|
|
|
void SetDebugObjectName(ID3D11DeviceChild* resource, const char* name)
|
|
|
|
{
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
if (resource)
|
|
|
|
resource->SetPrivateData(WKPDID_D3DDebugObjectName, (UINT)(name ? strlen(name) : 0), name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetDebugObjectName(ID3D11DeviceChild* resource)
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
|
|
if (resource)
|
|
|
|
{
|
|
|
|
UINT size = 0;
|
|
|
|
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, nullptr); // get required size
|
|
|
|
name.resize(size);
|
|
|
|
resource->GetPrivateData(WKPDID_D3DDebugObjectName, &size, const_cast<char*>(name.data()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
} // namespace D3D
|
|
|
|
|
2011-02-14 02:18:03 +00:00
|
|
|
} // namespace DX11
|