Skip to content

Commit

Permalink
Changes to model data handling and improved meshing performance on Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Jul 11, 2024
1 parent 62f3e36 commit 46313ea
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
*/
public class SodiumModelDataContainer {
private final Long2ObjectMap<SodiumModelData> modelDataMap;
private final boolean isEmpty;

public SodiumModelDataContainer(Long2ObjectMap<SodiumModelData> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public final class LevelSlice implements BlockAndTintGetter, RenderAttachedBlock
private final @Nullable Int2ReferenceMap<Object>[] 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;
Expand Down Expand Up @@ -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")
Expand All @@ -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());
Expand All @@ -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++) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -33,10 +31,6 @@ public BoundingBox getVolume() {
return this.volume;
}

public SodiumModelDataContainer getModelData() {
return modelData;
}

public List<?> getRenderers() {
return renderers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +44,7 @@ public class ClonedChunkSection {
private final @Nullable PalettedContainerRO<BlockState> blockData;

private final @Nullable PalettedContainerRO<Holder<Biome>> biomeData;
private final SodiumModelDataContainer modelMap;

private long lastUsedTimestamp = Long.MAX_VALUE;

Expand All @@ -56,6 +56,7 @@ public ClonedChunkSection(Level level, LevelChunk chunk, @Nullable LevelChunkSec

Int2ReferenceMap<BlockEntity> blockEntityMap = null;
Int2ReferenceMap<Object> blockEntityRenderDataMap = null;
SodiumModelDataContainer modelMap = PlatformModelAccess.getInstance().getModelDataContainer(level, pos);
auxLightManager = PlatformLevelAccess.INSTANCE.getLightManager(chunk, pos);

if (section != null) {
Expand All @@ -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;
Expand Down Expand Up @@ -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()];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public abstract class LevelSliceMixin implements BlockAndTintGetter {
@Final
private Object[] auxLightManager;

@Shadow
private @Nullable SodiumModelDataContainer modelDataSnapshot;

@Shadow
@Final
private ClientLevel level;
Expand Down

0 comments on commit 46313ea

Please sign in to comment.