Skip to content

Commit

Permalink
Add workaround to disable Threaded Optimizations on Linux (#1900)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCRcortex authored Jul 23, 2023
1 parent 6ff2224 commit 8ade8ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ private static Set<Reference> findNecessaryWorkarounds(List<GraphicsAdapterProbe
var workarounds = EnumSet.noneOf(Reference.class);
var operatingSystem = Util.getOperatingSystem();

if (operatingSystem == Util.OperatingSystem.WINDOWS && graphicsAdapters.stream()
.anyMatch(adapter -> adapter.vendor() == GraphicsAdapterProbe.Vendor.NVIDIA)) {
if ((operatingSystem == Util.OperatingSystem.WINDOWS || operatingSystem == Util.OperatingSystem.LINUX) &&
graphicsAdapters.stream().anyMatch(adapter -> adapter.vendor() == GraphicsAdapterProbe.Vendor.NVIDIA)) {
workarounds.add(Reference.NVIDIA_BAD_DRIVER_SETTINGS);
}

Expand Down Expand Up @@ -69,6 +69,6 @@ public enum Reference {
* Requesting a No Error Context causes a crash at startup when using a Wayland session.
* <a href="https://github.com/CaffeineMC/sodium-fabric/issues/1624">GitHub Issue</a>
*/
NO_ERROR_CONTEXT_UNSUPPORTED
NO_ERROR_CONTEXT_UNSUPPORTED,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,16 @@ public static void install() {
return;
}

if (Util.getOperatingSystem() != Util.OperatingSystem.WINDOWS) {
return;
}

LOGGER.info("Attempting to apply workarounds for the NVIDIA Graphics Driver...");
LOGGER.info("If the game crashes immediately after this point, please make a bug report: https://github.com/CaffeineMC/sodium-fabric/issues");

if (Util.getOperatingSystem() == Util.OperatingSystem.LINUX) {
setLinuxDisableEnv();
return;
} else if (Util.getOperatingSystem() != Util.OperatingSystem.WINDOWS) {
return;
}

try {
ACTIVE_HOOK = new Hook();
ACTIVE_HOOK.install();
Expand All @@ -199,6 +202,18 @@ public static void install() {
}
}

public static void setLinuxDisableEnv() {
try (SharedLibrary sharedLibrary = Library.loadNative("me.jellyquid.mods.sodium", "libc.so.6")) {
long pfnSetenv = APIUtil.apiGetFunctionAddress(sharedLibrary, "setenv");
try (var stack = MemoryStack.stackPush()) {
JNI.callPPI(MemoryUtil.memAddress0(stack.UTF8("__GL_THREADED_OPTIMIZATIONS")), MemoryUtil.memAddress0(stack.UTF8("0")), 1, pfnSetenv);
}
} catch (Throwable t) {
LOGGER.error("Failure while applying workarounds", t);
LOGGER.error("READ ME! The workarounds for the NVIDIA Graphics Driver did not apply correctly!");
}
}

public static void uninstall() {
if (ACTIVE_HOOK == null) {
return;
Expand Down

0 comments on commit 8ade8ab

Please sign in to comment.