mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-20 23:19:57 -04:00
Generate frames with DLSS on Linux through NGX itself: 2x to 6x or dynamic, switched while the game runs.
DLSS Frame Generation went through Streamline, which only the Windows build has, so framegen = dlss did nothing on Linux. There it now calls NGX's frame generation feature directly (vk_dlaa.c vk_ngxfg_*), with the depth, motion vectors, jitter, camera and HUD-less picture the Streamline path gets. With it on, each frame is composed into an image of its own instead of a swapchain image, the generated frames are made at the end of the frame's command buffer, and a presenter thread shows them and then the rendered frame, each copied into a swapchain image it acquires itself, spaced over the time between rendered frames (gfx_present.c ng_*). fg_mult is the multiplier: 2 to 6, or dynamic for only the frames the display's refresh rate needs. NVIDIA's libnvidia-ngx-dlssg.so.<version> goes beside the game; it is not in the repository or any kit. Pull request #124 by spidercraft. Its base was the present path of 0.5, so it is ported by hand: - taken as written: the NGX calls and the presenter (the block is lifted from the pull request's diff); - wired again into present_image, create_swapchain, the output rate and the HUD-less picture as they are now, with every new branch behind vk_ngxfg_wanted, which is 0 in the Windows build: the code is compiled there and never reached; - added: frame generation's feature and parameters are released before NGX shuts down, the presenter's images, semaphores and command pool go when the device is released and are made again for a new one, and a failed submit on this path settles its fence without an acquire semaphore; - the menu: the Frame generation row follows the setting at once on Linux and says "after restart" only when the game started without DLSS upscaling; the multiplier row lists what the GPU allows and Dynamic; - the overlay row of the pull request it builds on (#123) is not part of this. It touches vk_dlaa.c, which another session is changing: one field in N, the FG state after it, one FN line, the instance extension query, the capability read in vk_dlaa_init, the release, and a block appended at the end. Known gap, as in the pull request: on this path fps_limit counts the frames the game draws. The Windows files compile without new warnings; nothing was run, and the Linux path is not compiled here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -78,6 +78,7 @@ static struct {
|
||||
PFN_Create create;
|
||||
PFN_Evaluate evaluate;
|
||||
PFN_Release release;
|
||||
void *feat_inst_req; // NVSDK_NGX_VULKAN_GetFeatureInstanceExtensionRequirements (vk_ngxfg), NULL when the core lacks it
|
||||
int tried, loaded, available;
|
||||
int ngx_up; // vk_dlaa_init brought NGX up: DLSS serves this run
|
||||
NgxParam *capsp; // the capability parameters (vk_dlaa_init), which DLSS's statistics callback reads and fills
|
||||
@@ -90,6 +91,30 @@ static struct {
|
||||
#endif
|
||||
} N = {.wanted = -1};
|
||||
|
||||
// DLSS Frame Generation through NGX itself (vk_ngxfg_*, at the end of this file): NX2NAVI_FRAMEGEN = dlss where
|
||||
// Streamline does not run, which is every build but the Windows one.
|
||||
static struct {
|
||||
int max; // generated frames per rendered one this GPU allows (MultiFrameCountMax), 0: none
|
||||
NgxHandle *feat;
|
||||
NgxParam *p;
|
||||
uint32_t w, h; // the picture the feature was created for
|
||||
VkFormat fmt;
|
||||
int failed, reset, evals;
|
||||
} FG;
|
||||
|
||||
// Whether this run can generate frames through NGX: DLSS upscaling on at the start, as NGX comes up only then. The
|
||||
// swapchain then gets the presenter's extra images and NGX is asked about frame generation; whether frames are
|
||||
// generated is NX2NAVI_FRAMEGEN = dlss, read live (vk_ngxfg_on).
|
||||
int vk_ngxfg_wanted(void) {
|
||||
#ifdef _WIN32
|
||||
return 0; // Streamline's path there (vk_dlssg.c)
|
||||
#else
|
||||
static int w = -1;
|
||||
if (w < 0) w = cfg_bool("NX2NAVI_DLAA") && strcmp(cfg_value("NX2NAVI_TAA"), "fsr3");
|
||||
return w;
|
||||
#endif
|
||||
}
|
||||
|
||||
typedef void (*SetI)(NgxParam *, const char *, int);
|
||||
typedef void (*SetU)(NgxParam *, const char *, unsigned);
|
||||
typedef void (*SetF)(NgxParam *, const char *, float);
|
||||
@@ -217,6 +242,7 @@ static int core_load(void) {
|
||||
FN(create, "NVSDK_NGX_VULKAN_CreateFeature");
|
||||
FN(evaluate, "NVSDK_NGX_VULKAN_EvaluateFeature");
|
||||
FN(release, "NVSDK_NGX_VULKAN_ReleaseFeature");
|
||||
FN(feat_inst_req, "NVSDK_NGX_VULKAN_GetFeatureInstanceExtensionRequirements"); // optional: frame generation's
|
||||
#undef FN
|
||||
if (!N.init || !N.req_ext || !N.caps || !N.alloc_params || !N.create || !N.evaluate || !N.release) {
|
||||
rt_log("dlaa: the NGX core %s lacks Vulkan entry points: DLAA off", path);
|
||||
@@ -243,12 +269,51 @@ static int listed(const char *const *list, uint32_t n, const char *s) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// NVSDK_NGX_FeatureDiscoveryInfo with an application id (the layout of the SDK's struct, 64-bit), for the question
|
||||
// which instance extensions frame generation wants.
|
||||
typedef struct {
|
||||
int sdk, feature;
|
||||
struct { int type; union { struct { const char *id; int engine; const char *ver; } project; unsigned long long app; } v; } ident;
|
||||
const wchar_t *path;
|
||||
const void *info;
|
||||
} NgxDiscovery;
|
||||
typedef NgxResult (*PFN_FeatInstReq)(const NgxDiscovery *, uint32_t *, VkExtensionProperties **);
|
||||
#define NGX_FEATURE_FRAME_GENERATION 11 // NVSDK_NGX_Feature_FrameGeneration
|
||||
// The names NGX lists are copied: its list may not outlive the call, and the device creation keeps the pointers.
|
||||
static char fg_names[16][VK_MAX_EXTENSION_NAME_SIZE];
|
||||
static int fg_nnames;
|
||||
static const char *fg_keep(const char *name) {
|
||||
for (int i = 0; i < fg_nnames; i++)
|
||||
if (!strcmp(fg_names[i], name)) return fg_names[i];
|
||||
if (fg_nnames >= 16) return NULL;
|
||||
snprintf(fg_names[fg_nnames], sizeof fg_names[0], "%s", name);
|
||||
return fg_names[fg_nnames++];
|
||||
}
|
||||
|
||||
void vk_dlaa_instance_exts(const char **ext, uint32_t *n, uint32_t max) {
|
||||
unsigned ni = 0, nd = 0;
|
||||
const char **ie = NULL, **de = NULL;
|
||||
if (!vk_dlaa_wanted() || fsr3() || !core_load() || !NGX_OK(N.req_ext(&ni, &ie, &nd, &de))) return;
|
||||
for (unsigned i = 0; i < ni; i++)
|
||||
if (!listed(ext, *n, ie[i]) && *n < max) ext[(*n)++] = ie[i];
|
||||
// Frame generation's own instance extensions (vk_ngxfg_evaluate). Its device extensions are not asked for: the
|
||||
// core's GetFeatureDeviceExtensionRequirements answered "not implemented" (0xBAD00012) with NVIDIA's 615 Linux
|
||||
// driver, and NGX's general list (vk_dlaa_device_exts) served the feature.
|
||||
if (vk_ngxfg_wanted() && N.feat_inst_req) {
|
||||
static const wchar_t here[] = L".";
|
||||
NgxDiscovery d;
|
||||
memset(&d, 0, sizeof d);
|
||||
d.sdk = NGX_VERSION_API, d.feature = NGX_FEATURE_FRAME_GENERATION;
|
||||
d.ident.type = 0, d.ident.v.app = 231313132ull; // the application id vk_dlaa_init gives NGX
|
||||
d.path = here;
|
||||
uint32_t k = 0;
|
||||
VkExtensionProperties *pr = NULL;
|
||||
if (NGX_OK(((PFN_FeatInstReq)N.feat_inst_req)(&d, &k, &pr)) && pr)
|
||||
for (uint32_t i = 0; i < k; i++) {
|
||||
const char *nm = fg_keep(pr[i].extensionName);
|
||||
if (nm && !listed(ext, *n, nm) && *n < max) ext[(*n)++] = nm, rt_log("ngxfg: instance extension %s", nm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void vk_dlaa_device_exts(const VkExtensionProperties *have, uint32_t nhave, const char **ext, uint32_t *n,
|
||||
@@ -354,6 +419,18 @@ void vk_dlaa_init(void) {
|
||||
N.ngx_up = 1;
|
||||
rt_log("dlaa: NGX up; DLSS %s%s", avail ? "available" : "NOT available (is " DLSS_MODEL " next to the executable?)",
|
||||
driver ? "; the driver needs an update" : "");
|
||||
if (vk_ngxfg_wanted() && caps) {
|
||||
int fga = 0;
|
||||
unsigned mx = 0;
|
||||
p_geti(caps, "FrameGeneration.Available", &fga);
|
||||
if (!p_getu(caps, "DLSSG.MultiFrameCountMax", &mx)) mx = 1;
|
||||
// MultiFrameCountMax said 3 (4x) with the 310.9.16 library on an RTX 5090, yet that library evaluated 5
|
||||
// generated frames without an error on NVIDIA's 615 driver, and NVIDIA ships up to 6x on RTX 50 cards: a GPU
|
||||
// with multi-frame generation gets up to 5 (6x).
|
||||
FG.max = fga ? (mx >= 3 ? 5 : mx < 1 ? 1 : (int)mx) : 0;
|
||||
rt_log("ngxfg: DLSS Frame Generation %s (up to %dx)",
|
||||
fga ? "available" : "NOT available (is libnvidia-ngx-dlssg.so.<version> next to the executable?)", FG.max + 1);
|
||||
}
|
||||
}
|
||||
|
||||
int vk_dlaa_available(void) { return vk_dlaa_wanted() && N.available; }
|
||||
@@ -1250,6 +1327,9 @@ void vk_dlaa_release(void) {
|
||||
if (!c) return;
|
||||
for (int i = 0; i < 5; i++)
|
||||
if (D.sizes[i].feat) N.release(D.sizes[i].feat), D.sizes[i].feat = NULL;
|
||||
if (FG.feat) N.release(FG.feat); // frame generation's feature and parameters (vk_ngxfg_evaluate), before NGX goes
|
||||
if (FG.p) N.destroy_params(FG.p);
|
||||
memset(&FG, 0, sizeof FG);
|
||||
if (D.params) N.destroy_params(D.params);
|
||||
if (N.caps_map) N.destroy_params(N.caps_map);
|
||||
for (int i = 0; i < 5; i++) destroy_size(&D.sizes[i]);
|
||||
@@ -1994,3 +2074,129 @@ int vk_dlaa_skip_draw(VkExec *ex, const GfxShader *fs) {
|
||||
return !fxaa;
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------- DLSS Frame Generation through NGX (vk_ngxfg)
|
||||
// gfx_present.c records this into the present command buffer once the frame's picture, HUD included, is in bb. It
|
||||
// generates count frames between the previous call's picture and this one into out[0..count-1] (MultiFrameIndex 1 to
|
||||
// count) and a copy of bb into real; gfx_present.c's presenter shows them in that order, then the rendered one. The
|
||||
// inputs are the ones the Streamline path gets (vk_dlssg.c): the depth, motion vectors, jitter and camera of the frame
|
||||
// DLSS last ran on, and the picture without the HUD.
|
||||
int vk_ngxfg_max(void) { return vk_ngxfg_wanted() && N.available && !FG.failed ? FG.max : 0; }
|
||||
int vk_ngxfg_on(void) { return vk_ngxfg_max() && !strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "dlss"); }
|
||||
void vk_ngxfg_reset(void) { FG.reset = 1; }
|
||||
|
||||
int vk_ngxfg_evaluate(VkCommandBuffer cb, const VkNgxImage *bb, const VkNgxImage *hudless, const VkNgxImage *out, int count,
|
||||
const VkNgxImage *real) {
|
||||
if (!vk_ngxfg_max() || count < 1) return 0;
|
||||
if (count > FG.max) count = FG.max;
|
||||
VkFfxImage d, m;
|
||||
VkImageView dv, mvv;
|
||||
VkDlaaCamera c;
|
||||
float jx = 0, jy = 0;
|
||||
int reset = 0;
|
||||
if (!vk_dlaa_fg_inputs(&d, &m, &jx, &jy, &reset) || !vk_dlaa_fg_views(&dv, &mvv) || !vk_dlaa_fg_camera(&c)) {
|
||||
FG.reset = 1; // a frame without DLSS's inputs: the next generation has no valid previous frame
|
||||
return 0;
|
||||
}
|
||||
if (!FG.p && !NGX_OK(N.alloc_params(&FG.p))) {
|
||||
FG.failed = 1;
|
||||
rt_log("ngxfg: parameter allocation failed: frame generation off");
|
||||
return 0;
|
||||
}
|
||||
NgxParam *p = FG.p;
|
||||
if (FG.feat && (FG.w != bb->w || FG.h != bb->h || FG.fmt != bb->fmt)) {
|
||||
N.release(FG.feat);
|
||||
FG.feat = NULL;
|
||||
}
|
||||
if (!FG.feat) {
|
||||
p_setu(p, "CreationNodeMask", 1);
|
||||
p_setu(p, "VisibilityNodeMask", 1);
|
||||
p_setu(p, "Width", bb->w);
|
||||
p_setu(p, "Height", bb->h);
|
||||
p_setu(p, "DLSSG.BackbufferFormat", (unsigned)bb->fmt);
|
||||
NgxResult r = N.create(cb, NGX_FEATURE_FRAME_GENERATION, p, &FG.feat);
|
||||
if (!NGX_OK(r) || !FG.feat) {
|
||||
FG.failed = 1, FG.feat = NULL;
|
||||
rt_log("ngxfg: DLSS Frame Generation feature creation failed (%08x): frame generation off", (unsigned)r);
|
||||
return 0;
|
||||
}
|
||||
FG.w = bb->w, FG.h = bb->h, FG.fmt = bb->fmt, FG.reset = 1;
|
||||
rt_log("ngxfg: DLSS Frame Generation created at %ux%u (format %d), depth and motion %ux%u", bb->w, bb->h,
|
||||
(int)bb->fmt, d.width, d.height);
|
||||
}
|
||||
NgxResourceVK rb = res_image(bb->view, bb->img, bb->fmt, bb->w, bb->h, VK_IMAGE_ASPECT_COLOR_BIT, 0);
|
||||
NgxResourceVK rz = res_image(dv, d.image, d.format, d.width, d.height, VK_IMAGE_ASPECT_DEPTH_BIT, 0);
|
||||
NgxResourceVK rm = res_image(mvv, m.image, m.format, m.width, m.height, VK_IMAGE_ASPECT_COLOR_BIT, 0);
|
||||
NgxResourceVK rh = hudless ? res_image(hudless->view, hudless->img, hudless->fmt, hudless->w, hudless->h,
|
||||
VK_IMAGE_ASPECT_COLOR_BIT, 0) : rb;
|
||||
NgxResourceVK rr = res_image(real->view, real->img, real->fmt, real->w, real->h, VK_IMAGE_ASPECT_COLOR_BIT, 1);
|
||||
p_setp(p, "DLSSG.Backbuffer", &rb);
|
||||
p_setp(p, "DLSSG.Depth", &rz);
|
||||
p_setp(p, "DLSSG.MVecs", &rm);
|
||||
p_setp(p, "DLSSG.HUDLess", hudless ? &rh : NULL);
|
||||
p_setp(p, "DLSSG.UI", NULL);
|
||||
p_setp(p, "DLSSG.UIAlpha", NULL);
|
||||
p_setp(p, "DLSSG.BidirectionalDistortionField", NULL);
|
||||
p_setp(p, "DLSSG.OutputReal", &rr);
|
||||
p_setp(p, "DLSSG.OutputDisableInterpolation", NULL);
|
||||
p_setp(p, "DLSSG.CameraViewToClip", c.view_to_clip);
|
||||
p_setp(p, "DLSSG.ClipToCameraView", c.clip_to_view);
|
||||
p_setp(p, "DLSSG.ClipToLensClip", NULL);
|
||||
p_setp(p, "DLSSG.ClipToPrevClip", c.clip_to_prev_clip);
|
||||
p_setp(p, "DLSSG.PrevClipToClip", c.prev_clip_to_clip);
|
||||
p_setf(p, "DLSSG.JitterOffsetX", jx);
|
||||
p_setf(p, "DLSSG.JitterOffsetY", jy);
|
||||
p_setf(p, "DLSSG.MvecScaleX", 1.0f / (float)m.width); // the vectors are in render pixels
|
||||
p_setf(p, "DLSSG.MvecScaleY", 1.0f / (float)m.height);
|
||||
p_setf(p, "DLSSG.CameraPinholeOffsetX", 0.0f);
|
||||
p_setf(p, "DLSSG.CameraPinholeOffsetY", 0.0f);
|
||||
static const char *const pos[] = {"DLSSG.CameraPosX", "DLSSG.CameraPosY", "DLSSG.CameraPosZ"};
|
||||
static const char *const up[] = {"DLSSG.CameraUpX", "DLSSG.CameraUpY", "DLSSG.CameraUpZ"};
|
||||
static const char *const rt[] = {"DLSSG.CameraRightX", "DLSSG.CameraRightY", "DLSSG.CameraRightZ"};
|
||||
static const char *const fw[] = {"DLSSG.CameraFwdX", "DLSSG.CameraFwdY", "DLSSG.CameraFwdZ"};
|
||||
for (int i = 0; i < 3; i++)
|
||||
p_setf(p, pos[i], c.pos[i]), p_setf(p, up[i], c.up[i]), p_setf(p, rt[i], c.right[i]), p_setf(p, fw[i], c.fwd[i]);
|
||||
p_setf(p, "DLSSG.CameraNear", 0.1f); // Ocarina's projection, as vk_dlssg.c gives it
|
||||
p_setf(p, "DLSSG.CameraFar", 1e8f);
|
||||
p_setf(p, "DLSSG.CameraFOV", c.fov_y);
|
||||
p_setf(p, "DLSSG.CameraAspectRatio", c.aspect);
|
||||
p_setu(p, "DLSSG.ColorBuffersHDR", 0);
|
||||
p_setu(p, "DLSSG.DepthInverted", 0);
|
||||
p_setu(p, "DLSSG.CameraMotionIncluded", 1);
|
||||
p_setu(p, "DLSSG.Reset", (unsigned)(reset || FG.reset));
|
||||
p_setu(p, "DLSSG.AutomodeOverrideReset", 0);
|
||||
p_setu(p, "DLSSG.NotRenderingGameFrames", 0);
|
||||
p_setu(p, "DLSSG.OrthoProjection", 0);
|
||||
p_setu(p, "DLSSG.MvecDilated", 0);
|
||||
p_setu(p, "DLSSG.MenuDetectionEnabled", 0);
|
||||
struct { const char *base; uint32_t w, h; } rects[] = {
|
||||
{"DLSSG.MVecsSubrect", m.width, m.height}, {"DLSSG.DepthSubrect", d.width, d.height},
|
||||
{"DLSSG.HUDLessSubrect", bb->w, bb->h}, {"DLSSG.InputBackbufferSubrect", bb->w, bb->h},
|
||||
{"DLSSG.OutputInterpolatedSubrect", bb->w, bb->h}, {"DLSSG.OutputRealSubrect", bb->w, bb->h}};
|
||||
for (size_t i = 0; i < sizeof rects / sizeof *rects; i++) {
|
||||
char k[64];
|
||||
snprintf(k, sizeof k, "%sBaseX", rects[i].base), p_setu(p, k, 0);
|
||||
snprintf(k, sizeof k, "%sBaseY", rects[i].base), p_setu(p, k, 0);
|
||||
snprintf(k, sizeof k, "%sWidth", rects[i].base), p_setu(p, k, rects[i].w);
|
||||
snprintf(k, sizeof k, "%sHeight", rects[i].base), p_setu(p, k, rects[i].h);
|
||||
}
|
||||
p_setu(p, "DLSSG.MultiFrameCount", (unsigned)count);
|
||||
int made = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
NgxResourceVK ro = res_image(out[i].view, out[i].img, out[i].fmt, out[i].w, out[i].h, VK_IMAGE_ASPECT_COLOR_BIT, 1);
|
||||
p_setp(p, "DLSSG.OutputInterpolated", &ro);
|
||||
p_setu(p, "DLSSG.MultiFrameIndex", (unsigned)(i + 1));
|
||||
NgxResult r = N.evaluate(cb, FG.feat, p, NULL);
|
||||
if (!NGX_OK(r)) {
|
||||
static int told;
|
||||
if (told++ < 5) rt_log("ngxfg: evaluation %d of %d failed (%08x)", i + 1, count, (unsigned)r);
|
||||
break;
|
||||
}
|
||||
made++;
|
||||
}
|
||||
full_barrier(cb);
|
||||
FG.reset = 0;
|
||||
if (made && (++FG.evals == 1 || FG.evals % 3600 == 0))
|
||||
rt_log("ngxfg: %d frames with generation (%d generated per rendered frame)", FG.evals, made);
|
||||
return made;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user