mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-20 20:32:14 +00:00
Performance & allocation rate optimizations (#128)
This commit is contained in:
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"
|
||||
|
||||
+11
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user