diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 4ef08c393..2a6aade83 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -20,7 +20,6 @@ import com.jozufozu.flywheel.lib.model.PartialModel; import com.jozufozu.flywheel.lib.pipeline.Pipelines; import com.jozufozu.flywheel.lib.struct.StructTypes; import com.jozufozu.flywheel.lib.util.QuadConverter; -import com.jozufozu.flywheel.lib.util.RenderWork; import com.jozufozu.flywheel.lib.util.ShadersModHandler; import com.jozufozu.flywheel.mixin.PausedPartialTickAccessor; import com.jozufozu.flywheel.vanilla.VanillaInstances; @@ -97,8 +96,6 @@ public class Flywheel { forgeEventBus.addListener(ForgeEvents::unloadWorld); forgeEventBus.addListener(ForgeEvents::tickLight); - forgeEventBus.addListener(EventPriority.LOWEST, RenderWork::onRenderLevelLast); - modEventBus.addListener(PartialModel::onModelRegistry); modEventBus.addListener(PartialModel::onModelBake); diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 38bcd3b34..4bee70011 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -9,7 +9,6 @@ import com.jozufozu.flywheel.api.backend.BackendType; import com.jozufozu.flywheel.backend.task.ParallelTaskExecutor; import com.jozufozu.flywheel.config.FlwConfig; import com.jozufozu.flywheel.lib.backend.BackendTypes; -import com.jozufozu.flywheel.lib.util.RenderWork; import com.mojang.logging.LogUtils; import net.minecraft.client.Minecraft; @@ -86,7 +85,7 @@ public class Backend { } public static void reloadWorldRenderers() { - RenderWork.enqueue(Minecraft.getInstance().levelRenderer::allChanged); + Minecraft.getInstance().levelRenderer.allChanged(); } private static BackendType chooseEngine() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java index 20a4dcb7c..aabe7de76 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/batching/DrawBuffer.java @@ -127,6 +127,8 @@ public class DrawBuffer { } public void free() { + reset(); + if (memory == null) { return; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java index aa9c0b083..de904f5d5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstance.java @@ -9,10 +9,10 @@ import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.FlatLit; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager; -import com.jozufozu.flywheel.light.LightListener; -import com.jozufozu.flywheel.util.box.ImmutableBox; +import com.jozufozu.flywheel.lib.light.LightListener; import net.minecraft.core.BlockPos; +import net.minecraft.core.SectionPos; import net.minecraft.world.level.Level; import net.minecraft.world.level.LightLayer; @@ -86,7 +86,7 @@ public abstract class AbstractInstance implements Instance, LightListener { } @Override - public boolean isListenerInvalid() { + public boolean isInvalid() { return removed; } @@ -96,7 +96,7 @@ public abstract class AbstractInstance implements Instance, LightListener { } @Override - public void onLightUpdate(LightLayer type, ImmutableBox changed) { + public void onLightUpdate(LightLayer type, SectionPos pos) { updateLight(); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java index 51e3b7c22..10cf8a397 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -19,7 +19,7 @@ import com.jozufozu.flywheel.backend.instancing.ratelimit.DistanceUpdateLimiter; import com.jozufozu.flywheel.backend.instancing.ratelimit.NonLimiter; import com.jozufozu.flywheel.backend.instancing.storage.Storage; import com.jozufozu.flywheel.config.FlwConfig; -import com.jozufozu.flywheel.light.LightUpdater; +import com.jozufozu.flywheel.lib.light.LightUpdater; import net.minecraft.core.BlockPos; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java index 630a3b830..9c8d68d98 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/blockentity/BlockEntityInstance.java @@ -10,8 +10,8 @@ import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.InstancedPart; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.MutableBox; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.entity.BlockEntity; @@ -90,7 +90,7 @@ public abstract class BlockEntityInstance extends Abstrac @Override public ImmutableBox getVolume() { - return GridAlignedBB.from(pos); + return MutableBox.from(pos); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java index 7283eed47..01116c389 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/effect/EffectInstanceManager.java @@ -9,7 +9,7 @@ import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.storage.AbstractStorage; import com.jozufozu.flywheel.backend.instancing.storage.Storage; -import com.jozufozu.flywheel.light.LightUpdater; +import com.jozufozu.flywheel.lib.light.LightUpdater; public class EffectInstanceManager extends InstanceManager { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java index 125fc6718..044b137a8 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstance.java @@ -7,9 +7,9 @@ import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.blockentity.BlockEntityInstanceManager; -import com.jozufozu.flywheel.light.LightListener; -import com.jozufozu.flywheel.light.TickingLightListener; -import com.jozufozu.flywheel.util.box.GridAlignedBB; +import com.jozufozu.flywheel.lib.box.MutableBox; +import com.jozufozu.flywheel.lib.light.LightListener; +import com.jozufozu.flywheel.lib.light.TickingLightListener; import com.mojang.math.Vector3f; import net.minecraft.core.BlockPos; @@ -38,16 +38,16 @@ import net.minecraft.world.phys.Vec3; public abstract class EntityInstance extends AbstractInstance implements LightListener, TickingLightListener { protected final E entity; - protected final GridAlignedBB bounds; + protected final MutableBox bounds; public EntityInstance(InstancerManager instancerManager, E entity) { super(instancerManager, entity.level); this.entity = entity; - bounds = GridAlignedBB.from(entity.getBoundingBox()); + bounds = MutableBox.from(entity.getBoundingBox()); } @Override - public GridAlignedBB getVolume() { + public MutableBox getVolume() { return bounds; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java index 6dcb3df21..ef09014f1 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/AbstractStorage.java @@ -7,7 +7,7 @@ import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; -import com.jozufozu.flywheel.light.LightUpdater; +import com.jozufozu.flywheel.lib.light.LightUpdater; public abstract class AbstractStorage implements Storage { protected final List tickableInstances; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java index 6a8dacd8e..b852a82ca 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/storage/One2OneStorage.java @@ -7,7 +7,7 @@ import org.jetbrains.annotations.Nullable; import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; -import com.jozufozu.flywheel.light.LightUpdater; +import com.jozufozu.flywheel.lib.light.LightUpdater; public abstract class One2OneStorage extends AbstractStorage { private final Map instances; diff --git a/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java index f7d096250..ea2375778 100644 --- a/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/handler/ForgeEvents.java @@ -5,8 +5,8 @@ import java.util.ArrayList; import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; +import com.jozufozu.flywheel.lib.light.LightUpdater; import com.jozufozu.flywheel.lib.memory.FlwMemoryTracker; -import com.jozufozu.flywheel.light.LightUpdater; import com.jozufozu.flywheel.util.StringUtil; import com.jozufozu.flywheel.util.WorldAttached; diff --git a/src/main/java/com/jozufozu/flywheel/util/box/ImmutableBox.java b/src/main/java/com/jozufozu/flywheel/lib/box/ImmutableBox.java similarity index 88% rename from src/main/java/com/jozufozu/flywheel/util/box/ImmutableBox.java rename to src/main/java/com/jozufozu/flywheel/lib/box/ImmutableBox.java index f87903d96..e6cfc46dc 100644 --- a/src/main/java/com/jozufozu/flywheel/util/box/ImmutableBox.java +++ b/src/main/java/com/jozufozu/flywheel/lib/box/ImmutableBox.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.util.box; +package com.jozufozu.flywheel.lib.box; import static com.jozufozu.flywheel.util.RenderMath.isPowerOf2; @@ -65,14 +65,14 @@ public interface ImmutableBox { return isPowerOf2(volume()); } - default GridAlignedBB intersect(ImmutableBox other) { + default MutableBox intersect(ImmutableBox other) { int minX = Math.max(this.getMinX(), other.getMinX()); int minY = Math.max(this.getMinY(), other.getMinY()); int minZ = Math.max(this.getMinZ(), other.getMinZ()); int maxX = Math.min(this.getMaxX(), other.getMaxX()); int maxY = Math.min(this.getMaxY(), other.getMaxY()); int maxZ = Math.min(this.getMaxZ(), other.getMaxZ()); - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } default ImmutableBox union(ImmutableBox other) { @@ -82,7 +82,7 @@ public interface ImmutableBox { int maxX = Math.max(this.getMaxX(), other.getMaxX()); int maxY = Math.max(this.getMaxY(), other.getMaxY()); int maxZ = Math.max(this.getMaxZ(), other.getMaxZ()); - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } @@ -108,7 +108,7 @@ public interface ImmutableBox { && other.getMaxZ() <= this.getMaxZ(); } - default boolean isContainedBy(GridAlignedBB other) { + default boolean isContainedBy(MutableBox other) { return other.contains(this); } @@ -132,7 +132,12 @@ public interface ImmutableBox { return new AABB(getMinX(), getMinY(), getMinZ(), getMaxX(), getMaxY(), getMaxZ()); } - default GridAlignedBB copy() { - return new GridAlignedBB(getMinX(), getMinY(), getMinZ(), getMaxX(), getMaxY(), getMaxZ()); + default MutableBox copy() { + return new MutableBox(getMinX(), getMinY(), getMinZ(), getMaxX(), getMaxY(), getMaxZ()); + } + + @FunctionalInterface + interface CoordinateConsumer { + void consume(int x, int y, int z); } } diff --git a/src/main/java/com/jozufozu/flywheel/util/box/GridAlignedBB.java b/src/main/java/com/jozufozu/flywheel/lib/box/MutableBox.java similarity index 82% rename from src/main/java/com/jozufozu/flywheel/util/box/GridAlignedBB.java rename to src/main/java/com/jozufozu/flywheel/lib/box/MutableBox.java index 279669f11..f052647c8 100644 --- a/src/main/java/com/jozufozu/flywheel/util/box/GridAlignedBB.java +++ b/src/main/java/com/jozufozu/flywheel/lib/box/MutableBox.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.util.box; +package com.jozufozu.flywheel.lib.box; import java.util.Collection; @@ -10,7 +10,7 @@ import net.minecraft.core.SectionPos; import net.minecraft.core.Vec3i; import net.minecraft.world.phys.AABB; -public class GridAlignedBB implements ImmutableBox { +public class MutableBox implements ImmutableBox { private int minX; private int minY; private int minZ; @@ -18,11 +18,11 @@ public class GridAlignedBB implements ImmutableBox { private int maxY; private int maxZ; - public GridAlignedBB() { + public MutableBox() { } - public GridAlignedBB(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { + public MutableBox(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { this.minX = minX; this.minY = minY; this.minZ = minZ; @@ -31,41 +31,41 @@ public class GridAlignedBB implements ImmutableBox { this.maxZ = maxZ; } - public static GridAlignedBB ofRadius(int radius) { - return new GridAlignedBB(-radius, -radius, -radius, radius + 1, radius + 1, radius + 1); + public static MutableBox ofRadius(int radius) { + return new MutableBox(-radius, -radius, -radius, radius + 1, radius + 1, radius + 1); } - public static GridAlignedBB from(AABB aabb) { + public static MutableBox from(AABB aabb) { int minX = (int) Math.floor(aabb.minX); int minY = (int) Math.floor(aabb.minY); int minZ = (int) Math.floor(aabb.minZ); int maxX = (int) Math.ceil(aabb.maxX); int maxY = (int) Math.ceil(aabb.maxY); int maxZ = (int) Math.ceil(aabb.maxZ); - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } - public static GridAlignedBB from(SectionPos pos) { - return new GridAlignedBB(pos.minBlockX(), pos.minBlockY(), pos.minBlockZ(), pos.maxBlockX() + 1, pos.maxBlockY() + 1, pos.maxBlockZ() + 1); + public static MutableBox from(SectionPos pos) { + return new MutableBox(pos.minBlockX(), pos.minBlockY(), pos.minBlockZ(), pos.maxBlockX() + 1, pos.maxBlockY() + 1, pos.maxBlockZ() + 1); } - public static GridAlignedBB from(BlockPos start, BlockPos end) { - return new GridAlignedBB(start.getX(), start.getY(), start.getZ(), end.getX() + 1, end.getY() + 1, end.getZ() + 1); + public static MutableBox from(BlockPos start, BlockPos end) { + return new MutableBox(start.getX(), start.getY(), start.getZ(), end.getX() + 1, end.getY() + 1, end.getZ() + 1); } - public static GridAlignedBB from(BlockPos pos) { - return new GridAlignedBB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1); + public static MutableBox from(BlockPos pos) { + return new MutableBox(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1); } - public static GridAlignedBB from(int sectionX, int sectionZ) { + public static MutableBox from(int sectionX, int sectionZ) { int startX = sectionX << 4; int startZ = sectionZ << 4; - return new GridAlignedBB(startX, 0, startZ, startX + 16, 256, startZ + 16); + return new MutableBox(startX, 0, startZ, startX + 16, 256, startZ + 16); } public static ImmutableBox containingAll(Collection positions) { if (positions.isEmpty()) { - return new GridAlignedBB(); + return new MutableBox(); } int minX = Integer.MAX_VALUE; int minY = Integer.MAX_VALUE; @@ -81,7 +81,7 @@ public class GridAlignedBB implements ImmutableBox { maxY = Math.max(maxY, pos.getY()); maxZ = Math.max(maxZ, pos.getZ()); } - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } public void fixMinMax() { @@ -277,37 +277,37 @@ public class GridAlignedBB implements ImmutableBox { return maxZ; } - public GridAlignedBB setMinX(int minX) { + public MutableBox setMinX(int minX) { this.minX = minX; return this; } - public GridAlignedBB setMinY(int minY) { + public MutableBox setMinY(int minY) { this.minY = minY; return this; } - public GridAlignedBB setMinZ(int minZ) { + public MutableBox setMinZ(int minZ) { this.minZ = minZ; return this; } - public GridAlignedBB setMaxX(int maxX) { + public MutableBox setMaxX(int maxX) { this.maxX = maxX; return this; } - public GridAlignedBB setMaxY(int maxY) { + public MutableBox setMaxY(int maxY) { this.maxY = maxY; return this; } - public GridAlignedBB setMaxZ(int maxZ) { + public MutableBox setMaxZ(int maxZ) { this.maxZ = maxZ; return this; } - public GridAlignedBB assign(BlockPos start, BlockPos end) { + public MutableBox assign(BlockPos start, BlockPos end) { minX = start.getX(); minY = start.getY(); minZ = start.getZ(); @@ -317,22 +317,22 @@ public class GridAlignedBB implements ImmutableBox { return this; } - public GridAlignedBB setMax(Vec3i v) { + public MutableBox setMax(Vec3i v) { return setMax(v.getX(), v.getY(), v.getZ()); } - public GridAlignedBB setMin(Vec3i v) { + public MutableBox setMin(Vec3i v) { return setMin(v.getX(), v.getY(), v.getZ()); } - public GridAlignedBB setMax(int x, int y, int z) { + public MutableBox setMax(int x, int y, int z) { maxX = x; maxY = y; maxZ = z; return this; } - public GridAlignedBB setMin(int x, int y, int z) { + public MutableBox setMin(int x, int y, int z) { minX = x; minY = y; minZ = z; @@ -376,14 +376,14 @@ public class GridAlignedBB implements ImmutableBox { } @Override - public GridAlignedBB intersect(ImmutableBox other) { + public MutableBox intersect(ImmutableBox other) { int minX = Math.max(this.minX, other.getMinX()); int minY = Math.max(this.minY, other.getMinY()); int minZ = Math.max(this.minZ, other.getMinZ()); int maxX = Math.min(this.maxX, other.getMaxX()); int maxY = Math.min(this.maxY, other.getMaxY()); int maxZ = Math.min(this.maxZ, other.getMaxZ()); - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } @Override @@ -394,7 +394,7 @@ public class GridAlignedBB implements ImmutableBox { int maxX = Math.max(this.maxX, other.getMaxX()); int maxY = Math.max(this.maxY, other.getMaxY()); int maxZ = Math.max(this.maxZ, other.getMaxZ()); - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } @Override @@ -426,8 +426,8 @@ public class GridAlignedBB implements ImmutableBox { } @Override - public GridAlignedBB copy() { - return new GridAlignedBB(minX, minY, minZ, maxX, maxY, maxZ); + public MutableBox copy() { + return new MutableBox(minX, minY, minZ, maxX, maxY, maxZ); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/light/DummyLightUpdater.java b/src/main/java/com/jozufozu/flywheel/lib/light/DummyLightUpdater.java similarity index 73% rename from src/main/java/com/jozufozu/flywheel/light/DummyLightUpdater.java rename to src/main/java/com/jozufozu/flywheel/lib/light/DummyLightUpdater.java index 0cb04f944..f2256fce7 100644 --- a/src/main/java/com/jozufozu/flywheel/light/DummyLightUpdater.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/DummyLightUpdater.java @@ -1,9 +1,10 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import java.util.stream.Stream; -import com.jozufozu.flywheel.util.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import net.minecraft.core.SectionPos; import net.minecraft.world.level.LightLayer; public class DummyLightUpdater extends LightUpdater { @@ -29,12 +30,7 @@ public class DummyLightUpdater extends LightUpdater { } @Override - public void onLightUpdate(LightLayer type, long sectionPos) { - // noop - } - - @Override - public void onLightPacket(int chunkX, int chunkZ) { + public void onLightUpdate(LightLayer type, SectionPos pos) { // noop } diff --git a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java b/src/main/java/com/jozufozu/flywheel/lib/light/GPULightVolume.java similarity index 94% rename from src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java rename to src/main/java/com/jozufozu/flywheel/lib/light/GPULightVolume.java index ff93e80fd..8023c3b00 100644 --- a/src/main/java/com/jozufozu/flywheel/light/GPULightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/GPULightVolume.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import static org.lwjgl.opengl.GL11.GL_LINEAR; import static org.lwjgl.opengl.GL11.GL_TEXTURE_MAG_FILTER; @@ -23,14 +23,14 @@ import org.lwjgl.opengl.GL30; import com.jozufozu.flywheel.gl.GlTexture; import com.jozufozu.flywheel.gl.GlTextureUnit; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.MutableBox; import net.minecraft.world.level.BlockAndTintGetter; public class GPULightVolume extends LightVolume { - protected final GridAlignedBB sampleVolume = new GridAlignedBB(); + protected final MutableBox sampleVolume = new MutableBox(); private final GlTexture glTexture; private final GlTextureUnit textureUnit = GlTextureUnit.T4; diff --git a/src/main/java/com/jozufozu/flywheel/light/LightListener.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightListener.java similarity index 54% rename from src/main/java/com/jozufozu/flywheel/light/LightListener.java rename to src/main/java/com/jozufozu/flywheel/lib/light/LightListener.java index 55a1e8020..e906b37b8 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightListener.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightListener.java @@ -1,8 +1,8 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import net.minecraft.core.SectionPos; import net.minecraft.world.level.LightLayer; /** @@ -20,22 +20,10 @@ public interface LightListener { * @return {@code true} if the listener is invalid/removed/deleted, * and should no longer receive updates. */ - boolean isListenerInvalid(); + boolean isInvalid(); /** * Called when a light updates in a chunk the implementor cares about. */ - void onLightUpdate(LightLayer type, ImmutableBox changed); - - /** - * Called when the server sends light data to the client. - * - */ - default void onLightPacket(int chunkX, int chunkZ) { - GridAlignedBB changedVolume = GridAlignedBB.from(chunkX, chunkZ); - - onLightUpdate(LightLayer.BLOCK, changedVolume); - - onLightUpdate(LightLayer.SKY, changedVolume); - } + void onLightUpdate(LightLayer type, SectionPos pos); } diff --git a/src/main/java/com/jozufozu/flywheel/light/LightPacking.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightPacking.java similarity index 92% rename from src/main/java/com/jozufozu/flywheel/light/LightPacking.java rename to src/main/java/com/jozufozu/flywheel/lib/light/LightPacking.java index c18323b12..6f2617f9b 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightPacking.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightPacking.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; /** * Utility class for bit-twiddling light. diff --git a/src/main/java/com/jozufozu/flywheel/light/LightUpdated.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdated.java similarity index 95% rename from src/main/java/com/jozufozu/flywheel/light/LightUpdated.java rename to src/main/java/com/jozufozu/flywheel/lib/light/LightUpdated.java index 4173ef2a8..e15caacc1 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightUpdated.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdated.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import net.minecraft.client.Minecraft; import net.minecraft.world.level.LevelAccessor; diff --git a/src/main/java/com/jozufozu/flywheel/light/LightUpdater.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java similarity index 51% rename from src/main/java/com/jozufozu/flywheel/light/LightUpdater.java rename to src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java index e30daa013..06f6b89f6 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightUpdater.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightUpdater.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import java.util.Queue; import java.util.Set; @@ -6,15 +6,12 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.stream.Stream; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.task.ParallelTaskExecutor; import com.jozufozu.flywheel.backend.task.WorkGroup; +import com.jozufozu.flywheel.lib.box.ImmutableBox; import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.WorldAttached; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; import it.unimi.dsi.fastutil.longs.LongSet; -import net.minecraft.core.BlockPos; import net.minecraft.core.SectionPos; import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.LightLayer; @@ -28,7 +25,11 @@ import net.minecraft.world.level.LightLayer; public class LightUpdater { private static final WorldAttached LEVELS = new WorldAttached<>(LightUpdater::new); - private final ParallelTaskExecutor taskExecutor; + + private final LevelAccessor level; + + private final WeakContainmentMultiMap listenersBySection = new WeakContainmentMultiMap<>(); + private final Set tickingListeners = FlwUtil.createWeakHashSet(); public static LightUpdater get(LevelAccessor level) { if (LightUpdated.receivesLightUpdates(level)) { @@ -40,14 +41,7 @@ public class LightUpdater { } } - private final LevelAccessor level; - - private final Set tickingLightListeners = FlwUtil.createWeakHashSet(); - private final WeakContainmentMultiMap sections = new WeakContainmentMultiMap<>(); - private final WeakContainmentMultiMap chunks = new WeakContainmentMultiMap<>(); - public LightUpdater(LevelAccessor level) { - taskExecutor = Backend.getTaskExecutor(); this.level = level; } @@ -57,7 +51,7 @@ public class LightUpdater { } private void tickSerial() { - for (TickingLightListener tickingLightListener : tickingLightListeners) { + for (TickingLightListener tickingLightListener : tickingListeners) { if (tickingLightListener.tickLightListener()) { addListener(tickingLightListener); } @@ -68,13 +62,13 @@ public class LightUpdater { Queue listeners = new ConcurrentLinkedQueue<>(); WorkGroup.builder() - .addTasks(tickingLightListeners.stream(), listener -> { + .addTasks(tickingListeners.stream(), listener -> { if (listener.tickLightListener()) { listeners.add(listener); } }) .onComplete(() -> listeners.forEach(this::addListener)) - .execute(taskExecutor); + .execute(Backend.getTaskExecutor()); } /** @@ -84,12 +78,11 @@ public class LightUpdater { */ public void addListener(LightListener listener) { if (listener instanceof TickingLightListener) - tickingLightListeners.add(((TickingLightListener) listener)); + tickingListeners.add(((TickingLightListener) listener)); ImmutableBox box = listener.getVolume(); - LongSet sections = this.sections.getAndResetContainment(listener); - LongSet chunks = this.chunks.getAndResetContainment(listener); + LongSet sections = this.listenersBySection.getAndResetContainment(listener); int minX = SectionPos.blockToSectionCoord(box.getMinX()); int minY = SectionPos.blockToSectionCoord(box.getMinY()); @@ -99,75 +92,42 @@ public class LightUpdater { int maxZ = SectionPos.blockToSectionCoord(box.getMaxZ()); for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - for (int y = minY; y <= maxY; y++) { + for (int y = minY; y <= maxY; y++) { + for (int z = minZ; z <= maxZ; z++) { long sectionPos = SectionPos.asLong(x, y, z); - this.sections.put(sectionPos, listener); + this.listenersBySection.put(sectionPos, listener); sections.add(sectionPos); } - long chunkPos = SectionPos.asLong(x, 0, z); - this.chunks.put(chunkPos, listener); - chunks.add(chunkPos); } } } public void removeListener(LightListener listener) { - this.sections.remove(listener); - this.chunks.remove(listener); + this.listenersBySection.remove(listener); } /** * Dispatch light updates to all registered {@link LightListener}s. - * @param type The type of light that changed. - * @param sectionPos A long representing the section position where light changed. + * @param type The type of light that changed. + * @param pos The section position where light changed. */ - public void onLightUpdate(LightLayer type, long sectionPos) { - Set set = sections.get(sectionPos); + public void onLightUpdate(LightLayer type, SectionPos pos) { + Set listeners = listenersBySection.get(pos.asLong()); - if (set == null || set.isEmpty()) return; + if (listeners == null || listeners.isEmpty()) return; - set.removeIf(LightListener::isListenerInvalid); + listeners.removeIf(LightListener::isInvalid); - ImmutableBox chunkBox = GridAlignedBB.from(SectionPos.of(sectionPos)); - - for (LightListener listener : set) { - listener.onLightUpdate(type, chunkBox); + for (LightListener listener : listeners) { + listener.onLightUpdate(type, pos); } } - /** - * Dispatch light updates to all registered {@link LightListener}s - * when the server sends lighting data for an entire chunk. - * - */ - public void onLightPacket(int chunkX, int chunkZ) { - long chunkPos = SectionPos.asLong(chunkX, 0, chunkZ); - - Set set = chunks.get(chunkPos); - - if (set == null || set.isEmpty()) return; - - set.removeIf(LightListener::isListenerInvalid); - - for (LightListener listener : set) { - listener.onLightPacket(chunkX, chunkZ); - } - } - - public static long blockToSection(BlockPos pos) { - return SectionPos.asLong(pos.getX(), pos.getY(), pos.getZ()); - } - - public static long sectionToChunk(long sectionPos) { - return sectionPos & 0xFFFFFFFFFFF_00000L; - } - public Stream getAllBoxes() { - return chunks.stream().map(LightListener::getVolume); + return listenersBySection.stream().map(LightListener::getVolume); } public boolean isEmpty() { - return chunks.isEmpty(); + return listenersBySection.isEmpty(); } } diff --git a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java b/src/main/java/com/jozufozu/flywheel/lib/light/LightVolume.java similarity index 86% rename from src/main/java/com/jozufozu/flywheel/light/LightVolume.java rename to src/main/java/com/jozufozu/flywheel/lib/light/LightVolume.java index 018a152d8..f589b7091 100644 --- a/src/main/java/com/jozufozu/flywheel/light/LightVolume.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/LightVolume.java @@ -1,19 +1,20 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import org.lwjgl.system.MemoryUtil; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.MutableBox; import com.jozufozu.flywheel.lib.memory.MemoryBlock; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; import net.minecraft.core.BlockPos; +import net.minecraft.core.SectionPos; import net.minecraft.world.level.BlockAndTintGetter; import net.minecraft.world.level.LightLayer; public class LightVolume implements ImmutableBox, LightListener { protected final BlockAndTintGetter level; - protected final GridAlignedBB box = new GridAlignedBB(); + protected final MutableBox box = new MutableBox(); protected MemoryBlock lightData; public LightVolume(BlockAndTintGetter level, ImmutableBox sampleVolume) { @@ -59,7 +60,7 @@ public class LightVolume implements ImmutableBox, LightListener { } @Override - public boolean isListenerInvalid() { + public boolean isInvalid() { return lightData == null; } @@ -207,10 +208,10 @@ public class LightVolume implements ImmutableBox, LightListener { } @Override - public void onLightUpdate(LightLayer type, ImmutableBox changedVolume) { + public void onLightUpdate(LightLayer type, SectionPos pos) { if (lightData == null) return; - GridAlignedBB vol = changedVolume.copy(); + MutableBox vol = MutableBox.from(pos); if (!vol.intersects(getVolume())) return; vol.intersectAssign(getVolume()); // compute the region contained by us that has dirty lighting data. @@ -219,16 +220,4 @@ public class LightVolume implements ImmutableBox, LightListener { markDirty(); } - @Override - public void onLightPacket(int chunkX, int chunkZ) { - if (lightData == null) return; - - GridAlignedBB changedVolume = GridAlignedBB.from(chunkX, chunkZ); - if (!changedVolume.intersects(getVolume())) return; - changedVolume.intersectAssign(getVolume()); // compute the region contained by us that has dirty lighting data. - - copyLight(changedVolume); - markDirty(); - } - } diff --git a/src/main/java/com/jozufozu/flywheel/light/TickingLightListener.java b/src/main/java/com/jozufozu/flywheel/lib/light/TickingLightListener.java similarity index 82% rename from src/main/java/com/jozufozu/flywheel/light/TickingLightListener.java rename to src/main/java/com/jozufozu/flywheel/lib/light/TickingLightListener.java index cd3d00b7a..64fdf3b13 100644 --- a/src/main/java/com/jozufozu/flywheel/light/TickingLightListener.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/TickingLightListener.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; public interface TickingLightListener extends LightListener { /** diff --git a/src/main/java/com/jozufozu/flywheel/light/WeakContainmentMultiMap.java b/src/main/java/com/jozufozu/flywheel/lib/light/WeakContainmentMultiMap.java similarity index 92% rename from src/main/java/com/jozufozu/flywheel/light/WeakContainmentMultiMap.java rename to src/main/java/com/jozufozu/flywheel/lib/light/WeakContainmentMultiMap.java index dcba34350..f5973b097 100644 --- a/src/main/java/com/jozufozu/flywheel/light/WeakContainmentMultiMap.java +++ b/src/main/java/com/jozufozu/flywheel/lib/light/WeakContainmentMultiMap.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.light; +package com.jozufozu.flywheel.lib.light; import java.util.AbstractCollection; import java.util.Iterator; @@ -24,7 +24,7 @@ public class WeakContainmentMultiMap extends AbstractCollection { } /** - * This is a confusing function, but it maintains the internal state of the chunk/section maps. + * This is a confusing function, but it maintains the internal state of the section maps. * *

* First, uses the reverse lookup map to remove listener from all sets in the lookup map.
@@ -32,7 +32,7 @@ public class WeakContainmentMultiMap extends AbstractCollection { *

* * @param listener The listener to clean up. - * @return An empty set that should be populated with the chunks/sections the listener is contained in. + * @return An empty set that should be populated with the sections the listener is contained in. */ public LongSet getAndResetContainment(T listener) { LongSet containmentSet = reverse.computeIfAbsent(listener, $ -> new LongRBTreeSet()); diff --git a/src/main/java/com/jozufozu/flywheel/lib/util/RenderWork.java b/src/main/java/com/jozufozu/flywheel/lib/util/RenderWork.java deleted file mode 100644 index c179bc85a..000000000 --- a/src/main/java/com/jozufozu/flywheel/lib/util/RenderWork.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.jozufozu.flywheel.lib.util; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -import net.minecraftforge.client.event.RenderLevelLastEvent; - -public class RenderWork { - private static final Queue RUNS = new ConcurrentLinkedQueue<>(); - - public static void onRenderLevelLast(RenderLevelLastEvent event) { - while (!RUNS.isEmpty()) { - RUNS.remove() - .run(); - } - } - - /** - * Queue work to be executed at the end of a frame - */ - public static void enqueue(Runnable run) { - RUNS.add(run); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/LightUpdateMixin.java similarity index 59% rename from src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java rename to src/main/java/com/jozufozu/flywheel/mixin/LightUpdateMixin.java index 90ba7dfca..8824a9221 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/LightUpdateMixin.java @@ -1,4 +1,4 @@ -package com.jozufozu.flywheel.mixin.light; +package com.jozufozu.flywheel.mixin; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -7,7 +7,7 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.jozufozu.flywheel.light.LightUpdater; +import com.jozufozu.flywheel.lib.light.LightUpdater; import net.minecraft.client.multiplayer.ClientChunkCache; import net.minecraft.client.multiplayer.ClientLevel; @@ -22,14 +22,12 @@ public abstract class LightUpdateMixin extends ChunkSource { ClientLevel level; /** - * JUSTIFICATION: This method is called after a lighting tick once per subchunk where a - * lighting change occurred that tick. On the client, Minecraft uses this method to inform - * the rendering system that it needs to redraw a chunk. It does all that work asynchronously, - * and we should too. + * JUSTIFICATION: This method is called after lighting updates are finished processing + * per section where a lighting change occurred that frame. On the client, Minecraft + * uses this method to inform the rendering system that it needs to redraw a chunk. */ - @Inject(at = @At("HEAD"), method = "onLightUpdate") + @Inject(method = "onLightUpdate(Lnet/minecraft/world/level/LightLayer;Lnet/minecraft/core/SectionPos;)V", at = @At("HEAD")) private void flywheel$onLightUpdate(LightLayer type, SectionPos pos, CallbackInfo ci) { - LightUpdater.get(level) - .onLightUpdate(type, pos.asLong()); + LightUpdater.get(level).onLightUpdate(type, pos); } } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/light/NetworkLightUpdateMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/light/NetworkLightUpdateMixin.java deleted file mode 100644 index 319f2cca4..000000000 --- a/src/main/java/com/jozufozu/flywheel/mixin/light/NetworkLightUpdateMixin.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.jozufozu.flywheel.mixin.light; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import com.jozufozu.flywheel.lib.util.RenderWork; -import com.jozufozu.flywheel.light.LightUpdater; - -import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ClientLevel; -import net.minecraft.client.multiplayer.ClientPacketListener; -import net.minecraft.network.protocol.game.ClientboundLightUpdatePacket; - -@Mixin(ClientPacketListener.class) -public class NetworkLightUpdateMixin { - @Inject(at = @At("TAIL"), method = "handleLightUpdatePacket") - private void flywheel$onLightPacket(ClientboundLightUpdatePacket packet, CallbackInfo ci) { - RenderWork.enqueue(() -> { - ClientLevel level = Minecraft.getInstance().level; - - if (level == null) return; - - int chunkX = packet.getX(); - int chunkZ = packet.getZ(); - - LightUpdater.get(level) - .onLightPacket(chunkX, chunkZ); - }); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/util/box/CoordinateConsumer.java b/src/main/java/com/jozufozu/flywheel/util/box/CoordinateConsumer.java deleted file mode 100644 index f42c5a00b..000000000 --- a/src/main/java/com/jozufozu/flywheel/util/box/CoordinateConsumer.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.jozufozu.flywheel.util.box; - -@FunctionalInterface -public interface CoordinateConsumer { - void consume(int x, int y, int z); -} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java index 6fbca76e8..e16ba7f29 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/effect/ExampleEffect.java @@ -16,12 +16,12 @@ import com.jozufozu.flywheel.api.instancer.InstancerManager; import com.jozufozu.flywheel.backend.instancing.AbstractInstance; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.backend.instancing.effect.Effect; +import com.jozufozu.flywheel.lib.box.ImmutableBox; +import com.jozufozu.flywheel.lib.box.MutableBox; import com.jozufozu.flywheel.lib.model.Models; import com.jozufozu.flywheel.lib.struct.StructTypes; import com.jozufozu.flywheel.lib.struct.TransformedPart; import com.jozufozu.flywheel.util.AnimationTickHolder; -import com.jozufozu.flywheel.util.box.GridAlignedBB; -import com.jozufozu.flywheel.util.box.ImmutableBox; import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; @@ -67,7 +67,7 @@ public class ExampleEffect implements Effect { this.level = level; this.targetPoint = targetPoint; this.blockPos = new BlockPos(targetPoint.x, targetPoint.y, targetPoint.z); - this.volume = GridAlignedBB.from(this.blockPos); + this.volume = MutableBox.from(this.blockPos); this.effects = new ArrayList<>(INSTANCE_COUNT); this.boids = new ArrayList<>(INSTANCE_COUNT); } diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 849ea2f33..803dee3b5 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -15,6 +15,7 @@ "GlStateManagerMixin", "LevelRendererAccessor", "LevelRendererMixin", + "LightUpdateMixin", "PausedPartialTickAccessor", "RenderTypeMixin", "VertexFormatMixin", @@ -22,8 +23,6 @@ "instancemanage.InstanceAddMixin", "instancemanage.InstanceRemoveMixin", "instancemanage.InstanceUpdateMixin", - "light.LightUpdateMixin", - "light.NetworkLightUpdateMixin", "matrix.Matrix3fAccessor", "matrix.Matrix4fAccessor", "matrix.PoseStackMixin"