From d6c41d25a58a320f0b7c45291eda7c8726bc1e31 Mon Sep 17 00:00:00 2001 From: Tears of the SteelTemple Date: Wed, 4 Nov 2026 08:40:48 +0200 Subject: [PATCH] reflex: Chain VK_KHR_present_id, fix marker sType, and link frame/submission IDs --- runtime/gfx_present.c | 38 +++++++++++++++++++++++++++++++++++--- runtime/nvn_api_obj.c | 2 +- runtime/ov_input.c | 3 +++ runtime/vk.h | 1 + runtime/vk_device.c | 7 +++++-- runtime/vk_sync.c | 11 +++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index a3e3d737..4b8a466a 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -322,11 +322,15 @@ int vk_has_reflex(void) { return c ? c->has_nv_reflex : 0; } +uint64_t vk_reflex_current_present_id(void) { + return __atomic_load_n(&g_reflex_present_id, __ATOMIC_RELAXED); +} + 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}; + ReflexMarkerInfoNV mi = {.sType = (VkStructureType)1000505002, .presentID = pid, .marker = marker}; g_reflex_marker(c->device, g_sc, &mi); } @@ -372,7 +376,23 @@ static void flip_locked(void) { 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, + uint64_t cur_pid = __atomic_load_n(&g_reflex_present_id, __ATOMIC_RELAXED); + typedef struct { + VkStructureType sType; + const void *pNext; + uint32_t swapchainCount; + const uint64_t *pPresentIds; + } PresentIdKHR; + PresentIdKHR pid_khr = { + .sType = (VkStructureType)1000085001, + .pNext = NULL, + .swapchainCount = 1, + .pPresentIds = &cur_pid + }; + VkCtx *c = vk_ctx(); + VkPresentInfoKHR pi = {.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, + .pNext = (c && c->has_nv_reflex) ? &pid_khr : NULL, + .waitSemaphoreCount = 1, .pWaitSemaphores = &g_render_done[g_flip_idx], .swapchainCount = 1, .pSwapchains = &g_sc, .pImageIndices = &g_flip_idx}; vk_queue_lock(); @@ -424,8 +444,20 @@ static int create_swapchain(void) { vk_queue_lock(); vkDeviceWaitIdle(c->device); VkSwapchainKHR old = g_sc; + typedef struct { + VkStructureType sType; + const void *pNext; + VkBool32 latencyMode; + } SwapchainLatencyCreateInfoNV; + SwapchainLatencyCreateInfoNV slc = { + .sType = (VkStructureType)1000505004, + .pNext = NULL, + .latencyMode = VK_TRUE + }; VkSwapchainCreateInfoKHR ci = { - .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, .surface = g_surface, .minImageCount = count, + .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, + .pNext = c->has_nv_reflex ? &slc : NULL, + .surface = g_surface, .minImageCount = count, .imageFormat = sf.format, .imageColorSpace = sf.colorSpace, .imageExtent = ext, .imageArrayLayers = 1, .imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, // every surface supports it (the menu overlay draws into it) diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index 3ea68306..bbb209c4 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -685,7 +685,6 @@ NVN_API(nvnWindowAcquireTexture) { } 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) { @@ -700,6 +699,7 @@ NVN_API(nvnWindowAcquireTexture) { // Switch's display flips at vblank), so the present cadence does not follow the replay time; one period later the // image is free for the next acquire. Added latency: the slack of the frame (period minus its work), as on the Switch. NVN_API(nvnQueuePresentTexture) { + vk_reflex_marker(1); // VK_LATENCY_MARKER_SIMULATION_END_NV win_t *o = arg_p(c, 1); int idx = (int)arg_x(c, 2); movie_tick(c); // ov_movie.c: hand over decoded movie frames that are due; needs the core, so before the wait diff --git a/runtime/ov_input.c b/runtime/ov_input.c index 3cbc6cfb..ac38f9c9 100644 --- a/runtime/ov_input.c +++ b/runtime/ov_input.c @@ -17,6 +17,8 @@ #include "ov.h" #include "platform/platform.h" +void vk_reflex_marker(int marker); + typedef struct { int64_t sampling; uint64_t buttons; int32_t lx, ly, rx, ry; uint32_t attributes, pad; } NpadState; typedef struct { int64_t sampling; int32_t x, y, dx, dy, wx, wy; uint32_t buttons, attributes; } MouseState; typedef struct { int64_t sampling; uint32_t modifiers, attributes; uint64_t keys[4]; } KeyboardState; @@ -93,6 +95,7 @@ static void npad_states(Cpu *c, uint32_t want) { if (live) { s.attributes = ATTR_CONNECTED; input_poll(&s.buttons, &s.lx, &s.ly, &s.rx, &s.ry); + vk_reflex_marker(6); // VK_LATENCY_MARKER_INPUT_SAMPLE_NV } *out = s; ret_x(c, 1); diff --git a/runtime/vk.h b/runtime/vk.h index 31427d10..76c7f726 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -148,6 +148,7 @@ 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); +uint64_t vk_reflex_current_present_id(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. diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 29b5865f..020d18bd 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -217,6 +217,7 @@ typedef struct { VkPhysicalDeviceGraphicsPipelineLibraryFeaturesEXT gpl; VkPhysicalDeviceAttachmentFeedbackLoopLayoutFeaturesEXT fbl; VkPhysicalDeviceDynamicRenderingLocalReadFeaturesKHR lr; + VkPhysicalDevicePresentIdFeaturesKHR pid; } FeatureChain; static int create_device(void) { @@ -278,10 +279,12 @@ 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")) { + // NVIDIA Reflex Low Latency (VK_NV_low_latency2 requires VK_KHR_present_id) + 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); } // Graphics pipeline library: chained last so it can be dropped again when vk_caps_gpl declines it. void **gpl_link = NULL; diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 05d5388b..ef902537 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -434,7 +434,18 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = s->cb}}; VkSemaphoreSubmitInfo sig = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = c->timeline, .value = fence, .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}; + typedef struct { + VkStructureType sType; + const void *pNext; + uint64_t presentID; + } LatencySubmissionPresentIdNV; + LatencySubmissionPresentIdNV lsid = { + .sType = (VkStructureType)1000505005, + .pNext = NULL, + .presentID = vk_reflex_current_present_id() + }; VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, + .pNext = (c->has_nv_reflex) ? &lsid : NULL, .commandBufferInfoCount = (pro ? 1u : 0u) + (recorded ? 1u : 0u), .pCommandBufferInfos = pro ? cbi : cbi + 1, .signalSemaphoreInfoCount = 1, .pSignalSemaphoreInfos = &sig};