From e9c7564a03ce3e98a1925b4da817ef62aa410c13 Mon Sep 17 00:00:00 2001
From: Emmanuel Hansen <emmausssss@gmail.com>
Date: Fri, 7 Jul 2023 17:15:52 +0000
Subject: [PATCH] add more options, fix vsync option

---
 .../main/java/org/ryujinx/android/GameHost.kt | 15 ++--
 .../android/viewmodels/QuickSettings.kt       |  6 ++
 .../android/viewmodels/SettingsViewModel.kt   | 40 ++++++---
 .../org/ryujinx/android/views/SettingViews.kt | 87 +++++++++++++++++--
 4 files changed, 119 insertions(+), 29 deletions(-)

diff --git a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/GameHost.kt b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/GameHost.kt
index e741d25cf..eeb9e4c8e 100644
--- a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/GameHost.kt
+++ b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/GameHost.kt
@@ -91,7 +91,13 @@ class GameHost(context: Context?, val controller: GameController, val mainViewMo
 
         var surface = surfaceHolder.surface;
 
-        var success = _nativeRyujinx.graphicsInitialize(GraphicsConfiguration())
+        var settings = QuickSettings(mainViewModel.activity)
+
+        var success = _nativeRyujinx.graphicsInitialize(GraphicsConfiguration().apply {
+            EnableShaderCache = settings.enableShaderCache
+            EnableTextureRecompression = settings.enableTextureRecompression
+            ResScale = settings.resScale
+        })
 
 
         var nativeHelpers = NativeHelpers()
@@ -108,7 +114,6 @@ class GameHost(context: Context?, val controller: GameController, val mainViewMo
             window
         )
 
-        var settings = QuickSettings(mainViewModel.activity)
 
         success = _nativeRyujinx.deviceInitialize(
             settings.isHostMapped,
@@ -135,12 +140,6 @@ class GameHost(context: Context?, val controller: GameController, val mainViewMo
         );
 
         _guestThread = thread(start = true) {
-            runBlocking {
-                this.launch {
-                    delay(5000)
-                    _nativeRyujinx.graphicsRendererSetVsync(false);
-                }
-            }
             runGame()
         }
         _isStarted = success;
diff --git a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/QuickSettings.kt b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/QuickSettings.kt
index 4d07f5a99..188e9ec6f 100644
--- a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/QuickSettings.kt
+++ b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/QuickSettings.kt
@@ -11,6 +11,9 @@ class QuickSettings(val activity: MainActivity) {
     var enableVsync: Boolean
     var useNce: Boolean
     var isHostMapped: Boolean
+    var enableShaderCache: Boolean
+    var enableTextureRecompression: Boolean
+    var resScale : Float
 
     private var sharedPref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(activity)
 
@@ -21,5 +24,8 @@ class QuickSettings(val activity: MainActivity) {
         enableDocked = sharedPref.getBoolean("enableDocked", true)
         enablePtc = sharedPref.getBoolean("enablePtc", true)
         ignoreMissingServices = sharedPref.getBoolean("ignoreMissingServices", false)
+        enableShaderCache = sharedPref.getBoolean("enableShaderCache", true)
+        enableTextureRecompression = sharedPref.getBoolean("enableTextureRecompression", false)
+        resScale = sharedPref.getFloat("resScale", 1f)
     }
 }
\ No newline at end of file
diff --git a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/SettingsViewModel.kt b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/SettingsViewModel.kt
index 63d633fd9..095398921 100644
--- a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/SettingsViewModel.kt
+++ b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/viewmodels/SettingsViewModel.kt
@@ -17,12 +17,17 @@ class SettingsViewModel(var navController: NavHostController, val activity: Main
         return PreferenceManager.getDefaultSharedPreferences(activity)
     }
 
-    fun initializeState(isHostMapped : MutableState<Boolean>,
-                        useNce : MutableState<Boolean>,
-                        enableVsync : MutableState<Boolean>,
-                        enableDocked : MutableState<Boolean>,
-                        enablePtc : MutableState<Boolean>,
-                        ignoreMissingServices : MutableState<Boolean>)
+    fun initializeState(
+        isHostMapped: MutableState<Boolean>,
+        useNce: MutableState<Boolean>,
+        enableVsync: MutableState<Boolean>,
+        enableDocked: MutableState<Boolean>,
+        enablePtc: MutableState<Boolean>,
+        ignoreMissingServices: MutableState<Boolean>,
+        enableShaderCache: MutableState<Boolean>,
+        enableTextureRecompression: MutableState<Boolean>,
+        resScale: MutableState<Float>
+    )
     {
 
         isHostMapped.value = sharedPref.getBoolean("isHostMapped", true)
@@ -31,14 +36,22 @@ class SettingsViewModel(var navController: NavHostController, val activity: Main
         enableDocked.value = sharedPref.getBoolean("enableDocked", true)
         enablePtc.value = sharedPref.getBoolean("enablePtc", true)
         ignoreMissingServices.value = sharedPref.getBoolean("ignoreMissingServices", false)
+        enableShaderCache.value = sharedPref.getBoolean("enableShaderCache", true)
+        enableTextureRecompression.value = sharedPref.getBoolean("enableTextureRecompression", false)
+        resScale.value = sharedPref.getFloat("resScale", 1f)
     }
 
-    fun save(isHostMapped : MutableState<Boolean>,
-             useNce : MutableState<Boolean>,
-             enableVsync : MutableState<Boolean>,
-             enableDocked : MutableState<Boolean>,
-             enablePtc : MutableState<Boolean>,
-             ignoreMissingServices : MutableState<Boolean>){
+    fun save(
+        isHostMapped: MutableState<Boolean>,
+        useNce: MutableState<Boolean>,
+        enableVsync: MutableState<Boolean>,
+        enableDocked: MutableState<Boolean>,
+        enablePtc: MutableState<Boolean>,
+        ignoreMissingServices: MutableState<Boolean>,
+        enableShaderCache: MutableState<Boolean>,
+        enableTextureRecompression: MutableState<Boolean>,
+        resScale: MutableState<Float>
+    ){
         var editor = sharedPref.edit()
 
         editor.putBoolean("isHostMapped", isHostMapped?.value ?: true)
@@ -47,6 +60,9 @@ class SettingsViewModel(var navController: NavHostController, val activity: Main
         editor.putBoolean("enableDocked", enableDocked?.value ?: true)
         editor.putBoolean("enablePtc", enablePtc?.value ?: true)
         editor.putBoolean("ignoreMissingServices", ignoreMissingServices?.value ?: false)
+        editor.putBoolean("enableShaderCache", enableShaderCache?.value ?: true)
+        editor.putBoolean("enableTextureRecompression", enableTextureRecompression?.value ?: false)
+        editor.putFloat("resScale", resScale?.value ?: 1f)
 
         editor.apply()
     }
diff --git a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/views/SettingViews.kt b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/views/SettingViews.kt
index d523533a4..68cfd3416 100644
--- a/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/views/SettingViews.kt
+++ b/src/RyujinxAndroid/app/src/main/java/org/ryujinx/android/views/SettingViews.kt
@@ -3,7 +3,6 @@ package org.ryujinx.android.views
 import android.annotation.SuppressLint
 import androidx.activity.compose.BackHandler
 import androidx.compose.animation.AnimatedVisibility
-import androidx.compose.animation.animateColor
 import androidx.compose.animation.core.FastOutSlowInEasing
 import androidx.compose.animation.core.MutableTransitionState
 import androidx.compose.animation.core.animateDp
@@ -15,13 +14,10 @@ import androidx.compose.animation.fadeIn
 import androidx.compose.animation.fadeOut
 import androidx.compose.animation.shrinkVertically
 import androidx.compose.foundation.layout.Arrangement
-import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.Column
 import androidx.compose.foundation.layout.Row
-import androidx.compose.foundation.layout.Spacer
 import androidx.compose.foundation.layout.fillMaxSize
 import androidx.compose.foundation.layout.fillMaxWidth
-import androidx.compose.foundation.layout.heightIn
 import androidx.compose.foundation.layout.padding
 import androidx.compose.material.icons.Icons
 import androidx.compose.material.icons.filled.ArrowBack
@@ -32,9 +28,9 @@ import androidx.compose.material3.Icon
 import androidx.compose.material3.IconButton
 import androidx.compose.material3.MaterialTheme
 import androidx.compose.material3.Scaffold
+import androidx.compose.material3.Slider
 import androidx.compose.material3.Switch
 import androidx.compose.material3.Text
-import androidx.compose.material3.TextButton
 import androidx.compose.material3.TopAppBar
 import androidx.compose.runtime.Composable
 import androidx.compose.runtime.getValue
@@ -76,12 +72,24 @@ class SettingViews {
             var ignoreMissingServices = remember {
                 mutableStateOf(false)
             }
+            var enableShaderCache = remember {
+                mutableStateOf(false)
+            }
+            var enableTextureRecompression = remember {
+                mutableStateOf(false)
+            }
+            var resScale = remember {
+                mutableStateOf(1f)
+            }
 
             if (!loaded.value) {
                 settingsViewModel.initializeState(
                     isHostMapped,
                     useNce,
-                    enableVsync, enableDocked, enablePtc, ignoreMissingServices
+                    enableVsync, enableDocked, enablePtc, ignoreMissingServices,
+                    enableShaderCache,
+                    enableTextureRecompression,
+                    resScale
                 )
                 loaded.value = true
             }
@@ -99,7 +107,10 @@ class SettingViews {
                                     enableVsync,
                                     enableDocked,
                                     enablePtc,
-                                    ignoreMissingServices
+                                    ignoreMissingServices,
+                                    enableShaderCache,
+                                    enableTextureRecompression,
+                                    resScale
                                 )
                                 settingsViewModel.navController.popBackStack()
                             }) {
@@ -111,7 +122,10 @@ class SettingViews {
                     BackHandler {
                         settingsViewModel.save(
                             isHostMapped,
-                            useNce, enableVsync, enableDocked, enablePtc, ignoreMissingServices
+                            useNce, enableVsync, enableDocked, enablePtc, ignoreMissingServices,
+                            enableShaderCache,
+                            enableTextureRecompression,
+                            resScale
                         )
                     }
                     ExpandableView(onCardArrowClick = { }, title = "System") {
@@ -208,6 +222,59 @@ class SettingViews {
                             }
                         }
                     }
+                    ExpandableView(onCardArrowClick = { }, title = "Graphics") {
+                        Column(modifier = Modifier.fillMaxWidth()) {
+                            Row(
+                                modifier = Modifier
+                                    .fillMaxWidth()
+                                    .padding(8.dp),
+                                horizontalArrangement = Arrangement.SpaceBetween,
+                                verticalAlignment = Alignment.CenterVertically
+                            ) {
+                                Text(
+                                    text = "Enable Shader Cache",
+                                    modifier = Modifier.align(Alignment.CenterVertically)
+                                )
+                                Switch(checked = enableShaderCache.value, onCheckedChange = {
+                                    enableShaderCache.value = !enableShaderCache.value
+                                })
+                            }
+                            Row(
+                                modifier = Modifier
+                                    .fillMaxWidth()
+                                    .padding(8.dp),
+                                horizontalArrangement = Arrangement.SpaceBetween,
+                                verticalAlignment = Alignment.CenterVertically
+                            ) {
+                                Text(
+                                    text = "Resolution Scale",
+                                    modifier = Modifier.align(Alignment.CenterVertically)
+                                )
+                                Text(text = resScale.value.toString() +"x")
+                            }
+                            Slider(value = resScale.value,
+                                valueRange = 0.5f..4f,
+                                steps = 6,
+                                onValueChange = { it ->
+                                    resScale.value = it
+                                } )
+                            Row(
+                                modifier = Modifier
+                                    .fillMaxWidth()
+                                    .padding(8.dp),
+                                horizontalArrangement = Arrangement.SpaceBetween,
+                                verticalAlignment = Alignment.CenterVertically
+                            ) {
+                                Text(
+                                    text = "Enable Texture Recompression",
+                                    modifier = Modifier.align(Alignment.CenterVertically)
+                                )
+                                Switch(checked = enableTextureRecompression.value, onCheckedChange = {
+                                    enableTextureRecompression.value = !enableTextureRecompression.value
+                                })
+                            }
+                        }
+                    }
                 }
             }
         }
@@ -293,7 +360,9 @@ class SettingViews {
             Icon(
                 Icons.Filled.KeyboardArrowUp,
                 contentDescription = "Expandable Arrow",
-                modifier = Modifier.padding(8.dp).rotate(degrees),
+                modifier = Modifier
+                    .padding(8.dp)
+                    .rotate(degrees),
             )
         }