From 84e44f3aedc948714ec4e9925c969c68a55338e2 Mon Sep 17 00:00:00 2001 From: ripps818 Date: Sun, 22 Nov 2026 23:24:48 -0600 Subject: [PATCH 1/2] Add AMD Anti-Lag 2 to the low latency setting, with an auto value that picks Reflex or Anti-Lag for the GPU The reflex setting gains an auto value. With auto, on or boost the device uses NVIDIA Reflex when the driver has VK_NV_low_latency2, otherwise AMD Anti-Lag 2 when it has VK_AMD_anti_lag (Mesa RADV, AMD's Windows driver); with neither it does nothing. Anti-Lag marks the INPUT stage where the window texture is acquired and the PRESENT stage just before vkQueuePresentKHR, with the pacer's frame limit as maxFPS. The default stays off. The menu row is renamed Low Latency in all four languages. Co-Authored-By: Claude Sonnet 5.5 --- docs/settings.md | 2 +- runtime/cfg.c | 2 +- runtime/gfx.h | 4 ++++ runtime/gfx_present.c | 29 ++++++++++++++++++++++++++--- runtime/nvn_api_obj.c | 4 +++- runtime/ui/menu.cpp | 4 ++-- runtime/ui_text.c | 20 ++++++++++++-------- runtime/ui_text.h | 2 +- runtime/vk.h | 3 ++- runtime/vk_device.c | 17 ++++++++++++++--- totk.ini.example | 6 ++++-- 11 files changed, 70 insertions(+), 23 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 5258f39c..974f6f0c 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -208,7 +208,7 @@ Many rows below are developer tools that only act together with another setting | `mvprev_grow` (default `1`) | `0` keeps only three banks in the previous-frame tables. | Object motion that depends on the other banks is no longer tracked. | Caution | | `mvprev_skip` | Hex bank mask of banks left out of the previous-frame copy, replacing the default. | Different banks are tracked, which changes object motion. | Caution | | `mvprev_trust` | Hex bank mask of banks whose found previous-frame copy is taken without the check. | A copy that does not match is used anyway, which can give wrong motion. | Caution | -| `reflex` (default `off`) | `off`, `on`, or `boost`; enables NVIDIA Reflex Low Latency (`VK_NV_low_latency2`) to reduce system input latency. Read at start. | On NVIDIA GPUs supporting the extension, coordinates simulation and presentation cadence to lower latency, holding frames to the frame limit the pacer uses (`fps_limit`, the monitor's rate for `display`); `boost` keeps GPU clocks high in CPU-bound scenes. Has no effect on non-NVIDIA GPUs. | Safe | +| `reflex` (default `off`) | `off`, `auto`, `on`, or `boost`; reduces system input latency with what the GPU supports: NVIDIA Reflex Low Latency (`VK_NV_low_latency2`) or AMD Anti-Lag 2 (`VK_AMD_anti_lag`, Mesa RADV and AMD's Windows driver). Read at start. | `auto` and `on` pick the extension the GPU has and do nothing when it has neither. Both coordinate simulation and presentation so input reaches the screen sooner when the GPU is the limit, holding frames to the frame limit the pacer uses (`fps_limit`, the monitor's rate for `display`). `boost` also keeps NVIDIA GPU clocks high in CPU-bound scenes. | Safe | ## Scheduling and host diff --git a/runtime/cfg.c b/runtime/cfg.c index b3dcbb2b..77d2152d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -146,7 +146,7 @@ static const CfgMeta metas[] = { {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE}, {"FSR_SHARPNESS", "80", "0..100", LIVE}, // vk_dlaa.c fsr_evaluate {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"DLSS_FIXED_EXPOSURE", "0", "0|1", LIVE}, {"DLAA_EFFECTS_AFTER", "0", "0|1", LIVE}, - {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"REFLEX", "off", "off|on|boost", RESTART}, {"FXAA", "1", "0|1", LIVE}, + {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"REFLEX", "off", "off|auto|on|boost", RESTART}, {"FXAA", "1", "0|1", LIVE}, {"LOD_STEADY", "0", "0|1", RESTART}, // sets steady {"ASPECT", "fit", "fit|stretch", LIVE}, {"MOVIE_ASPECT", "keep", "keep|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, {"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features diff --git a/runtime/gfx.h b/runtime/gfx.h index c3dedfc8..43abcb00 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -33,6 +33,10 @@ void vk_reflex_marker(int marker, uint64_t present_id); // present_id: the fra uint64_t vk_reflex_frame_begin(void); // SIMULATION_START: the new frame's present ID uint64_t vk_reflex_frame(void); // the ID of the frame the game simulates now void vk_reflex_sleep(void); // before SIMULATION_START: the driver's latency sleep +// AMD Anti-Lag 2 (gfx_present.c, VK_AMD_anti_lag): the driver paces the CPU from the INPUT call to the PRESENT call of a +// frame. Both return at once with low latency off or the GPU lacking the extension. +void vk_antilag_input(uint64_t frame); // before the frame's input and simulation; may sleep +void vk_antilag_present(uint64_t frame); // right before the frame's vkQueuePresentKHR // ---- shader service client (gfx_shader.c) enum { GFX_STAGE_VS, GFX_STAGE_TCS, GFX_STAGE_TES, GFX_STAGE_GS, GFX_STAGE_FS, GFX_STAGE_CS }; diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index fc1ca561..6b3f9796 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -390,8 +390,9 @@ static void latency_note(VkCtx *c, uint64_t pid) { "end %.2f ms", full, gi.timingCount, full ? sum / full / 1000.0 : 0.0); } } - rt_log("reflex: frame start to present %.2f ms over %llu frames (reflex %s)%s", g_lat.n ? g_lat.ns / 1e6 / g_lat.n : 0.0, - (unsigned long long)g_lat.n, g_reflex_mode == 2 ? "boost" : g_reflex_mode == 1 ? "on" : "off", drv); + rt_log("reflex: frame start to present %.2f ms over %llu frames (reflex %s, anti-lag %s)%s", g_lat.n ? g_lat.ns / 1e6 / g_lat.n : 0.0, + (unsigned long long)g_lat.n, g_reflex_mode == 2 ? "boost" : g_reflex_mode == 1 ? "on" : "off", + c->has_amd_antilag ? "on" : "off", drv); g_lat.n = g_lat.ns = 0; } @@ -405,7 +406,8 @@ uint64_t vk_reflex_submit_id(void) { return t_reflex_submit ? t_reflex_submit : void vk_reflex_present_frame(uint64_t id) { // g_blit_pid feeds flip_locked's VkPresentIdKHR (Reflex) and latency_note (TOTK_TRACE=perf); with neither, the // present skips the lock - if (!vk_has_reflex() && !(rt_trace & RT_TRACE_PERF)) return; + VkCtx *c = vk_ctx(); + if (!vk_has_reflex() && !(c && c->has_amd_antilag) && !(rt_trace & RT_TRACE_PERF)) return; plat_rmutex_lock(&g_pl); g_blit_pid = id; plat_rmutex_unlock(&g_pl); @@ -452,6 +454,26 @@ void vk_reflex_sleep(void) { } } +// ---- AMD Anti-Lag 2 (VK_AMD_anti_lag) +static PFN_vkAntiLagUpdateAMD g_antilag_update; + +static void antilag_update(VkAntiLagStageAMD stage, uint64_t frame) { + VkCtx *c = vk_ctx(); + if (!c || !c->has_amd_antilag || !frame) return; + if (!g_antilag_update) g_antilag_update = (PFN_vkAntiLagUpdateAMD)vkGetDeviceProcAddr(c->device, "vkAntiLagUpdateAMD"); + if (!g_antilag_update) return; + // the driver holds frames to the pacer's frame limit, as Reflex's minimum interval does (0: no limit known) + double hz = frame_step_limit_hz(); + VkAntiLagPresentationInfoAMD pi = { + .sType = VK_STRUCTURE_TYPE_ANTI_LAG_PRESENTATION_INFO_AMD, .stage = stage, .frameIndex = frame}; + VkAntiLagDataAMD d = {.sType = VK_STRUCTURE_TYPE_ANTI_LAG_DATA_AMD, .mode = VK_ANTI_LAG_MODE_ON_AMD, + .maxFPS = hz > 0 ? (uint32_t)(hz + 0.5) : 0, .pPresentationInfo = &pi}; + g_antilag_update(c->device, &d); +} + +void vk_antilag_input(uint64_t frame) { antilag_update(VK_ANTI_LAG_STAGE_INPUT_AMD, frame); } +void vk_antilag_present(uint64_t frame) { antilag_update(VK_ANTI_LAG_STAGE_PRESENT_AMD, frame); } + // ---- swapchain static const char *g_present_got = ""; // the TOTK_VK_PRESENT value of the mode the swapchain got; "" before one @@ -503,6 +525,7 @@ static void flip_one(void) { pid_info.pPresentIds = &pid; vk_reflex_marker(4, pid); // VK_LATENCY_MARKER_PRESENT_START_NV } + if (!generated) vk_antilag_present(frame_pid); VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, .pNext = with_id ? &pid_info : NULL, .waitSemaphoreCount = 1, diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index 8500a70b..d2017718 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -688,7 +688,9 @@ NVN_API(nvnWindowAcquireTexture) { perf_end(PERF_PACE, p0); } vk_reflex_sleep(); - vk_reflex_marker(0, vk_reflex_frame_begin()); // VK_LATENCY_MARKER_SIMULATION_START_NV: a new frame and its ID + uint64_t fid = vk_reflex_frame_begin(); + vk_reflex_marker(0, fid); // VK_LATENCY_MARKER_SIMULATION_START_NV: a new frame and its ID + vk_antilag_input(fid); // 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) { diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 60684572..520b088b 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -838,8 +838,8 @@ static void tab_screen(float scale) { opt_toggle("TOTK_DLSS_FIXED_EXPOSURE", UI_MENU_DLSS_FIXED_EXPOSURE, UI_MENU_DLSS_FIXED_EXPOSURE_DESC); opt_toggle("TOTK_DLAA_EFFECTS_AFTER", UI_MENU_DLAA_EFFECTS_AFTER, UI_MENU_DLAA_EFFECTS_AFTER_DESC); } - 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); + static const UiTextId reflex[] = {UI_MENU_REFLEX_OFF, UI_MENU_REFLEX_AUTO, UI_MENU_REFLEX_ON, UI_MENU_REFLEX_BOOST}; + opt_choice("TOTK_REFLEX", UI_MENU_REFLEX, reflex, 4, UI_MENU_REFLEX_DESC); opt_toggle("TOTK_FXAA", UI_MENU_FXAA, UI_MENU_FXAA_DESC); rows_color(); opt_group(UI_MENU_HDR_GROUP); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 5d2a1a8f..3eb91d44 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -174,8 +174,9 @@ 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] = "Low Latency", + [UI_MENU_REFLEX_DESC] = "Reduces system latency. Auto picks NVIDIA Reflex or AMD Anti-Lag 2 for the GPU (VK_NV_low_latency2, VK_AMD_anti_lag); On + Boost keeps NVIDIA GPU clocks high in CPU-bound scenes. Applies after a restart.", + [UI_MENU_REFLEX_AUTO] = "Auto", [UI_MENU_REFLEX_OFF] = "Off", [UI_MENU_REFLEX_ON] = "On", [UI_MENU_REFLEX_BOOST] = "On + Boost", @@ -819,8 +820,9 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_DLSS_PERFORMANCE] = "Производительность", [UI_MENU_DLSS_ULTRA] = "Макс. производительность", [UI_MENU_FXAA] = "Сглаживание игры (FXAA)", [UI_MENU_FXAA_DESC] = "Собственное сглаживание FXAA. Слегка размывает мелкие детали; с DLAA оно не нужно.", - [UI_MENU_REFLEX] = "NVIDIA Reflex Low Latency", - [UI_MENU_REFLEX_DESC] = "Снижает задержку системы на видеокартах NVIDIA с помощью VK_NV_low_latency2. «Вкл + Ускорение» удерживает высокие частоты GPU при упоре в процессор.", + [UI_MENU_REFLEX] = "Низкая задержка", + [UI_MENU_REFLEX_DESC] = "Снижает задержку системы. «Авто» выбирает NVIDIA Reflex или AMD Anti-Lag 2 под видеокарту (VK_NV_low_latency2, VK_AMD_anti_lag); «Вкл + Ускорение» удерживает высокие частоты GPU NVIDIA при упоре в процессор. Применяется после перезапуска.", + [UI_MENU_REFLEX_AUTO] = "Авто", [UI_MENU_REFLEX_OFF] = "Выкл", [UI_MENU_REFLEX_ON] = "Вкл", [UI_MENU_REFLEX_BOOST] = "Вкл + Ускорение", @@ -1932,8 +1934,9 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_FXAA] = "Antisserrilhado do jogo (FXAA)", [UI_MENU_FXAA_DESC] = "A suavização de bordas FXAA do próprio jogo. Borra um pouco os detalhes finos; com o DLAA " "ligado não é necessária.", - [UI_MENU_REFLEX] = "NVIDIA Reflex Low Latency", - [UI_MENU_REFLEX_DESC] = "Reduz a latência do sistema em GPUs NVIDIA usando VK_NV_low_latency2. Ligado + Boost mantém os clocks da GPU altos quando limitado pela CPU.", + [UI_MENU_REFLEX] = "Baixa latência", + [UI_MENU_REFLEX_DESC] = "Reduz a latência do sistema. Automático escolhe NVIDIA Reflex ou AMD Anti-Lag 2 conforme a GPU (VK_NV_low_latency2, VK_AMD_anti_lag); Ligado + Boost mantém os clocks da GPU NVIDIA altos quando limitado pela CPU. Aplica após reiniciar.", + [UI_MENU_REFLEX_AUTO] = "Automático", [UI_MENU_REFLEX_OFF] = "Desligado", [UI_MENU_REFLEX_ON] = "Ligado", [UI_MENU_REFLEX_BOOST] = "Ligado + Boost", @@ -2689,8 +2692,9 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_DLSS_CHARS_FULL] = "强", [UI_MENU_FXAA] = "游戏抗锯齿(FXAA)", [UI_MENU_FXAA_DESC] = "游戏自身的 FXAA 边缘平滑。会让细节略微模糊;开启 DLAA 时不需要。", - [UI_MENU_REFLEX] = "NVIDIA Reflex 低延迟", - [UI_MENU_REFLEX_DESC] = "在支持 VK_NV_low_latency2 的 NVIDIA 显卡上降低系统延迟。开 + 增强 (On + Boost) 会在 CPU 瓶颈时保持 GPU 高频率。", + [UI_MENU_REFLEX] = "低延迟", + [UI_MENU_REFLEX_DESC] = "降低系统延迟。自动会按显卡选择 NVIDIA Reflex 或 AMD Anti-Lag 2 (VK_NV_low_latency2、VK_AMD_anti_lag);开 + 增强 (On + Boost) 会在 CPU 瓶颈时保持 NVIDIA 显卡高频率。重启后生效。", + [UI_MENU_REFLEX_AUTO] = "自动", [UI_MENU_REFLEX_OFF] = "关", [UI_MENU_REFLEX_ON] = "开", [UI_MENU_REFLEX_BOOST] = "开 + 增强", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 3cdb2ff4..1247dab5 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -63,7 +63,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, + UI_MENU_REFLEX, UI_MENU_REFLEX_DESC, UI_MENU_REFLEX_AUTO, UI_MENU_REFLEX_OFF, UI_MENU_REFLEX_ON, UI_MENU_REFLEX_BOOST, UI_MENU_SCENE_UPSCALER, UI_MENU_SCENE_UPSCALER_DESC, UI_MENU_SCENE_UPSCALER_OFF, UI_MENU_SCENE_UPSCALER_DLSS, UI_MENU_SCENE_UPSCALER_FSR3, UI_MENU_SCENE_UPSCALER_NO_DLSS_GPU, UI_MENU_SCENE_UPSCALER_NO_DLSS_DLL, UI_MENU_FSR3_SHARPNESS, UI_MENU_FSR3_SHARPNESS_DESC, diff --git a/runtime/vk.h b/runtime/vk.h index 9833a213..690be668 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -38,7 +38,8 @@ typedef struct VkCtx { 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_nv_reflex; // VK_NV_low_latency2 (NVIDIA Reflex Low Latency) + has_nv_reflex, // VK_NV_low_latency2 (NVIDIA Reflex Low Latency) + has_amd_antilag; // VK_AMD_anti_lag (AMD Anti-Lag 2) // 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 diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 07f128f7..31f522df 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -289,6 +289,7 @@ typedef struct { VkPhysicalDeviceDynamicRenderingLocalReadFeaturesKHR lr; VkPhysicalDeviceAddressBindingReportFeaturesEXT abr; VkPhysicalDevicePresentIdFeaturesKHR pid; + VkPhysicalDeviceAntiLagFeaturesAMD al; } FeatureChain; static int create_device(void) { @@ -357,16 +358,21 @@ 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 requires VK_KHR_present_id) + // Low latency (the reflex setting): auto, on and boost use what the GPU has, NVIDIA Reflex (VK_NV_low_latency2, + // which requires VK_KHR_present_id) or AMD Anti-Lag 2 (VK_AMD_anti_lag: RADV and AMD's Windows driver). const char *rv = cfg_value("TOTK_REFLEX"); - if (rv && strcmp(rv, "off") != 0 && strcmp(rv, "0") != 0) { + if (strcmp(rv, "off") != 0 && strcmp(rv, "0") != 0) { if (has_ext(ep, ne, VK_KHR_PRESENT_ID_EXTENSION_NAME) && has_ext(ep, ne, "VK_NV_low_latency2")) { g_vk.has_nv_reflex = 1; ext[n_ext++] = VK_KHR_PRESENT_ID_EXTENSION_NAME; ext[n_ext++] = "VK_NV_low_latency2"; CHAIN(pid, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR); + } else if (has_ext(ep, ne, VK_AMD_ANTI_LAG_EXTENSION_NAME)) { + g_vk.has_amd_antilag = 1; + ext[n_ext++] = VK_AMD_ANTI_LAG_EXTENSION_NAME; + CHAIN(al, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ANTI_LAG_FEATURES_AMD); } else { - rt_log("Reflex: VK_NV_low_latency2 not available"); + rt_log("Low latency: neither VK_NV_low_latency2 nor VK_AMD_anti_lag is available"); } } // Graphics pipeline library: chained last so it can be dropped again when vk_caps_gpl declines it. @@ -418,6 +424,11 @@ static int create_device(void) { g_vk.has_nv_reflex = 0; rt_log("Reflex: VK_NV_low_latency2 not available (no presentId feature)"); } + if (g_vk.has_amd_antilag && !c->al.antiLag) { + g_vk.has_amd_antilag = 0; + rt_log("Anti-Lag: VK_AMD_anti_lag not available (no antiLag feature)"); + } + if (g_vk.has_amd_antilag) rt_log("Anti-Lag: AMD Anti-Lag 2 on (VK_AMD_anti_lag)"); g_vk.has_astc_ldr = h->textureCompressionASTC_LDR; { const char *e = cfg_get("TOTK_FEEDBACK"); diff --git a/totk.ini.example b/totk.ini.example index d773f1df..0a1c263c 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -342,8 +342,10 @@ # processes these effects too, as before (the default until the inventory preview, the scope and water edges are # checked on NVIDIA). The upscaling DLSS modes always process them. Changes while the game runs take effect at once. # dlaa_effects_after = 0 -# 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. Read at start. +# Low latency: off, auto, on or boost. Reduces system input latency with what the GPU has: NVIDIA Reflex +# (VK_NV_low_latency2) or AMD Anti-Lag 2 (VK_AMD_anti_lag: Mesa RADV, AMD's Windows driver). auto and on pick the +# one the GPU supports and do nothing on a GPU with neither; boost also keeps NVIDIA GPU clocks high in CPU-bound +# scenes. Frames are held to the frame limit the pacer uses. Read at start. # 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. From e15aae0606af498d3e8eb883902347e4fef70190 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 23 Nov 2026 18:34:39 +0300 Subject: [PATCH 2/2] Anti-Lag 2 runs only with reflex = auto, so on and boost keep their old Reflex-only meaning A player who had reflex = on on an AMD GPU got nothing from it before #1416 and would have got Anti-Lag after an update without asking for it. on and boost now enable NVIDIA Reflex alone and log the same line as before when it is missing; auto picks Reflex or Anti-Lag. The Anti-Lag entry point loads with atomics, since the game thread and the present thread both make the first call, and the TOTK_TRACE=perf latency line keeps its old text unless Anti-Lag is on. The menu description, docs/settings.md and totk.ini.example say which value turns on what. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/gfx.h | 2 +- runtime/gfx_present.c | 17 ++++++++++++----- runtime/ui_text.c | 8 ++++---- runtime/vk_device.c | 13 +++++++++---- totk.ini.example | 8 ++++---- 6 files changed, 31 insertions(+), 19 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 4cc36576..85e9c519 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -211,7 +211,7 @@ Many rows below are developer tools that only act together with another setting | `mvprev_grow` (default `1`) | `0` keeps only three banks in the previous-frame tables. | Object motion that depends on the other banks is no longer tracked. | Caution | | `mvprev_skip` | Hex bank mask of banks left out of the previous-frame copy, replacing the default. | Different banks are tracked, which changes object motion. | Caution | | `mvprev_trust` | Hex bank mask of banks whose found previous-frame copy is taken without the check. | A copy that does not match is used anyway, which can give wrong motion. | Caution | -| `reflex` (default `off`) | `off`, `auto`, `on`, or `boost`; reduces system input latency with what the GPU supports: NVIDIA Reflex Low Latency (`VK_NV_low_latency2`) or AMD Anti-Lag 2 (`VK_AMD_anti_lag`, Mesa RADV and AMD's Windows driver). Read at start. | `auto` and `on` pick the extension the GPU has and do nothing when it has neither. Both coordinate simulation and presentation so input reaches the screen sooner when the GPU is the limit, holding frames to the frame limit the pacer uses (`fps_limit`, the monitor's rate for `display`). `boost` also keeps NVIDIA GPU clocks high in CPU-bound scenes. | Safe | +| `reflex` (default `off`) | `off`, `auto`, `on`, or `boost`; reduces system input latency. `on` and `boost` enable NVIDIA Reflex Low Latency (`VK_NV_low_latency2`); `auto` enables Reflex, or AMD Anti-Lag 2 (`VK_AMD_anti_lag`, Mesa RADV and AMD's Windows driver) on a GPU without Reflex. Read at start. | Only one of the two runs; on a GPU with neither extension the setting does nothing. Both coordinate simulation and presentation so input reaches the screen sooner when the GPU is the limit, holding frames to the frame limit the pacer uses (`fps_limit`, the monitor's rate for `display`). `boost` also keeps NVIDIA GPU clocks high in CPU-bound scenes. | Safe | ## Scheduling and host diff --git a/runtime/gfx.h b/runtime/gfx.h index 43abcb00..8bbc15d8 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -34,7 +34,7 @@ uint64_t vk_reflex_frame_begin(void); // SIMULATION_START: the new frame's pre uint64_t vk_reflex_frame(void); // the ID of the frame the game simulates now void vk_reflex_sleep(void); // before SIMULATION_START: the driver's latency sleep // AMD Anti-Lag 2 (gfx_present.c, VK_AMD_anti_lag): the driver paces the CPU from the INPUT call to the PRESENT call of a -// frame. Both return at once with low latency off or the GPU lacking the extension. +// frame. Both return at once unless reflex = auto turned Anti-Lag on at device creation (vk_device.c create_device). void vk_antilag_input(uint64_t frame); // before the frame's input and simulation; may sleep void vk_antilag_present(uint64_t frame); // right before the frame's vkQueuePresentKHR diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 6b3f9796..772c23cb 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -390,9 +390,9 @@ static void latency_note(VkCtx *c, uint64_t pid) { "end %.2f ms", full, gi.timingCount, full ? sum / full / 1000.0 : 0.0); } } - rt_log("reflex: frame start to present %.2f ms over %llu frames (reflex %s, anti-lag %s)%s", g_lat.n ? g_lat.ns / 1e6 / g_lat.n : 0.0, + rt_log("reflex: frame start to present %.2f ms over %llu frames (reflex %s%s)%s", g_lat.n ? g_lat.ns / 1e6 / g_lat.n : 0.0, (unsigned long long)g_lat.n, g_reflex_mode == 2 ? "boost" : g_reflex_mode == 1 ? "on" : "off", - c->has_amd_antilag ? "on" : "off", drv); + c->has_amd_antilag ? ", anti-lag on" : "", drv); g_lat.n = g_lat.ns = 0; } @@ -457,18 +457,25 @@ void vk_reflex_sleep(void) { // ---- AMD Anti-Lag 2 (VK_AMD_anti_lag) static PFN_vkAntiLagUpdateAMD g_antilag_update; +// The INPUT call runs on the game thread (nvnWindowAcquireTexture), the PRESENT call on the present thread (flip_one, +// g_pl held): both carry the frame's ID, as the spec asks of a frame's two calls. The first call of either thread loads +// the entry point; the atomics keep that load race-free. static void antilag_update(VkAntiLagStageAMD stage, uint64_t frame) { VkCtx *c = vk_ctx(); if (!c || !c->has_amd_antilag || !frame) return; - if (!g_antilag_update) g_antilag_update = (PFN_vkAntiLagUpdateAMD)vkGetDeviceProcAddr(c->device, "vkAntiLagUpdateAMD"); - if (!g_antilag_update) return; + PFN_vkAntiLagUpdateAMD fn = __atomic_load_n(&g_antilag_update, __ATOMIC_ACQUIRE); + if (!fn) { + fn = (PFN_vkAntiLagUpdateAMD)vkGetDeviceProcAddr(c->device, "vkAntiLagUpdateAMD"); + if (!fn) return; + __atomic_store_n(&g_antilag_update, fn, __ATOMIC_RELEASE); + } // the driver holds frames to the pacer's frame limit, as Reflex's minimum interval does (0: no limit known) double hz = frame_step_limit_hz(); VkAntiLagPresentationInfoAMD pi = { .sType = VK_STRUCTURE_TYPE_ANTI_LAG_PRESENTATION_INFO_AMD, .stage = stage, .frameIndex = frame}; VkAntiLagDataAMD d = {.sType = VK_STRUCTURE_TYPE_ANTI_LAG_DATA_AMD, .mode = VK_ANTI_LAG_MODE_ON_AMD, .maxFPS = hz > 0 ? (uint32_t)(hz + 0.5) : 0, .pPresentationInfo = &pi}; - g_antilag_update(c->device, &d); + fn(c->device, &d); } void vk_antilag_input(uint64_t frame) { antilag_update(VK_ANTI_LAG_STAGE_INPUT_AMD, frame); } diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 4a6b60cb..a52aaa2e 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -177,7 +177,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [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] = "Low Latency", - [UI_MENU_REFLEX_DESC] = "Reduces system latency. Auto picks NVIDIA Reflex or AMD Anti-Lag 2 for the GPU (VK_NV_low_latency2, VK_AMD_anti_lag); On + Boost keeps NVIDIA GPU clocks high in CPU-bound scenes. Applies after a restart.", + [UI_MENU_REFLEX_DESC] = "Reduces system latency. On uses NVIDIA Reflex (VK_NV_low_latency2); On + Boost also keeps NVIDIA GPU clocks high in CPU-bound scenes. Auto uses Reflex on NVIDIA and AMD Anti-Lag 2 (VK_AMD_anti_lag) on AMD. Applies after a restart.", [UI_MENU_REFLEX_AUTO] = "Auto", [UI_MENU_REFLEX_OFF] = "Off", [UI_MENU_REFLEX_ON] = "On", @@ -827,7 +827,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_FXAA] = "Сглаживание игры (FXAA)", [UI_MENU_FXAA_DESC] = "Собственное сглаживание FXAA. Слегка размывает мелкие детали; с DLAA оно не нужно.", [UI_MENU_REFLEX] = "Низкая задержка", - [UI_MENU_REFLEX_DESC] = "Снижает задержку системы. «Авто» выбирает NVIDIA Reflex или AMD Anti-Lag 2 под видеокарту (VK_NV_low_latency2, VK_AMD_anti_lag); «Вкл + Ускорение» удерживает высокие частоты GPU NVIDIA при упоре в процессор. Применяется после перезапуска.", + [UI_MENU_REFLEX_DESC] = "Снижает задержку системы. «Вкл» включает NVIDIA Reflex (VK_NV_low_latency2); «Вкл + Ускорение» также удерживает высокие частоты GPU NVIDIA при упоре в процессор. «Авто» включает Reflex на NVIDIA и AMD Anti-Lag 2 (VK_AMD_anti_lag) на AMD. Применяется после перезапуска.", [UI_MENU_REFLEX_AUTO] = "Авто", [UI_MENU_REFLEX_OFF] = "Выкл", [UI_MENU_REFLEX_ON] = "Вкл", @@ -1945,7 +1945,7 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_FXAA_DESC] = "A suavização de bordas FXAA do próprio jogo. Borra um pouco os detalhes finos; com o DLAA " "ligado não é necessária.", [UI_MENU_REFLEX] = "Baixa latência", - [UI_MENU_REFLEX_DESC] = "Reduz a latência do sistema. Automático escolhe NVIDIA Reflex ou AMD Anti-Lag 2 conforme a GPU (VK_NV_low_latency2, VK_AMD_anti_lag); Ligado + Boost mantém os clocks da GPU NVIDIA altos quando limitado pela CPU. Aplica após reiniciar.", + [UI_MENU_REFLEX_DESC] = "Reduz a latência do sistema. Ligado usa o NVIDIA Reflex (VK_NV_low_latency2); Ligado + Boost também mantém os clocks da GPU NVIDIA altos quando limitado pela CPU. Automático usa o Reflex na NVIDIA e o AMD Anti-Lag 2 (VK_AMD_anti_lag) na AMD. Aplica após reiniciar.", [UI_MENU_REFLEX_AUTO] = "Automático", [UI_MENU_REFLEX_OFF] = "Desligado", [UI_MENU_REFLEX_ON] = "Ligado", @@ -2707,7 +2707,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_FXAA] = "游戏抗锯齿(FXAA)", [UI_MENU_FXAA_DESC] = "游戏自身的 FXAA 边缘平滑。会让细节略微模糊;开启 DLAA 时不需要。", [UI_MENU_REFLEX] = "低延迟", - [UI_MENU_REFLEX_DESC] = "降低系统延迟。自动会按显卡选择 NVIDIA Reflex 或 AMD Anti-Lag 2 (VK_NV_low_latency2、VK_AMD_anti_lag);开 + 增强 (On + Boost) 会在 CPU 瓶颈时保持 NVIDIA 显卡高频率。重启后生效。", + [UI_MENU_REFLEX_DESC] = "降低系统延迟。开使用 NVIDIA Reflex (VK_NV_low_latency2);开 + 增强 (On + Boost) 还会在 CPU 瓶颈时保持 NVIDIA 显卡高频率。自动在 NVIDIA 显卡上使用 Reflex,在 AMD 显卡上使用 AMD Anti-Lag 2 (VK_AMD_anti_lag)。重启后生效。", [UI_MENU_REFLEX_AUTO] = "自动", [UI_MENU_REFLEX_OFF] = "关", [UI_MENU_REFLEX_ON] = "开", diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 361ec605..5d54d8bf 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -377,21 +377,26 @@ static int create_device(void) { g_vk.has_nv_checkpoints = 1; ext[n_ext++] = VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME; } - // Low latency (the reflex setting): auto, on and boost use what the GPU has, NVIDIA Reflex (VK_NV_low_latency2, - // which requires VK_KHR_present_id) or AMD Anti-Lag 2 (VK_AMD_anti_lag: RADV and AMD's Windows driver). + // Low latency (the reflex setting). on and boost: NVIDIA Reflex (VK_NV_low_latency2, which requires + // VK_KHR_present_id), as before Anti-Lag came in, so a GPU without Reflex still gets nothing new from them. auto: + // Reflex, or AMD Anti-Lag 2 (VK_AMD_anti_lag: RADV and AMD's Windows driver) on a GPU without Reflex. Only one of + // the two runs. off (the default) enables neither extension. const char *rv = cfg_value("TOTK_REFLEX"); if (strcmp(rv, "off") != 0 && strcmp(rv, "0") != 0) { + int want_antilag = !strcmp(rv, "auto"); if (has_ext(ep, ne, VK_KHR_PRESENT_ID_EXTENSION_NAME) && has_ext(ep, ne, "VK_NV_low_latency2")) { g_vk.has_nv_reflex = 1; ext[n_ext++] = VK_KHR_PRESENT_ID_EXTENSION_NAME; ext[n_ext++] = "VK_NV_low_latency2"; CHAIN(pid, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR); - } else if (has_ext(ep, ne, VK_AMD_ANTI_LAG_EXTENSION_NAME)) { + } else if (want_antilag && has_ext(ep, ne, VK_AMD_ANTI_LAG_EXTENSION_NAME)) { g_vk.has_amd_antilag = 1; ext[n_ext++] = VK_AMD_ANTI_LAG_EXTENSION_NAME; CHAIN(al, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ANTI_LAG_FEATURES_AMD); - } else { + } else if (want_antilag) { rt_log("Low latency: neither VK_NV_low_latency2 nor VK_AMD_anti_lag is available"); + } else { + rt_log("Reflex: VK_NV_low_latency2 not available"); } } // Graphics pipeline library: chained last so it can be dropped again when vk_caps_gpl declines it. diff --git a/totk.ini.example b/totk.ini.example index 776a3753..458967c5 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -345,10 +345,10 @@ # processes these effects too, as before (the default until the inventory preview, the scope and water edges are # checked on NVIDIA). The upscaling DLSS modes always process them. Changes while the game runs take effect at once. # dlaa_effects_after = 0 -# Low latency: off, auto, on or boost. Reduces system input latency with what the GPU has: NVIDIA Reflex -# (VK_NV_low_latency2) or AMD Anti-Lag 2 (VK_AMD_anti_lag: Mesa RADV, AMD's Windows driver). auto and on pick the -# one the GPU supports and do nothing on a GPU with neither; boost also keeps NVIDIA GPU clocks high in CPU-bound -# scenes. Frames are held to the frame limit the pacer uses. Read at start. +# Low latency: off, auto, on or boost. Reduces system input latency. on and boost enable NVIDIA Reflex +# (VK_NV_low_latency2); boost also keeps NVIDIA GPU clocks high in CPU-bound scenes. auto enables Reflex, or AMD +# Anti-Lag 2 (VK_AMD_anti_lag: Mesa RADV, AMD's Windows driver) on a GPU without Reflex. On a GPU with neither the +# setting does nothing. Frames are held to the frame limit the pacer uses. Read at start. # 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.