diff --git a/src/main/java/com/gtnewhorizons/angelica/event/ChunkBiomeDataChangedEvent.java b/src/main/java/com/gtnewhorizons/angelica/event/ChunkBiomeDataChangedEvent.java new file mode 100644 index 000000000..a3a6c8fbc --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/event/ChunkBiomeDataChangedEvent.java @@ -0,0 +1,21 @@ +package com.gtnewhorizons.angelica.event; + +import net.minecraftforge.eventbus.api.bus.EventBus; +import net.minecraftforge.eventbus.api.event.MutableEvent; + +public final class ChunkBiomeDataChangedEvent extends MutableEvent { + + public static final EventBus BUS = EventBus.create(ChunkBiomeDataChangedEvent.class); + + private static final ChunkBiomeDataChangedEvent INSTANCE = new ChunkBiomeDataChangedEvent(); + + public int chunkX; + public int chunkZ; + + public static void post(int chunkX, int chunkZ) { + if (!BUS.hasListeners()) return; + INSTANCE.chunkX = chunkX; + INSTANCE.chunkZ = chunkZ; + BUS.post(INSTANCE); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java index 5171d6794..e31a2fc24 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/AngelicaRenderSectionManager.java @@ -7,6 +7,8 @@ import com.gtnewhorizons.angelica.rendering.celeritas.api.IrisShaderProviderHold import com.gtnewhorizons.angelica.rendering.celeritas.threading.ChunkTaskProvider; import com.gtnewhorizons.angelica.rendering.celeritas.threading.ChunkTaskRegistry; import com.gtnewhorizons.angelica.rendering.celeritas.world.cloned.ClonedChunkSectionCache; +import it.unimi.dsi.fastutil.longs.LongIterator; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.renderer.texture.TextureAtlasSprite; @@ -40,6 +42,7 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { private final WorldClient world; private final ClonedChunkSectionCache sectionCache; private final ChunkTaskProvider taskProvider; + private final LongOpenHashSet biomeRebuildColumns = new LongOpenHashSet(); public AngelicaRenderSectionManager(RenderPassConfiguration configuration, WorldClient world, int renderDistance, CommandList commandList, int minSection, int maxSection, int requestedThreads, ChunkTaskProvider taskProvider) { super(configuration, () -> new AngelicaChunkBuildContext(configuration, world), AngelicaChunkRenderer::new, renderDistance, commandList, minSection, maxSection, requestedThreads, true /* hasShadowPass = true for Iris */); @@ -144,9 +147,35 @@ public class AngelicaRenderSectionManager extends RenderSectionManager { this.sectionCache.invalidate(section.getChunkX(), section.getChunkY(), section.getChunkZ()); } + public void onBiomesChanged(int chunkX, int chunkZ) { + for (int dx = -1; dx <= 1; dx++) { + for (int dz = -1; dz <= 1; dz++) { + this.biomeRebuildColumns.add(PositionUtil.packChunk(chunkX + dx, chunkZ + dz)); + } + } + } + + private void flushBiomeRebuilds() { + if (this.biomeRebuildColumns.isEmpty()) { + return; + } + final int sectionCountY = this.world.getHeight() >> 4; + for (final LongIterator it = this.biomeRebuildColumns.iterator(); it.hasNext(); ) { + final long column = it.nextLong(); + final int cx = PositionUtil.unpackChunkX(column); + final int cz = PositionUtil.unpackChunkZ(column); + for (int cy = 0; cy < sectionCountY; cy++) { + this.sectionCache.invalidate(cx, cy, cz); + this.scheduleRebuild(cx, cy, cz, false); + } + } + this.biomeRebuildColumns.clear(); + } + @Override public void updateChunks(boolean updateImmediately) { this.sectionCache.cleanup(); + this.flushBiomeRebuilds(); super.updateChunks(updateImmediately); } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasSetup.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasSetup.java index ac05fd4e4..9253a2fa3 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasSetup.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasSetup.java @@ -1,6 +1,7 @@ package com.gtnewhorizons.angelica.rendering.celeritas; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.event.ChunkBiomeDataChangedEvent; import com.gtnewhorizons.angelica.glsm.GLStateManager; import com.gtnewhorizons.angelica.loading.AngelicaClientTweaker; import org.embeddedt.embeddium.impl.gl.device.GLRenderDevice; @@ -12,8 +13,16 @@ public class CeleritasSetup { public static void ensureInitialized() { if (!initialized && AngelicaConfig.enableCeleritas) { GLRenderDevice.VANILLA_STATE_RESETTER = () -> GLStateManager.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + ChunkBiomeDataChangedEvent.BUS.addListener(CeleritasSetup::onChunkBiomeDataChanged); AngelicaClientTweaker.LOGGER.debug("Celeritas init"); initialized = true; } } + + private static void onChunkBiomeDataChanged(ChunkBiomeDataChangedEvent event) { + final CeleritasWorldRenderer renderer = CeleritasWorldRenderer.getInstanceOrNull(); + if (renderer != null && renderer.isActive()) { + renderer.getRenderSectionManager().onBiomesChanged(event.chunkX, event.chunkZ); + } + } } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java index 00e57a22a..fe7612979 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/CeleritasWorldRenderer.java @@ -74,6 +74,10 @@ public class CeleritasWorldRenderer extends SimpleWorldRenderer { + map.readLock(); + try { final Object2ObjectOpenHashMap delegate = map.getDelegate(); - if (delegate.isEmpty()) return; + if (!delegate.isEmpty()) { + final int minY = pos.getMinY(); + final int maxY = pos.getMaxY(); - final int minY = pos.getMinY(); - final int maxY = pos.getMaxY(); + for (Object2ObjectMap.Entry entry : Object2ObjectMaps.fastIterable(delegate)) { + final ChunkPosition tePos = entry.getKey(); + if (tePos.chunkPosY < minY || tePos.chunkPosY > maxY) continue; - for (Object2ObjectMap.Entry entry : Object2ObjectMaps.fastIterable(delegate)) { - final ChunkPosition tePos = entry.getKey(); - if (tePos.chunkPosY < minY || tePos.chunkPosY > maxY) continue; - - final TileEntity te = entry.getValue(); - if (te != null && !te.isInvalid()) { - this.tileEntities.put(ChunkSectionPos.packLocal(tePos.chunkPosX & 15, tePos.chunkPosY & 15, tePos.chunkPosZ & 15), te); + final TileEntity te = entry.getValue(); + if (te != null && !te.isInvalid()) { + this.tileEntities.put(ChunkSectionPos.packLocal(tePos.chunkPosX & 15, tePos.chunkPosY & 15, tePos.chunkPosZ & 15), te); + } } } - }); + } finally { + map.readUnlock(); + } this.tileEntities.trim(); } diff --git a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSectionCache.java b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSectionCache.java index 807e8f78e..78e4a27b2 100644 --- a/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSectionCache.java +++ b/src/main/java/com/gtnewhorizons/angelica/rendering/celeritas/world/cloned/ClonedChunkSectionCache.java @@ -21,7 +21,13 @@ public class ClonedChunkSectionCache { public synchronized void cleanup() { this.time = getMonotonicTimeSource(); - this.byPosition.values().removeIf(entry -> this.time > (entry.getLastUsedTimestamp() + MAX_CACHE_DURATION)); + while (!this.byPosition.isEmpty()) { + final ClonedChunkSection oldest = this.byPosition.get(this.byPosition.firstLongKey()); + if (this.time <= oldest.getLastUsedTimestamp() + MAX_CACHE_DURATION) { + break; + } + this.byPosition.removeFirst(); + } } public synchronized ClonedChunkSection acquire(int x, int y, int z) { @@ -32,18 +38,17 @@ public class ClonedChunkSectionCache { while (this.byPosition.size() >= MAX_CACHE_SIZE) { this.byPosition.removeFirst(); } - section = this.createSection(x, y, z); + section = this.createSection(key, x, y, z); } section.setLastUsedTimestamp(this.time); return section; } - private ClonedChunkSection createSection(int x, int y, int z) { + private ClonedChunkSection createSection(long key, int x, int y, int z) { final ClonedChunkSection section = new ClonedChunkSection(this, this.world); - final ChunkSectionPos pos = ChunkSectionPos.from(x, y, z); - section.init(pos); - this.byPosition.putAndMoveToLast(pos.asLong(), section); + section.init(ChunkSectionPos.from(x, y, z)); + this.byPosition.putAndMoveToLast(key, section); return section; } diff --git a/src/main/java/com/gtnewhorizons/angelica/utils/ConcurrentTileEntityMap.java b/src/main/java/com/gtnewhorizons/angelica/utils/ConcurrentTileEntityMap.java index ddd23a499..cdcc621c2 100644 --- a/src/main/java/com/gtnewhorizons/angelica/utils/ConcurrentTileEntityMap.java +++ b/src/main/java/com/gtnewhorizons/angelica/utils/ConcurrentTileEntityMap.java @@ -42,6 +42,14 @@ public class ConcurrentTileEntityMap implements Map { } } + public void readLock() { + lock.readLock().lock(); + } + + public void readUnlock() { + lock.readLock().unlock(); + } + public void withWriteLock(Runnable action) { lock.writeLock().lock(); try { diff --git a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinChunk.java b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinChunk.java index 2adaae84f..359f2edda 100644 --- a/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinChunk.java +++ b/src/mixin/java/com/gtnewhorizons/angelica/mixins/early/celeritas/terrain/MixinChunk.java @@ -2,6 +2,8 @@ package com.gtnewhorizons.angelica.mixins.early.celeritas.terrain; import java.util.Map; +import com.gtnewhorizon.gtnhlib.hash.Fnv1a64; +import com.gtnewhorizons.angelica.event.ChunkBiomeDataChangedEvent; import com.gtnewhorizons.angelica.mixins.interfaces.IChunkTileEntityMapHolder; import com.gtnewhorizons.angelica.rendering.RenderThreadContext; import com.gtnewhorizons.angelica.utils.ConcurrentTileEntityMap; @@ -9,12 +11,15 @@ import net.minecraft.block.Block; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.ChunkPosition; import net.minecraft.world.World; +import net.minecraft.world.biome.BiomeGenBase; +import net.minecraft.world.biome.WorldChunkManager; import net.minecraft.world.chunk.Chunk; import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Redirect; @@ -31,6 +36,11 @@ public abstract class MixinChunk implements IChunkTileEntityMapHolder { @Shadow public abstract void addTileEntity(TileEntity te); + @Shadow public abstract BiomeGenBase getBiomeGenForWorldCoords(int x, int z, WorldChunkManager wcm); + + @Unique private long angelica$biomeHash; + @Unique private boolean angelica$biomeHashValid; + @Override public ConcurrentTileEntityMap angelica$getConcurrentTEMap() { return (ConcurrentTileEntityMap) this.chunkTileEntityMap; @@ -116,4 +126,30 @@ public abstract class MixinChunk implements IChunkTileEntityMapHolder { } }); } + + @Inject(method = "fillChunk", at = @At("RETURN")) + private void angelica$onBiomesFilled(byte[] data, int primaryBitMask, int addBitMask, boolean groundUp, CallbackInfo ci) { + if (!groundUp || !this.worldObj.isRemote) return; + final long hash = angelica$hashBiomes(); + if (!this.angelica$biomeHashValid) { + this.angelica$biomeHashValid = true; + this.angelica$biomeHash = hash; + return; + } + if (hash == this.angelica$biomeHash) return; + this.angelica$biomeHash = hash; + ChunkBiomeDataChangedEvent.post(this.xPosition, this.zPosition); + } + + @Unique + private long angelica$hashBiomes() { + final WorldChunkManager wcm = this.worldObj.getWorldChunkManager(); + long hash = Fnv1a64.initialState(); + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + hash = Fnv1a64.hashStep(hash, getBiomeGenForWorldCoords(x, z, wcm).biomeID); + } + } + return hash; + } }