Performance & allocation rate optimizations (#128)

This commit is contained in:
embeddedt authored and GitHub committed 2024-01-03 22:35:05 -05:00
1 parent adb582cd1e
commit a8ccc8903a
10 files changed
+143 -75

No files matched your search

@@ -3,7 +3,8 @@ package com.gtnewhorizons.angelica.client.renderer;
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.mojang.VertexFormat;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.compat.nd.RecyclingList;
import com.gtnewhorizons.angelica.utils.ObjectPooler;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import me.jellysquid.mods.sodium.client.render.pipeline.BlockRenderer;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.BufferUtils;
@@ -24,7 +25,8 @@ import java.util.List;
@SuppressWarnings("unused")
public class CapturingTessellator extends Tessellator {
private final BlockRenderer.Flags FLAGS = new BlockRenderer.Flags(true, true, true, true);
private final RecyclingList<Quad> quadBuf = new RecyclingList<>(Quad::new);
private final ObjectPooler<Quad> quadBuf = new ObjectPooler<>(Quad::new);
private final List<Quad> collectedQuads = new ObjectArrayList<>();
// Any offset we need to the Tesselator's offset!
private final BlockPos offset = new BlockPos();
@@ -50,11 +52,13 @@ public class CapturingTessellator extends Tessellator {
for(int quadI = 0; quadI < this.vertexCount / verticesPerPrimitive; quadI++) {
final Quad quad = quadBuf.next();
final Quad quad = quadBuf.getInstance();
quad.setState(this.rawBuffer, quadI * (verticesPerPrimitive * 8), FLAGS, this.drawMode, -offset.x, -offset.y, -offset.z);
if(quad.deleted) {
quadBuf.remove();
quadBuf.releaseInstance(quad);
} else {
this.collectedQuads.add(quad);
}
}
@@ -71,11 +75,15 @@ public class CapturingTessellator extends Tessellator {
}
public List<Quad> getQuads() {
return quadBuf.getAsList();
return collectedQuads;
}
public void clearQuads() {
this.quadBuf.reset();
//noinspection ForLoopReplaceableByForEach
for(int i = 0; i < this.collectedQuads.size(); i++) {
this.quadBuf.releaseInstance(this.collectedQuads.get(i));
}
this.collectedQuads.clear();
}
public static ByteBuffer quadsToBuffer(List<Quad> quads, VertexFormat format) {
@@ -1,49 +0,0 @@
package com.gtnewhorizons.angelica.compat.nd;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class RecyclingList<T> {
// Adapted from Neodymium
private Supplier<T> constructor;
int nextIndex;
private final List<T> list;
public RecyclingList(Supplier<T> constructor) {
this.constructor = constructor;
this.list = new ArrayList<>();
}
public T get(int i) {
while(list.size() <= i) {
list.add(constructor.get());
}
return list.get(i);
}
public T next() {
return get(nextIndex++);
}
public void remove() {
if(nextIndex == 0) {
throw new IllegalStateException("Tried to remove from empty list");
}
nextIndex--;
}
public boolean isEmpty() {
return nextIndex == 0;
}
public void reset() {
nextIndex = 0;
}
public List<T> getAsList() {
return list.subList(0, nextIndex);
}
}
@@ -51,10 +51,13 @@ public class TessellatorManager {
final List<Quad> quads = tess.getQuads();
((ITessellatorInstance)tess).discard();
tess.clearQuads();
return quads;
}
public static void clearQuads() {
capturingTessellator.get().clearQuads();
}
static {
System.out.println("[TessellatorManager] Initialized on thread " + mainThread.getName());
}
@@ -88,6 +88,8 @@ public enum Mixins {
.setPhase(Phase.EARLY).setApplyIf(() -> AngelicaConfig.enableSodium).addMixinClasses(
"sodium.MixinChunkProviderClient"
,"sodium.MixinBlock"
,"sodium.AccessorBiomeColorEvent"
,"sodium.MixinBiomeGenBase"
,"sodium.MixinChunk"
,"sodium.MixinChunkProviderServer"
,"sodium.MixinEntity"
@@ -0,0 +1,11 @@
package com.gtnewhorizons.angelica.mixins.early.sodium;
import net.minecraftforge.event.terraingen.BiomeEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(BiomeEvent.BiomeColor.class)
public interface AccessorBiomeColorEvent {
@Accessor("originalColor")
void setOriginalColor(int value);
}
@@ -0,0 +1,45 @@
package com.gtnewhorizons.angelica.mixins.early.sodium;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.event.terraingen.BiomeEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(BiomeGenBase.class)
public class MixinBiomeGenBase {
private final ThreadLocal<BiomeEvent.GetWaterColor> waterColorEventLocal = ThreadLocal.withInitial(() -> new BiomeEvent.GetWaterColor((BiomeGenBase)(Object)this, 0));;
private final ThreadLocal<BiomeEvent.GetGrassColor> grassColorEventLocal = ThreadLocal.withInitial(() -> new BiomeEvent.GetGrassColor((BiomeGenBase)(Object)this, 0));;
private final ThreadLocal<BiomeEvent.GetFoliageColor> foliageColorEventLocal = ThreadLocal.withInitial(() -> new BiomeEvent.GetFoliageColor((BiomeGenBase)(Object)this, 0));
@Unique
private void prepareEvent(BiomeEvent.BiomeColor event, int defaultColor) {
event.newColor = defaultColor;
((AccessorBiomeColorEvent)event).setOriginalColor(defaultColor);
}
@Redirect(method = "getWaterColorMultiplier", at = @At(value = "NEW", target = "(Lnet/minecraft/world/biome/BiomeGenBase;I)Lnet/minecraftforge/event/terraingen/BiomeEvent$GetWaterColor;"))
private BiomeEvent.GetWaterColor memoizeWaterObject(BiomeGenBase biome, int original) {
BiomeEvent.GetWaterColor event = waterColorEventLocal.get();
prepareEvent(event, original);
return event;
}
@Redirect(method = "getModdedBiomeGrassColor", at = @At(value = "NEW", target = "(Lnet/minecraft/world/biome/BiomeGenBase;I)Lnet/minecraftforge/event/terraingen/BiomeEvent$GetGrassColor;"))
private BiomeEvent.GetGrassColor memoizeGrassObject(BiomeGenBase biome, int original) {
BiomeEvent.GetGrassColor event = grassColorEventLocal.get();
prepareEvent(event, original);
return event;
}
@Redirect(method = "getModdedBiomeFoliageColor", at = @At(value = "NEW", target = "(Lnet/minecraft/world/biome/BiomeGenBase;I)Lnet/minecraftforge/event/terraingen/BiomeEvent$GetFoliageColor;"))
private BiomeEvent.GetFoliageColor memoizeFoliageObject(BiomeGenBase biome, int original) {
BiomeEvent.GetFoliageColor event = foliageColorEventLocal.get();
prepareEvent(event, original);
return event;
}
}
@@ -0,0 +1,25 @@
package com.gtnewhorizons.angelica.utils;
import java.util.ArrayList;
import java.util.function.Supplier;
public class ObjectPooler<T> {
private final Supplier<T> instanceSupplier;
private final ArrayList<T> availableInstances;
public ObjectPooler(Supplier<T> instanceSupplier) {
this.instanceSupplier = instanceSupplier;
this.availableInstances = new ArrayList<>();
}
public T getInstance() {
if(this.availableInstances.isEmpty()) {
return this.instanceSupplier.get();
}
return this.availableInstances.remove(this.availableInstances.size() - 1);
}
public void releaseInstance(T instance) {
this.availableInstances.add(instance);
}
}
@@ -3,7 +3,6 @@ package me.jellysquid.mods.sodium.client.render.pipeline;
import com.gtnewhorizons.angelica.client.renderer.CapturingTessellator;
import com.gtnewhorizons.angelica.compat.mojang.BlockPos;
import com.gtnewhorizons.angelica.compat.nd.Quad;
import com.gtnewhorizons.angelica.compat.nd.RecyclingList;
import com.gtnewhorizons.angelica.config.AngelicaConfig;
import com.gtnewhorizons.angelica.glsm.TessellatorManager;
import me.jellysquid.mods.sodium.client.model.light.LightMode;
@@ -39,9 +38,6 @@ public class BlockRenderer {
private final LightPipelineProvider lighters;
private final Flags FLAGS = new Flags(true, true, true, false);
private final RecyclingList<Quad> quadBuf = new RecyclingList<>(Quad::new);
public BlockRenderer(LightPipelineProvider lighters) {
this.lighters = lighters;
@@ -73,6 +69,8 @@ public class BlockRenderer {
}
if (!quads.isEmpty()) rendered = true;
TessellatorManager.clearQuads();
} finally {
TessellatorManager.cleanup();
}
@@ -18,6 +18,7 @@ import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.gen.structure.StructureBoundingBox;
import net.minecraftforge.common.util.ForgeDirection;
@@ -33,6 +34,8 @@ import net.minecraftforge.common.util.ForgeDirection;
* Object pooling should be used to avoid huge allocations as this class contains many large arrays.
*/
public class WorldSlice implements IBlockAccess {
private static final EnumSkyBlock[] LIGHT_TYPES = EnumSkyBlock.values();
// The number of blocks on each axis in a section.
private static final int SECTION_BLOCK_LENGTH = 16;
@@ -65,6 +68,9 @@ public class WorldSlice implements IBlockAccess {
private final Block[][] blockArrays;
private final int[][] metadataArrays;
// Local Section->Light table
private final NibbleArray[][] lightArrays;
// Local section copies. Read-only.
private ClonedChunkSection[] sections;
@@ -76,6 +82,8 @@ public class WorldSlice implements IBlockAccess {
private final int worldHeight;
private final int[] defaultLightValues;
// The chunk origin of this slice
@Getter
private ChunkSectionPos origin;
@@ -123,17 +131,22 @@ public class WorldSlice implements IBlockAccess {
public WorldSlice(WorldClient world) {
this.world = world;
this.worldHeight = world.getHeight();
this.defaultLightValues = new int[LIGHT_TYPES.length];
this.defaultLightValues[EnumSkyBlock.Sky.ordinal()] = world.provider.hasNoSky ? 0 : EnumSkyBlock.Sky.defaultLightValue;
this.defaultLightValues[EnumSkyBlock.Block.ordinal()] = EnumSkyBlock.Block.defaultLightValue;
this.sections = new ClonedChunkSection[SECTION_TABLE_ARRAY_SIZE];
this.blockArrays = new Block[SECTION_TABLE_ARRAY_SIZE][];
this.metadataArrays = new int[SECTION_TABLE_ARRAY_SIZE][];
this.biomeData = new BiomeGenBase[SECTION_TABLE_ARRAY_SIZE][];
this.lightArrays = new NibbleArray[SECTION_TABLE_ARRAY_SIZE][LIGHT_TYPES.length];
for (int x = 0; x < SECTION_LENGTH; x++) {
for (int y = 0; y < SECTION_LENGTH; y++) {
for (int z = 0; z < SECTION_LENGTH; z++) {
final int i = getLocalSectionIndex(x, y, z);
this.blockArrays[i] = new Block[SECTION_BLOCK_COUNT];
Arrays.fill(this.blockArrays[i], Blocks.air);
this.metadataArrays[i] = new int[SECTION_BLOCK_COUNT];
}
}
@@ -154,8 +167,13 @@ public class WorldSlice implements IBlockAccess {
for (int z = 0; z < SECTION_LENGTH; z++) {
final int idx = getLocalSectionIndex(x, y, z);
this.unpackBlockData(this.blockArrays[idx], this.metadataArrays[idx], this.sections[idx], context.getVolume());
this.biomeData[idx] = this.sections[idx].getBiomeData();
ClonedChunkSection section = this.sections[idx];
this.unpackBlockData(this.blockArrays[idx], this.metadataArrays[idx], section, context.getVolume());
this.biomeData[idx] = section.getBiomeData();
this.lightArrays[idx][EnumSkyBlock.Block.ordinal()] = section.getLightArray(EnumSkyBlock.Block);
this.lightArrays[idx][EnumSkyBlock.Sky.ordinal()] = section.getLightArray(EnumSkyBlock.Sky);
}
}
}
@@ -163,6 +181,11 @@ public class WorldSlice implements IBlockAccess {
@Override
public int getLightBrightnessForSkyBlocks(int x, int y, int z, int min) {
if (y < 0 || y >= 256 || x < -30000000 || z < -30000000 || x >= 30000000 || z > 30000000) {
// skyBrightness = 15, blockBrightness = 0
return (15 << 20);
}
final int skyBrightness = this.getSkyBlockTypeBrightness(net.minecraft.world.EnumSkyBlock.Sky, x, y, z);
int blockBrightness = this.getSkyBlockTypeBrightness(net.minecraft.world.EnumSkyBlock.Block, x, y, z);
@@ -172,11 +195,8 @@ public class WorldSlice implements IBlockAccess {
return skyBrightness << 20 | blockBrightness << 4;
}
public int getSkyBlockTypeBrightness(EnumSkyBlock skyBlock, int x, int y, int z) {
y = MathHelper.clamp_int(y, 0, 255);
if (y < 0 || y >= 256 || x < -30000000 || z < -30000000 || x >= 30000000 || z > 30000000) {
return skyBlock.defaultLightValue;
}
private int getSkyBlockTypeBrightness(EnumSkyBlock skyBlock, int x, int y, int z) {
if (this.getBlock(x, y, z).getUseNeighborBrightness()) {
int yp = this.getLightLevel(skyBlock, x, y + 1, z);
final int xp = this.getLightLevel(skyBlock, x + 1, y, z);
@@ -289,8 +309,7 @@ public class WorldSlice implements IBlockAccess {
final int relX = x - this.baseX;
final int relY = y - this.baseY;
final int relZ = z - this.baseZ;
final Block block = this.blockArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)][getLocalBlockIndex(relX & 15, relY & 15, relZ & 15)];
return block == null ? Blocks.air : block;
return this.blockArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)][getLocalBlockIndex(relX & 15, relY & 15, relZ & 15)];
}
public Block getBlockRelative(int x, int y, int z) {
@@ -326,7 +345,14 @@ public class WorldSlice implements IBlockAccess {
int relY = y - this.baseY;
int relZ = z - this.baseZ;
return this.sections[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)].getLightLevel(type, relX & 15, relY & 15, relZ & 15);
NibbleArray lightArray = this.lightArrays[getLocalSectionIndex(relX >> 4, relY >> 4, relZ >> 4)][type.ordinal()];
if (lightArray == null) {
// If the array is null, it means the dimension for the current world does not support that light type
return this.defaultLightValues[type.ordinal()];
}
return lightArray.get(relX & 15, relY & 15, relZ & 15);
}
public static int getLocalBlockIndex(int x, int y, int z) {
@@ -14,6 +14,7 @@ import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraft.world.gen.structure.StructureBoundingBox;
@@ -109,13 +110,11 @@ public class ClonedChunkSection {
return data.getExtBlockMetadata(x, y, z);
}
public int getLightLevel(EnumSkyBlock type, int x, int y, int z) {
public NibbleArray getLightArray(EnumSkyBlock type) {
if(type == EnumSkyBlock.Sky) {
if(world.provider.hasNoSky)
return 0;
return data.hasSky ? data.getExtSkylightValue(x, y, z) : type.defaultLightValue;
return (!world.provider.hasNoSky && data.hasSky) ? data.getSkylightArray() : null;
}
return data.getExtBlocklightValue(x, y, z);
return data.getBlocklightArray();
}
public BiomeGenBase getBiomeForNoiseGen(int x, int y, int z) {