mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-16 15:59:59 -04:00
Frame generation now gets the picture without the HUD each frame: the upscaler's output before the HUD is drawn over it, encoded to sRGB as the game's last pass encodes its picture and put through the same colour, scaling and borders as the presented frame, so it tells the HUD apart and leaves it unwarped in the generated frames (NX2NAVI_FG_HUDLESS_SHOW=1 shows that picture, a test). The performance overlay gains an FG row and the fps line the display's rate, generated frames included. The device enables VK_KHR_get_memory_requirements2 whenever AMD's frame generation is on, which crashed at start with DLSS upscaling. DLSS Frame Generation through NVIDIA Streamline (vk_dlssg.c, and nx2navi_dlssg.dll from runtime/dlssg, built with MSVC by tools/build_dlssg.py against the SDK kept outside the repository) creates the instance and device through Streamline's proxies and calls its swapchain hooks; Streamline still fails in its plugin start-up here, so the menu lists it only while it is set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
6.9 KiB
C
158 lines
6.9 KiB
C
// DLSS Frame Generation (NX2NAVI_FRAMEGEN = dlss; applies after a restart) through nx2navi_dlssg.dll
|
|
// (runtime/dlssg/nx2_sl.cpp), which drives NVIDIA Streamline. Like AMD's frame generation (vk_fg.cpp) it needs DLSS or
|
|
// FSR 3.1 upscaling on, whose depth, motion vectors and camera it hands over, and an RTX 40 or 50 GPU.
|
|
//
|
|
// The DLL and Streamline (sl.interposer.dll and its plugins, nvngx_dlssg.dll) sit next to the game. The instance and
|
|
// device are created through Streamline's proxies (vk_device.c), so it can add the extensions, features and queues
|
|
// DLSS-G needs; the present path calls Streamline's swapchain functions (gfx_present.c), and every present records the
|
|
// frame's constants and tags first (vk_dlssg_frame). Generation pauses while the menu is open.
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "cfg.h"
|
|
#include "host.h"
|
|
#include "vk.h"
|
|
#include "vk_dlaa.h"
|
|
#include "vk_dlssg.h"
|
|
#include "dlssg/nx2_sl.h"
|
|
#include "ui/menu.h"
|
|
|
|
static struct {
|
|
int wanted, loaded, active;
|
|
PFN_nx2sl_init init;
|
|
PFN_nx2sl_instance_proc gipa;
|
|
PFN_nx2sl_device_proc gdpa;
|
|
PFN_nx2sl_available available;
|
|
PFN_nx2sl_frame frame;
|
|
PFN_nx2sl_present_marker marker;
|
|
PFN_nx2sl_shutdown shutdown;
|
|
PFN_vkCreateSwapchainKHR create_sc;
|
|
PFN_vkDestroySwapchainKHR destroy_sc;
|
|
PFN_vkGetSwapchainImagesKHR images;
|
|
PFN_vkAcquireNextImageKHR acquire;
|
|
PFN_vkQueuePresentKHR present;
|
|
} S = {.wanted = -1};
|
|
|
|
static void sl_line(const char *line) { rt_log("%s", line); }
|
|
|
|
int vk_dlssg_wanted(void) {
|
|
if (S.wanted < 0) {
|
|
S.wanted = !strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "dlss");
|
|
if (S.wanted && !vk_dlaa_wanted()) {
|
|
rt_log("dlssg: frame generation needs DLSS or FSR 3.1 upscaling (any mode): off");
|
|
S.wanted = 0;
|
|
}
|
|
}
|
|
return S.wanted;
|
|
}
|
|
|
|
int vk_dlssg_early(void) {
|
|
if (!vk_dlssg_wanted()) return 0;
|
|
if (S.loaded) return S.loaded > 0;
|
|
S.loaded = -1;
|
|
#ifdef _WIN32
|
|
wchar_t dir[MAX_PATH], path[MAX_PATH + 32];
|
|
DWORD n = GetModuleFileNameW(NULL, dir, MAX_PATH);
|
|
while (n && dir[n - 1] != L'\\') n--;
|
|
dir[n ? n - 1 : 0] = 0;
|
|
swprintf(path, MAX_PATH + 32, L"%ls\\nx2navi_dlssg.dll", dir);
|
|
HMODULE m = LoadLibraryW(path);
|
|
if (!m) {
|
|
rt_log("dlssg: nx2navi_dlssg.dll is not next to the game: DLSS Frame Generation off");
|
|
return 0;
|
|
}
|
|
PFN_nx2sl_version ver = (PFN_nx2sl_version)(void *)GetProcAddress(m, "nx2sl_version");
|
|
S.init = (PFN_nx2sl_init)(void *)GetProcAddress(m, "nx2sl_init");
|
|
S.gipa = (PFN_nx2sl_instance_proc)(void *)GetProcAddress(m, "nx2sl_instance_proc");
|
|
S.gdpa = (PFN_nx2sl_device_proc)(void *)GetProcAddress(m, "nx2sl_device_proc");
|
|
S.available = (PFN_nx2sl_available)(void *)GetProcAddress(m, "nx2sl_available");
|
|
S.frame = (PFN_nx2sl_frame)(void *)GetProcAddress(m, "nx2sl_frame");
|
|
S.marker = (PFN_nx2sl_present_marker)(void *)GetProcAddress(m, "nx2sl_present_marker");
|
|
S.shutdown = (PFN_nx2sl_shutdown)(void *)GetProcAddress(m, "nx2sl_shutdown");
|
|
if (!ver || ver() != NX2SL_VERSION || !S.init || !S.gipa || !S.gdpa || !S.available || !S.frame || !S.marker) {
|
|
rt_log("dlssg: nx2navi_dlssg.dll is from another build: DLSS Frame Generation off");
|
|
return 0;
|
|
}
|
|
if (!S.init(dir, sl_line)) return 0;
|
|
S.loaded = 1;
|
|
return 1;
|
|
#else
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
VkResult vk_dlssg_create_instance(const VkInstanceCreateInfo *ci, VkInstance *out) {
|
|
PFN_vkCreateInstance f = S.loaded > 0 ? (PFN_vkCreateInstance)S.gipa()(VK_NULL_HANDLE, "vkCreateInstance") : NULL;
|
|
return f ? f(ci, NULL, out) : vkCreateInstance(ci, NULL, out);
|
|
}
|
|
|
|
VkResult vk_dlssg_create_device(VkInstance inst, VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, VkDevice *out) {
|
|
PFN_vkCreateDevice f = S.loaded > 0 ? (PFN_vkCreateDevice)S.gipa()(inst, "vkCreateDevice") : NULL;
|
|
return f ? f(pd, ci, NULL, out) : vkCreateDevice(pd, ci, NULL, out);
|
|
}
|
|
|
|
void vk_dlssg_device_ready(VkDevice dev) {
|
|
if (S.loaded <= 0 || !S.available()) return;
|
|
PFN_vkGetDeviceProcAddr g = S.gdpa();
|
|
S.create_sc = (PFN_vkCreateSwapchainKHR)g(dev, "vkCreateSwapchainKHR");
|
|
S.destroy_sc = (PFN_vkDestroySwapchainKHR)g(dev, "vkDestroySwapchainKHR");
|
|
S.images = (PFN_vkGetSwapchainImagesKHR)g(dev, "vkGetSwapchainImagesKHR");
|
|
S.acquire = (PFN_vkAcquireNextImageKHR)g(dev, "vkAcquireNextImageKHR");
|
|
S.present = (PFN_vkQueuePresentKHR)g(dev, "vkQueuePresentKHR");
|
|
S.active = S.create_sc && S.destroy_sc && S.images && S.acquire && S.present;
|
|
rt_log("dlssg: %s", S.active ? "DLSS Frame Generation on the swapchain" : "the swapchain hooks are missing: off");
|
|
}
|
|
|
|
int vk_dlssg_active(void) { return S.active; }
|
|
|
|
VkResult vk_dlssg_create_swapchain(VkDevice d, const VkSwapchainCreateInfoKHR *ci, VkSwapchainKHR *sc) {
|
|
return S.create_sc(d, ci, NULL, sc);
|
|
}
|
|
void vk_dlssg_destroy_swapchain(VkDevice d, VkSwapchainKHR sc) { S.destroy_sc(d, sc, NULL); }
|
|
VkResult vk_dlssg_get_images(VkDevice d, VkSwapchainKHR sc, uint32_t *n, VkImage *img) { return S.images(d, sc, n, img); }
|
|
VkResult vk_dlssg_acquire(VkDevice d, VkSwapchainKHR sc, uint64_t t, VkSemaphore sem, uint32_t *idx) {
|
|
return S.acquire(d, sc, t, sem, VK_NULL_HANDLE, idx);
|
|
}
|
|
|
|
VkResult vk_dlssg_present(VkQueue q, const VkPresentInfoKHR *pi) {
|
|
S.marker(0);
|
|
VkResult r = S.present(q, pi);
|
|
S.marker(1);
|
|
return r;
|
|
}
|
|
|
|
void vk_dlssg_frame(VkCommandBuffer cb, VkExtent2D ext) {
|
|
if (!S.active) return;
|
|
VkFfxImage d, m;
|
|
VkImageView dv, mvv;
|
|
VkDlaaCamera c;
|
|
float jx = 0, jy = 0;
|
|
int reset = 0;
|
|
int have = vk_dlaa_fg_inputs(&d, &m, &jx, &jy, &reset) && vk_dlaa_fg_views(&dv, &mvv) && vk_dlaa_fg_camera(&c);
|
|
int on = have && !menu_is_open();
|
|
Nx2SlImage zd = {d.image, dv, d.format, d.width, d.height, VK_IMAGE_LAYOUT_GENERAL,
|
|
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT};
|
|
Nx2SlImage zm = {m.image, mvv, m.format, m.width, m.height, VK_IMAGE_LAYOUT_GENERAL,
|
|
VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT};
|
|
Nx2SlCamera k;
|
|
memset(&k, 0, sizeof k);
|
|
if (have) {
|
|
memcpy(k.view_to_clip, c.view_to_clip, 64), memcpy(k.clip_to_view, c.clip_to_view, 64);
|
|
memcpy(k.clip_to_prev_clip, c.clip_to_prev_clip, 64), memcpy(k.prev_clip_to_clip, c.prev_clip_to_clip, 64);
|
|
memcpy(k.pos, c.pos, 12), memcpy(k.up, c.up, 12), memcpy(k.right, c.right, 12), memcpy(k.fwd, c.fwd, 12);
|
|
k.znear = 0.1f, k.zfar = 1e8f, k.fov_y = c.fov_y, k.aspect = c.aspect; // Ocarina's projection (vk_ffx.cpp)
|
|
k.jitter[0] = jx, k.jitter[1] = jy;
|
|
k.mv_scale[0] = 1.0f / (float)d.width, k.mv_scale[1] = 1.0f / (float)d.height; // render pixels
|
|
k.reset = reset;
|
|
}
|
|
S.frame(cb, have ? &zd : NULL, have ? &zm : NULL, NULL, have ? &k : NULL, ext.width, ext.height, on);
|
|
}
|
|
|
|
void vk_dlssg_release(void) {
|
|
if (S.loaded > 0 && S.shutdown) S.shutdown();
|
|
S.active = 0;
|
|
}
|