The name game

- Rename the light visuals to have distinct names
- Rename SectionProperty -> SectionCollector because it isn't really a
  property
This commit is contained in:
Jozufozu 2024-07-14 17:13:53 -07:00
parent caa02f2666
commit 1ea94a859e
9 changed files with 53 additions and 50 deletions

View file

@ -5,9 +5,9 @@ package dev.engine_room.flywheel.api.visual;
* *
* <p>If your visual moves around in the level at all, you should use {@link TickableVisual} or {@link DynamicVisual}, * <p>If your visual moves around in the level at all, you should use {@link TickableVisual} or {@link DynamicVisual},
* and poll for light yourself along with listening for updates. When your visual moves to a different section, call * and poll for light yourself along with listening for updates. When your visual moves to a different section, call
* {@link SectionProperty#lightSections}.</p> * {@link SectionCollector#sections}.</p>
*/ */
public non-sealed interface LitVisual extends SectionTrackedVisual { public non-sealed interface LightUpdatedVisual extends SectionTrackedVisual {
/** /**
* Called when a section this visual is contained in receives a light update. * Called when a section this visual is contained in receives a light update.
* *

View file

@ -4,7 +4,7 @@ import org.jetbrains.annotations.ApiStatus;
import it.unimi.dsi.fastutil.longs.LongSet; import it.unimi.dsi.fastutil.longs.LongSet;
public sealed interface SectionTrackedVisual extends Visual permits SmoothLitVisual, LitVisual { public sealed interface SectionTrackedVisual extends Visual permits ShaderLightVisual, LightUpdatedVisual {
/** /**
* Set the section property object. * Set the section property object.
* *
@ -14,13 +14,13 @@ public sealed interface SectionTrackedVisual extends Visual permits SmoothLitVis
* *
* @param property The property. * @param property The property.
*/ */
void setSectionProperty(SectionProperty property); void setSectionCollector(SectionCollector property);
@ApiStatus.NonExtendable @ApiStatus.NonExtendable
interface SectionProperty { interface SectionCollector {
/** /**
* Assign the set of sections this visual wants to track itself in. * Assign the set of sections this visual wants to track itself in.
*/ */
void lightSections(LongSet sections); void sections(LongSet sections);
} }
} }

View file

@ -3,12 +3,12 @@ package dev.engine_room.flywheel.api.visual;
/** /**
* A marker interface allowing visuals to request light data on the GPU for a set of sections. * A marker interface allowing visuals to request light data on the GPU for a set of sections.
* *
* <p> Sections passed into {@link SectionProperty#lightSections} will have their light data handed to the * <p> Sections passed into {@link SectionCollector#sections} will have their light data handed to the
* backend and queryable by {@code flw_light*} functions in shaders. * backend and queryable by {@code flw_light*} functions in shaders.
* <br> * <br>
* Note that the queryable light data is shared across all visuals, so even if one specific visual does not * Note that the queryable light data is shared across all visuals, so even if one specific visual does not
* request a given section, the data will be available if another visual does. * request a given section, the data will be available if another visual does.
*/ */
public non-sealed interface SmoothLitVisual extends SectionTrackedVisual { public non-sealed interface ShaderLightVisual extends SectionTrackedVisual {
} }

View file

@ -5,7 +5,7 @@ package dev.engine_room.flywheel.api.visual;
* *
* @see DynamicVisual * @see DynamicVisual
* @see TickableVisual * @see TickableVisual
* @see LitVisual * @see LightUpdatedVisual
*/ */
public interface Visual { public interface Visual {
/** /**

View file

@ -5,7 +5,8 @@ import org.joml.FrustumIntersection;
import dev.engine_room.flywheel.api.visual.BlockEntityVisual; import dev.engine_room.flywheel.api.visual.BlockEntityVisual;
import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visual.DynamicVisual;
import dev.engine_room.flywheel.api.visual.LitVisual; import dev.engine_room.flywheel.api.visual.LightUpdatedVisual;
import dev.engine_room.flywheel.api.visual.ShaderLightVisual;
import dev.engine_room.flywheel.api.visual.TickableVisual; import dev.engine_room.flywheel.api.visual.TickableVisual;
import dev.engine_room.flywheel.api.visualization.VisualManager; import dev.engine_room.flywheel.api.visualization.VisualManager;
import dev.engine_room.flywheel.api.visualization.VisualizationContext; import dev.engine_room.flywheel.api.visualization.VisualizationContext;
@ -25,6 +26,8 @@ import net.minecraft.world.level.block.state.BlockState;
* <ul> * <ul>
* <li>{@link DynamicVisual}</li> * <li>{@link DynamicVisual}</li>
* <li>{@link TickableVisual}</li> * <li>{@link TickableVisual}</li>
* <li>{@link LightUpdatedVisual}</li>
* <li>{@link ShaderLightVisual}</li>
* </ul> * </ul>
* See the interfaces' documentation for more information about each one. * See the interfaces' documentation for more information about each one.
* *
@ -33,13 +36,13 @@ import net.minecraft.world.level.block.state.BlockState;
* *
* @param <T> The type of {@link BlockEntity}. * @param <T> The type of {@link BlockEntity}.
*/ */
public abstract class AbstractBlockEntityVisual<T extends BlockEntity> extends AbstractVisual implements BlockEntityVisual<T>, LitVisual { public abstract class AbstractBlockEntityVisual<T extends BlockEntity> extends AbstractVisual implements BlockEntityVisual<T>, LightUpdatedVisual {
protected final T blockEntity; protected final T blockEntity;
protected final BlockPos pos; protected final BlockPos pos;
protected final BlockPos visualPos; protected final BlockPos visualPos;
protected final BlockState blockState; protected final BlockState blockState;
@Nullable @Nullable
protected SectionProperty lightSections; protected SectionCollector lightSections;
public AbstractBlockEntityVisual(VisualizationContext ctx, T blockEntity, float partialTick) { public AbstractBlockEntityVisual(VisualizationContext ctx, T blockEntity, float partialTick) {
super(ctx, blockEntity.getLevel(), partialTick); super(ctx, blockEntity.getLevel(), partialTick);
@ -50,9 +53,9 @@ public abstract class AbstractBlockEntityVisual<T extends BlockEntity> extends A
} }
@Override @Override
public void setSectionProperty(SectionProperty property) { public void setSectionCollector(SectionCollector sectionCollector) {
this.lightSections = property; this.lightSections = sectionCollector;
lightSections.lightSections(LongSet.of(SectionPos.asLong(pos))); lightSections.sections(LongSet.of(SectionPos.asLong(pos)));
} }
/** /**

View file

@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicLong;
import dev.engine_room.flywheel.api.task.Plan; import dev.engine_room.flywheel.api.task.Plan;
import dev.engine_room.flywheel.api.task.TaskExecutor; import dev.engine_room.flywheel.api.task.TaskExecutor;
import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visual.DynamicVisual;
import dev.engine_room.flywheel.api.visual.LitVisual; import dev.engine_room.flywheel.api.visual.LightUpdatedVisual;
import dev.engine_room.flywheel.lib.task.Distribute; import dev.engine_room.flywheel.lib.task.Distribute;
import dev.engine_room.flywheel.lib.task.SimplyComposedPlan; import dev.engine_room.flywheel.lib.task.SimplyComposedPlan;
import dev.engine_room.flywheel.lib.task.Synchronizer; import dev.engine_room.flywheel.lib.task.Synchronizer;
@ -24,11 +24,11 @@ import it.unimi.dsi.fastutil.objects.ObjectArrayList;
/** /**
* Keeps track of what chunks/sections each listener is in, so we can update exactly what needs to be updated. * Keeps track of what chunks/sections each listener is in, so we can update exactly what needs to be updated.
*/ */
public class LitVisualStorage { public class LightUpdatedStorage {
private static final long NEVER_UPDATED = Long.MIN_VALUE; private static final long NEVER_UPDATED = Long.MIN_VALUE;
private static final long INITIAL_UPDATE_ID = NEVER_UPDATED + 1; private static final long INITIAL_UPDATE_ID = NEVER_UPDATED + 1;
private final Map<LitVisual, LongSet> visuals2Sections = new WeakHashMap<>(); private final Map<LightUpdatedVisual, LongSet> visuals2Sections = new WeakHashMap<>();
private final Long2ObjectMap<List<Updater>> sections2Visuals = new Long2ObjectOpenHashMap<>(); private final Long2ObjectMap<List<Updater>> sections2Visuals = new Long2ObjectOpenHashMap<>();
private final Queue<MovedVisual> movedVisuals = new ConcurrentLinkedQueue<>(); private final Queue<MovedVisual> movedVisuals = new ConcurrentLinkedQueue<>();
@ -90,14 +90,14 @@ public class LitVisualStorage {
return visuals2Sections.isEmpty(); return visuals2Sections.isEmpty();
} }
public void add(SectionPropertyImpl tracker, LitVisual visual) { public void add(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
var moved = new MovedVisual(tracker, visual); var moved = new MovedVisual(tracker, visual);
tracker.addListener(() -> movedVisuals.add(moved)); tracker.addListener(() -> movedVisuals.add(moved));
updateTracking(tracker, visual); updateTracking(tracker, visual);
} }
public void updateTracking(SectionPropertyImpl tracker, LitVisual visual) { public void updateTracking(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
if (tracker.sections.isEmpty()) { if (tracker.sections.isEmpty()) {
// Add the visual to the map even if sections is empty, this way we can distinguish from deleted visuals // Add the visual to the map even if sections is empty, this way we can distinguish from deleted visuals
visuals2Sections.put(visual, LongSet.of()); visuals2Sections.put(visual, LongSet.of());
@ -129,7 +129,7 @@ public class LitVisualStorage {
* @param visual The visual to remove. * @param visual The visual to remove.
* @return {@code true} if the visual was removed, {@code false} otherwise. * @return {@code true} if the visual was removed, {@code false} otherwise.
*/ */
public boolean remove(LitVisual visual) { public boolean remove(LightUpdatedVisual visual) {
var sections = visuals2Sections.remove(visual); var sections = visuals2Sections.remove(visual);
if (sections == null) { if (sections == null) {
@ -152,7 +152,7 @@ public class LitVisualStorage {
sectionsUpdatedThisFrame.clear(); sectionsUpdatedThisFrame.clear();
} }
private static int indexOfUpdater(List<Updater> listeners, LitVisual visual) { private static int indexOfUpdater(List<Updater> listeners, LightUpdatedVisual visual) {
for (int i = 0; i < listeners.size(); i++) { for (int i = 0; i < listeners.size(); i++) {
if (listeners.get(i) if (listeners.get(i)
.visual() == visual) { .visual() == visual) {
@ -162,7 +162,7 @@ public class LitVisualStorage {
return -1; return -1;
} }
private static Updater createUpdater(LitVisual visual, int sectionCount) { private static Updater createUpdater(LightUpdatedVisual visual, int sectionCount) {
if (sectionCount == 1) { if (sectionCount == 1) {
return new Updater.Simple(visual); return new Updater.Simple(visual);
} else { } else {
@ -174,10 +174,10 @@ public class LitVisualStorage {
sealed interface Updater { sealed interface Updater {
void updateLight(Context ctx); void updateLight(Context ctx);
LitVisual visual(); LightUpdatedVisual visual();
// The visual is only in one section. In this case, we can just update the visual directly. // The visual is only in one section. In this case, we can just update the visual directly.
record Simple(LitVisual visual) implements Updater { record Simple(LightUpdatedVisual visual) implements Updater {
@Override @Override
public void updateLight(Context ctx) { public void updateLight(Context ctx) {
visual.updateLight(ctx.partialTick); visual.updateLight(ctx.partialTick);
@ -186,7 +186,7 @@ public class LitVisualStorage {
// The visual is in multiple sections. Here we need to make sure that the visual only gets updated once, // The visual is in multiple sections. Here we need to make sure that the visual only gets updated once,
// even when multiple sections it was contained in are updated at the same time. // even when multiple sections it was contained in are updated at the same time.
record Synced(LitVisual visual, AtomicLong updateId) implements Updater { record Synced(LightUpdatedVisual visual, AtomicLong updateId) implements Updater {
@Override @Override
public void updateLight(Context ctx) { public void updateLight(Context ctx) {
// Different update ID means we won, so we can update the visual. // Different update ID means we won, so we can update the visual.
@ -201,6 +201,6 @@ public class LitVisualStorage {
} }
} }
private record MovedVisual(SectionPropertyImpl tracker, LitVisual visual) { private record MovedVisual(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
} }
} }

View file

@ -3,17 +3,17 @@ package dev.engine_room.flywheel.impl.visualization.storage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import dev.engine_room.flywheel.api.visual.SmoothLitVisual; import dev.engine_room.flywheel.api.visual.SectionTrackedVisual;
import it.unimi.dsi.fastutil.longs.LongArraySet; import it.unimi.dsi.fastutil.longs.LongArraySet;
import it.unimi.dsi.fastutil.longs.LongSet; import it.unimi.dsi.fastutil.longs.LongSet;
public class SectionPropertyImpl implements SmoothLitVisual.SectionProperty { public class SectionCollectorImpl implements SectionTrackedVisual.SectionCollector {
public final LongSet sections = new LongArraySet(); public final LongSet sections = new LongArraySet();
private final List<Runnable> listeners = new ArrayList<>(2); private final List<Runnable> listeners = new ArrayList<>(2);
@Override @Override
public void lightSections(LongSet sections) { public void sections(LongSet sections) {
this.sections.clear(); this.sections.clear();
this.sections.addAll(sections); this.sections.addAll(sections);

View file

@ -4,13 +4,13 @@ import java.util.Map;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import dev.engine_room.flywheel.api.visual.SmoothLitVisual; import dev.engine_room.flywheel.api.visual.ShaderLightVisual;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet; import it.unimi.dsi.fastutil.longs.LongSet;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
public class SmoothLitVisualStorage { public class ShaderLightStorage {
private final Map<SmoothLitVisual, SectionPropertyImpl> visuals = new Reference2ObjectOpenHashMap<>(); private final Map<ShaderLightVisual, SectionCollectorImpl> visuals = new Reference2ObjectOpenHashMap<>();
@Nullable @Nullable
private LongSet cachedSections; private LongSet cachedSections;
@ -31,12 +31,12 @@ public class SmoothLitVisualStorage {
return cachedSections; return cachedSections;
} }
public void remove(SmoothLitVisual smoothLit) { public void remove(ShaderLightVisual visual) {
visuals.remove(smoothLit); visuals.remove(visual);
} }
public void add(SectionPropertyImpl tracker, SmoothLitVisual smoothLit) { public void add(SectionCollectorImpl tracker, ShaderLightVisual visual) {
visuals.put(smoothLit, tracker); visuals.put(visual, tracker);
tracker.addListener(this::markDirty); tracker.addListener(this::markDirty);

View file

@ -10,9 +10,9 @@ import org.jetbrains.annotations.Nullable;
import dev.engine_room.flywheel.api.task.Plan; import dev.engine_room.flywheel.api.task.Plan;
import dev.engine_room.flywheel.api.visual.DynamicVisual; import dev.engine_room.flywheel.api.visual.DynamicVisual;
import dev.engine_room.flywheel.api.visual.LitVisual; import dev.engine_room.flywheel.api.visual.LightUpdatedVisual;
import dev.engine_room.flywheel.api.visual.SectionTrackedVisual; import dev.engine_room.flywheel.api.visual.SectionTrackedVisual;
import dev.engine_room.flywheel.api.visual.SmoothLitVisual; import dev.engine_room.flywheel.api.visual.ShaderLightVisual;
import dev.engine_room.flywheel.api.visual.TickableVisual; import dev.engine_room.flywheel.api.visual.TickableVisual;
import dev.engine_room.flywheel.api.visual.Visual; import dev.engine_room.flywheel.api.visual.Visual;
import dev.engine_room.flywheel.api.visualization.VisualizationContext; import dev.engine_room.flywheel.api.visualization.VisualizationContext;
@ -29,8 +29,8 @@ public abstract class Storage<T> {
protected final PlanMap<TickableVisual, TickableVisual.Context> tickableVisuals = new PlanMap<>(); protected final PlanMap<TickableVisual, TickableVisual.Context> tickableVisuals = new PlanMap<>();
protected final List<SimpleDynamicVisual> simpleDynamicVisuals = new ArrayList<>(); protected final List<SimpleDynamicVisual> simpleDynamicVisuals = new ArrayList<>();
protected final List<SimpleTickableVisual> simpleTickableVisuals = new ArrayList<>(); protected final List<SimpleTickableVisual> simpleTickableVisuals = new ArrayList<>();
protected final LitVisualStorage litVisuals = new LitVisualStorage(); protected final LightUpdatedStorage litVisuals = new LightUpdatedStorage();
protected final SmoothLitVisualStorage smoothLitVisuals = new SmoothLitVisualStorage(); protected final ShaderLightStorage smoothLitVisuals = new ShaderLightStorage();
private final Map<T, Visual> visuals = new Reference2ObjectOpenHashMap<>(); private final Map<T, Visual> visuals = new Reference2ObjectOpenHashMap<>();
@ -71,10 +71,10 @@ public abstract class Storage<T> {
dynamicVisuals.remove(dynamic); dynamicVisuals.remove(dynamic);
} }
} }
if (visual instanceof LitVisual lit) { if (visual instanceof LightUpdatedVisual lit) {
litVisuals.remove(lit); litVisuals.remove(lit);
} }
if (visual instanceof SmoothLitVisual smoothLit) { if (visual instanceof ShaderLightVisual smoothLit) {
smoothLitVisuals.remove(smoothLit); smoothLitVisuals.remove(smoothLit);
} }
visual.delete(); visual.delete();
@ -161,17 +161,17 @@ public abstract class Storage<T> {
} }
if (visual instanceof SectionTrackedVisual tracked) { if (visual instanceof SectionTrackedVisual tracked) {
SectionPropertyImpl sectionProperty = new SectionPropertyImpl(); SectionCollectorImpl sectionProperty = new SectionCollectorImpl();
// Give the visual a chance to fill in the property. // Give the visual a chance to fill in the property.
tracked.setSectionProperty(sectionProperty); tracked.setSectionCollector(sectionProperty);
if (visual instanceof LitVisual lit) { if (visual instanceof LightUpdatedVisual lightUpdated) {
litVisuals.add(sectionProperty, lit); litVisuals.add(sectionProperty, lightUpdated);
} }
if (visual instanceof SmoothLitVisual smoothLit) { if (visual instanceof ShaderLightVisual shaderLight) {
smoothLitVisuals.add(sectionProperty, smoothLit); smoothLitVisuals.add(sectionProperty, shaderLight);
} }
} }
} }
@ -183,7 +183,7 @@ public abstract class Storage<T> {
*/ */
public abstract boolean willAccept(T obj); public abstract boolean willAccept(T obj);
public SmoothLitVisualStorage smoothLitStorage() { public ShaderLightStorage smoothLitStorage() {
return smoothLitVisuals; return smoothLitVisuals;
} }
} }