Improve performance of vertex format mapping by caching hashcodes (#4370)

This commit is contained in:
Ben Staddon authored and LexManos committed 2017-10-02 16:45:58 -07:00
1 parent 4073891058
commit 0d34b02f88
2 files changed
+48 -16

No files matched your search

@@ -30,9 +30,8 @@ import net.minecraftforge.client.ForgeHooksClient;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class LightUtil
{
@@ -90,16 +89,7 @@ public class LightUtil
}
}
private static final LoadingCache<Pair<VertexFormat, VertexFormat>, int[]> formatMaps = CacheBuilder.newBuilder()
.maximumSize(10)
.build(new CacheLoader<Pair<VertexFormat, VertexFormat>, int[]>()
{
@Override
public int[] load(Pair<VertexFormat, VertexFormat> pair)
{
return mapFormats(pair.getLeft(), pair.getRight());
}
});
private static final ConcurrentMap<Pair<VertexFormat, VertexFormat>, int[]> formatMaps = new ConcurrentHashMap<>();
public static void putBakedQuad(IVertexConsumer consumer, BakedQuad quad)
{
@@ -110,20 +100,19 @@ public class LightUtil
consumer.setQuadTint(quad.getTintIndex());
}
consumer.setApplyDiffuseLighting(quad.shouldApplyDiffuseLighting());
//int[] eMap = mapFormats(consumer.getVertexFormat(), DefaultVertexFormats.ITEM);
float[] data = new float[4];
VertexFormat formatFrom = consumer.getVertexFormat();
VertexFormat formatTo = quad.getFormat();
int countFrom = formatFrom.getElementCount();
int countTo = formatTo.getElementCount();
int[] eMap = formatMaps.getUnchecked(Pair.of(formatFrom, formatTo));
int[] eMap = mapFormats(formatFrom, formatTo);
for(int v = 0; v < 4; v++)
{
for(int e = 0; e < countFrom; e++)
{
if(eMap[e] != countTo)
{
unpack(quad.getVertexData(), data, quad.getFormat(), v, eMap[e]);
unpack(quad.getVertexData(), data, formatTo, v, eMap[e]);
consumer.put(e, data);
}
else
@@ -135,6 +124,11 @@ public class LightUtil
}
public static int[] mapFormats(VertexFormat from, VertexFormat to)
{
return formatMaps.computeIfAbsent(Pair.of(from, to), pair -> generateMapping(pair.getLeft(), pair.getRight()));
}
private static int[] generateMapping(VertexFormat from, VertexFormat to)
{
int fromCount = from.getElementCount();
int toCount = to.getElementCount();