Apply some simple optimizations for vanilla section meshing

This commit is contained in:
embeddedt 2024-07-27 12:43:48 -04:00
parent ece2897c37
commit 2e52db6e93
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package org.embeddedt.modernfix.common.mixin.perf.chunk_meshing;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.client.renderer.chunk.RenderChunkRegion;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import org.embeddedt.modernfix.util.blockpos.SectionBlockPosIterator;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(targets = { "net/minecraft/client/renderer/chunk/ChunkRenderDispatcher$RenderChunk$RebuildTask"}, priority = 2000)
public class RebuildTaskMixin {
/**
* @author embeddedt
* @reason Use a much faster iterator implementation than vanilla's Guava-based one.
*/
@Redirect(method = "compile", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/BlockPos;betweenClosed(Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/BlockPos;)Ljava/lang/Iterable;"))
private Iterable<BlockPos> fastBetweenClosed(BlockPos firstPos, BlockPos secondPos) {
return () -> new SectionBlockPosIterator(firstPos);
}
/**
* @author embeddedt
* @reason RenderChunkRegion.getBlockState is expensive, avoid calling it multiple times for the same position
*/
@Redirect(method = "compile", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/chunk/RenderChunkRegion;getBlockState(Lnet/minecraft/core/BlockPos;)Lnet/minecraft/world/level/block/state/BlockState;", ordinal = 1))
private BlockState useExistingBlockState(RenderChunkRegion instance, BlockPos pos, @Local(ordinal = 0) BlockState state) {
return state;
}
}

View File

@ -0,0 +1,39 @@
package org.embeddedt.modernfix.util.blockpos;
import net.minecraft.core.BlockPos;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SectionBlockPosIterator implements Iterator<BlockPos> {
private final BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
private int index = 0;
private final int baseX, baseY, baseZ;
public SectionBlockPosIterator(int baseX, int baseY, int baseZ) {
this.baseX = baseX;
this.baseY = baseY;
this.baseZ = baseZ;
}
public SectionBlockPosIterator(BlockPos pos) {
this(pos.getX(), pos.getY(), pos.getZ());
}
@Override
public boolean hasNext() {
return index < 4096;
}
@Override
public BlockPos next() {
int i = index;
if (i >= 4096) {
throw new NoSuchElementException();
}
index = i + 1;
var pos = this.pos;
pos.set(this.baseX + (i & 15), this.baseY + ((i >> 8) & 15), this.baseZ + ((i >> 4) & 15));
return pos;
}
}