Skip to content

Commit

Permalink
The name game
Browse files Browse the repository at this point in the history
- Rename the light visuals to have distinct names
- Rename SectionProperty -> SectionCollector because it isn't really a
  property
  • Loading branch information
Jozufozu committed Jul 15, 2024
1 parent 1253024 commit 8a8258e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*
* <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
* {@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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

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.
*
Expand All @@ -14,13 +14,13 @@ public sealed interface SectionTrackedVisual extends Visual permits SmoothLitVis
*
* @param property The property.
*/
void setSectionProperty(SectionProperty property);
void setSectionCollector(SectionCollector property);

@ApiStatus.NonExtendable
interface SectionProperty {
interface SectionCollector {
/**
* Assign the set of sections this visual wants to track itself in.
*/
void lightSections(LongSet sections);
void sections(LongSet sections);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
/**
* 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.
* <br>
* 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.
*/
public non-sealed interface SmoothLitVisual extends SectionTrackedVisual {
public non-sealed interface ShaderLightVisual extends SectionTrackedVisual {

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*
* @see DynamicVisual
* @see TickableVisual
* @see LitVisual
* @see LightUpdatedVisual
*/
public interface Visual {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import dev.engine_room.flywheel.api.visual.BlockEntityVisual;
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.visualization.VisualManager;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
Expand All @@ -25,6 +26,8 @@
* <ul>
* <li>{@link DynamicVisual}</li>
* <li>{@link TickableVisual}</li>
* <li>{@link LightUpdatedVisual}</li>
* <li>{@link ShaderLightVisual}</li>
* </ul>
* See the interfaces' documentation for more information about each one.
*
Expand All @@ -33,13 +36,13 @@
*
* @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 BlockPos pos;
protected final BlockPos visualPos;
protected final BlockState blockState;
@Nullable
protected SectionProperty lightSections;
protected SectionCollector lightSections;

public AbstractBlockEntityVisual(VisualizationContext ctx, T blockEntity, float partialTick) {
super(ctx, blockEntity.getLevel(), partialTick);
Expand All @@ -50,9 +53,9 @@ public AbstractBlockEntityVisual(VisualizationContext ctx, T blockEntity, float
}

@Override
public void setSectionProperty(SectionProperty property) {
this.lightSections = property;
lightSections.lightSections(LongSet.of(SectionPos.asLong(pos)));
public void setSectionCollector(SectionCollector sectionCollector) {
this.lightSections = sectionCollector;
lightSections.sections(LongSet.of(SectionPos.asLong(pos)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import dev.engine_room.flywheel.api.task.Plan;
import dev.engine_room.flywheel.api.task.TaskExecutor;
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.SimplyComposedPlan;
import dev.engine_room.flywheel.lib.task.Synchronizer;
Expand All @@ -24,11 +24,11 @@
/**
* 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 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 Queue<MovedVisual> movedVisuals = new ConcurrentLinkedQueue<>();
Expand Down Expand Up @@ -90,14 +90,14 @@ public boolean isEmpty() {
return visuals2Sections.isEmpty();
}

public void add(SectionPropertyImpl tracker, LitVisual visual) {
public void add(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
var moved = new MovedVisual(tracker, visual);
tracker.addListener(() -> movedVisuals.add(moved));

updateTracking(tracker, visual);
}

public void updateTracking(SectionPropertyImpl tracker, LitVisual visual) {
public void updateTracking(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
if (tracker.sections.isEmpty()) {
// Add the visual to the map even if sections is empty, this way we can distinguish from deleted visuals
visuals2Sections.put(visual, LongSet.of());
Expand Down Expand Up @@ -129,7 +129,7 @@ public void enqueueLightUpdateSection(long section) {
* @param visual The visual to remove.
* @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);

if (sections == null) {
Expand All @@ -152,7 +152,7 @@ public void 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++) {
if (listeners.get(i)
.visual() == visual) {
Expand All @@ -162,7 +162,7 @@ private static int indexOfUpdater(List<Updater> listeners, LitVisual visual) {
return -1;
}

private static Updater createUpdater(LitVisual visual, int sectionCount) {
private static Updater createUpdater(LightUpdatedVisual visual, int sectionCount) {
if (sectionCount == 1) {
return new Updater.Simple(visual);
} else {
Expand All @@ -174,10 +174,10 @@ private static Updater createUpdater(LitVisual visual, int sectionCount) {
sealed interface Updater {
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.
record Simple(LitVisual visual) implements Updater {
record Simple(LightUpdatedVisual visual) implements Updater {
@Override
public void updateLight(Context ctx) {
visual.updateLight(ctx.partialTick);
Expand All @@ -186,7 +186,7 @@ public void updateLight(Context ctx) {

// 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.
record Synced(LitVisual visual, AtomicLong updateId) implements Updater {
record Synced(LightUpdatedVisual visual, AtomicLong updateId) implements Updater {
@Override
public void updateLight(Context ctx) {
// Different update ID means we won, so we can update the visual.
Expand All @@ -201,6 +201,6 @@ record Context(long updateId, float partialTick) {
}
}

private record MovedVisual(SectionPropertyImpl tracker, LitVisual visual) {
private record MovedVisual(SectionCollectorImpl tracker, LightUpdatedVisual visual) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import java.util.ArrayList;
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.LongSet;

public class SectionPropertyImpl implements SmoothLitVisual.SectionProperty {
public class SectionCollectorImpl implements SectionTrackedVisual.SectionCollector {
public final LongSet sections = new LongArraySet();

private final List<Runnable> listeners = new ArrayList<>(2);

@Override
public void lightSections(LongSet sections) {
public void sections(LongSet sections) {
this.sections.clear();
this.sections.addAll(sections);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

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.LongSet;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;

public class SmoothLitVisualStorage {
private final Map<SmoothLitVisual, SectionPropertyImpl> visuals = new Reference2ObjectOpenHashMap<>();
public class ShaderLightStorage {
private final Map<ShaderLightVisual, SectionCollectorImpl> visuals = new Reference2ObjectOpenHashMap<>();

@Nullable
private LongSet cachedSections;
Expand All @@ -31,12 +31,12 @@ public LongSet sections() {
return cachedSections;
}

public void remove(SmoothLitVisual smoothLit) {
visuals.remove(smoothLit);
public void remove(ShaderLightVisual visual) {
visuals.remove(visual);
}

public void add(SectionPropertyImpl tracker, SmoothLitVisual smoothLit) {
visuals.put(smoothLit, tracker);
public void add(SectionCollectorImpl tracker, ShaderLightVisual visual) {
visuals.put(visual, tracker);

tracker.addListener(this::markDirty);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import dev.engine_room.flywheel.api.task.Plan;
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.SmoothLitVisual;
import dev.engine_room.flywheel.api.visual.ShaderLightVisual;
import dev.engine_room.flywheel.api.visual.TickableVisual;
import dev.engine_room.flywheel.api.visual.Visual;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
Expand All @@ -29,8 +29,8 @@ public abstract class Storage<T> {
protected final PlanMap<TickableVisual, TickableVisual.Context> tickableVisuals = new PlanMap<>();
protected final List<SimpleDynamicVisual> simpleDynamicVisuals = new ArrayList<>();
protected final List<SimpleTickableVisual> simpleTickableVisuals = new ArrayList<>();
protected final LitVisualStorage litVisuals = new LitVisualStorage();
protected final SmoothLitVisualStorage smoothLitVisuals = new SmoothLitVisualStorage();
protected final LightUpdatedStorage litVisuals = new LightUpdatedStorage();
protected final ShaderLightStorage smoothLitVisuals = new ShaderLightStorage();

private final Map<T, Visual> visuals = new Reference2ObjectOpenHashMap<>();

Expand Down Expand Up @@ -71,10 +71,10 @@ public void remove(T obj) {
dynamicVisuals.remove(dynamic);
}
}
if (visual instanceof LitVisual lit) {
if (visual instanceof LightUpdatedVisual lit) {
litVisuals.remove(lit);
}
if (visual instanceof SmoothLitVisual smoothLit) {
if (visual instanceof ShaderLightVisual smoothLit) {
smoothLitVisuals.remove(smoothLit);
}
visual.delete();
Expand Down Expand Up @@ -161,17 +161,17 @@ private void setup(Visual visual) {
}

if (visual instanceof SectionTrackedVisual tracked) {
SectionPropertyImpl sectionProperty = new SectionPropertyImpl();
SectionCollectorImpl sectionProperty = new SectionCollectorImpl();

// Give the visual a chance to fill in the property.
tracked.setSectionProperty(sectionProperty);
tracked.setSectionCollector(sectionProperty);

if (visual instanceof LitVisual lit) {
litVisuals.add(sectionProperty, lit);
if (visual instanceof LightUpdatedVisual lightUpdated) {
litVisuals.add(sectionProperty, lightUpdated);
}

if (visual instanceof SmoothLitVisual smoothLit) {
smoothLitVisuals.add(sectionProperty, smoothLit);
if (visual instanceof ShaderLightVisual shaderLight) {
smoothLitVisuals.add(sectionProperty, shaderLight);
}
}
}
Expand All @@ -183,7 +183,7 @@ private void setup(Visual visual) {
*/
public abstract boolean willAccept(T obj);

public SmoothLitVisualStorage smoothLitStorage() {
public ShaderLightStorage smoothLitStorage() {
return smoothLitVisuals;
}
}

0 comments on commit 8a8258e

Please sign in to comment.