Skip to content

Commit

Permalink
Try reduce pointer space overlap with more extent padding
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralhalo committed Dec 29, 2023
1 parent 453c8b3 commit 462c059
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/main/java/grondag/canvas/light/color/LightDataAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class LightDataAllocator {
// also represents row size of pointer header because we are putting addresses in the data texture.
private static final int ROW_SIZE = LightRegionData.Const.SIZE3D;

// padding to prevent overlap. set to minimum achievable without error
private static final int EXTENT_PADDING = 2;

// address for the static empty region.
static final short EMPTY_ADDRESS = 0;

Expand Down Expand Up @@ -200,6 +203,7 @@ private short allocateAddressInner(LightRegion region) {
return setAddress(region, newAddress);
}

// MAINTENANCE NOTICE: this function is a special casing of setAddress(LightRegion, short)
private short clearAddress(LightRegion region) {
if (region.texAllocation != EMPTY_ADDRESS) {
clearPointerIfWas(region.originPos, region.texAllocation);
Expand All @@ -219,23 +223,24 @@ private short setAddress(LightRegion region, short newAddress) {
return newAddress;
}

private void setPointer(BlockPos regionOrigin, short regionAddress) {
private int getBufferIndex(BlockPos regionOrigin) {
final int xInExtent = ((regionOrigin.getX() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int yInExtent = ((regionOrigin.getY() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int zInExtent = ((regionOrigin.getZ() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int pointerIndex = xInExtent * pointerExtent * pointerExtent + yInExtent * pointerExtent + zInExtent;
final int bufferIndex = pointerIndex * 2;
return pointerIndex * 2;
}

private void setPointer(BlockPos regionOrigin, short regionAddress) {
final int bufferIndex = getBufferIndex(regionOrigin);
final short storedAddress = pointerBuffer.getShort(bufferIndex);
pointerBuffer.putShort(bufferIndex, regionAddress);
needUploadPointers |= storedAddress != regionAddress;
}

// MAINTENANCE NOTICE: this function is a special casing of setPointer(BlockPos, short)
private void clearPointerIfWas(BlockPos regionOrigin, short oldAddress) {
final int xInExtent = ((regionOrigin.getX() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int yInExtent = ((regionOrigin.getY() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int zInExtent = ((regionOrigin.getZ() / 16) % pointerExtent + pointerExtent) % pointerExtent;
final int pointerIndex = xInExtent * pointerExtent * pointerExtent + yInExtent * pointerExtent + zInExtent;
final int bufferIndex = pointerIndex * 2;
final int bufferIndex = getBufferIndex(regionOrigin);
final short storedAddress = pointerBuffer.getShort(bufferIndex);

if (storedAddress == oldAddress) {
Expand Down Expand Up @@ -283,7 +288,7 @@ public int dataRowStart() {

public boolean checkInvalid() {
final var viewDistance = Minecraft.getInstance().options.renderDistance().get();
final var expectedExtent = (viewDistance + 1) * 2;
final var expectedExtent = (viewDistance + EXTENT_PADDING) * 2;

if (pointerExtent < expectedExtent) {
resizePointerBuffer(expectedExtent);
Expand Down

0 comments on commit 462c059

Please sign in to comment.