From 8ee3cd52e89fab0d78d18fc2b99a5132712e25c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 15 Apr 2018 10:53:07 +0200 Subject: [PATCH] D3D11: Allow the user to select rendering device. --- Core/Config.cpp | 3 ++ Core/Config.h | 3 ++ UI/GameSettingsScreen.cpp | 8 +++-- Windows/GPU/D3D11Context.cpp | 53 +++++++++++++++++++++--------- Windows/GPU/D3D11Context.h | 5 ++- ext/native/thin3d/thin3d_create.h | 4 ++- ext/native/thin3d/thin3d_d3d11.cpp | 18 +++++----- 7 files changed, 65 insertions(+), 29 deletions(-) diff --git a/Core/Config.cpp b/Core/Config.cpp index 9350941f3c..04a7b488e6 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -508,6 +508,9 @@ static ConfigSetting graphicsSettings[] = { ConfigSetting("ShowFPSCounter", &g_Config.iShowFPSCounter, 0, true, true), ReportedConfigSetting("GraphicsBackend", &g_Config.iGPUBackend, &DefaultGPUBackend), ConfigSetting("VulkanDevice", &g_Config.VulkanDevice, "", true, false), +#ifdef _WIN32 + ConfigSetting("D3D11Device", &g_Config.D3D11Device, "", true, false), +#endif ReportedConfigSetting("RenderingMode", &g_Config.iRenderingMode, &DefaultRenderingMode, true, true), ConfigSetting("SoftwareRenderer", &g_Config.bSoftwareRendering, false, true, true), ReportedConfigSetting("HardwareTransform", &g_Config.bHardwareTransform, true, true, true), diff --git a/Core/Config.h b/Core/Config.h index 3a022646c9..24f963edee 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -156,6 +156,9 @@ public: // We have separate device parameters for each backend so it doesn't get erased if you switch backends. // If not set, will use the "best" device. std::string VulkanDevice; +#ifdef _WIN32 + std::string D3D11Device; +#endif bool bSoftwareRendering; bool bHardwareTransform; // only used in the GLES backend bool bSoftwareSkinning; // may speed up some games diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index be1d8ef3e6..f41d8b4fdb 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -203,13 +203,17 @@ void GameSettingsScreen::CreateViews() { } #endif Draw::DrawContext *draw = screenManager()->getDrawContext(); - if (draw->GetDeviceList().size() > 0) { + if (draw->GetDeviceList().size() > 1) { // Some backends don't support switching so no point in showing multiple devices. std::string *deviceNameSetting = nullptr; if (g_Config.iGPUBackend == (int)GPUBackend::VULKAN) { deviceNameSetting = &g_Config.VulkanDevice; } - +#ifdef _WIN32 + if (g_Config.iGPUBackend == (int)GPUBackend::DIRECT3D11) { + deviceNameSetting = &g_Config.D3D11Device; + } +#endif if (deviceNameSetting) { PopupMultiChoiceDynamic *deviceChoice = graphicsSettings->Add(new PopupMultiChoiceDynamic(deviceNameSetting, gr->T("Device"), draw->GetDeviceList(), nullptr, screenManager())); deviceChoice->OnChoice.Handle(this, &GameSettingsScreen::OnRenderingBackend); diff --git a/Windows/GPU/D3D11Context.cpp b/Windows/GPU/D3D11Context.cpp index 28b1540af3..4470464582 100644 --- a/Windows/GPU/D3D11Context.cpp +++ b/Windows/GPU/D3D11Context.cpp @@ -2,6 +2,7 @@ #include "Common/CommonWindows.h" #include +#include #include #include "base/logging.h" @@ -22,6 +23,12 @@ #define __uuidof(type) IID_##type #endif +#ifndef DXGI_ERROR_NOT_FOUND +#define _FACDXGI 0x87a +#define MAKE_DXGI_HRESULT(code) MAKE_HRESULT(1, _FACDXGI, code) +#define DXGI_ERROR_NOT_FOUND MAKE_DXGI_HRESULT(2) +#endif + #if PPSSPP_PLATFORM(UWP) #error This file should not be compiled for UWP. #endif @@ -41,7 +48,7 @@ void D3D11Context::SwapInterval(int interval) { // Dummy } -HRESULT D3D11Context::CreateTheDevice() { +HRESULT D3D11Context::CreateTheDevice(IDXGIAdapter *adapter) { bool windowed = true; #ifdef _DEBUG UINT createDeviceFlags = D3D11_CREATE_DEVICE_DEBUG; @@ -70,19 +77,13 @@ HRESULT D3D11Context::CreateTheDevice() { const UINT numFeatureLevels = ARRAYSIZE(featureLevels); HRESULT hr = S_OK; - // Temporarily commenting out until we can dynamically load D3D11CreateDevice. - for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++) { - driverType_ = driverTypes[driverTypeIndex]; - hr = ptr_D3D11CreateDevice(nullptr, driverType_, nullptr, createDeviceFlags, (D3D_FEATURE_LEVEL *)featureLevels, numFeatureLevels, + D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_UNKNOWN; + hr = ptr_D3D11CreateDevice(adapter, driverType, nullptr, createDeviceFlags, (D3D_FEATURE_LEVEL *)featureLevels, numFeatureLevels, + D3D11_SDK_VERSION, &device_, &featureLevel_, &context_); + if (hr == E_INVALIDARG) { + // DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it + hr = ptr_D3D11CreateDevice(adapter, driverType, nullptr, createDeviceFlags, (D3D_FEATURE_LEVEL *)&featureLevels[3], numFeatureLevels - 3, D3D11_SDK_VERSION, &device_, &featureLevel_, &context_); - - if (hr == E_INVALIDARG) { - // DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it - hr = ptr_D3D11CreateDevice(nullptr, driverType_, nullptr, createDeviceFlags, (D3D_FEATURE_LEVEL *)&featureLevels[3], numFeatureLevels - 3, - D3D11_SDK_VERSION, &device_, &featureLevel_, &context_); - } - if (SUCCEEDED(hr)) - break; } return hr; } @@ -99,8 +100,30 @@ bool D3D11Context::Init(HINSTANCE hInst, HWND wnd, std::string *error_message) { LoadD3D11Error result = LoadD3D11(); HRESULT hr = E_FAIL; + std::vector adapterNames; if (result == LoadD3D11Error::SUCCESS) { - hr = CreateTheDevice(); + std::vector adapters; + int chosenAdapter = 0; + + IDXGIFactory * pFactory = nullptr; + ptr_CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&pFactory); + + IDXGIAdapter *pAdapter; + for (UINT i = 0; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; i++) { + adapters.push_back(pAdapter); + DXGI_ADAPTER_DESC desc; + pAdapter->GetDesc(&desc); + std::string str = ConvertWStringToUTF8(desc.Description); + adapterNames.push_back(str); + if (str == g_Config.D3D11Device) { + chosenAdapter = i; + } + } + + hr = CreateTheDevice(adapters[chosenAdapter]); + for (int i = 0; i < (int)adapters.size(); i++) { + adapters[i]->Release(); + } } if (FAILED(hr)) { @@ -146,7 +169,7 @@ bool D3D11Context::Init(HINSTANCE hInst, HWND wnd, std::string *error_message) { } #endif - draw_ = Draw::T3DCreateD3D11Context(device_, context_, device1_, context1_, featureLevel_, hWnd_); + draw_ = Draw::T3DCreateD3D11Context(device_, context_, device1_, context1_, featureLevel_, hWnd_, adapterNames); SetGPUBackend(GPUBackend::DIRECT3D11); bool success = draw_->CreatePresets(); // If we can run D3D11, there's a compiler installed. I think. assert(success); diff --git a/Windows/GPU/D3D11Context.h b/Windows/GPU/D3D11Context.h index b0f4247628..88f1547295 100644 --- a/Windows/GPU/D3D11Context.h +++ b/Windows/GPU/D3D11Context.h @@ -40,7 +40,7 @@ public: Draw::DrawContext *GetDrawContext() override { return draw_; } private: - HRESULT CreateTheDevice(); + HRESULT CreateTheDevice(IDXGIAdapter *adapter); void LostBackbuffer(); void GotBackbuffer(); @@ -60,7 +60,6 @@ private: ID3D11InfoQueue *d3dInfoQueue_ = nullptr; #endif - D3D_DRIVER_TYPE driverType_; D3D_FEATURE_LEVEL featureLevel_ = D3D_FEATURE_LEVEL_11_0; int adapterId; HDC hDC; // Private GDI Device Context @@ -68,4 +67,4 @@ private: HMODULE hD3D11; int width; int height; -}; \ No newline at end of file +}; diff --git a/ext/native/thin3d/thin3d_create.h b/ext/native/thin3d/thin3d_create.h index 9d2ea4a844..a89c74ab96 100644 --- a/ext/native/thin3d/thin3d_create.h +++ b/ext/native/thin3d/thin3d_create.h @@ -1,5 +1,7 @@ #pragma once +#include +#include #include "thin3d/thin3d.h" // Separated this stuff into its own file so we don't get Windows.h included if all we want is the thin3d declarations. @@ -30,7 +32,7 @@ DrawContext *T3DCreateGLContext(); #ifdef _WIN32 DrawContext *T3DCreateDX9Context(IDirect3D9 *d3d, IDirect3D9Ex *d3dEx, int adapterId, IDirect3DDevice9 *device, IDirect3DDevice9Ex *deviceEx); -DrawContext *T3DCreateD3D11Context(ID3D11Device *device, ID3D11DeviceContext *context, ID3D11Device1 *device1, ID3D11DeviceContext1 *context1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd); +DrawContext *T3DCreateD3D11Context(ID3D11Device *device, ID3D11DeviceContext *context, ID3D11Device1 *device1, ID3D11DeviceContext1 *context1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd, std::vector adapterNames); #endif DrawContext *T3DCreateVulkanContext(VulkanContext *context, bool splitSubmit); diff --git a/ext/native/thin3d/thin3d_d3d11.cpp b/ext/native/thin3d/thin3d_d3d11.cpp index 9a3ce86e5a..f5e0c2da27 100644 --- a/ext/native/thin3d/thin3d_d3d11.cpp +++ b/ext/native/thin3d/thin3d_d3d11.cpp @@ -38,12 +38,15 @@ class D3D11RasterState; class D3D11DrawContext : public DrawContext { public: - D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Device1 *device1, ID3D11DeviceContext1 *deviceContext1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd); + D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Device1 *device1, ID3D11DeviceContext1 *deviceContext1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd, std::vector deviceList); ~D3D11DrawContext(); const DeviceCaps &GetDeviceCaps() const override { return caps_; } + std::vector GetDeviceList() const override { + return deviceList_; + } uint32_t GetSupportedShaderLanguages() const override { return (uint32_t)ShaderLanguage::HLSL_D3D11 | (uint32_t)ShaderLanguage::HLSL_D3D11_BYTECODE; } @@ -210,15 +213,17 @@ private: // System info D3D_FEATURE_LEVEL featureLevel_; std::string adapterDesc_; + std::vector deviceList_; }; -D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Device1 *device1, ID3D11DeviceContext1 *deviceContext1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd) +D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *deviceContext, ID3D11Device1 *device1, ID3D11DeviceContext1 *deviceContext1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd, std::vector deviceList) : device_(device), context_(deviceContext1), device1_(device1), context1_(deviceContext1), featureLevel_(featureLevel), - hWnd_(hWnd) { + hWnd_(hWnd), + deviceList_(deviceList) { // Seems like a fair approximation... caps_.dualSourceBlend = featureLevel_ >= D3D_FEATURE_LEVEL_10_0; @@ -229,7 +234,6 @@ D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *de caps_.framebufferDepthBlitSupported = false; caps_.framebufferDepthCopySupported = true; - D3D11_FEATURE_DATA_D3D11_OPTIONS options{}; HRESULT result = device_->CheckFeatureSupport(D3D11_FEATURE_D3D11_OPTIONS, &options, sizeof(options)); if (SUCCEEDED(result)) { @@ -239,8 +243,6 @@ D3D11DrawContext::D3D11DrawContext(ID3D11Device *device, ID3D11DeviceContext *de // caps_.logicOpSupported = true; } } - // Obtain DXGI factory from device (since we used nullptr for pAdapter above) - IDXGIFactory1* dxgiFactory = nullptr; IDXGIDevice* dxgiDevice = nullptr; IDXGIAdapter* adapter = nullptr; HRESULT hr = device_->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast(&dxgiDevice)); @@ -1552,8 +1554,8 @@ void D3D11DrawContext::GetFramebufferDimensions(Framebuffer *fbo, int *w, int *h } } -DrawContext *T3DCreateD3D11Context(ID3D11Device *device, ID3D11DeviceContext *context, ID3D11Device1 *device1, ID3D11DeviceContext1 *context1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd) { - return new D3D11DrawContext(device, context, device1, context1, featureLevel, hWnd); +DrawContext *T3DCreateD3D11Context(ID3D11Device *device, ID3D11DeviceContext *context, ID3D11Device1 *device1, ID3D11DeviceContext1 *context1, D3D_FEATURE_LEVEL featureLevel, HWND hWnd, std::vector adapterNames) { + return new D3D11DrawContext(device, context, device1, context1, featureLevel, hWnd, adapterNames); } } // namespace Draw