From cc3918abbc45ccb2716c279a603d1aaee96b974e Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 12 Aug 2019 14:08:50 +0200 Subject: [PATCH] RHI/Windows: Fix launching of MSVC binaries on Windows 7 Dynamically resolve CreateDXGIFactory2() which is not present on the platform. Task-number: QTBUG-76845 Change-Id: I4d15d72633544a8c11d2b78c8736cd20a2afdff1 Reviewed-by: Laszlo Agocs --- src/gui/rhi/qrhid3d11.cpp | 45 ++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/gui/rhi/qrhid3d11.cpp b/src/gui/rhi/qrhid3d11.cpp index 0c5df600a0..c0b13f1cc8 100644 --- a/src/gui/rhi/qrhid3d11.cpp +++ b/src/gui/rhi/qrhid3d11.cpp @@ -157,6 +157,36 @@ static inline Int aligned(Int v, Int byteAlign) return (v + byteAlign - 1) & ~(byteAlign - 1); } +static IDXGIFactory1 *createDXGIFactory2() +{ + IDXGIFactory1 *result = nullptr; + if (QOperatingSystemVersion::current() > QOperatingSystemVersion::Windows7) { + using PtrCreateDXGIFactory2 = HRESULT (WINAPI *)(UINT, REFIID, void **); + QSystemLibrary dxgilib(QStringLiteral("dxgi")); + if (auto createDXGIFactory2 = (PtrCreateDXGIFactory2)dxgilib.resolve("CreateDXGIFactory2")) { + const HRESULT hr = createDXGIFactory2(0, IID_IDXGIFactory2, reinterpret_cast(&result)); + if (FAILED(hr)) { + qWarning("CreateDXGIFactory2() failed to create DXGI factory: %s", qPrintable(comErrorMessage(hr))); + result = nullptr; + } + } else { + qWarning("Unable to resolve CreateDXGIFactory2()"); + } + } + return result; +} + +static IDXGIFactory1 *createDXGIFactory1() +{ + IDXGIFactory1 *result = nullptr; + const HRESULT hr = CreateDXGIFactory1(IID_IDXGIFactory1, reinterpret_cast(&result)); + if (FAILED(hr)) { + qWarning("CreateDXGIFactory1() failed to create DXGI factory: %s", qPrintable(comErrorMessage(hr))); + result = nullptr; + } + return result; +} + bool QRhiD3D11::create(QRhi::Flags flags) { Q_UNUSED(flags); @@ -165,19 +195,14 @@ bool QRhiD3D11::create(QRhi::Flags flags) if (debugLayer) devFlags |= D3D11_CREATE_DEVICE_DEBUG; - HRESULT hr; -#if !defined(Q_CC_MINGW) - hasDxgi2 = QOperatingSystemVersion::current() > QOperatingSystemVersion::Windows7; - if (hasDxgi2) - hr = CreateDXGIFactory2(0, IID_IDXGIFactory2, reinterpret_cast(&dxgiFactory)); + dxgiFactory = createDXGIFactory2(); + if (dxgiFactory != nullptr) + hasDxgi2 = true; else -#endif - hr = CreateDXGIFactory1(IID_IDXGIFactory1, reinterpret_cast(&dxgiFactory)); + dxgiFactory = createDXGIFactory1(); - if (FAILED(hr)) { - qWarning("Failed to create DXGI factory: %s", qPrintable(comErrorMessage(hr))); + if (dxgiFactory == nullptr) return false; - } if (!importedDevice) { IDXGIAdapter1 *adapterToUse = nullptr;