Skip to content

Commit 94164b8

Browse files
committed
GPU (WSL): unifies code with the windows version
1 parent 9cfe7a7 commit 94164b8

File tree

5 files changed

+96
-359
lines changed

5 files changed

+96
-359
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ if(LINUX)
561561
src/detection/gpu/gpu_linux.c
562562
src/detection/gpu/gpu_drm.c
563563
src/detection/gpu/gpu_pci.c
564-
src/detection/gpu/gpu_wsl.c
564+
src/detection/gpu/gpu_windows.c
565565
src/detection/gtk_qt/gtk.c
566566
src/detection/host/host_linux.c
567567
src/detection/icons/icons_linux.c

src/detection/gpu/d3dkmthk.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#else
1111
# include <sys/ioctl.h>
1212
# include <uchar.h>
13+
# include <errno.h>
1314

1415
typedef struct _LUID {
1516
uint32_t LowPart;
@@ -330,24 +331,26 @@ EXTERN_C _Check_return_ NTSTATUS APIENTRY D3DKMTQueryStatistics(_In_ CONST D3DKM
330331
# define LX_DXCLOSEADAPTER _IOWR(0x47, 0x15, D3DKMT_CLOSEADAPTER)
331332
# define LX_DXQUERYSTATISTICS _IOWR(0x47, 0x43, D3DKMT_QUERYSTATISTICS)
332333

333-
static inline NTSTATUS D3DKMTOpenAdapterFromLuid(int dxgfd, const D3DKMT_OPENADAPTERFROMLUID* params) {
334-
return ioctl(dxgfd, LX_DXOPENADAPTERFROMLUID, params);
334+
extern int dxgfd; // File descriptor for /dev/dxg, initialized in gpu_wsl.c
335+
336+
static inline NTSTATUS D3DKMTOpenAdapterFromLuid(const D3DKMT_OPENADAPTERFROMLUID* params) {
337+
return ioctl(dxgfd, LX_DXOPENADAPTERFROMLUID, params) < 0 ? -errno : 0;
335338
}
336339

337-
static inline NTSTATUS D3DKMTQueryAdapterInfo(int dxgfd, const D3DKMT_QUERYADAPTERINFO* params) {
338-
return ioctl(dxgfd, LX_DXQUERYADAPTERINFO, params);
340+
static inline NTSTATUS D3DKMTQueryAdapterInfo(const D3DKMT_QUERYADAPTERINFO* params) {
341+
return ioctl(dxgfd, LX_DXQUERYADAPTERINFO, params) < 0 ? -errno : 0;
339342
}
340343

341-
static inline NTSTATUS D3DKMTCloseAdapter(int dxgfd, const D3DKMT_CLOSEADAPTER* params) {
342-
return ioctl(dxgfd, LX_DXCLOSEADAPTER, params);
344+
static inline NTSTATUS D3DKMTCloseAdapter(const D3DKMT_CLOSEADAPTER* params) {
345+
return ioctl(dxgfd, LX_DXCLOSEADAPTER, params) < 0 ? -errno : 0;
343346
}
344347

345-
static inline NTSTATUS D3DKMTEnumAdapters2(int dxgfd, D3DKMT_ENUMADAPTERS2* params) {
346-
return ioctl(dxgfd, LX_DXENUMADAPTERS2, params);
348+
static inline NTSTATUS D3DKMTEnumAdapters2(D3DKMT_ENUMADAPTERS2* params) {
349+
return ioctl(dxgfd, LX_DXENUMADAPTERS2, params) < 0 ? -errno : 0;
347350
}
348351

349-
static inline NTSTATUS D3DKMTQueryStatistics(int dxgfd, const D3DKMT_QUERYSTATISTICS* params) {
350-
return ioctl(dxgfd, LX_DXQUERYSTATISTICS, params);
352+
static inline NTSTATUS D3DKMTQueryStatistics(const D3DKMT_QUERYSTATISTICS* params) {
353+
return ioctl(dxgfd, LX_DXQUERYSTATISTICS, params) < 0 ? -errno : 0;
351354
}
352355

353356
#endif

src/detection/gpu/gpu_driver_specific.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,40 @@ const char* ffDetectIntelGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverRe
4343
const char* ffDetectAmdGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName);
4444
const char* ffDetectMthreadsGpuInfo(const FFGpuDriverCondition* cond, FFGpuDriverResult result, const char* soName);
4545

46+
#ifndef FF_GPU_DRIVER_DLLNAME_PATH_PREFIX
47+
# define FF_GPU_DRIVER_DLLNAME_PATH_PREFIX
48+
#endif
49+
4650
FF_MAYBE_UNUSED static inline bool getDriverSpecificDetectionFn(const char* vendor, __typeof__(&ffDetectNvidiaGpuInfo)* pDetectFn, const char** pDllName) {
4751
if (vendor == FF_GPU_VENDOR_NAME_NVIDIA) {
4852
*pDetectFn = ffDetectNvidiaGpuInfo;
4953
#ifdef _WIN32
50-
*pDllName = "nvml.dll";
54+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "nvml.dll";
5155
#else
52-
*pDllName = "libnvidia-ml.so";
56+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "libnvidia-ml.so";
5357
#endif
5458
} else if (vendor == FF_GPU_VENDOR_NAME_MTHREADS) {
5559
*pDetectFn = ffDetectMthreadsGpuInfo;
5660
#ifdef _WIN32
57-
*pDllName = "mtml.dll";
61+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "mtml.dll";
5862
#else
59-
*pDllName = "libmtml.so";
63+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "libmtml.so";
6064
#endif
6165
}
6266
#ifdef _WIN32
6367
else if (vendor == FF_GPU_VENDOR_NAME_INTEL) {
6468
*pDetectFn = ffDetectIntelGpuInfo;
6569
# ifdef _WIN64
66-
*pDllName = "ControlLib.dll";
70+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "ControlLib.dll";
6771
# else
68-
*pDllName = "ControlLib32.dll";
72+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "ControlLib32.dll";
6973
# endif
7074
} else if (vendor == FF_GPU_VENDOR_NAME_AMD) {
7175
*pDetectFn = ffDetectAmdGpuInfo;
7276
# ifdef _WIN64
73-
*pDllName = "atiadlxx.dll";
77+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "atiadlxx.dll";
7478
# else
75-
*pDllName = "atiadlxy.dll";
79+
*pDllName = FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "atiadlxy.dll";
7680
# endif
7781
}
7882
#endif

src/detection/gpu/gpu_windows.c

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,79 @@
11
#include "detection/gpu/gpu.h"
2+
#if __linux__
3+
# define FF_GPU_DRIVER_DLLNAME_PATH_PREFIX "/usr/lib/wsl/lib/"
4+
#endif
25
#include "detection/gpu/gpu_driver_specific.h"
3-
#include "common/windows/unicode.h"
46
#include "common/debug.h"
57

68
#include <inttypes.h>
79
#include "d3dkmthk.h"
810

9-
const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) {
11+
#if _WIN32
12+
# include "common/windows/unicode.h"
13+
#else
14+
# include <unistd.h>
15+
# include <fcntl.h>
16+
# include <sys/ioctl.h>
17+
# include <uchar.h>
18+
19+
int dxgfd = -2;
20+
21+
static void ffStrbufSetWS(FFstrbuf* strbuf, const char16_t* str) {
22+
ffStrbufClear(strbuf);
23+
24+
mbstate_t state = {};
25+
while (*str) {
26+
char buf[5];
27+
size_t len = c16rtomb(buf, *str, &state);
28+
if (len == (size_t) -1) {
29+
ffStrbufAppendS(strbuf, "�"); // U+FFFD REPLACEMENT CHARACTER
30+
} else if (len > 0) {
31+
ffStrbufAppendNS(strbuf, (uint32_t) len, buf);
32+
}
33+
str++;
34+
}
35+
}
36+
37+
static void closeDxgfd(void) {
38+
if (dxgfd >= 0) {
39+
close(dxgfd);
40+
dxgfd = 0;
41+
FF_DEBUG("Closed /dev/dxg file descriptor");
42+
}
43+
}
44+
45+
static inline const char* ffDebugNtStatus(NTSTATUS status) {
46+
return status < 0 ? strerror(-status) : "Success";
47+
}
48+
#endif
49+
50+
const char*
51+
#if _WIN32
52+
ffDetectGPUImpl
53+
#else
54+
ffGPUDetectWsl2
55+
#endif
56+
(const FFGPUOptions* options, FFlist* gpus) {
57+
#if __linux__
58+
if (dxgfd == -2) {
59+
dxgfd = open("/dev/dxg", O_RDWR); // Windows DXCore/D3DKMT adapter driver for WSL
60+
if (dxgfd < 0) {
61+
if (errno == ENOENT) {
62+
FF_DEBUG("/dev/dxg is not available, WSL DXCore GPU driver not detected");
63+
return "No DXCore GPU driver detected (no /dev/dxg)";
64+
} else {
65+
FF_DEBUG("Failed to open /dev/dxg: %s", strerror(errno));
66+
return "Failed to open /dev/dxg";
67+
}
68+
}
69+
FF_DEBUG("Opened /dev/dxg successfully");
70+
atexit(closeDxgfd);
71+
}
72+
if (dxgfd < 0) {
73+
return "Failed to open /dev/dxg";
74+
}
75+
#endif
76+
1077
D3DKMT_ADAPTERINFO adapters[64];
1178
D3DKMT_ENUMADAPTERS2 enumAdapters = {
1279
.NumAdapters = ARRAY_SIZE(adapters),
@@ -351,6 +418,7 @@ const char* ffDetectGPUImpl(const FFGPUOptions* options, FFlist* gpus) {
351418
(uint32_t) gpu->type,
352419
(uint64_t) gpu->deviceId);
353420

421+
close_adapter:
354422
status = D3DKMTCloseAdapter(&(D3DKMT_CLOSEADAPTER) { .hAdapter = adapter->hAdapter });
355423
if (NT_SUCCESS(status)) {
356424
FF_DEBUG("Closed adapter #%u successfully", i);

0 commit comments

Comments
 (0)