diff --git a/app/src/main/cpp/deviceinfo/vkinfo.c b/app/src/main/cpp/deviceinfo/vkinfo.c index 81ccc6b0dd..5846e055da 100644 --- a/app/src/main/cpp/deviceinfo/vkinfo.c +++ b/app/src/main/cpp/deviceinfo/vkinfo.c @@ -62,6 +62,7 @@ Java_com_droiddeck_launcher_gpu_VulkanInfo_nativeQuery(JNIEnv *env, jclass clazz LINE("driver_version=%u.%u.%u\n", p.driverVersion >> 22, (p.driverVersion >> 12) & 0x3ff, p.driverVersion & 0xfff); LINE("vendor_id=0x%04x\ndevice_id=0x%08x\n", p.vendorID, p.deviceID); LINE("max_image_2d=%u\n", p.limits.maxImageDimension2D); + LINE("max_push_constants=%u\n", p.limits.maxPushConstantsSize); uint32_t extCount = 0; if (vkEnumerateDeviceExtensionProperties) vkEnumerateDeviceExtensionProperties(device, NULL, &extCount, NULL); @@ -91,6 +92,7 @@ Java_com_droiddeck_launcher_gpu_VulkanInfo_nativeQuery(JNIEnv *env, jclass clazz vkGetPhysicalDeviceFeatures(device, &f); LINE("geometry_shader=%s\ntessellation_shader=%s\ntexture_bc=%s\n", f.geometryShader ? "yes" : "no", f.tessellationShader ? "yes" : "no", f.textureCompressionBC ? "yes" : "no"); + LINE("shader_int64=%s\n", f.shaderInt64 ? "yes" : "no"); } if (vkGetPhysicalDeviceMemoryProperties) { VkPhysicalDeviceMemoryProperties m; diff --git a/app/src/main/java/com/droiddeck/launcher/gpu/AppVulkan.kt b/app/src/main/java/com/droiddeck/launcher/gpu/AppVulkan.kt new file mode 100644 index 0000000000..437f232f86 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/gpu/AppVulkan.kt @@ -0,0 +1,68 @@ +package com.droiddeck.launcher.gpu + +import android.content.Context +import android.util.Log +import com.droiddeck.launcher.core.DeviceSupport +import com.droiddeck.launcher.core.FileUtils +import java.io.File + +data class AppVulkan( + val major: Int, + val minor: Int, + val shaderInt64: Boolean?, + val maxPushConstants: Int?, + val source: String, +) { + fun atLeast(major: Int, minor: Int): Boolean = this.major > major || (this.major == major && this.minor >= minor) + + val label: String + get() = "Vulkan $major.$minor, shaderInt64 ${shaderInt64?.let { if (it) "yes" else "no" } ?: "?"}, " + + "push constants ${maxPushConstants ?: "?"} ($source)" + + companion object { + private const val TAG = "AppVulkan" + const val BRIDGE_FILE = "apps-vulkan" + + fun detect(context: Context): AppVulkan? { + if (!DeviceSupport.vkBridge(context)) return null + val native = VulkanInfo.query() + val file = File(VkBridgeComponent.cacheDir(context), BRIDGE_FILE) + val fromBridge = FileUtils.readString(file)?.let { fromBridgeFile(it, native["device"]) } + val found = fromBridge ?: fromDriver(native) + Log.i(TAG, found?.label ?: "unknown (driver query: ${native["error"] ?: "no api line"})") + return found + } + + internal fun fromBridgeFile(text: String, driverDevice: String?): AppVulkan? { + val map = keyValues(text) + val device = map["device"] + if (device != null && driverDevice != null && device != driverDevice) return null + val (major, minor) = version(map["api"]) ?: return null + return AppVulkan(major, minor, bool(map["shader_int64"]), map["max_push_constants"]?.toIntOrNull(), "bridge") + } + + internal fun fromDriver(map: Map): AppVulkan? { + val (major, minor) = version(map["api"]) ?: return null + return AppVulkan(major, minor, bool(map["shader_int64"]), map["max_push_constants"]?.toIntOrNull(), "driver") + } + + private fun keyValues(text: String): Map = text.lineSequence().mapNotNull { line -> + val eq = line.indexOf('=') + if (eq <= 0) null else line.substring(0, eq).trim() to line.substring(eq + 1).trim() + }.toMap() + + private fun version(v: String?): Pair? { + val parts = v?.trim()?.split('.') ?: return null + if (parts.size < 2) return null + val major = parts[0].toIntOrNull() ?: return null + val minor = parts[1].toIntOrNull() ?: return null + return major to minor + } + + private fun bool(v: String?): Boolean? = when (v?.trim()?.lowercase()) { + "1", "yes", "true" -> true + "0", "no", "false" -> false + else -> null + } + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt b/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt index f10ab12448..ca5afd557c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/ComponentsManager.kt @@ -7,6 +7,7 @@ import android.util.Log import com.droiddeck.launcher.core.Downloader import com.droiddeck.launcher.core.FileUtils import com.droiddeck.launcher.core.GameEnvironment +import com.droiddeck.launcher.gpu.AppVulkan import com.droiddeck.launcher.gpu.GpuInfo import com.droiddeck.launcher.runtime.LinuxRuntime import com.github.luben.zstd.ZstdInputStream @@ -629,29 +630,31 @@ object ComponentsManager { } - fun swap(context: Context, protonId: String, file: String): String = synchronized(lock) { + fun swap(context: Context, protonId: String, file: String, auto: Boolean = false): String = synchronized(lock) { val p = proton(context, protonId) val wcp = File(packagesDir(context), safeName(file)) val info = packageInfo(context, wcp) val state = loadState(context) if (inUse(context, p)) { - state.sub("queued").sub(p.id).put(info.comp, JSONObject().put("kind", "package").put("file", wcp.name).put("label", info.version)) + state.sub("queued").sub(p.id).put(info.comp, JSONObject().put("kind", "package").put("file", wcp.name).put("label", info.version) + .apply { if (auto) put("auto", true) }) saveState(context, state) return context.getString(R.string.cmgr_swap_queued, LABEL[info.comp], info.version, p.name) } - applyPackage(context, p, info, wcp, state) + applyPackage(context, p, info, wcp, state, auto) state.sub("queued").optJSONObject(p.id)?.remove(info.comp) saveState(context, state) context.getString(R.string.cmgr_swap_done, LABEL[info.comp], info.version, p.name) } - private fun applyPackage(context: Context, p: Proton, info: Package, wcp: File, state: JSONObject) { + private fun applyPackage(context: Context, p: Proton, info: Package, wcp: File, state: JSONObject, auto: Boolean = false) { ensureOriginal(context, p, info.comp, state) val hashes = installFiles(context, p.dir, info.comp, wcp) state.sub("active").sub(p.id).put(info.comp, JSONObject() .put("file", wcp.name).put("label", info.version).put("protonVersion", p.version) .put("files", JSONObject(hashes.filterKeys { owned(info.comp, it) } as Map<*, *>)) - .put("at", System.currentTimeMillis() / 1000)) + .put("at", System.currentTimeMillis() / 1000) + .apply { if (auto) put("auto", true) }) Log.i(TAG, "swapped ${info.comp} ${info.version} into ${p.name} ${p.version}") } @@ -699,7 +702,7 @@ object ComponentsManager { applyOriginal(context, p, comp, v, File(originalsDir(context), "$pid/${safeName(v)}/$comp.wcp"), state) } else { val wcp = File(packagesDir(context), q.optString("file")) - applyPackage(context, p, packageInfo(context, wcp), wcp, state) + applyPackage(context, p, packageInfo(context, wcp), wcp, state, q.optBoolean("auto")) } done += "${LABEL[comp]} ${q.optString("label")} → ${p.name}" }.onFailure { Log.w(TAG, "queued $comp for ${p.name}", it) } @@ -1005,6 +1008,21 @@ object ComponentsManager { internal fun forcedPackageWanted(activeFile: String?, protonVersionMatches: Boolean): Boolean = activeFile.isNullOrEmpty() || !protonVersionMatches + internal fun bridgeDxvk(vk: AppVulkan?): ForcedPackage? = when { + vk == null -> null + !vk.atLeast(1, 1) -> null + !vk.atLeast(1, 3) -> ForcedPackage(emptySet(), "dxvk", DXVK_FOR_VULKAN_1_1, "Dxvk-Linux") + vk.shaderInt64 == false || (vk.maxPushConstants ?: 256) < 256 -> + ForcedPackage(emptySet(), "dxvk", DXVK_FOR_VULKAN_1_3_NO_INT64, "Dxvk-Linux") + else -> null + } + + internal const val DXVK_FOR_VULKAN_1_1 = "dxvk-1.10.3-linux.wcp" + internal const val DXVK_FOR_VULKAN_1_3_NO_INT64 = "dxvk-2.6.2-linux.wcp" + + internal fun autoChoiceStale(entry: JSONObject?, wantedFile: String?): Boolean = + entry != null && entry.optBoolean("auto") && entry.optString("file") != wantedFile + internal fun catalogMatch(items: List, file: String, release: String): CatalogItem? = items.firstOrNull { it.file == file && it.release == release } @@ -1013,24 +1031,52 @@ object ComponentsManager { { file, release -> catalogItem(context, file, release) }, { download(context, it) {} }, { id, file -> swap(context, id, file) }, + vulkan = AppVulkan.detect(context), + bridgeSession = com.droiddeck.launcher.core.DeviceSupport.vkBridge(context), + applyAuto = { id, file -> swap(context, id, file, auto = true) }, + undoAuto = { id, comp -> undoAutoChoice(context, id, comp) }, ) + internal fun undoAutoChoice(context: Context, protonId: String, comp: String): String = synchronized(lock) { + val p = proton(context, protonId) + val state = loadState(context) + val entry = state.sub("active").optJSONObject(p.id)?.optJSONObject(comp) + if (entry?.optString("protonVersion") == p.version && originalFile(context, p, comp).isFile) + return restore(context, protonId, comp, safeName(p.version)) + state.sub("active").optJSONObject(p.id)?.remove(comp) + saveState(context, state) + "${LABEL[comp]}: ${p.name} keeps its own copy" + } + internal fun ensureForcedPackages( context: Context, gpu: GpuInfo, lookup: (file: String, release: String) -> CatalogItem?, fetch: (CatalogItem) -> Unit, apply: (protonId: String, file: String) -> String, + vulkan: AppVulkan? = null, + bridgeSession: Boolean = false, + applyAuto: (protonId: String, file: String) -> String = apply, + undoAuto: (protonId: String, comp: String) -> String = { _, _ -> "" }, ): String? { - val rules = FORCED_PACKAGES.filter { it.matches(gpu) } - if (rules.isEmpty()) return null + val bridgeRule = if (bridgeSession) bridgeDxvk(vulkan) else null + val rules = FORCED_PACKAGES.filter { it.matches(gpu) }.map { it to false } + listOfNotNull(bridgeRule?.let { it to true }) + if (rules.isEmpty() && !bridgeSession) return null val installed = protons(context) val lines = mutableListOf() - for (rule in rules) { + if (bridgeSession) { + val state = loadState(context) + for (p in installed) { + val entry = state.optJSONObject("active")?.optJSONObject(p.id)?.optJSONObject("dxvk") + if (bridgeRule == null && autoChoiceStale(entry, null)) lines.add(undoAuto(p.id, "dxvk")) + } + } + for ((rule, auto) in rules) { val state = loadState(context) val targets = installed.filter { p -> val active = state.optJSONObject("active")?.optJSONObject(p.id)?.optJSONObject(rule.comp) - forcedPackageWanted(active?.optString("file"), active?.optString("protonVersion") == p.version) + forcedPackageWanted(active?.optString("file"), active?.optString("protonVersion") == p.version) || + (auto && autoChoiceStale(active, rule.file)) } if (targets.isEmpty()) continue val missing = missingPackage(context, rule, lookup, fetch) @@ -1038,9 +1084,10 @@ object ComponentsManager { lines.add(missing) continue } - lines.add(targets.joinToString("; ") { apply(it.id, rule.file) }) + if (auto) lines.add("${vulkan?.label}: DXVK ${rule.file}") + lines.add(targets.joinToString("; ") { if (auto) applyAuto(it.id, rule.file) else apply(it.id, rule.file) }) } - return lines.joinToString("; ").ifEmpty { null } + return lines.filter { it.isNotEmpty() }.joinToString("; ").ifEmpty { null } } private fun missingPackage( diff --git a/app/src/test/java/com/droiddeck/launcher/session/BridgeDxvkTest.kt b/app/src/test/java/com/droiddeck/launcher/session/BridgeDxvkTest.kt new file mode 100644 index 0000000000..9bcc7b6829 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/BridgeDxvkTest.kt @@ -0,0 +1,179 @@ +package com.droiddeck.launcher.session + +import android.content.Context +import com.droiddeck.launcher.gpu.AppVulkan +import com.droiddeck.launcher.gpu.GpuInfo +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.github.luben.zstd.ZstdOutputStream +import org.apache.commons.compress.archivers.tar.TarArchiveEntry +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config +import java.io.File + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], manifest = Config.NONE) +class BridgeDxvkTest { + private val old = ComponentsManager.DXVK_FOR_VULKAN_1_1 + private val mid = ComponentsManager.DXVK_FOR_VULKAN_1_3_NO_INT64 + private lateinit var context: Context + + private val g710 = AppVulkan(1, 1, shaderInt64 = false, maxPushConstants = 128, source = "driver") + private val promoted = g710.copy(minor = 2, source = "bridge") + private val full = AppVulkan(1, 3, shaderInt64 = true, maxPushConstants = 256, source = "driver") + private val mali = GpuInfo("Mali-G710", 0, GpuInfo.Family.NOT_ADRENO, "", false) + + @Before fun setUp() { + context = RuntimeEnvironment.getApplication() + File(context.filesDir, "components").deleteRecursively() + LinuxRuntime.rootDir(context).deleteRecursively() + } + + @Test fun theNewestDxvkThatStartsIsChosen() { + assertEquals(old, ComponentsManager.bridgeDxvk(g710)?.file) + assertEquals(old, ComponentsManager.bridgeDxvk(promoted)?.file) + assertEquals(mid, ComponentsManager.bridgeDxvk(full.copy(shaderInt64 = false))?.file) + assertEquals(mid, ComponentsManager.bridgeDxvk(full.copy(maxPushConstants = 128))?.file) + assertNull(ComponentsManager.bridgeDxvk(full)) + assertNull(ComponentsManager.bridgeDxvk(full.copy(shaderInt64 = null, maxPushConstants = null))) + assertNull(ComponentsManager.bridgeDxvk(AppVulkan(1, 0, null, null, "driver"))) + assertNull(ComponentsManager.bridgeDxvk(null)) + assertEquals("Dxvk-Linux", ComponentsManager.bridgeDxvk(g710)?.release) + assertEquals("dxvk", ComponentsManager.bridgeDxvk(g710)?.comp) + } + + @Test fun theDriversLinesAreRead() { + val vk = AppVulkan.fromDriver(mapOf("device" to "Mali-G710 MC10", "api" to "1.1.177", "shader_int64" to "no", "max_push_constants" to "128")) + assertEquals(g710, vk) + assertNull(AppVulkan.fromDriver(mapOf("error" to "no Vulkan device"))) + assertNull(AppVulkan.fromDriver(mapOf("api" to "garbage"))) + val older = AppVulkan.fromDriver(mapOf("api" to "1.3.250"))!! + assertNull(older.shaderInt64) + assertNull(older.maxPushConstants) + } + + @Test fun theBridgesFileWinsForItsOwnGpuOnly() { + val text = "device=Mali-G710 MC10\napi=1.2.177\ndriver_api=1.1.177\nshader_int64=0\nmax_push_constants=128\n" + assertEquals(promoted, AppVulkan.fromBridgeFile(text, "Mali-G710 MC10")) + assertEquals(promoted, AppVulkan.fromBridgeFile(text, null)) + assertNull(AppVulkan.fromBridgeFile(text, "Mali-G720 Immortalis MC12")) + assertNull(AppVulkan.fromBridgeFile("device=x\n", null)) + } + + @Test fun anAdrenoSessionIsNotTouched() { + proton("proton-exp") + assertNull(run(g710, bridge = false)) + } + + @Test fun aShippedDxvk3IsReplacedOnAVulkan11Phone() { + proton("proton-exp") + proton("ge") + val applied = mutableListOf() + val result = run(g710, applyAuto = { id, file -> applied += "$id $file"; "swapped $id" }) + assertEquals(listOf("ge $old", "proton-exp $old"), applied.sorted()) + assertTrue(result!!.startsWith("Vulkan 1.1, shaderInt64 no, push constants 128 (driver): DXVK $old")) + } + + @Test fun thePlayersOwnChoiceIsKept() { + proton("proton-exp") + writeState("proton-exp", JSONObject().put("file", "dxvk-3.1-linux.wcp").put("protonVersion", "experimental-11")) + assertNull(run(g710)) + assertNull(run(full)) + } + + @Test fun anAutomaticChoiceFollowsTheDevice() { + proton("proton-exp") + writeState("proton-exp", JSONObject().put("file", mid).put("protonVersion", "experimental-11").put("auto", true)) + val applied = mutableListOf() + run(g710, applyAuto = { id, file -> applied += "$id $file"; "swapped" }) + assertEquals(listOf("proton-exp $old"), applied) + + writeState("proton-exp", JSONObject().put("file", old).put("protonVersion", "experimental-11").put("auto", true)) + assertNull(run(g710)) + val undone = mutableListOf() + run(full, undoAuto = { id, comp -> undone += "$id $comp"; "undone" }) + assertEquals(listOf("proton-exp dxvk"), undone) + } + + @Test fun theSwapAndItsUndoReachTheProton() { + proton("proton-exp") + val d3d11 = File(protonDir("proton-exp"), "files/lib/wine/dxvk/aarch64-windows/d3d11.dll") + writePackage(old, "DXVK 1.10.3") + val auto: (String, String) -> String = { id, file -> ComponentsManager.swap(context, id, file, auto = true) } + val undo: (String, String) -> String = { id, comp -> ComponentsManager.undoAutoChoice(context, id, comp) } + + assertNotNull(run(g710, applyAuto = auto, undoAuto = undo)) + assertEquals("DXVK 1.10.3", d3d11.readText()) + val entry = state().getJSONObject("active").getJSONObject("proton-exp").getJSONObject("dxvk") + assertEquals(old, entry.getString("file")) + assertTrue(entry.getBoolean("auto")) + assertNull(run(g710, applyAuto = auto, undoAuto = undo)) + + run(full, applyAuto = auto, undoAuto = undo) + assertEquals("bundled DXVK experimental-11", d3d11.readText()) + assertNull(state().getJSONObject("active").optJSONObject("proton-exp")?.optJSONObject("dxvk")) + } + + @Test fun aProtonUpdateWithNoRuleLeftJustForgetsTheChoice() { + proton("proton-exp", "experimental-12") + writeState("proton-exp", JSONObject().put("file", old).put("protonVersion", "experimental-11").put("auto", true)) + run(full, undoAuto = { id, comp -> ComponentsManager.undoAutoChoice(context, id, comp) }) + assertNull(state().getJSONObject("active").optJSONObject("proton-exp")?.optJSONObject("dxvk")) + assertEquals("bundled DXVK experimental-12", File(protonDir("proton-exp"), "files/lib/wine/dxvk/aarch64-windows/d3d11.dll").readText()) + } + + private fun run( + vk: AppVulkan?, + bridge: Boolean = true, + applyAuto: (String, String) -> String = { id, file -> error("apply $id $file") }, + undoAuto: (String, String) -> String = { id, comp -> error("undo $id $comp") }, + ) = ComponentsManager.ensureForcedPackages( + context, mali, + lookup = { file, release -> ComponentsManager.CatalogItem(file, "dxvk", release, "https://example.invalid/$file", 4, "sha256:" + "ab".repeat(32)) }, + fetch = {}, + apply = { id, file -> error("non-automatic apply $id $file") }, + vulkan = vk, bridgeSession = bridge, applyAuto = applyAuto, undoAuto = undoAuto, + ) + + private fun protonDir(name: String) = File(LinuxRuntime.rootDir(context), "root/.local/share/Steam/steamapps/common/$name") + + private fun proton(name: String, version: String = "experimental-11") { + val dir = protonDir(name) + File(dir, "files/lib/wine").mkdirs() + File(dir, "proton").writeText("#!/bin/sh\n") + File(dir, "version").writeText("1 $version\n") + File(dir, "files/lib/wine/dxvk/aarch64-windows/d3d11.dll").apply { + parentFile!!.mkdirs() + writeText("bundled DXVK $version") + } + } + + private fun state() = JSONObject(File(context.filesDir, "components/state.json").readText()) + + private fun writeState(protonId: String, dxvk: JSONObject) { + val f = File(context.filesDir, "components/state.json") + f.parentFile!!.mkdirs() + f.writeText(JSONObject().put("active", JSONObject().put(protonId, JSONObject().put("dxvk", dxvk))) + .put("queued", JSONObject()).toString()) + } + + private fun writePackage(file: String, d3d11: String) { + val dest = File(context.filesDir, "components/packages/$file") + dest.parentFile!!.mkdirs() + TarArchiveOutputStream(ZstdOutputStream(dest.outputStream())).use { tar -> + fun put(name: String, bytes: ByteArray) { + tar.putArchiveEntry(TarArchiveEntry(name).apply { size = bytes.size.toLong() }) + tar.write(bytes) + tar.closeArchiveEntry() + } + put("./profile.json", JSONObject().put("type", "DXVK-Linux").put("versionName", file.removeSuffix(".wcp")).toString().toByteArray()) + put("./files/lib/wine/dxvk/aarch64-windows/d3d11.dll", d3d11.toByteArray()) + } + } +}