diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java index d08ca405b2..5b58445bf3 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/setting/VersionSetting.java @@ -428,6 +428,21 @@ public void setNotPatchNatives(boolean notPatchNatives) { notPatchNativesProperty.set(notPatchNatives); } + private final BooleanProperty notAutoChooseGPUProperty = new SimpleBooleanProperty(this, "notAutoChooseGPU", false); + + public BooleanProperty notAutoChooseGPUProperty() { + return notAutoChooseGPUProperty; + + } + + public boolean isNotAutoChooseGPU() { + return notAutoChooseGPUProperty.get(); + } + + public void setNotAutoChooseGPU(boolean notAutoChooseGPU) { + notAutoChooseGPUProperty.set(notAutoChooseGPU); + } + private final BooleanProperty showLogsProperty = new SimpleBooleanProperty(this, "showLogs", false); public BooleanProperty showLogsProperty() { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java index c47cb30df9..40c60fafa8 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/AdvancedVersionSettingPage.java @@ -46,6 +46,7 @@ public final class AdvancedVersionSettingPage extends StackPane implements Decor private final OptionToggleButton noGameCheckPane; private final OptionToggleButton noJVMCheckPane; private final OptionToggleButton noNativesPatchPane; + private final OptionToggleButton noAutoChooseGPUPane; private final OptionToggleButton useNativeGLFWPane; private final OptionToggleButton useNativeOpenALPane; private final ComponentSublist nativesDirSublist; @@ -192,6 +193,9 @@ public AdvancedVersionSettingPage(Profile profile, @Nullable String versionId, V noNativesPatchPane = new OptionToggleButton(); noNativesPatchPane.setTitle(i18n("settings.advanced.dont_patch_natives")); + noAutoChooseGPUPane = new OptionToggleButton(); + noAutoChooseGPUPane.setTitle("不自动选择 GPU"); // TODO: i18n + useNativeGLFWPane = new OptionToggleButton(); useNativeGLFWPane.setTitle(i18n("settings.advanced.use_native_glfw")); @@ -200,7 +204,7 @@ public AdvancedVersionSettingPage(Profile profile, @Nullable String versionId, V workaroundPane.getContent().setAll( nativesDirSublist, rendererPane, noJVMArgsPane, noGameCheckPane, - noJVMCheckPane, noNativesPatchPane + noJVMCheckPane, noNativesPatchPane, noAutoChooseGPUPane ); if (OperatingSystem.CURRENT_OS.isLinuxOrBSD()) { @@ -236,6 +240,7 @@ void bindProperties() { noJVMCheckPane.selectedProperty().bindBidirectional(versionSetting.notCheckJVMProperty()); noJVMArgsPane.selectedProperty().bindBidirectional(versionSetting.noJVMArgsProperty()); noNativesPatchPane.selectedProperty().bindBidirectional(versionSetting.notPatchNativesProperty()); + noAutoChooseGPUPane.selectedProperty().bindBidirectional(versionSetting.notAutoChooseGPUProperty()); useNativeGLFWPane.selectedProperty().bindBidirectional(versionSetting.useNativeGLFWProperty()); useNativeOpenALPane.selectedProperty().bindBidirectional(versionSetting.useNativeOpenALProperty()); @@ -277,6 +282,7 @@ void unbindProperties() { noJVMCheckPane.selectedProperty().unbindBidirectional(versionSetting.notCheckJVMProperty()); noJVMArgsPane.selectedProperty().unbindBidirectional(versionSetting.noJVMArgsProperty()); noNativesPatchPane.selectedProperty().unbindBidirectional(versionSetting.notPatchNativesProperty()); + noAutoChooseGPUPane.selectedProperty().unbindBidirectional(versionSetting.notAutoChooseGPUProperty()); useNativeGLFWPane.selectedProperty().unbindBidirectional(versionSetting.useNativeGLFWProperty()); useNativeOpenALPane.selectedProperty().unbindBidirectional(versionSetting.useNativeOpenALProperty()); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java index 3a84c7658e..7256544055 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/launch/DefaultLauncher.java @@ -28,6 +28,8 @@ import org.jackhuang.hmcl.util.io.Unzipper; import org.jackhuang.hmcl.util.platform.Bits; import org.jackhuang.hmcl.util.platform.*; +import org.jackhuang.hmcl.util.platform.hardware.GraphicsCard; +import org.jackhuang.hmcl.util.platform.hardware.HardwareVendor; import org.jackhuang.hmcl.util.versioning.GameVersionNumber; import java.io.*; @@ -524,7 +526,24 @@ private Map getEnvVars() { env.put("INST_JAVA", options.getJava().getBinary().toString()); Renderer renderer = options.getRenderer(); - if (renderer != Renderer.DEFAULT) { + if (renderer == Renderer.DEFAULT) { + List graphicsCards = SystemInfo.getGraphicsCards(); + if (graphicsCards != null && graphicsCards.size() == 2) { + GraphicsCard card0 = graphicsCards.get(0); + GraphicsCard card1 = graphicsCards.get(1); + + if (card0.getType() != null && card1.getType() != null + && card0.getType() != card1.getType()) { + GraphicsCard discreteGraphicsCard = card0.getType() == GraphicsCard.Type.Discrete ? card0 : card1; + if (HardwareVendor.NVIDIA.equals(discreteGraphicsCard.getVendor())) { + // https://askubuntu.com/a/1350825 + env.put("__NV_PRIME_RENDER_OFFLOAD", "1"); + env.put("__GLX_VENDOR_LIBRARY_NAME", "nvidia"); + env.put("__VK_LAYER_NV_optimus", "NVIDIA_only"); + } + } + } + } else { if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { if (renderer != Renderer.LLVMPIPE) env.put("GALLIUM_DRIVER", renderer.name().toLowerCase(Locale.ROOT)); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/GraphicsCard.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/GraphicsCard.java index d7323836c7..d962f079ff 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/GraphicsCard.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/hardware/GraphicsCard.java @@ -71,6 +71,10 @@ public String getName() { return name; } + public @Nullable Type getType() { + return type; + } + public @Nullable HardwareVendor getVendor() { return vendor; }