Skip to content

Commit 2e52db6

Browse files
committed
Apply some simple optimizations for vanilla section meshing
1 parent ece2897 commit 2e52db6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.embeddedt.modernfix.common.mixin.perf.chunk_meshing;
2+
3+
import com.llamalad7.mixinextras.sugar.Local;
4+
import net.minecraft.client.renderer.chunk.RenderChunkRegion;
5+
import net.minecraft.core.BlockPos;
6+
import net.minecraft.world.level.block.state.BlockState;
7+
import org.embeddedt.modernfix.util.blockpos.SectionBlockPosIterator;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Redirect;
11+
12+
@Mixin(targets = { "net/minecraft/client/renderer/chunk/ChunkRenderDispatcher$RenderChunk$RebuildTask"}, priority = 2000)
13+
public class RebuildTaskMixin {
14+
/**
15+
* @author embeddedt
16+
* @reason Use a much faster iterator implementation than vanilla's Guava-based one.
17+
*/
18+
@Redirect(method = "compile", at = @At(value = "INVOKE", target = "Lnet/minecraft/core/BlockPos;betweenClosed(Lnet/minecraft/core/BlockPos;Lnet/minecraft/core/BlockPos;)Ljava/lang/Iterable;"))
19+
private Iterable<BlockPos> fastBetweenClosed(BlockPos firstPos, BlockPos secondPos) {
20+
return () -> new SectionBlockPosIterator(firstPos);
21+
}
22+
23+
/**
24+
* @author embeddedt
25+
* @reason RenderChunkRegion.getBlockState is expensive, avoid calling it multiple times for the same position
26+
*/
27+
@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))
28+
private BlockState useExistingBlockState(RenderChunkRegion instance, BlockPos pos, @Local(ordinal = 0) BlockState state) {
29+
return state;
30+
}
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.embeddedt.modernfix.util.blockpos;
2+
3+
import net.minecraft.core.BlockPos;
4+
5+
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
7+
8+
public class SectionBlockPosIterator implements Iterator<BlockPos> {
9+
private final BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
10+
private int index = 0;
11+
private final int baseX, baseY, baseZ;
12+
13+
public SectionBlockPosIterator(int baseX, int baseY, int baseZ) {
14+
this.baseX = baseX;
15+
this.baseY = baseY;
16+
this.baseZ = baseZ;
17+
}
18+
19+
public SectionBlockPosIterator(BlockPos pos) {
20+
this(pos.getX(), pos.getY(), pos.getZ());
21+
}
22+
23+
@Override
24+
public boolean hasNext() {
25+
return index < 4096;
26+
}
27+
28+
@Override
29+
public BlockPos next() {
30+
int i = index;
31+
if (i >= 4096) {
32+
throw new NoSuchElementException();
33+
}
34+
index = i + 1;
35+
var pos = this.pos;
36+
pos.set(this.baseX + (i & 15), this.baseY + ((i >> 8) & 15), this.baseZ + ((i >> 4) & 15));
37+
return pos;
38+
}
39+
}

0 commit comments

Comments
 (0)