From 0c99c5c26ebb45f57071fdba5b49bfa2e44e1a36 Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Wed, 6 Jan 2021 00:43:20 +0100 Subject: [PATCH] dx11, dx12: dlls loaded dynamically on demand --- WickedEngine/wiGraphicsDevice_DX11.cpp | 18 +++++- WickedEngine/wiGraphicsDevice_DX12.cpp | 84 ++++++++++++++------------ WickedEngine/wiVersion.cpp | 2 +- 3 files changed, 64 insertions(+), 40 deletions(-) diff --git a/WickedEngine/wiGraphicsDevice_DX11.cpp b/WickedEngine/wiGraphicsDevice_DX11.cpp index e5a676b54..accb959d9 100644 --- a/WickedEngine/wiGraphicsDevice_DX11.cpp +++ b/WickedEngine/wiGraphicsDevice_DX11.cpp @@ -6,8 +6,14 @@ #include "ResourceMapping.h" #include "wiBackLog.h" +#ifdef PLATFORM_UWP +// UWP will use static link + /DELAYLOAD linker feature for the dlls (optionally) #pragma comment(lib,"d3d11.lib") -#pragma comment(lib,"Dxgi.lib") +#define dll_D3D11CreateDevice D3D11CreateDevice +#else +static PFN_D3D11_CREATE_DEVICE dll_D3D11CreateDevice = nullptr; +#endif // PLATFORM_UWP + #pragma comment(lib,"dxguid.lib") #include @@ -1295,6 +1301,14 @@ GraphicsDevice_DX11::GraphicsDevice_DX11(wiPlatform::window_type window, bool fu RESOLUTIONHEIGHT = int(window->Bounds.Height * dpiscale); #endif + +#ifndef PLATFORM_UWP + HMODULE dx11 = LoadLibraryEx(L"d3d11.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + + dll_D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(dx11, "D3D11CreateDevice"); + assert(dll_D3D11CreateDevice != nullptr); +#endif // PLATFORM_UWP + HRESULT hr = E_FAIL; uint32_t createDeviceFlags = 0; @@ -1322,7 +1336,7 @@ GraphicsDevice_DX11::GraphicsDevice_DX11(wiPlatform::window_type window, bool fu for (uint32_t driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++) { driverType = driverTypes[driverTypeIndex]; - hr = D3D11CreateDevice(nullptr, driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &device + hr = dll_D3D11CreateDevice(nullptr, driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &device , &featureLevel, &immediateContext); if (SUCCEEDED(hr)) diff --git a/WickedEngine/wiGraphicsDevice_DX12.cpp b/WickedEngine/wiGraphicsDevice_DX12.cpp index 34b73991b..0c0e32e55 100644 --- a/WickedEngine/wiGraphicsDevice_DX12.cpp +++ b/WickedEngine/wiGraphicsDevice_DX12.cpp @@ -15,20 +15,29 @@ #include #include -#pragma comment(lib,"d3d12.lib") -#pragma comment(lib,"Dxgi.lib") -#pragma comment(lib,"dxguid.lib") - #ifdef _DEBUG #include #endif // _DEBUG -static DxcCreateInstanceProc pfn_DxcCreateInstance = nullptr; - #include #include #include +#ifdef PLATFORM_UWP +// UWP will use static link + /DELAYLOAD linker feature for the dlls (optionally) +#pragma comment(lib,"d3d12.lib") +#pragma comment(lib,"dxgi.lib") +#define dll_CreateDXGIFactory2 CreateDXGIFactory2 +#define dll_D3D12CreateDevice D3D12CreateDevice +#define dll_D3D12SerializeVersionedRootSignature D3D12SerializeVersionedRootSignature +#else +static decltype(&CreateDXGIFactory2) dll_CreateDXGIFactory2 = nullptr; +static PFN_D3D12_CREATE_DEVICE dll_D3D12CreateDevice = nullptr; +static PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE dll_D3D12SerializeVersionedRootSignature = nullptr; +#endif // PLATFORM_UWP + +static DxcCreateInstanceProc dll_DxcCreateInstance = nullptr; + using namespace Microsoft::WRL; namespace wiGraphics @@ -2163,17 +2172,34 @@ using namespace DX12_Internal; RESOLUTIONHEIGHT = int(window->Bounds.Height * dpiscale); #endif + +#ifdef PLATFORM_UWP + HMODULE dxcompiler = LoadPackagedLibrary(L"dxcompiler.dll", 0); +#else + HMODULE dxgi = LoadLibraryEx(L"dxgi.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + HMODULE dx12 = LoadLibraryEx(L"d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + HMODULE dxcompiler = LoadLibrary(L"dxcompiler.dll"); + + dll_CreateDXGIFactory2 = (decltype(&CreateDXGIFactory2))GetProcAddress(dxgi, "CreateDXGIFactory2"); + assert(dll_CreateDXGIFactory2 != nullptr); + + dll_D3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)GetProcAddress(dx12, "D3D12CreateDevice"); + assert(dll_D3D12CreateDevice != nullptr); + + dll_D3D12SerializeVersionedRootSignature = (PFN_D3D12_SERIALIZE_VERSIONED_ROOT_SIGNATURE)GetProcAddress(dx12, "D3D12SerializeVersionedRootSignature"); + assert(dll_D3D12SerializeVersionedRootSignature != nullptr); +#endif // PLATFORM_UWP + + dll_DxcCreateInstance = (DxcCreateInstanceProc)GetProcAddress(dxcompiler, "DxcCreateInstance"); + assert(dll_DxcCreateInstance != nullptr); + HRESULT hr = E_FAIL; #if !defined(PLATFORM_UWP) if (debuglayer) { // Enable the debug layer. - HMODULE dx12 = LoadLibraryEx(L"d3d12.dll", - nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); - auto pD3D12GetDebugInterface = - reinterpret_cast( - GetProcAddress(dx12, "D3D12GetDebugInterface")); + auto pD3D12GetDebugInterface = reinterpret_cast(GetProcAddress(dx12, "D3D12GetDebugInterface")); if (pD3D12GetDebugInterface) { ID3D12Debug* d3dDebug; @@ -2186,7 +2212,8 @@ using namespace DX12_Internal; } } #endif - hr = CreateDXGIFactory2(debuglayer ? DXGI_CREATE_FACTORY_DEBUG : 0, IID_PPV_ARGS(&factory)); + + hr = dll_CreateDXGIFactory2(debuglayer ? DXGI_CREATE_FACTORY_DEBUG : 0, IID_PPV_ARGS(&factory)); if (FAILED(hr)) { std::stringstream ss(""); @@ -2206,7 +2233,7 @@ using namespace DX12_Internal; // ignore software adapter and check device creation succeeds if (!(adapterDesc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) && - SUCCEEDED(D3D12CreateDevice(candidateAdapter.Get(), D3D_FEATURE_LEVEL_12_1, __uuidof(ID3D12Device), nullptr))) + SUCCEEDED(dll_D3D12CreateDevice(candidateAdapter.Get(), D3D_FEATURE_LEVEL_12_1, __uuidof(ID3D12Device), nullptr))) { candidateAdapter.As(&adapter); break; @@ -2219,7 +2246,7 @@ using namespace DX12_Internal; wiPlatform::Exit(); } - hr = D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS(&device)); + hr = dll_D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS(&device)); if (FAILED(hr)) { std::stringstream ss(""); @@ -2278,11 +2305,6 @@ using namespace DX12_Internal; // Create swapchain - - ComPtr pIDXGIFactory; - hr = CreateDXGIFactory1(IID_PPV_ARGS(&pIDXGIFactory)); - assert(SUCCEEDED(hr)); - ComPtr _swapChain; DXGI_SWAP_CHAIN_DESC1 sd = {}; @@ -2307,12 +2329,12 @@ using namespace DX12_Internal; fullscreenDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; // needs to be unspecified for correct fullscreen scaling! fullscreenDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_PROGRESSIVE; fullscreenDesc.Windowed = !fullscreen; - hr = pIDXGIFactory->CreateSwapChainForHwnd(directQueue.Get(), window, &sd, &fullscreenDesc, nullptr, &_swapChain); + hr = factory->CreateSwapChainForHwnd(directQueue.Get(), window, &sd, &fullscreenDesc, nullptr, &_swapChain); #else sd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // All Windows Store apps must use this SwapEffect. sd.Scaling = DXGI_SCALING_ASPECT_RATIO_STRETCH; - hr = pIDXGIFactory->CreateSwapChainForCoreWindow(directQueue.Get(), reinterpret_cast(window.Get()), &sd, nullptr, &_swapChain); + hr = factory->CreateSwapChainForCoreWindow(directQueue.Get(), reinterpret_cast(window.Get()), &sd, nullptr, &_swapChain); #endif if (FAILED(hr)) @@ -2609,18 +2631,6 @@ using namespace DX12_Internal; allocationhandler->queries_occlusion.init(allocationhandler.get(), D3D12_QUERY_HEAP_TYPE_OCCLUSION); allocationhandler->queries_timestamp.init(allocationhandler.get(), D3D12_QUERY_HEAP_TYPE_TIMESTAMP); - if (pfn_DxcCreateInstance == nullptr) - { -#ifdef PLATFORM_UWP - HMODULE dll = LoadPackagedLibrary(L"dxcompiler.dll", 0); -#else - HMODULE dll = LoadLibrary(L"dxcompiler.dll"); -#endif // PLATFORM_UWP - assert(dll != NULL); - pfn_DxcCreateInstance = (DxcCreateInstanceProc)GetProcAddress(dll, "DxcCreateInstance"); - assert(pfn_DxcCreateInstance != nullptr); - } - wiBackLog::post("Created GraphicsDevice_DX12"); } GraphicsDevice_DX12::~GraphicsDevice_DX12() @@ -3046,7 +3056,7 @@ using namespace DX12_Internal; blob.size = BytecodeLength; ComPtr container_reflection; - hr = pfn_DxcCreateInstance(CLSID_DxcContainerReflection, __uuidof(IDxcContainerReflection), (void**)&container_reflection); + hr = dll_DxcCreateInstance(CLSID_DxcContainerReflection, __uuidof(IDxcContainerReflection), (void**)&container_reflection); assert(SUCCEEDED(hr)); hr = container_reflection->Load(&blob); assert(SUCCEEDED(hr)); @@ -3202,7 +3212,7 @@ using namespace DX12_Internal; ID3DBlob* rootSigBlob; ID3DBlob* rootSigError; - hr = D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); + hr = dll_D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); if (FAILED(hr)) { OutputDebugStringA((char*)rootSigError->GetBufferPointer()); @@ -3435,7 +3445,7 @@ using namespace DX12_Internal; ID3DBlob* rootSigBlob; ID3DBlob* rootSigError; - HRESULT hr = D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); + HRESULT hr = dll_D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); if (FAILED(hr)) { assert(0); @@ -4268,7 +4278,7 @@ using namespace DX12_Internal; ID3DBlob* rootSigBlob; ID3DBlob* rootSigError; - HRESULT hr = D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); + HRESULT hr = dll_D3D12SerializeVersionedRootSignature(&versioned_rs, &rootSigBlob, &rootSigError); if (FAILED(hr)) { OutputDebugStringA((char*)rootSigError->GetBufferPointer()); diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 30fec2626..a4e19757a 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates, breaking compatibility changes const int minor = 51; // minor bug fixes, alterations, refactors, updates - const int revision = 45; + const int revision = 46; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);