From 68940c862bc8148c8de86568f56e8387b2e37312 Mon Sep 17 00:00:00 2001 From: Hy300leosquizz Date: Sat, 5 Dec 2026 12:53:03 -0300 Subject: [PATCH] fix(zerofg): four frame-generation fixes split out of #3611 (#3731) --- .../main/cpp/waylandcomp/src/effects_chain.c | 28 +++++++++++++------ .../main/cpp/waylandcomp/src/effects_chain.h | 2 +- .../cpp/waylandcomp/src/framegen_bridge.c | 14 +++++++++- .../cpp/waylandcomp/src/framegen_bridge.h | 3 ++ .../cpp/waylandcomp/src/framegen_zerofg.cpp | 22 +++++++++++++++ app/src/main/cpp/waylandcomp/src/sc_layer.c | 1 + app/src/main/cpp/waylandcomp/src/vk_present.c | 7 +++-- tools/tests/effects_source_cache_harness.c | 2 +- tools/tests/test_effects_source_resolution.py | 3 +- tools/tests/vk_terminal_callers_test.c | 2 +- 10 files changed, 68 insertions(+), 16 deletions(-) diff --git a/app/src/main/cpp/waylandcomp/src/effects_chain.c b/app/src/main/cpp/waylandcomp/src/effects_chain.c index 6d307dcd7d..83cc12c38a 100644 --- a/app/src/main/cpp/waylandcomp/src/effects_chain.c +++ b/app/src/main/cpp/waylandcomp/src/effects_chain.c @@ -785,6 +785,12 @@ struct fx_source_state { VkAccessFlags access; }; +/* NIS here is the SDR implementation and is documented for <=2x upscales: other outputs run FSR instead. */ +static int nis_falls_back(int scene_w, int scene_h, int cw, int ch) { + return g_scene_is_pq || cw < scene_w || ch < scene_h || (double)cw > (double)scene_w * 2.0 || + (double)ch > (double)scene_h * 2.0; +} + static VkImage effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int scene_h, int out_w, int out_h, int *res_w, int *res_h, VkImageLayout input_layout, const struct fx_settings *frozen, @@ -810,11 +816,7 @@ static VkImage effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int const int upscaling = cw > scene_w || ch > scene_h; int spatial = 0; if (spatial_mode(s->scaling) && (s->scaling == 6 || upscaling)) spatial = s->scaling; - /* NIS here is the SDR implementation and is documented for <=2x upscales. */ - const int nis_fallback = spatial == 7 && - (g_scene_is_pq || cw < scene_w || ch < scene_h || - (double)cw > (double)scene_w * 2.0 || - (double)ch > (double)scene_h * 2.0); + const int nis_fallback = spatial == 7 && nis_falls_back(scene_w, scene_h, cw, ch); static int last_nis_fallback = 0; const int was_nis_fallback = __atomic_exchange_n(&last_nis_fallback, nis_fallback, __ATOMIC_RELAXED); @@ -1037,13 +1039,23 @@ VkImage vkp_effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int sce VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, NULL, NULL); } -int vkp_effects_prepare_generated(void) { +int vkp_effects_prepare_generated(int in_w, int in_h, int out_w, int out_h) { vkp_effects_sync(); pthread_mutex_lock(&g_lock); g_generated_settings = g_cur; pthread_mutex_unlock(&g_lock); - return spatial_mode(g_generated_settings.scaling) && g_generated_settings.scaling != 6 && - effect_count(&g_generated_settings) == 0; + int spatial = g_generated_settings.scaling; + if (!spatial_mode(spatial) || spatial == 6 || effect_count(&g_generated_settings) != 0) return 0; + /* Build the spatial filter's pipelines now, before frame generation records commands that sample the + * chain's views: one failing later in the same pass would tear the chain down (chain_failed) under a + * recorded command buffer. Build only what effects_run will use for this output, as it decides it: + * NIS on an output it hands to FSR builds FSR, so a NIS that cannot build here costs nothing. */ + int cw = out_w > FX_MAX_DIM ? FX_MAX_DIM : out_w < 1 ? 1 : out_w; + int ch = out_h > FX_MAX_DIM ? FX_MAX_DIM : out_h < 1 ? 1 : out_h; + if (cw <= in_w && ch <= in_h) return 1; /* clamped, it no longer upscales: effects_run builds no filter */ + if (spatial == 7 && nis_falls_back(in_w, in_h, cw, ch)) spatial = 4; + if (objects_ensure() != 0 || pipelines_ensure(spatial, &g_generated_settings) != 0) return 0; + return 1; } VkImage vkp_effects_run_before_generated(VkCommandBuffer cmd, VkImage scene, int w, int h, diff --git a/app/src/main/cpp/waylandcomp/src/effects_chain.h b/app/src/main/cpp/waylandcomp/src/effects_chain.h index a0aba31138..b85becc0a5 100644 --- a/app/src/main/cpp/waylandcomp/src/effects_chain.h +++ b/app/src/main/cpp/waylandcomp/src/effects_chain.h @@ -18,7 +18,7 @@ VkFilter vkp_effects_blit_filter(void); VkImage vkp_effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int scene_h, int out_w, int out_h, int *res_w, int *res_h); -int vkp_effects_prepare_generated(void); +int vkp_effects_prepare_generated(int in_w, int in_h, int out_w, int out_h); void vkp_effects_release_generated_sources(void); VkImage vkp_effects_run_before_generated(VkCommandBuffer cmd, VkImage scene, int w, int h, int *res_w, int *res_h); diff --git a/app/src/main/cpp/waylandcomp/src/framegen_bridge.c b/app/src/main/cpp/waylandcomp/src/framegen_bridge.c index b3badeea27..b402dc0a8a 100644 --- a/app/src/main/cpp/waylandcomp/src/framegen_bridge.c +++ b/app/src/main/cpp/waylandcomp/src/framegen_bridge.c @@ -113,6 +113,7 @@ static int g_layer_path; static int g_out_gens; static int64_t g_out_planned[VKP_FG_MAX_GENERATIONS + 1]; static int64_t g_completion_issue; +static int g_paced_route = 1; void vkp_framegen_set_pacer(int on) { if (atomic_exchange(&g_pacer_on, on ? 1 : 0) != (on ? 1 : 0)) { @@ -178,6 +179,17 @@ void vkp_framegen_set_layer_timing(int layer) { fge_set_layer_timing(layer); } +void vkp_framegen_set_paced_route(int composed) { + composed = composed != 0; + if (composed == g_paced_route) return; + g_paced_route = composed; + if (atomic_load(&g_pacer_on)) { + fge_pacer_reset(); + FGLOG("ZeroPacer %s", composed ? "times the layer's frames again (composed route)" + : "waits: the direct route presents the real frame on its own timing"); + } +} + void vkp_framegen_set_tuning(float flow_scale, float refresh_hz) { if (flow_scale < 0.25f) flow_scale = 0.25f; if (flow_scale > 1.0f) flow_scale = 1.0f; @@ -517,7 +529,7 @@ int vkp_framegen_run(VkCommandBuffer cmd, VkImage scene, VkImageView scene_view, } } fge_set_phases(NULL, 0); - if (kind == VKP_FG_ENGINE_ZEROFG && pacer_wanted() && g_layer_path) { + if (kind == VKP_FG_ENGINE_ZEROFG && pacer_wanted() && g_layer_path && g_paced_route) { int64_t vsync = 0, period = 0; sc_layer_timeline(&vsync, &period); int64_t planned[VKP_FG_MAX_GENERATIONS + 1]; diff --git a/app/src/main/cpp/waylandcomp/src/framegen_bridge.h b/app/src/main/cpp/waylandcomp/src/framegen_bridge.h index 3412eda7f1..4b7e215446 100644 --- a/app/src/main/cpp/waylandcomp/src/framegen_bridge.h +++ b/app/src/main/cpp/waylandcomp/src/framegen_bridge.h @@ -30,6 +30,9 @@ int vkp_framegen_extra_images(void); void vkp_framegen_set_layer_timing(int layer); /* ZeroFG's pacer (include/zerofg/inline_pacer.h) times the layer's frames instead of the layer's own spacing. */ void vkp_framegen_set_pacer(int on); +/* 1 when the layer pass presents the real frame itself after its generated frames (the composed route): only + * then can the pacer time the whole group. The direct route keeps the layer's own timing. */ +void vkp_framegen_set_paced_route(int composed); /* Reset pacing history when a planned real-frame pair is aborted before presentation. */ void vkp_framegen_pacer_abort(void); /* Refresh planned for output k of the last run (k == gens: its real frame; 0: use layer timing). */ diff --git a/app/src/main/cpp/waylandcomp/src/framegen_zerofg.cpp b/app/src/main/cpp/waylandcomp/src/framegen_zerofg.cpp index 786c830964..b0da6b2af1 100644 --- a/app/src/main/cpp/waylandcomp/src/framegen_zerofg.cpp +++ b/app/src/main/cpp/waylandcomp/src/framegen_zerofg.cpp @@ -55,6 +55,15 @@ VkImage g_current = VK_NULL_HANDLE; VkImageView g_current_view = VK_NULL_HANDLE; uint64_t g_current_sequence = 0; bool g_pair_pending = false; +// The interpolator keeps per-pair work and the prepared Real by sequence. A pass dropped after recording +// Interpolate never ran on the GPU, so the next pair's A gets a fresh identity no record has seen: nothing the +// dropped pass would have prepared is reused, nor its flow start. The identity is stable for the whole pair, so +// its phases still share their work (and ReallyZero sees one pair, not a first picture per phase). +bool g_interpolate_uncommitted = false; +bool g_record_stale = false; +uint64_t g_pair_anonymous = 0; // the recovery pair's A identity, 0 for an ordinary pair +uint64_t g_anonymous_count = 0; +constexpr uint64_t kAnonymousSequence = 1ull << 63; // above any frame sequence the compositor hands out // Pacing. The compositor presents a pair's generated frames and its real frame back to back in FIFO, // so they reach the panel on consecutive refreshes right after the tick that drew them. @@ -475,6 +484,9 @@ int zfg_prepare(uint32_t width, uint32_t height, VkFormat format) { return 0; } if (g_width == width && g_height == height && g_format == format && g_history) return 1; + // ReallyZero's Resize accepts 64..16384 per side: refuse this frame only, before touching the + // engine's state, so a supported size later still generates. + if (width < 64 || height < 64 || width > 16384 || height > 16384) return 0; if (g_DeviceWaitIdle) g_DeviceWaitIdle(g_device); destroy_history(); const zerofg::Status resized = g_interpolator->Resize(width, height, format, format); @@ -540,6 +552,8 @@ void zfg_process(VkCommandBuffer cmd, VkImage source, VkImageView source_view, g_current_view = source_view; g_current_sequence = sequence; g_pair_pending = true; + g_pair_anonymous = g_record_stale ? (kAnonymousSequence | ++g_anonymous_count) : 0; + g_record_stale = false; } void zfg_generate(VkCommandBuffer cmd, uint32_t index, uint32_t count, @@ -564,6 +578,8 @@ void zfg_generate(VkCommandBuffer cmd, uint32_t index, uint32_t count, result.width = width; result.height = height; result.usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; + if (g_pair_anonymous) previous.sequence = g_pair_anonymous; + g_interpolate_uncommitted = true; const zerofg::Status status = g_interpolator->Interpolate(cmd, index, previous, current, phase, result); @@ -579,6 +595,7 @@ void zfg_generate(VkCommandBuffer cmd, uint32_t index, uint32_t count, } void zfg_commit_history_update(void) { + g_interpolate_uncommitted = false; if (!g_history_recorded_pending) return; g_history_layout_defined = true; g_history_valid = true; @@ -589,10 +606,15 @@ void zfg_commit_history_update(void) { void zfg_discard_history_update(void) { g_history_recorded_pending = false; g_history_recorded_sequence = 0; + if (g_interpolate_uncommitted) g_record_stale = true; + g_interpolate_uncommitted = false; + g_pair_pending = false; } void zfg_forget_targets(void) { g_pair_pending = false; + if (g_interpolate_uncommitted) g_record_stale = true; + g_interpolate_uncommitted = false; g_history_recorded_pending = false; g_history_recorded_sequence = 0; g_history_valid = false; diff --git a/app/src/main/cpp/waylandcomp/src/sc_layer.c b/app/src/main/cpp/waylandcomp/src/sc_layer.c index d38cc7ede5..fb93305e4b 100644 --- a/app/src/main/cpp/waylandcomp/src/sc_layer.c +++ b/app/src/main/cpp/waylandcomp/src/sc_layer.c @@ -1138,6 +1138,7 @@ int sc_layer_present_generated(const struct vkp_draw *draws, int n_draws, const if (vkp_update_map(scene_w, scene_h) != 0) return -1; const int deep = hf && !hf->tonemap; if (hf) composed = 1; + vkp_framegen_set_paced_route(composed); if (hf && hf->tonemap) color = NULL; int n = vkp_pass_begin_generate(scene_w, scene_h, draws, n_draws, hf, &rw, &rh); if (n < 0) return -1; diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index ab0d50bea6..8801c87286 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -2769,8 +2769,8 @@ static int render_impl(int scene_w, int scene_h, const struct vkp_draw *draws, i const int upscale_w = (int)(scene_w * g_map.kx + 0.5f); const int upscale_h = (int)(scene_h * g_map.ky + 0.5f); const int post_upscale = fx && fg && !hf && !cb && vkp_effects_upscaling() && - vkp_effects_prepare_generated() && - (upscale_w > input_w || upscale_h > input_h); + (upscale_w > input_w || upscale_h > input_h) && + vkp_effects_prepare_generated(input_w, input_h, upscale_w, upscale_h); if (post_upscale) { static int last_input_w, last_input_h, last_output_w, last_output_h; if (input_w != last_input_w || input_h != last_input_h || @@ -3545,7 +3545,8 @@ static int pass_begin_impl(int scene_w, int scene_h, const struct vkp_draw *draw int mapped_w = (int)(scene_w * g_map.kx + 0.5f), mapped_h = (int)(scene_h * g_map.ky + 0.5f); g_pass.post_upscale = generate && !hf && vkp_effects_active() && vkp_effects_upscaling() && - vkp_effects_prepare_generated() && (mapped_w > input_w || mapped_h > input_h); + (mapped_w > input_w || mapped_h > input_h) && + vkp_effects_prepare_generated(input_w, input_h, mapped_w, mapped_h); if (g_pass.post_upscale) { static int last_input_w, last_input_h, last_output_w, last_output_h; if (input_w != last_input_w || input_h != last_input_h || mapped_w != last_output_w || mapped_h != last_output_h) { diff --git a/tools/tests/effects_source_cache_harness.c b/tools/tests/effects_source_cache_harness.c index da9615d1c6..08a4ce5076 100644 --- a/tools/tests/effects_source_cache_harness.c +++ b/tools/tests/effects_source_cache_harness.c @@ -93,7 +93,7 @@ static void check_generated_settings_and_layout(void) { t->img = (VkImage)(uintptr_t)(30 + k); t->w = 1920; t->h = 1080; } vkp_effects_set_scaling(3); - assert(vkp_effects_prepare_generated()); + assert(vkp_effects_prepare_generated(1280, 720, 1920, 1080)); vkp_effects_set_scaling(0); vkp_effects_set_cas(1, 75); vkp_effects_sync(); diff --git a/tools/tests/test_effects_source_resolution.py b/tools/tests/test_effects_source_resolution.py index 38ca6f9f3c..09c9e16f2d 100644 --- a/tools/tests/test_effects_source_resolution.py +++ b/tools/tests/test_effects_source_resolution.py @@ -286,7 +286,8 @@ static VkImage vkp_effects_run(int cmd, int source, int w, int h, int ow, int oh if (fail_effects) { *rw = w; *rh = h; return source; } *rw = ow; *rh = oh; return 4; } -static int vkp_effects_prepare_generated(void) { +static int vkp_effects_prepare_generated(int in_w, int in_h, int out_w, int out_h) { + (void)in_w; (void)in_h; (void)out_w; (void)out_h; return g_cur.scaling >= 3 && g_cur.scaling <= 8 && g_cur.scaling != 6 && !postprocessing; } static VkImage vkp_effects_run_before_generated(int cmd, int source, int w, int h, int *rw, int *rh) { diff --git a/tools/tests/vk_terminal_callers_test.c b/tools/tests/vk_terminal_callers_test.c index be7b2f328a..72e7fa7c28 100644 --- a/tools/tests/vk_terminal_callers_test.c +++ b/tools/tests/vk_terminal_callers_test.c @@ -83,7 +83,7 @@ int vkp_effects_upscaling(void) { return 0; } int vkp_effects_sharpening(void) { return 0; } int vkp_effects_postprocessing(void) { return 0; } void vkp_effects_release_generated_sources(void) {} -int vkp_effects_prepare_generated(void) { assert(0); return 0; } +int vkp_effects_prepare_generated(int in_w, int in_h, int out_w, int out_h) { (void)in_w; (void)in_h; (void)out_w; (void)out_h; assert(0); return 0; } VkImage vkp_effects_run_before_generated(VkCommandBuffer c, VkImage i, int w, int h, int *rw, int *rh) { assert(0); *rw = w; *rh = h; return i; }