From 3a0d9c55c192053be2a651ebe51ecfbcb5d0ee5c Mon Sep 17 00:00:00 2001 From: Nemirtingas Date: Fri, 2 Aug 2019 09:06:59 +0200 Subject: [PATCH] Added support for dynamic d3dcompile load If you really want to use d3dcompile, then I think its better to find the most recent d3dcompile you have on your computer and load its D3DCompile function. This process is now automatically handled. Just define the right Preprocessor. --- ImGui/impls/imgui_impl_dx10.cpp | 12 ++++++++ ImGui/impls/imgui_impl_dx11.cpp | 10 +++++++ ImGui/impls/imgui_impl_dx12.cpp | 10 +++++++ overlay_experimental/ImGui_ShaderBlobs.cpp | 35 +++++++++++++++++++++- overlay_experimental/ImGui_ShaderBlobs.h | 9 ++++-- 5 files changed, 72 insertions(+), 4 deletions(-) diff --git a/ImGui/impls/imgui_impl_dx10.cpp b/ImGui/impls/imgui_impl_dx10.cpp index 8ff8381..dd462f9 100644 --- a/ImGui/impls/imgui_impl_dx10.cpp +++ b/ImGui/impls/imgui_impl_dx10.cpp @@ -335,6 +335,12 @@ bool ImGui_ImplDX10_CreateDeviceObjects() // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. // See https://github.com/ocornut/imgui/pull/638 for sources and details. +#ifdef USE_D3DCOMPILE + decltype(D3DCompile)* D3DCompile = load_d3dcompile(); + if (D3DCompile == nullptr) + return false; +#endif + // Create the vertex shader { #ifdef USE_D3DCOMPILE @@ -367,6 +373,7 @@ bool ImGui_ImplDX10_CreateDeviceObjects() }"; D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL); + if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! return false; @@ -424,6 +431,7 @@ bool ImGui_ImplDX10_CreateDeviceObjects() }"; D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL); + if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! return false; if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), &g_pPixelShader) != S_OK) @@ -434,6 +442,10 @@ bool ImGui_ImplDX10_CreateDeviceObjects() #endif } +#ifdef USE_D3DCOMPILE + unload_d3dcompile(); +#endif + // Create the blending setup { D3D10_BLEND_DESC desc; diff --git a/ImGui/impls/imgui_impl_dx11.cpp b/ImGui/impls/imgui_impl_dx11.cpp index 466bf25..0cf1b2b 100644 --- a/ImGui/impls/imgui_impl_dx11.cpp +++ b/ImGui/impls/imgui_impl_dx11.cpp @@ -342,6 +342,12 @@ bool ImGui_ImplDX11_CreateDeviceObjects() // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. // See https://github.com/ocornut/imgui/pull/638 for sources and details. +#ifdef USE_D3DCOMPILE + decltype(D3DCompile)* D3DCompile = load_d3dcompile(); + if (D3DCompile == nullptr) + return false; +#endif + // Create the vertex shader { #ifdef USE_D3DCOMPILE @@ -441,6 +447,10 @@ bool ImGui_ImplDX11_CreateDeviceObjects() #endif } +#ifdef USE_D3DCOMPILE + unload_d3dcompile(); +#endif + // Create the blending setup { D3D11_BLEND_DESC desc; diff --git a/ImGui/impls/imgui_impl_dx12.cpp b/ImGui/impls/imgui_impl_dx12.cpp index 846c2c8..2f57fe9 100644 --- a/ImGui/impls/imgui_impl_dx12.cpp +++ b/ImGui/impls/imgui_impl_dx12.cpp @@ -468,6 +468,12 @@ bool ImGui_ImplDX12_CreateDeviceObjects() psoDesc.SampleDesc.Count = 1; psoDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; +#ifdef USE_D3DCOMPILE + decltype(D3DCompile)* D3DCompile = load_d3dcompile(); + if (D3DCompile == nullptr) + return false; +#endif + // Create the vertex shader { #ifdef USE_D3DCOMPILE @@ -546,6 +552,10 @@ bool ImGui_ImplDX12_CreateDeviceObjects() #endif } +#ifdef USE_D3DCOMPILE + unload_d3dcompile(); +#endif + // Create the blending setup { D3D12_BLEND_DESC& desc = psoDesc.BlendState; diff --git a/overlay_experimental/ImGui_ShaderBlobs.cpp b/overlay_experimental/ImGui_ShaderBlobs.cpp index 3c536f6..4ab9d09 100644 --- a/overlay_experimental/ImGui_ShaderBlobs.cpp +++ b/overlay_experimental/ImGui_ShaderBlobs.cpp @@ -1,5 +1,36 @@ #include "ImGui_ShaderBlobs.h" +#ifdef USE_D3DCOMPILE + +#include + +static HMODULE d3dcompile_dll = nullptr; + +decltype(D3DCompile)* load_d3dcompile() +{ + decltype(D3DCompile)* func = nullptr; + for (int i = 47; i > 30; i--) + { + char dll_name[20]; + sprintf_s(dll_name, "d3dcompiler_%02d.dll", i); + if (d3dcompile_dll = LoadLibraryA(dll_name)) + { + func = (decltype(D3DCompile)*)GetProcAddress(d3dcompile_dll, "D3DCompile"); + break; + } + } + + return func; +} + +void unload_d3dcompile() +{ + if (d3dcompile_dll) + FreeLibrary(d3dcompile_dll); +} + +#else + extern unsigned char ImGui_vertexShaderDX10[] = { 0x44, 0x58, 0x42, 0x43, 0x7a, 0x54, 0x84, 0x96, 0xdf, 0xd1, 0x9e, 0x21, 0xfd, 0x85, 0x86, 0x3d, 0x28, 0xd1, 0x03, 0xae, 0x01, 0x00, 0x00, 0x00, @@ -283,4 +314,6 @@ extern unsigned char ImGui_pixelShaderDX12[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/overlay_experimental/ImGui_ShaderBlobs.h b/overlay_experimental/ImGui_ShaderBlobs.h index f278f4c..a6a523b 100644 --- a/overlay_experimental/ImGui_ShaderBlobs.h +++ b/overlay_experimental/ImGui_ShaderBlobs.h @@ -6,9 +6,12 @@ #ifdef USE_D3DCOMPILE #include - #ifdef _MSC_VER - #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. - #endif + //#ifdef _MSC_VER + // #pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. + //#endif + + decltype(D3DCompile)* load_d3dcompile(); + void unload_d3dcompile(); #else #define ImGui_vertexShaderDX10_len 876