Skip to content

Commit 1ff3a3e

Browse files
committed
Luma2: dxgi.dll exports work under Wine and Proton
Port PR #7 to Luma2/Source/Core/System/DXGI/DXGIExports.cpp so the 7 Wine/DXVK-implemented DXGI exports are real functions forwarding through System::Modules::FindExport instead of \\.\GLOBALROOT forwarders. Also remove the now-redundant CreateDXGIFactory1 /ALTERNATENAME workaround in D3D12Renderer.cpp and update UtilsTests.cpp accordingly.
1 parent dd65e05 commit 1ff3a3e

3 files changed

Lines changed: 120 additions & 32 deletions

File tree

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
// The loader resolves each export to the System32 dxgi.dll, so Luma doesn't need its own stubs.
2-
// "dxgi.X" would resolve to Luma itself, hence the full path.
3-
#define LUMA_DXGI_EXPORT(name, ordinal) \
4-
__pragma(comment(linker, "/EXPORT:" #name "=\\\\.\\GLOBALROOT\\SystemRoot\\System32\\dxgi.dll." #name ",@" #ordinal))
1+
#include <Windows.h>
2+
3+
#include <dxgi1_6.h>
4+
5+
#include "System/Modules.hpp"
6+
7+
// Most exports are forwarders the loader resolves to the system dxgi.dll, so Luma doesn't need its own stubs.
8+
// "dxgi.X" would resolve to Luma itself, hence the full path. 32 bit processes get SysWOW64, the loader doesn't redirect NT paths.
9+
// Wine can't open that path: the exports Wine's or DXVK's dxgi.dll have are Luma's own functions, calling the system dxgi.dll.
10+
// The others only exist on Windows.
11+
// On 32 bit, a pragma's export doesn't find a decorated symbol ("_Name@ArgumentsSize") by itself.
12+
#ifdef _WIN64
13+
#define LUMA_DXGI_PATH "\\\\.\\GLOBALROOT\\SystemRoot\\System32\\dxgi.dll."
14+
#define LUMA_DXGI_FUNCTION(name, ordinal, arguments_size) __pragma(comment(linker, "/EXPORT:" #name ",@" #ordinal))
15+
#else
16+
#define LUMA_DXGI_PATH "\\\\.\\GLOBALROOT\\SystemRoot\\SysWOW64\\dxgi.dll."
17+
#define LUMA_DXGI_FUNCTION(name, ordinal, arguments_size) __pragma(comment(linker, "/EXPORT:" #name "=_" #name "@" #arguments_size ",@" #ordinal))
18+
#endif
19+
#define LUMA_DXGI_EXPORT(name, ordinal) __pragma(comment(linker, "/EXPORT:" #name "=" LUMA_DXGI_PATH #name ",@" #ordinal))
520

621
// The names and ordinals of the System32 dxgi.dll ("dumpbin /exports C:\Windows\System32\dxgi.dll")
722
LUMA_DXGI_EXPORT(ApplyCompatResolutionQuirking, 1)
@@ -13,14 +28,86 @@ LUMA_DXGI_EXPORT(PIXEndCapture, 6)
1328
LUMA_DXGI_EXPORT(PIXGetCaptureState, 7)
1429
LUMA_DXGI_EXPORT(SetAppCompatStringPointer, 8)
1530
LUMA_DXGI_EXPORT(UpdateHMDEmulationStatus, 9)
16-
LUMA_DXGI_EXPORT(CreateDXGIFactory, 10)
17-
LUMA_DXGI_EXPORT(CreateDXGIFactory1, 11)
18-
LUMA_DXGI_EXPORT(CreateDXGIFactory2, 12)
19-
LUMA_DXGI_EXPORT(DXGID3D10CreateDevice, 13)
31+
LUMA_DXGI_FUNCTION(CreateDXGIFactory, 10, 8)
32+
LUMA_DXGI_FUNCTION(CreateDXGIFactory1, 11, 8)
33+
LUMA_DXGI_FUNCTION(CreateDXGIFactory2, 12, 12)
34+
LUMA_DXGI_FUNCTION(DXGID3D10CreateDevice, 13, 28)
2035
LUMA_DXGI_EXPORT(DXGID3D10CreateLayeredDevice, 14)
2136
LUMA_DXGI_EXPORT(DXGID3D10GetLayeredDeviceSize, 15)
22-
LUMA_DXGI_EXPORT(DXGID3D10RegisterLayers, 16)
23-
LUMA_DXGI_EXPORT(DXGIDeclareAdapterRemovalSupport, 17)
37+
LUMA_DXGI_FUNCTION(DXGID3D10RegisterLayers, 16, 8)
38+
LUMA_DXGI_FUNCTION(DXGIDeclareAdapterRemovalSupport, 17, 0)
2439
LUMA_DXGI_EXPORT(DXGIDisableVBlankVirtualization, 18)
25-
LUMA_DXGI_EXPORT(DXGIGetDebugInterface1, 19)
40+
LUMA_DXGI_FUNCTION(DXGIGetDebugInterface1, 19, 12)
2641
LUMA_DXGI_EXPORT(DXGIReportAdapterConfiguration, 20)
42+
43+
namespace
44+
{
45+
// Loads the system dxgi.dll if it isn't loaded yet. Not GetProcAddress (see FindExport).
46+
void* FindSystemExport(const char* name)
47+
{
48+
HMODULE module = Luma::System::Modules::FindSystemModule(L"dxgi.dll");
49+
if (!module)
50+
module = LoadLibraryW(Luma::System::Modules::SystemPath(L"dxgi.dll").c_str());
51+
if (!module || module == Luma::System::Modules::GetSelf())
52+
return nullptr;
53+
return Luma::System::Modules::FindExport(module, name);
54+
}
55+
} // namespace
56+
57+
extern "C" HRESULT WINAPI CreateDXGIFactory(REFIID riid, void** factory)
58+
{
59+
const auto function = reinterpret_cast<decltype(&CreateDXGIFactory)>(FindSystemExport("CreateDXGIFactory"));
60+
if (!function)
61+
return E_FAIL;
62+
return function(riid, factory);
63+
}
64+
65+
extern "C" HRESULT WINAPI CreateDXGIFactory1(REFIID riid, void** factory)
66+
{
67+
const auto function = reinterpret_cast<decltype(&CreateDXGIFactory1)>(FindSystemExport("CreateDXGIFactory1"));
68+
if (!function)
69+
return E_FAIL;
70+
return function(riid, factory);
71+
}
72+
73+
extern "C" HRESULT WINAPI CreateDXGIFactory2(UINT flags, REFIID riid, void** factory)
74+
{
75+
const auto function = reinterpret_cast<decltype(&CreateDXGIFactory2)>(FindSystemExport("CreateDXGIFactory2"));
76+
if (!function)
77+
return E_FAIL;
78+
return function(flags, riid, factory);
79+
}
80+
81+
// Not in the Windows SDK: Wine's own d3d11.dll and d3d10core.dll import them (Wine without DXVK). The signatures are Wine's and
82+
// ReShade's.
83+
extern "C" HRESULT WINAPI DXGID3D10CreateDevice(HMODULE module, IDXGIFactory* factory, IDXGIAdapter* adapter, UINT flags, const void* feature_levels, UINT feature_level_count, void** device)
84+
{
85+
const auto function = reinterpret_cast<decltype(&DXGID3D10CreateDevice)>(FindSystemExport("DXGID3D10CreateDevice"));
86+
if (!function)
87+
return E_FAIL;
88+
return function(module, factory, adapter, flags, feature_levels, feature_level_count, device);
89+
}
90+
91+
extern "C" HRESULT WINAPI DXGID3D10RegisterLayers(const void* layers, UINT layer_count)
92+
{
93+
const auto function = reinterpret_cast<decltype(&DXGID3D10RegisterLayers)>(FindSystemExport("DXGID3D10RegisterLayers"));
94+
if (!function)
95+
return E_FAIL;
96+
return function(layers, layer_count);
97+
}
98+
99+
extern "C" HRESULT WINAPI DXGIDeclareAdapterRemovalSupport()
100+
{
101+
const auto function = reinterpret_cast<decltype(&DXGIDeclareAdapterRemovalSupport)>(FindSystemExport("DXGIDeclareAdapterRemovalSupport"));
102+
if (!function)
103+
return E_FAIL;
104+
return function();
105+
}
106+
107+
extern "C" HRESULT WINAPI DXGIGetDebugInterface1(UINT flags, REFIID riid, void** debug)
108+
{
109+
const auto function = reinterpret_cast<decltype(&DXGIGetDebugInterface1)>(FindSystemExport("DXGIGetDebugInterface1"));
110+
if (!function)
111+
return E_FAIL;
112+
return function(flags, riid, debug);
113+
}

‎Luma2/Source/Core/UI/D3D12/D3D12Renderer.cpp‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@
2222

2323
using Microsoft::WRL::ComPtr;
2424

25-
// ImGui's DX12 backend calls CreateDXGIFactory1, but Luma can't link dxgi.lib: it is the dxgi.dll proxy. When nothing else
26-
// defines the function, the linker uses this one.
27-
extern "C" HRESULT WINAPI LumaCreateDXGIFactory1(REFIID riid, void** factory)
28-
{
29-
static const auto function = [] {
30-
wchar_t path[MAX_PATH] = {};
31-
const UINT length = GetSystemDirectoryW(path, MAX_PATH);
32-
HMODULE dxgi = length ? LoadLibraryW((std::wstring(path, length) + L"\\dxgi.dll").c_str()) : nullptr;
33-
return dxgi ? reinterpret_cast<HRESULT(WINAPI*)(REFIID, void**)>(GetProcAddress(dxgi, "CreateDXGIFactory1")) : nullptr;
34-
}();
35-
return function ? function(riid, factory) : E_FAIL;
36-
}
37-
38-
#pragma comment(linker, "/ALTERNATENAME:CreateDXGIFactory1=LumaCreateDXGIFactory1")
39-
4025
namespace Luma {
4126
namespace UI {
4227
namespace D3D12 {

‎Luma2/Source/Tests/UtilsTests.cpp‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,26 +154,42 @@ TEST_CASE("Toml: malformed documents fail and keys are quoted when needed")
154154

155155
#include <Windows.h>
156156
#include <Psapi.h>
157+
#include <dxgi1_4.h>
158+
#include <wrl/client.h>
157159

158160
#include "System/Modules.hpp"
159161

160-
TEST_CASE("Dll exports: every dxgi export forwards to the System32 dxgi.dll")
162+
TEST_CASE("Dll exports: every dxgi export reaches the System32 dxgi.dll")
161163
{
162-
// Tests.exe links the same forwarders as a game dll
164+
// Tests.exe links the same exports as a game dll
163165
HMODULE self = GetModuleHandleW(nullptr);
164166
HMODULE dxgi = Luma::System::Modules::FindSystemModule(L"dxgi.dll");
165167
REQUIRE(dxgi);
166-
for (const char* name : {"CreateDXGIFactory", "CreateDXGIFactory1", "CreateDXGIFactory2", "DXGIGetDebugInterface1", "DXGIDeclareAdapterRemovalSupport", "CompatValue", "CompatString", "SetAppCompatStringPointer"})
168+
// Wine/DXVK-implemented exports are Luma functions that call into the System32 dxgi.dll
169+
for (const char* name : {"CreateDXGIFactory", "CreateDXGIFactory1", "CreateDXGIFactory2", "DXGID3D10CreateDevice", "DXGID3D10RegisterLayers", "DXGIDeclareAdapterRemovalSupport", "DXGIGetDebugInterface1"})
170+
{
171+
CAPTURE(name);
172+
CHECK(GetProcAddress(self, name) != nullptr);
173+
}
174+
// Same ordinals as the system dll
175+
CHECK(GetProcAddress(self, MAKEINTRESOURCEA(11)) == GetProcAddress(self, "CreateDXGIFactory1"));
176+
// Calling through the exported wrapper succeeds
177+
const auto create_factory1 = reinterpret_cast<HRESULT(WINAPI*)(REFIID, void**)>(GetProcAddress(self, "CreateDXGIFactory1"));
178+
REQUIRE(create_factory1 != nullptr);
179+
Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
180+
CHECK(SUCCEEDED(create_factory1(IID_PPV_ARGS(&factory))));
181+
CHECK(factory != nullptr);
182+
183+
// Windows-only exports stay PE forwarders to the System32 dxgi.dll
184+
for (const char* name : {"CompatValue", "CompatString", "SetAppCompatStringPointer"})
167185
{
168186
CAPTURE(name);
169187
void* forwarded = reinterpret_cast<void*>(GetProcAddress(self, name));
170188
CHECK(forwarded);
171189
CHECK(forwarded == Luma::System::Modules::FindExport(dxgi, name));
172190
}
173-
// Same ordinals as the system dll
174-
CHECK(reinterpret_cast<void*>(GetProcAddress(self, MAKEINTRESOURCEA(11))) == Luma::System::Modules::FindExport(dxgi, "CreateDXGIFactory1"));
175191

176-
// The full path forwarders reuse the loaded dxgi instead of loading a second copy
192+
// The full path forwarders and wrappers reuse the loaded dxgi instead of loading a second copy
177193
HMODULE modules[1024];
178194
DWORD size = 0;
179195
REQUIRE(K32EnumProcessModules(GetCurrentProcess(), modules, sizeof(modules), &size));

0 commit comments

Comments
 (0)