Skip to content

Commit

Permalink
More allocation rate patches
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Mar 25, 2024
1 parent 05fd617 commit 9b0d314
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.embeddedt.vintagefix.mixin.allocation_rate;

import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.pipeline.VertexBufferConsumer;
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(value = VertexBufferConsumer.class, remap = false)
@ClientOnlyMixin
public interface AccessorVertexBufferConsumer {
@Accessor("offset")
void vintage$setOffset(BlockPos offset);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.embeddedt.vintagefix.mixin.allocation_rate;

import net.minecraft.block.state.IBlockState;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(value = ChunkCache.class ,priority = 500)
@ClientOnlyMixin
public abstract class MixinChunkCache {
@Shadow
protected World world;

@Shadow
public abstract IBlockState getBlockState(BlockPos pos);

@Shadow
public abstract int getLightFor(EnumSkyBlock type, BlockPos pos);

@Shadow
protected int chunkX;

@Shadow
protected int chunkZ;

@Shadow
protected abstract boolean withinBounds(int x, int z);

@Shadow
protected Chunk[][] chunkArray;

/**
* @author embeddedt
* @reason reduce BlockPos allocations when checking neighbor brightness
*/
@Overwrite
private int getLightForExt(EnumSkyBlock type, BlockPos pos) {
if (type == EnumSkyBlock.SKY && !this.world.provider.hasSkyLight()) {
return 0;
} else if (pos.getY() >= 0 && pos.getY() < 256) {
if (this.getBlockState(pos).useNeighborBrightness()) {
// VintageFix change: use mutable BlockPos so we only allocate once
BlockPos.MutableBlockPos offsetPos = new BlockPos.MutableBlockPos();
int l = 0;

for (EnumFacing enumfacing : EnumFacing.VALUES) {
offsetPos.setPos(pos.getX() + enumfacing.getXOffset(), pos.getY() + enumfacing.getYOffset(), pos.getZ() + enumfacing.getZOffset());
int k = this.getLightFor(type, offsetPos);

if (k > l) {
l = k;
}

if (l >= 15) {
return l;
}
}

return l;
} else {
int i = (pos.getX() >> 4) - this.chunkX;
int j = (pos.getZ() >> 4) - this.chunkZ;
if (!withinBounds(i, j)) return type.defaultLightValue;
return this.chunkArray[i][j].getLightFor(type, pos);
}
} else {
return type.defaultLightValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.embeddedt.vintagefix.mixin.allocation_rate;

import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.pipeline.ForgeBlockModelRenderer;
import net.minecraftforge.client.model.pipeline.VertexBufferConsumer;
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(ForgeBlockModelRenderer.class)
@ClientOnlyMixin
public class MixinForgeBlockModelRenderer {
@Redirect(method = "*", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/model/pipeline/VertexBufferConsumer;setOffset(Lnet/minecraft/util/math/BlockPos;)V"), require = 0)
private void setOffsetWithoutAlloc(VertexBufferConsumer consumer, BlockPos offset) {
((AccessorVertexBufferConsumer)consumer).vintage$setOffset(offset);
}
}

0 comments on commit 9b0d314

Please sign in to comment.