diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java index 5c65dd3f4..3f4754d3b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/FrameUniforms.java @@ -15,8 +15,10 @@ import net.minecraft.world.phys.Vec3; public class FrameUniforms implements UniformProvider { public static final int SIZE = 304; + public int debugMode; + public int debugOverlay; - @Nullable + @Nullable private RenderContext context; private final Matrix4f viewProjection = new Matrix4f(); @@ -70,7 +72,10 @@ public class FrameUniforms implements UniformProvider { MemoryUtil.memPutInt(ptr, getConstantAmbientLightFlag(context)); ptr += 4; - writeTime(ptr); + ptr = writeTime(ptr); + + MemoryUtil.memPutInt(ptr, debugMode); + MemoryUtil.memPutInt(ptr + 4, debugOverlay); } private long writeMatrices(long ptr) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java index cf941ab04..46924651e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java +++ b/src/main/java/com/jozufozu/flywheel/backend/engine/uniform/Uniforms.java @@ -3,6 +3,8 @@ package com.jozufozu.flywheel.backend.engine.uniform; import com.jozufozu.flywheel.api.event.ReloadLevelRendererEvent; import com.jozufozu.flywheel.api.event.RenderContext; import com.jozufozu.flywheel.backend.gl.GlStateTracker; +import com.jozufozu.flywheel.config.DebugMode; +import com.jozufozu.flywheel.config.DebugOverlay; public class Uniforms { public static boolean frustumPaused = false; @@ -53,6 +55,11 @@ public class Uniforms { ubo.update(); } + public static void setDebugMode(DebugMode mode, DebugOverlay visual) { + frame().provider.debugMode = mode.ordinal(); + frame().provider.debugOverlay = visual.ordinal(); + } + public static void onReloadLevelRenderer(ReloadLevelRendererEvent event) { if (frame != null) { frame.delete(); diff --git a/src/main/java/com/jozufozu/flywheel/config/DebugMode.java b/src/main/java/com/jozufozu/flywheel/config/DebugMode.java new file mode 100644 index 000000000..70179173c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/config/DebugMode.java @@ -0,0 +1,8 @@ +package com.jozufozu.flywheel.config; + +public enum DebugMode { + NONE, + NORMALS, + INSTANCE_ID, + LIGHT, +} diff --git a/src/main/java/com/jozufozu/flywheel/config/DebugOverlay.java b/src/main/java/com/jozufozu/flywheel/config/DebugOverlay.java new file mode 100644 index 000000000..61163eb53 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/config/DebugOverlay.java @@ -0,0 +1,7 @@ +package com.jozufozu.flywheel.config; + +public enum DebugOverlay { + NONE, + MUL, + OVERRIDE, +} diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index 58e4584c3..1f0e81ae6 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -23,6 +23,7 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; import net.minecraftforge.client.event.RegisterClientCommandsEvent; import net.minecraftforge.common.ForgeConfigSpec.ConfigValue; +import net.minecraftforge.server.command.EnumArgument; public class FlwCommands { public static void registerClientCommands(RegisterClientCommandsEvent event) { @@ -101,18 +102,32 @@ public class FlwCommands { } )); - // TODO - command.then(Commands.literal("debugNormals")) - .executes(context -> { - LocalPlayer player = Minecraft.getInstance().player; - if (player == null) return 0; + command.then(Commands.literal("debug") + .then(Commands.argument("mode", EnumArgument.enumArgument(DebugMode.class)) + .executes(context -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return 0; - player.displayClientMessage(Component.literal("This command is not yet implemented."), false); + DebugMode mode = context.getArgument("mode", DebugMode.class); - return Command.SINGLE_SUCCESS; - }); + Uniforms.setDebugMode(mode, DebugOverlay.OVERRIDE); - command.then(Commands.literal("debugCrumbling") + return Command.SINGLE_SUCCESS; + }) + .then(Commands.argument("overlay", EnumArgument.enumArgument(DebugOverlay.class)) + .executes(context -> { + LocalPlayer player = Minecraft.getInstance().player; + if (player == null) return 0; + + DebugMode mode = context.getArgument("mode", DebugMode.class); + DebugOverlay visual = context.getArgument("overlay", DebugOverlay.class); + + Uniforms.setDebugMode(mode, visual); + + return Command.SINGLE_SUCCESS; + })))); + + command.then(Commands.literal("crumbling") .then(Commands.argument("pos", BlockPosArgument.blockPos()) .then(Commands.argument("stage", IntegerArgumentType.integer(0, 9)) .executes(context -> { @@ -132,12 +147,7 @@ public class FlwCommands { return Command.SINGLE_SUCCESS; })))); - command.then(Commands.literal("debugFrustum") - .then(Commands.literal("pause") - .executes(context -> { - Uniforms.frustumPaused = true; - return 1; - })) + command.then(Commands.literal("frustum") .then(Commands.literal("unpause") .executes(context -> { Uniforms.frustumPaused = false; diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common.frag b/src/main/resources/assets/flywheel/flywheel/internal/common.frag index a88bdd6cc..661ae8d95 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/common.frag +++ b/src/main/resources/assets/flywheel/flywheel/internal/common.frag @@ -12,6 +12,8 @@ uniform sampler2D _flw_lightTex; out vec4 _flw_outputColor; +in vec4 _flw_debugColor; + void _flw_main() { flw_sampleColor = texture(_flw_diffuseTex, flw_vertexTexCoord); flw_fragColor = flw_vertexColor * flw_sampleColor; @@ -48,5 +50,14 @@ void _flw_main() { discard; } + switch (_flw_debugOverlay) { + case 1u: + color *= _flw_debugColor; + break; + case 2u: + color = _flw_debugColor; + break; + } + _flw_outputColor = flw_fogFilter(color); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/common.vert b/src/main/resources/assets/flywheel/flywheel/internal/common.vert index bb0b87f4d..761638149 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/common.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/common.vert @@ -1,6 +1,29 @@ #include "flywheel:internal/fog_distance.glsl" -void _flw_main(in FlwInstance instance) { +// https://stackoverflow.com/a/17479300 +uint _flw_hash(in uint x) { + x += (x << 10u); + x ^= (x >> 6u); + x += (x << 3u); + x ^= (x >> 11u); + x += (x << 15u); + return x; +} + +vec4 _flw_id2Color(in uint id) { + uint x = _flw_hash(id); + + return vec4( + float(x & 0xFFu) / 255.0, + float((x >> 8u) & 0xFFu) / 255.0, + float((x >> 16u) & 0xFFu) / 255.0, + 1. + ); +} + +out vec4 _flw_debugColor; + +void _flw_main(in FlwInstance instance, in uint stableInstanceID) { _flw_layoutVertex(); flw_beginVertex(); flw_instanceVertex(instance); @@ -12,4 +35,19 @@ void _flw_main(in FlwInstance instance) { flw_distance = fogDistance(flw_vertexPos.xyz, flw_cameraPos, flw_fogShape); gl_Position = flw_viewProjection * flw_vertexPos; + + switch (_flw_debugMode) { + case 0u: + _flw_debugColor = vec4(1.); + break; + case 1u: + _flw_debugColor = vec4(flw_vertexNormal * .5 + .5, 1.); + break; + case 2u: + _flw_debugColor = _flw_id2Color(stableInstanceID); + break; + case 3u: + _flw_debugColor = vec4(vec2((flw_vertexLight * 15.0 + 0.5) / 16.), 0., 1.); + break; + } } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert index f59d82268..68c48ae58 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/indirect/main.vert @@ -32,5 +32,5 @@ void main() { uint objectIndex = objectIndices[gl_BaseInstance + gl_InstanceID]; FlwInstance instance = _flw_unpackInstance(objects[objectIndex].instance); - _flw_main(instance); + _flw_main(instance, objectIndex); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert index 9bf8fd562..8d449146d 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert +++ b/src/main/resources/assets/flywheel/flywheel/internal/instancing/main.vert @@ -9,5 +9,5 @@ void main() { FlwInstance instance = _flw_unpackInstance(); - _flw_main(instance); + _flw_main(instance, uint(gl_InstanceID)); } diff --git a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl index 21560821d..8aee52fa6 100644 --- a/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl +++ b/src/main/resources/assets/flywheel/flywheel/internal/uniforms/frame.glsl @@ -29,6 +29,9 @@ layout(std140) uniform _FlwFrameUniforms { float flw_renderTicks; float flw_renderSeconds; + + uint _flw_debugMode; + uint _flw_debugOverlay; }; #define flw_cameraPos _flw_cameraPos.xyz