diff --git a/runtime/cfg.c b/runtime/cfg.c index 2f0f1af2..b322a4b8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -52,7 +52,7 @@ static const char *const known[] = { "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PREWARM_FULL", "PREWARM_WAIT_MS", "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", - "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", + "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", "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", "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", diff --git a/runtime/dlssg/nx2_sl.cpp b/runtime/dlssg/nx2_sl.cpp index 67e54df4..147340fc 100644 --- a/runtime/dlssg/nx2_sl.cpp +++ b/runtime/dlssg/nx2_sl.cpp @@ -5,9 +5,9 @@ // // Manual hooking (the SDK's ProgrammingGuideManualHooking): the game creates its instance and device through the // interposer's vkCreateInstance and vkCreateDevice, which add what DLSS-G needs, and calls the interposer's -// swapchain functions; everything else goes to the Vulkan loader as before. Each frame gets a frame token, the PCL -// latency markers, the camera constants and the depth and motion vector tags (valid until the present), then the -// present goes through the interposer, which inserts the generated frame. +// swapchain functions; everything else goes to the Vulkan loader as before. Each frame of the game sleeps in Reflex +// where it starts and gets the PCL latency markers, the camera constants and the depth and motion vector tags (valid +// until the present), then the present goes through the interposer, which inserts the generated frame. #define NX2SL_BUILD #include @@ -37,12 +37,11 @@ PFun_slDLSSGSetOptions *p_dlssg_options; PFun_slDLSSGGetState *p_dlssg_state; PFun_slPCLSetMarker *p_marker; PFun_slReflexSetOptions *p_reflex; +PFun_slReflexSleep *p_sleep; PFN_vkGetInstanceProcAddr p_gipa; PFN_vkGetDeviceProcAddr p_gdpa; -sl::FrameToken *g_frame; int g_available = -1, g_mode = -1; -uint64_t g_presented; -uint32_t g_generated; +uint64_t g_presented, g_shown; void say(const char *fmt, ...) { if (!g_log) return; @@ -62,6 +61,12 @@ void sl_log(sl::LogType type, const char *msg) { template T *proc(const char *name) { return reinterpret_cast(GetProcAddress(g_mod, name)); } +// The token of frame n. Tokens come from a small ring Streamline reuses, so each call takes its own right before use. +sl::FrameToken *token(uint32_t frame) { + sl::FrameToken *t = nullptr; + return frame && p_token(t, &frame) == sl::Result::eOk ? t : nullptr; +} + void matrix(sl::float4x4 &m, const float *v) { for (int r = 0; r < 4; r++) m[r] = sl::float4(v[r * 4], v[r * 4 + 1], v[r * 4 + 2], v[r * 4 + 3]); } @@ -134,24 +139,48 @@ extern "C" NX2SL_API int nx2sl_available(void) { if (g_available && p_feature(sl::kFeatureDLSS_G, "slDLSSGGetState", f) == sl::Result::eOk) p_dlssg_state = (PFun_slDLSSGGetState *)f; if (p_feature(sl::kFeaturePCL, "slPCLSetMarker", f) == sl::Result::eOk) p_marker = (PFun_slPCLSetMarker *)f; if (p_feature(sl::kFeatureReflex, "slReflexSetOptions", f) == sl::Result::eOk) p_reflex = (PFun_slReflexSetOptions *)f; + if (p_feature(sl::kFeatureReflex, "slReflexSleep", f) == sl::Result::eOk) p_sleep = (PFun_slReflexSleep *)f; if (!p_dlssg_options || !p_marker) g_available = 0; - if (g_available && p_reflex) { // DLSS-G needs Reflex's low-latency mode - sl::ReflexOptions o{}; - o.mode = sl::ReflexMode::eLowLatency; - p_reflex(o); - } say("dlssg: DLSS Frame Generation %s", g_available ? "available" : "not available on this GPU or driver"); return g_available; } +// Streamline sets Reflex's mode on the swapchain it was told of last (VK_NV_low_latency2) and drops it while there is +// none, so it is set again after each swapchain. +extern "C" NX2SL_API void nx2sl_reflex(int mode) { + if (g_available != 1 || !p_reflex) return; + sl::ReflexOptions o{}; + o.mode = mode == 2 ? sl::ReflexMode::eLowLatencyWithBoost : mode ? sl::ReflexMode::eLowLatency : sl::ReflexMode::eOff; + sl::Result r = p_reflex(o); + static int n; + if (n++ < 8) say("dlssg: Reflex low-latency mode %s (%d)", mode == 2 ? "on with boost" : mode ? "on" : "off", (int)r); +} + +// The game's simulation and its recording of the frame's commands are one span here: the guest's threads cannot be +// told apart, so the simulation markers sit at the frame's start and the render submission runs until the frame's +// submissions went out (nx2sl_submit_end). +extern "C" NX2SL_API void nx2sl_sleep(uint32_t frame, int hold) { + if (g_available != 1) return; + sl::FrameToken *t = token(frame); + if (hold && t && p_sleep) p_sleep(*t); + if ((t = token(frame))) { + p_marker(sl::PCLMarker::eSimulationStart, *t); + p_marker(sl::PCLMarker::eSimulationEnd, *t); + p_marker(sl::PCLMarker::eRenderSubmitStart, *t); + } +} + +extern "C" NX2SL_API void nx2sl_submit_end(uint32_t frame) { + sl::FrameToken *t = g_available == 1 ? token(frame) : nullptr; + if (t) p_marker(sl::PCLMarker::eRenderSubmitEnd, *t); +} + extern "C" NX2SL_API int nx2sl_frame(VkCommandBuffer cb, const Nx2SlImage *depth, const Nx2SlImage *mv, const Nx2SlImage *hudless, const Nx2SlCamera *cam, uint32_t out_w, uint32_t out_h, - int enable) { + int enable, uint32_t frame) { if (g_available != 1) return 0; - if (p_token(g_frame, nullptr) != sl::Result::eOk || !g_frame) return 0; - p_marker(sl::PCLMarker::eSimulationStart, *g_frame); - p_marker(sl::PCLMarker::eSimulationEnd, *g_frame); - p_marker(sl::PCLMarker::eRenderSubmitStart, *g_frame); + sl::FrameToken *t = token(frame); + if (!t) return 0; sl::ViewportHandle vp(0); if (enable && depth && mv && cam) { sl::Constants c{}; @@ -174,14 +203,14 @@ extern "C" NX2SL_API int nx2sl_frame(VkCommandBuffer cb, const Nx2SlImage *depth c.cameraMotionIncluded = sl::Boolean::eTrue; c.motionVectors3D = sl::Boolean::eFalse; c.reset = cam->reset ? sl::Boolean::eTrue : sl::Boolean::eFalse; - p_constants(c, *g_frame, vp); + p_constants(c, *t, vp); sl::Resource rd = resource(depth), rm = resource(mv), rh = hudless ? resource(hudless) : sl::Resource{}; sl::Extent ed{0, 0, depth->width, depth->height}, em{0, 0, mv->width, mv->height}; sl::Extent eh{0, 0, hudless ? hudless->width : 0, hudless ? hudless->height : 0}; sl::ResourceTag tags[3] = {sl::ResourceTag(&rd, sl::kBufferTypeDepth, sl::ResourceLifecycle::eValidUntilPresent, &ed), sl::ResourceTag(&rm, sl::kBufferTypeMotionVectors, sl::ResourceLifecycle::eValidUntilPresent, &em), sl::ResourceTag(&rh, sl::kBufferTypeHUDLessColor, sl::ResourceLifecycle::eValidUntilPresent, &eh)}; - p_tag(*g_frame, vp, tags, hudless ? 3 : 2, (sl::CommandBuffer *)cb); + if ((t = token(frame))) p_tag(*t, vp, tags, hudless ? 3 : 2, (sl::CommandBuffer *)cb); } if ((enable != 0) != g_mode) { sl::DLSSGOptions o{}; @@ -191,29 +220,25 @@ extern "C" NX2SL_API int nx2sl_frame(VkCommandBuffer cb, const Nx2SlImage *depth say("dlssg: frame generation %s (%d), output %ux%u", enable ? "on" : "off", (int)r, out_w, out_h); g_mode = enable != 0; } - p_marker(sl::PCLMarker::eRenderSubmitEnd, *g_frame); return 1; } -extern "C" NX2SL_API void nx2sl_present_marker(int after) { - if (g_available != 1 || !g_frame) return; - p_marker(after ? sl::PCLMarker::ePresentEnd : sl::PCLMarker::ePresentStart, *g_frame); - if (after) { - g_presented++; - if (p_dlssg_state && (g_presented == 600 || g_presented % 3600 == 0)) { - sl::DLSSGState s{}; - if (p_dlssg_state(sl::ViewportHandle(0), s, nullptr) == sl::Result::eOk) { - g_generated = s.numFramesActuallyPresented; - say("dlssg: %llu frames, %u presented per frame, status %u", (unsigned long long)g_presented, - s.numFramesActuallyPresented, (unsigned)s.status); - } - } - } +extern "C" NX2SL_API void nx2sl_present_marker(int after, uint32_t frame) { + sl::FrameToken *t = g_available == 1 ? token(frame) : nullptr; + if (!t) return; + p_marker(after ? sl::PCLMarker::ePresentEnd : sl::PCLMarker::ePresentStart, *t); + if (!after) return; + g_presented++; + // Asked after each present, numFramesActuallyPresented is what that present showed: 2 with a generated frame. + sl::DLSSGState s{}; + if (!p_dlssg_state || p_dlssg_state(sl::ViewportHandle(0), s, nullptr) != sl::Result::eOk) return; + g_shown += s.numFramesActuallyPresented; + if (g_presented == 600 || g_presented % 3600 == 0) + say("dlssg: %llu frames, %u presented per frame, status %u", (unsigned long long)g_presented, + s.numFramesActuallyPresented, (unsigned)s.status); } -extern "C" NX2SL_API void nx2sl_stats(uint64_t *presented, uint32_t *generated_per_frame) { - *presented = g_presented, *generated_per_frame = g_generated; -} +extern "C" NX2SL_API uint64_t nx2sl_shown(void) { return g_shown; } extern "C" NX2SL_API void nx2sl_shutdown(void) { if (p_shutdown && g_available >= 0) p_shutdown(); diff --git a/runtime/dlssg/nx2_sl.h b/runtime/dlssg/nx2_sl.h index 908b3674..44dab8f8 100644 --- a/runtime/dlssg/nx2_sl.h +++ b/runtime/dlssg/nx2_sl.h @@ -5,7 +5,7 @@ #include #include -#define NX2SL_VERSION 1 +#define NX2SL_VERSION 3 typedef struct { VkImage image; @@ -44,12 +44,23 @@ typedef PFN_vkGetInstanceProcAddr (*PFN_nx2sl_instance_proc)(void); typedef PFN_vkGetDeviceProcAddr (*PFN_nx2sl_device_proc)(void); // 1 when DLSS-G loaded and supports this GPU (after the device exists) typedef int (*PFN_nx2sl_available)(void); -// Each present, before the present call: a new frame, its latency markers up to the render's end, its constants and -// tags, and frame generation on or off. Then nx2sl_present_marker(0) before and (1) after the present. +// Reflex's low-latency mode, 0 off, 1 on, 2 on with boost: after the device, and after each swapchain the game creates +// through the interposer (the mode is the swapchain's) +typedef void (*PFN_nx2sl_reflex)(int mode); +// Frames are numbered by the game's presents: frame n ends with its n-th present (vk_dlssg.c vk_dlssg_game_present). +// Where the game starts a frame: with hold, Reflex's sleep, which holds the start until the time the driver computes +// from the measured render and queue times; then the frame's simulation and render-submit-start markers. +typedef void (*PFN_nx2sl_sleep)(uint32_t frame, int hold); +// The frame's submissions went out (at the game's present; with asynchronous replay, when the replay reaches it): its +// render-submit-end marker. +typedef void (*PFN_nx2sl_submit_end)(uint32_t frame); +// Each host present, before the present call: the frame's constants and tags, and frame generation on or off. Then +// nx2sl_present_marker(0) before and (1) after the present. typedef int (*PFN_nx2sl_frame)(VkCommandBuffer cb, const Nx2SlImage *depth, const Nx2SlImage *mv, const Nx2SlImage *hudless, - const Nx2SlCamera *cam, uint32_t out_w, uint32_t out_h, int enable); -typedef void (*PFN_nx2sl_present_marker)(int after); -typedef void (*PFN_nx2sl_stats)(uint64_t *presented, uint32_t *generated_per_frame); + const Nx2SlCamera *cam, uint32_t out_w, uint32_t out_h, int enable, uint32_t frame); +typedef void (*PFN_nx2sl_present_marker)(int after, uint32_t frame); +// The frames the display got, generated ones included, counted after each present (on the present thread) +typedef uint64_t (*PFN_nx2sl_shown)(void); typedef void (*PFN_nx2sl_shutdown)(void); #ifdef __cplusplus } diff --git a/runtime/gfx.h b/runtime/gfx.h index 8c2ec2ec..3e2de88a 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -142,6 +142,10 @@ typedef struct { // Queues the present and returns at once; the host present goes out at at_ns (kern_now_ns clock), at once when that // has passed or at_ns is 0. void (*present)(const tex_t *t, const int crop[4], uint64_t at_ns); + // The game starts a frame (nvn_api_obj.c nvnWindowAcquireTexture), after the image's own waits (the pacer's hold + // and the GPU's): the back end may hold it longer (vk_dlssg.c vk_dlssg_frame_start, Reflex's sleep for DLSS Frame + // Generation). NULL: nothing to do. + void (*frame_start)(void); void (*finish)(void); void (*read_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, uint8_t *dst, uint64_t row, uint64_t img); void (*write_texels)(const tex_t *t, const view_t *v, const NVNcopyRegion *r, const uint8_t *src, uint64_t row, uint64_t img); diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index b22ada1d..e77a8797 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -794,12 +794,13 @@ static int put_picture(VkCommandBuffer cb, unsigned slot, VkImage src, VkFormat return fsr; } -// The display's frame rate with frame generation: the swapchain's present count (generated frames included, vk_fg.cpp) -// over half a second, sampled at each of the game's presents (gfx_output_fps). +// The display's frame rate with frame generation: the frames the display got (generated ones included: vk_fg.cpp +// vk_fg_presents, vk_dlssg.c vk_dlssg_shown) over half a second, sampled at each of the game's presents +// (gfx_output_fps). static volatile double g_out_fps; static void output_rate(void) { static uint64_t t0, n0; - uint64_t n = vk_fg_presents(g_sc), t = plat_now_ns(); + uint64_t n = vk_dlssg_active() ? vk_dlssg_shown() : vk_fg_presents(g_sc), t = plat_now_ns(); if (!n) { g_out_fps = 0, t0 = 0; return; } if (!t0 || n < n0) { t0 = t, n0 = n; return; } if (t - t0 >= 500000000ull) g_out_fps = (double)(n - n0) * 1e9 / (double)(t - t0), t0 = t, n0 = n; diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index 2cd6d7a6..265f88b0 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -822,6 +822,7 @@ NVN_API(nvnWindowAcquireTexture) { perf_end(PERF_PACE, p0); } } + if (gfx_backend()->frame_start) gfx_backend()->frame_start(); // the frame's work starts here // 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/vk_backend.c b/runtime/vk_backend.c index b8f22acd..a9a86908 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -30,6 +30,7 @@ #include "vk_exec.h" #include "platform/platform.h" #include "vk_dlaa.h" +#include "vk_dlssg.h" #define WEAK __attribute__((weak)) @@ -59,6 +60,7 @@ static void present_main(void *arg) { plat_cond_broadcast(&pcv); plat_mutex_unlock(&pl); if (flight_on || vk_dump_white) vk_thumbs_frame(j.frame); // NX2NAVI_DUMP_WHITE reads them too + vk_dlssg_present_frame(j.frame); vk_present_blit(j.image, VK_IMAGE_LAYOUT_GENERAL, j.format, j.w, j.h, j.has_crop ? j.crop : NULL); uint64_t f = vk_sync_submit(NULL, 0); // a timeline value after the blit: deferred destruction waits for the blit too if (j.value && f != j.value) @@ -222,6 +224,7 @@ static void replay_main(void *arg) { free((void *)it.tex); } else { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); + vk_dlssg_submitted(frame); // the frame's submissions were replayed before its present present_image(it.tex, it.has_crop ? it.crop : NULL, it.at, it.queued, frame, it.value); } plat_mutex_lock(&rl); @@ -327,8 +330,10 @@ void gfx_exit_log(void) { static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) { vk_sync_start(); + vk_dlssg_game_present(); if (!gfx_replay_async()) { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); + vk_dlssg_submitted(frame); perf_present(frame); // before vk_exec_frame_log, which resets the frame's exec counts the flight recorder reads present_image(t, crop, at_ns, kern_now_ns(), frame, 0); return; @@ -524,6 +529,7 @@ const GfxBackend gfx_vulkan_backend = { .fence_wait = vk_sync_fence_wait, .fence_done = vk_sync_fence_done, .present = vk_be_present, + .frame_start = vk_dlssg_frame_start, .finish = vk_be_finish, .read_texels = vk_be_read_texels, .write_texels = vk_be_write_texels, diff --git a/runtime/vk_dlssg.c b/runtime/vk_dlssg.c index 37a09c74..209551f5 100644 --- a/runtime/vk_dlssg.c +++ b/runtime/vk_dlssg.c @@ -6,10 +6,16 @@ // 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. +// +// DLSS-G needs Reflex, whose low-latency mode paces the game (NX2NAVI_REFLEX, default 1; 0 off, 2 on with boost): the +// game's frame sleeps in Reflex where it starts (its acquire of a window image), and each frame carries the latency +// markers from that start to the host present, all with the frame's number, the count of the game's presents up to it. +// The mode is the swapchain's, so it is set again after each one (vk_dlssg_create_swapchain). #ifdef _WIN32 #include #endif #include +#include #include #include "cfg.h" @@ -28,6 +34,14 @@ static struct { PFN_nx2sl_available available; PFN_nx2sl_frame frame; PFN_nx2sl_present_marker marker; + PFN_nx2sl_sleep sleep; + PFN_nx2sl_submit_end submit_end; + PFN_nx2sl_shown shown; + PFN_nx2sl_reflex reflex; + uint32_t game; // the game's presents (vk_dlssg_game_present) + uint32_t cur; // the frame the present thread blits and presents next (vk_dlssg_present_frame) + uint32_t started; // the last frame vk_dlssg_frame_start started + uint64_t hosted_ns; // kern_now_ns after the last host present PFN_nx2sl_shutdown shutdown; PFN_vkCreateSwapchainKHR create_sc; PFN_vkDestroySwapchainKHR destroy_sc; @@ -38,6 +52,16 @@ static struct { static void sl_line(const char *line) { rt_log("%s", line); } +static int reflex_mode(void) { + static int m = -1; + if (m < 0) { + const char *s = cfg_get("NX2NAVI_REFLEX"); + m = s && *s ? atoi(s) : 1; + if (m < 0 || m > 2) m = 1; + } + return m; +} + int vk_dlssg_wanted(void) { if (S.wanted < 0) { S.wanted = !strcmp(cfg_value("NX2NAVI_FRAMEGEN"), "dlss"); @@ -71,8 +95,13 @@ int vk_dlssg_early(void) { 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.sleep = (PFN_nx2sl_sleep)(void *)GetProcAddress(m, "nx2sl_sleep"); + S.submit_end = (PFN_nx2sl_submit_end)(void *)GetProcAddress(m, "nx2sl_submit_end"); + S.shown = (PFN_nx2sl_shown)(void *)GetProcAddress(m, "nx2sl_shown"); 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) { + S.reflex = (PFN_nx2sl_reflex)(void *)GetProcAddress(m, "nx2sl_reflex"); + if (!ver || ver() != NX2SL_VERSION || !S.init || !S.gipa || !S.gdpa || !S.available || !S.frame || !S.marker || + !S.sleep || !S.submit_end || !S.shown || !S.reflex) { rt_log("dlssg: nx2navi_dlssg.dll is from another build: DLSS Frame Generation off"); return 0; } @@ -120,12 +149,15 @@ void vk_dlssg_device_ready(VkDevice dev) { 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"); + if (S.active) S.reflex(reflex_mode()); } 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); + VkResult r = S.create_sc(d, ci, NULL, sc); + if (r == VK_SUCCESS) S.reflex(reflex_mode()); + return r; } 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); } @@ -134,12 +166,38 @@ VkResult vk_dlssg_acquire(VkDevice d, VkSwapchainKHR sc, uint64_t t, VkSemaphore } VkResult vk_dlssg_present(VkQueue q, const VkPresentInfoKHR *pi) { - S.marker(0); + S.marker(0, S.cur); VkResult r = S.present(q, pi); - S.marker(1); + S.marker(1, S.cur); + __atomic_store_n(&S.hosted_ns, kern_now_ns(), __ATOMIC_RELAXED); return r; } +// Reflex holds the frame's start until the time the driver computes from the measured render and queue times, so the +// game's CPU work ends about when the GPU is free for it; then the frame's first markers. Once a frame: the game may +// acquire again before it presents. No hold while no host present went out for HOLD_IDLE_NS (the window minimized: +// gfx_present.c skips the host presents while the game runs on): the driver then has no present to time the frame by, +// and the sleep would wait out Streamline's half-second timeout each frame. +#define HOLD_IDLE_NS 200000000ull +void vk_dlssg_frame_start(void) { + if (!S.active) return; + uint32_t f = __atomic_load_n(&S.game, __ATOMIC_RELAXED) + 1; + if (f == S.started) return; + S.started = f; + uint64_t hosted = __atomic_load_n(&S.hosted_ns, __ATOMIC_RELAXED); + S.sleep(f, hosted && kern_now_ns() - hosted < HOLD_IDLE_NS); +} + +void vk_dlssg_game_present(void) { __atomic_add_fetch(&S.game, 1, __ATOMIC_RELAXED); } + +void vk_dlssg_submitted(uint64_t frame) { + if (S.active) S.submit_end((uint32_t)frame); +} + +void vk_dlssg_present_frame(uint64_t frame) { S.cur = (uint32_t)frame; } + +uint64_t vk_dlssg_shown(void) { return S.active ? S.shown() : 0; } + void vk_dlssg_frame(VkCommandBuffer cb, VkExtent2D ext) { if (!S.active) return; VkFfxImage d, m; @@ -164,7 +222,7 @@ void vk_dlssg_frame(VkCommandBuffer cb, VkExtent2D ext) { 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); + S.frame(cb, have ? &zd : NULL, have ? &zm : NULL, NULL, have ? &k : NULL, ext.width, ext.height, on, S.cur); } void vk_dlssg_release(void) { diff --git a/runtime/vk_dlssg.h b/runtime/vk_dlssg.h index e23ecb19..908accd6 100644 --- a/runtime/vk_dlssg.h +++ b/runtime/vk_dlssg.h @@ -19,4 +19,12 @@ VkResult vk_dlssg_get_images(VkDevice d, VkSwapchainKHR sc, uint32_t *n, VkImage VkResult vk_dlssg_acquire(VkDevice d, VkSwapchainKHR sc, uint64_t timeout, VkSemaphore sem, uint32_t *idx); VkResult vk_dlssg_present(VkQueue q, const VkPresentInfoKHR *pi); void vk_dlssg_frame(VkCommandBuffer cb, VkExtent2D ext); // each present, before its command buffer is submitted +// Frames are numbered by the game's presents (frame n ends with the n-th). Reflex's frame timing while active: +void vk_dlssg_frame_start(void); // the game starts a frame (nvn_api_obj.c nvnWindowAcquireTexture): Reflex's sleep +void vk_dlssg_game_present(void); // the game presents it (vk_backend.c vk_be_present): counts it +// its submissions went out (vk_backend.c: vk_be_present, or replay_main with asynchronous replay): the render-submit-end +// marker; frame is gfx_counters.frames of its present, the same count +void vk_dlssg_submitted(uint64_t frame); +void vk_dlssg_present_frame(uint64_t frame); // the present thread's next blit and present are of this frame +uint64_t vk_dlssg_shown(void); // the frames the display got, generated ones included (present thread); 0 when off void vk_dlssg_release(void);