Skip to content

Commit

Permalink
add optimized BlockInfo variant
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Jan 7, 2017
1 parent ceb8355 commit a231f8f
Showing 5 changed files with 117 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/main/java/pl/asie/foamfix/client/Deduplicator.java
Original file line number Diff line number Diff line change
@@ -82,6 +82,7 @@ public class Deduplicator {
private IDeduplicatingStorage<float[][]> FLOATAA_STORAGE = new DeduplicatingStorageTrove<float[][]>(HashingStrategies.FLOAT_ARRAY_ARRAY);
private IDeduplicatingStorage OBJECT_STORAGE = new DeduplicatingStorageTrove(HashingStrategies.GENERIC);
private IDeduplicatingStorage<ItemCameraTransforms> ICT_STORAGE = new DeduplicatingStorageTrove<>(HashingStrategies.ITEM_CAMERA_TRANSFORMS);
private IDeduplicatingStorage<ItemTransformVec3f> IT3_STORAGE = new DeduplicatingStorageTrove<>(HashingStrategies.ITEM_TRANSFORM_VEC3F);
private Set<Object> deduplicatedObjects = new TCustomHashSet<Object>(HashingStrategies.IDENTITY);

public Deduplicator() {
@@ -223,8 +224,14 @@ public Object deduplicateObject(Object o, int recursion) {
} catch (Throwable t) {
t.printStackTrace();
}
} else if (o instanceof ResourceLocation || o instanceof TRSRTransformation || (c == ItemCameraTransforms.class)) {
} else if (o instanceof ResourceLocation || o instanceof TRSRTransformation) {
return deduplicate0(o);
} else if (c == ItemCameraTransforms.class) {
Object d = deduplicate0(o);
if (d != o)
return d;
// TODO: Add ItemTransformVec3f dedup, maybe
return o;
} else if (o instanceof Item || o instanceof Block || o instanceof World
|| o instanceof Entity || o instanceof Logger || o instanceof IRegistry) {
BLACKLIST_CLASS.add(c);
84 changes: 84 additions & 0 deletions src/main/java/pl/asie/foamfix/coremod/BlockInfoPatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package pl.asie.foamfix.coremod;

import net.minecraft.block.state.IBlockState;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;

/**
* Created by asie on 1/7/17.
*/
public class BlockInfoPatch {
private IBlockAccess world;
private IBlockState state;
private BlockPos blockPos;

private final boolean[][][] translucent = new boolean[3][3][3];
private final int[][][] s = new int[3][3][3];
private final int[][][] b = new int[3][3][3];
private final float[][][][] skyLight = new float[3][2][2][2];
private final float[][][][] blockLight = new float[3][2][2][2];
private final float[][][] ao = new float[3][3][3];

public void updateLightMatrix() {
boolean full = false;
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
int xo = blockPos.getX() - 1;
int yo = blockPos.getY() - 1;
int zo = blockPos.getZ() - 1;

for(int x = 0; x <= 2; x++) {
for(int y = 0; y <= 2; y++) {
for(int z = 0; z <= 2; z++) {
pos.setPos(xo+x, yo+y, zo+z);
IBlockState state = world.getBlockState(pos);
translucent[x][y][z] = state.isTranslucent();
//translucent[x][y][z] = world.getBlockState(pos).getBlock().getLightOpacity(world, pos) == 0;
int brightness = state.getPackedLightmapCoords(world, pos);
s[x][y][z] = (brightness >> 0x14) & 0xF;
b[x][y][z] = (brightness >> 0x04) & 0xF;
ao[x][y][z] = state.getAmbientOcclusionLightValue();
if(x == 1 && y == 1 && z == 1) {
full = state.isFullCube();
}
}
}
}

if(!full) {
for(EnumFacing side : EnumFacing.values()) {
int x = side.getFrontOffsetX() + 1;
int y = side.getFrontOffsetY() + 1;
int z = side.getFrontOffsetZ() + 1;
s[x][y][z] = Math.max(s[1][1][1] - 1, s[x][y][z]);
b[x][y][z] = Math.max(b[1][1][1] - 1, b[x][y][z]);
}
}

for(int x = 0; x < 2; x++) {
int x1 = x * 2;
for(int y = 0; y < 2; y++) {
int y1 = y * 2;
for(int z = 0; z < 2; z++) {
int z1 = z * 2;

boolean tx = translucent[x1][1][z1] || translucent[x1][y1][1];
skyLight[0][x][y][z] = combine(s[x1][1][1], s[x1][1][z1], s[x1][y1][1], tx ? s[x1][y1][z1] : s[x1][1][1]);
blockLight[0][x][y][z] = combine(b[x1][1][1], b[x1][1][z1], b[x1][y1][1], tx ? b[x1][y1][z1] : b[x1][1][1]);

boolean ty = translucent[x1][y1][1] || translucent[1][y1][z1];
skyLight[1][x][y][z] = combine(s[1][y1][1], s[x1][y1][1], s[1][y1][z1], ty ? s[x1][y1][z1] : s[1][y1][1]);
blockLight[1][x][y][z] = combine(b[1][y1][1], b[x1][y1][1], b[1][y1][z1], ty ? b[x1][y1][z1] : b[1][y1][1]);

boolean tz = translucent[x1][1][z1] || translucent[1][y1][z1];
skyLight[2][x][y][z] = combine(s[1][1][z1], s[1][y1][z1], s[x1][1][z1], tz ? s[x1][y1][z1] : s[1][1][z1]);
blockLight[2][x][y][z] = combine(b[1][1][z1], b[1][y1][z1], b[x1][1][z1], tz ? b[x1][y1][z1] : b[1][1][z1]);
}
}
}
}

private float combine(int i, int i1, int i2, int i3) {
return 0;
}
}
6 changes: 6 additions & 0 deletions src/main/java/pl/asie/foamfix/coremod/FoamFixTransformer.java
Original file line number Diff line number Diff line change
@@ -104,6 +104,12 @@ public byte[] transform(final String name, final String transformedName, final b
}
}

if (FoamFixShared.config.clBlockInfoPatch) {
if ("net.minecraftforge.client.model.pipeline.BlockInfo".equals(transformedName)) {
data = spliceMethods(data, "pl.asie.foamfix.coremod.BlockInfoPatch", transformedName, "updateLightMatrix");
}
}

if (FoamFixShared.config.geSmallPropertyStorage) {
if ("net.minecraft.block.state.BlockStateContainer".equals(transformedName)) {
data = spliceMethods(data, "pl.asie.foamfix.common.FoamyBlockStateContainer", transformedName, "createState");
3 changes: 2 additions & 1 deletion src/main/java/pl/asie/foamfix/shared/FoamFixConfig.java
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
public class FoamFixConfig {
public boolean lwWeakenResourceCache, lwDummyPackageManifestMap;
public boolean clDeduplicate, clCleanRedundantModelRegistry;
public boolean geBlockPosPatch, clTextureDoubleBuffering;
public boolean geBlockPosPatch, clBlockInfoPatch, clTextureDoubleBuffering;
public boolean geDynamicRegistrySizeScaling;
public boolean geSmallPropertyStorage;
public int clDeduplicateRecursionLevel;
@@ -54,6 +54,7 @@ public void init(File file, boolean isCoremod) {
// clTextureDoubleBuffering = config.getBoolean("textureDoubleBuffering", "experimental", true, "Makes texture animations double-buffered, letting the GPU process them independently of scene rendering.");
geSmallPropertyStorage = config.getBoolean("smallPropertyStorage", "experimental", true, "Replaces the default BlockState/ExtendedBlockState implementations with a far more memory-efficient variant.");
geBlockPosPatch = config.getBoolean("optimizedBlockPos", "coremod", true, "Optimizes BlockPos mutable/immutable getters to run on the same variables, letting them be inlined and thus theoretically increasing performance.");
clBlockInfoPatch = config.getBoolean("optimizedBlockInfo", "coremod", true, "Prevents BlockInfo from generating as many BlockPos objects; also, fixes a lighting bug.");
}

config.save();
17 changes: 17 additions & 0 deletions src/main/java/pl/asie/foamfix/util/HashingStrategies.java
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@ public final class HashingStrategies {
public static final HashingStrategy GENERIC = new ObjectStrategy();
public static final HashingStrategy IDENTITY = new IdentityHashingStrategy();
public static final HashingStrategy<ItemCameraTransforms> ITEM_CAMERA_TRANSFORMS = new ItemCameraTransformsStrategy();
public static final HashingStrategy<ItemTransformVec3f> ITEM_TRANSFORM_VEC3F = new ItemTransformVecStrategy();

private static final class ItemCameraTransformsStrategy implements HashingStrategy<ItemCameraTransforms> {
@Override
@@ -74,6 +75,22 @@ public boolean equals(ItemCameraTransforms o1, ItemCameraTransforms o2) {
}
}

private static final class ItemTransformVecStrategy implements HashingStrategy<ItemTransformVec3f> {
@Override
public int computeHashCode(ItemTransformVec3f transform) {
int hash = 1;
for (org.lwjgl.util.vector.Vector3f vector : ImmutableSet.of(transform.rotation, transform.scale, transform.translation)) {
hash = ((hash * 31 + Float.floatToIntBits(vector.getX())) * 31 + Float.floatToIntBits(vector.getY())) * 31 + Float.floatToIntBits(vector.getZ());
}
return hash;
}

@Override
public boolean equals(ItemTransformVec3f o1, ItemTransformVec3f o2) {
return Objects.equals(o1, o2);
}
}

private static final class ObjectStrategy implements HashingStrategy {
@Override
public int computeHashCode(Object object) {

0 comments on commit a231f8f

Please sign in to comment.