From 46313ea5c2d7a81aaaca91687b6c09fd82fdf5e3 Mon Sep 17 00:00:00 2001 From: IMS212 Date: Thu, 11 Jul 2024 16:03:38 -0700 Subject: [PATCH] Changes to model data handling and improved meshing performance on Forge --- .../services/SodiumModelDataContainer.java | 6 ++++++ .../mods/sodium/client/world/LevelSlice.java | 20 ++++++++++++++----- .../world/cloned/ChunkRenderContext.java | 8 +------- .../world/cloned/ClonedChunkSection.java | 10 ++++++++-- .../neoforge/block/NeoForgeBlockAccess.java | 2 +- .../neoforge/mixin/LevelSliceMixin.java | 3 --- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/services/SodiumModelDataContainer.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/services/SodiumModelDataContainer.java index aad1e14674..acd20d8f67 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/services/SodiumModelDataContainer.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/services/SodiumModelDataContainer.java @@ -10,12 +10,18 @@ */ public class SodiumModelDataContainer { private final Long2ObjectMap modelDataMap; + private final boolean isEmpty; public SodiumModelDataContainer(Long2ObjectMap modelDataMap) { this.modelDataMap = modelDataMap; + this.isEmpty = modelDataMap.isEmpty(); } public SodiumModelData getModelData(BlockPos pos) { return modelDataMap.getOrDefault(pos.asLong(), SodiumModelData.EMPTY); } + + public boolean isEmpty() { + return isEmpty; + } } diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/LevelSlice.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/LevelSlice.java index 9b5afa4ecc..500c728de2 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/LevelSlice.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/LevelSlice.java @@ -99,7 +99,7 @@ public final class LevelSlice implements BlockAndTintGetter, RenderAttachedBlock private final @Nullable Int2ReferenceMap[] blockEntityRenderDataArrays; // (Local Section -> Model Data) table. - private @Nullable SodiumModelDataContainer modelDataSnapshot; + private final SodiumModelDataContainer[] modelMapArrays; // The starting point from which this slice captures blocks private int originBlockX, originBlockY, originBlockZ; @@ -145,10 +145,9 @@ public static ChunkRenderContext prepare(Level level, SectionPos pos, ClonedChun } } - SodiumModelDataContainer modelData = PlatformModelAccess.getInstance().getModelDataContainer(level, pos); List renderers = PlatformLevelAccess.getInstance().getExtraRenderers(level, pos.origin()); - return new ChunkRenderContext(pos, sections, box, modelData, renderers); + return new ChunkRenderContext(pos, sections, box, renderers); } @SuppressWarnings("unchecked") @@ -161,6 +160,7 @@ public LevelSlice(ClientLevel level) { this.blockEntityArrays = new Int2ReferenceMap[SECTION_ARRAY_SIZE]; this.blockEntityRenderDataArrays = new Int2ReferenceMap[SECTION_ARRAY_SIZE]; this.auxLightManager = new Object[SECTION_ARRAY_SIZE]; + this.modelMapArrays = new SodiumModelDataContainer[SECTION_ARRAY_SIZE]; this.biomeSlice = new LevelBiomeSlice(); this.biomeColors = new LevelColorCache(this.biomeSlice, Minecraft.getInstance().options.biomeBlendRadius().get()); @@ -176,7 +176,6 @@ public void copyData(ChunkRenderContext context) { this.originBlockZ = SectionPos.sectionToBlockCoord(context.getOrigin().getZ() - NEIGHBOR_CHUNK_RADIUS); this.volume = context.getVolume(); - this.modelDataSnapshot = context.getModelData(); for (int x = 0; x < SECTION_ARRAY_LENGTH; x++) { for (int y = 0; y < SECTION_ARRAY_LENGTH; y++) { @@ -203,6 +202,7 @@ private void copySectionData(ChunkRenderContext context, int sectionIndex) { this.blockEntityArrays[sectionIndex] = section.getBlockEntityMap(); this.auxLightManager[sectionIndex] = section.getAuxLightManager(); this.blockEntityRenderDataArrays[sectionIndex] = section.getBlockEntityRenderDataMap(); + this.modelMapArrays[sectionIndex] = section.getModelMap(); } private void unpackBlockData(BlockState[] blockArray, ChunkRenderContext context, ClonedChunkSection section) { @@ -389,7 +389,17 @@ public SodiumModelData getPlatformModelData(BlockPos pos) { return SodiumModelData.EMPTY; } - return modelDataSnapshot.getModelData(pos); + int relBlockX = pos.getX() - this.originBlockX; + int relBlockY = pos.getY() - this.originBlockY; + int relBlockZ = pos.getZ() - this.originBlockZ; + + var modelMap = this.modelMapArrays[getLocalSectionIndex(relBlockX >> 4, relBlockY >> 4, relBlockZ >> 4)]; + + if (modelMap.isEmpty()) { + return SodiumModelData.EMPTY; + } + + return modelMap.getModelData(pos); } //@Override diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ChunkRenderContext.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ChunkRenderContext.java index f1b26eac8a..0242748dcc 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ChunkRenderContext.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ChunkRenderContext.java @@ -10,14 +10,12 @@ public class ChunkRenderContext { private final SectionPos origin; private final ClonedChunkSection[] sections; private final BoundingBox volume; - private final SodiumModelDataContainer modelData; private final List renderers; - public ChunkRenderContext(SectionPos origin, ClonedChunkSection[] sections, BoundingBox volume, SodiumModelDataContainer modelData, List renderers) { + public ChunkRenderContext(SectionPos origin, ClonedChunkSection[] sections, BoundingBox volume, List renderers) { this.origin = origin; this.sections = sections; this.volume = volume; - this.modelData = modelData; this.renderers = renderers; } @@ -33,10 +31,6 @@ public BoundingBox getVolume() { return this.volume; } - public SodiumModelDataContainer getModelData() { - return modelData; - } - public List getRenderers() { return renderers; } diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ClonedChunkSection.java b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ClonedChunkSection.java index 7a9e332afd..aef6ace4ed 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ClonedChunkSection.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/client/world/cloned/ClonedChunkSection.java @@ -3,8 +3,7 @@ import it.unimi.dsi.fastutil.ints.Int2ReferenceMap; import it.unimi.dsi.fastutil.ints.Int2ReferenceMaps; import it.unimi.dsi.fastutil.ints.Int2ReferenceOpenHashMap; -import net.caffeinemc.mods.sodium.client.services.PlatformBlockAccess; -import net.caffeinemc.mods.sodium.client.services.PlatformLevelAccess; +import net.caffeinemc.mods.sodium.client.services.*; import net.caffeinemc.mods.sodium.client.world.PalettedContainerROExtension; import net.caffeinemc.mods.sodium.client.world.LevelSlice; import net.minecraft.core.BlockPos; @@ -45,6 +44,7 @@ public class ClonedChunkSection { private final @Nullable PalettedContainerRO blockData; private final @Nullable PalettedContainerRO> biomeData; + private final SodiumModelDataContainer modelMap; private long lastUsedTimestamp = Long.MAX_VALUE; @@ -56,6 +56,7 @@ public ClonedChunkSection(Level level, LevelChunk chunk, @Nullable LevelChunkSec Int2ReferenceMap blockEntityMap = null; Int2ReferenceMap blockEntityRenderDataMap = null; + SodiumModelDataContainer modelMap = PlatformModelAccess.getInstance().getModelDataContainer(level, pos); auxLightManager = PlatformLevelAccess.INSTANCE.getLightManager(chunk, pos); if (section != null) { @@ -76,6 +77,7 @@ public ClonedChunkSection(Level level, LevelChunk chunk, @Nullable LevelChunkSec this.blockData = blockData; this.biomeData = biomeData; + this.modelMap = modelMap; this.blockEntityMap = blockEntityMap; this.blockEntityRenderDataMap = blockEntityRenderDataMap; @@ -222,6 +224,10 @@ public SectionPos getPosition() { return this.blockEntityRenderDataMap; } + public SodiumModelDataContainer getModelMap() { + return modelMap; + } + public @Nullable DataLayer getLightArray(LightLayer lightType) { return this.lightDataArrays[lightType.ordinal()]; } diff --git a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/block/NeoForgeBlockAccess.java b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/block/NeoForgeBlockAccess.java index b44421f2dd..71d2687991 100644 --- a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/block/NeoForgeBlockAccess.java +++ b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/block/NeoForgeBlockAccess.java @@ -32,7 +32,7 @@ public int getLightEmission(BlockState state, BlockAndTintGetter level, BlockPos @Override public boolean shouldSkipRender(BlockGetter level, BlockState selfState, BlockState otherState, BlockPos selfPos, Direction facing) { - return selfState.supportsExternalFaceHiding() && (otherState.hidesNeighborFace(level, selfPos, selfState, DirectionUtil.getOpposite(facing))); + return (otherState.hidesNeighborFace(level, selfPos, otherState, DirectionUtil.getOpposite(facing))) && selfState.supportsExternalFaceHiding(); } @Override diff --git a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/LevelSliceMixin.java b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/LevelSliceMixin.java index 4263db5fa8..42e32222cb 100644 --- a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/LevelSliceMixin.java +++ b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/LevelSliceMixin.java @@ -24,9 +24,6 @@ public abstract class LevelSliceMixin implements BlockAndTintGetter { @Final private Object[] auxLightManager; - @Shadow - private @Nullable SodiumModelDataContainer modelDataSnapshot; - @Shadow @Final private ClientLevel level;