From cc4df7d140762a0f45d1776e841d8d9b94371746 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:18:21 +0100 Subject: [PATCH] Load the NGX core through the platform layer, so vk_dlaa.c has no Win32 calls tools/check_platform.py failed on vk_dlaa.c: it read the NGX core's folder from the registry, loaded it with LoadLibraryA and GetProcAddress and named the executable's folder with GetModuleFileNameW. platform.h gets plat_ngx_core_path (the registry folder plus _nvngx.dll on Windows, libnvidia-ngx.so.1 on Linux), plat_exe_dir_wide (the folder as NGX's wchar_t string; the UTF-8 to UTF-32 conversion moves from vk_dlaa.c to linux.c) and plat_lib_error, and the core loads through plat_lib_open and plat_lib_sym on both hosts. Co-Authored-By: Claude Opus 5.5 --- runtime/platform/linux.c | 35 +++++++++++++++++++++ runtime/platform/platform.h | 10 ++++++ runtime/platform/win32.c | 21 +++++++++++++ runtime/vk_dlaa.c | 61 +++++-------------------------------- 4 files changed, 73 insertions(+), 54 deletions(-) diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index fd30906b..158601cb 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1456,6 +1456,41 @@ int plat_open_folder(const char *path) { void *plat_lib_open(const char *name) { return dlopen(name, RTLD_NOW | RTLD_LOCAL); } void *plat_lib_sym(void *lib, const char *name) { return lib ? dlsym(lib, name) : NULL; } +void plat_lib_error(char *out, size_t cap) { + const char *e = dlerror(); + snprintf(out, cap, "%s", e ? e : "unknown error"); +} + +int plat_ngx_core_path(char *out, size_t cap) { return snprintf(out, cap, "libnvidia-ngx.so.1") < (int)cap; } + +// UTF-8 to UTF-32 wchar_t, without the C library's locale (the runtime never sets one, so mbstowcs would stop at the +// first non-ASCII byte). Returns 0 on malformed input or when out has no room. +static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) { + const unsigned char *u = (const unsigned char *)in; + size_t n = 0; + while (*u) { + unsigned c = *u++, more = c < 0x80 ? 0 : c >= 0xF0 ? 3 : c >= 0xE0 ? 2 : c >= 0xC0 ? 1 : 4; + if (more == 4) return 0; + if (more) c &= 0x3F >> more; + for (unsigned k = 0; k < more; k++, u++) { + if ((*u & 0xC0) != 0x80) return 0; + c = c << 6 | (*u & 0x3F); + } + if (n + 1 >= cap) return 0; + out[n++] = (wchar_t)c; + } + out[n] = 0; + return 1; +} + +int plat_exe_dir_wide(wchar_t *out, size_t cap) { + char dir[PLAT_PATH_MAX]; + char *cut = plat_exe_path(dir, sizeof dir) ? plat_last_sep(dir) : NULL; + if (!cut) return 0; + cut[1] = 0; + return utf8_to_wide(dir, out, cap); +} + // ---- machine load: /proc/stat and /proc/self/stat for the CPU, hwmon for the temperature, the amdgpu sysfs files of // the render device for the GPU. diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index d13eda95..eea834e0 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -387,6 +387,16 @@ int plat_open_folder(const char *path); // shows the folder in // in the system directories only. NULL when missing. void *plat_lib_open(const char *name); void *plat_lib_sym(void *lib, const char *name); +// Why the calling thread's last plat_lib_open failed, for a log line (the OS error code on Windows, dlerror on Linux). +void plat_lib_error(char *out, size_t cap); + +// ---- NVIDIA NGX (vk_dlaa.c). The path of the driver's NGX core library into out: on Windows _nvngx.dll in the folder +// the driver records in the registry (NGXCore FullPath), on Linux libnvidia-ngx.so.1, which the dynamic loader finds +// where the driver put it. 0 when the driver records none. +int plat_ngx_core_path(char *out, size_t cap); +// The running executable's folder with a trailing separator, as the wchar_t string NGX takes for its application data +// path (UTF-16 on Windows, UTF-32 code points on Linux). 0 on failure or when out has no room. +int plat_exe_dir_wide(wchar_t *out, size_t cap); // ---- machine load for the performance overlay (perfmon.c's sampling thread; each call can take milliseconds). A number // the system does not give is -1. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index aee2e3dc..4f17994f 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1389,6 +1389,27 @@ void *plat_lib_open(const char *name) { void *plat_lib_sym(void *lib, const char *name) { return lib ? (void *)GetProcAddress((HMODULE)lib, name) : NULL; } +void plat_lib_error(char *out, size_t cap) { snprintf(out, cap, "error %lu", GetLastError()); } + +int plat_ngx_core_path(char *out, size_t cap) { + wchar_t dir[WPATH]; + DWORD n = sizeof dir; + char d[PLAT_PATH_MAX]; + if (RegGetValueW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", L"FullPath", RRF_RT_REG_SZ, + NULL, dir, &n) != ERROR_SUCCESS || + !WideCharToMultiByte(CP_UTF8, 0, dir, -1, d, (int)sizeof d, NULL, NULL)) + return 0; + return snprintf(out, cap, "%s\\_nvngx.dll", d) < (int)cap; +} + +int plat_exe_dir_wide(wchar_t *out, size_t cap) { + DWORD len = GetModuleFileNameW(NULL, out, (DWORD)cap); + if (!len || len >= cap) return 0; + while (len && out[len - 1] != L'\\' && out[len - 1] != L'/') len--; + out[len] = 0; + return len != 0; +} + // ---- machine load: GetSystemTimes for the CPU, PDH (pdh.dll, loaded on first use) for the thermal zones and the GPU // engine and adapter memory counters that Task Manager reads. diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index 7ffff8c8..1fe39bde 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -11,9 +11,6 @@ // layout with a set-then-get round trip before anything relies on it. // // Where DLAA runs in the frame, the jitter and the motion vectors: see the sections below. -#ifdef _WIN32 -#include -#endif #include #include #include @@ -115,31 +112,19 @@ int vk_dlaa_wanted(void) { static int core_load(void) { if (N.tried) return N.loaded; N.tried = 1; -#ifdef _WIN32 - char dir[MAX_PATH] = {0}, path[MAX_PATH + 32]; - DWORD n = sizeof dir; - if (RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore", "FullPath", RRF_RT_REG_SZ, - NULL, dir, &n) != ERROR_SUCCESS) { + char path[PLAT_PATH_MAX]; + if (!plat_ngx_core_path(path, sizeof path)) { rt_log("dlaa: the driver has no NGX core (registry NGXCore missing): DLAA off"); return 0; } - snprintf(path, sizeof path, "%s\\_nvngx.dll", dir); - N.core = LoadLibraryA(path); - if (!N.core) { - rt_log("dlaa: cannot load %s (error %lu): DLAA off", path, GetLastError()); - return 0; - } -#define FN(f, name) N.f = (void *)GetProcAddress((HMODULE)N.core, name) -#else - // The driver's NGX core; the dynamic loader finds it where the driver put it (/usr/lib, /usr/lib/x86_64-linux-gnu). - const char *path = "libnvidia-ngx.so.1"; N.core = plat_lib_open(path); if (!N.core) { - rt_log("dlaa: cannot load %s (not an NVIDIA driver, or one without NGX): DLAA off", path); + char why[256]; + plat_lib_error(why, sizeof why); + rt_log("dlaa: cannot load %s (%s; not an NVIDIA driver, or one without NGX): DLAA off", path, why); return 0; } #define FN(f, name) N.f = plat_lib_sym(N.core, name) -#endif FN(init, "NVSDK_NGX_VULKAN_Init_Ext2"); FN(req_ext, "NVSDK_NGX_VULKAN_RequiredExtensions"); FN(caps, "NVSDK_NGX_VULKAN_GetCapabilityParameters"); @@ -203,46 +188,14 @@ static int vtable_ok(void) { return ok; } -#ifndef _WIN32 -// UTF-8 to UTF-32 wchar_t, without the C library's locale (the runtime never sets one, so mbstowcs would stop at the -// first non-ASCII byte). Returns 0 on malformed input or when out has no room. -static int utf8_to_wide(const char *in, wchar_t *out, size_t cap) { - const unsigned char *u = (const unsigned char *)in; - size_t n = 0; - while (*u) { - unsigned c = *u++, more = c < 0x80 ? 0 : c >= 0xF0 ? 3 : c >= 0xE0 ? 2 : c >= 0xC0 ? 1 : 4; - if (more == 4) return 0; - if (more) c &= 0x3F >> more; - for (unsigned k = 0; k < more; k++, u++) { - if ((*u & 0xC0) != 0x80) return 0; - c = c << 6 | (*u & 0x3F); - } - if (n + 1 >= cap) return 0; - out[n++] = (wchar_t)c; - } - out[n] = 0; - return 1; -} -#endif - void vk_dlaa_init(void) { if (!vk_dlaa_wanted() || !core_load()) return; -#ifdef _WIN32 - wchar_t wdir[MAX_PATH]; - DWORD len = GetModuleFileNameW(NULL, wdir, MAX_PATH); - while (len && wdir[len - 1] != L'\\') len--; - wdir[len] = 0; -#else - // The executable's folder, where NGX looks for libnvidia-ngx-dlss.so and writes its logs, as a wchar_t string: - // 32-bit code points here, as the Linux core expects. - char dir[PLAT_PATH_MAX]; + // The executable's folder, where NGX looks for the DLSS model library and writes its logs. wchar_t wdir[PLAT_PATH_MAX]; - char *cut = plat_exe_path(dir, sizeof dir) ? plat_last_sep(dir) : NULL; - if (!cut || !utf8_to_wide((cut[1] = 0, dir), wdir, PLAT_PATH_MAX)) { + if (!plat_exe_dir_wide(wdir, PLAT_PATH_MAX)) { rt_log("dlaa: cannot name the executable's folder for NGX: DLAA off"); return; } -#endif // 231313132: the application id NVIDIA's DLSS samples use for builds without an NVIDIA-issued id const VkCtx *c = vk_ctx(); NgxResult r = N.init(231313132ull, wdir, c->instance, c->phys, c->device, vkGetInstanceProcAddr,