From 2638e79aa2f6133c56d67193524dfd3336745e60 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 18 Jan 2022 13:44:28 -0800 Subject: [PATCH 1/7] Add accessor to BlockEntityRenderer map. --- .../BlockEntityRenderDispatcherAccessor.java | 16 ++++++++++++++++ .../java/com/jozufozu/flywheel/util/FlwUtil.java | 16 ++++++++++++++++ src/main/resources/flywheel.mixins.json | 1 + 3 files changed, 33 insertions(+) create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/BlockEntityRenderDispatcherAccessor.java diff --git a/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityRenderDispatcherAccessor.java b/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityRenderDispatcherAccessor.java new file mode 100644 index 000000000..7f7830218 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/BlockEntityRenderDispatcherAccessor.java @@ -0,0 +1,16 @@ +package com.jozufozu.flywheel.mixin; + +import java.util.Map; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +import net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.world.level.block.entity.BlockEntityType; + +@Mixin(BlockEntityRenderDispatcher.class) +public interface BlockEntityRenderDispatcherAccessor { + @Accessor("renderers") + Map, BlockEntityRenderer> flywheel$getRenderers(); +} diff --git a/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java b/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java index bcb2177d6..1d23634f6 100644 --- a/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java +++ b/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java @@ -1,9 +1,25 @@ package com.jozufozu.flywheel.util; import java.util.Arrays; +import java.util.Map; + +import com.jozufozu.flywheel.mixin.BlockEntityRenderDispatcherAccessor; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.blockentity.BlockEntityRenderer; +import net.minecraft.world.level.block.entity.BlockEntityType; public class FlwUtil { + /** + * Get the (effectively global) map of BlockEntityTypes to Renderers. + * @return An immutable map of BlockEntityTypes to BlockEntityRenderers. + */ + public static Map, BlockEntityRenderer> getBlockEntityRenderers() { + Minecraft mc = Minecraft.getInstance(); + return ((BlockEntityRenderDispatcherAccessor) mc.getBlockEntityRenderDispatcher()).flywheel$getRenderers(); + } + public static String repeatChar(char c, int n) { char[] arr = new char[n]; diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index bbf0648db..19f34505b 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -5,6 +5,7 @@ "compatibilityLevel": "JAVA_17", "refmap": "flywheel.refmap.json", "client": [ + "BlockEntityRenderDispatcherAccessor", "BlockEntityTypeMixin", "BufferBuilderMixin", "BufferUploaderMixin", From c490d15876c6eb414f70037dbab0afb54ad912d2 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 24 Jan 2022 10:46:17 -0800 Subject: [PATCH 2/7] Initial move to client commands. - Nothing fancy, just inlining packets. --- gradle.properties | 4 +- .../java/com/jozufozu/flywheel/Flywheel.java | 2 - .../flywheel/config/BooleanConfig.java | 26 ------------ .../flywheel/config/BooleanConfigCommand.java | 14 ++----- .../flywheel/config/BooleanDirective.java | 16 -------- .../flywheel/config/EngineArgument.java | 8 +--- .../jozufozu/flywheel/config/FlwCommands.java | 23 ++++------- .../jozufozu/flywheel/config/FlwPackets.java | 36 ---------------- .../config/SConfigureBooleanPacket.java | 41 ------------------- .../config/SConfigureEnginePacket.java | 36 ---------------- src/main/resources/META-INF/mods.toml | 8 ++-- 11 files changed, 18 insertions(+), 196 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/config/FlwPackets.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java delete mode 100644 src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java diff --git a/gradle.properties b/gradle.properties index 4082064d5..0476c1c99 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.daemon = false mod_version = 0.6.0 mc_update_version = 1.18 minecraft_version = 1.18.1 -forge_version = 39.0.8 +forge_version = 39.0.59 # build dependency versions forgegradle_version = 5.1.+ @@ -13,7 +13,7 @@ mixingradle_version = 0.7-SNAPSHOT mixin_version = 0.8.5 librarian_version = 1.+ cursegradle_version = 1.4.0 -parchment_version = 2021.12.19 +parchment_version = 2022.01.23 # curseforge info projectId = 486392 diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 6214f67f5..71c9394c6 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -6,7 +6,6 @@ import org.apache.logging.log4j.Logger; import com.jozufozu.flywheel.config.EngineArgument; import com.jozufozu.flywheel.config.FlwCommands; import com.jozufozu.flywheel.config.FlwConfig; -import com.jozufozu.flywheel.config.FlwPackets; import net.minecraft.commands.synchronization.ArgumentTypes; import net.minecraft.commands.synchronization.EmptyArgumentSerializer; @@ -41,7 +40,6 @@ public class Flywheel { } private void setup(final FMLCommonSetupEvent event) { - FlwPackets.registerPackets(); ArgumentTypes.register(rl("engine").toString(), EngineArgument.class, new EmptyArgumentSerializer<>(EngineArgument::getInstance)); } } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java index ae33c75f3..4014fe5bd 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfig.java @@ -6,7 +6,6 @@ import java.util.function.Supplier; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; -import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.TextComponent; @@ -23,31 +22,6 @@ public enum BooleanConfig { this.receiver = receiver; } - public SConfigureBooleanPacket packet(BooleanDirective directive) { - return new SConfigureBooleanPacket(this, directive); - } - - /** - * Encode a variant of BooleanConfig. Symmetrical function to {@link #decode} - */ - public void encode(FriendlyByteBuf buffer) { - buffer.writeByte(this.ordinal()); - } - - /** - * Safely decode a variant of BooleanConfig. Symmetrical function to {@link #encode} - */ - public static BooleanConfig decode(FriendlyByteBuf buffer) { - byte t = buffer.readByte(); - BooleanConfig[] values = values(); - // Protects against version differences. - // Shouldn't ever happen but do a sanity check for safety. - if (t >= 0 && t < values.length) - return values[t]; - else - return null; - } - @OnlyIn(Dist.CLIENT) private static void normalOverlay(BooleanDirective state) { LocalPlayer player = Minecraft.getInstance().player; diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java b/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java index b6c0d53ad..9c690af30 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanConfigCommand.java @@ -5,8 +5,6 @@ import com.mojang.brigadier.builder.ArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; -import net.minecraft.server.level.ServerPlayer; -import net.minecraftforge.network.PacketDistributor; public class BooleanConfigCommand { @@ -22,23 +20,17 @@ public class BooleanConfigCommand { public ArgumentBuilder register() { return Commands.literal(name) .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.DISPLAY)); + value.receiver.get().accept(BooleanDirective.DISPLAY); return Command.SINGLE_SUCCESS; }) .then(Commands.literal("on") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.TRUE)); + value.receiver.get().accept(BooleanDirective.TRUE); return Command.SINGLE_SUCCESS; })) .then(Commands.literal("off") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureBooleanPacket(value, BooleanDirective.FALSE)); + value.receiver.get().accept(BooleanDirective.FALSE); return Command.SINGLE_SUCCESS; })); } diff --git a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java index 86dabad8f..1d6892478 100644 --- a/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java +++ b/src/main/java/com/jozufozu/flywheel/config/BooleanDirective.java @@ -1,7 +1,5 @@ package com.jozufozu.flywheel.config; -import net.minecraft.network.FriendlyByteBuf; - public enum BooleanDirective { TRUE(true), FALSE(false), @@ -21,18 +19,4 @@ public enum BooleanDirective { if (this == DISPLAY) throw new IllegalStateException("DISPLAY directive has no value"); return b; } - - /** - * Encode a variant of BooleanDirective. Symmetrical function to {@link #decode} - */ - public void encode(FriendlyByteBuf buffer) { - buffer.writeByte(this.ordinal()); - } - - /** - * Safely decode a variant of BooleanDirective. Symmetrical function to {@link #encode} - */ - public static BooleanDirective decode(FriendlyByteBuf buffer) { - return values()[buffer.readByte()]; - } } diff --git a/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java b/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java index 90588554f..497a60870 100644 --- a/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java +++ b/src/main/java/com/jozufozu/flywheel/config/EngineArgument.java @@ -14,18 +14,14 @@ import com.mojang.brigadier.suggestion.SuggestionsBuilder; import net.minecraft.commands.SharedSuggestionProvider; import net.minecraft.network.chat.TranslatableComponent; -public class EngineArgument implements ArgumentType { - - private static final EngineArgument INSTANCE = new EngineArgument(); +public enum EngineArgument implements ArgumentType { + INSTANCE; private static final Dynamic2CommandExceptionType INVALID = new Dynamic2CommandExceptionType((found, constants) -> { // TODO: don't steal lang return new TranslatableComponent("commands.forge.arguments.enum.invalid", constants, found); }); - private EngineArgument() { - } - @Override public FlwEngine parse(StringReader reader) throws CommandSyntaxException { String string = reader.readUnquotedString(); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java index 6778d9996..5562d535a 100644 --- a/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java +++ b/src/main/java/com/jozufozu/flywheel/config/FlwCommands.java @@ -6,17 +6,11 @@ import com.mojang.brigadier.builder.ArgumentBuilder; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; -import net.minecraft.server.level.ServerPlayer; -import net.minecraftforge.event.server.ServerStartingEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.network.PacketDistributor; +import net.minecraftforge.client.event.RegisterClientCommandsEvent; public class FlwCommands { - @SubscribeEvent - public static void onServerStarting(ServerStartingEvent event) { - CommandDispatcher dispatcher = event.getServer() - .getCommands() - .getDispatcher(); + public static void onServerStarting(RegisterClientCommandsEvent event) { + CommandDispatcher dispatcher = event.getDispatcher(); dispatcher.register(Commands.literal("flywheel") .then(debugCommand()) @@ -31,18 +25,15 @@ public class FlwCommands { private static ArgumentBuilder backendCommand() { return Commands.literal("backend") .executes(context -> { - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket()); + FlwEngine.handle(null); + return Command.SINGLE_SUCCESS; }) - .then(Commands.argument("type", EngineArgument.getInstance()) + .then(Commands.argument("type", EngineArgument.INSTANCE) .executes(context -> { FlwEngine type = context.getArgument("type", FlwEngine.class); - ServerPlayer player = context.getSource() - .getPlayerOrException(); - FlwPackets.channel.send(PacketDistributor.PLAYER.with(() -> player), new SConfigureEnginePacket(type)); + FlwEngine.handle(type); return Command.SINGLE_SUCCESS; })); diff --git a/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java b/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java deleted file mode 100644 index c3fa59234..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/FlwPackets.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jozufozu.flywheel.config; - -import com.jozufozu.flywheel.Flywheel; - -import net.minecraft.resources.ResourceLocation; -import net.minecraftforge.network.NetworkDirection; -import net.minecraftforge.network.NetworkRegistry; -import net.minecraftforge.network.simple.SimpleChannel; - -public class FlwPackets { - public static final ResourceLocation CHANNEL_NAME = Flywheel.rl("main"); - public static final String NETWORK_VERSION = String.valueOf(1); - public static SimpleChannel channel; - - public static void registerPackets() { - channel = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME) - .serverAcceptedVersions(NETWORK_VERSION::equals) - .clientAcceptedVersions(NETWORK_VERSION::equals) - .networkProtocolVersion(() -> NETWORK_VERSION) - .simpleChannel(); - - int id = 0; - - channel.messageBuilder(SConfigureBooleanPacket.class, id++, NetworkDirection.PLAY_TO_CLIENT) - .decoder(SConfigureBooleanPacket::new) - .encoder(SConfigureBooleanPacket::encode) - .consumer(SConfigureBooleanPacket::execute) - .add(); - - channel.messageBuilder(SConfigureEnginePacket.class, id++, NetworkDirection.PLAY_TO_CLIENT) - .decoder(SConfigureEnginePacket::new) - .encoder(SConfigureEnginePacket::encode) - .consumer(SConfigureEnginePacket::execute) - .add(); - } -} diff --git a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java b/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java deleted file mode 100644 index 19853d9db..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/SConfigureBooleanPacket.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.jozufozu.flywheel.config; - - -import java.util.function.Supplier; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - -/** - * Thanks, @zelophed - */ -public class SConfigureBooleanPacket { - - private final BooleanConfig target; - private final BooleanDirective directive; - - public SConfigureBooleanPacket(BooleanConfig target, BooleanDirective directive) { - this.target = target; - this.directive = directive; - } - - public SConfigureBooleanPacket(FriendlyByteBuf buffer) { - target = BooleanConfig.decode(buffer); - directive = BooleanDirective.decode(buffer); - } - - public void encode(FriendlyByteBuf buffer) { - target.encode(buffer); - directive.encode(buffer); - } - - public void execute(Supplier ctx) { - if (directive != null) { - target.receiver.get() - .accept(directive); - } - ctx.get() - .setPacketHandled(true); - } - -} diff --git a/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java b/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java deleted file mode 100644 index d287d7aca..000000000 --- a/src/main/java/com/jozufozu/flywheel/config/SConfigureEnginePacket.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.jozufozu.flywheel.config; - -import java.util.function.Supplier; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraftforge.network.NetworkEvent; - -public class SConfigureEnginePacket { - - private final FlwEngine type; - - public SConfigureEnginePacket() { - type = null; - } - - public SConfigureEnginePacket(FlwEngine type) { - this.type = type; - } - - public SConfigureEnginePacket(FriendlyByteBuf buffer) { - type = FlwEngine.decode(buffer); - } - - public void encode(FriendlyByteBuf buffer) { - if (type != null) - type.encode(buffer); - else - buffer.writeByte(-1); - } - - public void execute(Supplier ctx) { - FlwEngine.handle(type); - ctx.get() - .setPacketHandled(true); - } -} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 5e2744c9f..3ac2d980c 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -17,13 +17,13 @@ A modern engine for modded minecraft. [[dependencies.flywheel]] modId = "forge" mandatory = true -versionRange = "[38,)" +versionRange = "[39.0.46,)" ordering = "NONE" -side = "BOTH" +side = "CLIENT" [[dependencies.flywheel]] modId = "minecraft" mandatory = true -versionRange = "[1.18,1.19)" +versionRange = "[1.18.1,1.19)" ordering = "NONE" -side = "BOTH" +side = "CLIENT" From 5a2de8095cc4a4e7fd4962f6319a1ae801483e29 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 25 Jan 2022 10:12:08 -0800 Subject: [PATCH 3/7] Update changelog and document Translate#nudge --- changelog.txt | 12 ++++++++++++ .../jozufozu/flywheel/util/transform/Translate.java | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/changelog.txt b/changelog.txt index ccd0e4240..981b3af55 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,15 @@ +0.6.0: +With this release, Flywheel is no longer needed on servers! Forge finally has client commands, +and the /flywheel command now takes advantage of this. +Fixes + - Fix crash when running with sodium/magnesium. +Technical/API + - Backend is now a static class. + - Shaders are now compiled on-the-fly and cached. + - Significantly reduced the amount of boilerplate needed in instancing shaders. + - Struct types no longer need to be registered ahead of time. + - Simplify unnecessarily complicated game state system. + 0.5.1: Fixes - Fix crash on resource reload with backend off diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/Translate.java b/src/main/java/com/jozufozu/flywheel/util/transform/Translate.java index 2e3da5a32..df6b91abd 100644 --- a/src/main/java/com/jozufozu/flywheel/util/transform/Translate.java +++ b/src/main/java/com/jozufozu/flywheel/util/transform/Translate.java @@ -56,8 +56,13 @@ public interface Translate { return translate(-vec.getX(), -vec.getY(), -vec.getZ()); } - default Self nudge(int id) { - long randomBits = (long) id * 31L * 493286711L; + /** + * Translates this object randomly by a very small amount. + * @param seed The seed to use to generate the random offsets. + * @return {@code this} + */ + default Self nudge(int seed) { + long randomBits = (long) seed * 31L * 493286711L; randomBits = randomBits * randomBits * 4392167121L + randomBits * 98761L; float xNudge = (((float) (randomBits >> 16 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F; float yNudge = (((float) (randomBits >> 20 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F; From 421e11ebc11f8abd7ec7c35342be5e98343ffa29 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 25 Jan 2022 10:13:24 -0800 Subject: [PATCH 4/7] Create FUNDING.yml --- .github/FUNDING.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..79643e968 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,13 @@ +# These are supported funding model platforms + +github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] +patreon: Jozufozu +open_collective: # Replace with a single Open Collective username +ko_fi: Jozufozu +tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel +community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry +liberapay: # Replace with a single Liberapay username +issuehunt: # Replace with a single IssueHunt username +otechie: # Replace with a single Otechie username +lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry +custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] From 84432fb83733d56c8c52e44cc1b237cebf2f4345 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Tue, 25 Jan 2022 18:01:36 -0800 Subject: [PATCH 5/7] Add 0.6.0 to bug report template --- .github/ISSUE_TEMPLATE/bug_report.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 2fc8897dc..099e65cce 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -59,6 +59,7 @@ body: label: Mod Version description: The version of the mod you were using when the bug occured options: + - "0.6.0" - "0.5.1" - "0.5.0a" - "0.5.0" From 24423c56a81c49f38f78dd28acd8b72761fa3b6f Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 31 Jan 2022 13:13:27 -0800 Subject: [PATCH 6/7] Add more debug info - Flywheel version - Vertex/Instance count - Origin coordinate for Instancing Engine --- .../java/com/jozufozu/flywheel/Flywheel.java | 11 +++++++++ .../com/jozufozu/flywheel/FlywheelClient.java | 2 +- .../jozufozu/flywheel/backend/Backend.java | 8 ------- .../backend/instancing/AbstractInstancer.java | 6 ++--- .../flywheel/backend/instancing/Engine.java | 3 +++ .../backend/instancing/InstanceManager.java | 9 +++++++ .../backend/instancing/InstanceWorld.java | 12 +++++++++- .../instancing/InstancedRenderDispatcher.java | 6 +++++ .../batching/BatchedMaterialGroup.java | 24 +++++++++++++++++-- .../instancing/batching/BatchingEngine.java | 17 ++++++++++++- .../instancing/batching/CPUInstancer.java | 2 +- .../instancing/InstancedMaterial.java | 8 +++++++ .../instancing/InstancedMaterialGroup.java | 24 +++++++++++++++++++ .../instancing/InstancingEngine.java | 13 ++++++++-- .../jozufozu/flywheel/event/ForgeEvents.java | 14 ++--------- .../com/jozufozu/flywheel/util/FlwUtil.java | 5 ++++ 16 files changed, 133 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/Flywheel.java b/src/main/java/com/jozufozu/flywheel/Flywheel.java index 71c9394c6..95effd761 100644 --- a/src/main/java/com/jozufozu/flywheel/Flywheel.java +++ b/src/main/java/com/jozufozu/flywheel/Flywheel.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.apache.maven.artifact.versioning.ArtifactVersion; import com.jozufozu.flywheel.config.EngineArgument; import com.jozufozu.flywheel.config.FlwCommands; @@ -13,17 +14,27 @@ import net.minecraft.resources.ResourceLocation; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.DistExecutor; +import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.forgespi.language.IModFileInfo; @Mod(Flywheel.ID) public class Flywheel { public static final String ID = "flywheel"; public static final Logger LOGGER = LogManager.getLogger(Flywheel.class); + public static ArtifactVersion VERSION; public Flywheel() { + IModFileInfo modFileById = ModList.get() + .getModFileById(ID); + + VERSION = modFileById.getMods() + .get(0) + .getVersion(); + FMLJavaModLoadingContext.get() .getModEventBus() .addListener(this::setup); diff --git a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java index 4b74ab5ce..2c4b342e0 100644 --- a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java +++ b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java @@ -18,7 +18,7 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; public class FlywheelClient { public static void clientInit() { - CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor); + CrashReportCallables.registerCrashCallable("Flywheel Backend", () -> Backend.getEngine().getProperName()); OptifineHandler.init(); Backend.init(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index b8cd61e82..715d3a5ce 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -25,14 +25,6 @@ public class Backend { private static final Loader loader = new Loader(); - /** - * Get a string describing the Flywheel backend. When there are eventually multiple backends - * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use. - */ - public static String getBackendDescriptor() { - return engine == null ? "" : engine.getProperName(); - } - public static FlwEngine getEngine() { return engine; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java index e353a7104..872335853 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/AbstractInstancer.java @@ -62,12 +62,12 @@ public abstract class AbstractInstancer implements Insta return modelData.vertexCount(); } - public int numInstances() { + public int getInstanceCount() { return data.size(); } - public int getTotalVertexCount() { - return getModelVertexCount() * numInstances(); + public int getVertexCount() { + return getModelVertexCount() * getInstanceCount(); } protected void removeDeletedInstances() { diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/Engine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/Engine.java index 1e33c16db..51cc0a93e 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/Engine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/Engine.java @@ -1,6 +1,9 @@ package com.jozufozu.flywheel.backend.instancing; +import java.util.List; + import com.jozufozu.flywheel.api.MaterialManager; public interface Engine extends RenderDispatcher, MaterialManager { + void addDebugInfo(List info); } 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 8c79fe8e9..38936f656 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -46,6 +46,15 @@ public abstract class InstanceManager implements InstancingEngine.OriginShift this.tickableInstances = new Object2ObjectOpenHashMap<>(); } + /** + * Get the number of game objects that are currently being instanced. + * + * @return The object count. + */ + public int getObjectCount() { + return instances.size(); + } + /** * Is the given object capable of being instanced at all? * diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java index 387c1c1c4..9595069eb 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceWorld.java @@ -1,5 +1,8 @@ package com.jozufozu.flywheel.backend.instancing; +import java.util.List; + +import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.api.instance.DynamicInstance; import com.jozufozu.flywheel.api.instance.TickableInstance; import com.jozufozu.flywheel.backend.Backend; @@ -74,7 +77,7 @@ public class InstanceWorld { * Free all acquired resources and invalidate this instance world. */ public void delete() { - this.taskEngine.stopWorkers(); + taskEngine.stopWorkers(); engine.delete(); entityInstanceManager.detachLightListeners(); blockEntityInstanceManager.detachLightListeners(); @@ -133,4 +136,11 @@ public class InstanceWorld { world.entitiesForRendering() .forEach(entityInstanceManager::add); } + + public void getDebugString(List debug) { + debug.add(""); + debug.add("Flywheel: " + Flywheel.VERSION); + debug.add("B: " + blockEntityInstanceManager.getObjectCount() + ", E: " + entityInstanceManager.getObjectCount()); + engine.addDebugInfo(debug); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index bfc580517..f434a4871 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -1,5 +1,7 @@ package com.jozufozu.flywheel.backend.instancing; +import java.util.List; + import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; @@ -113,4 +115,8 @@ public class InstancedRenderDispatcher { .loadEntities(world); } + public static void getDebugString(List debug) { + instanceWorlds.get(Minecraft.getInstance().level) + .getDebugString(debug); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java index 032a03457..fc6f242b7 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchedMaterialGroup.java @@ -20,6 +20,8 @@ public class BatchedMaterialGroup implements MaterialGroup { protected final RenderType state; private final Map, BatchedMaterial> materials = new HashMap<>(); + private int vertexCount; + private int instanceCount; public BatchedMaterialGroup(RenderType state) { this.state = state; @@ -37,11 +39,13 @@ public class BatchedMaterialGroup implements MaterialGroup { public void render(PoseStack stack, BatchDrawingTracker source, TaskEngine pool) { - int vertexCount = 0; + vertexCount = 0; + instanceCount = 0; for (BatchedMaterial material : materials.values()) { for (CPUInstancer instancer : material.models.values()) { instancer.setup(); - vertexCount += instancer.getTotalVertexCount(); + vertexCount += instancer.getVertexCount(); + instanceCount += instancer.getInstanceCount(); } } @@ -65,4 +69,20 @@ public class BatchedMaterialGroup implements MaterialGroup { public void delete() { materials.clear(); } + + /** + * Get the number of instances drawn last frame. + * @return The instance count. + */ + public int getInstanceCount() { + return instanceCount; + } + + /** + * Get the number of vertices drawn last frame. + * @return The vertex count. + */ + public int getVertexCount() { + return vertexCount; + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java index 39ea2343b..aa6e7f683 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/BatchingEngine.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.instancing.batching; import java.util.EnumMap; import java.util.HashMap; +import java.util.List; import java.util.Map; import com.jozufozu.flywheel.api.MaterialGroup; @@ -10,6 +11,7 @@ import com.jozufozu.flywheel.backend.instancing.BatchDrawingTracker; import com.jozufozu.flywheel.backend.instancing.Engine; import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.event.RenderLayerEvent; +import com.jozufozu.flywheel.util.FlwUtil; import com.mojang.blaze3d.platform.Lighting; import com.mojang.math.Matrix4f; @@ -28,7 +30,6 @@ public class BatchingEngine implements Engine { for (RenderLayer value : RenderLayer.values()) { layers.put(value, new HashMap<>()); } - } @Override @@ -70,4 +71,18 @@ public class BatchingEngine implements Engine { } + @Override + public void addDebugInfo(List info) { + info.add("Batching"); + info.add("Instances: " + layers.values() + .stream() + .flatMap(FlwUtil::mapValues) + .mapToInt(BatchedMaterialGroup::getInstanceCount) + .sum()); + info.add("Vertices: " + layers.values() + .stream() + .flatMap(FlwUtil::mapValues) + .mapToInt(BatchedMaterialGroup::getVertexCount) + .sum()); + } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java index d16f4a4d2..828827103 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java @@ -24,7 +24,7 @@ public class CPUInstancer extends AbstractInstancer { } void submitTasks(PoseStack stack, TaskEngine pool, DirectVertexConsumer consumer) { - int instances = numInstances(); + int instances = getInstanceCount(); while (instances > 0) { int end = instances; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java index becb71114..e22b57ce4 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterial.java @@ -46,6 +46,14 @@ public class InstancedMaterial implements Material { }); } + public int getInstanceCount() { + return models.values().stream().mapToInt(GPUInstancer::getInstanceCount).sum(); + } + + public int getVertexCount() { + return models.values().stream().mapToInt(GPUInstancer::getVertexCount).sum(); + } + public boolean nothingToRender() { return models.size() > 0 && models.values() .stream() diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java index 3e1ba850d..6c640f716 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancedMaterialGroup.java @@ -34,6 +34,9 @@ public class InstancedMaterialGroup

implements MaterialG private final Map, InstancedMaterial> materials = new HashMap<>(); private final ModelAllocator allocator; + private int vertexCount; + private int instanceCount; + public InstancedMaterialGroup(InstancingEngine

owner, RenderType type) { this.owner = owner; this.type = type; @@ -54,6 +57,22 @@ public class InstancedMaterialGroup

implements MaterialG } } + /** + * Get the number of instances drawn last frame. + * @return The instance count. + */ + public int getInstanceCount() { + return instanceCount; + } + + /** + * Get the number of vertices drawn last frame. + * @return The vertex count. + */ + public int getVertexCount() { + return vertexCount; + } + public void render(Matrix4f viewProjection, double camX, double camY, double camZ, RenderLayer layer) { type.setupRenderState(); Textures.bindActiveTextures(); @@ -75,6 +94,9 @@ public class InstancedMaterialGroup

implements MaterialG pool.flush(); } + vertexCount = 0; + instanceCount = 0; + for (Map.Entry, InstancedMaterial> entry : materials.entrySet()) { InstancedMaterial material = entry.getValue(); if (material.nothingToRender()) continue; @@ -90,6 +112,8 @@ public class InstancedMaterialGroup

implements MaterialG for (GPUInstancer instancer : material.getAllInstancers()) { instancer.render(); + vertexCount += instancer.getVertexCount(); + instanceCount += instancer.getInstanceCount(); } } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java index 651be0865..42cca0e13 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/instancing/InstancingEngine.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.backend.instancing.instancing; import java.util.EnumMap; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.stream.Stream; @@ -14,6 +15,7 @@ import com.jozufozu.flywheel.backend.instancing.TaskEngine; import com.jozufozu.flywheel.core.compile.ProgramCompiler; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.RenderLayerEvent; +import com.jozufozu.flywheel.util.FlwUtil; import com.jozufozu.flywheel.util.WeakHashSet; import com.mojang.math.Matrix4f; @@ -101,8 +103,7 @@ public class InstancingEngine

implements Engine { } else { return layers.values() .stream() - .flatMap(it -> it.values() - .stream()); + .flatMap(FlwUtil::mapValues); } } @@ -150,6 +151,14 @@ public class InstancingEngine

implements Engine { } } + @Override + public void addDebugInfo(List info) { + info.add("GL33 Instanced Arrays"); + info.add("Origin: " + originCoordinate.getX() + ", " + originCoordinate.getY() + ", " + originCoordinate.getZ()); + info.add("Instances: " + getGroupsToRender(null).mapToInt(InstancedMaterialGroup::getInstanceCount).sum()); + info.add("Vertices: " + getGroupsToRender(null).mapToInt(InstancedMaterialGroup::getVertexCount).sum()); + } + @FunctionalInterface public interface OriginShiftListener { void onOriginShift(); diff --git a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java index 9ca6cd351..64aec0c2a 100644 --- a/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java +++ b/src/main/java/com/jozufozu/flywheel/event/ForgeEvents.java @@ -1,8 +1,7 @@ package com.jozufozu.flywheel.event; -import java.util.ArrayList; - import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.light.LightUpdater; import com.jozufozu.flywheel.util.WorldAttached; @@ -22,16 +21,7 @@ public class ForgeEvents { if (Minecraft.getInstance().options.renderDebug) { - ArrayList right = event.getRight(); - - String text = "Flywheel: " + Backend.getBackendDescriptor(); - if (right.size() < 10) { - right.add(""); - right.add(text); - } else { - right.add(9, ""); - right.add(10, text); - } + InstancedRenderDispatcher.getDebugString(event.getLeft()); } } diff --git a/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java b/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java index 1d23634f6..beed0590a 100644 --- a/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java +++ b/src/main/java/com/jozufozu/flywheel/util/FlwUtil.java @@ -2,6 +2,7 @@ package com.jozufozu.flywheel.util; import java.util.Arrays; import java.util.Map; +import java.util.stream.Stream; import com.jozufozu.flywheel.mixin.BlockEntityRenderDispatcherAccessor; @@ -68,4 +69,8 @@ public class FlwUtil { } } } + + public static Stream mapValues(Map map) { + return map.values().stream(); + } } From a1791399b680d32ab56dd53d1a8bdf358e8df1ae Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 31 Jan 2022 13:26:55 -0800 Subject: [PATCH 7/7] Un-remove getBackendDescriptor - No point in incorrectly inlining it - One useage was replaced by InstancedRenderDispatcher#getDebugString --- src/main/java/com/jozufozu/flywheel/FlywheelClient.java | 2 +- src/main/java/com/jozufozu/flywheel/backend/Backend.java | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java index 2c4b342e0..4b74ab5ce 100644 --- a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java +++ b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java @@ -18,7 +18,7 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; public class FlywheelClient { public static void clientInit() { - CrashReportCallables.registerCrashCallable("Flywheel Backend", () -> Backend.getEngine().getProperName()); + CrashReportCallables.registerCrashCallable("Flywheel Backend", Backend::getBackendDescriptor); OptifineHandler.init(); Backend.init(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/Backend.java b/src/main/java/com/jozufozu/flywheel/backend/Backend.java index 715d3a5ce..6988f3239 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/Backend.java +++ b/src/main/java/com/jozufozu/flywheel/backend/Backend.java @@ -29,6 +29,14 @@ public class Backend { return engine; } + /** + * Get a string describing the Flywheel backend. When there are eventually multiple backends + * (Meshlet, MDI, GL31 Draw Instanced are planned), this will name which one is in use. + */ + public static String getBackendDescriptor() { + return engine == null ? "" : engine.getProperName(); + } + @Nullable public static ProgramSpec getSpec(ResourceLocation name) { return loader.get(name);