Skip to content

Commit

Permalink
Feat[EXP]: Allow Custom Mesa Load Driver Override(#292)
Browse files Browse the repository at this point in the history
Feat[EXP]: Allow Custom Mesa Load Driver Override
  • Loading branch information
Vera-Firefly authored Aug 19, 2024
2 parents ec11e9a + feb4533 commit e0bfebb
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ private void runCraft(String versionId, JMinecraftVersionList.Version version) t
if(Tools.DRIVER_MODEL == null) {
Tools.DRIVER_MODEL = LauncherPreferences.PREF_DRIVER_MODEL;
}
if(Tools.LOADER_OVERRIDE == null) {
Tools.LOADER_OVERRIDE = LauncherPreferences.PREF_LOCAL_LOADER_OVERRIDE;
}
if(!Tools.checkRendererCompatible(this, Tools.LOCAL_RENDERER)) {
Tools.RenderersList renderersList = Tools.getCompatibleRenderers(this);
String firstCompatibleRenderer = renderersList.rendererIds.get(0);
Expand Down
38 changes: 38 additions & 0 deletions app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ public final class Tools {

public static String DRIVER_MODEL = null;
public static String MESA_LIBS = null;
public static String LOADER_OVERRIDE = null;

private static CDriverModelList sCompatibleCDriverModel;
private static CMesaLibList sCompatibleCMesaLibs;
private static CMesaLDOList sCompatibleCMesaLDO;
private static RenderersList sCompatibleRenderers;

private static File getPojavStorageRoot(Context ctx) {
Expand Down Expand Up @@ -1333,6 +1335,42 @@ public static CDriverModelList getCompatibleCDriverModel(Context context) {
return sCompatibleCDriverModel;
}

public static class CMesaLDOList implements IListAndArry {
public final List<String> CMesaLDOIds;
public final String[] CMesaLDO;

public CMesaLDOList(List<String> CMesaLDOIds, String[] CMesaLDO) {
this.CMesaLDOIds = CMesaLDOIds;
this.CMesaLDO = CMesaLDO;
}

@Override
public List<String> getList() {
return CMesaLDOIds;
}

@Override
public String[] getArray() {
return CMesaLDO;
}
}

public static CMesaLDOList getCompatibleCMesaLDO(Context context) {
if (sCompatibleCMesaLDO != null) return sCompatibleCMesaLDO;
Resources resources = context.getResources();
String[] defaultCMesaLDO = resources.getStringArray(R.array.osmesa_mldo_values);
String[] defaultCMesaLDONames = resources.getStringArray(R.array.osmesa_mldo);
List<String> CMesaLDOIds = new ArrayList<>(defaultCMesaLDO.length);
List<String> CMesaLDONames = new ArrayList<>(defaultCMesaLDONames.length);
for (int i = 0; i < defaultCMesaLDO.length; i++) {
CMesaLDOIds.add(defaultCMesaLDO[i]);
CMesaLDONames.add(defaultCMesaLDONames[i]);
}
sCompatibleCMesaLDO = new CMesaLDOList(CMesaLDOIds,
CMesaLDONames.toArray(new String[0]));
return sCompatibleCMesaLDO;
}

@SuppressLint("DefaultLocale")
public static String formatFileSize(long bytes) {
if (bytes <= 0) return "0 B";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class LauncherPreferences {
public static String PREF_RENDERER = "opengles2";
public static String PREF_MESA_LIB = "default";
public static String PREF_DRIVER_MODEL = "driver_zink";
public static String PREF_LOCAL_LOADER_OVERRIDE = "kgsl";

public static boolean PREF_VERTYPE_RELEASE = true;
public static boolean PREF_VERTYPE_SNAPSHOT = false;
Expand Down Expand Up @@ -74,9 +75,10 @@ public class LauncherPreferences {

public static boolean PREF_SPARE_BRIDGE = false;
public static boolean PREF_SPARE_FRAME_BUFFER = false;
public static boolean PREF_EXP_ENABLE_SYSTEM = false;
public static boolean PREF_EXP_ENABLE_SYSTEM = true;
public static boolean PREF_EXP_ENABLE_SPECIFIC = false;
public static boolean PREF_EXP_ENABLE_CUSTOM = false;
public static boolean PREF_LOADER_OVERRIDE = false;

public static boolean PREF_VERIFY_MANIFEST = true;
public static String PREF_DOWNLOAD_SOURCE = "default";
Expand Down Expand Up @@ -138,13 +140,15 @@ public static void loadPreferences(Context ctx) {

PREF_SPARE_BRIDGE = DEFAULT_PREF.getBoolean("spareBridge", false);
PREF_SPARE_FRAME_BUFFER = DEFAULT_PREF.getBoolean("SpareFrameBuffer", false);
PREF_EXP_ENABLE_SYSTEM = DEFAULT_PREF.getBoolean("ebSystem", false);
PREF_EXP_ENABLE_SYSTEM = DEFAULT_PREF.getBoolean("ebSystem", true);
PREF_EXP_ENABLE_SPECIFIC = DEFAULT_PREF.getBoolean("ebSpecific", false);
PREF_EXP_ENABLE_CUSTOM = DEFAULT_PREF.getBoolean("ebCustom", false);
PREF_LOADER_OVERRIDE = DEFAULT_PREF.getBoolean("ebChooseMldo", false);

PREF_EXP_SETUP = DEFAULT_PREF.getBoolean("ExperimentalSetup", false);
PREF_MESA_LIB = DEFAULT_PREF.getString("CMesaLibrary", "default");
PREF_DRIVER_MODEL = DEFAULT_PREF.getString("CDriverModels", "driver_zink");
PREF_LOCAL_LOADER_OVERRIDE = DEFAULT_PREF.getString("ChooseMldo", "kgsl");

PREF_MESA_GL_VERSION = DEFAULT_PREF.getString("mesaGLVersion", "4.6");
PREF_MESA_GLSL_VERSION = DEFAULT_PREF.getString("mesaGLSLVersion", "460");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,27 @@ public void onCreatePreferences(Bundle b, String str) {

final ListPreference CMesaLibP = requirePreference("CMesaLibrary", ListPreference.class);
final ListPreference CDriverModelP = requirePreference("CDriverModels", ListPreference.class);
final ListPreference CMesaLDOP = requirePreference("ChooseMldo", ListPreference.class);

setListPreference(CMesaLibP, "CMesaLibrary");
setListPreference(CDriverModelP, "CDriverModels");
setListPreference(CMesaLDOP, "ChooseMldo");

CMesaLibP.setOnPreferenceChangeListener((pre, obj) -> {
Tools.MESA_LIBS = (String)obj;
setListPreference(CDriverModelP, "CDriverModels");
CDriverModelP.setValueIndex(0);
return true;
Tools.MESA_LIBS = (String)obj;
setListPreference(CDriverModelP, "CDriverModels");
CDriverModelP.setValueIndex(0);
return true;
});

CDriverModelP.setOnPreferenceChangeListener((pre, obj) -> {
Tools.DRIVER_MODEL = (String)obj;
return true;
Tools.DRIVER_MODEL = (String)obj;
return true;
});

CMesaLDOP.setOnPreferenceChangeListener((pre, obj) -> {
Tools.LOADER_OVERRIDE = (String)obj;
return true;
});

SwitchPreference expRendererPref = requirePreference("ExperimentalSetup", SwitchPreference.class);
Expand Down Expand Up @@ -117,6 +124,8 @@ private void computeVisibility(){
requirePreference("SpareFrameBuffer").setVisible(LauncherPreferences.PREF_EXP_SETUP);
requirePreference("MesaRendererChoose").setVisible(LauncherPreferences.PREF_EXP_SETUP);
requirePreference("customMesaVersionPref").setVisible(LauncherPreferences.PREF_EXP_SETUP);
requirePreference("customMesaLoaderDriverOverride").setVisible(LauncherPreferences.PREF_EXP_SETUP);
requirePreference("ChooseMldo").setVisible(LauncherPreferences.PREF_LOADER_OVERRIDE);
}

private void setListPreference(ListPreference listPreference, String preferenceKey) {
Expand All @@ -139,6 +148,9 @@ private void setListPreference(ListPreference listPreference, String preferenceK
} else if (preferenceKey.equals("CDriverModels")) {
array = Tools.getCompatibleCDriverModel(getContext());
Tools.DRIVER_MODEL = value;
} else if (preferenceKey.equals("ChooseMldo")) {
array = Tools.getCompatibleCMesaLDO(getContext());
Tools.LOADER_OVERRIDE = value;
}
listPreference.setEntries(array.getArray());
listPreference.setEntryValues(array.getList().toArray(new String[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static net.kdt.pojavlaunch.Tools.LOCAL_RENDERER;
import static net.kdt.pojavlaunch.Tools.MESA_LIBS;
import static net.kdt.pojavlaunch.Tools.DRIVER_MODEL;
import static net.kdt.pojavlaunch.Tools.LOADER_OVERRIDE;
import static net.kdt.pojavlaunch.Tools.NATIVE_LIB_DIR;
import static net.kdt.pojavlaunch.Tools.currentDisplayMetrics;
import static net.kdt.pojavlaunch.Tools.shareLog;
Expand Down Expand Up @@ -199,7 +200,6 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws
envMap.put("FORCE_VSYNC", String.valueOf(LauncherPreferences.PREF_FORCE_VSYNC));

envMap.put("MESA_GLSL_CACHE_DIR", Tools.DIR_CACHE.getAbsolutePath());
envMap.put("MESA_LOADER_DRIVER_OVERRIDE", "zink");

envMap.put("force_glsl_extensions_warn", "true");
envMap.put("allow_higher_compat_version", "true");
Expand All @@ -208,10 +208,12 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws
envMap.put("LD_LIBRARY_PATH", LD_LIBRARY_PATH);
envMap.put("PATH", jreHome + "/bin:" + Os.getenv("PATH"));

if(LauncherPreferences.PREF_BIG_CORE_AFFINITY) envMap.put("POJAV_BIG_CORE_AFFINITY", "1");
envMap.put("AWTSTUB_WIDTH", Integer.toString(CallbackBridge.windowWidth > 0 ? CallbackBridge.windowWidth : CallbackBridge.physicalWidth));
envMap.put("AWTSTUB_HEIGHT", Integer.toString(CallbackBridge.windowHeight > 0 ? CallbackBridge.windowHeight : CallbackBridge.physicalHeight));

if(LauncherPreferences.PREF_BIG_CORE_AFFINITY)
envMap.put("POJAV_BIG_CORE_AFFINITY", "1");

if(PREF_DUMP_SHADERS)
envMap.put("LIBGL_VGPU_DUMP", "1");
if(PREF_ZINK_PREFER_SYSTEM_DRIVER)
Expand All @@ -229,9 +231,8 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws

if(Tools.deviceHasHangingLinker())
envMap.put("POJAV_EMUI_ITERATOR_MITIGATE", "1");
if(FFmpegPlugin.isAvailable) {
if(FFmpegPlugin.isAvailable)
envMap.put("PATH", FFmpegPlugin.libraryPath+":"+envMap.get("PATH"));
}

if (LOCAL_RENDERER != null) {
if (!PREF_EXP_SETUP) {
Expand All @@ -250,6 +251,7 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws
case "freedreno":{
envMap.put("POJAV_BETA_RENDERER", "mesa_3d");
envMap.put("LOCAL_DRIVER_MODEL", "driver_freedreno");
envMap.put("LOCAL_LOADER_OVERRIDE", "kgsl");
envMap.put("MESA_LIBRARY", localMesaLibrary);
} break;
case "panfrost":{
Expand All @@ -263,12 +265,12 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws
envMap.put("POJAV_BETA_RENDERER", LOCAL_RENDERER);
} break;
}
} else {
envMap.put("POJAV_BETA_RENDERER", LOCAL_RENDERER);
}
} else envMap.put("POJAV_BETA_RENDERER", LOCAL_RENDERER);

if (LOCAL_RENDERER.equals("mesa_3d")) {
envMap.put("MESA_LIBRARY", localMesaLibrary);
envMap.put("LOCAL_DRIVER_MODEL", DRIVER_MODEL);

if (PREF_EXP_ENABLE_SPECIFIC) {
switch (DRIVER_MODEL) {
case "driver_zink":
Expand All @@ -291,24 +293,44 @@ public static void setJavaEnvironment(Activity activity, String jreHome) throws
envMap.put("MESA_GL_VERSION_OVERRIDE", glVersion);
envMap.put("MESA_GLSL_VERSION_OVERRIDE", glslVersion);
}
if (MESA_LIBS.equals("mesa2205")) {
envMap.put("DCLAT_FRAMEBUFFER", "1");
if(DRIVER_MODEL.equals("driver_zink"))
envMap.put("POJAV_LEGACY_ZINK_ALLOW", "1");

if (PREF_LOADER_OVERRIDE && DRIVER_MODEL.equals("driver_freedreno"))
{
switch (LOADER_OVERRIDE) {
case "kgsl":
envMap.put("LOCAL_LOADER_OVERRIDE", "kgsl");
break;
case "msm":
envMap.put("LOCAL_LOADER_OVERRIDE", "msm");
break;
case "virtio":
envMap.put("LOCAL_LOADER_OVERRIDE", "virtio");
break;
default:
envMap.put("LOCAL_LOADER_OVERRIDE", "kgsl");
}
}

if (DRIVER_MODEL.equals("driver_virgl"))
{
envMap.put("DCLAT_FRAMEBUFFER", "1");
envMap.put("VTEST_SOCKET_NAME", new File(Tools.DIR_CACHE, ".virgl_test").getAbsolutePath());
if (DRIVER_MODEL.equals("driver_panfrost")) {
}

if (DRIVER_MODEL.equals("driver_panfrost"))
{
envMap.put("MESA_DISK_CACHE_SINGLE_FILE", "1");
if (MESA_LIBS.equals("default")) {
if (MESA_LIBS.equals("default"))
envMap.put("PAN_MESA_DEBUG", "trace");
}
}
}
if (LOCAL_RENDERER.equals("opengles3_desktopgl_angle_vulkan")) {

if (LOCAL_RENDERER.equals("opengles3_desktopgl_angle_vulkan"))
{
envMap.put("LIBGL_ES", "3");
envMap.put("POJAVEXEC_EGL","libEGL_angle.so"); // Use ANGLE EGL
}

}

File customEnvFile = new File(ProfilePathManager.getCurrentPath(), "custom_env.txt");
Expand Down
44 changes: 23 additions & 21 deletions app_pojavlauncher/src/main/jni/egl_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,57 +198,63 @@ void renderer_load_config() {
int pojavInitOpenGL() {
// Only affects GL4ES as of now
const char *forceVsync = getenv("FORCE_VSYNC");
if (strcmp(forceVsync, "true") == 0)
if (!strcmp(forceVsync, "true"))
pojav_environ->force_vsync = true;

// NOTE: Override for now.
const char *renderer = getenv("POJAV_BETA_RENDERER");
const char *ldrivermodel = getenv("LOCAL_DRIVER_MODEL");
const char *mldo = getenv("LOCAL_LOADER_OVERRIDE");

if (strncmp("opengles", renderer, 8) == 0) {
if (mldo) printf("OSMDroid: MESA_LOADER_DRIVER_OVERRIDE = %s\n", mldo);

if (!strncmp("opengles", renderer, 8))
{
pojav_environ->config_renderer = RENDERER_GL4ES;
setenv("MESA_LOADER_DRIVER_OVERRIDE", "zink", 1);
if (!SpareBridge()) set_gl_bridge_tbl();
} else if (strcmp(renderer, "mesa_3d") == 0) {
} else if (!strcmp(renderer, "mesa_3d")) {

if (strcmp(ldrivermodel, "driver_zink") == 0)
if (!strcmp(ldrivermodel, "driver_zink"))
{
setenv("GALLIUM_DRIVER","zink",1);
setenv("GALLIUM_DRIVER", "zink", 1);
renderer_load_config();
load_vulkan();
}

if (strcmp(ldrivermodel, "driver_virgl") == 0)
if (!strcmp(ldrivermodel, "driver_virgl"))
{
pojav_environ->config_renderer = RENDERER_VIRGL;
setenv("GALLIUM_DRIVER","virpipe",1);
setenv("OSMESA_NO_FLUSH_FRONTBUFFER","1",false);
if(strcmp(getenv("OSMESA_NO_FLUSH_FRONTBUFFER"),"1") == 0) {
setenv("MESA_LOADER_DRIVER_OVERRIDE", "zink", 1);
setenv("GALLIUM_DRIVER", "virpipe", 1);
setenv("OSMESA_NO_FLUSH_FRONTBUFFER", "1", false);
if (!strcmp(getenv("OSMESA_NO_FLUSH_FRONTBUFFER"), "1"))
printf("VirGL: OSMesa buffer flush is DISABLED!\n");
}
loadSymbolsVirGL();
}

if (strcmp(ldrivermodel, "driver_panfrost") == 0)
if (!strcmp(ldrivermodel, "driver_panfrost"))
{
setenv("GALLIUM_DRIVER", "panfrost", 1);
renderer_load_config();
}

if (strcmp(ldrivermodel, "driver_freedreno") == 0)
if (!strcmp(ldrivermodel, "driver_freedreno"))
{
setenv("GALLIUM_DRIVER", "freedreno", 1);
setenv("MESA_LOADER_DRIVER_OVERRIDE", "kgsl", 1);
if (mldo) setenv("MESA_LOADER_DRIVER_OVERRIDE", mldo, 1);
else setenv("MESA_LOADER_DRIVER_OVERRIDE", "kgsl", 1);
renderer_load_config();
}

if (strcmp(ldrivermodel, "driver_softpipe") == 0)
if (!strcmp(ldrivermodel, "driver_softpipe"))
{
setenv("GALLIUM_DRIVER", "softpipe", 1);
setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
renderer_load_config();
}

if (strcmp(ldrivermodel, "driver_llvmpipe") == 0)
if (!strcmp(ldrivermodel, "driver_llvmpipe"))
{
setenv("GALLIUM_DRIVER", "llvmpipe", 1);
setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
Expand All @@ -260,19 +266,15 @@ int pojavInitOpenGL() {
|| pojav_environ->config_renderer == RENDERER_GL4ES)
{
if (gl_init() && SpareBridge() && pojav_environ->config_renderer == RENDERER_GL4ES)
{
gl_setup_window();
} else {
if (br_init()) br_setup_window();
}
else if (br_init()) br_setup_window();
}

if (pojav_environ->config_renderer == RENDERER_VK_ZINK_PREF && spare_init())
spare_setup_window();

if (pojav_environ->config_renderer == RENDERER_VIRGL) {
if (pojav_environ->config_renderer == RENDERER_VIRGL)
pojav_virgl_init();
}

return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion app_pojavlauncher/src/main/res/values-zh-rCN/pgw_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<string name="preference_renderer_setup_extra">渲染器 (拓展)</string>
<string name="preference_renderer_extra_default">Mesa24.2.0 (默认)</string>
<string name="preference_extra_mesa_version">Mesa版本</string>
<string name="preference_extra_driver_model">驱动模型</string>
<string name="preference_extra_driver_model">Gallium驱动程序</string>
<!-- Dialog -->
<string name="preference_rendererexp_alertdialog_warning">警告</string>
<string name="preference_rendererexp_alertdialog_message">您已打开实验性设置,此功能可能会产生意想不到的bug,是否继续使用?</string>
Expand Down Expand Up @@ -113,5 +113,10 @@
<string name="preference_rendererexp_mesa_downloading">正在下载Mesa运行库</string>
<string name="preference_rendererexp_mesa_download_fail">Mesa运行库下载失败</string>
<string name="preference_rendererexp_mesa_downloaded">Mesa运行库下载成功</string>
<!-- Custom Mesa Loader Driver Override -->
<string name="preference_extra_osm_cmldo_categ">自定义Mesa图形驱动程序</string>
<string name="osm_enable_cmldo">允许自定义Mesa图形驱动程序(目前仅Freedreno)</string>
<string name="osm_enable_cmldo_desc">如果需要,您可以自由选择处理方法</string>
<string name="osm_choose_cmldo">选择Mesa图形驱动程序</string>

</resources>
Loading

0 comments on commit e0bfebb

Please sign in to comment.