Skip to content

Commit

Permalink
Disable thread safety checks with FalseTweaks (#88)
Browse files Browse the repository at this point in the history
Also adds an API to detect if the thread's tesselator is being captured.
  • Loading branch information
ah-OOG-ah authored Nov 11, 2024
1 parent 9aa77b6 commit e6e2f86
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 8 deletions.
3 changes: 3 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ dependencies {
compileOnly('org.jetbrains:annotations:24.0.1')
compileOnly("org.projectlombok:lombok:1.18.22") {transitive = false }
annotationProcessor("org.projectlombok:lombok:1.18.22")

// For CapturingTesselator compat
compileOnly("com.falsepattern:falsetweaks-mc1.7.10:3.3.2:api")
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ replaceGradleTokenInFile =
# In case your mod provides an API for other mods to implement you may declare its package here. Otherwise, you can
# leave this property empty.
# Example value: (apiPackage = api) + (modGroup = com.myname.mymodid) -> com.myname.mymodid.api
apiPackage =
apiPackage = api

# Specify the configuration file for Forge's access transformers here. It must be placed into /src/main/resources/META-INF/
# There can be multiple files in a space-separated list.
Expand Down
8 changes: 7 additions & 1 deletion repositories.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Add any additional repositories for your dependencies here

repositories {

maven {
name = 'mavenpattern'
url = 'https://mvn.falsepattern.com/releases'
content {
includeGroup 'com.falsepattern'
}
}
}
11 changes: 10 additions & 1 deletion src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import com.gtnewhorizon.gtnhlib.client.model.ModelLoader;
import com.gtnewhorizon.gtnhlib.commands.ItemInHandCommand;
import com.gtnewhorizon.gtnhlib.compat.FalseTweaks;
import com.gtnewhorizon.gtnhlib.compat.Mods;
import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber;
import com.gtnewhorizon.gtnhlib.util.AboveHotbarHUD;

Expand All @@ -23,7 +25,7 @@
public class ClientProxy extends CommonProxy {

private static boolean modelsBaked = false;

public static boolean doThreadSafetyChecks = true;
private final Minecraft mc = Minecraft.getMinecraft();

@Override
Expand All @@ -41,6 +43,13 @@ public void init(FMLInitializationEvent event) {
public void postInit(FMLPostInitializationEvent event) {
super.postInit(event);

if (Mods.FALSETWEAKS) {
doThreadSafetyChecks = FalseTweaks.doTessSafetyChecks();
if (!doThreadSafetyChecks) {
GTNHLib.info("FalseTweaks threaded rendering is enabled - disabling GTNHLib's thread safety checks");
}
}

if (shouldLoadModels()) {
Minecraft.getMinecraft().refreshResources();
ModelLoader.loadModels();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.gtnewhorizon.gtnhlib.api;

import net.minecraft.client.renderer.Tessellator;

import com.gtnewhorizon.gtnhlib.client.renderer.TessellatorManager;

@SuppressWarnings("unused")
public interface CapturingTesselator {

/**
* @return True if this thread is capturing quads, false otherwise
*/
static boolean isCapturing() {
return TessellatorManager.isCurrentlyCapturing();
}

/**
* @throws IllegalStateException If the thread is not capturing and is not the main one.
* @return The CapturingTesselator for this thread if capturing, or else {@link Tessellator#instance} if on the main
* one.
*/
static Tessellator getThreadTesselator() {
return TessellatorManager.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public static Tessellator get() {
}
}

public static boolean isCurrentlyCapturing() {
return currentlyCapturing.get();
}

public static boolean isOnMainThread() {
return Thread.currentThread() == mainThread;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/compat/FalseTweaks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gtnewhorizon.gtnhlib.compat;

import com.falsepattern.falsetweaks.api.ThreadedChunkUpdates;

public class FalseTweaks {

/**
* When FalseTweaks is loaded, it may inject into the Tesselator to do its own threaded chunk building. If it's
* doing that, disable our checks and let FT handle it.
*/
public static boolean doTessSafetyChecks() {
return !ThreadedChunkUpdates.isEnabled();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/compat/Mods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gtnewhorizon.gtnhlib.compat;

import cpw.mods.fml.common.Loader;

public class Mods {

public static final boolean FALSETWEAKS = Loader.isModLoaded("falsetweaks");
}
4 changes: 2 additions & 2 deletions src/main/java/com/gtnewhorizon/gtnhlib/mixins/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

public enum Mixins {

TESSELLATOR(new Builder("Sodium").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT).setPhase(Phase.EARLY)
.setApplyIf(() -> true).addMixinClasses("MixinTessellator")),
TESSELLATOR(new Builder("Thread safety checks for the Tesselator").addTargetedMod(TargetedMod.VANILLA)
.setSide(Side.CLIENT).setPhase(Phase.EARLY).setApplyIf(() -> true).addMixinClasses("MixinTessellator")),
WAVEFRONT_VBO(new Builder("WavefrontObject").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT)
.setPhase(Phase.EARLY).setApplyIf(() -> true).addMixinClasses("MixinWavefrontObject")),
GUI_MOD_LIST(new Builder("Auto config ui").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.gtnewhorizon.gtnhlib.mixins.early;

import static com.gtnewhorizon.gtnhlib.ClientProxy.doThreadSafetyChecks;

import java.nio.Buffer;
import java.nio.ByteBuffer;

Expand All @@ -18,8 +20,6 @@
@Mixin(Tessellator.class)
public abstract class MixinTessellator implements ITessellatorInstance {

@Shadow
public int vertexCount;
@Shadow
public boolean isDrawing;

Expand All @@ -40,7 +40,7 @@ private Buffer removeStaticBufferResetOutsideSingleton(ByteBuffer buffer) {

@Inject(method = "draw", at = @At("HEAD"))
private void preventOffMainThreadDrawing(CallbackInfoReturnable<Integer> cir) {
if (!TessellatorManager.isMainInstance(this)) {
if (doThreadSafetyChecks && !TessellatorManager.isMainInstance(this)) {
throw new RuntimeException("Tried to draw on a tessellator that isn't on the main thread!");
}
}
Expand Down

0 comments on commit e6e2f86

Please sign in to comment.