Files
DroidDeck/tools/upscale/layer/fsr4_pipeline.c
T
The412Banner 7aa54ab9c7 upscale: hide VK_EXT_descriptor_buffer from games so Full FSR 4.1 shows the picture
Root cause of the black Full FSR 4.1 frame in D3D12 games on Turnip: vkd3d-proton uses its own
descriptor buffers in the command buffers the upscaler records into. Measured on the Red Magic 12
Pro (Adreno 850) with the XeSS DX12 sample: Full black in 9/9 shots, and still black with the direct
output write off and with the exposure pass off; with VKD3D_DISABLE_EXTENSIONS=VK_EXT_descriptor_buffer
the picture is there (5/6 shots coloured, the sixth caught the triangle off screen, as with the other
engines), with the direct write on.

The layer now hides the extension from the game (vkd3d then uses its classic descriptors) and still
enables it for itself; DROIDDECK_UPSCALE_APP_DESCRIPTOR_BUFFER=1 shows it again. The Lite fallback
now triggers only when the game enabled descriptor buffers anyway (Turnip), not for every D3D12
frame. The FULL_DIRECT_OUT / FULL_AUTO_EXPOSURE test switches are removed.
2026-12-04 20:43:28 -05:00

1124 lines
56 KiB
C

// Winlator NeuralScale Multi-Backend Upscaler Dispatcher + Smart Automatic Controller
//
// 6-mode architecture + Smart Automatic mode:
// 0. Automatic (Smart) — 30s FPS/power/FPS-cap/NPU-aware controller with fault blacklist
// 1. FSR 3.1 (GPU) — AMD FidelityFX FSR 3.1 temporal upscaling (safe universal fallback)
// 2. Snapdragon GSR 2 (Adreno GPU) — Qualcomm 3-pass packed YCoCg temporal upscaling (fastest)
// 3. FSR 4.1.2 Lite (Hexagon NPU) — Zero-copy AHB native S8 Hexagon HTP NPU (Sync or Async)
// 4. FSR 4.1.2 Lite (Adreno GPU) — Pure Vulkan DP4a compute with Tier 1 direct-out & SPD skip
// 5. FSR 4.1.1 Full (Hexagon NPU) — Genuine 12-block FSR 4.1.1 Full zero-copy S8 HTP NPU (Sync or Async)
// 6. FSR 4.1.1 Full (Adreno GPU) — Genuine 28-pass DP4a compute with Tier 1 direct-out & SPD skip
#define _GNU_SOURCE
#include "fsr4_pipeline.h"
#include "fsr411_full_gpu.h"
#include "fsr412_lite.h"
#include "fsr3_gpu.h"
#include "sgsr2_gpu.h"
#include "dd_upscale_compat.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define F4_TAG "DroidDeckUpscalePipe"
#define F4_LOGI(...) dd_upscale_log(DD_LOG_INFO, F4_TAG, __VA_ARGS__)
#define F4_LOGW(...) dd_upscale_log(DD_LOG_WARN, F4_TAG, __VA_ARGS__)
#define F4_LOGE(...) dd_upscale_log(DD_LOG_ERROR, F4_TAG, __VA_ARGS__)
// FSR 4.1.1 Full (NPU & GPU) enabled now that descriptor index 11u (prepass/U-Net scratch)
// is properly bound alongside 62u (postpass), eliminating the zero-input ghosting bug.
#define WL_ENABLE_DEPRECATED_FSR411_FULL 1
// Smart Automatic mode tuning constants
#define AUTO_EVAL_WINDOW_NS 30000000000LL /* 30 s active gameplay average before switching */
#define AUTO_UPGRADE_COOLDOWN_NS 60000000000LL /* 60 s dwell after any downgrade before climbing back up */
#define AUTO_CAP_SETTLE_NS 5000000000LL /* 5 s settle when user toggles FPS cap on/off */
#define AUTO_MIN_FRAMES 180u /* require >= 180 active frames in 30 s window */
struct WlFsr4Pipeline {
WlVkDispatch vk;
WlFsr411FullGpu *fsr411_full;
int fsr411_full_tried;
WlFsr412Lite *fsr412_lite;
int fsr412_lite_tried;
WlFsr3Gpu *fsr3_gpu;
int fsr3_gpu_tried;
WlSgsr2Gpu *sgsr2_gpu;
int sgsr2_gpu_tried;
int last_engine;
int turnip; /* -1 = not asked yet */
int full_dx12_logged;
// Smart Automatic controller state
pthread_mutex_t auto_mu;
int auto_inited;
int auto_cur_backend; /* active concrete WL_FSR_BACKEND_* chosen by Automatic */
int auto_cur_npu_sync; /* 1 = Sync, 0 = Async when auto_cur_backend is an NPU mode */
int auto_last_fps_limit; /* tracks live FPS-cap changes */
uint32_t auto_failed_mask; /* per-session permanent fault blacklist (prevents NPU<->FSR3 loops!) */
int64_t auto_win_ns; /* accumulated active-frame time in current window */
uint32_t auto_win_frames; /* accumulated active frames in current window */
uint64_t auto_win_gpu_us; /* sum of GPU upscaler time in window */
uint64_t auto_win_htp_us; /* sum of NPU time in window */
uint32_t auto_win_disp_n; /* number of upscaler dispatches in window */
int64_t auto_last_downgrade_ns; /* monotonic ns of most recent downgrade (for 60 s upgrade guard) */
int64_t auto_cap_changed_ns; /* monotonic ns when user changed FPS cap */
int64_t auto_last_disp_ns; /* fallback frame-delta timer if vkQueuePresentKHR isn't seen */
int auto_seen_present; /* 1 once vkQueuePresentKHR feeds real present deltas */
int64_t auto_hot_since_ns; /* monotonic ns when sustained thermal headroom/temp crossed trigger */
int auto_thermal_cooling; /* 1 while proactive anti-throttling holds cooler mode until <=39.5C / <=0.75 */
char auto_tag_buf[64];
/* Stand-in depth for callers that send none (XeSS makes depth optional): D32, cleared to "far". */
VkImage blank_depth; VkDeviceMemory blank_depth_mem; uint32_t blank_depth_w, blank_depth_h; int blank_depth_far_zero;
};
static int64_t pipe_now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (int64_t)ts.tv_sec * 1000000000LL + (int64_t)ts.tv_nsec;
}
// "" when no data dir is set: every backend then fails to load its files and frames take the blit fallback.
static const char *resolve_fsr4_dir(void) {
const char *dir = dd_upscale_dir();
return dir ? dir : "";
}
static const char *resolve_power_mode(void) {
const char *pwr = dd_upscale_env("POWER");
return pwr ? pwr : "balanced";
}
static void blank_depth_free(WlFsr4Pipeline *p) {
if (p->blank_depth) p->vk.DestroyImage(p->vk.device, p->blank_depth, NULL);
if (p->blank_depth_mem) p->vk.FreeMemory(p->vk.device, p->blank_depth_mem, NULL);
p->blank_depth = VK_NULL_HANDLE; p->blank_depth_mem = VK_NULL_HANDLE;
p->blank_depth_w = p->blank_depth_h = 0;
}
/* A render-sized D32 depth image cleared to the far plane (0 with inverted depth, 1 otherwise), left in
* SHADER_READ_ONLY_OPTIMAL like a game's depth input. Only for callers that pass no depth at all: FSR 4
* then reconstructs from colour and motion alone instead of being skipped. */
static VkImage blank_depth_get(WlFsr4Pipeline *p, VkCommandBuffer cb, uint32_t w, uint32_t h, int inverted) {
if (p->blank_depth && p->blank_depth_w == w && p->blank_depth_h == h && p->blank_depth_far_zero == inverted)
return p->blank_depth;
blank_depth_free(p);
if (!p->vk.CmdClearDepthStencilImage || !p->vk.GetPhysicalDeviceMemoryProperties) return VK_NULL_HANDLE;
VkImageCreateInfo ici = {
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.imageType = VK_IMAGE_TYPE_2D, .format = VK_FORMAT_D32_SFLOAT, .extent = {w, h, 1}, .mipLevels = 1,
.arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
if (p->vk.CreateImage(p->vk.device, &ici, NULL, &p->blank_depth) != VK_SUCCESS) { p->blank_depth = VK_NULL_HANDLE; return VK_NULL_HANDLE; }
VkMemoryRequirements mr;
p->vk.GetImageMemoryRequirements(p->vk.device, p->blank_depth, &mr);
VkPhysicalDeviceMemoryProperties mp;
p->vk.GetPhysicalDeviceMemoryProperties(p->vk.phys_dev, &mp);
uint32_t mt = UINT32_MAX;
for (uint32_t i = 0; i < mp.memoryTypeCount && mt == UINT32_MAX; i++)
if ((mr.memoryTypeBits & (1u << i)) && (mp.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) mt = i;
for (uint32_t i = 0; i < mp.memoryTypeCount && mt == UINT32_MAX; i++)
if (mr.memoryTypeBits & (1u << i)) mt = i;
VkMemoryAllocateInfo ai = {VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = mr.size, .memoryTypeIndex = mt};
if (mt == UINT32_MAX || p->vk.AllocateMemory(p->vk.device, &ai, NULL, &p->blank_depth_mem) != VK_SUCCESS ||
p->vk.BindImageMemory(p->vk.device, p->blank_depth, p->blank_depth_mem, 0) != VK_SUCCESS) {
blank_depth_free(p);
return VK_NULL_HANDLE;
}
VkImageSubresourceRange range = {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, 0, 1};
VkImageMemoryBarrier to_dst = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = p->blank_depth, .subresourceRange = range};
p->vk.CmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &to_dst);
VkClearDepthStencilValue far = {inverted ? 0.0f : 1.0f, 0};
p->vk.CmdClearDepthStencilImage(cb, p->blank_depth, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &far, 1, &range);
VkImageMemoryBarrier to_read = to_dst;
to_read.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; to_read.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
to_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; to_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
p->vk.CmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &to_read);
p->blank_depth_w = w; p->blank_depth_h = h; p->blank_depth_far_zero = inverted;
F4_LOGI("the game sends no depth; a %ux%u stand-in (far = %d) is used", w, h, inverted ? 0 : 1);
return p->blank_depth;
}
static const char *backend_display_name(int b) {
switch (b) {
case WL_FSR_BACKEND_FSR3_GPU: return "FSR3.1-GPU";
case WL_FSR_BACKEND_SGSR2_GPU: return "SGSR2-Adreno";
case WL_FSR_BACKEND_FSR4_1_LITE_NPU: return "FSR4.1-Lite-NPU";
case WL_FSR_BACKEND_FSR4_1_LITE_GPU: return "FSR4.1-Lite-GPU";
case WL_FSR_BACKEND_FSR4_1_FULL_NPU: return "FSR4.1-Full-NPU";
case WL_FSR_BACKEND_FSR4_1_FULL_GPU: return "FSR4.1-Full-GPU";
case WL_FSR_BACKEND_AUTO: return "Auto";
default: return "FSR4.1-Lite-GPU";
}
}
static void record_blit_fallback(WlFsr4Pipeline *p, VkCommandBuffer cb,
VkImage src, VkImageLayout src_lay, uint32_t rw, uint32_t rh,
VkImage dst, VkImageLayout dst_lay, uint32_t ow, uint32_t oh) {
VkImageMemoryBarrier pre[2] = {
{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT, .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
.oldLayout = src_lay, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = src, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}},
{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
.oldLayout = dst_lay, .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = dst, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}},
};
p->vk.CmdPipelineBarrier(cb, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 2, pre);
VkImageBlit region = {
.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.srcOffsets = {{0, 0, 0}, {(int32_t)rw, (int32_t)rh, 1}},
.dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
.dstOffsets = {{0, 0, 0}, {(int32_t)ow, (int32_t)oh, 1}},
};
p->vk.CmdBlitImage(cb, src, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region, VK_FILTER_LINEAR);
VkImageMemoryBarrier post[2] = {
{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT, .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, .newLayout = src_lay,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = src, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}},
{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT, .dstAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, .newLayout = dst_lay,
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
.image = dst, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}},
};
p->vk.CmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, NULL, 0, NULL, 2, post);
}
WlFsr4Pipeline *wl_fsr4_pipe_create(const WlVkDispatch *vk) {
WlFsr4Pipeline *p = (WlFsr4Pipeline *)calloc(1, sizeof *p);
if (!p) return NULL;
p->vk = *vk;
p->last_engine = -1;
p->turnip = -1;
p->auto_cur_backend = WL_FSR_BACKEND_FSR3_GPU;
pthread_mutex_init(&p->auto_mu, NULL);
return p;
}
void wl_fsr4_pipe_release_sync_events(WlFsr4Pipeline *p) {
if (!p) return;
if (p->fsr411_full) wl_fsr411_full_gpu_release_sync_events(p->fsr411_full);
if (p->fsr412_lite) wl_fsr412_lite_release_sync_events(p->fsr412_lite);
}
void wl_fsr4_pipe_destroy(WlFsr4Pipeline *p) {
if (!p) return;
wl_fsr4_pipe_release_sync_events(p);
if (p->vk.DeviceWaitIdle) p->vk.DeviceWaitIdle(p->vk.device);
if (p->fsr411_full) { wl_fsr411_full_gpu_destroy(p->fsr411_full); p->fsr411_full = NULL; }
if (p->fsr412_lite) { wl_fsr412_lite_destroy(p->fsr412_lite); p->fsr412_lite = NULL; }
if (p->fsr3_gpu) { wl_fsr3_gpu_destroy(p->fsr3_gpu); p->fsr3_gpu = NULL; }
if (p->sgsr2_gpu) { wl_sgsr2_gpu_destroy(p->sgsr2_gpu); p->sgsr2_gpu = NULL; }
blank_depth_free(p);
pthread_mutex_destroy(&p->auto_mu);
free(p);
}
void wl_fsr4_pipe_record_present(WlFsr4Pipeline *p, int64_t dt_ns) {
if (!p) return;
// Ignore loading screens, pauses, and micro-duplicates (keep 2 FPS .. 1000 FPS)
if (dt_ns <= 1000000LL || dt_ns >= 500000000LL) return;
pthread_mutex_lock(&p->auto_mu);
p->auto_seen_present = 1;
p->auto_win_ns += dt_ns;
p->auto_win_frames++;
pthread_mutex_unlock(&p->auto_mu);
}
uint64_t wl_fsr4_pipe_caps(const WlFsr4Pipeline *p) {
uint64_t c = 1u; // bit 0: layer alive
if (p && (wl_fsr411_full_gpu_is_ready(p->fsr411_full) ||
wl_fsr412_lite_is_gpu_ready(p->fsr412_lite) ||
wl_fsr3_gpu_is_ready(p->fsr3_gpu) ||
wl_sgsr2_gpu_is_ready(p->sgsr2_gpu)))
c |= (1u << 1) | (1u << 2);
return c;
}
void wl_fsr4_pipe_diag(const WlFsr4Pipeline *p, const char **out_ahb, const char **out_npu_state) {
const char *ahb = "pending";
const char *npu = "standby";
if (p) {
switch (p->last_engine) {
case WL_FSR_BACKEND_FSR3_GPU:
ahb = "gpu_only";
npu = wl_fsr3_gpu_is_ready(p->fsr3_gpu) ? "fsr3_gpu" : "standby";
break;
case WL_FSR_BACKEND_SGSR2_GPU:
ahb = "gpu_only";
npu = wl_sgsr2_gpu_is_ready(p->sgsr2_gpu) ? "sgsr2_gpu" : "standby";
break;
case WL_FSR_BACKEND_FSR4_1_FULL_GPU:
ahb = "gpu_only";
npu = wl_fsr411_full_gpu_is_ready(p->fsr411_full) ? "fsr411_full_gpu" : "standby";
break;
case WL_FSR_BACKEND_FSR4_1_FULL_NPU:
ahb = p->vk.has_ext_ahb ? "zc" : "staging";
npu = p->fsr411_full ? wl_fsr411_full_gpu_npu_state(p->fsr411_full) : "standby";
break;
case WL_FSR_BACKEND_FSR4_1_LITE_GPU:
ahb = "gpu_only";
npu = wl_fsr412_lite_is_gpu_ready(p->fsr412_lite) ? "fsr412_lite_gpu" : "standby";
break;
case WL_FSR_BACKEND_FSR4_1_LITE_NPU:
default:
ahb = p->vk.has_ext_ahb ? "zc" : "staging";
npu = p->fsr412_lite ? wl_fsr412_lite_npu_state(p->fsr412_lite) : "standby";
break;
}
}
if (out_ahb) *out_ahb = ahb;
if (out_npu_state) *out_npu_state = npu;
}
static void auto_reset_window_locked(WlFsr4Pipeline *p) {
p->auto_win_ns = 0;
p->auto_win_frames = 0;
p->auto_win_gpu_us = 0;
p->auto_win_htp_us = 0;
p->auto_win_disp_n = 0;
}
static int auto_allowed(const WlFsr4Pipeline *p, int backend, int has_supported_npu) {
if (p->auto_failed_mask & (1u << (uint32_t)backend)) return 0;
#if !WL_ENABLE_DEPRECATED_FSR411_FULL
if (backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU ||
backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
return 0;
}
#endif
if ((backend == WL_FSR_BACKEND_FSR4_1_LITE_NPU || backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU) && !has_supported_npu) {
return 0;
}
if ((backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU || backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU) &&
(!p->vk.has_desc_buf || !p->vk.has_mut_desc)) {
return 0;
}
return 1;
}
static int auto_is_npu_backend(int backend) {
return backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU || backend == WL_FSR_BACKEND_FSR4_1_LITE_NPU;
}
// Smart Automatic mode chooser:
// - Runs over 30-second averages of active gameplay (never thrashes every 2 s!)
// - Permanently blacklists any failing backend for the session (zero NPU<->FSR3 loops!)
// - Never enables any NPU mode on devices without verified Hexagon HTP NPU support
// - On Snapdragon 8 Elite / 8 Elite Gen 5: starts on FSR 4.1.1 Full (NPU Sync when capped <=45,
// NPU Async when uncapped) and steps smoothly down/up across Full NPU -> Lite NPU -> FSR 3.1 -> SGSR 2
// - On non-NPU devices: starts safely on FSR 3.1 GPU and scales across SGSR 2 <-> FSR 3.1 <-> Lite GPU <-> Full GPU
static void auto_select_backend(WlFsr4Pipeline *p, int fps_limit, int has_supported_npu,
int anti_throttle, float thermal_headroom, float batt_temp_c,
int *out_backend, int *out_npu_sync, int *out_thermal_cooling) {
int64_t now_ns = pipe_now_ns();
pthread_mutex_lock(&p->auto_mu);
// Fallback frame-time accumulation if vkQueuePresentKHR wasn't hooked on this process
if (!p->auto_seen_present && p->auto_last_disp_ns > 0) {
int64_t dt = now_ns - p->auto_last_disp_ns;
if (dt > 1000000LL && dt < 500000000LL) {
p->auto_win_ns += dt;
p->auto_win_frames++;
}
}
p->auto_last_disp_ns = now_ns;
// 1. One-time initialization based on hardware NPU capability and initial FPS cap
if (!p->auto_inited) {
p->auto_inited = 1;
p->auto_last_fps_limit = fps_limit;
auto_reset_window_locked(p);
if (has_supported_npu && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
// Capped 30/40 FPS -> Full NPU Sync (full FSR 4.1 quality, zero +1 frame lag, ~2.5ms GPU);
// Uncapped -> Full NPU Async (beats Full GPU by ~9 ms!).
p->auto_cur_npu_sync = (fps_limit > 0 && fps_limit <= 45) ? 1 : 0;
} else if (has_supported_npu && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
p->auto_cur_npu_sync = (fps_limit > 0 && fps_limit <= 45) ? 1 : 0;
} else {
// Non-NPU devices (8 Gen 3 / 8 Gen 2 / 8s / older): default safely to FSR 3.1 GPU
p->auto_cur_backend = WL_FSR_BACKEND_FSR3_GPU;
p->auto_cur_npu_sync = 0;
}
F4_LOGI("Auto mode init: npu_cap=%d fps_cap=%d -> %s (sync=%d)",
has_supported_npu, fps_limit, backend_display_name(p->auto_cur_backend), p->auto_cur_npu_sync);
}
// 2. Detect live user FPS-limit change in the sidebar
if (fps_limit != p->auto_last_fps_limit) {
p->auto_last_fps_limit = fps_limit;
p->auto_cap_changed_ns = now_ns;
auto_reset_window_locked(p);
// If already on an NPU backend, cleanly align Sync vs Async after cap toggle (no jarring backend swap)
if (auto_is_npu_backend(p->auto_cur_backend)) {
int want_sync = (fps_limit > 0 && fps_limit <= 45) ? 1 : 0;
if (want_sync != p->auto_cur_npu_sync) {
p->auto_cur_npu_sync = want_sync;
F4_LOGI("Auto mode cap=%d -> aligned %s Sync=%d",
fps_limit, backend_display_name(p->auto_cur_backend), want_sync);
}
}
}
// 3. Ensure current backend wasn't blacklisted
if (!auto_allowed(p, p->auto_cur_backend, has_supported_npu)) {
if (p->auto_cur_backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
p->auto_cur_npu_sync = (fps_limit > 0 && fps_limit <= 45) ? 1 : 0;
} else if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu) && has_supported_npu) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
p->auto_cur_npu_sync = 0;
} else {
p->auto_cur_backend = WL_FSR_BACKEND_FSR3_GPU;
p->auto_cur_npu_sync = 0;
}
auto_reset_window_locked(p);
}
// 3b. Proactive Anti-Throttling Mechanism (exists ONLY in Automatic mode AND when FPS is capped!)
int thermal_armed = (fps_limit > 0 && anti_throttle != 0);
if (!thermal_armed) {
p->auto_hot_since_ns = 0;
p->auto_thermal_cooling = 0;
} else {
int has_headroom = (thermal_headroom >= 0.0f);
int is_hot = has_headroom
? (thermal_headroom >= 0.88f || batt_temp_c >= 43.0f)
: (batt_temp_c >= 42.0f);
int is_severe = has_headroom
? (thermal_headroom >= 0.96f || batt_temp_c >= 43.8f)
: (batt_temp_c >= 43.8f);
int is_cool = has_headroom
? (thermal_headroom <= 0.75f && (batt_temp_c <= 0.0f || batt_temp_c <= 40.5f))
: (batt_temp_c > 0.0f && batt_temp_c <= 39.5f);
if (is_cool && p->auto_thermal_cooling) {
p->auto_thermal_cooling = 0;
p->auto_hot_since_ns = 0;
F4_LOGI("Auto anti-throttling cleared (cooled: headroom=%.2f batt=%.1fC) — upgrades re-enabled",
thermal_headroom, batt_temp_c);
}
if (is_hot) {
if (p->auto_hot_since_ns == 0) {
p->auto_hot_since_ns = now_ns;
} else if ((now_ns - p->auto_hot_since_ns) >= AUTO_EVAL_WINDOW_NS) {
int cur = p->auto_cur_backend;
int next = cur;
if (cur == WL_FSR_BACKEND_FSR4_1_FULL_NPU || cur == WL_FSR_BACKEND_FSR4_1_FULL_GPU ||
cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU || cur == WL_FSR_BACKEND_FSR4_1_LITE_GPU) {
// Proactively drop to FSR 3.1 GPU ~1.5-2C before OEM 44C clock cliff (0 W NPU + lower GPU)
next = WL_FSR_BACKEND_FSR3_GPU;
} else if (cur == WL_FSR_BACKEND_FSR3_GPU && is_severe &&
auto_allowed(p, WL_FSR_BACKEND_SGSR2_GPU, has_supported_npu)) {
// Still climbing near severe threshold -> drop to lowest-power SGSR 2
next = WL_FSR_BACKEND_SGSR2_GPU;
}
if (next != cur) {
F4_LOGI("Auto anti-throttling proactive cool (30s hot: headroom=%.2f batt=%.1fC cap=%d): %s -> %s",
thermal_headroom, batt_temp_c, fps_limit,
backend_display_name(cur), backend_display_name(next));
p->auto_cur_backend = next;
p->auto_cur_npu_sync = 0;
p->auto_thermal_cooling = 1;
p->auto_last_downgrade_ns = now_ns;
p->auto_hot_since_ns = now_ns;
auto_reset_window_locked(p);
}
}
} else {
p->auto_hot_since_ns = 0;
}
}
// 4. Evaluate ONLY after a full 30-second active gameplay average (>= 180 frames)
if (p->auto_win_ns >= AUTO_EVAL_WINDOW_NS && p->auto_win_frames >= AUTO_MIN_FRAMES) {
float avg_fps = (float)((double)p->auto_win_frames / ((double)p->auto_win_ns * 1e-9));
float avg_gpu_ms = p->auto_win_disp_n > 0
? (float)((double)p->auto_win_gpu_us / (double)p->auto_win_disp_n * 1e-3)
: 0.0f;
float avg_htp_ms = p->auto_win_disp_n > 0
? (float)((double)p->auto_win_htp_us / (double)p->auto_win_disp_n * 1e-3)
: 0.0f;
int can_upgrade = !p->auto_thermal_cooling &&
((p->auto_last_downgrade_ns == 0) ||
((now_ns - p->auto_last_downgrade_ns) >= AUTO_UPGRADE_COOLDOWN_NS));
int cur = p->auto_cur_backend;
int cur_sync = p->auto_cur_npu_sync;
int next = cur;
int next_sync = cur_sync;
int is_downgrade = 0;
if (fps_limit > 0) {
// ---- CAPPED FPS POLICY (e.g. 30 / 40 / 60 FPS) ----
float cap = (float)fps_limit;
if (avg_fps < cap - 3.0f) {
// Missing the user's FPS cap over 30 s -> step down smoothly
if (cur == WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 0;
} else if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
next_sync = 0;
} else {
next = WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
}
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_FULL_NPU && cur_sync == 1) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 0; // Drop Sync stall first before stepping down from Full 4.1
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_FULL_NPU) {
if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 0;
} else {
next = WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
}
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_GPU) {
if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 0;
} else {
next = WL_FSR_BACKEND_FSR3_GPU;
}
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU && cur_sync == 1) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 0;
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU) {
next = WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
is_downgrade = 1;
} else if (cur == WL_FSR_BACKEND_FSR3_GPU && avg_fps < cap - 4.0f &&
auto_allowed(p, WL_FSR_BACKEND_SGSR2_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_SGSR2_GPU;
is_downgrade = 1;
}
} else if (can_upgrade && avg_fps >= cap - 0.8f) {
// Holding the FPS cap solidly for 30 s -> climb quality & drop Async latency
float budget_ms = 1000.0f / cap;
if (cur == WL_FSR_BACKEND_SGSR2_GPU && avg_gpu_ms < 2.2f) {
next = WL_FSR_BACKEND_FSR3_GPU;
} else if (cur == WL_FSR_BACKEND_FSR3_GPU) {
if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu) && cap <= 45.0f) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 1;
} else if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu) && cap <= 45.0f) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 1;
} else if (!has_supported_npu && cap <= 45.0f && avg_gpu_ms < 2.0f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
}
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_GPU && !has_supported_npu && cap <= 35.0f &&
avg_gpu_ms < 3.5f && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_GPU;
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU && cur_sync == 0 && cap <= 45.0f &&
(avg_gpu_ms + avg_htp_ms) < budget_ms * 0.40f) {
next_sync = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU && cur_sync == 1 && cap <= 45.0f &&
(avg_gpu_ms + avg_htp_ms) < budget_ms * 0.35f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 1;
} else if (cur == WL_FSR_BACKEND_FSR4_1_FULL_NPU && cur_sync == 0 && cap <= 45.0f &&
(avg_gpu_ms + avg_htp_ms) < budget_ms * 0.45f) {
next_sync = 1;
}
}
} else {
// ---- UNCAPPED FPS POLICY (power/wattage + max FPS + wide hysteresis) ----
if (cur == WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
if (avg_fps < 45.0f && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 0;
is_downgrade = 1;
} else if (avg_fps < 35.0f) {
next = auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)
? WL_FSR_BACKEND_FSR4_1_LITE_GPU : WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
is_downgrade = 1;
}
} else if (cur == WL_FSR_BACKEND_FSR4_1_FULL_NPU) {
if (cur_sync == 1 && avg_fps < 48.0f) {
next_sync = 0; // Uncapped GPU-bound: stop stalling GPU on Sync
is_downgrade = 1;
} else if (avg_fps < 30.0f) {
next = auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)
? WL_FSR_BACKEND_FSR4_1_LITE_NPU : WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
is_downgrade = 1;
} else if (can_upgrade && cur_sync == 0 && avg_fps >= 60.0f) {
// Plenty of headroom at 60+ FPS -> eliminate +1 frame lag while keeping Full 4.1 quality!
next_sync = 1;
}
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_GPU) {
if (avg_fps < 42.0f && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 0;
is_downgrade = 1;
} else if (avg_fps < 32.0f) {
next = WL_FSR_BACKEND_FSR3_GPU;
is_downgrade = 1;
} else if (can_upgrade && !has_supported_npu && avg_fps >= 62.0f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_GPU;
}
} else if (cur == WL_FSR_BACKEND_FSR4_1_LITE_NPU) {
if (cur_sync == 1 && avg_fps < 45.0f) {
next_sync = 0;
is_downgrade = 1;
} else if (avg_fps < 27.0f) {
next = WL_FSR_BACKEND_FSR3_GPU;
next_sync = 0;
is_downgrade = 1;
} else if (can_upgrade && avg_fps >= 52.0f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 0;
} else if (can_upgrade && avg_fps >= 56.0f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
next_sync = 0;
}
} else if (cur == WL_FSR_BACKEND_FSR3_GPU) {
if (avg_fps < 24.0f && auto_allowed(p, WL_FSR_BACKEND_SGSR2_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_SGSR2_GPU;
is_downgrade = 1;
} else if (can_upgrade && avg_fps >= 44.0f) {
if (avg_fps >= 54.0f && auto_allowed(p, WL_FSR_BACKEND_FSR4_1_FULL_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_FULL_NPU;
next_sync = 0;
} else if (auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
next_sync = 0;
} else if (avg_fps >= 50.0f &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)) {
next = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
}
}
} else if (cur == WL_FSR_BACKEND_SGSR2_GPU) {
if (can_upgrade && avg_fps >= 38.0f) {
next = WL_FSR_BACKEND_FSR3_GPU;
}
}
}
if (next != cur || next_sync != cur_sync) {
F4_LOGI("Auto mode 30s decision: avg=%.1f fps (cap=%d gpu=%.2fms npu=%.2fms) %s(s=%d) -> %s(s=%d)",
avg_fps, fps_limit, avg_gpu_ms, avg_htp_ms,
backend_display_name(cur), cur_sync, backend_display_name(next), next_sync);
p->auto_cur_backend = next;
p->auto_cur_npu_sync = next_sync;
if (is_downgrade) p->auto_last_downgrade_ns = now_ns;
}
auto_reset_window_locked(p);
}
*out_backend = p->auto_cur_backend;
*out_npu_sync = p->auto_cur_npu_sync;
if (out_thermal_cooling) *out_thermal_cooling = p->auto_thermal_cooling;
pthread_mutex_unlock(&p->auto_mu);
}
static void auto_note_result(WlFsr4Pipeline *p, int attempted_backend, int has_supported_npu,
const WlFsrDispatchResult *res) {
pthread_mutex_lock(&p->auto_mu);
p->auto_win_gpu_us += res->gpu_us;
p->auto_win_htp_us += res->htp_us;
p->auto_win_disp_n++;
// Detect hard backend failure (NEVER blacklist during temporary 3s NPU compile warmup!)
int hard_fail = 0;
if (attempted_backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU) {
if (!res->npu_used) {
const char *st = p->fsr411_full ? wl_fsr411_full_gpu_npu_state(p->fsr411_full) : "failed";
int is_compiling = (st && (!strcmp(st, "compiling") || !strcmp(st, "standby")));
if (!is_compiling || res->rc < 2 || !p->fsr411_full || !wl_fsr411_full_gpu_is_ready(p->fsr411_full)) {
hard_fail = 1;
}
}
} else if (attempted_backend == WL_FSR_BACKEND_FSR4_1_LITE_NPU) {
if (!res->npu_used) {
const char *st = p->fsr412_lite ? wl_fsr412_lite_npu_state(p->fsr412_lite) : "failed";
int is_compiling = (st && (!strcmp(st, "compiling") || !strcmp(st, "standby")));
if (!is_compiling || res->rc < 2 || !p->fsr412_lite || !wl_fsr412_lite_is_gpu_ready(p->fsr412_lite)) {
hard_fail = 1;
}
}
} else if (attempted_backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU ||
attempted_backend == WL_FSR_BACKEND_FSR4_1_LITE_GPU ||
attempted_backend == WL_FSR_BACKEND_SGSR2_GPU) {
if (res->rc < 2 || res->fallback_used) {
hard_fail = 1;
}
}
if (hard_fail && attempted_backend != WL_FSR_BACKEND_FSR3_GPU) {
if ((p->auto_failed_mask & (1u << (uint32_t)attempted_backend)) == 0u) {
p->auto_failed_mask |= (1u << (uint32_t)attempted_backend);
F4_LOGW("Auto mode permanently blacklisted %s for this session (reason=%s, mask=0x%x) — zero retry loops!",
backend_display_name(attempted_backend),
res->reason ? res->reason : "failed", p->auto_failed_mask);
if (attempted_backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_NPU, has_supported_npu)) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
} else if ((attempted_backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU ||
attempted_backend == WL_FSR_BACKEND_FSR4_1_LITE_NPU) &&
auto_allowed(p, WL_FSR_BACKEND_FSR4_1_LITE_GPU, has_supported_npu)) {
p->auto_cur_backend = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
p->auto_cur_npu_sync = 0;
} else {
p->auto_cur_backend = WL_FSR_BACKEND_FSR3_GPU;
p->auto_cur_npu_sync = 0;
}
auto_reset_window_locked(p);
}
}
pthread_mutex_unlock(&p->auto_mu);
}
static int run_fsr3_gpu_safe(WlFsr4Pipeline *p, VkCommandBuffer cb,
const WlFsr4FrameDesc *frame,
VkImage src_img, VkFormat src_fmt, VkImageLayout src_lay,
uint32_t rw, uint32_t rh, uint32_t cw, uint32_t ch,
VkImage dep_img, VkFormat dep_fmt,
VkImage mv_img, VkFormat mv_fmt,
VkImage dst_img, VkImageLayout dst_lay, uint32_t ow, uint32_t oh,
float sharpness,
int is_fallback, const char *fallback_tag, const char *fallback_reason,
WlFsrDispatchResult *res) {
if (!p->fsr3_gpu_tried) {
p->fsr3_gpu_tried = 1;
p->fsr3_gpu = wl_fsr3_gpu_create(&p->vk, resolve_fsr4_dir());
}
if (p->fsr3_gpu) {
const char *f3_reason = NULL;
uint32_t gpu_us = 0u;
uint64_t dispatches = 0u;
int f3_rc = wl_fsr3_gpu_dispatch(p->fsr3_gpu, cb, frame,
src_img, src_fmt, cw, ch,
dep_img, dep_fmt,
mv_img, mv_fmt,
dst_img, ow, oh,
sharpness,
&gpu_us, &dispatches, &f3_reason);
if (f3_rc == 2) {
res->rc = 2;
res->npu_used = 0;
res->fallback_used = is_fallback;
res->htp_us = 0u;
res->gpu_us = gpu_us;
res->dispatches = dispatches;
res->backend_tag = fallback_tag ? fallback_tag : (is_fallback ? "FSR3.1-GPU (fallback)" : "FSR3.1-GPU");
res->reason = fallback_reason ? fallback_reason : (f3_reason ? f3_reason : "ok_fsr3_gpu");
return 2;
}
res->reason = f3_reason ? f3_reason : "fsr3_gpu_fallback";
} else {
res->reason = "missing_fsr3_shaders";
}
record_blit_fallback(p, cb, src_img, src_lay, rw, rh, dst_img, dst_lay, ow, oh);
res->rc = 1;
res->npu_used = 0;
res->fallback_used = 1;
res->backend_tag = "Blit (fallback)";
return 1;
}
static int run_fsr411_full(WlFsr4Pipeline *p, VkCommandBuffer cb,
const WlFsr4FrameDesc *frame,
VkImage src_img, VkFormat src_fmt,
VkImage dep_img, VkFormat dep_fmt,
VkImage mv_img, VkFormat mv_fmt,
VkImage exp_img, VkFormat exp_fmt,
VkImage dst_img, VkFormat dst_fmt, VkImageUsageFlags dst_usage,
uint32_t ow, uint32_t oh,
int use_npu, int npu_sync, float sharpness,
WlFsrDispatchResult *res) {
if (!p->fsr411_full_tried) {
p->fsr411_full_tried = 1;
p->fsr411_full = wl_fsr411_full_gpu_create(&p->vk, resolve_fsr4_dir(), resolve_power_mode());
}
if (!p->fsr411_full) {
res->reason = "missing_fsr411_full";
return 0;
}
int used_npu = 0;
uint32_t gpu_us = 0u, htp_us = 0u;
uint64_t dispatches = 0u;
const char *reason = NULL;
int rc = wl_fsr411_full_gpu_dispatch(p->fsr411_full, cb, frame,
src_img, src_fmt,
dep_img, dep_fmt,
mv_img, mv_fmt,
exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
use_npu, npu_sync, sharpness,
&used_npu, &gpu_us, &htp_us, &dispatches, &reason);
if (rc == 2) {
res->rc = 2;
res->npu_used = used_npu;
res->fallback_used = (use_npu && !used_npu);
res->gpu_us = gpu_us;
res->htp_us = htp_us;
res->dispatches = dispatches;
if (used_npu) {
res->backend_tag = "FSR4.1-Full-NPU";
} else if (use_npu) {
res->backend_tag = "FSR4.1-Full-GPU (NPU warmup)";
} else {
res->backend_tag = "FSR4.1-Full-GPU";
}
res->reason = reason ? reason : (used_npu ? "ok_fsr411_full_npu" : "ok_fsr411_full_gpu");
return 2;
}
res->reason = reason ? reason : "fsr411_full_unavailable";
return 0;
}
// FSR 4.1.1 Full writes a black frame on Turnip when the game itself uses VK_EXT_descriptor_buffer in the command
// buffers the upscaler records into (vkd3d-proton: XeSS DX12 sample black in 9/9 shots, picture back once vkd3d stops
// using the extension; Red Magic 12 Pro A850, Dec 2026). The layer hides the extension from games, so this only
// triggers when a game enables it anyway: FSR 4.1.2 Lite stands in. DROIDDECK_UPSCALE_FULL_ON_TURNIP=1 runs Full anyway.
static int full_black_here(WlFsr4Pipeline *p, const WlFsr4FrameDesc *frame) {
(void)frame;
if (!p->vk.app_desc_buf) return 0;
if (p->turnip < 0) {
VkPhysicalDeviceDriverProperties drv = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES };
VkPhysicalDeviceProperties2 dp2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &drv };
p->turnip = 0;
if (p->vk.GetPhysicalDeviceProperties2) {
p->vk.GetPhysicalDeviceProperties2(p->vk.phys_dev, &dp2);
p->turnip = drv.driverID == VK_DRIVER_ID_MESA_TURNIP;
}
}
if (!p->turnip) return 0;
const char *force = dd_upscale_env("FULL_ON_TURNIP");
if (force && force[0] == '1') return 0;
if (!p->full_dx12_logged) {
p->full_dx12_logged = 1;
F4_LOGW("FSR 4.1.1 Full off: the game uses descriptor buffers on Turnip (black frame) - FSR 4.1.2 Lite stands in");
}
return 1;
}
static int run_fsr412_lite(WlFsr4Pipeline *p, VkCommandBuffer cb,
const WlFsr4FrameDesc *frame,
VkImage src_img, VkFormat src_fmt, uint32_t cw, uint32_t ch,
VkImage dep_img, VkFormat dep_fmt,
VkImage mv_img, VkFormat mv_fmt,
VkImage exp_img, VkFormat exp_fmt,
VkImage dst_img, VkFormat dst_fmt, VkImageUsageFlags dst_usage,
uint32_t ow, uint32_t oh,
int use_npu, int npu_sync, float sharpness,
WlFsrDispatchResult *res) {
if (!p->fsr412_lite_tried) {
p->fsr412_lite_tried = 1;
p->fsr412_lite = wl_fsr412_lite_create(&p->vk, resolve_fsr4_dir(), resolve_power_mode());
}
if (!p->fsr412_lite) {
res->reason = "missing_fsr412_lite";
return 0;
}
int used_npu = 0;
uint32_t gpu_us = 0u, htp_us = 0u;
uint64_t dispatches = 0u;
const char *reason = NULL;
int rc = wl_fsr412_lite_dispatch(p->fsr412_lite, cb, frame,
src_img, src_fmt, cw, ch,
dep_img, dep_fmt,
mv_img, mv_fmt,
exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
use_npu, npu_sync, sharpness,
&used_npu, &gpu_us, &htp_us, &dispatches, &reason);
if (rc == 2) {
res->rc = 2;
res->npu_used = used_npu;
res->fallback_used = (use_npu && !used_npu);
res->gpu_us = gpu_us;
res->htp_us = htp_us;
res->dispatches = dispatches;
if (used_npu) {
res->backend_tag = "FSR4.1-Lite-NPU";
} else if (use_npu) {
res->backend_tag = "FSR4.1-Lite-GPU (NPU warmup)";
} else {
res->backend_tag = "FSR4.1-Lite-GPU";
}
res->reason = reason ? reason : (used_npu ? "ok_fsr412_lite_npu" : "ok_fsr412_lite_gpu");
return 2;
}
res->reason = reason ? reason : "fsr412_lite_unavailable";
return 0;
}
int wl_fsr4_pipe_dispatch(WlFsr4Pipeline *p, VkCommandBuffer cb,
const WlFsr4FrameDesc *frame,
const WlFsrDispatchConfig *cfg,
WlFsrDispatchResult *out_res) {
WlFsrDispatchResult local_res;
memset(&local_res, 0, sizeof local_res);
local_res.backend_tag = "Off";
WlFsrDispatchResult *res = out_res ? out_res : &local_res;
memset(res, 0, sizeof *res);
if (!p || !frame || !cb) return 0;
VkImage src_img = (VkImage)(uintptr_t)frame->color.vk_image;
VkImage dep_img = (VkImage)(uintptr_t)frame->depth.vk_image;
VkImage mv_img = (VkImage)(uintptr_t)frame->motion.vk_image;
VkImage exp_img = (VkImage)(uintptr_t)frame->exposure.vk_image;
VkImage dst_img = (VkImage)(uintptr_t)frame->output.vk_image;
if (!src_img || !dst_img) return 0;
uint32_t rw = frame->render_w, rh = frame->render_h;
uint32_t ow = frame->output_w, oh = frame->output_h;
VkFormat src_fmt = (VkFormat)frame->color.vk_format;
VkFormat dep_fmt = (VkFormat)frame->depth.vk_format;
VkFormat mv_fmt = (VkFormat)frame->motion.vk_format;
VkFormat exp_fmt = (VkFormat)frame->exposure.vk_format;
VkFormat dst_fmt = (VkFormat)frame->output.vk_format;
VkImageUsageFlags dst_usage = 0u;
if (!rw || !rh || src_fmt == VK_FORMAT_UNDEFINED) {
uint32_t w = 0, h = 0; VkFormat f = VK_FORMAT_UNDEFINED;
if (wl_fsr4_lookup_image_info(p->vk.device, src_img, &f, &w, &h, NULL)) {
if (!rw) rw = w;
if (!rh) rh = h;
if (src_fmt == VK_FORMAT_UNDEFINED) src_fmt = f;
}
}
{
uint32_t w = 0, h = 0; VkFormat f = VK_FORMAT_UNDEFINED; VkImageUsageFlags u = 0u;
if (wl_fsr4_lookup_image_info(p->vk.device, dst_img, &f, &w, &h, &u)) {
if (!ow) ow = w;
if (!oh) oh = h;
if (dst_fmt == VK_FORMAT_UNDEFINED) dst_fmt = f;
dst_usage = u;
}
}
if (dep_img && dep_fmt == VK_FORMAT_UNDEFINED) {
wl_fsr4_lookup_image_info(p->vk.device, dep_img, &dep_fmt, NULL, NULL, NULL);
}
if (!dep_img && rw && rh) {
dep_img = blank_depth_get(p, cb, rw, rh, (frame->flags & WLFSR4_FLAG_DEPTH_INVERTED) != 0);
if (dep_img) dep_fmt = VK_FORMAT_D32_SFLOAT;
}
if (mv_img && mv_fmt == VK_FORMAT_UNDEFINED) {
wl_fsr4_lookup_image_info(p->vk.device, mv_img, &mv_fmt, NULL, NULL, NULL);
}
if (exp_img && exp_fmt == VK_FORMAT_UNDEFINED) {
wl_fsr4_lookup_image_info(p->vk.device, exp_img, &exp_fmt, NULL, NULL, NULL);
}
if (!rw || !rh || !ow || !oh) return 0;
VkImageLayout src_lay = frame->color.vk_layout ? (VkImageLayout)frame->color.vk_layout : VK_IMAGE_LAYOUT_GENERAL;
VkImageLayout dst_lay = frame->output.vk_layout ? (VkImageLayout)frame->output.vk_layout : VK_IMAGE_LAYOUT_GENERAL;
int force_bypass = cfg ? cfg->force_bypass : 0;
int requested_backend = cfg ? cfg->backend : WL_FSR_BACKEND_AUTO;
int npu_sync = cfg ? cfg->npu_sync : 0;
int fps_limit = cfg ? cfg->fps_limit : 0;
int has_supported_npu = cfg ? cfg->has_supported_npu : 0;
int anti_throttle = cfg ? cfg->anti_throttle : 0;
float thermal_headroom = cfg ? cfg->thermal_headroom : -1.0f;
float batt_temp_c = cfg ? cfg->batt_temp_c : 0.0f;
float sharpness = (cfg && cfg->sharpness >= 0.0f) ? cfg->sharpness
: ((frame->flags & WLFSR4_FLAG_SHARPEN) ? frame->sharpness : 0.55f);
if (sharpness < 0.0f) sharpness = 0.0f;
if (sharpness > 1.0f) sharpness = 1.0f;
// Live OFF switch from Winlator's in-game left sidebar
if (force_bypass) {
record_blit_fallback(p, cb, src_img, src_lay, rw, rh, dst_img, dst_lay, ow, oh);
res->rc = 1;
res->backend_tag = "Off";
res->reason = "bypass_off";
return 1;
}
// Automatic 30s switching mode removed to avoid conflicts with Root Anti-Throttling & manual choices:
// map any legacy WL_FSR_BACKEND_AUTO directly to the device's optimal static backend (Full NPU Async or FSR 3.1).
(void)fps_limit;
(void)anti_throttle;
(void)thermal_headroom;
(void)batt_temp_c;
int is_auto = 0;
int thermal_cooling = 0;
int backend = (requested_backend == WL_FSR_BACKEND_AUTO)
? (has_supported_npu ? WL_FSR_BACKEND_FSR4_1_FULL_NPU : WL_FSR_BACKEND_FSR3_GPU)
: requested_backend;
{
#if !WL_ENABLE_DEPRECATED_FSR411_FULL
if (backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU) {
backend = WL_FSR_BACKEND_FSR4_1_LITE_NPU;
} else if (backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
backend = WL_FSR_BACKEND_FSR4_1_LITE_GPU;
}
#endif
if (backend != WL_FSR_BACKEND_FSR3_GPU &&
backend != WL_FSR_BACKEND_SGSR2_GPU &&
backend != WL_FSR_BACKEND_FSR4_1_LITE_NPU &&
backend != WL_FSR_BACKEND_FSR4_1_LITE_GPU &&
backend != WL_FSR_BACKEND_FSR4_1_FULL_NPU &&
backend != WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
// Normalize any legacy backend ID cleanly
backend = (backend == WL_FSR_BACKEND_FSR4_0_NPU)
? WL_FSR_BACKEND_FSR4_1_LITE_NPU
: WL_FSR_BACKEND_FSR3_GPU;
}
}
// Handle live backend switching with clean history reset (no ghost frames on mode change)
if (p->last_engine >= 0 && p->last_engine != backend) {
F4_LOGI("Live backend switch: %s -> %s%s",
backend_display_name(p->last_engine), backend_display_name(backend),
is_auto ? " [Auto]" : "");
wl_fsr3_gpu_reset(p->fsr3_gpu);
wl_sgsr2_gpu_reset(p->sgsr2_gpu);
wl_fsr412_lite_reset(p->fsr412_lite);
wl_fsr411_full_gpu_reset(p->fsr411_full);
}
p->last_engine = backend;
// Resolve color backing texture dimensions once for all backends
uint32_t cw = frame->color.width ? frame->color.width : rw;
uint32_t ch = frame->color.height ? frame->color.height : rh;
if (cw < rw || ch < rh) {
uint32_t img_w = 0, img_h = 0;
if (wl_fsr4_lookup_image_info(p->vk.device, src_img, NULL, &img_w, &img_h, NULL)) {
if (img_w >= rw) cw = img_w;
if (img_h >= rh) ch = img_h;
}
}
int rc = 0;
if (backend == WL_FSR_BACKEND_FSR3_GPU) {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 0, "FSR3.1-GPU", NULL, res);
} else if (backend == WL_FSR_BACKEND_SGSR2_GPU) {
if (!p->sgsr2_gpu_tried) {
p->sgsr2_gpu_tried = 1;
p->sgsr2_gpu = wl_sgsr2_gpu_create(&p->vk, resolve_fsr4_dir());
}
if (p->sgsr2_gpu && src_fmt != VK_FORMAT_UNDEFINED) {
uint32_t gpu_us = 0u;
uint64_t disps = 0u;
const char *s2_reason = NULL;
if (wl_sgsr2_gpu_dispatch(p->sgsr2_gpu, cb, frame,
src_img, src_fmt, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt,
dst_img, ow, oh, sharpness,
&gpu_us, &disps, &s2_reason) == 2) {
res->rc = 2;
res->npu_used = 0;
res->fallback_used = 0;
res->gpu_us = gpu_us;
res->htp_us = 0u;
res->dispatches = disps;
res->backend_tag = "SGSR2-Adreno";
res->reason = s2_reason ? s2_reason : "ok_sgsr2";
rc = 2;
}
}
if (rc != 2) {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 1, "FSR3.1-GPU (SGSR2 fallback)", "sgsr2_unavailable", res);
}
} else if (backend == WL_FSR_BACKEND_FSR4_1_FULL_NPU) {
if (!full_black_here(p, frame) && run_fsr411_full(p, cb, frame, src_img, src_fmt,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
1 /* NPU */, npu_sync, sharpness, res) == 2) {
rc = 2;
} else if (run_fsr412_lite(p, cb, frame, src_img, src_fmt, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
1 /* NPU */, npu_sync, sharpness, res) == 2) {
res->fallback_used = 1;
rc = 2;
} else {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 1, "FSR3.1-GPU (fallback)", res->reason, res);
}
} else if (backend == WL_FSR_BACKEND_FSR4_1_FULL_GPU) {
if (!full_black_here(p, frame) && run_fsr411_full(p, cb, frame, src_img, src_fmt,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
0 /* GPU */, 0, sharpness, res) == 2) {
rc = 2;
} else if (run_fsr412_lite(p, cb, frame, src_img, src_fmt, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
0 /* GPU */, 0, sharpness, res) == 2) {
res->fallback_used = 1;
rc = 2;
} else {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 1, "FSR3.1-GPU (fallback)", res->reason, res);
}
} else if (backend == WL_FSR_BACKEND_FSR4_1_LITE_NPU) {
if (run_fsr412_lite(p, cb, frame, src_img, src_fmt, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
1 /* NPU */, npu_sync, sharpness, res) == 2) {
rc = 2;
} else {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 1, "FSR3.1-GPU (fallback)", res->reason, res);
}
} else {
// WL_FSR_BACKEND_FSR4_1_LITE_GPU
if (run_fsr412_lite(p, cb, frame, src_img, src_fmt, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, exp_img, exp_fmt,
dst_img, dst_fmt, dst_usage, ow, oh,
0 /* GPU */, 0, sharpness, res) == 2) {
rc = 2;
} else {
rc = run_fsr3_gpu_safe(p, cb, frame, src_img, src_fmt, src_lay, rw, rh, cw, ch,
dep_img, dep_fmt, mv_img, mv_fmt, dst_img, dst_lay, ow, oh,
sharpness, 1, "FSR3.1-GPU (fallback)", res->reason, res);
}
}
if (is_auto) {
auto_note_result(p, backend, has_supported_npu, res);
if (res->backend_tag && strncmp(res->backend_tag, "Auto", 4) != 0) {
snprintf(p->auto_tag_buf, sizeof p->auto_tag_buf,
thermal_cooling ? "Auto (Cool): %s" : "Auto: %s",
res->backend_tag);
res->backend_tag = p->auto_tag_buf;
}
}
return rc;
}