From 3ac25d5e2af686b2e22d0ff3c5360f3dd804484e Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 18 Jan 2018 01:18:22 +0100 Subject: [PATCH] [d3d11] Updated D3D11CreateDevice Since D3D11Device and D3D11DeviceContext share the same reference counter, we can actually create a device if the device pointer is missing. Fixes Homefront. --- src/d3d11/d3d11_main.cpp | 47 ++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/d3d11/d3d11_main.cpp b/src/d3d11/d3d11_main.cpp index d62a84d8..4780e1c3 100644 --- a/src/d3d11/d3d11_main.cpp +++ b/src/d3d11/d3d11_main.cpp @@ -105,34 +105,29 @@ extern "C" { if (pFeatureLevel != nullptr) *pFeatureLevel = fl; - // The documentation is unclear about what exactly should be done if - // the application passes NULL to ppDevice, but a non-NULL pointer to - // ppImmediateContext. In our implementation, the immediate context - // does not hold a strong reference to the device that owns it, so - // if we cannot write back the device, it would be destroyed. - if (ppDevice != nullptr) { - Com dxvkDevice = nullptr; - - const VkPhysicalDeviceFeatures deviceFeatures - = D3D11Device::GetDeviceFeatures(adapter, fl); - - if (FAILED(DXGICreateDevicePrivate(dxvkAdapter.ptr(), &deviceFeatures, &dxvkDevice))) { - Logger::err("D3D11CreateDevice: Failed to create DXGI device"); - return E_FAIL; - } - - Com d3d11Device = new D3D11Device( - dxvkDevice.ptr(), fl, Flags); - - *ppDevice = d3d11Device.ref(); - if (ppImmediateContext != nullptr) - d3d11Device->GetImmediateContext(ppImmediateContext); - return S_OK; - } else { - Logger::warn("D3D11CreateDevice: ppDevice is null"); - return S_OK; + // If we cannot write back either the device or + // the context, don't create the device at all + if (ppDevice == nullptr && ppImmediateContext == nullptr) + return S_FALSE; + + Com dxvkDevice = nullptr; + + const VkPhysicalDeviceFeatures deviceFeatures + = D3D11Device::GetDeviceFeatures(adapter, fl); + + if (FAILED(DXGICreateDevicePrivate(dxvkAdapter.ptr(), &deviceFeatures, &dxvkDevice))) { + Logger::err("D3D11CreateDevice: Failed to create DXGI device"); + return E_FAIL; } + Com d3d11Device = new D3D11Device(dxvkDevice.ptr(), fl, Flags); + + if (ppDevice != nullptr) + *ppDevice = d3d11Device.ref(); + + if (ppImmediateContext != nullptr) + d3d11Device->GetImmediateContext(ppImmediateContext); + return S_OK; } catch (const DxvkError& e) { Logger::err("D3D11CreateDevice: Failed to create D3D11 device"); return E_FAIL;