Skip to content

Commit

Permalink
Wait a minute
Browse files Browse the repository at this point in the history
- Add debug flag to disable frame/tick plan execution
- Should help debugging cases where a visual constructor produces a
  visual in an invalid state like the weeping shulker bug
  • Loading branch information
Jozufozu committed Nov 16, 2024
1 parent f3845a1 commit 3811da1
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.engine_room.flywheel.backend;

public class BackendDebugFlags {
public static boolean LIGHT_STORAGE_VIEW = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.engine_room.flywheel.api.visual.EffectVisual;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
import dev.engine_room.flywheel.api.visualization.VisualizationManager;
import dev.engine_room.flywheel.backend.BackendDebugFlags;
import dev.engine_room.flywheel.backend.SkyLightSectionStorageExtension;
import dev.engine_room.flywheel.backend.engine.indirect.StagingBuffer;
import dev.engine_room.flywheel.backend.gl.buffer.GlBuffer;
Expand Down Expand Up @@ -49,8 +50,6 @@
* <p>Thus, each section occupies 5832 bytes.
*/
public class LightStorage implements Effect {
public static boolean DEBUG = false;

public static final int BLOCKS_PER_SECTION = 18 * 18 * 18;
public static final int LIGHT_SIZE_BYTES = BLOCKS_PER_SECTION;
public static final int SOLID_SIZE_BYTES = MoreMath.ceilingDiv(BLOCKS_PER_SECTION, Integer.SIZE) * Integer.BYTES;
Expand Down Expand Up @@ -108,20 +107,20 @@ public void onLightUpdate(long section) {

public <C> Plan<C> createFramePlan() {
return SimplePlan.of(() -> {
if (DEBUG != isDebugOn) {
if (BackendDebugFlags.LIGHT_STORAGE_VIEW != isDebugOn) {
var visualizationManager = VisualizationManager.get(level);

// Really should be non-null, but just in case.
if (visualizationManager != null) {
if (DEBUG) {
if (BackendDebugFlags.LIGHT_STORAGE_VIEW) {
visualizationManager.effects()
.queueAdd(this);
} else {
visualizationManager.effects()
.queueRemove(this);
}
}
isDebugOn = DEBUG;
isDebugOn = BackendDebugFlags.LIGHT_STORAGE_VIEW;
}

if (updatedSections.isEmpty() && requestedSections == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package dev.engine_room.flywheel.lib.task;

import dev.engine_room.flywheel.api.task.Plan;
import dev.engine_room.flywheel.api.task.TaskExecutor;
import dev.engine_room.flywheel.lib.task.functional.BooleanSupplierWithContext;

/**
* Executes one plan or another, depending on a dynamically evaluated condition.
*
* @param condition The condition to branch on.
* @param onTrue The plan to execute if the condition is true.
* @param <C> The type of the context object.
*/
public record ConditionalPlan<C>(BooleanSupplierWithContext<C> condition,
Plan<C> onTrue) implements SimplyComposedPlan<C> {
public static <C> Builder<C> on(BooleanSupplierWithContext<C> condition) {
return new Builder<>(condition);
}

public static <C> Builder<C> on(BooleanSupplierWithContext.Ignored<C> condition) {
return new Builder<>(condition);
}

@Override
public void execute(TaskExecutor taskExecutor, C context, Runnable onCompletion) {
if (condition.getAsBoolean(context)) {
onTrue.execute(taskExecutor, context, onCompletion);
} else {
onCompletion.run();
}
}

public static final class Builder<C> {
private final BooleanSupplierWithContext<C> condition;

public Builder(BooleanSupplierWithContext<C> condition) {
this.condition = condition;
}

public ConditionalPlan<C> then(Plan<C> onTrue) {
return new ConditionalPlan<>(condition, onTrue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.engine_room.flywheel.impl;

public class ImplDebugFlags {
/**
* Debug flag to globally turn beginFrame/tick off.
*/
public static boolean PAUSE_UPDATES = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import dev.engine_room.flywheel.api.visual.TickableVisual;
import dev.engine_room.flywheel.api.visual.Visual;
import dev.engine_room.flywheel.api.visualization.VisualizationContext;
import dev.engine_room.flywheel.impl.ImplDebugFlags;
import dev.engine_room.flywheel.lib.task.ConditionalPlan;
import dev.engine_room.flywheel.lib.task.ForEachPlan;
import dev.engine_room.flywheel.lib.task.NestedPlan;
import dev.engine_room.flywheel.lib.task.PlanMap;
Expand Down Expand Up @@ -42,11 +44,16 @@ public Collection<Visual> getAllVisuals() {
}

public Plan<DynamicVisual.Context> framePlan() {
return NestedPlan.of(dynamicVisuals, lightUpdatedVisuals.plan(), ForEachPlan.of(() -> simpleDynamicVisuals, SimpleDynamicVisual::beginFrame));
var update = ConditionalPlan.<DynamicVisual.Context>on(() -> !ImplDebugFlags.PAUSE_UPDATES)
.then(NestedPlan.of(dynamicVisuals, ForEachPlan.of(() -> simpleDynamicVisuals, SimpleDynamicVisual::beginFrame)));

// Do light updates regardless.
return NestedPlan.of(lightUpdatedVisuals.plan(), update);
}

public Plan<TickableVisual.Context> tickPlan() {
return NestedPlan.of(tickableVisuals, ForEachPlan.of(() -> simpleTickableVisuals, SimpleTickableVisual::tick));
return ConditionalPlan.<TickableVisual.Context>on(() -> !ImplDebugFlags.PAUSE_UPDATES)
.then(NestedPlan.of(tickableVisuals, ForEachPlan.of(() -> simpleTickableVisuals, SimpleTickableVisual::tick)));
}

public LightUpdatedVisualStorage lightUpdatedVisuals() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import dev.engine_room.flywheel.api.backend.Backend;
import dev.engine_room.flywheel.api.backend.BackendManager;
import dev.engine_room.flywheel.backend.BackendDebugFlags;
import dev.engine_room.flywheel.backend.compile.LightSmoothness;
import dev.engine_room.flywheel.backend.compile.PipelineCompiler;
import dev.engine_room.flywheel.backend.engine.LightStorage;
import dev.engine_room.flywheel.backend.engine.uniform.DebugMode;
import dev.engine_room.flywheel.backend.engine.uniform.FrameUniforms;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
Expand Down Expand Up @@ -179,12 +179,24 @@ private static LiteralArgumentBuilder<FabricClientCommandSource> createDebugComm
debug.then(ClientCommandManager.literal("lightSections")
.then(ClientCommandManager.literal("on")
.executes(context -> {
LightStorage.DEBUG = true;
BackendDebugFlags.LIGHT_STORAGE_VIEW = true;
return Command.SINGLE_SUCCESS;
}))
.then(ClientCommandManager.literal("off")
.executes(context -> {
LightStorage.DEBUG = false;
BackendDebugFlags.LIGHT_STORAGE_VIEW = false;
return Command.SINGLE_SUCCESS;
})));

debug.then(ClientCommandManager.literal("pauseUpdates")
.then(ClientCommandManager.literal("on")
.executes(context -> {
ImplDebugFlags.PAUSE_UPDATES = true;
return Command.SINGLE_SUCCESS;
}))
.then(ClientCommandManager.literal("off")
.executes(context -> {
ImplDebugFlags.PAUSE_UPDATES = false;
return Command.SINGLE_SUCCESS;
})));

Expand Down
19 changes: 16 additions & 3 deletions forge/src/main/java/dev/engine_room/flywheel/impl/FlwCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import dev.engine_room.flywheel.api.backend.Backend;
import dev.engine_room.flywheel.api.backend.BackendManager;
import dev.engine_room.flywheel.backend.BackendDebugFlags;
import dev.engine_room.flywheel.backend.compile.LightSmoothness;
import dev.engine_room.flywheel.backend.compile.PipelineCompiler;
import dev.engine_room.flywheel.backend.engine.LightStorage;
import dev.engine_room.flywheel.backend.engine.uniform.DebugMode;
import dev.engine_room.flywheel.backend.engine.uniform.FrameUniforms;
import net.minecraft.client.Minecraft;
Expand Down Expand Up @@ -172,14 +172,27 @@ private static LiteralArgumentBuilder<CommandSourceStack> createDebugCommand() {
debug.then(Commands.literal("lightSections")
.then(Commands.literal("on")
.executes(context -> {
LightStorage.DEBUG = true;
BackendDebugFlags.LIGHT_STORAGE_VIEW = true;
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("off")
.executes(context -> {
LightStorage.DEBUG = false;
BackendDebugFlags.LIGHT_STORAGE_VIEW = false;
return Command.SINGLE_SUCCESS;
})));

debug.then(Commands.literal("pauseUpdates")
.then(Commands.literal("on")
.executes(context -> {
ImplDebugFlags.PAUSE_UPDATES = true;
return Command.SINGLE_SUCCESS;
}))
.then(Commands.literal("off")
.executes(context -> {
ImplDebugFlags.PAUSE_UPDATES = false;
return Command.SINGLE_SUCCESS;
})));

return debug;
}

Expand Down

0 comments on commit 3811da1

Please sign in to comment.