Skip to content

Commit

Permalink
More conversion work
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiboo committed Sep 5, 2023
1 parent efa27d7 commit 0d644f0
Show file tree
Hide file tree
Showing 14 changed files with 610 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public final class ClientCacheUtil {

public static LazyPackedLightCache generateLazyPackedLightCache(
public static LightCache generateLazyPackedLightCache(
// from position
final int fromX, final int fromY, final int fromZ,
// to position
Expand All @@ -27,7 +27,7 @@ public static LazyPackedLightCache generateLazyPackedLightCache(
final int cacheSizeX = Math.abs(toX - fromX);
final int cacheSizeY = Math.abs(toY - fromY);
final int cacheSizeZ = Math.abs(toZ - fromZ);
return LazyPackedLightCache.retain(
return LightCache.retain(
startPaddingX, startPaddingY, startPaddingZ,
cacheSizeX, cacheSizeY, cacheSizeZ,
cache,
Expand Down

This file was deleted.

86 changes: 86 additions & 0 deletions src/main/java/io/github/cadiboo/nocubes/client/LightCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.github.cadiboo.nocubes.client;

import io.github.cadiboo.nocubes.util.Area;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos.MutableBlockPos;

import javax.annotation.Nonnull;

/**
* @author Cadiboo
*/
public final class LightCache implements AutoCloseable {

private static final ThreadLocal<LightCache> POOL = ThreadLocal.withInitial(() -> new LightCache(0));
private static final ThreadLocal<MutableBlockPos> MUTABLE_BLOCK_POS = ThreadLocal.withInitial(MutableBlockPos::new);

public int[] cache;
private Area area;
private boolean inUse;

private LightCache(int size) {
this.cache = new int[size];
System.arraycopy(ClientUtil.NEGATIVE_1_8000, 0, this.cache, 0, size);
this.inUse = false;
}

@Nonnull
public static LightCache retain(Area area) {
final LightCache pooled = POOL.get();

if (pooled.inUse) {
throw new IllegalStateException("LazyPackedLightCache is already in use!");
}
pooled.inUse = true;
pooled.area = area;

int size = area.size.getX() * area.size.getY() * area.size.getZ();

if (pooled.cache.length >= size) {
System.arraycopy(ClientUtil.NEGATIVE_1_8000, 0, pooled.cache, 0, size);
return pooled;
}

pooled.cache = new int[size];
System.arraycopy(ClientUtil.NEGATIVE_1_8000, 0, pooled.cache, 0, size);
return pooled;
}

@Deprecated
public int get(final int x, final int y, final int z) {
return get(x, y, z, MUTABLE_BLOCK_POS.get());
}

public int get(final int x, final int y, final int z, MutableBlockPos pos) {
return get(x, y, z, this.cache, this.area, pos);
}

public static int get(
final int x, final int y, final int z,
final int[] cache,
Area area,
MutableBlockPos mutableBlockPos
) {
int index = area.indexIfInsideCache(x, y, z);
if (index != -1) {
int packedLight = cache[index];
if (packedLight != -1)
return packedLight;
}

IBlockState state = area.getBlockState(index, mutableBlockPos);
int packedLight = state.getPackedLightmapCoords(
area.world,
mutableBlockPos.setPos(area.start).add(x, y, z)
);
if (index != -1)
cache[index] = packedLight;
return packedLight;
}

@Override
public void close() {
this.inUse = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private LightmapInfo(final int skylight0, final int skylight1, final int skyligh
}

public static LightmapInfo generateLightmapInfo(
@Nonnull final LazyPackedLightCache lazyPackedLightCache,
@Nonnull final LightCache lazyPackedLightCache,
@Nonnull final Vec3 v0,
@Nonnull final Vec3 v1,
@Nonnull final Vec3 v2,
Expand Down Expand Up @@ -69,7 +69,7 @@ private static LightmapInfo generateLightmapInfoSmoothAO(
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LazyPackedLightCache packedLightCache,
@Nonnull final LightCache packedLightCache,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
) {
return generateLightmapInfoSmooth(v0, v1, v2, v3, chunkRenderPosX, chunkRenderPosY, chunkRenderPosZ, packedLightCache, pooledMutableBlockPos);
Expand All @@ -80,7 +80,7 @@ private static LightmapInfo generateLightmapInfoSmooth(
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LazyPackedLightCache light,
@Nonnull final LightCache light,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
) {
// TODO pool these arrays? (I think pooling them is more overhead than its worth)
Expand Down Expand Up @@ -153,7 +153,7 @@ private static LightmapInfo generateLightmapInfoFlat(
final int chunkRenderPosX,
final int chunkRenderPosY,
final int chunkRenderPosZ,
@Nonnull final LazyPackedLightCache lazyPackedLightCache,
@Nonnull final LightCache lazyPackedLightCache,
@Nonnull final PooledMutableBlockPos pooledMutableBlockPos
) {

Expand Down
Loading

0 comments on commit 0d644f0

Please sign in to comment.