From 6a7c2520d02a2ec6ec4b3a6dc4920029cfd1de89 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Wed, 31 Jan 2024 16:53:45 -0800 Subject: [PATCH] Opt in thread saftey (#254) * Evolution of thread safe ISBRH * Add API package Co-authored-by: makamys --- gradle.properties | 2 +- .../angelica/api/ThreadSafeISBRH.java | 35 +++++++++++++++++++ .../angelica/api/ThreadSafeISBRHFactory.java | 22 ++++++++++++ .../angelica/interfaces/IThreadSafeISBRH.java | 8 ----- .../early/sodium/MixinRenderingRegistry.java | 32 ++++++++++++++--- .../AngelicaBlockSafetyRegistry.java | 17 ++++++--- 6 files changed, 97 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRH.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRHFactory.java delete mode 100644 src/main/java/com/gtnewhorizons/angelica/interfaces/IThreadSafeISBRH.java diff --git a/gradle.properties b/gradle.properties index b282dd38c..72edbf388 100644 --- a/gradle.properties +++ b/gradle.properties @@ -57,7 +57,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 comma-separated list. diff --git a/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRH.java b/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRH.java new file mode 100644 index 000000000..c773f5abe --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRH.java @@ -0,0 +1,35 @@ +package com.gtnewhorizons.angelica.api; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * Marks an ISBRH as thread-safe, allowing Angelica to run it off-thread. + * + *

Note: Any tessellator access must be done within the methods of the ISBRH and not cached in the class.

+ */ +@Documented +@Target({ TYPE }) +@Retention(RUNTIME) +public @interface ThreadSafeISBRH { + /** + * Specifies whether Angelica should create a thread local instance of the class for each thread. + * + */ + boolean perThread(); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRHFactory.java b/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRHFactory.java new file mode 100644 index 000000000..0354d715b --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/api/ThreadSafeISBRHFactory.java @@ -0,0 +1,22 @@ +package com.gtnewhorizons.angelica.api; + +import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; + +/** + * Marks an ISBRH as thread-safe using a thread local instance, allowing Angelica to run it off-thread. + * Use this when your ISBRH uses a non-default constructor. + *

To avoid a hard dependency on Angelica, you will likely want to mark this as an optional interface using:

+ *
+ * @Optional.Interface(modid = "angelica", iface = "com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory")
+ * 
+ * + *

Notes

+ * + */ +public interface ThreadSafeISBRHFactory extends ISimpleBlockRenderingHandler { + ThreadSafeISBRHFactory newInstance(); +} diff --git a/src/main/java/com/gtnewhorizons/angelica/interfaces/IThreadSafeISBRH.java b/src/main/java/com/gtnewhorizons/angelica/interfaces/IThreadSafeISBRH.java deleted file mode 100644 index 8bdab2753..000000000 --- a/src/main/java/com/gtnewhorizons/angelica/interfaces/IThreadSafeISBRH.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.gtnewhorizons.angelica.interfaces; - -import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; - -public interface IThreadSafeISBRH extends ISimpleBlockRenderingHandler { - IThreadSafeISBRH getThreadLocal(); - -} diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderingRegistry.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderingRegistry.java index 6cc5710ed..f05936a87 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderingRegistry.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/sodium/MixinRenderingRegistry.java @@ -1,21 +1,26 @@ package com.gtnewhorizons.angelica.mixins.early.sodium; -import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH; +import com.gtnewhorizons.angelica.api.ThreadSafeISBRH; +import com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory; +import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.mixins.interfaces.IRenderingRegistryExt; import com.llamalad7.mixinextras.injector.wrapoperation.Operation; import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; import cpw.mods.fml.client.registry.RenderingRegistry; +import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; +import java.lang.reflect.InvocationTargetException; import java.util.Map; @Mixin(value = RenderingRegistry.class, remap = false) public class MixinRenderingRegistry implements IRenderingRegistryExt { @Shadow private Map blockRenderers; + private ThreadLocal, Object>> THREAD_LOCAL_MAP = ThreadLocal.withInitial(Reference2ObjectOpenHashMap::new); @Override public ISimpleBlockRenderingHandler getISBRH(int modelId) { return this.blockRenderers.get(modelId); @@ -23,10 +28,27 @@ public class MixinRenderingRegistry implements IRenderingRegistryExt { @WrapOperation(method = { "renderWorldBlock", "renderInventoryBlock", "renderItemAsFull3DBlock" }, at = @At(value="INVOKE", target="Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;")) private Object getWrapped(Map instance, Object modelId, Operation original) { - final ISimpleBlockRenderingHandler res = original.call(instance, modelId); - if(res instanceof IThreadSafeISBRH isbhr) { - return isbhr.getThreadLocal(); + // TODO: Move this to BlockRenderer + + // Get the main thread handler + final ISimpleBlockRenderingHandler mainThreadHandler = original.call(instance, modelId); + if(Thread.currentThread() != GLStateManager.getMainThread()) { + ThreadSafeISBRH annotation = mainThreadHandler.getClass().getAnnotation(ThreadSafeISBRH.class); + if (annotation != null && annotation.perThread()) { + return THREAD_LOCAL_MAP.get().computeIfAbsent(mainThreadHandler.getClass(), k -> { + try { + // Won't work with non-default constructors, use ThreadSafeISBRHFactory instead + return mainThreadHandler.getClass().getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) { + throw new RuntimeException(e); + } catch(InvocationTargetException e) { + throw new RuntimeException(e.getCause()); + } + }); + } else if (mainThreadHandler.getClass().isInstance(ThreadSafeISBRHFactory.class)) { + return THREAD_LOCAL_MAP.get().computeIfAbsent(mainThreadHandler.getClass(), k -> ((ThreadSafeISBRHFactory) mainThreadHandler).newInstance()); + } } - return res; + return mainThreadHandler; } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/AngelicaBlockSafetyRegistry.java b/src/main/java/com/gtnewhorizons/angelica/rendering/AngelicaBlockSafetyRegistry.java index 5a5f2d58a..69e1a726d 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/AngelicaBlockSafetyRegistry.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/AngelicaBlockSafetyRegistry.java @@ -1,10 +1,10 @@ package com.gtnewhorizons.angelica.rendering; -import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH; +import com.gtnewhorizons.angelica.api.ThreadSafeISBRH; +import com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory; import com.gtnewhorizons.angelica.mixins.interfaces.IRenderingRegistryExt; +import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler; import cpw.mods.fml.client.registry.RenderingRegistry; -import cpw.mods.fml.common.Loader; -import cpw.mods.fml.common.versioning.ComparableVersion; import it.unimi.dsi.fastutil.objects.Reference2BooleanMap; import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap; import net.minecraft.block.Block; @@ -39,8 +39,15 @@ public class AngelicaBlockSafetyRegistry { } private static boolean populateCanRenderOffThread(Block block, Reference2BooleanMap map) { - @SuppressWarnings("deprecation") - final boolean canBeOffThread = map == ISBRH_SAFETY_MAP ? ((IRenderingRegistryExt)RenderingRegistry.instance()).getISBRH(block.getRenderType()) instanceof IThreadSafeISBRH : !(block.getClass().getName().startsWith("gregtech.")); + final boolean canBeOffThread; + if(map == ISBRH_SAFETY_MAP) { + @SuppressWarnings("deprecation") + final ISimpleBlockRenderingHandler isbrh = ((IRenderingRegistryExt)RenderingRegistry.instance()).getISBRH(block.getRenderType()); + canBeOffThread = isbrh.getClass().isAnnotationPresent(ThreadSafeISBRH.class) + || isbrh instanceof ThreadSafeISBRHFactory; + } else { + canBeOffThread = !(block.getClass().getName().startsWith("gregtech.")); + } final long stamp = LOCK.writeLock();