Implement Native NVIDIA Reflex Low Latency (VK_NV_low_latency2)

This commit is contained in:
Tears of the SteelTemple committed 2026-11-03 23:38:49 +02:00
1 parent 0f65b4ee07
commit 51fa4a982b
10 files changed
+137 -5

No files matched your search

+3 -3
View File
@@ -48,7 +48,7 @@ static const char *const known[] = {
"MV_SURVEY",
"ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT",
"PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC",
"PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX",
"RENDER_SCALE", "REPLAY_ASYNC",
"REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES",
"ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE",
@@ -72,7 +72,7 @@ static const struct { const char *key, *section; } sections[] = {
{"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"},
{"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"},
{"HDR_UI_NITS", "display"}, {"HDR_HUE", "display"}, {"SHADOW_RES", "display"},
{"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"FXAA", "display"},
{"DLAA", "display"}, {"DLSS", "display"}, {"DLSS_PRESET", "display"}, {"DLSS_CHAR_MASK", "display"}, {"REFLEX", "display"}, {"FXAA", "display"},
{"DEVICE_PROFILE", "display"}, {"ASSUME_FOCUS", "display"},
{"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"},
{"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"CONTACT_SHADOWS", "display"},
@@ -117,7 +117,7 @@ static const CfgMeta metas[] = {
{"UPSCALER", "fsr", "fsr|bilinear", LIVE}, {"SHARPNESS", "80", "0..100", LIVE},
{"DLAA", "0", "0|1", RESTART}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE},
{"DLSS_PRESET", "default", "default|k|j", LIVE},
{"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE},
{"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"REFLEX", "off", "off|on|boost", LIVE}, {"FXAA", "1", "0|1", LIVE},
{"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE},
{"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features
// colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses
+103 -1
View File
@@ -22,6 +22,7 @@
#include "fs.h"
#include "gfx.h"
#include "gfx_wsys.h"
#include "host.h"
#include "nvn_api.h"
#include "platform/platform.h"
#include "ui/menu.h"
@@ -227,6 +228,8 @@ unsigned gfx_window_focus_messages(void) { return (unsigned)g_focus_msgs; }
// Present path (g_pl held): a settings change (cfg_version) reaches the window thread, which applies a changed size or
// fullscreen; a changed TOTK_VK_PRESENT rebuilds the swapchain at this present.
static void reflex_apply_locked(void);
static void window_settings_live(void) {
static unsigned seen;
static int started;
@@ -241,12 +244,104 @@ static void window_settings_live(void) {
if (started) plat_xchg32(&g_dirty, 1);
}
if (started) gfx_window_apply_settings();
reflex_apply_locked();
started = 1;
}
// ---- swapchain
static const char *g_present_got = ""; // the TOTK_VK_PRESENT value of the mode the swapchain got; "" before one
// ---- NVIDIA Reflex Low Latency (VK_NV_low_latency2)
typedef struct {
VkStructureType sType;
const void *pNext;
VkBool32 lowLatencyMode;
VkBool32 lowLatencyBoost;
uint32_t minimumIntervalUs;
} ReflexModeInfoNV;
typedef struct {
VkStructureType sType;
const void *pNext;
VkSemaphore sleepSemaphore;
uint64_t value;
} ReflexSleepInfoNV;
typedef struct {
VkStructureType sType;
const void *pNext;
uint64_t presentID;
int marker;
} ReflexMarkerInfoNV;
typedef VkResult(VKAPI_PTR *ReflexSetModeFn)(VkDevice, VkSwapchainKHR, const ReflexModeInfoNV *);
typedef VkResult(VKAPI_PTR *ReflexSleepFn)(VkDevice, VkSwapchainKHR, const ReflexSleepInfoNV *);
typedef void(VKAPI_PTR *ReflexMarkerFn)(VkDevice, VkSwapchainKHR, const ReflexMarkerInfoNV *);
static ReflexSetModeFn g_reflex_set_mode;
static ReflexSleepFn g_reflex_sleep;
static ReflexMarkerFn g_reflex_marker;
static VkSemaphore g_reflex_sem;
static uint64_t g_reflex_val;
static uint64_t g_reflex_present_id = 1;
static int g_reflex_mode = -1; // 0=off, 1=on, 2=boost
static uint32_t g_reflex_min_us = 1;
static void reflex_apply_locked(void) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_sc) return;
if (!g_reflex_set_mode) {
g_reflex_set_mode = (ReflexSetModeFn)vkGetDeviceProcAddr(c->device, "vkSetLatencySleepModeNV");
g_reflex_sleep = (ReflexSleepFn)vkGetDeviceProcAddr(c->device, "vkLatencySleepNV");
g_reflex_marker = (ReflexMarkerFn)vkGetDeviceProcAddr(c->device, "vkSetLatencyMarkerNV");
}
if (!g_reflex_set_mode) return;
if (!g_reflex_sem) {
VkSemaphoreTypeCreateInfo st = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO,
.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE, .initialValue = 0};
VkSemaphoreCreateInfo si = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, .pNext = &st};
vkCreateSemaphore(c->device, &si, NULL, &g_reflex_sem);
g_reflex_val = 0;
}
const char *m = cfg_value("TOTK_REFLEX");
int mode = !strcmp(m, "boost") ? 2 : !strcmp(m, "on") ? 1 : 0;
double hz = frame_step_limit_hz();
uint32_t min_us = hz > 0 ? (uint32_t)(1000000.0 / hz + 0.5) : 0;
if (mode == g_reflex_mode && min_us == g_reflex_min_us) return;
g_reflex_mode = mode;
g_reflex_min_us = min_us;
ReflexModeInfoNV mi = {.sType = (VkStructureType)1000505000, .lowLatencyMode = mode > 0 ? VK_TRUE : VK_FALSE,
.lowLatencyBoost = mode == 2 ? VK_TRUE : VK_FALSE, .minimumIntervalUs = min_us};
VkResult r = g_reflex_set_mode(c->device, g_sc, &mi);
rt_log("REFLEX: NVIDIA Reflex Low Latency -> %s (minInterval=%u us, VK_NV_low_latency2=%s)",
mode == 2 ? "ON + BOOST" : mode == 1 ? "ON" : "OFF", min_us, r == VK_SUCCESS ? "OK" : "ERR");
}
int vk_has_reflex(void) {
VkCtx *c = vk_ctx();
return c ? c->has_nv_reflex : 0;
}
void vk_reflex_marker(int marker) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_reflex_marker || g_reflex_mode <= 0 || !g_sc) return;
uint64_t pid = __atomic_load_n(&g_reflex_present_id, __ATOMIC_RELAXED);
ReflexMarkerInfoNV mi = {.sType = (VkStructureType)1000505003, .presentID = pid, .marker = marker};
g_reflex_marker(c->device, g_sc, &mi);
}
void vk_reflex_sleep(void) {
VkCtx *c = vk_ctx();
if (!c || !c->has_nv_reflex || !g_reflex_sleep || !g_reflex_sem || g_reflex_mode <= 0 || !g_sc) return;
uint64_t val = __atomic_add_fetch(&g_reflex_val, 1, __ATOMIC_RELAXED);
ReflexSleepInfoNV si = {.sType = (VkStructureType)1000505001, .sleepSemaphore = g_reflex_sem, .value = val};
if (g_reflex_sleep(c->device, g_sc, &si) == VK_SUCCESS) {
VkSemaphoreWaitInfo wi = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO, .semaphoreCount = 1,
.pSemaphores = &g_reflex_sem, .pValues = &val};
vkWaitSemaphores(c->device, &wi, 50000000ull);
}
}
static VkPresentModeKHR pick_present_mode(VkPhysicalDevice pd) {
const char *e = cfg_value("TOTK_VK_PRESENT");
VkPresentModeKHR want = !strcmp(e, "mailbox") ? VK_PRESENT_MODE_MAILBOX_KHR
@@ -276,12 +371,15 @@ static void flip_locked(void) {
if (!g_flip_on) return;
g_flip_on = 0;
if (!g_sc) return;
vk_reflex_marker(4); // VK_LATENCY_MARKER_PRESENT_START_NV
VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .waitSemaphoreCount = 1,
.pWaitSemaphores = &g_render_done[g_flip_idx], .swapchainCount = 1, .pSwapchains = &g_sc,
.pImageIndices = &g_flip_idx};
vk_queue_lock();
VkResult r = vkQueuePresentKHR(vk_ctx()->gfx_queue, &pi);
vk_queue_unlock();
vk_reflex_marker(5); // VK_LATENCY_MARKER_PRESENT_END_NV
__atomic_add_fetch(&g_reflex_present_id, 1, __ATOMIC_RELAXED);
vk_device_fault_report(r);
if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR) plat_xchg32(&g_dirty, 1);
}
@@ -364,6 +462,8 @@ static int create_swapchain(void) {
}
free_capture();
vk_queue_unlock();
g_reflex_mode = -1;
reflex_apply_locked();
plat_xchg32(&g_dirty, 0);
return 1;
}
@@ -778,9 +878,11 @@ void gfx_window_release_vk(void) {
g_sc_view[i] = VK_NULL_HANDLE;
}
if (g_pool) vkDestroyCommandPool(c->device, g_pool, NULL);
if (g_reflex_sem) vkDestroySemaphore(c->device, g_reflex_sem, NULL);
if (g_sc) vkDestroySwapchainKHR(c->device, g_sc, NULL);
if (g_surface) vkDestroySurfaceKHR(c->instance, g_surface, NULL);
g_sc = VK_NULL_HANDLE; g_surface = VK_NULL_HANDLE; g_pool = VK_NULL_HANDLE; g_sc_n = 0;
g_sc = VK_NULL_HANDLE; g_surface = VK_NULL_HANDLE; g_pool = VK_NULL_HANDLE; g_reflex_sem = VK_NULL_HANDLE; g_sc_n = 0;
g_reflex_mode = -1;
g_started = 0; // the window and its thread stay; the next gfx_window_start makes a new surface
}
plat_rmutex_unlock(&g_pl);
+5
View File
@@ -16,6 +16,7 @@
#include "nvn_api.h"
#include "nvn_hmap.h"
#include "nvn_tex.h"
#include "vk.h"
#include "platform/platform.h"
// ---------- Descriptor pools
@@ -682,6 +683,9 @@ NVN_API(nvnWindowAcquireTexture) {
sleep_until(at);
perf_end(PERF_PACE, p0);
}
vk_reflex_sleep();
vk_reflex_marker(0); // VK_LATENCY_MARKER_SIMULATION_START_NV
vk_reflex_marker(6); // VK_LATENCY_MARKER_INPUT_SAMPLE_NV
// The sync means "the display no longer reads this image". Presents are copies queued in submit order, so it is
// already satisfied: fenced = 3 with fence 0.
if (s) {
@@ -704,6 +708,7 @@ NVN_API(nvnQueuePresentTexture) {
if (o->magic != NVN_WIN_MAGIC || idx < 0 || idx >= o->ntex || idx >= 4 || !o->tex[idx])
rt_fatal("nvn: QueuePresentTexture of image %d on window %p", idx, (void *)o);
o->presents++;
vk_reflex_marker(1); // VK_LATENCY_MARKER_SIMULATION_END_NV
const tex_t *t = (const tex_t *)o->tex[idx];
uint64_t period = present_period(o), now = kern_now_ns();
uint64_t slot = now + pace(&o->pace, now, period);
+5
View File
@@ -51,6 +51,7 @@ extern "C" {
void rt_log(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
void vk_queue_lock(void);
void vk_queue_unlock(void);
int vk_has_reflex(void);
void res_sizes(unsigned out[2], unsigned scene[2]);
double frame_step_limit_hz(void);
extern const int rt_game_version;
@@ -685,6 +686,10 @@ static void tab_screen(float scale) {
static const UiTextId chars[] = {UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM,
UI_MENU_DLSS_CHARS_FULL};
opt_choice("TOTK_DLSS_CHAR_MASK", UI_MENU_DLSS_CHARS, chars, 4, UI_MENU_DLSS_CHARS_DESC);
if (vk_has_reflex()) {
static const UiTextId reflex[] = {UI_MENU_REFLEX_OFF, UI_MENU_REFLEX_ON, UI_MENU_REFLEX_BOOST};
opt_choice("TOTK_REFLEX", UI_MENU_REFLEX, reflex, 3, UI_MENU_REFLEX_DESC);
}
opt_toggle("TOTK_FXAA", UI_MENU_FXAA, UI_MENU_FXAA_DESC);
rows_color();
opt_group(UI_MENU_HDR_GROUP);
+5
View File
@@ -160,6 +160,11 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_DLSS_PERFORMANCE] = "Performance", [UI_MENU_DLSS_ULTRA] = "Ultra Performance",
[UI_MENU_FXAA] = "Game anti-aliasing (FXAA)",
[UI_MENU_FXAA_DESC] = "The game's own FXAA edge smoothing. It blurs fine detail a little; with DLAA on it is not needed.",
[UI_MENU_REFLEX] = "NVIDIA Reflex Low Latency",
[UI_MENU_REFLEX_DESC] = "Reduces system latency on NVIDIA GPUs using VK_NV_low_latency2. On + Boost keeps GPU clocks high in CPU-bound scenes.",
[UI_MENU_REFLEX_OFF] = "Off",
[UI_MENU_REFLEX_ON] = "On",
[UI_MENU_REFLEX_BOOST] = "On + Boost",
[UI_MENU_FPS_LIMIT] = "Frame rate limit",
[UI_MENU_FPS_LIMIT_DESC] = "The most frames per second the game draws. Above 60 is experimental and needs Game speed "
+1
View File
@@ -57,6 +57,7 @@ typedef enum {
UI_MENU_DLSS_BALANCED, UI_MENU_DLSS_PERFORMANCE, UI_MENU_DLSS_ULTRA, UI_MENU_DLSS_PRESET, UI_MENU_DLSS_PRESET_DESC,
UI_MENU_DLSS_PRESET_DEFAULT, UI_MENU_DLSS_PRESET_K, UI_MENU_DLSS_PRESET_J, UI_MENU_DLSS_CHARS, UI_MENU_DLSS_CHARS_DESC,
UI_MENU_DLSS_CHARS_OFF, UI_MENU_DLSS_CHARS_LIGHT, UI_MENU_DLSS_CHARS_MEDIUM, UI_MENU_DLSS_CHARS_FULL, UI_MENU_FXAA, UI_MENU_FXAA_DESC,
UI_MENU_REFLEX, UI_MENU_REFLEX_DESC, UI_MENU_REFLEX_OFF, UI_MENU_REFLEX_ON, UI_MENU_REFLEX_BOOST,
// Game
UI_MENU_FPS_LIMIT, UI_MENU_FPS_LIMIT_DESC, UI_MENU_FPS_30, UI_MENU_FPS_40, UI_MENU_FPS_45, UI_MENU_FPS_60, UI_MENU_FPS_90,
+5 -1
View File
@@ -36,7 +36,8 @@ typedef struct VkCtx {
has_nv_checkpoints, // VK_NV_device_diagnostic_checkpoints (TOTK_PASS_CRUMBS=1, vk_crumbs.c)
has_astc_ldr, // textureCompressionASTC_LDR (enabled when reported)
has_calibrated, // VK_KHR/EXT_calibrated_timestamps (GPU idle split in the perf line)
has_hdr_metadata; // VK_EXT_hdr_metadata (vk_hdr.c vk_hdr_metadata)
has_hdr_metadata, // VK_EXT_hdr_metadata (vk_hdr.c vk_hdr_metadata)
has_nv_reflex; // VK_NV_low_latency2 (NVIDIA Reflex Low Latency)
// Feedback loops (maxwell.md 1.9, vk_exec_draw.c feedback_prepare): a draw samples a target of its open pass that
// an earlier draw of the pass wrote. TOTK_FEEDBACK=0: VK_FEEDBACK_OFF (sampled in GENERAL without a barrier, the
// old behaviour); VK_FEEDBACK_LAYOUT with VK_EXT_attachment_feedback_loop_layout and dynamicRenderingLocalRead
@@ -144,6 +145,9 @@ void vk_present_blit(VkImage src, VkImageLayout layout, VkFormat fmt, int w, int
int vk_fsr_record(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat fmt, int sw, int sh, const int crop[4],
VkImage dst, const int out[4]);
void vk_fsr_release(void); // before the device goes away
void vk_reflex_marker(int marker);
void vk_reflex_sleep(void);
int vk_has_reflex(void);
// Colour adjustment on the present (vk_color.c): with TOTK_COLOR_* away from their defaults, records a pass that writes
// src's crop rectangle {x, y, w, h}, adjusted, into an RGBA16F image of that size in GENERAL and returns it (the caller
// scales that one instead, crop {0, 0, w, h}); VK_NULL_HANDLE = nothing recorded. src is in GENERAL, fmt its format.
+5
View File
@@ -278,6 +278,11 @@ static int create_device(void) {
g_vk.has_nv_checkpoints = 1;
ext[n_ext++] = VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME;
}
// NVIDIA Reflex Low Latency (VK_NV_low_latency2)
if (has_ext(ep, ne, "VK_NV_low_latency2")) {
g_vk.has_nv_reflex = 1;
ext[n_ext++] = "VK_NV_low_latency2";
}
// Graphics pipeline library: chained last so it can be dropped again when vk_caps_gpl declines it.
void **gpl_link = NULL;
if (has_ext(ep, ne, VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME) && has_ext(ep, ne, VK_EXT_GRAPHICS_PIPELINE_LIBRARY_EXTENSION_NAME)) {
+2
View File
@@ -439,9 +439,11 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) {
.pCommandBufferInfos = pro ? cbi : cbi + 1, .signalSemaphoreInfoCount = 1,
.pSignalSemaphoreInfos = &sig};
ts_submitted();
if (recorded) vk_reflex_marker(2); // VK_LATENCY_MARKER_RENDERSUBMIT_START_NV
vk_queue_lock();
VkResult r = vkQueueSubmit2(c->gfx_queue, 1, &si, VK_NULL_HANDLE);
vk_queue_unlock();
if (recorded) vk_reflex_marker(3); // VK_LATENCY_MARKER_RENDERSUBMIT_END_NV
if (r != VK_SUCCESS) {
vk_device_fault_report(r);
rt_fatal("vk: vkQueueSubmit2 of value %llu failed (%d)", (unsigned long long)fence, (int)r);
+3
View File
@@ -220,6 +220,9 @@
# frame before, DLSS is told to favour the current frame by this percentage (0, 35, 65 or 100; 0 off). Higher removes
# more of the trails and leaves those edges a little less smooth. Changes while the game runs take effect at once.
# dlss_char_mask = 65
# NVIDIA Reflex Low Latency (NVIDIA GPUs supporting VK_NV_low_latency2): off, on, or boost.
# Reduces system input latency; boost keeps GPU clocks high in CPU-bound scenes. Changes take effect at once.
# reflex = off
# The game's own FXAA after the post effects; with DLAA on it is not needed. Changes while the game runs take effect at
# once.
# fxaa = 1