diff --git a/tools/upscale/layer/README.md b/tools/upscale/layer/README.md index b9fb14a395..19ee5cd279 100644 --- a/tools/upscale/layer/README.md +++ b/tools/upscale/layer/README.md @@ -85,6 +85,7 @@ Each key is read as `DROIDDECK_UPSCALE_` first, then `WINLATOR_FSR4_` | `APP_DESCRIPTOR_BUFFER` | `fsr4_layer.c` `hide_app_descriptor_buffer` | `1` = let the game see `VK_EXT_descriptor_buffer`. Default: hidden from the game (the layer still uses it), because vkd3d-proton's own descriptor buffers make FSR 4.1.1 Full write a black frame on Turnip | | `FULL_ON_TURNIP` | `fsr4_pipeline.c` `full_black_here` | `1` = run FSR 4.1.1 Full on Turnip even when the game enabled descriptor buffers itself. Default: Lite stands in then | | `NPU_SYNC` | `fsr4_layer.c:462` | `1` = GPU waits for the NPU each frame. Anything else = async (default). `sync`/`true` also mean sync | +| `NPU_CPU_WAIT` | `dd_upscale_compat.h` `dd_upscale_npu_gpu_wait` | Async only. `1`/`true` = the game's recording thread waits for the previous frame's NPU result (old behaviour). Default: the next frame's command buffer waits for it on the GPU, so recording never blocks | | `SHARPNESS` | `fsr4_layer.c:464` | RCAS 0.0-1.0, default 0.55 | | `PRESET` | `fsr4_pipeline.c:1239` | `ultraperf` or `performance` forces the preset; else from the game's quality mode / ratio | | `CTRL_FILE` | `fsr4_layer.c:472` | Live control file, format below | diff --git a/tools/upscale/layer/dd_upscale_compat.h b/tools/upscale/layer/dd_upscale_compat.h index 939c3fc164..4cb8085b4c 100644 --- a/tools/upscale/layer/dd_upscale_compat.h +++ b/tools/upscale/layer/dd_upscale_compat.h @@ -8,6 +8,7 @@ #include #include +#include static inline const char *dd_upscale_env(const char *key) { char name[96]; @@ -19,6 +20,13 @@ static inline const char *dd_upscale_env(const char *key) { return (v && *v) ? v : NULL; } +// Async NPU: the next frame's command buffer waits on the GPU for the NPU result, so the game's recording thread never +// blocks on the NPU. DROIDDECK_UPSCALE_NPU_CPU_WAIT=1 brings back the old wait on the recording thread. +static inline int dd_upscale_npu_gpu_wait(void) { + const char *v = dd_upscale_env("NPU_CPU_WAIT"); + return !(v && (!strcmp(v, "1") || !strcmp(v, "true"))); +} + // Data dir with the SPIR-V passes, weights and QNN files. NULL when no launcher set one. static inline const char *dd_upscale_dir(void) { const char *dir = dd_upscale_env("DIR"); diff --git a/tools/upscale/layer/fsr411_full_gpu.c b/tools/upscale/layer/fsr411_full_gpu.c index ce4d9ac445..215f3b9841 100644 --- a/tools/upscale/layer/fsr411_full_gpu.c +++ b/tools/upscale/layer/fsr411_full_gpu.c @@ -166,6 +166,8 @@ struct WlFsr411FullGpu { VkEvent ev_e1[2]; VkEvent ev_e2[2]; + VkEvent ev_done[2]; // async: set by the NPU worker, waited on by the next frame on the GPU + int gpu_wait; // async waits on the GPU (default) instead of the recording thread pthread_t npu_thr; int npu_thr_started; @@ -173,9 +175,9 @@ struct WlFsr411FullGpu { pthread_mutex_t npu_mu; pthread_cond_t npu_cv; pthread_cond_t npu_done_cv; - int npu_q_slot[4]; - int npu_q_sync[4]; - uint32_t npu_q_seq[4]; + int npu_q_slot[8]; + int npu_q_sync[8]; + uint32_t npu_q_seq[8]; uint32_t npu_q_head, npu_q_tail; uint32_t last_htp_us; }; @@ -519,17 +521,28 @@ static void *f411_npu_exec_worker(void *arg) { for (;;) { pthread_mutex_lock(&p->npu_mu); while (p->npu_q_head == p->npu_q_tail && !p->npu_stop) { - if (!htp_is_idle && p->npu_qnn) { + const int want_idle = !htp_is_idle && p->npu_qnn; + if (want_idle || p->gpu_wait) { struct timespec dl; clock_gettime(CLOCK_REALTIME, &dl); - dl.tv_nsec += 500000000L; // 500 ms idle drop-off -> release HTP/Bus/HMX vote + // 500 ms idle drop-off -> release HTP/Bus/HMX vote; 250 ms watchdog tick in GPU-wait mode + dl.tv_nsec += want_idle ? 500000000L : 250000000L; if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } int wrc = pthread_cond_timedwait(&p->npu_cv, &p->npu_mu, &dl); if (wrc == ETIMEDOUT && p->npu_q_head == p->npu_q_tail && !p->npu_stop) { - pthread_mutex_unlock(&p->npu_mu); - wl_fsr4_qnn_set_power_mode("idle"); - htp_is_idle = 1; - pthread_mutex_lock(&p->npu_mu); + if (p->gpu_wait) { + // No job queued: release any GPU wait left without a job (e.g. a command buffer run twice). + // Under npu_mu, so it cannot land after a newly queued frame's reset. + for (int s = 0; s < 2; s++) { + if (p->ev_done[s]) p->vk.SetEvent(p->vk.device, p->ev_done[s]); + } + } + if (want_idle) { + pthread_mutex_unlock(&p->npu_mu); + wl_fsr4_qnn_set_power_mode("idle"); + htp_is_idle = 1; + pthread_mutex_lock(&p->npu_mu); + } } } else { pthread_cond_wait(&p->npu_cv, &p->npu_mu); @@ -539,7 +552,7 @@ static void *f411_npu_exec_worker(void *arg) { pthread_mutex_unlock(&p->npu_mu); break; } - uint32_t qi = p->npu_q_head & 3u; + uint32_t qi = p->npu_q_head & 7u; int slot = p->npu_q_slot[qi]; int is_sync = p->npu_q_sync[qi]; uint32_t seq = p->npu_q_seq[qi]; @@ -619,7 +632,9 @@ static void *f411_npu_exec_worker(void *arg) { } else if (p->npu_retire_buf.mapped) { // Async mode: 1 ms initial sleep + 250 us adaptive poll (~99% fewer CPU timer wakeups!) volatile uint32_t *rp = (volatile uint32_t *)p->npu_retire_buf.mapped; - for (int iter = 0; iter < 1000 && !p->npu_stop; iter++) { + // GPU-wait mode: nothing blocks on the CPU here, so allow ~2 s for a slow or queued-up GPU frame. + const int max_iter = p->gpu_wait ? 8000 : 1000; + for (int iter = 0; iter < max_iter && !p->npu_stop; iter++) { uint32_t cur_seq = __atomic_load_n(&rp[slot], __ATOMIC_ACQUIRE); if ((int32_t)(cur_seq - seq) >= 0) { gpu_ready = 1; @@ -695,6 +710,10 @@ static void *f411_npu_exec_worker(void *arg) { if (is_sync && p->ev_e2[slot] && p->vk.SetEvent) { p->vk.SetEvent(p->vk.device, p->ev_e2[slot]); } + // Always, also after a timeout, so the next frame's GPU wait on it cannot hang. + if (p->ev_done[slot] && p->vk.SetEvent) { + p->vk.SetEvent(p->vk.device, p->ev_done[slot]); + } pthread_mutex_lock(&p->npu_mu); p->npu_slot_done[slot] = 1; @@ -821,8 +840,12 @@ static int f411_bind_npu_vk_resources(WlFsr411FullGpu *p) { for (int s = 0; s < 2; s++) { vk->CreateEvent(vk->device, &eci, NULL, &p->ev_e1[s]); vk->CreateEvent(vk->device, &eci, NULL, &p->ev_e2[s]); + vk->CreateEvent(vk->device, &eci, NULL, &p->ev_done[s]); } } + p->gpu_wait = dd_upscale_npu_gpu_wait() && vk->CmdWaitEvents && vk->CmdResetEvent && vk->SetEvent && + p->ev_done[0] && p->ev_done[1]; + F411_LOGI("FSR 4.1.1 Full NPU async wait: %s", p->gpu_wait ? "GPU, next frame" : "CPU, recording thread"); if (!p->npu_thr_started) { if (pthread_create(&p->npu_thr, NULL, f411_npu_exec_worker, p) == 0) { @@ -1025,6 +1048,7 @@ void wl_fsr411_full_gpu_release_sync_events(WlFsr411FullGpu *p) { if (p->vk.SetEvent) { for (int s = 0; s < 2; s++) { if (p->ev_e2[s]) p->vk.SetEvent(p->vk.device, p->ev_e2[s]); + if (p->ev_done[s]) p->vk.SetEvent(p->vk.device, p->ev_done[s]); } } } @@ -1472,7 +1496,7 @@ int wl_fsr411_full_gpu_dispatch(WlFsr411FullGpu *p, VkCommandBuffer cb, pthread_mutex_lock(&p->npu_mu); p->npu_slot_done[ns] = 0; { - uint32_t qi = p->npu_q_tail & 3u; + uint32_t qi = p->npu_q_tail & 7u; p->npu_q_slot[qi] = ns; p->npu_q_sync[qi] = 1; p->npu_q_seq[qi] = seq; @@ -1510,21 +1534,34 @@ int wl_fsr411_full_gpu_dispatch(WlFsr411FullGpu *p, VkCommandBuffer cb, uint32_t seq = (uint32_t)(++p->npu_frame_idx); int prev_ready = 0; + const int gpu_wait = p->gpu_wait; if (p->npu_slot_submitted[prev] && p->npu_slot_meta_valid[prev]) { - pthread_mutex_lock(&p->npu_mu); - struct timespec dl; - clock_gettime(CLOCK_REALTIME, &dl); - dl.tv_nsec += 200000000L; - if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } - while (!p->npu_slot_done[prev] && !p->npu_stop) { - if (pthread_cond_timedwait(&p->npu_done_cv, &p->npu_mu, &dl) == ETIMEDOUT) break; + if (gpu_wait) { + prev_ready = 1; // the GPU waits for ev_done[prev] below; the recording thread does not block + } else { + pthread_mutex_lock(&p->npu_mu); + struct timespec dl; + clock_gettime(CLOCK_REALTIME, &dl); + dl.tv_nsec += 200000000L; + if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } + while (!p->npu_slot_done[prev] && !p->npu_stop) { + if (pthread_cond_timedwait(&p->npu_done_cv, &p->npu_mu, &dl) == ETIMEDOUT) break; + } + prev_ready = p->npu_slot_done[prev]; + pthread_mutex_unlock(&p->npu_mu); } - prev_ready = p->npu_slot_done[prev]; - pthread_mutex_unlock(&p->npu_mu); } // 1. Run pass-27 (postpass) on prev slot's completed HTP output if (prev_ready && p->npu_color_copy[prev].view) { + if (gpu_wait) { + // The NPU worker sets ev_done[prev] once frame N-1's result is in the buffer. + wl_gpu_timer_pause(&p->timer, &p->vk, cb, tslot); + VkMemoryBarrier mb_done = { VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL, VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT }; + p->vk.CmdWaitEvents(cb, 1u, &p->ev_done[prev], VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + 1u, &mb_done, 0u, NULL, 0u, NULL); + wl_gpu_timer_resume(&p->timer, &p->vk, cb, tslot); + } uint8_t *pcm = (uint8_t *)p->const_buf[slot_post].mapped; memset(pcm, 0, F411_CONST_BYTES); memcpy(pcm, &p->npu_prev_c[prev], sizeof(F411Consts)); @@ -1588,6 +1625,9 @@ int wl_fsr411_full_gpu_dispatch(WlFsr411FullGpu *p, VkCommandBuffer cb, p->vk.CmdPushConstants(cb, p->pipe_lay, VK_SHADER_STAGE_COMPUTE_BIT, 0, 16, push_pre); p->vk.CmdDispatch(cb, f411_div_up(F411_UW, 16u), f411_div_up(F411_UH, 16u), 1u); + // Unsignal ev_done[cur] before the retire value that lets the worker start on this slot; it runs after all + // earlier compute work, so the previous wait on this event has finished. + if (gpu_wait) p->vk.CmdResetEvent(cb, p->ev_done[cur], VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); f411_mem_bar2(p, cb, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_WRITE_BIT, VK_PIPELINE_STAGE_2_TRANSFER_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT); p->vk.CmdFillBuffer(cb, p->npu_retire_buf.buf, (VkDeviceSize)cur * 4u, 4u, seq); @@ -1623,10 +1663,24 @@ int wl_fsr411_full_gpu_dispatch(WlFsr411FullGpu *p, VkCommandBuffer cb, p->npu_slot_meta_valid[cur] = 1; pthread_mutex_lock(&p->npu_mu); - p->npu_slot_submitted[cur] = 1; - p->npu_slot_done[cur] = 0; - { - uint32_t qi = p->npu_q_tail & 3u; + if (p->npu_q_tail - p->npu_q_head >= 8u) { + // Recording ran 8 frames ahead of the NPU: wait (bounded) for room instead of overwriting a queued job. + struct timespec dl; + clock_gettime(CLOCK_REALTIME, &dl); + dl.tv_nsec += 200000000L; + if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } + while (p->npu_q_tail - p->npu_q_head >= 8u && !p->npu_stop) { + if (pthread_cond_timedwait(&p->npu_done_cv, &p->npu_mu, &dl) == ETIMEDOUT) break; + } + } + if (p->npu_q_tail - p->npu_q_head >= 8u) { + // Still full: drop this frame's NPU job; the next frame neither waits for it nor uses its result. + p->npu_slot_meta_valid[cur] = 0; + p->npu_slot_submitted[cur] = 0; + } else { + p->npu_slot_submitted[cur] = 1; + p->npu_slot_done[cur] = 0; + uint32_t qi = p->npu_q_tail & 7u; p->npu_q_slot[qi] = cur; p->npu_q_sync[qi] = 0; p->npu_q_seq[qi] = seq; @@ -1840,6 +1894,7 @@ void wl_fsr411_full_gpu_destroy(WlFsr411FullGpu *p) { f411_free_img(p, &p->npu_color_copy[s]); if (p->ev_e1[s] && p->vk.DestroyEvent) p->vk.DestroyEvent(p->vk.device, p->ev_e1[s], NULL); if (p->ev_e2[s] && p->vk.DestroyEvent) p->vk.DestroyEvent(p->vk.device, p->ev_e2[s], NULL); + if (p->ev_done[s] && p->vk.DestroyEvent) p->vk.DestroyEvent(p->vk.device, p->ev_done[s], NULL); } f411_free_buf(p, &p->npu_retire_buf); if (p->npu_ahb_staging && p->npu_ahb_mapped && p->npu_ahb_mem) p->vk.UnmapMemory(p->vk.device, p->npu_ahb_mem); diff --git a/tools/upscale/layer/fsr412_lite.c b/tools/upscale/layer/fsr412_lite.c index b9205794fd..966cf8b8e6 100644 --- a/tools/upscale/layer/fsr412_lite.c +++ b/tools/upscale/layer/fsr412_lite.c @@ -225,15 +225,17 @@ struct WlFsr412Lite { // Sync/Async HTP worker + events for zero-latency Sync mode VkEvent ev_e1[2]; VkEvent ev_e2[2]; + VkEvent ev_done[2]; // async: set by the NPU worker, waited on by the next frame on the GPU + int gpu_wait; // async waits on the GPU (default) instead of the recording thread pthread_mutex_t npu_mu; pthread_cond_t npu_cv; pthread_cond_t npu_done_cv; pthread_t npu_thr; int npu_thr_started; volatile int npu_stop; - int npu_q_slot[4]; - int npu_q_sync[4]; - uint32_t npu_q_seq[4]; + int npu_q_slot[8]; + int npu_q_sync[8]; + uint32_t npu_q_seq[8]; uint32_t npu_q_head; uint32_t npu_q_tail; int npu_slot_submitted[2]; @@ -514,17 +516,28 @@ static void *l4_npu_exec_worker(void *arg) { for (;;) { pthread_mutex_lock(&c->npu_mu); while (c->npu_q_head == c->npu_q_tail && !c->npu_stop) { - if (!htp_is_idle && c->npu_qnn) { + const int want_idle = !htp_is_idle && c->npu_qnn; + if (want_idle || c->gpu_wait) { struct timespec dl; clock_gettime(CLOCK_REALTIME, &dl); - dl.tv_nsec += 500000000L; // 500 ms idle drop-off -> release HTP/Bus/HMX vote + // 500 ms idle drop-off -> release HTP/Bus/HMX vote; 250 ms watchdog tick in GPU-wait mode + dl.tv_nsec += want_idle ? 500000000L : 250000000L; if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } int wrc = pthread_cond_timedwait(&c->npu_cv, &c->npu_mu, &dl); if (wrc == ETIMEDOUT && c->npu_q_head == c->npu_q_tail && !c->npu_stop) { - pthread_mutex_unlock(&c->npu_mu); - wl_fsr4_qnn_set_power_mode("idle"); - htp_is_idle = 1; - pthread_mutex_lock(&c->npu_mu); + if (c->gpu_wait) { + // No job queued: release any GPU wait left without a job (e.g. a command buffer run twice). + // Under npu_mu, so it cannot land after a newly queued frame's reset. + for (int s = 0; s < 2; s++) { + if (c->ev_done[s]) c->vk.SetEvent(c->vk.device, c->ev_done[s]); + } + } + if (want_idle) { + pthread_mutex_unlock(&c->npu_mu); + wl_fsr4_qnn_set_power_mode("idle"); + htp_is_idle = 1; + pthread_mutex_lock(&c->npu_mu); + } } } else { pthread_cond_wait(&c->npu_cv, &c->npu_mu); @@ -534,7 +547,7 @@ static void *l4_npu_exec_worker(void *arg) { pthread_mutex_unlock(&c->npu_mu); break; } - uint32_t qi = c->npu_q_head & 3u; + uint32_t qi = c->npu_q_head & 7u; int slot = c->npu_q_slot[qi]; int is_sync = c->npu_q_sync[qi]; uint32_t seq = c->npu_q_seq[qi]; @@ -614,7 +627,9 @@ static void *l4_npu_exec_worker(void *arg) { } else if (c->npu_retire_buf.mapped) { // Async mode: 1 ms initial sleep + 250 us adaptive poll (~99% fewer CPU timer wakeups!) volatile uint32_t *rp = (volatile uint32_t *)c->npu_retire_buf.mapped; - for (int iter = 0; iter < 1000 && !c->npu_stop; iter++) { + // GPU-wait mode: nothing blocks on the CPU here, so allow ~2 s for a slow or queued-up GPU frame. + const int max_iter = c->gpu_wait ? 8000 : 1000; + for (int iter = 0; iter < max_iter && !c->npu_stop; iter++) { uint32_t cur_seq = __atomic_load_n(&rp[slot], __ATOMIC_ACQUIRE); if ((int32_t)(cur_seq - seq) >= 0) { gpu_ready = 1; @@ -691,6 +706,10 @@ static void *l4_npu_exec_worker(void *arg) { if (is_sync && c->ev_e2[slot] && c->vk.SetEvent) { c->vk.SetEvent(c->vk.device, c->ev_e2[slot]); } + // Always, also after a timeout, so the next frame's GPU wait on it cannot hang. + if (c->ev_done[slot] && c->vk.SetEvent) { + c->vk.SetEvent(c->vk.device, c->ev_done[slot]); + } pthread_mutex_lock(&c->npu_mu); c->npu_slot_done[slot] = 1; @@ -823,8 +842,12 @@ static int l4_bind_npu_vk(WlFsr412Lite *c) { for (int s = 0; s < 2; s++) { vk->CreateEvent(vk->device, &eci, NULL, &c->ev_e1[s]); vk->CreateEvent(vk->device, &eci, NULL, &c->ev_e2[s]); + vk->CreateEvent(vk->device, &eci, NULL, &c->ev_done[s]); } } + c->gpu_wait = dd_upscale_npu_gpu_wait() && vk->CmdWaitEvents && vk->CmdResetEvent && vk->SetEvent && + c->ev_done[0] && c->ev_done[1]; + L4_LOGI("FSR 4.1.2 Lite NPU async wait: %s", c->gpu_wait ? "GPU, next frame" : "CPU, recording thread"); if (!c->npu_thr_started) { if (pthread_create(&c->npu_thr, NULL, l4_npu_exec_worker, c) == 0) { @@ -1014,6 +1037,7 @@ void wl_fsr412_lite_release_sync_events(WlFsr412Lite *c) { if (c->vk.SetEvent) { for (int s = 0; s < 2; s++) { if (c->ev_e2[s]) c->vk.SetEvent(c->vk.device, c->ev_e2[s]); + if (c->ev_done[s]) c->vk.SetEvent(c->vk.device, c->ev_done[s]); } } } @@ -1440,7 +1464,7 @@ int wl_fsr412_lite_dispatch(WlFsr412Lite *c, VkCommandBuffer cb, pthread_mutex_lock(&c->npu_mu); c->npu_slot_done[ns] = 0; { - uint32_t qi = c->npu_q_tail & 3u; + uint32_t qi = c->npu_q_tail & 7u; c->npu_q_slot[qi] = ns; c->npu_q_sync[qi] = 1; c->npu_q_seq[qi] = seq; @@ -1471,20 +1495,33 @@ int wl_fsr412_lite_dispatch(WlFsr412Lite *c, VkCommandBuffer cb, // 1. Wait for previous slot's HTP job and run K1/K2 directly on npu_scratch_buf[prev] int prev_ready = 0; + const int gpu_wait = c->gpu_wait; if (c->npu_slot_submitted[prev] && c->npu_slot_meta_valid[prev]) { - pthread_mutex_lock(&c->npu_mu); - struct timespec dl; - clock_gettime(CLOCK_REALTIME, &dl); - dl.tv_nsec += 200000000L; // 200 ms bounded wait (handles 20-40 FPS GPU frame times without flicker) - if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } - while (!c->npu_slot_done[prev] && !c->npu_stop) { - if (pthread_cond_timedwait(&c->npu_done_cv, &c->npu_mu, &dl) == ETIMEDOUT) break; + if (gpu_wait) { + prev_ready = 1; // the GPU waits for ev_done[prev] below; the recording thread does not block + } else { + pthread_mutex_lock(&c->npu_mu); + struct timespec dl; + clock_gettime(CLOCK_REALTIME, &dl); + dl.tv_nsec += 200000000L; // 200 ms bounded wait (handles 20-40 FPS GPU frame times without flicker) + if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } + while (!c->npu_slot_done[prev] && !c->npu_stop) { + if (pthread_cond_timedwait(&c->npu_done_cv, &c->npu_mu, &dl) == ETIMEDOUT) break; + } + prev_ready = c->npu_slot_done[prev]; + pthread_mutex_unlock(&c->npu_mu); } - prev_ready = c->npu_slot_done[prev]; - pthread_mutex_unlock(&c->npu_mu); } if (prev_ready && c->npu_color_copy[prev].view) { + if (gpu_wait) { + // The NPU worker sets ev_done[prev] once frame N-1's result is in the buffer. + wl_gpu_timer_pause(&c->timer, vk, cb, tslot); + VkMemoryBarrier mb_done = { VK_STRUCTURE_TYPE_MEMORY_BARRIER, NULL, VK_ACCESS_HOST_WRITE_BIT, VK_ACCESS_SHADER_READ_BIT }; + vk->CmdWaitEvents(cb, 1u, &c->ev_done[prev], VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + 1u, &mb_done, 0u, NULL, 0u, NULL); + wl_gpu_timer_resume(&c->timer, vk, cb, tslot); + } uint32_t pslot = c->slot_idx; c->slot_idx = (c->slot_idx + 1u) % L4_SLOTS; uint8_t *pub = c->ubo.mapped + (VkDeviceSize)(pslot * L4_PASSES) * c->ubo_stride; @@ -1509,6 +1546,9 @@ int wl_fsr412_lite_dispatch(WlFsr412Lite *c, VkCommandBuffer cb, if (run_spd) l4_run_pass(c, cb, c->pipe[0], 0u, slot, rw, rh, 0); l4_run_pass(c, cb, c->pipe[1], 1u, slot, rw, rh, run_spd ? 1 : 0); + // Unsignal ev_done[cur] before the retire value that lets the worker start on this slot; it runs after all + // earlier compute work, so the previous wait on this event has finished. + if (gpu_wait) vk->CmdResetEvent(cb, c->ev_done[cur], VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); l4_mem_bar(vk, cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT); vk->CmdFillBuffer(cb, c->npu_retire_buf.buf, (VkDeviceSize)cur * 4u, 4u, seq); @@ -1540,10 +1580,24 @@ int wl_fsr412_lite_dispatch(WlFsr412Lite *c, VkCommandBuffer cb, c->npu_slot_meta_valid[cur] = 1; pthread_mutex_lock(&c->npu_mu); - c->npu_slot_submitted[cur] = 1; - c->npu_slot_done[cur] = 0; - { - uint32_t qi = c->npu_q_tail & 3u; + if (c->npu_q_tail - c->npu_q_head >= 8u) { + // Recording ran 8 frames ahead of the NPU: wait (bounded) for room instead of overwriting a queued job. + struct timespec dl; + clock_gettime(CLOCK_REALTIME, &dl); + dl.tv_nsec += 200000000L; + if (dl.tv_nsec >= 1000000000L) { dl.tv_sec++; dl.tv_nsec -= 1000000000L; } + while (c->npu_q_tail - c->npu_q_head >= 8u && !c->npu_stop) { + if (pthread_cond_timedwait(&c->npu_done_cv, &c->npu_mu, &dl) == ETIMEDOUT) break; + } + } + if (c->npu_q_tail - c->npu_q_head >= 8u) { + // Still full: drop this frame's NPU job; the next frame neither waits for it nor uses its result. + c->npu_slot_meta_valid[cur] = 0; + c->npu_slot_submitted[cur] = 0; + } else { + c->npu_slot_submitted[cur] = 1; + c->npu_slot_done[cur] = 0; + uint32_t qi = c->npu_q_tail & 7u; c->npu_q_slot[qi] = cur; c->npu_q_sync[qi] = 0; c->npu_q_seq[qi] = seq; @@ -1667,6 +1721,7 @@ void wl_fsr412_lite_destroy(WlFsr412Lite *c) { for (int s = 0; s < 2; s++) { if (c->ev_e1[s]) vk->DestroyEvent(vk->device, c->ev_e1[s], NULL); if (c->ev_e2[s]) vk->DestroyEvent(vk->device, c->ev_e2[s], NULL); + if (c->ev_done[s]) vk->DestroyEvent(vk->device, c->ev_done[s], NULL); } } l4_free_npu_scratch(c);