Frame generation in the compositor: Win-FG Native and LSFG Native

The compositor was built with its frame-generation leg stubbed out. Bannerlator's
two engines and the DXBC translator they need now ride along, so extra frames
are generated on gamescope's final output — any game, nothing changed in the
runtime. Win-FG needs nothing; LSFG takes its shader chain from the user's own
Lossless Scaling, which in this app is installed from the Steam client and read
straight out of the runtime's library. One picker on the main screen: Off, or an
engine at 2x/3x/4x, applied live on the next return to the session.
This commit is contained in:
The412Banner committed 2026-09-19 15:14:45 -04:00
1 parent d46bd4a0c3
commit 0425023063
114 files changed
+36459

No files matched your search

@@ -14,6 +14,7 @@ import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
import com.steamdeck.launcher.core.FileUtils
import com.steamdeck.launcher.gpu.FrameGen
import com.steamdeck.launcher.runtime.LinuxRuntime
import com.steamdeck.launcher.runtime.LinuxRuntimeInstaller
@@ -28,6 +29,7 @@ class MainActivity : Activity() {
private lateinit var progress: ProgressBar
private lateinit var playButton: Button
private lateinit var runtimeButton: Button
private lateinit var frameGenButton: Button
private val ui = Handler(Looper.getMainLooper())
@Volatile private var busy = false
@@ -41,6 +43,8 @@ class MainActivity : Activity() {
progress = findViewById(R.id.progress)
playButton = findViewById(R.id.play)
runtimeButton = findViewById(R.id.runtime)
frameGenButton = findViewById(R.id.frame_gen)
frameGenButton.setOnClickListener { pickFrameGen() }
playButton.setOnClickListener { startActivity(Intent(this, SessionActivity::class.java)) }
runtimeButton.setOnClickListener { onRuntimeButton() }
@@ -119,7 +123,36 @@ class MainActivity : Activity() {
ui.post { render() }
}
/** Off, or an engine at 2x/3x/4x. Saved, and pushed to a running session on the next resume. */
private fun pickFrameGen() {
val choices = arrayOf("Off",
"Win-FG 2×", "Win-FG 3×", "Win-FG 4×",
"LSFG 2× (needs Lossless Scaling)", "LSFG 3× (needs Lossless Scaling)", "LSFG 4× (needs Lossless Scaling)")
val engine = FrameGen.engine(this)
val current = when (engine) {
FrameGen.ENGINE_WINFG -> FrameGen.multiplier(this) - 1
FrameGen.ENGINE_LSFG -> FrameGen.multiplier(this) + 2
else -> 0
}
AlertDialog.Builder(this)
.setTitle(R.string.frame_gen_title)
.setSingleChoiceItems(choices, current) { dialog, which ->
when (which) {
0 -> FrameGen.set(this, FrameGen.ENGINE_OFF, 2)
in 1..3 -> FrameGen.set(this, FrameGen.ENGINE_WINFG, which + 1)
else -> FrameGen.set(this, FrameGen.ENGINE_LSFG, which - 2)
}
dialog.dismiss()
render()
}
.setNeutralButton(android.R.string.ok, null)
.setMessage(null)
.show()
Log.i(TAG, "frame generation picker: " + FrameGen.label(this))
}
private fun render() {
frameGenButton.text = getString(R.string.frame_gen, FrameGen.label(this))
val installed = LinuxRuntimeInstaller.installedVersion(this)
val ready = LinuxRuntime.isInstalled(this)
progress.visibility = if (busy) View.VISIBLE else View.GONE
@@ -16,6 +16,7 @@ import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import com.steamdeck.launcher.core.FileUtils
import com.steamdeck.launcher.gpu.FrameGen
import com.steamdeck.launcher.gpu.TurnipDriver
import com.steamdeck.launcher.input.EvdevKeys
import com.steamdeck.launcher.input.OnScreenControls
@@ -121,6 +122,7 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
CompositorHost.newSession()
SessionService.start(this)
}
applyFrameGen()
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
@@ -272,8 +274,23 @@ class SessionActivity : Activity(), SurfaceHolder.Callback {
Log.i(TAG, "on-screen controls " + (if (show) "shown" else "hidden"))
}
/**
* The saved frame-generation setting, pushed to the compositor. Off the main thread: LSFG's
* first use translates the shader chain out of Lossless.dll, which takes seconds.
*/
private fun applyFrameGen() {
val hz = refreshHz()
Thread({
val problem = FrameGen.apply(this, hz)
if (problem != null) runOnUiThread {
android.widget.Toast.makeText(this, problem, android.widget.Toast.LENGTH_LONG).show()
}
}, "frame-gen").start()
}
override fun onResume() {
super.onResume()
if (CompositorHost.isStarted) applyFrameGen()
(getSystemService(INPUT_SERVICE) as? InputManager)
?.registerInputDeviceListener(deviceListener, Handler(Looper.getMainLooper()))
updateOnScreenControls()
@@ -0,0 +1,86 @@
package com.steamdeck.launcher.gpu
import android.content.Context
import android.util.Log
import com.steamdeck.launcher.wayland.WaylandCompositor
/**
* The frame-generation setting and how it reaches the compositor.
*
* Both engines generate on gamescope's final output inside our compositor, so they work for any
* game with nothing changed in the runtime. Win-FG is our own and needs nothing; LSFG needs the
* shader chain from the user's copy of Lossless Scaling (see [LsfgNative]). Every compositor
* setter is a value store on the native side, so this can be applied before the compositor is
* up and changed live from the main screen while a session runs.
*/
object FrameGen {
const val ENGINE_OFF = "off"
const val ENGINE_WINFG = "winfg"
const val ENGINE_LSFG = "lsfg"
/** Win-FG's Performance preset, pinned: changing it live rebuilds the whole chain. */
private const val WINFG_PERF_PRESET = 2
/** Bidirectional flow with the occlusion gate — the chain's own default. */
private const val WINFG_MODEL = 4
/** Optical-flow resolution as a fraction of the output; Bannerlator's per-engine defaults. */
private const val FLOW_SCALE_WINFG = 0.60f
private const val FLOW_SCALE_LSFG = 0.80f
private const val PREFS = "frame_gen"
fun engine(context: Context): String =
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getString("engine", ENGINE_OFF) ?: ENGINE_OFF
fun multiplier(context: Context): Int =
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).getInt("multiplier", 2).coerceIn(2, 4)
fun set(context: Context, engine: String, multiplier: Int) {
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit()
.putString("engine", engine).putInt("multiplier", multiplier).apply()
}
/** "Off", "Win-FG 2×", "LSFG 3×" — for the main screen. */
fun label(context: Context): String = when (engine(context)) {
ENGINE_WINFG -> "Win-FG ${multiplier(context)}×"
ENGINE_LSFG -> "LSFG ${multiplier(context)}×"
else -> "Off"
}
/**
* Pushes the saved setting into the compositor. Returns a message for the user when the
* engine cannot run (LSFG without Lossless Scaling), or null.
*/
fun apply(context: Context, refreshHz: Float): String? {
val engine = engine(context)
val multiplier = multiplier(context)
var problem: String? = null
when (engine) {
ENGINE_LSFG -> {
val status = LsfgNative.ensureCache(context)
if (status != LsfgNative.STATUS_OK) {
problem = if (status == LsfgNative.STATUS_NOT_INSTALLED)
"LSFG needs Lossless Scaling installed from the Steam client"
else "LSFG: " + LsfgNative.statusName(status)
Log.w(TAG, problem)
WaylandCompositor.nativeSetFrameGenArmed(false, 0)
return problem
}
WaylandCompositor.nativeSetFrameGenEngine(WaylandCompositor.FG_ENGINE_LSFG)
WaylandCompositor.nativeSetLsfgCachePath(LsfgNative.cacheFile(context).absolutePath)
WaylandCompositor.nativeSetFrameGenTuning(FLOW_SCALE_LSFG, refreshHz)
WaylandCompositor.nativeSetFrameGenArmed(true, multiplier)
}
ENGINE_WINFG -> {
WaylandCompositor.nativeSetFrameGenEngine(WaylandCompositor.FG_ENGINE_WINFG)
WaylandCompositor.nativeSetWinFgTuning(WINFG_MODEL, WINFG_PERF_PRESET)
WaylandCompositor.nativeSetFrameGenTuning(FLOW_SCALE_WINFG, refreshHz)
WaylandCompositor.nativeSetFrameGenArmed(true, multiplier)
}
else -> WaylandCompositor.nativeSetFrameGenArmed(false, 0)
}
Log.i(TAG, "frame generation: $engine x$multiplier at ${refreshHz}Hz")
return problem
}
private const val TAG = "FrameGen"
}
@@ -0,0 +1,99 @@
package com.steamdeck.launcher.gpu;
import android.content.Context;
import android.util.Log;
import com.steamdeck.launcher.runtime.LinuxRuntime;
import java.io.File;
/**
* LSFG frame generation's shader chain: extracted from the user's own copy of Lossless Scaling
* and cached on device.
*
* <p>The 25 compute shaders are not redistributable. They live as resources inside the user's
* paid {@code Lossless.dll} (Steam app 993090), which in this app the user installs from the
* Steam client itself — so the DLL sits in the runtime's own Steam library. It is mapped
* read-only and parsed as data, never loaded or executed. Extraction is slow enough to be worth
* doing once (the DXBC chain is translated to SPIR-V), so the result is cached, keyed on the
* DLL's size and hash. Call {@link #ensureCache} off the main thread.
*
* <p>Ported from Bannerlator's LsfgNative; the native side is the same lsfg_jni.cpp compiled into
* the compositor library.
*/
public final class LsfgNative {
private static final String TAG = "LsfgNative";
static { System.loadLibrary("bannerwayland"); }
private LsfgNative() {}
// Mirrors lsfg::DllStatus.
public static final int STATUS_OK = 0;
public static final int STATUS_NOT_INSTALLED = 1;
public static final int STATUS_UNREADABLE_FILE = 2;
public static final int STATUS_NOT_PORTABLE_EXECUTABLE = 3;
public static final int STATUS_MISSING_SHADERS = 4;
public static final int STATUS_TRANSLATION_FAILED = 5;
public static final int STATUS_CACHE_UNUSABLE = 6;
private static native int nativeValidateDll(String dllPath);
private static native int nativeDllVariant(String dllPath, boolean preferFp16);
private static native int nativeBuildCache(String dllPath, String cachePath, boolean preferFp16);
private static native boolean nativeCacheMatchesSource(String cachePath, String dllPath);
private static native int nativeCacheVariant(String cachePath);
private static native String nativeStatusName(int status);
private static native String nativeVariantName(int variant);
/** Where Lossless Scaling lands when installed from the client inside the runtime. */
public static File losslessDll(Context context) {
return new File(LinuxRuntime.rootDir(context),
"root/.local/share/Steam/steamapps/common/Lossless Scaling/Lossless.dll");
}
/** Where the translated SPIR-V chain is cached. */
public static File cacheFile(Context context) {
return new File(context.getFilesDir(), "lsfg-native/shaders.cache");
}
public static boolean isInstalled(Context context) {
return losslessDll(context).isFile();
}
/**
* Makes sure a usable cache exists for the installed DLL, building it if the DLL is new or
* changed. Returns a {@code STATUS_*} code; {@link #STATUS_OK} means the engine can start.
*/
public static int ensureCache(Context context) {
File dll = losslessDll(context);
if (!dll.isFile()) return STATUS_NOT_INSTALLED;
File cache = cacheFile(context);
try {
if (cache.isFile() && nativeCacheMatchesSource(cache.getPath(), dll.getPath())) {
return STATUS_OK;
}
int status = nativeValidateDll(dll.getPath());
if (status != STATUS_OK) {
Log.w(TAG, "Lossless.dll rejected: " + statusName(status));
return status;
}
//noinspection ResultOfMethodCallIgnored
cache.getParentFile().mkdirs();
status = nativeBuildCache(dll.getPath(), cache.getPath(), true);
Log.i(TAG, "cache build: " + statusName(status)
+ (status == STATUS_OK ? " (" + nativeVariantName(nativeCacheVariant(cache.getPath())) + ")" : ""));
return status;
} catch (Throwable t) {
Log.e(TAG, "ensureCache", t);
return STATUS_CACHE_UNUSABLE;
}
}
public static String statusName(int status) {
try {
return nativeStatusName(status);
} catch (Throwable t) {
return "status " + status;
}
}
}