Off thread safe blocks (#177)

* Whitelist carpentersblocks for off main thread rendering

* Add bounds checking to WorldSlice,

* Add preliminary `IThreadSafeISBRH` support

* Update TODO

* Adjust min y

* remove unnecessary try/catch

* * Keep track of what should be rendered on the main thread
* Update AngelicaBlockSafetyRegistry to handle vanilla blocks, and ISBHR's

* of -> set
This commit is contained in:
Jason Mitchell authored and GitHub committed 2024-01-12 18:31:01 -08:00
1 parent 537eb06b66
commit c211d90faf
9 files changed
+145 -58

No files matched your search

@@ -80,6 +80,9 @@ public class BlockPos extends Vector3i {
return l;
}
public BlockPos set(long packedPos) {
return set(unpackLongX(packedPos), unpackLongY(packedPos), unpackLongZ(packedPos));
}
public static int unpackLongX(long packedPos) {
return (int)(packedPos << 64 - BIT_SHIFT_X - SIZE_BITS_X >> 64 - SIZE_BITS_X);
@@ -0,0 +1,8 @@
package com.gtnewhorizons.angelica.interfaces;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
public interface IThreadSafeISBRH extends ISimpleBlockRenderingHandler {
IThreadSafeISBRH getThreadLocal();
}
@@ -87,7 +87,7 @@ public enum Mixins {
SODIUM(new Builder("Sodium").addTargetedMod(TargetedMod.VANILLA).setSide(Side.CLIENT)
.setPhase(Phase.EARLY).setApplyIf(() -> AngelicaConfig.enableSodium).addMixinClasses(
"sodium.MixinChunkProviderClient"
"sodium.MixinChunkProviderClient"
,"sodium.MixinBlock"
,"sodium.AccessorBiomeColorEvent"
,"sodium.MixinBiomeGenBase"
@@ -113,6 +113,7 @@ public enum Mixins {
,"sodium.MixinTileEntityRendererDispatcher"
,"sodium.MixinLongHashMap"
,"sodium.MixinRender"
,"sodium.MixinRenderingRegistry"
)
),
@@ -0,0 +1,32 @@
package com.gtnewhorizons.angelica.mixins.early.sodium;
import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH;
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 org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import java.util.Map;
@Mixin(value = RenderingRegistry.class, remap = false)
public class MixinRenderingRegistry implements IRenderingRegistryExt {
@Shadow private Map<Integer, ISimpleBlockRenderingHandler> blockRenderers;
@Override
public ISimpleBlockRenderingHandler getISBRH(int modelId) {
return this.blockRenderers.get(modelId);
}
@WrapOperation(method = { "renderWorldBlock", "renderInventoryBlock", "renderItemAsFull3DBlock" }, at = @At(value="INVOKE", target="Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
private Object getWrapped(Map<Integer, ISimpleBlockRenderingHandler> instance, Object modelId, Operation<ISimpleBlockRenderingHandler> original) {
final ISimpleBlockRenderingHandler res = original.call(instance, modelId);
if(res instanceof IThreadSafeISBRH isbhr) {
return isbhr.getThreadLocal();
}
return res;
}
}
@@ -0,0 +1,7 @@
package com.gtnewhorizons.angelica.mixins.interfaces;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
public interface IRenderingRegistryExt {
ISimpleBlockRenderingHandler getISBRH(int modelId);
}
@@ -1,5 +1,10 @@
package com.gtnewhorizons.angelica.rendering;
import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH;
import com.gtnewhorizons.angelica.mixins.interfaces.IRenderingRegistryExt;
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;
@@ -7,41 +12,45 @@ import net.minecraft.block.Block;
import java.util.concurrent.locks.StampedLock;
public class AngelicaBlockSafetyRegistry {
private static final Reference2BooleanMap<Block> SAFETY_MAP = new Reference2BooleanOpenHashMap<>();
private static final Reference2BooleanMap<Block> BLOCK_SAFETY_MAP = new Reference2BooleanOpenHashMap<>();
private static final Reference2BooleanMap<Block> ISBRH_SAFETY_MAP = new Reference2BooleanOpenHashMap<>();
private static final StampedLock LOCK = new StampedLock();
public static boolean canBlockRenderOffThread(Block block) {
public static boolean canBlockRenderOffThread(Block block, boolean checkISBRH) {
final long stamp = LOCK.readLock();
final Reference2BooleanMap<Block> map = checkISBRH ? ISBRH_SAFETY_MAP : BLOCK_SAFETY_MAP;
boolean isOffThread, shouldPopulate;
try {
isOffThread = SAFETY_MAP.getBoolean(block);
isOffThread = map.getBoolean(block);
if (isOffThread) {
return true; // no need to check if 'false' was due to not being populated
}
shouldPopulate = !SAFETY_MAP.containsKey(block);
shouldPopulate = !map.containsKey(block);
} finally {
LOCK.unlock(stamp);
}
if(shouldPopulate) {
isOffThread = populateCanRenderOffThread(block);
isOffThread = populateCanRenderOffThread(block, map);
}
return isOffThread;
}
private static boolean populateCanRenderOffThread(Block block) {
final boolean canBeOffThread = !(block.getClass().getName().startsWith("gregtech."));
private static boolean populateCanRenderOffThread(Block block, Reference2BooleanMap<Block> map) {
@SuppressWarnings("deprecation")
final boolean canBeOffThread = map == ISBRH_SAFETY_MAP ? ((IRenderingRegistryExt)RenderingRegistry.instance()).getISBRH(block.getRenderType()) instanceof IThreadSafeISBRH : !(block.getClass().getName().startsWith("gregtech."));
final long stamp = LOCK.writeLock();
try {
SAFETY_MAP.put(block, canBeOffThread);
map.put(block, canBeOffThread);
} finally {
LOCK.unlock(stamp);
}
return canBeOffThread;
}
}