relight cleanup - think it works now. Little higher level, but that's what's needed.

This commit is contained in:
gnembon committed 2023-05-12 22:04:19 +02:00
1 parent 2cfedc733e
commit 755ec1daa8
11 files changed
+75 -365

No files matched your search

@@ -1,8 +0,0 @@
package carpet.fakes;
import net.minecraft.world.level.chunk.DataLayer;
public interface ChunkLightProviderInterface
{
int callGetCurrentLevelFromSection(DataLayer array, long blockPos);
}
@@ -1,12 +0,0 @@
package carpet.fakes;
import net.minecraft.world.level.lighting.LightEngine;
public interface LightStorageInterface extends Lighting_scarpetChunkCreationInterface
{
void processRemoveLightData(long pos);
void processRelight(LightEngine<?, ?> lightProvider, long pos);
int getLightLevelByLong(long blockPos);
}
@@ -3,8 +3,4 @@ package carpet.fakes;
public interface Lighting_scarpetChunkCreationInterface
{
void removeLightData(long pos);
void relight(long pos);
default void clearQueuedSectionBlocksPublicAccess(long sectionPos) {};
}
@@ -1,72 +0,0 @@
package carpet.mixins;
import org.spongepowered.asm.mixin.Mixin;
import carpet.fakes.ChunkLightProviderInterface;
import carpet.fakes.LightStorageInterface;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.chunk.LightChunkGetter;
import net.minecraft.world.level.lighting.BlockLightSectionStorage;
import net.minecraft.world.level.lighting.BlockLightSectionStorage.BlockDataLayerStorageMap;
import net.minecraft.world.level.lighting.LightEngine;
import net.minecraft.world.level.lighting.LayerLightSectionStorage;
@Mixin(BlockLightSectionStorage.class)
public abstract class BlockLightSectionStorage_scarpetChunkCreationMixin extends LayerLightSectionStorage<BlockDataLayerStorageMap> implements LightStorageInterface
{
private BlockLightSectionStorage_scarpetChunkCreationMixin(final LightLayer lightType, final LightChunkGetter chunkProvider, final BlockDataLayerStorageMap lightData)
{
super(lightType, chunkProvider, lightData);
}
@Override
public void processRelight(final LightEngine<?, ?> lightProvider, final long cPos)
{
//final DynamicGraphMinFixedPoint_resetChunkInterface levelPropagator = (DynamicGraphMinFixedPoint_resetChunkInterface) lightProvider.;
for (int y = -1; y < 17; ++y)
{
final long sectionPos = SectionPos.asLong(SectionPos.x(cPos), y, SectionPos.z(cPos));
final long pos = BlockPos.asLong(SectionPos.sectionToBlockCoord(SectionPos.x(sectionPos)), SectionPos.sectionToBlockCoord(y), SectionPos.sectionToBlockCoord(SectionPos.z(sectionPos)));
if (!this.storingLightForSection(sectionPos))
continue;
for (final Direction dir : Direction.Plane.HORIZONTAL)
{
final DataLayer neighborLightArray = this.getDataLayerData(SectionPos.offset(sectionPos, dir));
if (neighborLightArray == null)
continue;
final int ox = 15 * Math.max(dir.getStepX(), 0);
final int oz = 15 * Math.max(dir.getStepZ(), 0);
final int dx = Math.abs(dir.getStepZ());
final int dz = Math.abs(dir.getStepX());
for (int t = 0; t < 16; ++t)
for (int dy = 0; dy < 16; ++dy)
{
final long dst = BlockPos.offset(pos, ox + t * dx, dy, oz + t * dz);
lightProvider.checkBlock(BlockPos.of(dst));
//final long src = BlockPos.offset(dst, dir);
//final int srcLevel = ((ChunkLightProviderInterface) lightProvider).callGetCurrentLevelFromSection(neighborLightArray, src);
//levelPropagator.cmInvokeUpdateLevel(src, dst, levelPropagator.cmCallGetPropagatedLevel(src, dst, srcLevel), true);
}
}
}
}
@Override
public int getLightLevelByLong(long blockPos)
{
return getLightValue(blockPos);
}
}
@@ -398,34 +398,8 @@ public abstract class ChunkMap_scarpetChunkCreationMixin implements ThreadedAnvi
this.modified = true;
this.promoteChunkMap();
// Remove light for affected neighbors
for (final ChunkAccess chunk : affectedNeighbors)
((ServerLightingProviderInterface) this.lightEngine).removeLightData(chunk);
// Schedule relighting of neighbors
for (final ChunkAccess chunk : affectedNeighbors)
this.addRelightTicket(chunk.getPos());
this.tickTicketManager();
final List<CompletableFuture<?>> lightFutures = new ArrayList<>();
for (final ChunkAccess chunk : affectedNeighbors)
{
final ChunkPos pos = chunk.getPos();
lightFutures.add(this.getChunkRangeFuture (this.updatingChunkMap.get(pos.toLong()), 1, (pos_) -> ChunkStatus.LIGHT).thenCompose(
either -> either.map(
list -> ((ServerLightingProviderInterface) this.lightEngine).relight(chunk),
unloaded -> {
this.releaseRelightTicket(pos);
return CompletableFuture.completedFuture(null);
}
)
));
}
// Force generation to previous states
// This ensures that the world is in a consistent state after this method
@@ -473,7 +447,42 @@ public abstract class ChunkMap_scarpetChunkCreationMixin implements ThreadedAnvi
report.put("layer_time_" + status.getName(), (int) (System.currentTimeMillis() - start));
}
report.put("relight_count", lightFutures.size());
report.put("relight_count", affectedNeighbors.size());
// Remove light for affected neighbors
for (final ChunkAccess chunk : affectedNeighbors)
((ServerLightingProviderInterface) this.lightEngine).removeLightData(chunk);
// Schedule relighting of neighbors
for (final ChunkAccess chunk : affectedNeighbors)
this.addRelightTicket(chunk.getPos());
this.tickTicketManager();
final List<CompletableFuture<?>> lightFutures = new ArrayList<>();
for (final ChunkAccess chunk : affectedNeighbors)
{
final ChunkPos pos = chunk.getPos();
lightFutures.add(this.getChunkRangeFuture (this.updatingChunkMap.get(pos.toLong()), 1, (pos_) -> ChunkStatus.LIGHT).thenCompose(
either -> either.map(
list -> ((ServerLightingProviderInterface) this.lightEngine).relight(chunk),
unloaded -> {
this.releaseRelightTicket(pos);
return CompletableFuture.completedFuture(null);
}
)
));
}
final long relightStart = System.currentTimeMillis();
this.waitFor(lightFutures);
@@ -1,45 +1,23 @@
package carpet.mixins;
import carpet.fakes.LightStorageInterface;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import carpet.fakes.ChunkLightProviderInterface;
import carpet.fakes.Lighting_scarpetChunkCreationInterface;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.lighting.LightEngine;
import net.minecraft.world.level.lighting.LayerLightSectionStorage;
@Mixin(LightEngine.class)
public abstract class LayerLightEngine_scarpetChunkCreationMixin implements Lighting_scarpetChunkCreationInterface, ChunkLightProviderInterface
public abstract class LayerLightEngine_scarpetChunkCreationMixin implements Lighting_scarpetChunkCreationInterface
{
@Shadow
@Final
protected LayerLightSectionStorage<?> storage;
//@Shadow protected abstract void clearQueuedSectionBlocks(final long l);
@Override
public void removeLightData(final long pos)
{
((Lighting_scarpetChunkCreationInterface) this.storage).removeLightData(pos);
}
@Override
public void relight(final long pos)
{
((Lighting_scarpetChunkCreationInterface) this.storage).relight(pos);
}
@Override
public int callGetCurrentLevelFromSection(DataLayer array, long blockPos) {
return ((LightStorageInterface)storage).getLightLevelByLong(blockPos);
};
@Override
public void clearQueuedSectionBlocksPublicAccess(long sectionPos)
{
//clearQueuedSectionBlocks(sectionPos);
}
}
@@ -6,29 +6,19 @@ import carpet.fakes.Lighting_scarpetChunkCreationInterface;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.lighting.DataLayerStorageMap;
import net.minecraft.world.level.lighting.LightEngine;
import net.minecraft.world.level.lighting.LayerLightSectionStorage;
import org.spongepowered.asm.mixin.Final;
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.callback.CallbackInfo;
import carpet.fakes.LightStorageInterface;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
@Mixin(LayerLightSectionStorage.class)
public abstract class LayerLightSectionStorage_scarpetChunkCreationMixin implements LightStorageInterface
public abstract class LayerLightSectionStorage_scarpetChunkCreationMixin implements Lighting_scarpetChunkCreationInterface
{
@Shadow
protected abstract DataLayer getDataLayer(final long sectionPos, final boolean cached);
//@Shadow
//protected abstract void clearQueuedSectionBlocks(final LightEngine<?, ?> storage, final long blockChunkPos);
protected abstract DataLayer getDataLayer(long sectionPos, boolean cached);
@Shadow
@Final
@@ -38,79 +28,29 @@ public abstract class LayerLightSectionStorage_scarpetChunkCreationMixin impleme
@Final
protected DataLayerStorageMap<?> updatingSectionData;
@Shadow protected abstract boolean storingLightForSection(final long sectionPos);
@Shadow protected abstract boolean storingLightForSection(long sectionPos);
@Shadow
@Final
protected Long2ObjectMap<DataLayer> queuedSections;
//@Shadow protected abstract void enableLightSources(final long l, final boolean bl);
@Unique
private final LongSet removedChunks = new LongOpenHashSet();
@Unique
private final LongSet relightChunks = new LongOpenHashSet();
@Override
public void removeLightData(final long pos)
public void removeLightData(long cPos)
{
this.removedChunks.add(pos);
}
@Override
public void relight(final long pos)
{
this.relightChunks.add(pos);
}
@Inject(
method = "markNewInconsistencies",
at = @At("HEAD")
)
private void processData(final LightEngine<?, ?> lightProvider, final CallbackInfo ci)
{
// Process light removal
for (final long cPos : this.removedChunks)
for (int y = -1; y < 17; ++y)
{
for (int y = -1; y < 17; ++y)
long sectionPos = SectionPos.asLong(SectionPos.x(cPos), y, SectionPos.z(cPos));
this.queuedSections.remove(sectionPos);
if (this.storingLightForSection(sectionPos))
{
final long sectionPos = SectionPos.asLong(SectionPos.x(cPos), y, SectionPos.z(cPos));
if (this.changedSections.add(sectionPos))
this.updatingSectionData.copyDataLayer(sectionPos);
this.queuedSections.remove(sectionPos);
if (this.storingLightForSection(sectionPos))
{
//((Lighting_scarpetChunkCreationInterface)lightProvider).clearQueuedSectionBlocksPublicAccess(sectionPos);
if (this.changedSections.add(sectionPos))
this.updatingSectionData.copyDataLayer(sectionPos);
Arrays.fill(this.getDataLayer(sectionPos, true).getData(), (byte) 0);
}
Arrays.fill(this.getDataLayer(sectionPos, true).getData(), (byte) 0);
}
this.processRemoveLightData(cPos);
}
this.removedChunks.clear();
// Process relighting
for (final long cPos : this.relightChunks)
this.processRelight(lightProvider, cPos);
this.relightChunks.clear();
}
@Override
public void processRemoveLightData(final long pos)
{
}
@Override
public void processRelight(final LightEngine<?, ?> lightProvider, final long pos)
{
}
}
@@ -28,14 +28,4 @@ public abstract class LevelLightEngine_scarpetChunkCreationMixin implements Ligh
if (this.skyEngine != null)
((Lighting_scarpetChunkCreationInterface) this.skyEngine).removeLightData(pos);
}
@Override
public void relight(final long pos)
{
if (this.blockEngine != null)
((Lighting_scarpetChunkCreationInterface) this.blockEngine).relight(pos);
if (this.skyEngine != null)
((Lighting_scarpetChunkCreationInterface) this.skyEngine).relight(pos);
}
}
@@ -1,127 +0,0 @@
package carpet.mixins;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import carpet.fakes.ChunkLightProviderInterface;
import carpet.fakes.LightStorageInterface;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.world.level.chunk.LightChunkGetter;
import net.minecraft.world.level.lighting.BlockLightSectionStorage.BlockDataLayerStorageMap;
import net.minecraft.world.level.lighting.LightEngine;
import net.minecraft.world.level.lighting.LayerLightSectionStorage;
import net.minecraft.world.level.lighting.SkyLightSectionStorage;
@Mixin(SkyLightSectionStorage.class)
public abstract class SkyLightSectionStorage_scarpetChunkCreationMixin extends LayerLightSectionStorage<BlockDataLayerStorageMap> implements LightStorageInterface
{
protected SkyLightSectionStorage_scarpetChunkCreationMixin(final LightLayer lightType, final LightChunkGetter chunkProvider, final BlockDataLayerStorageMap lightData)
{
super(lightType, chunkProvider, lightData);
}
/*@Shadow
@Final
private LongSet sectionsToAddSourcesTo;
@Shadow
@Final
private LongSet sectionsToRemoveSourcesFrom;
@Shadow
@Final
private LongSet sectionsWithSources;
*/
@Shadow protected abstract boolean isAboveData(final long pos);
//@Shadow protected abstract boolean lightOnInSection(final long sectionPos);
@Override
public void processRemoveLightData(final long cPos)
{
for (int y = -1; y < 17; ++y)
{
final long sectionPos = SectionPos.asLong(SectionPos.x(cPos), y, SectionPos.z(cPos));
/* do something with this later
this.sectionsToAddSourcesTo.remove(sectionPos);
this.sectionsToRemoveSourcesFrom.remove(sectionPos);
this.sectionsWithSources.remove(sectionPos);
*/
}
}
@Override
public void processRelight(final LightEngine<?, ?> lightProvider, final long cPos)
{
//final DynamicGraphMinFixedPoint_resetChunkInterface levelPropagator = (DynamicGraphMinFixedPoint_resetChunkInterface) lightProvider;
for (int y = -1; y < 17; ++y)
{
final long sectionPos = SectionPos.asLong(SectionPos.x(cPos), y, SectionPos.z(cPos));
final long pos = BlockPos.asLong(SectionPos.sectionToBlockCoord(SectionPos.x(sectionPos)), SectionPos.sectionToBlockCoord(y), SectionPos.sectionToBlockCoord(SectionPos.z(sectionPos)));
if (!this.storingLightForSection(sectionPos))
continue;
for (final Direction dir : Direction.Plane.HORIZONTAL)
{
long neighborCeilingSectionPos = SectionPos.offset(sectionPos, dir);
final DataLayer neighborLightArray = this.getDataLayerData(neighborCeilingSectionPos);
DataLayer neighborCeilingLightArray = neighborLightArray;
while (neighborCeilingLightArray == null && !this.isAboveData(neighborCeilingSectionPos))
{
neighborCeilingSectionPos = SectionPos.offset(neighborCeilingSectionPos, Direction.UP);
neighborCeilingLightArray = this.getDataLayerData(neighborCeilingSectionPos);
}
final int ox = 15 * Math.max(dir.getStepX(), 0);
final int oz = 15 * Math.max(dir.getStepZ(), 0);
final int dx = Math.abs(dir.getStepZ());
final int dz = Math.abs(dir.getStepX());
int emptyLightLevel = (neighborCeilingLightArray == null)
? (this.lightOnInSection(SectionPos.getZeroNode(neighborCeilingSectionPos)) ? 0 : 15)
: 0;
int neighbourY = (neighborLightArray == null)
? SectionPos.sectionToBlockCoord(SectionPos.y(neighborCeilingSectionPos))
: 0;
for (int t = 0; t < 16; ++t)
for (int dy = 0; dy < 16; ++dy)
{
final long dst = BlockPos.offset(pos, ox + t * dx, dy, oz + t * dz);
//long src = BlockPos.offset(dst, dir);
//long adj_src = (neighborLightArray != null)
// ? src
// : BlockPos.asLong(BlockPos.getX(src), neighbourY, BlockPos.getZ(src));
//final int srcLevel = neighborCeilingLightArray != null
// ? ((ChunkLightProviderInterface) lightProvider).callGetCurrentLevelFromSection(neighborCeilingLightArray, adj_src)
// : emptyLightLevel;
lightProvider.checkBlock(BlockPos.of(dst));
//levelPropagator.cmInvokeUpdateLevel(src, dst, levelPropagator.cmCallGetPropagatedLevel(src, dst, srcLevel), true);
}
}
}
}
@Override
public int getLightLevelByLong(long blockPos)
{
return getLightValue(blockPos);
}
}
@@ -3,6 +3,7 @@ package carpet.mixins;
import java.util.concurrent.CompletableFuture;
import java.util.function.IntSupplier;
import net.minecraft.Util;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ThreadedLevelLightEngine;
@@ -42,7 +43,7 @@ public abstract class ThreadedLevelLightEngine_scarpetChunkCreationMixin extends
@Override
public void removeLightData(final ChunkAccess chunk)
{
final ChunkPos pos = chunk.getPos();
ChunkPos pos = chunk.getPos();
chunk.setLightCorrect(false);
this.addTask(pos.x, pos.z, () -> 0, ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> {
@@ -54,15 +55,32 @@ public abstract class ThreadedLevelLightEngine_scarpetChunkCreationMixin extends
}
@Override
public CompletableFuture<Void> relight(final ChunkAccess chunk)
public CompletableFuture<Void> relight(ChunkAccess chunk)
{
final ChunkPos pos = chunk.getPos();
ChunkPos pos = chunk.getPos();
propagateLightSources(pos);
this.addTask(pos.x, pos.z, () -> 0, ThreadedLevelLightEngine.TaskType.PRE_UPDATE, Util.name(() -> {
((Lighting_scarpetChunkCreationInterface) this).relight(SectionPos.getZeroNode(SectionPos.asLong(pos.x, 0, pos.z)));
super.propagateLightSources(pos);
int minY = chunk.getMinBuildHeight();
int maxY = chunk.getMaxBuildHeight();
int minX = pos.getMinBlockX();
int minZ = pos.getMinBlockZ();
BlockPos.MutableBlockPos poss = new BlockPos.MutableBlockPos();
for (int x = -1; x < 17; ++x)
{
for (int z = -1; z < 17; ++z)
{
if (x > 0 && x < 16 && z > 0 && z < 16)
{// not really efficient way to do it, but hey, we have bigger problems with this
continue;
}
for (int y = minY; y < maxY; ++y)
{
poss.set(x + minX, y, z + minZ);
super.checkBlock(poss);
}
}
}
},
() -> "Relight chunk " + pos
));